mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-10 06:40:24 +09:00
Reorganize file structure
This commit is contained in:
@@ -1,40 +1,38 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
// The ActionHandler connects the buffer and the window
|
||||
// It provides a cursor (or multiple) and defines a set of actions
|
||||
// that can be taken on the buffer
|
||||
// The ActionHandler can access the window for necessary info about
|
||||
// visual positions for mouse clicks and scrolling
|
||||
type ActionHandler struct {
|
||||
Buf *Buffer
|
||||
Win *Window
|
||||
type Event interface{}
|
||||
|
||||
// Since tcell doesn't differentiate between a mouse release event
|
||||
// and a mouse move event with no keys pressed, we need to keep
|
||||
// track of whether or not the mouse was pressed (or not released) last event to determine
|
||||
// mouse release events
|
||||
mouseReleased bool
|
||||
|
||||
// We need to keep track of insert key press toggle
|
||||
isOverwriteMode bool
|
||||
// This stores when the last click was
|
||||
// This is useful for detecting double and triple clicks
|
||||
lastClickTime time.Time
|
||||
lastLoc Loc
|
||||
|
||||
// lastCutTime stores when the last ctrl+k was issued.
|
||||
// It is used for clearing the clipboard to replace it with fresh cut lines.
|
||||
lastCutTime time.Time
|
||||
|
||||
// freshClip returns true if the clipboard has never been pasted.
|
||||
freshClip bool
|
||||
|
||||
// Was the last mouse event actually a double click?
|
||||
// Useful for detecting triple clicks -- if a double click is detected
|
||||
// but the last mouse event was actually a double click, it's a triple click
|
||||
doubleClick bool
|
||||
// Same here, just to keep track for mouse move events
|
||||
tripleClick bool
|
||||
// RawEvent is simply an escape code
|
||||
// We allow users to directly bind escape codes
|
||||
// to get around some of a limitations of terminals
|
||||
type RawEvent struct {
|
||||
esc string
|
||||
}
|
||||
|
||||
// KeyEvent is a key event containing a key code,
|
||||
// some possible modifiers (alt, ctrl, etc...) and
|
||||
// a rune if it was simply a character press
|
||||
// Note: to be compatible with tcell events,
|
||||
// for ctrl keys r=code
|
||||
type KeyEvent struct {
|
||||
code tcell.Key
|
||||
mod tcell.ModMask
|
||||
r rune
|
||||
}
|
||||
|
||||
// MouseEvent is a mouse event with a mouse button and
|
||||
// any possible key modifiers
|
||||
type MouseEvent struct {
|
||||
btn tcell.ButtonMask
|
||||
mod tcell.ModMask
|
||||
}
|
||||
|
||||
// An ActionHandler will take a tcell event and execute it
|
||||
// appropriately
|
||||
type ActionHandler interface {
|
||||
HandleEvent(tcell.Event)
|
||||
}
|
||||
|
||||
492
cmd/micro/actions.go
Normal file
492
cmd/micro/actions.go
Normal file
@@ -0,0 +1,492 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
// MousePress is the event that should happen when a normal click happens
|
||||
// This is almost always bound to left click
|
||||
func (a *BufActionHandler) MousePress(e *tcell.EventMouse) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ScrollUpAction scrolls the view up
|
||||
func (a *BufActionHandler) ScrollUpAction() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ScrollDownAction scrolls the view up
|
||||
func (a *BufActionHandler) ScrollDownAction() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Center centers the view on the cursor
|
||||
func (a *BufActionHandler) Center() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorUp moves the cursor up
|
||||
func (a *BufActionHandler) CursorUp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorDown moves the cursor down
|
||||
func (a *BufActionHandler) CursorDown() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorLeft moves the cursor left
|
||||
func (a *BufActionHandler) CursorLeft() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorRight moves the cursor right
|
||||
func (a *BufActionHandler) CursorRight() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// WordRight moves the cursor one word to the right
|
||||
func (a *BufActionHandler) WordRight() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// WordLeft moves the cursor one word to the left
|
||||
func (a *BufActionHandler) WordLeft() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectUp selects up one line
|
||||
func (a *BufActionHandler) SelectUp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectDown selects down one line
|
||||
func (a *BufActionHandler) SelectDown() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectLeft selects the character to the left of the cursor
|
||||
func (a *BufActionHandler) SelectLeft() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectRight selects the character to the right of the cursor
|
||||
func (a *BufActionHandler) SelectRight() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectWordRight selects the word to the right of the cursor
|
||||
func (a *BufActionHandler) SelectWordRight() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectWordLeft selects the word to the left of the cursor
|
||||
func (a *BufActionHandler) SelectWordLeft() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// StartOfLine moves the cursor to the start of the line
|
||||
func (a *BufActionHandler) StartOfLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// EndOfLine moves the cursor to the end of the line
|
||||
func (a *BufActionHandler) EndOfLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectLine selects the entire current line
|
||||
func (a *BufActionHandler) SelectLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectToStartOfLine selects to the start of the current line
|
||||
func (a *BufActionHandler) SelectToStartOfLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectToEndOfLine selects to the end of the current line
|
||||
func (a *BufActionHandler) SelectToEndOfLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none
|
||||
func (a *BufActionHandler) ParagraphPrevious() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none
|
||||
func (a *BufActionHandler) ParagraphNext() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Retab changes all tabs to spaces or all spaces to tabs depending
|
||||
// on the user's settings
|
||||
func (a *BufActionHandler) Retab() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorStart moves the cursor to the start of the buffer
|
||||
func (a *BufActionHandler) CursorStart() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorEnd moves the cursor to the end of the buffer
|
||||
func (a *BufActionHandler) CursorEnd() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectToStart selects the text from the cursor to the start of the buffer
|
||||
func (a *BufActionHandler) SelectToStart() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectToEnd selects the text from the cursor to the end of the buffer
|
||||
func (a *BufActionHandler) SelectToEnd() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// InsertSpace inserts a space
|
||||
func (a *BufActionHandler) InsertSpace() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// InsertNewline inserts a newline plus possible some whitespace if autoindent is on
|
||||
func (a *BufActionHandler) InsertNewline() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Backspace deletes the previous character
|
||||
func (a *BufActionHandler) Backspace() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteWordRight deletes the word to the right of the cursor
|
||||
func (a *BufActionHandler) DeleteWordRight() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteWordLeft deletes the word to the left of the cursor
|
||||
func (a *BufActionHandler) DeleteWordLeft() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Delete deletes the next character
|
||||
func (a *BufActionHandler) Delete() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IndentSelection indents the current selection
|
||||
func (a *BufActionHandler) IndentSelection() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// OutdentLine moves the current line back one indentation
|
||||
func (a *BufActionHandler) OutdentLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// OutdentSelection takes the current selection and moves it back one indent level
|
||||
func (a *BufActionHandler) OutdentSelection() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// InsertTab inserts a tab or spaces
|
||||
func (a *BufActionHandler) InsertTab() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SaveAll saves all open buffers
|
||||
func (a *BufActionHandler) SaveAll() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Save the buffer to disk
|
||||
func (a *BufActionHandler) Save() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SaveAs saves the buffer to disk with the given name
|
||||
func (a *BufActionHandler) SaveAs() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Find opens a prompt and searches forward for the input
|
||||
func (a *BufActionHandler) Find() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// FindNext searches forwards for the last used search term
|
||||
func (a *BufActionHandler) FindNext() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// FindPrevious searches backwards for the last used search term
|
||||
func (a *BufActionHandler) FindPrevious() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Undo undoes the last action
|
||||
func (a *BufActionHandler) Undo() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Redo redoes the last action
|
||||
func (a *BufActionHandler) Redo() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Copy the selection to the system clipboard
|
||||
func (a *BufActionHandler) Copy() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CutLine cuts the current line to the clipboard
|
||||
func (a *BufActionHandler) CutLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Cut the selection to the system clipboard
|
||||
func (a *BufActionHandler) Cut() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DuplicateLine duplicates the current line or selection
|
||||
func (a *BufActionHandler) DuplicateLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteLine deletes the current line
|
||||
func (a *BufActionHandler) DeleteLine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// MoveLinesUp moves up the current line or selected lines if any
|
||||
func (a *BufActionHandler) MoveLinesUp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// MoveLinesDown moves down the current line or selected lines if any
|
||||
func (a *BufActionHandler) MoveLinesDown() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Paste whatever is in the system clipboard into the buffer
|
||||
// Delete and paste if the user has a selection
|
||||
func (a *BufActionHandler) Paste() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// PastePrimary pastes from the primary clipboard (only use on linux)
|
||||
func (a *BufActionHandler) PastePrimary() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// JumpToMatchingBrace moves the cursor to the matching brace if it is
|
||||
// currently on a brace
|
||||
func (a *BufActionHandler) JumpToMatchingBrace() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectAll selects the entire buffer
|
||||
func (a *BufActionHandler) SelectAll() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// OpenFile opens a new file in the buffer
|
||||
func (a *BufActionHandler) OpenFile() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Start moves the viewport to the start of the buffer
|
||||
func (a *BufActionHandler) Start() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// End moves the viewport to the end of the buffer
|
||||
func (a *BufActionHandler) End() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// PageUp scrolls the view up a page
|
||||
func (a *BufActionHandler) PageUp() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// PageDown scrolls the view down a page
|
||||
func (a *BufActionHandler) PageDown() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SelectPageUp selects up one page
|
||||
func (a *BufActionHandler) SelectPageUp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectPageDown selects down one page
|
||||
func (a *BufActionHandler) SelectPageDown() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorPageUp places the cursor a page up
|
||||
func (a *BufActionHandler) CursorPageUp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CursorPageDown places the cursor a page up
|
||||
func (a *BufActionHandler) CursorPageDown() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// HalfPageUp scrolls the view up half a page
|
||||
func (a *BufActionHandler) HalfPageUp() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// HalfPageDown scrolls the view down half a page
|
||||
func (a *BufActionHandler) HalfPageDown() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ToggleRuler turns line numbers off and on
|
||||
func (a *BufActionHandler) ToggleRuler() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// JumpLine jumps to a line and moves the view accordingly.
|
||||
func (a *BufActionHandler) JumpLine() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ClearStatus clears the messenger bar
|
||||
func (a *BufActionHandler) ClearStatus() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ToggleHelp toggles the help screen
|
||||
func (a *BufActionHandler) ToggleHelp() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ToggleKeyMenu toggles the keymenu option and resizes all tabs
|
||||
func (a *BufActionHandler) ToggleKeyMenu() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ShellMode opens a terminal to run a shell command
|
||||
func (a *BufActionHandler) ShellMode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// CommandMode lets the user enter a command
|
||||
func (a *BufActionHandler) CommandMode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ToggleOverwriteMode lets the user toggle the text overwrite mode
|
||||
func (a *BufActionHandler) ToggleOverwriteMode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Escape leaves current mode
|
||||
func (a *BufActionHandler) Escape() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Quit this will close the current tab or view that is open
|
||||
func (a *BufActionHandler) Quit() bool {
|
||||
screen.Screen.Fini()
|
||||
os.Exit(0)
|
||||
return false
|
||||
}
|
||||
|
||||
// QuitAll quits the whole editor; all splits and tabs
|
||||
func (a *BufActionHandler) QuitAll() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AddTab adds a new tab with an empty buffer
|
||||
func (a *BufActionHandler) AddTab() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// PreviousTab switches to the previous tab in the tab list
|
||||
func (a *BufActionHandler) PreviousTab() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// NextTab switches to the next tab in the tab list
|
||||
func (a *BufActionHandler) NextTab() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// VSplitBinding opens an empty vertical split
|
||||
func (a *BufActionHandler) VSplitBinding() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// HSplitBinding opens an empty horizontal split
|
||||
func (a *BufActionHandler) HSplitBinding() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Unsplit closes all splits in the current tab except the active one
|
||||
func (a *BufActionHandler) Unsplit() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// NextSplit changes the view to the next split
|
||||
func (a *BufActionHandler) NextSplit() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// PreviousSplit changes the view to the previous split
|
||||
func (a *BufActionHandler) PreviousSplit() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var curMacro []interface{}
|
||||
var recordingMacro bool
|
||||
|
||||
// ToggleMacro toggles recording of a macro
|
||||
func (a *BufActionHandler) ToggleMacro() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// PlayMacro plays back the most recently recorded macro
|
||||
func (a *BufActionHandler) PlayMacro() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
|
||||
func (a *BufActionHandler) SpawnMultiCursor() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SpawnMultiCursorSelect adds a cursor at the beginning of each line of a selection
|
||||
func (a *BufActionHandler) SpawnMultiCursorSelect() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
|
||||
func (a *BufActionHandler) MouseMultiCursor(e *tcell.EventMouse) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SkipMultiCursor moves the current multiple cursor to the next available position
|
||||
func (a *BufActionHandler) SkipMultiCursor() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveMultiCursor removes the latest multiple cursor
|
||||
func (a *BufActionHandler) RemoveMultiCursor() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveAllMultiCursors removes all cursors except the base cursor
|
||||
func (a *BufActionHandler) RemoveAllMultiCursors() bool {
|
||||
return false
|
||||
}
|
||||
8
cmd/micro/actions_other.go
Normal file
8
cmd/micro/actions_other.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build plan9 nacl windows
|
||||
|
||||
package main
|
||||
|
||||
func (*BufActionHandler) Suspend() bool {
|
||||
// TODO: error message
|
||||
return false
|
||||
}
|
||||
35
cmd/micro/actions_posix.go
Normal file
35
cmd/micro/actions_posix.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// +build linux darwin dragonfly solaris openbsd netbsd freebsd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
)
|
||||
|
||||
// Suspend sends micro to the background. This is the same as pressing CtrlZ in most unix programs.
|
||||
// This only works on linux and has no default binding.
|
||||
// This code was adapted from the suspend code in nsf/godit
|
||||
func (*BufActionHandler) Suspend() bool {
|
||||
screenWasNil := screen.Screen == nil
|
||||
|
||||
if !screenWasNil {
|
||||
screen.Screen.Fini()
|
||||
screen.Screen = nil
|
||||
}
|
||||
|
||||
// suspend the process
|
||||
pid := syscall.Getpid()
|
||||
err := syscall.Kill(pid, syscall.SIGSTOP)
|
||||
if err != nil {
|
||||
util.TermMessage(err)
|
||||
}
|
||||
|
||||
if !screenWasNil {
|
||||
screen.Init()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
439
cmd/micro/bindings.go
Normal file
439
cmd/micro/bindings.go
Normal file
@@ -0,0 +1,439 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/flynn/json5"
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
var bindings = DefaultBindings()
|
||||
|
||||
func InitBindings() {
|
||||
var parsed map[string]string
|
||||
defaults := DefaultBindings()
|
||||
|
||||
filename := config.ConfigDir + "/bindings.json"
|
||||
if _, e := os.Stat(filename); e == nil {
|
||||
input, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
util.TermMessage("Error reading bindings.json file: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = json5.Unmarshal(input, &parsed)
|
||||
if err != nil {
|
||||
util.TermMessage("Error reading bindings.json:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range defaults {
|
||||
BindKey(k, v)
|
||||
}
|
||||
for k, v := range parsed {
|
||||
BindKey(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func BindKey(k, v string) {
|
||||
event, ok := findEvent(k)
|
||||
if !ok {
|
||||
util.TermMessage(k, "is not a bindable event")
|
||||
}
|
||||
|
||||
switch e := event.(type) {
|
||||
case KeyEvent:
|
||||
BufMapKey(e, v)
|
||||
case MouseEvent:
|
||||
BufMapMouse(e, v)
|
||||
case RawEvent:
|
||||
util.TermMessage("Raw events not supported yet")
|
||||
}
|
||||
|
||||
bindings[k] = v
|
||||
}
|
||||
|
||||
// findKeyEvent will find binding Key 'b' using string 'k'
|
||||
func findEvent(k string) (b Event, ok bool) {
|
||||
modifiers := tcell.ModNone
|
||||
|
||||
// First, we'll strip off all the modifiers in the name and add them to the
|
||||
// ModMask
|
||||
modSearch:
|
||||
for {
|
||||
switch {
|
||||
case strings.HasPrefix(k, "-"):
|
||||
// We optionally support dashes between modifiers
|
||||
k = k[1:]
|
||||
case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
|
||||
// CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
|
||||
k = k[4:]
|
||||
modifiers |= tcell.ModCtrl
|
||||
case strings.HasPrefix(k, "Alt"):
|
||||
k = k[3:]
|
||||
modifiers |= tcell.ModAlt
|
||||
case strings.HasPrefix(k, "Shift"):
|
||||
k = k[5:]
|
||||
modifiers |= tcell.ModShift
|
||||
case strings.HasPrefix(k, "\x1b"):
|
||||
return RawEvent{
|
||||
esc: k,
|
||||
}, true
|
||||
default:
|
||||
break modSearch
|
||||
}
|
||||
}
|
||||
|
||||
if len(k) == 0 {
|
||||
return KeyEvent{}, false
|
||||
}
|
||||
|
||||
// Control is handled in a special way, since the terminal sends explicitly
|
||||
// marked escape sequences for control keys
|
||||
// We should check for Control keys first
|
||||
if modifiers&tcell.ModCtrl != 0 {
|
||||
// see if the key is in bindingKeys with the Ctrl prefix.
|
||||
k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
|
||||
if code, ok := keyEvents["Ctrl"+k]; ok {
|
||||
// It is, we're done.
|
||||
return KeyEvent{
|
||||
code: code,
|
||||
mod: modifiers,
|
||||
r: rune(code),
|
||||
}, true
|
||||
}
|
||||
}
|
||||
|
||||
// See if we can find the key in bindingKeys
|
||||
if code, ok := keyEvents[k]; ok {
|
||||
return KeyEvent{
|
||||
code: code,
|
||||
mod: modifiers,
|
||||
r: 0,
|
||||
}, true
|
||||
}
|
||||
|
||||
// See if we can find the key in bindingMouse
|
||||
if code, ok := mouseEvents[k]; ok {
|
||||
return MouseEvent{
|
||||
btn: code,
|
||||
mod: modifiers,
|
||||
}, true
|
||||
}
|
||||
|
||||
// If we were given one character, then we've got a rune.
|
||||
if len(k) == 1 {
|
||||
return KeyEvent{
|
||||
code: tcell.KeyRune,
|
||||
mod: modifiers,
|
||||
r: rune(k[0]),
|
||||
}, true
|
||||
}
|
||||
|
||||
// We don't know what happened.
|
||||
return KeyEvent{}, false
|
||||
}
|
||||
|
||||
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
|
||||
// Returns true if the keybinding already existed and a possible error
|
||||
func TryBindKey(k, v string, overwrite bool) (bool, error) {
|
||||
var e error
|
||||
var parsed map[string]string
|
||||
|
||||
filename := config.ConfigDir + "/bindings.json"
|
||||
if _, e = os.Stat(filename); e == nil {
|
||||
input, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return false, errors.New("Error reading bindings.json file: " + err.Error())
|
||||
}
|
||||
|
||||
err = json5.Unmarshal(input, &parsed)
|
||||
if err != nil {
|
||||
return false, errors.New("Error reading bindings.json: " + err.Error())
|
||||
}
|
||||
|
||||
key, ok := findEvent(k)
|
||||
if !ok {
|
||||
return false, errors.New("Invalid event " + k)
|
||||
}
|
||||
|
||||
found := false
|
||||
for ev := range parsed {
|
||||
if e, ok := findEvent(ev); ok {
|
||||
if e == key {
|
||||
if overwrite {
|
||||
parsed[ev] = v
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if found && !overwrite {
|
||||
return true, nil
|
||||
} else if !found {
|
||||
parsed[k] = v
|
||||
}
|
||||
|
||||
BindKey(k, v)
|
||||
|
||||
txt, _ := json.MarshalIndent(parsed, "", " ")
|
||||
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
|
||||
}
|
||||
return false, e
|
||||
}
|
||||
|
||||
var mouseEvents = map[string]tcell.ButtonMask{
|
||||
"MouseLeft": tcell.Button1,
|
||||
"MouseMiddle": tcell.Button2,
|
||||
"MouseRight": tcell.Button3,
|
||||
"MouseWheelUp": tcell.WheelUp,
|
||||
"MouseWheelDown": tcell.WheelDown,
|
||||
"MouseWheelLeft": tcell.WheelLeft,
|
||||
"MouseWheelRight": tcell.WheelRight,
|
||||
}
|
||||
|
||||
var keyEvents = map[string]tcell.Key{
|
||||
"Up": tcell.KeyUp,
|
||||
"Down": tcell.KeyDown,
|
||||
"Right": tcell.KeyRight,
|
||||
"Left": tcell.KeyLeft,
|
||||
"UpLeft": tcell.KeyUpLeft,
|
||||
"UpRight": tcell.KeyUpRight,
|
||||
"DownLeft": tcell.KeyDownLeft,
|
||||
"DownRight": tcell.KeyDownRight,
|
||||
"Center": tcell.KeyCenter,
|
||||
"PageUp": tcell.KeyPgUp,
|
||||
"PageDown": tcell.KeyPgDn,
|
||||
"Home": tcell.KeyHome,
|
||||
"End": tcell.KeyEnd,
|
||||
"Insert": tcell.KeyInsert,
|
||||
"Delete": tcell.KeyDelete,
|
||||
"Help": tcell.KeyHelp,
|
||||
"Exit": tcell.KeyExit,
|
||||
"Clear": tcell.KeyClear,
|
||||
"Cancel": tcell.KeyCancel,
|
||||
"Print": tcell.KeyPrint,
|
||||
"Pause": tcell.KeyPause,
|
||||
"Backtab": tcell.KeyBacktab,
|
||||
"F1": tcell.KeyF1,
|
||||
"F2": tcell.KeyF2,
|
||||
"F3": tcell.KeyF3,
|
||||
"F4": tcell.KeyF4,
|
||||
"F5": tcell.KeyF5,
|
||||
"F6": tcell.KeyF6,
|
||||
"F7": tcell.KeyF7,
|
||||
"F8": tcell.KeyF8,
|
||||
"F9": tcell.KeyF9,
|
||||
"F10": tcell.KeyF10,
|
||||
"F11": tcell.KeyF11,
|
||||
"F12": tcell.KeyF12,
|
||||
"F13": tcell.KeyF13,
|
||||
"F14": tcell.KeyF14,
|
||||
"F15": tcell.KeyF15,
|
||||
"F16": tcell.KeyF16,
|
||||
"F17": tcell.KeyF17,
|
||||
"F18": tcell.KeyF18,
|
||||
"F19": tcell.KeyF19,
|
||||
"F20": tcell.KeyF20,
|
||||
"F21": tcell.KeyF21,
|
||||
"F22": tcell.KeyF22,
|
||||
"F23": tcell.KeyF23,
|
||||
"F24": tcell.KeyF24,
|
||||
"F25": tcell.KeyF25,
|
||||
"F26": tcell.KeyF26,
|
||||
"F27": tcell.KeyF27,
|
||||
"F28": tcell.KeyF28,
|
||||
"F29": tcell.KeyF29,
|
||||
"F30": tcell.KeyF30,
|
||||
"F31": tcell.KeyF31,
|
||||
"F32": tcell.KeyF32,
|
||||
"F33": tcell.KeyF33,
|
||||
"F34": tcell.KeyF34,
|
||||
"F35": tcell.KeyF35,
|
||||
"F36": tcell.KeyF36,
|
||||
"F37": tcell.KeyF37,
|
||||
"F38": tcell.KeyF38,
|
||||
"F39": tcell.KeyF39,
|
||||
"F40": tcell.KeyF40,
|
||||
"F41": tcell.KeyF41,
|
||||
"F42": tcell.KeyF42,
|
||||
"F43": tcell.KeyF43,
|
||||
"F44": tcell.KeyF44,
|
||||
"F45": tcell.KeyF45,
|
||||
"F46": tcell.KeyF46,
|
||||
"F47": tcell.KeyF47,
|
||||
"F48": tcell.KeyF48,
|
||||
"F49": tcell.KeyF49,
|
||||
"F50": tcell.KeyF50,
|
||||
"F51": tcell.KeyF51,
|
||||
"F52": tcell.KeyF52,
|
||||
"F53": tcell.KeyF53,
|
||||
"F54": tcell.KeyF54,
|
||||
"F55": tcell.KeyF55,
|
||||
"F56": tcell.KeyF56,
|
||||
"F57": tcell.KeyF57,
|
||||
"F58": tcell.KeyF58,
|
||||
"F59": tcell.KeyF59,
|
||||
"F60": tcell.KeyF60,
|
||||
"F61": tcell.KeyF61,
|
||||
"F62": tcell.KeyF62,
|
||||
"F63": tcell.KeyF63,
|
||||
"F64": tcell.KeyF64,
|
||||
"CtrlSpace": tcell.KeyCtrlSpace,
|
||||
"CtrlA": tcell.KeyCtrlA,
|
||||
"CtrlB": tcell.KeyCtrlB,
|
||||
"CtrlC": tcell.KeyCtrlC,
|
||||
"CtrlD": tcell.KeyCtrlD,
|
||||
"CtrlE": tcell.KeyCtrlE,
|
||||
"CtrlF": tcell.KeyCtrlF,
|
||||
"CtrlG": tcell.KeyCtrlG,
|
||||
"CtrlH": tcell.KeyCtrlH,
|
||||
"CtrlI": tcell.KeyCtrlI,
|
||||
"CtrlJ": tcell.KeyCtrlJ,
|
||||
"CtrlK": tcell.KeyCtrlK,
|
||||
"CtrlL": tcell.KeyCtrlL,
|
||||
"CtrlM": tcell.KeyCtrlM,
|
||||
"CtrlN": tcell.KeyCtrlN,
|
||||
"CtrlO": tcell.KeyCtrlO,
|
||||
"CtrlP": tcell.KeyCtrlP,
|
||||
"CtrlQ": tcell.KeyCtrlQ,
|
||||
"CtrlR": tcell.KeyCtrlR,
|
||||
"CtrlS": tcell.KeyCtrlS,
|
||||
"CtrlT": tcell.KeyCtrlT,
|
||||
"CtrlU": tcell.KeyCtrlU,
|
||||
"CtrlV": tcell.KeyCtrlV,
|
||||
"CtrlW": tcell.KeyCtrlW,
|
||||
"CtrlX": tcell.KeyCtrlX,
|
||||
"CtrlY": tcell.KeyCtrlY,
|
||||
"CtrlZ": tcell.KeyCtrlZ,
|
||||
"CtrlLeftSq": tcell.KeyCtrlLeftSq,
|
||||
"CtrlBackslash": tcell.KeyCtrlBackslash,
|
||||
"CtrlRightSq": tcell.KeyCtrlRightSq,
|
||||
"CtrlCarat": tcell.KeyCtrlCarat,
|
||||
"CtrlUnderscore": tcell.KeyCtrlUnderscore,
|
||||
"CtrlPageUp": tcell.KeyCtrlPgUp,
|
||||
"CtrlPageDown": tcell.KeyCtrlPgDn,
|
||||
"Tab": tcell.KeyTab,
|
||||
"Esc": tcell.KeyEsc,
|
||||
"Escape": tcell.KeyEscape,
|
||||
"Enter": tcell.KeyEnter,
|
||||
"Backspace": tcell.KeyBackspace2,
|
||||
"OldBackspace": tcell.KeyBackspace,
|
||||
|
||||
// I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
|
||||
"PgUp": tcell.KeyPgUp,
|
||||
"PgDown": tcell.KeyPgDn,
|
||||
}
|
||||
|
||||
// DefaultBindings returns a map containing micro's default keybindings
|
||||
func DefaultBindings() map[string]string {
|
||||
return map[string]string{
|
||||
"Up": "CursorUp",
|
||||
"Down": "CursorDown",
|
||||
"Right": "CursorRight",
|
||||
"Left": "CursorLeft",
|
||||
"ShiftUp": "SelectUp",
|
||||
"ShiftDown": "SelectDown",
|
||||
"ShiftLeft": "SelectLeft",
|
||||
"ShiftRight": "SelectRight",
|
||||
"AltLeft": "WordLeft",
|
||||
"AltRight": "WordRight",
|
||||
"AltUp": "MoveLinesUp",
|
||||
"AltDown": "MoveLinesDown",
|
||||
"AltShiftRight": "SelectWordRight",
|
||||
"AltShiftLeft": "SelectWordLeft",
|
||||
"CtrlLeft": "StartOfLine",
|
||||
"CtrlRight": "EndOfLine",
|
||||
"CtrlShiftLeft": "SelectToStartOfLine",
|
||||
"ShiftHome": "SelectToStartOfLine",
|
||||
"CtrlShiftRight": "SelectToEndOfLine",
|
||||
"ShiftEnd": "SelectToEndOfLine",
|
||||
"CtrlUp": "CursorStart",
|
||||
"CtrlDown": "CursorEnd",
|
||||
"CtrlShiftUp": "SelectToStart",
|
||||
"CtrlShiftDown": "SelectToEnd",
|
||||
"Alt-{": "ParagraphPrevious",
|
||||
"Alt-}": "ParagraphNext",
|
||||
"Enter": "InsertNewline",
|
||||
"CtrlH": "Backspace",
|
||||
"Backspace": "Backspace",
|
||||
"Alt-CtrlH": "DeleteWordLeft",
|
||||
"Alt-Backspace": "DeleteWordLeft",
|
||||
"Tab": "IndentSelection,InsertTab",
|
||||
"Backtab": "OutdentSelection,OutdentLine",
|
||||
"CtrlO": "OpenFile",
|
||||
"CtrlS": "Save",
|
||||
"CtrlF": "Find",
|
||||
"CtrlN": "FindNext",
|
||||
"CtrlP": "FindPrevious",
|
||||
"CtrlZ": "Undo",
|
||||
"CtrlY": "Redo",
|
||||
"CtrlC": "Copy",
|
||||
"CtrlX": "Cut",
|
||||
"CtrlK": "CutLine",
|
||||
"CtrlD": "DuplicateLine",
|
||||
"CtrlV": "Paste",
|
||||
"CtrlA": "SelectAll",
|
||||
"CtrlT": "AddTab",
|
||||
"Alt,": "PreviousTab",
|
||||
"Alt.": "NextTab",
|
||||
"Home": "StartOfLine",
|
||||
"End": "EndOfLine",
|
||||
"CtrlHome": "CursorStart",
|
||||
"CtrlEnd": "CursorEnd",
|
||||
"PageUp": "CursorPageUp",
|
||||
"PageDown": "CursorPageDown",
|
||||
"CtrlPageUp": "PreviousTab",
|
||||
"CtrlPageDown": "NextTab",
|
||||
"CtrlG": "ToggleHelp",
|
||||
"Alt-g": "ToggleKeyMenu",
|
||||
"CtrlR": "ToggleRuler",
|
||||
"CtrlL": "JumpLine",
|
||||
"Delete": "Delete",
|
||||
"CtrlB": "ShellMode",
|
||||
"CtrlQ": "Quit",
|
||||
"CtrlE": "CommandMode",
|
||||
"CtrlW": "NextSplit",
|
||||
"CtrlU": "ToggleMacro",
|
||||
"CtrlJ": "PlayMacro",
|
||||
"Insert": "ToggleOverwriteMode",
|
||||
|
||||
// Emacs-style keybindings
|
||||
"Alt-f": "WordRight",
|
||||
"Alt-b": "WordLeft",
|
||||
"Alt-a": "StartOfLine",
|
||||
"Alt-e": "EndOfLine",
|
||||
// "Alt-p": "CursorUp",
|
||||
// "Alt-n": "CursorDown",
|
||||
|
||||
// Integration with file managers
|
||||
"F2": "Save",
|
||||
"F3": "Find",
|
||||
"F4": "Quit",
|
||||
"F7": "Find",
|
||||
"F10": "Quit",
|
||||
"Esc": "Escape",
|
||||
|
||||
// Mouse bindings
|
||||
"MouseWheelUp": "ScrollUp",
|
||||
"MouseWheelDown": "ScrollDown",
|
||||
"MouseLeft": "MousePress",
|
||||
"MouseMiddle": "PastePrimary",
|
||||
"Ctrl-MouseLeft": "MouseMultiCursor",
|
||||
|
||||
"Alt-n": "SpawnMultiCursor",
|
||||
"Alt-m": "SpawnMultiCursorSelect",
|
||||
"Alt-p": "RemoveMultiCursor",
|
||||
"Alt-c": "RemoveAllMultiCursors",
|
||||
"Alt-x": "SkipMultiCursor",
|
||||
}
|
||||
}
|
||||
207
cmd/micro/bufactionhandler.go
Normal file
207
cmd/micro/bufactionhandler.go
Normal file
@@ -0,0 +1,207 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/buffer"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
type BufKeyAction func(*BufActionHandler) bool
|
||||
type BufMouseAction func(*BufActionHandler, *tcell.EventMouse) bool
|
||||
|
||||
var BufKeyBindings map[KeyEvent]BufKeyAction
|
||||
var BufMouseBindings map[MouseEvent]BufMouseAction
|
||||
|
||||
func init() {
|
||||
BufKeyBindings = make(map[KeyEvent]BufKeyAction)
|
||||
BufMouseBindings = make(map[MouseEvent]BufMouseAction)
|
||||
}
|
||||
|
||||
func BufMapKey(k KeyEvent, action string) {
|
||||
BufKeyBindings[k] = BufKeyActions[action]
|
||||
}
|
||||
func BufMapMouse(k MouseEvent, action string) {
|
||||
BufMouseBindings[k] = BufMouseActions[action]
|
||||
}
|
||||
|
||||
// The BufActionHandler connects the buffer and the window
|
||||
// It provides a cursor (or multiple) and defines a set of actions
|
||||
// that can be taken on the buffer
|
||||
// The ActionHandler can access the window for necessary info about
|
||||
// visual positions for mouse clicks and scrolling
|
||||
type BufActionHandler struct {
|
||||
Buf *buffer.Buffer
|
||||
Win *Window
|
||||
|
||||
cursors []*buffer.Cursor
|
||||
Cursor *buffer.Cursor // the active cursor
|
||||
|
||||
// Since tcell doesn't differentiate between a mouse release event
|
||||
// and a mouse move event with no keys pressed, we need to keep
|
||||
// track of whether or not the mouse was pressed (or not released) last event to determine
|
||||
// mouse release events
|
||||
mouseReleased bool
|
||||
|
||||
// We need to keep track of insert key press toggle
|
||||
isOverwriteMode bool
|
||||
// This stores when the last click was
|
||||
// This is useful for detecting double and triple clicks
|
||||
lastClickTime time.Time
|
||||
lastLoc buffer.Loc
|
||||
|
||||
// lastCutTime stores when the last ctrl+k was issued.
|
||||
// It is used for clearing the clipboard to replace it with fresh cut lines.
|
||||
lastCutTime time.Time
|
||||
|
||||
// freshClip returns true if the clipboard has never been pasted.
|
||||
freshClip bool
|
||||
|
||||
// Was the last mouse event actually a double click?
|
||||
// Useful for detecting triple clicks -- if a double click is detected
|
||||
// but the last mouse event was actually a double click, it's a triple click
|
||||
doubleClick bool
|
||||
// Same here, just to keep track for mouse move events
|
||||
tripleClick bool
|
||||
}
|
||||
|
||||
func NewBufActionHandler(buf *buffer.Buffer, win *Window) *BufActionHandler {
|
||||
a := new(BufActionHandler)
|
||||
a.Buf = buf
|
||||
a.Win = win
|
||||
|
||||
a.cursors = []*buffer.Cursor{&buffer.Cursor{
|
||||
Buf: buf,
|
||||
Loc: buf.StartCursor,
|
||||
}}
|
||||
|
||||
buf.SetCursors(a.cursors)
|
||||
return a
|
||||
}
|
||||
|
||||
// HandleEvent executes the tcell event properly
|
||||
// TODO: multiple actions bound to one key
|
||||
func (a *BufActionHandler) HandleEvent(event tcell.Event) {
|
||||
switch e := event.(type) {
|
||||
case *tcell.EventKey:
|
||||
ke := KeyEvent{
|
||||
code: e.Key(),
|
||||
mod: e.Modifiers(),
|
||||
r: e.Rune(),
|
||||
}
|
||||
if action, ok := BufKeyBindings[ke]; ok {
|
||||
action(a)
|
||||
}
|
||||
case *tcell.EventMouse:
|
||||
me := MouseEvent{
|
||||
btn: e.Buttons(),
|
||||
mod: e.Modifiers(),
|
||||
}
|
||||
if action, ok := BufMouseBindings[me]; ok {
|
||||
action(a, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var BufKeyActions = map[string]BufKeyAction{
|
||||
"CursorUp": (*BufActionHandler).CursorUp,
|
||||
"CursorDown": (*BufActionHandler).CursorDown,
|
||||
"CursorPageUp": (*BufActionHandler).CursorPageUp,
|
||||
"CursorPageDown": (*BufActionHandler).CursorPageDown,
|
||||
"CursorLeft": (*BufActionHandler).CursorLeft,
|
||||
"CursorRight": (*BufActionHandler).CursorRight,
|
||||
"CursorStart": (*BufActionHandler).CursorStart,
|
||||
"CursorEnd": (*BufActionHandler).CursorEnd,
|
||||
"SelectToStart": (*BufActionHandler).SelectToStart,
|
||||
"SelectToEnd": (*BufActionHandler).SelectToEnd,
|
||||
"SelectUp": (*BufActionHandler).SelectUp,
|
||||
"SelectDown": (*BufActionHandler).SelectDown,
|
||||
"SelectLeft": (*BufActionHandler).SelectLeft,
|
||||
"SelectRight": (*BufActionHandler).SelectRight,
|
||||
"WordRight": (*BufActionHandler).WordRight,
|
||||
"WordLeft": (*BufActionHandler).WordLeft,
|
||||
"SelectWordRight": (*BufActionHandler).SelectWordRight,
|
||||
"SelectWordLeft": (*BufActionHandler).SelectWordLeft,
|
||||
"DeleteWordRight": (*BufActionHandler).DeleteWordRight,
|
||||
"DeleteWordLeft": (*BufActionHandler).DeleteWordLeft,
|
||||
"SelectLine": (*BufActionHandler).SelectLine,
|
||||
"SelectToStartOfLine": (*BufActionHandler).SelectToStartOfLine,
|
||||
"SelectToEndOfLine": (*BufActionHandler).SelectToEndOfLine,
|
||||
"ParagraphPrevious": (*BufActionHandler).ParagraphPrevious,
|
||||
"ParagraphNext": (*BufActionHandler).ParagraphNext,
|
||||
"InsertNewline": (*BufActionHandler).InsertNewline,
|
||||
"InsertSpace": (*BufActionHandler).InsertSpace,
|
||||
"Backspace": (*BufActionHandler).Backspace,
|
||||
"Delete": (*BufActionHandler).Delete,
|
||||
"InsertTab": (*BufActionHandler).InsertTab,
|
||||
"Save": (*BufActionHandler).Save,
|
||||
"SaveAll": (*BufActionHandler).SaveAll,
|
||||
"SaveAs": (*BufActionHandler).SaveAs,
|
||||
"Find": (*BufActionHandler).Find,
|
||||
"FindNext": (*BufActionHandler).FindNext,
|
||||
"FindPrevious": (*BufActionHandler).FindPrevious,
|
||||
"Center": (*BufActionHandler).Center,
|
||||
"Undo": (*BufActionHandler).Undo,
|
||||
"Redo": (*BufActionHandler).Redo,
|
||||
"Copy": (*BufActionHandler).Copy,
|
||||
"Cut": (*BufActionHandler).Cut,
|
||||
"CutLine": (*BufActionHandler).CutLine,
|
||||
"DuplicateLine": (*BufActionHandler).DuplicateLine,
|
||||
"DeleteLine": (*BufActionHandler).DeleteLine,
|
||||
"MoveLinesUp": (*BufActionHandler).MoveLinesUp,
|
||||
"MoveLinesDown": (*BufActionHandler).MoveLinesDown,
|
||||
"IndentSelection": (*BufActionHandler).IndentSelection,
|
||||
"OutdentSelection": (*BufActionHandler).OutdentSelection,
|
||||
"OutdentLine": (*BufActionHandler).OutdentLine,
|
||||
"Paste": (*BufActionHandler).Paste,
|
||||
"PastePrimary": (*BufActionHandler).PastePrimary,
|
||||
"SelectAll": (*BufActionHandler).SelectAll,
|
||||
"OpenFile": (*BufActionHandler).OpenFile,
|
||||
"Start": (*BufActionHandler).Start,
|
||||
"End": (*BufActionHandler).End,
|
||||
"PageUp": (*BufActionHandler).PageUp,
|
||||
"PageDown": (*BufActionHandler).PageDown,
|
||||
"SelectPageUp": (*BufActionHandler).SelectPageUp,
|
||||
"SelectPageDown": (*BufActionHandler).SelectPageDown,
|
||||
"HalfPageUp": (*BufActionHandler).HalfPageUp,
|
||||
"HalfPageDown": (*BufActionHandler).HalfPageDown,
|
||||
"StartOfLine": (*BufActionHandler).StartOfLine,
|
||||
"EndOfLine": (*BufActionHandler).EndOfLine,
|
||||
"ToggleHelp": (*BufActionHandler).ToggleHelp,
|
||||
"ToggleKeyMenu": (*BufActionHandler).ToggleKeyMenu,
|
||||
"ToggleRuler": (*BufActionHandler).ToggleRuler,
|
||||
"JumpLine": (*BufActionHandler).JumpLine,
|
||||
"ClearStatus": (*BufActionHandler).ClearStatus,
|
||||
"ShellMode": (*BufActionHandler).ShellMode,
|
||||
"CommandMode": (*BufActionHandler).CommandMode,
|
||||
"ToggleOverwriteMode": (*BufActionHandler).ToggleOverwriteMode,
|
||||
"Escape": (*BufActionHandler).Escape,
|
||||
"Quit": (*BufActionHandler).Quit,
|
||||
"QuitAll": (*BufActionHandler).QuitAll,
|
||||
"AddTab": (*BufActionHandler).AddTab,
|
||||
"PreviousTab": (*BufActionHandler).PreviousTab,
|
||||
"NextTab": (*BufActionHandler).NextTab,
|
||||
"NextSplit": (*BufActionHandler).NextSplit,
|
||||
"PreviousSplit": (*BufActionHandler).PreviousSplit,
|
||||
"Unsplit": (*BufActionHandler).Unsplit,
|
||||
"VSplit": (*BufActionHandler).VSplitBinding,
|
||||
"HSplit": (*BufActionHandler).HSplitBinding,
|
||||
"ToggleMacro": (*BufActionHandler).ToggleMacro,
|
||||
"PlayMacro": (*BufActionHandler).PlayMacro,
|
||||
"Suspend": (*BufActionHandler).Suspend,
|
||||
"ScrollUp": (*BufActionHandler).ScrollUpAction,
|
||||
"ScrollDown": (*BufActionHandler).ScrollDownAction,
|
||||
"SpawnMultiCursor": (*BufActionHandler).SpawnMultiCursor,
|
||||
"SpawnMultiCursorSelect": (*BufActionHandler).SpawnMultiCursorSelect,
|
||||
"RemoveMultiCursor": (*BufActionHandler).RemoveMultiCursor,
|
||||
"RemoveAllMultiCursors": (*BufActionHandler).RemoveAllMultiCursors,
|
||||
"SkipMultiCursor": (*BufActionHandler).SkipMultiCursor,
|
||||
"JumpToMatchingBrace": (*BufActionHandler).JumpToMatchingBrace,
|
||||
|
||||
// This was changed to InsertNewline but I don't want to break backwards compatibility
|
||||
"InsertEnter": (*BufActionHandler).InsertNewline,
|
||||
}
|
||||
var BufMouseActions = map[string]BufMouseAction{
|
||||
"MousePress": (*BufActionHandler).MousePress,
|
||||
"MouseMultiCursor": (*BufActionHandler).MouseMultiCursor,
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
@@ -16,13 +16,42 @@ import (
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/highlight"
|
||||
|
||||
. "github.com/zyedidia/micro/cmd/micro/util"
|
||||
)
|
||||
|
||||
// LargeFileThreshold is the number of bytes when fastdirty is forced
|
||||
// because hashing is too slow
|
||||
const LargeFileThreshold = 50000
|
||||
|
||||
// overwriteFile opens the given file for writing, truncating if one exists, and then calls
|
||||
// the supplied function with the file as io.Writer object, also making sure the file is
|
||||
// closed afterwards.
|
||||
func overwriteFile(name string, fn func(io.Writer) error) (err error) {
|
||||
var file *os.File
|
||||
|
||||
if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if e := file.Close(); e != nil && err == nil {
|
||||
err = e
|
||||
}
|
||||
}()
|
||||
|
||||
w := bufio.NewWriter(file)
|
||||
|
||||
if err = fn(w); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = w.Flush()
|
||||
return
|
||||
}
|
||||
|
||||
// The BufType defines what kind of buffer this is
|
||||
type BufType struct {
|
||||
Kind int
|
||||
@@ -31,17 +60,20 @@ type BufType struct {
|
||||
}
|
||||
|
||||
var (
|
||||
btDefault = BufType{0, false, false}
|
||||
btHelp = BufType{1, true, true}
|
||||
btLog = BufType{2, true, true}
|
||||
btScratch = BufType{3, false, true}
|
||||
btRaw = BufType{4, true, true}
|
||||
BTDefault = BufType{0, false, false}
|
||||
BTHelp = BufType{1, true, true}
|
||||
BTLog = BufType{2, true, true}
|
||||
BTScratch = BufType{3, false, true}
|
||||
BTRaw = BufType{4, true, true}
|
||||
)
|
||||
|
||||
type Buffer struct {
|
||||
*LineArray
|
||||
*EventHandler
|
||||
|
||||
cursors []*Cursor
|
||||
StartCursor Loc
|
||||
|
||||
// Path to the file on disk
|
||||
Path string
|
||||
// Absolute path to the file on disk
|
||||
@@ -55,8 +87,8 @@ type Buffer struct {
|
||||
// Stores the last modification time of the file the buffer is pointing to
|
||||
ModTime time.Time
|
||||
|
||||
syntaxDef *highlight.Def
|
||||
highlighter *highlight.Highlighter
|
||||
SyntaxDef *highlight.Def
|
||||
Highlighter *highlight.Highlighter
|
||||
|
||||
// Hash of the original buffer -- empty if fastdirty is on
|
||||
origHash [md5.Size]byte
|
||||
@@ -68,6 +100,14 @@ type Buffer struct {
|
||||
Type BufType
|
||||
}
|
||||
|
||||
// The SerializedBuffer holds the types that get serialized when a buffer is saved
|
||||
// These are used for the savecursor and saveundo options
|
||||
type SerializedBuffer struct {
|
||||
EventHandler *EventHandler
|
||||
Cursor Loc
|
||||
ModTime time.Time
|
||||
}
|
||||
|
||||
// NewBufferFromFile opens a new buffer using the given path
|
||||
// It will also automatically handle `~`, and line/column with filename:l:c
|
||||
// It will return an empty buffer if the path does not exist
|
||||
@@ -111,13 +151,13 @@ func NewBufferFromString(text, path string) *Buffer {
|
||||
func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []string) *Buffer {
|
||||
b := new(Buffer)
|
||||
|
||||
b.Settings = DefaultLocalSettings()
|
||||
for k, v := range globalSettings {
|
||||
b.Settings = config.DefaultLocalSettings()
|
||||
for k, v := range config.GlobalSettings {
|
||||
if _, ok := b.Settings[k]; ok {
|
||||
b.Settings[k] = v
|
||||
}
|
||||
}
|
||||
InitLocalSettings(b)
|
||||
config.InitLocalSettings(b.Settings, b.Path)
|
||||
|
||||
b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
|
||||
|
||||
@@ -132,7 +172,23 @@ func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []strin
|
||||
b.EventHandler = NewEventHandler(b)
|
||||
|
||||
b.UpdateRules()
|
||||
log.Println("Filetype detected: ", b.Settings["filetype"])
|
||||
|
||||
if _, err := os.Stat(config.ConfigDir + "/buffers/"); os.IsNotExist(err) {
|
||||
os.Mkdir(config.ConfigDir+"/buffers/", os.ModePerm)
|
||||
}
|
||||
|
||||
// cursorLocation, err := GetBufferCursorLocation(cursorPosition, b)
|
||||
// b.startcursor = Cursor{
|
||||
// Loc: cursorLocation,
|
||||
// buf: b,
|
||||
// }
|
||||
// TODO flagstartpos
|
||||
if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
|
||||
err := b.Unserialize()
|
||||
if err != nil {
|
||||
TermMessage(err)
|
||||
}
|
||||
}
|
||||
|
||||
if !b.Settings["fastdirty"].(bool) {
|
||||
if size > LargeFileThreshold {
|
||||
@@ -286,35 +342,7 @@ func (b *Buffer) SaveAs(filename string) error {
|
||||
absPath, _ := filepath.Abs(filename)
|
||||
b.AbsPath = absPath
|
||||
b.isModified = false
|
||||
// TODO: serialize
|
||||
// return b.Serialize()
|
||||
return nil
|
||||
}
|
||||
|
||||
// overwriteFile opens the given file for writing, truncating if one exists, and then calls
|
||||
// the supplied function with the file as io.Writer object, also making sure the file is
|
||||
// closed afterwards.
|
||||
func overwriteFile(name string, fn func(io.Writer) error) (err error) {
|
||||
var file *os.File
|
||||
|
||||
if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if e := file.Close(); e != nil && err == nil {
|
||||
err = e
|
||||
}
|
||||
}()
|
||||
|
||||
w := bufio.NewWriter(file)
|
||||
|
||||
if err = fn(w); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = w.Flush()
|
||||
return
|
||||
return b.Serialize()
|
||||
}
|
||||
|
||||
// SaveWithSudo saves the buffer to the default path with sudo
|
||||
@@ -331,7 +359,7 @@ func (b *Buffer) SaveAsWithSudo(filename string) error {
|
||||
b.AbsPath = absPath
|
||||
|
||||
// Set up everything for the command
|
||||
cmd := exec.Command(globalSettings["sucmd"].(string), "tee", filename)
|
||||
cmd := exec.Command(config.GlobalSettings["sucmd"].(string), "tee", filename)
|
||||
cmd.Stdin = bytes.NewBuffer(b.Bytes())
|
||||
|
||||
// This is a trap for Ctrl-C so that it doesn't kill micro
|
||||
@@ -351,25 +379,29 @@ func (b *Buffer) SaveAsWithSudo(filename string) error {
|
||||
if err == nil {
|
||||
b.isModified = false
|
||||
b.ModTime, _ = GetModTime(filename)
|
||||
// TODO: serialize
|
||||
return b.Serialize()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *Buffer) SetCursors(c []*Cursor) {
|
||||
b.cursors = c
|
||||
}
|
||||
|
||||
func (b *Buffer) GetActiveCursor() *Cursor {
|
||||
return nil
|
||||
return b.cursors[0]
|
||||
}
|
||||
|
||||
func (b *Buffer) GetCursor(n int) *Cursor {
|
||||
return nil
|
||||
return b.cursors[n]
|
||||
}
|
||||
|
||||
func (b *Buffer) GetCursors() []*Cursor {
|
||||
return nil
|
||||
return b.cursors
|
||||
}
|
||||
|
||||
func (b *Buffer) NumCursors() int {
|
||||
return 0
|
||||
return len(b.cursors)
|
||||
}
|
||||
|
||||
func (b *Buffer) LineBytes(n int) []byte {
|
||||
@@ -440,12 +472,62 @@ func calcHash(b *Buffer, out *[md5.Size]byte) {
|
||||
h.Sum((*out)[:0])
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register(TextEvent{})
|
||||
gob.Register(SerializedBuffer{})
|
||||
}
|
||||
|
||||
// Serialize serializes the buffer to config.ConfigDir/buffers
|
||||
func (b *Buffer) Serialize() error {
|
||||
if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
|
||||
return nil
|
||||
}
|
||||
|
||||
name := config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath)
|
||||
|
||||
return overwriteFile(name, func(file io.Writer) error {
|
||||
return gob.NewEncoder(file).Encode(SerializedBuffer{
|
||||
b.EventHandler,
|
||||
b.GetActiveCursor().Loc,
|
||||
b.ModTime,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Buffer) Unserialize() error {
|
||||
// If either savecursor or saveundo is turned on, we need to load the serialized information
|
||||
// from ~/.config/micro/buffers
|
||||
file, err := os.Open(config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath))
|
||||
defer file.Close()
|
||||
if err == nil {
|
||||
var buffer SerializedBuffer
|
||||
decoder := gob.NewDecoder(file)
|
||||
gob.Register(TextEvent{})
|
||||
err = decoder.Decode(&buffer)
|
||||
if err != nil {
|
||||
return errors.New(err.Error() + "\nYou may want to remove the files in ~/.config/micro/buffers (these files store the information for the 'saveundo' and 'savecursor' options) if this problem persists.")
|
||||
}
|
||||
if b.Settings["savecursor"].(bool) {
|
||||
b.StartCursor = buffer.Cursor
|
||||
}
|
||||
|
||||
if b.Settings["saveundo"].(bool) {
|
||||
// We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
|
||||
if b.ModTime == buffer.ModTime {
|
||||
b.EventHandler = buffer.EventHandler
|
||||
b.EventHandler.buf = b
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateRules updates the syntax rules and filetype for this buffer
|
||||
// This is called when the colorscheme changes
|
||||
func (b *Buffer) UpdateRules() {
|
||||
rehighlight := false
|
||||
var files []*highlight.File
|
||||
for _, f := range ListRuntimeFiles(RTSyntax) {
|
||||
for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
|
||||
data, err := f.Data()
|
||||
if err != nil {
|
||||
TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
|
||||
@@ -467,7 +549,7 @@ func (b *Buffer) UpdateRules() {
|
||||
header := new(highlight.Header)
|
||||
header.FileType = file.FileType
|
||||
header.FtDetect = ftdetect
|
||||
b.syntaxDef, err = highlight.ParseDef(file, header)
|
||||
b.SyntaxDef, err = highlight.ParseDef(file, header)
|
||||
if err != nil {
|
||||
TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
|
||||
continue
|
||||
@@ -479,7 +561,7 @@ func (b *Buffer) UpdateRules() {
|
||||
header := new(highlight.Header)
|
||||
header.FileType = file.FileType
|
||||
header.FtDetect = ftdetect
|
||||
b.syntaxDef, err = highlight.ParseDef(file, header)
|
||||
b.SyntaxDef, err = highlight.ParseDef(file, header)
|
||||
if err != nil {
|
||||
TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
|
||||
continue
|
||||
@@ -491,16 +573,16 @@ func (b *Buffer) UpdateRules() {
|
||||
}
|
||||
}
|
||||
|
||||
if b.syntaxDef != nil {
|
||||
highlight.ResolveIncludes(b.syntaxDef, files)
|
||||
if b.SyntaxDef != nil {
|
||||
highlight.ResolveIncludes(b.SyntaxDef, files)
|
||||
}
|
||||
|
||||
if b.highlighter == nil || rehighlight {
|
||||
if b.syntaxDef != nil {
|
||||
b.Settings["filetype"] = b.syntaxDef.FileType
|
||||
b.highlighter = highlight.NewHighlighter(b.syntaxDef)
|
||||
if b.Highlighter == nil || rehighlight {
|
||||
if b.SyntaxDef != nil {
|
||||
b.Settings["filetype"] = b.SyntaxDef.FileType
|
||||
b.Highlighter = highlight.NewHighlighter(b.SyntaxDef)
|
||||
if b.Settings["syntax"].(bool) {
|
||||
b.highlighter.HighlightStates(b)
|
||||
b.Highlighter.HighlightStates(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
|
||||
runewidth "github.com/mattn/go-runewidth"
|
||||
"github.com/zyedidia/clipboard"
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
)
|
||||
|
||||
// InBounds returns whether the given location is a valid character position in the given buffer
|
||||
@@ -19,7 +20,7 @@ func InBounds(pos Loc, buf *Buffer) bool {
|
||||
// The Cursor struct stores the location of the cursor in the buffer
|
||||
// as well as the selection
|
||||
type Cursor struct {
|
||||
buf *Buffer
|
||||
Buf *Buffer
|
||||
Loc
|
||||
|
||||
// Last cursor x position
|
||||
@@ -57,20 +58,20 @@ func (c *Cursor) GetVisualX() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
bytes := c.buf.LineBytes(c.Y)
|
||||
tabsize := int(c.buf.Settings["tabsize"].(float64))
|
||||
bytes := c.Buf.LineBytes(c.Y)
|
||||
tabsize := int(c.Buf.Settings["tabsize"].(float64))
|
||||
if c.X > utf8.RuneCount(bytes) {
|
||||
c.X = utf8.RuneCount(bytes) - 1
|
||||
}
|
||||
|
||||
return StringWidth(bytes, c.X, tabsize)
|
||||
return util.StringWidth(bytes, c.X, tabsize)
|
||||
}
|
||||
|
||||
// GetCharPosInLine gets the char position of a visual x y
|
||||
// coordinate (this is necessary because tabs are 1 char but
|
||||
// 4 visual spaces)
|
||||
func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
|
||||
tabsize := int(c.buf.Settings["tabsize"].(float64))
|
||||
tabsize := int(c.Buf.Settings["tabsize"].(float64))
|
||||
|
||||
// Scan rune by rune until we exceed the visual width that we are
|
||||
// looking for. Then we can return the character position we have found
|
||||
@@ -106,7 +107,7 @@ func (c *Cursor) Start() {
|
||||
|
||||
// End moves the cursor to the end of the line it is on
|
||||
func (c *Cursor) End() {
|
||||
c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
|
||||
c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
|
||||
c.LastVisualX = c.GetVisualX()
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ func (c *Cursor) End() {
|
||||
// or "clipboard"
|
||||
func (c *Cursor) CopySelection(target string) {
|
||||
if c.HasSelection() {
|
||||
if target != "primary" || c.buf.Settings["useprimary"].(bool) {
|
||||
if target != "primary" || c.Buf.Settings["useprimary"].(bool) {
|
||||
clipboard.WriteAll(string(c.GetSelection()), target)
|
||||
}
|
||||
}
|
||||
@@ -122,8 +123,8 @@ func (c *Cursor) CopySelection(target string) {
|
||||
|
||||
// ResetSelection resets the user's selection
|
||||
func (c *Cursor) ResetSelection() {
|
||||
c.CurSelection[0] = c.buf.Start()
|
||||
c.CurSelection[1] = c.buf.Start()
|
||||
c.CurSelection[0] = c.Buf.Start()
|
||||
c.CurSelection[1] = c.Buf.Start()
|
||||
}
|
||||
|
||||
// SetSelectionStart sets the start of the selection
|
||||
@@ -144,23 +145,23 @@ func (c *Cursor) HasSelection() bool {
|
||||
// DeleteSelection deletes the currently selected text
|
||||
func (c *Cursor) DeleteSelection() {
|
||||
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
|
||||
c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
|
||||
c.Buf.Remove(c.CurSelection[1], c.CurSelection[0])
|
||||
c.Loc = c.CurSelection[1]
|
||||
} else if !c.HasSelection() {
|
||||
return
|
||||
} else {
|
||||
c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
|
||||
c.Buf.Remove(c.CurSelection[0], c.CurSelection[1])
|
||||
c.Loc = c.CurSelection[0]
|
||||
}
|
||||
}
|
||||
|
||||
// GetSelection returns the cursor's selection
|
||||
func (c *Cursor) GetSelection() []byte {
|
||||
if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
|
||||
if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
|
||||
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
|
||||
return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
|
||||
return c.Buf.Substr(c.CurSelection[1], c.CurSelection[0])
|
||||
}
|
||||
return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
|
||||
return c.Buf.Substr(c.CurSelection[0], c.CurSelection[1])
|
||||
}
|
||||
return []byte{}
|
||||
}
|
||||
@@ -170,8 +171,8 @@ func (c *Cursor) SelectLine() {
|
||||
c.Start()
|
||||
c.SetSelectionStart(c.Loc)
|
||||
c.End()
|
||||
if len(c.buf.lines)-1 > c.Y {
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
|
||||
if len(c.Buf.lines)-1 > c.Y {
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
|
||||
} else {
|
||||
c.SetSelectionEnd(c.Loc)
|
||||
}
|
||||
@@ -188,7 +189,7 @@ func (c *Cursor) AddLineToSelection() {
|
||||
}
|
||||
if c.Loc.GreaterThan(c.OrigSelection[1]) {
|
||||
c.End()
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
|
||||
c.SetSelectionStart(c.OrigSelection[0])
|
||||
}
|
||||
|
||||
@@ -202,11 +203,11 @@ func (c *Cursor) UpN(amount int) {
|
||||
proposedY := c.Y - amount
|
||||
if proposedY < 0 {
|
||||
proposedY = 0
|
||||
} else if proposedY >= len(c.buf.lines) {
|
||||
proposedY = len(c.buf.lines) - 1
|
||||
} else if proposedY >= len(c.Buf.lines) {
|
||||
proposedY = len(c.Buf.lines) - 1
|
||||
}
|
||||
|
||||
bytes := c.buf.LineBytes(proposedY)
|
||||
bytes := c.Buf.LineBytes(proposedY)
|
||||
c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
|
||||
|
||||
if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
|
||||
@@ -234,7 +235,7 @@ func (c *Cursor) Down() {
|
||||
// Left moves the cursor left one cell (if possible) or to
|
||||
// the previous line if it is at the beginning
|
||||
func (c *Cursor) Left() {
|
||||
if c.Loc == c.buf.Start() {
|
||||
if c.Loc == c.Buf.Start() {
|
||||
return
|
||||
}
|
||||
if c.X > 0 {
|
||||
@@ -249,10 +250,10 @@ func (c *Cursor) Left() {
|
||||
// Right moves the cursor right one cell (if possible) or
|
||||
// to the next line if it is at the end
|
||||
func (c *Cursor) Right() {
|
||||
if c.Loc == c.buf.End() {
|
||||
if c.Loc == c.Buf.End() {
|
||||
return
|
||||
}
|
||||
if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
|
||||
if c.X < utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
|
||||
c.X++
|
||||
} else {
|
||||
c.Down()
|
||||
@@ -267,13 +268,13 @@ func (c *Cursor) Right() {
|
||||
func (c *Cursor) Relocate() {
|
||||
if c.Y < 0 {
|
||||
c.Y = 0
|
||||
} else if c.Y >= len(c.buf.lines) {
|
||||
c.Y = len(c.buf.lines) - 1
|
||||
} else if c.Y >= len(c.Buf.lines) {
|
||||
c.Y = len(c.Buf.lines) - 1
|
||||
}
|
||||
|
||||
if c.X < 0 {
|
||||
c.X = 0
|
||||
} else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
|
||||
c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
|
||||
} else if c.X > utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
|
||||
c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -16,6 +16,8 @@ const (
|
||||
TextEventRemove = -1
|
||||
// TextEventReplace represents a replace event
|
||||
TextEventReplace = 0
|
||||
|
||||
undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them
|
||||
)
|
||||
|
||||
// TextEvent holds data for a manipulation on some text that can be undone
|
||||
@@ -186,7 +188,7 @@ func (eh *EventHandler) Execute(t *TextEvent) {
|
||||
// for pl := range loadedPlugins {
|
||||
// ret, err := Call(pl+".onBeforeTextEvent", t)
|
||||
// if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
|
||||
// TermMessage(err)
|
||||
// util.TermMessage(err)
|
||||
// }
|
||||
// if val, ok := ret.(lua.LBool); ok && val == lua.LFalse {
|
||||
// return
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import "unicode/utf8"
|
||||
import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
)
|
||||
|
||||
// Loc stores a location
|
||||
type Loc struct {
|
||||
@@ -107,7 +111,7 @@ func (l Loc) Move(n int, buf *Buffer) Loc {
|
||||
}
|
||||
return l
|
||||
}
|
||||
for i := 0; i < Abs(n); i++ {
|
||||
for i := 0; i < util.Abs(n); i++ {
|
||||
l = l.left(buf)
|
||||
}
|
||||
return l
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
// TEStack is a simple implementation of a LIFO stack for text events
|
||||
type TEStack struct {
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -10,17 +10,14 @@ import (
|
||||
)
|
||||
|
||||
// Micro's default style
|
||||
var defStyle tcell.Style = tcell.StyleDefault
|
||||
|
||||
// Colorscheme is a map from string to style -- it represents a colorscheme
|
||||
type Colorscheme map[string]tcell.Style
|
||||
var DefStyle tcell.Style = tcell.StyleDefault
|
||||
|
||||
// The current colorscheme
|
||||
var colorscheme Colorscheme
|
||||
var Colorscheme map[string]tcell.Style
|
||||
|
||||
// GetColor takes in a syntax group and returns the colorscheme's style for that group
|
||||
func GetColor(color string) tcell.Style {
|
||||
st := defStyle
|
||||
st := DefStyle
|
||||
if color == "" {
|
||||
return st
|
||||
}
|
||||
@@ -32,11 +29,11 @@ func GetColor(color string) tcell.Style {
|
||||
curGroup += "."
|
||||
}
|
||||
curGroup += g
|
||||
if style, ok := colorscheme[curGroup]; ok {
|
||||
if style, ok := Colorscheme[curGroup]; ok {
|
||||
st = style
|
||||
}
|
||||
}
|
||||
} else if style, ok := colorscheme[color]; ok {
|
||||
} else if style, ok := Colorscheme[color]; ok {
|
||||
st = style
|
||||
} else {
|
||||
st = StringToStyle(color)
|
||||
@@ -52,15 +49,15 @@ func ColorschemeExists(colorschemeName string) bool {
|
||||
|
||||
// InitColorscheme picks and initializes the colorscheme when micro starts
|
||||
func InitColorscheme() error {
|
||||
colorscheme = make(Colorscheme)
|
||||
defStyle = tcell.StyleDefault
|
||||
Colorscheme = make(map[string]tcell.Style)
|
||||
DefStyle = tcell.StyleDefault
|
||||
|
||||
return LoadDefaultColorscheme()
|
||||
}
|
||||
|
||||
// LoadDefaultColorscheme loads the default colorscheme from $(configDir)/colorschemes
|
||||
// LoadDefaultColorscheme loads the default colorscheme from $(ConfigDir)/colorschemes
|
||||
func LoadDefaultColorscheme() error {
|
||||
return LoadColorscheme(globalSettings["colorscheme"].(string))
|
||||
return LoadColorscheme(GlobalSettings["colorscheme"].(string))
|
||||
}
|
||||
|
||||
// LoadColorscheme loads the given colorscheme from a directory
|
||||
@@ -72,7 +69,7 @@ func LoadColorscheme(colorschemeName string) error {
|
||||
if data, err := file.Data(); err != nil {
|
||||
return errors.New("Error loading colorscheme: " + err.Error())
|
||||
} else {
|
||||
colorscheme, err = ParseColorscheme(string(data))
|
||||
Colorscheme, err = ParseColorscheme(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -84,13 +81,13 @@ func LoadColorscheme(colorschemeName string) error {
|
||||
// Colorschemes are made up of color-link statements linking a color group to a list of colors
|
||||
// For example, color-link keyword (blue,red) makes all keywords have a blue foreground and
|
||||
// red background
|
||||
func ParseColorscheme(text string) (Colorscheme, error) {
|
||||
func ParseColorscheme(text string) (map[string]tcell.Style, error) {
|
||||
var err error
|
||||
parser := regexp.MustCompile(`color-link\s+(\S*)\s+"(.*)"`)
|
||||
|
||||
lines := strings.Split(text, "\n")
|
||||
|
||||
c := make(Colorscheme)
|
||||
c := make(map[string]tcell.Style)
|
||||
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line) == "" ||
|
||||
@@ -108,7 +105,7 @@ func ParseColorscheme(text string) (Colorscheme, error) {
|
||||
c[link] = style
|
||||
|
||||
if link == "default" {
|
||||
defStyle = style
|
||||
DefStyle = style
|
||||
}
|
||||
} else {
|
||||
err = errors.New("Color-link statement is not valid: " + line)
|
||||
@@ -140,17 +137,17 @@ func StringToStyle(str string) tcell.Style {
|
||||
|
||||
var fgColor, bgColor tcell.Color
|
||||
if fg == "" {
|
||||
fgColor, _, _ = defStyle.Decompose()
|
||||
fgColor, _, _ = DefStyle.Decompose()
|
||||
} else {
|
||||
fgColor = StringToColor(fg)
|
||||
}
|
||||
if bg == "" {
|
||||
_, bgColor, _ = defStyle.Decompose()
|
||||
_, bgColor, _ = DefStyle.Decompose()
|
||||
} else {
|
||||
bgColor = StringToColor(bg)
|
||||
}
|
||||
|
||||
style := defStyle.Foreground(fgColor).Background(bgColor)
|
||||
style := DefStyle.Foreground(fgColor).Background(bgColor)
|
||||
if strings.Contains(str, "bold") {
|
||||
style = style.Bold(true)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
54
cmd/micro/config/config.go
Normal file
54
cmd/micro/config/config.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
var ConfigDir string
|
||||
|
||||
// InitConfigDir finds the configuration directory for micro according to the XDG spec.
|
||||
// If no directory is found, it creates one.
|
||||
func InitConfigDir(flagConfigDir string) error {
|
||||
var e error
|
||||
|
||||
xdgHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgHome == "" {
|
||||
// The user has not set $XDG_CONFIG_HOME so we should act like it was set to ~/.config
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
return errors.New("Error finding your home directory\nCan't load config files")
|
||||
}
|
||||
xdgHome = home + "/.config"
|
||||
}
|
||||
ConfigDir = xdgHome + "/micro"
|
||||
|
||||
if len(flagConfigDir) > 0 {
|
||||
if _, err := os.Stat(flagConfigDir); os.IsNotExist(err) {
|
||||
e = errors.New("Error: " + flagConfigDir + " does not exist. Defaulting to " + ConfigDir + ".")
|
||||
} else {
|
||||
ConfigDir = flagConfigDir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
|
||||
// If the xdgHome doesn't exist we should create it
|
||||
err = os.Mkdir(xdgHome, os.ModePerm)
|
||||
if err != nil {
|
||||
return errors.New("Error creating XDG_CONFIG_HOME directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(ConfigDir); os.IsNotExist(err) {
|
||||
// If the micro specific config directory doesn't exist we should create that too
|
||||
err = os.Mkdir(ConfigDir, os.ModePerm)
|
||||
if err != nil {
|
||||
return errors.New("Error creating configuration directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@@ -125,7 +125,7 @@ func ListRuntimeFiles(fileType RTFiletype) []RuntimeFile {
|
||||
// InitRuntimeFiles initializes all assets file and the config directory
|
||||
func InitRuntimeFiles() {
|
||||
add := func(fileType RTFiletype, dir, pattern string) {
|
||||
AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern)
|
||||
AddRuntimeFilesFromDirectory(fileType, filepath.Join(ConfigDir, dir), pattern)
|
||||
AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
|
||||
}
|
||||
|
||||
@@ -133,13 +133,13 @@ func InitRuntimeFiles() {
|
||||
add(RTSyntax, "syntax", "*.yaml")
|
||||
add(RTHelp, "help", "*.md")
|
||||
|
||||
// Search configDir for plugin-scripts
|
||||
files, _ := ioutil.ReadDir(filepath.Join(configDir, "plugins"))
|
||||
// Search ConfigDir for plugin-scripts
|
||||
files, _ := ioutil.ReadDir(filepath.Join(ConfigDir, "plugins"))
|
||||
for _, f := range files {
|
||||
realpath, _ := filepath.EvalSymlinks(filepath.Join(configDir, "plugins", f.Name()))
|
||||
realpath, _ := filepath.EvalSymlinks(filepath.Join(ConfigDir, "plugins", f.Name()))
|
||||
realpathStat, _ := os.Stat(realpath)
|
||||
if realpathStat.IsDir() {
|
||||
scriptPath := filepath.Join(configDir, "plugins", f.Name(), f.Name()+".lua")
|
||||
scriptPath := filepath.Join(ConfigDir, "plugins", f.Name(), f.Name()+".lua")
|
||||
if _, err := os.Stat(scriptPath); err == nil {
|
||||
AddRuntimeFile(RTPlugin, realFile(scriptPath))
|
||||
}
|
||||
@@ -178,7 +178,7 @@ func PluginListRuntimeFiles(fileType RTFiletype) []string {
|
||||
|
||||
// PluginAddRuntimeFile adds a file to the runtime files for a plugin
|
||||
func PluginAddRuntimeFile(plugin string, filetype RTFiletype, filePath string) {
|
||||
fullpath := filepath.Join(configDir, "plugins", plugin, filePath)
|
||||
fullpath := filepath.Join(ConfigDir, "plugins", plugin, filePath)
|
||||
if _, err := os.Stat(fullpath); err == nil {
|
||||
AddRuntimeFile(filetype, realFile(fullpath))
|
||||
} else {
|
||||
@@ -189,7 +189,7 @@ func PluginAddRuntimeFile(plugin string, filetype RTFiletype, filePath string) {
|
||||
|
||||
// PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
|
||||
func PluginAddRuntimeFilesFromDirectory(plugin string, filetype RTFiletype, directory, pattern string) {
|
||||
fullpath := filepath.Join(configDir, "plugins", plugin, directory)
|
||||
fullpath := filepath.Join(ConfigDir, "plugins", plugin, directory)
|
||||
if _, err := os.Stat(fullpath); err == nil {
|
||||
AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
|
||||
} else {
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
4027
cmd/micro/config/runtime.go
Normal file
4027
cmd/micro/config/runtime.go
Normal file
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
type optionValidator func(string, interface{}) error
|
||||
|
||||
// The options that the user can set
|
||||
var globalSettings map[string]interface{}
|
||||
var GlobalSettings map[string]interface{}
|
||||
|
||||
// This is the raw parsed json
|
||||
var parsedSettings map[string]interface{}
|
||||
@@ -31,7 +31,7 @@ var optionValidators = map[string]optionValidator{
|
||||
}
|
||||
|
||||
func ReadSettings() error {
|
||||
filename := configDir + "/settings.json"
|
||||
filename := ConfigDir + "/settings.json"
|
||||
if _, e := os.Stat(filename); e == nil {
|
||||
input, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
@@ -51,26 +51,26 @@ func ReadSettings() error {
|
||||
// InitGlobalSettings initializes the options map and sets all options to their default values
|
||||
// Must be called after ReadSettings
|
||||
func InitGlobalSettings() {
|
||||
globalSettings = DefaultGlobalSettings()
|
||||
GlobalSettings = DefaultGlobalSettings()
|
||||
|
||||
for k, v := range parsedSettings {
|
||||
if !strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
|
||||
globalSettings[k] = v
|
||||
GlobalSettings[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InitLocalSettings scans the json in settings.json and sets the options locally based
|
||||
// on whether the buffer matches the glob
|
||||
// on whether the filetype or path matches ft or glob local settings
|
||||
// Must be called after ReadSettings
|
||||
func InitLocalSettings(buf *Buffer) error {
|
||||
func InitLocalSettings(settings map[string]interface{}, path string) error {
|
||||
var parseError error
|
||||
for k, v := range parsedSettings {
|
||||
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
|
||||
if strings.HasPrefix(k, "ft:") {
|
||||
if buf.Settings["filetype"].(string) == k[3:] {
|
||||
if settings["filetype"].(string) == k[3:] {
|
||||
for k1, v1 := range v.(map[string]interface{}) {
|
||||
buf.Settings[k1] = v1
|
||||
settings[k1] = v1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -80,9 +80,9 @@ func InitLocalSettings(buf *Buffer) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.MatchString(buf.Path) {
|
||||
if g.MatchString(path) {
|
||||
for k1, v1 := range v.(map[string]interface{}) {
|
||||
buf.Settings[k1] = v1
|
||||
settings[k1] = v1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,8 +94,8 @@ func InitLocalSettings(buf *Buffer) error {
|
||||
// WriteSettings writes the settings to the specified filename as JSON
|
||||
func WriteSettings(filename string) error {
|
||||
var err error
|
||||
if _, e := os.Stat(configDir); e == nil {
|
||||
for k, v := range globalSettings {
|
||||
if _, e := os.Stat(ConfigDir); e == nil {
|
||||
for k, v := range GlobalSettings {
|
||||
parsedSettings[k] = v
|
||||
}
|
||||
|
||||
@@ -107,8 +107,8 @@ func WriteSettings(filename string) error {
|
||||
|
||||
// AddOption creates a new option. This is meant to be called by plugins to add options.
|
||||
func AddOption(name string, value interface{}) error {
|
||||
globalSettings[name] = value
|
||||
err := WriteSettings(configDir + "/settings.json")
|
||||
GlobalSettings[name] = value
|
||||
err := WriteSettings(ConfigDir + "/settings.json")
|
||||
if err != nil {
|
||||
return errors.New("Error writing settings.json file: " + err.Error())
|
||||
}
|
||||
@@ -117,13 +117,13 @@ func AddOption(name string, value interface{}) error {
|
||||
|
||||
// GetGlobalOption returns the global value of the given option
|
||||
func GetGlobalOption(name string) interface{} {
|
||||
return globalSettings[name]
|
||||
return GlobalSettings[name]
|
||||
}
|
||||
|
||||
// GetLocalOption returns the local value of the given option
|
||||
func GetLocalOption(name string, buf *Buffer) interface{} {
|
||||
return buf.Settings[name]
|
||||
}
|
||||
// func GetLocalOption(name string, buf *Buffer) interface{} {
|
||||
// return buf.Settings[name]
|
||||
// }
|
||||
|
||||
// TODO: get option for current buffer
|
||||
// GetOption returns the value of the given option
|
||||
@@ -203,7 +203,7 @@ func DefaultLocalSettings() map[string]interface{} {
|
||||
// is local only it will set the local version
|
||||
// Use setlocal to force an option to be set locally
|
||||
// func SetOption(option, value string) error {
|
||||
// if _, ok := globalSettings[option]; !ok {
|
||||
// if _, ok := GlobalSettings[option]; !ok {
|
||||
// if _, ok := CurView().Buf.Settings[option]; !ok {
|
||||
// return errors.New("Invalid option")
|
||||
// }
|
||||
@@ -213,7 +213,7 @@ func DefaultLocalSettings() map[string]interface{} {
|
||||
//
|
||||
// var nativeValue interface{}
|
||||
//
|
||||
// kind := reflect.TypeOf(globalSettings[option]).Kind()
|
||||
// kind := reflect.TypeOf(GlobalSettings[option]).Kind()
|
||||
// if kind == reflect.Bool {
|
||||
// b, err := ParseBool(value)
|
||||
// if err != nil {
|
||||
@@ -236,7 +236,7 @@ func DefaultLocalSettings() map[string]interface{} {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// globalSettings[option] = nativeValue
|
||||
// GlobalSettings[option] = nativeValue
|
||||
//
|
||||
// if option == "colorscheme" {
|
||||
// // LoadSyntaxFiles()
|
||||
@@ -372,7 +372,7 @@ func DefaultLocalSettings() map[string]interface{} {
|
||||
//
|
||||
// // SetOptionAndSettings sets the given option and saves the option setting to the settings config file
|
||||
// func SetOptionAndSettings(option, value string) {
|
||||
// filename := configDir + "/settings.json"
|
||||
// filename := ConfigDir + "/settings.json"
|
||||
//
|
||||
// err := SetOption(option, value)
|
||||
//
|
||||
@@ -1 +0,0 @@
|
||||
package main
|
||||
@@ -5,12 +5,15 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// NullWriter simply sends writes into the void
|
||||
type NullWriter struct{}
|
||||
|
||||
// Write is empty
|
||||
func (NullWriter) Write(data []byte) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// InitLog sets up the debug log system for micro if it has been enabled by compile-time variables
|
||||
func InitLog() {
|
||||
if Debug == "ON" {
|
||||
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
@@ -22,6 +25,5 @@ func InitLog() {
|
||||
log.Println("Micro started")
|
||||
} else {
|
||||
log.SetOutput(NullWriter{})
|
||||
log.Println("Micro started")
|
||||
}
|
||||
}
|
||||
|
||||
82
cmd/micro/infobar.go
Normal file
82
cmd/micro/infobar.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
runewidth "github.com/mattn/go-runewidth"
|
||||
"github.com/zyedidia/micro/cmd/micro/buffer"
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
type InfoBar struct {
|
||||
*buffer.Buffer
|
||||
|
||||
hasPrompt bool
|
||||
hasMessage bool
|
||||
|
||||
message string
|
||||
// style to use when drawing the message
|
||||
style tcell.Style
|
||||
|
||||
width int
|
||||
y int
|
||||
|
||||
// This map stores the history for all the different kinds of uses Prompt has
|
||||
// It's a map of history type -> history array
|
||||
history map[string][]string
|
||||
historyNum int
|
||||
|
||||
// Is the current message a message from the gutter
|
||||
gutterMessage bool
|
||||
}
|
||||
|
||||
func NewInfoBar() *InfoBar {
|
||||
ib := new(InfoBar)
|
||||
ib.style = config.DefStyle
|
||||
ib.history = make(map[string][]string)
|
||||
|
||||
ib.Buffer = buffer.NewBufferFromString("", "infobar")
|
||||
ib.Type = buffer.BTScratch
|
||||
|
||||
ib.width, ib.y = screen.Screen.Size()
|
||||
|
||||
return ib
|
||||
}
|
||||
|
||||
func (i *InfoBar) Clear() {
|
||||
for x := 0; x < i.width; x++ {
|
||||
screen.Screen.SetContent(x, i.y, ' ', nil, config.DefStyle)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *InfoBar) Display() {
|
||||
x := 0
|
||||
if i.hasPrompt || config.GlobalSettings["infobar"].(bool) {
|
||||
display := i.message + strings.TrimSpace(string(i.Bytes()))
|
||||
for _, c := range display {
|
||||
screen.Screen.SetContent(x, i.y, c, nil, i.style)
|
||||
x += runewidth.RuneWidth(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Message sends a message to the user
|
||||
func (i *InfoBar) Message(msg ...interface{}) {
|
||||
displayMessage := fmt.Sprint(msg...)
|
||||
// only display a new message if there isn't an active prompt
|
||||
// this is to prevent overwriting an existing prompt to the user
|
||||
if i.hasPrompt == false {
|
||||
// if there is no active prompt then style and display the message as normal
|
||||
i.message = displayMessage
|
||||
i.style = config.DefStyle
|
||||
|
||||
if _, ok := config.Colorscheme["message"]; ok {
|
||||
i.style = config.Colorscheme["message"]
|
||||
}
|
||||
|
||||
i.hasMessage = true
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package lua
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -4,29 +4,21 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"github.com/zyedidia/micro/cmd/micro/terminfo"
|
||||
"github.com/zyedidia/micro/cmd/micro/buffer"
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
const (
|
||||
doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
|
||||
undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them
|
||||
autosaveTime = 8 // Number of seconds to wait before autosaving
|
||||
)
|
||||
|
||||
var (
|
||||
// The main screen
|
||||
screen tcell.Screen
|
||||
|
||||
// Where the user's configuration is
|
||||
// This should be $XDG_CONFIG_HOME/micro
|
||||
// If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
|
||||
configDir string
|
||||
|
||||
// Version is the version number or commit hash
|
||||
// These variables should be set by the linker when compiling
|
||||
Version = "0.0.0-unknown"
|
||||
@@ -51,104 +43,6 @@ var (
|
||||
flagOptions = flag.Bool("options", false, "Show all option help")
|
||||
)
|
||||
|
||||
// InitConfigDir finds the configuration directory for micro according to the XDG spec.
|
||||
// If no directory is found, it creates one.
|
||||
func InitConfigDir() {
|
||||
xdgHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgHome == "" {
|
||||
// The user has not set $XDG_CONFIG_HOME so we should act like it was set to ~/.config
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
TermMessage("Error finding your home directory\nCan't load config files")
|
||||
return
|
||||
}
|
||||
xdgHome = home + "/.config"
|
||||
}
|
||||
configDir = xdgHome + "/micro"
|
||||
|
||||
if len(*flagConfigDir) > 0 {
|
||||
if _, err := os.Stat(*flagConfigDir); os.IsNotExist(err) {
|
||||
TermMessage("Error: " + *flagConfigDir + " does not exist. Defaulting to " + configDir + ".")
|
||||
} else {
|
||||
configDir = *flagConfigDir
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
|
||||
// If the xdgHome doesn't exist we should create it
|
||||
err = os.Mkdir(xdgHome, os.ModePerm)
|
||||
if err != nil {
|
||||
TermMessage("Error creating XDG_CONFIG_HOME directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
||||
// If the micro specific config directory doesn't exist we should create that too
|
||||
err = os.Mkdir(configDir, os.ModePerm)
|
||||
if err != nil {
|
||||
TermMessage("Error creating configuration directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InitScreen creates and initializes the tcell screen
|
||||
func InitScreen() {
|
||||
// Should we enable true color?
|
||||
truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
|
||||
|
||||
tcelldb := os.Getenv("TCELLDB")
|
||||
os.Setenv("TCELLDB", configDir+"/.tcelldb")
|
||||
|
||||
// In order to enable true color, we have to set the TERM to `xterm-truecolor` when
|
||||
// initializing tcell, but after that, we can set the TERM back to whatever it was
|
||||
oldTerm := os.Getenv("TERM")
|
||||
if truecolor {
|
||||
os.Setenv("TERM", "xterm-truecolor")
|
||||
}
|
||||
|
||||
// Initilize tcell
|
||||
var err error
|
||||
screen, err = tcell.NewScreen()
|
||||
if err != nil {
|
||||
if err == tcell.ErrTermNotFound {
|
||||
err = terminfo.WriteDB(configDir + "/.tcelldb")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not create tcelldb")
|
||||
os.Exit(1)
|
||||
}
|
||||
screen, err = tcell.NewScreen()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if err = screen.Init(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Now we can put the TERM back to what it was before
|
||||
if truecolor {
|
||||
os.Setenv("TERM", oldTerm)
|
||||
}
|
||||
|
||||
if GetGlobalOption("mouse").(bool) {
|
||||
screen.EnableMouse()
|
||||
}
|
||||
|
||||
os.Setenv("TCELLDB", tcelldb)
|
||||
|
||||
// screen.SetStyle(defStyle)
|
||||
}
|
||||
|
||||
func InitFlags() {
|
||||
flag.Usage = func() {
|
||||
fmt.Println("Usage: micro [OPTIONS] [FILE]...")
|
||||
@@ -172,7 +66,7 @@ func InitFlags() {
|
||||
|
||||
optionFlags := make(map[string]*string)
|
||||
|
||||
for k, v := range DefaultGlobalSettings() {
|
||||
for k, v := range config.DefaultGlobalSettings() {
|
||||
optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
|
||||
}
|
||||
|
||||
@@ -188,7 +82,7 @@ func InitFlags() {
|
||||
|
||||
if *flagOptions {
|
||||
// If -options was passed
|
||||
for k, v := range DefaultGlobalSettings() {
|
||||
for k, v := range config.DefaultGlobalSettings() {
|
||||
fmt.Printf("-%s value\n", k)
|
||||
fmt.Printf(" \tDefault value: '%v'\n", v)
|
||||
}
|
||||
@@ -201,26 +95,30 @@ func main() {
|
||||
|
||||
InitLog()
|
||||
InitFlags()
|
||||
InitConfigDir()
|
||||
InitRuntimeFiles()
|
||||
err = ReadSettings()
|
||||
err = config.InitConfigDir(*flagConfigDir)
|
||||
if err != nil {
|
||||
TermMessage(err)
|
||||
util.TermMessage(err)
|
||||
}
|
||||
InitGlobalSettings()
|
||||
err = InitColorscheme()
|
||||
config.InitRuntimeFiles()
|
||||
err = config.ReadSettings()
|
||||
if err != nil {
|
||||
TermMessage(err)
|
||||
util.TermMessage(err)
|
||||
}
|
||||
config.InitGlobalSettings()
|
||||
InitBindings()
|
||||
err = config.InitColorscheme()
|
||||
if err != nil {
|
||||
util.TermMessage(err)
|
||||
}
|
||||
|
||||
InitScreen()
|
||||
screen.Init()
|
||||
|
||||
// If we have an error, we can exit cleanly and not completely
|
||||
// mess up the terminal being worked in
|
||||
// In other words we need to shut down tcell before the program crashes
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
screen.Fini()
|
||||
screen.Screen.Fini()
|
||||
fmt.Println("Micro encountered an error:", err)
|
||||
// Print the stack trace too
|
||||
fmt.Print(errors.Wrap(err, 2).ErrorStack())
|
||||
@@ -228,26 +126,46 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
b, err := NewBufferFromFile(os.Args[1])
|
||||
TryBindKey("Ctrl-z", "Undo", true)
|
||||
|
||||
b, err := buffer.NewBufferFromFile(os.Args[1])
|
||||
|
||||
if err != nil {
|
||||
TermMessage(err)
|
||||
util.TermMessage(err)
|
||||
}
|
||||
|
||||
width, height := screen.Size()
|
||||
width, height := screen.Screen.Size()
|
||||
w := NewWindow(0, 0, width, height-1, b)
|
||||
|
||||
w := NewWindow(0, 0, width/2, height/2, b)
|
||||
a := NewBufActionHandler(b, w)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
screen.Clear()
|
||||
// Here is the event loop which runs in a separate thread
|
||||
go func() {
|
||||
events = make(chan tcell.Event)
|
||||
for {
|
||||
// TODO: fix race condition with screen.Screen = nil
|
||||
events <- screen.Screen.PollEvent()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
// Display everything
|
||||
w.Clear()
|
||||
w.DisplayBuffer()
|
||||
w.DisplayStatusLine()
|
||||
screen.Show()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
w.StartLine++
|
||||
screen.Screen.Show()
|
||||
|
||||
var event tcell.Event
|
||||
|
||||
// Check for new events
|
||||
select {
|
||||
case event = <-events:
|
||||
}
|
||||
|
||||
if event != nil {
|
||||
a.HandleEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
// time.Sleep(2 * time.Second)
|
||||
|
||||
screen.Fini()
|
||||
screen.Screen.Fini()
|
||||
}
|
||||
|
||||
4119
cmd/micro/runtime.go
4119
cmd/micro/runtime.go
File diff suppressed because one or more lines are too long
67
cmd/micro/screen/screen.go
Normal file
67
cmd/micro/screen/screen.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package screen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/terminfo"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
var Screen tcell.Screen
|
||||
|
||||
// Init creates and initializes the tcell screen
|
||||
func Init() {
|
||||
// Should we enable true color?
|
||||
truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
|
||||
|
||||
tcelldb := os.Getenv("TCELLDB")
|
||||
os.Setenv("TCELLDB", config.ConfigDir+"/.tcelldb")
|
||||
|
||||
// In order to enable true color, we have to set the TERM to `xterm-truecolor` when
|
||||
// initializing tcell, but after that, we can set the TERM back to whatever it was
|
||||
oldTerm := os.Getenv("TERM")
|
||||
if truecolor {
|
||||
os.Setenv("TERM", "xterm-truecolor")
|
||||
}
|
||||
|
||||
// Initilize tcell
|
||||
var err error
|
||||
Screen, err = tcell.NewScreen()
|
||||
if err != nil {
|
||||
if err == tcell.ErrTermNotFound {
|
||||
err = terminfo.WriteDB(config.ConfigDir + "/.tcelldb")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not create terminal database file", config.ConfigDir+"/.tcelldb")
|
||||
os.Exit(1)
|
||||
}
|
||||
Screen, err = tcell.NewScreen()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if err = Screen.Init(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Now we can put the TERM back to what it was before
|
||||
if truecolor {
|
||||
os.Setenv("TERM", oldTerm)
|
||||
}
|
||||
|
||||
if config.GetGlobalOption("mouse").(bool) {
|
||||
Screen.EnableMouse()
|
||||
}
|
||||
|
||||
os.Setenv("TCELLDB", tcelldb)
|
||||
}
|
||||
@@ -7,6 +7,10 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/buffer"
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
)
|
||||
|
||||
// StatusLine represents the information line at the bottom
|
||||
@@ -16,7 +20,7 @@ import (
|
||||
type StatusLine struct {
|
||||
FormatLeft string
|
||||
FormatRight string
|
||||
Info map[string]func(*Buffer) string
|
||||
Info map[string]func(*buffer.Buffer) string
|
||||
|
||||
win *Window
|
||||
}
|
||||
@@ -29,20 +33,20 @@ func NewStatusLine(win *Window) *StatusLine {
|
||||
// s.FormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat)"
|
||||
s.FormatLeft = "$(filename) $(modified)(line,col) $(opt:filetype) $(opt:fileformat)"
|
||||
s.FormatRight = "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): open help"
|
||||
s.Info = map[string]func(*Buffer) string{
|
||||
"filename": func(b *Buffer) string {
|
||||
s.Info = map[string]func(*buffer.Buffer) string{
|
||||
"filename": func(b *buffer.Buffer) string {
|
||||
if b.Settings["basename"].(bool) {
|
||||
return path.Base(b.GetName())
|
||||
}
|
||||
return b.GetName()
|
||||
},
|
||||
"line": func(b *Buffer) string {
|
||||
"line": func(b *buffer.Buffer) string {
|
||||
return strconv.Itoa(b.GetActiveCursor().Y)
|
||||
},
|
||||
"col": func(b *Buffer) string {
|
||||
"col": func(b *buffer.Buffer) string {
|
||||
return strconv.Itoa(b.GetActiveCursor().X)
|
||||
},
|
||||
"modified": func(b *Buffer) string {
|
||||
"modified": func(b *buffer.Buffer) string {
|
||||
if b.Modified() {
|
||||
return "+ "
|
||||
}
|
||||
@@ -71,7 +75,7 @@ func (s *StatusLine) Display() {
|
||||
// }
|
||||
|
||||
// We'll draw the line at the lowest line in the window
|
||||
y := s.win.Height + s.win.Y
|
||||
y := s.win.Height + s.win.Y - 1
|
||||
|
||||
formatter := func(match []byte) []byte {
|
||||
name := match[2 : len(match)-1]
|
||||
@@ -79,8 +83,13 @@ func (s *StatusLine) Display() {
|
||||
option := name[4:]
|
||||
return []byte(fmt.Sprint(s.FindOpt(string(option))))
|
||||
} else if bytes.HasPrefix(name, []byte("bind")) {
|
||||
// TODO: status line bindings
|
||||
return []byte("TODO")
|
||||
binding := string(name[5:])
|
||||
for k, v := range bindings {
|
||||
if v == binding {
|
||||
return []byte(k)
|
||||
}
|
||||
}
|
||||
return []byte("null")
|
||||
} else {
|
||||
return []byte(s.Info[string(name)](s.win.Buf))
|
||||
}
|
||||
@@ -91,8 +100,8 @@ func (s *StatusLine) Display() {
|
||||
rightText := []byte(s.FormatRight)
|
||||
rightText = formatParser.ReplaceAllFunc([]byte(s.FormatRight), formatter)
|
||||
|
||||
statusLineStyle := defStyle.Reverse(true)
|
||||
if style, ok := colorscheme["statusline"]; ok {
|
||||
statusLineStyle := config.DefStyle.Reverse(true)
|
||||
if style, ok := config.Colorscheme["statusline"]; ok {
|
||||
statusLineStyle = style
|
||||
}
|
||||
|
||||
@@ -104,13 +113,13 @@ func (s *StatusLine) Display() {
|
||||
if x < leftLen {
|
||||
r, size := utf8.DecodeRune(leftText)
|
||||
leftText = leftText[size:]
|
||||
screen.SetContent(winX+x, y, r, nil, statusLineStyle)
|
||||
screen.Screen.SetContent(winX+x, y, r, nil, statusLineStyle)
|
||||
} else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen {
|
||||
r, size := utf8.DecodeRune(rightText)
|
||||
rightText = rightText[size:]
|
||||
screen.SetContent(winX+x, y, r, nil, statusLineStyle)
|
||||
screen.Screen.SetContent(winX+x, y, r, nil, statusLineStyle)
|
||||
} else {
|
||||
screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
|
||||
screen.Screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
package main
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
)
|
||||
|
||||
// TermMessage sends a message to the user in the terminal. This usually occurs before
|
||||
// micro has been fully initialized -- ie if there is an error in the syntax highlighting
|
||||
// regular expressions
|
||||
// The function must be called when the screen is not initialized
|
||||
// The function must be called when the Screen is not initialized
|
||||
// This will write the message, and wait for the user
|
||||
// to press and key to continue
|
||||
func TermMessage(msg ...interface{}) {
|
||||
screenWasNil := screen == nil
|
||||
screenWasNil := screen.Screen == nil
|
||||
if !screenWasNil {
|
||||
screen.Fini()
|
||||
screen = nil
|
||||
screen.Screen.Fini()
|
||||
screen.Screen = nil
|
||||
}
|
||||
|
||||
fmt.Println(msg...)
|
||||
@@ -27,7 +29,7 @@ func TermMessage(msg ...interface{}) {
|
||||
reader.ReadString('\n')
|
||||
|
||||
if !screenWasNil {
|
||||
InitScreen()
|
||||
screen.Init()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
@@ -12,3 +14,15 @@ func GetMemStats() string {
|
||||
runtime.ReadMemStats(&memstats)
|
||||
return fmt.Sprintf("Alloc: %s, Sys: %s, GC: %d, PauseTotalNs: %dns", humanize.Bytes(memstats.Alloc), humanize.Bytes(memstats.Sys), memstats.NumGC, memstats.PauseTotalNs)
|
||||
}
|
||||
|
||||
var start time.Time
|
||||
|
||||
func tic(s string) {
|
||||
log.Println("START:", s)
|
||||
start = time.Now()
|
||||
}
|
||||
|
||||
func toc() {
|
||||
end := time.Now()
|
||||
log.Println("END: ElapsedTime in seconds:", end.Sub(start))
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package main
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -213,3 +214,9 @@ func GetModTime(path string) (time.Time, error) {
|
||||
}
|
||||
return info.ModTime(), nil
|
||||
}
|
||||
|
||||
// EscapePath replaces every path separator in a given path with a %
|
||||
func EscapePath(path string) string {
|
||||
path = filepath.ToSlash(path)
|
||||
return strings.Replace(path, "/", "%", -1)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -5,6 +5,10 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
runewidth "github.com/mattn/go-runewidth"
|
||||
"github.com/zyedidia/micro/cmd/micro/buffer"
|
||||
"github.com/zyedidia/micro/cmd/micro/config"
|
||||
"github.com/zyedidia/micro/cmd/micro/screen"
|
||||
"github.com/zyedidia/micro/cmd/micro/util"
|
||||
"github.com/zyedidia/tcell"
|
||||
)
|
||||
|
||||
@@ -23,12 +27,12 @@ type Window struct {
|
||||
StartCol int
|
||||
|
||||
// Buffer being shown in this window
|
||||
Buf *Buffer
|
||||
Buf *buffer.Buffer
|
||||
|
||||
sline *StatusLine
|
||||
}
|
||||
|
||||
func NewWindow(x, y, width, height int, buf *Buffer) *Window {
|
||||
func NewWindow(x, y, width, height int, buf *buffer.Buffer) *Window {
|
||||
w := new(Window)
|
||||
w.X, w.Y, w.Width, w.Height, w.Buf = x, y, width, height, buf
|
||||
|
||||
@@ -37,40 +41,48 @@ func NewWindow(x, y, width, height int, buf *Buffer) *Window {
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Window) DrawLineNum(lineNumStyle tcell.Style, softwrapped bool, maxLineNumLength int, vloc *Loc, bloc *Loc) {
|
||||
func (w *Window) Clear() {
|
||||
for y := 0; y < w.Height; y++ {
|
||||
for x := 0; x < w.Width; x++ {
|
||||
screen.Screen.SetContent(w.X+x, w.Y+y, ' ', nil, config.DefStyle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Window) DrawLineNum(lineNumStyle tcell.Style, softwrapped bool, maxLineNumLength int, vloc *buffer.Loc, bloc *buffer.Loc) {
|
||||
lineNum := strconv.Itoa(bloc.Y + 1)
|
||||
|
||||
// Write the spaces before the line number if necessary
|
||||
for i := 0; i < maxLineNumLength-len(lineNum); i++ {
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
vloc.X++
|
||||
}
|
||||
// Write the actual line number
|
||||
for _, ch := range lineNum {
|
||||
if softwrapped {
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
} else {
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ch, nil, lineNumStyle)
|
||||
}
|
||||
vloc.X++
|
||||
}
|
||||
|
||||
// Write the extra space
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, ' ', nil, lineNumStyle)
|
||||
vloc.X++
|
||||
}
|
||||
|
||||
// GetStyle returns the highlight style for the given character position
|
||||
// If there is no change to the current highlight style it just returns that
|
||||
func (w *Window) GetStyle(style tcell.Style, bloc Loc, r rune) tcell.Style {
|
||||
func (w *Window) GetStyle(style tcell.Style, bloc buffer.Loc, r rune) tcell.Style {
|
||||
if group, ok := w.Buf.Match(bloc.Y)[bloc.X]; ok {
|
||||
s := GetColor(group.String())
|
||||
s := config.GetColor(group.String())
|
||||
return s
|
||||
}
|
||||
return style
|
||||
}
|
||||
|
||||
// DisplayBuffer draws the buffer being shown in this window on the screen
|
||||
// DisplayBuffer draws the buffer being shown in this window on the screen.Screen
|
||||
func (w *Window) DisplayBuffer() {
|
||||
b := w.Buf
|
||||
|
||||
@@ -81,7 +93,7 @@ func (w *Window) DisplayBuffer() {
|
||||
|
||||
// TODO: Rehighlighting
|
||||
// start := w.StartLine
|
||||
if b.Settings["syntax"].(bool) && b.syntaxDef != nil {
|
||||
if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
|
||||
// if start > 0 && b.lines[start-1].rehighlight {
|
||||
// b.highlighter.ReHighlightLine(b, start-1)
|
||||
// b.lines[start-1].rehighlight = false
|
||||
@@ -89,29 +101,29 @@ func (w *Window) DisplayBuffer() {
|
||||
//
|
||||
// b.highlighter.ReHighlightStates(b, start)
|
||||
//
|
||||
b.highlighter.HighlightMatches(b, w.StartLine, w.StartLine+bufHeight)
|
||||
b.Highlighter.HighlightMatches(b, w.StartLine, w.StartLine+bufHeight)
|
||||
}
|
||||
|
||||
lineNumStyle := defStyle
|
||||
if style, ok := colorscheme["line-number"]; ok {
|
||||
lineNumStyle := config.DefStyle
|
||||
if style, ok := config.Colorscheme["line-number"]; ok {
|
||||
lineNumStyle = style
|
||||
}
|
||||
|
||||
// We need to know the string length of the largest line number
|
||||
// so we can pad appropriately when displaying line numbers
|
||||
maxLineNumLength := len(strconv.Itoa(len(b.lines)))
|
||||
maxLineNumLength := len(strconv.Itoa(b.LinesNum()))
|
||||
|
||||
tabsize := int(b.Settings["tabsize"].(float64))
|
||||
softwrap := b.Settings["softwrap"].(bool)
|
||||
|
||||
// this represents the current draw position
|
||||
// within the current window
|
||||
vloc := Loc{0, 0}
|
||||
vloc := buffer.Loc{0, 0}
|
||||
|
||||
// this represents the current draw position in the buffer (char positions)
|
||||
bloc := Loc{w.StartCol, w.StartLine}
|
||||
bloc := buffer.Loc{w.StartCol, w.StartLine}
|
||||
|
||||
curStyle := defStyle
|
||||
curStyle := config.DefStyle
|
||||
for vloc.Y = 0; vloc.Y < bufHeight; vloc.Y++ {
|
||||
vloc.X = 0
|
||||
if b.Settings["ruler"].(bool) {
|
||||
@@ -119,7 +131,7 @@ func (w *Window) DisplayBuffer() {
|
||||
}
|
||||
|
||||
line := b.LineBytes(bloc.Y)
|
||||
line, nColsBeforeStart := SliceVisualEnd(line, bloc.X, tabsize)
|
||||
line, nColsBeforeStart := util.SliceVisualEnd(line, bloc.X, tabsize)
|
||||
totalwidth := bloc.X - nColsBeforeStart
|
||||
for len(line) > 0 {
|
||||
r, size := utf8.DecodeRune(line)
|
||||
@@ -127,7 +139,7 @@ func (w *Window) DisplayBuffer() {
|
||||
curStyle = w.GetStyle(curStyle, bloc, r)
|
||||
|
||||
if nColsBeforeStart <= 0 {
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, nil, curStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, nil, curStyle)
|
||||
vloc.X++
|
||||
}
|
||||
nColsBeforeStart--
|
||||
@@ -151,7 +163,7 @@ func (w *Window) DisplayBuffer() {
|
||||
if width > 1 {
|
||||
for i := 1; i < width; i++ {
|
||||
if nColsBeforeStart <= 0 {
|
||||
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, curStyle)
|
||||
screen.Screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, char, nil, curStyle)
|
||||
vloc.X++
|
||||
}
|
||||
nColsBeforeStart--
|
||||
@@ -176,7 +188,7 @@ func (w *Window) DisplayBuffer() {
|
||||
}
|
||||
bloc.X = w.StartCol
|
||||
bloc.Y++
|
||||
if bloc.Y >= len(b.lines) {
|
||||
if bloc.Y >= b.LinesNum() {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user