$EDITOR integration (wave 5, backlog A8)

Watch mode opens the current exercise in $EDITOR (or $VISUAL) when
it starts and whenever the exercise changes: on n, and on a jump
that moves. The editor is spawned detached, so the watcher never
blocks, and a missing editor binary is ignored. A one-line notice
names the opened file, on terminals only. --no-editor and
LISPLINGS_NO_EDITOR turn it off; usage text and the README
workflow/commands sections document the flag.
This commit is contained in:
venomade committed 2026-11-22 11:52:48 +00:00
1 parent 7424463d5e
commit a28a35991f
2 files changed
+58 -12

No files matched your search

+6 -3
View File
@@ -54,9 +54,11 @@ nix run /path/to/lisplings # watch mode, on this directory
progress bar, and the keys that work right now. Crossing into a new
chapter prints that chapter's README above the exercise; `run <name>`
prints the target chapter's README too.
3. Edit that file in any editor and save. The exercise runs again and
the screen redraws; when every check passes it shows `Exercise
done ✓` and the matching solution to compare against.
3. The watcher opens each new exercise in $EDITOR (or $VISUAL). Edit
and save; the exercise runs again and the screen redraws; when
every check passes it shows `Exercise done ✓` and the matching
solution to compare against. Pass `--no-editor`, or set
`LISPLINGS_NO_EDITOR`, to open files by hand instead.
4. Press `n` for the next exercise.
Keys in watch mode (each key is a line: type it, then press ENTER):
@@ -80,6 +82,7 @@ solved.
```sh
./lisplings watch the current exercise (the default)
./lisplings --no-editor watch without opening $EDITOR
./lisplings run [name] run one exercise; also selects it as current
./lisplings hint [name] print every hint paragraph, numbered
./lisplings list [pending|done|all]
+52 -9
View File
@@ -523,6 +523,35 @@ always prints linearly."
(setf (screen-state-notice state) nil))))
(finish-output)))
;;; ---------------------------------------------------------------------
;;; Editor integration
(defparameter *no-editor* nil)
(defun editor-command ()
"The editor to open exercises in: $EDITOR, else $VISUAL. NIL when
the user passed --no-editor, set LISPLINGS_NO_EDITOR, or set
neither."
(and (not *no-editor*)
(let ((env (sb-ext:posix-getenv "LISPLINGS_NO_EDITOR")))
(not (and env (plusp (length env)))))
(or (sb-ext:posix-getenv "EDITOR")
(sb-ext:posix-getenv "VISUAL"))))
(defun open-editor-for (ex state)
"Spawn $EDITOR on EX's file, detached, and note it once on the next
frame. Watch mode never waits for the editor."
(let ((editor (editor-command)))
(when editor
(let ((proc (ignore-errors
(sb-ext:run-program editor
(list (namestring (exercise-path ex)))
:wait nil :search t))))
(when (and proc (stdout-is-tty-p))
(setf (screen-state-notice state)
(format nil "Opened exercises/~a/~a.lisp in ~a"
(exercise-dir ex) (exercise-name ex) editor)))))))
;;; ---------------------------------------------------------------------
;;; Watch mode
@@ -649,12 +678,15 @@ empty answer (or EOF) stays on the current exercise."
(let ((number (ignore-errors (parse-integer answer :junk-allowed t))))
(cond ((and (integerp number)
(<= 1 number) (<= number (length *exercises*)))
(let ((old-dir (exercise-dir (current-exercise state))))
(let* ((old-index (screen-state-index state))
(old-dir (exercise-dir (nth old-index *exercises*))))
(setf (screen-state-index state) (1- number)
(screen-state-revealed state) 0
(screen-state-chapter-note state)
(chapter-note-for old-dir (1- number)))
(set-current-name (exercise-name (nth (1- number) *exercises*)))
(unless (= old-index (1- number))
(open-editor-for (current-exercise state) state))
(run-and-show state queue lock)))
((integerp number)
(setf (screen-state-notice state)
@@ -665,12 +697,15 @@ empty answer (or EOF) stays on the current exercise."
:test #'string=)))
(cond (match
(let* ((new-index (position match *exercises*))
(old-dir (exercise-dir (current-exercise state))))
(old-index (screen-state-index state))
(old-dir (exercise-dir (nth old-index *exercises*))))
(setf (screen-state-index state) new-index
(screen-state-revealed state) 0
(screen-state-chapter-note state)
(chapter-note-for old-dir new-index))
(set-current-name (exercise-name match))
(unless (= old-index new-index)
(open-editor-for (current-exercise state) state))
(run-and-show state queue lock)))
(t
(setf (screen-state-notice state)
@@ -705,6 +740,7 @@ empty answer (or EOF) stays on the current exercise."
(set-current-name (exercise-name (nth index *exercises*)))
(setf (screen-state-chapter-note state)
(chapter-note-for nil index))
(open-editor-for (current-exercise state) state)
(run-and-show state queue lock)
(block watch-loop
(loop
@@ -729,6 +765,7 @@ empty answer (or EOF) stays on the current exercise."
(chapter-note-for (exercise-dir current) next))
(set-current-name
(exercise-name (nth next *exercises*)))
(open-editor-for (current-exercise state) state)
(run-and-show state queue lock))
(t
(setf (screen-state-result state) :alldone)
@@ -886,7 +923,9 @@ empty answer (or EOF) stays on the current exercise."
(format t "writing Common Lisp code.~%~%")
(format t "Usage: lisplings [command]~%~%")
(format t "Running lisplings with no command starts the watcher: it runs~%")
(format t "the current exercise and reruns it whenever you save the file.~%~%")
(format t "the current exercise and reruns it whenever you save the file.~%")
(format t "Watch mode also opens the current exercise in $EDITOR (or $VISUAL);~%")
(format t "pass --no-editor, or set LISPLINGS_NO_EDITOR, to keep it closed.~%~%")
(format t "Commands:~%")
(format t " run [name] run one exercise, and select it for watch mode~%")
(format t " hint [name] print every hint paragraph, numbered~%")
@@ -898,19 +937,23 @@ empty answer (or EOF) stays on the current exercise."
(defun main (argv)
(handler-case
(let ((command (first argv)))
(let* ((args (remove "--no-editor" argv :test #'string=))
(command (first args)))
;; --no-editor keeps watch mode from opening $EDITOR for this
;; run; the LISPLINGS_NO_EDITOR environment is checked per spawn.
(setf *no-editor* (/= (length args) (length argv)))
(cond
((null command) (setup) (watch) (sb-ext:exit :code 0))
((string-equal command "init")
(init-command (or (second argv) "lisplings")))
(init-command (or (second args) "lisplings")))
((string-equal command "help") (usage) (sb-ext:exit :code 0))
(t
(setup)
(cond
((string-equal command "run") (run-command (second argv)))
((string-equal command "hint") (hint-command (second argv)))
((string-equal command "list") (list-command (second argv)))
((string-equal command "reset") (reset-command (second argv)))
((string-equal command "run") (run-command (second args)))
((string-equal command "hint") (hint-command (second args)))
((string-equal command "list") (list-command (second args)))
((string-equal command "reset") (reset-command (second args)))
((string-equal command "check-all") (check-all-command))
(t
(format t "~&Unknown command: ~a~%~%" command)