Commit | Line | Data |
---|---|---|
b3bf02fa RS |
1 | ;;; bookmark.el --- set bookmarks, jump to them later. |
2 | ||
8f1204db | 3 | ;; Copyright (C) 1993, 1994 Free Software Foundation |
b3bf02fa RS |
4 | |
5 | ;; Author: Karl Fogel <kfogel@cs.oberlin.edu> | |
8027e2ad | 6 | ;; Maintainer: Karl Fogel <kfogel@cs.oberlin.edu> |
b3bf02fa | 7 | ;; Created: July, 1993 |
d23e2c3f | 8 | ;; Version: 2.5 |
b3bf02fa RS |
9 | ;; Keywords: bookmarks, placeholders |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
15 | ;; the Free Software Foundation; either version 2, or (at your option) | |
16 | ;; any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
25 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | ||
27 | ;; Thanks to David Bremner <bremner@cs.sfu.ca> for thinking of and | |
28 | ;; then implementing the bookmark-current-bookmark idea. He even | |
d23e2c3f | 29 | ;; sent *patches*, bless his soul... |
b3bf02fa RS |
30 | |
31 | ;; Thanks to Gregory M. Saunders <saunders@cis.ohio-state.edu> for | |
32 | ;; fixing and improving bookmark-time-to-save-p. | |
33 | ||
d23e2c3f KF |
34 | ;; Thanks go to Andrew V. Klein <avk@rtsg.mot.com> for the code that |
35 | ;; sorts the alist before presenting it to the user (in list-bookmarks | |
36 | ;; and the menu-bar). | |
37 | ||
d22d6453 RS |
38 | ;; And much thanks to David Hughes <djh@harston.cv.com> for many small |
39 | ;; suggestions and the code to implement them (like | |
40 | ;; Bookmark-menu-check-position, and some of the Lucid compatibility | |
41 | ;; stuff). | |
42 | ||
d23e2c3f KF |
43 | ;; Kudos (whatever they are) go to Jim Blandy <jimb@cs.oberlin.edu> |
44 | ;; for his eminently sensible suggestion to separate bookmark-jump | |
45 | ;; into bookmark-jump and bookmark-jump-noselect, which made many | |
46 | ;; other things cleaner as well. | |
47 | ||
d22d6453 RS |
48 | ;; Thanks to Roland McGrath for encouragement and help with defining |
49 | ;; autoloads on the menu-bar. | |
50 | ||
d23e2c3f KF |
51 | ;; Jonathan Stigelman <stig@key.amdahl.com> gave patches for default |
52 | ;; values in bookmark-jump and bookmark-set. Everybody please keep | |
53 | ;; all the keystrokes they save thereby and send them to him at the | |
54 | ;; end of each year :-) (No, seriously, thanks Jonathan!) | |
55 | ||
b3bf02fa RS |
56 | ;; Based on info-bookmark.el, by Karl Fogel and Ken Olstad |
57 | ;; <olstad@msc.edu>. | |
58 | ||
59 | ;; LCD Archive Entry: | |
60 | ;; bookmark|Karl Fogel|kfogel@cs.oberlin.edu| | |
61 | ;; Setting bookmarks in files or directories, jumping to them later.| | |
d23e2c3f | 62 | ;; 16-July-93|Version: 2.5|~/misc/bookmark.el.Z| |
d22d6453 RS |
63 | |
64 | ;; Enough with the credits already, get on to the good stuff: | |
b3bf02fa RS |
65 | |
66 | ;; FAVORITE CHINESE RESTAURANT: | |
67 | ;; Boy, that's a tough one. Probably Hong Min, or maybe Emperor's | |
68 | ;; Choice (both in Chicago's Chinatown). Well, both. How about you? | |
69 | ||
07b3798c | 70 | ;;; Commentary: |
b3bf02fa RS |
71 | |
72 | ;; bookmark alist format: | |
73 | ;; (... | |
74 | ;; (bookmark-name (filename | |
75 | ;; string-in-front | |
76 | ;; string-behind | |
77 | ;; point)) | |
78 | ;; ...) | |
79 | ;; | |
80 | ;; bookmark-name is the string the user gives the bookmark and | |
81 | ;; accesses it by from then on. filename is the location of the file | |
82 | ;; in which the bookmark is set. string-in-front is a string of | |
83 | ;; `bookmark-search-size' chars of context in front of the point the | |
84 | ;; bookmark is set at, string-behind is the same thing after the | |
85 | ;; point. bookmark-jump will search for string-behind and | |
86 | ;; string-in-front in case the file has changed since the bookmark was | |
87 | ;; set. It will attempt to place the user before the changes, if | |
88 | ;; there were any. | |
11eb4275 | 89 | ;; |
d22d6453 RS |
90 | ;; The bookmark list is sorted lexically by default, but you can turn |
91 | ;; this off by setting bookmark-sort-flag to nil. If it is nil, then | |
92 | ;; the list will be presented in the order it is recorded | |
93 | ;; (chronologically), which is actually fairly useful as well. | |
b3bf02fa RS |
94 | |
95 | ;;; Code: | |
96 | ||
97 | ;; Added for lucid emacs compatibility, db | |
98 | (or (fboundp 'defalias) (fset 'defalias 'fset)) | |
99 | ||
d22d6453 RS |
100 | ;; suggested for lucid compatibility by david hughes: |
101 | (or (fboundp 'frame-height) (fset 'frame-height 'screen-height)) | |
102 | ||
103 | ;; some people have C-x r set to rmail or whatever. We don't want to | |
104 | ;; assume that C-x r is a prefix map just because it's distributed | |
105 | ;; that way... | |
106 | ;; These are the distribution keybindings suggested by RMS, everything | |
b3bf02fa | 107 | ;; else will be done with M-x or the menubar: |
d22d6453 | 108 | ;;;###autoload |
5f5d794a RS |
109 | (if (or (symbolp (key-binding "\C-xr")) |
110 | (fboundp 'bookmark-set)) | |
d22d6453 RS |
111 | nil |
112 | (progn (define-key ctl-x-map "rb" 'bookmark-jump) | |
113 | (define-key ctl-x-map "rm" 'bookmark-set) | |
114 | (define-key ctl-x-map "rl" 'list-bookmarks))) | |
b3bf02fa RS |
115 | |
116 | ;; define the map, so it can be bound by those who desire to do so: | |
117 | ||
d22d6453 | 118 | ;;;###autoload |
9aef3b21 RS |
119 | (defvar bookmark-map nil |
120 | "Keymap containing bindings to bookmark functions. | |
121 | It is not bound to any key by default: to bind it | |
122 | so that you have a bookmark prefix, just use `global-set-key' and bind a | |
123 | key of your choice to `bookmark-map'. All interactive bookmark | |
b3bf02fa RS |
124 | functions have a binding in this keymap.") |
125 | ||
d22d6453 | 126 | ;;;###autoload |
b3bf02fa RS |
127 | (define-prefix-command 'bookmark-map) |
128 | ||
129 | ;; Read the help on all of these functions for details... | |
d22d6453 | 130 | ;;;###autoload |
b3bf02fa | 131 | (define-key bookmark-map "x" 'bookmark-set) |
d22d6453 RS |
132 | ;;;###autoload |
133 | (define-key bookmark-map "m" 'bookmark-set) ; "m" for "mark" | |
134 | ;;;###autoload | |
b3bf02fa | 135 | (define-key bookmark-map "j" 'bookmark-jump) |
d22d6453 RS |
136 | ;;;###autoload |
137 | (define-key bookmark-map "g" 'bookmark-jump) ; "g" for "go" | |
138 | ;;;###autoload | |
b3bf02fa | 139 | (define-key bookmark-map "i" 'bookmark-insert) |
d22d6453 RS |
140 | ;;;###autoload |
141 | (define-key bookmark-map "e" 'edit-bookmarks) | |
142 | ;;;###autoload | |
b3bf02fa | 143 | (define-key bookmark-map "f" 'bookmark-locate) ; "f" for "find" |
d22d6453 | 144 | ;;;###autoload |
9aef3b21 | 145 | (define-key bookmark-map "r" 'bookmark-rename) |
d22d6453 | 146 | ;;;###autoload |
b3bf02fa | 147 | (define-key bookmark-map "d" 'bookmark-delete) |
d22d6453 | 148 | ;;;###autoload |
b3bf02fa | 149 | (define-key bookmark-map "l" 'bookmark-load) |
d22d6453 RS |
150 | ;;;###autoload |
151 | (define-key bookmark-map "w" 'bookmark-write) | |
152 | ;;;###autoload | |
b3bf02fa RS |
153 | (define-key bookmark-map "s" 'bookmark-save) |
154 | ||
7e2d009d RS |
155 | (defvar bookmark-alist () |
156 | "Association list of bookmarks. | |
157 | You probably don't want to change the value of this alist yourself; | |
158 | instead, let the various bookmark functions do it for you.") | |
159 | ||
d22d6453 RS |
160 | (defvar bookmarks-already-loaded nil) |
161 | ||
b3bf02fa RS |
162 | ;; just add the hook to make sure that people don't lose bookmarks |
163 | ;; when they kill Emacs, unless they don't want to save them. | |
b3bf02fa RS |
164 | (add-hook 'kill-emacs-hook |
165 | (function | |
d22d6453 RS |
166 | (lambda () (and (featurep 'bookmark) |
167 | bookmark-alist | |
168 | (bookmark-time-to-save-p t) | |
169 | (bookmark-save))))) | |
b3bf02fa RS |
170 | |
171 | ;; more stuff added by db. | |
d22d6453 | 172 | |
b3bf02fa | 173 | (defvar bookmark-current-bookmark nil |
9aef3b21 RS |
174 | "Name of bookmark most recently used in the current file. |
175 | It is buffer local, used to make moving a bookmark forward | |
8027e2ad | 176 | through a file easier.") |
b3bf02fa RS |
177 | |
178 | (make-variable-buffer-local 'bookmark-current-bookmark) | |
179 | ||
180 | (defvar bookmark-save-flag t | |
9aef3b21 | 181 | "*Controls when Emacs saves bookmarks to a file. |
cdf2fffe | 182 | --> nil means never save bookmarks, except when `bookmark-save' is |
9aef3b21 RS |
183 | explicitly called \(\\[bookmark-save]\). |
184 | --> t means save bookmarks when Emacs is killed. | |
185 | --> Otherise, it should be a number that is the frequency with which | |
186 | the bookmark list is saved \(i.e.: the number of times which | |
187 | Emacs' bookmark list may be modified before it is automatically | |
188 | saved.\). If it is a number, Emacs will also automatically save | |
189 | bookmarks when it is killed. | |
b3bf02fa RS |
190 | |
191 | Therefore, the way to get it to save every time you make or delete a | |
192 | bookmark is to set this variable to 1 \(or 0, which produces the same | |
193 | behavior.\) | |
194 | ||
195 | To specify the file in which to save them, modify the variable | |
9aef3b21 | 196 | bookmark-file, which is `~/.emacs-bkmrks' by default.") |
b3bf02fa RS |
197 | |
198 | (defvar bookmark-alist-modification-count 0 | |
9aef3b21 | 199 | "Number of modifications to bookmark list since it was last saved.") |
b3bf02fa | 200 | |
da0ac66a RS |
201 | (defvar bookmark-file |
202 | (if (eq system-type 'ms-dos) | |
203 | "~/emacs.bmk" ; Cannot have initial dot [Yuck!] | |
204 | "~/.emacs-bkmrks") | |
b3bf02fa RS |
205 | "*File in which to save bookmarks by default.") |
206 | ||
d22d6453 | 207 | (defvar bookmark-version-control 'nospecial |
cdf2fffe RS |
208 | "Control whether to make numbered backups of the master bookmark file. |
209 | This variable can have four values: t, nil, `never', and `nospecial'. | |
210 | The first three have the same meaning that they do for the | |
211 | variable `version-control'. | |
212 | The value `nospecial' means just use the value of `version-control'.") | |
d22d6453 | 213 | |
b3bf02fa | 214 | (defvar bookmark-completion-ignore-case t |
9aef3b21 | 215 | "*Non-nil means bookmark functions ignore case in completion.") |
b3bf02fa | 216 | |
d22d6453 | 217 | (defvar bookmark-sort-flag t |
cdf2fffe RS |
218 | "*Non-nil means display bookmarks sorted by name. |
219 | Otherwise they are displayed in LIFO order (that is, most | |
d22d6453 RS |
220 | recently set ones come first, oldest ones come last).") |
221 | ||
9aef3b21 RS |
222 | (defvar bookmark-search-size 500 |
223 | "Length of the context strings recorded on either side of a bookmark.") | |
b3bf02fa | 224 | |
b3bf02fa RS |
225 | (defvar bookmark-current-point 0) |
226 | (defvar bookmark-yank-point 0) | |
227 | (defvar bookmark-current-buffer nil) | |
228 | ||
d22d6453 | 229 | ;;;###autoload |
b3bf02fa | 230 | (defun bookmark-set (&optional parg) |
cdf2fffe | 231 | "Set a bookmark named NAME inside the visited file. |
9aef3b21 RS |
232 | With prefix arg, will not overwrite a bookmark that has the same name |
233 | as NAME if such a bookmark already exists, but instead will \"push\" | |
234 | the new bookmark onto the bookmark alist. Thus the most recently set | |
235 | bookmark with name NAME would be the one in effect at any given time, | |
236 | but the others are still there, should you decide to delete the most | |
237 | recent one. | |
b3bf02fa RS |
238 | |
239 | To yank words from the text of the buffer and use them as part of the | |
9aef3b21 | 240 | bookmark name, type C-w while setting a bookmark. Successive C-w's |
b3bf02fa RS |
241 | yank successive words. |
242 | ||
cdf2fffe | 243 | Typing C-v inserts the name of the current file being visited. Typing |
b3bf02fa RS |
244 | C-u inserts the name of the last bookmark used in the buffer \(as an |
245 | aid in using a single bookmark name to track your progress through a | |
246 | large file\). If no bookmark was used, then C-u behaves like C-v and | |
247 | inserts the name of the file being visited. | |
248 | ||
249 | Use \\[bookmark-delete] to remove bookmarks \(you give it a name, | |
250 | and it removes only the first instance of a bookmark with that name from | |
251 | the list of bookmarks.\)" | |
252 | (interactive "P") | |
253 | (if (not (bookmark-buffer-file-name)) | |
254 | (error "Buffer not visiting a file or directory.")) | |
d22d6453 | 255 | (bookmark-try-default-file) |
b3bf02fa RS |
256 | (setq bookmark-current-point (point)) |
257 | (setq bookmark-yank-point (point)) | |
258 | (setq bookmark-current-buffer (current-buffer)) | |
d22d6453 RS |
259 | (let* ((default (or bookmark-current-bookmark |
260 | (buffer-name (current-buffer)))) | |
261 | (str | |
262 | (read-from-minibuffer | |
263 | (format "Set bookmark (%s): " default) | |
264 | nil | |
265 | (let ((now-map (copy-keymap minibuffer-local-map))) | |
266 | (progn (define-key now-map "\C-w" | |
267 | 'bookmark-yank-word) | |
268 | (define-key now-map "\C-v" | |
269 | 'bookmark-insert-current-file-name) | |
270 | (define-key now-map "\C-u" | |
271 | 'bookmark-insert-current-bookmark)) | |
272 | now-map)))) | |
273 | (and (string-equal str "") (setq str default)) | |
b3bf02fa RS |
274 | (progn |
275 | (bookmark-make parg str) | |
d22d6453 RS |
276 | (setq bookmark-current-bookmark str) |
277 | (if (get-buffer "*Bookmark List*") ;rebuild the bookmark list | |
278 | (save-excursion | |
279 | (save-window-excursion | |
280 | (list-bookmarks)))) | |
b3bf02fa RS |
281 | (goto-char bookmark-current-point)))) |
282 | ||
283 | (defun bookmark-insert-current-bookmark () | |
284 | ;; insert this buffer's value of bookmark-current-bookmark, default | |
285 | ;; to file name if it's nil. | |
286 | (interactive) | |
287 | (let ((str | |
288 | (save-excursion | |
289 | (set-buffer bookmark-current-buffer) | |
290 | bookmark-current-bookmark))) | |
291 | (if str (insert str) (bookmark-insert-current-file-name)))) | |
292 | ||
293 | (defun bookmark-insert-current-file-name () | |
294 | ;; insert the name (sans path) of the current file into the bookmark | |
295 | ;; name that is being set. | |
296 | (interactive) | |
297 | (let ((str (save-excursion | |
d22d6453 RS |
298 | (set-buffer bookmark-current-buffer) |
299 | (bookmark-buffer-file-name)))) | |
b3bf02fa RS |
300 | (insert (substring |
301 | str | |
302 | (1+ (string-match | |
303 | "\\(/[^/]*\\)/*$" | |
304 | str)))))) | |
305 | ||
306 | (defun bookmark-yank-word () | |
307 | (interactive) | |
308 | ;; get the next word from the buffer and append it to the name of | |
309 | ;; the bookmark currently being set. | |
310 | (let ((string (save-excursion | |
d22d6453 RS |
311 | (set-buffer bookmark-current-buffer) |
312 | (goto-char bookmark-yank-point) | |
313 | (buffer-substring | |
314 | (point) | |
315 | (save-excursion | |
316 | (forward-word 1) | |
317 | (setq bookmark-yank-point (point))))))) | |
b3bf02fa RS |
318 | (insert string))) |
319 | ||
320 | (defun bookmark-make (parg str) | |
321 | (if (and (assoc str bookmark-alist) (not parg)) | |
322 | ;; already existing boookmark under that name and | |
323 | ;; no prefix arg means just overwrite old bookmark | |
324 | (setcdr (assoc str bookmark-alist) | |
325 | (list (bookmark-make-cell))) | |
326 | ||
327 | ;; otherwise just cons it onto the front (either the bookmark | |
328 | ;; doesn't exist already, or there is no prefix arg. In either | |
329 | ;; case, we want the new bookmark consed onto the alist...) | |
330 | ||
331 | (setq bookmark-alist | |
332 | (cons | |
333 | (list str | |
334 | (bookmark-make-cell)) | |
335 | bookmark-alist))) | |
336 | ;; Added by db | |
337 | (setq bookmark-current-bookmark str) | |
338 | (setq bookmark-alist-modification-count | |
339 | (1+ bookmark-alist-modification-count)) | |
340 | (if (bookmark-time-to-save-p) | |
341 | (bookmark-save))) | |
342 | ||
343 | (defun bookmark-make-cell () | |
344 | ;; make the cell that is the cdr of a bookmark alist element. It | |
345 | ;; looks like this: | |
346 | ;; (filename search-forward-str search-back-str point) | |
347 | (list | |
348 | (bookmark-buffer-file-name) | |
349 | (if (>= (- (point-max) (point)) bookmark-search-size) | |
350 | (buffer-substring | |
351 | (point) | |
352 | (+ (point) bookmark-search-size)) | |
353 | nil) | |
354 | (if (>= (- (point) (point-min)) bookmark-search-size) | |
355 | (buffer-substring | |
356 | (point) | |
357 | (- (point) bookmark-search-size)) | |
358 | nil) | |
359 | (point))) | |
360 | ||
361 | (defun bookmark-buffer-file-name () | |
362 | (or | |
363 | buffer-file-name | |
364 | (if (and (boundp 'dired-directory) dired-directory) | |
365 | (if (stringp dired-directory) | |
366 | dired-directory | |
367 | (car dired-directory))))) | |
368 | ||
369 | (defun bookmark-try-default-file () | |
d22d6453 RS |
370 | (and (not bookmarks-already-loaded) |
371 | (null bookmark-alist) | |
372 | (file-readable-p (expand-file-name bookmark-file)) | |
373 | (progn | |
374 | (bookmark-load bookmark-file t t) | |
375 | (setq bookmarks-already-loaded t)))) | |
376 | ||
377 | (defun bookmark-maybe-sort-alist () | |
378 | ;;Return the bookmark-alist for display. If the bookmark-sort-flag | |
379 | ;;is non-nil, then return a sorted copy of the alist. | |
380 | (if bookmark-sort-flag | |
381 | (setq bookmark-alist | |
382 | (sort (copy-alist bookmark-alist) | |
383 | (function | |
384 | (lambda (x y) (string-lessp (car x) (car y)))))))) | |
385 | ||
386 | ;;;###autoload | |
b3bf02fa | 387 | (defun bookmark-jump (str) |
9aef3b21 RS |
388 | "Jump to bookmark BOOKMARK (a point in some file). |
389 | You may have a problem using this function if the value of variable | |
390 | `bookmark-alist' is nil. If that happens, you need to load in some | |
391 | bookmarks. See help on function `bookmark-load' for more about | |
d22d6453 RS |
392 | this. |
393 | ||
cdf2fffe RS |
394 | If the file pointed to by BOOKMARK no longer exists, `bookmark-jump' |
395 | asks you to specify a different file to use instead. If you specify | |
396 | one, it also updates BOOKMARK to refer to that file." | |
d22d6453 RS |
397 | (interactive (progn (bookmark-try-default-file) |
398 | (let* ((completion-ignore-case | |
399 | bookmark-completion-ignore-case) | |
400 | (default | |
401 | (or (and | |
402 | (assoc bookmark-current-bookmark | |
403 | bookmark-alist) | |
404 | bookmark-current-bookmark) | |
405 | (and (assoc (buffer-name (current-buffer)) | |
406 | bookmark-alist) | |
407 | (buffer-name (current-buffer))))) | |
408 | (str | |
409 | (completing-read | |
410 | (if default | |
411 | (format "Jump to bookmark (%s): " | |
412 | default) | |
413 | "Jump to bookmark: ") | |
414 | bookmark-alist | |
415 | nil | |
416 | 0))) | |
417 | (and (string-equal "" str) | |
418 | (setq str default)) | |
419 | (list str)))) | |
420 | (let ((cell (bookmark-jump-noselect str))) | |
421 | (and cell | |
422 | (switch-to-buffer (car cell)) | |
423 | (goto-char (cdr cell))))) | |
424 | ||
425 | (defun bookmark-jump-noselect (str) | |
426 | ;; a leetle helper for bookmark-jump :-) | |
427 | ;; returns (BUFFER . POINT) | |
428 | (let ((whereto-list (car (cdr (assoc str bookmark-alist))))) | |
429 | (let* ((file (expand-file-name (car whereto-list))) | |
430 | (orig-file file) | |
431 | (forward-str (car (cdr whereto-list))) | |
432 | (behind-str (car (cdr (cdr whereto-list)))) | |
433 | (place (car (cdr (cdr (cdr whereto-list)))))) | |
434 | (if (or | |
435 | (file-exists-p file) | |
436 | ;; else try some common compression extensions | |
437 | ;; and Emacs better handle it right! | |
438 | (setq file | |
439 | (or | |
440 | (let ((altname (concat file ".Z"))) | |
441 | (and (file-exists-p altname) | |
442 | altname)) | |
443 | (let ((altname (concat file ".gz"))) | |
444 | (and (file-exists-p altname) | |
445 | altname)) | |
446 | (let ((altname (concat file ".z"))) | |
447 | (and (file-exists-p altname) | |
448 | altname))))) | |
449 | (save-excursion | |
450 | (set-buffer (find-file-noselect file)) | |
451 | (goto-char place) | |
452 | ;; Go searching forward first. Then, if forward-str exists and | |
453 | ;; was found in the file, we can search backward for behind-str. | |
454 | ;; Rationale is that if text was inserted between the two in the | |
455 | ;; file, it's better to be put before it so you can read it, | |
456 | ;; rather than after and remain perhaps unaware of the changes. | |
457 | (if forward-str | |
458 | (if (search-forward forward-str (point-max) t) | |
459 | (backward-char bookmark-search-size))) | |
460 | (if behind-str | |
461 | (if (search-backward behind-str (point-min) t) | |
462 | (forward-char bookmark-search-size))) | |
463 | ;; added by db | |
464 | (setq bookmark-current-bookmark str) | |
465 | (cons (current-buffer) (point))) | |
466 | (progn | |
467 | (ding) | |
468 | (if (y-or-n-p (concat (file-name-nondirectory orig-file) | |
469 | " nonexistent. Relocate \"" | |
470 | str | |
471 | "\"? ")) | |
472 | (progn | |
473 | (bookmark-relocate str) | |
474 | ;; gasp! It's a recursive function call in Emacs Lisp! | |
475 | (bookmark-jump-noselect str)) | |
476 | (message | |
477 | "Bookmark not relocated, but deleting it would be a good idea.") | |
478 | nil)))))) | |
479 | ||
480 | ;;;###autoload | |
cdf2fffe RS |
481 | (defun bookmark-relocate (bookmark) |
482 | "Relocate bookmark BOOKMARK. | |
483 | Prompt for a filename, and makes the bookmark BOOKMARK point to that | |
484 | file, instead of the one it used to point at. Useful when a file has | |
485 | been renamed after a bookmark was set in it." | |
11eb4275 RS |
486 | (interactive (let ((completion-ignore-case |
487 | bookmark-completion-ignore-case)) | |
d22d6453 RS |
488 | (progn (bookmark-try-default-file) |
489 | (list (completing-read | |
490 | "Bookmark to relocate: " | |
491 | bookmark-alist | |
492 | nil | |
493 | 0))))) | |
cdf2fffe | 494 | (let* ((bmrk (assoc bookmark bookmark-alist)) |
d22d6453 RS |
495 | (bmrk-filename (car (car (cdr bmrk)))) |
496 | (newloc (expand-file-name | |
497 | (read-file-name | |
cdf2fffe | 498 | (format "Relocate %s to: " bookmark) |
d22d6453 RS |
499 | (file-name-directory bmrk-filename))))) |
500 | (setcar (car (cdr bmrk)) newloc))) | |
501 | ||
502 | ;;;###autoload | |
503 | (defun bookmark-locate (str &optional no-insertion) | |
504 | "Insert the name of the file associated with BOOKMARK. | |
505 | Optional second arg NO-INSERTION means merely return the filename as a | |
506 | string." | |
507 | (interactive (let ((completion-ignore-case | |
508 | bookmark-completion-ignore-case)) | |
509 | (progn (bookmark-try-default-file) | |
510 | (list (completing-read | |
511 | "Insert bookmark location: " | |
512 | bookmark-alist | |
513 | nil | |
514 | 0))))) | |
515 | (let ((where (car (car (cdr (assoc str bookmark-alist)))))) | |
516 | (if no-insertion | |
517 | where | |
518 | (insert where)))) | |
519 | ||
520 | ;;;###autoload | |
9aef3b21 | 521 | (defun bookmark-rename (old &optional new) |
d22d6453 RS |
522 | "Change the name of OLD-BOOKMARK to NEWNAME. |
523 | If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME. | |
524 | If called from menubar, OLD-BOOKMARK is selected from a menu, and | |
525 | prompts for NEWNAME. | |
526 | If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was | |
527 | passed as an argument. If called with two strings, then no prompting | |
528 | is done. You must pass at least OLD-BOOKMARK when calling from Lisp. | |
9aef3b21 RS |
529 | |
530 | While you are entering the new name, consecutive C-w's insert | |
531 | consectutive words from the text of the buffer into the new bookmark | |
532 | name, and C-v inserts the name of the file." | |
11eb4275 RS |
533 | (interactive (let ((completion-ignore-case |
534 | bookmark-completion-ignore-case)) | |
d22d6453 RS |
535 | (progn (bookmark-try-default-file) |
536 | (list (completing-read "Old bookmark name: " | |
537 | bookmark-alist | |
538 | nil | |
539 | 0))))) | |
b3bf02fa RS |
540 | (progn |
541 | (setq bookmark-current-point (point)) | |
542 | (setq bookmark-yank-point (point)) | |
543 | (setq bookmark-current-buffer (current-buffer)) | |
544 | (let ((cell (assoc old bookmark-alist)) | |
545 | (str | |
9aef3b21 RS |
546 | (or new ; use second arg, if non-nil |
547 | (read-from-minibuffer | |
548 | "New name: " | |
549 | nil | |
550 | (let ((now-map (copy-keymap minibuffer-local-map))) | |
551 | (progn (define-key now-map "\C-w" | |
552 | 'bookmark-yank-word) | |
553 | (define-key now-map "\C-v" | |
554 | 'bookmark-insert-current-file-name)) | |
555 | now-map))))) | |
b3bf02fa RS |
556 | (progn |
557 | (setcar cell str) | |
558 | (setq bookmark-current-bookmark str) | |
d22d6453 RS |
559 | (if (get-buffer "*Bookmark List*") |
560 | (save-excursion (save-window-excursion (list-bookmarks)))) | |
b3bf02fa RS |
561 | (setq bookmark-alist-modification-count |
562 | (1+ bookmark-alist-modification-count)) | |
563 | (if (bookmark-time-to-save-p) | |
564 | (bookmark-save)))))) | |
565 | ||
d22d6453 | 566 | ;;;###autoload |
b3bf02fa | 567 | (defun bookmark-insert (str) |
9aef3b21 RS |
568 | "Insert the text of the file pointed to by bookmark BOOKMARK. |
569 | You may have a problem using this function if the value of variable | |
570 | `bookmark-alist' is nil. If that happens, you need to load in some | |
571 | bookmarks. See help on function `bookmark-load' for more about | |
8027e2ad | 572 | this." |
11eb4275 RS |
573 | (interactive (let ((completion-ignore-case |
574 | bookmark-completion-ignore-case)) | |
d22d6453 RS |
575 | (progn (bookmark-try-default-file) |
576 | (list (completing-read | |
577 | "Insert bookmark contents: " | |
578 | bookmark-alist | |
579 | nil | |
580 | 0))))) | |
581 | (let ((orig-point (point)) | |
582 | (str-to-insert | |
583 | (save-excursion | |
584 | (set-buffer (car (bookmark-jump-noselect str))) | |
585 | (buffer-substring (point-min) (point-max))))) | |
586 | (insert str-to-insert) | |
587 | (push-mark) | |
588 | (goto-char orig-point))) | |
589 | ||
590 | ;;;###autoload | |
b3bf02fa | 591 | (defun bookmark-delete (str) |
9aef3b21 RS |
592 | "Delete the bookmark named NAME from the bookmark list. |
593 | Removes only the first instance of a bookmark with that name. If | |
594 | there are one or more other bookmarks with the same name, they will | |
595 | not be deleted. Defaults to the \"current\" bookmark \(that is, the | |
596 | one most recently used in this file, if any\)." | |
b3bf02fa RS |
597 | (interactive (let ((completion-ignore-case |
598 | bookmark-completion-ignore-case)) | |
d22d6453 RS |
599 | (progn (bookmark-try-default-file) |
600 | (list | |
601 | (completing-read | |
602 | "Delete bookmark: " | |
603 | bookmark-alist | |
604 | nil | |
605 | 0 | |
606 | bookmark-current-bookmark))))) | |
11eb4275 RS |
607 | (let ((will-go (assoc str bookmark-alist))) |
608 | (setq bookmark-alist (delq will-go bookmark-alist)) | |
609 | ;; Added by db, nil bookmark-current-bookmark if the last | |
610 | ;; occurence has been deleted | |
b3bf02fa | 611 | (or (assoc bookmark-current-bookmark bookmark-alist) |
11eb4275 | 612 | (setq bookmark-current-bookmark nil))) |
d22d6453 RS |
613 | (if (get-buffer "*Bookmark List*") |
614 | (save-excursion (save-window-excursion (list-bookmarks)))) | |
11eb4275 RS |
615 | (setq bookmark-alist-modification-count |
616 | (1+ bookmark-alist-modification-count)) | |
617 | (if (bookmark-time-to-save-p) | |
618 | (bookmark-save))) | |
b3bf02fa RS |
619 | |
620 | (defun bookmark-time-to-save-p (&optional last-time) | |
621 | ;; By Gregory M. Saunders <saunders@cis.ohio-state.edu> | |
622 | ;; finds out whether it's time to save bookmarks to a file, by | |
623 | ;; examining the value of variable bookmark-save-flag, and maybe | |
624 | ;; bookmark-alist-modification-count. Returns t if they should be | |
625 | ;; saved, nil otherwise. if last-time is non-nil, then this is | |
626 | ;; being called when emacs is killed. | |
627 | (cond (last-time | |
628 | (and (> bookmark-alist-modification-count 0) | |
629 | bookmark-save-flag)) | |
630 | ((numberp bookmark-save-flag) | |
631 | (>= bookmark-alist-modification-count bookmark-save-flag)) | |
632 | (t | |
633 | nil))) | |
634 | ||
d22d6453 | 635 | ;;;###autoload |
b3bf02fa | 636 | (defun bookmark-write () |
cdf2fffe RS |
637 | "Write bookmarks to a specified file. |
638 | Don't use this in Lisp programs; use `bookmark-save' instead." | |
b3bf02fa | 639 | (interactive) |
d22d6453 | 640 | (bookmark-try-default-file) |
b3bf02fa RS |
641 | (bookmark-save t)) |
642 | ||
d22d6453 | 643 | ;;;###autoload |
11eb4275 | 644 | (defun bookmark-save (&optional parg file) |
9aef3b21 RS |
645 | "Save currently defined bookmarks. |
646 | Saves by default in the file defined by the variable | |
647 | `bookmark-file'. With a prefix arg, save it in file FILE. | |
b3bf02fa RS |
648 | |
649 | If you are calling this from Lisp, the two arguments are PREFIX-ARG | |
650 | and FILE, and if you just want it to write to the default file, then | |
651 | pass no arguments. Or pass in nil and FILE, and it will save in FILE | |
652 | instead. If you pass in one argument, and it is non-nil, then the | |
653 | user will be interactively queried for a file to save in. | |
654 | ||
11eb4275 | 655 | When you want to load in the bookmarks from a file, use |
9aef3b21 | 656 | \`bookmark-load\', \\[bookmark-load]. That function will prompt you |
11eb4275 | 657 | for a file, defaulting to the file defined by variable |
9aef3b21 | 658 | `bookmark-file'." |
b3bf02fa | 659 | (interactive "P") |
d22d6453 | 660 | (bookmark-try-default-file) |
b3bf02fa RS |
661 | (cond |
662 | ((and (null parg) (null file)) | |
663 | ;;whether interactive or not, write to default file | |
664 | (bookmark-write-file bookmark-file)) | |
665 | ((and (null parg) file) | |
666 | ;;whether interactive or not, write to given file | |
667 | (bookmark-write-file file)) | |
668 | ((and parg (not file)) | |
669 | ;;have been called interactively w/ prefix arg | |
670 | (let ((file (read-file-name "File to save bookmarks in: "))) | |
671 | (bookmark-write-file file))) | |
672 | (t ; someone called us with prefix-arg *and* a file, so just write to file | |
673 | (bookmark-write-file file))) | |
674 | ;; signal that we have synced the bookmark file by setting this to | |
675 | ;; 0. If there was an error at any point before, it will not get | |
676 | ;; set, which is what we want. | |
677 | (setq bookmark-alist-modification-count 0)) | |
678 | ||
679 | (defun bookmark-write-file (file) | |
680 | (save-excursion | |
d22d6453 RS |
681 | (save-window-excursion |
682 | (if (>= baud-rate 9600) | |
683 | (message (format "Saving bookmarks to file %s." file))) | |
186a7127 | 684 | (set-buffer (let ((enable-local-variables nil)) |
d23e2c3f | 685 | (find-file-noselect file))) |
d22d6453 RS |
686 | (goto-char (point-min)) |
687 | (delete-region (point-min) (point-max)) | |
688 | (print bookmark-alist (current-buffer)) | |
689 | (let ((version-control | |
690 | (cond | |
691 | ((null bookmark-version-control) nil) | |
692 | ((eq 'never bookmark-version-control) 'never) | |
693 | ((eq 'nospecial bookmark-version-control) version-control) | |
694 | (t | |
695 | t)))) | |
696 | (write-file file) | |
697 | (kill-buffer (current-buffer)))))) | |
698 | ||
699 | ;;;###autoload | |
b3bf02fa | 700 | (defun bookmark-load (file &optional revert no-msg) |
9aef3b21 RS |
701 | "Load bookmarks from FILE (which must be in bookmark format). |
702 | Appends loaded bookmarks to the front of the list of bookmarks. If | |
703 | optional second argument REVERT is non-nil, existing bookmarks are | |
704 | destroyed. Optional third arg NO-MSG means don't display any messages | |
705 | while loading. | |
b3bf02fa RS |
706 | |
707 | If you load a file that doesn't contain a proper bookmark alist, you | |
9aef3b21 | 708 | will corrupt Emacs's bookmark list. Generally, you should only load |
b3bf02fa | 709 | in files that were created with the bookmark functions in the first |
9aef3b21 | 710 | place. Your own personal bookmark file, `~/.emacs-bkmrks', is |
8027e2ad | 711 | maintained automatically by Emacs; you shouldn't need to load it |
11eb4275 | 712 | explicitly." |
b3bf02fa | 713 | (interactive |
d22d6453 RS |
714 | (progn (bookmark-try-default-file) |
715 | (list (read-file-name | |
716 | (format "Load bookmarks from: (%s) " | |
717 | bookmark-file) | |
718 | ;;Default might not be used often, | |
719 | ;;but there's no better default, and | |
720 | ;;I guess it's better than none at all. | |
721 | "~/" bookmark-file 'confirm)))) | |
b3bf02fa RS |
722 | (setq file (expand-file-name file)) |
723 | (if (file-readable-p file) | |
724 | (save-excursion | |
d22d6453 RS |
725 | (save-window-excursion |
726 | (if (and (null no-msg) (>= baud-rate 9600)) | |
727 | (message (format "Loading bookmarks from %s..." file))) | |
186a7127 | 728 | (set-buffer (let ((enable-local-variables nil)) |
d23e2c3f | 729 | (find-file-noselect file))) |
d22d6453 RS |
730 | (goto-char (point-min)) |
731 | (let ((blist (car (read-from-string | |
732 | (buffer-substring (point-min) (point-max)))))) | |
733 | (if (listp blist) | |
734 | (progn | |
735 | (if (not revert) | |
736 | (setq bookmark-alist-modification-count | |
737 | (1+ bookmark-alist-modification-count)) | |
738 | (setq bookmark-alist-modification-count 0)) | |
739 | (setq bookmark-alist | |
740 | (append blist (if (not revert) bookmark-alist))) | |
741 | (if (get-buffer "*Bookmark List*") | |
742 | (save-excursion (list-bookmarks)))) | |
743 | (error (format "Invalid bookmark list in %s." file)))) | |
744 | (kill-buffer (current-buffer))) | |
745 | (if (and (null no-msg) (>= baud-rate 9600)) | |
746 | (message (format "Loading bookmarks from %s... done" file)))) | |
b3bf02fa RS |
747 | (error (format "Cannot read bookmark file %s." file)))) |
748 | ||
d22d6453 RS |
749 | ;;;; bookmark-menu-mode stuff ;;;; |
750 | ||
751 | (defvar Bookmark-menu-bookmark-column nil) | |
752 | ||
753 | (defvar Bookmark-menu-hidden-bookmarks ()) | |
754 | ||
755 | (defvar Bookmark-menu-file-column 30 | |
756 | "*Column at which to display filenames in a buffer listing bookmarks. | |
757 | You can toggle whether files are shown with \\<Bookmark-menu-mode-map>\\[Bookmark-menu-toggle-filenames].") | |
758 | ||
759 | (defvar Bookmark-menu-toggle-filenames t | |
760 | "*Non-nil means show filenames when listing bookmarks. | |
761 | This may result in truncated bookmark names. To disable this, put the | |
762 | following in your .emacs: | |
763 | ||
764 | \(setq Bookmark-menu-toggle-filenames nil\)") | |
765 | ||
766 | (defvar Bookmark-menu-mode-map nil) | |
767 | ||
768 | (if Bookmark-menu-mode-map | |
769 | nil | |
770 | (setq Bookmark-menu-mode-map (make-keymap)) | |
771 | (suppress-keymap Bookmark-menu-mode-map t) | |
772 | (define-key Bookmark-menu-mode-map "q" 'Bookmark-menu-quit) | |
773 | (define-key Bookmark-menu-mode-map "v" 'Bookmark-menu-select) | |
774 | (define-key Bookmark-menu-mode-map "w" 'Bookmark-menu-locate) | |
775 | (define-key Bookmark-menu-mode-map "2" 'Bookmark-menu-2-window) | |
776 | (define-key Bookmark-menu-mode-map "1" 'Bookmark-menu-1-window) | |
777 | (define-key Bookmark-menu-mode-map "j" 'Bookmark-menu-this-window) | |
778 | (define-key Bookmark-menu-mode-map "f" 'Bookmark-menu-this-window) | |
779 | (define-key Bookmark-menu-mode-map "o" 'Bookmark-menu-other-window) | |
780 | (define-key Bookmark-menu-mode-map "\C-o" 'Bookmark-menu-switch-other-window) | |
781 | (define-key Bookmark-menu-mode-map "s" 'Bookmark-menu-save) | |
782 | (define-key Bookmark-menu-mode-map "k" 'Bookmark-menu-delete) | |
783 | (define-key Bookmark-menu-mode-map "\C-d" 'Bookmark-menu-delete-backwards) | |
784 | (define-key Bookmark-menu-mode-map "x" 'Bookmark-menu-execute) | |
785 | (define-key Bookmark-menu-mode-map "\C-k" 'Bookmark-menu-delete) | |
786 | (define-key Bookmark-menu-mode-map "d" 'Bookmark-menu-delete) | |
787 | (define-key Bookmark-menu-mode-map " " 'next-line) | |
788 | (define-key Bookmark-menu-mode-map "n" 'next-line) | |
789 | (define-key Bookmark-menu-mode-map "p" 'previous-line) | |
790 | (define-key Bookmark-menu-mode-map "\177" 'Bookmark-menu-backup-unmark) | |
791 | (define-key Bookmark-menu-mode-map "?" 'describe-mode) | |
792 | (define-key Bookmark-menu-mode-map "u" 'Bookmark-menu-unmark) | |
793 | (define-key Bookmark-menu-mode-map "m" 'Bookmark-menu-mark) | |
794 | (define-key Bookmark-menu-mode-map "l" 'Bookmark-menu-load) | |
795 | (define-key Bookmark-menu-mode-map "r" 'Bookmark-menu-rename) | |
796 | (define-key Bookmark-menu-mode-map "t" 'Bookmark-menu-toggle-filenames)) | |
797 | ||
798 | ;; Bookmark Menu mode is suitable only for specially formatted data. | |
799 | (put 'Bookmark-menu-mode 'mode-class 'special) | |
800 | ||
801 | ;; need to display whether or not bookmark exists as a buffer in flag | |
802 | ;; column. | |
803 | ||
804 | ;; Format: | |
805 | ;; FLAGS BOOKMARK (/FILE/NAME/HERE/WHAT/REGEXP/TO/USE?) | |
806 | ;; goto bookmark-column and then search till "(/[^)]*)$" or "(/.*)$" ? | |
807 | ||
808 | ;;;###autoload | |
809 | (defalias 'edit-bookmarks 'list-bookmarks) | |
810 | ||
811 | ;;;###autoload | |
812 | (defun list-bookmarks () | |
813 | "Display a list of existing bookmarks. | |
814 | The list is displayed in a buffer named `*Bookmark List*'. | |
cdf2fffe RS |
815 | The leftmost column displays a `D' if the bookmark is flagged for |
816 | deletion, or `>' if it is flagged for displaying." | |
d22d6453 RS |
817 | (interactive) |
818 | (bookmark-try-default-file) | |
819 | (switch-to-buffer (get-buffer-create "*Bookmark List*")) | |
820 | (let ((buffer-read-only nil)) | |
821 | (delete-region (point-max) (point-min)) | |
822 | (goto-char (point-min)) ;sure are playing it safe... | |
823 | (insert "% Bookmark\n- --------\n") | |
824 | (bookmark-maybe-sort-alist) | |
825 | (let ((lst bookmark-alist)) | |
826 | (while lst | |
827 | (insert | |
828 | (concat " " (car (car lst)) "\n")) | |
829 | (setq lst (cdr lst))))) | |
830 | (goto-char (point-min)) | |
831 | (forward-line 2) | |
832 | (bookmark-menu-mode) | |
833 | (if Bookmark-menu-toggle-filenames | |
834 | (Bookmark-menu-toggle-filenames t))) | |
835 | ||
836 | (defun bookmark-menu-mode () | |
837 | "Major mode for editing a list of bookmarks. | |
838 | Each line describes one of the bookmarks in Emacs. | |
839 | Letters do not insert themselves; instead, they are commands. | |
840 | \\<Bookmark-menu-mode-map> | |
841 | \\[Bookmark-menu-mark] -- mark bookmark to be displayed. | |
842 | \\[Bookmark-menu-select] -- select bookmark of line point is on. | |
cdf2fffe | 843 | Also show bookmarks marked using `m' in other windows. |
d22d6453 RS |
844 | \\[Bookmark-menu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names). |
845 | \\[Bookmark-menu-locate] -- display (in minibuffer) location of this bookmark. | |
846 | \\[Bookmark-menu-1-window] -- select this bookmark in full-frame window. | |
847 | \\[Bookmark-menu-2-window] -- select this bookmark in one window, | |
848 | together with bookmark selected before this one in another window. | |
849 | \\[Bookmark-menu-this-window] -- select this bookmark in place of the bookmark menu buffer. | |
850 | \\[Bookmark-menu-other-window] -- select this bookmark in another window, | |
851 | so the bookmark menu bookmark remains visible in its window. | |
852 | \\[Bookmark-menu-switch-other-window] -- switch the other window to this bookmark. | |
853 | \\[Bookmark-menu-rename] -- rename this bookmark \(prompts for new name\). | |
854 | \\[Bookmark-menu-delete] -- mark this bookmark to be deleted, and move down. | |
855 | \\[Bookmark-menu-delete-backwards] -- mark this bookmark to be deleted, and move up. | |
856 | \\[Bookmark-menu-execute] -- delete marked bookmarks. | |
857 | \\[Bookmark-menu-save] -- save the current bookmark list in the default file. | |
858 | With a prefix arg, prompts for a file to save in. | |
859 | \\[Bookmark-menu-load] -- load in a file of bookmarks (prompts for file.) | |
860 | \\[Bookmark-menu-unmark] -- remove all kinds of marks from current line. | |
861 | With prefix argument, also move up one line. | |
862 | \\[Bookmark-menu-backup-unmark] -- back up a line and remove marks." | |
863 | (kill-all-local-variables) | |
864 | (use-local-map Bookmark-menu-mode-map) | |
865 | (setq truncate-lines t) | |
866 | (setq buffer-read-only t) | |
867 | (setq major-mode 'bookmark-menu-mode) | |
868 | (setq mode-name "Bookmark Menu") | |
869 | (run-hooks 'bookmark-menu-mode-hook)) | |
870 | ||
871 | (defun Bookmark-menu-toggle-filenames (&optional parg) | |
872 | "Toggle whether filenames are shown in the bookmark list. | |
873 | Optional argument SHOW means show them unconditionally." | |
874 | (interactive) | |
875 | (cond | |
876 | (parg | |
877 | (setq Bookmark-menu-toggle-filenames nil) | |
878 | (Bookmark-menu-show-filenames) | |
879 | (setq Bookmark-menu-toggle-filenames t)) | |
880 | (Bookmark-menu-toggle-filenames | |
881 | (Bookmark-menu-hide-filenames) | |
882 | (setq Bookmark-menu-toggle-filenames nil)) | |
883 | (t | |
884 | (Bookmark-menu-show-filenames) | |
885 | (setq Bookmark-menu-toggle-filenames t)))) | |
886 | ||
887 | (defun Bookmark-menu-show-filenames (&optional force) | |
888 | (if (and (not force) Bookmark-menu-toggle-filenames) | |
889 | nil ;already shown, so do nothing | |
890 | (save-excursion | |
891 | (save-window-excursion | |
892 | (goto-char (point-min)) | |
893 | (forward-line 2) | |
894 | (setq Bookmark-menu-hidden-bookmarks ()) | |
895 | (let ((buffer-read-only nil)) | |
896 | (while (< (point) (point-max)) | |
897 | (let ((bmrk (Bookmark-menu-bookmark))) | |
898 | (setq Bookmark-menu-hidden-bookmarks | |
899 | (cons bmrk Bookmark-menu-hidden-bookmarks)) | |
900 | (move-to-column Bookmark-menu-file-column t) | |
901 | (delete-region (point) (progn (end-of-line) (point))) | |
902 | (insert " ") | |
903 | (bookmark-locate bmrk) | |
904 | (forward-line 1)))))))) | |
905 | ||
906 | (defun Bookmark-menu-hide-filenames (&optional force) | |
907 | (if (and (not force) Bookmark-menu-toggle-filenames) | |
908 | ;; nothing to hide if above is nil | |
909 | (save-excursion | |
910 | (save-window-excursion | |
911 | (goto-char (point-min)) | |
912 | (forward-line 2) | |
913 | (setq Bookmark-menu-hidden-bookmarks | |
914 | (nreverse Bookmark-menu-hidden-bookmarks)) | |
915 | (save-excursion | |
916 | (goto-char (point-min)) | |
917 | (search-forward "Bookmark") | |
918 | (backward-word 1) | |
919 | (setq Bookmark-menu-bookmark-column (current-column))) | |
920 | (save-excursion | |
921 | (let ((buffer-read-only nil)) | |
922 | (while Bookmark-menu-hidden-bookmarks | |
923 | (move-to-column Bookmark-menu-bookmark-column t) | |
924 | (kill-line) | |
925 | (insert (car Bookmark-menu-hidden-bookmarks)) | |
926 | (setq Bookmark-menu-hidden-bookmarks | |
927 | (cdr Bookmark-menu-hidden-bookmarks)) | |
928 | (forward-line 1)))))))) | |
929 | ||
930 | ;; if you look at this next function from far away, it resembles a | |
931 | ;; gun. But only with this comment above... | |
932 | (defun Bookmark-menu-check-position () | |
933 | ;; Returns t if on a line with a bookmark. | |
934 | ;; Otherwise, repositions and returns t. | |
935 | ;; written by David Hughes <djh@harston.cv.com> | |
936 | ;; Mucho thanks, David! -karl | |
937 | (cond ((< (count-lines (point-min) (point)) 2) | |
938 | (goto-char (point-min)) | |
939 | (forward-line 2) | |
940 | t) | |
941 | ((and (bolp) (eobp)) | |
942 | (beginning-of-line 0) | |
943 | t) | |
944 | (t | |
945 | t))) | |
946 | ||
947 | (defun Bookmark-menu-bookmark () | |
948 | ;; return a string which is bookmark of this line. | |
949 | (if (Bookmark-menu-check-position) | |
950 | (save-excursion | |
951 | (save-window-excursion | |
952 | (goto-char (point-min)) | |
953 | (search-forward "Bookmark") | |
954 | (backward-word 1) | |
955 | (setq Bookmark-menu-bookmark-column (current-column))))) | |
956 | (if Bookmark-menu-toggle-filenames | |
957 | (Bookmark-menu-hide-filenames)) | |
958 | (save-excursion | |
959 | (save-window-excursion | |
960 | (beginning-of-line) | |
961 | (forward-char Bookmark-menu-bookmark-column) | |
962 | (prog1 | |
963 | (buffer-substring (point) | |
964 | (progn | |
965 | (end-of-line) | |
966 | (point))) | |
967 | ;; well, this is certainly crystal-clear: | |
968 | (if Bookmark-menu-toggle-filenames | |
969 | (Bookmark-menu-toggle-filenames t)))))) | |
970 | ||
971 | (defun Bookmark-menu-mark () | |
972 | "Mark bookmark on this line to be displayed by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-select] command." | |
973 | (interactive) | |
974 | (beginning-of-line) | |
975 | (if (Bookmark-menu-check-position) | |
976 | (let ((buffer-read-only nil)) | |
977 | (delete-char 1) | |
978 | (insert ?>) | |
979 | (forward-line 1)))) | |
980 | ||
981 | (defun Bookmark-menu-select () | |
982 | "Select this line's bookmark; also display bookmarks marked with `>'. | |
983 | You can mark bookmarks with the \\<Bookmark-menu-mode-map>\\[Bookmark-menu-mark] command." | |
984 | (interactive) | |
985 | (if (Bookmark-menu-check-position) | |
986 | (let ((bmrk (Bookmark-menu-bookmark)) | |
987 | (menu (current-buffer)) | |
988 | (others ()) | |
989 | tem) | |
990 | (goto-char (point-min)) | |
991 | (while (re-search-forward "^>" nil t) | |
992 | (setq tem (Bookmark-menu-bookmark)) | |
993 | (let ((buffer-read-only nil)) | |
994 | (delete-char -1) | |
995 | (insert ?\ )) | |
996 | (or (string-equal tem bmrk) | |
997 | (memq tem others) | |
998 | (setq others (cons tem others)))) | |
999 | (setq others (nreverse others) | |
1000 | tem (/ (1- (frame-height)) (1+ (length others)))) | |
1001 | (delete-other-windows) | |
1002 | (bookmark-jump bmrk) | |
1003 | (bury-buffer menu) | |
1004 | (if (equal (length others) 0) | |
1005 | nil | |
1006 | (while others | |
1007 | (split-window nil tem) | |
1008 | (other-window 1) | |
1009 | (bookmark-jump (car others)) | |
1010 | (setq others (cdr others))) | |
1011 | (other-window 1))))) | |
1012 | ||
1013 | (defun Bookmark-menu-save (parg) | |
1014 | "Save the current list into a bookmark file. | |
1015 | With a prefix arg, prompts for a file to save them in." | |
1016 | (interactive "P") | |
1017 | (save-excursion | |
1018 | (save-window-excursion | |
1019 | (bookmark-save parg)))) | |
1020 | ||
1021 | (defun Bookmark-menu-load () | |
1022 | "Load a bookmark file and rebuild list." | |
1023 | (interactive) | |
1024 | (if (Bookmark-menu-check-position) | |
1025 | (save-excursion | |
1026 | (save-window-excursion | |
1027 | (call-interactively 'bookmark-load))))) | |
1028 | ||
1029 | (defun Bookmark-menu-1-window () | |
1030 | "Select this line's bookmark, alone, in full frame." | |
1031 | (interactive) | |
1032 | (if (Bookmark-menu-check-position) | |
1033 | (progn | |
1034 | (bookmark-jump (Bookmark-menu-bookmark)) | |
1035 | (bury-buffer (other-buffer)) | |
1036 | (delete-other-windows)))) | |
1037 | ||
1038 | (defun Bookmark-menu-2-window () | |
1039 | "Select this line's bookmark, with previous buffer in second window." | |
1040 | (interactive) | |
1041 | (if (Bookmark-menu-check-position) | |
1042 | (let ((bmrk (Bookmark-menu-bookmark)) | |
1043 | (menu (current-buffer)) | |
1044 | (pop-up-windows t)) | |
1045 | (delete-other-windows) | |
1046 | (switch-to-buffer (other-buffer)) | |
1047 | (let ((buff (car (bookmark-jump-noselect bmrk)))) | |
1048 | (pop-to-buffer buff)) | |
1049 | (bury-buffer menu)))) | |
1050 | ||
1051 | (defun Bookmark-menu-this-window () | |
1052 | "Select this line's bookmark in this window." | |
1053 | (interactive) | |
1054 | (if (Bookmark-menu-check-position) | |
1055 | (bookmark-jump (Bookmark-menu-bookmark)))) | |
1056 | ||
1057 | (defun Bookmark-menu-other-window () | |
1058 | "Select this line's bookmark in other window, leaving bookmark menu visible." | |
1059 | (interactive) | |
1060 | (if (Bookmark-menu-check-position) | |
1061 | (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark))))) | |
1062 | (switch-to-buffer-other-window buff)))) | |
1063 | ||
1064 | (defun Bookmark-menu-switch-other-window () | |
1065 | "Make the other window select this line's bookmark. | |
1066 | The current window remains selected." | |
1067 | (interactive) | |
1068 | (if (Bookmark-menu-check-position) | |
1069 | (let ((buff (car (bookmark-jump-noselect (Bookmark-menu-bookmark))))) | |
1070 | (display-buffer buff)))) | |
1071 | ||
1072 | (defun Bookmark-menu-quit () | |
1073 | "Quit the bookmark menu." | |
1074 | (interactive) | |
1075 | (let ((buffer (current-buffer))) | |
1076 | (switch-to-buffer (other-buffer)) | |
1077 | (bury-buffer buffer))) | |
1078 | ||
1079 | (defun Bookmark-menu-unmark (&optional backup) | |
1080 | "Cancel all requested operations on bookmark on this line and move down. | |
1081 | Optional ARG means move up." | |
1082 | (interactive "P") | |
1083 | (beginning-of-line) | |
1084 | (if (Bookmark-menu-check-position) | |
1085 | (progn | |
1086 | (let ((buffer-read-only nil)) | |
1087 | (delete-char 1) | |
1088 | ;; any flags to reset according to circumstances? How about a | |
1089 | ;; flag indicating whether this bookmark is being visited? | |
1090 | ;; well, we don't have this now, so maybe later. | |
1091 | (insert " ")) | |
1092 | (forward-line (if backup -1 1))))) | |
1093 | ||
1094 | (defun Bookmark-menu-backup-unmark () | |
1095 | "Move up and cancel all requested operations on bookmark on line above." | |
1096 | (interactive) | |
1097 | (forward-line -1) | |
1098 | (if (Bookmark-menu-check-position) | |
1099 | (progn | |
1100 | (Bookmark-menu-unmark) | |
1101 | (forward-line -1)))) | |
1102 | ||
1103 | (defun Bookmark-menu-delete () | |
1104 | "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command." | |
1105 | (interactive) | |
1106 | (beginning-of-line) | |
1107 | (if (Bookmark-menu-check-position) | |
1108 | (let ((buffer-read-only nil)) | |
1109 | (delete-char 1) | |
1110 | (insert ?D) | |
1111 | (forward-line 1)))) | |
1112 | ||
1113 | (defun Bookmark-menu-delete-backwards () | |
cdf2fffe RS |
1114 | "Mark bookmark on this line to be deleted by \\<Bookmark-menu-mode-map>\\[Bookmark-menu-execute] command. |
1115 | Then move up one line" | |
d22d6453 RS |
1116 | (interactive) |
1117 | (Bookmark-menu-delete) | |
1118 | (forward-line -2) | |
1119 | (if (Bookmark-menu-check-position) | |
1120 | (forward-line 1))) | |
1121 | ||
1122 | (defun Bookmark-menu-execute () | |
1123 | "Delete bookmarks marked with \\<Buffer-menu-mode-map>\\[Buffer-menu-delete] commands." | |
1124 | (interactive) | |
1125 | (let ((hide-em Bookmark-menu-toggle-filenames)) | |
1126 | (if hide-em (Bookmark-menu-hide-filenames)) | |
1127 | (setq Bookmark-menu-toggle-filenames nil) | |
1128 | (goto-char (point-min)) | |
1129 | (forward-line 1) | |
1130 | (let ((deaders ())) | |
1131 | (while (re-search-forward "^D" (point-max) t) | |
1132 | (setq deaders (cons (Bookmark-menu-bookmark) deaders))) | |
1133 | (mapcar (lambda (str) | |
1134 | (setq bookmark-alist | |
1135 | (delq (assoc str bookmark-alist) bookmark-alist))) | |
1136 | deaders)) | |
1137 | (list-bookmarks) | |
1138 | (goto-char (point-min)) | |
1139 | (forward-line 2) | |
1140 | (setq Bookmark-menu-toggle-filenames hide-em) | |
1141 | (if Bookmark-menu-toggle-filenames | |
1142 | (Bookmark-menu-toggle-filenames t)))) | |
1143 | ||
1144 | (defun Bookmark-menu-rename () | |
1145 | "Rename bookmark on current line. Prompts for a new name." | |
1146 | (interactive) | |
1147 | (if (Bookmark-menu-check-position) | |
1148 | (let ((bmrk (Bookmark-menu-bookmark)) | |
1149 | (thispoint (point))) | |
1150 | (bookmark-rename bmrk) | |
1151 | (list-bookmarks) | |
1152 | (goto-char thispoint)))) | |
1153 | ||
1154 | (defun Bookmark-menu-locate () | |
1155 | "Display location of this bookmark. Displays in the minibuffer." | |
1156 | (interactive) | |
1157 | (if (Bookmark-menu-check-position) | |
1158 | (let ((bmrk (Bookmark-menu-bookmark))) | |
1159 | (message (bookmark-locate bmrk t))))) | |
1160 | ||
11eb4275 RS |
1161 | ;;;; bookmark menu bar stuff ;;;; |
1162 | ||
d22d6453 | 1163 | (defvar bookmark-menu-bar-length 70 |
9aef3b21 | 1164 | "*Maximum length of a bookmark name displayed on a popup menu.") |
b3bf02fa | 1165 | |
11eb4275 | 1166 | (defun bookmark-make-menu-bar-alist () |
d22d6453 RS |
1167 | (bookmark-try-default-file) |
1168 | (bookmark-maybe-sort-alist) | |
b3bf02fa RS |
1169 | (if bookmark-alist |
1170 | (mapcar (lambda (cell) | |
1171 | (let ((str (car cell))) | |
1172 | (cons | |
11eb4275 RS |
1173 | (if (> (length str) bookmark-menu-bar-length) |
1174 | (substring str 0 bookmark-menu-bar-length) | |
b3bf02fa RS |
1175 | str) |
1176 | str))) | |
1177 | bookmark-alist) | |
1178 | (error "No bookmarks currently set."))) | |
1179 | ||
11eb4275 RS |
1180 | (defun bookmark-make-menu-bar-with-function (func-sym |
1181 | menu-label | |
1182 | menu-str event) | |
b3bf02fa RS |
1183 | ;; help function for making menus that need to apply a bookmark |
1184 | ;; function to a string. | |
11eb4275 | 1185 | (let* ((menu (bookmark-make-menu-bar-alist)) |
b3bf02fa RS |
1186 | (str (x-popup-menu event |
1187 | (list menu-label | |
d22d6453 RS |
1188 | (cons menu-str menu))))) |
1189 | (if str (apply func-sym (list str))))) | |
b3bf02fa | 1190 | |
cdff424e | 1191 | ;;;###autoload |
11eb4275 | 1192 | (defun bookmark-menu-bar-insert (event) |
9aef3b21 RS |
1193 | "Insert the text of the file pointed to by bookmark BOOKMARK. |
1194 | You may have a problem using this function if the value of variable | |
1195 | `bookmark-alist' is nil. If that happens, you need to load in some | |
1196 | bookmarks. See help on function `bookmark-load' for more about | |
1197 | this." | |
b3bf02fa | 1198 | (interactive "e") |
11eb4275 | 1199 | (bookmark-make-menu-bar-with-function 'bookmark-insert |
d22d6453 RS |
1200 | "Bookmark Insert Menu" |
1201 | "--- Insert Contents ---" | |
1202 | event)) | |
b3bf02fa | 1203 | |
cdff424e | 1204 | ;;;###autoload |
11eb4275 | 1205 | (defun bookmark-menu-bar-jump (event) |
9aef3b21 RS |
1206 | "Jump to bookmark BOOKMARK (a point in some file). |
1207 | You may have a problem using this function if the value of variable | |
1208 | `bookmark-alist' is nil. If that happens, you need to load in some | |
1209 | bookmarks. See help on function `bookmark-load' for more about | |
1210 | this." | |
b3bf02fa | 1211 | (interactive "e") |
11eb4275 | 1212 | (bookmark-make-menu-bar-with-function 'bookmark-jump |
d22d6453 RS |
1213 | "Bookmark Jump Menu" |
1214 | "--- Jump to Bookmark ---" | |
1215 | event)) | |
b3bf02fa | 1216 | |
cdff424e | 1217 | ;;;###autoload |
11eb4275 | 1218 | (defun bookmark-menu-bar-locate (event) |
cdf2fffe | 1219 | "Insert the name of the file associated with BOOKMARK. |
9aef3b21 | 1220 | \(This is not the same as the contents of that file\)." |
b3bf02fa | 1221 | (interactive "e") |
11eb4275 | 1222 | (bookmark-make-menu-bar-with-function 'bookmark-locate |
d22d6453 RS |
1223 | "Bookmark Locate Menu" |
1224 | "--- Insert Location ---" | |
1225 | event)) | |
b3bf02fa | 1226 | |
cdff424e | 1227 | ;;;###autoload |
11eb4275 | 1228 | (defun bookmark-menu-bar-rename (event) |
d22d6453 RS |
1229 | "Change the name of OLD-BOOKMARK to NEWNAME. |
1230 | If called from keyboard, prompts for OLD-BOOKMARK and NEWNAME. | |
1231 | If called from menubar, OLD-BOOKMARK is selected from a menu, and | |
1232 | prompts for NEWNAME. | |
1233 | If called from Lisp, prompts for NEWNAME if only OLD-BOOKMARK was | |
1234 | passed as an argument. If called with two strings, then no prompting | |
1235 | is done. You must pass at least OLD-BOOKMARK when calling from Lisp. | |
9aef3b21 RS |
1236 | |
1237 | While you are entering the new name, consecutive C-w's insert | |
1238 | consectutive words from the text of the buffer into the new bookmark | |
1239 | name, and C-v inserts the name of the file." | |
b3bf02fa | 1240 | (interactive "e") |
11eb4275 | 1241 | (bookmark-make-menu-bar-with-function 'bookmark-rename |
d22d6453 RS |
1242 | "Bookmark Rename Menu" |
1243 | "--- Rename Bookmark ---" | |
1244 | event)) | |
b3bf02fa | 1245 | |
cdff424e | 1246 | ;;;###autoload |
11eb4275 | 1247 | (defun bookmark-menu-bar-delete (event) |
9aef3b21 RS |
1248 | "Delete the bookmark named NAME from the bookmark list. |
1249 | Removes only the first instance of a bookmark with that name. If | |
1250 | there are one or more other bookmarks with the same name, they will | |
1251 | not be deleted. Defaults to the \"current\" bookmark \(that is, the | |
1252 | one most recently used in this file, if any\)." | |
b3bf02fa | 1253 | (interactive "e") |
11eb4275 | 1254 | (bookmark-make-menu-bar-with-function 'bookmark-delete |
d22d6453 RS |
1255 | "Bookmark Delete Menu" |
1256 | "--- Delete Bookmark ---" | |
1257 | event)) | |
b3bf02fa | 1258 | |
d22d6453 RS |
1259 | ;; Thanks to Roland McGrath for fixing menubar.el so that the |
1260 | ;; following works, and for explaining what to do to make it work. | |
b3bf02fa | 1261 | |
5875fdac | 1262 | ;;;###autoload |
d22d6453 | 1263 | (defvar menu-bar-bookmark-map (make-sparse-keymap "Bookmark functions.")) |
b3bf02fa | 1264 | |
5875fdac RS |
1265 | ;;;###autoload |
1266 | (fset 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map)) | |
b3bf02fa | 1267 | |
5875fdac | 1268 | ;;;###autoload |
d22d6453 RS |
1269 | (define-key menu-bar-bookmark-map [load] |
1270 | '("Load a bookmark file" . bookmark-load)) | |
b3bf02fa | 1271 | |
5875fdac | 1272 | ;;;###autoload |
d22d6453 RS |
1273 | (define-key menu-bar-bookmark-map [write] |
1274 | '("Write \(to another file\)" . bookmark-write)) | |
1275 | ||
5875fdac | 1276 | ;;;###autoload |
d22d6453 RS |
1277 | (define-key menu-bar-bookmark-map [save] |
1278 | '("Save \(in default file\)" . bookmark-save)) | |
1279 | ||
5875fdac | 1280 | ;;;###autoload |
d22d6453 RS |
1281 | (define-key menu-bar-bookmark-map [edit] |
1282 | '("Edit Bookmark List" . list-bookmarks)) | |
1283 | ||
5875fdac | 1284 | ;;;###autoload |
d22d6453 RS |
1285 | (define-key menu-bar-bookmark-map [delete] |
1286 | '("Delete bookmark" . bookmark-menu-bar-delete)) | |
1287 | ||
5875fdac | 1288 | ;;;###autoload |
d22d6453 RS |
1289 | (define-key menu-bar-bookmark-map [rename] |
1290 | '("Rename bookmark" . bookmark-menu-bar-rename)) | |
1291 | ||
5875fdac | 1292 | ;;;###autoload |
d22d6453 RS |
1293 | (define-key menu-bar-bookmark-map [locate] |
1294 | '("Insert location" . bookmark-menu-bar-locate)) | |
b3bf02fa | 1295 | |
5875fdac | 1296 | ;;;###autoload |
d22d6453 RS |
1297 | (define-key menu-bar-bookmark-map [insert] |
1298 | '("Insert contents" . bookmark-menu-bar-insert)) | |
1299 | ||
5875fdac | 1300 | ;;;###autoload |
d22d6453 RS |
1301 | (define-key menu-bar-bookmark-map [set] |
1302 | '("Set bookmark" . bookmark-set)) | |
1303 | ||
5875fdac | 1304 | ;;;###autoload |
d22d6453 RS |
1305 | (define-key menu-bar-bookmark-map [jump] |
1306 | '("Jump to bookmark" . bookmark-menu-bar-jump)) | |
d22d6453 RS |
1307 | |
1308 | (fset 'menu-bar-bookmark-map (symbol-value 'menu-bar-bookmark-map)) | |
1309 | ||
1310 | ;;;; end bookmark menu-bar stuff ;;;; | |
b3bf02fa | 1311 | |
b3bf02fa RS |
1312 | (provide 'bookmark) |
1313 | ||
11eb4275 | 1314 | ;;; bookmark.el ends here |