]> git.eshelyaron.com Git - emacs.git/commitdiff
Narrow bookmark completions by bookmark type with 'C-x n m'
authorEshel Yaron <me@eshelyaron.com>
Mon, 25 Dec 2023 19:20:54 +0000 (20:20 +0100)
committerEshel Yaron <me@eshelyaron.com>
Mon, 25 Dec 2023 19:20:54 +0000 (20:20 +0100)
* lisp/bookmark.el (bookmark-narrow-completions-by-type): New
function.
(bookmark-completing-read): Use it as a 'narrow-completions-function'.
* doc/emacs/regs.texi (Bookmarks): Document it, as well as bookmark
types in general.
* etc/NEWS: Mention it.

doc/emacs/regs.texi
etc/NEWS
lisp/bookmark.el

index 20b39b8408f87dbd4287a4e22fa985fb3e2f22c6..9ced50a7e512d4663b782b86b720b3f5b6d9cafe 100644 (file)
@@ -428,3 +428,18 @@ points to.
 Insert in the buffer the @emph{contents} of the file that bookmark
 @var{bookmark} points to.
 @end table
+
+@cindex bookmark types
+@cindex types of bookmarks
+  Bookmarks can also record positions in buffers that are not visiting
+any file, such as web pages that you browse with EWW
+(@pxref{Top,EWW,,eww,}).  While jumping to a regular bookmark simply
+visits the corresponding file, different @dfn{bookmark types} can do
+different things when you jump to them.  As an example, EWW bookmarks
+store a URL instead of a file name, and jumping to such an EWW
+bookmark opens that URL in EWW.
+
+When you specify a bookmark name in the minibuffer in response to a
+prompt from one of the above commands, you can use @kbd{C-x n m} to
+restrict the list of bookmark completion candidates to only include
+bookmarks of a given type.
index ac7f8b12abb3ec76af15a56839818c98ba3957f1..afc59520dd4015325ffa351fb89e26437904562f 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -719,8 +719,15 @@ in the minibuffer history, with more recent candidates appearing first.
 *** New commands for narrowing (restricting) minibuffer completions list.
 You can now use 'C-x n n' ('minibuffer-narrow-completions-to-current')
 in the minibuffer to restrict the list of possible completions to only
-include candidates matching the current minibuffer input.  See the
-Info node "(emacs) Narrow Completions" for more information.
+include candidates matching the current minibuffer input.  New command
+'C-x n m' ('minibuffer-narrow-completions') is similar, but uses an
+alternative restriction method that depends on the relevant completion
+candidates.  For example, commands that read an Elisp symbol let you
+restrict the completions list with 'C-x n m' to only include symbols
+with a given property.  Similarly, commands that read a bookmark name
+let you restrict the completions list to only include bookmarks of a
+given type.  See the new Info node "(emacs) Narrow Completions" for
+more information.
 
 *** New minor mode 'completions-auto-update-mode'.
 This global minor mode automatically updates the *Completions* buffer
index 71d76cb4291c4ced2fc28f918e1b62ec6cdc1e9e..e0e880244730eeb52b24557899d2353251a71047 100644 (file)
@@ -561,11 +561,31 @@ If DEFAULT is nil then return empty string for empty input."
       (completing-read (format-prompt prompt default)
                        (lambda (string pred action)
                          (if (eq action 'metadata)
-                             '(metadata (category . bookmark))
+                             '(metadata
+                               (category . bookmark)
+                               (narrow-completions-function
+                                . bookmark-narrow-completions-by-type))
                              (complete-with-action
                               action bookmark-alist string pred)))
                        nil 0 nil 'bookmark-history default))))
 
+(defun bookmark-narrow-completions-by-type ()
+  "Restrict bookmark completions list to bookmarks of a given type."
+  (let* ((get-type
+          (lambda (bmk-record)
+            (let ((res (bookmark-type-from-full-record bmk-record)))
+              (if (and res (not (string-empty-p res))) res "Regular"))))
+         (types (delete-dups (mapcar get-type bookmark-alist)))
+         (type (completing-read "Keep bookmark completions of type: "
+                                types nil t)))
+    (cons (lambda (cand &rest _)
+            (let ((string (cond
+                           ((stringp cand)              cand)
+                           ((symbolp cand) (symbol-name cand))
+                           (t              (car         cand)))))
+              (string= type (funcall get-type (assoc string bookmark-alist)))))
+          (concat "bookmark type " (prin1-to-string
+                                    (substring-no-properties type))))))
 
 (defmacro bookmark-maybe-historicize-string (string)
   "Put STRING into the bookmark prompt history, if caller non-interactive.