10 shellquote "github.com/kballard/go-shellquote"
11 "github.com/zyedidia/clipboard"
12 "github.com/zyedidia/micro/internal/buffer"
13 "github.com/zyedidia/micro/internal/config"
14 "github.com/zyedidia/micro/internal/screen"
15 "github.com/zyedidia/micro/internal/shell"
16 "github.com/zyedidia/micro/internal/util"
17 "github.com/zyedidia/tcell"
20 // ScrollUp is not an action
21 func (h *BufPane) ScrollUp(n int) {
31 // ScrollDown is not an action
32 func (h *BufPane) ScrollDown(n int) {
34 if v.StartLine <= h.Buf.LinesNum()-1-n {
40 // MousePress is the event that should happen when a normal click happens
41 // This is almost always bound to left click
42 func (h *BufPane) MousePress(e *tcell.EventMouse) bool {
44 mx, my := e.Position()
45 mouseLoc := h.LocFromVisual(buffer.Loc{mx, my})
46 h.Cursor.Loc = mouseLoc
48 if b.NumCursors() > 1 {
51 h.Cursor = h.Buf.GetActiveCursor()
52 h.Cursor.Loc = mouseLoc
54 if time.Since(h.lastClickTime)/time.Millisecond < config.DoubleClickThreshold && (mouseLoc.X == h.lastLoc.X && mouseLoc.Y == h.lastLoc.Y) {
57 h.lastClickTime = time.Now()
63 h.Cursor.CopySelection("primary")
66 h.lastClickTime = time.Now()
72 h.Cursor.CopySelection("primary")
77 h.lastClickTime = time.Now()
79 h.Cursor.OrigSelection[0] = h.Cursor.Loc
80 h.Cursor.CurSelection[0] = h.Cursor.Loc
81 h.Cursor.CurSelection[1] = h.Cursor.Loc
83 h.mouseReleased = false
84 } else if !h.mouseReleased {
86 h.Cursor.AddLineToSelection()
87 } else if h.doubleClick {
88 h.Cursor.AddWordToSelection()
90 h.Cursor.SetSelectionEnd(h.Cursor.Loc)
94 h.Cursor.StoreVisualX()
99 // ScrollUpAction scrolls the view up
100 func (h *BufPane) ScrollUpAction() bool {
101 h.ScrollUp(util.IntOpt(h.Buf.Settings["scrollspeed"]))
105 // ScrollDownAction scrolls the view up
106 func (h *BufPane) ScrollDownAction() bool {
107 h.ScrollDown(util.IntOpt(h.Buf.Settings["scrollspeed"]))
111 // Center centers the view on the cursor
112 func (h *BufPane) Center() bool {
114 v.StartLine = h.Cursor.Y - v.Height/2
115 if v.StartLine+v.Height > h.Buf.LinesNum() {
116 v.StartLine = h.Buf.LinesNum() - v.Height
126 // CursorUp moves the cursor up
127 func (h *BufPane) CursorUp() bool {
128 h.Cursor.Deselect(true)
134 // CursorDown moves the cursor down
135 func (h *BufPane) CursorDown() bool {
136 h.Cursor.Deselect(true)
142 // CursorLeft moves the cursor left
143 func (h *BufPane) CursorLeft() bool {
144 if h.Cursor.HasSelection() {
145 h.Cursor.Deselect(true)
147 tabstospaces := h.Buf.Settings["tabstospaces"].(bool)
148 tabmovement := h.Buf.Settings["tabmovement"].(bool)
149 if tabstospaces && tabmovement {
150 tabsize := int(h.Buf.Settings["tabsize"].(float64))
151 line := h.Buf.LineBytes(h.Cursor.Y)
152 if h.Cursor.X-tabsize >= 0 && util.IsSpaces(line[h.Cursor.X-tabsize:h.Cursor.X]) && util.IsBytesWhitespace(line[0:h.Cursor.X-tabsize]) {
153 for i := 0; i < tabsize; i++ {
167 // CursorRight moves the cursor right
168 func (h *BufPane) CursorRight() bool {
169 if h.Cursor.HasSelection() {
170 h.Cursor.Deselect(false)
171 h.Cursor.Loc = h.Cursor.Loc.Move(1, h.Buf)
173 tabstospaces := h.Buf.Settings["tabstospaces"].(bool)
174 tabmovement := h.Buf.Settings["tabmovement"].(bool)
175 if tabstospaces && tabmovement {
176 tabsize := int(h.Buf.Settings["tabsize"].(float64))
177 line := h.Buf.LineBytes(h.Cursor.Y)
178 if h.Cursor.X+tabsize < utf8.RuneCount(line) && util.IsSpaces(line[h.Cursor.X:h.Cursor.X+tabsize]) && util.IsBytesWhitespace(line[0:h.Cursor.X]) {
179 for i := 0; i < tabsize; i++ {
194 // WordRight moves the cursor one word to the right
195 func (h *BufPane) WordRight() bool {
196 h.Cursor.Deselect(false)
202 // WordLeft moves the cursor one word to the left
203 func (h *BufPane) WordLeft() bool {
204 h.Cursor.Deselect(true)
210 // SelectUp selects up one line
211 func (h *BufPane) SelectUp() bool {
212 if !h.Cursor.HasSelection() {
213 h.Cursor.OrigSelection[0] = h.Cursor.Loc
216 h.Cursor.SelectTo(h.Cursor.Loc)
221 // SelectDown selects down one line
222 func (h *BufPane) SelectDown() bool {
223 if !h.Cursor.HasSelection() {
224 h.Cursor.OrigSelection[0] = h.Cursor.Loc
227 h.Cursor.SelectTo(h.Cursor.Loc)
232 // SelectLeft selects the character to the left of the cursor
233 func (h *BufPane) SelectLeft() bool {
236 if loc.GreaterThan(count) {
239 if !h.Cursor.HasSelection() {
240 h.Cursor.OrigSelection[0] = loc
243 h.Cursor.SelectTo(h.Cursor.Loc)
248 // SelectRight selects the character to the right of the cursor
249 func (h *BufPane) SelectRight() bool {
252 if loc.GreaterThan(count) {
255 if !h.Cursor.HasSelection() {
256 h.Cursor.OrigSelection[0] = loc
259 h.Cursor.SelectTo(h.Cursor.Loc)
264 // SelectWordRight selects the word to the right of the cursor
265 func (h *BufPane) SelectWordRight() bool {
266 if !h.Cursor.HasSelection() {
267 h.Cursor.OrigSelection[0] = h.Cursor.Loc
270 h.Cursor.SelectTo(h.Cursor.Loc)
275 // SelectWordLeft selects the word to the left of the cursor
276 func (h *BufPane) SelectWordLeft() bool {
277 if !h.Cursor.HasSelection() {
278 h.Cursor.OrigSelection[0] = h.Cursor.Loc
281 h.Cursor.SelectTo(h.Cursor.Loc)
286 // StartOfLine moves the cursor to the start of the text of the line
287 func (h *BufPane) StartOfText() bool {
288 h.Cursor.Deselect(true)
289 h.Cursor.StartOfText()
294 // StartOfLine moves the cursor to the start of the line
295 func (h *BufPane) StartOfLine() bool {
296 h.Cursor.Deselect(true)
302 // EndOfLine moves the cursor to the end of the line
303 func (h *BufPane) EndOfLine() bool {
304 h.Cursor.Deselect(true)
310 // SelectLine selects the entire current line
311 func (h *BufPane) SelectLine() bool {
312 h.Cursor.SelectLine()
317 // SelectToStartOfText selects to the start of the text on the current line
318 func (h *BufPane) SelectToStartOfText() bool {
319 if !h.Cursor.HasSelection() {
320 h.Cursor.OrigSelection[0] = h.Cursor.Loc
322 h.Cursor.StartOfText()
323 h.Cursor.SelectTo(h.Cursor.Loc)
328 // SelectToStartOfLine selects to the start of the current line
329 func (h *BufPane) SelectToStartOfLine() bool {
330 if !h.Cursor.HasSelection() {
331 h.Cursor.OrigSelection[0] = h.Cursor.Loc
334 h.Cursor.SelectTo(h.Cursor.Loc)
339 // SelectToEndOfLine selects to the end of the current line
340 func (h *BufPane) SelectToEndOfLine() bool {
341 if !h.Cursor.HasSelection() {
342 h.Cursor.OrigSelection[0] = h.Cursor.Loc
345 h.Cursor.SelectTo(h.Cursor.Loc)
350 // ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none
351 func (h *BufPane) ParagraphPrevious() bool {
353 for line = h.Cursor.Y; line > 0; line-- {
354 if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
360 // If no empty line found. move cursor to end of buffer
362 h.Cursor.Loc = h.Buf.Start()
368 // ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none
369 func (h *BufPane) ParagraphNext() bool {
371 for line = h.Cursor.Y; line < h.Buf.LinesNum(); line++ {
372 if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
378 // If no empty line found. move cursor to end of buffer
379 if line == h.Buf.LinesNum() {
380 h.Cursor.Loc = h.Buf.End()
386 // Retab changes all tabs to spaces or all spaces to tabs depending
387 // on the user's settings
388 func (h *BufPane) Retab() bool {
394 // CursorStart moves the cursor to the start of the buffer
395 func (h *BufPane) CursorStart() bool {
396 h.Cursor.Deselect(true)
403 // CursorEnd moves the cursor to the end of the buffer
404 func (h *BufPane) CursorEnd() bool {
405 h.Cursor.Deselect(true)
406 h.Cursor.Loc = h.Buf.End()
407 h.Cursor.StoreVisualX()
412 // SelectToStart selects the text from the cursor to the start of the buffer
413 func (h *BufPane) SelectToStart() bool {
414 if !h.Cursor.HasSelection() {
415 h.Cursor.OrigSelection[0] = h.Cursor.Loc
418 h.Cursor.SelectTo(h.Buf.Start())
423 // SelectToEnd selects the text from the cursor to the end of the buffer
424 func (h *BufPane) SelectToEnd() bool {
425 if !h.Cursor.HasSelection() {
426 h.Cursor.OrigSelection[0] = h.Cursor.Loc
429 h.Cursor.SelectTo(h.Buf.End())
434 // InsertNewline inserts a newline plus possible some whitespace if autoindent is on
435 func (h *BufPane) InsertNewline() bool {
437 if h.Cursor.HasSelection() {
438 h.Cursor.DeleteSelection()
439 h.Cursor.ResetSelection()
442 ws := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))
444 h.Buf.Insert(h.Cursor.Loc, "\n")
447 if h.Buf.Settings["autoindent"].(bool) {
451 h.Buf.Insert(h.Cursor.Loc, string(ws))
452 // for i := 0; i < len(ws); i++ {
456 // Remove the whitespaces if keepautoindent setting is off
457 if util.IsSpacesOrTabs(h.Buf.LineBytes(h.Cursor.Y-1)) && !h.Buf.Settings["keepautoindent"].(bool) {
458 line := h.Buf.LineBytes(h.Cursor.Y - 1)
459 h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y - 1}, buffer.Loc{X: utf8.RuneCount(line), Y: h.Cursor.Y - 1})
462 h.Cursor.LastVisualX = h.Cursor.GetVisualX()
467 // Backspace deletes the previous character
468 func (h *BufPane) Backspace() bool {
469 if h.Cursor.HasSelection() {
470 h.Cursor.DeleteSelection()
471 h.Cursor.ResetSelection()
472 } else if h.Cursor.Loc.GreaterThan(h.Buf.Start()) {
473 // We have to do something a bit hacky here because we want to
474 // delete the line by first moving left and then deleting backwards
475 // but the undo redo would place the cursor in the wrong place
476 // So instead we move left, save the position, move back, delete
477 // and restore the position
479 // If the user is using spaces instead of tabs and they are deleting
480 // whitespace at the start of the line, we should delete as if it's a
481 // tab (tabSize number of spaces)
482 lineStart := util.SliceStart(h.Buf.LineBytes(h.Cursor.Y), h.Cursor.X)
483 tabSize := int(h.Buf.Settings["tabsize"].(float64))
484 if h.Buf.Settings["tabstospaces"].(bool) && util.IsSpaces(lineStart) && len(lineStart) != 0 && utf8.RuneCount(lineStart)%tabSize == 0 {
486 h.Buf.Remove(loc.Move(-tabSize, h.Buf), loc)
489 h.Buf.Remove(loc.Move(-1, h.Buf), loc)
492 h.Cursor.LastVisualX = h.Cursor.GetVisualX()
497 // DeleteWordRight deletes the word to the right of the cursor
498 func (h *BufPane) DeleteWordRight() bool {
500 if h.Cursor.HasSelection() {
501 h.Cursor.DeleteSelection()
502 h.Cursor.ResetSelection()
508 // DeleteWordLeft deletes the word to the left of the cursor
509 func (h *BufPane) DeleteWordLeft() bool {
511 if h.Cursor.HasSelection() {
512 h.Cursor.DeleteSelection()
513 h.Cursor.ResetSelection()
519 // Delete deletes the next character
520 func (h *BufPane) Delete() bool {
521 if h.Cursor.HasSelection() {
522 h.Cursor.DeleteSelection()
523 h.Cursor.ResetSelection()
526 if loc.LessThan(h.Buf.End()) {
527 h.Buf.Remove(loc, loc.Move(1, h.Buf))
534 // IndentSelection indents the current selection
535 func (h *BufPane) IndentSelection() bool {
536 if h.Cursor.HasSelection() {
537 start := h.Cursor.CurSelection[0]
538 end := h.Cursor.CurSelection[1]
540 start, end = end, start
541 h.Cursor.SetSelectionStart(start)
542 h.Cursor.SetSelectionEnd(end)
546 endY := end.Move(-1, h.Buf).Y
547 endX := end.Move(-1, h.Buf).X
548 tabsize := int(h.Buf.Settings["tabsize"].(float64))
549 indentsize := len(h.Buf.IndentString(tabsize))
550 for y := startY; y <= endY; y++ {
551 if len(h.Buf.LineBytes(y)) > 0 {
552 h.Buf.Insert(buffer.Loc{X: 0, Y: y}, h.Buf.IndentString(tabsize))
553 if y == startY && start.X > 0 {
554 h.Cursor.SetSelectionStart(start.Move(indentsize, h.Buf))
557 h.Cursor.SetSelectionEnd(buffer.Loc{X: endX + indentsize + 1, Y: endY})
561 h.Buf.RelocateCursors()
569 // OutdentLine moves the current line back one indentation
570 func (h *BufPane) OutdentLine() bool {
571 if h.Cursor.HasSelection() {
575 for x := 0; x < len(h.Buf.IndentString(util.IntOpt(h.Buf.Settings["tabsize"]))); x++ {
576 if len(util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))) == 0 {
579 h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y}, buffer.Loc{X: 1, Y: h.Cursor.Y})
581 h.Buf.RelocateCursors()
586 // OutdentSelection takes the current selection and moves it back one indent level
587 func (h *BufPane) OutdentSelection() bool {
588 if h.Cursor.HasSelection() {
589 start := h.Cursor.CurSelection[0]
590 end := h.Cursor.CurSelection[1]
592 start, end = end, start
593 h.Cursor.SetSelectionStart(start)
594 h.Cursor.SetSelectionEnd(end)
598 endY := end.Move(-1, h.Buf).Y
599 for y := startY; y <= endY; y++ {
600 for x := 0; x < len(h.Buf.IndentString(util.IntOpt(h.Buf.Settings["tabsize"]))); x++ {
601 if len(util.GetLeadingWhitespace(h.Buf.LineBytes(y))) == 0 {
604 h.Buf.Remove(buffer.Loc{X: 0, Y: y}, buffer.Loc{X: 1, Y: y})
607 h.Buf.RelocateCursors()
615 // Autocomplete cycles the suggestions and performs autocompletion if there are suggestions
616 func (h *BufPane) Autocomplete() bool {
619 if h.Cursor.HasSelection() {
623 if !util.IsNonAlphaNumeric(h.Cursor.RuneUnder(h.Cursor.X)) {
624 // don't autocomplete if cursor is on alpha numeric character (middle of a word)
628 if b.HasSuggestions {
629 b.CycleAutocomplete(true)
632 return b.Autocomplete(buffer.BufferComplete)
635 // CycleAutocompleteBack cycles back in the autocomplete suggestion list
636 func (h *BufPane) CycleAutocompleteBack() bool {
637 if h.Cursor.HasSelection() {
641 if h.Buf.HasSuggestions {
642 h.Buf.CycleAutocomplete(false)
648 // InsertTab inserts a tab or spaces
649 func (h *BufPane) InsertTab() bool {
651 indent := b.IndentString(util.IntOpt(b.Settings["tabsize"]))
652 tabBytes := len(indent)
653 bytesUntilIndent := tabBytes - (h.Cursor.GetVisualX() % tabBytes)
654 b.Insert(h.Cursor.Loc, indent[:bytesUntilIndent])
659 // SaveAll saves all open buffers
660 func (h *BufPane) SaveAll() bool {
661 for _, b := range buffer.OpenBuffers {
667 // SaveCB performs a save and does a callback at the very end (after all prompts have been resolved)
668 func (h *BufPane) SaveCB(action string, callback func()) bool {
669 // If this is an empty buffer, ask for a filename
670 if h.Buf.Path == "" {
671 h.SaveAsCB(action, callback)
673 noPrompt := h.saveBufToFile(h.Buf.Path, action, callback)
681 // Save the buffer to disk
682 func (h *BufPane) Save() bool {
683 return h.SaveCB("Save", nil)
686 // SaveAsCB performs a save as and does a callback at the very end (after all prompts have been resolved)
687 func (h *BufPane) SaveAsCB(action string, callback func()) bool {
688 InfoBar.Prompt("Filename: ", "", "Save", nil, func(resp string, canceled bool) {
690 // the filename might or might not be quoted, so unquote first then join the strings.
691 args, err := shellquote.Split(resp)
693 InfoBar.Error("Error parsing arguments: ", err)
697 InfoBar.Error("No filename given")
700 filename := strings.Join(args, " ")
701 noPrompt := h.saveBufToFile(filename, action, callback)
703 h.completeAction(action)
710 // SaveAs saves the buffer to disk with the given name
711 func (h *BufPane) SaveAs() bool {
712 return h.SaveAsCB("SaveAs", nil)
715 // This function saves the buffer to `filename` and changes the buffer's path and name
716 // to `filename` if the save is successful
717 func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
718 err := h.Buf.SaveAs(filename)
720 if strings.HasSuffix(err.Error(), "permission denied") {
721 InfoBar.YNPrompt("Permission denied. Do you want to save this file using sudo? (y,n)", func(yes, canceled bool) {
722 if yes && !canceled {
723 err = h.Buf.SaveAsWithSudo(filename)
727 h.Buf.Path = filename
728 h.Buf.SetName(filename)
729 InfoBar.Message("Saved " + filename)
731 h.completeAction(action)
742 h.Buf.Path = filename
743 h.Buf.SetName(filename)
744 InfoBar.Message("Saved " + filename)
752 // Find opens a prompt and searches forward for the input
753 func (h *BufPane) Find() bool {
754 h.searchOrig = h.Cursor.Loc
755 InfoBar.Prompt("Find: ", "", "Find", func(resp string) {
757 match, found, _ := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, true)
759 h.Cursor.SetSelectionStart(match[0])
760 h.Cursor.SetSelectionEnd(match[1])
761 h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
762 h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
763 h.Cursor.GotoLoc(match[1])
765 h.Cursor.GotoLoc(h.searchOrig)
766 h.Cursor.ResetSelection()
769 }, func(resp string, canceled bool) {
772 match, found, err := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, true)
777 h.Cursor.SetSelectionStart(match[0])
778 h.Cursor.SetSelectionEnd(match[1])
779 h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
780 h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
781 h.Cursor.GotoLoc(h.Cursor.CurSelection[1])
784 h.Cursor.ResetSelection()
785 InfoBar.Message("No matches found")
788 h.Cursor.ResetSelection()
796 // FindNext searches forwards for the last used search term
797 func (h *BufPane) FindNext() bool {
798 // If the cursor is at the start of a selection and we search we want
799 // to search from the end of the selection in the case that
800 // the selection is a search result in which case we wouldn't move at
801 // at all which would be bad
802 searchLoc := h.Cursor.Loc
803 if h.Cursor.HasSelection() {
804 searchLoc = h.Cursor.CurSelection[1]
806 match, found, err := h.Buf.FindNext(h.lastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, true, true)
811 h.Cursor.SetSelectionStart(match[0])
812 h.Cursor.SetSelectionEnd(match[1])
813 h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
814 h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
815 h.Cursor.Loc = h.Cursor.CurSelection[1]
817 h.Cursor.ResetSelection()
823 // FindPrevious searches backwards for the last used search term
824 func (h *BufPane) FindPrevious() bool {
825 // If the cursor is at the end of a selection and we search we want
826 // to search from the beginning of the selection in the case that
827 // the selection is a search result in which case we wouldn't move at
828 // at all which would be bad
829 searchLoc := h.Cursor.Loc
830 if h.Cursor.HasSelection() {
831 searchLoc = h.Cursor.CurSelection[0]
833 match, found, err := h.Buf.FindNext(h.lastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, false, true)
838 h.Cursor.SetSelectionStart(match[0])
839 h.Cursor.SetSelectionEnd(match[1])
840 h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
841 h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
842 h.Cursor.Loc = h.Cursor.CurSelection[1]
844 h.Cursor.ResetSelection()
850 // Undo undoes the last action
851 func (h *BufPane) Undo() bool {
853 InfoBar.Message("Undid action")
858 // Redo redoes the last action
859 func (h *BufPane) Redo() bool {
861 InfoBar.Message("Redid action")
866 // Copy the selection to the system clipboard
867 func (h *BufPane) Copy() bool {
868 if h.Cursor.HasSelection() {
869 h.Cursor.CopySelection("clipboard")
871 if clipboard.Unsupported {
872 InfoBar.Message("Copied selection (install xclip for external clipboard)")
874 InfoBar.Message("Copied selection")
881 // CutLine cuts the current line to the clipboard
882 func (h *BufPane) CutLine() bool {
883 h.Cursor.SelectLine()
884 if !h.Cursor.HasSelection() {
887 if h.freshClip == true {
888 if h.Cursor.HasSelection() {
889 if clip, err := clipboard.ReadAll("clipboard"); err != nil {
890 // messenger.Error(err)
892 clipboard.WriteAll(clip+string(h.Cursor.GetSelection()), "clipboard")
895 } else if time.Since(h.lastCutTime)/time.Second > 10*time.Second || h.freshClip == false {
899 h.lastCutTime = time.Now()
900 h.Cursor.DeleteSelection()
901 h.Cursor.ResetSelection()
902 InfoBar.Message("Cut line")
907 // Cut the selection to the system clipboard
908 func (h *BufPane) Cut() bool {
909 if h.Cursor.HasSelection() {
910 h.Cursor.CopySelection("clipboard")
911 h.Cursor.DeleteSelection()
912 h.Cursor.ResetSelection()
914 InfoBar.Message("Cut selection")
923 // DuplicateLine duplicates the current line or selection
924 func (h *BufPane) DuplicateLine() bool {
925 if h.Cursor.HasSelection() {
926 h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection()))
929 h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
933 InfoBar.Message("Duplicated line")
938 // DeleteLine deletes the current line
939 func (h *BufPane) DeleteLine() bool {
940 h.Cursor.SelectLine()
941 if !h.Cursor.HasSelection() {
944 h.Cursor.DeleteSelection()
945 h.Cursor.ResetSelection()
946 InfoBar.Message("Deleted line")
951 // MoveLinesUp moves up the current line or selected lines if any
952 func (h *BufPane) MoveLinesUp() bool {
953 if h.Cursor.HasSelection() {
954 if h.Cursor.CurSelection[0].Y == 0 {
955 InfoBar.Message("Cannot move further up")
958 start := h.Cursor.CurSelection[0].Y
959 end := h.Cursor.CurSelection[1].Y
961 end, start = start, end
968 h.Cursor.CurSelection[1].Y -= 1
970 if h.Cursor.Loc.Y == 0 {
971 InfoBar.Message("Cannot move further up")
984 // MoveLinesDown moves down the current line or selected lines if any
985 func (h *BufPane) MoveLinesDown() bool {
986 if h.Cursor.HasSelection() {
987 if h.Cursor.CurSelection[1].Y >= h.Buf.LinesNum() {
988 InfoBar.Message("Cannot move further down")
991 start := h.Cursor.CurSelection[0].Y
992 end := h.Cursor.CurSelection[1].Y
994 end, start = start, end
1002 if h.Cursor.Loc.Y >= h.Buf.LinesNum()-1 {
1003 InfoBar.Message("Cannot move further down")
1006 h.Buf.MoveLinesDown(
1016 // Paste whatever is in the system clipboard into the buffer
1017 // Delete and paste if the user has a selection
1018 func (h *BufPane) Paste() bool {
1019 clip, _ := clipboard.ReadAll("clipboard")
1025 // PastePrimary pastes from the primary clipboard (only use on linux)
1026 func (h *BufPane) PastePrimary() bool {
1027 clip, _ := clipboard.ReadAll("primary")
1033 func (h *BufPane) paste(clip string) {
1034 if h.Buf.Settings["smartpaste"].(bool) {
1035 if h.Cursor.X > 0 && len(util.GetLeadingWhitespace([]byte(strings.TrimLeft(clip, "\r\n")))) == 0 {
1036 leadingWS := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))
1037 clip = strings.Replace(clip, "\n", "\n"+string(leadingWS), -1)
1041 if h.Cursor.HasSelection() {
1042 h.Cursor.DeleteSelection()
1043 h.Cursor.ResetSelection()
1046 h.Buf.Insert(h.Cursor.Loc, clip)
1047 // h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf)
1049 if clipboard.Unsupported {
1050 InfoBar.Message("Pasted clipboard (install xclip for external clipboard)")
1052 InfoBar.Message("Pasted clipboard")
1056 // JumpToMatchingBrace moves the cursor to the matching brace if it is
1057 // currently on a brace
1058 func (h *BufPane) JumpToMatchingBrace() bool {
1059 for _, bp := range buffer.BracePairs {
1060 r := h.Cursor.RuneUnder(h.Cursor.X)
1061 rl := h.Cursor.RuneUnder(h.Cursor.X - 1)
1062 if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
1063 matchingBrace, left := h.Buf.FindMatchingBrace(bp, h.Cursor.Loc)
1065 h.Cursor.GotoLoc(matchingBrace)
1067 h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
1076 // SelectAll selects the entire buffer
1077 func (h *BufPane) SelectAll() bool {
1078 h.Cursor.SetSelectionStart(h.Buf.Start())
1079 h.Cursor.SetSelectionEnd(h.Buf.End())
1080 // Put the cursor at the beginning
1087 // OpenFile opens a new file in the buffer
1088 func (h *BufPane) OpenFile() bool {
1089 InfoBar.Prompt("> ", "open ", "Open", nil, func(resp string, canceled bool) {
1091 h.HandleCommand(resp)
1097 // Start moves the viewport to the start of the buffer
1098 func (h *BufPane) Start() bool {
1105 // End moves the viewport to the end of the buffer
1106 func (h *BufPane) End() bool {
1107 // TODO: softwrap problems?
1109 if v.Height > h.Buf.LinesNum() {
1113 v.StartLine = h.Buf.LinesNum() - v.Height
1119 // PageUp scrolls the view up a page
1120 func (h *BufPane) PageUp() bool {
1122 if v.StartLine > v.Height {
1123 h.ScrollUp(v.Height)
1131 // PageDown scrolls the view down a page
1132 func (h *BufPane) PageDown() bool {
1134 if h.Buf.LinesNum()-(v.StartLine+v.Height) > v.Height {
1135 h.ScrollDown(v.Height)
1136 } else if h.Buf.LinesNum() >= v.Height {
1137 v.StartLine = h.Buf.LinesNum() - v.Height
1142 // SelectPageUp selects up one page
1143 func (h *BufPane) SelectPageUp() bool {
1144 if !h.Cursor.HasSelection() {
1145 h.Cursor.OrigSelection[0] = h.Cursor.Loc
1147 h.Cursor.UpN(h.GetView().Height)
1148 h.Cursor.SelectTo(h.Cursor.Loc)
1153 // SelectPageDown selects down one page
1154 func (h *BufPane) SelectPageDown() bool {
1155 if !h.Cursor.HasSelection() {
1156 h.Cursor.OrigSelection[0] = h.Cursor.Loc
1158 h.Cursor.DownN(h.GetView().Height)
1159 h.Cursor.SelectTo(h.Cursor.Loc)
1164 // CursorPageUp places the cursor a page up
1165 func (h *BufPane) CursorPageUp() bool {
1166 h.Cursor.Deselect(true)
1168 if h.Cursor.HasSelection() {
1169 h.Cursor.Loc = h.Cursor.CurSelection[0]
1170 h.Cursor.ResetSelection()
1171 h.Cursor.StoreVisualX()
1173 h.Cursor.UpN(h.GetView().Height)
1178 // CursorPageDown places the cursor a page up
1179 func (h *BufPane) CursorPageDown() bool {
1180 h.Cursor.Deselect(false)
1182 if h.Cursor.HasSelection() {
1183 h.Cursor.Loc = h.Cursor.CurSelection[1]
1184 h.Cursor.ResetSelection()
1185 h.Cursor.StoreVisualX()
1187 h.Cursor.DownN(h.GetView().Height)
1192 // HalfPageUp scrolls the view up half a page
1193 func (h *BufPane) HalfPageUp() bool {
1195 if v.StartLine > v.Height/2 {
1196 h.ScrollUp(v.Height / 2)
1204 // HalfPageDown scrolls the view down half a page
1205 func (h *BufPane) HalfPageDown() bool {
1207 if h.Buf.LinesNum()-(v.StartLine+v.Height) > v.Height/2 {
1208 h.ScrollDown(v.Height / 2)
1210 if h.Buf.LinesNum() >= v.Height {
1211 v.StartLine = h.Buf.LinesNum() - v.Height
1218 // ToggleDiffGutter turns the diff gutter off and on
1219 func (h *BufPane) ToggleDiffGutter() bool {
1220 if !h.Buf.Settings["diffgutter"].(bool) {
1221 h.Buf.Settings["diffgutter"] = true
1222 h.Buf.UpdateDiff(func(synchronous bool) {
1223 screen.DrawChan <- true
1225 InfoBar.Message("Enabled diff gutter")
1227 h.Buf.Settings["diffgutter"] = false
1228 InfoBar.Message("Disabled diff gutter")
1233 // ToggleRuler turns line numbers off and on
1234 func (h *BufPane) ToggleRuler() bool {
1235 if !h.Buf.Settings["ruler"].(bool) {
1236 h.Buf.Settings["ruler"] = true
1237 InfoBar.Message("Enabled ruler")
1239 h.Buf.Settings["ruler"] = false
1240 InfoBar.Message("Disabled ruler")
1245 // ClearStatus clears the messenger bar
1246 func (h *BufPane) ClearStatus() bool {
1251 // ToggleHelp toggles the help screen
1252 func (h *BufPane) ToggleHelp() bool {
1253 if h.Buf.Type == buffer.BTHelp {
1261 // ToggleKeyMenu toggles the keymenu option and resizes all tabs
1262 func (h *BufPane) ToggleKeyMenu() bool {
1263 config.GlobalSettings["keymenu"] = !config.GetGlobalOption("keymenu").(bool)
1268 // ShellMode opens a terminal to run a shell command
1269 func (h *BufPane) ShellMode() bool {
1270 InfoBar.Prompt("$ ", "", "Shell", nil, func(resp string, canceled bool) {
1272 // The true here is for openTerm to make the command interactive
1273 shell.RunInteractiveShell(resp, true, false)
1280 // CommandMode lets the user enter a command
1281 func (h *BufPane) CommandMode() bool {
1282 InfoBar.Prompt("> ", "", "Command", nil, func(resp string, canceled bool) {
1284 h.HandleCommand(resp)
1290 // ToggleOverwriteMode lets the user toggle the text overwrite mode
1291 func (h *BufPane) ToggleOverwriteMode() bool {
1292 h.isOverwriteMode = !h.isOverwriteMode
1296 // Escape leaves current mode
1297 func (h *BufPane) Escape() bool {
1301 // Quit this will close the current tab or view that is open
1302 func (h *BufPane) Quit() bool {
1305 if len(MainTab().Panes) > 1 {
1307 } else if len(Tabs.List) > 1 {
1308 Tabs.RemoveTab(h.splitID)
1310 screen.Screen.Fini()
1315 if h.Buf.Modified() {
1316 if config.GlobalSettings["autosave"].(float64) > 0 {
1317 // autosave on means we automatically save when quitting
1318 h.SaveCB("Quit", func() {
1322 InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1323 if !canceled && !yes {
1325 } else if !canceled && yes {
1326 h.SaveCB("Quit", func() {
1338 // QuitAll quits the whole editor; all splits and tabs
1339 func (h *BufPane) QuitAll() bool {
1340 anyModified := false
1341 for _, b := range buffer.OpenBuffers {
1349 for _, b := range buffer.OpenBuffers {
1352 screen.Screen.Fini()
1358 InfoBar.YNPrompt("Quit micro? (all open buffers will be closed without saving)", func(yes, canceled bool) {
1359 if !canceled && yes {
1370 // AddTab adds a new tab with an empty buffer
1371 func (h *BufPane) AddTab() bool {
1372 width, height := screen.Screen.Size()
1373 iOffset := config.GetInfoBarOffset()
1374 b := buffer.NewBufferFromString("", "", buffer.BTDefault)
1375 tp := NewTabFromBuffer(0, 0, width, height-iOffset, b)
1377 Tabs.SetActive(len(Tabs.List) - 1)
1382 // PreviousTab switches to the previous tab in the tab list
1383 func (h *BufPane) PreviousTab() bool {
1385 Tabs.SetActive(util.Clamp(a-1, 0, len(Tabs.List)-1))
1390 // NextTab switches to the next tab in the tab list
1391 func (h *BufPane) NextTab() bool {
1393 Tabs.SetActive(util.Clamp(a+1, 0, len(Tabs.List)-1))
1397 // VSplitAction opens an empty vertical split
1398 func (h *BufPane) VSplitAction() bool {
1399 h.VSplitBuf(buffer.NewBufferFromString("", "", buffer.BTDefault))
1404 // HSplitAction opens an empty horizontal split
1405 func (h *BufPane) HSplitAction() bool {
1406 h.HSplitBuf(buffer.NewBufferFromString("", "", buffer.BTDefault))
1411 // Unsplit closes all splits in the current tab except the active one
1412 func (h *BufPane) Unsplit() bool {
1414 n := tab.GetNode(h.splitID)
1417 tab.RemovePane(tab.GetPane(h.splitID))
1419 tab.SetActive(len(tab.Panes) - 1)
1426 // NextSplit changes the view to the next split
1427 func (h *BufPane) NextSplit() bool {
1429 if a < len(h.tab.Panes)-1 {
1440 // PreviousSplit changes the view to the previous split
1441 func (h *BufPane) PreviousSplit() bool {
1446 a = len(h.tab.Panes) - 1
1453 var curmacro []interface{}
1454 var recording_macro bool
1456 // ToggleMacro toggles recording of a macro
1457 func (h *BufPane) ToggleMacro() bool {
1458 recording_macro = !recording_macro
1459 if recording_macro {
1460 curmacro = []interface{}{}
1461 InfoBar.Message("Recording")
1463 InfoBar.Message("Stopped recording")
1469 // PlayMacro plays back the most recently recorded macro
1470 func (h *BufPane) PlayMacro() bool {
1471 if recording_macro {
1474 for _, action := range curmacro {
1475 switch t := action.(type) {
1478 case func(*BufPane) bool:
1486 // SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
1487 func (h *BufPane) SpawnMultiCursor() bool {
1488 spawner := h.Buf.GetCursor(h.Buf.NumCursors() - 1)
1489 if !spawner.HasSelection() {
1490 spawner.SelectWord()
1496 sel := spawner.GetSelection()
1497 searchStart := spawner.CurSelection[1]
1499 search := string(sel)
1500 search = regexp.QuoteMeta(search)
1502 search = "\\b" + search + "\\b"
1504 match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true)
1509 c := buffer.NewCursor(h.Buf, buffer.Loc{})
1510 c.SetSelectionStart(match[0])
1511 c.SetSelectionEnd(match[1])
1512 c.OrigSelection[0] = c.CurSelection[0]
1513 c.OrigSelection[1] = c.CurSelection[1]
1514 c.Loc = c.CurSelection[1]
1517 h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
1518 h.Buf.MergeCursors()
1520 InfoBar.Message("No matches found")
1527 // SpawnMultiCursorUp creates additional cursor, at the same X (if possible), one Y less.
1528 func (h *BufPane) SpawnMultiCursorUp() bool {
1529 if h.Cursor.Y == 0 {
1532 h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y - 1})
1536 c := buffer.NewCursor(h.Buf, buffer.Loc{h.Cursor.X, h.Cursor.Y + 1})
1538 h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
1539 h.Buf.MergeCursors()
1545 // SpawnMultiCursorUp creates additional cursor, at the same X (if possible), one Y more.
1546 func (h *BufPane) SpawnMultiCursorDown() bool {
1547 if h.Cursor.Y+1 == h.Buf.LinesNum() {
1550 h.Cursor.GotoLoc(buffer.Loc{h.Cursor.X, h.Cursor.Y + 1})
1554 c := buffer.NewCursor(h.Buf, buffer.Loc{h.Cursor.X, h.Cursor.Y - 1})
1556 h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
1557 h.Buf.MergeCursors()
1562 // SpawnMultiCursorSelect adds a cursor at the beginning of each line of a selection
1563 func (h *BufPane) SpawnMultiCursorSelect() bool {
1564 // Avoid cases where multiple cursors already exist, that would create problems
1565 if h.Buf.NumCursors() > 1 {
1572 a, b := h.Cursor.CurSelection[0].Y, h.Cursor.CurSelection[1].Y
1574 startLine, endLine = b, a
1576 startLine, endLine = a, b
1579 if h.Cursor.HasSelection() {
1580 h.Cursor.ResetSelection()
1581 h.Cursor.GotoLoc(buffer.Loc{0, startLine})
1583 for i := startLine; i <= endLine; i++ {
1584 c := buffer.NewCursor(h.Buf, buffer.Loc{0, i})
1588 h.Buf.MergeCursors()
1592 InfoBar.Message("Added cursors from selection")
1596 // MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
1597 func (h *BufPane) MouseMultiCursor(e *tcell.EventMouse) bool {
1599 mx, my := e.Position()
1600 mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my})
1601 c := buffer.NewCursor(b, mouseLoc)
1608 // SkipMultiCursor moves the current multiple cursor to the next available position
1609 func (h *BufPane) SkipMultiCursor() bool {
1610 lastC := h.Buf.GetCursor(h.Buf.NumCursors() - 1)
1611 sel := lastC.GetSelection()
1612 searchStart := lastC.CurSelection[1]
1614 search := string(sel)
1615 search = regexp.QuoteMeta(search)
1617 search = "\\b" + search + "\\b"
1620 match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true)
1625 lastC.SetSelectionStart(match[0])
1626 lastC.SetSelectionEnd(match[1])
1627 lastC.OrigSelection[0] = lastC.CurSelection[0]
1628 lastC.OrigSelection[1] = lastC.CurSelection[1]
1629 lastC.Loc = lastC.CurSelection[1]
1631 h.Buf.MergeCursors()
1632 h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
1634 InfoBar.Message("No matches found")
1640 // RemoveMultiCursor removes the latest multiple cursor
1641 func (h *BufPane) RemoveMultiCursor() bool {
1642 if h.Buf.NumCursors() > 1 {
1643 h.Buf.RemoveCursor(h.Buf.NumCursors() - 1)
1644 h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
1645 h.Buf.UpdateCursors()
1653 // RemoveAllMultiCursors removes all cursors except the base cursor
1654 func (h *BufPane) RemoveAllMultiCursors() bool {
1655 h.Buf.ClearCursors()
1661 // None is an action that does nothing
1662 func (h *BufPane) None() bool {