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>
570 lines
23 KiB
Text
570 lines
23 KiB
Text
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;; (bjosh editor): what the shell gives the line editor.
|
|
;;
|
|
;; - `input-done?`: whether Enter runs the input or starts a new line.
|
|
;; - `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 path-candidates abbreviated-candidates command-line-at)
|
|
|
|
|
|
;; Whether Enter runs `text`: #f while a quote is open, or the line ends
|
|
;; with |, &&, || or \. A line with another error runs, to show it.
|
|
(: (-> string bool))
|
|
(defun (input-done? text)
|
|
(and (not (odd-backslashes-at-end? text))
|
|
(match (parse-line/message text)
|
|
((Ok _) #t)
|
|
((Err message) (not (string-contains? message "end of input"))))))
|
|
|
|
;; Whether `text` ends with a \ that escapes the line break after it.
|
|
(: (-> string bool))
|
|
(defun (odd-backslashes-at-end? text)
|
|
(def chars (string->vec text))
|
|
(let go ((i (vec-length chars)) (n 0))
|
|
(if (and (> i 0) (= (vec-ref chars (- i 1)) #\\))
|
|
(go (- i 1) (+ n 1))
|
|
(= (remainder n 2) 1))))
|
|
|
|
;; Where the word before the cursor starts: after the last space or
|
|
;; operator character.
|
|
(: (-> string int))
|
|
(defun (word-start text)
|
|
(def chars (string->vec text))
|
|
(let go ((i (vec-length chars)))
|
|
(cond
|
|
((= i 0) 0)
|
|
((string-contains? " \t\n|&;<>" (char->string (vec-ref chars (- i 1)))) i)
|
|
(else (go (- i 1))))))
|
|
|
|
;; Whether the word at `start` is the first word of a command.
|
|
(: (-> string int bool))
|
|
(defun (command-position? text start)
|
|
(def before (string-trim-right (string-take text start)))
|
|
(or (string-empty? before)
|
|
(string-contains? "|&;" (string-take-right before 1))))
|
|
|
|
;; `name` with \ before characters that the parser would read as syntax.
|
|
(: (-> string string))
|
|
(defun (quote-name name)
|
|
(string-join (vec-map (fun (c)
|
|
(def text (char->string c))
|
|
(if (string-contains? " \t'\"\\$`|&;<>()*?[]#~" text) (str "\\" text) text))
|
|
(string->vec name))
|
|
""))
|
|
|
|
;; The last part of a path.
|
|
(: (-> Path string))
|
|
(defun (base-name p)
|
|
(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)
|
|
((Ok paths) (vec-map base-name paths))
|
|
((Err _) [])))
|
|
|
|
(: (-> string (Vec string)))
|
|
(defun (directory-names dir)
|
|
(match (directory-subdirectories dir)
|
|
((Ok paths) (vec-map base-name paths))
|
|
((Err _) [])))
|
|
|
|
;; ===========================================================================
|
|
;; 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 (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 [])))
|
|
(vec-flat path-names dirs))
|
|
|
|
;; `names` sorted, each once.
|
|
(: (-> (Vec string) (Vec string)))
|
|
(defun (sorted-distinct names)
|
|
(def sorted (vec-sort names))
|
|
(let go ((i 0) (out []))
|
|
(cond
|
|
((= i (vec-length sorted)) out)
|
|
((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)))))))
|
|
|
|
;; 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)))))))))
|
|
|
|
;; `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 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)
|
|
((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"))
|
|
|
|
;; 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
|
|
((vec-empty? entries) None)
|
|
((not cd?) (Some (vec-ref entries 0)))
|
|
(else
|
|
(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.
|
|
(: (-> 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)))
|