feat(emacs): customisable syd-insert-file-name

This commit is contained in:
2026-04-23 07:37:49 -06:00
parent a35e9b63c1
commit a55570bbf6

View File

@@ -199,15 +199,29 @@ form."
(propertize " " 'display `(space :align-to (- right ,(+ 1 (length x)))))
x))
(defun syd-project-relative-file-name (file-name)
(file-relative-name file-name (project-root (project-current))))
(defvar syd-insert-file-name-alist
`((,#'syd-project-relative-file-name . "Project-relative")
(,#'file-relative-name . "File-relative")
(,#'identity . "Absolute"))
"List of pairs where each cons is a function mapping paths to paths
or nil, and each cons is a string description.")
(defun syd--evaluate-syd-insert-file-name-alist (path)
(cl-loop for (fn . lbl) in syd-insert-file-name-alist
for r = (funcall fn path)
when r
collect (cons r lbl)))
(defun syd-insert-file-name ()
(interactive)
(let* ((path (read-file-name "Path: " nil nil 'confirm))
(proj-root (project-root (project-current)))
(alts
`((,(file-relative-name path proj-root) . "Project-relative")
(,(file-relative-name path default-directory) . "File-relative")
(,path . "Absolute")
("... (choose a dir)")))
(let* ((path (expand-file-name
(read-file-name "Path: " nil nil 'confirm)))
(choose-a-dir "... (choose a dir)")
(alts (cons `(,choose-a-dir)
(syd--evaluate-syd-insert-file-name-alist path)))
(choice
(completing-read
"Variant: "
@@ -221,7 +235,7 @@ form."
(syd--insert-file-name-annotation
desc))))))
(_ (all-completions s (mapcar #'car alts) p)))))))
(if (equal choice "... (choose a root)")
(if (equal choice choose-a-dir)
(insert (file-relative-name
path (read-file-name "Relative to: " nil nil
'confirm)))