History suggestions, frecency, cd by keyword, fish completions; bjoterm 0.2.0
The editor suggests history lines whose command exists, cd lines by the frecency of their directory, paths ignoring case, and the visited directory that cd KEYWORD... goes to. Tab completes arguments with fish's completions when fish is on PATH, and paths by the start of each part (~/P/B/B). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5012a5d993
commit
61d5a96eda
10 changed files with 751 additions and 110 deletions
486
src/editor.bjo
486
src/editor.bjo
|
|
@ -5,21 +5,25 @@
|
|||
;; (bjosh editor): what the shell gives the line editor.
|
||||
;;
|
||||
;; - `input-done?`: whether Enter runs the input or starts a new line.
|
||||
;; - `complete`: the completer. The first word of a command completes from
|
||||
;; 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`.
|
||||
;; - `complete`: the completer. See `complete`.
|
||||
;; - `suggest`: the suggester, as fish's autosuggestions. See `suggest`.
|
||||
;; - Names are quoted with \ where the parser needs it. Directories end
|
||||
;; with /, other names with a space.
|
||||
|
||||
(import (std prelude))
|
||||
(import (std maths))
|
||||
(import (std run))
|
||||
(import (bjoterm line))
|
||||
(import (bjosh syntax))
|
||||
(import (bjosh api))
|
||||
(import (bjosh shell))
|
||||
(import (bjosh dirs))
|
||||
(import (bjosh builtins))
|
||||
(import (bjosh commands))
|
||||
|
||||
(export input-done? complete suggest word-start command-position? quote-name file-candidates)
|
||||
(export
|
||||
input-done? complete suggest word-start command-position? quote-name
|
||||
file-candidates path-candidates abbreviated-candidates command-line-at)
|
||||
|
||||
|
||||
;; Whether Enter runs `text`: #f while a quote is open, or the line ends
|
||||
|
|
@ -73,6 +77,10 @@
|
|||
(def parts (string-split (path->string/lossy p) "/"))
|
||||
(vec-ref parts (- (vec-length parts) 1)))
|
||||
|
||||
(: (-> (-> %a (Vec %b)) (Vec %a) (Vec %b)))
|
||||
(defun (vec-flat f v)
|
||||
(vec-fold (fun (x acc) (vec-append acc (f x))) [] v))
|
||||
|
||||
(: (-> string (Vec string)))
|
||||
(defun (path-names dir)
|
||||
(match (directory-files dir)
|
||||
|
|
@ -85,18 +93,189 @@
|
|||
((Ok paths) (vec-map base-name paths))
|
||||
((Err _) [])))
|
||||
|
||||
;; Programs on PATH and shell commands that start with `prefix`.
|
||||
;; ===========================================================================
|
||||
;; Names
|
||||
;; ===========================================================================
|
||||
|
||||
;; The entries of `dir` that start with `part`, each with whether it is a
|
||||
;; directory. With `fold?`, case is ignored. Hidden ones only for a `part`
|
||||
;; that starts with a dot.
|
||||
(: (-> string string bool (Vec (Tuple string bool))))
|
||||
(defun (entries-matching dir part fold?)
|
||||
(def p (if fold? (string-downcase part) part))
|
||||
(defun (wanted? n)
|
||||
(and (string-starts-with? (if fold? (string-downcase n) n) p)
|
||||
(or (string-starts-with? part ".") (not (string-starts-with? n ".")))))
|
||||
(vec-append (vec-map (fun (n) (Tuple n #t)) (vec-filter wanted? (directory-names dir)))
|
||||
(vec-map (fun (n) (Tuple n #f)) (vec-filter wanted? (path-names dir)))))
|
||||
|
||||
;; `n` quoted, with / after a directory and a space after a file.
|
||||
(: (-> (Tuple string bool) string))
|
||||
(defun (entry-text e)
|
||||
(def (Tuple n dir?) e)
|
||||
(str (quote-name n) (if dir? "/" " ")))
|
||||
|
||||
;; The files and directories that start with `word`, which can have a
|
||||
;; directory before it. With `fold?`, the last part ignores case.
|
||||
(: (-> string bool (Vec string)))
|
||||
(defun (path-candidates word fold?)
|
||||
(def parts (string-split word "/"))
|
||||
(def name-part (vec-ref parts (- (vec-length parts) 1)))
|
||||
(def dir-part (string-take word (- (string-length word) (string-length name-part))))
|
||||
(def dir (if (string-empty? dir-part) "." (expand-home (unquote-name dir-part))))
|
||||
(vec-sort (vec-map (fun (e) (str dir-part (entry-text e)))
|
||||
(entries-matching dir (unquote-name name-part) fold?))))
|
||||
|
||||
;; The files and directories that start with `word`.
|
||||
(: (-> string (Vec string)))
|
||||
(defun (command-candidates prefix)
|
||||
(defun (file-candidates word)
|
||||
(path-candidates word #f))
|
||||
|
||||
;; `word` with each part taken as the start of a name, as ~/P/B/B for
|
||||
;; ~/Programmering/Bjolang/Bjolang. A part matches with case, or without
|
||||
;; when nothing does. At most 50.
|
||||
(: (-> string (Vec string)))
|
||||
(defun (abbreviated-candidates word)
|
||||
(def parts (string-split word "/"))
|
||||
(def n (vec-length parts))
|
||||
(def root (vec-ref parts 0))
|
||||
(def start (cond
|
||||
((string-empty? root) (Some (Tuple "/" "/")))
|
||||
((= root "~") (Some (Tuple "~/" (expand-home "~"))))
|
||||
(else None)))
|
||||
(defun (matching dir part)
|
||||
(def exact (entries-matching dir part #f))
|
||||
(if (vec-empty? exact) (entries-matching dir part #t) exact))
|
||||
(if (< n 2)
|
||||
[]
|
||||
(let go ((i (if (some? start) 1 0))
|
||||
(at (match start ((Some s) [s]) (None [(Tuple "" ".")]))))
|
||||
(def part (unquote-name (vec-ref parts i)))
|
||||
(cond
|
||||
((> (vec-length at) 50) [])
|
||||
((= i (- n 1))
|
||||
(vec-sort (vec-flat (fun (t)
|
||||
(def (Tuple shown dir) t)
|
||||
(vec-map (fun (e) (str shown (entry-text e))) (matching dir part)))
|
||||
at)))
|
||||
((vec-contains? ["" "." ".."] part)
|
||||
(go (+ i 1) (vec-map (fun (t) (Tuple (str (fst t) part "/") (path-combine (snd t) part))) at)))
|
||||
(else
|
||||
(go (+ i 1)
|
||||
(vec-flat (fun (t)
|
||||
(def (Tuple shown dir) t)
|
||||
(vec-map (fun (e) (Tuple (str shown (quote-name (fst e)) "/") (path-combine dir (fst e))))
|
||||
(vec-filter snd (matching dir part))))
|
||||
at)))))))
|
||||
|
||||
;; The candidates for a path: those that start with `word`, else those
|
||||
;; that do ignoring case, else `abbreviated-candidates`.
|
||||
(: (-> string (Vec string)))
|
||||
(defun (paths-for word)
|
||||
(def exact (path-candidates word #f))
|
||||
(cond
|
||||
((not (vec-empty? exact)) exact)
|
||||
(else
|
||||
(def folded (path-candidates word #t))
|
||||
(if (vec-empty? folded) (abbreviated-candidates word) folded))))
|
||||
|
||||
;; `path` with ~ for home, quoted.
|
||||
(: (-> string string))
|
||||
(defun (shown-path path)
|
||||
(def home (match (getenv "HOME") ((Some h) h) (None "")))
|
||||
(if (and (not (string-empty? home)) (string-starts-with? path (str home "/")))
|
||||
(str "~/" (quote-name (string-drop path (+ (string-length home) 1))))
|
||||
(quote-name path)))
|
||||
|
||||
;; Whether `word` is written as a path: it has a / or starts with ~.
|
||||
(: (-> string bool))
|
||||
(defun (path-like? word)
|
||||
(or (string-contains? word "/") (string-starts-with? word "~")))
|
||||
|
||||
;; ===========================================================================
|
||||
;; Completion
|
||||
;; ===========================================================================
|
||||
|
||||
;; The completer: the candidates for the word before the cursor.
|
||||
;; - $NAME: shell and environment variables.
|
||||
;; - The first word of a command: shell commands, the user's commands,
|
||||
;; aliases and programs on PATH, with their help.
|
||||
;; - After `cd`: directories, else visited directories that match (see
|
||||
;; `dirs-match`).
|
||||
;; - A word with a / or ~: paths.
|
||||
;; - Another word: fish's completions for the command, with their
|
||||
;; descriptions, when fish is on PATH; else paths.
|
||||
;; - Paths: those that start with the word, else ignoring case, else with
|
||||
;; each part as the start of a name (see `abbreviated-candidates`).
|
||||
(: (-> Shell string Completion))
|
||||
(defun (complete sh text)
|
||||
(def start (word-start text))
|
||||
(def word (string-drop text start))
|
||||
(defun (plain candidates)
|
||||
(Completion (prefix word) (candidates candidates) (descriptions [])))
|
||||
(cond
|
||||
((string-starts-with? word "$")
|
||||
(plain (vec-map (fun (n) (str "$" n " "))
|
||||
(vec-filter (fun (n) (string-starts-with? n (string-drop word 1))) (var-names sh)))))
|
||||
((and (command-position? text start) (not (path-like? word))) (command-completion sh word))
|
||||
((cd-line? text start) (plain (cd-candidates sh word)))
|
||||
((path-like? word) (plain (paths-for word)))
|
||||
(else
|
||||
(match (fish-completion sh text word)
|
||||
((Some c) c)
|
||||
(None (plain (paths-for word)))))))
|
||||
|
||||
;; The directories for `cd WORD`; else visited directories that match.
|
||||
(: (-> Shell string (Vec string)))
|
||||
(defun (cd-candidates sh word)
|
||||
(def dirs (vec-filter (fun (c) (string-ends-with? c "/")) (paths-for word)))
|
||||
(if (or (not (vec-empty? dirs)) (not (jump-keyword? word)))
|
||||
dirs
|
||||
(vec-map (fun (e) (str (shown-path (record-ref e path)) "/"))
|
||||
(dirs-match (record-ref sh dirs) [(unquote-name word)] (current-directory)))))
|
||||
|
||||
;; Whether `word` can name a visited directory to jump to: not empty, and
|
||||
;; not a path, an option or -.
|
||||
(: (-> string bool))
|
||||
(defun (jump-keyword? word)
|
||||
(and (not (string-empty? word))
|
||||
(not (path-like? word))
|
||||
(not (string-starts-with? word "."))
|
||||
(not (string-starts-with? word "-"))))
|
||||
|
||||
;; Commands that start with `word`, with their help: shell commands, the
|
||||
;; user's commands, aliases and programs on PATH.
|
||||
(: (-> Shell string Completion))
|
||||
(defun (command-completion sh word)
|
||||
(def ext (record-ref sh extension))
|
||||
(def help
|
||||
(vec-append
|
||||
(vec-append (vec-map (fun (n) (Tuple n (builtin-help n))) builtin-names)
|
||||
(vec-flat (fun (c) [(Tuple (record-ref c short) (record-ref c help))
|
||||
(Tuple (record-ref c long) (record-ref c help))])
|
||||
(vec-append (value-commands) (record-ref ext commands))))
|
||||
(vec-append (vec-map (fun (c) (Tuple (record-ref c name) (record-ref c help))) (record-ref ext shell-commands))
|
||||
(map-fold (fun (k v acc) (vec-add acc (Tuple k (str "alias for " v)))) [] (record-ref sh aliases)))))
|
||||
(def programs (path-programs))
|
||||
(defun (described name)
|
||||
(match (find (fun (t) (= (fst t) name)) help)
|
||||
((Some (Tuple _ h)) h)
|
||||
(None "")))
|
||||
(defun (starting fold?)
|
||||
(def w (if fold? (string-downcase word) word))
|
||||
(sorted-distinct (vec-filter (fun (n) (string-starts-with? (if fold? (string-downcase n) n) w))
|
||||
(vec-append (vec-map fst help) programs))))
|
||||
(def exact (starting #f))
|
||||
(def names (if (vec-empty? exact) (starting #t) exact))
|
||||
(Completion (prefix word)
|
||||
(candidates (vec-map (fun (n) (str (quote-name n) " ")) names))
|
||||
(descriptions (vec-map described names))))
|
||||
|
||||
;; The programs on PATH.
|
||||
(: (-> (Vec string)))
|
||||
(defun (path-programs)
|
||||
(def dirs (match (getenv "PATH") ((Some p) (string-split p ":")) (None [])))
|
||||
(def programs
|
||||
(let go ((i 0) (all []))
|
||||
(if (= i (vec-length dirs))
|
||||
all
|
||||
(go (+ i 1) (vec-append all (path-names (vec-ref dirs i)))))))
|
||||
(vec-map (fun (n) (str (quote-name n) " "))
|
||||
(sorted-distinct (vec-filter (fun (n) (string-starts-with? n prefix))
|
||||
(vec-append (vec-append builtin-names (value-names)) programs)))))
|
||||
(vec-flat path-names dirs))
|
||||
|
||||
;; `names` sorted, each once.
|
||||
(: (-> (Vec string) (Vec string)))
|
||||
|
|
@ -108,83 +287,238 @@
|
|||
((and (> i 0) (= (vec-ref sorted i) (vec-ref sorted (- i 1)))) (go (+ i 1) out))
|
||||
(else (go (+ i 1) (vec-add out (vec-ref sorted i)))))))
|
||||
|
||||
;; Files and directories that start with `word`, which can have a
|
||||
;; directory before it. Directories end with /, files with a space.
|
||||
(: (-> string (Vec string)))
|
||||
(defun (file-candidates word)
|
||||
(def parts (string-split word "/"))
|
||||
(def name-part (vec-ref parts (- (vec-length parts) 1)))
|
||||
(def dir-part (string-take word (- (string-length word) (string-length name-part))))
|
||||
(def dir (cond
|
||||
((string-empty? dir-part) ".")
|
||||
((string-starts-with? dir-part "~/")
|
||||
(str (match (getenv "HOME") ((Some h) h) (None "~")) (string-drop dir-part 1)))
|
||||
(else dir-part)))
|
||||
(defun (wanted? n)
|
||||
(and (string-starts-with? n name-part)
|
||||
(or (string-starts-with? name-part ".") (not (string-starts-with? n ".")))))
|
||||
(vec-sort
|
||||
(vec-append (vec-map (fun (n) (str dir-part (quote-name n) "/"))
|
||||
(vec-filter wanted? (directory-names dir)))
|
||||
(vec-map (fun (n) (str dir-part (quote-name n) " "))
|
||||
(vec-filter wanted? (path-names dir))))))
|
||||
;; The command that the end of `text` is in: the text after the last |, ;,
|
||||
;; & or ( outside quotes, without leading spaces.
|
||||
(: (-> string string))
|
||||
(defun (command-line-at text)
|
||||
(def chars (string->vec text))
|
||||
(def n (vec-length chars))
|
||||
(let go ((i 0) (quote None) (after 0))
|
||||
(cond
|
||||
((= i n) (string-trim-left (string-drop text after)))
|
||||
(else
|
||||
(def c (vec-ref chars i))
|
||||
(match quote
|
||||
((Some q) (go (+ i 1) (if (= c q) None quote) after))
|
||||
(None
|
||||
(cond
|
||||
((= c #\\) (go (+ i 2) None after))
|
||||
((or (= c #\') (= c #\")) (go (+ i 1) (Some c) after))
|
||||
((string-contains? "|;&(" (char->string c)) (go (+ i 1) None (+ i 1)))
|
||||
(else (go (+ i 1) None after)))))))))
|
||||
|
||||
(: (-> string Completion))
|
||||
(defun (complete text)
|
||||
(def start (word-start text))
|
||||
(def word (string-drop text start))
|
||||
(Completion (prefix word)
|
||||
(candidates (if (and (command-position? text start) (not (string-contains? word "/")))
|
||||
(command-candidates word)
|
||||
(file-candidates word)))))
|
||||
;; `line` with its first word replaced by its alias.
|
||||
(: (-> Shell string string))
|
||||
(defun (with-alias sh line)
|
||||
(match (string-split-once line " ")
|
||||
((Some (Tuple name rest))
|
||||
(match (alias-ref sh name)
|
||||
((Some text) (str text " " rest))
|
||||
(None line)))
|
||||
(None line)))
|
||||
|
||||
;; fish's completions of the command at the end of `text`, for `word`.
|
||||
;; None when fish is not on PATH, fails, takes more than 2 seconds, or
|
||||
;; has none. The line goes to fish as an argument, not as code.
|
||||
(: (-> Shell string string (Option Completion)))
|
||||
(defun (fish-completion sh text word)
|
||||
(match (find-program "fish")
|
||||
(None None)
|
||||
((Some fish)
|
||||
(def line (with-alias sh (command-line-at text)))
|
||||
(def argv (vec-append (match (find-program "timeout") ((Some t) [t "2"]) (None []))
|
||||
[fish "--no-config" "-c" "complete --do-complete=$argv[1]" "--" line]))
|
||||
(match (run/strings '(errors-into-file "/dev/null" ,argv))
|
||||
((Ok lines)
|
||||
(def found (vec-map (fun (l) (match (string-split-once l "\t")
|
||||
((Some (Tuple c d)) (Tuple c d))
|
||||
(None (Tuple l ""))))
|
||||
(vec-filter (fun (l) (not (string-empty? l))) lines)))
|
||||
(if (vec-empty? found)
|
||||
None
|
||||
(Some (Completion (prefix word)
|
||||
(candidates (vec-map (fun (t) (fish-candidate (fst t))) found))
|
||||
(descriptions (vec-map snd found))))))
|
||||
((Err _) None)))))
|
||||
|
||||
;; A candidate from fish: quoted, with a space after it unless it ends
|
||||
;; with / or =.
|
||||
(: (-> string string))
|
||||
(defun (fish-candidate c)
|
||||
(cond
|
||||
((string-ends-with? c "/") (str (quote-name (string-drop-right c 1)) "/"))
|
||||
((string-ends-with? c "=") c)
|
||||
((string-starts-with? c "~/") (str "~/" (quote-name (string-drop c 2)) " "))
|
||||
(else (str (quote-name c) " "))))
|
||||
|
||||
;; ===========================================================================
|
||||
;; 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)
|
||||
;; The suggester: what to show after `text`, as fish's autosuggestions.
|
||||
;; Only for one line that is not empty.
|
||||
;; 1. A line of `history` that starts with `text`, is longer, and runs a
|
||||
;; command that exists. For `cd`, its directory must exist, and the
|
||||
;; line whose directory has the highest frecency wins; else the newest.
|
||||
;; Then the same ignoring case, newest first.
|
||||
;; 2. After `cd`: the directories that start with the word, else ignoring
|
||||
;; case, else the visited directory that the words match, shown as
|
||||
;; "-> DIR" (see `dirs-match`).
|
||||
;; 3. Another argument: the files and directories that start with it,
|
||||
;; else ignoring case.
|
||||
;; From names: the rest of the only one, or of their common prefix. A file
|
||||
;; gets no space after it. A match ignoring case must be the only one, and
|
||||
;; accepting it replaces the word.
|
||||
(: (-> Shell string (Vec string) (Option Suggestion)))
|
||||
(defun (suggest sh 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)))))
|
||||
((string-empty? (string-trim text)) None)
|
||||
(else
|
||||
(match (history-suggestion sh text history)
|
||||
((Some s) (Some s))
|
||||
(None
|
||||
(cond
|
||||
((cd-line? text start) (cd-suggestion sh text start word))
|
||||
((jump-line? text) (jump-suggestion sh text))
|
||||
((command-position? text start) None)
|
||||
((string-empty? word) None)
|
||||
(else (names-suggestion text start word (fun (c) #t)))))))))
|
||||
|
||||
;; 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)))
|
||||
;; Whether `text` is `cd` and more than one word.
|
||||
(: (-> string bool))
|
||||
(defun (jump-line? text)
|
||||
(and (string-starts-with? text "cd ")
|
||||
(> (vec-length (line-words text)) 2)))
|
||||
|
||||
(: (-> string (Vec string)))
|
||||
(defun (line-words text)
|
||||
(vec-filter (fun (w) (not (string-empty? w))) (string-split text " ")))
|
||||
|
||||
;; From the candidates for `word` that `keep?` takes: the rest of the only
|
||||
;; one or of their common prefix; else the only one that matches ignoring
|
||||
;; case, replacing the word.
|
||||
(: (-> string int string (-> string bool) (Option Suggestion)))
|
||||
(defun (names-suggestion text start word keep?)
|
||||
(match (extension word (vec-filter keep? (file-candidates word)))
|
||||
((Some rest) (Some (suggestion-after text rest)))
|
||||
(None
|
||||
(match (vec-filter keep? (path-candidates word #t))
|
||||
([only]
|
||||
(def name (string-trim-right only))
|
||||
(if (> (string-length name) (string-length word))
|
||||
(Some (Suggestion (ghost (string-drop name (string-length word)))
|
||||
(line (str (string-take text start) name))))
|
||||
None))
|
||||
(_ None)))))
|
||||
|
||||
(: (-> Shell string int string (Option Suggestion)))
|
||||
(defun (cd-suggestion sh text start word)
|
||||
(match (names-suggestion text start word (fun (c) (string-ends-with? c "/")))
|
||||
((Some s) (Some s))
|
||||
(None (jump-suggestion sh text))))
|
||||
|
||||
;; For `cd KEYWORD...` that names no directory: the visited directory that
|
||||
;; the keywords match, shown as " -> DIR".
|
||||
(: (-> Shell string (Option Suggestion)))
|
||||
(defun (jump-suggestion sh text)
|
||||
(def words (vec-drop (line-words text) 1))
|
||||
(if (or (vec-empty? words)
|
||||
(not (all? jump-keyword? words))
|
||||
(directory-exists? (unquote-name (vec-ref words 0))))
|
||||
None
|
||||
(match (dirs-match (record-ref sh dirs) (vec-map unquote-name words) (current-directory))
|
||||
([] None)
|
||||
(found
|
||||
(def path (shown-path (record-ref (vec-ref found 0) path)))
|
||||
(Some (Suggestion (ghost (str " \u{2192} " path)) (line (str "cd " path))))))))
|
||||
|
||||
;; The suggestion from `history`. See `suggest`.
|
||||
(: (-> Shell string (Vec string) (Option Suggestion)))
|
||||
(defun (history-suggestion sh text history)
|
||||
(def cd? (string-starts-with? text "cd "))
|
||||
(defun (candidates fold?)
|
||||
(def t (if fold? (string-downcase text) text))
|
||||
(let go ((i (- (vec-length history) 1)) (out []))
|
||||
(cond
|
||||
((< i 0) out)
|
||||
(else
|
||||
(def entry (vec-ref history i))
|
||||
(go (- i 1)
|
||||
(if (and (> (string-length entry) (string-length text))
|
||||
(string-starts-with? (if fold? (string-downcase entry) entry) t)
|
||||
(= (string-length (string-downcase entry)) (string-length entry))
|
||||
(not (vec-contains? out entry))
|
||||
(runnable? sh entry))
|
||||
(vec-add out entry)
|
||||
out))))))
|
||||
(defun (best entries)
|
||||
(cond
|
||||
((< i 0) None)
|
||||
((vec-empty? entries) None)
|
||||
((not cd?) (Some (vec-ref entries 0)))
|
||||
(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)))))))
|
||||
(Some (vec-fold (fun (e acc)
|
||||
(if (> (cd-score sh e) (cd-score sh acc)) e acc))
|
||||
(vec-ref entries 0)
|
||||
entries)))))
|
||||
(match (best (candidates #f))
|
||||
((Some e) (Some (suggestion-after text (string-drop e (string-length text)))))
|
||||
(None
|
||||
(match (candidates #t)
|
||||
([] None)
|
||||
(found
|
||||
(def e (vec-ref found 0))
|
||||
(Some (Suggestion (ghost (string-drop e (string-length text))) (line e))))))))
|
||||
|
||||
;; The frecency of the directory of a `cd DIR` line.
|
||||
(: (-> Shell string int))
|
||||
(defun (cd-score sh line)
|
||||
(match (try (path-absolute (expand-home (unquote-name (string-trim (string-drop line 3)))))
|
||||
#:catch (System.Exception))
|
||||
((Ok path) (dirs-score (record-ref sh dirs)
|
||||
(if (and (> (string-length path) 1) (string-ends-with? path "/"))
|
||||
(string-drop-right path 1)
|
||||
path)))
|
||||
((Err _) 0)))
|
||||
|
||||
;; Whether a history line can run: one line, whose first command exists.
|
||||
;; A `cd` line must name a directory that exists.
|
||||
(: (-> Shell string bool))
|
||||
(defun (runnable? sh entry)
|
||||
(def words (line-words entry))
|
||||
(cond
|
||||
((string-contains? entry "\n") #f)
|
||||
((vec-empty? words) #f)
|
||||
((= (vec-ref words 0) "cd")
|
||||
(or (= (vec-length words) 1)
|
||||
(and (= (vec-length words) 2)
|
||||
(directory-exists? (expand-home (unquote-name (vec-ref words 1)))))))
|
||||
(else (command-exists? sh (vec-ref words 0)))))
|
||||
|
||||
;; Whether `name` is a command: an assignment, a shell command, an alias,
|
||||
;; a user command, a value command or a program.
|
||||
(: (-> Shell string bool))
|
||||
(defun (command-exists? sh name)
|
||||
(or (string-contains? name "=")
|
||||
(not (char-alphabetic-or-path? name))
|
||||
(builtin? name)
|
||||
(some? (alias-ref sh name))
|
||||
(some? (user-shell-command sh name))
|
||||
(some? (user-value-command sh name (fun (_) #f)))
|
||||
(vec-contains? (value-names) name)
|
||||
(some? (find-program (expand-home (unquote-name name))))))
|
||||
|
||||
;; Whether `name` starts like a command name, not like syntax such as (.
|
||||
(: (-> string bool))
|
||||
(defun (char-alphabetic-or-path? name)
|
||||
(not (string-contains? "({!$" (string-take name 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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue