- "Esc": "ClearStatus",
- }
-}
-
-// CursorUp moves the cursor up
-func (v *View) CursorUp() bool {
- if v.Cursor.HasSelection() {
- v.Cursor.SetLoc(v.Cursor.curSelection[0])
- v.Cursor.ResetSelection()
- }
- v.Cursor.Up()
- return true
-}
-
-// CursorDown moves the cursor down
-func (v *View) CursorDown() bool {
- if v.Cursor.HasSelection() {
- v.Cursor.SetLoc(v.Cursor.curSelection[1])
- v.Cursor.ResetSelection()
- }
- v.Cursor.Down()
- return true
-}
-
-// CursorLeft moves the cursor left
-func (v *View) CursorLeft() bool {
- if v.Cursor.HasSelection() {
- v.Cursor.SetLoc(v.Cursor.curSelection[0])
- v.Cursor.ResetSelection()
- } else {
- v.Cursor.Left()
- }
- return true
-}
-
-// CursorRight moves the cursor right
-func (v *View) CursorRight() bool {
- if v.Cursor.HasSelection() {
- v.Cursor.SetLoc(v.Cursor.curSelection[1] - 1)
- v.Cursor.ResetSelection()
- } else {
- v.Cursor.Right()
- }
- return true
-}
-
-// WordRight moves the cursor one word to the right
-func (v *View) WordRight() bool {
- v.Cursor.WordRight()
- return true
-}
-
-// WordLeft moves the cursor one word to the left
-func (v *View) WordLeft() bool {
- v.Cursor.WordLeft()
- return true
-}
-
-// SelectUp selects up one line
-func (v *View) SelectUp() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.Up()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectDown selects down one line
-func (v *View) SelectDown() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.Down()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectLeft selects the character to the left of the cursor
-func (v *View) SelectLeft() bool {
- loc := v.Cursor.Loc()
- count := v.Buf.Len() - 1
- if loc > count {
- loc = count
- }
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.Left()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectRight selects the character to the right of the cursor
-func (v *View) SelectRight() bool {
- loc := v.Cursor.Loc()
- count := v.Buf.Len() - 1
- if loc > count {
- loc = count
- }
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.Right()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectWordRight selects the word to the right of the cursor
-func (v *View) SelectWordRight() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.WordRight()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectWordLeft selects the word to the left of the cursor
-func (v *View) SelectWordLeft() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.WordLeft()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// StartOfLine moves the cursor to the start of the line
-func (v *View) StartOfLine() bool {
- v.Cursor.Start()
- return true
-}
-
-// EndOfLine moves the cursor to the end of the line
-func (v *View) EndOfLine() bool {
- v.Cursor.End()
- return true
-}
-
-// SelectToStartOfLine selects to the start of the current line
-func (v *View) SelectToStartOfLine() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.Start()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// SelectToEndOfLine selects to the end of the current line
-func (v *View) SelectToEndOfLine() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.Cursor.End()
- v.Cursor.SelectTo(v.Cursor.Loc())
- return true
-}
-
-// CursorStart moves the cursor to the start of the buffer
-func (v *View) CursorStart() bool {
- v.Cursor.x = 0
- v.Cursor.y = 0
- return true
-}
-
-// CursorEnd moves the cursor to the end of the buffer
-func (v *View) CursorEnd() bool {
- v.Cursor.SetLoc(v.Buf.Len())
- return true
-}
-
-// SelectToStart selects the text from the cursor to the start of the buffer
-func (v *View) SelectToStart() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.CursorStart()
- v.Cursor.SelectTo(0)
- return true
-}
-
-// SelectToEnd selects the text from the cursor to the end of the buffer
-func (v *View) SelectToEnd() bool {
- loc := v.Cursor.Loc()
- if !v.Cursor.HasSelection() {
- v.Cursor.origSelection[0] = loc
- }
- v.CursorEnd()
- v.Cursor.SelectTo(v.Buf.Len())
- return true
-}
-
-// InsertSpace inserts a space
-func (v *View) InsertSpace() bool {
- // Insert a space
- if v.Cursor.HasSelection() {
- v.Cursor.DeleteSelection()
- v.Cursor.ResetSelection()
- }
- v.eh.Insert(v.Cursor.Loc(), " ")
- v.Cursor.Right()
- return true
-}
-
-// InsertEnter inserts a newline plus possible some whitespace if autoindent is on
-func (v *View) InsertEnter() bool {
- // Insert a newline
- if v.Cursor.HasSelection() {
- v.Cursor.DeleteSelection()
- v.Cursor.ResetSelection()
- }
-
- v.eh.Insert(v.Cursor.Loc(), "\n")
- ws := GetLeadingWhitespace(v.Buf.Lines[v.Cursor.y])
- v.Cursor.Right()
-
- if settings["autoindent"].(bool) {
- v.eh.Insert(v.Cursor.Loc(), ws)
- for i := 0; i < len(ws); i++ {
- v.Cursor.Right()
- }
- }
- v.Cursor.lastVisualX = v.Cursor.GetVisualX()
- return true
-}
-
-// Backspace deletes the previous character
-func (v *View) Backspace() bool {
- // Delete a character
- if v.Cursor.HasSelection() {
- v.Cursor.DeleteSelection()
- v.Cursor.ResetSelection()
- } else if v.Cursor.Loc() > 0 {
- // We have to do something a bit hacky here because we want to
- // delete the line by first moving left and then deleting backwards
- // but the undo redo would place the cursor in the wrong place
- // So instead we move left, save the position, move back, delete
- // and restore the position
-
- // If the user is using spaces instead of tabs and they are deleting
- // whitespace at the start of the line, we should delete as if its a
- // tab (tabSize number of spaces)
- lineStart := v.Buf.Lines[v.Cursor.y][:v.Cursor.x]
- tabSize := int(settings["tabsize"].(float64))
- if settings["tabsToSpaces"].(bool) && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%tabSize == 0 {
- loc := v.Cursor.Loc()
- v.Cursor.SetLoc(loc - tabSize)
- cx, cy := v.Cursor.x, v.Cursor.y
- v.Cursor.SetLoc(loc)
- v.eh.Remove(loc-tabSize, loc)
- v.Cursor.x, v.Cursor.y = cx, cy
- } else {
- v.Cursor.Left()
- cx, cy := v.Cursor.x, v.Cursor.y
- v.Cursor.Right()
- loc := v.Cursor.Loc()
- v.eh.Remove(loc-1, loc)
- v.Cursor.x, v.Cursor.y = cx, cy
- }
- }
- v.Cursor.lastVisualX = v.Cursor.GetVisualX()
- return true
-}
-
-// Delete deletes the next character
-func (v *View) Delete() bool {
- if v.Cursor.HasSelection() {
- v.Cursor.DeleteSelection()
- v.Cursor.ResetSelection()
- } else {
- loc := v.Cursor.Loc()
- if loc < v.Buf.Len() {
- v.eh.Remove(loc, loc+1)
- }
- }
- return true
-}
-
-// InsertTab inserts a tab or spaces
-func (v *View) InsertTab() bool {
- // Insert a tab
- if v.Cursor.HasSelection() {
- v.Cursor.DeleteSelection()
- v.Cursor.ResetSelection()
- }
- if settings["tabsToSpaces"].(bool) {
- tabSize := int(settings["tabsize"].(float64))
- v.eh.Insert(v.Cursor.Loc(), Spaces(tabSize))
- for i := 0; i < tabSize; i++ {
- v.Cursor.Right()
- }
- } else {
- v.eh.Insert(v.Cursor.Loc(), "\t")
- v.Cursor.Right()
- }
- return true
-}
-
-// Save the buffer to disk
-func (v *View) Save() bool {
- // If this is an empty buffer, ask for a filename
- if v.Buf.Path == "" {
- filename, canceled := messenger.Prompt("Filename: ")
- if !canceled {
- v.Buf.Path = filename
- v.Buf.Name = filename
- } else {
- return true
- }
- }
- err := v.Buf.Save()
- if err != nil {
- messenger.Error(err.Error())
- } else {
- messenger.Message("Saved " + v.Buf.Path)
- }
- return true
-}
-
-// Find opens a prompt and searches forward for the input
-func (v *View) Find() bool {
- if v.Cursor.HasSelection() {
- searchStart = v.Cursor.curSelection[1]
- } else {
- searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
- }
- BeginSearch()
- return true
-}
-
-// FindNext searches forwards for the last used search term
-func (v *View) FindNext() bool {
- if v.Cursor.HasSelection() {
- searchStart = v.Cursor.curSelection[1]
- } else {
- searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
- }
- messenger.Message("Finding: " + lastSearch)
- Search(lastSearch, v, true)
- return true
-}
-
-// FindPrevious searches backwards for the last used search term
-func (v *View) FindPrevious() bool {
- if v.Cursor.HasSelection() {
- searchStart = v.Cursor.curSelection[0]
- } else {
- searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
- }
- messenger.Message("Finding: " + lastSearch)
- Search(lastSearch, v, false)
- return true
-}
-
-// Undo undoes the last action
-func (v *View) Undo() bool {
- v.eh.Undo()
- return true
-}
-
-// Redo redoes the last action
-func (v *View) Redo() bool {
- v.eh.Redo()
- return true
-}
-
-// Copy the selection to the system clipboard
-func (v *View) Copy() bool {
- if v.Cursor.HasSelection() {
- clipboard.WriteAll(v.Cursor.GetSelection())
- v.freshClip = true
- messenger.Message("Copied selection to clipboard")