From aa72245a2a1715ef4fb8b1c3019826540320be80 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 10 May 2026 14:20:45 -0500 Subject: 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. --- modules/system-lib.el | 16 ++++++ modules/system-utils.el | 18 ++----- tests/test-system-lib-file-from-context.el | 76 +++++++++++++++++++++++++++ tests/test-system-utils--file-from-context.el | 76 --------------------------- 4 files changed, 96 insertions(+), 90 deletions(-) create mode 100644 tests/test-system-lib-file-from-context.el delete mode 100644 tests/test-system-utils--file-from-context.el 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." diff --git a/modules/system-utils.el b/modules/system-utils.el index 9a81c402..e266cd15 100644 --- a/modules/system-utils.el +++ b/modules/system-utils.el @@ -23,6 +23,8 @@ ;; ;;; Code: +(require 'system-lib) + (declare-function dired-get-file-for-visit "dired" ()) (declare-function dired-file-name-at-point "dired" ()) (declare-function env-linux-p "host-environment" ()) @@ -55,18 +57,6 @@ ;;; ------------------------------- Open File With ------------------------------ ;; TASK: Favor this method over cj/open-this-file-with and add to custom buffer funcs -(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." - (or explicit-filename - buffer-file-name - (and (derived-mode-p 'dired-mode) - (dired-file-name-at-point)))) - (defun cj/--open-with-is-launcher-p (command) "Return non-nil if COMMAND is a desktop launcher. Launchers (xdg-open, open, start) need to be called with `call-process' @@ -80,7 +70,7 @@ Works in both Dired buffers and regular file buffers. Prompts for a file only when neither context yields one. The command runs fully detached from Emacs." (interactive "MOpen with command: ") - (let* ((file (or (cj/--file-from-context) + (let* ((file (or (cj/file-from-context) (read-file-name "File to open: ")))) (unless (and file (file-exists-p file)) (error "No valid file found or selected")) @@ -114,7 +104,7 @@ Signals an error if the host is unsupported." Logs output and exit code to buffer *external-open.log*." (interactive) (let* ((file (expand-file-name - (or (cj/--file-from-context filename) + (or (cj/file-from-context filename) (user-error "No file associated with this buffer")))) (cmd (cj/identify-external-open-command)) (logbuf (get-buffer-create "*external-open.log*"))) diff --git a/tests/test-system-lib-file-from-context.el b/tests/test-system-lib-file-from-context.el new file mode 100644 index 00000000..050535c2 --- /dev/null +++ b/tests/test-system-lib-file-from-context.el @@ -0,0 +1,76 @@ +;;; test-system-lib-file-from-context.el --- Tests for cj/file-from-context -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for `cj/file-from-context' in system-lib.el. The helper +;; returns a file path from the current context, resolving in priority +;; order: explicit argument, `buffer-file-name', dired file at point. +;; Returns nil when none of these yield a file. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'system-lib) + +(defmacro test-ffc--with-context (buffer-file in-dired dired-file &rest body) + "Run BODY with context stubbed for testing `cj/file-from-context'. +BUFFER-FILE becomes the value of `buffer-file-name'. +IN-DIRED controls `derived-mode-p' (t to simulate dired-mode). +DIRED-FILE becomes the return value of `dired-file-name-at-point'." + (declare (indent 3)) + `(let ((buffer-file-name ,buffer-file)) + (cl-letf (((symbol-function 'derived-mode-p) + (lambda (&rest modes) + (and ,in-dired (memq 'dired-mode modes)))) + ((symbol-function 'dired-file-name-at-point) + (lambda () ,dired-file))) + ,@body))) + +;;; Normal cases + +(ert-deftest test-ffc-explicit-wins-over-buffer-file () + "Normal: an explicit filename argument wins over `buffer-file-name'." + (test-ffc--with-context "/from-buffer.el" nil nil + (should (string= "/explicit.el" + (cj/file-from-context "/explicit.el"))))) + +(ert-deftest test-ffc-explicit-wins-over-dired () + "Normal: an explicit filename argument wins over dired file at point." + (test-ffc--with-context nil t "/from-dired.el" + (should (string= "/explicit.el" + (cj/file-from-context "/explicit.el"))))) + +(ert-deftest test-ffc-buffer-file-used-when-no-explicit () + "Normal: falls back to `buffer-file-name' when no explicit arg." + (test-ffc--with-context "/from-buffer.el" nil nil + (should (string= "/from-buffer.el" + (cj/file-from-context))))) + +(ert-deftest test-ffc-dired-used-when-no-explicit-no-buffer-file () + "Normal: in a dired buffer, falls back to dired file at point." + (test-ffc--with-context nil t "/from-dired.el" + (should (string= "/from-dired.el" + (cj/file-from-context))))) + +;;; Boundary cases + +(ert-deftest test-ffc-all-sources-nil-returns-nil () + "Boundary: no explicit, no buffer-file, not in dired -> nil." + (test-ffc--with-context nil nil nil + (should-not (cj/file-from-context)))) + +(ert-deftest test-ffc-explicit-nil-uses-fallback-chain () + "Boundary: explicitly passing nil as the arg still uses the fallback chain." + (test-ffc--with-context "/from-buffer.el" nil nil + (should (string= "/from-buffer.el" + (cj/file-from-context nil))))) + +(ert-deftest test-ffc-dired-mode-but-no-file-at-point () + "Boundary: in dired but nothing at point returns nil (buffer-file also nil)." + (test-ffc--with-context nil t nil + (should-not (cj/file-from-context)))) + +(provide 'test-system-lib-file-from-context) +;;; test-system-lib-file-from-context.el ends here diff --git a/tests/test-system-utils--file-from-context.el b/tests/test-system-utils--file-from-context.el deleted file mode 100644 index af4b0f82..00000000 --- a/tests/test-system-utils--file-from-context.el +++ /dev/null @@ -1,76 +0,0 @@ -;;; test-system-utils--file-from-context.el --- Tests for cj/--file-from-context -*- lexical-binding: t; -*- - -;;; Commentary: -;; Unit tests for `cj/--file-from-context' in system-utils.el. The -;; helper returns a file path from the current context, resolving in -;; priority order: explicit argument, `buffer-file-name', dired file -;; at point. Returns nil when none of these yield a file. - -;;; Code: - -(require 'ert) -(require 'cl-lib) - -(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) -(require 'system-utils) - -(defmacro test-ffc--with-context (buffer-file in-dired dired-file &rest body) - "Run BODY with context stubbed for testing `cj/--file-from-context'. -BUFFER-FILE becomes the value of `buffer-file-name'. -IN-DIRED controls `derived-mode-p' (t to simulate dired-mode). -DIRED-FILE becomes the return value of `dired-file-name-at-point'." - (declare (indent 3)) - `(let ((buffer-file-name ,buffer-file)) - (cl-letf (((symbol-function 'derived-mode-p) - (lambda (&rest modes) - (and ,in-dired (memq 'dired-mode modes)))) - ((symbol-function 'dired-file-name-at-point) - (lambda () ,dired-file))) - ,@body))) - -;;; Normal cases - -(ert-deftest test-ffc-explicit-wins-over-buffer-file () - "Normal: an explicit filename argument wins over `buffer-file-name'." - (test-ffc--with-context "/from-buffer.el" nil nil - (should (string= "/explicit.el" - (cj/--file-from-context "/explicit.el"))))) - -(ert-deftest test-ffc-explicit-wins-over-dired () - "Normal: an explicit filename argument wins over dired file at point." - (test-ffc--with-context nil t "/from-dired.el" - (should (string= "/explicit.el" - (cj/--file-from-context "/explicit.el"))))) - -(ert-deftest test-ffc-buffer-file-used-when-no-explicit () - "Normal: falls back to `buffer-file-name' when no explicit arg." - (test-ffc--with-context "/from-buffer.el" nil nil - (should (string= "/from-buffer.el" - (cj/--file-from-context))))) - -(ert-deftest test-ffc-dired-used-when-no-explicit-no-buffer-file () - "Normal: in a dired buffer, falls back to dired file at point." - (test-ffc--with-context nil t "/from-dired.el" - (should (string= "/from-dired.el" - (cj/--file-from-context))))) - -;;; Boundary cases - -(ert-deftest test-ffc-all-sources-nil-returns-nil () - "Boundary: no explicit, no buffer-file, not in dired → nil." - (test-ffc--with-context nil nil nil - (should-not (cj/--file-from-context)))) - -(ert-deftest test-ffc-explicit-nil-uses-fallback-chain () - "Boundary: explicitly passing nil as the arg still uses the fallback chain." - (test-ffc--with-context "/from-buffer.el" nil nil - (should (string= "/from-buffer.el" - (cj/--file-from-context nil))))) - -(ert-deftest test-ffc-dired-mode-but-no-file-at-point () - "Boundary: in dired but nothing at point returns nil (buffer-file also nil)." - (test-ffc--with-context nil t nil - (should-not (cj/--file-from-context)))) - -(provide 'test-system-utils--file-from-context) -;;; test-system-utils--file-from-context.el ends here -- cgit v1.2.3