rewrite um-find-file-at-point to fallback intelligently

This commit is contained in:
bth 2025-12-16 14:10:13 -08:00
commit 3815d3e403
2 changed files with 50 additions and 28 deletions

View file

@ -48,16 +48,16 @@ This "database" depends on a few simple ideas:
Clone it: Clone it:
```sh ```sh
git clone https://git.bth.moi/bth/um.git ~/um git clone https://git.bth.moi/bth/um.git ~/.emacs.d/um
``` ```
Symlink the `um` CLI somewhere in your `PATH`: Symlink the `um` CLI somewhere in your `PATH`:
```sh ```sh
ln -s ~/um/shell/um /usr/local/bin/um ln -s ~/.emacs.d/um/shell/um /usr/local/bin/um
``` ```
## emacs config ## config
My config looks like this: My config looks like this:
@ -66,15 +66,22 @@ My config looks like this:
:after (project embark) :after (project embark)
:config :config
(defalias 'journal 'um-journal-header) (advice-add 'embark-target-file-at-point :around 'um-target-file-at-point-advice)
(advice-add 'embark-target-file-at-point :around 'um-file-at-point-advice) :load-path "um/elisp"
:load-path "~/um/elisp"
) )
``` ```
Note the optional setup of `embark`, which allows us to visit files just by invoking `emark-dwim` on a file header. This will also work with `C-x C-f M-n` out of the box. Note the optional setup of `embark`, which allows us to find files intelligently, explained below.
# ultralight "database"
What I consider the killer feature of `um` is the ability to jump to files from filenames listed in any "child" project back to the "source" project, defined via `um-journal-path-glob`. This means that child projects can be initially defined as simple lists of filenames, which I do to compose larger pieces. A simple filename anywhere can also serve as a "link".
There are two primary ways this is accessed:
1. `find-file` and `next-history-element`, which will work as `C-x C-f M-n` out of the box.
2. By invoking `emark-dwim` on any filename. Requires `embark` and the `advice-add` shown above.
# CLI usage # CLI usage

View file

@ -28,9 +28,9 @@
;; these source files. ;; these source files.
;; features provided: ;; features provided:
;; `um-journal-find-file' via `find-file': open a file under point in the ;; `um-find-file-at-point' via `find-file': open a file under point in the
;; current project, falling back to a source directory. ;; current project, falling back to a source directory.
;; `um-file-at-point-advice' via `embark-dwim': open file under point in all ;; `um-target-file-at-point-advice' via `embark-dwim': open file under point in all
;; known projects, falling back to a source directory. ;; known projects, falling back to a source directory.
;; `um-grep-tag': search files with same tag ;; `um-grep-tag': search files with same tag
@ -61,9 +61,10 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; find-file-at-point ;; find-file-at-point
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; the journal/ serves as content origin, so it must be treated specially. ;; the journal/ serves as content origin, so it must be treated specially.
(defun um-journal-path() (defun um-journal-path ()
(or (car (seq-filter (or (car (seq-filter
(lambda (path) (lambda (path)
;; this way I don't have to hardcode the full path: ;; this way I don't have to hardcode the full path:
@ -74,29 +75,43 @@
default-directory default-directory
)) ))
;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; find-file C-x C-f solution ;; `find-file' integration
;; combined with the hook into `file-name-at-point-functions', this means I can
;; run find-file at point, followed by M-n :
;; Load like this:
;; (add-hook 'file-name-at-point-functions 'um-journal-find-file)
;; ;;
;; TODO: reuse same fallback logic as the embark advice if I keep this. ;; combined with the hook into `file-name-at-point-functions', this means we can
;; run `find-file' at point, followed by `next-history-element'. By default:
;; C-x C-f M-n
;;;###autoload ;;;###autoload
(defun um-journal-find-file () (defun um-find-file-at-point ()
(let ((default-directory (um-journal-path)) "Return full file path of thing-at-point, falling back first to
(thing (thing-at-point 'filename)) `project-root', then to `um-journal-path'."
) (let ((dirs (list
(expand-file-name thing)) default-directory
) (project-root (project-current))
(um-journal-path)))
dir
file)
(while (and dirs (not file))
(setq dir (car dirs)
dirs (cdr dirs))
(let* ((default-directory dir)
(fap (thing-at-point 'existing-filename t)))
(when fap
(setq file (expand-file-name fap)))))
file))
;;;;;;;;;;;;;;;;;; ;; NOTE: we now load this by default if the package is loaded.
(add-hook 'file-name-at-point-functions 'um-find-file-at-point)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Embark solution ;; Embark solution
;; TODO: unite these two routines. Consider removing the subdirectory recursion
;; and searching all known projects.
;; Load this with an advice: ;; Load this with an advice:
;; (advice-add 'embark-target-file-at-point :around 'um-file-at-point-advice) ;; (advice-add 'embark-target-file-at-point :around 'um-target-file-at-point-advice)
;; `embark-target-file-at-point' uses `ffap-file-at-point', which uses ;; `embark-target-file-at-point' uses `ffap-file-at-point', which uses
;; `default-directory' to expand the filename. ;; `default-directory' to expand the filename.
@ -106,7 +121,7 @@
;; function in the same place in the list anyway. ;; function in the same place in the list anyway.
;;;###autoload ;;;###autoload
(defun um-file-at-point-advice (origfunc) (defun um-target-file-at-point-advice (origfunc)
"Like find-file-at-point, but checks all known project paths. "Like find-file-at-point, but checks all known project paths.
In this order: In this order: