import (
"encoding/json"
+ "errors"
"io/ioutil"
"os"
+ "os/exec"
+ "strconv"
"strings"
+ "time"
- "github.com/gdamore/tcell"
"github.com/mitchellh/go-homedir"
+ "github.com/yuin/gopher-lua"
"github.com/zyedidia/clipboard"
+ "github.com/zyedidia/tcell"
)
var bindings map[tcell.Key]func(*View) bool
bindings = make(map[tcell.Key]func(*View) bool)
actions := map[string]func(*View) bool{
- "CursorUp": CursorUp,
- "CursorDown": CursorDown,
- "CursorLeft": CursorLeft,
- "CursorRight": CursorRight,
- "InsertEnter": InsertEnter,
- "InsertSpace": InsertSpace,
- "Backspace": Backspace,
- "Delete": Delete,
- "InsertTab": InsertTab,
- "Save": Save,
- "Find": Find,
- "FindNext": FindNext,
- "FindPrevious": FindPrevious,
- "Undo": Undo,
- "Redo": Redo,
- "Copy": Copy,
- "Cut": Cut,
- "Paste": Paste,
- "SelectAll": SelectAll,
- "OpenFile": OpenFile,
- "Beginning": Beginning,
- "End": End,
- "PageUp": PageUp,
- "PageDown": PageDown,
- "HalfPageUp": HalfPageUp,
- "HalfPageDown": HalfPageDown,
- "ToggleRuler": ToggleRuler,
+ "CursorUp": (*View).CursorUp,
+ "CursorDown": (*View).CursorDown,
+ "CursorLeft": (*View).CursorLeft,
+ "CursorRight": (*View).CursorRight,
+ "CursorStart": (*View).CursorStart,
+ "CursorEnd": (*View).CursorEnd,
+ "SelectToStart": (*View).SelectToStart,
+ "SelectToEnd": (*View).SelectToEnd,
+ "SelectUp": (*View).SelectUp,
+ "SelectDown": (*View).SelectDown,
+ "SelectLeft": (*View).SelectLeft,
+ "SelectRight": (*View).SelectRight,
+ "WordRight": (*View).WordRight,
+ "WordLeft": (*View).WordLeft,
+ "SelectWordRight": (*View).SelectWordRight,
+ "SelectWordLeft": (*View).SelectWordLeft,
+ "SelectToStartOfLine": (*View).SelectToStartOfLine,
+ "SelectToEndOfLine": (*View).SelectToEndOfLine,
+ "InsertEnter": (*View).InsertEnter,
+ "InsertSpace": (*View).InsertSpace,
+ "Backspace": (*View).Backspace,
+ "Delete": (*View).Delete,
+ "InsertTab": (*View).InsertTab,
+ "Save": (*View).Save,
+ "Find": (*View).Find,
+ "FindNext": (*View).FindNext,
+ "FindPrevious": (*View).FindPrevious,
+ "Undo": (*View).Undo,
+ "Redo": (*View).Redo,
+ "Copy": (*View).Copy,
+ "Cut": (*View).Cut,
+ "CutLine": (*View).CutLine,
+ "Paste": (*View).Paste,
+ "SelectAll": (*View).SelectAll,
+ "OpenFile": (*View).OpenFile,
+ "Start": (*View).Start,
+ "End": (*View).End,
+ "PageUp": (*View).PageUp,
+ "PageDown": (*View).PageDown,
+ "HalfPageUp": (*View).HalfPageUp,
+ "HalfPageDown": (*View).HalfPageDown,
+ "StartOfLine": (*View).StartOfLine,
+ "EndOfLine": (*View).EndOfLine,
+ "ToggleRuler": (*View).ToggleRuler,
+ "JumpLine": (*View).JumpLine,
}
keys := map[string]tcell.Key{
"Down": tcell.KeyDown,
"Right": tcell.KeyRight,
"Left": tcell.KeyLeft,
+ "AltUp": tcell.KeyAltUp,
+ "AltDown": tcell.KeyAltDown,
+ "AltLeft": tcell.KeyAltLeft,
+ "AltRight": tcell.KeyAltRight,
+ "CtrlUp": tcell.KeyCtrlUp,
+ "CtrlDown": tcell.KeyCtrlDown,
+ "CtrlLeft": tcell.KeyCtrlLeft,
+ "CtrlRight": tcell.KeyCtrlRight,
+ "ShiftUp": tcell.KeyShiftUp,
+ "ShiftDown": tcell.KeyShiftDown,
+ "ShiftLeft": tcell.KeyShiftLeft,
+ "ShiftRight": tcell.KeyShiftRight,
+ "AltShiftUp": tcell.KeyAltShiftUp,
+ "AltShiftDown": tcell.KeyAltShiftDown,
+ "AltShiftLeft": tcell.KeyAltShiftLeft,
+ "AltShiftRight": tcell.KeyAltShiftRight,
+ "CtrlShiftUp": tcell.KeyCtrlShiftUp,
+ "CtrlShiftDown": tcell.KeyCtrlShiftDown,
+ "CtrlShiftLeft": tcell.KeyCtrlShiftLeft,
+ "CtrlShiftRight": tcell.KeyCtrlShiftRight,
"UpLeft": tcell.KeyUpLeft,
"UpRight": tcell.KeyUpRight,
"DownLeft": tcell.KeyDownLeft,
if _, e := os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
if err != nil {
- TermMessage("Error reading settings.json file: " + err.Error())
+ TermMessage("Error reading bindings.json file: " + err.Error())
return
}
- json.Unmarshal(input, &parsed)
+ err = json.Unmarshal(input, &parsed)
+ if err != nil {
+ TermMessage("Error reading bindings.json:", err.Error())
+ }
}
for k, v := range defaults {
// DefaultBindings returns a map containing micro's default keybindings
func DefaultBindings() map[string]string {
return map[string]string{
- "Up": "CursorUp",
- "Down": "CursorDown",
- "Right": "CursorRight",
- "Left": "CursorLeft",
- "Enter": "InsertEnter",
- "Space": "InsertSpace",
- "Backspace": "Backspace",
- "Backspace2": "Backspace",
- "Tab": "InsertTab",
- "CtrlO": "OpenFile",
- "CtrlS": "Save",
- "CtrlF": "Find",
- "CtrlN": "FindNext",
- "CtrlP": "FindPrevious",
- "CtrlZ": "Undo",
- "CtrlY": "Redo",
- "CtrlC": "Copy",
- "CtrlX": "Cut",
- "CtrlV": "Paste",
- "CtrlA": "SelectAll",
- "Home": "Beginning",
- "End": "End",
- "PageUp": "PageUp",
- "PageDown": "PageDown",
- "CtrlU": "HalfPageUp",
- "CtrlD": "HalfPageDown",
- "CtrlR": "ToggleRuler",
- "Delete": "Delete",
+ "Up": "CursorUp",
+ "Down": "CursorDown",
+ "Right": "CursorRight",
+ "Left": "CursorLeft",
+ "ShiftUp": "SelectUp",
+ "ShiftDown": "SelectDown",
+ "ShiftLeft": "SelectLeft",
+ "ShiftRight": "SelectRight",
+ "AltLeft": "WordLeft",
+ "AltRight": "WordRight",
+ "AltShiftRight": "SelectWordRight",
+ "AltShiftLeft": "SelectWordLeft",
+ "CtrlLeft": "StartOfLine",
+ "CtrlRight": "EndOfLine",
+ "CtrlShiftLeft": "SelectToStartOfLine",
+ "CtrlShiftRight": "SelectToEndOfLine",
+ "CtrlUp": "CursorStart",
+ "CtrlDown": "CursorEnd",
+ "CtrlShiftUp": "SelectToStart",
+ "CtrlShiftDown": "SelectToEnd",
+ "Enter": "InsertEnter",
+ "Space": "InsertSpace",
+ "Backspace": "Backspace",
+ "Backspace2": "Backspace",
+ "Tab": "InsertTab",
+ "CtrlO": "OpenFile",
+ "CtrlS": "Save",
+ "CtrlF": "Find",
+ "CtrlN": "FindNext",
+ "CtrlP": "FindPrevious",
+ "CtrlZ": "Undo",
+ "CtrlY": "Redo",
+ "CtrlC": "Copy",
+ "CtrlX": "Cut",
+ "CtrlK": "CutLine",
+ "CtrlV": "Paste",
+ "CtrlA": "SelectAll",
+ "Home": "Start",
+ "End": "End",
+ "PgUp": "PageUp",
+ "PgDn": "PageDown",
+ "CtrlU": "HalfPageUp",
+ "CtrlD": "HalfPageDown",
+ "CtrlR": "ToggleRuler",
+ "CtrlL": "JumpLine",
+ "Delete": "Delete",
}
}
// CursorUp moves the cursor up
-func CursorUp(v *View) bool {
- v.cursor.ResetSelection()
- v.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 CursorDown(v *View) bool {
- v.cursor.ResetSelection()
- v.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 CursorLeft(v *View) bool {
- v.cursor.ResetSelection()
- v.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 CursorRight(v *View) bool {
- v.cursor.ResetSelection()
- v.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 InsertSpace(v *View) bool {
+func (v *View) InsertSpace() bool {
// Insert a space
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+ if v.Cursor.HasSelection() {
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
}
- v.eh.Insert(v.cursor.Loc(), " ")
- v.cursor.Right()
+ v.eh.Insert(v.Cursor.Loc(), " ")
+ v.Cursor.Right()
return true
}
// InsertEnter inserts a newline plus possible some whitespace if autoindent is on
-func InsertEnter(v *View) bool {
+func (v *View) InsertEnter() bool {
// Insert a newline
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+ 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()
+ v.eh.Insert(v.Cursor.Loc(), "\n")
+ ws := GetLeadingWhitespace(v.Buf.Lines[v.Cursor.y])
+ v.Cursor.Right()
- if settings.AutoIndent {
- v.eh.Insert(v.cursor.Loc(), ws)
+ if settings["autoindent"].(bool) {
+ v.eh.Insert(v.Cursor.Loc(), ws)
for i := 0; i < len(ws); i++ {
- v.cursor.Right()
+ v.Cursor.Right()
}
}
- v.cursor.lastVisualX = v.cursor.GetVisualX()
+ v.Cursor.lastVisualX = v.Cursor.GetVisualX()
return true
}
// Backspace deletes the previous character
-func Backspace(v *View) bool {
+func (v *View) Backspace() bool {
// Delete a character
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- } else if v.cursor.Loc() > 0 {
+ 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
// 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]
- if settings.TabsToSpaces && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%settings.TabSize == 0 {
- loc := v.cursor.Loc()
- v.cursor.SetLoc(loc - settings.TabSize)
- cx, cy := v.cursor.x, v.cursor.y
- v.cursor.SetLoc(loc)
- v.eh.Remove(loc-settings.TabSize, loc)
- v.cursor.x, v.cursor.y = cx, cy
+ 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.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.x, v.Cursor.y = cx, cy
}
}
- v.cursor.lastVisualX = v.cursor.GetVisualX()
+ v.Cursor.lastVisualX = v.Cursor.GetVisualX()
return true
}
// Delete deletes the next character
-func Delete(v *View) bool {
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+func (v *View) Delete() bool {
+ if v.Cursor.HasSelection() {
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
} else {
- loc := v.cursor.Loc()
- if loc < len(v.buf.text) {
+ loc := v.Cursor.Loc()
+ if loc < v.Buf.Len() {
v.eh.Remove(loc, loc+1)
}
}
}
// InsertTab inserts a tab or spaces
-func InsertTab(v *View) bool {
+func (v *View) InsertTab() bool {
// Insert a tab
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- }
- if settings.TabsToSpaces {
- v.eh.Insert(v.cursor.Loc(), Spaces(settings.TabSize))
- for i := 0; i < settings.TabSize; i++ {
- v.cursor.Right()
+ 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()
+ v.eh.Insert(v.Cursor.Loc(), "\t")
+ v.Cursor.Right()
}
return true
}
// Save the buffer to disk
-func Save(v *View) bool {
+func (v *View) Save() bool {
// If this is an empty buffer, ask for a filename
- if v.buf.path == "" {
+ if v.Buf.Path == "" {
filename, canceled := messenger.Prompt("Filename: ")
if !canceled {
- v.buf.path = filename
- v.buf.name = filename
+ v.Buf.Path = filename
+ v.Buf.Name = filename
} else {
return true
}
}
- err := v.buf.Save()
+ err := v.Buf.Save()
if err != nil {
messenger.Error(err.Error())
} else {
- messenger.Message("Saved " + v.buf.path)
+ messenger.Message("Saved " + v.Buf.Path)
+ switch v.Buf.Filetype {
+ case "Go":
+ v.GoSave()
+ }
+ }
+ if err := L.CallByParam(lua.P{
+ Fn: L.GetGlobal("onSave"),
+ NRet: 0,
+ Protect: true,
+ }); err != nil {
+ // The function isn't defined by this plugin
+ messenger.Error(err)
+ return true
}
return true
}
+// GoSave saves the current file (must be a go file) and runs goimports or gofmt
+// depending on the user's configuration
+func (v *View) GoSave() {
+ if settings["goimports"] == true {
+ messenger.Message("Running goimports...")
+ err := goimports(v.Buf.Path)
+ if err != nil {
+ messenger.Error(err)
+ } else {
+ messenger.Message("Saved " + v.Buf.Path)
+ }
+ v.ReOpen()
+ } else if settings["gofmt"] == true {
+ messenger.Message("Running gofmt...")
+ err := gofmt(v.Buf.Path)
+ if err != nil {
+ messenger.Error(err)
+ } else {
+ messenger.Message("Saved " + v.Buf.Path)
+ }
+ v.ReOpen()
+ return
+ }
+
+ return
+}
+
// Find opens a prompt and searches forward for the input
-func Find(v *View) bool {
- if v.cursor.HasSelection() {
- searchStart = v.cursor.curSelection[1]
+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)
+ searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
}
BeginSearch()
return true
}
// FindNext searches forwards for the last used search term
-func FindNext(v *View) bool {
- if v.cursor.HasSelection() {
- searchStart = v.cursor.curSelection[1]
+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)
+ searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
}
messenger.Message("Find: " + lastSearch)
Search(lastSearch, v, true)
}
// FindPrevious searches backwards for the last used search term
-func FindPrevious(v *View) bool {
- if v.cursor.HasSelection() {
- searchStart = v.cursor.curSelection[0]
+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)
+ searchStart = ToCharPos(v.Cursor.x, v.Cursor.y, v.Buf)
}
messenger.Message("Find: " + lastSearch)
Search(lastSearch, v, false)
}
// Undo undoes the last action
-func Undo(v *View) bool {
+func (v *View) Undo() bool {
v.eh.Undo()
return true
}
// Redo redoes the last action
-func Redo(v *View) bool {
+func (v *View) Redo() bool {
v.eh.Redo()
return true
}
// Copy the selection to the system clipboard
-func Copy(v *View) bool {
- if v.cursor.HasSelection() {
- clipboard.WriteAll(v.cursor.GetSelection())
+func (v *View) Copy() bool {
+ if v.Cursor.HasSelection() {
+ clipboard.WriteAll(v.Cursor.GetSelection())
+ v.freshClip = true
}
return true
}
+// CutLine cuts the current line to the clipboard
+func (v *View) CutLine() bool {
+ v.Cursor.SelectLine()
+ if v.freshClip == true {
+
+ if v.Cursor.HasSelection() {
+ if clip, err := clipboard.ReadAll(); err != nil {
+ messenger.Error(err)
+ } else {
+ clipboard.WriteAll(clip + v.Cursor.GetSelection())
+ }
+ }
+ } else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
+ v.Copy()
+ }
+ v.freshClip = true
+ v.lastCutTime = time.Now()
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
+ return true
+}
+
// Cut the selection to the system clipboard
-func Cut(v *View) bool {
- if v.cursor.HasSelection() {
- clipboard.WriteAll(v.cursor.GetSelection())
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+func (v *View) Cut() bool {
+ if v.Cursor.HasSelection() {
+ clipboard.WriteAll(v.Cursor.GetSelection())
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
+ v.freshClip = true
}
return true
}
// Paste whatever is in the system clipboard into the buffer
// Delete and paste if the user has a selection
-func Paste(v *View) bool {
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
+func (v *View) Paste() bool {
+ if v.Cursor.HasSelection() {
+ v.Cursor.DeleteSelection()
+ v.Cursor.ResetSelection()
}
clip, _ := clipboard.ReadAll()
- v.eh.Insert(v.cursor.Loc(), clip)
- v.cursor.SetLoc(v.cursor.Loc() + Count(clip))
+ v.eh.Insert(v.Cursor.Loc(), clip)
+ v.Cursor.SetLoc(v.Cursor.Loc() + Count(clip))
+ v.freshClip = false
return true
}
// SelectAll selects the entire buffer
-func SelectAll(v *View) bool {
- v.cursor.curSelection[1] = 0
- v.cursor.curSelection[0] = v.buf.Len()
+func (v *View) SelectAll() bool {
+ v.Cursor.curSelection[1] = 0
+ v.Cursor.curSelection[0] = v.Buf.Len()
// Put the cursor at the beginning
- v.cursor.x = 0
- v.cursor.y = 0
+ v.Cursor.x = 0
+ v.Cursor.y = 0
return true
}
// OpenFile opens a new file in the buffer
-func OpenFile(v *View) bool {
+func (v *View) OpenFile() bool {
if v.CanClose("Continue? (yes, no, save) ") {
filename, canceled := messenger.Prompt("File to open: ")
if canceled {
return true
}
-// Beginning moves the viewport to the start of the buffer
-func Beginning(v *View) bool {
- v.topline = 0
+// Start moves the viewport to the start of the buffer
+func (v *View) Start() bool {
+ v.Topline = 0
return false
}
// End moves the viewport to the end of the buffer
-func End(v *View) bool {
- if v.height > len(v.buf.lines) {
- v.topline = 0
+func (v *View) End() bool {
+ if v.height > v.Buf.NumLines {
+ v.Topline = 0
} else {
- v.topline = len(v.buf.lines) - v.height
+ v.Topline = v.Buf.NumLines - v.height
}
return false
}
// PageUp scrolls the view up a page
-func PageUp(v *View) bool {
- if v.topline > v.height {
+func (v *View) PageUp() bool {
+ if v.Topline > v.height {
v.ScrollUp(v.height)
} else {
- v.topline = 0
+ v.Topline = 0
}
return false
}
// PageDown scrolls the view down a page
-func PageDown(v *View) bool {
- if len(v.buf.lines)-(v.topline+v.height) > v.height {
+func (v *View) PageDown() bool {
+ if v.Buf.NumLines-(v.Topline+v.height) > v.height {
v.ScrollDown(v.height)
- } else {
- if len(v.buf.lines) >= v.height {
- v.topline = len(v.buf.lines) - v.height
- }
+ } else if v.Buf.NumLines >= v.height {
+ v.Topline = v.Buf.NumLines - v.height
}
return false
}
// HalfPageUp scrolls the view up half a page
-func HalfPageUp(v *View) bool {
- if v.topline > v.height/2 {
+func (v *View) HalfPageUp() bool {
+ if v.Topline > v.height/2 {
v.ScrollUp(v.height / 2)
} else {
- v.topline = 0
+ v.Topline = 0
}
return false
}
// HalfPageDown scrolls the view down half a page
-func HalfPageDown(v *View) bool {
- if len(v.buf.lines)-(v.topline+v.height) > v.height/2 {
+func (v *View) HalfPageDown() bool {
+ if v.Buf.NumLines-(v.Topline+v.height) > v.height/2 {
v.ScrollDown(v.height / 2)
} else {
- if len(v.buf.lines) >= v.height {
- v.topline = len(v.buf.lines) - v.height
+ if v.Buf.NumLines >= v.height {
+ v.Topline = v.Buf.NumLines - v.height
}
}
return false
}
// ToggleRuler turns line numbers off and on
-func ToggleRuler(v *View) bool {
- if settings.Ruler == false {
- settings.Ruler = true
+func (v *View) ToggleRuler() bool {
+ if settings["ruler"] == false {
+ settings["ruler"] = true
} else {
- settings.Ruler = false
+ settings["ruler"] = false
}
return false
}
+// JumpLine jumps to a line and moves the view accordingly.
+func (v *View) JumpLine() bool {
+ // Prompt for line number
+ linestring, canceled := messenger.Prompt("Jump to line # ")
+ if canceled {
+ return false
+ }
+ lineint, err := strconv.Atoi(linestring)
+ lineint = lineint - 1 // fix offset
+ if err != nil {
+ messenger.Error(err) // return errors
+ return false
+ }
+ // Move cursor and view if possible.
+ if lineint < v.Buf.NumLines {
+ v.Cursor.x = 0
+ v.Cursor.y = lineint
+ return true
+ }
+ messenger.Error("Only ", v.Buf.NumLines, " lines to jump")
+ return false
+}
+
// None is no action
func None() bool {
return false
}
+
+// gofmt runs gofmt on a file
+func gofmt(file string) error {
+ cmd := exec.Command("gofmt", "-w", file)
+ cmd.Start()
+ err := cmd.Wait()
+ if err != nil {
+ return errors.New("Check syntax ") //TODO: highlight or display locations
+ }
+ return nil
+}
+
+// goimports runs goimports on a file
+func goimports(file string) error {
+ cmd := exec.Command("goimports", "-w", file)
+ cmd.Start()
+ err := cmd.Wait()
+ if err != nil {
+ return errors.New("Check syntax ") //TODO: highlight or display locations
+ }
+ return nil
+}