aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2024-11-03 16:39:45 +1100
committerYuchen Pei <id@ypei.org>2024-11-03 16:39:45 +1100
commit3755a65776e7f2bd921152dafb289365b31f8b9c (patch)
tree3ec7dc4d956d6ba9f66a849a244f7e0364cd3ed9
parenta6a381db15ad5a2134746c92c5847c2808dbfc21 (diff)
[emacs] Add org narrow or widen to focus write
-rw-r--r--emacs/.emacs.d/init/ycp-buffer.el2
-rw-r--r--emacs/.emacs.d/lisp/my/my-buffer.el22
2 files changed, 18 insertions, 6 deletions
diff --git a/emacs/.emacs.d/init/ycp-buffer.el b/emacs/.emacs.d/init/ycp-buffer.el
index 7e1128a..944a45e 100644
--- a/emacs/.emacs.d/init/ycp-buffer.el
+++ b/emacs/.emacs.d/init/ycp-buffer.el
@@ -225,7 +225,7 @@
(my-package my-buffer
(:delay 10)
(my-keybind global-map
- "<f1>" #'my-focus-write
+ "<f1>" #'my-toggle-focus-write
"<insert>" #'my-cycle-windows
"C-M-<mouse-4>" #'my-increase-default-face-height
"C-M-<mouse-5>" #'my-decrease-default-face-height)
diff --git a/emacs/.emacs.d/lisp/my/my-buffer.el b/emacs/.emacs.d/lisp/my/my-buffer.el
index 21401b5..f06956f 100644
--- a/emacs/.emacs.d/lisp/my/my-buffer.el
+++ b/emacs/.emacs.d/lisp/my/my-buffer.el
@@ -239,12 +239,24 @@ that point."
(setq buffer temp-buffer))
(set-window-buffer first-window buffer)))
-(defun my-focus-write ()
- "Make the current window the only one centered with width 80."
+(defun my-toggle-focus-write ()
+ "Toggle focus write mode.
+
+Focus write: make the current window the only one centered with
+width 80. If in org-mode, also narrow to current subtree."
(interactive)
- (delete-other-windows)
- (let ((margin (/ (- (window-width) 80) 2)))
- (set-window-margins nil margin margin)))
+ ;; Only one window in the current frame indicates we are in focus
+ ;; write mode.
+ (if (length= (window-list) 1)
+ (progn
+ (winner-undo)
+ (when (derived-mode-p 'org-mode)
+ (widen)))
+ (when (derived-mode-p 'org-mode)
+ (org-narrow-to-subtree))
+ (delete-other-windows)
+ (let ((margin (/ (- (window-width) 80) 2)))
+ (set-window-margins nil margin margin))))
(defun my-select-new-window-matching-mode (mode)
"Select a new window."