2021-04-02 12:03:33 -04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
2021-04-07 13:10:40 -04:00
|
|
|
"timshome.page/gilo/editor/document"
|
|
|
|
"timshome.page/gilo/gilo"
|
|
|
|
"timshome.page/gilo/key"
|
|
|
|
"timshome.page/gilo/terminal"
|
2021-04-02 12:03:33 -04:00
|
|
|
)
|
|
|
|
|
2021-04-02 15:36:43 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Constants representing input keys
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyUp = "ARROW_UP"
|
|
|
|
keyDown = "ARROW_DOWN"
|
|
|
|
keyLeft = "ARROW_LEFT"
|
|
|
|
keyRight = "ARROW_RIGHT"
|
|
|
|
keyPageUp = "PAGE_UP"
|
|
|
|
keyPageDown = "PAGE_DOWN"
|
|
|
|
keyHome = "HOME"
|
|
|
|
keyEnd = "END"
|
|
|
|
keyDelete = "DELETE"
|
|
|
|
)
|
|
|
|
|
2021-04-02 12:03:33 -04:00
|
|
|
/**
|
|
|
|
* Determine what to do with an individual character of input
|
|
|
|
*/
|
|
|
|
func (e *editor) processKeypressChar(ch rune) bool {
|
|
|
|
switch ch {
|
|
|
|
case key.Ctrl('q'):
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.doc.IsDirty() && e.quitTimes > 0 {
|
2021-04-02 19:24:20 -04:00
|
|
|
e.SetStatusMessage("WARNING!!! File has unsaved changes. Press Ctrl-Q %d more times to quit.", e.quitTimes)
|
2021-04-02 12:03:33 -04:00
|
|
|
e.quitTimes -= 1
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up on exit
|
|
|
|
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
case key.Ctrl('s'):
|
2021-04-06 10:59:24 -04:00
|
|
|
e.save()
|
|
|
|
|
|
|
|
case key.Ctrl('f'):
|
|
|
|
e.find()
|
2021-04-02 12:03:33 -04:00
|
|
|
|
|
|
|
case key.Enter:
|
2021-04-07 16:33:00 -04:00
|
|
|
e.doc.InsertNewline(e.cursor)
|
2021-04-02 18:43:49 -04:00
|
|
|
e.cursor.Y += 1
|
|
|
|
e.cursor.X = 0
|
2021-04-02 12:03:33 -04:00
|
|
|
|
|
|
|
case key.Backspace, key.Ctrl('h'):
|
|
|
|
e.delChar()
|
|
|
|
|
|
|
|
case key.Esc, key.Ctrl('l'):
|
|
|
|
// Modifier keys that return ANSI escape sequences
|
|
|
|
str := parseEscapeSequence()
|
|
|
|
|
|
|
|
// Don't swallow a character after ESC if it doesn't
|
|
|
|
// start an ANSI escape sequence
|
|
|
|
if len(str) == 1 {
|
|
|
|
return e.processKeypressChar(rune(str[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
e.processKeypressStr(str)
|
|
|
|
|
|
|
|
default:
|
|
|
|
e.insertChar(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the quit message and restart the
|
|
|
|
// confirmation count if confirmation is not
|
|
|
|
// completed
|
2021-04-02 15:36:43 -04:00
|
|
|
if e.quitTimes != gilo.QuitTimes {
|
|
|
|
e.quitTimes = gilo.QuitTimes
|
2021-04-02 12:03:33 -04:00
|
|
|
e.SetStatusMessage("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what do do with a parsed ANSI escape sequence
|
|
|
|
*/
|
|
|
|
func (e *editor) processKeypressStr(key string) {
|
|
|
|
switch key {
|
|
|
|
case keyUp,
|
|
|
|
keyDown,
|
|
|
|
keyLeft,
|
|
|
|
keyRight,
|
|
|
|
keyPageUp,
|
|
|
|
keyPageDown,
|
|
|
|
keyHome,
|
|
|
|
keyEnd:
|
|
|
|
|
|
|
|
e.moveCursor(key)
|
|
|
|
|
|
|
|
case keyDelete:
|
|
|
|
e.moveCursor(keyRight)
|
|
|
|
e.delChar()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *editor) moveCursor(key string) {
|
2021-04-02 15:36:43 -04:00
|
|
|
var row *document.Row
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y >= e.doc.RowCount() {
|
2021-04-02 12:03:33 -04:00
|
|
|
row = nil
|
|
|
|
} else {
|
2021-04-07 16:33:00 -04:00
|
|
|
row = e.doc.GetRow(e.cursor.Y)
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case keyLeft:
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.X != 0 {
|
|
|
|
e.cursor.X -= 1
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move from beginning of current row to end of previous row
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y > 0 {
|
|
|
|
e.cursor.Y -= 1
|
2021-04-07 16:33:00 -04:00
|
|
|
e.cursor.X = e.doc.GetRow(e.cursor.Y).Size()
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
case keyRight:
|
2021-04-02 14:52:44 -04:00
|
|
|
if row != nil && e.cursor.X < row.Size() {
|
|
|
|
e.cursor.X += 1
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move from end of current line to beginning of next line
|
2021-04-07 16:33:00 -04:00
|
|
|
if row != nil && e.cursor.X == row.Size() && e.cursor.Y < e.doc.RowCount()-1 {
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.Y += 1
|
|
|
|
e.cursor.X = 0
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
case keyUp:
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y != 0 {
|
|
|
|
e.cursor.Y -= 1
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
case keyDown:
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y < e.doc.RowCount() {
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.Y += 1
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
case keyPageUp:
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.Y > e.screen.Rows {
|
|
|
|
e.cursor.Y -= e.screen.Rows
|
2021-04-02 12:03:33 -04:00
|
|
|
} else {
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.Y = 0
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case keyPageDown:
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y+e.screen.Rows > e.doc.RowCount() {
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.Y += e.screen.Rows
|
2021-04-02 12:03:33 -04:00
|
|
|
} else {
|
2021-04-07 16:33:00 -04:00
|
|
|
e.cursor.Y = e.doc.RowCount() - 1
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case keyHome:
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.X = 0
|
2021-04-02 12:03:33 -04:00
|
|
|
case keyEnd:
|
|
|
|
if row != nil {
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.X = row.Size()
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y < e.doc.RowCount() {
|
|
|
|
row = e.doc.GetRow(e.cursor.Y)
|
2021-04-02 14:52:44 -04:00
|
|
|
rowLen := row.Size()
|
2021-04-02 12:03:33 -04:00
|
|
|
|
|
|
|
// Snap to the end of a shorter line from a longer one
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.X > rowLen {
|
|
|
|
e.cursor.X = rowLen
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the raw ANSI escape sequences to the type of key input
|
|
|
|
func parseEscapeSequence() string {
|
|
|
|
// If we aren't starting an escape sequence,
|
|
|
|
// return the character
|
|
|
|
startChar, _ := terminal.ReadKey()
|
|
|
|
if startChar != '[' && startChar != 'O' {
|
|
|
|
return string(startChar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read one or two characters after
|
|
|
|
// \e[ or \eO, which is the end of the
|
|
|
|
// handled escape sequences
|
|
|
|
runes := [2]rune{'\000', '\000'}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
ch, size := terminal.ReadKey()
|
|
|
|
if size == 0 {
|
2021-04-09 13:48:39 -04:00
|
|
|
return string(key.Esc)
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
runes[i] = ch
|
|
|
|
|
|
|
|
if i == 0 && runes[0] >= 'A' && runes[0] <= 'Z' {
|
|
|
|
return escSeqToKey([]rune{startChar, runes[0]})
|
|
|
|
}
|
|
|
|
|
|
|
|
// \e[*~
|
|
|
|
if i == 1 && startChar == '[' && runes[1] == '~' {
|
|
|
|
return escSeqToKey([]rune{startChar, runes[0], runes[1]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:48:39 -04:00
|
|
|
return string(key.Esc)
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func escSeqToKey(seq []rune) string {
|
|
|
|
// \eO*
|
|
|
|
// \e[*
|
|
|
|
if len(seq) == 2 {
|
|
|
|
startChar, cmd := seq[0], seq[1]
|
|
|
|
if startChar == 'O' {
|
|
|
|
switch cmd {
|
|
|
|
case 'H':
|
|
|
|
return keyHome
|
|
|
|
case 'F':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
} else if startChar == '[' {
|
|
|
|
switch cmd {
|
|
|
|
case 'A':
|
|
|
|
return keyUp
|
|
|
|
case 'B':
|
|
|
|
return keyDown
|
|
|
|
case 'C':
|
|
|
|
return keyRight
|
|
|
|
case 'D':
|
|
|
|
return keyLeft
|
|
|
|
case 'H':
|
|
|
|
return keyHome
|
|
|
|
case 'F':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(seq) == 3 { // \e[*~
|
|
|
|
cmd := seq[1]
|
|
|
|
switch cmd {
|
|
|
|
case '1':
|
|
|
|
return keyHome
|
|
|
|
case '3':
|
|
|
|
return keyDelete
|
|
|
|
case '4':
|
|
|
|
return keyEnd
|
|
|
|
case '5':
|
|
|
|
return keyPageUp
|
|
|
|
case '6':
|
|
|
|
return keyPageDown
|
|
|
|
case '7':
|
|
|
|
return keyHome
|
|
|
|
case '8':
|
|
|
|
return keyEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:48:39 -04:00
|
|
|
return string(key.Esc)
|
2021-04-02 12:03:33 -04:00
|
|
|
}
|