diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-10 14:20:45 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-10 14:20:45 -0500 |
| commit | aa72245a2a1715ef4fb8b1c3019826540320be80 (patch) | |
| tree | bf339d2ff0385fc524e4f4111ceeb21ce380fe42 /modules/system-lib.el | |
| parent | 57e558ce13fae77eed07095638a753ba75af55d4 (diff) | |
| download | dotemacs-aa72245a2a1715ef4fb8b1c3019826540320be80.tar.gz dotemacs-aa72245a2a1715ef4fb8b1c3019826540320be80.zip | |
refactor(system-lib): extract cj/file-from-context from system-utils
Phase 2.4 of utility-consolidation, the last item in the spec's recommended order. `cj/--file-from-context' resolves "the current file" via a three-step fallback chain (explicit arg, `buffer-file-name', dired file at point) -- a useful pattern for any command that operates on the current file regardless of which kind of buffer the user is in. Promote to public `cj/file-from-context' and re-home in system-lib.el so other modules (mail capture, external-open, AI conversation, dirvish helpers) can use it without an awkward dependency on system-utils.
Migrate the two callers in system-utils.el (`cj/open-this-file-with' and `cj/open-file-with-command') and add `(require \='system-lib)' there per the Phase 2 exit criterion.
Move the existing 7-test file to `tests/test-system-lib-file-from-context.el' and update its references to the new public name. The test shape is unchanged: 4 Normal + 3 Boundary cases covering explicit-arg precedence, buffer-file-name fallback, dired fallback, and the all-nil case.
Diffstat (limited to 'modules/system-lib.el')
| -rw-r--r-- | modules/system-lib.el | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/modules/system-lib.el b/modules/system-lib.el index 3ccec06b..96159179 100644 --- a/modules/system-lib.el +++ b/modules/system-lib.el @@ -80,6 +80,22 @@ Thin wrapper around `cj/process-output-or-error' with `git' as the program." (apply #'cj/process-output-or-error "git" args)) +(defun cj/file-from-context (&optional explicit-filename) + "Return a file path from the current context, or nil. + +Resolves in priority order: + 1. EXPLICIT-FILENAME, if non-nil. + 2. `buffer-file-name' of the current buffer. + 3. The file at point if the current buffer is in `dired-mode'. + +Returns nil when none of these yield a file. Useful for any command +that operates on \"the current file\" -- buffer commands, dired +commands, and external-open dispatchers all want this resolution." + (or explicit-filename + buffer-file-name + (and (derived-mode-p 'dired-mode) + (dired-file-name-at-point)))) + (defun cj/log-silently (format-string &rest args) "Append formatted message (FORMAT-STRING with ARGS) to *Messages* buffer. This does so without echoing in the minibuffer." |
