Fix some issues with `window-divider-mode'
[emacs.git] / lisp / bookmark.el
Commit [+]AuthorDateLineData
e8af40ee Pavel Janík2001-07-16 12:23:00 +00001;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1993-1997, 2001-2015 Free Software Foundation, Inc.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00004
60d0378e
KF
Karl Fogel1996-01-05 21:57:54 +00005;; Author: Karl Fogel <kfogel@red-bean.com>
6;; Maintainer: Karl Fogel <kfogel@red-bean.com>
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00007;; Created: July, 1993
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00008;; Keywords: bookmarks, placeholders, annotations
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00009
10;; This file is part of GNU Emacs.
11
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000012;; GNU Emacs is free software: you can redistribute it and/or modify
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +000013;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000014;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +000016
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000023;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267
EN
Erik Naggum1996-01-14 07:34:30 +000024
25;;; Commentary:
26
27;; This package is for setting "bookmarks" in files. A bookmark
28;; associates a string with a location in a certain file. Thus, you
29;; can navigate your way to that location by providing the string.
30;; See the "User Variables" section for customizations.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +000031
e3437989 Richard M. Stallman1995-03-11 02:05:20 +000032\f
e8af40ee Pavel Janík2001-07-16 12:23:00 +000033;;; Code:
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +000034
b578f267 Erik Naggum1996-01-14 07:34:30 +000035(require 'pp)
f58e0fd5 Stefan Monnier2012-07-10 07:51:54 -040036(eval-when-compile (require 'cl-lib))
b578f267 Erik Naggum1996-01-14 07:34:30 +000037
e3437989 Richard M. Stallman1995-03-11 02:05:20 +000038;;; Misc comments:
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +000039;;
e3437989 Richard M. Stallman1995-03-11 02:05:20 +000040;; If variable bookmark-use-annotations is non-nil, an annotation is
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +000041;; queried for when setting a bookmark.
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +000042;;
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +000043;; The bookmark list is sorted lexically by default, but you can turn
44;; this off by setting bookmark-sort-flag to nil. If it is nil, then
45;; the list will be presented in the order it is recorded
46;; (chronologically), which is actually fairly useful as well.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +000047
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000048;;; User Variables
49
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +000050(defgroup bookmark nil
9848f0ca Richard M. Stallman1997-04-12 03:22:41 +000051 "Setting, annotation and jumping to bookmarks."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +000052 :group 'matching)
53
54
55(defcustom bookmark-use-annotations nil
fc22668d Stefan Monnier2008-03-09 21:01:54 +000056 "If non-nil, saving a bookmark queries for an annotation in a buffer."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +000057 :type 'boolean
58 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000059
60
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +000061(defcustom bookmark-save-flag t
fc22668d Stefan Monnier2008-03-09 21:01:54 +000062 "Controls when Emacs saves bookmarks to a file.
9848f0ca Richard M. Stallman1997-04-12 03:22:41 +000063--> nil means never save bookmarks, except when `bookmark-save' is
0472835f Juanma Barranquero2010-01-02 02:05:44 +010064 explicitly called (\\[bookmark-save]).
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +000065--> t means save bookmarks when Emacs is killed.
3b526bae Karl Heuer1996-01-04 23:28:24 +000066--> Otherwise, it should be a number that is the frequency with which
0472835f Juanma Barranquero2010-01-02 02:05:44 +010067 the bookmark list is saved (i.e.: the number of times which
44e97401 Glenn Morris2012-02-28 00:17:21 -080068 Emacs's bookmark list may be modified before it is automatically
0472835f Juanma Barranquero2010-01-02 02:05:44 +010069 saved.). If it is a number, Emacs will also automatically save
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000070 bookmarks when it is killed.
71
72Therefore, the way to get it to save every time you make or delete a
0472835f
JB
Juanma Barranquero2010-01-02 02:05:44 +010073bookmark is to set this variable to 1 (or 0, which produces the same
74behavior.)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000075
76To specify the file in which to save them, modify the variable
9848f0ca Richard M. Stallman1997-04-12 03:22:41 +000077`bookmark-default-file', which is `~/.emacs.bmk' by default."
31fa1bd1 Andreas Schwab1998-06-24 08:48:57 +000078 :type '(choice (const nil) integer (other t))
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +000079 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000080
81
82(defconst bookmark-old-default-file "~/.emacs-bkmrks"
9201cc28 Lute Kamstra2008-12-03 05:48:14 +000083 "The `.emacs.bmk' file used to be called this name.")
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000084
85
e4769531 Paul Eggert2011-11-26 20:43:11 -080086;; defvared to avoid a compilation warning:
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000087(defvar bookmark-file nil
88 "Old name for `bookmark-default-file'.")
89
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +000090(defcustom bookmark-default-file
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000091 (if bookmark-file
92 ;; In case user set `bookmark-file' in her .emacs:
93 bookmark-file
398a825b Stefan Monnier2010-04-12 12:09:47 -040094 (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
fc22668d Stefan Monnier2008-03-09 21:01:54 +000095 "File in which to save bookmarks by default."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +000096 :type 'file
97 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +000098
99
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000100(defcustom bookmark-version-control 'nospecial
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000101 "Whether or not to make numbered backups of the bookmark file.
16ce6ccd Karl Fogel2012-09-30 22:32:06 -0500102It can have four values: t, nil, `never', or `nospecial'.
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000103The first three have the same meaning that they do for the
16ce6ccd
KF
Karl Fogel2012-09-30 22:32:06 -0500104variable `version-control'; the value `nospecial' (the default) means
105just use the value of `version-control'."
106 :type '(choice (const :tag "If existing" nil)
107 (const :tag "Never" never)
0a9cc82a
KF
Karl Fogel2012-09-30 23:15:48 -0500108 (const :tag "Use value of option `version-control'" nospecial)
109 (other :tag "Always" t))
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000110 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +0000111
112
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000113(defcustom bookmark-completion-ignore-case t
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000114 "Non-nil means bookmark functions ignore case in completion."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +0000115 :type 'boolean
116 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +0000117
118
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000119(defcustom bookmark-sort-flag t
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000120 "Non-nil means that bookmarks will be displayed sorted by bookmark name.
9848f0ca Richard M. Stallman1997-04-12 03:22:41 +0000121Otherwise they will be displayed in LIFO order (that is, most
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +0000122recently set ones come first, oldest ones come last)."
123 :type 'boolean
124 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +0000125
126
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000127(defcustom bookmark-automatically-show-annotations t
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000128 "Non-nil means show annotations when jumping to a bookmark."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +0000129 :type 'boolean
130 :group 'bookmark)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000131
7a78e19f Karl Fogel2013-03-07 16:33:22 -0600132(defcustom bookmark-bmenu-use-header-line t
bb098075
GM
Glenn Morris2013-12-27 18:16:05 -0800133 "Non-nil means to use an immovable header line.
134This is as opposed to inline text at the top of the buffer."
135 :version "24.4"
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -0600136 :type 'boolean
137 :group 'bookmark)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000138
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -0600139(defconst bookmark-bmenu-inline-header-height 2
140 "Number of lines used for the *Bookmark List* header
141\(only significant when `bookmark-bmenu-use-header-line' is nil\).")
aef053eb Karl Fogel2010-01-01 23:36:17 -0500142
54136282
KF
Karl Fogel2010-01-02 14:14:13 -0500143(defconst bookmark-bmenu-marks-width 2
144 "Number of columns (chars) used for the *Bookmark List* marks column,
145including the annotations column.")
aef053eb Karl Fogel2010-01-01 23:36:17 -0500146
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000147(defcustom bookmark-bmenu-file-column 30
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000148 "Column at which to display filenames in a buffer listing bookmarks.
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +0000149You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
150 :type 'integer
151 :group 'bookmark)
4eadcdf8
RS
Richard M. Stallman1995-06-18 22:46:02 +0000152
153
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000154(defcustom bookmark-bmenu-toggle-filenames t
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000155 "Non-nil means show filenames when listing bookmarks.
865fe16f Chong Yidong2012-09-17 13:41:04 +0800156A non-nil value may result in truncated bookmark names."
bbf5eb28
RS
Richard M. Stallman1997-04-12 03:18:33 +0000157 :type 'boolean
158 :group 'bookmark)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000159
c095b770
KF
Karl Fogel2013-03-07 16:35:47 -0600160(defface bookmark-menu-bookmark
161 '((t (:weight bold)))
162 "Face used to highlight bookmark names in bookmark menu buffers."
163 :group 'bookmark)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000164
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000165(defcustom bookmark-menu-length 70
fc22668d Stefan Monnier2008-03-09 21:01:54 +0000166 "Maximum length of a bookmark name displayed on a popup menu."
bbf5eb28 Richard M. Stallman1997-04-12 03:18:33 +0000167 :type 'integer
85d457c6 Markus Rost2002-11-18 00:15:37 +0000168 :group 'bookmark)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000169
26d9285f Stefan Monnier2009-11-21 04:43:10 +0000170;; FIXME: Is it really worth a customization option?
1e7d4475 Stefan Monnier2009-11-20 15:47:26 +0000171(defcustom bookmark-search-delay 0.2
26d9285f Stefan Monnier2009-11-21 04:43:10 +0000172 "Time before `bookmark-bmenu-search' updates the display."
1e7d4475 Stefan Monnier2009-11-20 15:47:26 +0000173 :group 'bookmark
a931698a Glenn Morris2013-05-08 21:40:20 -0400174 :type 'number)
1e7d4475 Stefan Monnier2009-11-20 15:47:26 +0000175
37789292
RF
Romain Francoise2005-10-19 09:45:43 +0000176(defface bookmark-menu-heading
177 '((t (:inherit font-lock-type-face)))
178 "Face used to highlight the heading in bookmark menu buffers."
179 :group 'bookmark
180 :version "22.1")
181
182
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000183;;; No user-serviceable parts beyond this point.
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000184
185;; Added for lucid emacs compatibility, db
186(or (fboundp 'defalias) (fset 'defalias 'fset))
187
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000188;; suggested for lucid compatibility by david hughes:
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000189(or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000190
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000191\f
192;;; Keymap stuff:
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000193
77bc05c7
RS
Richard M. Stallman1997-07-10 06:43:19 +0000194;; Set up these bindings dumping time *only*;
195;; if the user alters them, don't override the user when loading bookmark.el.
196
e1321788
JL
Juri Linkov2008-06-30 19:37:44 +0000197;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
198;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
199;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
36ae4ff7 Karl Fogel2007-07-15 01:34:15 +0000200
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000201;;;###autoload
79363d93
SM
Stefan Monnier2008-03-07 05:00:18 +0000202(defvar bookmark-map
203 (let ((map (make-sparse-keymap)))
204 ;; Read the help on all of these functions for details...
205 (define-key map "x" 'bookmark-set)
206 (define-key map "m" 'bookmark-set) ;"m"ark
207 (define-key map "j" 'bookmark-jump)
208 (define-key map "g" 'bookmark-jump) ;"g"o
209 (define-key map "o" 'bookmark-jump-other-window)
210 (define-key map "i" 'bookmark-insert)
211 (define-key map "e" 'edit-bookmarks)
212 (define-key map "f" 'bookmark-insert-location) ;"f"ind
213 (define-key map "r" 'bookmark-rename)
214 (define-key map "d" 'bookmark-delete)
215 (define-key map "l" 'bookmark-load)
216 (define-key map "w" 'bookmark-write)
217 (define-key map "s" 'bookmark-save)
218 map)
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +0000219 "Keymap containing bindings to bookmark functions.
220It is not bound to any key by default: to bind it
221so that you have a bookmark prefix, just use `global-set-key' and bind a
222key of your choice to `bookmark-map'. All interactive bookmark
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000223functions have a binding in this keymap.")
224
79363d93 Stefan Monnier2008-03-07 05:00:18 +0000225;;;###autoload (fset 'bookmark-map bookmark-map)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000226
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000227\f
228;;; Core variables and data structures:
7e2d009d Richard M. Stallman1993-08-14 19:52:32 +0000229(defvar bookmark-alist ()
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000230 "Association list of bookmarks and their records.
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000231Bookmark functions update the value automatically.
232You probably do NOT want to change the value yourself.
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000233
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000234The value is an alist with entries of the form
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000235
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000236 (BOOKMARK-NAME . PARAM-ALIST)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000237
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000238or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000239
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000240 BOOKMARK-NAME is the name you gave to the bookmark when creating it.
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000241
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000242 PARAM-ALIST is an alist of bookmark information. The order of the
243 entries in PARAM-ALIST is not important. The possible entries are
244 described below. An entry with a key but null value means the entry
245 is not used.
43f8b275 Stefan Monnier2008-06-25 16:51:33 +0000246
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000247 (filename . FILENAME)
43f8b275 Stefan Monnier2008-06-25 16:51:33 +0000248 (position . POS)
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000249 (front-context-string . STR-AFTER-POS)
250 (rear-context-string . STR-BEFORE-POS)
251 (handler . HANDLER)
252 (annotation . ANNOTATION)
253
254 FILENAME names the bookmarked file.
255 POS is the bookmarked buffer position (position in the file).
256 STR-AFTER-POS is buffer text that immediately follows POS.
257 STR-BEFORE-POS is buffer text that immediately precedes POS.
258 ANNOTATION is a string that describes the bookmark.
259 See options `bookmark-use-annotations' and
260 `bookmark-automatically-show-annotations'.
261 HANDLER is a function that provides the bookmark-jump behavior for a
262 specific kind of bookmark. This is the case for Info bookmarks,
263 for instance. HANDLER must accept a bookmark as argument.")
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000264
67b70de9 Karl Fogel2009-10-10 21:48:22 +0000265(defvar bookmarks-already-loaded nil
0472835f Juanma Barranquero2010-01-02 02:05:44 +0100266 "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000267
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000268
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000269;; more stuff added by db.
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000270
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000271(defvar bookmark-current-bookmark nil
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +0000272 "Name of bookmark most recently used in the current file.
273It is buffer local, used to make moving a bookmark forward
8027e2ad Richard M. Stallman1993-08-14 11:04:06 +0000274through a file easier.")
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000275
276(make-variable-buffer-local 'bookmark-current-bookmark)
277
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000278
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000279(defvar bookmark-alist-modification-count 0
9aef3b21 Richard M. Stallman1993-08-14 19:49:27 +0000280 "Number of modifications to bookmark list since it was last saved.")
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000281
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000282
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000283(defvar bookmark-search-size 16
9aef3b21 Richard M. Stallman1993-08-14 19:49:27 +0000284 "Length of the context strings recorded on either side of a bookmark.")
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000285
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000286
67b70de9
KF
Karl Fogel2009-10-10 21:48:22 +0000287(defvar bookmark-current-buffer nil
288 "The buffer in which a bookmark is currently being set or renamed.
289Functions that insert strings into the minibuffer use this to know
ce0fcefa
JL
Juri Linkov2012-08-08 11:48:57 +0300290the source buffer for that information; see `bookmark-yank-word'
291for example.")
67b70de9
KF
Karl Fogel2009-10-10 21:48:22 +0000292
293
294(defvar bookmark-yank-point 0
295 "The next point from which to pull source text for `bookmark-yank-word'.
0472835f Juanma Barranquero2010-01-02 02:05:44 +0100296This point is in `bookmark-current-buffer'.")
67b70de9 Karl Fogel2009-10-10 21:48:22 +0000297
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000298
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +0000299(defvar bookmark-quit-flag nil
300 "Non nil make `bookmark-bmenu-search' quit immediately.")
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000301\f
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -0500302;; Helper functions and macros.
303
304(defmacro with-buffer-modified-unmodified (&rest body)
305 "Run BODY while preserving the buffer's `buffer-modified-p' state."
306 (let ((was-modified (make-symbol "was-modified")))
307 `(let ((,was-modified (buffer-modified-p)))
308 (unwind-protect
309 (progn ,@body)
310 (set-buffer-modified-p ,was-modified)))))
311
312;; Only functions below, in this page and the next one (file formats),
313;; need to know anything about the format of bookmark-alist entries.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000314;; Everyone else should go through them.
315
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -0500316(defun bookmark-name-from-full-record (bookmark-record)
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500317 "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
318one element from `bookmark-alist'."
319 (car bookmark-record))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000320
321
322(defun bookmark-all-names ()
323 "Return a list of all current bookmark names."
324 (bookmark-maybe-load-default-file)
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -0500325 (mapcar 'bookmark-name-from-full-record bookmark-alist))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000326
327
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500328(defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
329 "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
330If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
331bookmark record in `bookmark-alist'; return it if found, otherwise
332error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
333just return it."
43f8b275 Stefan Monnier2008-06-25 16:51:33 +0000334 (cond
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500335 ((consp bookmark-name-or-record) bookmark-name-or-record)
336 ((stringp bookmark-name-or-record)
337 (or (assoc-string bookmark-name-or-record bookmark-alist
338 bookmark-completion-ignore-case)
339 (unless noerror (error "Invalid bookmark %s"
340 bookmark-name-or-record))))))
341
342
343(defun bookmark-get-bookmark-record (bookmark-name-or-record)
344 "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
345`bookmark-alist' (that is, all information but the name)."
346 (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +0000347 ;; The bookmark objects can either look like (NAME ALIST) or
348 ;; (NAME . ALIST), so we have to distinguish the two here.
349 (if (and (null (cdr alist)) (consp (caar alist)))
350 (car alist) alist)))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000351
352
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500353(defun bookmark-set-name (bookmark-name-or-record newname)
354 "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
355 (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000356
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500357(defun bookmark-prop-get (bookmark-name-or-record prop)
358 "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
359 (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
02a5ba27 Stefan Monnier2008-03-08 22:07:23 +0000360
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500361(defun bookmark-prop-set (bookmark-name-or-record prop val)
362 "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
363 (let ((cell (assq
364 prop (bookmark-get-bookmark-record bookmark-name-or-record))))
02a5ba27
SM
Stefan Monnier2008-03-08 22:07:23 +0000365 (if cell
366 (setcdr cell val)
eff37c5e Karl Fogel2010-12-08 03:09:27 -0500367 (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
02a5ba27 Stefan Monnier2008-03-08 22:07:23 +0000368 (list (cons prop val))))))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000369
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500370(defun bookmark-get-annotation (bookmark-name-or-record)
371 "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
372 (bookmark-prop-get bookmark-name-or-record 'annotation))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000373
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500374(defun bookmark-set-annotation (bookmark-name-or-record ann)
375 "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
376 (bookmark-prop-set bookmark-name-or-record 'annotation ann))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000377
378
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500379(defun bookmark-get-filename (bookmark-name-or-record)
380 "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
381 (bookmark-prop-get bookmark-name-or-record 'filename))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000382
383
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500384(defun bookmark-set-filename (bookmark-name-or-record filename)
385 "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
386 (bookmark-prop-set bookmark-name-or-record 'filename filename))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000387
388
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500389(defun bookmark-get-position (bookmark-name-or-record)
390 "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
391 (bookmark-prop-get bookmark-name-or-record 'position))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000392
393
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500394(defun bookmark-set-position (bookmark-name-or-record position)
395 "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
396 (bookmark-prop-set bookmark-name-or-record 'position position))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000397
398
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500399(defun bookmark-get-front-context-string (bookmark-name-or-record)
400 "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
401 (bookmark-prop-get bookmark-name-or-record 'front-context-string))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000402
403
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500404(defun bookmark-set-front-context-string (bookmark-name-or-record string)
405 "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
406 (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000407
408
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500409(defun bookmark-get-rear-context-string (bookmark-name-or-record)
410 "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
411 (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000412
413
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500414(defun bookmark-set-rear-context-string (bookmark-name-or-record string)
415 "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
416 (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000417
418
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500419(defun bookmark-get-handler (bookmark-name-or-record)
420 "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
421 (bookmark-prop-get bookmark-name-or-record 'handler))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000422
4c0b5ad1
KF
Karl Fogel1995-03-13 22:50:35 +0000423(defvar bookmark-history nil
424 "The history list for bookmark functions.")
425
426
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000427(defun bookmark-completing-read (prompt &optional default)
428 "Prompting with PROMPT, read a bookmark name in completion.
429PROMPT will get a \": \" stuck on the end no matter what, so you
430probably don't want to include one yourself.
7d688336
TV
Thierry Volpiatto2013-04-19 01:11:16 -0400431Optional arg DEFAULT is a string to return if the user input is empty.
432If DEFAULT is nil then return empty string for empty input."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000433 (bookmark-maybe-load-default-file) ; paranoia
175d0960 Stefan Monnier2003-08-11 17:30:47 +0000434 (if (listp last-nonmenu-event)
fd4489f0
CY
Chong Yidong2009-11-08 23:01:45 +0000435 (bookmark-menu-popup-paned-menu t prompt
436 (if bookmark-sort-flag
437 (sort (bookmark-all-names)
438 'string-lessp)
439 (bookmark-all-names)))
175d0960 Stefan Monnier2003-08-11 17:30:47 +0000440 (let* ((completion-ignore-case bookmark-completion-ignore-case)
7d688336 Thierry Volpiatto2013-04-19 01:11:16 -0400441 (default (unless (equal "" default) default))
b861e45e
SM
Stefan Monnier2009-10-28 03:09:11 +0000442 (prompt (concat prompt (if default
443 (format " (%s): " default)
7d688336
TV
Thierry Volpiatto2013-04-19 01:11:16 -0400444 ": "))))
445 (completing-read prompt
446 (lambda (string pred action)
447 (if (eq action 'metadata)
448 '(metadata (category . bookmark))
449 (complete-with-action
450 action bookmark-alist string pred)))
451 nil 0 nil 'bookmark-history default))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000452
453
4c0b5ad1
KF
Karl Fogel1995-03-13 22:50:35 +0000454(defmacro bookmark-maybe-historicize-string (string)
455 "Put STRING into the bookmark prompt history, if caller non-interactive.
456We need this because sometimes bookmark functions are invoked from
457menus, so `completing-read' never gets a chance to set `bookmark-history'."
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000458 `(or
32226619 Juanma Barranquero2009-10-02 03:48:36 +0000459 (called-interactively-p 'interactive)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000460 (setq bookmark-history (cons ,string bookmark-history))))
4c0b5ad1 Karl Fogel1995-03-13 22:50:35 +0000461
43f8b275 Stefan Monnier2008-06-25 16:51:33 +0000462(defvar bookmark-make-record-function 'bookmark-make-record-default
e0385bf4
KF
Karl Fogel2008-03-07 05:44:37 +0000463 "A function that should be called to create a bookmark record.
464Modes may set this variable buffer-locally to enable bookmarking of
465locations that should be treated specially, such as Info nodes,
466news posts, images, pdf documents, etc.
1666a6b3 Tassilo Horn2007-12-26 08:53:06 +0000467
32a091dd Stefan Monnier2008-03-09 03:05:34 +0000468The function will be called with no arguments.
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +0000469It should signal a user error if it is unable to construct a record for
470the current location.
136894c8
SM
Stefan Monnier2008-03-09 21:35:01 +0000471
472The returned record should be a cons cell of the form (NAME . ALIST)
473where ALIST is as described in `bookmark-alist' and may typically contain
5c1b3e94
KF
Karl Fogel2009-10-04 23:48:33 +0000474a special cons (handler . HANDLER-FUNC) which specifies the handler function
475that should be used instead of `bookmark-default-handler' to open this
476bookmark. See the documentation for `bookmark-alist' for more.
136894c8
SM
Stefan Monnier2008-03-09 21:35:01 +0000477
478NAME is a suggested name for the constructed bookmark. It can be nil
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +0000479in which case a default heuristic will be used. The function can also
480equivalently just return ALIST without NAME.")
136894c8
SM
Stefan Monnier2008-03-09 21:35:01 +0000481
482(defun bookmark-make-record ()
483 "Return a new bookmark record (NAME . ALIST) for the current location."
484 (let ((record (funcall bookmark-make-record-function)))
c3512092
LL
Leo Liu2013-07-23 08:58:28 +0800485 ;; Set up default name if the function does not provide one.
486 (unless (stringp (car record))
487 (if (car record) (push nil record))
488 (setcar record (or bookmark-current-bookmark (bookmark-buffer-name))))
ce0fcefa
JL
Juri Linkov2012-08-08 11:48:57 +0300489 ;; Set up defaults.
490 (bookmark-prop-set
491 record 'defaults
492 (delq nil (delete-dups (append (bookmark-prop-get record 'defaults)
493 (list bookmark-current-bookmark
c3512092
LL
Leo Liu2013-07-23 08:58:28 +0800494 (car record)
495 (bookmark-buffer-name))))))
496 record))
136894c8
SM
Stefan Monnier2008-03-09 21:35:01 +0000497
498(defun bookmark-store (name alist no-overwrite)
499 "Store the bookmark NAME with data ALIST.
500If NO-OVERWRITE is non-nil and another bookmark of the same name already
501exists in `bookmark-alist', record the new bookmark without throwing away the
502old one."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000503 (bookmark-maybe-load-default-file)
88415bfc Karl Heuer1995-07-17 23:19:51 +0000504 (let ((stripped-name (copy-sequence name)))
4211e434 Dan Nicolaescu2007-09-22 03:11:04 +0000505 (or (featurep 'xemacs)
09fd6588
KF
Karl Fogel1995-12-30 19:52:40 +0000506 ;; XEmacs's `set-text-properties' doesn't work on
507 ;; free-standing strings, apparently.
508 (set-text-properties 0 (length stripped-name) nil stripped-name))
2ef435bf
SM
Stefan Monnier2008-11-21 19:29:16 +0000509 (if (and (not no-overwrite)
510 (bookmark-get-bookmark stripped-name 'noerror))
3b526bae Karl Heuer1996-01-04 23:28:24 +0000511 ;; already existing bookmark under that name and
88415bfc Karl Heuer1995-07-17 23:19:51 +0000512 ;; no prefix arg means just overwrite old bookmark
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +0000513 ;; Use the new (NAME . ALIST) format.
514 (setcdr (bookmark-get-bookmark stripped-name) alist)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000515
88415bfc
KH
Karl Heuer1995-07-17 23:19:51 +0000516 ;; otherwise just cons it onto the front (either the bookmark
517 ;; doesn't exist already, or there is no prefix arg. In either
518 ;; case, we want the new bookmark consed onto the alist...)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000519
43f8b275 Stefan Monnier2008-06-25 16:51:33 +0000520 (push (cons stripped-name alist) bookmark-alist))
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000521
88415bfc
KH
Karl Heuer1995-07-17 23:19:51 +0000522 ;; Added by db
523 (setq bookmark-current-bookmark stripped-name)
524 (setq bookmark-alist-modification-count
525 (1+ bookmark-alist-modification-count))
526 (if (bookmark-time-to-save-p)
136894c8 Stefan Monnier2008-03-09 21:35:01 +0000527 (bookmark-save))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000528
136894c8
SM
Stefan Monnier2008-03-09 21:35:01 +0000529 (setq bookmark-current-bookmark stripped-name)
530 (bookmark-bmenu-surreptitiously-rebuild-list)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000531
e44fa724 Karl Fogel2010-07-14 11:57:54 -0400532(defun bookmark-make-record-default (&optional no-file no-context posn)
32a091dd Stefan Monnier2008-03-09 03:05:34 +0000533 "Return the record describing the location of a new bookmark.
e44fa724
KF
Karl Fogel2010-07-14 11:57:54 -0400534Point should be at the buffer in which the bookmark is being set,
535and normally should be at the position where the bookmark is desired,
536but see the optional arguments for other possibilities.
537
538If NO-FILE is non-nil, then only return the subset of the
539record that pertains to the location within the buffer, leaving off
540the part that records the filename.
541
542If NO-CONTEXT is non-nil, do not include the front- and rear-context
543strings in the record -- the position is enough.
544
545If POSN is non-nil, record POSN as the point instead of `(point)'."
546 `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
547 ,@(unless no-context `((front-context-string
548 . ,(if (>= (- (point-max) (point))
549 bookmark-search-size)
550 (buffer-substring-no-properties
551 (point)
552 (+ (point) bookmark-search-size))
553 nil))))
554 ,@(unless no-context `((rear-context-string
555 . ,(if (>= (- (point) (point-min))
556 bookmark-search-size)
557 (buffer-substring-no-properties
558 (point)
559 (- (point) bookmark-search-size))
560 nil))))
561 (position . ,(or posn (point)))))
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000562
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000563\f
564;;; File format stuff
565
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000566;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
567;; the bookmark file format -- please don't. The current format
568;; should be extensible enough. If you feel the need to change it,
569;; please discuss it with other Emacs developers first.
570;;
571;; The format of `bookmark-alist' has changed twice in its lifetime.
572;; This comment describes the three formats, FIRST, SECOND, and
573;; CURRENT.
574;;
575;; The FIRST format was used prior to Emacs 20:
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000576;;
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000577;; ((BOOKMARK-NAME (FILENAME
185ae1f1
SM
Stefan Monnier2008-04-05 18:01:52 +0000578;; STRING-IN-FRONT
579;; STRING-BEHIND
580;; POINT))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000581;; ...)
582;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000583;; The SECOND format was introduced in Emacs 20:
584;;
585;; ((BOOKMARK-NAME ((filename . FILENAME)
586;; (position . POS)
587;; (front-context-string . STR-AFTER-POS)
588;; (rear-context-string . STR-BEFORE-POS)
589;; (annotation . ANNOTATION)
590;; (whatever . VALUE)
591;; ...
592;; ))
593;; ...)
594;;
595;; The CURRENT format was introduced in Emacs 22:
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000596;;
185ae1f1 Stefan Monnier2008-04-05 18:01:52 +0000597;; ((BOOKMARK-NAME (filename . FILENAME)
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000598;; (position . POS)
599;; (front-context-string . STR-AFTER-POS)
600;; (rear-context-string . STR-BEFORE-POS)
185ae1f1
SM
Stefan Monnier2008-04-05 18:01:52 +0000601;; (annotation . ANNOTATION)
602;; (whatever . VALUE)
603;; ...
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000604;; )
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000605;; ...)
606;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000607;; Both FIRST and SECOND have the same level of nesting: the cadr of a
608;; bookmark record is a list of entry information. FIRST and SECOND
609;; differ in the form of the record information: FIRST uses a list of
610;; atoms, and SECOND uses an alist. In the FIRST format, the order of
611;; the list elements matters. In the SECOND format, the order of the
612;; alist elements is unimportant. The SECOND format facilitates the
613;; addition of new kinds of elements, to support new kinds of
614;; bookmarks or code evolution.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000615;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000616;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
617;; saving one cons cell per bookmark: the cadr of a bookmark record is
618;; no longer a cons. Why that change was made remains a mystery --
619;; just be aware of it. (Be aware too that this explanatory comment
620;; was incorrect in Emacs 22 and Emacs 23.1.)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000621;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000622;; To deal with the change from FIRST format to SECOND, conversion
623;; code was added, and it is still in use. See
624;; `bookmark-maybe-upgrade-file-format'.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000625;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000626;; No conversion from SECOND to CURRENT is done. Instead, the code
627;; handles both formats OK. It must continue to do so.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000628;;
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000629;; See the doc string of `bookmark-alist' for information about the
630;; elements that define a bookmark (e.g. `filename').
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000631
632
633(defconst bookmark-file-format-version 1
634 "The current version of the format used by bookmark files.
635You should never need to change this.")
636
637
638(defconst bookmark-end-of-version-stamp-marker
639 "-*- End Of Bookmark File Format Version Stamp -*-\n"
640 "This string marks the end of the version stamp in a bookmark file.")
641
642
643(defun bookmark-alist-from-buffer ()
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000644 "Return a `bookmark-alist' (in any format) from the current buffer.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000645The buffer must of course contain bookmark format information.
646Does not care from where in the buffer it is called, and does not
647affect point."
648 (save-excursion
649 (goto-char (point-min))
650 (if (search-forward bookmark-end-of-version-stamp-marker nil t)
651 (read (current-buffer))
652 ;; Else we're dealing with format version 0
653 (if (search-forward "(" nil t)
654 (progn
655 (forward-char -1)
656 (read (current-buffer)))
657 ;; Else no hope of getting information here.
a3ad20c2 Karl Heuer1996-01-25 06:02:21 +0000658 (error "Not bookmark format")))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000659
660
661(defun bookmark-upgrade-version-0-alist (old-list)
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000662 "Upgrade a version 0 alist OLD-LIST to the current version."
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000663 (mapcar
664 (lambda (bookmark)
665 (let* ((name (car bookmark))
666 (record (car (cdr bookmark)))
667 (filename (nth 0 record))
668 (front-str (nth 1 record))
669 (rear-str (nth 2 record))
670 (position (nth 3 record))
671 (ann (nth 4 record)))
672 (list
673 name
8a946354
SS
Sam Steingold2001-11-27 15:52:52 +0000674 `((filename . ,filename)
675 (front-context-string . ,(or front-str ""))
676 (rear-context-string . ,(or rear-str ""))
677 (position . ,position)
678 (annotation . ,ann)))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000679 old-list))
680
681
682(defun bookmark-upgrade-file-format-from-0 ()
683 "Upgrade a bookmark file of format 0 (the original format) to format 1.
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000684This expects to be called from `point-min' in a bookmark file."
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000685 (message "Upgrading bookmark format from 0 to %d..."
686 bookmark-file-format-version)
687 (let* ((old-list (bookmark-alist-from-buffer))
688 (new-list (bookmark-upgrade-version-0-alist old-list)))
689 (delete-region (point-min) (point-max))
690 (bookmark-insert-file-format-version-stamp)
691 (pp new-list (current-buffer))
692 (save-buffer))
693 (goto-char (point-min))
60d0378e Karl Fogel1996-01-05 21:57:54 +0000694 (message "Upgrading bookmark format from 0 to %d...done"
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000695 bookmark-file-format-version)
696 )
697
698
699(defun bookmark-grok-file-format-version ()
700 "Return an integer which is the file-format version of this bookmark file.
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000701This expects to be called from `point-min' in a bookmark file."
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000702 (if (looking-at "^;;;;")
703 (save-excursion
704 (save-match-data
705 (re-search-forward "[0-9]")
706 (forward-char -1)
707 (read (current-buffer))))
708 ;; Else this is format version 0, the original one, which didn't
709 ;; even have version stamps.
710 0))
711
712
713(defun bookmark-maybe-upgrade-file-format ()
714 "Check the file-format version of this bookmark file.
715If the version is not up-to-date, upgrade it automatically.
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000716This expects to be called from `point-min' in a bookmark file."
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000717 (let ((version (bookmark-grok-file-format-version)))
718 (cond
719 ((= version bookmark-file-format-version)
720 ) ; home free -- version is current
721 ((= version 0)
722 (bookmark-upgrade-file-format-from-0))
723 (t
a3ad20c2 Karl Heuer1996-01-25 06:02:21 +0000724 (error "Bookmark file format version strangeness")))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000725
726
727(defun bookmark-insert-file-format-version-stamp ()
5e0e5feb Richard M. Stallman1996-05-09 17:34:55 +0000728 "Insert text indicating current version of bookmark file format."
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000729 (insert
730 (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
731 bookmark-file-format-version))
732 (insert ";;; This format is meant to be slightly human-readable;\n"
733 ";;; nevertheless, you probably don't want to edit it.\n"
734 ";;; "
735 bookmark-end-of-version-stamp-marker))
736
737
738;;; end file-format stuff
739
740\f
9a9f1fdd
KF
Karl Fogel2005-11-12 20:46:10 +0000741;;; Generic helpers.
742
743(defun bookmark-maybe-message (fmt &rest args)
744 "Apply `message' to FMT and ARGS, but only if the display is fast enough."
745 (if (>= baud-rate 9600)
746 (apply 'message fmt args)))
747
748\f
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000749;;; Core code:
750
fc22668d
SM
Stefan Monnier2008-03-09 21:01:54 +0000751(defvar bookmark-minibuffer-read-name-map
752 (let ((map (make-sparse-keymap)))
753 (set-keymap-parent map minibuffer-local-map)
754 (define-key map "\C-w" 'bookmark-yank-word)
fc22668d
SM
Stefan Monnier2008-03-09 21:01:54 +0000755 map))
756
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000757;;;###autoload
13901bcb Karl Fogel2009-10-05 01:35:34 +0000758(defun bookmark-set (&optional name no-overwrite)
825382c0 Karl Fogel2009-10-05 01:47:00 +0000759 "Set a bookmark named NAME at the current location.
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +0000760If name is nil, then prompt the user.
761
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000762With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
763existing bookmark that has the same name as NAME, but instead push the
764new bookmark onto the bookmark alist. The most recently set bookmark
765with name NAME is thus the one in effect at any given time, but the
766others are still there, should the user decide to delete the most
767recent one.
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000768
769To yank words from the text of the buffer and use them as part of the
9aef3b21 Richard M. Stallman1993-08-14 19:49:27 +0000770bookmark name, type C-w while setting a bookmark. Successive C-w's
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000771yank successive words.
772
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000773Typing C-u inserts (at the bookmark name prompt) the name of the last
774bookmark used in the document where the new bookmark is being set;
775this helps you use a single bookmark name to track progress through a
776large document. If there is no prior bookmark for this document, then
777C-u inserts an appropriate name based on the buffer or file.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000778
0472835f Juanma Barranquero2010-01-02 02:05:44 +0100779Use \\[bookmark-delete] to remove bookmarks (you give it a name and
7c85c02b Karl Fogel2009-10-25 02:07:45 +0000780it removes only the first instance of a bookmark with that name from
0472835f Juanma Barranquero2010-01-02 02:05:44 +0100781the list of bookmarks.)"
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +0000782 (interactive (list nil current-prefix-arg))
8f2eaa8a
TV
Thierry Volpiatto2010-07-14 12:41:40 -0400783 (unwind-protect
784 (let* ((record (bookmark-make-record))
ce0fcefa
JL
Juri Linkov2012-08-08 11:48:57 +0300785 ;; `defaults' is a transient element of the
786 ;; extensible format described above in the section
787 ;; `File format stuff'. Bookmark record functions
788 ;; can use it to specify a list of default values
789 ;; accessible via M-n while reading a bookmark name.
790 (defaults (bookmark-prop-get record 'defaults))
791 (default (if (consp defaults) (car defaults) defaults)))
792
793 (if defaults
794 ;; Don't store default values in the record.
795 (setq record (assq-delete-all 'defaults record))
796 ;; When no defaults in the record, use its first element.
797 (setq defaults (car record) default defaults))
8f2eaa8a
TV
Thierry Volpiatto2010-07-14 12:41:40 -0400798
799 (bookmark-maybe-load-default-file)
800 ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
801 ;; if they have been already set in another buffer. (e.g gnus-art).
802 (unless (and bookmark-yank-point
803 bookmark-current-buffer)
804 (setq bookmark-yank-point (point))
805 (setq bookmark-current-buffer (current-buffer)))
806
807 (let ((str
808 (or name
809 (read-from-minibuffer
810 (format "Set bookmark (%s): " default)
811 nil
812 bookmark-minibuffer-read-name-map
ce0fcefa Juri Linkov2012-08-08 11:48:57 +0300813 nil nil defaults))))
8f2eaa8a
TV
Thierry Volpiatto2010-07-14 12:41:40 -0400814 (and (string-equal str "") (setq str default))
815 (bookmark-store str (cdr record) no-overwrite)
816
817 ;; Ask for an annotation buffer for this bookmark
818 (when bookmark-use-annotations
819 (bookmark-edit-annotation str))))
820 (setq bookmark-yank-point nil)
821 (setq bookmark-current-buffer nil)))
822
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000823
824(defun bookmark-kill-line (&optional newline-too)
825 "Kill from point to end of line.
826If optional arg NEWLINE-TOO is non-nil, delete the newline too.
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000827Does not affect the kill ring."
5ed619e0 Glenn Morris2010-11-06 13:23:42 -0700828 (let ((eol (line-end-position)))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000829 (delete-region (point) eol)
830 (if (and newline-too (looking-at "\n"))
831 (delete-char 1))))
832
833
4c0b5ad1 Karl Fogel1995-03-13 22:50:35 +0000834;; Defvars to avoid compilation warnings:
a7e83b26
SM
Stefan Monnier2008-03-09 21:16:26 +0000835(defvar bookmark-annotation-name nil
836 "Variable holding the name of the bookmark.
837This is used in `bookmark-edit-annotation' to record the bookmark
838whose annotation is being edited.")
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000839
840
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500841(defun bookmark-default-annotation-text (bookmark-name)
842 "Return default annotation text for BOOKMARK-NAME.
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +0000843The default annotation text is simply some text explaining how to use
844annotations."
eff37c5e Karl Fogel2010-12-08 03:09:27 -0500845 (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000846 "# All lines which start with a '#' will be deleted.\n"
847 "# Type C-c C-c when done.\n#\n"
848 "# Author: " (user-full-name) " <" (user-login-name) "@"
849 (system-name) ">\n"
850 "# Date: " (current-time-string) "\n"))
851
852
e5bd0a28
SM
Stefan Monnier2012-05-12 23:05:06 -0400853(define-obsolete-variable-alias 'bookmark-read-annotation-text-func
854 'bookmark-edit-annotation-text-func "23.1")
a7e83b26 Stefan Monnier2008-03-09 21:16:26 +0000855(defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
5e0e5feb Richard M. Stallman1996-05-09 17:34:55 +0000856 "Function to return default text to use for a bookmark annotation.
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +0000857It takes one argument, the name of the bookmark, as a string.")
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000858
a7e83b26
SM
Stefan Monnier2008-03-09 21:16:26 +0000859(defvar bookmark-edit-annotation-mode-map
860 (let ((map (make-sparse-keymap)))
861 (set-keymap-parent map text-mode-map)
862 (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
863 map)
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000864 "Keymap for editing an annotation of a bookmark.")
865
1b3b87df Stefan Monnier2013-09-10 23:31:56 -0400866(defun bookmark-insert-annotation (bookmark-name-or-record)
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500867 (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
868 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +0000869 (if (and annotation (not (string-equal annotation "")))
1b3b87df
SM
Stefan Monnier2013-09-10 23:31:56 -0400870 (insert annotation))))
871
872(define-derived-mode bookmark-edit-annotation-mode
873 text-mode "Edit Bookmark Annotation"
874 "Mode for editing the annotation of bookmarks.
875When you have finished composing, type \\[bookmark-send-annotation].
876
877\\{bookmark-edit-annotation-mode-map}")
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000878
879
880(defun bookmark-send-edited-annotation ()
dc5dcc00
JB
Juanma Barranquero2004-04-15 23:13:16 +0000881 "Use buffer contents as annotation for a bookmark.
882Lines beginning with `#' are ignored."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000883 (interactive)
1b3b87df Stefan Monnier2013-09-10 23:31:56 -0400884 (if (not (derived-mode-p 'bookmark-edit-annotation-mode))
a3ad20c2 Karl Heuer1996-01-25 06:02:21 +0000885 (error "Not in bookmark-edit-annotation-mode"))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000886 (goto-char (point-min))
887 (while (< (point) (point-max))
888 (if (looking-at "^#")
889 (bookmark-kill-line t)
890 (forward-line 1)))
fc22668d
SM
Stefan Monnier2008-03-09 21:01:54 +0000891 ;; Take no chances with text properties.
892 (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500893 (bookmark-name bookmark-annotation-name))
894 (bookmark-set-annotation bookmark-name annotation)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -0500895 (setq bookmark-alist-modification-count
896 (1+ bookmark-alist-modification-count))
67b70de9 Karl Fogel2009-10-10 21:48:22 +0000897 (bookmark-bmenu-surreptitiously-rebuild-list))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000898 (kill-buffer (current-buffer)))
899
900
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500901(defun bookmark-edit-annotation (bookmark-name-or-record)
902 "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
175d0960 Stefan Monnier2003-08-11 17:30:47 +0000903 (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
1b3b87df
SM
Stefan Monnier2013-09-10 23:31:56 -0400904 (bookmark-insert-annotation bookmark-name-or-record)
905 (bookmark-edit-annotation-mode)
906 (set (make-local-variable 'bookmark-annotation-name)
907 bookmark-name-or-record))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000908
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000909
4cd51172 Richard M. Stallman1995-03-11 20:17:37 +0000910(defun bookmark-buffer-name ()
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +0000911 "Return the name of the current buffer in a form usable as a bookmark name.
912If the buffer is associated with a file or directory, use that name."
4cd51172 Richard M. Stallman1995-03-11 20:17:37 +0000913 (cond
4cd51172
RS
Richard M. Stallman1995-03-11 20:17:37 +0000914 ;; Or are we a file?
915 (buffer-file-name (file-name-nondirectory buffer-file-name))
916 ;; Or are we a directory?
917 ((and (boundp 'dired-directory) dired-directory)
918 (let* ((dirname (if (stringp dired-directory)
919 dired-directory
920 (car dired-directory)))
921 (idx (1- (length dirname))))
922 ;; Strip the trailing slash.
923 (if (= ?/ (aref dirname idx))
924 (file-name-nondirectory (substring dirname 0 idx))
925 ;; Else return the current-buffer
926 (buffer-name (current-buffer)))))
927 ;; If all else fails, use the buffer's name.
928 (t
929 (buffer-name (current-buffer)))))
930
931
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000932(defun bookmark-yank-word ()
6d6a5b7e
KF
Karl Fogel2009-10-10 21:54:13 +0000933 "Get the next word from buffer `bookmark-current-buffer' and append
934it to the name of the bookmark currently being set, advancing
0472835f Juanma Barranquero2010-01-02 02:05:44 +0100935`bookmark-yank-point' by one word."
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000936 (interactive)
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +0000937 (let ((string (with-current-buffer bookmark-current-buffer
938 (goto-char bookmark-yank-point)
939 (buffer-substring-no-properties
940 (point)
941 (progn
942 (forward-word 1)
943 (setq bookmark-yank-point (point)))))))
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +0000944 (insert string)))
945
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +0000946(defun bookmark-buffer-file-name ()
d4432055 Stefan Monnier2008-03-13 01:57:05 +0000947 "Return the current buffer's file in a way useful for bookmarks."
3889f0fa
SM
Stefan Monnier2008-11-21 19:07:21 +0000948 ;; Abbreviate the path, both so it's shorter and so it's more
949 ;; portable. E.g., the user's home dir might be a different
950 ;; path on different machines, but "~/" will still reach it.
9201cc28 Lute Kamstra2008-12-03 05:48:14 +0000951 (abbreviate-file-name
3889f0fa
SM
Stefan Monnier2008-11-21 19:07:21 +0000952 (cond
953 (buffer-file-name buffer-file-name)
954 ((and (boundp 'dired-directory) dired-directory)
955 (if (stringp dired-directory)
956 dired-directory
957 (car dired-directory)))
958 (t (error "Buffer not visiting a file or directory")))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000959
960
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000961(defun bookmark-maybe-load-default-file ()
13901bcb Karl Fogel2009-10-05 01:35:34 +0000962 "If bookmarks have not been loaded from the default place, load them."
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +0000963 (and (not bookmarks-already-loaded)
964 (null bookmark-alist)
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000965 (prog2
966 (and
967 ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
968 ;; to be renamed.
fe8c0b47
SM
Stefan Monnier2010-02-18 13:52:36 -0500969 (file-exists-p bookmark-old-default-file)
970 (not (file-exists-p bookmark-default-file))
971 (rename-file bookmark-old-default-file
972 bookmark-default-file))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +0000973 ;; return t so the `and' will continue...
974 t)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +0000975
fe8c0b47 Stefan Monnier2010-02-18 13:52:36 -0500976 (file-readable-p bookmark-default-file)
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +0000977 (bookmark-load bookmark-default-file t t)
978 (setq bookmarks-already-loaded t)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000979
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000980
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000981(defun bookmark-maybe-sort-alist ()
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +0000982 "Return `bookmark-alist' for display.
983If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000984 (if bookmark-sort-flag
1de49d4e
TTN
Thien-Thi Nguyen2007-07-16 16:59:58 +0000985 (sort (copy-alist bookmark-alist)
986 (function
987 (lambda (x y) (string-lessp (car x) (car y)))))
988 bookmark-alist))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +0000989
e3437989 Richard M. Stallman1995-03-11 02:05:20 +0000990
866a7257
CD
Carsten Dominik2005-08-24 10:18:29 +0000991(defvar bookmark-after-jump-hook nil
992 "Hook run after `bookmark-jump' jumps to a bookmark.
993Useful for example to unhide text in `outline-mode'.")
994
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -0500995(defun bookmark--jump-via (bookmark-name-or-record display-function)
996 "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
997current buffer as argument.
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +0000998
999After calling DISPLAY-FUNCTION, set window point to the point specified
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001000by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
1001and then show any annotations for this bookmark."
1002 (bookmark-handle-bookmark bookmark-name-or-record)
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001003 (save-current-buffer
1004 (funcall display-function (current-buffer)))
1005 (let ((win (get-buffer-window (current-buffer) 0)))
1006 (if win (set-window-point win (point))))
1007 ;; FIXME: we used to only run bookmark-after-jump-hook in
1008 ;; `bookmark-jump' itself, but in none of the other commands.
1009 (run-hooks 'bookmark-after-jump-hook)
1010 (if bookmark-automatically-show-annotations
1011 ;; if there is an annotation for this bookmark,
1012 ;; show it in a buffer.
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001013 (bookmark-show-annotation bookmark-name-or-record)))
0f219a97 Juanma Barranquero2008-11-21 10:32:39 +00001014
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001015
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001016;;;###autoload
e6227f06 Karl Fogel2009-10-09 05:54:57 +00001017(defun bookmark-jump (bookmark &optional display-func)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001018 "Jump to bookmark BOOKMARK (a point in some file).
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001019You may have a problem using this function if the value of variable
1020`bookmark-alist' is nil. If that happens, you need to load in some
1021bookmarks. See help on function `bookmark-load' for more about
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001022this.
1023
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001024If the file pointed to by BOOKMARK no longer exists, you will be asked
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +00001025if you wish to give the bookmark a new location, and `bookmark-jump'
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001026will then jump to the new location, as well as recording it in place
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +00001027of the old one in the permanent bookmark record.
1028
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001029BOOKMARK is usually a bookmark name (a string). It can also be a
1030bookmark record, but this is usually only done by programmatic callers.
e6227f06
KF
Karl Fogel2009-10-09 05:54:57 +00001031
1032If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +00001033bookmark. It defaults to `switch-to-buffer'. A typical value for
1034DISPLAY-FUNC would be `switch-to-buffer-other-window'."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001035 (interactive
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00001036 (list (bookmark-completing-read "Jump to bookmark"
1037 bookmark-current-bookmark)))
5d8d3a34
RS
Richard M. Stallman2004-12-27 16:41:59 +00001038 (unless bookmark
1039 (error "No bookmark specified"))
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +00001040 (bookmark-maybe-historicize-string bookmark)
e6227f06 Karl Fogel2009-10-09 05:54:57 +00001041 (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001042
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001043
241ab2b5
KF
Karl Fogel2007-07-13 18:20:55 +00001044;;;###autoload
1045(defun bookmark-jump-other-window (bookmark)
13901bcb Karl Fogel2009-10-05 01:35:34 +00001046 "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
241ab2b5 Karl Fogel2007-07-13 18:20:55 +00001047 (interactive
e6227f06
KF
Karl Fogel2009-10-09 05:54:57 +00001048 (list (bookmark-completing-read "Jump to bookmark (in another window)"
1049 bookmark-current-bookmark)))
1050 (bookmark-jump bookmark 'switch-to-buffer-other-window))
241ab2b5
KF
Karl Fogel2007-07-13 18:20:55 +00001051
1052
79363d93 Stefan Monnier2008-03-07 05:00:18 +00001053(defun bookmark-jump-noselect (bookmark)
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001054 "Return the location pointed to by BOOKMARK (see `bookmark-jump').
8f7028a8
KF
Karl Fogel2008-11-19 08:03:04 +00001055The return value has the form (BUFFER . POINT).
1056
1057Note: this function is deprecated and is present for Emacs 22
0f219a97 Juanma Barranquero2008-11-21 10:32:39 +00001058compatibility only."
59f7af81 Chong Yidong2012-09-25 12:13:02 +08001059 (declare (obsolete bookmark-handle-bookmark "23.1"))
73ba5f6d
CY
Chong Yidong2008-10-29 17:42:49 +00001060 (save-excursion
1061 (bookmark-handle-bookmark bookmark)
1062 (cons (current-buffer) (point))))
1063
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001064(defun bookmark-handle-bookmark (bookmark-name-or-record)
1065 "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
1066if it has none. This changes current buffer and point and returns nil,
abef340a Sam Steingold2011-02-01 16:22:21 -05001067or signals a `file-error'.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001068
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001069If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
1070BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
1071then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001072 (condition-case err
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001073 (funcall (or (bookmark-get-handler bookmark-name-or-record)
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001074 'bookmark-default-handler)
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001075 (bookmark-get-bookmark bookmark-name-or-record))
dbf8402b Stefan Monnier2010-02-10 15:02:54 -05001076 (bookmark-error-no-filename ;file-error
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001077 ;; We were unable to find the marked file, so ask if user wants to
1078 ;; relocate the bookmark, else remind them to consider deletion.
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001079 (when (stringp bookmark-name-or-record)
1080 ;; `bookmark-name-or-record' can be either a bookmark name
1081 ;; (from `bookmark-alist') or a bookmark object. If it's an
1082 ;; object, we assume it's a bookmark used internally by some
1083 ;; other package.
1084 (let ((file (bookmark-get-filename bookmark-name-or-record)))
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001085 (when file ;Don't know how to relocate if there's no `file'.
5feb1ba7
KF
Karl Fogel2009-10-05 04:19:04 +00001086 ;; If file is not a dir, directory-file-name just returns file.
1087 (let ((display-name (directory-file-name file)))
1088 (ding)
1089 ;; Dialog boxes can accept a file target, but usually don't
1090 ;; know how to accept a directory target (at least, this
1091 ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
1092 ;; true on Windows as well). So we suppress file dialogs
1093 ;; when relocating.
1094 (let ((use-dialog-box nil)
1095 (use-file-dialog nil))
1096 (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001097 bookmark-name-or-record "\"? "))
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001098 (progn
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001099 (bookmark-relocate bookmark-name-or-record)
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001100 ;; Try again.
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001101 (funcall (or (bookmark-get-handler bookmark-name-or-record)
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001102 'bookmark-default-handler)
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001103 (bookmark-get-bookmark bookmark-name-or-record)))
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001104 (message
0472835f Juanma Barranquero2010-01-02 02:05:44 +01001105 "Bookmark not relocated; consider removing it (%s)."
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001106 bookmark-name-or-record)
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001107 (signal (car err) (cdr err))))))))))
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001108 ;; Added by db.
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001109 (when (stringp bookmark-name-or-record)
1110 (setq bookmark-current-bookmark bookmark-name-or-record))
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001111 nil)
1112
54bd972f
SM
Stefan Monnier2013-08-09 17:22:44 -04001113(define-error 'bookmark-errors nil)
1114(define-error 'bookmark-error-no-filename
1115 "Bookmark has no associated file (or directory)" 'bookmark-errors)
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001116
13901bcb Karl Fogel2009-10-05 01:35:34 +00001117(defun bookmark-default-handler (bmk-record)
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001118 "Default handler to jump to a particular bookmark location.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001119BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001120Changes current buffer and point and returns nil, or signals a `file-error'."
5feb1ba7 Karl Fogel2009-10-05 04:19:04 +00001121 (let ((file (bookmark-get-filename bmk-record))
dbf8402b Stefan Monnier2010-02-10 15:02:54 -05001122 (buf (bookmark-prop-get bmk-record 'buffer))
5feb1ba7
KF
Karl Fogel2009-10-05 04:19:04 +00001123 (forward-str (bookmark-get-front-context-string bmk-record))
1124 (behind-str (bookmark-get-rear-context-string bmk-record))
1125 (place (bookmark-get-position bmk-record)))
dbf8402b
SM
Stefan Monnier2010-02-10 15:02:54 -05001126 (set-buffer
1127 (cond
1128 ((and file (file-readable-p file) (not (buffer-live-p buf)))
1129 (find-file-noselect file))
1130 ;; No file found. See if buffer BUF have been created.
1131 ((and buf (get-buffer buf)))
1132 (t ;; If not, raise error.
1133 (signal 'bookmark-error-no-filename (list 'stringp file)))))
1134 (if place (goto-char place))
1135 ;; Go searching forward first. Then, if forward-str exists and
1136 ;; was found in the file, we can search backward for behind-str.
1137 ;; Rationale is that if text was inserted between the two in the
1138 ;; file, it's better to be put before it so you can read it,
1139 ;; rather than after and remain perhaps unaware of the changes.
1140 (when (and forward-str (search-forward forward-str (point-max) t))
1141 (goto-char (match-beginning 0)))
1142 (when (and behind-str (search-backward behind-str (point-min) t))
1143 (goto-char (match-end 0)))
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001144 nil))
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001145
1146;;;###autoload
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001147(defun bookmark-relocate (bookmark-name)
1148 "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001149
5e0e5feb
RS
Richard M. Stallman1996-05-09 17:34:55 +00001150This makes an already existing bookmark point to that file, instead of
1151the one it used to point at. Useful when a file has been renamed
1152after a bookmark was set in it."
175d0960 Stefan Monnier2003-08-11 17:30:47 +00001153 (interactive (list (bookmark-completing-read "Bookmark to relocate")))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001154 (bookmark-maybe-historicize-string bookmark-name)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001155 (bookmark-maybe-load-default-file)
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001156 (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
fe8c0b47
SM
Stefan Monnier2010-02-18 13:52:36 -05001157 (newloc (abbreviate-file-name
1158 (expand-file-name
1159 (read-file-name
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001160 (format "Relocate %s to: " bookmark-name)
fe8c0b47 Stefan Monnier2010-02-18 13:52:36 -05001161 (file-name-directory bmrk-filename))))))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001162 (bookmark-set-filename bookmark-name newloc)
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001163 (setq bookmark-alist-modification-count
1164 (1+ bookmark-alist-modification-count))
1165 (if (bookmark-time-to-save-p)
1166 (bookmark-save))
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +00001167 (bookmark-bmenu-surreptitiously-rebuild-list)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001168
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001169
1170;;;###autoload
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001171(defun bookmark-insert-location (bookmark-name &optional no-history)
1172 "Insert the name of the file associated with BOOKMARK-NAME.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001173
4c0b5ad1
KF
Karl Fogel1995-03-13 22:50:35 +00001174Optional second arg NO-HISTORY means don't record this in the
1175minibuffer history list `bookmark-history'."
175d0960 Stefan Monnier2003-08-11 17:30:47 +00001176 (interactive (list (bookmark-completing-read "Insert bookmark location")))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001177 (or no-history (bookmark-maybe-historicize-string bookmark-name))
c095b770 Karl Fogel2013-03-07 16:35:47 -06001178 (insert (bookmark-location bookmark-name)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001179
fbd98d61 Karl Fogel1996-05-08 19:16:51 +00001180;;;###autoload
a15269c0 Karl Fogel1996-05-08 02:22:12 +00001181(defalias 'bookmark-locate 'bookmark-insert-location)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001182
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001183(defun bookmark-location (bookmark-name-or-record)
1184 "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001185 (bookmark-maybe-load-default-file)
ebb9641f
SM
Stefan Monnier2010-04-14 11:07:53 -04001186 ;; We could call the `handler' and ask for it to construct a description
1187 ;; dynamically: it would open up several new possibilities, but it
1188 ;; would have the major disadvantage of forcing to load each and
1189 ;; every handler when the user calls bookmark-menu.
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001190 (or (bookmark-prop-get bookmark-name-or-record 'location)
1191 (bookmark-get-filename bookmark-name-or-record)
ebb9641f Stefan Monnier2010-04-14 11:07:53 -04001192 "-- Unknown location --"))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001193
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001194
1195;;;###autoload
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001196(defun bookmark-rename (old-name &optional new-name)
1197 "Change the name of OLD-NAME bookmark to NEW-NAME name.
1198If called from keyboard, prompt for OLD-NAME and NEW-NAME.
1199If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001200
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001201If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
1202as an argument. If called with two strings, then no prompting is done.
1203You must pass at least OLD-NAME when calling from Lisp.
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001204
1205While you are entering the new name, consecutive C-w's insert
3b526bae Karl Heuer1996-01-04 23:28:24 +00001206consecutive words from the text of the buffer into the new bookmark
4c0b5ad1 Karl Fogel1995-03-13 22:50:35 +00001207name."
175d0960 Stefan Monnier2003-08-11 17:30:47 +00001208 (interactive (list (bookmark-completing-read "Old bookmark name")))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001209 (bookmark-maybe-historicize-string old-name)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001210 (bookmark-maybe-load-default-file)
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +00001211
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001212 (setq bookmark-yank-point (point))
1213 (setq bookmark-current-buffer (current-buffer))
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001214 (let ((final-new-name
1215 (or new-name ; use second arg, if non-nil
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001216 (read-from-minibuffer
1217 "New name: "
1218 nil
1219 (let ((now-map (copy-keymap minibuffer-local-map)))
0472835f Juanma Barranquero2010-01-02 02:05:44 +01001220 (define-key now-map "\C-w" 'bookmark-yank-word)
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001221 now-map)
1222 nil
1223 'bookmark-history))))
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001224 (bookmark-set-name old-name final-new-name)
1225 (setq bookmark-current-bookmark final-new-name)
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001226 (bookmark-bmenu-surreptitiously-rebuild-list)
1227 (setq bookmark-alist-modification-count
1228 (1+ bookmark-alist-modification-count))
1229 (if (bookmark-time-to-save-p)
1230 (bookmark-save))))
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001231
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001232
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001233;;;###autoload
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001234(defun bookmark-insert (bookmark-name)
1235 "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
1236BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001237
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001238You may have a problem using this function if the value of variable
1239`bookmark-alist' is nil. If that happens, you need to load in some
1240bookmarks. See help on function `bookmark-load' for more about
8027e2ad Richard M. Stallman1993-08-14 11:04:06 +00001241this."
175d0960 Stefan Monnier2003-08-11 17:30:47 +00001242 (interactive (list (bookmark-completing-read "Insert bookmark contents")))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001243 (bookmark-maybe-historicize-string bookmark-name)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001244 (bookmark-maybe-load-default-file)
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001245 (let ((orig-point (point))
1666a6b3 Tassilo Horn2007-12-26 08:53:06 +00001246 (str-to-insert
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001247 (save-current-buffer
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001248 (bookmark-handle-bookmark bookmark-name)
1666a6b3 Tassilo Horn2007-12-26 08:53:06 +00001249 (buffer-string))))
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001250 (insert str-to-insert)
1251 (push-mark)
1252 (goto-char orig-point)))
1253
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001254
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001255;;;###autoload
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001256(defun bookmark-delete (bookmark-name &optional batch)
1257 "Delete BOOKMARK-NAME from the bookmark list.
13901bcb Karl Fogel2009-10-05 01:35:34 +00001258
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001259Removes only the first instance of a bookmark with that name. If
1260there are one or more other bookmarks with the same name, they will
0472835f
JB
Juanma Barranquero2010-01-02 02:05:44 +01001261not be deleted. Defaults to the \"current\" bookmark (that is, the
1262one most recently used in this file, if any).
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001263Optional second arg BATCH means don't update the bookmark list buffer,
1264probably because we were called from there."
1265 (interactive
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00001266 (list (bookmark-completing-read "Delete bookmark"
1267 bookmark-current-bookmark)))
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001268 (bookmark-maybe-historicize-string bookmark-name)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001269 (bookmark-maybe-load-default-file)
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001270 (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
11eb4275
RS
Richard M. Stallman1993-08-13 00:59:51 +00001271 (setq bookmark-alist (delq will-go bookmark-alist))
1272 ;; Added by db, nil bookmark-current-bookmark if the last
3b526bae Karl Heuer1996-01-04 23:28:24 +00001273 ;; occurrence has been deleted
2ef435bf Stefan Monnier2008-11-21 19:29:16 +00001274 (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +00001275 (setq bookmark-current-bookmark nil)))
33e97a28
KF
Karl Fogel2009-10-09 06:44:40 +00001276 (unless batch
1277 (bookmark-bmenu-surreptitiously-rebuild-list))
1278 (setq bookmark-alist-modification-count
1279 (1+ bookmark-alist-modification-count))
1280 (when (bookmark-time-to-save-p)
1281 (bookmark-save)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001282
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001283
13901bcb
KF
Karl Fogel2009-10-05 01:35:34 +00001284(defun bookmark-time-to-save-p (&optional final-time)
1285 "Return t if it is time to save bookmarks to disk, nil otherwise.
1286Optional argument FINAL-TIME means this is being called when Emacs
1287is being killed, so save even if `bookmark-save-flag' is a number and
1288is greater than `bookmark-alist-modification-count'."
1289 ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
1290 (cond (final-time
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001291 (and (> bookmark-alist-modification-count 0)
1292 bookmark-save-flag))
1293 ((numberp bookmark-save-flag)
1294 (>= bookmark-alist-modification-count bookmark-save-flag))
1295 (t
1296 nil)))
1297
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001298
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001299;;;###autoload
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001300(defun bookmark-write ()
f9c81e7b
GM
Glenn Morris2014-03-22 15:36:29 -07001301 "Write bookmarks to a file (reading the file name with the minibuffer)."
1302 (declare (interactive-only bookmark-save))
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001303 (interactive)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001304 (bookmark-maybe-load-default-file)
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001305 (bookmark-save t))
1306
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001307
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001308;;;###autoload
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001309(defun bookmark-save (&optional parg file)
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001310 "Save currently defined bookmarks.
1311Saves by default in the file defined by the variable
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +00001312`bookmark-default-file'. With a prefix arg, save it in file FILE
0472835f Juanma Barranquero2010-01-02 02:05:44 +01001313\(second argument).
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001314
d32ff76a
JB
Juanma Barranquero2005-07-26 13:56:01 +00001315If you are calling this from Lisp, the two arguments are PARG and
1316FILE, and if you just want it to write to the default file, then
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001317pass no arguments. Or pass in nil and FILE, and it will save in FILE
1318instead. If you pass in one argument, and it is non-nil, then the
1319user will be interactively queried for a file to save in.
1320
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +00001321When you want to load in the bookmarks from a file, use
0472835f Juanma Barranquero2010-01-02 02:05:44 +01001322`bookmark-load', \\[bookmark-load]. That function will prompt you
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +00001323for a file, defaulting to the file defined by variable
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001324`bookmark-default-file'."
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001325 (interactive "P")
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001326 (bookmark-maybe-load-default-file)
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001327 (cond
1328 ((and (null parg) (null file))
1329 ;;whether interactive or not, write to default file
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001330 (bookmark-write-file bookmark-default-file))
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001331 ((and (null parg) file)
1332 ;;whether interactive or not, write to given file
1333 (bookmark-write-file file))
1334 ((and parg (not file))
1335 ;;have been called interactively w/ prefix arg
1336 (let ((file (read-file-name "File to save bookmarks in: ")))
1337 (bookmark-write-file file)))
1338 (t ; someone called us with prefix-arg *and* a file, so just write to file
1339 (bookmark-write-file file)))
1340 ;; signal that we have synced the bookmark file by setting this to
1341 ;; 0. If there was an error at any point before, it will not get
1342 ;; set, which is what we want.
1343 (setq bookmark-alist-modification-count 0))
1344
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001345
1346\f
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001347(defun bookmark-write-file (file)
13901bcb Karl Fogel2009-10-05 01:35:34 +00001348 "Write `bookmark-alist' to FILE."
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001349 (bookmark-maybe-message "Saving bookmarks to file %s..." file)
1350 (with-current-buffer (get-buffer-create " *Bookmarks*")
1351 (goto-char (point-min))
1352 (delete-region (point-min) (point-max))
1353 (let ((print-length nil)
e4c4abdd
KF
Karl Fogel2012-09-30 22:44:59 -05001354 (print-level nil)
1355 ;; See bug #12503 for why we bind `print-circle'. Users
1356 ;; can define their own bookmark types, which can result in
1357 ;; arbitrary Lisp objects being stored in bookmark records,
1358 ;; and some users create objects containing circularities.
1359 (print-circle t))
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001360 (bookmark-insert-file-format-version-stamp)
e3f36d03
SM
Stefan Monnier2009-09-20 14:54:17 +00001361 (insert "(")
1362 ;; Rather than a single call to `pp' we make one per bookmark.
1363 ;; Apparently `pp' has a poor algorithmic complexity, so this
1364 ;; scales a lot better. bug#4485.
0472835f Juanma Barranquero2010-01-02 02:05:44 +01001365 (dolist (i bookmark-alist) (pp i (current-buffer)))
e3f36d03 Stefan Monnier2009-09-20 14:54:17 +00001366 (insert ")")
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001367 (let ((version-control
1368 (cond
1369 ((null bookmark-version-control) nil)
1370 ((eq 'never bookmark-version-control) 'never)
1371 ((eq 'nospecial bookmark-version-control) version-control)
1372 (t t))))
1373 (condition-case nil
1374 (write-region (point-min) (point-max) file)
1375 (file-error (message "Can't write %s" file)))
1376 (kill-buffer (current-buffer))
1377 (bookmark-maybe-message
1378 "Saving bookmarks to file %s...done" file)))))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001379
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001380
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +00001381(defun bookmark-import-new-list (new-list)
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +00001382 "Add NEW-LIST of bookmarks to `bookmark-alist'.
1383Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
1384they conflict with existing bookmark names."
b861e45e
SM
Stefan Monnier2009-10-28 03:09:11 +00001385 (let ((names (bookmark-all-names)))
1386 (dolist (full-record new-list)
1387 (bookmark-maybe-rename full-record names)
1388 (setq bookmark-alist (nconc bookmark-alist (list full-record)))
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -05001389 (push (bookmark-name-from-full-record full-record) names))))
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001390
1391
1392(defun bookmark-maybe-rename (full-record names)
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +00001393 "Rename bookmark FULL-RECORD if its current name is already used.
1394This is a helper for `bookmark-import-new-list'."
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -05001395 (let ((found-name (bookmark-name-from-full-record full-record)))
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001396 (if (member found-name names)
1397 ;; We've got a conflict, so generate a new name
1398 (let ((count 2)
1399 (new-name found-name))
1400 (while (member new-name names)
1401 (setq new-name (concat found-name (format "<%d>" count)))
1402 (setq count (1+ count)))
1403 (bookmark-set-name full-record new-name)))))
1404
1405
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001406;;;###autoload
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +00001407(defun bookmark-load (file &optional overwrite no-msg)
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001408 "Load bookmarks from FILE (which must be in bookmark format).
1409Appends loaded bookmarks to the front of the list of bookmarks. If
91169ba7 Richard M. Stallman1997-08-22 19:14:10 +00001410optional second argument OVERWRITE is non-nil, existing bookmarks are
9aef3b21
RS
Richard M. Stallman1993-08-14 19:49:27 +00001411destroyed. Optional third arg NO-MSG means don't display any messages
1412while loading.
b3bf02fa
RS
Richard M. Stallman1993-08-10 20:35:31 +00001413
1414If you load a file that doesn't contain a proper bookmark alist, you
9aef3b21 Richard M. Stallman1993-08-14 19:49:27 +00001415will corrupt Emacs's bookmark list. Generally, you should only load
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001416in files that were created with the bookmark functions in the first
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001417place. Your own personal bookmark file, `~/.emacs.bmk', is
8027e2ad Richard M. Stallman1993-08-14 11:04:06 +00001418maintained automatically by Emacs; you shouldn't need to load it
91169ba7
RS
Richard M. Stallman1997-08-22 19:14:10 +00001419explicitly.
1420
1421If you load a file containing bookmarks with the same names as
1422bookmarks already present in your Emacs, the new bookmarks will get
99d8aedf Glenn Morris2014-06-08 00:41:27 -07001423unique numeric suffixes \"<2>\", \"<3>\", etc."
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001424 (interactive
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001425 (list (read-file-name
1426 (format "Load bookmarks from: (%s) "
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001427 bookmark-default-file)
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001428 ;;Default might not be used often,
1429 ;;but there's no better default, and
1430 ;;I guess it's better than none at all.
1431 "~/" bookmark-default-file 'confirm)))
fe8c0b47 Stefan Monnier2010-02-18 13:52:36 -05001432 (setq file (abbreviate-file-name (expand-file-name file)))
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001433 (if (not (file-readable-p file))
1434 (error "Cannot read bookmark file %s" file)
1435 (if (null no-msg)
1436 (bookmark-maybe-message "Loading bookmarks from %s..." file))
1437 (with-current-buffer (let ((enable-local-variables nil))
1438 (find-file-noselect file))
1439 (goto-char (point-min))
1440 (bookmark-maybe-upgrade-file-format)
1441 (let ((blist (bookmark-alist-from-buffer)))
1442 (if (listp blist)
1443 (progn
1444 (if overwrite
1445 (progn
1446 (setq bookmark-alist blist)
1447 (setq bookmark-alist-modification-count 0))
1448 ;; else
1449 (bookmark-import-new-list blist)
1450 (setq bookmark-alist-modification-count
1451 (1+ bookmark-alist-modification-count)))
1452 (if (string-equal
fe8c0b47
SM
Stefan Monnier2010-02-18 13:52:36 -05001453 (abbreviate-file-name
1454 (expand-file-name bookmark-default-file))
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001455 file)
1456 (setq bookmarks-already-loaded t))
1457 (bookmark-bmenu-surreptitiously-rebuild-list))
1458 (error "Invalid bookmark list in %s" file)))
1459 (kill-buffer (current-buffer)))
1460 (if (null no-msg)
1461 (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00001462
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001463
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001464\f
43f8b275
SM
Stefan Monnier2008-06-25 16:51:33 +00001465;;; Code supporting the dired-like bookmark menu.
1466;; Prefix is "bookmark-bmenu" for "buffer-menu":
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001467
1468
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001469(defvar bookmark-bmenu-hidden-bookmarks ())
1470
1471
b016851c
SM
Stefan Monnier2011-02-10 11:56:00 -05001472(defvar bookmark-bmenu-mode-map
1473 (let ((map (make-keymap)))
1474 (set-keymap-parent map special-mode-map)
1475 (define-key map "v" 'bookmark-bmenu-select)
1476 (define-key map "w" 'bookmark-bmenu-locate)
1477 (define-key map "2" 'bookmark-bmenu-2-window)
1478 (define-key map "1" 'bookmark-bmenu-1-window)
1479 (define-key map "j" 'bookmark-bmenu-this-window)
1480 (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
1481 (define-key map "f" 'bookmark-bmenu-this-window)
1482 (define-key map "\C-m" 'bookmark-bmenu-this-window)
1483 (define-key map "o" 'bookmark-bmenu-other-window)
1484 (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
1485 (define-key map "s" 'bookmark-bmenu-save)
1486 (define-key map "k" 'bookmark-bmenu-delete)
1487 (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
1488 (define-key map "x" 'bookmark-bmenu-execute-deletions)
1489 (define-key map "d" 'bookmark-bmenu-delete)
1490 (define-key map " " 'next-line)
1491 (define-key map "n" 'next-line)
1492 (define-key map "p" 'previous-line)
1493 (define-key map "\177" 'bookmark-bmenu-backup-unmark)
1494 (define-key map "u" 'bookmark-bmenu-unmark)
1495 (define-key map "m" 'bookmark-bmenu-mark)
1496 (define-key map "l" 'bookmark-bmenu-load)
1497 (define-key map "r" 'bookmark-bmenu-rename)
1498 (define-key map "R" 'bookmark-bmenu-relocate)
1499 (define-key map "t" 'bookmark-bmenu-toggle-filenames)
1500 (define-key map "a" 'bookmark-bmenu-show-annotation)
1501 (define-key map "A" 'bookmark-bmenu-show-all-annotations)
1502 (define-key map "e" 'bookmark-bmenu-edit-annotation)
8b340240 Leo Liu2011-05-11 03:34:51 +08001503 (define-key map "/" 'bookmark-bmenu-search)
b016851c
SM
Stefan Monnier2011-02-10 11:56:00 -05001504 (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
1505 map))
1506
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001507;; Bookmark Buffer Menu mode is suitable only for specially formatted
1508;; data.
1509(put 'bookmark-bmenu-mode 'mode-class 'special)
1510
1511
1512;; todo: need to display whether or not bookmark exists as a buffer in
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001513;; flag column.
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001514
1515;; Format:
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001516;; FLAGS BOOKMARK [ LOCATION ]
1517
1518
1519(defun bookmark-bmenu-surreptitiously-rebuild-list ()
1520 "Rebuild the Bookmark List if it exists.
1521Don't affect the buffer ring order."
1522 (if (get-buffer "*Bookmark List*")
1523 (save-excursion
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001524 (save-window-excursion
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001525 (bookmark-bmenu-list)))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001526
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001527
1528;;;###autoload
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001529(defun bookmark-bmenu-list ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001530 "Display a list of existing bookmarks.
1531The list is displayed in a buffer named `*Bookmark List*'.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001532The leftmost column displays a D if the bookmark is flagged for
1533deletion, or > if it is flagged for displaying."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001534 (interactive)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001535 (bookmark-maybe-load-default-file)
955ef430
SM
Stefan Monnier2009-10-25 18:09:57 +00001536 (let ((buf (get-buffer-create "*Bookmark List*")))
1537 (if (called-interactively-p 'interactive)
c3313451 Chong Yidong2011-09-02 12:38:40 -04001538 (switch-to-buffer buf)
955ef430 Stefan Monnier2009-10-25 18:09:57 +00001539 (set-buffer buf)))
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00001540 (let ((inhibit-read-only t))
1541 (erase-buffer)
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001542 (if (not bookmark-bmenu-use-header-line)
1543 (insert "% Bookmark\n- --------\n"))
37789292
RF
Romain Francoise2005-10-19 09:45:43 +00001544 (add-text-properties (point-min) (point)
1545 '(font-lock-face bookmark-menu-heading))
b861e45e Stefan Monnier2009-10-28 03:09:11 +00001546 (dolist (full-record (bookmark-maybe-sort-alist))
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -05001547 (let ((name (bookmark-name-from-full-record full-record))
fc9d6ad6
SM
Stefan Monnier2009-11-21 06:43:45 +00001548 (annotation (bookmark-get-annotation full-record))
1549 (start (point))
1550 end)
1551 ;; if a bookmark has an annotation, prepend a "*"
1552 ;; in the list of bookmarks.
b861e45e Stefan Monnier2009-10-28 03:09:11 +00001553 (insert (if (and annotation (not (string-equal annotation "")))
fc9d6ad6
SM
Stefan Monnier2009-11-21 06:43:45 +00001554 " *" " ")
1555 name)
1556 (setq end (point))
54136282
KF
Karl Fogel2010-01-02 14:14:13 -05001557 (put-text-property
1558 (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
fc9d6ad6
SM
Stefan Monnier2009-11-21 06:43:45 +00001559 (when (display-mouse-p)
1560 (add-text-properties
54136282 Karl Fogel2010-01-02 14:14:13 -05001561 (+ bookmark-bmenu-marks-width start) end
c095b770
KF
Karl Fogel2013-03-07 16:35:47 -06001562 '(font-lock-face bookmark-menu-bookmark
1563 mouse-face highlight
fc9d6ad6
SM
Stefan Monnier2009-11-21 06:43:45 +00001564 follow-link t
1565 help-echo "mouse-2: go to this bookmark in other window")))
1566 (insert "\n")))
b894c439 Karl Fogel2010-01-02 13:52:32 -05001567 (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
fc9d6ad6 Stefan Monnier2009-11-21 06:43:45 +00001568 (goto-char (point-min))
fc9d6ad6 Stefan Monnier2009-11-21 06:43:45 +00001569 (bookmark-bmenu-mode)
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001570 (if bookmark-bmenu-use-header-line
1571 (bookmark-bmenu-set-header)
1572 (forward-line bookmark-bmenu-inline-header-height))
2575da50
TV
Thierry Volpiatto2013-04-03 21:59:09 -04001573 (when (and bookmark-alist bookmark-bmenu-toggle-filenames)
1574 (bookmark-bmenu-toggle-filenames t))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001575
1576;;;###autoload
1577(defalias 'list-bookmarks 'bookmark-bmenu-list)
1578;;;###autoload
1579(defalias 'edit-bookmarks 'bookmark-bmenu-list)
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001580
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001581(defun bookmark-bmenu-set-header ()
1582 "Sets the immutable header line."
1583 (let ((header (concat "%% " "Bookmark")))
1584 (when bookmark-bmenu-toggle-filenames
1585 (setq header (concat header
1586 (make-string (- bookmark-bmenu-file-column
1587 (- (length header) 3)) ?\s)
1588 "File")))
1589 (let ((pos 0))
1590 (while (string-match "[ \t\n]+" header pos)
1591 (setq pos (match-end 0))
1592 (put-text-property (match-beginning 0) pos 'display
1593 (list 'space :align-to (- pos 1))
1594 header)))
1595 (put-text-property 0 2 'face 'fixed-pitch header)
1596 (setq header (concat (propertize " " 'display '(space :align-to 0))
1597 header))
1598 ;; Code derived from `buff-menu.el'.
1599 (setq header-line-format header)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001600
abef340a Sam Steingold2011-02-01 16:22:21 -05001601(define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001602 "Major mode for editing a list of bookmarks.
1603Each line describes one of the bookmarks in Emacs.
1604Letters do not insert themselves; instead, they are commands.
3b526bae Karl Heuer1996-01-04 23:28:24 +00001605Bookmark names preceded by a \"*\" have annotations.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001606\\<bookmark-bmenu-mode-map>
1607\\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
1608\\[bookmark-bmenu-select] -- select bookmark of line point is on.
1609 Also show bookmarks marked using m in other windows.
1610\\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
1611\\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
1612\\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
1613\\[bookmark-bmenu-2-window] -- select this bookmark in one window,
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001614 together with bookmark selected before this one in another window.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001615\\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
1616\\[bookmark-bmenu-other-window] -- select this bookmark in another window,
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001617 so the bookmark menu bookmark remains visible in its window.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001618\\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
0472835f
JB
Juanma Barranquero2010-01-02 02:05:44 +01001619\\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
1620\\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001621\\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00001622\\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
60d0378e Karl Fogel1996-01-05 21:57:54 +00001623\\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001624\\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001625 With a prefix arg, prompts for a file to save in.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001626\\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
1627\\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001628 With prefix argument, also move up one line.
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001629\\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
1630\\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
1631 in another buffer.
1632\\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
1633\\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001634 (setq truncate-lines t)
abef340a
SS
Sam Steingold2011-02-01 16:22:21 -05001635 (setq buffer-read-only t))
1636
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001637
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +00001638(defun bookmark-bmenu-toggle-filenames (&optional show)
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001639 "Toggle whether filenames are shown in the bookmark list.
1640Optional argument SHOW means show them unconditionally."
1641 (interactive)
1642 (cond
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +00001643 (show
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001644 (setq bookmark-bmenu-toggle-filenames nil)
1645 (bookmark-bmenu-show-filenames)
1646 (setq bookmark-bmenu-toggle-filenames t))
1647 (bookmark-bmenu-toggle-filenames
1648 (bookmark-bmenu-hide-filenames)
1649 (setq bookmark-bmenu-toggle-filenames nil))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001650 (t
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001651 (bookmark-bmenu-show-filenames)
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001652 (setq bookmark-bmenu-toggle-filenames t)))
1653 (when bookmark-bmenu-use-header-line
1654 (bookmark-bmenu-set-header)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001655
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001656
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001657(defun bookmark-bmenu-show-filenames (&optional force)
13901bcb Karl Fogel2009-10-05 01:35:34 +00001658 "In an interactive bookmark list, show filenames along with bookmarks.
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +00001659Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1660mainly for debugging, and should not be necessary in normal use."
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001661 (if (and (not force) bookmark-bmenu-toggle-filenames)
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001662 nil ;already shown, so do nothing
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001663 (with-buffer-modified-unmodified
1664 (save-excursion
1665 (save-window-excursion
1666 (goto-char (point-min))
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001667 (if (not bookmark-bmenu-use-header-line)
1668 (forward-line bookmark-bmenu-inline-header-height))
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001669 (setq bookmark-bmenu-hidden-bookmarks ())
1670 (let ((inhibit-read-only t))
1671 (while (< (point) (point-max))
1672 (let ((bmrk (bookmark-bmenu-bookmark)))
1673 (push bmrk bookmark-bmenu-hidden-bookmarks)
5ed619e0 Glenn Morris2010-11-06 13:23:42 -07001674 (let ((start (line-end-position)))
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001675 (move-to-column bookmark-bmenu-file-column t)
1676 ;; Strip off `mouse-face' from the white spaces region.
1677 (if (display-mouse-p)
1678 (remove-text-properties start (point)
1679 '(mouse-face nil help-echo nil))))
1680 (delete-region (point) (progn (end-of-line) (point)))
1681 (insert " ")
1682 ;; Pass the NO-HISTORY arg:
1683 (bookmark-insert-location bmrk t)
1684 (forward-line 1)))))))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001685
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001686
1687(defun bookmark-bmenu-hide-filenames (&optional force)
13901bcb Karl Fogel2009-10-05 01:35:34 +00001688 "In an interactive bookmark list, hide the filenames of the bookmarks.
7c85c02b
KF
Karl Fogel2009-10-25 02:07:45 +00001689Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
1690mainly for debugging, and should not be necessary in normal use."
fc9d6ad6
SM
Stefan Monnier2009-11-21 06:43:45 +00001691 (when (and (not force) bookmark-bmenu-toggle-filenames)
1692 ;; nothing to hide if above is nil
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001693 (with-buffer-modified-unmodified
1694 (save-excursion
1695 (goto-char (point-min))
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001696 (if (not bookmark-bmenu-use-header-line)
1697 (forward-line bookmark-bmenu-inline-header-height))
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001698 (setq bookmark-bmenu-hidden-bookmarks
1699 (nreverse bookmark-bmenu-hidden-bookmarks))
1700 (let ((inhibit-read-only t))
1701 (while bookmark-bmenu-hidden-bookmarks
54136282 Karl Fogel2010-01-02 14:14:13 -05001702 (move-to-column bookmark-bmenu-marks-width t)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001703 (bookmark-kill-line)
1704 (let ((name (pop bookmark-bmenu-hidden-bookmarks))
1705 (start (point)))
1706 (insert name)
54136282 Karl Fogel2010-01-02 14:14:13 -05001707 (put-text-property start (point) 'bookmark-name-prop name)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001708 (if (display-mouse-p)
1709 (add-text-properties
1710 start (point)
c095b770
KF
Karl Fogel2013-03-07 16:35:47 -06001711 '(font-lock-face bookmark-menu-bookmark
1712 mouse-face highlight
1713 follow-link t help-echo
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001714 "mouse-2: go to this bookmark in other window"))))
1715 (forward-line 1)))))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001716
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001717
aef053eb Karl Fogel2010-01-01 23:36:17 -05001718(defun bookmark-bmenu-ensure-position ()
7c85c02b Karl Fogel2009-10-25 02:07:45 +00001719 "If point is not on a bookmark line, move it to one.
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001720If before the first bookmark line, move to the first; if after the
1721last full line, move to the last full line. The return value is undefined."
7a78e19f
KF
Karl Fogel2013-03-07 16:33:22 -06001722 (cond ((and (not bookmark-bmenu-use-header-line)
1723 (< (count-lines (point-min) (point))
1724 bookmark-bmenu-inline-header-height))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001725 (goto-char (point-min))
7a78e19f Karl Fogel2013-03-07 16:33:22 -06001726 (forward-line bookmark-bmenu-inline-header-height))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001727 ((and (bolp) (eobp))
aef053eb Karl Fogel2010-01-01 23:36:17 -05001728 (beginning-of-line 0))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001729
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001730
1731(defun bookmark-bmenu-bookmark ()
13901bcb Karl Fogel2009-10-05 01:35:34 +00001732 "Return the bookmark for this line in an interactive bookmark list buffer."
aef053eb Karl Fogel2010-01-01 23:36:17 -05001733 (bookmark-bmenu-ensure-position)
5ae329f4
KF
Karl Fogel2010-01-02 00:00:55 -05001734 (save-excursion
1735 (beginning-of-line)
1736 (forward-char bookmark-bmenu-marks-width)
1737 (get-text-property (point) 'bookmark-name-prop)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001738
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001739
eff37c5e
KF
Karl Fogel2010-12-08 03:09:27 -05001740(defun bookmark-show-annotation (bookmark-name-or-record)
1741 "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001742if an annotation exists."
eff37c5e Karl Fogel2010-12-08 03:09:27 -05001743 (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
0c4371fe
KF
Karl Fogel2010-07-13 18:17:51 -04001744 (when (and annotation (not (string-equal annotation "")))
1745 (save-excursion
1746 (let ((old-buf (current-buffer)))
1747 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1748 (delete-region (point-min) (point-max))
0c4371fe
KF
Karl Fogel2010-07-13 18:17:51 -04001749 (insert annotation)
1750 (goto-char (point-min))
1751 (switch-to-buffer-other-window old-buf))))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001752
1753
1754(defun bookmark-show-all-annotations ()
1755 "Display the annotations for all bookmarks in a buffer."
b861e45e Stefan Monnier2009-10-28 03:09:11 +00001756 (save-selected-window
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001757 (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
1758 (delete-region (point-min) (point-max))
3e0fc97b Boruch Baum2015-03-25 21:43:51 -04001759 (dolist (full-record (bookmark-maybe-sort-alist))
fdbb4d85 Karl Fogel2010-12-12 11:53:24 -05001760 (let* ((name (bookmark-name-from-full-record full-record))
b861e45e
SM
Stefan Monnier2009-10-28 03:09:11 +00001761 (ann (bookmark-get-annotation full-record)))
1762 (insert (concat name ":\n"))
1763 (if (and ann (not (string-equal ann "")))
1764 ;; insert the annotation, indented by 4 spaces.
1765 (progn
1766 (save-excursion (insert ann) (unless (bolp)
1767 (insert "\n")))
1768 (while (< (point) (point-max))
1769 (beginning-of-line) ; paranoia
1770 (insert " ")
1771 (forward-line)
1772 (end-of-line))))))
1773 (goto-char (point-min))))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001774
1775
1776(defun bookmark-bmenu-mark ()
5e0e5feb Richard M. Stallman1996-05-09 17:34:55 +00001777 "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001778 (interactive)
1779 (beginning-of-line)
aef053eb Karl Fogel2010-01-01 23:36:17 -05001780 (bookmark-bmenu-ensure-position)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001781 (with-buffer-modified-unmodified
1782 (let ((inhibit-read-only t))
1783 (delete-char 1)
1784 (insert ?>)
1785 (forward-line 1)
1786 (bookmark-bmenu-ensure-position))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001787
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001788
1789(defun bookmark-bmenu-select ()
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001790 "Select this line's bookmark; also display bookmarks marked with `>'.
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001791You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001792 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001793 (let ((bmrk (bookmark-bmenu-bookmark))
1794 (menu (current-buffer))
1795 (others ())
1796 tem)
1797 (goto-char (point-min))
1798 (while (re-search-forward "^>" nil t)
1799 (setq tem (bookmark-bmenu-bookmark))
1800 (let ((inhibit-read-only t))
1801 (delete-char -1)
1802 (insert ?\s))
1803 (or (string-equal tem bmrk)
1804 (member tem others)
1805 (setq others (cons tem others))))
1806 (setq others (nreverse others)
1807 tem (/ (1- (frame-height)) (1+ (length others))))
1808 (delete-other-windows)
1809 (bookmark-jump bmrk)
1810 (bury-buffer menu)
1811 (if others
1812 (while others
1813 (split-window nil tem)
1814 (other-window 1)
1815 (bookmark-jump (car others))
1816 (setq others (cdr others)))
1817 (other-window 1))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001818
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001819
47989945
KF
Karl Fogel2010-01-02 02:08:02 -05001820(defun bookmark-bmenu-any-marks ()
1821 "Return non-nil if any bookmarks are marked in the marks column."
1822 (save-excursion
1823 (goto-char (point-min))
1824 (bookmark-bmenu-ensure-position)
1825 (catch 'found-mark
1826 (while (not (eobp))
1827 (beginning-of-line)
1828 (if (looking-at "^\\S-")
1829 (throw 'found-mark t)
1830 (forward-line 1)))
1831 nil)))
1832
1833
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001834(defun bookmark-bmenu-save (parg)
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001835 "Save the current list into a bookmark file.
1836With a prefix arg, prompts for a file to save them in."
1837 (interactive "P")
1838 (save-excursion
1839 (save-window-excursion
cc4d3cad
KF
Karl Fogel2010-01-02 13:57:02 -05001840 (bookmark-save parg)
1841 (set-buffer-modified-p nil))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001842
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001843
1844(defun bookmark-bmenu-load ()
1845 "Load the bookmark file and rebuild the bookmark menu-buffer."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001846 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001847 (bookmark-bmenu-ensure-position)
1848 (save-excursion
1849 (save-window-excursion
1850 ;; This will call `bookmark-bmenu-list'
1851 (call-interactively 'bookmark-load))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001852
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001853
1854(defun bookmark-bmenu-1-window ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001855 "Select this line's bookmark, alone, in full frame."
1856 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001857 (bookmark-jump (bookmark-bmenu-bookmark))
1858 (bury-buffer (other-buffer))
1859 (delete-other-windows))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001860
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001861
1862(defun bookmark-bmenu-2-window ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001863 "Select this line's bookmark, with previous buffer in second window."
1864 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001865 (let ((bmrk (bookmark-bmenu-bookmark))
1866 (menu (current-buffer))
1867 (pop-up-windows t))
1868 (delete-other-windows)
8bdfa064 Chong Yidong2011-07-13 21:40:30 -04001869 (switch-to-buffer (other-buffer) nil t)
5b98e31f Karl Fogel2010-07-04 15:57:02 -04001870 (bookmark--jump-via bmrk 'pop-to-buffer)
aef053eb Karl Fogel2010-01-01 23:36:17 -05001871 (bury-buffer menu)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001872
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001873
1874(defun bookmark-bmenu-this-window ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001875 "Select this line's bookmark in this window."
1876 (interactive)
aef053eb Karl Fogel2010-01-01 23:36:17 -05001877 (bookmark-jump (bookmark-bmenu-bookmark)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001878
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001879
1880(defun bookmark-bmenu-other-window ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001881 "Select this line's bookmark in other window, leaving bookmark menu visible."
1882 (interactive)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001883 (let ((bookmark (bookmark-bmenu-bookmark)))
5b98e31f Karl Fogel2010-07-04 15:57:02 -04001884 (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
e40e3151
KF
Karl Fogel1995-03-15 14:47:09 +00001885
1886
1887(defun bookmark-bmenu-switch-other-window ()
1888 "Make the other window select this line's bookmark.
1889The current window remains selected."
1890 (interactive)
e197b151 Richard M. Stallman1998-05-26 18:27:47 +00001891 (let ((bookmark (bookmark-bmenu-bookmark))
2cbafa56
CY
Chong Yidong2012-11-04 11:25:18 +08001892 (fun (lambda (b) (display-buffer b t))))
1893 (bookmark--jump-via bookmark fun)))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001894
7e510a5e
RS
Richard M. Stallman1997-03-30 21:38:43 +00001895(defun bookmark-bmenu-other-window-with-mouse (event)
1896 "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
1897 (interactive "e")
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00001898 (with-current-buffer (window-buffer (posn-window (event-end event)))
7e510a5e
RS
Richard M. Stallman1997-03-30 21:38:43 +00001899 (save-excursion
1900 (goto-char (posn-point (event-end event)))
1901 (bookmark-bmenu-other-window))))
1902
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001903
1904(defun bookmark-bmenu-show-annotation ()
1905 "Show the annotation for the current bookmark in another window."
1906 (interactive)
1907 (let ((bookmark (bookmark-bmenu-bookmark)))
aef053eb Karl Fogel2010-01-01 23:36:17 -05001908 (bookmark-show-annotation bookmark)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001909
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001910
1911(defun bookmark-bmenu-show-all-annotations ()
1912 "Show the annotation for all bookmarks in another window."
1913 (interactive)
1914 (bookmark-show-all-annotations))
1915
1916
1917(defun bookmark-bmenu-edit-annotation ()
1918 "Edit the annotation for the current bookmark in another window."
1919 (interactive)
1920 (let ((bookmark (bookmark-bmenu-bookmark)))
aef053eb Karl Fogel2010-01-01 23:36:17 -05001921 (bookmark-edit-annotation bookmark)))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001922
1923
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001924(defun bookmark-bmenu-unmark (&optional backup)
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001925 "Cancel all requested operations on bookmark on this line and move down.
4eadcdf8 Richard M. Stallman1995-06-18 22:46:02 +00001926Optional BACKUP means move up."
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001927 (interactive "P")
1928 (beginning-of-line)
aef053eb Karl Fogel2010-01-01 23:36:17 -05001929 (bookmark-bmenu-ensure-position)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001930 (with-buffer-modified-unmodified
1931 (let ((inhibit-read-only t))
1932 (delete-char 1)
1933 ;; any flags to reset according to circumstances? How about a
1934 ;; flag indicating whether this bookmark is being visited?
1935 ;; well, we don't have this now, so maybe later.
1936 (insert " "))
1937 (forward-line (if backup -1 1))
1938 (bookmark-bmenu-ensure-position)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001939
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001940
1941(defun bookmark-bmenu-backup-unmark ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001942 "Move up and cancel all requested operations on bookmark on line above."
1943 (interactive)
1944 (forward-line -1)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001945 (bookmark-bmenu-ensure-position)
1946 (bookmark-bmenu-unmark)
1947 (forward-line -1)
1948 (bookmark-bmenu-ensure-position))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001949
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001950
1951(defun bookmark-bmenu-delete ()
5e0e5feb
RS
Richard M. Stallman1996-05-09 17:34:55 +00001952 "Mark bookmark on this line to be deleted.
1953To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00001954 (interactive)
1955 (beginning-of-line)
aef053eb Karl Fogel2010-01-01 23:36:17 -05001956 (bookmark-bmenu-ensure-position)
b894c439
KF
Karl Fogel2010-01-02 13:52:32 -05001957 (with-buffer-modified-unmodified
1958 (let ((inhibit-read-only t))
1959 (delete-char 1)
1960 (insert ?D)
1961 (forward-line 1)
1962 (bookmark-bmenu-ensure-position))))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001963
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001964
1965(defun bookmark-bmenu-delete-backwards ()
5e0e5feb
RS
Richard M. Stallman1996-05-09 17:34:55 +00001966 "Mark bookmark on this line to be deleted, then move up one line.
1967To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001968 (interactive)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00001969 (bookmark-bmenu-delete)
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001970 (forward-line -2)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05001971 (bookmark-bmenu-ensure-position)
1972 (forward-line 1)
1973 (bookmark-bmenu-ensure-position))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001974
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001975
1976(defun bookmark-bmenu-execute-deletions ()
38bee102 Karl Fogel2010-01-13 18:25:01 -05001977 "Delete bookmarks flagged `D'."
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001978 (interactive)
60d0378e Karl Fogel1996-01-05 21:57:54 +00001979 (message "Deleting bookmarks...")
fc9d6ad6 Stefan Monnier2009-11-21 06:43:45 +00001980 (let ((o-point (point))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001981 (o-str (save-excursion
1982 (beginning-of-line)
fc9d6ad6 Stefan Monnier2009-11-21 06:43:45 +00001983 (unless (looking-at "^D")
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001984 (buffer-substring
1985 (point)
1986 (progn (end-of-line) (point))))))
1987 (o-col (current-column)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00001988 (goto-char (point-min))
2575da50
TV
Thierry Volpiatto2013-04-03 21:59:09 -04001989 (unless bookmark-bmenu-use-header-line
1990 (forward-line 1))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001991 (while (re-search-forward "^D" (point-max) t)
1992 (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
1993 (bookmark-bmenu-list)
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00001994 (if o-str
1995 (progn
1996 (goto-char (point-min))
1997 (search-forward o-str)
1998 (beginning-of-line)
1999 (forward-char o-col))
2000 (goto-char o-point))
2001 (beginning-of-line)
60d0378e
KF
Karl Fogel1996-01-05 21:57:54 +00002002 (message "Deleting bookmarks...done")
2003 ))
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00002004
2005
2006(defun bookmark-bmenu-rename ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00002007 "Rename bookmark on current line. Prompts for a new name."
2008 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05002009 (let ((bmrk (bookmark-bmenu-bookmark))
2010 (thispoint (point)))
2011 (bookmark-rename bmrk)
2012 (goto-char thispoint)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00002013
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00002014
2015(defun bookmark-bmenu-locate ()
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00002016 "Display location of this bookmark. Displays in the minibuffer."
2017 (interactive)
3a69db53
KF
Karl Fogel2010-01-02 13:13:28 -05002018 (let ((bmrk (bookmark-bmenu-bookmark)))
2019 (message "%s" (bookmark-location bmrk))))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002020
037b0a87
KF
Karl Fogel2004-06-13 01:59:21 +00002021(defun bookmark-bmenu-relocate ()
2022 "Change the file path of the bookmark on the current line,
2023 prompting with completion for the new path."
2024 (interactive)
aef053eb
KF
Karl Fogel2010-01-01 23:36:17 -05002025 (let ((bmrk (bookmark-bmenu-bookmark))
2026 (thispoint (point)))
2027 (bookmark-relocate bmrk)
2028 (goto-char thispoint)))
d22d6453 Richard M. Stallman1993-10-13 05:59:54 +00002029
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002030;;; Bookmark-bmenu search
2031
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002032(defun bookmark-bmenu-filter-alist-by-regexp (regexp)
2033 "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
26d9285f Stefan Monnier2009-11-21 04:43:10 +00002034 (let ((bookmark-alist
f58e0fd5
SM
Stefan Monnier2012-07-10 07:51:54 -04002035 (cl-loop for i in bookmark-alist
2036 when (string-match regexp (car i)) collect i into new
2037 finally return new)))
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002038 (bookmark-bmenu-list)))
2039
26d9285f Stefan Monnier2009-11-21 04:43:10 +00002040
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002041;;;###autoload
2042(defun bookmark-bmenu-search ()
26d9285f Stefan Monnier2009-11-21 04:43:10 +00002043 "Incremental search of bookmarks, hiding the non-matches as we go."
1e7d4475 Stefan Monnier2009-11-20 15:47:26 +00002044 (interactive)
26d9285f Stefan Monnier2009-11-21 04:43:10 +00002045 (let ((bmk (bookmark-bmenu-bookmark))
d83ef976 Stefan Monnier2012-10-01 22:47:12 -04002046 (timer nil))
26d9285f Stefan Monnier2009-11-21 04:43:10 +00002047 (unwind-protect
d83ef976
SM
Stefan Monnier2012-10-01 22:47:12 -04002048 (minibuffer-with-setup-hook
2049 (lambda ()
2050 (setq timer (run-with-idle-timer
2051 bookmark-search-delay 'repeat
2052 #'(lambda (buf)
2053 (with-current-buffer buf
2054 (bookmark-bmenu-filter-alist-by-regexp
2055 (minibuffer-contents))))
2056 (current-buffer))))
2057 (read-string "Pattern: ")
2058 (when timer (cancel-timer timer) (setq timer nil)))
2059 (when timer ;; Signalled an error or a `quit'.
2060 (cancel-timer timer)
2061 (bookmark-bmenu-list)
2062 (bookmark-bmenu-goto-bookmark bmk)))))
0472835f Juanma Barranquero2010-01-02 02:05:44 +01002063
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002064(defun bookmark-bmenu-goto-bookmark (name)
2065 "Move point to bookmark with name NAME."
2066 (goto-char (point-min))
62e75060
BB
Boruch Baum2015-04-09 11:07:15 -04002067 (while (not (or (equal name (bookmark-bmenu-bookmark))
2068 (eobp)))
1e7d4475
SM
Stefan Monnier2009-11-20 15:47:26 +00002069 (forward-line 1))
2070 (forward-line 0))
0472835f Juanma Barranquero2010-01-02 02:05:44 +01002071
1e7d4475 Stefan Monnier2009-11-20 15:47:26 +00002072
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00002073\f
2074;;; Menu bar stuff. Prefix is "bookmark-menu".
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +00002075
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00002076(defun bookmark-menu-popup-paned-menu (event name entries)
2077 "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
2078That is, ENTRIES is a list of strings which appear as the choices
2079in the menu.
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00002080The number of panes depends on the number of entries.
2081The visible entries are truncated to `bookmark-menu-length', but the
2082strings returned are not."
2083 (let ((f-height (/ (frame-height) 2))
2084 (pane-list nil)
2085 (iter 0))
2086 (while entries
2087 (let (lst
2088 (count 0))
2089 (while (and (< count f-height) entries)
2090 (let ((str (car entries)))
2091 (push (cons
2092 (if (> (length str) bookmark-menu-length)
2093 (substring str 0 bookmark-menu-length)
2094 str)
2095 str)
2096 lst)
2097 (setq entries (cdr entries))
2098 (setq count (1+ count))))
2099 (setq iter (1+ iter))
2100 (push (cons
2101 (format "-*- %s (%d) -*-" name iter)
2102 (nreverse lst))
2103 pane-list)))
d32ff76a Juanma Barranquero2005-07-26 13:56:01 +00002104
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00002105 ;; Popup the menu and return the string.
2106 (x-popup-menu event (cons (concat "-*- " name " -*-")
2107 (nreverse pane-list)))))
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002108
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002109
d22d6453
RS
Richard M. Stallman1993-10-13 05:59:54 +00002110;; Thanks to Roland McGrath for fixing menubar.el so that the
2111;; following works, and for explaining what to do to make it work.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002112
7af04c89
RS
Richard M. Stallman1995-04-06 08:44:21 +00002113;; We MUST autoload EACH form used to set up this variable's value, so
2114;; that the whole job is done in loaddefs.el.
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002115
5e0e5feb Richard M. Stallman1996-05-09 17:34:55 +00002116;; Emacs menubar stuff.
09fd6588 Karl Fogel1995-12-30 19:52:40 +00002117
7af04c89 Richard M. Stallman1995-04-06 08:44:21 +00002118;;;###autoload
175d0960
SM
Stefan Monnier2003-08-11 17:30:47 +00002119(defvar menu-bar-bookmark-map
2120 (let ((map (make-sparse-keymap "Bookmark functions")))
1ec4b7b2
SM
Stefan Monnier2012-06-27 17:15:13 -04002121 (bindings--define-key map [load]
2122 '(menu-item "Load a Bookmark File..." bookmark-load
2123 :help "Load bookmarks from a bookmark file)"))
2124 (bindings--define-key map [write]
2125 '(menu-item "Save Bookmarks As..." bookmark-write
2126 :help "Write bookmarks to a file (reading the file name with the minibuffer)"))
2127 (bindings--define-key map [save]
2128 '(menu-item "Save Bookmarks" bookmark-save
2129 :help "Save currently defined bookmarks"))
2130 (bindings--define-key map [edit]
2131 '(menu-item "Edit Bookmark List" bookmark-bmenu-list
2132 :help "Display a list of existing bookmarks"))
2133 (bindings--define-key map [delete]
2134 '(menu-item "Delete Bookmark..." bookmark-delete
2135 :help "Delete a bookmark from the bookmark list"))
2136 (bindings--define-key map [rename]
2137 '(menu-item "Rename Bookmark..." bookmark-rename
2138 :help "Change the name of a bookmark"))
2139 (bindings--define-key map [locate]
2140 '(menu-item "Insert Location..." bookmark-locate
2141 :help "Insert the name of the file associated with a bookmark"))
2142 (bindings--define-key map [insert]
2143 '(menu-item "Insert Contents..." bookmark-insert
2144 :help "Insert the text of the file pointed to by a bookmark"))
2145 (bindings--define-key map [set]
2146 '(menu-item "Set Bookmark..." bookmark-set
2147 :help "Set a bookmark named inside a file."))
2148 (bindings--define-key map [jump]
2149 '(menu-item "Jump to Bookmark..." bookmark-jump
2150 :help "Jump to a bookmark (a point in some file)"))
175d0960 Stefan Monnier2003-08-11 17:30:47 +00002151 map))
09fd6588 Karl Fogel1995-12-30 19:52:40 +00002152
7af04c89 Richard M. Stallman1995-04-06 08:44:21 +00002153;;;###autoload
175d0960 Stefan Monnier2003-08-11 17:30:47 +00002154(defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002155
09fd6588
KF
Karl Fogel1995-12-30 19:52:40 +00002156;; make bookmarks appear toward the right side of the menu.
2157(if (boundp 'menu-bar-final-items)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00002158 (if menu-bar-final-items
b861e45e Stefan Monnier2009-10-28 03:09:11 +00002159 (push 'bookmark menu-bar-final-items))
09fd6588
KF
Karl Fogel1995-12-30 19:52:40 +00002160 (setq menu-bar-final-items '(bookmark)))
2161
e3437989
RS
Richard M. Stallman1995-03-11 02:05:20 +00002162;;;; end bookmark menu stuff ;;;;
2163
2164\f
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00002165;; Load Hook
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002166(defvar bookmark-load-hook nil
077e0753 Karl Fogel2010-07-14 15:09:28 -04002167 "Hook run at the end of loading library `bookmark.el'.")
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002168
43f8b275 Stefan Monnier2008-06-25 16:51:33 +00002169;; Exit Hook, called from kill-emacs-hook
e5bd0a28
SM
Stefan Monnier2012-05-12 23:05:06 -04002170(define-obsolete-variable-alias 'bookmark-exit-hooks
2171 'bookmark-exit-hook "22.1")
6192b604 Karl Fogel2000-12-13 22:56:38 +00002172(defvar bookmark-exit-hook nil
d32ff76a
JB
Juanma Barranquero2005-07-26 13:56:01 +00002173 "Hook run when Emacs exits.")
2174
6192b604
KF
Karl Fogel2000-12-13 22:56:38 +00002175(defun bookmark-exit-hook-internal ()
2176 "Save bookmark state, if necessary, at Emacs exit time.
d32ff76a
JB
Juanma Barranquero2005-07-26 13:56:01 +00002177This also runs `bookmark-exit-hook'."
2178 (run-hooks 'bookmark-exit-hook)
2575da50 Thierry Volpiatto2013-04-03 21:59:09 -04002179 (and (bookmark-time-to-save-p t)
175d0960 Stefan Monnier2003-08-11 17:30:47 +00002180 (bookmark-save)))
6192b604 Karl Fogel2000-12-13 22:56:38 +00002181
845fc5e5
JB
Juanma Barranquero2011-03-06 01:30:16 +01002182(unless noninteractive
2183 (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
6192b604 Karl Fogel2000-12-13 22:56:38 +00002184
a35809ba
JB
Juanma Barranquero2008-10-06 11:49:59 +00002185(defun bookmark-unload-function ()
2186 "Unload the Bookmark library."
2187 (when bookmark-save-flag (bookmark-save))
2188 ;; continue standard unloading
2189 nil)
2190
6192b604 Karl Fogel2000-12-13 22:56:38 +00002191
e3437989 Richard M. Stallman1995-03-11 02:05:20 +00002192(run-hooks 'bookmark-load-hook)
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002193
b3bf02fa Richard M. Stallman1993-08-10 20:35:31 +00002194(provide 'bookmark)
dbe99ae9 Sam Steingold2001-01-12 22:49:40 +00002195
11eb4275 Richard M. Stallman1993-08-13 00:59:51 +00002196;;; bookmark.el ends here