137 lines
5.2 KiB
Text
137 lines
5.2 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. 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.
|
||
|
|
|
||
|
|
(import (std prelude))
|
||
|
|
(import (std maths))
|
||
|
|
(import (bjoterm line))
|
||
|
|
(import (bjosh syntax))
|
||
|
|
(import (bjosh builtins))
|
||
|
|
|
||
|
|
(export input-done? complete word-start command-position? quote-name file-candidates)
|
||
|
|
|
||
|
|
|
||
|
|
;; 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)))
|
||
|
|
|
||
|
|
(: (-> 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 _) [])))
|
||
|
|
|
||
|
|
;; Programs on PATH and shell commands that start with `prefix`.
|
||
|
|
(: (-> string (Vec string)))
|
||
|
|
(defun (command-candidates prefix)
|
||
|
|
(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 builtin-names programs)))))
|
||
|
|
|
||
|
|
;; `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)))))))
|
||
|
|
|
||
|
|
;; 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))))))
|
||
|
|
|
||
|
|
(: (-> 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)))))
|