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