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