2021-04-13 14:43:31 -04:00
|
|
|
// Package editor The main interface/implementation of the editor object
|
2021-03-19 16:36:02 -04:00
|
|
|
package editor
|
2021-03-19 17:03:56 -04:00
|
|
|
|
|
|
|
import (
|
2021-04-01 13:25:59 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2023-10-06 11:30:21 -04:00
|
|
|
"timshome.page/gilo/char"
|
2021-04-07 16:33:00 -04:00
|
|
|
"timshome.page/gilo/editor/document"
|
2021-04-13 14:54:44 -04:00
|
|
|
"timshome.page/gilo/internal/gilo"
|
2021-04-07 13:10:40 -04:00
|
|
|
"timshome.page/gilo/terminal"
|
2021-03-19 17:03:56 -04:00
|
|
|
)
|
|
|
|
|
2021-03-24 15:09:28 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Editor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-04-01 13:25:59 -04:00
|
|
|
type statusMsg struct {
|
|
|
|
message string
|
|
|
|
created time.Time
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
type Editor struct {
|
2021-04-02 10:48:51 -04:00
|
|
|
screen *terminal.Screen
|
2021-04-13 14:54:44 -04:00
|
|
|
cursor *gilo.Point
|
|
|
|
offset *gilo.Point
|
2021-04-07 16:33:00 -04:00
|
|
|
doc *document.Document
|
2021-04-02 10:48:51 -04:00
|
|
|
status *statusMsg
|
2021-04-07 16:26:05 -04:00
|
|
|
search *search
|
2021-04-02 10:34:19 -04:00
|
|
|
quitTimes uint8
|
2021-04-02 10:48:51 -04:00
|
|
|
renderX int
|
2021-03-23 15:51:59 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func NewEditor() *Editor {
|
2021-04-01 13:25:59 -04:00
|
|
|
// Subtract rows for status bar and message bar/prompt
|
2021-04-09 13:48:39 -04:00
|
|
|
screen := terminal.Size()
|
2021-04-01 13:25:59 -04:00
|
|
|
screen.Rows -= 2
|
2021-04-01 09:41:16 -04:00
|
|
|
|
2021-04-01 13:25:59 -04:00
|
|
|
status := &statusMsg{
|
|
|
|
"",
|
|
|
|
time.Now(),
|
|
|
|
}
|
2021-03-24 14:17:29 -04:00
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
return &Editor{
|
2021-03-31 09:43:47 -04:00
|
|
|
screen,
|
2021-04-13 14:54:44 -04:00
|
|
|
gilo.DefaultPoint(),
|
|
|
|
gilo.DefaultPoint(),
|
2021-04-09 13:48:39 -04:00
|
|
|
document.NewDocument(),
|
2021-04-01 13:25:59 -04:00
|
|
|
status,
|
2021-04-07 16:26:05 -04:00
|
|
|
newSearch(),
|
2021-04-13 14:54:44 -04:00
|
|
|
gilo.QuitTimes,
|
2021-03-31 14:56:46 -04:00
|
|
|
0,
|
2021-03-31 09:43:47 -04:00
|
|
|
}
|
2021-03-22 09:12:39 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) Open(filename string) {
|
2021-04-07 16:33:00 -04:00
|
|
|
e.doc.Open(filename)
|
2021-03-30 18:00:06 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) SetStatusMessage(template string, a ...interface{}) {
|
2021-04-06 10:59:24 -04:00
|
|
|
e.status = &statusMsg{
|
|
|
|
fmt.Sprintf(template, a...),
|
|
|
|
time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) ProcessKeypress() bool {
|
2021-04-06 10:59:24 -04:00
|
|
|
ch, _ := terminal.ReadKey()
|
|
|
|
|
|
|
|
return e.processKeypressChar(ch)
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) save() {
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.doc.Filename == "" {
|
|
|
|
e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil)
|
|
|
|
if e.doc.Filename == "" {
|
2021-04-06 10:30:12 -04:00
|
|
|
e.SetStatusMessage("Save aborted")
|
2023-10-04 10:37:47 -04:00
|
|
|
return
|
2021-04-06 10:30:12 -04:00
|
|
|
}
|
2021-04-06 09:58:45 -04:00
|
|
|
}
|
2021-04-02 19:24:20 -04:00
|
|
|
|
2023-10-04 10:37:47 -04:00
|
|
|
// Set the syntax highlighting type, now that we now the file extension
|
|
|
|
e.doc.SelectSyntaxHighlight()
|
|
|
|
|
2021-04-07 16:33:00 -04:00
|
|
|
size := e.doc.Save()
|
2021-04-01 20:23:51 -04:00
|
|
|
|
|
|
|
if size > 0 {
|
|
|
|
e.SetStatusMessage("%d bytes written to disk", size)
|
|
|
|
} else {
|
|
|
|
e.SetStatusMessage("Failed to save file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) prompt(prompt string, callback func(string, string)) string {
|
2021-04-13 14:54:44 -04:00
|
|
|
buf := gilo.NewBuffer()
|
2021-04-02 19:24:20 -04:00
|
|
|
|
2021-04-06 09:58:45 -04:00
|
|
|
// Show the prompt message
|
|
|
|
e.SetStatusMessage(prompt, "")
|
|
|
|
e.RefreshScreen()
|
|
|
|
|
2021-04-02 19:24:20 -04:00
|
|
|
for {
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
e.SetStatusMessage(prompt, buf.ToString())
|
2021-04-06 09:58:45 -04:00
|
|
|
e.RefreshScreen()
|
2021-04-02 19:24:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, _ := terminal.ReadKey()
|
2023-10-06 11:30:21 -04:00
|
|
|
if ch == char.Enter {
|
2021-04-02 19:24:20 -04:00
|
|
|
if buf.Len() != 0 {
|
|
|
|
e.SetStatusMessage("")
|
2021-04-06 16:37:58 -04:00
|
|
|
if callback != nil {
|
|
|
|
callback(buf.ToString(), string(ch))
|
|
|
|
}
|
2021-04-02 19:24:20 -04:00
|
|
|
|
|
|
|
return buf.ToString()
|
|
|
|
}
|
2023-10-06 11:30:21 -04:00
|
|
|
} else if char.IsAscii(ch) && !char.IsCtrl(ch) {
|
2021-04-06 09:58:45 -04:00
|
|
|
buf.AppendRune(ch)
|
2023-10-06 11:30:21 -04:00
|
|
|
} else if ch == char.Backspace || ch == char.Ctrl('h') {
|
2021-04-06 10:30:12 -04:00
|
|
|
buf.Truncate(buf.Len() - 1)
|
2023-10-06 11:30:21 -04:00
|
|
|
} else if ch == char.Esc {
|
2021-04-06 10:30:12 -04:00
|
|
|
k := parseEscapeSequence()
|
|
|
|
if k == keyDelete {
|
|
|
|
buf.Truncate(buf.Len() - 1)
|
2023-10-06 11:30:21 -04:00
|
|
|
} else if k == string(char.Esc) {
|
2021-04-06 10:30:12 -04:00
|
|
|
e.SetStatusMessage("")
|
2021-04-06 16:37:58 -04:00
|
|
|
if callback != nil {
|
|
|
|
callback(buf.ToString(), k)
|
|
|
|
}
|
2021-04-06 10:30:12 -04:00
|
|
|
return ""
|
2021-04-06 16:37:58 -04:00
|
|
|
} else {
|
|
|
|
if callback != nil {
|
|
|
|
callback(buf.ToString(), k)
|
|
|
|
}
|
2021-04-06 10:30:12 -04:00
|
|
|
}
|
2021-04-02 19:24:20 -04:00
|
|
|
}
|
2021-04-06 16:37:58 -04:00
|
|
|
|
|
|
|
if callback != nil {
|
|
|
|
callback(buf.ToString(), string(ch))
|
|
|
|
}
|
2021-04-02 19:24:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) insertChar(ch rune) {
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y == e.doc.RowCount() {
|
|
|
|
e.doc.AppendRow("")
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-07 16:33:00 -04:00
|
|
|
e.doc.InsertChar(e.cursor, ch)
|
2021-04-02 14:52:44 -04:00
|
|
|
e.cursor.X += 1
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 17:02:22 -04:00
|
|
|
func (e *Editor) delChar() {
|
2021-04-07 16:33:00 -04:00
|
|
|
if e.cursor.Y == e.doc.RowCount() {
|
2021-04-02 10:48:51 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:14:19 -04:00
|
|
|
if e.cursor.X == 0 && e.cursor.Y == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if e.cursor.X > 0 {
|
2021-04-02 15:36:43 -04:00
|
|
|
at := e.cursor
|
|
|
|
at.X -= 1
|
2021-04-07 16:33:00 -04:00
|
|
|
e.doc.DelChar(at)
|
2021-04-02 16:14:19 -04:00
|
|
|
} else {
|
|
|
|
// Move cursor to the current end of the previous line
|
2021-04-07 16:33:00 -04:00
|
|
|
e.cursor.X = e.doc.GetRow(e.cursor.Y - 1).Size()
|
2021-04-02 16:14:19 -04:00
|
|
|
|
|
|
|
// Move the contents of the current row to the previous
|
2021-04-07 16:33:00 -04:00
|
|
|
e.doc.MergeRows(e.cursor.Y-1, e.cursor.Y)
|
2021-04-02 16:14:19 -04:00
|
|
|
|
|
|
|
e.cursor.Y -= 1
|
2021-04-02 10:48:51 -04:00
|
|
|
}
|
|
|
|
}
|