]> code.delx.au - gnu-emacs/blob - lisp/bookmark.el
Offer non-overwrite bookmark setter (Bug#15746)
[gnu-emacs] / lisp / bookmark.el
1 ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
2
3 ;; Copyright (C) 1993-1997, 2001-2015 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', which is `~/.emacs.bmk' by default."
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 ;;;###autoload
760 (defun bookmark-set-internal (prompt name overwrite-or-push)
761 "Interactively set a bookmark named NAME at the current location.
762
763 Begin the interactive prompt with PROMPT, followed by a space, a
764 generated default name in parentheses, a colon and a space.
765
766 If OVERWRITE-OR-PUSH is nil, then error if there is already a
767 bookmark named NAME; if `overwrite', then replace any existing
768 bookmark if there is one; if `push' then push the new bookmark
769 onto the bookmark alist. The `push' behavior means that among
770 bookmarks named NAME, this most recently set one becomes the one in
771 effect, but the others are still there, in order, if the topmost one
772 is ever deleted."
773 (interactive (list nil current-prefix-arg))
774 (unwind-protect
775 (let* ((record (bookmark-make-record))
776 ;; `defaults' is a transient element of the
777 ;; extensible format described above in the section
778 ;; `File format stuff'. Bookmark record functions
779 ;; can use it to specify a list of default values
780 ;; accessible via M-n while reading a bookmark name.
781 (defaults (bookmark-prop-get record 'defaults))
782 (default (if (consp defaults) (car defaults) defaults)))
783
784 (if defaults
785 ;; Don't store default values in the record.
786 (setq record (assq-delete-all 'defaults record))
787 ;; When no defaults in the record, use its first element.
788 (setq defaults (car record) default defaults))
789
790 (bookmark-maybe-load-default-file)
791 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
792 ;; if they have been already set in another buffer. (e.g gnus-art).
793 (unless (and bookmark-yank-point
794 bookmark-current-buffer)
795 (setq bookmark-yank-point (point))
796 (setq bookmark-current-buffer (current-buffer)))
797
798 (let ((str
799 (or name
800 (read-from-minibuffer
801 (format "%s (default: \"%s\"): " prompt default)
802 nil
803 bookmark-minibuffer-read-name-map
804 nil nil defaults))))
805 (and (string-equal str "") (setq str default))
806
807 (cond
808 ((eq overwrite-or-push nil)
809 (if (bookmark-get-bookmark str t)
810 (error "A bookmark named \"%s\" already exists." str)
811 (bookmark-store str (cdr record) nil)))
812 ((eq overwrite-or-push 'overwrite)
813 (bookmark-store str (cdr record) nil))
814 ((eq overwrite-or-push 'push)
815 (bookmark-store str (cdr record) t))
816 (t
817 (error "Unrecognized value for `overwrite-or-push': %S"
818 overwrite-or-push)))
819
820 ;; Ask for an annotation buffer for this bookmark
821 (when bookmark-use-annotations
822 (bookmark-edit-annotation str))))
823 (setq bookmark-yank-point nil)
824 (setq bookmark-current-buffer nil)))
825
826
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 (defun bookmark-set-no-overwrite (&optional name push-bookmark)
857 "Set a bookmark named NAME at the current location.
858 If NAME is nil, then prompt the user.
859
860 If a bookmark named NAME already exists and prefix argument
861 PUSH-BOOKMARK is non-nil, then push the new bookmark onto the
862 bookmark alist. Pushing it means that among bookmarks named
863 NAME, this one becomes the one in effect, but the others are
864 still there, in order, and become effective again if the user
865 ever deletes the most recent one.
866
867 Otherwise, if a bookmark named NAME already exists but PUSH-BOOKMARK
868 is nil, raise an error.
869
870 To yank words from the text of the buffer and use them as part of the
871 bookmark name, type C-w while setting a bookmark. Successive C-w's
872 yank successive words.
873
874 Typing C-u inserts (at the bookmark name prompt) the name of the last
875 bookmark used in the document where the new bookmark is being set;
876 this helps you use a single bookmark name to track progress through a
877 large document. If there is no prior bookmark for this document, then
878 C-u inserts an appropriate name based on the buffer or file.
879
880 Use \\[bookmark-delete] to remove bookmarks (you give it a name and
881 it removes only the first instance of a bookmark with that name from
882 the list of bookmarks.)"
883 (interactive (list nil current-prefix-arg))
884 (bookmark-set-internal "Set bookmark" name (if push-bookmark 'push nil)))
885
886
887 (defun bookmark-kill-line (&optional newline-too)
888 "Kill from point to end of line.
889 If optional arg NEWLINE-TOO is non-nil, delete the newline too.
890 Does not affect the kill ring."
891 (let ((eol (line-end-position)))
892 (delete-region (point) eol)
893 (if (and newline-too (looking-at "\n"))
894 (delete-char 1))))
895
896
897 ;; Defvars to avoid compilation warnings:
898 (defvar bookmark-annotation-name nil
899 "Variable holding the name of the bookmark.
900 This is used in `bookmark-edit-annotation' to record the bookmark
901 whose annotation is being edited.")
902
903
904 (defun bookmark-default-annotation-text (bookmark-name)
905 "Return default annotation text for BOOKMARK-NAME.
906 The default annotation text is simply some text explaining how to use
907 annotations."
908 (concat (format-message
909 "# Type the annotation for bookmark `%s' here.\n"
910 bookmark-name)
911 (format-message
912 "# All lines which start with a `#' will be deleted.\n")
913 "# Type C-c C-c when done.\n#\n"
914 "# Author: " (user-full-name) " <" (user-login-name) "@"
915 (system-name) ">\n"
916 "# Date: " (current-time-string) "\n"))
917
918
919 (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
920 'bookmark-edit-annotation-text-func "23.1")
921 (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
922 "Function to return default text to use for a bookmark annotation.
923 It takes one argument, the name of the bookmark, as a string.")
924
925 (defvar bookmark-edit-annotation-mode-map
926 (let ((map (make-sparse-keymap)))
927 (set-keymap-parent map text-mode-map)
928 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
929 map)
930 "Keymap for editing an annotation of a bookmark.")
931
932 (defun bookmark-insert-annotation (bookmark-name-or-record)
933 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
934 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
935 (if (and annotation (not (string-equal annotation "")))
936 (insert annotation))))
937
938 (define-derived-mode bookmark-edit-annotation-mode
939 text-mode "Edit Bookmark Annotation"
940 "Mode for editing the annotation of bookmarks.
941 When you have finished composing, type \\[bookmark-send-annotation].
942
943 \\{bookmark-edit-annotation-mode-map}")
944
945
946 (defun bookmark-send-edited-annotation ()
947 "Use buffer contents as annotation for a bookmark.
948 Lines beginning with `#' are ignored."
949 (interactive)
950 (if (not (derived-mode-p 'bookmark-edit-annotation-mode))
951 (error "Not in bookmark-edit-annotation-mode"))
952 (goto-char (point-min))
953 (while (< (point) (point-max))
954 (if (looking-at "^#")
955 (bookmark-kill-line t)
956 (forward-line 1)))
957 ;; Take no chances with text properties.
958 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
959 (bookmark-name bookmark-annotation-name))
960 (bookmark-set-annotation bookmark-name annotation)
961 (setq bookmark-alist-modification-count
962 (1+ bookmark-alist-modification-count))
963 (bookmark-bmenu-surreptitiously-rebuild-list))
964 (kill-buffer (current-buffer)))
965
966
967 (defun bookmark-edit-annotation (bookmark-name-or-record)
968 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
969 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
970 (bookmark-insert-annotation bookmark-name-or-record)
971 (bookmark-edit-annotation-mode)
972 (set (make-local-variable 'bookmark-annotation-name)
973 bookmark-name-or-record))
974
975
976 (defun bookmark-buffer-name ()
977 "Return the name of the current buffer in a form usable as a bookmark name.
978 If the buffer is associated with a file or directory, use that name."
979 (cond
980 ;; Or are we a file?
981 (buffer-file-name (file-name-nondirectory buffer-file-name))
982 ;; Or are we a directory?
983 ((and (boundp 'dired-directory) dired-directory)
984 (let* ((dirname (if (stringp dired-directory)
985 dired-directory
986 (car dired-directory)))
987 (idx (1- (length dirname))))
988 ;; Strip the trailing slash.
989 (if (= ?/ (aref dirname idx))
990 (file-name-nondirectory (substring dirname 0 idx))
991 ;; Else return the current-buffer
992 (buffer-name (current-buffer)))))
993 ;; If all else fails, use the buffer's name.
994 (t
995 (buffer-name (current-buffer)))))
996
997
998 (defun bookmark-yank-word ()
999 "Get the next word from buffer `bookmark-current-buffer' and append
1000 it to the name of the bookmark currently being set, advancing
1001 `bookmark-yank-point' by one word."
1002 (interactive)
1003 (let ((string (with-current-buffer bookmark-current-buffer
1004 (goto-char bookmark-yank-point)
1005 (buffer-substring-no-properties
1006 (point)
1007 (progn
1008 (forward-word 1)
1009 (setq bookmark-yank-point (point)))))))
1010 (insert string)))
1011
1012 (defun bookmark-buffer-file-name ()
1013 "Return the current buffer's file in a way useful for bookmarks."
1014 ;; Abbreviate the path, both so it's shorter and so it's more
1015 ;; portable. E.g., the user's home dir might be a different
1016 ;; path on different machines, but "~/" will still reach it.
1017 (abbreviate-file-name
1018 (cond
1019 (buffer-file-name buffer-file-name)
1020 ((and (boundp 'dired-directory) dired-directory)
1021 (if (stringp dired-directory)
1022 dired-directory
1023 (car dired-directory)))
1024 (t (error "Buffer not visiting a file or directory")))))
1025
1026
1027 (defun bookmark-maybe-load-default-file ()
1028 "If bookmarks have not been loaded from the default place, load them."
1029 (and (not bookmarks-already-loaded)
1030 (null bookmark-alist)
1031 (prog2
1032 (and
1033 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
1034 ;; to be renamed.
1035 (file-exists-p bookmark-old-default-file)
1036 (not (file-exists-p bookmark-default-file))
1037 (rename-file bookmark-old-default-file
1038 bookmark-default-file))
1039 ;; return t so the `and' will continue...
1040 t)
1041
1042 (file-readable-p bookmark-default-file)
1043 (bookmark-load bookmark-default-file t t)
1044 (setq bookmarks-already-loaded t)))
1045
1046
1047 (defun bookmark-maybe-sort-alist ()
1048 "Return `bookmark-alist' for display.
1049 If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
1050 (if bookmark-sort-flag
1051 (sort (copy-alist bookmark-alist)
1052 (function
1053 (lambda (x y) (string-lessp (car x) (car y)))))
1054 bookmark-alist))
1055
1056
1057 (defvar bookmark-after-jump-hook nil
1058 "Hook run after `bookmark-jump' jumps to a bookmark.
1059 Useful for example to unhide text in `outline-mode'.")
1060
1061 (defun bookmark--jump-via (bookmark-name-or-record display-function)
1062 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
1063 current buffer as argument.
1064
1065 After calling DISPLAY-FUNCTION, set window point to the point specified
1066 by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1067 and then show any annotations for this bookmark."
1068 (bookmark-handle-bookmark bookmark-name-or-record)
1069 (save-current-buffer
1070 (funcall display-function (current-buffer)))
1071 (let ((win (get-buffer-window (current-buffer) 0)))
1072 (if win (set-window-point win (point))))
1073 ;; FIXME: we used to only run bookmark-after-jump-hook in
1074 ;; `bookmark-jump' itself, but in none of the other commands.
1075 (run-hooks 'bookmark-after-jump-hook)
1076 (if bookmark-automatically-show-annotations
1077 ;; if there is an annotation for this bookmark,
1078 ;; show it in a buffer.
1079 (bookmark-show-annotation bookmark-name-or-record)))
1080
1081
1082 ;;;###autoload
1083 (defun bookmark-jump (bookmark &optional display-func)
1084 "Jump to bookmark BOOKMARK (a point in some file).
1085 You may have a problem using this function if the value of variable
1086 `bookmark-alist' is nil. If that happens, you need to load in some
1087 bookmarks. See help on function `bookmark-load' for more about
1088 this.
1089
1090 If the file pointed to by BOOKMARK no longer exists, you will be asked
1091 if you wish to give the bookmark a new location, and `bookmark-jump'
1092 will then jump to the new location, as well as recording it in place
1093 of the old one in the permanent bookmark record.
1094
1095 BOOKMARK is usually a bookmark name (a string). It can also be a
1096 bookmark record, but this is usually only done by programmatic callers.
1097
1098 If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
1099 bookmark. It defaults to `switch-to-buffer'. A typical value for
1100 DISPLAY-FUNC would be `switch-to-buffer-other-window'."
1101 (interactive
1102 (list (bookmark-completing-read "Jump to bookmark"
1103 bookmark-current-bookmark)))
1104 (unless bookmark
1105 (error "No bookmark specified"))
1106 (bookmark-maybe-historicize-string bookmark)
1107 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
1108
1109
1110 ;;;###autoload
1111 (defun bookmark-jump-other-window (bookmark)
1112 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
1113 (interactive
1114 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1115 bookmark-current-bookmark)))
1116 (bookmark-jump bookmark 'switch-to-buffer-other-window))
1117
1118
1119 (defun bookmark-jump-noselect (bookmark)
1120 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
1121 The return value has the form (BUFFER . POINT).
1122
1123 Note: this function is deprecated and is present for Emacs 22
1124 compatibility only."
1125 (declare (obsolete bookmark-handle-bookmark "23.1"))
1126 (save-excursion
1127 (bookmark-handle-bookmark bookmark)
1128 (cons (current-buffer) (point))))
1129
1130 (defun bookmark-handle-bookmark (bookmark-name-or-record)
1131 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1132 if it has none. This changes current buffer and point and returns nil,
1133 or signals a `file-error'.
1134
1135 If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1136 BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1137 then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
1138 (condition-case err
1139 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1140 'bookmark-default-handler)
1141 (bookmark-get-bookmark bookmark-name-or-record))
1142 (bookmark-error-no-filename ;file-error
1143 ;; We were unable to find the marked file, so ask if user wants to
1144 ;; relocate the bookmark, else remind them to consider deletion.
1145 (when (stringp bookmark-name-or-record)
1146 ;; `bookmark-name-or-record' can be either a bookmark name
1147 ;; (from `bookmark-alist') or a bookmark object. If it's an
1148 ;; object, we assume it's a bookmark used internally by some
1149 ;; other package.
1150 (let ((file (bookmark-get-filename bookmark-name-or-record)))
1151 (when file ;Don't know how to relocate if there's no `file'.
1152 ;; If file is not a dir, directory-file-name just returns file.
1153 (let ((display-name (directory-file-name file)))
1154 (ding)
1155 ;; Dialog boxes can accept a file target, but usually don't
1156 ;; know how to accept a directory target (at least, this
1157 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1158 ;; true on Windows as well). So we suppress file dialogs
1159 ;; when relocating.
1160 (let ((use-dialog-box nil)
1161 (use-file-dialog nil))
1162 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
1163 bookmark-name-or-record "\"? "))
1164 (progn
1165 (bookmark-relocate bookmark-name-or-record)
1166 ;; Try again.
1167 (funcall (or (bookmark-get-handler bookmark-name-or-record)
1168 'bookmark-default-handler)
1169 (bookmark-get-bookmark bookmark-name-or-record)))
1170 (message
1171 "Bookmark not relocated; consider removing it (%s)."
1172 bookmark-name-or-record)
1173 (signal (car err) (cdr err))))))))))
1174 ;; Added by db.
1175 (when (stringp bookmark-name-or-record)
1176 (setq bookmark-current-bookmark bookmark-name-or-record))
1177 nil)
1178
1179 (define-error 'bookmark-errors nil)
1180 (define-error 'bookmark-error-no-filename
1181 "Bookmark has no associated file (or directory)" 'bookmark-errors)
1182
1183 (defun bookmark-default-handler (bmk-record)
1184 "Default handler to jump to a particular bookmark location.
1185 BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
1186 Changes current buffer and point and returns nil, or signals a `file-error'."
1187 (let ((file (bookmark-get-filename bmk-record))
1188 (buf (bookmark-prop-get bmk-record 'buffer))
1189 (forward-str (bookmark-get-front-context-string bmk-record))
1190 (behind-str (bookmark-get-rear-context-string bmk-record))
1191 (place (bookmark-get-position bmk-record)))
1192 (set-buffer
1193 (cond
1194 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1195 (find-file-noselect file))
1196 ;; No file found. See if buffer BUF have been created.
1197 ((and buf (get-buffer buf)))
1198 (t ;; If not, raise error.
1199 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1200 (if place (goto-char place))
1201 ;; Go searching forward first. Then, if forward-str exists and
1202 ;; was found in the file, we can search backward for behind-str.
1203 ;; Rationale is that if text was inserted between the two in the
1204 ;; file, it's better to be put before it so you can read it,
1205 ;; rather than after and remain perhaps unaware of the changes.
1206 (when (and forward-str (search-forward forward-str (point-max) t))
1207 (goto-char (match-beginning 0)))
1208 (when (and behind-str (search-backward behind-str (point-min) t))
1209 (goto-char (match-end 0)))
1210 nil))
1211
1212 ;;;###autoload
1213 (defun bookmark-relocate (bookmark-name)
1214 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
1215
1216 This makes an already existing bookmark point to that file, instead of
1217 the one it used to point at. Useful when a file has been renamed
1218 after a bookmark was set in it."
1219 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
1220 (bookmark-maybe-historicize-string bookmark-name)
1221 (bookmark-maybe-load-default-file)
1222 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
1223 (newloc (abbreviate-file-name
1224 (expand-file-name
1225 (read-file-name
1226 (format "Relocate %s to: " bookmark-name)
1227 (file-name-directory bmrk-filename))))))
1228 (bookmark-set-filename bookmark-name newloc)
1229 (setq bookmark-alist-modification-count
1230 (1+ bookmark-alist-modification-count))
1231 (if (bookmark-time-to-save-p)
1232 (bookmark-save))
1233 (bookmark-bmenu-surreptitiously-rebuild-list)))
1234
1235
1236 ;;;###autoload
1237 (defun bookmark-insert-location (bookmark-name &optional no-history)
1238 "Insert the name of the file associated with BOOKMARK-NAME.
1239
1240 Optional second arg NO-HISTORY means don't record this in the
1241 minibuffer history list `bookmark-history'."
1242 (interactive (list (bookmark-completing-read "Insert bookmark location")))
1243 (or no-history (bookmark-maybe-historicize-string bookmark-name))
1244 (insert (bookmark-location bookmark-name)))
1245
1246 ;;;###autoload
1247 (defalias 'bookmark-locate 'bookmark-insert-location)
1248
1249 (defun bookmark-location (bookmark-name-or-record)
1250 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
1251 (bookmark-maybe-load-default-file)
1252 ;; We could call the `handler' and ask for it to construct a description
1253 ;; dynamically: it would open up several new possibilities, but it
1254 ;; would have the major disadvantage of forcing to load each and
1255 ;; every handler when the user calls bookmark-menu.
1256 (or (bookmark-prop-get bookmark-name-or-record 'location)
1257 (bookmark-get-filename bookmark-name-or-record)
1258 "-- Unknown location --"))
1259
1260
1261 ;;;###autoload
1262 (defun bookmark-rename (old-name &optional new-name)
1263 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1264 If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1265 If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
1266
1267 If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1268 as an argument. If called with two strings, then no prompting is done.
1269 You must pass at least OLD-NAME when calling from Lisp.
1270
1271 While you are entering the new name, consecutive C-w's insert
1272 consecutive words from the text of the buffer into the new bookmark
1273 name."
1274 (interactive (list (bookmark-completing-read "Old bookmark name")))
1275 (bookmark-maybe-historicize-string old-name)
1276 (bookmark-maybe-load-default-file)
1277
1278 (setq bookmark-yank-point (point))
1279 (setq bookmark-current-buffer (current-buffer))
1280 (let ((final-new-name
1281 (or new-name ; use second arg, if non-nil
1282 (read-from-minibuffer
1283 "New name: "
1284 nil
1285 (let ((now-map (copy-keymap minibuffer-local-map)))
1286 (define-key now-map "\C-w" 'bookmark-yank-word)
1287 now-map)
1288 nil
1289 'bookmark-history))))
1290 (bookmark-set-name old-name final-new-name)
1291 (setq bookmark-current-bookmark final-new-name)
1292 (bookmark-bmenu-surreptitiously-rebuild-list)
1293 (setq bookmark-alist-modification-count
1294 (1+ bookmark-alist-modification-count))
1295 (if (bookmark-time-to-save-p)
1296 (bookmark-save))))
1297
1298
1299 ;;;###autoload
1300 (defun bookmark-insert (bookmark-name)
1301 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1302 BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
1303
1304 You may have a problem using this function if the value of variable
1305 `bookmark-alist' is nil. If that happens, you need to load in some
1306 bookmarks. See help on function `bookmark-load' for more about
1307 this."
1308 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
1309 (bookmark-maybe-historicize-string bookmark-name)
1310 (bookmark-maybe-load-default-file)
1311 (let ((orig-point (point))
1312 (str-to-insert
1313 (save-current-buffer
1314 (bookmark-handle-bookmark bookmark-name)
1315 (buffer-string))))
1316 (insert str-to-insert)
1317 (push-mark)
1318 (goto-char orig-point)))
1319
1320
1321 ;;;###autoload
1322 (defun bookmark-delete (bookmark-name &optional batch)
1323 "Delete BOOKMARK-NAME from the bookmark list.
1324
1325 Removes only the first instance of a bookmark with that name. If
1326 there are one or more other bookmarks with the same name, they will
1327 not be deleted. Defaults to the \"current\" bookmark (that is, the
1328 one most recently used in this file, if any).
1329 Optional second arg BATCH means don't update the bookmark list buffer,
1330 probably because we were called from there."
1331 (interactive
1332 (list (bookmark-completing-read "Delete bookmark"
1333 bookmark-current-bookmark)))
1334 (bookmark-maybe-historicize-string bookmark-name)
1335 (bookmark-maybe-load-default-file)
1336 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
1337 (setq bookmark-alist (delq will-go bookmark-alist))
1338 ;; Added by db, nil bookmark-current-bookmark if the last
1339 ;; occurrence has been deleted
1340 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
1341 (setq bookmark-current-bookmark nil)))
1342 (unless batch
1343 (bookmark-bmenu-surreptitiously-rebuild-list))
1344 (setq bookmark-alist-modification-count
1345 (1+ bookmark-alist-modification-count))
1346 (when (bookmark-time-to-save-p)
1347 (bookmark-save)))
1348
1349
1350 (defun bookmark-time-to-save-p (&optional final-time)
1351 "Return t if it is time to save bookmarks to disk, nil otherwise.
1352 Optional argument FINAL-TIME means this is being called when Emacs
1353 is being killed, so save even if `bookmark-save-flag' is a number and
1354 is greater than `bookmark-alist-modification-count'."
1355 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1356 (cond (final-time
1357 (and (> bookmark-alist-modification-count 0)
1358 bookmark-save-flag))
1359 ((numberp bookmark-save-flag)
1360 (>= bookmark-alist-modification-count bookmark-save-flag))
1361 (t
1362 nil)))
1363
1364
1365 ;;;###autoload
1366 (defun bookmark-write ()
1367 "Write bookmarks to a file (reading the file name with the minibuffer)."
1368 (declare (interactive-only bookmark-save))
1369 (interactive)
1370 (bookmark-maybe-load-default-file)
1371 (bookmark-save t))
1372
1373
1374 ;;;###autoload
1375 (defun bookmark-save (&optional parg file)
1376 "Save currently defined bookmarks.
1377 Saves by default in the file defined by the variable
1378 `bookmark-default-file'. With a prefix arg, save it in file FILE
1379 \(second argument).
1380
1381 If you are calling this from Lisp, the two arguments are PARG and
1382 FILE, and if you just want it to write to the default file, then
1383 pass no arguments. Or pass in nil and FILE, and it will save in FILE
1384 instead. If you pass in one argument, and it is non-nil, then the
1385 user will be interactively queried for a file to save in.
1386
1387 When you want to load in the bookmarks from a file, use
1388 `bookmark-load', \\[bookmark-load]. That function will prompt you
1389 for a file, defaulting to the file defined by variable
1390 `bookmark-default-file'."
1391 (interactive "P")
1392 (bookmark-maybe-load-default-file)
1393 (cond
1394 ((and (null parg) (null file))
1395 ;;whether interactive or not, write to default file
1396 (bookmark-write-file bookmark-default-file))
1397 ((and (null parg) file)
1398 ;;whether interactive or not, write to given file
1399 (bookmark-write-file file))
1400 ((and parg (not file))
1401 ;;have been called interactively w/ prefix arg
1402 (let ((file (read-file-name "File to save bookmarks in: ")))
1403 (bookmark-write-file file)))
1404 (t ; someone called us with prefix-arg *and* a file, so just write to file
1405 (bookmark-write-file file)))
1406 ;; signal that we have synced the bookmark file by setting this to
1407 ;; 0. If there was an error at any point before, it will not get
1408 ;; set, which is what we want.
1409 (setq bookmark-alist-modification-count 0))
1410
1411
1412 \f
1413 (defun bookmark-write-file (file)
1414 "Write `bookmark-alist' to FILE."
1415 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1416 (with-current-buffer (get-buffer-create " *Bookmarks*")
1417 (goto-char (point-min))
1418 (delete-region (point-min) (point-max))
1419 (let ((print-length nil)
1420 (print-level nil)
1421 ;; See bug #12503 for why we bind `print-circle'. Users
1422 ;; can define their own bookmark types, which can result in
1423 ;; arbitrary Lisp objects being stored in bookmark records,
1424 ;; and some users create objects containing circularities.
1425 (print-circle t))
1426 (bookmark-insert-file-format-version-stamp)
1427 (insert "(")
1428 ;; Rather than a single call to `pp' we make one per bookmark.
1429 ;; Apparently `pp' has a poor algorithmic complexity, so this
1430 ;; scales a lot better. bug#4485.
1431 (dolist (i bookmark-alist) (pp i (current-buffer)))
1432 (insert ")")
1433 (let ((version-control
1434 (cond
1435 ((null bookmark-version-control) nil)
1436 ((eq 'never bookmark-version-control) 'never)
1437 ((eq 'nospecial bookmark-version-control) version-control)
1438 (t t))))
1439 (condition-case nil
1440 (write-region (point-min) (point-max) file)
1441 (file-error (message "Can't write %s" file)))
1442 (kill-buffer (current-buffer))
1443 (bookmark-maybe-message
1444 "Saving bookmarks to file %s...done" file)))))
1445
1446
1447 (defun bookmark-import-new-list (new-list)
1448 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1449 Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1450 they conflict with existing bookmark names."
1451 (let ((names (bookmark-all-names)))
1452 (dolist (full-record new-list)
1453 (bookmark-maybe-rename full-record names)
1454 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
1455 (push (bookmark-name-from-full-record full-record) names))))
1456
1457
1458 (defun bookmark-maybe-rename (full-record names)
1459 "Rename bookmark FULL-RECORD if its current name is already used.
1460 This is a helper for `bookmark-import-new-list'."
1461 (let ((found-name (bookmark-name-from-full-record full-record)))
1462 (if (member found-name names)
1463 ;; We've got a conflict, so generate a new name
1464 (let ((count 2)
1465 (new-name found-name))
1466 (while (member new-name names)
1467 (setq new-name (concat found-name (format "<%d>" count)))
1468 (setq count (1+ count)))
1469 (bookmark-set-name full-record new-name)))))
1470
1471
1472 ;;;###autoload
1473 (defun bookmark-load (file &optional overwrite no-msg)
1474 "Load bookmarks from FILE (which must be in bookmark format).
1475 Appends loaded bookmarks to the front of the list of bookmarks. If
1476 optional second argument OVERWRITE is non-nil, existing bookmarks are
1477 destroyed. Optional third arg NO-MSG means don't display any messages
1478 while loading.
1479
1480 If you load a file that doesn't contain a proper bookmark alist, you
1481 will corrupt Emacs's bookmark list. Generally, you should only load
1482 in files that were created with the bookmark functions in the first
1483 place. Your own personal bookmark file, `~/.emacs.bmk', is
1484 maintained automatically by Emacs; you shouldn't need to load it
1485 explicitly.
1486
1487 If you load a file containing bookmarks with the same names as
1488 bookmarks already present in your Emacs, the new bookmarks will get
1489 unique numeric suffixes \"<2>\", \"<3>\", etc."
1490 (interactive
1491 (list (read-file-name
1492 (format "Load bookmarks from: (%s) "
1493 bookmark-default-file)
1494 ;;Default might not be used often,
1495 ;;but there's no better default, and
1496 ;;I guess it's better than none at all.
1497 "~/" bookmark-default-file 'confirm)))
1498 (setq file (abbreviate-file-name (expand-file-name file)))
1499 (if (not (file-readable-p file))
1500 (error "Cannot read bookmark file %s" file)
1501 (if (null no-msg)
1502 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1503 (with-current-buffer (let ((enable-local-variables nil))
1504 (find-file-noselect file))
1505 (goto-char (point-min))
1506 (bookmark-maybe-upgrade-file-format)
1507 (let ((blist (bookmark-alist-from-buffer)))
1508 (if (listp blist)
1509 (progn
1510 (if overwrite
1511 (progn
1512 (setq bookmark-alist blist)
1513 (setq bookmark-alist-modification-count 0))
1514 ;; else
1515 (bookmark-import-new-list blist)
1516 (setq bookmark-alist-modification-count
1517 (1+ bookmark-alist-modification-count)))
1518 (if (string-equal
1519 (abbreviate-file-name
1520 (expand-file-name bookmark-default-file))
1521 file)
1522 (setq bookmarks-already-loaded t))
1523 (bookmark-bmenu-surreptitiously-rebuild-list))
1524 (error "Invalid bookmark list in %s" file)))
1525 (kill-buffer (current-buffer)))
1526 (if (null no-msg)
1527 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
1528
1529
1530 \f
1531 ;;; Code supporting the dired-like bookmark menu.
1532 ;; Prefix is "bookmark-bmenu" for "buffer-menu":
1533
1534
1535 (defvar bookmark-bmenu-hidden-bookmarks ())
1536
1537
1538 (defvar bookmark-bmenu-mode-map
1539 (let ((map (make-keymap)))
1540 (set-keymap-parent map special-mode-map)
1541 (define-key map "v" 'bookmark-bmenu-select)
1542 (define-key map "w" 'bookmark-bmenu-locate)
1543 (define-key map "2" 'bookmark-bmenu-2-window)
1544 (define-key map "1" 'bookmark-bmenu-1-window)
1545 (define-key map "j" 'bookmark-bmenu-this-window)
1546 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1547 (define-key map "f" 'bookmark-bmenu-this-window)
1548 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1549 (define-key map "o" 'bookmark-bmenu-other-window)
1550 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1551 (define-key map "s" 'bookmark-bmenu-save)
1552 (define-key map "k" 'bookmark-bmenu-delete)
1553 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1554 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1555 (define-key map "d" 'bookmark-bmenu-delete)
1556 (define-key map " " 'next-line)
1557 (define-key map "n" 'next-line)
1558 (define-key map "p" 'previous-line)
1559 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1560 (define-key map "u" 'bookmark-bmenu-unmark)
1561 (define-key map "m" 'bookmark-bmenu-mark)
1562 (define-key map "l" 'bookmark-bmenu-load)
1563 (define-key map "r" 'bookmark-bmenu-rename)
1564 (define-key map "R" 'bookmark-bmenu-relocate)
1565 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1566 (define-key map "a" 'bookmark-bmenu-show-annotation)
1567 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1568 (define-key map "e" 'bookmark-bmenu-edit-annotation)
1569 (define-key map "/" 'bookmark-bmenu-search)
1570 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1571 map))
1572
1573 ;; Bookmark Buffer Menu mode is suitable only for specially formatted
1574 ;; data.
1575 (put 'bookmark-bmenu-mode 'mode-class 'special)
1576
1577
1578 ;; todo: need to display whether or not bookmark exists as a buffer in
1579 ;; flag column.
1580
1581 ;; Format:
1582 ;; FLAGS BOOKMARK [ LOCATION ]
1583
1584
1585 (defun bookmark-bmenu-surreptitiously-rebuild-list ()
1586 "Rebuild the Bookmark List if it exists.
1587 Don't affect the buffer ring order."
1588 (if (get-buffer "*Bookmark List*")
1589 (save-excursion
1590 (save-window-excursion
1591 (bookmark-bmenu-list)))))
1592
1593
1594 ;;;###autoload
1595 (defun bookmark-bmenu-list ()
1596 "Display a list of existing bookmarks.
1597 The list is displayed in a buffer named `*Bookmark List*'.
1598 The leftmost column displays a D if the bookmark is flagged for
1599 deletion, or > if it is flagged for displaying."
1600 (interactive)
1601 (bookmark-maybe-load-default-file)
1602 (let ((buf (get-buffer-create "*Bookmark List*")))
1603 (if (called-interactively-p 'interactive)
1604 (switch-to-buffer buf)
1605 (set-buffer buf)))
1606 (let ((inhibit-read-only t))
1607 (erase-buffer)
1608 (if (not bookmark-bmenu-use-header-line)
1609 (insert "% Bookmark\n- --------\n"))
1610 (add-text-properties (point-min) (point)
1611 '(font-lock-face bookmark-menu-heading))
1612 (dolist (full-record (bookmark-maybe-sort-alist))
1613 (let ((name (bookmark-name-from-full-record full-record))
1614 (annotation (bookmark-get-annotation full-record))
1615 (start (point))
1616 end)
1617 ;; if a bookmark has an annotation, prepend a "*"
1618 ;; in the list of bookmarks.
1619 (insert (if (and annotation (not (string-equal annotation "")))
1620 " *" " ")
1621 name)
1622 (setq end (point))
1623 (put-text-property
1624 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
1625 (when (display-mouse-p)
1626 (add-text-properties
1627 (+ bookmark-bmenu-marks-width start) end
1628 '(font-lock-face bookmark-menu-bookmark
1629 mouse-face highlight
1630 follow-link t
1631 help-echo "mouse-2: go to this bookmark in other window")))
1632 (insert "\n")))
1633 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
1634 (goto-char (point-min))
1635 (bookmark-bmenu-mode)
1636 (if bookmark-bmenu-use-header-line
1637 (bookmark-bmenu-set-header)
1638 (forward-line bookmark-bmenu-inline-header-height))
1639 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1640 (bookmark-bmenu-toggle-filenames t))))
1641
1642 ;;;###autoload
1643 (defalias 'list-bookmarks 'bookmark-bmenu-list)
1644 ;;;###autoload
1645 (defalias 'edit-bookmarks 'bookmark-bmenu-list)
1646
1647 (defun bookmark-bmenu-set-header ()
1648 "Sets the immutable header line."
1649 (let ((header (concat "%% " "Bookmark")))
1650 (when bookmark-bmenu-toggle-filenames
1651 (setq header (concat header
1652 (make-string (- bookmark-bmenu-file-column
1653 (- (length header) 3)) ?\s)
1654 "File")))
1655 (let ((pos 0))
1656 (while (string-match "[ \t\n]+" header pos)
1657 (setq pos (match-end 0))
1658 (put-text-property (match-beginning 0) pos 'display
1659 (list 'space :align-to (- pos 1))
1660 header)))
1661 (put-text-property 0 2 'face 'fixed-pitch header)
1662 (setq header (concat (propertize " " 'display '(space :align-to 0))
1663 header))
1664 ;; Code derived from `buff-menu.el'.
1665 (setq header-line-format header)))
1666
1667 (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
1668 "Major mode for editing a list of bookmarks.
1669 Each line describes one of the bookmarks in Emacs.
1670 Letters do not insert themselves; instead, they are commands.
1671 Bookmark names preceded by a \"*\" have annotations.
1672 \\<bookmark-bmenu-mode-map>
1673 \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1674 \\[bookmark-bmenu-select] -- select bookmark of line point is on.
1675 Also show bookmarks marked using m in other windows.
1676 \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1677 \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1678 \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1679 \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
1680 together with bookmark selected before this one in another window.
1681 \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1682 \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
1683 so the bookmark menu bookmark remains visible in its window.
1684 \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
1685 \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1686 \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
1687 \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
1688 \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
1689 \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
1690 \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
1691 With a prefix arg, prompts for a file to save in.
1692 \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1693 \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
1694 With prefix argument, also move up one line.
1695 \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1696 \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1697 in another buffer.
1698 \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1699 \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
1700 (setq truncate-lines t)
1701 (setq buffer-read-only t))
1702
1703
1704 (defun bookmark-bmenu-toggle-filenames (&optional show)
1705 "Toggle whether filenames are shown in the bookmark list.
1706 Optional argument SHOW means show them unconditionally."
1707 (interactive)
1708 (cond
1709 (show
1710 (setq bookmark-bmenu-toggle-filenames nil)
1711 (bookmark-bmenu-show-filenames)
1712 (setq bookmark-bmenu-toggle-filenames t))
1713 (bookmark-bmenu-toggle-filenames
1714 (bookmark-bmenu-hide-filenames)
1715 (setq bookmark-bmenu-toggle-filenames nil))
1716 (t
1717 (bookmark-bmenu-show-filenames)
1718 (setq bookmark-bmenu-toggle-filenames t)))
1719 (when bookmark-bmenu-use-header-line
1720 (bookmark-bmenu-set-header)))
1721
1722
1723 (defun bookmark-bmenu-show-filenames (&optional force)
1724 "In an interactive bookmark list, show filenames along with bookmarks.
1725 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1726 mainly for debugging, and should not be necessary in normal use."
1727 (if (and (not force) bookmark-bmenu-toggle-filenames)
1728 nil ;already shown, so do nothing
1729 (with-buffer-modified-unmodified
1730 (save-excursion
1731 (save-window-excursion
1732 (goto-char (point-min))
1733 (if (not bookmark-bmenu-use-header-line)
1734 (forward-line bookmark-bmenu-inline-header-height))
1735 (setq bookmark-bmenu-hidden-bookmarks ())
1736 (let ((inhibit-read-only t))
1737 (while (< (point) (point-max))
1738 (let ((bmrk (bookmark-bmenu-bookmark)))
1739 (push bmrk bookmark-bmenu-hidden-bookmarks)
1740 (let ((start (line-end-position)))
1741 (move-to-column bookmark-bmenu-file-column t)
1742 ;; Strip off `mouse-face' from the white spaces region.
1743 (if (display-mouse-p)
1744 (remove-text-properties start (point)
1745 '(mouse-face nil help-echo nil))))
1746 (delete-region (point) (progn (end-of-line) (point)))
1747 (insert " ")
1748 ;; Pass the NO-HISTORY arg:
1749 (bookmark-insert-location bmrk t)
1750 (forward-line 1)))))))))
1751
1752
1753 (defun bookmark-bmenu-hide-filenames (&optional force)
1754 "In an interactive bookmark list, hide the filenames of the bookmarks.
1755 Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1756 mainly for debugging, and should not be necessary in normal use."
1757 (when (and (not force) bookmark-bmenu-toggle-filenames)
1758 ;; nothing to hide if above is nil
1759 (with-buffer-modified-unmodified
1760 (save-excursion
1761 (goto-char (point-min))
1762 (if (not bookmark-bmenu-use-header-line)
1763 (forward-line bookmark-bmenu-inline-header-height))
1764 (setq bookmark-bmenu-hidden-bookmarks
1765 (nreverse bookmark-bmenu-hidden-bookmarks))
1766 (let ((inhibit-read-only t))
1767 (while bookmark-bmenu-hidden-bookmarks
1768 (move-to-column bookmark-bmenu-marks-width t)
1769 (bookmark-kill-line)
1770 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1771 (start (point)))
1772 (insert name)
1773 (put-text-property start (point) 'bookmark-name-prop name)
1774 (if (display-mouse-p)
1775 (add-text-properties
1776 start (point)
1777 '(font-lock-face bookmark-menu-bookmark
1778 mouse-face highlight
1779 follow-link t help-echo
1780 "mouse-2: go to this bookmark in other window"))))
1781 (forward-line 1)))))))
1782
1783
1784 (defun bookmark-bmenu-ensure-position ()
1785 "If point is not on a bookmark line, move it to one.
1786 If before the first bookmark line, move to the first; if after the
1787 last full line, move to the last full line. The return value is undefined."
1788 (cond ((and (not bookmark-bmenu-use-header-line)
1789 (< (count-lines (point-min) (point))
1790 bookmark-bmenu-inline-header-height))
1791 (goto-char (point-min))
1792 (forward-line bookmark-bmenu-inline-header-height))
1793 ((and (bolp) (eobp))
1794 (beginning-of-line 0))))
1795
1796
1797 (defun bookmark-bmenu-bookmark ()
1798 "Return the bookmark for this line in an interactive bookmark list buffer."
1799 (bookmark-bmenu-ensure-position)
1800 (save-excursion
1801 (beginning-of-line)
1802 (forward-char bookmark-bmenu-marks-width)
1803 (get-text-property (point) 'bookmark-name-prop)))
1804
1805
1806 (defun bookmark-show-annotation (bookmark-name-or-record)
1807 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
1808 if an annotation exists."
1809 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
1810 (when (and annotation (not (string-equal annotation "")))
1811 (save-excursion
1812 (let ((old-buf (current-buffer)))
1813 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1814 (delete-region (point-min) (point-max))
1815 (insert annotation)
1816 (goto-char (point-min))
1817 (switch-to-buffer-other-window old-buf))))))
1818
1819
1820 (defun bookmark-show-all-annotations ()
1821 "Display the annotations for all bookmarks in a buffer."
1822 (save-selected-window
1823 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1824 (delete-region (point-min) (point-max))
1825 (dolist (full-record (bookmark-maybe-sort-alist))
1826 (let* ((name (bookmark-name-from-full-record full-record))
1827 (ann (bookmark-get-annotation full-record)))
1828 (insert (concat name ":\n"))
1829 (if (and ann (not (string-equal ann "")))
1830 ;; insert the annotation, indented by 4 spaces.
1831 (progn
1832 (save-excursion (insert ann) (unless (bolp)
1833 (insert "\n")))
1834 (while (< (point) (point-max))
1835 (beginning-of-line) ; paranoia
1836 (insert " ")
1837 (forward-line)
1838 (end-of-line))))))
1839 (goto-char (point-min))))
1840
1841
1842 (defun bookmark-bmenu-mark ()
1843 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
1844 (interactive)
1845 (beginning-of-line)
1846 (bookmark-bmenu-ensure-position)
1847 (with-buffer-modified-unmodified
1848 (let ((inhibit-read-only t))
1849 (delete-char 1)
1850 (insert ?>)
1851 (forward-line 1)
1852 (bookmark-bmenu-ensure-position))))
1853
1854
1855 (defun bookmark-bmenu-select ()
1856 "Select this line's bookmark; also display bookmarks marked with `>'.
1857 You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
1858 (interactive)
1859 (let ((bmrk (bookmark-bmenu-bookmark))
1860 (menu (current-buffer))
1861 (others ())
1862 tem)
1863 (goto-char (point-min))
1864 (while (re-search-forward "^>" nil t)
1865 (setq tem (bookmark-bmenu-bookmark))
1866 (let ((inhibit-read-only t))
1867 (delete-char -1)
1868 (insert ?\s))
1869 (or (string-equal tem bmrk)
1870 (member tem others)
1871 (setq others (cons tem others))))
1872 (setq others (nreverse others)
1873 tem (/ (1- (frame-height)) (1+ (length others))))
1874 (delete-other-windows)
1875 (bookmark-jump bmrk)
1876 (bury-buffer menu)
1877 (if others
1878 (while others
1879 (split-window nil tem)
1880 (other-window 1)
1881 (bookmark-jump (car others))
1882 (setq others (cdr others)))
1883 (other-window 1))))
1884
1885
1886 (defun bookmark-bmenu-any-marks ()
1887 "Return non-nil if any bookmarks are marked in the marks column."
1888 (save-excursion
1889 (goto-char (point-min))
1890 (bookmark-bmenu-ensure-position)
1891 (catch 'found-mark
1892 (while (not (eobp))
1893 (beginning-of-line)
1894 (if (looking-at "^\\S-")
1895 (throw 'found-mark t)
1896 (forward-line 1)))
1897 nil)))
1898
1899
1900 (defun bookmark-bmenu-save (parg)
1901 "Save the current list into a bookmark file.
1902 With a prefix arg, prompts for a file to save them in."
1903 (interactive "P")
1904 (save-excursion
1905 (save-window-excursion
1906 (bookmark-save parg)
1907 (set-buffer-modified-p nil))))
1908
1909
1910 (defun bookmark-bmenu-load ()
1911 "Load the bookmark file and rebuild the bookmark menu-buffer."
1912 (interactive)
1913 (bookmark-bmenu-ensure-position)
1914 (save-excursion
1915 (save-window-excursion
1916 ;; This will call `bookmark-bmenu-list'
1917 (call-interactively 'bookmark-load))))
1918
1919
1920 (defun bookmark-bmenu-1-window ()
1921 "Select this line's bookmark, alone, in full frame."
1922 (interactive)
1923 (bookmark-jump (bookmark-bmenu-bookmark))
1924 (bury-buffer (other-buffer))
1925 (delete-other-windows))
1926
1927
1928 (defun bookmark-bmenu-2-window ()
1929 "Select this line's bookmark, with previous buffer in second window."
1930 (interactive)
1931 (let ((bmrk (bookmark-bmenu-bookmark))
1932 (menu (current-buffer))
1933 (pop-up-windows t))
1934 (delete-other-windows)
1935 (switch-to-buffer (other-buffer) nil t)
1936 (bookmark--jump-via bmrk 'pop-to-buffer)
1937 (bury-buffer menu)))
1938
1939
1940 (defun bookmark-bmenu-this-window ()
1941 "Select this line's bookmark in this window."
1942 (interactive)
1943 (bookmark-jump (bookmark-bmenu-bookmark)))
1944
1945
1946 (defun bookmark-bmenu-other-window ()
1947 "Select this line's bookmark in other window, leaving bookmark menu visible."
1948 (interactive)
1949 (let ((bookmark (bookmark-bmenu-bookmark)))
1950 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
1951
1952
1953 (defun bookmark-bmenu-switch-other-window ()
1954 "Make the other window select this line's bookmark.
1955 The current window remains selected."
1956 (interactive)
1957 (let ((bookmark (bookmark-bmenu-bookmark))
1958 (fun (lambda (b) (display-buffer b t))))
1959 (bookmark--jump-via bookmark fun)))
1960
1961 (defun bookmark-bmenu-other-window-with-mouse (event)
1962 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1963 (interactive "e")
1964 (with-current-buffer (window-buffer (posn-window (event-end event)))
1965 (save-excursion
1966 (goto-char (posn-point (event-end event)))
1967 (bookmark-bmenu-other-window))))
1968
1969
1970 (defun bookmark-bmenu-show-annotation ()
1971 "Show the annotation for the current bookmark in another window."
1972 (interactive)
1973 (let ((bookmark (bookmark-bmenu-bookmark)))
1974 (bookmark-show-annotation bookmark)))
1975
1976
1977 (defun bookmark-bmenu-show-all-annotations ()
1978 "Show the annotation for all bookmarks in another window."
1979 (interactive)
1980 (bookmark-show-all-annotations))
1981
1982
1983 (defun bookmark-bmenu-edit-annotation ()
1984 "Edit the annotation for the current bookmark in another window."
1985 (interactive)
1986 (let ((bookmark (bookmark-bmenu-bookmark)))
1987 (bookmark-edit-annotation bookmark)))
1988
1989
1990 (defun bookmark-bmenu-unmark (&optional backup)
1991 "Cancel all requested operations on bookmark on this line and move down.
1992 Optional BACKUP means move up."
1993 (interactive "P")
1994 (beginning-of-line)
1995 (bookmark-bmenu-ensure-position)
1996 (with-buffer-modified-unmodified
1997 (let ((inhibit-read-only t))
1998 (delete-char 1)
1999 ;; any flags to reset according to circumstances? How about a
2000 ;; flag indicating whether this bookmark is being visited?
2001 ;; well, we don't have this now, so maybe later.
2002 (insert " "))
2003 (forward-line (if backup -1 1))
2004 (bookmark-bmenu-ensure-position)))
2005
2006
2007 (defun bookmark-bmenu-backup-unmark ()
2008 "Move up and cancel all requested operations on bookmark on line above."
2009 (interactive)
2010 (forward-line -1)
2011 (bookmark-bmenu-ensure-position)
2012 (bookmark-bmenu-unmark)
2013 (forward-line -1)
2014 (bookmark-bmenu-ensure-position))
2015
2016
2017 (defun bookmark-bmenu-delete ()
2018 "Mark bookmark on this line to be deleted.
2019 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2020 (interactive)
2021 (beginning-of-line)
2022 (bookmark-bmenu-ensure-position)
2023 (with-buffer-modified-unmodified
2024 (let ((inhibit-read-only t))
2025 (delete-char 1)
2026 (insert ?D)
2027 (forward-line 1)
2028 (bookmark-bmenu-ensure-position))))
2029
2030
2031 (defun bookmark-bmenu-delete-backwards ()
2032 "Mark bookmark on this line to be deleted, then move up one line.
2033 To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
2034 (interactive)
2035 (bookmark-bmenu-delete)
2036 (forward-line -2)
2037 (bookmark-bmenu-ensure-position)
2038 (forward-line 1)
2039 (bookmark-bmenu-ensure-position))
2040
2041
2042 (defun bookmark-bmenu-execute-deletions ()
2043 "Delete bookmarks flagged `D'."
2044 (interactive)
2045 (message "Deleting bookmarks...")
2046 (let ((o-point (point))
2047 (o-str (save-excursion
2048 (beginning-of-line)
2049 (unless (looking-at "^D")
2050 (buffer-substring
2051 (point)
2052 (progn (end-of-line) (point))))))
2053 (o-col (current-column)))
2054 (goto-char (point-min))
2055 (unless bookmark-bmenu-use-header-line
2056 (forward-line 1))
2057 (while (re-search-forward "^D" (point-max) t)
2058 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
2059 (bookmark-bmenu-list)
2060 (if o-str
2061 (progn
2062 (goto-char (point-min))
2063 (search-forward o-str)
2064 (beginning-of-line)
2065 (forward-char o-col))
2066 (goto-char o-point))
2067 (beginning-of-line)
2068 (message "Deleting bookmarks...done")
2069 ))
2070
2071
2072 (defun bookmark-bmenu-rename ()
2073 "Rename bookmark on current line. Prompts for a new name."
2074 (interactive)
2075 (let ((bmrk (bookmark-bmenu-bookmark))
2076 (thispoint (point)))
2077 (bookmark-rename bmrk)
2078 (goto-char thispoint)))
2079
2080
2081 (defun bookmark-bmenu-locate ()
2082 "Display location of this bookmark. Displays in the minibuffer."
2083 (interactive)
2084 (let ((bmrk (bookmark-bmenu-bookmark)))
2085 (message "%s" (bookmark-location bmrk))))
2086
2087 (defun bookmark-bmenu-relocate ()
2088 "Change the file path of the bookmark on the current line,
2089 prompting with completion for the new path."
2090 (interactive)
2091 (let ((bmrk (bookmark-bmenu-bookmark))
2092 (thispoint (point)))
2093 (bookmark-relocate bmrk)
2094 (goto-char thispoint)))
2095
2096 ;;; Bookmark-bmenu search
2097
2098 (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2099 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
2100 (let ((bookmark-alist
2101 (cl-loop for i in bookmark-alist
2102 when (string-match regexp (car i)) collect i into new
2103 finally return new)))
2104 (bookmark-bmenu-list)))
2105
2106
2107 ;;;###autoload
2108 (defun bookmark-bmenu-search ()
2109 "Incremental search of bookmarks, hiding the non-matches as we go."
2110 (interactive)
2111 (let ((bmk (bookmark-bmenu-bookmark))
2112 (timer nil))
2113 (unwind-protect
2114 (minibuffer-with-setup-hook
2115 (lambda ()
2116 (setq timer (run-with-idle-timer
2117 bookmark-search-delay 'repeat
2118 #'(lambda (buf)
2119 (with-current-buffer buf
2120 (bookmark-bmenu-filter-alist-by-regexp
2121 (minibuffer-contents))))
2122 (current-buffer))))
2123 (read-string "Pattern: ")
2124 (when timer (cancel-timer timer) (setq timer nil)))
2125 (when timer ;; Signalled an error or a `quit'.
2126 (cancel-timer timer)
2127 (bookmark-bmenu-list)
2128 (bookmark-bmenu-goto-bookmark bmk)))))
2129
2130 (defun bookmark-bmenu-goto-bookmark (name)
2131 "Move point to bookmark with name NAME."
2132 (goto-char (point-min))
2133 (while (not (or (equal name (bookmark-bmenu-bookmark))
2134 (eobp)))
2135 (forward-line 1))
2136 (forward-line 0))
2137
2138
2139 \f
2140 ;;; Menu bar stuff. Prefix is "bookmark-menu".
2141
2142 (defun bookmark-menu-popup-paned-menu (event name entries)
2143 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2144 That is, ENTRIES is a list of strings which appear as the choices
2145 in the menu.
2146 The number of panes depends on the number of entries.
2147 The visible entries are truncated to `bookmark-menu-length', but the
2148 strings returned are not."
2149 (let ((f-height (/ (frame-height) 2))
2150 (pane-list nil)
2151 (iter 0))
2152 (while entries
2153 (let (lst
2154 (count 0))
2155 (while (and (< count f-height) entries)
2156 (let ((str (car entries)))
2157 (push (cons
2158 (if (> (length str) bookmark-menu-length)
2159 (substring str 0 bookmark-menu-length)
2160 str)
2161 str)
2162 lst)
2163 (setq entries (cdr entries))
2164 (setq count (1+ count))))
2165 (setq iter (1+ iter))
2166 (push (cons
2167 (format "-*- %s (%d) -*-" name iter)
2168 (nreverse lst))
2169 pane-list)))
2170
2171 ;; Popup the menu and return the string.
2172 (x-popup-menu event (cons (concat "-*- " name " -*-")
2173 (nreverse pane-list)))))
2174
2175
2176 ;; Thanks to Roland McGrath for fixing menubar.el so that the
2177 ;; following works, and for explaining what to do to make it work.
2178
2179 ;; We MUST autoload EACH form used to set up this variable's value, so
2180 ;; that the whole job is done in loaddefs.el.
2181
2182 ;; Emacs menubar stuff.
2183
2184 ;;;###autoload
2185 (defvar menu-bar-bookmark-map
2186 (let ((map (make-sparse-keymap "Bookmark functions")))
2187 (bindings--define-key map [load]
2188 '(menu-item "Load a Bookmark File..." bookmark-load
2189 :help "Load bookmarks from a bookmark file)"))
2190 (bindings--define-key map [write]
2191 '(menu-item "Save Bookmarks As..." bookmark-write
2192 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2193 (bindings--define-key map [save]
2194 '(menu-item "Save Bookmarks" bookmark-save
2195 :help "Save currently defined bookmarks"))
2196 (bindings--define-key map [edit]
2197 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2198 :help "Display a list of existing bookmarks"))
2199 (bindings--define-key map [delete]
2200 '(menu-item "Delete Bookmark..." bookmark-delete
2201 :help "Delete a bookmark from the bookmark list"))
2202 (bindings--define-key map [rename]
2203 '(menu-item "Rename Bookmark..." bookmark-rename
2204 :help "Change the name of a bookmark"))
2205 (bindings--define-key map [locate]
2206 '(menu-item "Insert Location..." bookmark-locate
2207 :help "Insert the name of the file associated with a bookmark"))
2208 (bindings--define-key map [insert]
2209 '(menu-item "Insert Contents..." bookmark-insert
2210 :help "Insert the text of the file pointed to by a bookmark"))
2211 (bindings--define-key map [set]
2212 '(menu-item "Set Bookmark..." bookmark-set
2213 :help "Set a bookmark named inside a file."))
2214 (bindings--define-key map [jump]
2215 '(menu-item "Jump to Bookmark..." bookmark-jump
2216 :help "Jump to a bookmark (a point in some file)"))
2217 map))
2218
2219 ;;;###autoload
2220 (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
2221
2222 ;; make bookmarks appear toward the right side of the menu.
2223 (if (boundp 'menu-bar-final-items)
2224 (if menu-bar-final-items
2225 (push 'bookmark menu-bar-final-items))
2226 (setq menu-bar-final-items '(bookmark)))
2227
2228 ;;;; end bookmark menu stuff ;;;;
2229
2230 \f
2231 ;; Load Hook
2232 (defvar bookmark-load-hook nil
2233 "Hook run at the end of loading library `bookmark.el'.")
2234
2235 ;; Exit Hook, called from kill-emacs-hook
2236 (define-obsolete-variable-alias 'bookmark-exit-hooks
2237 'bookmark-exit-hook "22.1")
2238 (defvar bookmark-exit-hook nil
2239 "Hook run when Emacs exits.")
2240
2241 (defun bookmark-exit-hook-internal ()
2242 "Save bookmark state, if necessary, at Emacs exit time.
2243 This also runs `bookmark-exit-hook'."
2244 (run-hooks 'bookmark-exit-hook)
2245 (and (bookmark-time-to-save-p t)
2246 (bookmark-save)))
2247
2248 (unless noninteractive
2249 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
2250
2251 (defun bookmark-unload-function ()
2252 "Unload the Bookmark library."
2253 (when bookmark-save-flag (bookmark-save))
2254 ;; continue standard unloading
2255 nil)
2256
2257
2258 (run-hooks 'bookmark-load-hook)
2259
2260 (provide 'bookmark)
2261
2262 ;;; bookmark.el ends here