feat(emacs): customisable syd-insert-file-name
This commit is contained in:
@@ -199,15 +199,29 @@ form."
|
|||||||
(propertize " " 'display `(space :align-to (- right ,(+ 1 (length x)))))
|
(propertize " " 'display `(space :align-to (- right ,(+ 1 (length x)))))
|
||||||
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 ()
|
(defun syd-insert-file-name ()
|
||||||
(interactive)
|
(interactive)
|
||||||
(let* ((path (read-file-name "Path: " nil nil 'confirm))
|
(let* ((path (expand-file-name
|
||||||
(proj-root (project-root (project-current)))
|
(read-file-name "Path: " nil nil 'confirm)))
|
||||||
(alts
|
(choose-a-dir "... (choose a dir)")
|
||||||
`((,(file-relative-name path proj-root) . "Project-relative")
|
(alts (cons `(,choose-a-dir)
|
||||||
(,(file-relative-name path default-directory) . "File-relative")
|
(syd--evaluate-syd-insert-file-name-alist path)))
|
||||||
(,path . "Absolute")
|
|
||||||
("... (choose a dir)")))
|
|
||||||
(choice
|
(choice
|
||||||
(completing-read
|
(completing-read
|
||||||
"Variant: "
|
"Variant: "
|
||||||
@@ -221,7 +235,7 @@ form."
|
|||||||
(syd--insert-file-name-annotation
|
(syd--insert-file-name-annotation
|
||||||
desc))))))
|
desc))))))
|
||||||
(_ (all-completions s (mapcar #'car alts) p)))))))
|
(_ (all-completions s (mapcar #'car alts) p)))))))
|
||||||
(if (equal choice "... (choose a root)")
|
(if (equal choice choose-a-dir)
|
||||||
(insert (file-relative-name
|
(insert (file-relative-name
|
||||||
path (read-file-name "Relative to: " nil nil
|
path (read-file-name "Relative to: " nil nil
|
||||||
'confirm)))
|
'confirm)))
|
||||||
|
|||||||
Reference in New Issue
Block a user