10 "github.com/yuin/gopher-lua"
11 "github.com/zyedidia/clipboard"
12 "github.com/zyedidia/tcell"
15 // PreActionCall executes the lua pre callback if possible
16 func PreActionCall(funcName string, view *View, args ...interface{}) bool {
18 for pl := range loadedPlugins {
19 ret, err := Call(pl+".pre"+funcName, append([]interface{}{view}, args...)...)
20 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
24 if ret == lua.LFalse {
31 // PostActionCall executes the lua plugin callback if possible
32 func PostActionCall(funcName string, view *View, args ...interface{}) bool {
34 for pl := range loadedPlugins {
35 ret, err := Call(pl+".on"+funcName, append([]interface{}{view}, args...)...)
36 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
40 if ret == lua.LFalse {
47 func (v *View) deselect(index int) bool {
48 if v.Cursor.HasSelection() {
49 v.Cursor.Loc = v.Cursor.CurSelection[index]
50 v.Cursor.ResetSelection()
51 v.Cursor.StoreVisualX()
57 // MousePress is the event that should happen when a normal click happens
58 // This is almost always bound to left click
59 func (v *View) MousePress(usePlugin bool, e *tcell.EventMouse) bool {
60 if usePlugin && !PreActionCall("MousePress", v, e) {
65 x -= v.lineNumOffset - v.leftCol + v.x
68 // This is usually bound to left click
69 v.MoveToMouseClick(x, y)
71 if len(v.Buf.cursors) > 1 {
72 for i := 1; i < len(v.Buf.cursors); i++ {
73 v.Buf.cursors[i] = nil
75 v.Buf.cursors = v.Buf.cursors[:1]
77 v.Cursor.ResetSelection()
80 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
83 v.lastClickTime = time.Now()
89 v.Cursor.CopySelection("primary")
92 v.lastClickTime = time.Now()
98 v.Cursor.CopySelection("primary")
101 v.doubleClick = false
102 v.tripleClick = false
103 v.lastClickTime = time.Now()
105 v.Cursor.OrigSelection[0] = v.Cursor.Loc
106 v.Cursor.CurSelection[0] = v.Cursor.Loc
107 v.Cursor.CurSelection[1] = v.Cursor.Loc
109 v.mouseReleased = false
110 } else if !v.mouseReleased {
112 v.Cursor.AddLineToSelection()
113 } else if v.doubleClick {
114 v.Cursor.AddWordToSelection()
116 v.Cursor.SetSelectionEnd(v.Cursor.Loc)
117 v.Cursor.CopySelection("primary")
122 PostActionCall("MousePress", v, e)
127 // ScrollUpAction scrolls the view up
128 func (v *View) ScrollUpAction(usePlugin bool) bool {
130 if usePlugin && !PreActionCall("ScrollUp", v) {
134 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
135 v.ScrollUp(scrollspeed)
138 PostActionCall("ScrollUp", v)
144 // ScrollDownAction scrolls the view up
145 func (v *View) ScrollDownAction(usePlugin bool) bool {
147 if usePlugin && !PreActionCall("ScrollDown", v) {
151 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
152 v.ScrollDown(scrollspeed)
155 PostActionCall("ScrollDown", v)
161 // Center centers the view on the cursor
162 func (v *View) Center(usePlugin bool) bool {
163 if usePlugin && !PreActionCall("Center", v) {
167 v.Topline = v.Cursor.Y - v.Height/2
168 if v.Topline+v.Height > v.Buf.NumLines {
169 v.Topline = v.Buf.NumLines - v.Height
176 return PostActionCall("Center", v)
181 // CursorUp moves the cursor up
182 func (v *View) CursorUp(usePlugin bool) bool {
183 if usePlugin && !PreActionCall("CursorUp", v) {
191 return PostActionCall("CursorUp", v)
196 // CursorDown moves the cursor down
197 func (v *View) CursorDown(usePlugin bool) bool {
198 if usePlugin && !PreActionCall("CursorDown", v) {
206 return PostActionCall("CursorDown", v)
211 // CursorLeft moves the cursor left
212 func (v *View) CursorLeft(usePlugin bool) bool {
213 if usePlugin && !PreActionCall("CursorLeft", v) {
217 if v.Cursor.HasSelection() {
218 v.Cursor.Loc = v.Cursor.CurSelection[0]
219 v.Cursor.ResetSelection()
220 v.Cursor.StoreVisualX()
222 tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
223 tabmovement := v.Buf.Settings["tabmovement"].(bool)
224 if tabstospaces && tabmovement {
225 tabsize := int(v.Buf.Settings["tabsize"].(float64))
226 line := v.Buf.Line(v.Cursor.Y)
227 if v.Cursor.X-tabsize >= 0 && line[v.Cursor.X-tabsize:v.Cursor.X] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X-tabsize]) {
228 for i := 0; i < tabsize; i++ {
240 return PostActionCall("CursorLeft", v)
245 // CursorRight moves the cursor right
246 func (v *View) CursorRight(usePlugin bool) bool {
247 if usePlugin && !PreActionCall("CursorRight", v) {
251 if v.Cursor.HasSelection() {
252 v.Cursor.Loc = v.Cursor.CurSelection[1]
253 v.Cursor.ResetSelection()
254 v.Cursor.StoreVisualX()
256 tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
257 tabmovement := v.Buf.Settings["tabmovement"].(bool)
258 if tabstospaces && tabmovement {
259 tabsize := int(v.Buf.Settings["tabsize"].(float64))
260 line := v.Buf.Line(v.Cursor.Y)
261 if v.Cursor.X+tabsize < Count(line) && line[v.Cursor.X:v.Cursor.X+tabsize] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X]) {
262 for i := 0; i < tabsize; i++ {
274 return PostActionCall("CursorRight", v)
279 // WordRight moves the cursor one word to the right
280 func (v *View) WordRight(usePlugin bool) bool {
281 if usePlugin && !PreActionCall("WordRight", v) {
288 return PostActionCall("WordRight", v)
293 // WordLeft moves the cursor one word to the left
294 func (v *View) WordLeft(usePlugin bool) bool {
295 if usePlugin && !PreActionCall("WordLeft", v) {
302 return PostActionCall("WordLeft", v)
307 // SelectUp selects up one line
308 func (v *View) SelectUp(usePlugin bool) bool {
309 if usePlugin && !PreActionCall("SelectUp", v) {
313 if !v.Cursor.HasSelection() {
314 v.Cursor.OrigSelection[0] = v.Cursor.Loc
317 v.Cursor.SelectTo(v.Cursor.Loc)
320 return PostActionCall("SelectUp", v)
325 // SelectDown selects down one line
326 func (v *View) SelectDown(usePlugin bool) bool {
327 if usePlugin && !PreActionCall("SelectDown", v) {
331 if !v.Cursor.HasSelection() {
332 v.Cursor.OrigSelection[0] = v.Cursor.Loc
335 v.Cursor.SelectTo(v.Cursor.Loc)
338 return PostActionCall("SelectDown", v)
343 // SelectLeft selects the character to the left of the cursor
344 func (v *View) SelectLeft(usePlugin bool) bool {
345 if usePlugin && !PreActionCall("SelectLeft", v) {
351 if loc.GreaterThan(count) {
354 if !v.Cursor.HasSelection() {
355 v.Cursor.OrigSelection[0] = loc
358 v.Cursor.SelectTo(v.Cursor.Loc)
361 return PostActionCall("SelectLeft", v)
366 // SelectRight selects the character to the right of the cursor
367 func (v *View) SelectRight(usePlugin bool) bool {
368 if usePlugin && !PreActionCall("SelectRight", v) {
374 if loc.GreaterThan(count) {
377 if !v.Cursor.HasSelection() {
378 v.Cursor.OrigSelection[0] = loc
381 v.Cursor.SelectTo(v.Cursor.Loc)
384 return PostActionCall("SelectRight", v)
389 // SelectWordRight selects the word to the right of the cursor
390 func (v *View) SelectWordRight(usePlugin bool) bool {
391 if usePlugin && !PreActionCall("SelectWordRight", v) {
395 if !v.Cursor.HasSelection() {
396 v.Cursor.OrigSelection[0] = v.Cursor.Loc
399 v.Cursor.SelectTo(v.Cursor.Loc)
402 return PostActionCall("SelectWordRight", v)
407 // SelectWordLeft selects the word to the left of the cursor
408 func (v *View) SelectWordLeft(usePlugin bool) bool {
409 if usePlugin && !PreActionCall("SelectWordLeft", v) {
413 if !v.Cursor.HasSelection() {
414 v.Cursor.OrigSelection[0] = v.Cursor.Loc
417 v.Cursor.SelectTo(v.Cursor.Loc)
420 return PostActionCall("SelectWordLeft", v)
425 // StartOfLine moves the cursor to the start of the line
426 func (v *View) StartOfLine(usePlugin bool) bool {
427 if usePlugin && !PreActionCall("StartOfLine", v) {
436 return PostActionCall("StartOfLine", v)
441 // EndOfLine moves the cursor to the end of the line
442 func (v *View) EndOfLine(usePlugin bool) bool {
443 if usePlugin && !PreActionCall("EndOfLine", v) {
452 return PostActionCall("EndOfLine", v)
457 // SelectToStartOfLine selects to the start of the current line
458 func (v *View) SelectToStartOfLine(usePlugin bool) bool {
459 if usePlugin && !PreActionCall("SelectToStartOfLine", v) {
463 if !v.Cursor.HasSelection() {
464 v.Cursor.OrigSelection[0] = v.Cursor.Loc
467 v.Cursor.SelectTo(v.Cursor.Loc)
470 return PostActionCall("SelectToStartOfLine", v)
475 // SelectToEndOfLine selects to the end of the current line
476 func (v *View) SelectToEndOfLine(usePlugin bool) bool {
477 if usePlugin && !PreActionCall("SelectToEndOfLine", v) {
481 if !v.Cursor.HasSelection() {
482 v.Cursor.OrigSelection[0] = v.Cursor.Loc
485 v.Cursor.SelectTo(v.Cursor.Loc)
488 return PostActionCall("SelectToEndOfLine", v)
493 // CursorStart moves the cursor to the start of the buffer
494 func (v *View) CursorStart(usePlugin bool) bool {
495 if usePlugin && !PreActionCall("CursorStart", v) {
505 return PostActionCall("CursorStart", v)
510 // CursorEnd moves the cursor to the end of the buffer
511 func (v *View) CursorEnd(usePlugin bool) bool {
512 if usePlugin && !PreActionCall("CursorEnd", v) {
518 v.Cursor.Loc = v.Buf.End()
519 v.Cursor.StoreVisualX()
522 return PostActionCall("CursorEnd", v)
527 // SelectToStart selects the text from the cursor to the start of the buffer
528 func (v *View) SelectToStart(usePlugin bool) bool {
529 if usePlugin && !PreActionCall("SelectToStart", v) {
533 if !v.Cursor.HasSelection() {
534 v.Cursor.OrigSelection[0] = v.Cursor.Loc
537 v.Cursor.SelectTo(v.Buf.Start())
540 return PostActionCall("SelectToStart", v)
545 // SelectToEnd selects the text from the cursor to the end of the buffer
546 func (v *View) SelectToEnd(usePlugin bool) bool {
547 if usePlugin && !PreActionCall("SelectToEnd", v) {
551 if !v.Cursor.HasSelection() {
552 v.Cursor.OrigSelection[0] = v.Cursor.Loc
555 v.Cursor.SelectTo(v.Buf.End())
558 return PostActionCall("SelectToEnd", v)
563 // InsertSpace inserts a space
564 func (v *View) InsertSpace(usePlugin bool) bool {
565 if usePlugin && !PreActionCall("InsertSpace", v) {
569 if v.Cursor.HasSelection() {
570 v.Cursor.DeleteSelection()
571 v.Cursor.ResetSelection()
573 v.Buf.Insert(v.Cursor.Loc, " ")
577 return PostActionCall("InsertSpace", v)
582 // InsertNewline inserts a newline plus possible some whitespace if autoindent is on
583 func (v *View) InsertNewline(usePlugin bool) bool {
584 if usePlugin && !PreActionCall("InsertNewline", v) {
589 if v.Cursor.HasSelection() {
590 v.Cursor.DeleteSelection()
591 v.Cursor.ResetSelection()
594 ws := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
595 v.Buf.Insert(v.Cursor.Loc, "\n")
598 if v.Buf.Settings["autoindent"].(bool) {
599 v.Buf.Insert(v.Cursor.Loc, ws)
600 // for i := 0; i < len(ws); i++ {
604 // Remove the whitespaces if keepautoindent setting is off
605 if IsSpacesOrTabs(v.Buf.Line(v.Cursor.Y-1)) && !v.Buf.Settings["keepautoindent"].(bool) {
606 line := v.Buf.Line(v.Cursor.Y - 1)
607 v.Buf.Remove(Loc{0, v.Cursor.Y - 1}, Loc{Count(line), v.Cursor.Y - 1})
610 v.Cursor.LastVisualX = v.Cursor.GetVisualX()
613 return PostActionCall("InsertNewline", v)
618 // Backspace deletes the previous character
619 func (v *View) Backspace(usePlugin bool) bool {
620 if usePlugin && !PreActionCall("Backspace", v) {
624 // Delete a character
625 if v.Cursor.HasSelection() {
626 v.Cursor.DeleteSelection()
627 v.Cursor.ResetSelection()
628 } else if v.Cursor.Loc.GreaterThan(v.Buf.Start()) {
629 // We have to do something a bit hacky here because we want to
630 // delete the line by first moving left and then deleting backwards
631 // but the undo redo would place the cursor in the wrong place
632 // So instead we move left, save the position, move back, delete
633 // and restore the position
635 // If the user is using spaces instead of tabs and they are deleting
636 // whitespace at the start of the line, we should delete as if it's a
637 // tab (tabSize number of spaces)
638 lineStart := v.Buf.Line(v.Cursor.Y)[:v.Cursor.X]
639 tabSize := int(v.Buf.Settings["tabsize"].(float64))
640 if v.Buf.Settings["tabstospaces"].(bool) && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%tabSize == 0 {
642 v.Buf.Remove(loc.Move(-tabSize, v.Buf), loc)
645 v.Buf.Remove(loc.Move(-1, v.Buf), loc)
648 v.Cursor.LastVisualX = v.Cursor.GetVisualX()
651 return PostActionCall("Backspace", v)
656 // DeleteWordRight deletes the word to the right of the cursor
657 func (v *View) DeleteWordRight(usePlugin bool) bool {
658 if usePlugin && !PreActionCall("DeleteWordRight", v) {
662 v.SelectWordRight(false)
663 if v.Cursor.HasSelection() {
664 v.Cursor.DeleteSelection()
665 v.Cursor.ResetSelection()
669 return PostActionCall("DeleteWordRight", v)
674 // DeleteWordLeft deletes the word to the left of the cursor
675 func (v *View) DeleteWordLeft(usePlugin bool) bool {
676 if usePlugin && !PreActionCall("DeleteWordLeft", v) {
680 v.SelectWordLeft(false)
681 if v.Cursor.HasSelection() {
682 v.Cursor.DeleteSelection()
683 v.Cursor.ResetSelection()
687 return PostActionCall("DeleteWordLeft", v)
692 // Delete deletes the next character
693 func (v *View) Delete(usePlugin bool) bool {
694 if usePlugin && !PreActionCall("Delete", v) {
698 if v.Cursor.HasSelection() {
699 v.Cursor.DeleteSelection()
700 v.Cursor.ResetSelection()
703 if loc.LessThan(v.Buf.End()) {
704 v.Buf.Remove(loc, loc.Move(1, v.Buf))
709 return PostActionCall("Delete", v)
714 // IndentSelection indents the current selection
715 func (v *View) IndentSelection(usePlugin bool) bool {
716 if usePlugin && !PreActionCall("IndentSelection", v) {
720 if v.Cursor.HasSelection() {
721 start := v.Cursor.CurSelection[0]
722 end := v.Cursor.CurSelection[1]
724 start, end = end, start
728 endY := end.Move(-1, v.Buf).Y
729 endX := end.Move(-1, v.Buf).X
730 for y := startY; y <= endY; y++ {
731 tabsize := len(v.Buf.IndentString())
732 v.Buf.Insert(Loc{0, y}, v.Buf.IndentString())
733 if y == startY && start.X > 0 {
734 v.Cursor.SetSelectionStart(start.Move(tabsize, v.Buf))
737 v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, endY})
743 return PostActionCall("IndentSelection", v)
750 // OutdentLine moves the current line back one indentation
751 func (v *View) OutdentLine(usePlugin bool) bool {
752 if usePlugin && !PreActionCall("OutdentLine", v) {
756 if v.Cursor.HasSelection() {
760 for x := 0; x < len(v.Buf.IndentString()); x++ {
761 if len(GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))) == 0 {
764 v.Buf.Remove(Loc{0, v.Cursor.Y}, Loc{1, v.Cursor.Y})
769 return PostActionCall("OutdentLine", v)
774 // OutdentSelection takes the current selection and moves it back one indent level
775 func (v *View) OutdentSelection(usePlugin bool) bool {
776 if usePlugin && !PreActionCall("OutdentSelection", v) {
780 if v.Cursor.HasSelection() {
781 start := v.Cursor.CurSelection[0]
782 end := v.Cursor.CurSelection[1]
784 start, end = end, start
788 endY := end.Move(-1, v.Buf).Y
789 for y := startY; y <= endY; y++ {
790 for x := 0; x < len(v.Buf.IndentString()); x++ {
791 if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 {
794 v.Buf.Remove(Loc{0, y}, Loc{1, y})
800 return PostActionCall("OutdentSelection", v)
807 // InsertTab inserts a tab or spaces
808 func (v *View) InsertTab(usePlugin bool) bool {
809 if usePlugin && !PreActionCall("InsertTab", v) {
813 if v.Cursor.HasSelection() {
817 tabBytes := len(v.Buf.IndentString())
818 bytesUntilIndent := tabBytes - (v.Cursor.GetVisualX() % tabBytes)
819 v.Buf.Insert(v.Cursor.Loc, v.Buf.IndentString()[:bytesUntilIndent])
820 // for i := 0; i < bytesUntilIndent; i++ {
825 return PostActionCall("InsertTab", v)
830 // SaveAll saves all open buffers
831 func (v *View) SaveAll(usePlugin bool) bool {
833 if usePlugin && !PreActionCall("SaveAll", v) {
837 for _, t := range tabs {
838 for _, v := range t.views {
844 return PostActionCall("SaveAll", v)
850 // Save the buffer to disk
851 func (v *View) Save(usePlugin bool) bool {
853 if usePlugin && !PreActionCall("Save", v) {
857 if v.Type.scratch == true {
858 // We can't save any view type with scratch set. eg help and log text
861 // If this is an empty buffer, ask for a filename
862 if v.Buf.Path == "" {
865 v.saveToFile(v.Buf.Path)
869 return PostActionCall("Save", v)
875 // This function saves the buffer to `filename` and changes the buffer's path and name
876 // to `filename` if the save is successful
877 func (v *View) saveToFile(filename string) {
878 err := v.Buf.SaveAs(filename)
880 if strings.HasSuffix(err.Error(), "permission denied") {
881 choice, _ := messenger.YesNoPrompt("Permission denied. Do you want to save this file using sudo? (y,n)")
883 err = v.Buf.SaveAsWithSudo(filename)
885 messenger.Error(err.Error())
887 v.Buf.Path = filename
888 v.Buf.name = filename
889 messenger.Message("Saved " + filename)
895 messenger.Error(err.Error())
898 v.Buf.Path = filename
899 v.Buf.name = filename
900 messenger.Message("Saved " + filename)
904 // SaveAs saves the buffer to disk with the given name
905 func (v *View) SaveAs(usePlugin bool) bool {
907 if usePlugin && !PreActionCall("Find", v) {
911 filename, canceled := messenger.Prompt("Filename: ", "", "Save", NoCompletion)
913 // the filename might or might not be quoted, so unquote first then join the strings.
914 filename = strings.Join(SplitCommandArgs(filename), " ")
915 v.saveToFile(filename)
919 PostActionCall("Find", v)
925 // Find opens a prompt and searches forward for the input
926 func (v *View) Find(usePlugin bool) bool {
928 if usePlugin && !PreActionCall("Find", v) {
933 if v.Cursor.HasSelection() {
934 searchStart = v.Cursor.CurSelection[1]
935 searchStart = v.Cursor.CurSelection[1]
936 searchStr = v.Cursor.GetSelection()
938 searchStart = v.Cursor.Loc
940 BeginSearch(searchStr)
943 return PostActionCall("Find", v)
949 // FindNext searches forwards for the last used search term
950 func (v *View) FindNext(usePlugin bool) bool {
951 if usePlugin && !PreActionCall("FindNext", v) {
955 if v.Cursor.HasSelection() {
956 searchStart = v.Cursor.CurSelection[1]
957 // lastSearch = v.Cursor.GetSelection()
959 searchStart = v.Cursor.Loc
961 if lastSearch == "" {
964 messenger.Message("Finding: " + lastSearch)
965 Search(lastSearch, v, true)
968 return PostActionCall("FindNext", v)
973 // FindPrevious searches backwards for the last used search term
974 func (v *View) FindPrevious(usePlugin bool) bool {
975 if usePlugin && !PreActionCall("FindPrevious", v) {
979 if v.Cursor.HasSelection() {
980 searchStart = v.Cursor.CurSelection[0]
982 searchStart = v.Cursor.Loc
984 messenger.Message("Finding: " + lastSearch)
985 Search(lastSearch, v, false)
988 return PostActionCall("FindPrevious", v)
993 // Undo undoes the last action
994 func (v *View) Undo(usePlugin bool) bool {
995 if usePlugin && !PreActionCall("Undo", v) {
1000 messenger.Message("Undid action")
1003 return PostActionCall("Undo", v)
1008 // Redo redoes the last action
1009 func (v *View) Redo(usePlugin bool) bool {
1010 if usePlugin && !PreActionCall("Redo", v) {
1015 messenger.Message("Redid action")
1018 return PostActionCall("Redo", v)
1023 // Copy the selection to the system clipboard
1024 func (v *View) Copy(usePlugin bool) bool {
1026 if usePlugin && !PreActionCall("Copy", v) {
1030 if v.Cursor.HasSelection() {
1031 v.Cursor.CopySelection("clipboard")
1033 messenger.Message("Copied selection")
1037 return PostActionCall("Copy", v)
1043 // CutLine cuts the current line to the clipboard
1044 func (v *View) CutLine(usePlugin bool) bool {
1045 if usePlugin && !PreActionCall("CutLine", v) {
1049 v.Cursor.SelectLine()
1050 if !v.Cursor.HasSelection() {
1053 if v.freshClip == true {
1054 if v.Cursor.HasSelection() {
1055 if clip, err := clipboard.ReadAll("clipboard"); err != nil {
1056 messenger.Error(err)
1058 clipboard.WriteAll(clip+v.Cursor.GetSelection(), "clipboard")
1061 } else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
1065 v.lastCutTime = time.Now()
1066 v.Cursor.DeleteSelection()
1067 v.Cursor.ResetSelection()
1068 messenger.Message("Cut line")
1071 return PostActionCall("CutLine", v)
1076 // Cut the selection to the system clipboard
1077 func (v *View) Cut(usePlugin bool) bool {
1078 if usePlugin && !PreActionCall("Cut", v) {
1082 if v.Cursor.HasSelection() {
1083 v.Cursor.CopySelection("clipboard")
1084 v.Cursor.DeleteSelection()
1085 v.Cursor.ResetSelection()
1087 messenger.Message("Cut selection")
1090 return PostActionCall("Cut", v)
1098 // DuplicateLine duplicates the current line or selection
1099 func (v *View) DuplicateLine(usePlugin bool) bool {
1100 if usePlugin && !PreActionCall("DuplicateLine", v) {
1104 if v.Cursor.HasSelection() {
1105 v.Buf.Insert(v.Cursor.CurSelection[1], v.Cursor.GetSelection())
1108 v.Buf.Insert(v.Cursor.Loc, "\n"+v.Buf.Line(v.Cursor.Y))
1112 messenger.Message("Duplicated line")
1115 return PostActionCall("DuplicateLine", v)
1120 // DeleteLine deletes the current line
1121 func (v *View) DeleteLine(usePlugin bool) bool {
1122 if usePlugin && !PreActionCall("DeleteLine", v) {
1126 v.Cursor.SelectLine()
1127 if !v.Cursor.HasSelection() {
1130 v.Cursor.DeleteSelection()
1131 v.Cursor.ResetSelection()
1132 messenger.Message("Deleted line")
1135 return PostActionCall("DeleteLine", v)
1140 // MoveLinesUp moves up the current line or selected lines if any
1141 func (v *View) MoveLinesUp(usePlugin bool) bool {
1142 if usePlugin && !PreActionCall("MoveLinesUp", v) {
1146 if v.Cursor.HasSelection() {
1147 if v.Cursor.CurSelection[0].Y == 0 {
1148 messenger.Message("Can not move further up")
1151 start := v.Cursor.CurSelection[0].Y
1152 end := v.Cursor.CurSelection[1].Y
1154 end, start = start, end
1161 v.Cursor.CurSelection[1].Y -= 1
1162 messenger.Message("Moved up selected line(s)")
1164 if v.Cursor.Loc.Y == 0 {
1165 messenger.Message("Can not move further up")
1172 messenger.Message("Moved up current line")
1174 v.Buf.IsModified = true
1177 return PostActionCall("MoveLinesUp", v)
1182 // MoveLinesDown moves down the current line or selected lines if any
1183 func (v *View) MoveLinesDown(usePlugin bool) bool {
1184 if usePlugin && !PreActionCall("MoveLinesDown", v) {
1188 if v.Cursor.HasSelection() {
1189 if v.Cursor.CurSelection[1].Y >= len(v.Buf.lines) {
1190 messenger.Message("Can not move further down")
1193 start := v.Cursor.CurSelection[0].Y
1194 end := v.Cursor.CurSelection[1].Y
1196 end, start = start, end
1199 v.Buf.MoveLinesDown(
1203 messenger.Message("Moved down selected line(s)")
1205 if v.Cursor.Loc.Y >= len(v.Buf.lines)-1 {
1206 messenger.Message("Can not move further down")
1209 v.Buf.MoveLinesDown(
1213 messenger.Message("Moved down current line")
1215 v.Buf.IsModified = true
1218 return PostActionCall("MoveLinesDown", v)
1223 // Paste whatever is in the system clipboard into the buffer
1224 // Delete and paste if the user has a selection
1225 func (v *View) Paste(usePlugin bool) bool {
1226 if usePlugin && !PreActionCall("Paste", v) {
1230 clip, _ := clipboard.ReadAll("clipboard")
1234 return PostActionCall("Paste", v)
1239 // PastePrimary pastes from the primary clipboard (only use on linux)
1240 func (v *View) PastePrimary(usePlugin bool) bool {
1241 if usePlugin && !PreActionCall("Paste", v) {
1245 clip, _ := clipboard.ReadAll("primary")
1249 return PostActionCall("Paste", v)
1254 // SelectAll selects the entire buffer
1255 func (v *View) SelectAll(usePlugin bool) bool {
1256 if usePlugin && !PreActionCall("SelectAll", v) {
1260 v.Cursor.SetSelectionStart(v.Buf.Start())
1261 v.Cursor.SetSelectionEnd(v.Buf.End())
1262 // Put the cursor at the beginning
1267 return PostActionCall("SelectAll", v)
1272 // OpenFile opens a new file in the buffer
1273 func (v *View) OpenFile(usePlugin bool) bool {
1275 if usePlugin && !PreActionCall("OpenFile", v) {
1280 input, canceled := messenger.Prompt("> ", "open ", "Open", CommandCompletion)
1282 HandleCommand(input)
1284 return PostActionCall("OpenFile", v)
1292 // Start moves the viewport to the start of the buffer
1293 func (v *View) Start(usePlugin bool) bool {
1295 if usePlugin && !PreActionCall("Start", v) {
1302 return PostActionCall("Start", v)
1308 // End moves the viewport to the end of the buffer
1309 func (v *View) End(usePlugin bool) bool {
1311 if usePlugin && !PreActionCall("End", v) {
1315 if v.Height > v.Buf.NumLines {
1318 v.Topline = v.Buf.NumLines - v.Height
1322 return PostActionCall("End", v)
1328 // PageUp scrolls the view up a page
1329 func (v *View) PageUp(usePlugin bool) bool {
1331 if usePlugin && !PreActionCall("PageUp", v) {
1335 if v.Topline > v.Height {
1336 v.ScrollUp(v.Height)
1342 return PostActionCall("PageUp", v)
1348 // PageDown scrolls the view down a page
1349 func (v *View) PageDown(usePlugin bool) bool {
1351 if usePlugin && !PreActionCall("PageDown", v) {
1355 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height {
1356 v.ScrollDown(v.Height)
1357 } else if v.Buf.NumLines >= v.Height {
1358 v.Topline = v.Buf.NumLines - v.Height
1362 return PostActionCall("PageDown", v)
1368 // CursorPageUp places the cursor a page up
1369 func (v *View) CursorPageUp(usePlugin bool) bool {
1370 if usePlugin && !PreActionCall("CursorPageUp", v) {
1376 if v.Cursor.HasSelection() {
1377 v.Cursor.Loc = v.Cursor.CurSelection[0]
1378 v.Cursor.ResetSelection()
1379 v.Cursor.StoreVisualX()
1381 v.Cursor.UpN(v.Height)
1384 return PostActionCall("CursorPageUp", v)
1389 // CursorPageDown places the cursor a page up
1390 func (v *View) CursorPageDown(usePlugin bool) bool {
1391 if usePlugin && !PreActionCall("CursorPageDown", v) {
1397 if v.Cursor.HasSelection() {
1398 v.Cursor.Loc = v.Cursor.CurSelection[1]
1399 v.Cursor.ResetSelection()
1400 v.Cursor.StoreVisualX()
1402 v.Cursor.DownN(v.Height)
1405 return PostActionCall("CursorPageDown", v)
1410 // HalfPageUp scrolls the view up half a page
1411 func (v *View) HalfPageUp(usePlugin bool) bool {
1413 if usePlugin && !PreActionCall("HalfPageUp", v) {
1417 if v.Topline > v.Height/2 {
1418 v.ScrollUp(v.Height / 2)
1424 return PostActionCall("HalfPageUp", v)
1430 // HalfPageDown scrolls the view down half a page
1431 func (v *View) HalfPageDown(usePlugin bool) bool {
1433 if usePlugin && !PreActionCall("HalfPageDown", v) {
1437 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height/2 {
1438 v.ScrollDown(v.Height / 2)
1440 if v.Buf.NumLines >= v.Height {
1441 v.Topline = v.Buf.NumLines - v.Height
1446 return PostActionCall("HalfPageDown", v)
1452 // ToggleRuler turns line numbers off and on
1453 func (v *View) ToggleRuler(usePlugin bool) bool {
1455 if usePlugin && !PreActionCall("ToggleRuler", v) {
1459 if v.Buf.Settings["ruler"] == false {
1460 v.Buf.Settings["ruler"] = true
1461 messenger.Message("Enabled ruler")
1463 v.Buf.Settings["ruler"] = false
1464 messenger.Message("Disabled ruler")
1468 return PostActionCall("ToggleRuler", v)
1474 // JumpLine jumps to a line and moves the view accordingly.
1475 func (v *View) JumpLine(usePlugin bool) bool {
1476 if usePlugin && !PreActionCall("JumpLine", v) {
1480 // Prompt for line number
1481 message := fmt.Sprintf("Jump to line (1 - %v) # ", v.Buf.NumLines)
1482 linestring, canceled := messenger.Prompt(message, "", "LineNumber", NoCompletion)
1486 lineint, err := strconv.Atoi(linestring)
1487 lineint = lineint - 1 // fix offset
1489 messenger.Error(err) // return errors
1492 // Move cursor and view if possible.
1493 if lineint < v.Buf.NumLines && lineint >= 0 {
1495 v.Cursor.Y = lineint
1498 return PostActionCall("JumpLine", v)
1502 messenger.Error("Only ", v.Buf.NumLines, " lines to jump")
1506 // ClearStatus clears the messenger bar
1507 func (v *View) ClearStatus(usePlugin bool) bool {
1509 if usePlugin && !PreActionCall("ClearStatus", v) {
1513 messenger.Message("")
1516 return PostActionCall("ClearStatus", v)
1522 // ToggleHelp toggles the help screen
1523 func (v *View) ToggleHelp(usePlugin bool) bool {
1525 if usePlugin && !PreActionCall("ToggleHelp", v) {
1529 if v.Type != vtHelp {
1530 // Open the default help
1537 return PostActionCall("ToggleHelp", v)
1543 // ShellMode opens a terminal to run a shell command
1544 func (v *View) ShellMode(usePlugin bool) bool {
1546 if usePlugin && !PreActionCall("ShellMode", v) {
1550 input, canceled := messenger.Prompt("$ ", "", "Shell", NoCompletion)
1552 // The true here is for openTerm to make the command interactive
1553 HandleShellCommand(input, true, true)
1555 return PostActionCall("ShellMode", v)
1562 // CommandMode lets the user enter a command
1563 func (v *View) CommandMode(usePlugin bool) bool {
1565 if usePlugin && !PreActionCall("CommandMode", v) {
1569 input, canceled := messenger.Prompt("> ", "", "Command", CommandCompletion)
1571 HandleCommand(input)
1573 return PostActionCall("CommandMode", v)
1581 // Escape leaves current mode
1582 func (v *View) Escape(usePlugin bool) bool {
1584 // check if user is searching, or the last search is still active
1585 if searching || lastSearch != "" {
1589 // check if a prompt is shown, hide it and don't quit
1590 if messenger.hasPrompt {
1591 messenger.Reset() // FIXME
1599 // Quit this will close the current tab or view that is open
1600 func (v *View) Quit(usePlugin bool) bool {
1602 if usePlugin && !PreActionCall("Quit", v) {
1606 // Make sure not to quit if there are unsaved changes
1609 if len(tabs[curTab].views) > 1 {
1610 v.splitNode.Delete()
1611 tabs[v.TabNum].Cleanup()
1612 tabs[v.TabNum].Resize()
1613 } else if len(tabs) > 1 {
1614 if len(tabs[v.TabNum].views) == 1 {
1615 tabs = tabs[:v.TabNum+copy(tabs[v.TabNum:], tabs[v.TabNum+1:])]
1616 for i, t := range tabs {
1619 if curTab >= len(tabs) {
1623 CurView().ToggleTabbar()
1628 PostActionCall("Quit", v)
1637 return PostActionCall("Quit", v)
1643 // QuitAll quits the whole editor; all splits and tabs
1644 func (v *View) QuitAll(usePlugin bool) bool {
1646 if usePlugin && !PreActionCall("QuitAll", v) {
1651 for _, tab := range tabs {
1652 for _, v := range tab.views {
1660 // only quit if all of the buffers can be closed and the user confirms that they actually want to quit everything
1661 shouldQuit, _ := messenger.YesNoPrompt("Do you want to quit micro (all open files will be closed)?")
1664 for _, tab := range tabs {
1665 for _, v := range tab.views {
1671 PostActionCall("QuitAll", v)
1683 // AddTab adds a new tab with an empty buffer
1684 func (v *View) AddTab(usePlugin bool) bool {
1686 if usePlugin && !PreActionCall("AddTab", v) {
1690 tab := NewTabFromView(NewView(NewBufferFromString("", "")))
1691 tab.SetNum(len(tabs))
1692 tabs = append(tabs, tab)
1693 curTab = len(tabs) - 1
1695 for _, t := range tabs {
1696 for _, v := range t.views {
1703 return PostActionCall("AddTab", v)
1709 // PreviousTab switches to the previous tab in the tab list
1710 func (v *View) PreviousTab(usePlugin bool) bool {
1712 if usePlugin && !PreActionCall("PreviousTab", v) {
1718 } else if curTab == 0 {
1719 curTab = len(tabs) - 1
1723 return PostActionCall("PreviousTab", v)
1729 // NextTab switches to the next tab in the tab list
1730 func (v *View) NextTab(usePlugin bool) bool {
1732 if usePlugin && !PreActionCall("NextTab", v) {
1736 if curTab < len(tabs)-1 {
1738 } else if curTab == len(tabs)-1 {
1743 return PostActionCall("NextTab", v)
1749 // VSplitBinding opens an empty vertical split
1750 func (v *View) VSplitBinding(usePlugin bool) bool {
1752 if usePlugin && !PreActionCall("VSplit", v) {
1756 v.VSplit(NewBufferFromString("", ""))
1759 return PostActionCall("VSplit", v)
1765 // HSplitBinding opens an empty horizontal split
1766 func (v *View) HSplitBinding(usePlugin bool) bool {
1768 if usePlugin && !PreActionCall("HSplit", v) {
1772 v.HSplit(NewBufferFromString("", ""))
1775 return PostActionCall("HSplit", v)
1781 // Unsplit closes all splits in the current tab except the active one
1782 func (v *View) Unsplit(usePlugin bool) bool {
1784 if usePlugin && !PreActionCall("Unsplit", v) {
1788 curView := tabs[curTab].CurView
1789 for i := len(tabs[curTab].views) - 1; i >= 0; i-- {
1790 view := tabs[curTab].views[i]
1791 if view != nil && view.Num != curView {
1793 // messenger.Message("Quit ", view.Buf.Path)
1798 return PostActionCall("Unsplit", v)
1804 // NextSplit changes the view to the next split
1805 func (v *View) NextSplit(usePlugin bool) bool {
1807 if usePlugin && !PreActionCall("NextSplit", v) {
1812 if tab.CurView < len(tab.views)-1 {
1819 return PostActionCall("NextSplit", v)
1825 // PreviousSplit changes the view to the previous split
1826 func (v *View) PreviousSplit(usePlugin bool) bool {
1828 if usePlugin && !PreActionCall("PreviousSplit", v) {
1833 if tab.CurView > 0 {
1836 tab.CurView = len(tab.views) - 1
1840 return PostActionCall("PreviousSplit", v)
1846 var curMacro []interface{}
1847 var recordingMacro bool
1849 // ToggleMacro toggles recording of a macro
1850 func (v *View) ToggleMacro(usePlugin bool) bool {
1852 if usePlugin && !PreActionCall("ToggleMacro", v) {
1856 recordingMacro = !recordingMacro
1859 curMacro = []interface{}{}
1860 messenger.Message("Recording")
1862 messenger.Message("Stopped recording")
1866 return PostActionCall("ToggleMacro", v)
1872 // PlayMacro plays back the most recently recorded macro
1873 func (v *View) PlayMacro(usePlugin bool) bool {
1874 if usePlugin && !PreActionCall("PlayMacro", v) {
1878 for _, action := range curMacro {
1879 switch t := action.(type) {
1881 // Insert a character
1882 if v.Cursor.HasSelection() {
1883 v.Cursor.DeleteSelection()
1884 v.Cursor.ResetSelection()
1886 v.Buf.Insert(v.Cursor.Loc, string(t))
1889 for pl := range loadedPlugins {
1890 _, err := Call(pl+".onRune", string(t), v)
1891 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
1895 case func(*View, bool) bool:
1901 return PostActionCall("PlayMacro", v)
1906 // SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
1907 func (v *View) SpawnMultiCursor(usePlugin bool) bool {
1908 spawner := v.Buf.cursors[len(v.Buf.cursors)-1]
1909 // You can only spawn a cursor from the main cursor
1910 if v.Cursor == spawner {
1911 if usePlugin && !PreActionCall("SpawnMultiCursor", v) {
1915 if !spawner.HasSelection() {
1916 spawner.SelectWord()
1922 sel := spawner.GetSelection()
1924 searchStart = spawner.CurSelection[1]
1926 Search(sel, v, true)
1928 for _, cur := range v.Buf.cursors {
1929 if c.Loc == cur.Loc {
1933 v.Buf.cursors = append(v.Buf.cursors, c)
1934 v.Buf.UpdateCursors()
1940 PostActionCall("SpawnMultiCursor", v)
1947 // MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
1948 func (v *View) MouseMultiCursor(usePlugin bool, e *tcell.EventMouse) bool {
1949 if v.Cursor == &v.Buf.Cursor {
1950 if usePlugin && !PreActionCall("SpawnMultiCursorAtMouse", v, e) {
1953 x, y := e.Position()
1954 x -= v.lineNumOffset - v.leftCol + v.x
1955 y += v.Topline - v.y
1961 v.MoveToMouseClick(x, y)
1963 v.Cursor = &v.Buf.Cursor
1965 v.Buf.cursors = append(v.Buf.cursors, c)
1966 v.Buf.MergeCursors()
1967 v.Buf.UpdateCursors()
1970 PostActionCall("SpawnMultiCursorAtMouse", v)
1976 // SkipMultiCursor moves the current multiple cursor to the next available position
1977 func (v *View) SkipMultiCursor(usePlugin bool) bool {
1978 cursor := v.Buf.cursors[len(v.Buf.cursors)-1]
1981 if usePlugin && !PreActionCall("SkipMultiCursor", v) {
1984 sel := cursor.GetSelection()
1986 searchStart = cursor.CurSelection[1]
1988 Search(sel, v, true)
1993 PostActionCall("SkipMultiCursor", v)
1999 // RemoveMultiCursor removes the latest multiple cursor
2000 func (v *View) RemoveMultiCursor(usePlugin bool) bool {
2001 end := len(v.Buf.cursors)
2004 if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
2008 v.Buf.cursors[end-1] = nil
2009 v.Buf.cursors = v.Buf.cursors[:end-1]
2010 v.Buf.UpdateCursors()
2014 return PostActionCall("RemoveMultiCursor", v)
2019 v.RemoveAllMultiCursors(usePlugin)
2024 // RemoveAllMultiCursors removes all cursors except the base cursor
2025 func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
2027 if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
2031 for i := 1; i < len(v.Buf.cursors); i++ {
2032 v.Buf.cursors[i] = nil
2034 v.Buf.cursors = v.Buf.cursors[:1]
2035 v.Buf.UpdateCursors()
2036 v.Cursor.ResetSelection()
2040 return PostActionCall("RemoveAllMultiCursors", v)