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