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