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