2018-08-06 00:38:38 +10:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gocui
|
|
|
|
|
2018-08-28 19:58:18 +10:00
|
|
|
import (
|
2021-04-02 15:08:15 +11:00
|
|
|
"unicode"
|
2018-08-28 19:58:18 +10:00
|
|
|
)
|
2018-08-06 00:38:38 +10:00
|
|
|
|
|
|
|
// Editor interface must be satisfied by gocui editors.
|
|
|
|
type Editor interface {
|
2021-02-18 19:39:33 +11:00
|
|
|
Edit(v *View, key Key, ch rune, mod Modifier) bool
|
2018-08-06 00:38:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// The EditorFunc type is an adapter to allow the use of ordinary functions as
|
|
|
|
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
|
|
|
|
// is an Editor object that calls f.
|
2021-02-18 19:39:33 +11:00
|
|
|
type EditorFunc func(v *View, key Key, ch rune, mod Modifier) bool
|
2018-08-06 00:38:38 +10:00
|
|
|
|
|
|
|
// Edit calls f(v, key, ch, mod)
|
2021-02-18 19:39:33 +11:00
|
|
|
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
|
|
|
|
return f(v, key, ch, mod)
|
2018-08-06 00:38:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultEditor is the default editor.
|
2022-08-14 14:33:44 +10:00
|
|
|
var DefaultEditor Editor = EditorFunc(SimpleEditor)
|
2018-08-06 00:38:38 +10:00
|
|
|
|
2022-08-14 14:33:44 +10:00
|
|
|
// SimpleEditor is used as the default gocui editor.
|
|
|
|
func SimpleEditor(v *View, key Key, ch rune, mod Modifier) bool {
|
2018-08-06 00:38:38 +10:00
|
|
|
switch {
|
|
|
|
case key == KeyBackspace || key == KeyBackspace2:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.BackSpaceChar()
|
2021-02-18 19:39:33 +11:00
|
|
|
case key == KeyCtrlD || key == KeyDelete:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.DeleteChar()
|
2018-08-06 00:38:38 +10:00
|
|
|
case key == KeyArrowDown:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.MoveCursorDown()
|
2018-08-06 00:38:38 +10:00
|
|
|
case key == KeyArrowUp:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.MoveCursorUp()
|
2022-10-02 12:47:57 -07:00
|
|
|
case key == KeyArrowLeft && (mod&ModAlt) != 0:
|
|
|
|
v.TextArea.MoveLeftWord()
|
2018-08-06 00:38:38 +10:00
|
|
|
case key == KeyArrowLeft:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.MoveCursorLeft()
|
2022-10-02 12:47:57 -07:00
|
|
|
case key == KeyArrowRight && (mod&ModAlt) != 0:
|
|
|
|
v.TextArea.MoveRightWord()
|
2018-08-06 00:38:38 +10:00
|
|
|
case key == KeyArrowRight:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.MoveCursorRight()
|
2021-04-02 15:08:15 +11:00
|
|
|
case key == KeyEnter:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.TypeRune('\n')
|
|
|
|
case key == KeySpace:
|
|
|
|
v.TextArea.TypeRune(' ')
|
|
|
|
case key == KeyInsert:
|
|
|
|
v.TextArea.ToggleOverwrite()
|
2019-05-26 12:27:59 +10:00
|
|
|
case key == KeyCtrlU:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.DeleteToStartOfLine()
|
2022-09-23 20:01:44 +09:00
|
|
|
case key == KeyCtrlK:
|
|
|
|
v.TextArea.DeleteToEndOfLine()
|
2021-10-17 20:11:16 +11:00
|
|
|
case key == KeyCtrlA || key == KeyHome:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.GoToStartOfLine()
|
2021-10-17 20:11:16 +11:00
|
|
|
case key == KeyCtrlE || key == KeyEnd:
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.GoToEndOfLine()
|
2022-09-23 20:01:44 +09:00
|
|
|
case key == KeyCtrlW:
|
|
|
|
v.TextArea.BackSpaceWord()
|
|
|
|
case key == KeyCtrlY:
|
|
|
|
v.TextArea.Yank()
|
2021-04-02 15:08:15 +11:00
|
|
|
|
2021-10-17 13:00:44 +11:00
|
|
|
// TODO: see if we need all three of these conditions: maybe the final one is sufficient
|
2021-04-02 15:08:15 +11:00
|
|
|
case ch != 0 && mod == 0 && unicode.IsPrint(ch):
|
2021-10-17 13:00:44 +11:00
|
|
|
v.TextArea.TypeRune(ch)
|
2019-05-26 12:27:59 +10:00
|
|
|
default:
|
2021-10-17 13:00:44 +11:00
|
|
|
return false
|
2021-02-09 21:49:03 +11:00
|
|
|
}
|
|
|
|
|
2021-10-17 13:00:44 +11:00
|
|
|
v.RenderTextArea()
|
2018-08-06 00:38:38 +10:00
|
|
|
|
2021-10-17 13:00:44 +11:00
|
|
|
return true
|
2018-08-06 00:38:38 +10:00
|
|
|
}
|