fix: completing-read for project bookmarks, no alist contamination

This commit is contained in:
2026-07-17 16:52:35 -04:00
parent 48d04ba73a
commit 994216e32a
2 changed files with 26 additions and 74 deletions

View File

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

View File

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