fix: bookmark-bmenu-list with file-switch, restore on quit

This commit is contained in:
2026-07-17 16:56:20 -04:00
parent 994216e32a
commit 84006b4412
2 changed files with 32 additions and 26 deletions

View File

@@ -3683,24 +3683,27 @@ Project bookmarks go to .bookmarks file in project root."
;; Bindings
(defun gortium/list-project-bookmarks ()
"Pick and jump to a project bookmark via completing-read.
Never touches global bookmark-alist."
"Show project bookmark menu. Switches to .bookmarks, restores globals on quit."
(interactive)
(if (not (projectile-project-p))
(user-error "Not in a project")
(let* ((proj-file (expand-file-name ".bookmarks" (projectile-project-root)))
(proj-bms (and (file-exists-p proj-file)
(with-temp-buffer
(insert-file-contents proj-file)
(ignore-errors (read (current-buffer)))))))
(if (not proj-bms)
(saved-alist (copy-tree bookmark-alist))
(saved-file bookmark-default-file))
(if (not (file-exists-p proj-file))
(message "No .bookmarks file in this project.")
(let* ((names (mapcar (lambda (b) (if (consp b) (car b) b)) proj-bms))
(choice (completing-read "Project bookmark: " names nil t)))
(when choice
;; Temporarily set bookmark-alist to project bookmarks for the jump
(let ((bookmark-alist proj-bms))
(bookmark-jump choice))))))))
;; Switch to project bookmarks
(setq bookmark-default-file proj-file)
(bookmark-load proj-file t)
(bookmark-bmenu-list)
;; Restore on quit
(with-current-buffer "*Bookmark List*"
(add-hook 'kill-buffer-hook
(lambda ()
(ignore-errors (bookmark-save))
(setq bookmark-default-file saved-file)
(setq bookmark-alist saved-alist))
nil t))))))
(map! :leader
:desc "Set bookmark (global/project)"

View File

@@ -2753,24 +2753,27 @@ Project bookmarks go to .bookmarks file in project root."
;; Bindings
(defun gortium/list-project-bookmarks ()
"Pick and jump to a project bookmark via completing-read.
Never touches global bookmark-alist."
"Show project bookmark menu. Switches to .bookmarks, restores globals on quit."
(interactive)
(if (not (projectile-project-p))
(user-error "Not in a project")
(let* ((proj-file (expand-file-name ".bookmarks" (projectile-project-root)))
(proj-bms (and (file-exists-p proj-file)
(with-temp-buffer
(insert-file-contents proj-file)
(ignore-errors (read (current-buffer)))))))
(if (not proj-bms)
(saved-alist (copy-tree bookmark-alist))
(saved-file bookmark-default-file))
(if (not (file-exists-p proj-file))
(message "No .bookmarks file in this project.")
(let* ((names (mapcar (lambda (b) (if (consp b) (car b) b)) proj-bms))
(choice (completing-read "Project bookmark: " names nil t)))
(when choice
;; Temporarily set bookmark-alist to project bookmarks for the jump
(let ((bookmark-alist proj-bms))
(bookmark-jump choice))))))))
;; Switch to project bookmarks
(setq bookmark-default-file proj-file)
(bookmark-load proj-file t)
(bookmark-bmenu-list)
;; Restore on quit
(with-current-buffer "*Bookmark List*"
(add-hook 'kill-buffer-hook
(lambda ()
(ignore-errors (bookmark-save))
(setq bookmark-default-file saved-file)
(setq bookmark-alist saved-alist))
nil t))))))
(map! :leader
:desc "Set bookmark (global/project)"