diff --git a/bjo.lock b/bjo.lock index 60b03d5..c7c46bb 100644 --- a/bjo.lock +++ b/bjo.lock @@ -1,5 +1,5 @@ (lock (locked - (package (name (bjoterm)) (version "0.2.0") + (package (name (bjoterm)) (version "0.2.1") (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git"))) - (commit "2b7749013d8b784c8185d3d14a291ef66a096257c33f151c0f7331301a5e861a")))) + (commit "702fcedf2d1536ffb4a3e04e9be6fd1417d18e9db299d70d0a36e746fdc047da")))) diff --git a/manifest.bjodat b/manifest.bjodat index 45cbfa9..4fd6f36 100644 --- a/manifest.bjodat +++ b/manifest.bjodat @@ -10,5 +10,5 @@ ;; The line editor and the terminal. To work on both at once, use ;; (source (path (dir "../bjoterm"))) instead. (package (name (bjoterm)) - (version (version-at-least "0.2.0")) + (version (version-at-least "0.2.1")) (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjoterm.git")))))) diff --git a/src/editor.bjo b/src/editor.bjo index 4e29094..73ad7cc 100644 --- a/src/editor.bjo +++ b/src/editor.bjo @@ -199,7 +199,8 @@ ;; 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. +;; aliases and programs on PATH, with their help, and directories, which +;; an interactive shell changes to. ;; - After `cd`: directories, else visited directories that match (see ;; `dirs-match`). ;; - A word with a / or ~: paths. @@ -244,7 +245,8 @@ (not (string-starts-with? word "-")))) ;; Commands that start with `word`, with their help: shell commands, the -;; user's commands, aliases and programs on PATH. +;; user's commands, aliases and programs on PATH; and directories. With +;; case, else without. (: (-> Shell string Completion)) (defun (command-completion sh word) (def ext (record-ref sh extension)) @@ -265,11 +267,15 @@ (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)) + (defun (found fold?) + (vec-append (vec-map (fun (n) (Tuple (str (quote-name n) " ") (described n))) (starting fold?)) + (vec-map (fun (d) (Tuple d "directory")) + (vec-filter (fun (c) (string-ends-with? c "/")) (path-candidates word fold?))))) + (def exact (found #f)) + (def all (if (vec-empty? exact) (found #t) exact)) (Completion (prefix word) - (candidates (vec-map (fun (n) (str (quote-name n) " ")) names)) - (descriptions (vec-map described names)))) + (candidates (vec-map fst all)) + (descriptions (vec-map snd all)))) ;; The programs on PATH. (: (-> (Vec string))) diff --git a/src/exec.bjo b/src/exec.bjo index ab1898e..600da15 100644 --- a/src/exec.bjo +++ b/src/exec.bjo @@ -241,6 +241,7 @@ ((any? (fun (e) (vec-empty? (record-ref e argv))) expanded) (shell-error "a command in a pipe has no words") 2) + ((and (not background?) (implicit-cd? sh expanded)) (run-builtin sh "cd" (record-ref first argv))) ((and (= (vec-length expanded) 1) (not (record-ref first program?)) (shell-command? sh name)) (def args (vec-drop (record-ref first argv) 1)) (cond @@ -298,6 +299,22 @@ (flush-port (parameter-ref current-output-port)) status) +;; Whether `expanded` is one directory alone, which an interactive shell +;; changes to, as fish does: a word with a /, or . or .., that names a +;; directory. +(: (-> Shell (Vec Expanded) bool)) +(defun (implicit-cd? sh expanded) + (def first (vec-ref expanded 0)) + (def argv (record-ref first argv)) + (and (record-ref sh persist?) + (= (vec-length expanded) 1) + (= (vec-length argv) 1) + (vec-empty? (record-ref first assigns)) + (vec-empty? (record-ref first redirects)) + (let ((word (vec-ref argv 0))) + (and (or (string-contains? word "/") (= word ".") (= word "..")) + (directory-exists? word))))) + ;; Whether `name` is a shell command: the user's or the shell's own. (: (-> Shell string bool)) (defun (shell-command? sh name) diff --git a/tests/editor.bjo b/tests/editor.bjo index 5ae2f4c..1994e3e 100644 --- a/tests/editor.bjo +++ b/tests/editor.bjo @@ -68,6 +68,9 @@ (let ((c (complete sh "prevd"))) (Tuple (record-ref c candidates) (record-ref c descriptions))) (Tuple ["prevd "] ["go back in the directory history; also Alt-Left"])) + (expect "and directories" (complete sh "ab") + (Completion (prefix "ab") (candidates ["abd/"]) (descriptions ["directory"]))) + (expect "ignoring case" (cands "AB") ["abd/"]) (expect "a first word with a / completes files" (cands "./ab") ["./abc.txt " "./abd/"]) (expect "a path ignoring case when nothing else matches" (cands "./ABC") ["./abc.txt "]) (expect "each part as the start of a name" (cands "ls ./a/in") ["./abd/inner "]) diff --git a/tests/interactive.bjo b/tests/interactive.bjo index 77f8feb..bea54a5 100644 --- a/tests/interactive.bjo +++ b/tests/interactive.bjo @@ -42,6 +42,8 @@ ;; An empty commands directory: no package of user commands is written. (ignore (directory-create (path-combine state "bjosh/commands"))) (ignore (file-write-text (path-combine state "bjosh/rc") "alias fromrc='echo set in rc'\n")) + (ignore (directory-create (path-combine state "tabs/alpha"))) + (ignore (directory-create (path-combine state "tabs/apple"))) (def keys (string-join ["sleep 2.5" "printf 'sleep 30\\r'" "sleep 1" "printf '\\032'" "sleep 0.5" "printf 'jobs\\r'" "sleep 0.5" @@ -62,6 +64,8 @@ "printf 'echo after $?\\r'" "sleep 0.5" "printf 'fromrc\\r'" "sleep 0.5" "printf 'sleep 0.5 &\\r'" "sleep 0.3" "printf 'ec'" "sleep 1.5" "printf 'ho typed\\r'" "sleep 0.5" + "printf '/usr/\\r'" "sleep 0.3" "printf 'pwd\\r'" "sleep 0.3" + (str "printf 'ls " state "/tabs/a\\t'") "sleep 0.5" "printf '\\003'" "sleep 0.3" "printf 'cd /tmp\\r'" "sleep 0.3" "printf \"PROMPT='<\\\\W \\$(echo s)>\\\\$ '\\r\"" "sleep 0.5" "printf 'exit\\r'" "sleep 1"] @@ -77,6 +81,9 @@ (expect-true "Ctrl-C ends a job, and the prompt starts a new line" (string-contains? out "^C\n")) (expect-true "with status 130" (string-contains? out "status 130")) (expect-true "Ctrl-Z at the prompt does not stop the shell" (string-contains? out "\nalive\n")) + (expect-true "a directory alone changes to it" (string-contains? out "pwd\n/usr\n")) + (expect-true "Tab lists the candidates when it can insert nothing" + (and (string-contains? out "tabs/alpha/") (string-contains? out "tabs/apple/"))) (expect-true "jobs in a pipe" (string-contains? out "jobs | g command\nsleep 30\n")) (expect-true "history in a pipe, with this line last" (string-contains? out "history | tl 1 | g line\nhistory | tl 1 | g line\n"))