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