]> git.lizzy.rs Git - micro.git/blob - cmd/micro/actions.go
0042b00add2041f95fac9dc4e67ccf37faf8139c
[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         v.Buf.Undo()
1000         messenger.Message("Undid action")
1001
1002         if usePlugin {
1003                 return PostActionCall("Undo", v)
1004         }
1005         return true
1006 }
1007
1008 // Redo redoes the last action
1009 func (v *View) Redo(usePlugin bool) bool {
1010         if usePlugin && !PreActionCall("Redo", v) {
1011                 return false
1012         }
1013
1014         v.Buf.Redo()
1015         messenger.Message("Redid action")
1016
1017         if usePlugin {
1018                 return PostActionCall("Redo", v)
1019         }
1020         return true
1021 }
1022
1023 // Copy the selection to the system clipboard
1024 func (v *View) Copy(usePlugin bool) bool {
1025         if v.mainCursor() {
1026                 if usePlugin && !PreActionCall("Copy", v) {
1027                         return false
1028                 }
1029
1030                 if v.Cursor.HasSelection() {
1031                         v.Cursor.CopySelection("clipboard")
1032                         v.freshClip = true
1033                         messenger.Message("Copied selection")
1034                 }
1035
1036                 if usePlugin {
1037                         return PostActionCall("Copy", v)
1038                 }
1039         }
1040         return true
1041 }
1042
1043 // CutLine cuts the current line to the clipboard
1044 func (v *View) CutLine(usePlugin bool) bool {
1045         if usePlugin && !PreActionCall("CutLine", v) {
1046                 return false
1047         }
1048
1049         v.Cursor.SelectLine()
1050         if !v.Cursor.HasSelection() {
1051                 return false
1052         }
1053         if v.freshClip == true {
1054                 if v.Cursor.HasSelection() {
1055                         if clip, err := clipboard.ReadAll("clipboard"); err != nil {
1056                                 messenger.Error(err)
1057                         } else {
1058                                 clipboard.WriteAll(clip+v.Cursor.GetSelection(), "clipboard")
1059                         }
1060                 }
1061         } else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
1062                 v.Copy(true)
1063         }
1064         v.freshClip = true
1065         v.lastCutTime = time.Now()
1066         v.Cursor.DeleteSelection()
1067         v.Cursor.ResetSelection()
1068         messenger.Message("Cut line")
1069
1070         if usePlugin {
1071                 return PostActionCall("CutLine", v)
1072         }
1073         return true
1074 }
1075
1076 // Cut the selection to the system clipboard
1077 func (v *View) Cut(usePlugin bool) bool {
1078         if usePlugin && !PreActionCall("Cut", v) {
1079                 return false
1080         }
1081
1082         if v.Cursor.HasSelection() {
1083                 v.Cursor.CopySelection("clipboard")
1084                 v.Cursor.DeleteSelection()
1085                 v.Cursor.ResetSelection()
1086                 v.freshClip = true
1087                 messenger.Message("Cut selection")
1088
1089                 if usePlugin {
1090                         return PostActionCall("Cut", v)
1091                 }
1092                 return true
1093         }
1094
1095         return false
1096 }
1097
1098 // DuplicateLine duplicates the current line or selection
1099 func (v *View) DuplicateLine(usePlugin bool) bool {
1100         if usePlugin && !PreActionCall("DuplicateLine", v) {
1101                 return false
1102         }
1103
1104         if v.Cursor.HasSelection() {
1105                 v.Buf.Insert(v.Cursor.CurSelection[1], v.Cursor.GetSelection())
1106         } else {
1107                 v.Cursor.End()
1108                 v.Buf.Insert(v.Cursor.Loc, "\n"+v.Buf.Line(v.Cursor.Y))
1109                 // v.Cursor.Right()
1110         }
1111
1112         messenger.Message("Duplicated line")
1113
1114         if usePlugin {
1115                 return PostActionCall("DuplicateLine", v)
1116         }
1117         return true
1118 }
1119
1120 // DeleteLine deletes the current line
1121 func (v *View) DeleteLine(usePlugin bool) bool {
1122         if usePlugin && !PreActionCall("DeleteLine", v) {
1123                 return false
1124         }
1125
1126         v.Cursor.SelectLine()
1127         if !v.Cursor.HasSelection() {
1128                 return false
1129         }
1130         v.Cursor.DeleteSelection()
1131         v.Cursor.ResetSelection()
1132         messenger.Message("Deleted line")
1133
1134         if usePlugin {
1135                 return PostActionCall("DeleteLine", v)
1136         }
1137         return true
1138 }
1139
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) {
1143                 return false
1144         }
1145
1146         if v.Cursor.HasSelection() {
1147                 if v.Cursor.CurSelection[0].Y == 0 {
1148                         messenger.Message("Can not move further up")
1149                         return true
1150                 }
1151                 start := v.Cursor.CurSelection[0].Y
1152                 end := v.Cursor.CurSelection[1].Y
1153                 if start > end {
1154                         end, start = start, end
1155                 }
1156
1157                 v.Buf.MoveLinesUp(
1158                         start,
1159                         end,
1160                 )
1161                 v.Cursor.CurSelection[1].Y -= 1
1162                 messenger.Message("Moved up selected line(s)")
1163         } else {
1164                 if v.Cursor.Loc.Y == 0 {
1165                         messenger.Message("Can not move further up")
1166                         return true
1167                 }
1168                 v.Buf.MoveLinesUp(
1169                         v.Cursor.Loc.Y,
1170                         v.Cursor.Loc.Y+1,
1171                 )
1172                 messenger.Message("Moved up current line")
1173         }
1174         v.Buf.IsModified = true
1175
1176         if usePlugin {
1177                 return PostActionCall("MoveLinesUp", v)
1178         }
1179         return true
1180 }
1181
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) {
1185                 return false
1186         }
1187
1188         if v.Cursor.HasSelection() {
1189                 if v.Cursor.CurSelection[1].Y >= len(v.Buf.lines) {
1190                         messenger.Message("Can not move further down")
1191                         return true
1192                 }
1193                 start := v.Cursor.CurSelection[0].Y
1194                 end := v.Cursor.CurSelection[1].Y
1195                 if start > end {
1196                         end, start = start, end
1197                 }
1198
1199                 v.Buf.MoveLinesDown(
1200                         start,
1201                         end,
1202                 )
1203                 messenger.Message("Moved down selected line(s)")
1204         } else {
1205                 if v.Cursor.Loc.Y >= len(v.Buf.lines)-1 {
1206                         messenger.Message("Can not move further down")
1207                         return true
1208                 }
1209                 v.Buf.MoveLinesDown(
1210                         v.Cursor.Loc.Y,
1211                         v.Cursor.Loc.Y+1,
1212                 )
1213                 messenger.Message("Moved down current line")
1214         }
1215         v.Buf.IsModified = true
1216
1217         if usePlugin {
1218                 return PostActionCall("MoveLinesDown", v)
1219         }
1220         return true
1221 }
1222
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) {
1227                 return false
1228         }
1229
1230         clip, _ := clipboard.ReadAll("clipboard")
1231         v.paste(clip)
1232
1233         if usePlugin {
1234                 return PostActionCall("Paste", v)
1235         }
1236         return true
1237 }
1238
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) {
1242                 return false
1243         }
1244
1245         clip, _ := clipboard.ReadAll("primary")
1246         v.paste(clip)
1247
1248         if usePlugin {
1249                 return PostActionCall("Paste", v)
1250         }
1251         return true
1252 }
1253
1254 // SelectAll selects the entire buffer
1255 func (v *View) SelectAll(usePlugin bool) bool {
1256         if usePlugin && !PreActionCall("SelectAll", v) {
1257                 return false
1258         }
1259
1260         v.Cursor.SetSelectionStart(v.Buf.Start())
1261         v.Cursor.SetSelectionEnd(v.Buf.End())
1262         // Put the cursor at the beginning
1263         v.Cursor.X = 0
1264         v.Cursor.Y = 0
1265
1266         if usePlugin {
1267                 return PostActionCall("SelectAll", v)
1268         }
1269         return true
1270 }
1271
1272 // OpenFile opens a new file in the buffer
1273 func (v *View) OpenFile(usePlugin bool) bool {
1274         if v.mainCursor() {
1275                 if usePlugin && !PreActionCall("OpenFile", v) {
1276                         return false
1277                 }
1278
1279                 if v.CanClose() {
1280                         input, canceled := messenger.Prompt("> ", "open ", "Open", CommandCompletion)
1281                         if !canceled {
1282                                 HandleCommand(input)
1283                                 if usePlugin {
1284                                         return PostActionCall("OpenFile", v)
1285                                 }
1286                         }
1287                 }
1288         }
1289         return false
1290 }
1291
1292 // Start moves the viewport to the start of the buffer
1293 func (v *View) Start(usePlugin bool) bool {
1294         if v.mainCursor() {
1295                 if usePlugin && !PreActionCall("Start", v) {
1296                         return false
1297                 }
1298
1299                 v.Topline = 0
1300
1301                 if usePlugin {
1302                         return PostActionCall("Start", v)
1303                 }
1304         }
1305         return false
1306 }
1307
1308 // End moves the viewport to the end of the buffer
1309 func (v *View) End(usePlugin bool) bool {
1310         if v.mainCursor() {
1311                 if usePlugin && !PreActionCall("End", v) {
1312                         return false
1313                 }
1314
1315                 if v.Height > v.Buf.NumLines {
1316                         v.Topline = 0
1317                 } else {
1318                         v.Topline = v.Buf.NumLines - v.Height
1319                 }
1320
1321                 if usePlugin {
1322                         return PostActionCall("End", v)
1323                 }
1324         }
1325         return false
1326 }
1327
1328 // PageUp scrolls the view up a page
1329 func (v *View) PageUp(usePlugin bool) bool {
1330         if v.mainCursor() {
1331                 if usePlugin && !PreActionCall("PageUp", v) {
1332                         return false
1333                 }
1334
1335                 if v.Topline > v.Height {
1336                         v.ScrollUp(v.Height)
1337                 } else {
1338                         v.Topline = 0
1339                 }
1340
1341                 if usePlugin {
1342                         return PostActionCall("PageUp", v)
1343                 }
1344         }
1345         return false
1346 }
1347
1348 // PageDown scrolls the view down a page
1349 func (v *View) PageDown(usePlugin bool) bool {
1350         if v.mainCursor() {
1351                 if usePlugin && !PreActionCall("PageDown", v) {
1352                         return false
1353                 }
1354
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
1359                 }
1360
1361                 if usePlugin {
1362                         return PostActionCall("PageDown", v)
1363                 }
1364         }
1365         return false
1366 }
1367
1368 // CursorPageUp places the cursor a page up
1369 func (v *View) CursorPageUp(usePlugin bool) bool {
1370         if usePlugin && !PreActionCall("CursorPageUp", v) {
1371                 return false
1372         }
1373
1374         v.deselect(0)
1375
1376         if v.Cursor.HasSelection() {
1377                 v.Cursor.Loc = v.Cursor.CurSelection[0]
1378                 v.Cursor.ResetSelection()
1379                 v.Cursor.StoreVisualX()
1380         }
1381         v.Cursor.UpN(v.Height)
1382
1383         if usePlugin {
1384                 return PostActionCall("CursorPageUp", v)
1385         }
1386         return true
1387 }
1388
1389 // CursorPageDown places the cursor a page up
1390 func (v *View) CursorPageDown(usePlugin bool) bool {
1391         if usePlugin && !PreActionCall("CursorPageDown", v) {
1392                 return false
1393         }
1394
1395         v.deselect(0)
1396
1397         if v.Cursor.HasSelection() {
1398                 v.Cursor.Loc = v.Cursor.CurSelection[1]
1399                 v.Cursor.ResetSelection()
1400                 v.Cursor.StoreVisualX()
1401         }
1402         v.Cursor.DownN(v.Height)
1403
1404         if usePlugin {
1405                 return PostActionCall("CursorPageDown", v)
1406         }
1407         return true
1408 }
1409
1410 // HalfPageUp scrolls the view up half a page
1411 func (v *View) HalfPageUp(usePlugin bool) bool {
1412         if v.mainCursor() {
1413                 if usePlugin && !PreActionCall("HalfPageUp", v) {
1414                         return false
1415                 }
1416
1417                 if v.Topline > v.Height/2 {
1418                         v.ScrollUp(v.Height / 2)
1419                 } else {
1420                         v.Topline = 0
1421                 }
1422
1423                 if usePlugin {
1424                         return PostActionCall("HalfPageUp", v)
1425                 }
1426         }
1427         return false
1428 }
1429
1430 // HalfPageDown scrolls the view down half a page
1431 func (v *View) HalfPageDown(usePlugin bool) bool {
1432         if v.mainCursor() {
1433                 if usePlugin && !PreActionCall("HalfPageDown", v) {
1434                         return false
1435                 }
1436
1437                 if v.Buf.NumLines-(v.Topline+v.Height) > v.Height/2 {
1438                         v.ScrollDown(v.Height / 2)
1439                 } else {
1440                         if v.Buf.NumLines >= v.Height {
1441                                 v.Topline = v.Buf.NumLines - v.Height
1442                         }
1443                 }
1444
1445                 if usePlugin {
1446                         return PostActionCall("HalfPageDown", v)
1447                 }
1448         }
1449         return false
1450 }
1451
1452 // ToggleRuler turns line numbers off and on
1453 func (v *View) ToggleRuler(usePlugin bool) bool {
1454         if v.mainCursor() {
1455                 if usePlugin && !PreActionCall("ToggleRuler", v) {
1456                         return false
1457                 }
1458
1459                 if v.Buf.Settings["ruler"] == false {
1460                         v.Buf.Settings["ruler"] = true
1461                         messenger.Message("Enabled ruler")
1462                 } else {
1463                         v.Buf.Settings["ruler"] = false
1464                         messenger.Message("Disabled ruler")
1465                 }
1466
1467                 if usePlugin {
1468                         return PostActionCall("ToggleRuler", v)
1469                 }
1470         }
1471         return false
1472 }
1473
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) {
1477                 return false
1478         }
1479
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)
1483         if canceled {
1484                 return false
1485         }
1486         lineint, err := strconv.Atoi(linestring)
1487         lineint = lineint - 1 // fix offset
1488         if err != nil {
1489                 messenger.Error(err) // return errors
1490                 return false
1491         }
1492         // Move cursor and view if possible.
1493         if lineint < v.Buf.NumLines && lineint >= 0 {
1494                 v.Cursor.X = 0
1495                 v.Cursor.Y = lineint
1496
1497                 if usePlugin {
1498                         return PostActionCall("JumpLine", v)
1499                 }
1500                 return true
1501         }
1502         messenger.Error("Only ", v.Buf.NumLines, " lines to jump")
1503         return false
1504 }
1505
1506 // ClearStatus clears the messenger bar
1507 func (v *View) ClearStatus(usePlugin bool) bool {
1508         if v.mainCursor() {
1509                 if usePlugin && !PreActionCall("ClearStatus", v) {
1510                         return false
1511                 }
1512
1513                 messenger.Message("")
1514
1515                 if usePlugin {
1516                         return PostActionCall("ClearStatus", v)
1517                 }
1518         }
1519         return false
1520 }
1521
1522 // ToggleHelp toggles the help screen
1523 func (v *View) ToggleHelp(usePlugin bool) bool {
1524         if v.mainCursor() {
1525                 if usePlugin && !PreActionCall("ToggleHelp", v) {
1526                         return false
1527                 }
1528
1529                 if v.Type != vtHelp {
1530                         // Open the default help
1531                         v.openHelp("help")
1532                 } else {
1533                         v.Quit(true)
1534                 }
1535
1536                 if usePlugin {
1537                         return PostActionCall("ToggleHelp", v)
1538                 }
1539         }
1540         return true
1541 }
1542
1543 // ShellMode opens a terminal to run a shell command
1544 func (v *View) ShellMode(usePlugin bool) bool {
1545         if v.mainCursor() {
1546                 if usePlugin && !PreActionCall("ShellMode", v) {
1547                         return false
1548                 }
1549
1550                 input, canceled := messenger.Prompt("$ ", "", "Shell", NoCompletion)
1551                 if !canceled {
1552                         // The true here is for openTerm to make the command interactive
1553                         HandleShellCommand(input, true, true)
1554                         if usePlugin {
1555                                 return PostActionCall("ShellMode", v)
1556                         }
1557                 }
1558         }
1559         return false
1560 }
1561
1562 // CommandMode lets the user enter a command
1563 func (v *View) CommandMode(usePlugin bool) bool {
1564         if v.mainCursor() {
1565                 if usePlugin && !PreActionCall("CommandMode", v) {
1566                         return false
1567                 }
1568
1569                 input, canceled := messenger.Prompt("> ", "", "Command", CommandCompletion)
1570                 if !canceled {
1571                         HandleCommand(input)
1572                         if usePlugin {
1573                                 return PostActionCall("CommandMode", v)
1574                         }
1575                 }
1576         }
1577
1578         return false
1579 }
1580
1581 // Escape leaves current mode
1582 func (v *View) Escape(usePlugin bool) bool {
1583         if v.mainCursor() {
1584                 // check if user is searching, or the last search is still active
1585                 if searching || lastSearch != "" {
1586                         ExitSearch(v)
1587                         return true
1588                 }
1589                 // check if a prompt is shown, hide it and don't quit
1590                 if messenger.hasPrompt {
1591                         messenger.Reset() // FIXME
1592                         return true
1593                 }
1594         }
1595
1596         return false
1597 }
1598
1599 // Quit this will close the current tab or view that is open
1600 func (v *View) Quit(usePlugin bool) bool {
1601         if v.mainCursor() {
1602                 if usePlugin && !PreActionCall("Quit", v) {
1603                         return false
1604                 }
1605
1606                 // Make sure not to quit if there are unsaved changes
1607                 if v.CanClose() {
1608                         v.CloseBuffer()
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 {
1617                                                 t.SetNum(i)
1618                                         }
1619                                         if curTab >= len(tabs) {
1620                                                 curTab--
1621                                         }
1622                                         if curTab == 0 {
1623                                                 CurView().ToggleTabbar()
1624                                         }
1625                                 }
1626                         } else {
1627                                 if usePlugin {
1628                                         PostActionCall("Quit", v)
1629                                 }
1630
1631                                 screen.Fini()
1632                                 os.Exit(0)
1633                         }
1634                 }
1635
1636                 if usePlugin {
1637                         return PostActionCall("Quit", v)
1638                 }
1639         }
1640         return false
1641 }
1642
1643 // QuitAll quits the whole editor; all splits and tabs
1644 func (v *View) QuitAll(usePlugin bool) bool {
1645         if v.mainCursor() {
1646                 if usePlugin && !PreActionCall("QuitAll", v) {
1647                         return false
1648                 }
1649
1650                 closeAll := true
1651                 for _, tab := range tabs {
1652                         for _, v := range tab.views {
1653                                 if !v.CanClose() {
1654                                         closeAll = false
1655                                 }
1656                         }
1657                 }
1658
1659                 if closeAll {
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)?")
1662
1663                         if shouldQuit {
1664                                 for _, tab := range tabs {
1665                                         for _, v := range tab.views {
1666                                                 v.CloseBuffer()
1667                                         }
1668                                 }
1669
1670                                 if usePlugin {
1671                                         PostActionCall("QuitAll", v)
1672                                 }
1673
1674                                 screen.Fini()
1675                                 os.Exit(0)
1676                         }
1677                 }
1678         }
1679
1680         return false
1681 }
1682
1683 // AddTab adds a new tab with an empty buffer
1684 func (v *View) AddTab(usePlugin bool) bool {
1685         if v.mainCursor() {
1686                 if usePlugin && !PreActionCall("AddTab", v) {
1687                         return false
1688                 }
1689
1690                 tab := NewTabFromView(NewView(NewBufferFromString("", "")))
1691                 tab.SetNum(len(tabs))
1692                 tabs = append(tabs, tab)
1693                 curTab = len(tabs) - 1
1694                 if len(tabs) == 2 {
1695                         for _, t := range tabs {
1696                                 for _, v := range t.views {
1697                                         v.ToggleTabbar()
1698                                 }
1699                         }
1700                 }
1701
1702                 if usePlugin {
1703                         return PostActionCall("AddTab", v)
1704                 }
1705         }
1706         return true
1707 }
1708
1709 // PreviousTab switches to the previous tab in the tab list
1710 func (v *View) PreviousTab(usePlugin bool) bool {
1711         if v.mainCursor() {
1712                 if usePlugin && !PreActionCall("PreviousTab", v) {
1713                         return false
1714                 }
1715
1716                 if curTab > 0 {
1717                         curTab--
1718                 } else if curTab == 0 {
1719                         curTab = len(tabs) - 1
1720                 }
1721
1722                 if usePlugin {
1723                         return PostActionCall("PreviousTab", v)
1724                 }
1725         }
1726         return false
1727 }
1728
1729 // NextTab switches to the next tab in the tab list
1730 func (v *View) NextTab(usePlugin bool) bool {
1731         if v.mainCursor() {
1732                 if usePlugin && !PreActionCall("NextTab", v) {
1733                         return false
1734                 }
1735
1736                 if curTab < len(tabs)-1 {
1737                         curTab++
1738                 } else if curTab == len(tabs)-1 {
1739                         curTab = 0
1740                 }
1741
1742                 if usePlugin {
1743                         return PostActionCall("NextTab", v)
1744                 }
1745         }
1746         return false
1747 }
1748
1749 // VSplitBinding opens an empty vertical split
1750 func (v *View) VSplitBinding(usePlugin bool) bool {
1751         if v.mainCursor() {
1752                 if usePlugin && !PreActionCall("VSplit", v) {
1753                         return false
1754                 }
1755
1756                 v.VSplit(NewBufferFromString("", ""))
1757
1758                 if usePlugin {
1759                         return PostActionCall("VSplit", v)
1760                 }
1761         }
1762         return false
1763 }
1764
1765 // HSplitBinding opens an empty horizontal split
1766 func (v *View) HSplitBinding(usePlugin bool) bool {
1767         if v.mainCursor() {
1768                 if usePlugin && !PreActionCall("HSplit", v) {
1769                         return false
1770                 }
1771
1772                 v.HSplit(NewBufferFromString("", ""))
1773
1774                 if usePlugin {
1775                         return PostActionCall("HSplit", v)
1776                 }
1777         }
1778         return false
1779 }
1780
1781 // Unsplit closes all splits in the current tab except the active one
1782 func (v *View) Unsplit(usePlugin bool) bool {
1783         if v.mainCursor() {
1784                 if usePlugin && !PreActionCall("Unsplit", v) {
1785                         return false
1786                 }
1787
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 {
1792                                 view.Quit(true)
1793                                 // messenger.Message("Quit ", view.Buf.Path)
1794                         }
1795                 }
1796
1797                 if usePlugin {
1798                         return PostActionCall("Unsplit", v)
1799                 }
1800         }
1801         return false
1802 }
1803
1804 // NextSplit changes the view to the next split
1805 func (v *View) NextSplit(usePlugin bool) bool {
1806         if v.mainCursor() {
1807                 if usePlugin && !PreActionCall("NextSplit", v) {
1808                         return false
1809                 }
1810
1811                 tab := tabs[curTab]
1812                 if tab.CurView < len(tab.views)-1 {
1813                         tab.CurView++
1814                 } else {
1815                         tab.CurView = 0
1816                 }
1817
1818                 if usePlugin {
1819                         return PostActionCall("NextSplit", v)
1820                 }
1821         }
1822         return false
1823 }
1824
1825 // PreviousSplit changes the view to the previous split
1826 func (v *View) PreviousSplit(usePlugin bool) bool {
1827         if v.mainCursor() {
1828                 if usePlugin && !PreActionCall("PreviousSplit", v) {
1829                         return false
1830                 }
1831
1832                 tab := tabs[curTab]
1833                 if tab.CurView > 0 {
1834                         tab.CurView--
1835                 } else {
1836                         tab.CurView = len(tab.views) - 1
1837                 }
1838
1839                 if usePlugin {
1840                         return PostActionCall("PreviousSplit", v)
1841                 }
1842         }
1843         return false
1844 }
1845
1846 var curMacro []interface{}
1847 var recordingMacro bool
1848
1849 // ToggleMacro toggles recording of a macro
1850 func (v *View) ToggleMacro(usePlugin bool) bool {
1851         if v.mainCursor() {
1852                 if usePlugin && !PreActionCall("ToggleMacro", v) {
1853                         return false
1854                 }
1855
1856                 recordingMacro = !recordingMacro
1857
1858                 if recordingMacro {
1859                         curMacro = []interface{}{}
1860                         messenger.Message("Recording")
1861                 } else {
1862                         messenger.Message("Stopped recording")
1863                 }
1864
1865                 if usePlugin {
1866                         return PostActionCall("ToggleMacro", v)
1867                 }
1868         }
1869         return true
1870 }
1871
1872 // PlayMacro plays back the most recently recorded macro
1873 func (v *View) PlayMacro(usePlugin bool) bool {
1874         if usePlugin && !PreActionCall("PlayMacro", v) {
1875                 return false
1876         }
1877
1878         for _, action := range curMacro {
1879                 switch t := action.(type) {
1880                 case rune:
1881                         // Insert a character
1882                         if v.Cursor.HasSelection() {
1883                                 v.Cursor.DeleteSelection()
1884                                 v.Cursor.ResetSelection()
1885                         }
1886                         v.Buf.Insert(v.Cursor.Loc, string(t))
1887                         // v.Cursor.Right()
1888
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") {
1892                                         TermMessage(err)
1893                                 }
1894                         }
1895                 case func(*View, bool) bool:
1896                         t(v, true)
1897                 }
1898         }
1899
1900         if usePlugin {
1901                 return PostActionCall("PlayMacro", v)
1902         }
1903         return true
1904 }
1905
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) {
1912                         return false
1913                 }
1914
1915                 if !spawner.HasSelection() {
1916                         spawner.SelectWord()
1917                 } else {
1918                         c := &Cursor{
1919                                 buf: v.Buf,
1920                         }
1921
1922                         sel := spawner.GetSelection()
1923
1924                         searchStart = spawner.CurSelection[1]
1925                         v.Cursor = c
1926                         Search(sel, v, true)
1927
1928                         for _, cur := range v.Buf.cursors {
1929                                 if c.Loc == cur.Loc {
1930                                         return false
1931                                 }
1932                         }
1933                         v.Buf.cursors = append(v.Buf.cursors, c)
1934                         v.Buf.UpdateCursors()
1935                         v.Relocate()
1936                         v.Cursor = spawner
1937                 }
1938
1939                 if usePlugin {
1940                         PostActionCall("SpawnMultiCursor", v)
1941                 }
1942         }
1943
1944         return false
1945 }
1946
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) {
1951                         return false
1952                 }
1953                 x, y := e.Position()
1954                 x -= v.lineNumOffset - v.leftCol + v.x
1955                 y += v.Topline - v.y
1956
1957                 c := &Cursor{
1958                         buf: v.Buf,
1959                 }
1960                 v.Cursor = c
1961                 v.MoveToMouseClick(x, y)
1962                 v.Relocate()
1963                 v.Cursor = &v.Buf.Cursor
1964
1965                 v.Buf.cursors = append(v.Buf.cursors, c)
1966                 v.Buf.MergeCursors()
1967                 v.Buf.UpdateCursors()
1968
1969                 if usePlugin {
1970                         PostActionCall("SpawnMultiCursorAtMouse", v)
1971                 }
1972         }
1973         return false
1974 }
1975
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]
1979
1980         if v.mainCursor() {
1981                 if usePlugin && !PreActionCall("SkipMultiCursor", v) {
1982                         return false
1983                 }
1984                 sel := cursor.GetSelection()
1985
1986                 searchStart = cursor.CurSelection[1]
1987                 v.Cursor = cursor
1988                 Search(sel, v, true)
1989                 v.Relocate()
1990                 v.Cursor = cursor
1991
1992                 if usePlugin {
1993                         PostActionCall("SkipMultiCursor", v)
1994                 }
1995         }
1996         return false
1997 }
1998
1999 // RemoveMultiCursor removes the latest multiple cursor
2000 func (v *View) RemoveMultiCursor(usePlugin bool) bool {
2001         end := len(v.Buf.cursors)
2002         if end > 1 {
2003                 if v.mainCursor() {
2004                         if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
2005                                 return false
2006                         }
2007
2008                         v.Buf.cursors[end-1] = nil
2009                         v.Buf.cursors = v.Buf.cursors[:end-1]
2010                         v.Buf.UpdateCursors()
2011                         v.Relocate()
2012
2013                         if usePlugin {
2014                                 return PostActionCall("RemoveMultiCursor", v)
2015                         }
2016                         return true
2017                 }
2018         } else {
2019                 v.RemoveAllMultiCursors(usePlugin)
2020         }
2021         return false
2022 }
2023
2024 // RemoveAllMultiCursors removes all cursors except the base cursor
2025 func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
2026         if v.mainCursor() {
2027                 if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
2028                         return false
2029                 }
2030
2031                 for i := 1; i < len(v.Buf.cursors); i++ {
2032                         v.Buf.cursors[i] = nil
2033                 }
2034                 v.Buf.cursors = v.Buf.cursors[:1]
2035                 v.Buf.UpdateCursors()
2036                 v.Cursor.ResetSelection()
2037                 v.Relocate()
2038
2039                 if usePlugin {
2040                         return PostActionCall("RemoveAllMultiCursors", v)
2041                 }
2042                 return true
2043         }
2044         return false
2045 }