diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-25 08:00:20 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-25 08:00:20 -0500 |
| commit | 92eaba30ae35727509f1d9d6dcc467c248f627dc (patch) | |
| tree | 941a9dfc2229185df5a8962da96e3473a99f4a1e | |
| parent | 3f75b39bbbc4e1c136d3f786024c5c1ed19011ce (diff) | |
| download | dotemacs-92eaba30ae35727509f1d9d6dcc467c248f627dc.tar.gz dotemacs-92eaba30ae35727509f1d9d6dcc467c248f627dc.zip | |
fix(prog-general): open the project todo in the other window when split
C-c p t (cj/open-project-root-todo) called find-file, which always opened todo.org in the selected window, replacing whatever I was looking at. Now it opens in the other window when the frame is split and in the current window when it isn't, through a small cj/--find-file-respecting-split helper. The helper is a top-level defun rather than buried in the projectile :config block so it can be unit-tested without loading projectile.
I left cj/project-switch-actions alone. Opening the todo on a project switch is a different trigger and not what this fixes.
| -rw-r--r-- | modules/prog-general.el | 11 | ||||
| -rw-r--r-- | tests/test-prog-general--find-file-respecting-split.el | 63 |
2 files changed, 73 insertions, 1 deletions
diff --git a/modules/prog-general.el b/modules/prog-general.el index dedf8a80..93a3a95c 100644 --- a/modules/prog-general.el +++ b/modules/prog-general.el @@ -137,6 +137,15 @@ (unless (file-exists-p projectile-bookmark-file) (run-at-time "3" nil 'projectile-discover-projects-in-search-path)))) +(defun cj/--find-file-respecting-split (file) + "Open FILE in another window when the frame is split, else in this window. +\"Split\" means the selected window is not the only one in its frame, so +opening the file lands it in the other pane rather than replacing the +buffer the user is looking at." + (if (one-window-p) + (find-file file) + (find-file-other-window file))) + (use-package projectile :bind-keymap ("C-c p" . projectile-command-map) @@ -171,7 +180,7 @@ If no such file exists there, display a message." (if-let ((root (projectile-project-root))) (let ((file (cj/find-project-root-file "^todo\\.org$"))) (if file - (find-file (expand-file-name file root)) + (cj/--find-file-respecting-split (expand-file-name file root)) (message "No todo.org in project root: %s" root))) (message "Not in a Projectile project"))) diff --git a/tests/test-prog-general--find-file-respecting-split.el b/tests/test-prog-general--find-file-respecting-split.el new file mode 100644 index 00000000..6d45c51c --- /dev/null +++ b/tests/test-prog-general--find-file-respecting-split.el @@ -0,0 +1,63 @@ +;;; test-prog-general--find-file-respecting-split.el --- split-aware file open -*- lexical-binding: t; -*- + +;;; Commentary: +;; `cj/--find-file-respecting-split' opens a file in another window when the +;; frame is split and in the current window otherwise. It backs +;; `cj/open-project-root-todo' (C-c p t) so the project todo lands in the +;; other pane instead of replacing the buffer in the selected window. +;; +;; `find-file' / `find-file-other-window' are stubbed -- they are the file-I/O +;; boundary -- while the window layout is real. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'prog-general) + +(ert-deftest test-prog-general--find-file-respecting-split-single-window-uses-current () + "Normal: a single-window frame opens the file in the current window." + (save-window-excursion + (delete-other-windows) + (let (current-arg other-called) + (cl-letf (((symbol-function 'find-file) + (lambda (f) (setq current-arg f))) + ((symbol-function 'find-file-other-window) + (lambda (_f) (setq other-called t)))) + (cj/--find-file-respecting-split "/tmp/proj/todo.org")) + (should (equal current-arg "/tmp/proj/todo.org")) + (should-not other-called)))) + +(ert-deftest test-prog-general--find-file-respecting-split-two-windows-uses-other () + "Normal: a two-window split opens the file in the other window." + (save-window-excursion + (delete-other-windows) + (split-window-right) + (let (other-arg current-called) + (cl-letf (((symbol-function 'find-file-other-window) + (lambda (f) (setq other-arg f))) + ((symbol-function 'find-file) + (lambda (_f) (setq current-called t)))) + (cj/--find-file-respecting-split "/tmp/proj/todo.org")) + (should (equal other-arg "/tmp/proj/todo.org")) + (should-not current-called)))) + +(ert-deftest test-prog-general--find-file-respecting-split-three-windows-uses-other () + "Boundary: more than two windows still counts as split -> other window." + (save-window-excursion + (delete-other-windows) + (split-window-right) + (split-window-below) + (let (other-called current-called) + (cl-letf (((symbol-function 'find-file-other-window) + (lambda (_f) (setq other-called t))) + ((symbol-function 'find-file) + (lambda (_f) (setq current-called t)))) + (cj/--find-file-respecting-split "/tmp/proj/todo.org")) + (should other-called) + (should-not current-called)))) + +(provide 'test-prog-general--find-file-respecting-split) +;;; test-prog-general--find-file-respecting-split.el ends here |
