Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move modes into separate package; start using commands
Insert mode is fully converted, Normal mode has only basic movement covered. The only exception right now is Command mode, which uses a lot of internal editor machinery.
- Loading branch information
Showing
10 changed files
with
167 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mode | ||
|
||
import ( | ||
cmd "github.com/kisielk/vigo/commands" | ||
"github.com/kisielk/vigo/editor" | ||
"github.com/nsf/termbox-go" | ||
) | ||
|
||
type insertMode struct { | ||
editor *editor.Editor | ||
count int | ||
} | ||
|
||
func NewInsertMode(editor *editor.Editor, count int) insertMode { | ||
m := insertMode{editor: editor} | ||
m.editor.SetStatus("Insert") | ||
m.count = count | ||
return m | ||
} | ||
|
||
func (m insertMode) OnKey(ev *termbox.Event) { | ||
g := m.editor | ||
|
||
switch ev.Key { | ||
case termbox.KeyEsc, termbox.KeyCtrlC: | ||
g.SetMode(NewNormalMode(g)) | ||
case termbox.KeyBackspace, termbox.KeyBackspace2: | ||
g.Commands <- cmd.DeleteRuneBackward{} | ||
case termbox.KeyDelete, termbox.KeyCtrlD: | ||
g.Commands <- cmd.DeleteRune{} | ||
case termbox.KeySpace: | ||
g.Commands <- cmd.InsertRune{' '} | ||
case termbox.KeyEnter: | ||
// we use '\r' for <enter>, because it doesn't cause autoindent | ||
g.Commands <- cmd.InsertRune{'\r'} | ||
case termbox.KeyCtrlJ: | ||
g.Commands <- cmd.InsertRune{'\n'} | ||
default: | ||
if ev.Ch != 0 { | ||
g.Commands <- cmd.InsertRune{ev.Ch} | ||
} | ||
} | ||
} | ||
|
||
func (m insertMode) Exit() { | ||
// repeat action specified number of times | ||
v := m.editor.ActiveView() | ||
b := v.Buffer() | ||
for i := 0; i < m.count-1; i++ { | ||
a := b.History.LastAction() | ||
a.Apply(b) | ||
} | ||
} |
Oops, something went wrong.