]> git.lizzy.rs Git - micro.git/blob - cmd/micro/actions.go
Clear cursors before performing undo or redo
[micro.git] / cmd / micro / actions.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "strconv"
7         "strings"
8         "time"
9
10         "github.com/yuin/gopher-lua"
11         "github.com/zyedidia/clipboard"
12         "github.com/zyedidia/tcell"
13 )
14
15 // PreActionCall executes the lua pre callback if possible
16 func PreActionCall(funcName string, view *View, args ...interface{}) bool {
17         executeAction := true
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") {
21                         TermMessage(err)
22                         continue
23                 }
24                 if ret == lua.LFalse {
25                         executeAction = false
26                 }
27         }
28         return executeAction
29 }
30
31 // PostActionCall executes the lua plugin callback if possible
32 func PostActionCall(funcName string, view *View, args ...interface{}) bool {
33         relocate := true
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") {
37                         TermMessage(err)
38                         continue
39                 }
40                 if ret == lua.LFalse {
41                         relocate = false
42                 }
43         }
44         return relocate
45 }
46
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()
52                 return true
53         }
54         return false
55 }
56
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) {
61                 return false
62         }
63
64         x, y := e.Position()
65         x -= v.lineNumOffset - v.leftCol + v.x
66         y += v.Topline - v.y
67
68         // This is usually bound to left click
69         v.MoveToMouseClick(x, y)
70         if v.mouseReleased {
71                 if len(v.Buf.cursors) > 1 {
72                         for i := 1; i < len(v.Buf.cursors); i++ {
73                                 v.Buf.cursors[i] = nil
74                         }
75                         v.Buf.cursors = v.Buf.cursors[:1]
76                         v.Buf.UpdateCursors()
77                         v.Cursor.ResetSelection()
78                         v.Relocate()
79                 }
80                 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
81                         if v.doubleClick {
82                                 // Triple click
83                                 v.lastClickTime = time.Now()
84
85                                 v.tripleClick = true
86                                 v.doubleClick = false
87
88                                 v.Cursor.SelectLine()
89                                 v.Cursor.CopySelection("primary")
90                         } else {
91                                 // Double click
92                                 v.lastClickTime = time.Now()
93
94                                 v.doubleClick = true
95                                 v.tripleClick = false
96
97                                 v.Cursor.SelectWord()
98                                 v.Cursor.CopySelection("primary")
99                         }
100                 } else {
101                         v.doubleClick = false
102                         v.tripleClick = false
103                         v.lastClickTime = time.Now()
104
105                         v.Cursor.OrigSelection[0] = v.Cursor.Loc
106                         v.Cursor.CurSelection[0] = v.Cursor.Loc
107                         v.Cursor.CurSelection[1] = v.Cursor.Loc
108                 }
109                 v.mouseReleased = false
110         } else if !v.mouseReleased {
111                 if v.tripleClick {
112                         v.Cursor.AddLineToSelection()
113                 } else if v.doubleClick {
114                         v.Cursor.AddWordToSelection()
115                 } else {
116                         v.Cursor.SetSelectionEnd(v.Cursor.Loc)
117                         v.Cursor.CopySelection("primary")
118                 }
119         }
120
121         if usePlugin {
122                 PostActionCall("MousePress", v, e)
123         }
124         return false
125 }
126
127 // ScrollUpAction scrolls the view up
128 func (v *View) ScrollUpAction(usePlugin bool) bool {
129         if v.mainCursor() {
130                 if usePlugin && !PreActionCall("ScrollUp", v) {
131                         return false
132                 }
133
134                 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
135                 v.ScrollUp(scrollspeed)
136
137                 if usePlugin {
138                         PostActionCall("ScrollUp", v)
139                 }
140         }
141         return false
142 }
143
144 // ScrollDownAction scrolls the view up
145 func (v *View) ScrollDownAction(usePlugin bool) bool {
146         if v.mainCursor() {
147                 if usePlugin && !PreActionCall("ScrollDown", v) {
148                         return false
149                 }
150
151                 scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
152                 v.ScrollDown(scrollspeed)
153
154                 if usePlugin {
155                         PostActionCall("ScrollDown", v)
156                 }
157         }
158         return false
159 }
160
161 // Center centers the view on the cursor
162 func (v *View) Center(usePlugin bool) bool {
163         if usePlugin && !PreActionCall("Center", v) {
164                 return false
165         }
166
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
170         }
171         if v.Topline < 0 {
172                 v.Topline = 0
173         }
174
175         if usePlugin {
176                 return PostActionCall("Center", v)
177         }
178         return true
179 }
180
181 // CursorUp moves the cursor up
182 func (v *View) CursorUp(usePlugin bool) bool {
183         if usePlugin && !PreActionCall("CursorUp", v) {
184                 return false
185         }
186
187         v.deselect(0)
188         v.Cursor.Up()
189
190         if usePlugin {
191                 return PostActionCall("CursorUp", v)
192         }
193         return true
194 }
195
196 // CursorDown moves the cursor down
197 func (v *View) CursorDown(usePlugin bool) bool {
198         if usePlugin && !PreActionCall("CursorDown", v) {
199                 return false
200         }
201
202         v.deselect(1)
203         v.Cursor.Down()
204
205         if usePlugin {
206                 return PostActionCall("CursorDown", v)
207         }
208         return true
209 }
210
211 // CursorLeft moves the cursor left
212 func (v *View) CursorLeft(usePlugin bool) bool {
213         if usePlugin && !PreActionCall("CursorLeft", v) {
214                 return false
215         }
216
217         if v.Cursor.HasSelection() {
218                 v.Cursor.Loc = v.Cursor.CurSelection[0]
219                 v.Cursor.ResetSelection()
220                 v.Cursor.StoreVisualX()
221         } else {
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++ {
229                                         v.Cursor.Left()
230                                 }
231                         } else {
232                                 v.Cursor.Left()
233                         }
234                 } else {
235                         v.Cursor.Left()
236                 }
237         }
238
239         if usePlugin {
240                 return PostActionCall("CursorLeft", v)
241         }
242         return true
243 }
244
245 // CursorRight moves the cursor right
246 func (v *View) CursorRight(usePlugin bool) bool {
247         if usePlugin && !PreActionCall("CursorRight", v) {
248                 return false
249         }
250
251         if v.Cursor.HasSelection() {
252                 v.Cursor.Loc = v.Cursor.CurSelection[1]
253                 v.Cursor.ResetSelection()
254                 v.Cursor.StoreVisualX()
255         } else {
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++ {
263                                         v.Cursor.Right()
264                                 }
265                         } else {
266                                 v.Cursor.Right()
267                         }
268                 } else {
269                         v.Cursor.Right()
270                 }
271         }
272
273         if usePlugin {
274                 return PostActionCall("CursorRight", v)
275         }
276         return true
277 }
278
279 // WordRight moves the cursor one word to the right
280 func (v *View) WordRight(usePlugin bool) bool {
281         if usePlugin && !PreActionCall("WordRight", v) {
282                 return false
283         }
284
285         v.Cursor.WordRight()
286
287         if usePlugin {
288                 return PostActionCall("WordRight", v)
289         }
290         return true
291 }
292
293 // WordLeft moves the cursor one word to the left
294 func (v *View) WordLeft(usePlugin bool) bool {
295         if usePlugin && !PreActionCall("WordLeft", v) {
296                 return false
297         }
298
299         v.Cursor.WordLeft()
300
301         if usePlugin {
302                 return PostActionCall("WordLeft", v)
303         }
304         return true
305 }
306
307 // SelectUp selects up one line
308 func (v *View) SelectUp(usePlugin bool) bool {
309         if usePlugin && !PreActionCall("SelectUp", v) {
310                 return false
311         }
312
313         if !v.Cursor.HasSelection() {
314                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
315         }
316         v.Cursor.Up()
317         v.Cursor.SelectTo(v.Cursor.Loc)
318
319         if usePlugin {
320                 return PostActionCall("SelectUp", v)
321         }
322         return true
323 }
324
325 // SelectDown selects down one line
326 func (v *View) SelectDown(usePlugin bool) bool {
327         if usePlugin && !PreActionCall("SelectDown", v) {
328                 return false
329         }
330
331         if !v.Cursor.HasSelection() {
332                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
333         }
334         v.Cursor.Down()
335         v.Cursor.SelectTo(v.Cursor.Loc)
336
337         if usePlugin {
338                 return PostActionCall("SelectDown", v)
339         }
340         return true
341 }
342
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) {
346                 return false
347         }
348
349         loc := v.Cursor.Loc
350         count := v.Buf.End()
351         if loc.GreaterThan(count) {
352                 loc = count
353         }
354         if !v.Cursor.HasSelection() {
355                 v.Cursor.OrigSelection[0] = loc
356         }
357         v.Cursor.Left()
358         v.Cursor.SelectTo(v.Cursor.Loc)
359
360         if usePlugin {
361                 return PostActionCall("SelectLeft", v)
362         }
363         return true
364 }
365
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) {
369                 return false
370         }
371
372         loc := v.Cursor.Loc
373         count := v.Buf.End()
374         if loc.GreaterThan(count) {
375                 loc = count
376         }
377         if !v.Cursor.HasSelection() {
378                 v.Cursor.OrigSelection[0] = loc
379         }
380         v.Cursor.Right()
381         v.Cursor.SelectTo(v.Cursor.Loc)
382
383         if usePlugin {
384                 return PostActionCall("SelectRight", v)
385         }
386         return true
387 }
388
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) {
392                 return false
393         }
394
395         if !v.Cursor.HasSelection() {
396                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
397         }
398         v.Cursor.WordRight()
399         v.Cursor.SelectTo(v.Cursor.Loc)
400
401         if usePlugin {
402                 return PostActionCall("SelectWordRight", v)
403         }
404         return true
405 }
406
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) {
410                 return false
411         }
412
413         if !v.Cursor.HasSelection() {
414                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
415         }
416         v.Cursor.WordLeft()
417         v.Cursor.SelectTo(v.Cursor.Loc)
418
419         if usePlugin {
420                 return PostActionCall("SelectWordLeft", v)
421         }
422         return true
423 }
424
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) {
428                 return false
429         }
430
431         v.deselect(0)
432
433         v.Cursor.Start()
434
435         if usePlugin {
436                 return PostActionCall("StartOfLine", v)
437         }
438         return true
439 }
440
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) {
444                 return false
445         }
446
447         v.deselect(0)
448
449         v.Cursor.End()
450
451         if usePlugin {
452                 return PostActionCall("EndOfLine", v)
453         }
454         return true
455 }
456
457 // SelectToStartOfLine selects to the start of the current line
458 func (v *View) SelectToStartOfLine(usePlugin bool) bool {
459         if usePlugin && !PreActionCall("SelectToStartOfLine", v) {
460                 return false
461         }
462
463         if !v.Cursor.HasSelection() {
464                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
465         }
466         v.Cursor.Start()
467         v.Cursor.SelectTo(v.Cursor.Loc)
468
469         if usePlugin {
470                 return PostActionCall("SelectToStartOfLine", v)
471         }
472         return true
473 }
474
475 // SelectToEndOfLine selects to the end of the current line
476 func (v *View) SelectToEndOfLine(usePlugin bool) bool {
477         if usePlugin && !PreActionCall("SelectToEndOfLine", v) {
478                 return false
479         }
480
481         if !v.Cursor.HasSelection() {
482                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
483         }
484         v.Cursor.End()
485         v.Cursor.SelectTo(v.Cursor.Loc)
486
487         if usePlugin {
488                 return PostActionCall("SelectToEndOfLine", v)
489         }
490         return true
491 }
492
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) {
496                 return false
497         }
498
499         v.deselect(0)
500
501         v.Cursor.X = 0
502         v.Cursor.Y = 0
503
504         if usePlugin {
505                 return PostActionCall("CursorStart", v)
506         }
507         return true
508 }
509
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) {
513                 return false
514         }
515
516         v.deselect(0)
517
518         v.Cursor.Loc = v.Buf.End()
519         v.Cursor.StoreVisualX()
520
521         if usePlugin {
522                 return PostActionCall("CursorEnd", v)
523         }
524         return true
525 }
526
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) {
530                 return false
531         }
532
533         if !v.Cursor.HasSelection() {
534                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
535         }
536         v.CursorStart(false)
537         v.Cursor.SelectTo(v.Buf.Start())
538
539         if usePlugin {
540                 return PostActionCall("SelectToStart", v)
541         }
542         return true
543 }
544
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) {
548                 return false
549         }
550
551         if !v.Cursor.HasSelection() {
552                 v.Cursor.OrigSelection[0] = v.Cursor.Loc
553         }
554         v.CursorEnd(false)
555         v.Cursor.SelectTo(v.Buf.End())
556
557         if usePlugin {
558                 return PostActionCall("SelectToEnd", v)
559         }
560         return true
561 }
562
563 // InsertSpace inserts a space
564 func (v *View) InsertSpace(usePlugin bool) bool {
565         if usePlugin && !PreActionCall("InsertSpace", v) {
566                 return false
567         }
568
569         if v.Cursor.HasSelection() {
570                 v.Cursor.DeleteSelection()
571                 v.Cursor.ResetSelection()
572         }
573         v.Buf.Insert(v.Cursor.Loc, " ")
574         // v.Cursor.Right()
575
576         if usePlugin {
577                 return PostActionCall("InsertSpace", v)
578         }
579         return true
580 }
581
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) {
585                 return false
586         }
587
588         // Insert a newline
589         if v.Cursor.HasSelection() {
590                 v.Cursor.DeleteSelection()
591                 v.Cursor.ResetSelection()
592         }
593
594         ws := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
595         v.Buf.Insert(v.Cursor.Loc, "\n")
596         // v.Cursor.Right()
597
598         if v.Buf.Settings["autoindent"].(bool) {
599                 v.Buf.Insert(v.Cursor.Loc, ws)
600                 // for i := 0; i < len(ws); i++ {
601                 //      v.Cursor.Right()
602                 // }
603
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})
608                 }
609         }
610         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
611
612         if usePlugin {
613                 return PostActionCall("InsertNewline", v)
614         }
615         return true
616 }
617
618 // Backspace deletes the previous character
619 func (v *View) Backspace(usePlugin bool) bool {
620         if usePlugin && !PreActionCall("Backspace", v) {
621                 return false
622         }
623
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
634
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 {
641                         loc := v.Cursor.Loc
642                         v.Buf.Remove(loc.Move(-tabSize, v.Buf), loc)
643                 } else {
644                         loc := v.Cursor.Loc
645                         v.Buf.Remove(loc.Move(-1, v.Buf), loc)
646                 }
647         }
648         v.Cursor.LastVisualX = v.Cursor.GetVisualX()
649
650         if usePlugin {
651                 return PostActionCall("Backspace", v)
652         }
653         return true
654 }
655
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) {
659                 return false
660         }
661
662         v.SelectWordRight(false)
663         if v.Cursor.HasSelection() {
664                 v.Cursor.DeleteSelection()
665                 v.Cursor.ResetSelection()
666         }
667
668         if usePlugin {
669                 return PostActionCall("DeleteWordRight", v)
670         }
671         return true
672 }
673
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) {
677                 return false
678         }
679
680         v.SelectWordLeft(false)
681         if v.Cursor.HasSelection() {
682                 v.Cursor.DeleteSelection()
683                 v.Cursor.ResetSelection()
684         }
685
686         if usePlugin {
687                 return PostActionCall("DeleteWordLeft", v)
688         }
689         return true
690 }
691
692 // Delete deletes the next character
693 func (v *View) Delete(usePlugin bool) bool {
694         if usePlugin && !PreActionCall("Delete", v) {
695                 return false
696         }
697
698         if v.Cursor.HasSelection() {
699                 v.Cursor.DeleteSelection()
700                 v.Cursor.ResetSelection()
701         } else {
702                 loc := v.Cursor.Loc
703                 if loc.LessThan(v.Buf.End()) {
704                         v.Buf.Remove(loc, loc.Move(1, v.Buf))
705                 }
706         }
707
708         if usePlugin {
709                 return PostActionCall("Delete", v)
710         }
711         return true
712 }
713
714 // IndentSelection indents the current selection
715 func (v *View) IndentSelection(usePlugin bool) bool {
716         if usePlugin && !PreActionCall("IndentSelection", v) {
717                 return false
718         }
719
720         if v.Cursor.HasSelection() {
721                 start := v.Cursor.CurSelection[0]
722                 end := v.Cursor.CurSelection[1]
723                 if end.Y < start.Y {
724                         start, end = end, start
725                 }
726
727                 startY := start.Y
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))
735                         }
736                         if y == endY {
737                                 v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, endY})
738                         }
739                 }
740                 v.Cursor.Relocate()
741
742                 if usePlugin {
743                         return PostActionCall("IndentSelection", v)
744                 }
745                 return true
746         }
747         return false
748 }
749
750 // OutdentLine moves the current line back one indentation
751 func (v *View) OutdentLine(usePlugin bool) bool {
752         if usePlugin && !PreActionCall("OutdentLine", v) {
753                 return false
754         }
755
756         if v.Cursor.HasSelection() {
757                 return false
758         }
759
760         for x := 0; x < len(v.Buf.IndentString()); x++ {
761                 if len(GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))) == 0 {
762                         break
763                 }
764                 v.Buf.Remove(Loc{0, v.Cursor.Y}, Loc{1, v.Cursor.Y})
765         }
766         v.Cursor.Relocate()
767
768         if usePlugin {
769                 return PostActionCall("OutdentLine", v)
770         }
771         return true
772 }
773
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) {
777                 return false
778         }
779
780         if v.Cursor.HasSelection() {
781                 start := v.Cursor.CurSelection[0]
782                 end := v.Cursor.CurSelection[1]
783                 if end.Y < start.Y {
784                         start, end = end, start
785                 }
786
787                 startY := start.Y
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 {
792                                         break
793                                 }
794                                 v.Buf.Remove(Loc{0, y}, Loc{1, y})
795                         }
796                 }
797                 v.Cursor.Relocate()
798
799                 if usePlugin {
800                         return PostActionCall("OutdentSelection", v)
801                 }
802                 return true
803         }
804         return false
805 }
806
807 // InsertTab inserts a tab or spaces
808 func (v *View) InsertTab(usePlugin bool) bool {
809         if usePlugin && !PreActionCall("InsertTab", v) {
810                 return false
811         }
812
813         if v.Cursor.HasSelection() {
814                 return false
815         }
816
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++ {
821         //      v.Cursor.Right()
822         // }
823
824         if usePlugin {
825                 return PostActionCall("InsertTab", v)
826         }
827         return true
828 }
829
830 // SaveAll saves all open buffers
831 func (v *View) SaveAll(usePlugin bool) bool {
832         if v.mainCursor() {
833                 if usePlugin && !PreActionCall("SaveAll", v) {
834                         return false
835                 }
836
837                 for _, t := range tabs {
838                         for _, v := range t.views {
839                                 v.Save(false)
840                         }
841                 }
842
843                 if usePlugin {
844                         return PostActionCall("SaveAll", v)
845                 }
846         }
847         return false
848 }
849
850 // Save the buffer to disk
851 func (v *View) Save(usePlugin bool) bool {
852         if v.mainCursor() {
853                 if usePlugin && !PreActionCall("Save", v) {
854                         return false
855                 }
856
857                 if v.Type.scratch == true {
858                         // We can't save any view type with scratch set. eg help and log text
859                         return false
860                 }
861                 // If this is an empty buffer, ask for a filename
862                 if v.Buf.Path == "" {
863                         v.SaveAs(false)
864                 } else {
865                         v.saveToFile(v.Buf.Path)
866                 }
867
868                 if usePlugin {
869                         return PostActionCall("Save", v)
870                 }
871         }
872         return false
873 }
874
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)
879         if err != nil {
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)")
882                         if choice {
883                                 err = v.Buf.SaveAsWithSudo(filename)
884                                 if err != nil {
885                                         messenger.Error(err.Error())
886                                 } else {
887                                         v.Buf.Path = filename
888                                         v.Buf.name = filename
889                                         messenger.Message("Saved " + filename)
890                                 }
891                         }
892                         messenger.Reset()
893                         messenger.Clear()
894                 } else {
895                         messenger.Error(err.Error())
896                 }
897         } else {
898                 v.Buf.Path = filename
899                 v.Buf.name = filename
900                 messenger.Message("Saved " + filename)
901         }
902 }
903
904 // SaveAs saves the buffer to disk with the given name
905 func (v *View) SaveAs(usePlugin bool) bool {
906         if v.mainCursor() {
907                 if usePlugin && !PreActionCall("Find", v) {
908                         return false
909                 }
910
911                 filename, canceled := messenger.Prompt("Filename: ", "", "Save", NoCompletion)
912                 if !canceled {
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)
916                 }
917
918                 if usePlugin {
919                         PostActionCall("Find", v)
920                 }
921         }
922         return false
923 }
924
925 // Find opens a prompt and searches forward for the input
926 func (v *View) Find(usePlugin bool) bool {
927         if v.mainCursor() {
928                 if usePlugin && !PreActionCall("Find", v) {
929                         return false
930                 }
931
932                 searchStr := ""
933                 if v.Cursor.HasSelection() {
934                         searchStart = v.Cursor.CurSelection[1]
935                         searchStart = v.Cursor.CurSelection[1]
936                         searchStr = v.Cursor.GetSelection()
937                 } else {
938                         searchStart = v.Cursor.Loc
939                 }
940                 BeginSearch(searchStr)
941
942                 if usePlugin {
943                         return PostActionCall("Find", v)
944                 }
945         }
946         return true
947 }
948
949 // FindNext searches forwards for the last used search term
950 func (v *View) FindNext(usePlugin bool) bool {
951         if usePlugin && !PreActionCall("FindNext", v) {
952                 return false
953         }
954
955         if v.Cursor.HasSelection() {
956                 searchStart = v.Cursor.CurSelection[1]
957                 // lastSearch = v.Cursor.GetSelection()
958         } else {
959                 searchStart = v.Cursor.Loc
960         }
961         if lastSearch == "" {
962                 return true
963         }
964         messenger.Message("Finding: " + lastSearch)
965         Search(lastSearch, v, true)
966
967         if usePlugin {
968                 return PostActionCall("FindNext", v)
969         }
970         return true
971 }
972
973 // FindPrevious searches backwards for the last used search term
974 func (v *View) FindPrevious(usePlugin bool) bool {
975         if usePlugin && !PreActionCall("FindPrevious", v) {
976                 return false
977         }
978
979         if v.Cursor.HasSelection() {
980                 searchStart = v.Cursor.CurSelection[0]
981         } else {
982                 searchStart = v.Cursor.Loc
983         }
984         messenger.Message("Finding: " + lastSearch)
985         Search(lastSearch, v, false)
986
987         if usePlugin {
988                 return PostActionCall("FindPrevious", v)
989         }
990         return true
991 }
992
993 // Undo undoes the last action
994 func (v *View) Undo(usePlugin bool) bool {
995         if usePlugin && !PreActionCall("Undo", v) {
996                 return false
997         }
998
999         if v.Buf.curCursor == 0 {
1000                 v.Buf.clearCursors()
1001         }
1002
1003         v.Buf.Undo()
1004         messenger.Message("Undid action")
1005
1006         if usePlugin {
1007                 return PostActionCall("Undo", v)
1008         }
1009         return true
1010 }
1011
1012 // Redo redoes the last action
1013 func (v *View) Redo(usePlugin bool) bool {
1014         if usePlugin && !PreActionCall("Redo", v) {
1015                 return false
1016         }
1017
1018         if v.Buf.curCursor == 0 {
1019                 v.Buf.clearCursors()
1020         }
1021
1022         v.Buf.Redo()
1023         messenger.Message("Redid action")
1024
1025         if usePlugin {
1026                 return PostActionCall("Redo", v)
1027         }
1028         return true
1029 }
1030
1031 // Copy the selection to the system clipboard
1032 func (v *View) Copy(usePlugin bool) bool {
1033         if v.mainCursor() {
1034                 if usePlugin && !PreActionCall("Copy", v) {
1035                         return false
1036                 }
1037
1038                 if v.Cursor.HasSelection() {
1039                         v.Cursor.CopySelection("clipboard")
1040                         v.freshClip = true
1041                         messenger.Message("Copied selection")
1042                 }
1043
1044                 if usePlugin {
1045                         return PostActionCall("Copy", v)
1046                 }
1047         }
1048         return true
1049 }
1050
1051 // CutLine cuts the current line to the clipboard
1052 func (v *View) CutLine(usePlugin bool) bool {
1053         if usePlugin && !PreActionCall("CutLine", v) {
1054                 return false
1055         }
1056
1057         v.Cursor.SelectLine()
1058         if !v.Cursor.HasSelection() {
1059                 return false
1060         }
1061         if v.freshClip == true {
1062                 if v.Cursor.HasSelection() {
1063                         if clip, err := clipboard.ReadAll("clipboard"); err != nil {
1064                                 messenger.Error(err)
1065                         } else {
1066                                 clipboard.WriteAll(clip+v.Cursor.GetSelection(), "clipboard")
1067                         }
1068                 }
1069         } else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
1070                 v.Copy(true)
1071         }
1072         v.freshClip = true
1073         v.lastCutTime = time.Now()
1074         v.Cursor.DeleteSelection()
1075         v.Cursor.ResetSelection()
1076         messenger.Message("Cut line")
1077
1078         if usePlugin {
1079                 return PostActionCall("CutLine", v)
1080         }
1081         return true
1082 }
1083
1084 // Cut the selection to the system clipboard
1085 func (v *View) Cut(usePlugin bool) bool {
1086         if usePlugin && !PreActionCall("Cut", v) {
1087                 return false
1088         }
1089
1090         if v.Cursor.HasSelection() {
1091                 v.Cursor.CopySelection("clipboard")
1092                 v.Cursor.DeleteSelection()
1093                 v.Cursor.ResetSelection()
1094                 v.freshClip = true
1095                 messenger.Message("Cut selection")
1096
1097                 if usePlugin {
1098                         return PostActionCall("Cut", v)
1099                 }
1100                 return true
1101         }
1102
1103         return false
1104 }
1105
1106 // DuplicateLine duplicates the current line or selection
1107 func (v *View) DuplicateLine(usePlugin bool) bool {
1108         if usePlugin && !PreActionCall("DuplicateLine", v) {
1109                 return false
1110         }
1111
1112         if v.Cursor.HasSelection() {
1113                 v.Buf.Insert(v.Cursor.CurSelection[1], v.Cursor.GetSelection())
1114         } else {
1115                 v.Cursor.End()
1116                 v.Buf.Insert(v.Cursor.Loc, "\n"+v.Buf.Line(v.Cursor.Y))
1117                 // v.Cursor.Right()
1118         }
1119
1120         messenger.Message("Duplicated line")
1121
1122         if usePlugin {
1123                 return PostActionCall("DuplicateLine", v)
1124         }
1125         return true
1126 }
1127
1128 // DeleteLine deletes the current line
1129 func (v *View) DeleteLine(usePlugin bool) bool {
1130         if usePlugin && !PreActionCall("DeleteLine", v) {
1131                 return false
1132         }
1133
1134         v.Cursor.SelectLine()
1135         if !v.Cursor.HasSelection() {
1136                 return false
1137         }
1138         v.Cursor.DeleteSelection()
1139         v.Cursor.ResetSelection()
1140         messenger.Message("Deleted line")
1141
1142         if usePlugin {
1143                 return PostActionCall("DeleteLine", v)
1144         }
1145         return true
1146 }
1147
1148 // MoveLinesUp moves up the current line or selected lines if any
1149 func (v *View) MoveLinesUp(usePlugin bool) bool {
1150         if usePlugin && !PreActionCall("MoveLinesUp", v) {
1151                 return false
1152         }
1153
1154         if v.Cursor.HasSelection() {
1155                 if v.Cursor.CurSelection[0].Y == 0 {
1156                         messenger.Message("Can not move further up")
1157                         return true
1158                 }
1159                 start := v.Cursor.CurSelection[0].Y
1160                 end := v.Cursor.CurSelection[1].Y
1161                 if start > end {
1162                         end, start = start, end
1163                 }
1164
1165                 v.Buf.MoveLinesUp(
1166                         start,
1167                         end,
1168                 )
1169                 v.Cursor.CurSelection[1].Y -= 1
1170                 messenger.Message("Moved up selected line(s)")
1171         } else {
1172                 if v.Cursor.Loc.Y == 0 {
1173                         messenger.Message("Can not move further up")
1174                         return true
1175                 }
1176                 v.Buf.MoveLinesUp(
1177                         v.Cursor.Loc.Y,
1178                         v.Cursor.Loc.Y+1,
1179                 )
1180                 messenger.Message("Moved up current line")
1181         }
1182         v.Buf.IsModified = true
1183
1184         if usePlugin {
1185                 return PostActionCall("MoveLinesUp", v)
1186         }
1187         return true
1188 }
1189
1190 // MoveLinesDown moves down the current line or selected lines if any
1191 func (v *View) MoveLinesDown(usePlugin bool) bool {
1192         if usePlugin && !PreActionCall("MoveLinesDown", v) {
1193                 return false
1194         }
1195
1196         if v.Cursor.HasSelection() {
1197                 if v.Cursor.CurSelection[1].Y >= len(v.Buf.lines) {
1198                         messenger.Message("Can not move further down")
1199                         return true
1200                 }
1201                 start := v.Cursor.CurSelection[0].Y
1202                 end := v.Cursor.CurSelection[1].Y
1203                 if start > end {
1204                         end, start = start, end
1205                 }
1206
1207                 v.Buf.MoveLinesDown(
1208                         start,
1209                         end,
1210                 )
1211                 messenger.Message("Moved down selected line(s)")
1212         } else {
1213                 if v.Cursor.Loc.Y >= len(v.Buf.lines)-1 {
1214                         messenger.Message("Can not move further down")
1215                         return true
1216                 }
1217                 v.Buf.MoveLinesDown(
1218                         v.Cursor.Loc.Y,
1219                         v.Cursor.Loc.Y+1,
1220                 )
1221                 messenger.Message("Moved down current line")
1222         }
1223         v.Buf.IsModified = true
1224
1225         if usePlugin {
1226                 return PostActionCall("MoveLinesDown", v)
1227         }
1228         return true
1229 }
1230
1231 // Paste whatever is in the system clipboard into the buffer
1232 // Delete and paste if the user has a selection
1233 func (v *View) Paste(usePlugin bool) bool {
1234         if usePlugin && !PreActionCall("Paste", v) {
1235                 return false
1236         }
1237
1238         clip, _ := clipboard.ReadAll("clipboard")
1239         v.paste(clip)
1240
1241         if usePlugin {
1242                 return PostActionCall("Paste", v)
1243         }
1244         return true
1245 }
1246
1247 // PastePrimary pastes from the primary clipboard (only use on linux)
1248 func (v *View) PastePrimary(usePlugin bool) bool {
1249         if usePlugin && !PreActionCall("Paste", v) {
1250                 return false
1251         }
1252
1253         clip, _ := clipboard.ReadAll("primary")
1254         v.paste(clip)
1255
1256         if usePlugin {
1257                 return PostActionCall("Paste", v)
1258         }
1259         return true
1260 }
1261
1262 // SelectAll selects the entire buffer
1263 func (v *View) SelectAll(usePlugin bool) bool {
1264         if usePlugin && !PreActionCall("SelectAll", v) {
1265                 return false
1266         }
1267
1268         v.Cursor.SetSelectionStart(v.Buf.Start())
1269         v.Cursor.SetSelectionEnd(v.Buf.End())
1270         // Put the cursor at the beginning
1271         v.Cursor.X = 0
1272         v.Cursor.Y = 0
1273
1274         if usePlugin {
1275                 return PostActionCall("SelectAll", v)
1276         }
1277         return true
1278 }
1279
1280 // OpenFile opens a new file in the buffer
1281 func (v *View) OpenFile(usePlugin bool) bool {
1282         if v.mainCursor() {
1283                 if usePlugin && !PreActionCall("OpenFile", v) {
1284                         return false
1285                 }
1286
1287                 if v.CanClose() {
1288                         input, canceled := messenger.Prompt("> ", "open ", "Open", CommandCompletion)
1289                         if !canceled {
1290                                 HandleCommand(input)
1291                                 if usePlugin {
1292                                         return PostActionCall("OpenFile", v)
1293                                 }
1294                         }
1295                 }
1296         }
1297         return false
1298 }
1299
1300 // Start moves the viewport to the start of the buffer
1301 func (v *View) Start(usePlugin bool) bool {
1302         if v.mainCursor() {
1303                 if usePlugin && !PreActionCall("Start", v) {
1304                         return false
1305                 }
1306
1307                 v.Topline = 0
1308
1309                 if usePlugin {
1310                         return PostActionCall("Start", v)
1311                 }
1312         }
1313         return false
1314 }
1315
1316 // End moves the viewport to the end of the buffer
1317 func (v *View) End(usePlugin bool) bool {
1318         if v.mainCursor() {
1319                 if usePlugin && !PreActionCall("End", v) {
1320                         return false
1321                 }
1322
1323                 if v.Height > v.Buf.NumLines {
1324                         v.Topline = 0
1325                 } else {
1326                         v.Topline = v.Buf.NumLines - v.Height
1327                 }
1328
1329                 if usePlugin {
1330                         return PostActionCall("End", v)
1331                 }
1332         }
1333         return false
1334 }
1335
1336 // PageUp scrolls the view up a page
1337 func (v *View) PageUp(usePlugin bool) bool {
1338         if v.mainCursor() {
1339                 if usePlugin && !PreActionCall("PageUp", v) {
1340                         return false
1341                 }
1342
1343                 if v.Topline > v.Height {
1344                         v.ScrollUp(v.Height)
1345                 } else {
1346                         v.Topline = 0
1347                 }
1348
1349                 if usePlugin {
1350                         return PostActionCall("PageUp", v)
1351                 }
1352         }
1353         return false
1354 }
1355
1356 // PageDown scrolls the view down a page
1357 func (v *View) PageDown(usePlugin bool) bool {
1358         if v.mainCursor() {
1359                 if usePlugin && !PreActionCall("PageDown", v) {
1360                         return false
1361                 }
1362
1363                 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height {
1364                         v.ScrollDown(v.Height)
1365                 } else if v.Buf.NumLines >= v.Height {
1366                         v.Topline = v.Buf.NumLines - v.Height
1367                 }
1368
1369                 if usePlugin {
1370                         return PostActionCall("PageDown", v)
1371                 }
1372         }
1373         return false
1374 }
1375
1376 // CursorPageUp places the cursor a page up
1377 func (v *View) CursorPageUp(usePlugin bool) bool {
1378         if usePlugin && !PreActionCall("CursorPageUp", v) {
1379                 return false
1380         }
1381
1382         v.deselect(0)
1383
1384         if v.Cursor.HasSelection() {
1385                 v.Cursor.Loc = v.Cursor.CurSelection[0]
1386                 v.Cursor.ResetSelection()
1387                 v.Cursor.StoreVisualX()
1388         }
1389         v.Cursor.UpN(v.Height)
1390
1391         if usePlugin {
1392                 return PostActionCall("CursorPageUp", v)
1393         }
1394         return true
1395 }
1396
1397 // CursorPageDown places the cursor a page up
1398 func (v *View) CursorPageDown(usePlugin bool) bool {
1399         if usePlugin && !PreActionCall("CursorPageDown", v) {
1400                 return false
1401         }
1402
1403         v.deselect(0)
1404
1405         if v.Cursor.HasSelection() {
1406                 v.Cursor.Loc = v.Cursor.CurSelection[1]
1407                 v.Cursor.ResetSelection()
1408                 v.Cursor.StoreVisualX()
1409         }
1410         v.Cursor.DownN(v.Height)
1411
1412         if usePlugin {
1413                 return PostActionCall("CursorPageDown", v)
1414         }
1415         return true
1416 }
1417
1418 // HalfPageUp scrolls the view up half a page
1419 func (v *View) HalfPageUp(usePlugin bool) bool {
1420         if v.mainCursor() {
1421                 if usePlugin && !PreActionCall("HalfPageUp", v) {
1422                         return false
1423                 }
1424
1425                 if v.Topline > v.Height/2 {
1426                         v.ScrollUp(v.Height / 2)
1427                 } else {
1428                         v.Topline = 0
1429                 }
1430
1431                 if usePlugin {
1432                         return PostActionCall("HalfPageUp", v)
1433                 }
1434         }
1435         return false
1436 }
1437
1438 // HalfPageDown scrolls the view down half a page
1439 func (v *View) HalfPageDown(usePlugin bool) bool {
1440         if v.mainCursor() {
1441                 if usePlugin && !PreActionCall("HalfPageDown", v) {
1442                         return false
1443                 }
1444
1445                 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height/2 {
1446                         v.ScrollDown(v.Height / 2)
1447                 } else {
1448                         if v.Buf.NumLines >= v.Height {
1449                                 v.Topline = v.Buf.NumLines - v.Height
1450                         }
1451                 }
1452
1453                 if usePlugin {
1454                         return PostActionCall("HalfPageDown", v)
1455                 }
1456         }
1457         return false
1458 }
1459
1460 // ToggleRuler turns line numbers off and on
1461 func (v *View) ToggleRuler(usePlugin bool) bool {
1462         if v.mainCursor() {
1463                 if usePlugin && !PreActionCall("ToggleRuler", v) {
1464                         return false
1465                 }
1466
1467                 if v.Buf.Settings["ruler"] == false {
1468                         v.Buf.Settings["ruler"] = true
1469                         messenger.Message("Enabled ruler")
1470                 } else {
1471                         v.Buf.Settings["ruler"] = false
1472                         messenger.Message("Disabled ruler")
1473                 }
1474
1475                 if usePlugin {
1476                         return PostActionCall("ToggleRuler", v)
1477                 }
1478         }
1479         return false
1480 }
1481
1482 // JumpLine jumps to a line and moves the view accordingly.
1483 func (v *View) JumpLine(usePlugin bool) bool {
1484         if usePlugin && !PreActionCall("JumpLine", v) {
1485                 return false
1486         }
1487
1488         // Prompt for line number
1489         message := fmt.Sprintf("Jump to line (1 - %v) # ", v.Buf.NumLines)
1490         linestring, canceled := messenger.Prompt(message, "", "LineNumber", NoCompletion)
1491         if canceled {
1492                 return false
1493         }
1494         lineint, err := strconv.Atoi(linestring)
1495         lineint = lineint - 1 // fix offset
1496         if err != nil {
1497                 messenger.Error(err) // return errors
1498                 return false
1499         }
1500         // Move cursor and view if possible.
1501         if lineint < v.Buf.NumLines && lineint >= 0 {
1502                 v.Cursor.X = 0
1503                 v.Cursor.Y = lineint
1504
1505                 if usePlugin {
1506                         return PostActionCall("JumpLine", v)
1507                 }
1508                 return true
1509         }
1510         messenger.Error("Only ", v.Buf.NumLines, " lines to jump")
1511         return false
1512 }
1513
1514 // ClearStatus clears the messenger bar
1515 func (v *View) ClearStatus(usePlugin bool) bool {
1516         if v.mainCursor() {
1517                 if usePlugin && !PreActionCall("ClearStatus", v) {
1518                         return false
1519                 }
1520
1521                 messenger.Message("")
1522
1523                 if usePlugin {
1524                         return PostActionCall("ClearStatus", v)
1525                 }
1526         }
1527         return false
1528 }
1529
1530 // ToggleHelp toggles the help screen
1531 func (v *View) ToggleHelp(usePlugin bool) bool {
1532         if v.mainCursor() {
1533                 if usePlugin && !PreActionCall("ToggleHelp", v) {
1534                         return false
1535                 }
1536
1537                 if v.Type != vtHelp {
1538                         // Open the default help
1539                         v.openHelp("help")
1540                 } else {
1541                         v.Quit(true)
1542                 }
1543
1544                 if usePlugin {
1545                         return PostActionCall("ToggleHelp", v)
1546                 }
1547         }
1548         return true
1549 }
1550
1551 // ShellMode opens a terminal to run a shell command
1552 func (v *View) ShellMode(usePlugin bool) bool {
1553         if v.mainCursor() {
1554                 if usePlugin && !PreActionCall("ShellMode", v) {
1555                         return false
1556                 }
1557
1558                 input, canceled := messenger.Prompt("$ ", "", "Shell", NoCompletion)
1559                 if !canceled {
1560                         // The true here is for openTerm to make the command interactive
1561                         HandleShellCommand(input, true, true)
1562                         if usePlugin {
1563                                 return PostActionCall("ShellMode", v)
1564                         }
1565                 }
1566         }
1567         return false
1568 }
1569
1570 // CommandMode lets the user enter a command
1571 func (v *View) CommandMode(usePlugin bool) bool {
1572         if v.mainCursor() {
1573                 if usePlugin && !PreActionCall("CommandMode", v) {
1574                         return false
1575                 }
1576
1577                 input, canceled := messenger.Prompt("> ", "", "Command", CommandCompletion)
1578                 if !canceled {
1579                         HandleCommand(input)
1580                         if usePlugin {
1581                                 return PostActionCall("CommandMode", v)
1582                         }
1583                 }
1584         }
1585
1586         return false
1587 }
1588
1589 // Escape leaves current mode
1590 func (v *View) Escape(usePlugin bool) bool {
1591         if v.mainCursor() {
1592                 // check if user is searching, or the last search is still active
1593                 if searching || lastSearch != "" {
1594                         ExitSearch(v)
1595                         return true
1596                 }
1597                 // check if a prompt is shown, hide it and don't quit
1598                 if messenger.hasPrompt {
1599                         messenger.Reset() // FIXME
1600                         return true
1601                 }
1602         }
1603
1604         return false
1605 }
1606
1607 // Quit this will close the current tab or view that is open
1608 func (v *View) Quit(usePlugin bool) bool {
1609         if v.mainCursor() {
1610                 if usePlugin && !PreActionCall("Quit", v) {
1611                         return false
1612                 }
1613
1614                 // Make sure not to quit if there are unsaved changes
1615                 if v.CanClose() {
1616                         v.CloseBuffer()
1617                         if len(tabs[curTab].views) > 1 {
1618                                 v.splitNode.Delete()
1619                                 tabs[v.TabNum].Cleanup()
1620                                 tabs[v.TabNum].Resize()
1621                         } else if len(tabs) > 1 {
1622                                 if len(tabs[v.TabNum].views) == 1 {
1623                                         tabs = tabs[:v.TabNum+copy(tabs[v.TabNum:], tabs[v.TabNum+1:])]
1624                                         for i, t := range tabs {
1625                                                 t.SetNum(i)
1626                                         }
1627                                         if curTab >= len(tabs) {
1628                                                 curTab--
1629                                         }
1630                                         if curTab == 0 {
1631                                                 CurView().ToggleTabbar()
1632                                         }
1633                                 }
1634                         } else {
1635                                 if usePlugin {
1636                                         PostActionCall("Quit", v)
1637                                 }
1638
1639                                 screen.Fini()
1640                                 os.Exit(0)
1641                         }
1642                 }
1643
1644                 if usePlugin {
1645                         return PostActionCall("Quit", v)
1646                 }
1647         }
1648         return false
1649 }
1650
1651 // QuitAll quits the whole editor; all splits and tabs
1652 func (v *View) QuitAll(usePlugin bool) bool {
1653         if v.mainCursor() {
1654                 if usePlugin && !PreActionCall("QuitAll", v) {
1655                         return false
1656                 }
1657
1658                 closeAll := true
1659                 for _, tab := range tabs {
1660                         for _, v := range tab.views {
1661                                 if !v.CanClose() {
1662                                         closeAll = false
1663                                 }
1664                         }
1665                 }
1666
1667                 if closeAll {
1668                         // only quit if all of the buffers can be closed and the user confirms that they actually want to quit everything
1669                         shouldQuit, _ := messenger.YesNoPrompt("Do you want to quit micro (all open files will be closed)?")
1670
1671                         if shouldQuit {
1672                                 for _, tab := range tabs {
1673                                         for _, v := range tab.views {
1674                                                 v.CloseBuffer()
1675                                         }
1676                                 }
1677
1678                                 if usePlugin {
1679                                         PostActionCall("QuitAll", v)
1680                                 }
1681
1682                                 screen.Fini()
1683                                 os.Exit(0)
1684                         }
1685                 }
1686         }
1687
1688         return false
1689 }
1690
1691 // AddTab adds a new tab with an empty buffer
1692 func (v *View) AddTab(usePlugin bool) bool {
1693         if v.mainCursor() {
1694                 if usePlugin && !PreActionCall("AddTab", v) {
1695                         return false
1696                 }
1697
1698                 tab := NewTabFromView(NewView(NewBufferFromString("", "")))
1699                 tab.SetNum(len(tabs))
1700                 tabs = append(tabs, tab)
1701                 curTab = len(tabs) - 1
1702                 if len(tabs) == 2 {
1703                         for _, t := range tabs {
1704                                 for _, v := range t.views {
1705                                         v.ToggleTabbar()
1706                                 }
1707                         }
1708                 }
1709
1710                 if usePlugin {
1711                         return PostActionCall("AddTab", v)
1712                 }
1713         }
1714         return true
1715 }
1716
1717 // PreviousTab switches to the previous tab in the tab list
1718 func (v *View) PreviousTab(usePlugin bool) bool {
1719         if v.mainCursor() {
1720                 if usePlugin && !PreActionCall("PreviousTab", v) {
1721                         return false
1722                 }
1723
1724                 if curTab > 0 {
1725                         curTab--
1726                 } else if curTab == 0 {
1727                         curTab = len(tabs) - 1
1728                 }
1729
1730                 if usePlugin {
1731                         return PostActionCall("PreviousTab", v)
1732                 }
1733         }
1734         return false
1735 }
1736
1737 // NextTab switches to the next tab in the tab list
1738 func (v *View) NextTab(usePlugin bool) bool {
1739         if v.mainCursor() {
1740                 if usePlugin && !PreActionCall("NextTab", v) {
1741                         return false
1742                 }
1743
1744                 if curTab < len(tabs)-1 {
1745                         curTab++
1746                 } else if curTab == len(tabs)-1 {
1747                         curTab = 0
1748                 }
1749
1750                 if usePlugin {
1751                         return PostActionCall("NextTab", v)
1752                 }
1753         }
1754         return false
1755 }
1756
1757 // VSplitBinding opens an empty vertical split
1758 func (v *View) VSplitBinding(usePlugin bool) bool {
1759         if v.mainCursor() {
1760                 if usePlugin && !PreActionCall("VSplit", v) {
1761                         return false
1762                 }
1763
1764                 v.VSplit(NewBufferFromString("", ""))
1765
1766                 if usePlugin {
1767                         return PostActionCall("VSplit", v)
1768                 }
1769         }
1770         return false
1771 }
1772
1773 // HSplitBinding opens an empty horizontal split
1774 func (v *View) HSplitBinding(usePlugin bool) bool {
1775         if v.mainCursor() {
1776                 if usePlugin && !PreActionCall("HSplit", v) {
1777                         return false
1778                 }
1779
1780                 v.HSplit(NewBufferFromString("", ""))
1781
1782                 if usePlugin {
1783                         return PostActionCall("HSplit", v)
1784                 }
1785         }
1786         return false
1787 }
1788
1789 // Unsplit closes all splits in the current tab except the active one
1790 func (v *View) Unsplit(usePlugin bool) bool {
1791         if v.mainCursor() {
1792                 if usePlugin && !PreActionCall("Unsplit", v) {
1793                         return false
1794                 }
1795
1796                 curView := tabs[curTab].CurView
1797                 for i := len(tabs[curTab].views) - 1; i >= 0; i-- {
1798                         view := tabs[curTab].views[i]
1799                         if view != nil && view.Num != curView {
1800                                 view.Quit(true)
1801                                 // messenger.Message("Quit ", view.Buf.Path)
1802                         }
1803                 }
1804
1805                 if usePlugin {
1806                         return PostActionCall("Unsplit", v)
1807                 }
1808         }
1809         return false
1810 }
1811
1812 // NextSplit changes the view to the next split
1813 func (v *View) NextSplit(usePlugin bool) bool {
1814         if v.mainCursor() {
1815                 if usePlugin && !PreActionCall("NextSplit", v) {
1816                         return false
1817                 }
1818
1819                 tab := tabs[curTab]
1820                 if tab.CurView < len(tab.views)-1 {
1821                         tab.CurView++
1822                 } else {
1823                         tab.CurView = 0
1824                 }
1825
1826                 if usePlugin {
1827                         return PostActionCall("NextSplit", v)
1828                 }
1829         }
1830         return false
1831 }
1832
1833 // PreviousSplit changes the view to the previous split
1834 func (v *View) PreviousSplit(usePlugin bool) bool {
1835         if v.mainCursor() {
1836                 if usePlugin && !PreActionCall("PreviousSplit", v) {
1837                         return false
1838                 }
1839
1840                 tab := tabs[curTab]
1841                 if tab.CurView > 0 {
1842                         tab.CurView--
1843                 } else {
1844                         tab.CurView = len(tab.views) - 1
1845                 }
1846
1847                 if usePlugin {
1848                         return PostActionCall("PreviousSplit", v)
1849                 }
1850         }
1851         return false
1852 }
1853
1854 var curMacro []interface{}
1855 var recordingMacro bool
1856
1857 // ToggleMacro toggles recording of a macro
1858 func (v *View) ToggleMacro(usePlugin bool) bool {
1859         if v.mainCursor() {
1860                 if usePlugin && !PreActionCall("ToggleMacro", v) {
1861                         return false
1862                 }
1863
1864                 recordingMacro = !recordingMacro
1865
1866                 if recordingMacro {
1867                         curMacro = []interface{}{}
1868                         messenger.Message("Recording")
1869                 } else {
1870                         messenger.Message("Stopped recording")
1871                 }
1872
1873                 if usePlugin {
1874                         return PostActionCall("ToggleMacro", v)
1875                 }
1876         }
1877         return true
1878 }
1879
1880 // PlayMacro plays back the most recently recorded macro
1881 func (v *View) PlayMacro(usePlugin bool) bool {
1882         if usePlugin && !PreActionCall("PlayMacro", v) {
1883                 return false
1884         }
1885
1886         for _, action := range curMacro {
1887                 switch t := action.(type) {
1888                 case rune:
1889                         // Insert a character
1890                         if v.Cursor.HasSelection() {
1891                                 v.Cursor.DeleteSelection()
1892                                 v.Cursor.ResetSelection()
1893                         }
1894                         v.Buf.Insert(v.Cursor.Loc, string(t))
1895                         // v.Cursor.Right()
1896
1897                         for pl := range loadedPlugins {
1898                                 _, err := Call(pl+".onRune", string(t), v)
1899                                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
1900                                         TermMessage(err)
1901                                 }
1902                         }
1903                 case func(*View, bool) bool:
1904                         t(v, true)
1905                 }
1906         }
1907
1908         if usePlugin {
1909                 return PostActionCall("PlayMacro", v)
1910         }
1911         return true
1912 }
1913
1914 // SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
1915 func (v *View) SpawnMultiCursor(usePlugin bool) bool {
1916         spawner := v.Buf.cursors[len(v.Buf.cursors)-1]
1917         // You can only spawn a cursor from the main cursor
1918         if v.Cursor == spawner {
1919                 if usePlugin && !PreActionCall("SpawnMultiCursor", v) {
1920                         return false
1921                 }
1922
1923                 if !spawner.HasSelection() {
1924                         spawner.SelectWord()
1925                 } else {
1926                         c := &Cursor{
1927                                 buf: v.Buf,
1928                         }
1929
1930                         sel := spawner.GetSelection()
1931
1932                         searchStart = spawner.CurSelection[1]
1933                         v.Cursor = c
1934                         Search(sel, v, true)
1935
1936                         for _, cur := range v.Buf.cursors {
1937                                 if c.Loc == cur.Loc {
1938                                         return false
1939                                 }
1940                         }
1941                         v.Buf.cursors = append(v.Buf.cursors, c)
1942                         v.Buf.UpdateCursors()
1943                         v.Relocate()
1944                         v.Cursor = spawner
1945                 }
1946
1947                 if usePlugin {
1948                         PostActionCall("SpawnMultiCursor", v)
1949                 }
1950         }
1951
1952         return false
1953 }
1954
1955 // MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
1956 func (v *View) MouseMultiCursor(usePlugin bool, e *tcell.EventMouse) bool {
1957         if v.Cursor == &v.Buf.Cursor {
1958                 if usePlugin && !PreActionCall("SpawnMultiCursorAtMouse", v, e) {
1959                         return false
1960                 }
1961                 x, y := e.Position()
1962                 x -= v.lineNumOffset - v.leftCol + v.x
1963                 y += v.Topline - v.y
1964
1965                 c := &Cursor{
1966                         buf: v.Buf,
1967                 }
1968                 v.Cursor = c
1969                 v.MoveToMouseClick(x, y)
1970                 v.Relocate()
1971                 v.Cursor = &v.Buf.Cursor
1972
1973                 v.Buf.cursors = append(v.Buf.cursors, c)
1974                 v.Buf.MergeCursors()
1975                 v.Buf.UpdateCursors()
1976
1977                 if usePlugin {
1978                         PostActionCall("SpawnMultiCursorAtMouse", v)
1979                 }
1980         }
1981         return false
1982 }
1983
1984 // SkipMultiCursor moves the current multiple cursor to the next available position
1985 func (v *View) SkipMultiCursor(usePlugin bool) bool {
1986         cursor := v.Buf.cursors[len(v.Buf.cursors)-1]
1987
1988         if v.mainCursor() {
1989                 if usePlugin && !PreActionCall("SkipMultiCursor", v) {
1990                         return false
1991                 }
1992                 sel := cursor.GetSelection()
1993
1994                 searchStart = cursor.CurSelection[1]
1995                 v.Cursor = cursor
1996                 Search(sel, v, true)
1997                 v.Relocate()
1998                 v.Cursor = cursor
1999
2000                 if usePlugin {
2001                         PostActionCall("SkipMultiCursor", v)
2002                 }
2003         }
2004         return false
2005 }
2006
2007 // RemoveMultiCursor removes the latest multiple cursor
2008 func (v *View) RemoveMultiCursor(usePlugin bool) bool {
2009         end := len(v.Buf.cursors)
2010         if end > 1 {
2011                 if v.mainCursor() {
2012                         if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
2013                                 return false
2014                         }
2015
2016                         v.Buf.cursors[end-1] = nil
2017                         v.Buf.cursors = v.Buf.cursors[:end-1]
2018                         v.Buf.UpdateCursors()
2019                         v.Relocate()
2020
2021                         if usePlugin {
2022                                 return PostActionCall("RemoveMultiCursor", v)
2023                         }
2024                         return true
2025                 }
2026         } else {
2027                 v.RemoveAllMultiCursors(usePlugin)
2028         }
2029         return false
2030 }
2031
2032 // RemoveAllMultiCursors removes all cursors except the base cursor
2033 func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
2034         if v.mainCursor() {
2035                 if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
2036                         return false
2037                 }
2038
2039                 v.Buf.clearCursors()
2040                 v.Relocate()
2041
2042                 if usePlugin {
2043                         return PostActionCall("RemoveAllMultiCursors", v)
2044                 }
2045                 return true
2046         }
2047         return false
2048 }