Skip to content

Commit

Permalink
Move modes into separate package; start using commands
Browse files Browse the repository at this point in the history
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
dtcaciuc committed May 27, 2014
1 parent a9ad8f3 commit 352917f
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 149 deletions.
2 changes: 2 additions & 0 deletions commands/modes.go
@@ -1,5 +1,6 @@
package commands

/*
import (
"github.com/kisielk/vigo/editor"
)
Expand All @@ -17,3 +18,4 @@ type EnterInsertMode struct {
func (m EnterInsertMode) Apply(e *editor.Editor) {
e.SetMode(editor.NewInsertMode(e, m.Count))
}
*/
8 changes: 4 additions & 4 deletions editor/command.go
Expand Up @@ -12,11 +12,11 @@ import (
type commandMode struct {
Overlay
editor *Editor
mode editorMode
mode EditorMode
buffer *bytes.Buffer
}

func newCommandMode(editor *Editor, mode editorMode) commandMode {
func NewCommandMode(editor *Editor, mode EditorMode) commandMode {
m := commandMode{editor: editor, mode: mode, buffer: &bytes.Buffer{}}
return m
}
Expand All @@ -31,7 +31,7 @@ func (m commandMode) cursorPosition() (int, int) {
return m.buffer.Len() + 1, r.Height - 1
}

func (m commandMode) onKey(ev *termbox.Event) {
func (m commandMode) OnKey(ev *termbox.Event) {
switch ev.Key {
case termbox.KeyEsc, termbox.KeyCtrlC:
m.editor.SetMode(m.mode)
Expand All @@ -55,7 +55,7 @@ func (m commandMode) onKey(ev *termbox.Event) {
}
}

func (m commandMode) exit() {
func (m commandMode) Exit() {
}

func (m commandMode) draw() {
Expand Down
15 changes: 9 additions & 6 deletions editor/editor.go
Expand Up @@ -61,7 +61,7 @@ type Editor struct {

cutBuffers *cutBuffers

mode editorMode
mode EditorMode
overlay Overlay
}

Expand Down Expand Up @@ -92,9 +92,8 @@ func NewEditor(filenames []string) *Editor {
e.redraw = make(chan struct{})
e.views = newViewTreeLeaf(nil, newView(e.viewContext(), e.buffers[0], e.redraw))
e.active = e.views
e.SetMode(NewNormalMode(e))
e.UIEvents = make(chan termbox.Event, 20)
e.Commands = make(chan Command)
e.Commands = make(chan Command, 20)
return e
}

Expand Down Expand Up @@ -343,6 +342,10 @@ func (e *Editor) fixEdges(v *viewTree) {
}
}

func (e *Editor) Height() int {
return e.uiBuf.Rect.Height
}

// cursorPosition returns the absolute screen coordinates of the cursor
func (e *Editor) CursorPosition() (int, int) {
x, y := e.active.leaf.cursorPosition()
Expand Down Expand Up @@ -392,7 +395,7 @@ func (e *Editor) handleUIEvent(ev *termbox.Event) error {
case termbox.EventKey:
e.SetStatus("") // reset status on every key event
e.onSysKey(ev)
e.mode.onKey(ev)
e.mode.OnKey(ev)

if e.quitFlag {
return ErrQuit
Expand All @@ -410,9 +413,9 @@ func (e *Editor) handleUIEvent(ev *termbox.Event) error {
return nil
}

func (e *Editor) SetMode(m editorMode) {
func (e *Editor) SetMode(m EditorMode) {
if e.mode != nil {
e.mode.exit()
e.mode.Exit()
}
e.mode = m
e.overlay = nil
Expand Down
55 changes: 0 additions & 55 deletions editor/insert.go

This file was deleted.

6 changes: 3 additions & 3 deletions editor/mode.go
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/nsf/termbox-go"
)

type editorMode interface {
onKey(ev *termbox.Event)
exit()
type EditorMode interface {
OnKey(ev *termbox.Event)
Exit()
}
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/kisielk/vigo/editor"
"github.com/kisielk/vigo/mode"
"github.com/nsf/termbox-go"
)

Expand All @@ -15,6 +16,7 @@ func main() {
termbox.SetInputMode(termbox.InputEsc)

e := editor.NewEditor(os.Args[1:])
e.SetMode(mode.NewNormalMode(e))
e.Resize()
e.Draw()
termbox.SetCursor(e.CursorPosition())
Expand Down
53 changes: 53 additions & 0 deletions mode/insert.go
@@ -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)
}
}

0 comments on commit 352917f

Please sign in to comment.