Suggestions for cd and paths, and a directory history; bjoterm 0.1.2

The editor suggests, as fish does, the newest cd line of the history whose
directory exists, else the directories or files that start with the word.
prevd, nextd and cdh move in the directory history, and so do Alt-Left and
Alt-Right on an empty line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 13:10:06 +01:00
parent 77c6d7977a
commit 5012a5d993
8 changed files with 262 additions and 14 deletions

View file

@ -9,6 +9,8 @@
;; the shell commands and the programs on PATH; other words, and words
;; with a /, from file names. Names are quoted with \ where the parser
;; needs it. Directories end with /, everything else with a space.
;; - `suggest`: the suggester, as fish's autosuggestions, for `cd` and
;; paths. See `suggest`.
(import (std prelude))
(import (std maths))
@ -17,7 +19,7 @@
(import (bjosh builtins))
(import (bjosh commands))
(export input-done? complete word-start command-position? quote-name file-candidates)
(export input-done? complete suggest word-start command-position? quote-name file-candidates)
;; Whether Enter runs `text`: #f while a quote is open, or the line ends
@ -135,3 +137,100 @@
(candidates (if (and (command-position? text start) (not (string-contains? word "/")))
(command-candidates word)
(file-candidates word)))))
;; ===========================================================================
;; Suggestions
;; ===========================================================================
;; The text to show after `text`, for `#:suggester`. Only for one line.
;; - `cd WORD`: the newest `cd` line in `history` that starts with `text`
;; and whose directory exists from here; else the directories that start
;; with WORD.
;; - Another argument: the files and directories that start with it.
;; - From names: the rest of the only one, or of their common prefix.
;; A file gets no space after it.
(: (-> string (Vec string) (Option string)))
(defun (suggest text history)
(def start (word-start text))
(def word (string-drop text start))
(cond
((string-contains? text "\n") None)
((cd-line? text start)
(match (cd-from-history text history)
((Some rest) (Some rest))
(None (extension word (vec-filter (fun (c) (string-ends-with? c "/")) (file-candidates word))))))
((command-position? text start) None)
((string-empty? word) None)
(else (extension word (file-candidates word)))))
;; Whether `text` is `cd` and the word from `start`.
(: (-> string int bool))
(defun (cd-line? text start)
(= (string-trim (string-take text start)) "cd"))
;; The rest of the newest `cd DIR` line in `history` that starts with
;; `text`, is longer, and names a directory that exists.
(: (-> string (Vec string) (Option string)))
(defun (cd-from-history text history)
(let go ((i (- (vec-length history) 1)))
(cond
((< i 0) None)
(else
(def entry (vec-ref history i))
(def start (word-start entry))
(if (and (> (string-length entry) (string-length text))
(string-starts-with? entry text)
(not (string-contains? entry "\n"))
(cd-line? entry start)
(directory-exists? (expand-home (unquote-name (string-drop entry start)))))
(Some (string-drop entry (string-length text)))
(go (- i 1)))))))
;; What `word` can be extended by from `candidates`: the rest of the only
;; one, or of their common prefix. A trailing space is left out.
(: (-> string (Vec string) (Option string)))
(defun (extension word candidates)
(def matching (vec-filter (fun (c) (string-starts-with? c word)) candidates))
(cond
((vec-empty? matching) None)
(else
(def common (string-trim-right (common-prefix matching)))
(if (> (string-length common) (string-length word))
(Some (string-drop common (string-length word)))
None))))
;; The longest prefix that all of `items` have.
(: (-> (Vec string) string))
(defun (common-prefix items)
(def first (string->vec (vec-ref items 0)))
(def n (loop (:for item (in-vec items))
(:acc k (folding (vec-length first)
(let ((cs (string->vec item)))
(let go ((i 0))
(if (and (< i k) (< i (vec-length cs))
(= (vec-ref first i) (vec-ref cs i)))
(go (+ i 1))
i)))))))
(string-join (vec-map char->string (vec-slice first 0 n)) ""))
;; `name` with the \ escapes of `quote-name` taken out.
(: (-> string string))
(defun (unquote-name name)
(def out (stringbuilder-empty))
(let go ((cs (string->list name)))
(match cs
(Nil unit)
((Cons #\\ (Cons c more)) (stringbuilder-add! out c) (go more))
((Cons c more) (stringbuilder-add! out c) (go more))))
(stringbuilder->string out))
;; `path` with a leading ~ as $HOME.
(: (-> string string))
(defun (expand-home path)
(match (getenv "HOME")
((Some home)
(cond
((= path "~") home)
((string-starts-with? path "~/") (str home (string-drop path 1)))
(else path)))
(None path)))