From 3755a65776e7f2bd921152dafb289365b31f8b9c Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sun, 3 Nov 2024 16:39:45 +1100 Subject: [emacs] Add org narrow or widen to focus write --- emacs/.emacs.d/init/ycp-buffer.el | 2 +- emacs/.emacs.d/lisp/my/my-buffer.el | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'emacs/.emacs.d') 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 - "" #'my-focus-write + "" #'my-toggle-focus-write "" #'my-cycle-windows "C-M-" #'my-increase-default-face-height "C-M-" #'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." -- cgit v1.2.3