branch: externals/bufferlo
commit 9a37df23e0b61fdc1ff9be4fabb28ddfd58005ac
Author: Stéphane Marks <[email protected]>
Commit: Flo Rommel <[email protected]>
Add functions to restore custom buffer bookmark properties
- New defcustoms 'bufferlo-bookmark-buffer-locals',
'bufferlo-bookmark-buffer-handler-functions'.
- New defuns 'bufferlo-bookmark-map-function-buffer-locals',
'bufferlo-bookmark-buffer-handler-buffer-locals',
'bufferlo-bookmark-map-function-text-scale-mode-amount',
'bufferlo-bookmark-buffer-handler-text-scale-mode-amount'.
- Update documentation.
---
README.org | 19 +++++++++++++
bufferlo.el | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+)
diff --git a/README.org b/README.org
index 9fc2f0f8c1..29bd6ce211 100644
--- a/README.org
+++ b/README.org
@@ -694,11 +694,30 @@ have ~tab-bar-tab-post-open-functions~, the bookmark
context will not
be available when those functions are called.
#+begin_src emacs-lisp
+ (add-hook 'bufferlo-bookmark-map-functions #'buffer-bookmark-store-fun)
+ (add-hook 'bufferlo-bookmark-buffer-handler-functions
#'buffer-bookmark-restore-fun)
(add-hook 'bufferlo-bookmark-tab-handler-functions #'tab-bookmark-fun)
(add-hook 'bufferlo-bookmark-frame-handler-functions #'frame-bookmark-fun)
(add-hook 'bufferlo-bookmark-set-handler-functions #'set-bookmark-fun)
#+end_src
+There are buffer-bookmark convenience functions you can use to persist
+text-scale-mode-amount, and persist buffer-local variables (though you should
+prefer
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/File-Variables.html][file-local
variables]], and directory variables
[[https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html][directory
variables]] and
+take care to store buffer local variables that you consider safe to restore,
+see
[[https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Local-Variables.html#index-safe_002dlocal_002dvariable_002dp][risky-local-variable-p]]).
+
+#+begin_src emacs-lisp
+ ;; To persist text-scale-mode-amount
+ (add-hook 'bufferlo-bookmark-map-functions
#'bufferlo-bookmark-map-function-text-scale-mode-amount)
+ (add-hook 'bufferlo-bookmark-buffer-handler-functions
#'bufferlo-bookmark-buffer-handler-text-scale-mode-amount)
+
+ ;; To persist buffer-local variables
+ (setq bufferlo-bookmark-buffer-locals '(my:special-local-1
my:special-local-2))
+ (add-hook 'bufferlo-bookmark-map-functions
#'bufferlo-bookmark-map-function-buffer-locals)
+ (add-hook 'bufferlo-bookmark-buffer-handler-functions
#'bufferlo-bookmark-buffer-handler-buffer-locals)
+#+end_src
+
*** Frame geometry options
Bufferlo provides wrappers around Emacs frame functions to provide
diff --git a/bufferlo.el b/bufferlo.el
index 78d42b7dd4..9c1c59c581 100644
--- a/bufferlo.el
+++ b/bufferlo.el
@@ -57,6 +57,7 @@
(require 'bookmark)
(require 'ibuffer)
(require 'ibuf-ext)
+(require 'face-remap)
(defgroup bufferlo nil
"Manage frame/tab-local buffer lists."
@@ -191,6 +192,26 @@ This overrides buffers excluded by
`bufferlo-bookmark-buffers-exclude-filters'."
:package-version '(bufferlo . "1.1")
:type '(repeat regexp))
+(defcustom bufferlo-bookmark-buffer-locals '()
+ "List of buffer-local variables to store/restore in/from buffer bookmarks.
+This is a list of symbols; e.g., \\='(my:special-local my:special-local-2).
+
+To use this, add the function
+`bufferlo-bookmark-map-function-buffer-locals` to
+`bufferlo-bookmark-map-functions` to store buffer locals, and its
+corollary function `bufferlo-bookmark-buffer-handler-buffer-locals`
+`bufferlo-bookmark-buffer-handler-functions` to restore them when a
+bufferlo buffer bookmark is restored.
+
+Note: Local file variables and directory variables are the preferred
+methods for managing buffer-local variables. Use this facility only
+when necessary.
+
+See Info nodes `(elisp) File Local Variables' and `(elisp) Directory
+Local Variables'."
+ :package-version '(bufferlo . "1.3")
+ :type '(repeat symbol))
+
(defcustom bufferlo-bookmark-frame-load-make-frame nil
"Frame bookmark loading frame and geometry policy.
If nil, reuse the existing frame.
@@ -637,6 +658,17 @@ frame bookmark is a collection of tab bookmarks."
:package-version '(bufferlo . "0.8")
:type 'hook)
+(defcustom bufferlo-bookmark-buffer-handler-functions nil
+ "Abnormal hooks to call after a bookmark buffer is handled.
+This is the corollary to `bufferlo-bookmark-map-functions` to process
+extra properties added to a buffer bookmark.
+
+Each function is called with the restored buffer current, and with the
+following arguments:
+ bookmark-record: See `bookmark-make-record-default`."
+ :package-version '(bufferlo . "1.3")
+ :type 'hook)
+
(defcustom bufferlo-bookmark-tab-handler-functions nil
"Abnormal hooks to call after a bookmark tab is handled.
Each function takes the following arguments:
@@ -2501,6 +2533,62 @@ empty (no-op) display-func."
err bookmark)))
nil)))
+(defun bufferlo-bookmark-map-function-buffer-locals (bookmark-record)
+ "Store buffer local variables in BOOKMARK-RECORD.
+Add this function to `bufferlo-bookmark-map-functions`, and add a list
+of symbols representing the variables you want to store in
+`bufferlo-buffer-locals`.
+
+These are restored upon buffer bookmark loading if
+`bufferlo-bookmark-buffer-handler-buffer-locals` is a member of
+`bufferlo-bookmark-buffer-handler-functions`.
+
+Note: Take care to store buffer local variables that you consider safe
+to restore, which see `risky-local-variable-p`."
+ (when-let* ((buffer-locals
+ (seq-filter
+ (lambda (x) (not (null x)))
+ (mapcar (lambda (sym)
+ (when (local-variable-p sym)
+ (cons sym (symbol-value sym))))
+ bufferlo-bookmark-buffer-locals))))
+ (bookmark-prop-set bookmark-record
+ 'bufferlo-buffer-locals
+ buffer-locals))
+ bookmark-record)
+
+(defun bufferlo-bookmark-buffer-handler-buffer-locals (bookmark-record)
+ "Restore buffer local variables from BOOKMARK-RECORD.
+See `bufferlo-bookmark-map-function-buffer-locals`."
+ (when-let*
+ ((buffer-locals
+ (bookmark-prop-get bookmark-record
+ 'bufferlo-buffer-locals)))
+ (mapc (lambda (pair)
+ (make-local-variable (car pair))
+ (set (car pair) (cdr pair)))
+ buffer-locals)))
+
+(defun bufferlo-bookmark-map-function-text-scale-mode-amount (bookmark-record)
+ "Store the buffer local `text-scale-mode-amount` in BOOKMARK-RECORD.
+Add this function to `bufferlo-bookmark-map-functions`, and its
+corollary `bufferlo-bookmark-buffer-handler-text-scale-mode-amount`
+to`bufferlo-bookmark-buffer-handler-functions`."
+ (when text-scale-mode-amount
+ (bookmark-prop-set bookmark-record
+ 'bufferlo-text-scale-mode-amount
+ text-scale-mode-amount))
+ bookmark-record)
+
+(defun bufferlo-bookmark-buffer-handler-text-scale-mode-amount
(bookmark-record)
+ "Restore `text-scale-mode-amount` from BOOKMARK-RECORD.
+See `bufferlo-bookmark-map-function-text-scale-mode-amount`."
+ (when-let*
+ ((val
+ (bookmark-prop-get bookmark-record
+ 'bufferlo-text-scale-mode-amount)))
+ (text-scale-set val)))
+
(defun bufferlo--bookmark-get-for-buffer (buffer)
"Get `buffer-name' and bookmark for BUFFER."
(with-current-buffer buffer
@@ -2902,6 +2990,9 @@ Returns nil on success, non-nil on abort."
(funcall (or (bookmark-get-handler
record)
'bookmark-default-handler)
record)
+ (run-hook-with-args
+
'bufferlo-bookmark-buffer-handler-functions
+ record)
(run-hooks 'bookmark-after-jump-hook)
nil)
(error