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