rewrite um-find-file-at-point to fallback intelligently
This commit is contained in:
parent
c823ea1930
commit
3815d3e403
2 changed files with 50 additions and 28 deletions
23
README.md
23
README.md
|
|
@ -48,16 +48,16 @@ This "database" depends on a few simple ideas:
|
|||
Clone it:
|
||||
|
||||
```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`:
|
||||
|
||||
```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:
|
||||
|
||||
|
|
@ -66,15 +66,22 @@ My config looks like this:
|
|||
:after (project embark)
|
||||
|
||||
: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
|
||||
|
||||
|
|
|
|||
55
elisp/um.el
55
elisp/um.el
|
|
@ -28,9 +28,9 @@
|
|||
;; these source files.
|
||||
|
||||
;; 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.
|
||||
;; `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.
|
||||
|
||||
;; `um-grep-tag': search files with same tag
|
||||
|
|
@ -61,9 +61,10 @@
|
|||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; find-file-at-point
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
;; the journal/ serves as content origin, so it must be treated specially.
|
||||
(defun um-journal-path()
|
||||
(defun um-journal-path ()
|
||||
(or (car (seq-filter
|
||||
(lambda (path)
|
||||
;; this way I don't have to hardcode the full path:
|
||||
|
|
@ -74,29 +75,43 @@
|
|||
default-directory
|
||||
))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;
|
||||
;; find-file C-x C-f solution
|
||||
;; 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)
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; `find-file' integration
|
||||
;;
|
||||
;; 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
|
||||
(defun um-journal-find-file ()
|
||||
(let ((default-directory (um-journal-path))
|
||||
(thing (thing-at-point 'filename))
|
||||
)
|
||||
(expand-file-name thing))
|
||||
)
|
||||
(defun um-find-file-at-point ()
|
||||
"Return full file path of thing-at-point, falling back first to
|
||||
`project-root', then to `um-journal-path'."
|
||||
(let ((dirs (list
|
||||
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
|
||||
|
||||
;; TODO: unite these two routines. Consider removing the subdirectory recursion
|
||||
;; and searching all known projects.
|
||||
|
||||
;; 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
|
||||
;; `default-directory' to expand the filename.
|
||||
|
|
@ -106,7 +121,7 @@
|
|||
;; function in the same place in the list anyway.
|
||||
|
||||
;;;###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.
|
||||
|
||||
In this order:
|
||||
|
|
|
|||
Loading…
Reference in a new issue