11 "github.com/yuin/gopher-lua"
12 "github.com/zyedidia/clipboard"
13 "github.com/zyedidia/tcell"
16 // PreActionCall executes the lua pre callback if possible
17 func PreActionCall(funcName string, view *View, args ...interface{}) bool {
19 for pl := range loadedPlugins {
20 ret, err := Call(pl+".pre"+funcName, append([]interface{}{view}, args...)...)
21 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
25 if ret == lua.LFalse {
32 // PostActionCall executes the lua plugin callback if possible
33 func PostActionCall(funcName string, view *View, args ...interface{}) bool {
35 for pl := range loadedPlugins {
36 ret, err := Call(pl+".on"+funcName, append([]interface{}{view}, args...)...)
37 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
41 if ret == lua.LFalse {
48 func (v *View) deselect(index int) bool {
49 if v.Cursor.HasSelection() {
50 v.Cursor.Loc = v.Cursor.CurSelection[index]
51 v.Cursor.ResetSelection()
52 v.Cursor.StoreVisualX()
58 // MousePress is the event that should happen when a normal click happens
59 // This is almost always bound to left click
60 func (v *View) MousePress(usePlugin bool, e *tcell.EventMouse) bool {
61 if usePlugin && !PreActionCall("MousePress", v, e) {
66 x -= v.lineNumOffset - v.leftCol + v.x
69 // This is usually bound to left click
70 v.MoveToMouseClick(x, y)
72 if len(v.Buf.cursors) > 1 {
73 for i := 1; i < len(v.Buf.cursors); i++ {
74 v.Buf.cursors[i] = nil
76 v.Buf.cursors = v.Buf.cursors[:1]
78 v.Cursor.ResetSelection()
81 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold && (x == v.lastLoc.X && y == v.lastLoc.Y) {
84 v.lastClickTime = time.Now()
90 v.Cursor.CopySelection("primary")
93 v.lastClickTime = time.Now()
99 v.Cursor.CopySelection("primary")
102 v.doubleClick = false
103 v.tripleClick = false
104 v.lastClickTime = time.Now()
106 v.Cursor.OrigSelection[0] = v.Cursor.Loc
107 v.Cursor.CurSelection[0] = v.Cursor.Loc
108 v.Cursor.CurSelection[1] = v.Cursor.Loc
110 v.mouseReleased = false
111 } else if !v.mouseReleased {
113 v.Cursor.AddLineToSelection()
114 } else if v.doubleClick {
115 v.Cursor.AddWordToSelection()
117 v.Cursor.SetSelectionEnd(v.Cursor.Loc)
118 v.Cursor.CopySelection("primary")
122 v.lastLoc = Loc{x, y}
125 PostActionCall("MousePress", v, e)
130 // ScrollUpAction scrolls the view up
131 func (v *View) ScrollUpAction(usePlugin bool) bool {
133 if usePlugin && !PreActionCall("ScrollUp", v) {
137 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
138 v.ScrollUp(scrollspeed)
141 PostActionCall("ScrollUp", v)
147 // ScrollDownAction scrolls the view up
148 func (v *View) ScrollDownAction(usePlugin bool) bool {
150 if usePlugin && !PreActionCall("ScrollDown", v) {
154 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
155 v.ScrollDown(scrollspeed)
158 PostActionCall("ScrollDown", v)
164 // Center centers the view on the cursor
165 func (v *View) Center(usePlugin bool) bool {
166 if usePlugin && !PreActionCall("Center", v) {
170 v.Topline = v.Cursor.Y - v.Height/2
171 if v.Topline+v.Height > v.Buf.NumLines {
172 v.Topline = v.Buf.NumLines - v.Height
179 return PostActionCall("Center", v)
184 // CursorUp moves the cursor up
185 func (v *View) CursorUp(usePlugin bool) bool {
186 if usePlugin && !PreActionCall("CursorUp", v) {
194 return PostActionCall("CursorUp", v)
199 // CursorDown moves the cursor down
200 func (v *View) CursorDown(usePlugin bool) bool {
201 if usePlugin && !PreActionCall("CursorDown", v) {
209 return PostActionCall("CursorDown", v)
214 // CursorLeft moves the cursor left
215 func (v *View) CursorLeft(usePlugin bool) bool {
216 if usePlugin && !PreActionCall("CursorLeft", v) {
220 if v.Cursor.HasSelection() {
221 v.Cursor.Loc = v.Cursor.CurSelection[0]
222 v.Cursor.ResetSelection()
223 v.Cursor.StoreVisualX()
225 tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
226 tabmovement := v.Buf.Settings["tabmovement"].(bool)
227 if tabstospaces && tabmovement {
228 tabsize := int(v.Buf.Settings["tabsize"].(float64))
229 line := v.Buf.Line(v.Cursor.Y)
230 if v.Cursor.X-tabsize >= 0 && line[v.Cursor.X-tabsize:v.Cursor.X] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X-tabsize]) {
231 for i := 0; i < tabsize; i++ {
243 return PostActionCall("CursorLeft", v)
248 // CursorRight moves the cursor right
249 func (v *View) CursorRight(usePlugin bool) bool {
250 if usePlugin && !PreActionCall("CursorRight", v) {
254 if v.Cursor.HasSelection() {
255 v.Cursor.Loc = v.Cursor.CurSelection[1]
256 v.Cursor.ResetSelection()
257 v.Cursor.StoreVisualX()
259 tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
260 tabmovement := v.Buf.Settings["tabmovement"].(bool)
261 if tabstospaces && tabmovement {
262 tabsize := int(v.Buf.Settings["tabsize"].(float64))
263 line := v.Buf.Line(v.Cursor.Y)
264 if v.Cursor.X+tabsize < Count(line) && line[v.Cursor.X:v.Cursor.X+tabsize] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X]) {
265 for i := 0; i < tabsize; i++ {
277 return PostActionCall("CursorRight", v)
282 // WordRight moves the cursor one word to the right
283 func (v *View) WordRight(usePlugin bool) bool {
284 if usePlugin && !PreActionCall("WordRight", v) {
291 return PostActionCall("WordRight", v)
296 // WordLeft moves the cursor one word to the left
297 func (v *View) WordLeft(usePlugin bool) bool {
298 if usePlugin && !PreActionCall("WordLeft", v) {
305 return PostActionCall("WordLeft", v)
310 // SelectUp selects up one line
311 func (v *View) SelectUp(usePlugin bool) bool {
312 if usePlugin && !PreActionCall("SelectUp", v) {
316 if !v.Cursor.HasSelection() {
317 v.Cursor.OrigSelection[0] = v.Cursor.Loc
320 v.Cursor.SelectTo(v.Cursor.Loc)
323 return PostActionCall("SelectUp", v)
328 // SelectDown selects down one line
329 func (v *View) SelectDown(usePlugin bool) bool {
330 if usePlugin && !PreActionCall("SelectDown", v) {
334 if !v.Cursor.HasSelection() {
335 v.Cursor.OrigSelection[0] = v.Cursor.Loc
338 v.Cursor.SelectTo(v.Cursor.Loc)
341 return PostActionCall("SelectDown", v)
346 // SelectLeft selects the character to the left of the cursor
347 func (v *View) SelectLeft(usePlugin bool) bool {
348 if usePlugin && !PreActionCall("SelectLeft", v) {
354 if loc.GreaterThan(count) {
357 if !v.Cursor.HasSelection() {
358 v.Cursor.OrigSelection[0] = loc
361 v.Cursor.SelectTo(v.Cursor.Loc)
364 return PostActionCall("SelectLeft", v)
369 // SelectRight selects the character to the right of the cursor
370 func (v *View) SelectRight(usePlugin bool) bool {
371 if usePlugin && !PreActionCall("SelectRight", v) {
377 if loc.GreaterThan(count) {
380 if !v.Cursor.HasSelection() {
381 v.Cursor.OrigSelection[0] = loc
384 v.Cursor.SelectTo(v.Cursor.Loc)
387 return PostActionCall("SelectRight", v)
392 // SelectWordRight selects the word to the right of the cursor
393 func (v *View) SelectWordRight(usePlugin bool) bool {
394 if usePlugin && !PreActionCall("SelectWordRight", v) {
398 if !v.Cursor.HasSelection() {
399 v.Cursor.OrigSelection[0] = v.Cursor.Loc
402 v.Cursor.SelectTo(v.Cursor.Loc)
405 return PostActionCall("SelectWordRight", v)
410 // SelectWordLeft selects the word to the left of the cursor
411 func (v *View) SelectWordLeft(usePlugin bool) bool {
412 if usePlugin && !PreActionCall("SelectWordLeft", v) {
416 if !v.Cursor.HasSelection() {
417 v.Cursor.OrigSelection[0] = v.Cursor.Loc
420 v.Cursor.SelectTo(v.Cursor.Loc)
423 return PostActionCall("SelectWordLeft", v)
428 // StartOfLine moves the cursor to the start of the line
429 func (v *View) StartOfLine(usePlugin bool) bool {
430 if usePlugin && !PreActionCall("StartOfLine", v) {
439 return PostActionCall("StartOfLine", v)
444 // EndOfLine moves the cursor to the end of the line
445 func (v *View) EndOfLine(usePlugin bool) bool {
446 if usePlugin && !PreActionCall("EndOfLine", v) {
455 return PostActionCall("EndOfLine", v)
460 // SelectToStartOfLine selects to the start of the current line
461 func (v *View) SelectToStartOfLine(usePlugin bool) bool {
462 if usePlugin && !PreActionCall("SelectToStartOfLine", v) {
466 if !v.Cursor.HasSelection() {
467 v.Cursor.OrigSelection[0] = v.Cursor.Loc
470 v.Cursor.SelectTo(v.Cursor.Loc)
473 return PostActionCall("SelectToStartOfLine", v)
478 // SelectToEndOfLine selects to the end of the current line
479 func (v *View) SelectToEndOfLine(usePlugin bool) bool {
480 if usePlugin && !PreActionCall("SelectToEndOfLine", v) {
484 if !v.Cursor.HasSelection() {
485 v.Cursor.OrigSelection[0] = v.Cursor.Loc
488 v.Cursor.SelectTo(v.Cursor.Loc)
491 return PostActionCall("SelectToEndOfLine", v)
496 // CursorStart moves the cursor to the start of the buffer
497 func (v *View) CursorStart(usePlugin bool) bool {
498 if usePlugin && !PreActionCall("CursorStart", v) {
508 return PostActionCall("CursorStart", v)
513 // CursorEnd moves the cursor to the end of the buffer
514 func (v *View) CursorEnd(usePlugin bool) bool {
515 if usePlugin && !PreActionCall("CursorEnd", v) {
521 v.Cursor.Loc = v.Buf.End()
522 v.Cursor.StoreVisualX()
525 return PostActionCall("CursorEnd", v)
530 // SelectToStart selects the text from the cursor to the start of the buffer
531 func (v *View) SelectToStart(usePlugin bool) bool {
532 if usePlugin && !PreActionCall("SelectToStart", v) {
536 if !v.Cursor.HasSelection() {
537 v.Cursor.OrigSelection[0] = v.Cursor.Loc
540 v.Cursor.SelectTo(v.Buf.Start())
543 return PostActionCall("SelectToStart", v)
548 // SelectToEnd selects the text from the cursor to the end of the buffer
549 func (v *View) SelectToEnd(usePlugin bool) bool {
550 if usePlugin && !PreActionCall("SelectToEnd", v) {
554 if !v.Cursor.HasSelection() {
555 v.Cursor.OrigSelection[0] = v.Cursor.Loc
558 v.Cursor.SelectTo(v.Buf.End())
561 return PostActionCall("SelectToEnd", v)
566 // InsertSpace inserts a space
567 func (v *View) InsertSpace(usePlugin bool) bool {
568 if usePlugin && !PreActionCall("InsertSpace", v) {
572 if v.Cursor.HasSelection() {
573 v.Cursor.DeleteSelection()
574 v.Cursor.ResetSelection()
576 v.Buf.Insert(v.Cursor.Loc, " ")
580 return PostActionCall("InsertSpace", v)
585 // InsertNewline inserts a newline plus possible some whitespace if autoindent is on
586 func (v *View) InsertNewline(usePlugin bool) bool {
587 if usePlugin && !PreActionCall("InsertNewline", v) {
592 if v.Cursor.HasSelection() {
593 v.Cursor.DeleteSelection()
594 v.Cursor.ResetSelection()
597 ws := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
598 v.Buf.Insert(v.Cursor.Loc, "\n")
601 if v.Buf.Settings["autoindent"].(bool) {
602 v.Buf.Insert(v.Cursor.Loc, ws)
603 // for i := 0; i < len(ws); i++ {
607 // Remove the whitespaces if keepautoindent setting is off
608 if IsSpacesOrTabs(v.Buf.Line(v.Cursor.Y-1)) && !v.Buf.Settings["keepautoindent"].(bool) {
609 line := v.Buf.Line(v.Cursor.Y - 1)
610 v.Buf.Remove(Loc{0, v.Cursor.Y - 1}, Loc{Count(line), v.Cursor.Y - 1})
613 v.Cursor.LastVisualX = v.Cursor.GetVisualX()
616 return PostActionCall("InsertNewline", v)
621 // Backspace deletes the previous character
622 func (v *View) Backspace(usePlugin bool) bool {
623 if usePlugin && !PreActionCall("Backspace", v) {
627 // Delete a character
628 if v.Cursor.HasSelection() {
629 v.Cursor.DeleteSelection()
630 v.Cursor.ResetSelection()
631 } else if v.Cursor.Loc.GreaterThan(v.Buf.Start()) {
632 // We have to do something a bit hacky here because we want to
633 // delete the line by first moving left and then deleting backwards
634 // but the undo redo would place the cursor in the wrong place
635 // So instead we move left, save the position, move back, delete
636 // and restore the position
638 // If the user is using spaces instead of tabs and they are deleting
639 // whitespace at the start of the line, we should delete as if it's a
640 // tab (tabSize number of spaces)
641 lineStart := v.Buf.Line(v.Cursor.Y)[:v.Cursor.X]
642 tabSize := int(v.Buf.Settings["tabsize"].(float64))
643 if v.Buf.Settings["tabstospaces"].(bool) && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%tabSize == 0 {
645 v.Buf.Remove(loc.Move(-tabSize, v.Buf), loc)
648 v.Buf.Remove(loc.Move(-1, v.Buf), loc)
651 v.Cursor.LastVisualX = v.Cursor.GetVisualX()
654 return PostActionCall("Backspace", v)
659 // DeleteWordRight deletes the word to the right of the cursor
660 func (v *View) DeleteWordRight(usePlugin bool) bool {
661 if usePlugin && !PreActionCall("DeleteWordRight", v) {
665 v.SelectWordRight(false)
666 if v.Cursor.HasSelection() {
667 v.Cursor.DeleteSelection()
668 v.Cursor.ResetSelection()
672 return PostActionCall("DeleteWordRight", v)
677 // DeleteWordLeft deletes the word to the left of the cursor
678 func (v *View) DeleteWordLeft(usePlugin bool) bool {
679 if usePlugin && !PreActionCall("DeleteWordLeft", v) {
683 v.SelectWordLeft(false)
684 if v.Cursor.HasSelection() {
685 v.Cursor.DeleteSelection()
686 v.Cursor.ResetSelection()
690 return PostActionCall("DeleteWordLeft", v)
695 // Delete deletes the next character
696 func (v *View) Delete(usePlugin bool) bool {
697 if usePlugin && !PreActionCall("Delete", v) {
701 if v.Cursor.HasSelection() {
702 v.Cursor.DeleteSelection()
703 v.Cursor.ResetSelection()
706 if loc.LessThan(v.Buf.End()) {
707 v.Buf.Remove(loc, loc.Move(1, v.Buf))
712 return PostActionCall("Delete", v)
717 // IndentSelection indents the current selection
718 func (v *View) IndentSelection(usePlugin bool) bool {
719 if usePlugin && !PreActionCall("IndentSelection", v) {
723 if v.Cursor.HasSelection() {
724 start := v.Cursor.CurSelection[0]
725 end := v.Cursor.CurSelection[1]
727 start, end = end, start
731 endY := end.Move(-1, v.Buf).Y
732 endX := end.Move(-1, v.Buf).X
733 for y := startY; y <= endY; y++ {
734 tabsize := len(v.Buf.IndentString())
735 v.Buf.Insert(Loc{0, y}, v.Buf.IndentString())
736 if y == startY && start.X > 0 {
737 v.Cursor.SetSelectionStart(start.Move(tabsize, v.Buf))
740 v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, endY})
746 return PostActionCall("IndentSelection", v)
753 // OutdentLine moves the current line back one indentation
754 func (v *View) OutdentLine(usePlugin bool) bool {
755 if usePlugin && !PreActionCall("OutdentLine", v) {
759 if v.Cursor.HasSelection() {
763 for x := 0; x < len(v.Buf.IndentString()); x++ {
764 if len(GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))) == 0 {
767 v.Buf.Remove(Loc{0, v.Cursor.Y}, Loc{1, v.Cursor.Y})
772 return PostActionCall("OutdentLine", v)
777 // OutdentSelection takes the current selection and moves it back one indent level
778 func (v *View) OutdentSelection(usePlugin bool) bool {
779 if usePlugin && !PreActionCall("OutdentSelection", v) {
783 if v.Cursor.HasSelection() {
784 start := v.Cursor.CurSelection[0]
785 end := v.Cursor.CurSelection[1]
787 start, end = end, start
791 endY := end.Move(-1, v.Buf).Y
792 for y := startY; y <= endY; y++ {
793 for x := 0; x < len(v.Buf.IndentString()); x++ {
794 if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 {
797 v.Buf.Remove(Loc{0, y}, Loc{1, y})
803 return PostActionCall("OutdentSelection", v)
810 // InsertTab inserts a tab or spaces
811 func (v *View) InsertTab(usePlugin bool) bool {
812 if usePlugin && !PreActionCall("InsertTab", v) {
816 if v.Cursor.HasSelection() {
820 tabBytes := len(v.Buf.IndentString())
821 bytesUntilIndent := tabBytes - (v.Cursor.GetVisualX() % tabBytes)
822 v.Buf.Insert(v.Cursor.Loc, v.Buf.IndentString()[:bytesUntilIndent])
823 // for i := 0; i < bytesUntilIndent; i++ {
828 return PostActionCall("InsertTab", v)
833 // SaveAll saves all open buffers
834 func (v *View) SaveAll(usePlugin bool) bool {
836 if usePlugin && !PreActionCall("SaveAll", v) {
840 for _, t := range tabs {
841 for _, v := range t.views {
847 return PostActionCall("SaveAll", v)
853 // Save the buffer to disk
854 func (v *View) Save(usePlugin bool) bool {
856 if usePlugin && !PreActionCall("Save", v) {
860 if v.Type.scratch == true {
861 // We can't save any view type with scratch set. eg help and log text
864 // If this is an empty buffer, ask for a filename
865 if v.Buf.Path == "" {
868 v.saveToFile(v.Buf.Path)
872 return PostActionCall("Save", v)
878 // This function saves the buffer to `filename` and changes the buffer's path and name
879 // to `filename` if the save is successful
880 func (v *View) saveToFile(filename string) {
881 err := v.Buf.SaveAs(filename)
883 if strings.HasSuffix(err.Error(), "permission denied") {
884 choice, _ := messenger.YesNoPrompt("Permission denied. Do you want to save this file using sudo? (y,n)")
886 err = v.Buf.SaveAsWithSudo(filename)
888 messenger.Error(err.Error())
890 v.Buf.Path = filename
891 v.Buf.name = filename
892 messenger.Message("Saved " + filename)
898 messenger.Error(err.Error())
901 v.Buf.Path = filename
902 v.Buf.name = filename
903 messenger.Message("Saved " + filename)
907 // SaveAs saves the buffer to disk with the given name
908 func (v *View) SaveAs(usePlugin bool) bool {
910 if usePlugin && !PreActionCall("Find", v) {
914 filename, canceled := messenger.Prompt("Filename: ", "", "Save", NoCompletion)
916 // the filename might or might not be quoted, so unquote first then join the strings.
917 filename = strings.Join(SplitCommandArgs(filename), " ")
918 v.saveToFile(filename)
922 PostActionCall("Find", v)
928 // Find opens a prompt and searches forward for the input
929 func (v *View) Find(usePlugin bool) bool {
931 if usePlugin && !PreActionCall("Find", v) {
936 if v.Cursor.HasSelection() {
937 searchStart = v.Cursor.CurSelection[1]
938 searchStart = v.Cursor.CurSelection[1]
939 searchStr = v.Cursor.GetSelection()
941 searchStart = v.Cursor.Loc
943 BeginSearch(searchStr)
946 return PostActionCall("Find", v)
952 // FindNext searches forwards for the last used search term
953 func (v *View) FindNext(usePlugin bool) bool {
954 if usePlugin && !PreActionCall("FindNext", v) {
958 if v.Cursor.HasSelection() {
959 searchStart = v.Cursor.CurSelection[1]
960 // lastSearch = v.Cursor.GetSelection()
962 searchStart = v.Cursor.Loc
964 if lastSearch == "" {
967 messenger.Message("Finding: " + lastSearch)
968 Search(lastSearch, v, true)
971 return PostActionCall("FindNext", v)
976 // FindPrevious searches backwards for the last used search term
977 func (v *View) FindPrevious(usePlugin bool) bool {
978 if usePlugin && !PreActionCall("FindPrevious", v) {
982 if v.Cursor.HasSelection() {
983 searchStart = v.Cursor.CurSelection[0]
985 searchStart = v.Cursor.Loc
987 messenger.Message("Finding: " + lastSearch)
988 Search(lastSearch, v, false)
991 return PostActionCall("FindPrevious", v)
996 // Undo undoes the last action
997 func (v *View) Undo(usePlugin bool) bool {
998 if usePlugin && !PreActionCall("Undo", v) {
1002 if v.Buf.curCursor == 0 {
1003 v.Buf.clearCursors()
1007 messenger.Message("Undid action")
1010 return PostActionCall("Undo", v)
1015 // Redo redoes the last action
1016 func (v *View) Redo(usePlugin bool) bool {
1017 if usePlugin && !PreActionCall("Redo", v) {
1021 if v.Buf.curCursor == 0 {
1022 v.Buf.clearCursors()
1026 messenger.Message("Redid action")
1029 return PostActionCall("Redo", v)
1034 // Copy the selection to the system clipboard
1035 func (v *View) Copy(usePlugin bool) bool {
1037 if usePlugin && !PreActionCall("Copy", v) {
1041 if v.Cursor.HasSelection() {
1042 v.Cursor.CopySelection("clipboard")
1044 messenger.Message("Copied selection")
1048 return PostActionCall("Copy", v)
1054 // CutLine cuts the current line to the clipboard
1055 func (v *View) CutLine(usePlugin bool) bool {
1056 if usePlugin && !PreActionCall("CutLine", v) {
1060 v.Cursor.SelectLine()
1061 if !v.Cursor.HasSelection() {
1064 if v.freshClip == true {
1065 if v.Cursor.HasSelection() {
1066 if clip, err := clipboard.ReadAll("clipboard"); err != nil {
1067 messenger.Error(err)
1069 clipboard.WriteAll(clip+v.Cursor.GetSelection(), "clipboard")
1072 } else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
1076 v.lastCutTime = time.Now()
1077 v.Cursor.DeleteSelection()
1078 v.Cursor.ResetSelection()
1079 messenger.Message("Cut line")
1082 return PostActionCall("CutLine", v)
1087 // Cut the selection to the system clipboard
1088 func (v *View) Cut(usePlugin bool) bool {
1089 if usePlugin && !PreActionCall("Cut", v) {
1093 if v.Cursor.HasSelection() {
1094 v.Cursor.CopySelection("clipboard")
1095 v.Cursor.DeleteSelection()
1096 v.Cursor.ResetSelection()
1098 messenger.Message("Cut selection")
1101 return PostActionCall("Cut", v)
1109 // DuplicateLine duplicates the current line or selection
1110 func (v *View) DuplicateLine(usePlugin bool) bool {
1111 if usePlugin && !PreActionCall("DuplicateLine", v) {
1115 if v.Cursor.HasSelection() {
1116 v.Buf.Insert(v.Cursor.CurSelection[1], v.Cursor.GetSelection())
1119 v.Buf.Insert(v.Cursor.Loc, "\n"+v.Buf.Line(v.Cursor.Y))
1123 messenger.Message("Duplicated line")
1126 return PostActionCall("DuplicateLine", v)
1131 // DeleteLine deletes the current line
1132 func (v *View) DeleteLine(usePlugin bool) bool {
1133 if usePlugin && !PreActionCall("DeleteLine", v) {
1137 v.Cursor.SelectLine()
1138 if !v.Cursor.HasSelection() {
1141 v.Cursor.DeleteSelection()
1142 v.Cursor.ResetSelection()
1143 messenger.Message("Deleted line")
1146 return PostActionCall("DeleteLine", v)
1151 // MoveLinesUp moves up the current line or selected lines if any
1152 func (v *View) MoveLinesUp(usePlugin bool) bool {
1153 if usePlugin && !PreActionCall("MoveLinesUp", v) {
1157 if v.Cursor.HasSelection() {
1158 if v.Cursor.CurSelection[0].Y == 0 {
1159 messenger.Message("Can not move further up")
1162 start := v.Cursor.CurSelection[0].Y
1163 end := v.Cursor.CurSelection[1].Y
1165 end, start = start, end
1172 v.Cursor.CurSelection[1].Y -= 1
1173 messenger.Message("Moved up selected line(s)")
1175 if v.Cursor.Loc.Y == 0 {
1176 messenger.Message("Can not move further up")
1183 messenger.Message("Moved up current line")
1185 v.Buf.IsModified = true
1188 return PostActionCall("MoveLinesUp", v)
1193 // MoveLinesDown moves down the current line or selected lines if any
1194 func (v *View) MoveLinesDown(usePlugin bool) bool {
1195 if usePlugin && !PreActionCall("MoveLinesDown", v) {
1199 if v.Cursor.HasSelection() {
1200 if v.Cursor.CurSelection[1].Y >= len(v.Buf.lines) {
1201 messenger.Message("Can not move further down")
1204 start := v.Cursor.CurSelection[0].Y
1205 end := v.Cursor.CurSelection[1].Y
1207 end, start = start, end
1210 v.Buf.MoveLinesDown(
1214 messenger.Message("Moved down selected line(s)")
1216 if v.Cursor.Loc.Y >= len(v.Buf.lines)-1 {
1217 messenger.Message("Can not move further down")
1220 v.Buf.MoveLinesDown(
1224 messenger.Message("Moved down current line")
1226 v.Buf.IsModified = true
1229 return PostActionCall("MoveLinesDown", v)
1234 // Paste whatever is in the system clipboard into the buffer
1235 // Delete and paste if the user has a selection
1236 func (v *View) Paste(usePlugin bool) bool {
1237 if usePlugin && !PreActionCall("Paste", v) {
1241 clip, _ := clipboard.ReadAll("clipboard")
1245 return PostActionCall("Paste", v)
1250 // PastePrimary pastes from the primary clipboard (only use on linux)
1251 func (v *View) PastePrimary(usePlugin bool) bool {
1252 if usePlugin && !PreActionCall("Paste", v) {
1256 clip, _ := clipboard.ReadAll("primary")
1260 return PostActionCall("Paste", v)
1265 // SelectAll selects the entire buffer
1266 func (v *View) SelectAll(usePlugin bool) bool {
1267 if usePlugin && !PreActionCall("SelectAll", v) {
1271 v.Cursor.SetSelectionStart(v.Buf.Start())
1272 v.Cursor.SetSelectionEnd(v.Buf.End())
1273 // Put the cursor at the beginning
1278 return PostActionCall("SelectAll", v)
1283 // OpenFile opens a new file in the buffer
1284 func (v *View) OpenFile(usePlugin bool) bool {
1286 if usePlugin && !PreActionCall("OpenFile", v) {
1291 input, canceled := messenger.Prompt("> ", "open ", "Open", CommandCompletion)
1293 HandleCommand(input)
1295 return PostActionCall("OpenFile", v)
1303 // Start moves the viewport to the start of the buffer
1304 func (v *View) Start(usePlugin bool) bool {
1306 if usePlugin && !PreActionCall("Start", v) {
1313 return PostActionCall("Start", v)
1319 // End moves the viewport to the end of the buffer
1320 func (v *View) End(usePlugin bool) bool {
1322 if usePlugin && !PreActionCall("End", v) {
1326 if v.Height > v.Buf.NumLines {
1329 v.Topline = v.Buf.NumLines - v.Height
1333 return PostActionCall("End", v)
1339 // PageUp scrolls the view up a page
1340 func (v *View) PageUp(usePlugin bool) bool {
1342 if usePlugin && !PreActionCall("PageUp", v) {
1346 if v.Topline > v.Height {
1347 v.ScrollUp(v.Height)
1353 return PostActionCall("PageUp", v)
1359 // PageDown scrolls the view down a page
1360 func (v *View) PageDown(usePlugin bool) bool {
1362 if usePlugin && !PreActionCall("PageDown", v) {
1366 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height {
1367 v.ScrollDown(v.Height)
1368 } else if v.Buf.NumLines >= v.Height {
1369 v.Topline = v.Buf.NumLines - v.Height
1373 return PostActionCall("PageDown", v)
1379 // CursorPageUp places the cursor a page up
1380 func (v *View) CursorPageUp(usePlugin bool) bool {
1381 if usePlugin && !PreActionCall("CursorPageUp", v) {
1387 if v.Cursor.HasSelection() {
1388 v.Cursor.Loc = v.Cursor.CurSelection[0]
1389 v.Cursor.ResetSelection()
1390 v.Cursor.StoreVisualX()
1392 v.Cursor.UpN(v.Height)
1395 return PostActionCall("CursorPageUp", v)
1400 // CursorPageDown places the cursor a page up
1401 func (v *View) CursorPageDown(usePlugin bool) bool {
1402 if usePlugin && !PreActionCall("CursorPageDown", v) {
1408 if v.Cursor.HasSelection() {
1409 v.Cursor.Loc = v.Cursor.CurSelection[1]
1410 v.Cursor.ResetSelection()
1411 v.Cursor.StoreVisualX()
1413 v.Cursor.DownN(v.Height)
1416 return PostActionCall("CursorPageDown", v)
1421 // HalfPageUp scrolls the view up half a page
1422 func (v *View) HalfPageUp(usePlugin bool) bool {
1424 if usePlugin && !PreActionCall("HalfPageUp", v) {
1428 if v.Topline > v.Height/2 {
1429 v.ScrollUp(v.Height / 2)
1435 return PostActionCall("HalfPageUp", v)
1441 // HalfPageDown scrolls the view down half a page
1442 func (v *View) HalfPageDown(usePlugin bool) bool {
1444 if usePlugin && !PreActionCall("HalfPageDown", v) {
1448 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height/2 {
1449 v.ScrollDown(v.Height / 2)
1451 if v.Buf.NumLines >= v.Height {
1452 v.Topline = v.Buf.NumLines - v.Height
1457 return PostActionCall("HalfPageDown", v)
1463 // ToggleRuler turns line numbers off and on
1464 func (v *View) ToggleRuler(usePlugin bool) bool {
1466 if usePlugin && !PreActionCall("ToggleRuler", v) {
1470 if v.Buf.Settings["ruler"] == false {
1471 v.Buf.Settings["ruler"] = true
1472 messenger.Message("Enabled ruler")
1474 v.Buf.Settings["ruler"] = false
1475 messenger.Message("Disabled ruler")
1479 return PostActionCall("ToggleRuler", v)
1485 // JumpLine jumps to a line and moves the view accordingly.
1486 func (v *View) JumpLine(usePlugin bool) bool {
1487 if usePlugin && !PreActionCall("JumpLine", v) {
1491 // Prompt for line number
1492 message := fmt.Sprintf("Jump to line (1 - %v) # ", v.Buf.NumLines)
1493 linestring, canceled := messenger.Prompt(message, "", "LineNumber", NoCompletion)
1497 lineint, err := strconv.Atoi(linestring)
1498 lineint = lineint - 1 // fix offset
1500 messenger.Error(err) // return errors
1503 // Move cursor and view if possible.
1504 if lineint < v.Buf.NumLines && lineint >= 0 {
1506 v.Cursor.Y = lineint
1509 return PostActionCall("JumpLine", v)
1513 messenger.Error("Only ", v.Buf.NumLines, " lines to jump")
1517 // ClearStatus clears the messenger bar
1518 func (v *View) ClearStatus(usePlugin bool) bool {
1520 if usePlugin && !PreActionCall("ClearStatus", v) {
1524 messenger.Message("")
1527 return PostActionCall("ClearStatus", v)
1533 // ToggleHelp toggles the help screen
1534 func (v *View) ToggleHelp(usePlugin bool) bool {
1536 if usePlugin && !PreActionCall("ToggleHelp", v) {
1540 if v.Type != vtHelp {
1541 // Open the default help
1548 return PostActionCall("ToggleHelp", v)
1554 // ShellMode opens a terminal to run a shell command
1555 func (v *View) ShellMode(usePlugin bool) bool {
1557 if usePlugin && !PreActionCall("ShellMode", v) {
1561 input, canceled := messenger.Prompt("$ ", "", "Shell", NoCompletion)
1563 // The true here is for openTerm to make the command interactive
1564 HandleShellCommand(input, true, true)
1566 return PostActionCall("ShellMode", v)
1573 // CommandMode lets the user enter a command
1574 func (v *View) CommandMode(usePlugin bool) bool {
1576 if usePlugin && !PreActionCall("CommandMode", v) {
1580 input, canceled := messenger.Prompt("> ", "", "Command", CommandCompletion)
1582 HandleCommand(input)
1584 return PostActionCall("CommandMode", v)
1592 // Escape leaves current mode
1593 func (v *View) Escape(usePlugin bool) bool {
1595 // check if user is searching, or the last search is still active
1596 if searching || lastSearch != "" {
1600 // check if a prompt is shown, hide it and don't quit
1601 if messenger.hasPrompt {
1602 messenger.Reset() // FIXME
1610 // Quit this will close the current tab or view that is open
1611 func (v *View) Quit(usePlugin bool) bool {
1613 if usePlugin && !PreActionCall("Quit", v) {
1617 // Make sure not to quit if there are unsaved changes
1620 if len(tabs[curTab].views) > 1 {
1621 v.splitNode.Delete()
1622 tabs[v.TabNum].Cleanup()
1623 tabs[v.TabNum].Resize()
1624 } else if len(tabs) > 1 {
1625 if len(tabs[v.TabNum].views) == 1 {
1626 tabs = tabs[:v.TabNum+copy(tabs[v.TabNum:], tabs[v.TabNum+1:])]
1627 for i, t := range tabs {
1630 if curTab >= len(tabs) {
1634 CurView().ToggleTabbar()
1639 PostActionCall("Quit", v)
1648 return PostActionCall("Quit", v)
1654 // QuitAll quits the whole editor; all splits and tabs
1655 func (v *View) QuitAll(usePlugin bool) bool {
1657 if usePlugin && !PreActionCall("QuitAll", v) {
1662 for _, tab := range tabs {
1663 for _, v := range tab.views {
1671 // only quit if all of the buffers can be closed and the user confirms that they actually want to quit everything
1672 shouldQuit, _ := messenger.YesNoPrompt("Do you want to quit micro (all open files will be closed)?")
1675 for _, tab := range tabs {
1676 for _, v := range tab.views {
1682 PostActionCall("QuitAll", v)
1694 // AddTab adds a new tab with an empty buffer
1695 func (v *View) AddTab(usePlugin bool) bool {
1697 if usePlugin && !PreActionCall("AddTab", v) {
1701 tab := NewTabFromView(NewView(NewBufferFromString("", "")))
1702 tab.SetNum(len(tabs))
1703 tabs = append(tabs, tab)
1704 curTab = len(tabs) - 1
1706 for _, t := range tabs {
1707 for _, v := range t.views {
1714 return PostActionCall("AddTab", v)
1720 // PreviousTab switches to the previous tab in the tab list
1721 func (v *View) PreviousTab(usePlugin bool) bool {
1723 if usePlugin && !PreActionCall("PreviousTab", v) {
1729 } else if curTab == 0 {
1730 curTab = len(tabs) - 1
1734 return PostActionCall("PreviousTab", v)
1740 // NextTab switches to the next tab in the tab list
1741 func (v *View) NextTab(usePlugin bool) bool {
1743 if usePlugin && !PreActionCall("NextTab", v) {
1747 if curTab < len(tabs)-1 {
1749 } else if curTab == len(tabs)-1 {
1754 return PostActionCall("NextTab", v)
1760 // VSplitBinding opens an empty vertical split
1761 func (v *View) VSplitBinding(usePlugin bool) bool {
1763 if usePlugin && !PreActionCall("VSplit", v) {
1767 v.VSplit(NewBufferFromString("", ""))
1770 return PostActionCall("VSplit", v)
1776 // HSplitBinding opens an empty horizontal split
1777 func (v *View) HSplitBinding(usePlugin bool) bool {
1779 if usePlugin && !PreActionCall("HSplit", v) {
1783 v.HSplit(NewBufferFromString("", ""))
1786 return PostActionCall("HSplit", v)
1792 // Unsplit closes all splits in the current tab except the active one
1793 func (v *View) Unsplit(usePlugin bool) bool {
1795 if usePlugin && !PreActionCall("Unsplit", v) {
1799 curView := tabs[curTab].CurView
1800 for i := len(tabs[curTab].views) - 1; i >= 0; i-- {
1801 view := tabs[curTab].views[i]
1802 if view != nil && view.Num != curView {
1804 // messenger.Message("Quit ", view.Buf.Path)
1809 return PostActionCall("Unsplit", v)
1815 // NextSplit changes the view to the next split
1816 func (v *View) NextSplit(usePlugin bool) bool {
1818 if usePlugin && !PreActionCall("NextSplit", v) {
1823 if tab.CurView < len(tab.views)-1 {
1830 return PostActionCall("NextSplit", v)
1836 // PreviousSplit changes the view to the previous split
1837 func (v *View) PreviousSplit(usePlugin bool) bool {
1839 if usePlugin && !PreActionCall("PreviousSplit", v) {
1844 if tab.CurView > 0 {
1847 tab.CurView = len(tab.views) - 1
1851 return PostActionCall("PreviousSplit", v)
1857 var curMacro []interface{}
1858 var recordingMacro bool
1860 // ToggleMacro toggles recording of a macro
1861 func (v *View) ToggleMacro(usePlugin bool) bool {
1863 if usePlugin && !PreActionCall("ToggleMacro", v) {
1867 recordingMacro = !recordingMacro
1870 curMacro = []interface{}{}
1871 messenger.Message("Recording")
1873 messenger.Message("Stopped recording")
1877 return PostActionCall("ToggleMacro", v)
1883 // PlayMacro plays back the most recently recorded macro
1884 func (v *View) PlayMacro(usePlugin bool) bool {
1885 if usePlugin && !PreActionCall("PlayMacro", v) {
1889 for _, action := range curMacro {
1890 switch t := action.(type) {
1892 // Insert a character
1893 if v.Cursor.HasSelection() {
1894 v.Cursor.DeleteSelection()
1895 v.Cursor.ResetSelection()
1897 v.Buf.Insert(v.Cursor.Loc, string(t))
1900 for pl := range loadedPlugins {
1901 _, err := Call(pl+".onRune", string(t), v)
1902 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
1906 case func(*View, bool) bool:
1912 return PostActionCall("PlayMacro", v)
1917 // SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
1918 func (v *View) SpawnMultiCursor(usePlugin bool) bool {
1919 spawner := v.Buf.cursors[len(v.Buf.cursors)-1]
1920 // You can only spawn a cursor from the main cursor
1921 if v.Cursor == spawner {
1922 if usePlugin && !PreActionCall("SpawnMultiCursor", v) {
1926 if !spawner.HasSelection() {
1927 spawner.SelectWord()
1933 sel := spawner.GetSelection()
1935 searchStart = spawner.CurSelection[1]
1937 Search(regexp.QuoteMeta(sel), v, true)
1939 for _, cur := range v.Buf.cursors {
1940 if c.Loc == cur.Loc {
1944 v.Buf.cursors = append(v.Buf.cursors, c)
1945 v.Buf.UpdateCursors()
1951 PostActionCall("SpawnMultiCursor", v)
1958 // MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
1959 func (v *View) MouseMultiCursor(usePlugin bool, e *tcell.EventMouse) bool {
1960 if v.Cursor == &v.Buf.Cursor {
1961 if usePlugin && !PreActionCall("SpawnMultiCursorAtMouse", v, e) {
1964 x, y := e.Position()
1965 x -= v.lineNumOffset - v.leftCol + v.x
1966 y += v.Topline - v.y
1972 v.MoveToMouseClick(x, y)
1974 v.Cursor = &v.Buf.Cursor
1976 v.Buf.cursors = append(v.Buf.cursors, c)
1977 v.Buf.MergeCursors()
1978 v.Buf.UpdateCursors()
1981 PostActionCall("SpawnMultiCursorAtMouse", v)
1987 // SkipMultiCursor moves the current multiple cursor to the next available position
1988 func (v *View) SkipMultiCursor(usePlugin bool) bool {
1989 cursor := v.Buf.cursors[len(v.Buf.cursors)-1]
1992 if usePlugin && !PreActionCall("SkipMultiCursor", v) {
1995 sel := cursor.GetSelection()
1997 searchStart = cursor.CurSelection[1]
1999 Search(regexp.QuoteMeta(sel), v, true)
2004 PostActionCall("SkipMultiCursor", v)
2010 // RemoveMultiCursor removes the latest multiple cursor
2011 func (v *View) RemoveMultiCursor(usePlugin bool) bool {
2012 end := len(v.Buf.cursors)
2015 if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
2019 v.Buf.cursors[end-1] = nil
2020 v.Buf.cursors = v.Buf.cursors[:end-1]
2021 v.Buf.UpdateCursors()
2025 return PostActionCall("RemoveMultiCursor", v)
2030 v.RemoveAllMultiCursors(usePlugin)
2035 // RemoveAllMultiCursors removes all cursors except the base cursor
2036 func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
2038 if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
2042 v.Buf.clearCursors()
2046 return PostActionCall("RemoveAllMultiCursors", v)