From 8e70024cae3f4569d6a0c86a0e4d8079126fe9e5 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Fri, 4 Nov 2022 14:52:58 -0500 Subject: [PATCH] * lisp/org.el: (org-get-indirect-buffer) Allow indirect base buffers Previously, calling this function on an indirect buffer would fail, preventing the user from making a new indirect buffer based on an indirect buffer (e.g. imagine the user makes an indirect buffer for a large subtree, then wants to make another one for a subtree of that). Now, the base buffer of the buffer is used, when applicable. Also, the function is partially rewritten to be clearer, and a docstring and a FIXME are added. --- lisp/org.el | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index d8708f8f2..3b67040f7 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -6160,25 +6160,32 @@ frame is not changed." (run-hook-with-args 'org-cycle-hook 'all) (and (window-live-p cwin) (select-window cwin)))) -(defun org-get-indirect-buffer (&optional buffer heading) - (setq buffer (or buffer (current-buffer))) - (let ((n 1) (base (buffer-name buffer)) bname) - (while (buffer-live-p - (get-buffer - (setq bname - (concat base "-" - (if heading (concat heading "-" (number-to-string n)) - (number-to-string n)))))) - (setq n (1+ n))) +(cl-defun org-get-indirect-buffer (&optional (buffer (current-buffer)) heading) + "Return an indirect buffer based on BUFFER. +If HEADING, prepend it to the name of the new buffer." + (let* ((base-buffer (or (buffer-base-buffer buffer) buffer)) + (suffix-prefix (if heading + (concat heading "-") + "")) + (buffer-name (cl-loop for n from 1 to 100 + for suffix = (format "%s%s" suffix-prefix n) + for name = (format "%s-%s" + (buffer-name base-buffer) + suffix) + while (buffer-live-p (get-buffer name)) + finally return name))) (condition-case nil - (let ((indirect-buffer (make-indirect-buffer buffer bname 'clone))) + (let ((indirect-buffer (make-indirect-buffer base-buffer buffer-name 'clone))) ;; Decouple folding state. We need to do it manually since ;; `make-indirect-buffer' does not run ;; `clone-indirect-buffer-hook'. (org-fold-core-decouple-indirect-buffer-folds) ;; Return the buffer. indirect-buffer) - (error (make-indirect-buffer buffer bname))))) + ;; FIXME: Explain why this `condition-case' is necessary. Why + ;; could an error be signaled with the CLONE argument non-nil, + ;; and why would trying again without CLONE solve the problem? + (error (make-indirect-buffer base-buffer buffer-name))))) (defun org-set-frame-title (title) "Set the title of the current frame to the string TITLE." -- 2.34.0