]> code.delx.au - gnu-emacs/blob - lisp/bookmark.el
* lisp/descr-text.el (describe-char-unicode-data): Fix copy/paste errors.
[gnu-emacs] / lisp / bookmark.el
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
2
3 ;; Copyright (C) 1993-1997, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: Karl Fogel <kfogel@red-bean.com>
6 ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
7 ;; Created: July, 1993
8 ;; Keywords: bookmarks, placeholders, annotations
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package is for setting "bookmarks" in files. A bookmark
28 ;; associates a string with a location in a certain file. Thus, you
29 ;; can navigate your way to that location by providing the string.
30 ;; See the "User Variables" section for customizations.
31
32 \f
33 ;;; Code:
34
35 (require 'pp)
36 (eval-when-compile (require 'cl-lib))
37
38 ;;; Misc comments:
39 ;;
40 ;; If variable bookmark-use-annotations is non-nil, an annotation is
41 ;; queried for when setting a bookmark.
42 ;;
43 ;; The bookmark list is sorted lexically by default, but you can turn
44 ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
45 ;; the list will be presented in the order it is recorded
46 ;; (chronologically), which is actually fairly useful as well.
47
48 ;;; User Variables
49
50 (defgroup bookmark nil
51 "Setting, annotation and jumping to bookmarks."
52 :group 'matching)
53
54
55 (defcustom bookmark-use-annotations nil
56 "If non-nil, saving a bookmark queries for an annotation in a buffer."
57 :type 'boolean
58 :group 'bookmark)
59
60
61 (defcustom bookmark-save-flag t
62 "Controls when Emacs saves bookmarks to a file.
63 --> nil means never save bookmarks, except when `bookmark-save' is
64 explicitly called (\\[bookmark-save]).
65 --> t means save bookmarks when Emacs is killed.
66 --> Otherwise, it should be a number that is the frequency with which
67 the bookmark list is saved (i.e.: the number of times which
68 Emacs's bookmark list may be modified before it is automatically
69 saved.). If it is a number, Emacs will also automatically save
70 bookmarks when it is killed.
71
72 Therefore, the way to get it to save every time you make or delete a
73 bookmark is to set this variable to 1 (or 0, which produces the same
74 behavior.)
75
76 To specify the file in which to save them, modify the variable
77 `bookmark-default-file'."
78 :type '(choice (const nil) integer (other t))
79 :group 'bookmark)
80
81
82 (defconst bookmark-old-default-file "~/.emacs-bkmrks"
83 "The `.emacs.bmk' file used to be called this name.")
84
85
86 ;; defvared to avoid a compilation warning:
87 (defvar bookmark-file nil
88 "Old name for `bookmark-default-file'.")
89
90 (defcustom bookmark-default-file
91 (if bookmark-file
92 ;; In case user set `bookmark-file' in her .emacs:
93 bookmark-file
94 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
95 "File in which to save bookmarks by default."
96 :type 'file
97 :group 'bookmark)
98
99
100 (defcustom bookmark-version-control 'nospecial
101 "Whether or not to make numbered backups of the bookmark file.
102 It can have four values: t, nil, `never', or `nospecial'.
103 The first three have the same meaning that they do for the
104 variable `version-control'; the value `nospecial' (the default) means
105 just use the value of `version-control'."
106 :type '(choice (const :tag "If existing" nil)
107 (const :tag "Never" never)
108 (const :tag "Use value of option `version-control'" nospecial)
109 (other :tag "Always" t))
110 :group 'bookmark)
111
112
113 (defcustom bookmark-completion-ignore-case t
114 "Non-nil means bookmark functions ignore case in completion."
115 :type 'boolean
116 :group 'bookmark)
117
118
119 (defcustom bookmark-sort-flag t
120 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
121 Otherwise they will be displayed in LIFO order (that is, most
122 recently set ones come first, oldest ones come last)."
123 :type 'boolean
124 :group 'bookmark)
125
126
127 (defcustom bookmark-automatically-show-annotations t
128 "Non-nil means show annotations when jumping to a bookmark."
129 :type 'boolean
130 :group 'bookmark)
131
132 (defcustom bookmark-bmenu-use-header-line t
133 "Non-nil means to use an immovable header line.
134 This is as opposed to inline text at the top of the buffer."
135 :version "24.4"
136 :type 'boolean
137 :group 'bookmark)
138
139 (defconst bookmark-bmenu-inline-header-height 2
140 "Number of lines used for the *Bookmark List* header
141 \(only significant when `bookmark-bmenu-use-header-line' is nil).")
142
143 (defconst bookmark-bmenu-marks-width 2
144 "Number of columns (chars) used for the *Bookmark List* marks column,
145 including the annotations column.")
146
147 (defcustom bookmark-bmenu-file-column 30
148 "Column at which to display filenames in a buffer listing bookmarks.
149 You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
150 :type 'integer
151 :group 'bookmark)
152
153
154 (defcustom bookmark-bmenu-toggle-filenames t
155 "Non-nil means show filenames when listing bookmarks.
156 A non-nil value may result in truncated bookmark names."
157 :type 'boolean
158 :group 'bookmark)
159
160 (defface bookmark-menu-bookmark
161 '((t (:weight bold)))
162 "Face used to highlight bookmark names in bookmark menu buffers."
163 :group 'bookmark)
164
165 (defcustom bookmark-menu-length 70
166 "Maximum length of a bookmark name displayed on a popup menu."
167 :type 'integer
168 :group 'bookmark)
169
170 ;; FIXME: Is it really worth a customization option?
171 (defcustom bookmark-search-delay 0.2
172 "Time before `bookmark-bmenu-search' updates the display."
173 :group 'bookmark
174 :type 'number)
175
176 (defface bookmark-menu-heading
177 '((t (:inherit font-lock-type-face)))
178 "Face used to highlight the heading in bookmark menu buffers."
179 :group 'bookmark
180 :version "22.1")
181
182
183 ;;; No user-serviceable parts beyond this point.
184
185 ;; Added for lucid emacs compatibility, db
186 (or (fboundp 'defalias) (fset 'defalias 'fset))
187
188 ;; suggested for lucid compatibility by david hughes:
189 (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
190
191 \f
192 ;;; Keymap stuff:
193
194 ;; Set up these bindings dumping time *only*;
195 ;; if the user alters them, don't override the user when loading bookmark.el.
196
197 ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
198 ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
199 ;;;###autoload (define-key ctl-x-r-map "M" 'bookmark-set-no-overwrite)
200 ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
201
202 ;;;###autoload
203 (defvar bookmark-map
204 (let ((map (make-sparse-keymap)))
205 ;; Read the help on all of these functions for details...
206 (define-key map "x" 'bookmark-set)
207 (define-key map "m" 'bookmark-set) ;"m"ark
208 (define-key map "M" 'bookmark-set-no-overwrite) ;"M"aybe mark
209 (define-key map "j" 'bookmark-jump)
210 (define-key map "g" 'bookmark-jump) ;"g"o
211 (define-key map "o" 'bookmark-jump-other-window)
212 (define-key map "i" 'bookmark-insert)
213 (define-key map "e" 'edit-bookmarks)
214 (define-key map "f" 'bookmark-insert-location) ;"f"ind
215 (define-key map "r" 'bookmark-rename)
216 (define-key map "d" 'bookmark-delete)
217 (define-key map "l" 'bookmark-load)
218 (define-key map "w" 'bookmark-write)
219 (define-key map "s" 'bookmark-save)
220 map)
221 "Keymap containing bindings to bookmark functions.
222 It is not bound to any key by default: to bind it
223 so that you have a bookmark prefix, just use `global-set-key' and bind a
224 key of your choice to `bookmark-map'. All interactive bookmark
225 functions have a binding in this keymap.")
226
227 ;;;###autoload (fset 'bookmark-map bookmark-map)
228
229 \f
230 ;;; Core variables and data structures:
231 (defvar bookmark-alist ()
232 "Association list of bookmarks and their records.
233 Bookmark functions update the value automatically.
234 You probably do NOT want to change the value yourself.
235
236 The value is an alist with entries of the form
237
238 (BOOKMARK-NAME . PARAM-ALIST)
239
240 or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
241
242 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
243
244 PARAM-ALIST is an alist of bookmark information. The order of the
245 entries in PARAM-ALIST is not important. The possible entries are
246 described below. An entry with a key but null value means the entry
247 is not used.
248
249 (filename . FILENAME)
250 (position . POS)
251 (front-context-string . STR-AFTER-POS)
252 (rear-context-string . STR-BEFORE-POS)
253 (handler . HANDLER)
254 (annotation . ANNOTATION)
255
256 FILENAME names the bookmarked file.
257 POS is the bookmarked buffer position (position in the file).
258 STR-AFTER-POS is buffer text that immediately follows POS.
259 STR-BEFORE-POS is buffer text that immediately precedes POS.
260 ANNOTATION is a string that describes the bookmark.
261 See options `bookmark-use-annotations' and
262 `bookmark-automatically-show-annotations'.
263 HANDLER is a function that provides the bookmark-jump behavior for a
264 specific kind of bookmark. This is the case for Info bookmarks,
265 for instance. HANDLER must accept a bookmark as argument.")
266
267 (defvar bookmarks-already-loaded nil
268 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
269
270
271 ;; more stuff added by db.
272
273 (defvar bookmark-current-bookmark nil
274 "Name of bookmark most recently used in the current file.
275 It is buffer local, used to make moving a bookmark forward
276 through a file easier.")
277
278 (make-variable-buffer-local 'bookmark-current-bookmark)
279
280
281 (defvar bookmark-alist-modification-count 0
282 "Number of modifications to bookmark list since it was last saved.")
283
284
285 (defvar bookmark-search-size 16
286 "Length of the context strings recorded on either side of a bookmark.")
287
288
289 (defvar bookmark-current-buffer nil
290 "The buffer in which a bookmark is currently being set or renamed.
291 Functions that insert strings into the minibuffer use this to know
292 the source buffer for that information; see `bookmark-yank-word'
293 for example.")
294
295
296 (defvar bookmark-yank-point 0
297 "The next point from which to pull source text for `bookmark-yank-word'.
298 This point is in `bookmark-current-buffer'.")
299
300
301 (defvar bookmark-quit-flag nil
302 "Non nil make `bookmark-bmenu-search' quit immediately.")
303 \f
304 ;; Helper functions and macros.
305
306 (defmacro with-buffer-modified-unmodified (&rest body)
307 "Run BODY while preserving the buffer's `buffer-modified-p' state."
308 (let ((was-modified (make-symbol "was-modified")))
309 `(let ((,was-modified (buffer-modified-p)))
310 (unwind-protect
311 (progn ,@body)
312 (set-buffer-modified-p ,was-modified)))))
313
314 ;; Only functions below, in this page and the next one (file formats),
315 ;; need to know anything about the format of bookmark-alist entries.
316 ;; Everyone else should go through them.
317
318 (defun bookmark-name-from-full-record (bookmark-record)
319 "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
320 one element from `bookmark-alist'."
321 (car bookmark-record))
322
323
324 (defun bookmark-all-names ()
325 "Return a list of all current bookmark names."
326 (bookmark-maybe-load-default-file)
327 (mapcar 'bookmark-name-from-full-record bookmark-alist))
328
329
330 (defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
331 "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
332 If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
333 bookmark record in `bookmark-alist'; return it if found, otherwise
334 error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
335 just return it."
336 (cond
337 ((consp bookmark-name-or-record) bookmark-name-or-record)
338 ((stringp bookmark-name-or-record)
339 (or (assoc-string bookmark-name-or-record bookmark-alist
340 bookmark-completion-ignore-case)
341 (unless noerror (error "Invalid bookmark %s"
342 bookmark-name-or-record))))))
343
344
345 (defun bookmark-get-bookmark-record (bookmark-name-or-record)
346 "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
347 `bookmark-alist' (that is, all information but the name)."
348 (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
349 ;; The bookmark objects can either look like (NAME ALIST) or
350 ;; (NAME . ALIST), so we have to distinguish the two here.
351 (if (and (null (cdr alist)) (consp (caar alist)))
352 (car alist) alist)))
353
354
355 (defun bookmark-set-name (bookmark-name-or-record newname)
356 "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
357 (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
358
359 (defun bookmark-prop-get (bookmark-name-or-record prop)
360 "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
361 (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
362
363 (defun bookmark-prop-set (bookmark-name-or-record prop val)
364 "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
365 (let ((cell (assq
366 prop (bookmark-get-bookmark-record bookmark-name-or-record))))
367 (if cell
368 (setcdr cell val)
369 (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
370 (list (cons prop val))))))
371
372 (defun bookmark-get-annotation (bookmark-name-or-record)
373 "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
374 (bookmark-prop-get bookmark-name-or-record 'annotation))
375
376 (defun bookmark-set-annotation (bookmark-name-or-record ann)
377 "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
378 (bookmark-prop-set bookmark-name-or-record 'annotation ann))
379
380
381 (defun bookmark-get-filename (bookmark-name-or-record)
382 "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
383 (bookmark-prop-get bookmark-name-or-record 'filename))
384
385
386 (defun bookmark-set-filename (bookmark-name-or-record filename)
387 "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
388 (bookmark-prop-set bookmark-name-or-record 'filename filename))
389
390
391 (defun bookmark-get-position (bookmark-name-or-record)
392 "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
393 (bookmark-prop-get bookmark-name-or-record 'position))
394
395
396 (defun bookmark-set-position (bookmark-name-or-record position)
397 "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
398 (bookmark-prop-set bookmark-name-or-record 'position position))
399
400
401 (defun bookmark-get-front-context-string (bookmark-name-or-record)
402 "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
403 (bookmark-prop-get bookmark-name-or-record 'front-context-string))
404
405
406 (defun bookmark-set-front-context-string (bookmark-name-or-record string)
407 "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
408 (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
409
410
411 (defun bookmark-get-rear-context-string (bookmark-name-or-record)
412 "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
413 (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
414
415
416 (defun bookmark-set-rear-context-string (bookmark-name-or-record string)
417 "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
418 (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
419
420
421 (defun bookmark-get-handler (bookmark-name-or-record)
422 "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
423 (bookmark-prop-get bookmark-name-or-record 'handler))
424
425 (defvar bookmark-history nil
426 "The history list for bookmark functions.")
427
428
429 (defun bookmark-completing-read (prompt &optional default)
430 "Prompting with PROMPT, read a bookmark name in completion.
431 PROMPT will get a \": \" stuck on the end no matter what, so you
432 probably don't want to include one yourself.
433 Optional arg DEFAULT is a string to return if the user input is empty.
434 If DEFAULT is nil then return empty string for empty input."
435 (bookmark-maybe-load-default-file) ; paranoia
436 (if (listp last-nonmenu-event)
437 (bookmark-menu-popup-paned-menu t prompt
438 (if bookmark-sort-flag
439 (sort (bookmark-all-names)
440 'string-lessp)
441 (bookmark-all-names)))
442 (let* ((completion-ignore-case bookmark-completion-ignore-case)
443 (default (unless (equal "" default) default))
444 (prompt (concat prompt (if default
445 (format " (%s): " default)
446 ": "))))
447 (completing-read prompt
448 (lambda (string pred action)
449 (if (eq action 'metadata)
450 '(metadata (category . bookmark))
451 (complete-with-action
452 action bookmark-alist string pred)))
453 nil 0 nil 'bookmark-history default))))
454
455
456 (defmacro bookmark-maybe-historicize-string (string)
457 "Put STRING into the bookmark prompt history, if caller non-interactive.
458 We need this because sometimes bookmark functions are invoked from
459 menus, so `completing-read' never gets a chance to set `bookmark-history'."
460 `(or
461 (called-interactively-p 'interactive)
462 (setq bookmark-history (cons ,string bookmark-history))))
463
464 (defvar bookmark-make-record-function 'bookmark-make-record-default
465 "A function that should be called to create a bookmark record.
466 Modes may set this variable buffer-locally to enable bookmarking of
467 locations that should be treated specially, such as Info nodes,
468 news posts, images, pdf documents, etc.
469
470 The function will be called with no arguments.
471 It should signal a user error if it is unable to construct a record for
472 the current location.
473
474 The returned record should be a cons cell of the form (NAME . ALIST)
475 where ALIST is as described in `bookmark-alist' and may typically contain
476 a special cons (handler . HANDLER-FUNC) which specifies the handler function
477 that should be used instead of `bookmark-default-handler' to open this
478 bookmark. See the documentation for `bookmark-alist' for more.
479
480 NAME is a suggested name for the constructed bookmark. It can be nil
481 in which case a default heuristic will be used. The function can also
482 equivalently just return ALIST without NAME.")
483
484 (defun bookmark-make-record ()
485 "Return a new bookmark record (NAME . ALIST) for the current location."
486 (let ((record (funcall bookmark-make-record-function)))
487 ;; Set up default name if the function does not provide one.
488 (unless (stringp (car record))
489 (if (car record) (push nil record))
490 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name))))
491 ;; Set up defaults.
492 (bookmark-prop-set
493 record 'defaults
494 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
495 (list bookmark-current-bookmark
496 (car record)
497 (bookmark-buffer-name))))))
498 record))
499
500 (defun bookmark-store (name alist no-overwrite)
501 "Store the bookmark NAME with data ALIST.
502 If NO-OVERWRITE is non-nil and another bookmark of the same name already
503 exists in `bookmark-alist', record the new bookmark without throwing away the
504 old one."
505 (bookmark-maybe-load-default-file)
506 (let ((stripped-name (copy-sequence name)))
507 (or (featurep 'xemacs)
508 ;; XEmacs's `set-text-properties' doesn't work on
509 ;; free-standing strings, apparently.
510 (set-text-properties 0 (length stripped-name) nil stripped-name))
511 (if (and (not no-overwrite)
512 (bookmark-get-bookmark stripped-name 'noerror))
513 ;; already existing bookmark under that name and
514 ;; no prefix arg means just overwrite old bookmark
515 ;; Use the new (NAME . ALIST) format.
516 (setcdr (bookmark-get-bookmark stripped-name) alist)
517
518 ;; otherwise just cons it onto the front (either the bookmark
519 ;; doesn't exist already, or there is no prefix arg. In either
520 ;; case, we want the new bookmark consed onto the alist...)
521
522 (push (cons stripped-name alist) bookmark-alist))
523
524 ;; Added by db
525 (setq bookmark-current-bookmark stripped-name)
526 (setq bookmark-alist-modification-count
527 (1+ bookmark-alist-modification-count))
528 (if (bookmark-time-to-save-p)
529 (bookmark-save))
530
531 (setq bookmark-current-bookmark stripped-name)
532 (bookmark-bmenu-surreptitiously-rebuild-list)))
533
534 (defun bookmark-make-record-default (&optional no-file no-context posn)
535 "Return the record describing the location of a new bookmark.
536 Point should be at the buffer in which the bookmark is being set,
537 and normally should be at the position where the bookmark is desired,
538 but see the optional arguments for other possibilities.
539
540 If NO-FILE is non-nil, then only return the subset of the
541 record that pertains to the location within the buffer, leaving off
542 the part that records the filename.
543
544 If NO-CONTEXT is non-nil, do not include the front- and rear-context
545 strings in the record -- the position is enough.
546
547 If POSN is non-nil, record POSN as the point instead of `(point)'."
548 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
549 ,@(unless no-context `((front-context-string
550 . ,(if (>= (- (point-max) (point))
551 bookmark-search-size)
552 (buffer-substring-no-properties
553 (point)
554 (+ (point) bookmark-search-size))
555 nil))))
556 ,@(unless no-context `((rear-context-string
557 . ,(if (>= (- (point) (point-min))
558 bookmark-search-size)
559 (buffer-substring-no-properties
560 (point)
561 (- (point) bookmark-search-size))
562 nil))))
563 (position . ,(or posn (point)))))
564
565 \f
566 ;;; File format stuff
567
568 ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
569 ;; the bookmark file format -- please don't. The current format
570 ;; should be extensible enough. If you feel the need to change it,
571 ;; please discuss it with other Emacs developers first.
572 ;;
573 ;; The format of `bookmark-alist' has changed twice in its lifetime.
574 ;; This comment describes the three formats, FIRST, SECOND, and
575 ;; CURRENT.
576 ;;
577 ;; The FIRST format was used prior to Emacs 20:
578 ;;
579 ;; ((BOOKMARK-NAME (FILENAME
580 ;; STRING-IN-FRONT
581 ;; STRING-BEHIND
582 ;; POINT))
583 ;; ...)
584 ;;
585 ;; The SECOND format was introduced in Emacs 20:
586 ;;
587 ;; ((BOOKMARK-NAME ((filename . FILENAME)
588 ;; (position . POS)
589 ;; (front-context-string . STR-AFTER-POS)
590 ;; (rear-context-string . STR-BEFORE-POS)
591 ;; (annotation . ANNOTATION)
592 ;; (whatever . VALUE)
593 ;; ...
594 ;; ))
595 ;; ...)
596 ;;
597 ;; The CURRENT format was introduced in Emacs 22:
598 ;;
599 ;; ((BOOKMARK-NAME (filename . FILENAME)
600 ;; (position . POS)
601 ;; (front-context-string . STR-AFTER-POS)
602 ;; (rear-context-string . STR-BEFORE-POS)
603 ;; (annotation . ANNOTATION)
604 ;; (whatever . VALUE)
605 ;; ...
606 ;; )
607 ;; ...)
608 ;;
609 ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
610 ;; bookmark record is a list of entry information. FIRST and SECOND
611 ;; differ in the form of the record information: FIRST uses a list of
612 ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
613 ;; the list elements matters. In the SECOND format, the order of the
614 ;; alist elements is unimportant. The SECOND format facilitates the
615 ;; addition of new kinds of elements, to support new kinds of
616 ;; bookmarks or code evolution.
617 ;;
618 ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
619 ;; saving one cons cell per bookmark: the cadr of a bookmark record is
620 ;; no longer a cons. Why that change was made remains a mystery --
621 ;; just be aware of it. (Be aware too that this explanatory comment
622 ;; was incorrect in Emacs 22 and Emacs 23.1.)
623 ;;
624 ;; To deal with the change from FIRST format to SECOND, conversion
625 ;; code was added, and it is still in use. See
626 ;; `bookmark-maybe-upgrade-file-format'.
627 ;;
628 ;; No conversion from SECOND to CURRENT is done. Instead, the code
629 ;; handles both formats OK. It must continue to do so.
630 ;;
631 ;; See the doc string of `bookmark-alist' for information about the
632 ;; elements that define a bookmark (e.g. `filename').
633
634
635 (defconst bookmark-file-format-version 1
636 "The current version of the format used by bookmark files.
637 You should never need to change this.")
638
639
640 (defconst bookmark-end-of-version-stamp-marker
641 "-*- End Of Bookmark File Format Version Stamp -*-\n"
642 "This string marks the end of the version stamp in a bookmark file.")
643
644
645 (defun bookmark-alist-from-buffer ()
646 "Return a `bookmark-alist' (in any format) from the current buffer.
647 The buffer must of course contain bookmark format information.
648 Does not care from where in the buffer it is called, and does not
649 affect point."
650 (save-excursion
651 (goto-char (point-min))
652 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
653 (read (current-buffer))
654 ;; Else we're dealing with format version 0
655 (if (search-forward "(" nil t)
656 (progn
657 (forward-char -1)
658 (read (current-buffer)))
659 ;; Else no hope of getting information here.
660 (error "Not bookmark format")))))
661
662
663 (defun bookmark-upgrade-version-0-alist (old-list)
664 "Upgrade a version 0 alist OLD-LIST to the current version."
665 (mapcar
666 (lambda (bookmark)
667 (let* ((name (car bookmark))
668 (record (car (cdr bookmark)))
669 (filename (nth 0 record))
670 (front-str (nth 1 record))
671 (rear-str (nth 2 record))
672 (position (nth 3 record))
673 (ann (nth 4 record)))
674 (list
675 name
676 `((filename . ,filename)
677 (front-context-string . ,(or front-str ""))
678 (rear-context-string . ,(or rear-str ""))
679 (position . ,position)
680 (annotation . ,ann)))))
681 old-list))
682
683
684 (defun bookmark-upgrade-file-format-from-0 ()
685 "Upgrade a bookmark file of format 0 (the original format) to format 1.
686 This expects to be called from `point-min' in a bookmark file."
687 (message "Upgrading bookmark format from 0 to %d..."
688 bookmark-file-format-version)
689 (let* ((old-list (bookmark-alist-from-buffer))
690 (new-list (bookmark-upgrade-version-0-alist old-list)))
691 (delete-region (point-min) (point-max))
692 (bookmark-insert-file-format-version-stamp)
693 (pp new-list (current-buffer))
694 (save-buffer))
695 (goto-char (point-min))
696 (message "Upgrading bookmark format from 0 to %d...done"
697 bookmark-file-format-version)
698 )
699
700
701 (defun bookmark-grok-file-format-version ()
702 "Return an integer which is the file-format version of this bookmark file.
703 This expects to be called from `point-min' in a bookmark file."
704 (if (looking-at "^;;;;")
705 (save-excursion
706 (save-match-data
707 (re-search-forward "[0-9]")
708 (forward-char -1)
709 (read (current-buffer))))
710 ;; Else this is format version 0, the original one, which didn't
711 ;; even have version stamps.
712 0))
713
714
715 (defun bookmark-maybe-upgrade-file-format ()
716 "Check the file-format version of this bookmark file.
717 If the version is not up-to-date, upgrade it automatically.
718 This expects to be called from `point-min' in a bookmark file."
719 (let ((version (bookmark-grok-file-format-version)))
720 (cond
721 ((= version bookmark-file-format-version)
722 ) ; home free -- version is current
723 ((= version 0)
724 (bookmark-upgrade-file-format-from-0))
725 (t
726 (error "Bookmark file format version strangeness")))))
727
728
729 (defun bookmark-insert-file-format-version-stamp ()
730 "Insert text indicating current version of bookmark file format."
731 (insert
732 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
733 bookmark-file-format-version))
734 (insert ";;; This format is meant to be slightly human-readable;\n"
735 ";;; nevertheless, you probably don't want to edit it.\n"
736 ";;; "
737 bookmark-end-of-version-stamp-marker))
738
739
740 ;;; end file-format stuff
741
742 \f
743 ;;; Generic helpers.
744
745 (defun bookmark-maybe-message (fmt &rest args)
746 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
747 (if (>= baud-rate 9600)
748 (apply 'message fmt args)))
749
750 \f
751 ;;; Core code:
752
753 (defvar bookmark-minibuffer-read-name-map
754 (let ((map (make-sparse-keymap)))
755 (set-keymap-parent map minibuffer-local-map)
756 (define-key map "\C-w" 'bookmark-yank-word)
757 map))
758
759 (defun bookmark-set-internal (prompt name overwrite-or-push)
760 "Interactively set a bookmark named NAME at the current location.
761
762 Begin the interactive prompt with PROMPT, followed by a space, a
763 generated default name in parentheses, a colon and a space.
764
765 If OVERWRITE-OR-PUSH is nil, then error if there is already a
766 bookmark named NAME; if `overwrite', then replace any existing
767 bookmark if there is one; if `push' then push the new bookmark
768 onto the bookmark alist. The `push' behavior means that among
769 bookmarks named NAME, this most recently set one becomes the one in
770 effect, but the others are still there, in order, if the topmost one
771 is ever deleted."
772 (interactive (list nil current-prefix-arg))
773 (unwind-protect
774 (let* ((record (bookmark-make-record))
775 ;; `defaults' is a transient element of the
776 ;; extensible format described above in the section
777 ;; `File format stuff'. Bookmark record functions
778 ;; can use it to specify a list of default values
779 ;; accessible via M-n while reading a bookmark name.
780 (defaults (bookmark-prop-get record 'defaults))
781 (default (if (consp defaults) (car defaults) defaults)))
782
783 (if defaults
784 ;; Don't store default values in the record.
785 (setq record (assq-delete-all 'defaults record))
786 ;; When no defaults in the record, use its first element.
787 (setq defaults (car record) default defaults))
788
789 (bookmark-maybe-load-default-file)
790 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
791 ;; if they have been already set in another buffer. (e.g gnus-art).
792 (unless (and bookmark-yank-point
793 bookmark-current-buffer)
794 (setq bookmark-yank-point (point))
795 (setq bookmark-current-buffer (current-buffer)))
796
797 (let ((str
798 (or name
799 (read-from-minibuffer
800 (format "%s (default: \"%s\"): " prompt default)
801 nil
802 bookmark-minibuffer-read-name-map
803 nil nil defaults))))
804 (and (string-equal str "") (setq str default))
805
806 (cond
807 ((eq overwrite-or-push nil)
808 (if (bookmark-get-bookmark str t)
809 (error "A bookmark named \"%s\" already exists." str)
810 (bookmark-store str (cdr record) nil)))
811 ((eq overwrite-or-push 'overwrite)
812 (bookmark-store str (cdr record) nil))
813 ((eq overwrite-or-push 'push)
814 (bookmark-store str (cdr record) t))
815 (t
816 (error "Unrecognized value for `overwrite-or-push': %S"
817 overwrite-or-push)))
818
819 ;; Ask for an annotation buffer for this bookmark
820 (when bookmark-use-annotations
821 (bookmark-edit-annotation str))))
822 (setq bookmark-yank-point nil)
823 (setq bookmark-current-buffer nil)))
824
825
826 ;;;###autoload
827 (defun bookmark-set (&optional name no-overwrite)
828 "Set a bookmark named NAME at the current location.
829 If NAME is nil, then prompt the user.
830
831 With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
832 existing bookmark that has the same name as NAME, but instead push the
833 new bookmark onto the bookmark alist. The most recently set bookmark
834 with name NAME is thus the one in effect at any given time, but the
835 others are still there, should the user decide to delete the most
836 recent one.
837
838 To yank words from the text of the buffer and use them as part of the
839 bookmark name, type C-w while setting a bookmark. Successive C-w's
840 yank successive words.
841
842 Typing C-u inserts (at the bookmark name prompt) the name of the last
843 bookmark used in the document where the new bookmark is being set;
844 this helps you use a single bookmark name to track progress through a
845 large document. If there is no prior bookmark for this document, then
846 C-u inserts an appropriate name based on the buffer or file.
847
848 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
849 it removes only the first instance of a bookmark with that name from
850 the list of bookmarks.)"
851 (interactive (list nil current-prefix-arg))
852 (let ((prompt
853 (if no-overwrite "Set bookmark" "Set bookmark unconditionally")))
854 (bookmark-set-internal prompt name (if no-overwrite 'push 'overwrite))))
855
856 ;;;###autoload
857 (defun bookmark-set-no-overwrite (&optional name push-bookmark)
858 "Set a bookmark named NAME at the current location.
859 If NAME is nil, then prompt the user.
860
861 If a bookmark named NAME already exists and prefix argument
862 PUSH-BOOKMARK is non-nil, then push the new bookmark onto the
863 bookmark alist. Pushing it means that among bookmarks named
864 NAME, this one becomes the one in effect, but the others are
865 still there, in order, and become effective again if the user
866 ever deletes the most recent one.
867
868 Otherwise, if a bookmark named NAME already exists but PUSH-BOOKMARK
869 is nil, raise an error.
870
871 To yank words from the text of the buffer and use them as part of the
872 bookmark name, type C-w while setting a bookmark. Successive C-w's
873 yank successive words.
874
875 Typing C-u inserts (at the bookmark name prompt) the name of the last
876 bookmark used in the document where the new bookmark is being set;
877 this helps you use a single bookmark name to track progress through a
878 large document. If there is no prior bookmark for this document, then
879 C-u inserts an appropriate name based on the buffer or file.
880
881 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
882 it removes only the first instance of a bookmark with that name from
883 the list of bookmarks.)"
884 (interactive (list nil current-prefix-arg))
885 (bookmark-set-internal "Set bookmark" name (if push-bookmark 'push nil)))
886
887
888 (defun bookmark-kill-line (&optional newline-too)
889 "Kill from point to end of line.
890 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
891 Does not affect the kill ring."
892 (let ((eol (line-end-position)))
893 (delete-region (point) eol)
894 (if (and newline-too (looking-at "\n"))
895 (delete-char 1))))
896
897
898 ;; Defvars to avoid compilation warnings:
899 (defvar bookmark-annotation-name nil
900 "Variable holding the name of the bookmark.
901 This is used in `bookmark-edit-annotation' to record the bookmark
902 whose annotation is being edited.")
903
904
905 (defun bookmark-default-annotation-text (bookmark-name)
906 "Return default annotation text for BOOKMARK-NAME.
907 The default annotation text is simply some text explaining how to use
908 annotations."
909 (concat (format-message
910 "# Type the annotation for bookmark `%s' here.\n"
911 bookmark-name)
912 (format-message
913 "# All lines which start with a `#' will be deleted.\n")
914 "# Type C-c C-c when done.\n#\n"
915 "# Author: " (user-full-name) " <" (user-login-name) "@"
916 (system-name) ">\n"
917 "# Date: " (current-time-string) "\n"))
918
919
920 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
921 'bookmark-edit-annotation-text-func "23.1")
922 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
923 "Function to return default text to use for a bookmark annotation.
924 It takes one argument, the name of the bookmark, as a string.")
925
926 (defvar bookmark-edit-annotation-mode-map
927 (let ((map (make-sparse-keymap)))
928 (set-keymap-parent map text-mode-map)
929 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
930 map)
931 "Keymap for editing an annotation of a bookmark.")
932
933 (defun bookmark-insert-annotation (bookmark-name-or-record)
934 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
935 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
936 (if (and annotation (not (string-equal annotation "")))
937 (insert annotation))))
938
939 (define-derived-mode bookmark-edit-annotation-mode
940 text-mode "Edit Bookmark Annotation"
941 "Mode for editing the annotation of bookmarks.
942 When you have finished composing, type \\[bookmark-send-annotation].
943
944 \\{bookmark-edit-annotation-mode-map}")
945
946
947 (defun bookmark-send-edited-annotation ()
948 "Use buffer contents as annotation for a bookmark.
949 Lines beginning with `#' are ignored."
950 (interactive)
951 (if (not (derived-mode-p 'bookmark-edit-annotation-mode))
952 (error "Not in bookmark-edit-annotation-mode"))
953 (goto-char (point-min))
954 (while (< (point) (point-max))
955 (if (looking-at "^#")
956 (bookmark-kill-line t)
957 (forward-line 1)))
958 ;; Take no chances with text properties.
959 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
960 (bookmark-name bookmark-annotation-name))
961 (bookmark-set-annotation bookmark-name annotation)
962 (setq bookmark-alist-modification-count
963 (1+ bookmark-alist-modification-count))
964 (bookmark-bmenu-surreptitiously-rebuild-list))
965 (kill-buffer (current-buffer)))
966
967
968 (defun bookmark-edit-annotation (bookmark-name-or-record)
969 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
970 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
971 (bookmark-insert-annotation bookmark-name-or-record)
972 (bookmark-edit-annotation-mode)
973 (set (make-local-variable 'bookmark-annotation-name)
974 bookmark-name-or-record))
975
976
977 (defun bookmark-buffer-name ()
978 "Return the name of the current buffer in a form usable as a bookmark name.
979 If the buffer is associated with a file or directory, use that name."
980 (cond
981 ;; Or are we a file?
982 (buffer-file-name (file-name-nondirectory buffer-file-name))
983 ;; Or are we a directory?
984 ((and (boundp 'dired-directory) dired-directory)
985 (let* ((dirname (if (stringp dired-directory)
986 dired-directory
987 (car dired-directory)))
988 (idx (1- (length dirname))))
989 ;; Strip the trailing slash.
990 (if (= ?/ (aref dirname idx))
991 (file-name-nondirectory (substring dirname 0 idx))
992 ;; Else return the current-buffer
993 (buffer-name (current-buffer)))))
994 ;; If all else fails, use the buffer's name.
995 (t
996 (buffer-name (current-buffer)))))
997
998
999 (defun bookmark-yank-word ()
1000 "Get the next word from buffer `bookmark-current-buffer' and append
1001 it to the name of the bookmark currently being set, advancing
1002 `bookmark-yank-point' by one word."
1003 (interactive)
1004 (let ((string (with-current-buffer bookmark-current-buffer
1005 (goto-char bookmark-yank-point)
1006 (buffer-substring-no-properties
1007 (point)
1008 (progn
1009 (forward-word 1)
1010 (setq bookmark-yank-point (point)))))))
1011 (insert string)))
1012
1013 (defun bookmark-buffer-file-name ()
1014 "Return the current buffer's file in a way useful for bookmarks."
1015 ;; Abbreviate the path, both so it's shorter and so it's more
1016 ;; portable. E.g., the user's home dir might be a different
1017 ;; path on different machines, but "~/" will still reach it.
1018 (abbreviate-file-name
1019 (cond
1020 (buffer-file-name buffer-file-name)
1021 ((and (boundp 'dired-directory) dired-directory)
1022 (if (stringp dired-directory)
1023 dired-directory
1024 (car dired-directory)))
1025 (t (error "Buffer not visiting a file or directory")))))
1026
1027
1028 (defun bookmark-maybe-load-default-file ()
1029 "If bookmarks have not been loaded from the default place, load them."
1030 (and (not bookmarks-already-loaded)
1031 (null bookmark-alist)
1032 (prog2
1033 (and
1034 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1035 ;; to be renamed.
1036 (file-exists-p bookmark-old-default-file)
1037 (not (file-exists-p bookmark-default-file))
1038 (rename-file bookmark-old-default-file
1039 bookmark-default-file))
1040 ;; return t so the `and' will continue...
1041 t)
1042
1043 (file-readable-p bookmark-default-file)
1044 (bookmark-load bookmark-default-file t t)
1045 (setq bookmarks-already-loaded t)))
1046
1047
1048 (defun bookmark-maybe-sort-alist ()
1049 "Return `bookmark-alist' for display.
1050 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
1051 (if bookmark-sort-flag
1052 (sort (copy-alist bookmark-alist)
1053 (function
1054 (lambda (x y) (string-lessp (car x) (car y)))))
1055 bookmark-alist))
1056
1057
1058 (defvar bookmark-after-jump-hook nil
1059 "Hook run after `bookmark-jump' jumps to a bookmark.
1060 Useful for example to unhide text in `outline-mode'.")
1061
1062 (defun bookmark--jump-via (bookmark-name-or-record display-function)
1063 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
1064 current buffer as argument.
1065
1066 After calling DISPLAY-FUNCTION, set window point to the point specified
1067 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1068 and then show any annotations for this bookmark."
1069 (bookmark-handle-bookmark bookmark-name-or-record)
1070 (save-current-buffer
1071 (funcall display-function (current-buffer)))
1072 (let ((win (get-buffer-window (current-buffer) 0)))
1073 (if win (set-window-point win (point))))
1074 ;; FIXME: we used to only run bookmark-after-jump-hook in
1075 ;; `bookmark-jump' itself, but in none of the other commands.
1076 (run-hooks 'bookmark-after-jump-hook)
1077 (if bookmark-automatically-show-annotations
1078 ;; if there is an annotation for this bookmark,
1079 ;; show it in a buffer.
1080 (bookmark-show-annotation bookmark-name-or-record)))
1081
1082
1083 ;;;###autoload
1084 (defun bookmark-jump (bookmark &optional display-func)
1085 "Jump to bookmark BOOKMARK (a point in some file).
1086 You may have a problem using this function if the value of variable
1087 `bookmark-alist' is nil. If that happens, you need to load in some
1088 bookmarks. See help on function `bookmark-load' for more about
1089 this.
1090
1091 If the file pointed to by BOOKMARK no longer exists, you will be asked
1092 if you wish to give the bookmark a new location, and `bookmark-jump'
1093 will then jump to the new location, as well as recording it in place
1094 of the old one in the permanent bookmark record.
1095
1096 BOOKMARK is usually a bookmark name (a string). It can also be a
1097 bookmark record, but this is usually only done by programmatic callers.
1098
1099 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1100 bookmark. It defaults to `switch-to-buffer'. A typical value for
1101 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1102 (interactive
1103 (list (bookmark-completing-read "Jump to bookmark"
1104 bookmark-current-bookmark)))
1105 (unless bookmark
1106 (error "No bookmark specified"))
1107 (bookmark-maybe-historicize-string bookmark)
1108 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1109
1110
1111 ;;;###autoload
1112 (defun bookmark-jump-other-window (bookmark)
1113 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1114 (interactive
1115 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1116 bookmark-current-bookmark)))
1117 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1118
1119
1120 (defun bookmark-jump-noselect (bookmark)
1121 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1122 The return value has the form (BUFFER . POINT).
1123
1124 Note: this function is deprecated and is present for Emacs 22
1125 compatibility only."
1126 (declare (obsolete bookmark-handle-bookmark "23.1"))
1127 (save-excursion
1128 (bookmark-handle-bookmark bookmark)
1129 (cons (current-buffer) (point))))
1130
1131 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1132 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1133 if it has none. This changes current buffer and point and returns nil,
1134 or signals a `file-error'.
1135
1136 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1137 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1138 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1139 (condition-case err
1140 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1141 'bookmark-default-handler)
1142 (bookmark-get-bookmark bookmark-name-or-record))
1143 (bookmark-error-no-filename ;file-error
1144 ;; We were unable to find the marked file, so ask if user wants to
1145 ;; relocate the bookmark, else remind them to consider deletion.
1146 (when (stringp bookmark-name-or-record)
1147 ;; `bookmark-name-or-record' can be either a bookmark name
1148 ;; (from `bookmark-alist') or a bookmark object. If it's an
1149 ;; object, we assume it's a bookmark used internally by some
1150 ;; other package.
1151 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1152 (when file ;Don't know how to relocate if there's no `file'.
1153 ;; If file is not a dir, directory-file-name just returns file.
1154 (let ((display-name (directory-file-name file)))
1155 (ding)
1156 ;; Dialog boxes can accept a file target, but usually don't
1157 ;; know how to accept a directory target (at least, this
1158 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1159 ;; true on Windows as well). So we suppress file dialogs
1160 ;; when relocating.
1161 (let ((use-dialog-box nil)
1162 (use-file-dialog nil))
1163 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1164 bookmark-name-or-record "\"? "))
1165 (progn
1166 (bookmark-relocate bookmark-name-or-record)
1167 ;; Try again.
1168 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1169 'bookmark-default-handler)
1170 (bookmark-get-bookmark bookmark-name-or-record)))
1171 (message
1172 "Bookmark not relocated; consider removing it (%s)."
1173 bookmark-name-or-record)
1174 (signal (car err) (cdr err))))))))))
1175 ;; Added by db.
1176 (when (stringp bookmark-name-or-record)
1177 (setq bookmark-current-bookmark bookmark-name-or-record))
1178 nil)
1179
1180 (define-error 'bookmark-errors nil)
1181 (define-error 'bookmark-error-no-filename
1182 "Bookmark has no associated file (or directory)" 'bookmark-errors)
1183
1184 (defun bookmark-default-handler (bmk-record)
1185 "Default handler to jump to a particular bookmark location.
1186 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1187 Changes current buffer and point and returns nil, or signals a `file-error'."
1188 (let ((file (bookmark-get-filename bmk-record))
1189 (buf (bookmark-prop-get bmk-record 'buffer))
1190 (forward-str (bookmark-get-front-context-string bmk-record))
1191 (behind-str (bookmark-get-rear-context-string bmk-record))
1192 (place (bookmark-get-position bmk-record)))
1193 (set-buffer
1194 (cond
1195 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1196 (find-file-noselect file))
1197 ;; No file found. See if buffer BUF have been created.
1198 ((and buf (get-buffer buf)))
1199 (t ;; If not, raise error.
1200 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1201 (if place (goto-char place))
1202 ;; Go searching forward first. Then, if forward-str exists and
1203 ;; was found in the file, we can search backward for behind-str.
1204 ;; Rationale is that if text was inserted between the two in the
1205 ;; file, it's better to be put before it so you can read it,
1206 ;; rather than after and remain perhaps unaware of the changes.
1207 (when (and forward-str (search-forward forward-str (point-max) t))
1208 (goto-char (match-beginning 0)))
1209 (when (and behind-str (search-backward behind-str (point-min) t))
1210 (goto-char (match-end 0)))
1211 nil))
1212
1213 ;;;###autoload
1214 (defun bookmark-relocate (bookmark-name)
1215 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1216
1217 This makes an already existing bookmark point to that file, instead of
1218 the one it used to point at. Useful when a file has been renamed
1219 after a bookmark was set in it."
1220 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1221 (bookmark-maybe-historicize-string bookmark-name)
1222 (bookmark-maybe-load-default-file)
1223 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1224 (newloc (abbreviate-file-name
1225 (expand-file-name
1226 (read-file-name
1227 (format "Relocate %s to: " bookmark-name)
1228 (file-name-directory bmrk-filename))))))
1229 (bookmark-set-filename bookmark-name newloc)
1230 (setq bookmark-alist-modification-count
1231 (1+ bookmark-alist-modification-count))
1232 (if (bookmark-time-to-save-p)
1233 (bookmark-save))
1234 (bookmark-bmenu-surreptitiously-rebuild-list)))
1235
1236
1237 ;;;###autoload
1238 (defun bookmark-insert-location (bookmark-name &optional no-history)
1239 "Insert the name of the file associated with BOOKMARK-NAME.
1240
1241 Optional second arg NO-HISTORY means don't record this in the
1242 minibuffer history list `bookmark-history'."
1243 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1244 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1245 (insert (bookmark-location bookmark-name)))
1246
1247 ;;;###autoload
1248 (defalias 'bookmark-locate 'bookmark-insert-location)
1249
1250 (defun bookmark-location (bookmark-name-or-record)
1251 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1252 (bookmark-maybe-load-default-file)
1253 ;; We could call the `handler' and ask for it to construct a description
1254 ;; dynamically: it would open up several new possibilities, but it
1255 ;; would have the major disadvantage of forcing to load each and
1256 ;; every handler when the user calls bookmark-menu.
1257 (or (bookmark-prop-get bookmark-name-or-record 'location)
1258 (bookmark-get-filename bookmark-name-or-record)
1259 "-- Unknown location --"))
1260
1261
1262 ;;;###autoload
1263 (defun bookmark-rename (old-name &optional new-name)
1264 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1265 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1266 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1267
1268 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1269 as an argument. If called with two strings, then no prompting is done.
1270 You must pass at least OLD-NAME when calling from Lisp.
1271
1272 While you are entering the new name, consecutive C-w's insert
1273 consecutive words from the text of the buffer into the new bookmark
1274 name."
1275 (interactive (list (bookmark-completing-read "Old bookmark name")))
1276 (bookmark-maybe-historicize-string old-name)
1277 (bookmark-maybe-load-default-file)
1278
1279 (setq bookmark-yank-point (point))
1280 (setq bookmark-current-buffer (current-buffer))
1281 (let ((final-new-name
1282 (or new-name ; use second arg, if non-nil
1283 (read-from-minibuffer
1284 "New name: "
1285 nil
1286 (let ((now-map (copy-keymap minibuffer-local-map)))
1287 (define-key now-map "\C-w" 'bookmark-yank-word)
1288 now-map)
1289 nil
1290 'bookmark-history))))
1291 (bookmark-set-name old-name final-new-name)
1292 (setq bookmark-current-bookmark final-new-name)
1293 (bookmark-bmenu-surreptitiously-rebuild-list)
1294 (setq bookmark-alist-modification-count
1295 (1+ bookmark-alist-modification-count))
1296 (if (bookmark-time-to-save-p)
1297 (bookmark-save))))
1298
1299
1300 ;;;###autoload
1301 (defun bookmark-insert (bookmark-name)
1302 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1303 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1304
1305 You may have a problem using this function if the value of variable
1306 `bookmark-alist' is nil. If that happens, you need to load in some
1307 bookmarks. See help on function `bookmark-load' for more about
1308 this."
1309 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1310 (bookmark-maybe-historicize-string bookmark-name)
1311 (bookmark-maybe-load-default-file)
1312 (let ((orig-point (point))
1313 (str-to-insert
1314 (save-current-buffer
1315 (bookmark-handle-bookmark bookmark-name)
1316 (buffer-string))))
1317 (insert str-to-insert)
1318 (push-mark)
1319 (goto-char orig-point)))
1320
1321
1322 ;;;###autoload
1323 (defun bookmark-delete (bookmark-name &optional batch)
1324 "Delete BOOKMARK-NAME from the bookmark list.
1325
1326 Removes only the first instance of a bookmark with that name. If
1327 there are one or more other bookmarks with the same name, they will
1328 not be deleted. Defaults to the \"current\" bookmark (that is, the
1329 one most recently used in this file, if any).
1330 Optional second arg BATCH means don't update the bookmark list buffer,
1331 probably because we were called from there."
1332 (interactive
1333 (list (bookmark-completing-read "Delete bookmark"
1334 bookmark-current-bookmark)))
1335 (bookmark-maybe-historicize-string bookmark-name)
1336 (bookmark-maybe-load-default-file)
1337 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1338 (setq bookmark-alist (delq will-go bookmark-alist))
1339 ;; Added by db, nil bookmark-current-bookmark if the last
1340 ;; occurrence has been deleted
1341 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1342 (setq bookmark-current-bookmark nil)))
1343 (unless batch
1344 (bookmark-bmenu-surreptitiously-rebuild-list))
1345 (setq bookmark-alist-modification-count
1346 (1+ bookmark-alist-modification-count))
1347 (when (bookmark-time-to-save-p)
1348 (bookmark-save)))
1349
1350
1351 (defun bookmark-time-to-save-p (&optional final-time)
1352 "Return t if it is time to save bookmarks to disk, nil otherwise.
1353 Optional argument FINAL-TIME means this is being called when Emacs
1354 is being killed, so save even if `bookmark-save-flag' is a number and
1355 is greater than `bookmark-alist-modification-count'."
1356 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1357 (cond (final-time
1358 (and (> bookmark-alist-modification-count 0)
1359 bookmark-save-flag))
1360 ((numberp bookmark-save-flag)
1361 (>= bookmark-alist-modification-count bookmark-save-flag))
1362 (t
1363 nil)))
1364
1365
1366 ;;;###autoload
1367 (defun bookmark-write ()
1368 "Write bookmarks to a file (reading the file name with the minibuffer)."
1369 (declare (interactive-only bookmark-save))
1370 (interactive)
1371 (bookmark-maybe-load-default-file)
1372 (bookmark-save t))
1373
1374
1375 ;;;###autoload
1376 (defun bookmark-save (&optional parg file)
1377 "Save currently defined bookmarks.
1378 Saves by default in the file defined by the variable
1379 `bookmark-default-file'. With a prefix arg, save it in file FILE
1380 \(second argument).
1381
1382 If you are calling this from Lisp, the two arguments are PARG and
1383 FILE, and if you just want it to write to the default file, then
1384 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1385 instead. If you pass in one argument, and it is non-nil, then the
1386 user will be interactively queried for a file to save in.
1387
1388 When you want to load in the bookmarks from a file, use
1389 `bookmark-load', \\[bookmark-load]. That function will prompt you
1390 for a file, defaulting to the file defined by variable
1391 `bookmark-default-file'."
1392 (interactive "P")
1393 (bookmark-maybe-load-default-file)
1394 (cond
1395 ((and (null parg) (null file))
1396 ;;whether interactive or not, write to default file
1397 (bookmark-write-file bookmark-default-file))
1398 ((and (null parg) file)
1399 ;;whether interactive or not, write to given file
1400 (bookmark-write-file file))
1401 ((and parg (not file))
1402 ;;have been called interactively w/ prefix arg
1403 (let ((file (read-file-name "File to save bookmarks in: ")))
1404 (bookmark-write-file file)))
1405 (t ; someone called us with prefix-arg *and* a file, so just write to file
1406 (bookmark-write-file file)))
1407 ;; signal that we have synced the bookmark file by setting this to
1408 ;; 0. If there was an error at any point before, it will not get
1409 ;; set, which is what we want.
1410 (setq bookmark-alist-modification-count 0))
1411
1412
1413 \f
1414 (defun bookmark-write-file (file)
1415 "Write `bookmark-alist' to FILE."
1416 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1417 (with-current-buffer (get-buffer-create " *Bookmarks*")
1418 (goto-char (point-min))
1419 (delete-region (point-min) (point-max))
1420 (let ((print-length nil)
1421 (print-level nil)
1422 ;; See bug #12503 for why we bind `print-circle'. Users
1423 ;; can define their own bookmark types, which can result in
1424 ;; arbitrary Lisp objects being stored in bookmark records,
1425 ;; and some users create objects containing circularities.
1426 (print-circle t))
1427 (bookmark-insert-file-format-version-stamp)
1428 (insert "(")
1429 ;; Rather than a single call to `pp' we make one per bookmark.
1430 ;; Apparently `pp' has a poor algorithmic complexity, so this
1431 ;; scales a lot better. bug#4485.
1432 (dolist (i bookmark-alist) (pp i (current-buffer)))
1433 (insert ")")
1434 (let ((version-control
1435 (cond
1436 ((null bookmark-version-control) nil)
1437 ((eq 'never bookmark-version-control) 'never)
1438 ((eq 'nospecial bookmark-version-control) version-control)
1439 (t t))))
1440 (condition-case nil
1441 (write-region (point-min) (point-max) file)
1442 (file-error (message "Can't write %s" file)))
1443 (kill-buffer (current-buffer))
1444 (bookmark-maybe-message
1445 "Saving bookmarks to file %s...done" file)))))
1446
1447
1448 (defun bookmark-import-new-list (new-list)
1449 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1450 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1451 they conflict with existing bookmark names."
1452 (let ((names (bookmark-all-names)))
1453 (dolist (full-record new-list)
1454 (bookmark-maybe-rename full-record names)
1455 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1456 (push (bookmark-name-from-full-record full-record) names))))
1457
1458
1459 (defun bookmark-maybe-rename (full-record names)
1460 "Rename bookmark FULL-RECORD if its current name is already used.
1461 This is a helper for `bookmark-import-new-list'."
1462 (let ((found-name (bookmark-name-from-full-record full-record)))
1463 (if (member found-name names)
1464 ;; We've got a conflict, so generate a new name
1465 (let ((count 2)
1466 (new-name found-name))
1467 (while (member new-name names)
1468 (setq new-name (concat found-name (format "<%d>" count)))
1469 (setq count (1+ count)))
1470 (bookmark-set-name full-record new-name)))))
1471
1472
1473 ;;;###autoload
1474 (defun bookmark-load (file &optional overwrite no-msg)
1475 "Load bookmarks from FILE (which must be in bookmark format).
1476 Appends loaded bookmarks to the front of the list of bookmarks. If
1477 optional second argument OVERWRITE is non-nil, existing bookmarks are
1478 destroyed. Optional third arg NO-MSG means don't display any messages
1479 while loading.
1480
1481 If you load a file that doesn't contain a proper bookmark alist, you
1482 will corrupt Emacs's bookmark list. Generally, you should only load
1483 in files that were created with the bookmark functions in the first
1484 place. Your own personal bookmark file, specified by the variable
1485 `bookmark-default-file', is maintained automatically by Emacs; you
1486 shouldn't need to load it explicitly.
1487
1488 If you load a file containing bookmarks with the same names as
1489 bookmarks already present in your Emacs, the new bookmarks will get
1490 unique numeric suffixes \"<2>\", \"<3>\", etc."
1491 (interactive
1492 (list (read-file-name
1493 (format "Load bookmarks from: (%s) "
1494 bookmark-default-file)
1495 ;;Default might not be used often,
1496 ;;but there's no better default, and
1497 ;;I guess it's better than none at all.
1498 "~/" bookmark-default-file 'confirm)))
1499 (setq file (abbreviate-file-name (expand-file-name file)))
1500 (if (not (file-readable-p file))
1501 (error "Cannot read bookmark file %s" file)
1502 (if (null no-msg)
1503 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1504 (with-current-buffer (let ((enable-local-variables nil))
1505 (find-file-noselect file))
1506 (goto-char (point-min))
1507 (bookmark-maybe-upgrade-file-format)
1508 (let ((blist (bookmark-alist-from-buffer)))
1509 (if (listp blist)
1510 (progn
1511 (if overwrite
1512 (progn
1513 (setq bookmark-alist blist)
1514 (setq bookmark-alist-modification-count 0))
1515 ;; else
1516 (bookmark-import-new-list blist)
1517 (setq bookmark-alist-modification-count
1518 (1+ bookmark-alist-modification-count)))
1519 (if (string-equal
1520 (abbreviate-file-name
1521 (expand-file-name bookmark-default-file))
1522 file)
1523 (setq bookmarks-already-loaded t))
1524 (bookmark-bmenu-surreptitiously-rebuild-list))
1525 (error "Invalid bookmark list in %s" file)))
1526 (kill-buffer (current-buffer)))
1527 (if (null no-msg)
1528 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1529
1530
1531 \f
1532 ;;; Code supporting the dired-like bookmark menu.
1533 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1534
1535
1536 (defvar bookmark-bmenu-hidden-bookmarks ())
1537
1538
1539 (defvar bookmark-bmenu-mode-map
1540 (let ((map (make-keymap)))
1541 (set-keymap-parent map special-mode-map)
1542 (define-key map "v" 'bookmark-bmenu-select)
1543 (define-key map "w" 'bookmark-bmenu-locate)
1544 (define-key map "2" 'bookmark-bmenu-2-window)
1545 (define-key map "1" 'bookmark-bmenu-1-window)
1546 (define-key map "j" 'bookmark-bmenu-this-window)
1547 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1548 (define-key map "f" 'bookmark-bmenu-this-window)
1549 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1550 (define-key map "o" 'bookmark-bmenu-other-window)
1551 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1552 (define-key map "s" 'bookmark-bmenu-save)
1553 (define-key map "k" 'bookmark-bmenu-delete)
1554 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1555 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1556 (define-key map "d" 'bookmark-bmenu-delete)
1557 (define-key map " " 'next-line)
1558 (define-key map "n" 'next-line)
1559 (define-key map "p" 'previous-line)
1560 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1561 (define-key map "u" 'bookmark-bmenu-unmark)
1562 (define-key map "m" 'bookmark-bmenu-mark)
1563 (define-key map "l" 'bookmark-bmenu-load)
1564 (define-key map "r" 'bookmark-bmenu-rename)
1565 (define-key map "R" 'bookmark-bmenu-relocate)
1566 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1567 (define-key map "a" 'bookmark-bmenu-show-annotation)
1568 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1569 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1570 (define-key map "/" 'bookmark-bmenu-search)
1571 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1572 map))
1573
1574 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1575 ;; data.
1576 (put 'bookmark-bmenu-mode 'mode-class 'special)
1577
1578
1579 ;; todo: need to display whether or not bookmark exists as a buffer in
1580 ;; flag column.
1581
1582 ;; Format:
1583 ;; FLAGS BOOKMARK [ LOCATION ]
1584
1585
1586 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1587 "Rebuild the Bookmark List if it exists.
1588 Don't affect the buffer ring order."
1589 (if (get-buffer "*Bookmark List*")
1590 (save-excursion
1591 (save-window-excursion
1592 (bookmark-bmenu-list)))))
1593
1594
1595 ;;;###autoload
1596 (defun bookmark-bmenu-list ()
1597 "Display a list of existing bookmarks.
1598 The list is displayed in a buffer named `*Bookmark List*'.
1599 The leftmost column displays a D if the bookmark is flagged for
1600 deletion, or > if it is flagged for displaying."
1601 (interactive)
1602 (bookmark-maybe-load-default-file)
1603 (let ((buf (get-buffer-create "*Bookmark List*")))
1604 (if (called-interactively-p 'interactive)
1605 (switch-to-buffer buf)
1606 (set-buffer buf)))
1607 (let ((inhibit-read-only t))
1608 (erase-buffer)
1609 (if (not bookmark-bmenu-use-header-line)
1610 (insert "% Bookmark\n- --------\n"))
1611 (add-text-properties (point-min) (point)
1612 '(font-lock-face bookmark-menu-heading))
1613 (dolist (full-record (bookmark-maybe-sort-alist))
1614 (let ((name (bookmark-name-from-full-record full-record))
1615 (annotation (bookmark-get-annotation full-record))
1616 (start (point))
1617 end)
1618 ;; if a bookmark has an annotation, prepend a "*"
1619 ;; in the list of bookmarks.
1620 (insert (if (and annotation (not (string-equal annotation "")))
1621 " *" " ")
1622 name)
1623 (setq end (point))
1624 (put-text-property
1625 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1626 (when (display-mouse-p)
1627 (add-text-properties
1628 (+ bookmark-bmenu-marks-width start) end
1629 '(font-lock-face bookmark-menu-bookmark
1630 mouse-face highlight
1631 follow-link t
1632 help-echo "mouse-2: go to this bookmark in other window")))
1633 (insert "\n")))
1634 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1635 (goto-char (point-min))
1636 (bookmark-bmenu-mode)
1637 (if bookmark-bmenu-use-header-line
1638 (bookmark-bmenu-set-header)
1639 (forward-line bookmark-bmenu-inline-header-height))
1640 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1641 (bookmark-bmenu-toggle-filenames t))))
1642
1643 ;;;###autoload
1644 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1645 ;;;###autoload
1646 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1647
1648 (defun bookmark-bmenu-set-header ()
1649 "Sets the immutable header line."
1650 (let ((header (concat "%% " "Bookmark")))
1651 (when bookmark-bmenu-toggle-filenames
1652 (setq header (concat header
1653 (make-string (- bookmark-bmenu-file-column
1654 (- (length header) 3)) ?\s)
1655 "File")))
1656 (let ((pos 0))
1657 (while (string-match "[ \t\n]+" header pos)
1658 (setq pos (match-end 0))
1659 (put-text-property (match-beginning 0) pos 'display
1660 (list 'space :align-to (- pos 1))
1661 header)))
1662 (put-text-property 0 2 'face 'fixed-pitch header)
1663 (setq header (concat (propertize " " 'display '(space :align-to 0))
1664 header))
1665 ;; Code derived from `buff-menu.el'.
1666 (setq header-line-format header)))
1667
1668 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1669 "Major mode for editing a list of bookmarks.
1670 Each line describes one of the bookmarks in Emacs.
1671 Letters do not insert themselves; instead, they are commands.
1672 Bookmark names preceded by a \"*\" have annotations.
1673 \\<bookmark-bmenu-mode-map>
1674 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1675 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1676 Also show bookmarks marked using m in other windows.
1677 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1678 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1679 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1680 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1681 together with bookmark selected before this one in another window.
1682 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1683 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1684 so the bookmark menu bookmark remains visible in its window.
1685 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1686 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1687 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1688 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1689 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1690 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1691 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1692 With a prefix arg, prompts for a file to save in.
1693 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1694 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1695 With prefix argument, also move up one line.
1696 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1697 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1698 in another buffer.
1699 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1700 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1701 (setq truncate-lines t)
1702 (setq buffer-read-only t))
1703
1704
1705 (defun bookmark-bmenu-toggle-filenames (&optional show)
1706 "Toggle whether filenames are shown in the bookmark list.
1707 Optional argument SHOW means show them unconditionally."
1708 (interactive)
1709 (cond
1710 (show
1711 (setq bookmark-bmenu-toggle-filenames nil)
1712 (bookmark-bmenu-show-filenames)
1713 (setq bookmark-bmenu-toggle-filenames t))
1714 (bookmark-bmenu-toggle-filenames
1715 (bookmark-bmenu-hide-filenames)
1716 (setq bookmark-bmenu-toggle-filenames nil))
1717 (t
1718 (bookmark-bmenu-show-filenames)
1719 (setq bookmark-bmenu-toggle-filenames t)))
1720 (when bookmark-bmenu-use-header-line
1721 (bookmark-bmenu-set-header)))
1722
1723
1724 (defun bookmark-bmenu-show-filenames (&optional force)
1725 "In an interactive bookmark list, show filenames along with bookmarks.
1726 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1727 mainly for debugging, and should not be necessary in normal use."
1728 (if (and (not force) bookmark-bmenu-toggle-filenames)
1729 nil ;already shown, so do nothing
1730 (with-buffer-modified-unmodified
1731 (save-excursion
1732 (save-window-excursion
1733 (goto-char (point-min))
1734 (if (not bookmark-bmenu-use-header-line)
1735 (forward-line bookmark-bmenu-inline-header-height))
1736 (setq bookmark-bmenu-hidden-bookmarks ())
1737 (let ((inhibit-read-only t))
1738 (while (< (point) (point-max))
1739 (let ((bmrk (bookmark-bmenu-bookmark)))
1740 (push bmrk bookmark-bmenu-hidden-bookmarks)
1741 (let ((start (line-end-position)))
1742 (move-to-column bookmark-bmenu-file-column t)
1743 ;; Strip off `mouse-face' from the white spaces region.
1744 (if (display-mouse-p)
1745 (remove-text-properties start (point)
1746 '(mouse-face nil help-echo nil))))
1747 (delete-region (point) (progn (end-of-line) (point)))
1748 (insert " ")
1749 ;; Pass the NO-HISTORY arg:
1750 (bookmark-insert-location bmrk t)
1751 (forward-line 1)))))))))
1752
1753
1754 (defun bookmark-bmenu-hide-filenames (&optional force)
1755 "In an interactive bookmark list, hide the filenames of the bookmarks.
1756 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1757 mainly for debugging, and should not be necessary in normal use."
1758 (when (and (not force) bookmark-bmenu-toggle-filenames)
1759 ;; nothing to hide if above is nil
1760 (with-buffer-modified-unmodified
1761 (save-excursion
1762 (goto-char (point-min))
1763 (if (not bookmark-bmenu-use-header-line)
1764 (forward-line bookmark-bmenu-inline-header-height))
1765 (setq bookmark-bmenu-hidden-bookmarks
1766 (nreverse bookmark-bmenu-hidden-bookmarks))
1767 (let ((inhibit-read-only t))
1768 (while bookmark-bmenu-hidden-bookmarks
1769 (move-to-column bookmark-bmenu-marks-width t)
1770 (bookmark-kill-line)
1771 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1772 (start (point)))
1773 (insert name)
1774 (put-text-property start (point) 'bookmark-name-prop name)
1775 (if (display-mouse-p)
1776 (add-text-properties
1777 start (point)
1778 '(font-lock-face bookmark-menu-bookmark
1779 mouse-face highlight
1780 follow-link t help-echo
1781 "mouse-2: go to this bookmark in other window"))))
1782 (forward-line 1)))))))
1783
1784
1785 (defun bookmark-bmenu-ensure-position ()
1786 "If point is not on a bookmark line, move it to one.
1787 If before the first bookmark line, move to the first; if after the
1788 last full line, move to the last full line. The return value is undefined."
1789 (cond ((and (not bookmark-bmenu-use-header-line)
1790 (< (count-lines (point-min) (point))
1791 bookmark-bmenu-inline-header-height))
1792 (goto-char (point-min))
1793 (forward-line bookmark-bmenu-inline-header-height))
1794 ((and (bolp) (eobp))
1795 (beginning-of-line 0))))
1796
1797
1798 (defun bookmark-bmenu-bookmark ()
1799 "Return the bookmark for this line in an interactive bookmark list buffer."
1800 (bookmark-bmenu-ensure-position)
1801 (save-excursion
1802 (beginning-of-line)
1803 (forward-char bookmark-bmenu-marks-width)
1804 (get-text-property (point) 'bookmark-name-prop)))
1805
1806
1807 (defun bookmark-show-annotation (bookmark-name-or-record)
1808 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1809 if an annotation exists."
1810 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1811 (when (and annotation (not (string-equal annotation "")))
1812 (save-excursion
1813 (let ((old-buf (current-buffer)))
1814 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1815 (delete-region (point-min) (point-max))
1816 (insert annotation)
1817 (goto-char (point-min))
1818 (switch-to-buffer-other-window old-buf))))))
1819
1820
1821 (defun bookmark-show-all-annotations ()
1822 "Display the annotations for all bookmarks in a buffer."
1823 (save-selected-window
1824 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1825 (delete-region (point-min) (point-max))
1826 (dolist (full-record (bookmark-maybe-sort-alist))
1827 (let* ((name (bookmark-name-from-full-record full-record))
1828 (ann (bookmark-get-annotation full-record)))
1829 (insert (concat name ":\n"))
1830 (if (and ann (not (string-equal ann "")))
1831 ;; insert the annotation, indented by 4 spaces.
1832 (progn
1833 (save-excursion (insert ann) (unless (bolp)
1834 (insert "\n")))
1835 (while (< (point) (point-max))
1836 (beginning-of-line) ; paranoia
1837 (insert " ")
1838 (forward-line)
1839 (end-of-line))))))
1840 (goto-char (point-min))))
1841
1842
1843 (defun bookmark-bmenu-mark ()
1844 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1845 (interactive)
1846 (beginning-of-line)
1847 (bookmark-bmenu-ensure-position)
1848 (with-buffer-modified-unmodified
1849 (let ((inhibit-read-only t))
1850 (delete-char 1)
1851 (insert ?>)
1852 (forward-line 1)
1853 (bookmark-bmenu-ensure-position))))
1854
1855
1856 (defun bookmark-bmenu-select ()
1857 "Select this line's bookmark; also display bookmarks marked with `>'.
1858 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1859 (interactive)
1860 (let ((bmrk (bookmark-bmenu-bookmark))
1861 (menu (current-buffer))
1862 (others ())
1863 tem)
1864 (goto-char (point-min))
1865 (while (re-search-forward "^>" nil t)
1866 (setq tem (bookmark-bmenu-bookmark))
1867 (let ((inhibit-read-only t))
1868 (delete-char -1)
1869 (insert ?\s))
1870 (or (string-equal tem bmrk)
1871 (member tem others)
1872 (setq others (cons tem others))))
1873 (setq others (nreverse others)
1874 tem (/ (1- (frame-height)) (1+ (length others))))
1875 (delete-other-windows)
1876 (bookmark-jump bmrk)
1877 (bury-buffer menu)
1878 (if others
1879 (while others
1880 (split-window nil tem)
1881 (other-window 1)
1882 (bookmark-jump (car others))
1883 (setq others (cdr others)))
1884 (other-window 1))))
1885
1886
1887 (defun bookmark-bmenu-any-marks ()
1888 "Return non-nil if any bookmarks are marked in the marks column."
1889 (save-excursion
1890 (goto-char (point-min))
1891 (bookmark-bmenu-ensure-position)
1892 (catch 'found-mark
1893 (while (not (eobp))
1894 (beginning-of-line)
1895 (if (looking-at "^\\S-")
1896 (throw 'found-mark t)
1897 (forward-line 1)))
1898 nil)))
1899
1900
1901 (defun bookmark-bmenu-save (parg)
1902 "Save the current list into a bookmark file.
1903 With a prefix arg, prompts for a file to save them in."
1904 (interactive "P")
1905 (save-excursion
1906 (save-window-excursion
1907 (bookmark-save parg)
1908 (set-buffer-modified-p nil))))
1909
1910
1911 (defun bookmark-bmenu-load ()
1912 "Load the bookmark file and rebuild the bookmark menu-buffer."
1913 (interactive)
1914 (bookmark-bmenu-ensure-position)
1915 (save-excursion
1916 (save-window-excursion
1917 ;; This will call `bookmark-bmenu-list'
1918 (call-interactively 'bookmark-load))))
1919
1920
1921 (defun bookmark-bmenu-1-window ()
1922 "Select this line's bookmark, alone, in full frame."
1923 (interactive)
1924 (bookmark-jump (bookmark-bmenu-bookmark))
1925 (bury-buffer (other-buffer))
1926 (delete-other-windows))
1927
1928
1929 (defun bookmark-bmenu-2-window ()
1930 "Select this line's bookmark, with previous buffer in second window."
1931 (interactive)
1932 (let ((bmrk (bookmark-bmenu-bookmark))
1933 (menu (current-buffer))
1934 (pop-up-windows t))
1935 (delete-other-windows)
1936 (switch-to-buffer (other-buffer) nil t)
1937 (bookmark--jump-via bmrk 'pop-to-buffer)
1938 (bury-buffer menu)))
1939
1940
1941 (defun bookmark-bmenu-this-window ()
1942 "Select this line's bookmark in this window."
1943 (interactive)
1944 (bookmark-jump (bookmark-bmenu-bookmark)))
1945
1946
1947 (defun bookmark-bmenu-other-window ()
1948 "Select this line's bookmark in other window, leaving bookmark menu visible."
1949 (interactive)
1950 (let ((bookmark (bookmark-bmenu-bookmark)))
1951 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1952
1953
1954 (defun bookmark-bmenu-switch-other-window ()
1955 "Make the other window select this line's bookmark.
1956 The current window remains selected."
1957 (interactive)
1958 (let ((bookmark (bookmark-bmenu-bookmark))
1959 (fun (lambda (b) (display-buffer b t))))
1960 (bookmark--jump-via bookmark fun)))
1961
1962 (defun bookmark-bmenu-other-window-with-mouse (event)
1963 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1964 (interactive "e")
1965 (with-current-buffer (window-buffer (posn-window (event-end event)))
1966 (save-excursion
1967 (goto-char (posn-point (event-end event)))
1968 (bookmark-bmenu-other-window))))
1969
1970
1971 (defun bookmark-bmenu-show-annotation ()
1972 "Show the annotation for the current bookmark in another window."
1973 (interactive)
1974 (let ((bookmark (bookmark-bmenu-bookmark)))
1975 (bookmark-show-annotation bookmark)))
1976
1977
1978 (defun bookmark-bmenu-show-all-annotations ()
1979 "Show the annotation for all bookmarks in another window."
1980 (interactive)
1981 (bookmark-show-all-annotations))
1982
1983
1984 (defun bookmark-bmenu-edit-annotation ()
1985 "Edit the annotation for the current bookmark in another window."
1986 (interactive)
1987 (let ((bookmark (bookmark-bmenu-bookmark)))
1988 (bookmark-edit-annotation bookmark)))
1989
1990
1991 (defun bookmark-bmenu-unmark (&optional backup)
1992 "Cancel all requested operations on bookmark on this line and move down.
1993 Optional BACKUP means move up."
1994 (interactive "P")
1995 (beginning-of-line)
1996 (bookmark-bmenu-ensure-position)
1997 (with-buffer-modified-unmodified
1998 (let ((inhibit-read-only t))
1999 (delete-char 1)
2000 ;; any flags to reset according to circumstances? How about a
2001 ;; flag indicating whether this bookmark is being visited?
2002 ;; well, we don't have this now, so maybe later.
2003 (insert " "))
2004 (forward-line (if backup -1 1))
2005 (bookmark-bmenu-ensure-position)))
2006
2007
2008 (defun bookmark-bmenu-backup-unmark ()
2009 "Move up and cancel all requested operations on bookmark on line above."
2010 (interactive)
2011 (forward-line -1)
2012 (bookmark-bmenu-ensure-position)
2013 (bookmark-bmenu-unmark)
2014 (forward-line -1)
2015 (bookmark-bmenu-ensure-position))
2016
2017
2018 (defun bookmark-bmenu-delete ()
2019 "Mark bookmark on this line to be deleted.
2020 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2021 (interactive)
2022 (beginning-of-line)
2023 (bookmark-bmenu-ensure-position)
2024 (with-buffer-modified-unmodified
2025 (let ((inhibit-read-only t))
2026 (delete-char 1)
2027 (insert ?D)
2028 (forward-line 1)
2029 (bookmark-bmenu-ensure-position))))
2030
2031
2032 (defun bookmark-bmenu-delete-backwards ()
2033 "Mark bookmark on this line to be deleted, then move up one line.
2034 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2035 (interactive)
2036 (bookmark-bmenu-delete)
2037 (forward-line -2)
2038 (bookmark-bmenu-ensure-position)
2039 (forward-line 1)
2040 (bookmark-bmenu-ensure-position))
2041
2042
2043 (defun bookmark-bmenu-execute-deletions ()
2044 "Delete bookmarks flagged `D'."
2045 (interactive)
2046 (message "Deleting bookmarks...")
2047 (let ((o-point (point))
2048 (o-str (save-excursion
2049 (beginning-of-line)
2050 (unless (looking-at "^D")
2051 (buffer-substring
2052 (point)
2053 (progn (end-of-line) (point))))))
2054 (o-col (current-column)))
2055 (goto-char (point-min))
2056 (unless bookmark-bmenu-use-header-line
2057 (forward-line 1))
2058 (while (re-search-forward "^D" (point-max) t)
2059 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2060 (bookmark-bmenu-list)
2061 (if o-str
2062 (progn
2063 (goto-char (point-min))
2064 (search-forward o-str)
2065 (beginning-of-line)
2066 (forward-char o-col))
2067 (goto-char o-point))
2068 (beginning-of-line)
2069 (message "Deleting bookmarks...done")
2070 ))
2071
2072
2073 (defun bookmark-bmenu-rename ()
2074 "Rename bookmark on current line. Prompts for a new name."
2075 (interactive)
2076 (let ((bmrk (bookmark-bmenu-bookmark))
2077 (thispoint (point)))
2078 (bookmark-rename bmrk)
2079 (goto-char thispoint)))
2080
2081
2082 (defun bookmark-bmenu-locate ()
2083 "Display location of this bookmark. Displays in the minibuffer."
2084 (interactive)
2085 (let ((bmrk (bookmark-bmenu-bookmark)))
2086 (message "%s" (bookmark-location bmrk))))
2087
2088 (defun bookmark-bmenu-relocate ()
2089 "Change the file path of the bookmark on the current line,
2090 prompting with completion for the new path."
2091 (interactive)
2092 (let ((bmrk (bookmark-bmenu-bookmark))
2093 (thispoint (point)))
2094 (bookmark-relocate bmrk)
2095 (goto-char thispoint)))
2096
2097 ;;; Bookmark-bmenu search
2098
2099 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2100 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2101 (let ((bookmark-alist
2102 (cl-loop for i in bookmark-alist
2103 when (string-match regexp (car i)) collect i into new
2104 finally return new)))
2105 (bookmark-bmenu-list)))
2106
2107
2108 ;;;###autoload
2109 (defun bookmark-bmenu-search ()
2110 "Incremental search of bookmarks, hiding the non-matches as we go."
2111 (interactive)
2112 (let ((bmk (bookmark-bmenu-bookmark))
2113 (timer nil))
2114 (unwind-protect
2115 (minibuffer-with-setup-hook
2116 (lambda ()
2117 (setq timer (run-with-idle-timer
2118 bookmark-search-delay 'repeat
2119 #'(lambda (buf)
2120 (with-current-buffer buf
2121 (bookmark-bmenu-filter-alist-by-regexp
2122 (minibuffer-contents))))
2123 (current-buffer))))
2124 (read-string "Pattern: ")
2125 (when timer (cancel-timer timer) (setq timer nil)))
2126 (when timer ;; Signalled an error or a `quit'.
2127 (cancel-timer timer)
2128 (bookmark-bmenu-list)
2129 (bookmark-bmenu-goto-bookmark bmk)))))
2130
2131 (defun bookmark-bmenu-goto-bookmark (name)
2132 "Move point to bookmark with name NAME."
2133 (goto-char (point-min))
2134 (while (not (or (equal name (bookmark-bmenu-bookmark))
2135 (eobp)))
2136 (forward-line 1))
2137 (forward-line 0))
2138
2139
2140 \f
2141 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2142
2143 (defun bookmark-menu-popup-paned-menu (event name entries)
2144 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2145 That is, ENTRIES is a list of strings which appear as the choices
2146 in the menu.
2147 The number of panes depends on the number of entries.
2148 The visible entries are truncated to `bookmark-menu-length', but the
2149 strings returned are not."
2150 (let ((f-height (/ (frame-height) 2))
2151 (pane-list nil)
2152 (iter 0))
2153 (while entries
2154 (let (lst
2155 (count 0))
2156 (while (and (< count f-height) entries)
2157 (let ((str (car entries)))
2158 (push (cons
2159 (if (> (length str) bookmark-menu-length)
2160 (substring str 0 bookmark-menu-length)
2161 str)
2162 str)
2163 lst)
2164 (setq entries (cdr entries))
2165 (setq count (1+ count))))
2166 (setq iter (1+ iter))
2167 (push (cons
2168 (format "-*- %s (%d) -*-" name iter)
2169 (nreverse lst))
2170 pane-list)))
2171
2172 ;; Popup the menu and return the string.
2173 (x-popup-menu event (cons (concat "-*- " name " -*-")
2174 (nreverse pane-list)))))
2175
2176
2177 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2178 ;; following works, and for explaining what to do to make it work.
2179
2180 ;; We MUST autoload EACH form used to set up this variable's value, so
2181 ;; that the whole job is done in loaddefs.el.
2182
2183 ;; Emacs menubar stuff.
2184
2185 ;;;###autoload
2186 (defvar menu-bar-bookmark-map
2187 (let ((map (make-sparse-keymap "Bookmark functions")))
2188 (bindings--define-key map [load]
2189 '(menu-item "Load a Bookmark File..." bookmark-load
2190 :help "Load bookmarks from a bookmark file)"))
2191 (bindings--define-key map [write]
2192 '(menu-item "Save Bookmarks As..." bookmark-write
2193 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2194 (bindings--define-key map [save]
2195 '(menu-item "Save Bookmarks" bookmark-save
2196 :help "Save currently defined bookmarks"))
2197 (bindings--define-key map [edit]
2198 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2199 :help "Display a list of existing bookmarks"))
2200 (bindings--define-key map [delete]
2201 '(menu-item "Delete Bookmark..." bookmark-delete
2202 :help "Delete a bookmark from the bookmark list"))
2203 (bindings--define-key map [rename]
2204 '(menu-item "Rename Bookmark..." bookmark-rename
2205 :help "Change the name of a bookmark"))
2206 (bindings--define-key map [locate]
2207 '(menu-item "Insert Location..." bookmark-locate
2208 :help "Insert the name of the file associated with a bookmark"))
2209 (bindings--define-key map [insert]
2210 '(menu-item "Insert Contents..." bookmark-insert
2211 :help "Insert the text of the file pointed to by a bookmark"))
2212 (bindings--define-key map [set]
2213 '(menu-item "Set Bookmark..." bookmark-set
2214 :help "Set a bookmark named inside a file."))
2215 (bindings--define-key map [jump]
2216 '(menu-item "Jump to Bookmark..." bookmark-jump
2217 :help "Jump to a bookmark (a point in some file)"))
2218 map))
2219
2220 ;;;###autoload
2221 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2222
2223 ;; make bookmarks appear toward the right side of the menu.
2224 (if (boundp 'menu-bar-final-items)
2225 (if menu-bar-final-items
2226 (push 'bookmark menu-bar-final-items))
2227 (setq menu-bar-final-items '(bookmark)))
2228
2229 ;;;; end bookmark menu stuff ;;;;
2230
2231 \f
2232 ;; Load Hook
2233 (defvar bookmark-load-hook nil
2234 "Hook run at the end of loading library `bookmark.el'.")
2235
2236 ;; Exit Hook, called from kill-emacs-hook
2237 (define-obsolete-variable-alias 'bookmark-exit-hooks
2238 'bookmark-exit-hook "22.1")
2239 (defvar bookmark-exit-hook nil
2240 "Hook run when Emacs exits.")
2241
2242 (defun bookmark-exit-hook-internal ()
2243 "Save bookmark state, if necessary, at Emacs exit time.
2244 This also runs `bookmark-exit-hook'."
2245 (run-hooks 'bookmark-exit-hook)
2246 (and (bookmark-time-to-save-p t)
2247 (bookmark-save)))
2248
2249 (unless noninteractive
2250 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2251
2252 (defun bookmark-unload-function ()
2253 "Unload the Bookmark library."
2254 (when bookmark-save-flag (bookmark-save))
2255 ;; continue standard unloading
2256 nil)
2257
2258
2259 (run-hooks 'bookmark-load-hook)
2260
2261 (provide 'bookmark)
2262
2263 ;;; bookmark.el ends here