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