diff --git a/app.go b/app.go index 161d2d3f..253ac1f5 100644 --- a/app.go +++ b/app.go @@ -49,6 +49,7 @@ func (app *app) readFile(path string) { defer f.Close() p := newParser(f) + for p.parse() { p.expr.eval(app, nil) } @@ -58,9 +59,10 @@ func (app *app) readFile(path string) { } } -// This is the main event loop of the application. There are two channels to -// read expressions from client and server. Reading and evaluation are done on -// separate goroutines. +// This is the main event loop of the application. Expressions are read from +// the client and the server on separate goroutines and sent here over channels +// for evaluation. Similarly directories and regular files are also read in +// separate goroutines and sent here for update. func (app *app) loop() { clientChan := app.ui.readExpr() serverChan := readExpr() @@ -170,13 +172,13 @@ func waitKey() error { return nil } -// This function is used to run a command in shell. Following modes are used: +// This function is used to run a shell command. Modes are as follows: // -// Prefix Wait Async Stdin Stdout Stderr UI action -// $ No No Yes Yes Yes Pause and then resume -// % No No Yes Yes Yes Use statline for input/output -// ! Yes No Yes Yes Yes Pause and then resume -// & No Yes No No No Do nothing +// Prefix Wait Async Stdin Stdout Stderr UI action +// $ No No Yes Yes Yes Pause and then resume +// % No No Yes Yes Yes Statline for input/output +// ! Yes No Yes Yes Yes Pause and then resume +// & No Yes No No No Do nothing func (app *app) runShell(s string, args []string, prefix string) { app.exportVars() diff --git a/client.go b/client.go index ac32ee82..8de32951 100644 --- a/client.go +++ b/client.go @@ -13,13 +13,13 @@ import ( ) func run() { - logFile, err := os.Create(gLogPath) + f, err := os.Create(gLogPath) if err != nil { panic(err) } defer os.Remove(gLogPath) - defer logFile.Close() - log.SetOutput(logFile) + defer f.Close() + log.SetOutput(f) log.Print("hi!") diff --git a/complete.go b/complete.go index 9b5e960a..e98e0076 100644 --- a/complete.go +++ b/complete.go @@ -108,13 +108,15 @@ func matchLongest(s1, s2 string) string { func matchWord(s string, words []string) (matches []string, longest string) { for _, w := range words { - if strings.HasPrefix(w, s) { - matches = append(matches, w) - if longest != "" { - longest = matchLongest(longest, w) - } else if s != "" { - longest = w + " " - } + if !strings.HasPrefix(w, s) { + continue + } + + matches = append(matches, w) + if longest != "" { + longest = matchLongest(longest, w) + } else if s != "" { + longest = w + " " } } @@ -135,25 +137,27 @@ func matchExec(s string) (matches []string, longest string) { continue } - fi, err := ioutil.ReadDir(p) + files, err := ioutil.ReadDir(p) if err != nil { log.Printf("reading path: %s", err) } - for _, f := range fi { - if strings.HasPrefix(f.Name(), s) { - f, err = os.Stat(filepath.Join(p, f.Name())) - if err != nil { - log.Printf("getting file information: %s", err) - continue - } - - if !f.Mode().IsRegular() || f.Mode()&0111 == 0 { - continue - } - - words = append(words, f.Name()) + for _, f := range files { + if !strings.HasPrefix(f.Name(), s) { + continue } + + f, err = os.Stat(filepath.Join(p, f.Name())) + if err != nil { + log.Printf("getting file information: %s", err) + continue + } + + if !f.Mode().IsRegular() || f.Mode()&0111 == 0 { + continue + } + + words = append(words, f.Name()) } } @@ -185,12 +189,12 @@ func matchFile(s string) (matches []string, longest string) { dir = unescape(filepath.Dir(dir)) - fi, err := ioutil.ReadDir(dir) + files, err := ioutil.ReadDir(dir) if err != nil { log.Printf("reading directory: %s", err) } - for _, f := range fi { + for _, f := range files { f, err := os.Stat(filepath.Join(dir, f.Name())) if err != nil { log.Printf("getting file information: %s", err) @@ -198,25 +202,29 @@ func matchFile(s string) (matches []string, longest string) { } _, last := filepath.Split(s) - if strings.HasPrefix(escape(f.Name()), last) { - name := f.Name() - if isRoot(s) || filepath.Base(s) != s { - name = filepath.Join(filepath.Dir(s), f.Name()) - } - name = escape(name) - item := f.Name() - if f.Mode().IsDir() { - item += string(filepath.Separator) - } - matches = append(matches, item) - if longest != "" { - longest = matchLongest(longest, name) - } else if s != "" { - if f.Mode().IsRegular() { - longest = name + " " - } else { - longest = name + string(filepath.Separator) - } + if !strings.HasPrefix(escape(f.Name()), last) { + continue + } + + name := f.Name() + if isRoot(s) || filepath.Base(s) != s { + name = filepath.Join(filepath.Dir(s), f.Name()) + } + name = escape(name) + + item := f.Name() + if f.Mode().IsDir() { + item += string(filepath.Separator) + } + matches = append(matches, item) + + if longest != "" { + longest = matchLongest(longest, name) + } else if s != "" { + if f.Mode().IsRegular() { + longest = name + " " + } else { + longest = name + string(filepath.Separator) } } } diff --git a/doc.go b/doc.go index 531a34c7..845c854d 100644 --- a/doc.go +++ b/doc.go @@ -161,10 +161,11 @@ The following command prefixes are used by lf: / search search file in current directory ? search-back search file in the reverse order -The same evaluator is used for the command line and the configuration file. The -difference is that prefixes are not necessary in the command line. Instead -different modes are provided to read corresponding commands. Note that by -default these modes are mapped to the prefix keys above. +The same evaluator is used for the command line and the configuration file for +read and shell commands. The difference is that prefixes are not necessary in +the command line. Instead, different modes are provided to read corresponding +commands. These modes are mapped to the prefix keys above by default. Searching +commands are only used from the command line. Syntax diff --git a/docstring.go b/docstring.go index 848a5f50..246597df 100644 --- a/docstring.go +++ b/docstring.go @@ -168,10 +168,11 @@ The following command prefixes are used by lf: / search search file in current directory ? search-back search file in the reverse order -The same evaluator is used for the command line and the configuration file. -The difference is that prefixes are not necessary in the command line. -Instead different modes are provided to read corresponding commands. Note -that by default these modes are mapped to the prefix keys above. +The same evaluator is used for the command line and the configuration file +for read and shell commands. The difference is that prefixes are not +necessary in the command line. Instead, different modes are provided to read +corresponding commands. These modes are mapped to the prefix keys above by +default. Searching commands are only used from the command line. Syntax diff --git a/eval.go b/eval.go index 1baa6be9..571888ea 100644 --- a/eval.go +++ b/eval.go @@ -209,14 +209,14 @@ func (e *cmdExpr) eval(app *app, args []string) { func splitKeys(s string) (keys []string) { for i := 0; i < len(s); { - c, w := utf8.DecodeRuneInString(s[i:]) - if c != '<' { + r, w := utf8.DecodeRuneInString(s[i:]) + if r != '<' { keys = append(keys, s[i:i+w]) i += w } else { j := i + w - for c != '>' && j < len(s) { - c, w = utf8.DecodeRuneInString(s[j:]) + for r != '>' && j < len(s) { + r, w = utf8.DecodeRuneInString(s[j:]) j += w } keys = append(keys, s[i:j]) @@ -437,16 +437,19 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav) app.ui.loadFileInfo(app.nav) case "push": - if len(e.args) > 0 { - log.Println("pushing keys", e.args[0]) - for _, key := range splitKeys(e.args[0]) { - app.ui.keyChan <- key - } + if len(e.args) != 1 { + app.ui.print("push: requires an argument") + return + } + log.Println("pushing keys", e.args[0]) + for _, key := range splitKeys(e.args[0]) { + app.ui.keyChan <- key } case "cmd-insert": - if len(e.args) > 0 { - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(e.args[0])...) + if len(e.args) == 0 { + return } + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(e.args[0])...) case "cmd-escape": if app.ui.cmdPrefix == ">" { return @@ -560,23 +563,27 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.cmdAccRight = nil app.ui.menuBuf = nil case "cmd-delete": - if len(app.ui.cmdAccRight) > 0 { - app.ui.cmdAccRight = app.ui.cmdAccRight[1:] + if len(app.ui.cmdAccRight) == 0 { + return } + app.ui.cmdAccRight = app.ui.cmdAccRight[1:] case "cmd-delete-back": - if len(app.ui.cmdAccLeft) > 0 { - app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-1] + if len(app.ui.cmdAccLeft) == 0 { + return } + app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-1] case "cmd-left": - if len(app.ui.cmdAccLeft) > 0 { - app.ui.cmdAccRight = append([]rune{app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1]}, app.ui.cmdAccRight...) - app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-1] + if len(app.ui.cmdAccLeft) == 0 { + return } + app.ui.cmdAccRight = append([]rune{app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1]}, app.ui.cmdAccRight...) + app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-1] case "cmd-right": - if len(app.ui.cmdAccRight) > 0 { - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[0]) - app.ui.cmdAccRight = app.ui.cmdAccRight[1:] + if len(app.ui.cmdAccRight) == 0 { + return } + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[0]) + app.ui.cmdAccRight = app.ui.cmdAccRight[1:] case "cmd-home": app.ui.cmdAccRight = append(app.ui.cmdAccLeft, app.ui.cmdAccRight...) app.ui.cmdAccLeft = nil @@ -584,25 +591,28 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight...) app.ui.cmdAccRight = nil case "cmd-delete-home": - if len(app.ui.cmdAccLeft) > 0 { - app.ui.cmdBuf = app.ui.cmdAccLeft - app.ui.cmdAccLeft = nil + if len(app.ui.cmdAccLeft) == 0 { + return } + app.ui.cmdYankBuf = app.ui.cmdAccLeft + app.ui.cmdAccLeft = nil case "cmd-delete-end": - if len(app.ui.cmdAccRight) > 0 { - app.ui.cmdBuf = app.ui.cmdAccRight - app.ui.cmdAccRight = nil + if len(app.ui.cmdAccRight) == 0 { + return } + app.ui.cmdYankBuf = app.ui.cmdAccRight + app.ui.cmdAccRight = nil case "cmd-delete-unix-word": ind := strings.LastIndex(strings.TrimRight(string(app.ui.cmdAccLeft), " "), " ") + 1 - app.ui.cmdBuf = app.ui.cmdAccLeft[ind:] + app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:] app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] case "cmd-yank": - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdBuf...) + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdYankBuf...) case "cmd-transpose": - if len(app.ui.cmdAccLeft) > 1 { - app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2] = app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1] + if len(app.ui.cmdAccLeft) < 2 { + return } + app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2] = app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1] case "cmd-interrupt": if app.cmd != nil { app.cmd.Process.Kill() @@ -612,100 +622,110 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.cmdAccRight = nil app.ui.cmdPrefix = "" case "cmd-word": - if len(app.ui.cmdAccRight) > 0 { - loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) - if loc == nil { - return - } - ind := loc[0] + 1 - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:ind]...) - app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] + if len(app.ui.cmdAccRight) == 0 { + return } + loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc == nil { + return + } + ind := loc[0] + 1 + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:ind]...) + app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] case "cmd-word-back": - if len(app.ui.cmdAccLeft) > 0 { - locs := reWordBeg.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) - if locs == nil { - return - } - ind := locs[len(locs)-1][1] - 1 - app.ui.cmdAccRight = append(app.ui.cmdAccLeft[ind:], app.ui.cmdAccRight...) - app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] + if len(app.ui.cmdAccLeft) == 0 { + return } + locs := reWordBeg.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) + if locs == nil { + return + } + ind := locs[len(locs)-1][1] - 1 + app.ui.cmdAccRight = append(app.ui.cmdAccLeft[ind:], app.ui.cmdAccRight...) + app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] case "cmd-capitalize-word": - if len(app.ui.cmdAccRight) > 0 { - ind := 0 - for ; ind < len(app.ui.cmdAccRight) && unicode.IsSpace(app.ui.cmdAccRight[ind]); ind++ { - } - if ind >= len(app.ui.cmdAccRight) { - return - } - app.ui.cmdAccRight[ind] = unicode.ToUpper(app.ui.cmdAccRight[ind]) - loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) - if loc == nil { - return - } - ind = loc[0] + 1 - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:ind]...) - app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] + if len(app.ui.cmdAccRight) == 0 { + return } + ind := 0 + for ; ind < len(app.ui.cmdAccRight) && unicode.IsSpace(app.ui.cmdAccRight[ind]); ind++ { + } + if ind >= len(app.ui.cmdAccRight) { + return + } + app.ui.cmdAccRight[ind] = unicode.ToUpper(app.ui.cmdAccRight[ind]) + loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc == nil { + return + } + ind = loc[0] + 1 + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:ind]...) + app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] case "cmd-delete-word": - if len(app.ui.cmdAccRight) > 0 { - loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) - if loc == nil { - return - } - ind := loc[0] + 1 - app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] + if len(app.ui.cmdAccRight) == 0 { + return } + loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc == nil { + return + } + ind := loc[0] + 1 + app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] case "cmd-uppercase-word": - if len(app.ui.cmdAccRight) > 0 { - loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) - if loc == nil { - return - } - ind := loc[0] + 1 - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToUpper(string(app.ui.cmdAccRight[:ind])))...) - app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] + if len(app.ui.cmdAccRight) == 0 { + return } + loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc == nil { + return + } + ind := loc[0] + 1 + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToUpper(string(app.ui.cmdAccRight[:ind])))...) + app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] case "cmd-lowercase-word": + if len(app.ui.cmdAccRight) == 0 { + return + } + loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc == nil { + return + } + ind := loc[0] + 1 + app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToLower(string(app.ui.cmdAccRight[:ind])))...) + app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] + case "cmd-transpose-word": + if len(app.ui.cmdAccLeft) == 0 { + return + } + + locs := reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) + if len(locs) < 2 { + return + } + if len(app.ui.cmdAccRight) > 0 { loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) - if loc == nil { - return - } - ind := loc[0] + 1 - app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToLower(string(app.ui.cmdAccRight[:ind])))...) - app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] - } - case "cmd-transpose-word": - if len(app.ui.cmdAccLeft) > 0 { - locs := reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) - if len(locs) < 2 { - return - } - - if len(app.ui.cmdAccRight) > 0 { - loc := reWordEnd.FindStringIndex(string(app.ui.cmdAccRight)) + if loc != nil { ind := loc[0] + 1 app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:ind]...) app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] } - - locs = reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) - - beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1] - beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1] - - var acc []rune - - acc = append(acc, app.ui.cmdAccLeft[:beg1]...) - acc = append(acc, app.ui.cmdAccLeft[beg2:end2]...) - acc = append(acc, app.ui.cmdAccLeft[end1:beg2]...) - acc = append(acc, app.ui.cmdAccLeft[beg1:end1]...) - acc = append(acc, app.ui.cmdAccLeft[end2:]...) - - app.ui.cmdAccLeft = acc } + + locs = reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1) + + beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1] + beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1] + + var acc []rune + + acc = append(acc, app.ui.cmdAccLeft[:beg1]...) + acc = append(acc, app.ui.cmdAccLeft[beg2:end2]...) + acc = append(acc, app.ui.cmdAccLeft[end1:beg2]...) + acc = append(acc, app.ui.cmdAccLeft[beg1:end1]...) + acc = append(acc, app.ui.cmdAccLeft[end2:]...) + + app.ui.cmdAccLeft = acc default: cmd, ok := gOpts.cmds[e.name] if !ok { diff --git a/misc.go b/misc.go index 0cf9fe55..ec16a3dd 100644 --- a/misc.go +++ b/misc.go @@ -195,6 +195,8 @@ func naturalLess(s1, s2 string) bool { return len(parts1) < len(parts2) } +var reAltKey = regexp.MustCompile(``) + var reWord = regexp.MustCompile(`(\pL|\pN)+`) var reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`) var reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`) @@ -214,7 +216,7 @@ func max(a, b int) int { } // We don't need no generic code -// We don't need no thought control +// We don't need no type control // No dark templates in compiler // Haskell leave them kids alone // Hey Bjarne leave them kids alone diff --git a/nav.go b/nav.go index 1bb80f16..701764cf 100644 --- a/nav.go +++ b/nav.go @@ -27,21 +27,7 @@ type file struct { os.FileInfo linkState linkState path string - count int -} - -type filesSortable struct { - files []*file - less func(i, j int) bool -} - -func (f filesSortable) Len() int { return len(f.files) } -func (f filesSortable) Swap(i, j int) { f.files[i], f.files[j] = f.files[j], f.files[i] } -func (f filesSortable) Less(i, j int) bool { return f.less(i, j) } - -// TODO: Replace with `sort.SliceStable` once available -func sortFilesStable(files []*file, less func(i, j int) bool) { - sort.Stable(filesSortable{files: files, less: less}) + dirCount int } func readdir(path string) ([]*file, error) { @@ -49,61 +35,59 @@ func readdir(path string) ([]*file, error) { if err != nil { return nil, err } - names, err := f.Readdirnames(-1) f.Close() - fi := make([]*file, 0, len(names)) - for _, filename := range names { - fpath := filepath.Join(path, filename) + files := make([]*file, 0, len(names)) + for _, fname := range names { + fpath := filepath.Join(path, fname) - lstat, lerr := os.Lstat(fpath) - if os.IsNotExist(lerr) { + lstat, err := os.Lstat(fpath) + if os.IsNotExist(err) { continue } - if lerr != nil { - return fi, lerr + if err != nil { + return files, err } var linkState linkState if lstat.Mode()&os.ModeSymlink != 0 { - stat, serr := os.Stat(fpath) - if serr == nil { + stat, err := os.Stat(fpath) + if err == nil { linkState = working lstat = stat } else { linkState = broken - log.Printf("getting link destination info: %s", serr) } } - fi = append(fi, &file{ + files = append(files, &file{ FileInfo: lstat, linkState: linkState, path: fpath, - count: -1, + dirCount: -1, }) } - return fi, err + return files, err } type dir struct { loading bool // directory is loading from disk loadTime time.Time // current loading or last load time - ind int // index of current entry in fi + ind int // index of current entry in files pos int // position of current entry in ui path string // full path of directory - fi []*file // displayed files in directory including or excluding hidden ones - all []*file // all files in directory including hidden ones (same array as fi) + files []*file // displayed files in directory including or excluding hidden ones + allFiles []*file // all files in directory including hidden ones (same array as files) sortType sortType // sort method and options from last sort } func newDir(path string) *dir { time := time.Now() - fi, err := readdir(path) + files, err := readdir(path) if err != nil { log.Printf("reading directory: %s", err) } @@ -111,57 +95,47 @@ func newDir(path string) *dir { return &dir{ loadTime: time, path: path, - fi: fi, - all: fi, + files: files, + allFiles: files, } } -func (dir *dir) renew() { - fi, err := readdir(dir.path) - if err != nil { - log.Printf("reading directory: %s", err) - } - - dir.fi = fi - dir.all = fi -} - func (dir *dir) sort() { dir.sortType = gOpts.sortType - dir.fi = dir.all + dir.files = dir.allFiles switch gOpts.sortType.method { case naturalSort: - sortFilesStable(dir.fi, func(i, j int) bool { - return naturalLess(strings.ToLower(dir.fi[i].Name()), strings.ToLower(dir.fi[j].Name())) + sort.SliceStable(dir.files, func(i, j int) bool { + return naturalLess(strings.ToLower(dir.files[i].Name()), strings.ToLower(dir.files[j].Name())) }) case nameSort: - sortFilesStable(dir.fi, func(i, j int) bool { - return strings.ToLower(dir.fi[i].Name()) < strings.ToLower(dir.fi[j].Name()) + sort.SliceStable(dir.files, func(i, j int) bool { + return strings.ToLower(dir.files[i].Name()) < strings.ToLower(dir.files[j].Name()) }) case sizeSort: - sortFilesStable(dir.fi, func(i, j int) bool { - return dir.fi[i].Size() < dir.fi[j].Size() + sort.SliceStable(dir.files, func(i, j int) bool { + return dir.files[i].Size() < dir.files[j].Size() }) case timeSort: - sortFilesStable(dir.fi, func(i, j int) bool { - return dir.fi[i].ModTime().Before(dir.fi[j].ModTime()) + sort.SliceStable(dir.files, func(i, j int) bool { + return dir.files[i].ModTime().Before(dir.files[j].ModTime()) }) } if gOpts.sortType.option&reverseSort != 0 { - for i, j := 0, len(dir.fi)-1; i < j; i, j = i+1, j-1 { - dir.fi[i], dir.fi[j] = dir.fi[j], dir.fi[i] + for i, j := 0, len(dir.files)-1; i < j; i, j = i+1, j-1 { + dir.files[i], dir.files[j] = dir.files[j], dir.files[i] } } if gOpts.sortType.option&dirfirstSort != 0 { - sortFilesStable(dir.fi, func(i, j int) bool { - if dir.fi[i].IsDir() == dir.fi[j].IsDir() { + sort.SliceStable(dir.files, func(i, j int) bool { + if dir.files[i].IsDir() == dir.files[j].IsDir() { return i < j } - return dir.fi[i].IsDir() + return dir.files[i].IsDir() }) } @@ -169,39 +143,39 @@ func (dir *dir) sort() { // beginning of our file list and then set the beginning of displayed // files to the first non-hidden file in the list if gOpts.sortType.option&hiddenSort == 0 { - sortFilesStable(dir.fi, func(i, j int) bool { - if dir.fi[i].Name()[0] == '.' && dir.fi[j].Name()[0] == '.' { + sort.SliceStable(dir.files, func(i, j int) bool { + if dir.files[i].Name()[0] == '.' && dir.files[j].Name()[0] == '.' { return i < j } - return dir.fi[i].Name()[0] == '.' + return dir.files[i].Name()[0] == '.' }) - for i, f := range dir.fi { + for i, f := range dir.files { if f.Name()[0] != '.' { - dir.fi = dir.fi[i:] + dir.files = dir.files[i:] return } } - dir.fi = dir.fi[len(dir.fi):] + dir.files = dir.files[len(dir.files):] } } func (dir *dir) name() string { - if len(dir.fi) == 0 { + if len(dir.files) == 0 { return "" } - return dir.fi[dir.ind].Name() + return dir.files[dir.ind].Name() } func (dir *dir) find(name string, height int) { - if len(dir.fi) == 0 { + if len(dir.files) == 0 { dir.ind, dir.pos = 0, 0 return } - dir.ind = min(dir.ind, len(dir.fi)-1) + dir.ind = min(dir.ind, len(dir.files)-1) - if dir.fi[dir.ind].Name() != name { - for i, f := range dir.fi { + if dir.files[dir.ind].Name() != name { + for i, f := range dir.files { if f.Name() == name { dir.ind = i break @@ -209,7 +183,7 @@ func (dir *dir) find(name string, height int) { } } - edge := min(min(height/2, gOpts.scrolloff), len(dir.fi)-dir.ind-1) + edge := min(min(height/2, gOpts.scrolloff), len(dir.files)-dir.ind-1) dir.pos = min(dir.ind, height-edge-1) } @@ -226,34 +200,32 @@ type nav struct { search string } -func newNav(height int) *nav { - wd, err := os.Getwd() - if err != nil { - log.Printf("getting current directory: %s", err) +func (nav *nav) loadDir(path string) *dir { + d, ok := nav.dirCache[path] + if !ok { + go func() { + d := newDir(path) + d.sort() + d.ind, d.pos = 0, 0 + nav.dirChan <- d + }() + d := &dir{loading: true, path: path, sortType: gOpts.sortType} + nav.dirCache[path] = d + return d } - nav := &nav{ - dirChan: make(chan *dir), - regChan: make(chan *reg), - dirCache: make(map[string]*dir), - regCache: make(map[string]*reg), - marks: make(map[string]int), - saves: make(map[string]bool), - markInd: 0, - height: height, + if d.sortType != gOpts.sortType { + go func() { + d.loading = true + name := d.name() + d.sort() + d.find(name, nav.height) + d.loading = false + nav.dirChan <- d + }() } - nav.getDirs(wd) - - return nav -} - -func (nav *nav) position() { - path := nav.currDir().path - for i := len(nav.dirs) - 2; i >= 0; i-- { - nav.dirs[i].find(filepath.Base(path), nav.height) - path = filepath.Dir(path) - } + return d } func (nav *nav) getDirs(wd string) { @@ -272,6 +244,28 @@ func (nav *nav) getDirs(wd string) { nav.dirs = dirs } +func newNav(height int) *nav { + wd, err := os.Getwd() + if err != nil { + log.Printf("getting current directory: %s", err) + } + + nav := &nav{ + dirChan: make(chan *dir), + regChan: make(chan *reg), + dirCache: make(map[string]*dir), + regCache: make(map[string]*reg), + saves: make(map[string]bool), + marks: make(map[string]int), + markInd: 0, + height: height, + } + + nav.getDirs(wd) + + return nav +} + func (nav *nav) renew() { nav.dirCache = make(map[string]*dir) for _, d := range nav.dirs { @@ -317,36 +311,16 @@ func (nav *nav) reload() { nav.getDirs(wd) if err == nil { last := nav.dirs[len(nav.dirs)-1] - last.fi = append(last.fi, curr) + last.files = append(last.files, curr) } } -func (nav *nav) loadDir(path string) *dir { - d, ok := nav.dirCache[path] - if !ok { - go func() { - d := newDir(path) - d.sort() - d.ind, d.pos = 0, 0 - nav.dirChan <- d - }() - d := &dir{loading: true, path: path, sortType: gOpts.sortType} - nav.dirCache[path] = d - return d +func (nav *nav) position() { + path := nav.currDir().path + for i := len(nav.dirs) - 2; i >= 0; i-- { + nav.dirs[i].find(filepath.Base(path), nav.height) + path = filepath.Dir(path) } - - if d.sortType != gOpts.sortType { - go func() { - d.loading = true - name := d.name() - d.sort() - d.find(name, nav.height) - d.loading = false - nav.dirChan <- d - }() - } - - return d } func (nav *nav) preview() { @@ -441,7 +415,7 @@ func (nav *nav) up(dist int) { func (nav *nav) down(dist int) { dir := nav.currDir() - maxind := len(dir.fi) - 1 + maxind := len(dir.files) - 1 if dir.ind >= maxind { return @@ -506,7 +480,7 @@ func (nav *nav) top() { func (nav *nav) bottom() { dir := nav.currDir() - dir.ind = len(dir.fi) - 1 + dir.ind = len(dir.files) - 1 dir.pos = min(dir.ind, nav.height-1) } @@ -535,7 +509,7 @@ func (nav *nav) toggle() { func (nav *nav) invert() { last := nav.currDir() - for _, f := range last.fi { + for _, f := range last.files { path := filepath.Join(last.path, f.Name()) nav.toggleMark(path) } @@ -647,7 +621,7 @@ func (nav *nav) find(path string) error { last := nav.dirs[len(nav.dirs)-1] if last.loading { - last.fi = append(last.fi, &file{FileInfo: lstat}) + last.files = append(last.files, &file{FileInfo: lstat}) } else { last.find(base, nav.height) } @@ -671,8 +645,8 @@ func match(pattern, name string) (matched bool, err error) { func (nav *nav) searchNext() error { last := nav.currDir() - for i := last.ind + 1; i < len(last.fi); i++ { - matched, err := match(nav.search, last.fi[i].Name()) + for i := last.ind + 1; i < len(last.files); i++ { + matched, err := match(nav.search, last.files[i].Name()) if err != nil { return err } @@ -683,7 +657,7 @@ func (nav *nav) searchNext() error { } if gOpts.wrapscan { for i := 0; i < last.ind; i++ { - matched, err := match(nav.search, last.fi[i].Name()) + matched, err := match(nav.search, last.files[i].Name()) if err != nil { return err } @@ -699,7 +673,7 @@ func (nav *nav) searchNext() error { func (nav *nav) searchPrev() error { last := nav.currDir() for i := last.ind - 1; i >= 0; i-- { - matched, err := match(nav.search, last.fi[i].Name()) + matched, err := match(nav.search, last.files[i].Name()) if err != nil { return err } @@ -709,8 +683,8 @@ func (nav *nav) searchPrev() error { } } if gOpts.wrapscan { - for i := len(last.fi) - 1; i > last.ind; i-- { - matched, err := match(nav.search, last.fi[i].Name()) + for i := len(last.files) - 1; i > last.ind; i-- { + matched, err := match(nav.search, last.files[i].Name()) if err != nil { return err } @@ -730,10 +704,10 @@ func (nav *nav) currDir() *dir { func (nav *nav) currFile() (*file, error) { last := nav.dirs[len(nav.dirs)-1] - if len(last.fi) == 0 { + if len(last.files) == 0 { return nil, fmt.Errorf("empty directory") } - return last.fi[last.ind], nil + return last.files[last.ind], nil } type indexedMarks struct { diff --git a/os.go b/os.go index 8beada81..681e7ef7 100644 --- a/os.go +++ b/os.go @@ -47,15 +47,15 @@ func init() { } func pauseCommand() *exec.Cmd { - c := `echo - echo -n 'Press any key to continue' - old=$(stty -g) - stty raw -echo - eval "ignore=\$(dd bs=1 count=1 2> /dev/null)" - stty $old - echo` + cmd := `echo + echo -n 'Press any key to continue' + old=$(stty -g) + stty raw -echo + eval "ignore=\$(dd bs=1 count=1 2> /dev/null)" + stty $old + echo` - return exec.Command(gOpts.shell, "-c", c) + return exec.Command(gOpts.shell, "-c", cmd) } func shellCommand(s string, args []string) *exec.Cmd { @@ -80,10 +80,11 @@ func putCommand(list []string, dir *dir, copy bool) *exec.Cmd { } // XXX: POSIX standard states that -i flag shall do nothing when the - // response is not affirmative. Since this command is run with a nil stdin, - // it should not give an affirmative answer and in return this command - // should not overwrite existing files. Our intention here is to use the - // standard -i flag in place of non-standard -n flag to avoid overwrites. + // response is not affirmative. Since this command is run with a nil + // stdin, it should not give an affirmative answer and in return this + // command should not overwrite existing files. Our intention here is + // to use the standard -i flag in place of non-standard -n flag to + // avoid overwrites. args = append(args, "-i") args = append(args, list...) diff --git a/parse.go b/parse.go index 5d9de5fc..bfbdc9dd 100644 --- a/parse.go +++ b/parse.go @@ -23,7 +23,7 @@ package main // ExecExpr = Prefix '\n' // | Prefix '{{' '}}' ';' // -// Prefix = '$' | '!' | '&' | '/' | '?' +// Prefix = '$' | '%' | '!' | '&' // // ListExpr = ':' Expr ListRest '\n' // | ':' '{{' Expr ListRest '}}' ';' diff --git a/server.go b/server.go index ebab8014..1b1ad6aa 100644 --- a/server.go +++ b/server.go @@ -16,12 +16,12 @@ var ( ) func serve() { - logFile, err := os.Create(gServerLogPath) + f, err := os.Create(gServerLogPath) if err != nil { panic(err) } - defer logFile.Close() - log.SetOutput(logFile) + defer f.Close() + log.SetOutput(f) log.Print("hi!") diff --git a/ui.go b/ui.go index c9e9479b..5a0fe906 100644 --- a/ui.go +++ b/ui.go @@ -7,7 +7,6 @@ import ( "log" "os" "path/filepath" - "regexp" "sort" "strconv" "strings" @@ -22,63 +21,63 @@ import ( const gEscapeCode = 27 -var gKeyVal = map[termbox.Key][]rune{ - termbox.KeyF1: {'<', 'f', '-', '1', '>'}, - termbox.KeyF2: {'<', 'f', '-', '2', '>'}, - termbox.KeyF3: {'<', 'f', '-', '3', '>'}, - termbox.KeyF4: {'<', 'f', '-', '4', '>'}, - termbox.KeyF5: {'<', 'f', '-', '5', '>'}, - termbox.KeyF6: {'<', 'f', '-', '6', '>'}, - termbox.KeyF7: {'<', 'f', '-', '7', '>'}, - termbox.KeyF8: {'<', 'f', '-', '8', '>'}, - termbox.KeyF9: {'<', 'f', '-', '9', '>'}, - termbox.KeyF10: {'<', 'f', '-', '1', '0', '>'}, - termbox.KeyF11: {'<', 'f', '-', '1', '1', '>'}, - termbox.KeyF12: {'<', 'f', '-', '1', '2', '>'}, - termbox.KeyInsert: {'<', 'i', 'n', 's', 'e', 'r', 't', '>'}, - termbox.KeyDelete: {'<', 'd', 'e', 'l', 'e', 't', 'e', '>'}, - termbox.KeyHome: {'<', 'h', 'o', 'm', 'e', '>'}, - termbox.KeyEnd: {'<', 'e', 'n', 'd', '>'}, - termbox.KeyPgup: {'<', 'p', 'g', 'u', 'p', '>'}, - termbox.KeyPgdn: {'<', 'p', 'g', 'd', 'n', '>'}, - termbox.KeyArrowUp: {'<', 'u', 'p', '>'}, - termbox.KeyArrowDown: {'<', 'd', 'o', 'w', 'n', '>'}, - termbox.KeyArrowLeft: {'<', 'l', 'e', 'f', 't', '>'}, - termbox.KeyArrowRight: {'<', 'r', 'i', 'g', 'h', 't', '>'}, - termbox.KeyCtrlSpace: {'<', 'c', '-', 's', 'p', 'a', 'c', 'e', '>'}, - termbox.KeyCtrlA: {'<', 'c', '-', 'a', '>'}, - termbox.KeyCtrlB: {'<', 'c', '-', 'b', '>'}, - termbox.KeyCtrlC: {'<', 'c', '-', 'c', '>'}, - termbox.KeyCtrlD: {'<', 'c', '-', 'd', '>'}, - termbox.KeyCtrlE: {'<', 'c', '-', 'e', '>'}, - termbox.KeyCtrlF: {'<', 'c', '-', 'f', '>'}, - termbox.KeyCtrlG: {'<', 'c', '-', 'g', '>'}, - termbox.KeyBackspace: {'<', 'b', 's', '>'}, - termbox.KeyTab: {'<', 't', 'a', 'b', '>'}, - termbox.KeyCtrlJ: {'<', 'c', '-', 'j', '>'}, - termbox.KeyCtrlK: {'<', 'c', '-', 'k', '>'}, - termbox.KeyCtrlL: {'<', 'c', '-', 'l', '>'}, - termbox.KeyEnter: {'<', 'e', 'n', 't', 'e', 'r', '>'}, - termbox.KeyCtrlN: {'<', 'c', '-', 'n', '>'}, - termbox.KeyCtrlO: {'<', 'c', '-', 'o', '>'}, - termbox.KeyCtrlP: {'<', 'c', '-', 'p', '>'}, - termbox.KeyCtrlQ: {'<', 'c', '-', 'q', '>'}, - termbox.KeyCtrlR: {'<', 'c', '-', 'r', '>'}, - termbox.KeyCtrlS: {'<', 'c', '-', 's', '>'}, - termbox.KeyCtrlT: {'<', 'c', '-', 't', '>'}, - termbox.KeyCtrlU: {'<', 'c', '-', 'u', '>'}, - termbox.KeyCtrlV: {'<', 'c', '-', 'v', '>'}, - termbox.KeyCtrlW: {'<', 'c', '-', 'w', '>'}, - termbox.KeyCtrlX: {'<', 'c', '-', 'x', '>'}, - termbox.KeyCtrlY: {'<', 'c', '-', 'y', '>'}, - termbox.KeyCtrlZ: {'<', 'c', '-', 'z', '>'}, - termbox.KeyEsc: {'<', 'e', 's', 'c', '>'}, - termbox.KeyCtrlBackslash: {'<', 'c', '-', '\\', '>'}, - termbox.KeyCtrlRsqBracket: {'<', 'c', '-', ']', '>'}, - termbox.KeyCtrl6: {'<', 'c', '-', '6', '>'}, - termbox.KeyCtrlSlash: {'<', 'c', '-', '/', '>'}, - termbox.KeySpace: {'<', 's', 'p', 'a', 'c', 'e', '>'}, - termbox.KeyBackspace2: {'<', 'b', 's', '2', '>'}, +var gKeyVal = map[termbox.Key]string{ + termbox.KeyF1: "", + termbox.KeyF2: "", + termbox.KeyF3: "", + termbox.KeyF4: "", + termbox.KeyF5: "", + termbox.KeyF6: "", + termbox.KeyF7: "", + termbox.KeyF8: "", + termbox.KeyF9: "", + termbox.KeyF10: "", + termbox.KeyF11: "", + termbox.KeyF12: "", + termbox.KeyInsert: "", + termbox.KeyDelete: "", + termbox.KeyHome: "", + termbox.KeyEnd: "", + termbox.KeyPgup: "", + termbox.KeyPgdn: "", + termbox.KeyArrowUp: "", + termbox.KeyArrowDown: "", + termbox.KeyArrowLeft: "", + termbox.KeyArrowRight: "", + termbox.KeyCtrlSpace: "", + termbox.KeyCtrlA: "", + termbox.KeyCtrlB: "", + termbox.KeyCtrlC: "", + termbox.KeyCtrlD: "", + termbox.KeyCtrlE: "", + termbox.KeyCtrlF: "", + termbox.KeyCtrlG: "", + termbox.KeyBackspace: "", + termbox.KeyTab: "", + termbox.KeyCtrlJ: "", + termbox.KeyCtrlK: "", + termbox.KeyCtrlL: "", + termbox.KeyEnter: "", + termbox.KeyCtrlN: "", + termbox.KeyCtrlO: "", + termbox.KeyCtrlP: "", + termbox.KeyCtrlQ: "", + termbox.KeyCtrlR: "", + termbox.KeyCtrlS: "", + termbox.KeyCtrlT: "", + termbox.KeyCtrlU: "", + termbox.KeyCtrlV: "", + termbox.KeyCtrlW: "", + termbox.KeyCtrlX: "", + termbox.KeyCtrlY: "", + termbox.KeyCtrlZ: "", + termbox.KeyEsc: "", + termbox.KeyCtrlBackslash: "", + termbox.KeyCtrlRsqBracket: "", + termbox.KeyCtrl6: "", + termbox.KeyCtrlSlash: "", + termbox.KeySpace: "", + termbox.KeyBackspace2: "", } var gValKey map[string]termbox.Key @@ -86,7 +85,7 @@ var gValKey map[string]termbox.Key func init() { gValKey = make(map[string]termbox.Key) for k, v := range gKeyVal { - gValKey[string(v)] = k + gValKey[v] = k } } @@ -202,27 +201,27 @@ func fileInfo(f *file, d *dir) string { continue } - if f.count == -1 { + if f.dirCount == -1 { d, err := os.Open(path) if err != nil { - f.count = -2 + f.dirCount = -2 } names, err := d.Readdirnames(1000) d.Close() if names == nil && err != io.EOF { - f.count = -2 + f.dirCount = -2 } else { - f.count = len(names) + f.dirCount = len(names) } } switch { - case f.count < 0: + case f.dirCount < 0: info = fmt.Sprintf("%s ?", info) - case f.count < 1000: - info = fmt.Sprintf("%s %4d", info, f.count) + case f.dirCount < 1000: + info = fmt.Sprintf("%s %4d", info, f.dirCount) default: info = fmt.Sprintf("%s 999+", info) } @@ -249,20 +248,20 @@ func (win *win) printDir(dir *dir, marks map[string]int, saves map[string]bool, return } - if len(dir.fi) == 0 { + if len(dir.files) == 0 { fg = termbox.AttrBold win.print(2, 0, fg, bg, "empty") return } beg := max(dir.ind-dir.pos, 0) - end := min(beg+win.h, len(dir.fi)) + end := min(beg+win.h, len(dir.files)) if beg > end { return } - for i, f := range dir.fi[beg:end] { + for i, f := range dir.files[beg:end] { fg, bg = colors.get(f) path := filepath.Join(dir.path, f.Name()) @@ -335,7 +334,7 @@ type ui struct { cmdPrefix string cmdAccLeft []rune cmdAccRight []rune - cmdBuf []rune + cmdYankBuf []rune keyAcc []rune keyCount []rune colors colorMap @@ -343,8 +342,8 @@ type ui struct { func getWidths(wtot int) []int { rsum := 0 - for _, rat := range gOpts.ratios { - rsum += rat + for _, r := range gOpts.ratios { + rsum += r } wlen := len(gOpts.ratios) @@ -538,7 +537,7 @@ func (ui *ui) drawStatLine(nav *nav) { ui.msgWin.print(0, 0, fg, bg, ui.msg) - tot := len(currDir.fi) + tot := len(currDir.files) ind := min(currDir.ind+1, tot) acc := string(ui.keyCount) + string(ui.keyAcc) @@ -662,11 +661,12 @@ func (ui *ui) draw(nav *nav) { func findBinds(keys map[string]expr, prefix string) (binds map[string]expr, ok bool) { binds = make(map[string]expr) for key, expr := range keys { - if strings.HasPrefix(key, prefix) { - binds[key] = expr - if key == prefix { - ok = true - } + if !strings.HasPrefix(key, prefix) { + continue + } + binds[key] = expr + if key == prefix { + ok = true } } return @@ -692,8 +692,6 @@ func listBinds(binds map[string]expr) *bytes.Buffer { return b } -var altKey = regexp.MustCompile(``) - func (ui *ui) pollEvent() termbox.Event { select { case key := <-ui.keyChan: @@ -707,8 +705,8 @@ func (ui *ui) pollEvent() termbox.Event { ev.Ch = '<' case key == "": ev.Ch = '>' - case altKey.MatchString(key): - match := altKey.FindStringSubmatch(key)[1] + case reAltKey.MatchString(key): + match := reAltKey.FindStringSubmatch(key)[1] ev.Ch, _ = utf8.DecodeRuneInString(match) ev.Mod = termbox.ModAlt default: @@ -727,24 +725,9 @@ func (ui *ui) pollEvent() termbox.Event { } } -func readCmdEvent(ch chan<- expr, ev termbox.Event) { - if ev.Ch != 0 { - if ev.Mod == termbox.ModAlt { - val := []rune{'<', 'a', '-', ev.Ch, '>'} - if expr, ok := gOpts.cmdkeys[string(val)]; ok { - ch <- expr - } - } else { - ch <- &callExpr{"cmd-insert", []string{string(ev.Ch)}, 1} - } - } else { - val := gKeyVal[ev.Key] - if expr, ok := gOpts.cmdkeys[string(val)]; ok { - ch <- expr - } - } -} - +// This function is used to read a normal event on the client side. For keys, +// digits are interpreted as command counts but this is only done for digits +// preceding any non-digit characters (e.g. "42y2k" as 42 times "y2k"). func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) { draw := &callExpr{"draw", nil, 1} count := 1 @@ -754,9 +737,9 @@ func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) { if ev.Ch != 0 { switch { case ev.Ch == '<': - ui.keyAcc = append(ui.keyAcc, '<', 'l', 't', '>') + ui.keyAcc = append(ui.keyAcc, []rune("")...) case ev.Ch == '>': - ui.keyAcc = append(ui.keyAcc, '<', 'g', 't', '>') + ui.keyAcc = append(ui.keyAcc, []rune("")...) case ev.Mod == termbox.ModAlt: ui.keyAcc = append(ui.keyAcc, '<', 'a', '-', ev.Ch, '>') case unicode.IsDigit(ev.Ch) && len(ui.keyAcc) == 0: @@ -766,12 +749,12 @@ func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) { } } else { val := gKeyVal[ev.Key] - if string(val) == "" { + if val == "" { ch <- draw ui.keyAcc = nil ui.keyCount = nil } - ui.keyAcc = append(ui.keyAcc, val...) + ui.keyAcc = append(ui.keyAcc, []rune(val)...) } if len(ui.keyAcc) == 0 { @@ -843,9 +826,24 @@ func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) { } } -// This function is used to read expressions on the client side. Digits are -// interpreted as command counts but this is only done for digits preceding any -// non-digit characters (e.g. "42y2k" as 42 times "y2k"). +func readCmdEvent(ch chan<- expr, ev termbox.Event) { + if ev.Ch != 0 { + if ev.Mod == termbox.ModAlt { + val := string([]rune{'<', 'a', '-', ev.Ch, '>'}) + if expr, ok := gOpts.cmdkeys[val]; ok { + ch <- expr + } + } else { + ch <- &callExpr{"cmd-insert", []string{string(ev.Ch)}, 1} + } + } else { + val := gKeyVal[ev.Key] + if expr, ok := gOpts.cmdkeys[val]; ok { + ch <- expr + } + } +} + func (ui *ui) readExpr() <-chan expr { ch := make(chan expr)