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