simple editor commands are implemented in the preferences file

This commit is contained in:
Felix Angell 2018-02-28 00:54:59 +00:00
parent 3d49b5ecee
commit 41851db6b7
6 changed files with 84 additions and 13 deletions

View File

@ -1,12 +1,14 @@
package cfg
import "strconv"
import "log"
type TomlConfig struct {
Editor EditorConfig
Cursor CursorConfig
Render RenderConfig
Theme ThemeConfig
Editor EditorConfig
Cursor CursorConfig
Render RenderConfig
Theme ThemeConfig
Commands map[string]Command
}
var DEFUALT_TOML_CONFIG string = `[editor]
@ -32,8 +34,19 @@ flash_rate = 400
reset_delay = 400
draw = true
flash = true
[commands]
[commands.save]
shortcut = "super+s"
[commands.delete_line]
shortcut = "super+d"
`
type Command struct {
Shortcut string
}
type CursorConfig struct {
Flash_Rate int64
Reset_Delay int64
@ -84,6 +97,7 @@ type EditorConfig struct {
}
func NewDefaultConfig() *TomlConfig {
log.Println("Loading default configuration... this should never happen")
return &TomlConfig{
Editor: EditorConfig{},
Theme: ThemeConfig{

View File

@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
// fork of BurntSushi with hexadecimal support.
"github.com/felixangell/toml"
@ -22,6 +23,37 @@ const (
var CONFIG_FULL_PATH string = ""
// TODO we only had double key combos
// e.g. cmd+s. we want to handle things
// like cmd+alt+s
type shortcutRegister struct {
Supers map[string]string
}
var Shortcuts = &shortcutRegister{
Supers: map[string]string{},
}
func configureAndValidate(conf TomlConfig) {
{
log.Println("Configuring keyboard shortcuts")
// keyboard commands
for commandName, cmd := range conf.Commands {
shortcut := cmd.Shortcut
vals := strings.Split(shortcut, "+")
// TODO handle conflicts
switch vals[0] {
case "super":
Shortcuts.Supers[vals[1]] = commandName
break
}
}
}
}
func Setup() TomlConfig {
log.Println("Setting up Phi Editor")
@ -68,5 +100,6 @@ func Setup() TomlConfig {
panic(err)
}
configureAndValidate(conf)
return conf
}

8
gui/action.go Normal file
View File

@ -0,0 +1,8 @@
package gui
type BufferAction func(*Buffer) bool
var actions = map[string]BufferAction{
"save": Save,
"delete_line": DeleteLine,
}

View File

@ -106,11 +106,6 @@ var shiftAlternative = map[rune]rune{
'§': '±',
}
var shortcuts = map[rune]func(*Buffer) bool{
's': Save,
'd': DeleteLine,
}
func (b *Buffer) processTextInput(r rune) bool {
if CAPS_LOCK {
if unicode.IsLetter(r) {
@ -119,8 +114,11 @@ func (b *Buffer) processTextInput(r rune) bool {
}
if SUPER_DOWN {
if proc, ok := shortcuts[unicode.ToLower(r)]; ok {
return proc(b)
actionName, actionExists := cfg.Shortcuts.Supers[string(unicode.ToLower(r))]
if actionExists {
if proc, ok := actions[actionName]; ok {
return proc(b)
}
}
}
@ -168,6 +166,10 @@ func (b *Buffer) processTextInput(r rune) bool {
return true
}
func remove(slice []*rope.Rope, s int) []*rope.Rope {
return append(slice[:s], slice[s+1:]...)
}
func (b *Buffer) deletePrev() {
if b.curs.x > 0 {
offs := -1

View File

@ -1,11 +1,25 @@
package gui
func Save(b *Buffer) bool {
import "log"
func Save(b *Buffer) bool {
log.Println("Save: unimplemented!")
return false
}
// FIXME make this better!
// behaviour is a little off at the moment.
func DeleteLine(b *Buffer) bool {
panic("unimplemented! :(")
if b.curs.y == 0 {
for b.curs.x < b.contents[b.curs.y].Len() {
b.curs.move(1, 0)
}
b.deleteBeforeCursor()
return true
}
if b.curs.y >= len(b.contents) {
return false
}
b.contents = remove(b.contents[:], b.curs.y)
return true
}

Binary file not shown.