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