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

@ -25,6 +25,9 @@
;; turns this off.
;; - The prompt is $PROMPT when it is set (see `custom-prompt`), else the
;; user's prompt function, else the directory and $.
;; - The line editor suggests `cd` directories and paths as fish does (see
;; `suggest`). On an empty line, Alt-Left and Alt-Right go back and
;; forward in the directory history, as `prevd` and `nextd`.
(import (std prelude))
(import (std run))
@ -159,12 +162,28 @@
(flush!)
(stringbuilder->string out))
;; Whether `ev` is Alt-Left or Alt-Right on an empty line: the keys the
;; shell takes for the directory history.
(: (-> TermEvent string bool))
(defun (directory-key? ev text)
(and (string-empty? (string-trim text))
(some? (directory-step ev))))
;; #t for back, #f for forward, for Alt-Left and Alt-Right.
(: (-> TermEvent (Option bool)))
(defun (directory-step ev)
(match ev
((TermKey KeyLeft (Mods (alt? #t) (ctrl? #f))) (Some #t))
((TermKey KeyRight (Mods (alt? #t) (ctrl? #f))) (Some #f))
(_ None)))
(: (-> Terminal int))
(defun (interactive term)
(def jc (match (job-control)
((Ok jc) (Some jc))
((Err _) (shell-error "no job control: & and Ctrl-Z do not work") None)))
(def ed (make-line-editor #:completer (Some complete) #:input-done? (Some input-done?)
#:suggester (Some suggest) #:command-key? (Some directory-key?)
#:suspend? #f))
(def path (history-path))
(ignore (line-history-load! ed path))
@ -184,7 +203,13 @@
(ignore (line-history-save ed path))
(ignore (run-text sh text)))
(LineCancelled (set-status! sh 130))
(LineEof (ignore (run-text sh "exit"))))
(LineEof (ignore (run-text sh "exit")))
((LineKey ev _)
(match (directory-step ev)
((Some back?)
(unless (= (step-directory sh back? #t) 0)
(write-string (parameter-ref current-output-port) "\u{7}")))
(None unit))))
(match (record-ref sh exit-code)
((Some code) code)
(None (go)))))