bookmark.el 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. ;;; bookmark.el --- set bookmarks, maybe annotate them, jump to them later
  2. ;; Copyright (C) 1993-1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Karl Fogel <kfogel@red-bean.com>
  4. ;; Maintainer: Karl Fogel <kfogel@red-bean.com>
  5. ;; Created: July, 1993
  6. ;; Keywords: bookmarks, placeholders, annotations
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package is for setting "bookmarks" in files. A bookmark
  20. ;; associates a string with a location in a certain file. Thus, you
  21. ;; can navigate your way to that location by providing the string.
  22. ;; See the "User Variables" section for customizations.
  23. ;;; Code:
  24. (require 'pp)
  25. (eval-when-compile (require 'cl))
  26. ;;; Misc comments:
  27. ;;
  28. ;; If variable bookmark-use-annotations is non-nil, an annotation is
  29. ;; queried for when setting a bookmark.
  30. ;;
  31. ;; The bookmark list is sorted lexically by default, but you can turn
  32. ;; this off by setting bookmark-sort-flag to nil. If it is nil, then
  33. ;; the list will be presented in the order it is recorded
  34. ;; (chronologically), which is actually fairly useful as well.
  35. ;;; User Variables
  36. (defgroup bookmark nil
  37. "Setting, annotation and jumping to bookmarks."
  38. :group 'matching)
  39. (defcustom bookmark-use-annotations nil
  40. "If non-nil, saving a bookmark queries for an annotation in a buffer."
  41. :type 'boolean
  42. :group 'bookmark)
  43. (defcustom bookmark-save-flag t
  44. "Controls when Emacs saves bookmarks to a file.
  45. --> nil means never save bookmarks, except when `bookmark-save' is
  46. explicitly called (\\[bookmark-save]).
  47. --> t means save bookmarks when Emacs is killed.
  48. --> Otherwise, it should be a number that is the frequency with which
  49. the bookmark list is saved (i.e.: the number of times which
  50. Emacs's bookmark list may be modified before it is automatically
  51. saved.). If it is a number, Emacs will also automatically save
  52. bookmarks when it is killed.
  53. Therefore, the way to get it to save every time you make or delete a
  54. bookmark is to set this variable to 1 (or 0, which produces the same
  55. behavior.)
  56. To specify the file in which to save them, modify the variable
  57. `bookmark-default-file', which is `~/.emacs.bmk' by default."
  58. :type '(choice (const nil) integer (other t))
  59. :group 'bookmark)
  60. (defconst bookmark-old-default-file "~/.emacs-bkmrks"
  61. "The `.emacs.bmk' file used to be called this name.")
  62. ;; defvared to avoid a compilation warning:
  63. (defvar bookmark-file nil
  64. "Old name for `bookmark-default-file'.")
  65. (defcustom bookmark-default-file
  66. (if bookmark-file
  67. ;; In case user set `bookmark-file' in her .emacs:
  68. bookmark-file
  69. (locate-user-emacs-file "bookmarks" ".emacs.bmk"))
  70. "File in which to save bookmarks by default."
  71. :type 'file
  72. :group 'bookmark)
  73. (defcustom bookmark-version-control 'nospecial
  74. "Whether or not to make numbered backups of the bookmark file.
  75. It can have four values: t, nil, `never', and `nospecial'.
  76. The first three have the same meaning that they do for the
  77. variable `version-control', and the final value `nospecial' means just
  78. use the value of `version-control'."
  79. :type '(choice (const nil) (const never) (const nospecial)
  80. (other t))
  81. :group 'bookmark)
  82. (defcustom bookmark-completion-ignore-case t
  83. "Non-nil means bookmark functions ignore case in completion."
  84. :type 'boolean
  85. :group 'bookmark)
  86. (defcustom bookmark-sort-flag t
  87. "Non-nil means that bookmarks will be displayed sorted by bookmark name.
  88. Otherwise they will be displayed in LIFO order (that is, most
  89. recently set ones come first, oldest ones come last)."
  90. :type 'boolean
  91. :group 'bookmark)
  92. (defcustom bookmark-automatically-show-annotations t
  93. "Non-nil means show annotations when jumping to a bookmark."
  94. :type 'boolean
  95. :group 'bookmark)
  96. (defconst bookmark-bmenu-header-height 2
  97. "Number of lines used for the *Bookmark List* header.")
  98. (defconst bookmark-bmenu-marks-width 2
  99. "Number of columns (chars) used for the *Bookmark List* marks column,
  100. including the annotations column.")
  101. (defcustom bookmark-bmenu-file-column 30
  102. "Column at which to display filenames in a buffer listing bookmarks.
  103. You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]."
  104. :type 'integer
  105. :group 'bookmark)
  106. (defcustom bookmark-bmenu-toggle-filenames t
  107. "Non-nil means show filenames when listing bookmarks.
  108. This may result in truncated bookmark names. To disable this, put the
  109. following in your `.emacs' file:
  110. \(setq bookmark-bmenu-toggle-filenames nil)"
  111. :type 'boolean
  112. :group 'bookmark)
  113. (defcustom bookmark-menu-length 70
  114. "Maximum length of a bookmark name displayed on a popup menu."
  115. :type 'integer
  116. :group 'bookmark)
  117. ;; FIXME: Is it really worth a customization option?
  118. (defcustom bookmark-search-delay 0.2
  119. "Time before `bookmark-bmenu-search' updates the display."
  120. :group 'bookmark
  121. :type 'integer)
  122. (defface bookmark-menu-heading
  123. '((t (:inherit font-lock-type-face)))
  124. "Face used to highlight the heading in bookmark menu buffers."
  125. :group 'bookmark
  126. :version "22.1")
  127. ;;; No user-serviceable parts beyond this point.
  128. ;; Added for lucid emacs compatibility, db
  129. (or (fboundp 'defalias) (fset 'defalias 'fset))
  130. ;; suggested for lucid compatibility by david hughes:
  131. (or (fboundp 'frame-height) (defalias 'frame-height 'screen-height))
  132. ;;; Keymap stuff:
  133. ;; Set up these bindings dumping time *only*;
  134. ;; if the user alters them, don't override the user when loading bookmark.el.
  135. ;;;###autoload (define-key ctl-x-r-map "b" 'bookmark-jump)
  136. ;;;###autoload (define-key ctl-x-r-map "m" 'bookmark-set)
  137. ;;;###autoload (define-key ctl-x-r-map "l" 'bookmark-bmenu-list)
  138. ;;;###autoload
  139. (defvar bookmark-map
  140. (let ((map (make-sparse-keymap)))
  141. ;; Read the help on all of these functions for details...
  142. (define-key map "x" 'bookmark-set)
  143. (define-key map "m" 'bookmark-set) ;"m"ark
  144. (define-key map "j" 'bookmark-jump)
  145. (define-key map "g" 'bookmark-jump) ;"g"o
  146. (define-key map "o" 'bookmark-jump-other-window)
  147. (define-key map "i" 'bookmark-insert)
  148. (define-key map "e" 'edit-bookmarks)
  149. (define-key map "f" 'bookmark-insert-location) ;"f"ind
  150. (define-key map "r" 'bookmark-rename)
  151. (define-key map "d" 'bookmark-delete)
  152. (define-key map "l" 'bookmark-load)
  153. (define-key map "w" 'bookmark-write)
  154. (define-key map "s" 'bookmark-save)
  155. map)
  156. "Keymap containing bindings to bookmark functions.
  157. It is not bound to any key by default: to bind it
  158. so that you have a bookmark prefix, just use `global-set-key' and bind a
  159. key of your choice to `bookmark-map'. All interactive bookmark
  160. functions have a binding in this keymap.")
  161. ;;;###autoload (fset 'bookmark-map bookmark-map)
  162. ;;; Core variables and data structures:
  163. (defvar bookmark-alist ()
  164. "Association list of bookmarks and their records.
  165. Bookmark functions update the value automatically.
  166. You probably do NOT want to change the value yourself.
  167. The value is an alist with entries of the form
  168. (BOOKMARK-NAME . PARAM-ALIST)
  169. or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
  170. BOOKMARK-NAME is the name you gave to the bookmark when creating it.
  171. PARAM-ALIST is an alist of bookmark information. The order of the
  172. entries in PARAM-ALIST is not important. The possible entries are
  173. described below. An entry with a key but null value means the entry
  174. is not used.
  175. (filename . FILENAME)
  176. (position . POS)
  177. (front-context-string . STR-AFTER-POS)
  178. (rear-context-string . STR-BEFORE-POS)
  179. (handler . HANDLER)
  180. (annotation . ANNOTATION)
  181. FILENAME names the bookmarked file.
  182. POS is the bookmarked buffer position (position in the file).
  183. STR-AFTER-POS is buffer text that immediately follows POS.
  184. STR-BEFORE-POS is buffer text that immediately precedes POS.
  185. ANNOTATION is a string that describes the bookmark.
  186. See options `bookmark-use-annotations' and
  187. `bookmark-automatically-show-annotations'.
  188. HANDLER is a function that provides the bookmark-jump behavior for a
  189. specific kind of bookmark. This is the case for Info bookmarks,
  190. for instance. HANDLER must accept a bookmark as argument.")
  191. (defvar bookmarks-already-loaded nil
  192. "Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
  193. ;; more stuff added by db.
  194. (defvar bookmark-current-bookmark nil
  195. "Name of bookmark most recently used in the current file.
  196. It is buffer local, used to make moving a bookmark forward
  197. through a file easier.")
  198. (make-variable-buffer-local 'bookmark-current-bookmark)
  199. (defvar bookmark-alist-modification-count 0
  200. "Number of modifications to bookmark list since it was last saved.")
  201. (defvar bookmark-search-size 16
  202. "Length of the context strings recorded on either side of a bookmark.")
  203. (defvar bookmark-current-buffer nil
  204. "The buffer in which a bookmark is currently being set or renamed.
  205. Functions that insert strings into the minibuffer use this to know
  206. the source buffer for that information; see `bookmark-yank-word' and
  207. `bookmark-insert-current-bookmark' for example.")
  208. (defvar bookmark-yank-point 0
  209. "The next point from which to pull source text for `bookmark-yank-word'.
  210. This point is in `bookmark-current-buffer'.")
  211. (defvar bookmark-quit-flag nil
  212. "Non nil make `bookmark-bmenu-search' quit immediately.")
  213. ;; Helper functions and macros.
  214. (defmacro with-buffer-modified-unmodified (&rest body)
  215. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  216. (let ((was-modified (make-symbol "was-modified")))
  217. `(let ((,was-modified (buffer-modified-p)))
  218. (unwind-protect
  219. (progn ,@body)
  220. (set-buffer-modified-p ,was-modified)))))
  221. ;; Only functions below, in this page and the next one (file formats),
  222. ;; need to know anything about the format of bookmark-alist entries.
  223. ;; Everyone else should go through them.
  224. (defun bookmark-name-from-full-record (bookmark-record)
  225. "Return the name of BOOKMARK-RECORD. BOOKMARK-RECORD is, e.g.,
  226. one element from `bookmark-alist'."
  227. (car bookmark-record))
  228. (defun bookmark-all-names ()
  229. "Return a list of all current bookmark names."
  230. (bookmark-maybe-load-default-file)
  231. (mapcar 'bookmark-name-from-full-record bookmark-alist))
  232. (defun bookmark-get-bookmark (bookmark-name-or-record &optional noerror)
  233. "Return the bookmark record corresponding to BOOKMARK-NAME-OR-RECORD.
  234. If BOOKMARK-NAME-OR-RECORD is a string, look for the corresponding
  235. bookmark record in `bookmark-alist'; return it if found, otherwise
  236. error. Else if BOOKMARK-NAME-OR-RECORD is already a bookmark record,
  237. just return it."
  238. (cond
  239. ((consp bookmark-name-or-record) bookmark-name-or-record)
  240. ((stringp bookmark-name-or-record)
  241. (or (assoc-string bookmark-name-or-record bookmark-alist
  242. bookmark-completion-ignore-case)
  243. (unless noerror (error "Invalid bookmark %s"
  244. bookmark-name-or-record))))))
  245. (defun bookmark-get-bookmark-record (bookmark-name-or-record)
  246. "Return the record portion of the entry for BOOKMARK-NAME-OR-RECORD in
  247. `bookmark-alist' (that is, all information but the name)."
  248. (let ((alist (cdr (bookmark-get-bookmark bookmark-name-or-record))))
  249. ;; The bookmark objects can either look like (NAME ALIST) or
  250. ;; (NAME . ALIST), so we have to distinguish the two here.
  251. (if (and (null (cdr alist)) (consp (caar alist)))
  252. (car alist) alist)))
  253. (defun bookmark-set-name (bookmark-name-or-record newname)
  254. "Set BOOKMARK-NAME-OR-RECORD's name to NEWNAME."
  255. (setcar (bookmark-get-bookmark bookmark-name-or-record) newname))
  256. (defun bookmark-prop-get (bookmark-name-or-record prop)
  257. "Return the property PROP of BOOKMARK-NAME-OR-RECORD, or nil if none."
  258. (cdr (assq prop (bookmark-get-bookmark-record bookmark-name-or-record))))
  259. (defun bookmark-prop-set (bookmark-name-or-record prop val)
  260. "Set the property PROP of BOOKMARK-NAME-OR-RECORD to VAL."
  261. (let ((cell (assq
  262. prop (bookmark-get-bookmark-record bookmark-name-or-record))))
  263. (if cell
  264. (setcdr cell val)
  265. (nconc (bookmark-get-bookmark-record bookmark-name-or-record)
  266. (list (cons prop val))))))
  267. (defun bookmark-get-annotation (bookmark-name-or-record)
  268. "Return the annotation of BOOKMARK-NAME-OR-RECORD, or nil if none."
  269. (bookmark-prop-get bookmark-name-or-record 'annotation))
  270. (defun bookmark-set-annotation (bookmark-name-or-record ann)
  271. "Set the annotation of BOOKMARK-NAME-OR-RECORD to ANN."
  272. (bookmark-prop-set bookmark-name-or-record 'annotation ann))
  273. (defun bookmark-get-filename (bookmark-name-or-record)
  274. "Return the full filename of BOOKMARK-NAME-OR-RECORD, or nil if none."
  275. (bookmark-prop-get bookmark-name-or-record 'filename))
  276. (defun bookmark-set-filename (bookmark-name-or-record filename)
  277. "Set the full filename of BOOKMARK-NAME-OR-RECORD to FILENAME."
  278. (bookmark-prop-set bookmark-name-or-record 'filename filename))
  279. (defun bookmark-get-position (bookmark-name-or-record)
  280. "Return the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD, or nil if none."
  281. (bookmark-prop-get bookmark-name-or-record 'position))
  282. (defun bookmark-set-position (bookmark-name-or-record position)
  283. "Set the position (i.e.: point) of BOOKMARK-NAME-OR-RECORD to POSITION."
  284. (bookmark-prop-set bookmark-name-or-record 'position position))
  285. (defun bookmark-get-front-context-string (bookmark-name-or-record)
  286. "Return the front-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
  287. (bookmark-prop-get bookmark-name-or-record 'front-context-string))
  288. (defun bookmark-set-front-context-string (bookmark-name-or-record string)
  289. "Set the front-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
  290. (bookmark-prop-set bookmark-name-or-record 'front-context-string string))
  291. (defun bookmark-get-rear-context-string (bookmark-name-or-record)
  292. "Return the rear-context-string of BOOKMARK-NAME-OR-RECORD, or nil if none."
  293. (bookmark-prop-get bookmark-name-or-record 'rear-context-string))
  294. (defun bookmark-set-rear-context-string (bookmark-name-or-record string)
  295. "Set the rear-context-string of BOOKMARK-NAME-OR-RECORD to STRING."
  296. (bookmark-prop-set bookmark-name-or-record 'rear-context-string string))
  297. (defun bookmark-get-handler (bookmark-name-or-record)
  298. "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none."
  299. (bookmark-prop-get bookmark-name-or-record 'handler))
  300. (defvar bookmark-history nil
  301. "The history list for bookmark functions.")
  302. (defun bookmark-completing-read (prompt &optional default)
  303. "Prompting with PROMPT, read a bookmark name in completion.
  304. PROMPT will get a \": \" stuck on the end no matter what, so you
  305. probably don't want to include one yourself.
  306. Optional second arg DEFAULT is a string to return if the user enters
  307. the empty string."
  308. (bookmark-maybe-load-default-file) ; paranoia
  309. (if (listp last-nonmenu-event)
  310. (bookmark-menu-popup-paned-menu t prompt
  311. (if bookmark-sort-flag
  312. (sort (bookmark-all-names)
  313. 'string-lessp)
  314. (bookmark-all-names)))
  315. (let* ((completion-ignore-case bookmark-completion-ignore-case)
  316. (default default)
  317. (prompt (concat prompt (if default
  318. (format " (%s): " default)
  319. ": ")))
  320. (str
  321. (completing-read prompt
  322. bookmark-alist
  323. nil
  324. 0
  325. nil
  326. 'bookmark-history)))
  327. (if (string-equal "" str) default str))))
  328. (defmacro bookmark-maybe-historicize-string (string)
  329. "Put STRING into the bookmark prompt history, if caller non-interactive.
  330. We need this because sometimes bookmark functions are invoked from
  331. menus, so `completing-read' never gets a chance to set `bookmark-history'."
  332. `(or
  333. (called-interactively-p 'interactive)
  334. (setq bookmark-history (cons ,string bookmark-history))))
  335. (defvar bookmark-make-record-function 'bookmark-make-record-default
  336. "A function that should be called to create a bookmark record.
  337. Modes may set this variable buffer-locally to enable bookmarking of
  338. locations that should be treated specially, such as Info nodes,
  339. news posts, images, pdf documents, etc.
  340. The function will be called with no arguments.
  341. It should signal a user error if it is unable to construct a record for
  342. the current location.
  343. The returned record should be a cons cell of the form (NAME . ALIST)
  344. where ALIST is as described in `bookmark-alist' and may typically contain
  345. a special cons (handler . HANDLER-FUNC) which specifies the handler function
  346. that should be used instead of `bookmark-default-handler' to open this
  347. bookmark. See the documentation for `bookmark-alist' for more.
  348. NAME is a suggested name for the constructed bookmark. It can be nil
  349. in which case a default heuristic will be used. The function can also
  350. equivalently just return ALIST without NAME.")
  351. (defun bookmark-make-record ()
  352. "Return a new bookmark record (NAME . ALIST) for the current location."
  353. (let ((record (funcall bookmark-make-record-function)))
  354. ;; Set up default name.
  355. (if (stringp (car record))
  356. ;; The function already provided a default name.
  357. record
  358. (if (car record) (push nil record))
  359. (setcar record (or bookmark-current-bookmark (bookmark-buffer-name)))
  360. record)))
  361. (defun bookmark-store (name alist no-overwrite)
  362. "Store the bookmark NAME with data ALIST.
  363. If NO-OVERWRITE is non-nil and another bookmark of the same name already
  364. exists in `bookmark-alist', record the new bookmark without throwing away the
  365. old one."
  366. (bookmark-maybe-load-default-file)
  367. (let ((stripped-name (copy-sequence name)))
  368. (or (featurep 'xemacs)
  369. ;; XEmacs's `set-text-properties' doesn't work on
  370. ;; free-standing strings, apparently.
  371. (set-text-properties 0 (length stripped-name) nil stripped-name))
  372. (if (and (not no-overwrite)
  373. (bookmark-get-bookmark stripped-name 'noerror))
  374. ;; already existing bookmark under that name and
  375. ;; no prefix arg means just overwrite old bookmark
  376. ;; Use the new (NAME . ALIST) format.
  377. (setcdr (bookmark-get-bookmark stripped-name) alist)
  378. ;; otherwise just cons it onto the front (either the bookmark
  379. ;; doesn't exist already, or there is no prefix arg. In either
  380. ;; case, we want the new bookmark consed onto the alist...)
  381. (push (cons stripped-name alist) bookmark-alist))
  382. ;; Added by db
  383. (setq bookmark-current-bookmark stripped-name)
  384. (setq bookmark-alist-modification-count
  385. (1+ bookmark-alist-modification-count))
  386. (if (bookmark-time-to-save-p)
  387. (bookmark-save))
  388. (setq bookmark-current-bookmark stripped-name)
  389. (bookmark-bmenu-surreptitiously-rebuild-list)))
  390. (defun bookmark-make-record-default (&optional no-file no-context posn)
  391. "Return the record describing the location of a new bookmark.
  392. Point should be at the buffer in which the bookmark is being set,
  393. and normally should be at the position where the bookmark is desired,
  394. but see the optional arguments for other possibilities.
  395. If NO-FILE is non-nil, then only return the subset of the
  396. record that pertains to the location within the buffer, leaving off
  397. the part that records the filename.
  398. If NO-CONTEXT is non-nil, do not include the front- and rear-context
  399. strings in the record -- the position is enough.
  400. If POSN is non-nil, record POSN as the point instead of `(point)'."
  401. `(,@(unless no-file `((filename . ,(bookmark-buffer-file-name))))
  402. ,@(unless no-context `((front-context-string
  403. . ,(if (>= (- (point-max) (point))
  404. bookmark-search-size)
  405. (buffer-substring-no-properties
  406. (point)
  407. (+ (point) bookmark-search-size))
  408. nil))))
  409. ,@(unless no-context `((rear-context-string
  410. . ,(if (>= (- (point) (point-min))
  411. bookmark-search-size)
  412. (buffer-substring-no-properties
  413. (point)
  414. (- (point) bookmark-search-size))
  415. nil))))
  416. (position . ,(or posn (point)))))
  417. ;;; File format stuff
  418. ;; *IMPORTANT NOTICE* If you are thinking about modifying (redefining)
  419. ;; the bookmark file format -- please don't. The current format
  420. ;; should be extensible enough. If you feel the need to change it,
  421. ;; please discuss it with other Emacs developers first.
  422. ;;
  423. ;; The format of `bookmark-alist' has changed twice in its lifetime.
  424. ;; This comment describes the three formats, FIRST, SECOND, and
  425. ;; CURRENT.
  426. ;;
  427. ;; The FIRST format was used prior to Emacs 20:
  428. ;;
  429. ;; ((BOOKMARK-NAME (FILENAME
  430. ;; STRING-IN-FRONT
  431. ;; STRING-BEHIND
  432. ;; POINT))
  433. ;; ...)
  434. ;;
  435. ;; The SECOND format was introduced in Emacs 20:
  436. ;;
  437. ;; ((BOOKMARK-NAME ((filename . FILENAME)
  438. ;; (position . POS)
  439. ;; (front-context-string . STR-AFTER-POS)
  440. ;; (rear-context-string . STR-BEFORE-POS)
  441. ;; (annotation . ANNOTATION)
  442. ;; (whatever . VALUE)
  443. ;; ...
  444. ;; ))
  445. ;; ...)
  446. ;;
  447. ;; The CURRENT format was introduced in Emacs 22:
  448. ;;
  449. ;; ((BOOKMARK-NAME (filename . FILENAME)
  450. ;; (position . POS)
  451. ;; (front-context-string . STR-AFTER-POS)
  452. ;; (rear-context-string . STR-BEFORE-POS)
  453. ;; (annotation . ANNOTATION)
  454. ;; (whatever . VALUE)
  455. ;; ...
  456. ;; )
  457. ;; ...)
  458. ;;
  459. ;; Both FIRST and SECOND have the same level of nesting: the cadr of a
  460. ;; bookmark record is a list of entry information. FIRST and SECOND
  461. ;; differ in the form of the record information: FIRST uses a list of
  462. ;; atoms, and SECOND uses an alist. In the FIRST format, the order of
  463. ;; the list elements matters. In the SECOND format, the order of the
  464. ;; alist elements is unimportant. The SECOND format facilitates the
  465. ;; addition of new kinds of elements, to support new kinds of
  466. ;; bookmarks or code evolution.
  467. ;;
  468. ;; The CURRENT format removes a level of nesting wrt FIRST and SECOND,
  469. ;; saving one cons cell per bookmark: the cadr of a bookmark record is
  470. ;; no longer a cons. Why that change was made remains a mystery --
  471. ;; just be aware of it. (Be aware too that this explanatory comment
  472. ;; was incorrect in Emacs 22 and Emacs 23.1.)
  473. ;;
  474. ;; To deal with the change from FIRST format to SECOND, conversion
  475. ;; code was added, and it is still in use. See
  476. ;; `bookmark-maybe-upgrade-file-format'.
  477. ;;
  478. ;; No conversion from SECOND to CURRENT is done. Instead, the code
  479. ;; handles both formats OK. It must continue to do so.
  480. ;;
  481. ;; See the doc string of `bookmark-alist' for information about the
  482. ;; elements that define a bookmark (e.g. `filename').
  483. (defconst bookmark-file-format-version 1
  484. "The current version of the format used by bookmark files.
  485. You should never need to change this.")
  486. (defconst bookmark-end-of-version-stamp-marker
  487. "-*- End Of Bookmark File Format Version Stamp -*-\n"
  488. "This string marks the end of the version stamp in a bookmark file.")
  489. (defun bookmark-alist-from-buffer ()
  490. "Return a `bookmark-alist' (in any format) from the current buffer.
  491. The buffer must of course contain bookmark format information.
  492. Does not care from where in the buffer it is called, and does not
  493. affect point."
  494. (save-excursion
  495. (goto-char (point-min))
  496. (if (search-forward bookmark-end-of-version-stamp-marker nil t)
  497. (read (current-buffer))
  498. ;; Else we're dealing with format version 0
  499. (if (search-forward "(" nil t)
  500. (progn
  501. (forward-char -1)
  502. (read (current-buffer)))
  503. ;; Else no hope of getting information here.
  504. (error "Not bookmark format")))))
  505. (defun bookmark-upgrade-version-0-alist (old-list)
  506. "Upgrade a version 0 alist OLD-LIST to the current version."
  507. (mapcar
  508. (lambda (bookmark)
  509. (let* ((name (car bookmark))
  510. (record (car (cdr bookmark)))
  511. (filename (nth 0 record))
  512. (front-str (nth 1 record))
  513. (rear-str (nth 2 record))
  514. (position (nth 3 record))
  515. (ann (nth 4 record)))
  516. (list
  517. name
  518. `((filename . ,filename)
  519. (front-context-string . ,(or front-str ""))
  520. (rear-context-string . ,(or rear-str ""))
  521. (position . ,position)
  522. (annotation . ,ann)))))
  523. old-list))
  524. (defun bookmark-upgrade-file-format-from-0 ()
  525. "Upgrade a bookmark file of format 0 (the original format) to format 1.
  526. This expects to be called from `point-min' in a bookmark file."
  527. (message "Upgrading bookmark format from 0 to %d..."
  528. bookmark-file-format-version)
  529. (let* ((old-list (bookmark-alist-from-buffer))
  530. (new-list (bookmark-upgrade-version-0-alist old-list)))
  531. (delete-region (point-min) (point-max))
  532. (bookmark-insert-file-format-version-stamp)
  533. (pp new-list (current-buffer))
  534. (save-buffer))
  535. (goto-char (point-min))
  536. (message "Upgrading bookmark format from 0 to %d...done"
  537. bookmark-file-format-version)
  538. )
  539. (defun bookmark-grok-file-format-version ()
  540. "Return an integer which is the file-format version of this bookmark file.
  541. This expects to be called from `point-min' in a bookmark file."
  542. (if (looking-at "^;;;;")
  543. (save-excursion
  544. (save-match-data
  545. (re-search-forward "[0-9]")
  546. (forward-char -1)
  547. (read (current-buffer))))
  548. ;; Else this is format version 0, the original one, which didn't
  549. ;; even have version stamps.
  550. 0))
  551. (defun bookmark-maybe-upgrade-file-format ()
  552. "Check the file-format version of this bookmark file.
  553. If the version is not up-to-date, upgrade it automatically.
  554. This expects to be called from `point-min' in a bookmark file."
  555. (let ((version (bookmark-grok-file-format-version)))
  556. (cond
  557. ((= version bookmark-file-format-version)
  558. ) ; home free -- version is current
  559. ((= version 0)
  560. (bookmark-upgrade-file-format-from-0))
  561. (t
  562. (error "Bookmark file format version strangeness")))))
  563. (defun bookmark-insert-file-format-version-stamp ()
  564. "Insert text indicating current version of bookmark file format."
  565. (insert
  566. (format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
  567. bookmark-file-format-version))
  568. (insert ";;; This format is meant to be slightly human-readable;\n"
  569. ";;; nevertheless, you probably don't want to edit it.\n"
  570. ";;; "
  571. bookmark-end-of-version-stamp-marker))
  572. ;;; end file-format stuff
  573. ;;; Generic helpers.
  574. (defun bookmark-maybe-message (fmt &rest args)
  575. "Apply `message' to FMT and ARGS, but only if the display is fast enough."
  576. (if (>= baud-rate 9600)
  577. (apply 'message fmt args)))
  578. ;;; Core code:
  579. (defvar bookmark-minibuffer-read-name-map
  580. (let ((map (make-sparse-keymap)))
  581. (set-keymap-parent map minibuffer-local-map)
  582. (define-key map "\C-w" 'bookmark-yank-word)
  583. ;; This C-u binding might not be very useful any more now that we
  584. ;; provide access to the default via the standard M-n binding.
  585. ;; Maybe we should just remove it? --Stef-08
  586. (define-key map "\C-u" 'bookmark-insert-current-bookmark)
  587. map))
  588. ;;;###autoload
  589. (defun bookmark-set (&optional name no-overwrite)
  590. "Set a bookmark named NAME at the current location.
  591. If name is nil, then prompt the user.
  592. With a prefix arg (non-nil NO-OVERWRITE), do not overwrite any
  593. existing bookmark that has the same name as NAME, but instead push the
  594. new bookmark onto the bookmark alist. The most recently set bookmark
  595. with name NAME is thus the one in effect at any given time, but the
  596. others are still there, should the user decide to delete the most
  597. recent one.
  598. To yank words from the text of the buffer and use them as part of the
  599. bookmark name, type C-w while setting a bookmark. Successive C-w's
  600. yank successive words.
  601. Typing C-u inserts (at the bookmark name prompt) the name of the last
  602. bookmark used in the document where the new bookmark is being set;
  603. this helps you use a single bookmark name to track progress through a
  604. large document. If there is no prior bookmark for this document, then
  605. C-u inserts an appropriate name based on the buffer or file.
  606. Use \\[bookmark-delete] to remove bookmarks (you give it a name and
  607. it removes only the first instance of a bookmark with that name from
  608. the list of bookmarks.)"
  609. (interactive (list nil current-prefix-arg))
  610. (unwind-protect
  611. (let* ((record (bookmark-make-record))
  612. (default (car record)))
  613. (bookmark-maybe-load-default-file)
  614. ;; Don't set `bookmark-yank-point' and `bookmark-current-buffer'
  615. ;; if they have been already set in another buffer. (e.g gnus-art).
  616. (unless (and bookmark-yank-point
  617. bookmark-current-buffer)
  618. (setq bookmark-yank-point (point))
  619. (setq bookmark-current-buffer (current-buffer)))
  620. (let ((str
  621. (or name
  622. (read-from-minibuffer
  623. (format "Set bookmark (%s): " default)
  624. nil
  625. bookmark-minibuffer-read-name-map
  626. nil nil default))))
  627. (and (string-equal str "") (setq str default))
  628. (bookmark-store str (cdr record) no-overwrite)
  629. ;; Ask for an annotation buffer for this bookmark
  630. (when bookmark-use-annotations
  631. (bookmark-edit-annotation str))))
  632. (setq bookmark-yank-point nil)
  633. (setq bookmark-current-buffer nil)))
  634. (defun bookmark-kill-line (&optional newline-too)
  635. "Kill from point to end of line.
  636. If optional arg NEWLINE-TOO is non-nil, delete the newline too.
  637. Does not affect the kill ring."
  638. (let ((eol (line-end-position)))
  639. (delete-region (point) eol)
  640. (if (and newline-too (looking-at "\n"))
  641. (delete-char 1))))
  642. ;; Defvars to avoid compilation warnings:
  643. (defvar bookmark-annotation-name nil
  644. "Variable holding the name of the bookmark.
  645. This is used in `bookmark-edit-annotation' to record the bookmark
  646. whose annotation is being edited.")
  647. (defun bookmark-default-annotation-text (bookmark-name)
  648. "Return default annotation text for BOOKMARK-NAME.
  649. The default annotation text is simply some text explaining how to use
  650. annotations."
  651. (concat "# Type the annotation for bookmark '" bookmark-name "' here.\n"
  652. "# All lines which start with a '#' will be deleted.\n"
  653. "# Type C-c C-c when done.\n#\n"
  654. "# Author: " (user-full-name) " <" (user-login-name) "@"
  655. (system-name) ">\n"
  656. "# Date: " (current-time-string) "\n"))
  657. (defvar bookmark-edit-annotation-text-func 'bookmark-default-annotation-text
  658. "Function to return default text to use for a bookmark annotation.
  659. It takes one argument, the name of the bookmark, as a string.")
  660. (define-obsolete-variable-alias 'bookmark-read-annotation-text-func
  661. 'bookmark-edit-annotation-text-func "23.1")
  662. (defvar bookmark-edit-annotation-mode-map
  663. (let ((map (make-sparse-keymap)))
  664. (set-keymap-parent map text-mode-map)
  665. (define-key map "\C-c\C-c" 'bookmark-send-edited-annotation)
  666. map)
  667. "Keymap for editing an annotation of a bookmark.")
  668. (defun bookmark-edit-annotation-mode (bookmark-name-or-record)
  669. "Mode for editing the annotation of bookmark BOOKMARK-NAME-OR-RECORD.
  670. When you have finished composing, type \\[bookmark-send-annotation].
  671. \\{bookmark-edit-annotation-mode-map}"
  672. (interactive)
  673. (kill-all-local-variables)
  674. (make-local-variable 'bookmark-annotation-name)
  675. (setq bookmark-annotation-name bookmark-name-or-record)
  676. (use-local-map bookmark-edit-annotation-mode-map)
  677. (setq major-mode 'bookmark-edit-annotation-mode
  678. mode-name "Edit Bookmark Annotation")
  679. (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record))
  680. (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
  681. (if (and annotation (not (string-equal annotation "")))
  682. (insert annotation)))
  683. (run-mode-hooks 'text-mode-hook))
  684. (defun bookmark-send-edited-annotation ()
  685. "Use buffer contents as annotation for a bookmark.
  686. Lines beginning with `#' are ignored."
  687. (interactive)
  688. (if (not (eq major-mode 'bookmark-edit-annotation-mode))
  689. (error "Not in bookmark-edit-annotation-mode"))
  690. (goto-char (point-min))
  691. (while (< (point) (point-max))
  692. (if (looking-at "^#")
  693. (bookmark-kill-line t)
  694. (forward-line 1)))
  695. ;; Take no chances with text properties.
  696. (let ((annotation (buffer-substring-no-properties (point-min) (point-max)))
  697. (bookmark-name bookmark-annotation-name))
  698. (bookmark-set-annotation bookmark-name annotation)
  699. (setq bookmark-alist-modification-count
  700. (1+ bookmark-alist-modification-count))
  701. (bookmark-bmenu-surreptitiously-rebuild-list))
  702. (kill-buffer (current-buffer)))
  703. (defun bookmark-edit-annotation (bookmark-name-or-record)
  704. "Pop up a buffer for editing bookmark BOOKMARK-NAME-OR-RECORD's annotation."
  705. (pop-to-buffer (generate-new-buffer-name "*Bookmark Annotation Compose*"))
  706. (bookmark-edit-annotation-mode bookmark-name-or-record))
  707. (defun bookmark-insert-current-bookmark ()
  708. "Insert into the bookmark name currently being set the value of
  709. `bookmark-current-bookmark' in `bookmark-current-buffer', defaulting
  710. to the buffer's file name if `bookmark-current-bookmark' is nil."
  711. (interactive)
  712. (let ((str
  713. (with-current-buffer bookmark-current-buffer
  714. (or bookmark-current-bookmark
  715. (bookmark-buffer-name)))))
  716. (insert str)))
  717. (defun bookmark-buffer-name ()
  718. "Return the name of the current buffer in a form usable as a bookmark name.
  719. If the buffer is associated with a file or directory, use that name."
  720. (cond
  721. ;; Or are we a file?
  722. (buffer-file-name (file-name-nondirectory buffer-file-name))
  723. ;; Or are we a directory?
  724. ((and (boundp 'dired-directory) dired-directory)
  725. (let* ((dirname (if (stringp dired-directory)
  726. dired-directory
  727. (car dired-directory)))
  728. (idx (1- (length dirname))))
  729. ;; Strip the trailing slash.
  730. (if (= ?/ (aref dirname idx))
  731. (file-name-nondirectory (substring dirname 0 idx))
  732. ;; Else return the current-buffer
  733. (buffer-name (current-buffer)))))
  734. ;; If all else fails, use the buffer's name.
  735. (t
  736. (buffer-name (current-buffer)))))
  737. (defun bookmark-yank-word ()
  738. "Get the next word from buffer `bookmark-current-buffer' and append
  739. it to the name of the bookmark currently being set, advancing
  740. `bookmark-yank-point' by one word."
  741. (interactive)
  742. (let ((string (with-current-buffer bookmark-current-buffer
  743. (goto-char bookmark-yank-point)
  744. (buffer-substring-no-properties
  745. (point)
  746. (progn
  747. (forward-word 1)
  748. (setq bookmark-yank-point (point)))))))
  749. (insert string)))
  750. (defun bookmark-buffer-file-name ()
  751. "Return the current buffer's file in a way useful for bookmarks."
  752. ;; Abbreviate the path, both so it's shorter and so it's more
  753. ;; portable. E.g., the user's home dir might be a different
  754. ;; path on different machines, but "~/" will still reach it.
  755. (abbreviate-file-name
  756. (cond
  757. (buffer-file-name buffer-file-name)
  758. ((and (boundp 'dired-directory) dired-directory)
  759. (if (stringp dired-directory)
  760. dired-directory
  761. (car dired-directory)))
  762. (t (error "Buffer not visiting a file or directory")))))
  763. (defun bookmark-maybe-load-default-file ()
  764. "If bookmarks have not been loaded from the default place, load them."
  765. (and (not bookmarks-already-loaded)
  766. (null bookmark-alist)
  767. (prog2
  768. (and
  769. ;; Possibly the old bookmark file, "~/.emacs-bkmrks", needs
  770. ;; to be renamed.
  771. (file-exists-p bookmark-old-default-file)
  772. (not (file-exists-p bookmark-default-file))
  773. (rename-file bookmark-old-default-file
  774. bookmark-default-file))
  775. ;; return t so the `and' will continue...
  776. t)
  777. (file-readable-p bookmark-default-file)
  778. (bookmark-load bookmark-default-file t t)
  779. (setq bookmarks-already-loaded t)))
  780. (defun bookmark-maybe-sort-alist ()
  781. "Return `bookmark-alist' for display.
  782. If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist."
  783. (if bookmark-sort-flag
  784. (sort (copy-alist bookmark-alist)
  785. (function
  786. (lambda (x y) (string-lessp (car x) (car y)))))
  787. bookmark-alist))
  788. (defvar bookmark-after-jump-hook nil
  789. "Hook run after `bookmark-jump' jumps to a bookmark.
  790. Useful for example to unhide text in `outline-mode'.")
  791. (defun bookmark--jump-via (bookmark-name-or-record display-function)
  792. "Handle BOOKMARK-NAME-OR-RECORD, then call DISPLAY-FUNCTION with
  793. current buffer as argument.
  794. After calling DISPLAY-FUNCTION, set window point to the point specified
  795. by BOOKMARK-NAME-OR-RECORD, if necessary, run `bookmark-after-jump-hook',
  796. and then show any annotations for this bookmark."
  797. (bookmark-handle-bookmark bookmark-name-or-record)
  798. (save-current-buffer
  799. (funcall display-function (current-buffer)))
  800. (let ((win (get-buffer-window (current-buffer) 0)))
  801. (if win (set-window-point win (point))))
  802. ;; FIXME: we used to only run bookmark-after-jump-hook in
  803. ;; `bookmark-jump' itself, but in none of the other commands.
  804. (run-hooks 'bookmark-after-jump-hook)
  805. (if bookmark-automatically-show-annotations
  806. ;; if there is an annotation for this bookmark,
  807. ;; show it in a buffer.
  808. (bookmark-show-annotation bookmark-name-or-record)))
  809. ;;;###autoload
  810. (defun bookmark-jump (bookmark &optional display-func)
  811. "Jump to bookmark BOOKMARK (a point in some file).
  812. You may have a problem using this function if the value of variable
  813. `bookmark-alist' is nil. If that happens, you need to load in some
  814. bookmarks. See help on function `bookmark-load' for more about
  815. this.
  816. If the file pointed to by BOOKMARK no longer exists, you will be asked
  817. if you wish to give the bookmark a new location, and `bookmark-jump'
  818. will then jump to the new location, as well as recording it in place
  819. of the old one in the permanent bookmark record.
  820. BOOKMARK is usually a bookmark name (a string). It can also be a
  821. bookmark record, but this is usually only done by programmatic callers.
  822. If DISPLAY-FUNC is non-nil, it is a function to invoke to display the
  823. bookmark. It defaults to `switch-to-buffer'. A typical value for
  824. DISPLAY-FUNC would be `switch-to-buffer-other-window'."
  825. (interactive
  826. (list (bookmark-completing-read "Jump to bookmark"
  827. bookmark-current-bookmark)))
  828. (unless bookmark
  829. (error "No bookmark specified"))
  830. (bookmark-maybe-historicize-string bookmark)
  831. (bookmark--jump-via bookmark (or display-func 'switch-to-buffer)))
  832. ;;;###autoload
  833. (defun bookmark-jump-other-window (bookmark)
  834. "Jump to BOOKMARK in another window. See `bookmark-jump' for more."
  835. (interactive
  836. (list (bookmark-completing-read "Jump to bookmark (in another window)"
  837. bookmark-current-bookmark)))
  838. (bookmark-jump bookmark 'switch-to-buffer-other-window))
  839. (defun bookmark-jump-noselect (bookmark)
  840. "Return the location pointed to by BOOKMARK (see `bookmark-jump').
  841. The return value has the form (BUFFER . POINT).
  842. Note: this function is deprecated and is present for Emacs 22
  843. compatibility only."
  844. (save-excursion
  845. (bookmark-handle-bookmark bookmark)
  846. (cons (current-buffer) (point))))
  847. (make-obsolete 'bookmark-jump-noselect 'bookmark-handle-bookmark "23.1")
  848. (defun bookmark-handle-bookmark (bookmark-name-or-record)
  849. "Call BOOKMARK-NAME-OR-RECORD's handler or `bookmark-default-handler'
  850. if it has none. This changes current buffer and point and returns nil,
  851. or signals a `file-error'.
  852. If BOOKMARK-NAME-OR-RECORD has no file, this is a no-op. If
  853. BOOKMARK-NAME-OR-RECORD has a file, but that file no longer exists,
  854. then offer interactively to relocate BOOKMARK-NAME-OR-RECORD."
  855. (condition-case err
  856. (funcall (or (bookmark-get-handler bookmark-name-or-record)
  857. 'bookmark-default-handler)
  858. (bookmark-get-bookmark bookmark-name-or-record))
  859. (bookmark-error-no-filename ;file-error
  860. ;; We were unable to find the marked file, so ask if user wants to
  861. ;; relocate the bookmark, else remind them to consider deletion.
  862. (when (stringp bookmark-name-or-record)
  863. ;; `bookmark-name-or-record' can be either a bookmark name
  864. ;; (from `bookmark-alist') or a bookmark object. If it's an
  865. ;; object, we assume it's a bookmark used internally by some
  866. ;; other package.
  867. (let ((file (bookmark-get-filename bookmark-name-or-record)))
  868. (when file ;Don't know how to relocate if there's no `file'.
  869. ;; If file is not a dir, directory-file-name just returns file.
  870. (let ((display-name (directory-file-name file)))
  871. (ding)
  872. ;; Dialog boxes can accept a file target, but usually don't
  873. ;; know how to accept a directory target (at least, this
  874. ;; is true in Gnome on GNU/Linux, and Bug#4230 says it's
  875. ;; true on Windows as well). So we suppress file dialogs
  876. ;; when relocating.
  877. (let ((use-dialog-box nil)
  878. (use-file-dialog nil))
  879. (if (y-or-n-p (concat display-name " nonexistent. Relocate \""
  880. bookmark-name-or-record "\"? "))
  881. (progn
  882. (bookmark-relocate bookmark-name-or-record)
  883. ;; Try again.
  884. (funcall (or (bookmark-get-handler bookmark-name-or-record)
  885. 'bookmark-default-handler)
  886. (bookmark-get-bookmark bookmark-name-or-record)))
  887. (message
  888. "Bookmark not relocated; consider removing it (%s)."
  889. bookmark-name-or-record)
  890. (signal (car err) (cdr err))))))))))
  891. ;; Added by db.
  892. (when (stringp bookmark-name-or-record)
  893. (setq bookmark-current-bookmark bookmark-name-or-record))
  894. nil)
  895. (put 'bookmark-error-no-filename
  896. 'error-conditions
  897. '(error bookmark-errors bookmark-error-no-filename))
  898. (put 'bookmark-error-no-filename
  899. 'error-message
  900. "Bookmark has no associated file (or directory)")
  901. (defun bookmark-default-handler (bmk-record)
  902. "Default handler to jump to a particular bookmark location.
  903. BMK-RECORD is a bookmark record, not a bookmark name (i.e., not a string).
  904. Changes current buffer and point and returns nil, or signals a `file-error'."
  905. (let ((file (bookmark-get-filename bmk-record))
  906. (buf (bookmark-prop-get bmk-record 'buffer))
  907. (forward-str (bookmark-get-front-context-string bmk-record))
  908. (behind-str (bookmark-get-rear-context-string bmk-record))
  909. (place (bookmark-get-position bmk-record)))
  910. (set-buffer
  911. (cond
  912. ((and file (file-readable-p file) (not (buffer-live-p buf)))
  913. (find-file-noselect file))
  914. ;; No file found. See if buffer BUF have been created.
  915. ((and buf (get-buffer buf)))
  916. (t ;; If not, raise error.
  917. (signal 'bookmark-error-no-filename (list 'stringp file)))))
  918. (if place (goto-char place))
  919. ;; Go searching forward first. Then, if forward-str exists and
  920. ;; was found in the file, we can search backward for behind-str.
  921. ;; Rationale is that if text was inserted between the two in the
  922. ;; file, it's better to be put before it so you can read it,
  923. ;; rather than after and remain perhaps unaware of the changes.
  924. (when (and forward-str (search-forward forward-str (point-max) t))
  925. (goto-char (match-beginning 0)))
  926. (when (and behind-str (search-backward behind-str (point-min) t))
  927. (goto-char (match-end 0)))
  928. nil))
  929. ;;;###autoload
  930. (defun bookmark-relocate (bookmark-name)
  931. "Relocate BOOKMARK-NAME to another file, reading file name with minibuffer.
  932. This makes an already existing bookmark point to that file, instead of
  933. the one it used to point at. Useful when a file has been renamed
  934. after a bookmark was set in it."
  935. (interactive (list (bookmark-completing-read "Bookmark to relocate")))
  936. (bookmark-maybe-historicize-string bookmark-name)
  937. (bookmark-maybe-load-default-file)
  938. (let* ((bmrk-filename (bookmark-get-filename bookmark-name))
  939. (newloc (abbreviate-file-name
  940. (expand-file-name
  941. (read-file-name
  942. (format "Relocate %s to: " bookmark-name)
  943. (file-name-directory bmrk-filename))))))
  944. (bookmark-set-filename bookmark-name newloc)
  945. (setq bookmark-alist-modification-count
  946. (1+ bookmark-alist-modification-count))
  947. (if (bookmark-time-to-save-p)
  948. (bookmark-save))
  949. (bookmark-bmenu-surreptitiously-rebuild-list)))
  950. ;;;###autoload
  951. (defun bookmark-insert-location (bookmark-name &optional no-history)
  952. "Insert the name of the file associated with BOOKMARK-NAME.
  953. Optional second arg NO-HISTORY means don't record this in the
  954. minibuffer history list `bookmark-history'."
  955. (interactive (list (bookmark-completing-read "Insert bookmark location")))
  956. (or no-history (bookmark-maybe-historicize-string bookmark-name))
  957. (let ((start (point)))
  958. (prog1
  959. (insert (bookmark-location bookmark-name))
  960. (if (display-mouse-p)
  961. (add-text-properties
  962. start
  963. (save-excursion (re-search-backward
  964. "[^ \t]")
  965. (1+ (point)))
  966. '(mouse-face highlight
  967. follow-link t
  968. help-echo "mouse-2: go to this bookmark in other window"))))))
  969. ;;;###autoload
  970. (defalias 'bookmark-locate 'bookmark-insert-location)
  971. (defun bookmark-location (bookmark-name-or-record)
  972. "Return a description of the location of BOOKMARK-NAME-OR-RECORD."
  973. (bookmark-maybe-load-default-file)
  974. ;; We could call the `handler' and ask for it to construct a description
  975. ;; dynamically: it would open up several new possibilities, but it
  976. ;; would have the major disadvantage of forcing to load each and
  977. ;; every handler when the user calls bookmark-menu.
  978. (or (bookmark-prop-get bookmark-name-or-record 'location)
  979. (bookmark-get-filename bookmark-name-or-record)
  980. "-- Unknown location --"))
  981. ;;;###autoload
  982. (defun bookmark-rename (old-name &optional new-name)
  983. "Change the name of OLD-NAME bookmark to NEW-NAME name.
  984. If called from keyboard, prompt for OLD-NAME and NEW-NAME.
  985. If called from menubar, select OLD-NAME from a menu and prompt for NEW-NAME.
  986. If called from Lisp, prompt for NEW-NAME if only OLD-NAME was passed
  987. as an argument. If called with two strings, then no prompting is done.
  988. You must pass at least OLD-NAME when calling from Lisp.
  989. While you are entering the new name, consecutive C-w's insert
  990. consecutive words from the text of the buffer into the new bookmark
  991. name."
  992. (interactive (list (bookmark-completing-read "Old bookmark name")))
  993. (bookmark-maybe-historicize-string old-name)
  994. (bookmark-maybe-load-default-file)
  995. (setq bookmark-yank-point (point))
  996. (setq bookmark-current-buffer (current-buffer))
  997. (let ((final-new-name
  998. (or new-name ; use second arg, if non-nil
  999. (read-from-minibuffer
  1000. "New name: "
  1001. nil
  1002. (let ((now-map (copy-keymap minibuffer-local-map)))
  1003. (define-key now-map "\C-w" 'bookmark-yank-word)
  1004. now-map)
  1005. nil
  1006. 'bookmark-history))))
  1007. (bookmark-set-name old-name final-new-name)
  1008. (setq bookmark-current-bookmark final-new-name)
  1009. (bookmark-bmenu-surreptitiously-rebuild-list)
  1010. (setq bookmark-alist-modification-count
  1011. (1+ bookmark-alist-modification-count))
  1012. (if (bookmark-time-to-save-p)
  1013. (bookmark-save))))
  1014. ;;;###autoload
  1015. (defun bookmark-insert (bookmark-name)
  1016. "Insert the text of the file pointed to by bookmark BOOKMARK-NAME.
  1017. BOOKMARK-NAME is a bookmark name (a string), not a bookmark record.
  1018. You may have a problem using this function if the value of variable
  1019. `bookmark-alist' is nil. If that happens, you need to load in some
  1020. bookmarks. See help on function `bookmark-load' for more about
  1021. this."
  1022. (interactive (list (bookmark-completing-read "Insert bookmark contents")))
  1023. (bookmark-maybe-historicize-string bookmark-name)
  1024. (bookmark-maybe-load-default-file)
  1025. (let ((orig-point (point))
  1026. (str-to-insert
  1027. (save-current-buffer
  1028. (bookmark-handle-bookmark bookmark-name)
  1029. (buffer-string))))
  1030. (insert str-to-insert)
  1031. (push-mark)
  1032. (goto-char orig-point)))
  1033. ;;;###autoload
  1034. (defun bookmark-delete (bookmark-name &optional batch)
  1035. "Delete BOOKMARK-NAME from the bookmark list.
  1036. Removes only the first instance of a bookmark with that name. If
  1037. there are one or more other bookmarks with the same name, they will
  1038. not be deleted. Defaults to the \"current\" bookmark (that is, the
  1039. one most recently used in this file, if any).
  1040. Optional second arg BATCH means don't update the bookmark list buffer,
  1041. probably because we were called from there."
  1042. (interactive
  1043. (list (bookmark-completing-read "Delete bookmark"
  1044. bookmark-current-bookmark)))
  1045. (bookmark-maybe-historicize-string bookmark-name)
  1046. (bookmark-maybe-load-default-file)
  1047. (let ((will-go (bookmark-get-bookmark bookmark-name 'noerror)))
  1048. (setq bookmark-alist (delq will-go bookmark-alist))
  1049. ;; Added by db, nil bookmark-current-bookmark if the last
  1050. ;; occurrence has been deleted
  1051. (or (bookmark-get-bookmark bookmark-current-bookmark 'noerror)
  1052. (setq bookmark-current-bookmark nil)))
  1053. (unless batch
  1054. (bookmark-bmenu-surreptitiously-rebuild-list))
  1055. (setq bookmark-alist-modification-count
  1056. (1+ bookmark-alist-modification-count))
  1057. (when (bookmark-time-to-save-p)
  1058. (bookmark-save)))
  1059. (defun bookmark-time-to-save-p (&optional final-time)
  1060. "Return t if it is time to save bookmarks to disk, nil otherwise.
  1061. Optional argument FINAL-TIME means this is being called when Emacs
  1062. is being killed, so save even if `bookmark-save-flag' is a number and
  1063. is greater than `bookmark-alist-modification-count'."
  1064. ;; By Gregory M. Saunders <saunders{_AT_}cis.ohio-state.edu>
  1065. (cond (final-time
  1066. (and (> bookmark-alist-modification-count 0)
  1067. bookmark-save-flag))
  1068. ((numberp bookmark-save-flag)
  1069. (>= bookmark-alist-modification-count bookmark-save-flag))
  1070. (t
  1071. nil)))
  1072. ;;;###autoload
  1073. (defun bookmark-write ()
  1074. "Write bookmarks to a file (reading the file name with the minibuffer).
  1075. Don't use this in Lisp programs; use `bookmark-save' instead."
  1076. (interactive)
  1077. (bookmark-maybe-load-default-file)
  1078. (bookmark-save t))
  1079. ;;;###autoload
  1080. (defun bookmark-save (&optional parg file)
  1081. "Save currently defined bookmarks.
  1082. Saves by default in the file defined by the variable
  1083. `bookmark-default-file'. With a prefix arg, save it in file FILE
  1084. \(second argument).
  1085. If you are calling this from Lisp, the two arguments are PARG and
  1086. FILE, and if you just want it to write to the default file, then
  1087. pass no arguments. Or pass in nil and FILE, and it will save in FILE
  1088. instead. If you pass in one argument, and it is non-nil, then the
  1089. user will be interactively queried for a file to save in.
  1090. When you want to load in the bookmarks from a file, use
  1091. `bookmark-load', \\[bookmark-load]. That function will prompt you
  1092. for a file, defaulting to the file defined by variable
  1093. `bookmark-default-file'."
  1094. (interactive "P")
  1095. (bookmark-maybe-load-default-file)
  1096. (cond
  1097. ((and (null parg) (null file))
  1098. ;;whether interactive or not, write to default file
  1099. (bookmark-write-file bookmark-default-file))
  1100. ((and (null parg) file)
  1101. ;;whether interactive or not, write to given file
  1102. (bookmark-write-file file))
  1103. ((and parg (not file))
  1104. ;;have been called interactively w/ prefix arg
  1105. (let ((file (read-file-name "File to save bookmarks in: ")))
  1106. (bookmark-write-file file)))
  1107. (t ; someone called us with prefix-arg *and* a file, so just write to file
  1108. (bookmark-write-file file)))
  1109. ;; signal that we have synced the bookmark file by setting this to
  1110. ;; 0. If there was an error at any point before, it will not get
  1111. ;; set, which is what we want.
  1112. (setq bookmark-alist-modification-count 0))
  1113. (defun bookmark-write-file (file)
  1114. "Write `bookmark-alist' to FILE."
  1115. (bookmark-maybe-message "Saving bookmarks to file %s..." file)
  1116. (with-current-buffer (get-buffer-create " *Bookmarks*")
  1117. (goto-char (point-min))
  1118. (delete-region (point-min) (point-max))
  1119. (let ((print-length nil)
  1120. (print-level nil))
  1121. (bookmark-insert-file-format-version-stamp)
  1122. (insert "(")
  1123. ;; Rather than a single call to `pp' we make one per bookmark.
  1124. ;; Apparently `pp' has a poor algorithmic complexity, so this
  1125. ;; scales a lot better. bug#4485.
  1126. (dolist (i bookmark-alist) (pp i (current-buffer)))
  1127. (insert ")")
  1128. (let ((version-control
  1129. (cond
  1130. ((null bookmark-version-control) nil)
  1131. ((eq 'never bookmark-version-control) 'never)
  1132. ((eq 'nospecial bookmark-version-control) version-control)
  1133. (t t))))
  1134. (condition-case nil
  1135. (write-region (point-min) (point-max) file)
  1136. (file-error (message "Can't write %s" file)))
  1137. (kill-buffer (current-buffer))
  1138. (bookmark-maybe-message
  1139. "Saving bookmarks to file %s...done" file)))))
  1140. (defun bookmark-import-new-list (new-list)
  1141. "Add NEW-LIST of bookmarks to `bookmark-alist'.
  1142. Rename new bookmarks as needed using suffix \"<N>\" (N=1,2,3...), when
  1143. they conflict with existing bookmark names."
  1144. (let ((names (bookmark-all-names)))
  1145. (dolist (full-record new-list)
  1146. (bookmark-maybe-rename full-record names)
  1147. (setq bookmark-alist (nconc bookmark-alist (list full-record)))
  1148. (push (bookmark-name-from-full-record full-record) names))))
  1149. (defun bookmark-maybe-rename (full-record names)
  1150. "Rename bookmark FULL-RECORD if its current name is already used.
  1151. This is a helper for `bookmark-import-new-list'."
  1152. (let ((found-name (bookmark-name-from-full-record full-record)))
  1153. (if (member found-name names)
  1154. ;; We've got a conflict, so generate a new name
  1155. (let ((count 2)
  1156. (new-name found-name))
  1157. (while (member new-name names)
  1158. (setq new-name (concat found-name (format "<%d>" count)))
  1159. (setq count (1+ count)))
  1160. (bookmark-set-name full-record new-name)))))
  1161. ;;;###autoload
  1162. (defun bookmark-load (file &optional overwrite no-msg)
  1163. "Load bookmarks from FILE (which must be in bookmark format).
  1164. Appends loaded bookmarks to the front of the list of bookmarks. If
  1165. optional second argument OVERWRITE is non-nil, existing bookmarks are
  1166. destroyed. Optional third arg NO-MSG means don't display any messages
  1167. while loading.
  1168. If you load a file that doesn't contain a proper bookmark alist, you
  1169. will corrupt Emacs's bookmark list. Generally, you should only load
  1170. in files that were created with the bookmark functions in the first
  1171. place. Your own personal bookmark file, `~/.emacs.bmk', is
  1172. maintained automatically by Emacs; you shouldn't need to load it
  1173. explicitly.
  1174. If you load a file containing bookmarks with the same names as
  1175. bookmarks already present in your Emacs, the new bookmarks will get
  1176. unique numeric suffixes \"<2>\", \"<3>\", ... following the same
  1177. method buffers use to resolve name collisions."
  1178. (interactive
  1179. (list (read-file-name
  1180. (format "Load bookmarks from: (%s) "
  1181. bookmark-default-file)
  1182. ;;Default might not be used often,
  1183. ;;but there's no better default, and
  1184. ;;I guess it's better than none at all.
  1185. "~/" bookmark-default-file 'confirm)))
  1186. (setq file (abbreviate-file-name (expand-file-name file)))
  1187. (if (not (file-readable-p file))
  1188. (error "Cannot read bookmark file %s" file)
  1189. (if (null no-msg)
  1190. (bookmark-maybe-message "Loading bookmarks from %s..." file))
  1191. (with-current-buffer (let ((enable-local-variables nil))
  1192. (find-file-noselect file))
  1193. (goto-char (point-min))
  1194. (bookmark-maybe-upgrade-file-format)
  1195. (let ((blist (bookmark-alist-from-buffer)))
  1196. (if (listp blist)
  1197. (progn
  1198. (if overwrite
  1199. (progn
  1200. (setq bookmark-alist blist)
  1201. (setq bookmark-alist-modification-count 0))
  1202. ;; else
  1203. (bookmark-import-new-list blist)
  1204. (setq bookmark-alist-modification-count
  1205. (1+ bookmark-alist-modification-count)))
  1206. (if (string-equal
  1207. (abbreviate-file-name
  1208. (expand-file-name bookmark-default-file))
  1209. file)
  1210. (setq bookmarks-already-loaded t))
  1211. (bookmark-bmenu-surreptitiously-rebuild-list))
  1212. (error "Invalid bookmark list in %s" file)))
  1213. (kill-buffer (current-buffer)))
  1214. (if (null no-msg)
  1215. (bookmark-maybe-message "Loading bookmarks from %s...done" file))))
  1216. ;;; Code supporting the dired-like bookmark menu.
  1217. ;; Prefix is "bookmark-bmenu" for "buffer-menu":
  1218. (defvar bookmark-bmenu-hidden-bookmarks ())
  1219. (defvar bookmark-bmenu-mode-map
  1220. (let ((map (make-keymap)))
  1221. (set-keymap-parent map special-mode-map)
  1222. (define-key map "v" 'bookmark-bmenu-select)
  1223. (define-key map "w" 'bookmark-bmenu-locate)
  1224. (define-key map "2" 'bookmark-bmenu-2-window)
  1225. (define-key map "1" 'bookmark-bmenu-1-window)
  1226. (define-key map "j" 'bookmark-bmenu-this-window)
  1227. (define-key map "\C-c\C-c" 'bookmark-bmenu-this-window)
  1228. (define-key map "f" 'bookmark-bmenu-this-window)
  1229. (define-key map "\C-m" 'bookmark-bmenu-this-window)
  1230. (define-key map "o" 'bookmark-bmenu-other-window)
  1231. (define-key map "\C-o" 'bookmark-bmenu-switch-other-window)
  1232. (define-key map "s" 'bookmark-bmenu-save)
  1233. (define-key map "k" 'bookmark-bmenu-delete)
  1234. (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
  1235. (define-key map "x" 'bookmark-bmenu-execute-deletions)
  1236. (define-key map "d" 'bookmark-bmenu-delete)
  1237. (define-key map " " 'next-line)
  1238. (define-key map "n" 'next-line)
  1239. (define-key map "p" 'previous-line)
  1240. (define-key map "\177" 'bookmark-bmenu-backup-unmark)
  1241. (define-key map "u" 'bookmark-bmenu-unmark)
  1242. (define-key map "m" 'bookmark-bmenu-mark)
  1243. (define-key map "l" 'bookmark-bmenu-load)
  1244. (define-key map "r" 'bookmark-bmenu-rename)
  1245. (define-key map "R" 'bookmark-bmenu-relocate)
  1246. (define-key map "t" 'bookmark-bmenu-toggle-filenames)
  1247. (define-key map "a" 'bookmark-bmenu-show-annotation)
  1248. (define-key map "A" 'bookmark-bmenu-show-all-annotations)
  1249. (define-key map "e" 'bookmark-bmenu-edit-annotation)
  1250. (define-key map "/" 'bookmark-bmenu-search)
  1251. (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
  1252. map))
  1253. ;; Bookmark Buffer Menu mode is suitable only for specially formatted
  1254. ;; data.
  1255. (put 'bookmark-bmenu-mode 'mode-class 'special)
  1256. ;; todo: need to display whether or not bookmark exists as a buffer in
  1257. ;; flag column.
  1258. ;; Format:
  1259. ;; FLAGS BOOKMARK [ LOCATION ]
  1260. (defun bookmark-bmenu-surreptitiously-rebuild-list ()
  1261. "Rebuild the Bookmark List if it exists.
  1262. Don't affect the buffer ring order."
  1263. (if (get-buffer "*Bookmark List*")
  1264. (save-excursion
  1265. (save-window-excursion
  1266. (bookmark-bmenu-list)))))
  1267. ;;;###autoload
  1268. (defun bookmark-bmenu-list ()
  1269. "Display a list of existing bookmarks.
  1270. The list is displayed in a buffer named `*Bookmark List*'.
  1271. The leftmost column displays a D if the bookmark is flagged for
  1272. deletion, or > if it is flagged for displaying."
  1273. (interactive)
  1274. (bookmark-maybe-load-default-file)
  1275. (let ((buf (get-buffer-create "*Bookmark List*")))
  1276. (if (called-interactively-p 'interactive)
  1277. (switch-to-buffer buf)
  1278. (set-buffer buf)))
  1279. (let ((inhibit-read-only t))
  1280. (erase-buffer)
  1281. (insert "% Bookmark\n- --------\n")
  1282. (add-text-properties (point-min) (point)
  1283. '(font-lock-face bookmark-menu-heading))
  1284. (dolist (full-record (bookmark-maybe-sort-alist))
  1285. (let ((name (bookmark-name-from-full-record full-record))
  1286. (annotation (bookmark-get-annotation full-record))
  1287. (start (point))
  1288. end)
  1289. ;; if a bookmark has an annotation, prepend a "*"
  1290. ;; in the list of bookmarks.
  1291. (insert (if (and annotation (not (string-equal annotation "")))
  1292. " *" " ")
  1293. name)
  1294. (setq end (point))
  1295. (put-text-property
  1296. (+ bookmark-bmenu-marks-width start) end 'bookmark-name-prop name)
  1297. (when (display-mouse-p)
  1298. (add-text-properties
  1299. (+ bookmark-bmenu-marks-width start) end
  1300. '(mouse-face highlight
  1301. follow-link t
  1302. help-echo "mouse-2: go to this bookmark in other window")))
  1303. (insert "\n")))
  1304. (set-buffer-modified-p (not (= bookmark-alist-modification-count 0)))
  1305. (goto-char (point-min))
  1306. (forward-line 2)
  1307. (bookmark-bmenu-mode)
  1308. (if bookmark-bmenu-toggle-filenames
  1309. (bookmark-bmenu-toggle-filenames t))))
  1310. ;;;###autoload
  1311. (defalias 'list-bookmarks 'bookmark-bmenu-list)
  1312. ;;;###autoload
  1313. (defalias 'edit-bookmarks 'bookmark-bmenu-list)
  1314. (define-derived-mode bookmark-bmenu-mode special-mode "Bookmark Menu"
  1315. "Major mode for editing a list of bookmarks.
  1316. Each line describes one of the bookmarks in Emacs.
  1317. Letters do not insert themselves; instead, they are commands.
  1318. Bookmark names preceded by a \"*\" have annotations.
  1319. \\<bookmark-bmenu-mode-map>
  1320. \\[bookmark-bmenu-mark] -- mark bookmark to be displayed.
  1321. \\[bookmark-bmenu-select] -- select bookmark of line point is on.
  1322. Also show bookmarks marked using m in other windows.
  1323. \\[bookmark-bmenu-toggle-filenames] -- toggle displaying of filenames (they may obscure long bookmark names).
  1324. \\[bookmark-bmenu-locate] -- display (in minibuffer) location of this bookmark.
  1325. \\[bookmark-bmenu-1-window] -- select this bookmark in full-frame window.
  1326. \\[bookmark-bmenu-2-window] -- select this bookmark in one window,
  1327. together with bookmark selected before this one in another window.
  1328. \\[bookmark-bmenu-this-window] -- select this bookmark in place of the bookmark menu buffer.
  1329. \\[bookmark-bmenu-other-window] -- select this bookmark in another window,
  1330. so the bookmark menu bookmark remains visible in its window.
  1331. \\[bookmark-bmenu-switch-other-window] -- switch the other window to this bookmark.
  1332. \\[bookmark-bmenu-rename] -- rename this bookmark (prompts for new name).
  1333. \\[bookmark-bmenu-relocate] -- relocate this bookmark's file (prompts for new file).
  1334. \\[bookmark-bmenu-delete] -- mark this bookmark to be deleted, and move down.
  1335. \\[bookmark-bmenu-delete-backwards] -- mark this bookmark to be deleted, and move up.
  1336. \\[bookmark-bmenu-execute-deletions] -- delete bookmarks marked with `\\[bookmark-bmenu-delete]'.
  1337. \\[bookmark-bmenu-save] -- save the current bookmark list in the default file.
  1338. With a prefix arg, prompts for a file to save in.
  1339. \\[bookmark-bmenu-load] -- load in a file of bookmarks (prompts for file.)
  1340. \\[bookmark-bmenu-unmark] -- remove all kinds of marks from current line.
  1341. With prefix argument, also move up one line.
  1342. \\[bookmark-bmenu-backup-unmark] -- back up a line and remove marks.
  1343. \\[bookmark-bmenu-show-annotation] -- show the annotation, if it exists, for the current bookmark
  1344. in another buffer.
  1345. \\[bookmark-bmenu-show-all-annotations] -- show the annotations of all bookmarks in another buffer.
  1346. \\[bookmark-bmenu-edit-annotation] -- edit the annotation for the current bookmark."
  1347. (setq truncate-lines t)
  1348. (setq buffer-read-only t))
  1349. (defun bookmark-bmenu-toggle-filenames (&optional show)
  1350. "Toggle whether filenames are shown in the bookmark list.
  1351. Optional argument SHOW means show them unconditionally."
  1352. (interactive)
  1353. (cond
  1354. (show
  1355. (setq bookmark-bmenu-toggle-filenames nil)
  1356. (bookmark-bmenu-show-filenames)
  1357. (setq bookmark-bmenu-toggle-filenames t))
  1358. (bookmark-bmenu-toggle-filenames
  1359. (bookmark-bmenu-hide-filenames)
  1360. (setq bookmark-bmenu-toggle-filenames nil))
  1361. (t
  1362. (bookmark-bmenu-show-filenames)
  1363. (setq bookmark-bmenu-toggle-filenames t))))
  1364. (defun bookmark-bmenu-show-filenames (&optional force)
  1365. "In an interactive bookmark list, show filenames along with bookmarks.
  1366. Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
  1367. mainly for debugging, and should not be necessary in normal use."
  1368. (if (and (not force) bookmark-bmenu-toggle-filenames)
  1369. nil ;already shown, so do nothing
  1370. (with-buffer-modified-unmodified
  1371. (save-excursion
  1372. (save-window-excursion
  1373. (goto-char (point-min))
  1374. (forward-line 2)
  1375. (setq bookmark-bmenu-hidden-bookmarks ())
  1376. (let ((inhibit-read-only t))
  1377. (while (< (point) (point-max))
  1378. (let ((bmrk (bookmark-bmenu-bookmark)))
  1379. (push bmrk bookmark-bmenu-hidden-bookmarks)
  1380. (let ((start (line-end-position)))
  1381. (move-to-column bookmark-bmenu-file-column t)
  1382. ;; Strip off `mouse-face' from the white spaces region.
  1383. (if (display-mouse-p)
  1384. (remove-text-properties start (point)
  1385. '(mouse-face nil help-echo nil))))
  1386. (delete-region (point) (progn (end-of-line) (point)))
  1387. (insert " ")
  1388. ;; Pass the NO-HISTORY arg:
  1389. (bookmark-insert-location bmrk t)
  1390. (forward-line 1)))))))))
  1391. (defun bookmark-bmenu-hide-filenames (&optional force)
  1392. "In an interactive bookmark list, hide the filenames of the bookmarks.
  1393. Non-nil FORCE forces a redisplay showing the filenames. FORCE is used
  1394. mainly for debugging, and should not be necessary in normal use."
  1395. (when (and (not force) bookmark-bmenu-toggle-filenames)
  1396. ;; nothing to hide if above is nil
  1397. (with-buffer-modified-unmodified
  1398. (save-excursion
  1399. (goto-char (point-min))
  1400. (forward-line 2)
  1401. (setq bookmark-bmenu-hidden-bookmarks
  1402. (nreverse bookmark-bmenu-hidden-bookmarks))
  1403. (let ((inhibit-read-only t))
  1404. (while bookmark-bmenu-hidden-bookmarks
  1405. (move-to-column bookmark-bmenu-marks-width t)
  1406. (bookmark-kill-line)
  1407. (let ((name (pop bookmark-bmenu-hidden-bookmarks))
  1408. (start (point)))
  1409. (insert name)
  1410. (put-text-property start (point) 'bookmark-name-prop name)
  1411. (if (display-mouse-p)
  1412. (add-text-properties
  1413. start (point)
  1414. '(mouse-face
  1415. highlight follow-link t help-echo
  1416. "mouse-2: go to this bookmark in other window"))))
  1417. (forward-line 1)))))))
  1418. (defun bookmark-bmenu-ensure-position ()
  1419. "If point is not on a bookmark line, move it to one.
  1420. If before the first bookmark line, move to the first; if after the
  1421. last full line, move to the last full line. The return value is undefined."
  1422. (cond ((< (count-lines (point-min) (point)) bookmark-bmenu-header-height)
  1423. (goto-char (point-min))
  1424. (forward-line bookmark-bmenu-header-height))
  1425. ((and (bolp) (eobp))
  1426. (beginning-of-line 0))))
  1427. (defun bookmark-bmenu-bookmark ()
  1428. "Return the bookmark for this line in an interactive bookmark list buffer."
  1429. (bookmark-bmenu-ensure-position)
  1430. (save-excursion
  1431. (beginning-of-line)
  1432. (forward-char bookmark-bmenu-marks-width)
  1433. (get-text-property (point) 'bookmark-name-prop)))
  1434. (defun bookmark-show-annotation (bookmark-name-or-record)
  1435. "Display the annotation for BOOKMARK-NAME-OR-RECORD in a buffer,
  1436. if an annotation exists."
  1437. (let ((annotation (bookmark-get-annotation bookmark-name-or-record)))
  1438. (when (and annotation (not (string-equal annotation "")))
  1439. (save-excursion
  1440. (let ((old-buf (current-buffer)))
  1441. (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
  1442. (delete-region (point-min) (point-max))
  1443. (insert annotation)
  1444. (goto-char (point-min))
  1445. (switch-to-buffer-other-window old-buf))))))
  1446. (defun bookmark-show-all-annotations ()
  1447. "Display the annotations for all bookmarks in a buffer."
  1448. (save-selected-window
  1449. (pop-to-buffer (get-buffer-create "*Bookmark Annotation*") t)
  1450. (delete-region (point-min) (point-max))
  1451. (dolist (full-record bookmark-alist)
  1452. (let* ((name (bookmark-name-from-full-record full-record))
  1453. (ann (bookmark-get-annotation full-record)))
  1454. (insert (concat name ":\n"))
  1455. (if (and ann (not (string-equal ann "")))
  1456. ;; insert the annotation, indented by 4 spaces.
  1457. (progn
  1458. (save-excursion (insert ann) (unless (bolp)
  1459. (insert "\n")))
  1460. (while (< (point) (point-max))
  1461. (beginning-of-line) ; paranoia
  1462. (insert " ")
  1463. (forward-line)
  1464. (end-of-line))))))
  1465. (goto-char (point-min))))
  1466. (defun bookmark-bmenu-mark ()
  1467. "Mark bookmark on this line to be displayed by \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-select]."
  1468. (interactive)
  1469. (beginning-of-line)
  1470. (bookmark-bmenu-ensure-position)
  1471. (with-buffer-modified-unmodified
  1472. (let ((inhibit-read-only t))
  1473. (delete-char 1)
  1474. (insert ?>)
  1475. (forward-line 1)
  1476. (bookmark-bmenu-ensure-position))))
  1477. (defun bookmark-bmenu-select ()
  1478. "Select this line's bookmark; also display bookmarks marked with `>'.
  1479. You can mark bookmarks with the \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-mark] command."
  1480. (interactive)
  1481. (let ((bmrk (bookmark-bmenu-bookmark))
  1482. (menu (current-buffer))
  1483. (others ())
  1484. tem)
  1485. (goto-char (point-min))
  1486. (while (re-search-forward "^>" nil t)
  1487. (setq tem (bookmark-bmenu-bookmark))
  1488. (let ((inhibit-read-only t))
  1489. (delete-char -1)
  1490. (insert ?\s))
  1491. (or (string-equal tem bmrk)
  1492. (member tem others)
  1493. (setq others (cons tem others))))
  1494. (setq others (nreverse others)
  1495. tem (/ (1- (frame-height)) (1+ (length others))))
  1496. (delete-other-windows)
  1497. (bookmark-jump bmrk)
  1498. (bury-buffer menu)
  1499. (if others
  1500. (while others
  1501. (split-window nil tem)
  1502. (other-window 1)
  1503. (bookmark-jump (car others))
  1504. (setq others (cdr others)))
  1505. (other-window 1))))
  1506. (defun bookmark-bmenu-any-marks ()
  1507. "Return non-nil if any bookmarks are marked in the marks column."
  1508. (save-excursion
  1509. (goto-char (point-min))
  1510. (bookmark-bmenu-ensure-position)
  1511. (catch 'found-mark
  1512. (while (not (eobp))
  1513. (beginning-of-line)
  1514. (if (looking-at "^\\S-")
  1515. (throw 'found-mark t)
  1516. (forward-line 1)))
  1517. nil)))
  1518. (defun bookmark-bmenu-save (parg)
  1519. "Save the current list into a bookmark file.
  1520. With a prefix arg, prompts for a file to save them in."
  1521. (interactive "P")
  1522. (save-excursion
  1523. (save-window-excursion
  1524. (bookmark-save parg)
  1525. (set-buffer-modified-p nil))))
  1526. (defun bookmark-bmenu-load ()
  1527. "Load the bookmark file and rebuild the bookmark menu-buffer."
  1528. (interactive)
  1529. (bookmark-bmenu-ensure-position)
  1530. (save-excursion
  1531. (save-window-excursion
  1532. ;; This will call `bookmark-bmenu-list'
  1533. (call-interactively 'bookmark-load))))
  1534. (defun bookmark-bmenu-1-window ()
  1535. "Select this line's bookmark, alone, in full frame."
  1536. (interactive)
  1537. (bookmark-jump (bookmark-bmenu-bookmark))
  1538. (bury-buffer (other-buffer))
  1539. (delete-other-windows))
  1540. (defun bookmark-bmenu-2-window ()
  1541. "Select this line's bookmark, with previous buffer in second window."
  1542. (interactive)
  1543. (let ((bmrk (bookmark-bmenu-bookmark))
  1544. (menu (current-buffer))
  1545. (pop-up-windows t))
  1546. (delete-other-windows)
  1547. (switch-to-buffer (other-buffer) nil t)
  1548. (bookmark--jump-via bmrk 'pop-to-buffer)
  1549. (bury-buffer menu)))
  1550. (defun bookmark-bmenu-this-window ()
  1551. "Select this line's bookmark in this window."
  1552. (interactive)
  1553. (bookmark-jump (bookmark-bmenu-bookmark)))
  1554. (defun bookmark-bmenu-other-window ()
  1555. "Select this line's bookmark in other window, leaving bookmark menu visible."
  1556. (interactive)
  1557. (let ((bookmark (bookmark-bmenu-bookmark)))
  1558. (bookmark--jump-via bookmark 'switch-to-buffer-other-window)))
  1559. (defun bookmark-bmenu-switch-other-window ()
  1560. "Make the other window select this line's bookmark.
  1561. The current window remains selected."
  1562. (interactive)
  1563. (let ((bookmark (bookmark-bmenu-bookmark))
  1564. (pop-up-windows t)
  1565. same-window-buffer-names
  1566. same-window-regexps)
  1567. (bookmark--jump-via bookmark 'display-buffer)))
  1568. (defun bookmark-bmenu-other-window-with-mouse (event)
  1569. "Select bookmark at the mouse pointer in other window, leaving bookmark menu visible."
  1570. (interactive "e")
  1571. (with-current-buffer (window-buffer (posn-window (event-end event)))
  1572. (save-excursion
  1573. (goto-char (posn-point (event-end event)))
  1574. (bookmark-bmenu-other-window))))
  1575. (defun bookmark-bmenu-show-annotation ()
  1576. "Show the annotation for the current bookmark in another window."
  1577. (interactive)
  1578. (let ((bookmark (bookmark-bmenu-bookmark)))
  1579. (bookmark-show-annotation bookmark)))
  1580. (defun bookmark-bmenu-show-all-annotations ()
  1581. "Show the annotation for all bookmarks in another window."
  1582. (interactive)
  1583. (bookmark-show-all-annotations))
  1584. (defun bookmark-bmenu-edit-annotation ()
  1585. "Edit the annotation for the current bookmark in another window."
  1586. (interactive)
  1587. (let ((bookmark (bookmark-bmenu-bookmark)))
  1588. (bookmark-edit-annotation bookmark)))
  1589. (defun bookmark-bmenu-unmark (&optional backup)
  1590. "Cancel all requested operations on bookmark on this line and move down.
  1591. Optional BACKUP means move up."
  1592. (interactive "P")
  1593. (beginning-of-line)
  1594. (bookmark-bmenu-ensure-position)
  1595. (with-buffer-modified-unmodified
  1596. (let ((inhibit-read-only t))
  1597. (delete-char 1)
  1598. ;; any flags to reset according to circumstances? How about a
  1599. ;; flag indicating whether this bookmark is being visited?
  1600. ;; well, we don't have this now, so maybe later.
  1601. (insert " "))
  1602. (forward-line (if backup -1 1))
  1603. (bookmark-bmenu-ensure-position)))
  1604. (defun bookmark-bmenu-backup-unmark ()
  1605. "Move up and cancel all requested operations on bookmark on line above."
  1606. (interactive)
  1607. (forward-line -1)
  1608. (bookmark-bmenu-ensure-position)
  1609. (bookmark-bmenu-unmark)
  1610. (forward-line -1)
  1611. (bookmark-bmenu-ensure-position))
  1612. (defun bookmark-bmenu-delete ()
  1613. "Mark bookmark on this line to be deleted.
  1614. To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
  1615. (interactive)
  1616. (beginning-of-line)
  1617. (bookmark-bmenu-ensure-position)
  1618. (with-buffer-modified-unmodified
  1619. (let ((inhibit-read-only t))
  1620. (delete-char 1)
  1621. (insert ?D)
  1622. (forward-line 1)
  1623. (bookmark-bmenu-ensure-position))))
  1624. (defun bookmark-bmenu-delete-backwards ()
  1625. "Mark bookmark on this line to be deleted, then move up one line.
  1626. To carry out the deletions that you've marked, use \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-execute-deletions]."
  1627. (interactive)
  1628. (bookmark-bmenu-delete)
  1629. (forward-line -2)
  1630. (bookmark-bmenu-ensure-position)
  1631. (forward-line 1)
  1632. (bookmark-bmenu-ensure-position))
  1633. (defun bookmark-bmenu-execute-deletions ()
  1634. "Delete bookmarks flagged `D'."
  1635. (interactive)
  1636. (message "Deleting bookmarks...")
  1637. (let ((o-point (point))
  1638. (o-str (save-excursion
  1639. (beginning-of-line)
  1640. (unless (looking-at "^D")
  1641. (buffer-substring
  1642. (point)
  1643. (progn (end-of-line) (point))))))
  1644. (o-col (current-column)))
  1645. (goto-char (point-min))
  1646. (forward-line 1)
  1647. (while (re-search-forward "^D" (point-max) t)
  1648. (bookmark-delete (bookmark-bmenu-bookmark) t)) ; pass BATCH arg
  1649. (bookmark-bmenu-list)
  1650. (if o-str
  1651. (progn
  1652. (goto-char (point-min))
  1653. (search-forward o-str)
  1654. (beginning-of-line)
  1655. (forward-char o-col))
  1656. (goto-char o-point))
  1657. (beginning-of-line)
  1658. (message "Deleting bookmarks...done")
  1659. ))
  1660. (defun bookmark-bmenu-rename ()
  1661. "Rename bookmark on current line. Prompts for a new name."
  1662. (interactive)
  1663. (let ((bmrk (bookmark-bmenu-bookmark))
  1664. (thispoint (point)))
  1665. (bookmark-rename bmrk)
  1666. (goto-char thispoint)))
  1667. (defun bookmark-bmenu-locate ()
  1668. "Display location of this bookmark. Displays in the minibuffer."
  1669. (interactive)
  1670. (let ((bmrk (bookmark-bmenu-bookmark)))
  1671. (message "%s" (bookmark-location bmrk))))
  1672. (defun bookmark-bmenu-relocate ()
  1673. "Change the file path of the bookmark on the current line,
  1674. prompting with completion for the new path."
  1675. (interactive)
  1676. (let ((bmrk (bookmark-bmenu-bookmark))
  1677. (thispoint (point)))
  1678. (bookmark-relocate bmrk)
  1679. (goto-char thispoint)))
  1680. ;;; Bookmark-bmenu search
  1681. ;; Store keyboard input for incremental search.
  1682. (defvar bookmark-search-pattern)
  1683. (defun bookmark-read-search-input ()
  1684. "Read each keyboard input and add it to `bookmark-search-pattern'."
  1685. (let ((prompt (propertize "Pattern: " 'face 'minibuffer-prompt))
  1686. ;; (inhibit-quit t) ; inhibit-quit is evil. Use it with extreme care!
  1687. (tmp-list ()))
  1688. (while
  1689. (let ((char (read-key (concat prompt bookmark-search-pattern))))
  1690. (case char
  1691. ((?\e ?\r) nil) ; RET or ESC break the search loop.
  1692. (?\C-g (setq bookmark-quit-flag t) nil)
  1693. (?\d (pop tmp-list) t) ; Delete last char of pattern with DEL
  1694. (t
  1695. (if (characterp char)
  1696. (push char tmp-list)
  1697. (setq unread-command-events
  1698. (nconc (mapcar 'identity
  1699. (this-single-command-raw-keys))
  1700. unread-command-events))
  1701. nil))))
  1702. (setq bookmark-search-pattern
  1703. (apply 'string (reverse tmp-list))))))
  1704. (defun bookmark-bmenu-filter-alist-by-regexp (regexp)
  1705. "Filter `bookmark-alist' with bookmarks matching REGEXP and rebuild list."
  1706. (let ((bookmark-alist
  1707. (loop for i in bookmark-alist
  1708. when (string-match regexp (car i)) collect i into new
  1709. finally return new)))
  1710. (bookmark-bmenu-list)))
  1711. ;;;###autoload
  1712. (defun bookmark-bmenu-search ()
  1713. "Incremental search of bookmarks, hiding the non-matches as we go."
  1714. (interactive)
  1715. (let ((bmk (bookmark-bmenu-bookmark))
  1716. (bookmark-search-pattern "")
  1717. (timer (run-with-idle-timer
  1718. bookmark-search-delay 'repeat
  1719. #'(lambda ()
  1720. (bookmark-bmenu-filter-alist-by-regexp
  1721. bookmark-search-pattern)))))
  1722. (unwind-protect
  1723. (bookmark-read-search-input)
  1724. (cancel-timer timer)
  1725. (message nil)
  1726. (when bookmark-quit-flag ; C-g hit restore menu list.
  1727. (bookmark-bmenu-list) (bookmark-bmenu-goto-bookmark bmk))
  1728. (setq bookmark-quit-flag nil))))
  1729. (defun bookmark-bmenu-goto-bookmark (name)
  1730. "Move point to bookmark with name NAME."
  1731. (goto-char (point-min))
  1732. (while (not (equal name (bookmark-bmenu-bookmark)))
  1733. (forward-line 1))
  1734. (forward-line 0))
  1735. ;;; Menu bar stuff. Prefix is "bookmark-menu".
  1736. (defun bookmark-menu-popup-paned-menu (event name entries)
  1737. "Pop up multi-paned menu at EVENT, return string chosen from ENTRIES.
  1738. That is, ENTRIES is a list of strings which appear as the choices
  1739. in the menu.
  1740. The number of panes depends on the number of entries.
  1741. The visible entries are truncated to `bookmark-menu-length', but the
  1742. strings returned are not."
  1743. (let ((f-height (/ (frame-height) 2))
  1744. (pane-list nil)
  1745. (iter 0))
  1746. (while entries
  1747. (let (lst
  1748. (count 0))
  1749. (while (and (< count f-height) entries)
  1750. (let ((str (car entries)))
  1751. (push (cons
  1752. (if (> (length str) bookmark-menu-length)
  1753. (substring str 0 bookmark-menu-length)
  1754. str)
  1755. str)
  1756. lst)
  1757. (setq entries (cdr entries))
  1758. (setq count (1+ count))))
  1759. (setq iter (1+ iter))
  1760. (push (cons
  1761. (format "-*- %s (%d) -*-" name iter)
  1762. (nreverse lst))
  1763. pane-list)))
  1764. ;; Popup the menu and return the string.
  1765. (x-popup-menu event (cons (concat "-*- " name " -*-")
  1766. (nreverse pane-list)))))
  1767. ;; Thanks to Roland McGrath for fixing menubar.el so that the
  1768. ;; following works, and for explaining what to do to make it work.
  1769. ;; We MUST autoload EACH form used to set up this variable's value, so
  1770. ;; that the whole job is done in loaddefs.el.
  1771. ;; Emacs menubar stuff.
  1772. ;;;###autoload
  1773. (defvar menu-bar-bookmark-map
  1774. (let ((map (make-sparse-keymap "Bookmark functions")))
  1775. (define-key map [load]
  1776. `(menu-item ,(purecopy "Load a Bookmark File...") bookmark-load
  1777. :help ,(purecopy "Load bookmarks from a bookmark file)")))
  1778. (define-key map [write]
  1779. `(menu-item ,(purecopy "Save Bookmarks As...") bookmark-write
  1780. :help ,(purecopy "Write bookmarks to a file (reading the file name with the minibuffer)")))
  1781. (define-key map [save]
  1782. `(menu-item ,(purecopy "Save Bookmarks") bookmark-save
  1783. :help ,(purecopy "Save currently defined bookmarks")))
  1784. (define-key map [edit]
  1785. `(menu-item ,(purecopy "Edit Bookmark List") bookmark-bmenu-list
  1786. :help ,(purecopy "Display a list of existing bookmarks")))
  1787. (define-key map [delete]
  1788. `(menu-item ,(purecopy "Delete Bookmark...") bookmark-delete
  1789. :help ,(purecopy "Delete a bookmark from the bookmark list")))
  1790. (define-key map [rename]
  1791. `(menu-item ,(purecopy "Rename Bookmark...") bookmark-rename
  1792. :help ,(purecopy "Change the name of a bookmark")))
  1793. (define-key map [locate]
  1794. `(menu-item ,(purecopy "Insert Location...") bookmark-locate
  1795. :help ,(purecopy "Insert the name of the file associated with a bookmark")))
  1796. (define-key map [insert]
  1797. `(menu-item ,(purecopy "Insert Contents...") bookmark-insert
  1798. :help ,(purecopy "Insert the text of the file pointed to by a bookmark")))
  1799. (define-key map [set]
  1800. `(menu-item ,(purecopy "Set Bookmark...") bookmark-set
  1801. :help ,(purecopy "Set a bookmark named inside a file.")))
  1802. (define-key map [jump]
  1803. `(menu-item ,(purecopy "Jump to Bookmark...") bookmark-jump
  1804. :help ,(purecopy "Jump to a bookmark (a point in some file)")))
  1805. map))
  1806. ;;;###autoload
  1807. (defalias 'menu-bar-bookmark-map menu-bar-bookmark-map)
  1808. ;; make bookmarks appear toward the right side of the menu.
  1809. (if (boundp 'menu-bar-final-items)
  1810. (if menu-bar-final-items
  1811. (push 'bookmark menu-bar-final-items))
  1812. (setq menu-bar-final-items '(bookmark)))
  1813. ;;;; end bookmark menu stuff ;;;;
  1814. ;; Load Hook
  1815. (defvar bookmark-load-hook nil
  1816. "Hook run at the end of loading library `bookmark.el'.")
  1817. ;; Exit Hook, called from kill-emacs-hook
  1818. (defvar bookmark-exit-hook nil
  1819. "Hook run when Emacs exits.")
  1820. (define-obsolete-variable-alias 'bookmark-exit-hooks 'bookmark-exit-hook "22.1")
  1821. (defun bookmark-exit-hook-internal ()
  1822. "Save bookmark state, if necessary, at Emacs exit time.
  1823. This also runs `bookmark-exit-hook'."
  1824. (run-hooks 'bookmark-exit-hook)
  1825. (and bookmark-alist
  1826. (bookmark-time-to-save-p t)
  1827. (bookmark-save)))
  1828. (unless noninteractive
  1829. (add-hook 'kill-emacs-hook 'bookmark-exit-hook-internal))
  1830. (defun bookmark-unload-function ()
  1831. "Unload the Bookmark library."
  1832. (when bookmark-save-flag (bookmark-save))
  1833. ;; continue standard unloading
  1834. nil)
  1835. (run-hooks 'bookmark-load-hook)
  1836. (provide 'bookmark)
  1837. ;;; bookmark.el ends here