gocui/edit.go

78 lines
2.2 KiB
Go
Raw Normal View History

// Copyright 2014 The gocui Authors. All rights reserved.
2014-01-22 23:44:42 +01:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2014-01-22 23:44:08 +01:00
package gocui
2016-11-05 11:41:43 +01:00
import (
2021-04-02 15:07:18 +11:00
"unicode"
2016-11-05 11:41:43 +01:00
)
// Editor interface must be satisfied by gocui editors.
type Editor interface {
Edit(v *View, key Key, ch rune, mod Modifier) bool
}
// 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.
type EditorFunc func(v *View, key Key, ch rune, mod Modifier) bool
// Edit calls f(v, key, ch, mod)
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
return f(v, key, ch, mod)
}
// DefaultEditor is the default editor.
2022-08-15 19:57:08 +10:00
var DefaultEditor Editor = EditorFunc(SimpleEditor)
2022-08-15 19:57:08 +10:00
// SimpleEditor is used as the default gocui editor.
func SimpleEditor(v *View, key Key, ch rune, mod Modifier) bool {
2015-02-13 18:40:45 +01:00
switch {
2015-02-24 00:26:26 +01:00
case key == KeyBackspace || key == KeyBackspace2:
2021-10-17 14:52:23 +11:00
v.TextArea.BackSpaceChar()
2021-02-18 19:38:41 +11:00
case key == KeyCtrlD || key == KeyDelete:
2021-10-17 14:52:23 +11:00
v.TextArea.DeleteChar()
2015-02-24 00:26:26 +01:00
case key == KeyArrowDown:
2021-10-17 14:52:23 +11:00
v.TextArea.MoveCursorDown()
2015-02-24 00:26:26 +01:00
case key == KeyArrowUp:
2021-10-17 14:52:23 +11:00
v.TextArea.MoveCursorUp()
case key == KeyArrowLeft && (mod&ModAlt) != 0:
v.TextArea.MoveLeftWord()
2015-02-24 00:26:26 +01:00
case key == KeyArrowLeft:
2021-10-17 14:52:23 +11:00
v.TextArea.MoveCursorLeft()
case key == KeyArrowRight && (mod&ModAlt) != 0:
v.TextArea.MoveRightWord()
2015-02-24 00:26:26 +01:00
case key == KeyArrowRight:
2021-10-17 14:52:23 +11:00
v.TextArea.MoveCursorRight()
2021-04-02 15:07:18 +11:00
case key == KeyEnter:
2021-10-17 14:52:23 +11:00
v.TextArea.TypeRune('\n')
case key == KeySpace:
v.TextArea.TypeRune(' ')
case key == KeyInsert:
v.TextArea.ToggleOverwrite()
case key == KeyCtrlU:
2021-10-17 14:52:23 +11:00
v.TextArea.DeleteToStartOfLine()
2022-03-28 22:51:17 +09:00
case key == KeyCtrlK:
v.TextArea.DeleteToEndOfLine()
2021-10-17 20:10:15 +11:00
case key == KeyCtrlA || key == KeyHome:
2021-10-17 14:52:23 +11:00
v.TextArea.GoToStartOfLine()
2021-10-17 20:10:15 +11:00
case key == KeyCtrlE || key == KeyEnd:
2021-10-17 14:52:23 +11:00
v.TextArea.GoToEndOfLine()
case key == KeyCtrlW:
v.TextArea.BackSpaceWord()
2022-03-28 22:22:09 +09:00
case key == KeyCtrlY:
v.TextArea.Yank()
2021-04-02 15:07:18 +11:00
2021-10-17 14:52:23 +11:00
// TODO: see if we need all three of these conditions: maybe the final one is sufficient
2021-04-02 15:07:18 +11:00
case ch != 0 && mod == 0 && unicode.IsPrint(ch):
2021-10-17 14:52:23 +11:00
v.TextArea.TypeRune(ch)
default:
2021-10-17 14:52:23 +11:00
return false
}
2021-10-17 14:52:23 +11:00
v.RenderTextArea()
2021-10-17 14:52:23 +11:00
return true
}