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