variable bibfiles removed from ebib-get-local-databases
[ebib.git] / src / ebib.el
blob91733e334d9b7e131944ddc2962be4c434d0aa45
1 ;; Ebib v==VERSION==
2 ;;
3 ;; Copyright (c) 2003-2007 Joost Kremers
4 ;; All rights reserved.
5 ;;
6 ;; Redistribution and use in source and binary forms, with or without
7 ;; modification, are permitted provided that the following conditions
8 ;; are met:
9 ;;
10 ;; 1. Redistributions of source code must retain the above copyright
11 ;; notice, this list of conditions and the following disclaimer.
12 ;; 2. Redistributions in binary form must reproduce the above copyright
13 ;; notice, this list of conditions and the following disclaimer in the
14 ;; documentation and/or other materials provided with the distribution.
15 ;; 3. The name of the author may not be used to endorse or promote products
16 ;; derived from this software without specific prior written permission.
18 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 ;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 ;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE,
24 ;; DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 ;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (require 'cl)
31 ;;;;;;;;;;;;;;;;;;;;;;
32 ;; global variables ;;
33 ;;;;;;;;;;;;;;;;;;;;;;
35 ;; user customisation
37 (defgroup ebib nil "Ebib: a BibTeX database manager" :group 'Tex)
39 (defcustom ebib-default-type 'article
40 "*The default type for a newly created BibTeX entry."
41 :group 'ebib
42 :type 'symbol)
44 (defcustom ebib-preload-bib-files nil
45 "*List of .bib files to load automatically when Ebib starts."
46 :group 'ebib
47 :type '(repeat (file :must-match t)))
49 (defcustom ebib-additional-fields '(crossref url annote abstract keywords timestamp)
50 "*Holds a list of the additional fields."
51 :group 'ebib
52 :type '(repeat (symbol :tag "Field")))
54 (defcustom ebib-index-window-size 10
55 "*The number of lines used for the keys buffer window."
56 :group 'ebib
57 :type 'integer)
59 (defcustom ebib-insertion-strings '((0 . "%s")
60 (1 . "\\cite{%s}"))
61 "*The string to insert when calling EBIB-INSERT-BIBTEX-KEY.
63 The directive \"%s\" is replaced with the entry key."
64 :group 'ebib
65 :type '(repeat (cons :tag "Insertion string" (integer :tag "Number") (string))))
67 (defcustom ebib-sort-order nil
68 "*The fields on which the BibTeX entries are to be sorted in the .bib file.
70 Sorting is done on different sort levels, and each sort level contains one
71 or more sort keys."
72 :group 'ebib
73 :type '(repeat (repeat :tag "Sort level" (symbol :tag "Sort field"))))
75 (defcustom ebib-save-xrefs-first nil
76 "*If true, entries with a crossref field will be saved first in the .bib-file.
78 Setting this option has unpredictable results for the sort order
79 of entries, so it is not compatible with setting the Sort Order option."
80 :group 'ebib
81 :type 'boolean)
83 (defface ebib-crossref-face '((t (:foreground "red")))
84 "*Face used to indicate values inherited from crossreferenced entries."
85 :group 'ebib)
87 (defcustom ebib-use-timestamp nil
88 "*If true, new entries will get a time stamp.
89 The time stamp will be stored in a field \"timestamp\" that can
90 be made visible with the `H' command in the index buffer."
91 :group 'ebib
92 :type 'boolean)
94 (defcustom ebib-timestamp-format "%a %b %e %T %Y"
95 "*Format of the time string used in the timestamp.
97 The format is given directly to FORMAT-TIME-STRING, see the
98 documentation of that function for details."
99 :group 'ebib
100 :type 'string)
102 (defcustom ebib-standard-url-field 'url
103 "*Standard field to store urls in.
105 In the index buffer, the command ebib-browse-url can be used to
106 send a url to a browser. This option sets the field from which
107 this command extracts the url."
108 :group 'ebib
109 :type 'symbol)
111 (defcustom ebib-url-regexp "\\\\url{\\(.*\\)}\\|https?://[^ '<>\"\n\t\f]+"
112 "*Regexp to extract urls."
113 :group 'ebib
114 :type 'string)
116 (defcustom ebib-browser-command ""
117 "*Command to call the browser with.
119 GNU/Emacs has a function call-browser, which is used if this
120 option is unset."
121 :group 'ebib
122 :type '(string :tag "Browser command"))
124 (defcustom ebib-print-preamble nil
125 "*Preamble used for the LaTeX file for printing the database.
127 Each string is added to the preamble on a separate line."
128 :group 'ebib
129 :type '(repeat (string :tag "Add to preamble")))
131 (defcustom ebib-print-multiline nil
132 "*If set, multiline fields are included when printing the database."
133 :group 'ebib
134 :type 'boolean)
136 (defcustom ebib-latex-preamble '("\\bibliographystyle{plain}")
137 "*Preamble used for the LaTeX file for BibTeXing the database.
139 Each string is added to the preamble on a separate line."
140 :group 'ebib
141 :type '(repeat (string :tag "Add to preamble")))
143 (defcustom ebib-print-tempfile ""
144 "*Temporary file for use with EBIB-PRINT-DATABASE and EBIB-LATEX-DATABASE."
145 :group 'ebib
146 :type '(file))
148 (defcustom ebib-allow-identical-fields nil
149 "*If set, Ebib handles multiple occurrences of a field gracefully."
150 :group 'ebib
151 :type 'boolean)
153 (defvar ebib-entry-types-hash (make-hash-table)
154 "Holds the hash table containing the entry type definitions.")
155 (defvar ebib-unique-field-list nil
156 "Holds a list of all field names.")
158 (defmacro add-to-listq (listvar element &optional append fn)
159 (if (or (featurep 'xemacs)
160 (string< emacs-version "22"))
161 `(add-to-list (quote ,listvar) ,element ,append)
162 `(add-to-list (quote ,listvar) ,element ,append ,fn)))
164 (defun ebib-set-entry-types-hash (var value)
165 "Sets EBIB-ENTRY-TYPES-HASH on the basis of EBIB-ENTRY-TYPES"
166 (set-default var value)
167 (clrhash ebib-entry-types-hash)
168 (setq ebib-unique-field-list nil)
169 (mapc #'(lambda (entry)
170 (puthash (car entry) (cdr entry) ebib-entry-types-hash)
171 (mapc #'(lambda (field)
172 (add-to-listq ebib-unique-field-list field t 'eq))
173 (cadr entry))
174 (mapc #'(lambda (field)
175 (add-to-listq ebib-unique-field-list field t 'eq))
176 (caddr entry)))
177 value))
179 (defcustom ebib-entry-types
180 '((article ;; name of entry type
181 (author title journal year) ;; obligatory fields
182 (volume number pages month note)) ;; optional fields
184 (book
185 (author title publisher year)
186 (editor volume number series address edition month note))
188 (booklet
189 (title)
190 (author howpublished address month year note))
192 (inbook
193 (author title chapter pages publisher year)
194 (editor volume series address edition month note))
196 (incollection
197 (author title booktitle publisher year)
198 (editor volume number series type chapter pages address edition month note))
200 (inproceedings
201 (author title booktitle year)
202 (editor pages organization publisher address month note))
204 (manual
205 (title)
206 (author organization address edition month year note))
208 (misc
210 (title author howpublished month year note))
212 (mastersthesis
213 (author title school year)
214 (address month note))
216 (phdthesis
217 (author title school year)
218 (address month note))
220 (proceedings
221 (title year)
222 (editor publisher organization address month note))
224 (techreport
225 (author title institution year)
226 (type number address month note))
228 (unpublished
229 (author title note)
230 (month year)))
232 "List of entry type definitions for Ebib"
233 :group 'ebib
234 :type '(repeat (list :tag "Entry type" (symbol :tag "Name")
235 (repeat :tag "Obligatory fields" (symbol :tag "Field"))
236 (repeat :tag "Optional fields" (symbol :tag "Field"))))
237 :set 'ebib-set-entry-types-hash)
239 ;; generic for all databases
241 ;; constants and variables that are set only once
242 (defconst ebib-bibtex-identifier "[^\"#%'(),={} \t\n\f]*" "Regex describing a licit BibTeX identifier.")
243 (defconst ebib-version "==VERSION==")
244 (defvar ebib-initialized nil "T if Ebib has been initialized.")
246 ;; buffers and highlights
247 (defvar ebib-index-buffer nil "The index buffer.")
248 (defvar ebib-entry-buffer nil "The entry buffer.")
249 (defvar ebib-strings-buffer nil "The strings buffer.")
250 (defvar ebib-multiline-buffer nil "Buffer for editing multiline strings.")
251 (defvar ebib-help-buffer nil "Buffer showing Ebib help.")
252 (defvar ebib-index-highlight nil "Highlight to mark the current entry.")
253 (defvar ebib-fields-highlight nil "Highlight to mark the current field.")
254 (defvar ebib-strings-highlight nil "Highlight to mark the current string.")
256 ;; general bookkeeping
257 (defvar ebib-minibuf-hist nil "Holds the minibuffer history for Ebib")
258 (defvar ebib-saved-window-config nil "Stores the window configuration when Ebib is called.")
259 (defvar ebib-export-filename nil "Filename to export entries to.")
260 (defvar ebib-push-buffer nil "Buffer to push entries to.")
261 (defvar ebib-search-string nil "Stores the last search string.")
262 (defvar ebib-editing nil "Indicates what the user is editing.
263 Its value can be 'strings, 'fields, or 'preamble.")
264 (defvar ebib-multiline-raw nil "Indicates whether the multiline text being edited is raw.")
265 (defvar ebib-before-help nil "Stores the buffer the user was in when he displayed the help message.")
266 (defvar ebib-local-bibtex-filenames nil "A buffer-local variable holding a list of the name(s) of that buffer's .bib file")
267 (make-variable-buffer-local 'ebib-local-bibtex-filenames)
269 ;; the databases
271 ;; each database is represented by a struct
272 (defstruct edb
273 (database (make-hash-table :test 'equal)) ; hashtable containing the database itself
274 (keys-list nil) ; sorted list of the keys in the database
275 (cur-entry nil) ; sublist of KEYS-LIST that starts with the current entry
276 (marked-entries nil) ; list of marked entries
277 (n-entries 0) ; number of entries stored in this database
278 (strings (make-hash-table :test 'equal)) ; hashtable with the @STRING definitions
279 (strings-list nil) ; sorted list of the @STRING abbreviations
280 (preamble nil) ; string with the @PREAMBLE definition
281 (filename nil) ; name of the BibTeX file that holds this database
282 (name nil) ; name of the database
283 (modified nil) ; has this database been modified?
284 (make-backup nil) ; do we need to make a backup of the .bib file?
285 (virtual nil)) ; is this a virtual database?
287 ;; the master list and the current database
288 (defvar ebib-databases nil "List of structs containing the databases")
289 (defvar ebib-cur-db nil "The database that is currently active")
291 ;;;;;; bookkeeping required when editing field values or @STRING definitions
293 (defvar ebib-hide-hidden-fields t "If set to T, hidden fields are not shown.")
295 ;; these two variables are set when the user enters the entry buffer
296 (defvar ebib-cur-entry-hash nil "The hash table containing the data of the current entry.")
297 (defvar ebib-cur-entry-fields nil "The fields of the type of the current entry.")
299 ;; and these two are set by EBIB-FILL-ENTRY-BUFFER and EBIB-FILL-STRINGS-BUFFER, respectively
300 (defvar ebib-current-field nil "The current field.")
301 (defvar ebib-current-string nil "The current @STRING definition.")
303 ;; we define these variables here, but only give them a value at the end of
304 ;; the file, because the long strings mess up Emacs' syntax highlighting.
305 (defvar ebib-index-buffer-help nil "Help for the index buffer.")
306 (defvar ebib-entry-buffer-help nil "Help for the entry buffer.")
307 (defvar ebib-strings-buffer-help nil "Help for the strings buffer.")
309 ;; for highlighting marked entries
310 (if (featurep 'xemacs)
311 (defvar ebib-marked-face 'highlight)
312 (defvar ebib-marked-face '(:inverse-video t)))
314 ;; the prefix key is stored in a variable so that the user can customise it.
315 (defvar ebib-prefix-key ?\;)
317 ;; this is an AucTeX variable, but we want to check its value, so let's
318 ;; keep the compiler from complaining.
319 (eval-when-compile
320 (defvar TeX-master))
322 ;; this is to keep XEmacs from complaining.
323 (eval-when-compile
324 (if (featurep 'xemacs)
325 (defvar mark-active)))
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 ;; useful macros and functions ;;
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 (defmacro nor (&rest args)
332 "Returns T if none of its arguments are true."
333 `(not (or ,@args)))
335 ;; we sometimes (often, in fact ;-) need to do something with a string, but
336 ;; take special action (or do nothing) if that string is empty. IF-STR
337 ;; makes that easier:
339 (defmacro if-str (bindvar then &rest else)
340 "Execute THEN only if STRING is nonempty.
342 Format: (if-str (var value) then-form [else-forms])
343 VAR is bound to VALUE, which is evaluated. If VAR is a nonempty string,
344 THEN-FORM is executed. If VAR is either \"\" or nil, ELSE-FORM is
345 executed. Returns the value of THEN or of ELSE."
346 (declare (indent 2))
347 `(let ,(list bindvar)
348 (if (nor (null ,(car bindvar))
349 (equal ,(car bindvar) ""))
350 ,then
351 ,@else)))
353 (defmacro last1 (lst &optional n)
354 "Returns the last (or Nth last) element of LST."
355 `(car (last ,lst ,n)))
357 ;; we sometimes need to walk through lists. these functions yield the
358 ;; element directly preceding or following ELEM in LIST. in order to work
359 ;; properly, ELEM must be unique in LIST, obviously. if ELEM is the
360 ;; first/last element of LIST, or if it is not contained in LIST at all,
361 ;; the result is nil.
362 (defun next-elem (elem list)
363 (cadr (member elem list)))
365 (defun prev-elem (elem list)
366 (if (or (equal elem (car list))
367 (not (member elem list)))
369 (last1 list (1+ (length (member elem list))))))
371 (defun read-string-at-point (chars)
372 "Reads a string at POINT delimited by CHARS and returns it.
374 CHARS is a string of characters that should not occur in the string."
375 (save-excursion
376 (skip-chars-backward (concat "^" chars))
377 (let ((beg (point)))
378 (looking-at-goto-end (concat "[^" chars "]*"))
379 (buffer-substring-no-properties beg (point)))))
381 (defun remove-from-string (string remove)
382 "Returns a copy of STRING with all the occurrences of REMOVE taken out.
384 REMOVE can be a regex."
385 (apply 'concat (split-string string remove)))
387 (defun in-string (char string)
388 "Returns T if CHAR is in STRING, otherwise NIL."
389 (catch 'found
390 (do ((len (length string))
391 (i 0 (1+ i)))
392 ((= i len) nil)
393 (if (eq char (aref string i))
394 (throw 'found t)))))
396 (defun ensure-extension (string ext)
397 "Makes sure STRING has the extension EXT, by appending it if necessary.
399 EXT should be an extension without the dot."
400 (if (string-match (concat "\\." ext "$") string)
401 string
402 (concat string "." ext)))
404 (defmacro with-buffer-writable (&rest body)
405 "Makes the current buffer writable and executes the commands in BODY.
406 After BODY is executed, the buffer modified flag is unset."
407 (declare (indent defun))
408 `(unwind-protect
409 (let ((buffer-read-only nil))
410 ,@body)
411 (set-buffer-modified-p nil)))
413 (defmacro safe-write-region (start end filename &optional append visit lockname mustbenew)
414 "XEmacs does not have the MUSTBENEW argument, so this is a way to implement it."
415 (if (featurep 'xemacs)
416 `(if (and (file-exists-p ,filename)
417 (not (y-or-n-p (format "File %s already exists; overwrite anyway? " ,filename))))
418 (error "File %s exist" ,filename)
419 (write-region ,start ,end ,filename ,append ,visit ,lockname))
420 `(write-region ,start ,end ,filename ,append ,visit ,lockname ,mustbenew)))
422 (defun symbol-or-string (x)
423 "Returns the symbol-name of X if X is a symbol, otherwise return X.
425 Much like SYMBOL-NAME, except it does not throw an error if X is not a
426 symbol."
427 (if (symbolp x)
428 (symbol-name x)
431 ;; XEmacs doesn't know about propertize...
432 (if (not (fboundp 'propertize))
433 (defun propertize (string &rest properties)
434 "Return a copy of STRING with text properties added.
435 First argument is the string to copy.
436 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
437 properties to add to the result."
438 (let ((new-string (copy-sequence string)))
439 (add-text-properties 0 (length new-string) properties new-string)
440 new-string)))
442 (defun region-active ()
443 (if (featurep 'xemacs)
444 (region-active-p)
445 mark-active))
447 ;; RAW-P determines if STRING is raw. note that we cannot do this by
448 ;; simply checking whether STRING begins with { and ends with } (or
449 ;; begins and ends with "), because something like "{abc} # D # {efg}"
450 ;; would then be incorrectly recognised as non-raw. so we need to do
451 ;; the following: take out everything that is between braces or
452 ;; quotes, and see if anything is left. if there is, the original
453 ;; string was raw, otherwise it was not.
455 ;; so i first check whether the string begins with { or ". if not, we
456 ;; certainly have a raw string. (RAW-P recognises this through the default
457 ;; clause of the COND.) if the first character is { or ", we first take out
458 ;; every occurrence of backslash-escaped { and } or ", so that the rest of
459 ;; the function does not get confused over them.
461 ;; then, if the first character is {, i use REMOVE-FROM-STRING to take out
462 ;; every occurrence of the regex "{[^{]*?}", which translates to "the
463 ;; smallest string that starts with { and ends with }, and does not contain
464 ;; another {. IOW, it takes out the innermost braces and their
465 ;; contents. because braces may be embedded, we have to repeat this step
466 ;; until no more balanced braces are found in the string. (note that it
467 ;; would be unwise to check for just the occurrence of { or }, because that
468 ;; would throw RAW-P in an infinite loop if a string contains an unbalanced
469 ;; brace.)
471 ;; for strings beginning with " i do the same, except that it is not
472 ;; necessary to repeat this in a WHILE loop, for the simple reason that
473 ;; strings surrounded with double quotes cannot be embedded; i.e.,
474 ;; "ab"cd"ef" is not a valid (BibTeX) string, while {ab{cd}ef} is.
476 ;; note: because these strings are to be fed to BibTeX and ultimately
477 ;; (La)TeX, it might seem that we don't need to worry about strings
478 ;; containing unbalanced braces, because (La)TeX would choke on them. but
479 ;; the user may inadvertently enter such a string, and we therefore need to
480 ;; be able to handle it. (alternatively, we could perform a check on
481 ;; strings and warn the user.)
483 (defun raw-p (string)
484 "Non-nil if STRING is raw."
485 (when (stringp string)
486 (cond
487 ((eq (string-to-char string) ?\{)
488 ;; we remove all occurrences of `\{' and of `\}' from the string:
489 (let ((clear-str (remove-from-string (remove-from-string string "[\\][{]")
490 "[\\][}]")))
491 (while (and (in-string ?\{ clear-str) (in-string ?\} clear-str))
492 (setq clear-str (remove-from-string clear-str "{[^{]*?}")))
493 (> (length clear-str) 0)))
494 ((eq (string-to-char string) ?\")
495 (let ((clear-str (remove-from-string string "[\\][\"]"))) ; remove occurrences of `\"'
496 (setq clear-str (remove-from-string clear-str "\"[^\"]*?\""))
497 (> (length clear-str) 0)))
498 (t t))))
500 (defun to-raw (string)
501 "Converts a string to its raw counterpart."
502 (if (and (stringp string)
503 (not (raw-p string)))
504 (substring string 1 -1)
505 string))
507 (defun from-raw (string)
508 "Converts a raw string to a non-raw one."
509 (if (raw-p string)
510 (concat "{" string "}")
511 string))
513 (defun multiline-p (string)
514 "True if STRING is multiline."
515 (if (stringp string)
516 (string-match "\n" string)))
518 (defun first-line (string)
519 "Returns the first line of a multi-line string."
520 (string-match "\n" string)
521 (substring string 0 (match-beginning 0)))
523 (defun sort-in-buffer (limit str)
524 "Moves POINT to the right position to insert STR in a buffer with lines sorted A-Z."
525 (let ((upper limit)
526 middle)
527 (when (> limit 0)
528 (let ((lower 0))
529 (goto-char (point-min))
530 (while (progn
531 (setq middle (/ (+ lower upper 1) 2))
532 (goto-line middle) ; if this turns out to be where we need to be,
533 (beginning-of-line) ; this puts POINT at the right spot.
534 (> (- upper lower) 1)) ; if upper and lower differ by only 1, we have found the
535 ; position to insert the entry in.
536 (save-excursion
537 (let ((beg (point)))
538 (end-of-line)
539 (if (string< (buffer-substring-no-properties beg (point)) str)
540 (setq lower middle)
541 (setq upper middle)))))))))
543 (defun match-all (match-str string)
544 "Highlights all the matches of MATCH-STR in STRING.
546 The return value is a list of two elements: the first is the modified
547 string, the second either t or nil, indicating whether a match was found at
548 all."
549 (do ((counter 0 (match-end 0)))
550 ((not (string-match match-str string counter)) (values string (not (= counter 0))))
551 (add-text-properties (match-beginning 0) (match-end 0) '(face highlight) string)))
553 (defun looking-at-goto-end (str &optional match)
554 "Like LOOKING-AT but moves point to the end of the matching string.
556 MATCH acts just like the argument to MATCH-END, and defaults to 0."
557 (or match (setq match 0))
558 (let ((case-fold-search t))
559 (if (looking-at str)
560 (goto-char (match-end match)))))
562 ;; this needs to be wrapped in an eval-and-compile, to keep Emacs from
563 ;; complaining that ebib-execute-helper isn't defined when it compiles
564 ;; ebib-execute-when.
565 (eval-and-compile
566 (defun ebib-execute-helper (env)
567 "Helper function for EBIB-EXECUTE-WHEN."
568 (cond
569 ((eq env 'entries)
570 '(and ebib-cur-db
571 (edb-cur-entry ebib-cur-db)))
572 ((eq env 'marked-entries)
573 '(and ebib-cur-db
574 (edb-marked-entries ebib-cur-db)))
575 ((eq env 'database)
576 'ebib-cur-db)
577 ((eq env 'real-db)
578 '(and ebib-cur-db
579 (not (edb-virtual ebib-cur-db))))
580 ((eq env 'virtual-db)
581 '(and ebib-cur-db
582 (edb-virtual ebib-cur-db)))
583 ((eq env 'no-database)
584 '(not ebib-cur-db))
585 (t t))))
587 (defmacro ebib-execute-when (&rest forms)
588 "Macro to facilitate writing Ebib functions.
590 This functions essentially like a COND clause: the basic format
591 is (ebib-execute-when FORMS ...), where each FORM is built up
592 as (ENVIRONMENTS BODY). ENVIRONMENTS is a list of symbols (not
593 quoted) that specify under which conditions BODY is to be
594 executed. Valid symbols are:
596 entries: execute when there are entries in the database,
597 marked-entries: execute when there are marked entries in the database,
598 database: execute if there is a database,
599 no-database: execute if there is no database,
600 real-db: execute when there is a database and it is real,
601 virtual-db: execute when there is a database and it is virtual,
602 default: execute if all else fails.
604 Just like with COND, only one form is actually executed, the
605 first one that matches. If ENVIRONMENT contains more than one
606 condition, BODY is executed if they all match (i.e., the
607 conditions are AND'ed.)"
608 (declare (indent defun))
609 `(cond
610 ,@(mapcar #'(lambda (form)
611 (cons (if (= 1 (length (car form)))
612 (ebib-execute-helper (caar form))
613 `(and ,@(mapcar #'(lambda (env)
614 (ebib-execute-helper env))
615 (car form))))
616 (cdr form)))
617 forms)))
619 ;; the numeric prefix argument is 1 if the user gave no prefix argument at
620 ;; all. the raw prefix argument is not always a number. so we need to do
621 ;; our own conversion.
622 (defun ebib-prefix (num)
623 (when (numberp num)
624 num))
626 (defun ebib-called-with-prefix ()
627 "Returns T if the command was called with a prefix key."
628 (if (featurep 'xemacs)
629 (member (character-to-event ebib-prefix-key) (append (this-command-keys) nil))
630 (member (event-convert-list (list ebib-prefix-key))
631 (append (this-command-keys-vector) nil))))
633 (defmacro ebib-export-to-db (num message copy-fn)
634 "Exports data to another database.
636 NUM is the number of the database to which the data is to be copied.
638 MESSAGE is a string displayed in the echo area if the export was
639 succesful. It must contain a %d directive, which is used to
640 display the database number to which the entry was exported.
642 COPY-FN is the function that actually copies the relevant
643 data. It must take as argument the database to which the data is
644 to be copied. COPY-FN must return T if the copying was
645 successful, and NIL otherwise."
646 `(let ((goal-db (nth (1- ,num) ebib-databases)))
647 (cond
648 ((not goal-db)
649 (error "Database %d does not exist" ,num))
650 ((edb-virtual goal-db)
651 (error "Database %d is virtual" ,num))
652 (t (when (funcall ,copy-fn goal-db)
653 (ebib-set-modified t goal-db)
654 (message ,message ,num))))))
656 (defmacro ebib-export-to-file (prompt-string message insert-fn)
657 "Exports data to a file.
659 PROMPT-STRING is the string that is used to ask for the filename
660 to export to. INSERT-FN must insert the data to be exported into
661 the current buffer: it is called within a WITH-TEMP-BUFFER, whose
662 contents is appended to the file the user enters.
664 MESSAGE is shown in the echo area when the export was
665 successful. It must contain a %s directive, which is used to
666 display the actual filename."
667 `(let ((insert-default-directory (not ebib-export-filename)))
668 (if-str (filename (read-file-name
669 ,prompt-string "~/" nil nil ebib-export-filename))
670 (with-temp-buffer
671 (funcall ,insert-fn)
672 (append-to-file (point-min) (point-max) filename)
673 (setq ebib-export-filename filename)))))
675 (defun ebib-get-obl-fields (entry-type)
676 "Returns the obligatory fields of ENTRY-TYPE."
677 (car (gethash entry-type ebib-entry-types-hash)))
679 (defun ebib-get-opt-fields (entry-type)
680 "Returns the optional fields of ENTRY-TYPE."
681 (cadr (gethash entry-type ebib-entry-types-hash)))
683 (defun ebib-get-all-fields (entry-type)
684 "Returns all the fields of ENTRY-TYPE."
685 (cons 'type* (append (ebib-get-obl-fields entry-type)
686 (ebib-get-opt-fields entry-type)
687 ebib-additional-fields)))
689 (defun ebib-erase-buffer (buffer)
690 (set-buffer buffer)
691 (with-buffer-writable
692 (erase-buffer)))
694 (defun ebib-make-highlight (begin end buffer)
695 (let (highlight)
696 (if (featurep 'xemacs)
697 (progn
698 (setq highlight (make-extent begin end buffer))
699 (set-extent-face highlight 'highlight))
700 (progn
701 (setq highlight (make-overlay begin end buffer))
702 (overlay-put highlight 'face 'highlight)))
703 highlight))
705 (defun ebib-move-highlight (highlight begin end buffer)
706 (if (featurep 'xemacs)
707 (set-extent-endpoints highlight begin end buffer)
708 (move-overlay highlight begin end buffer)))
710 (defun ebib-highlight-start (highlight)
711 (if (featurep 'xemacs)
712 (extent-start-position highlight)
713 (overlay-start highlight)))
715 (defun ebib-highlight-end (highlight)
716 (if (featurep 'xemacs)
717 (extent-end-position highlight)
718 (overlay-end highlight)))
720 (defun ebib-delete-highlight (highlight)
721 (if (featurep 'xemacs)
722 (detach-extent highlight)
723 (delete-overlay highlight)))
725 (defun ebib-set-index-highlight ()
726 (set-buffer ebib-index-buffer)
727 (beginning-of-line)
728 (let ((beg (point)))
729 (end-of-line)
730 (ebib-move-highlight ebib-index-highlight beg (point) ebib-index-buffer)))
732 (defun ebib-set-fields-highlight ()
733 (set-buffer ebib-entry-buffer)
734 (beginning-of-line)
735 (let ((beg (point)))
736 (looking-at-goto-end "[^ \t\n\f]*")
737 (ebib-move-highlight ebib-fields-highlight beg (point) ebib-entry-buffer)))
739 (defun ebib-set-strings-highlight ()
740 (set-buffer ebib-strings-buffer)
741 (beginning-of-line)
742 (let ((beg (point)))
743 (looking-at-goto-end "[^ \t\n\f]*")
744 (ebib-move-highlight ebib-strings-highlight beg (point) ebib-strings-buffer)))
746 (defun ebib-redisplay-current-field ()
747 "Redisplays the contents of the current field in the entry buffer."
748 (set-buffer ebib-entry-buffer)
749 (if (eq ebib-current-field 'crossref)
750 (ebib-fill-entry-buffer)
751 (with-buffer-writable
752 (goto-char (ebib-highlight-start ebib-fields-highlight))
753 (let ((beg (point)))
754 (end-of-line)
755 (delete-region beg (point)))
756 (insert (format "%-17s " (symbol-name ebib-current-field))
757 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash))
758 (ebib-set-fields-highlight))))
760 (defun ebib-redisplay-current-string ()
761 "Redisplays the current string definition in the strings buffer."
762 (set-buffer ebib-strings-buffer)
763 (with-buffer-writable
764 (let ((str (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
765 (goto-char (ebib-highlight-start ebib-strings-highlight))
766 (let ((beg (point)))
767 (end-of-line)
768 (delete-region beg (point)))
769 (insert (format "%-18s %s" ebib-current-string
770 (if (multiline-p str)
771 (concat "+" (first-line str))
772 (concat " " str))))
773 (ebib-set-strings-highlight))))
775 (defun ebib-move-to-field (field direction)
776 "Moves the fields overlay to the line containing FIELD.
778 If DIRECTION is positive, searches forward, if DIRECTION is
779 negative, searches backward. If DIRECTION is 1 or -1, searches
780 from POINT, if DIRECTION is 2 or -2, searches from beginning or
781 end of buffer. If FIELD is not found in the entry buffer, the
782 overlay is not moved. FIELD must be a symbol."
784 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
786 (set-buffer ebib-entry-buffer)
787 (if (eq field 'type*)
788 (goto-char (point-min))
789 (multiple-value-bind (fn start limit) (if (>= direction 0)
790 (values 're-search-forward (point-min) (point-max))
791 (values 're-search-backward (point-max) (point-min)))
792 ;; make sure we can get back to our original position, if the field
793 ;; cannot be found in the buffer:
794 (let ((current-pos (point)))
795 (when (evenp direction)
796 (goto-char start))
797 (unless (funcall fn (concat "^" (symbol-name field)) limit t)
798 (goto-char current-pos)))))
799 (ebib-set-fields-highlight))
801 (defun ebib-create-collection (hashtable)
802 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
804 The keys of HASHTABLE must be either symbols or strings."
805 (let ((result nil))
806 (maphash #'(lambda (x y)
807 (setq result (cons (cons (symbol-or-string x)
809 result)))
810 hashtable)
811 result))
813 (defmacro ebib-retrieve-entry (entry-key db)
814 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
815 `(gethash ,entry-key (edb-database ,db)))
817 (defun ebib-get-field-highlighted (field current-entry &optional match-str)
818 ;; note: we need to work on a copy of the string, otherwise the highlights
819 ;; are made to the string as stored in the database. hence copy-sequence.
820 (let ((case-fold-search t)
821 (string (copy-sequence (gethash field current-entry)))
822 (raw " ")
823 (multiline " ")
824 (matched nil))
825 ;; we have to do a couple of things now:
826 ;; - remove {} or "" around the string, if they're there
827 ;; - search for match-str
828 ;; - properly adjust the string if it's multiline
829 ;; but all this is not necessary if there was no string
830 (if (null string)
831 (let* ((xref (to-raw (gethash 'crossref current-entry)))
832 (xref-entry (ebib-retrieve-entry xref ebib-cur-db)))
833 (when xref-entry
834 (setq string (gethash field xref-entry))
835 (if string
836 (setq string (propertize (to-raw string) 'face 'ebib-crossref-face 'fontified t))
837 (setq string ""))))
838 (if (raw-p string)
839 (setq raw "*")
840 (setq string (to-raw string))) ; we have to make the string look nice
841 (when match-str
842 (multiple-value-setq (string matched) (match-all match-str string)))
843 (when (multiline-p string)
844 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
845 ;; multiline is local and therefore the object it refers to should
846 ;; be GC'ed when the function returns. but for some reason, the
847 ;; plus sign is persistent, and if it's been highlighted as the
848 ;; result of a search, it stays that way.
849 (setq multiline (propertize "+" 'face nil))
850 (setq string (first-line string))))
851 (when (and matched
852 (string= multiline "+"))
853 (add-text-properties 0 1 '(face highlight) multiline))
854 (concat raw multiline string)))
856 (defun ebib-format-fields (entry fn &optional match-str)
857 (let* ((entry-type (gethash 'type* entry))
858 (obl-fields (ebib-get-obl-fields entry-type))
859 (opt-fields (ebib-get-opt-fields entry-type)))
860 (funcall fn (format "%-19s %s\n" "type" entry-type))
861 (mapc #'(lambda (fields)
862 (funcall fn "\n")
863 (mapcar #'(lambda (field)
864 (unless (and (get field 'ebib-hidden)
865 ebib-hide-hidden-fields)
866 (funcall fn (format "%-17s " field))
867 (funcall fn (or
868 (ebib-get-field-highlighted field entry match-str)
869 ""))
870 (funcall fn "\n")))
871 fields))
872 (list obl-fields opt-fields ebib-additional-fields))))
874 (defun ebib-fill-entry-buffer (&optional match-str)
875 "Fills the entry buffer with the fields of the current entry.
877 MATCH-STRING is a regexp that will be highlighted when it occurs in the
878 field contents."
879 (set-buffer ebib-entry-buffer)
880 (with-buffer-writable
881 (erase-buffer)
882 (when (and ebib-cur-db ; do we have a database?
883 (edb-keys-list ebib-cur-db) ; does it contain entries?
884 (gethash (car (edb-cur-entry ebib-cur-db))
885 (edb-database ebib-cur-db))) ; does the current entry exist?
886 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db))
887 (edb-database ebib-cur-db)) 'insert match-str)
888 (setq ebib-current-field 'type*)
889 (goto-char (point-min))
890 (ebib-set-fields-highlight))))
892 (defun ebib-set-modified (mod &optional db)
893 "Sets the modified flag of the database DB to MOD.
895 If DB is nil, it defaults to the current database, and the modified flag of
896 the index buffer is also (re)set. MOD must be either T or NIL."
897 (unless db
898 (setq db ebib-cur-db))
899 (setf (edb-modified db) mod)
900 (when (eq db ebib-cur-db)
901 (save-excursion
902 (set-buffer ebib-index-buffer)
903 (set-buffer-modified-p mod))))
905 (defun ebib-modified-p ()
906 "Checks if any of the databases in Ebib were modified.
908 Returns the first modified database, or NIL if none was modified."
909 (let ((db (car ebib-databases)))
910 (while (and db
911 (not (edb-modified db)))
912 (setq db (next-elem db ebib-databases)))
913 db))
915 (defun ebib-create-new-database (&optional db)
916 "Creates a new database instance and returns it.
918 If DB is set to a database, the new database is a copy of DB."
919 (let ((new-db
920 (if (edb-p db)
921 (copy-edb db)
922 (make-edb))))
923 (setq ebib-databases (append ebib-databases (list new-db)))
924 new-db))
926 ;; the next two functions are used in loading the database. a database is
927 ;; loaded by reading the raw file into a temp buffer and then reading all
928 ;; the entries in it. for this we need to be able to search a matching
929 ;; parenthesis and a matching double quote.
931 (defun ebib-match-paren-forward (limit)
932 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
934 Does not search/move beyond LIMIT. Returns T if a matching parenthesis was
935 found, NIL otherwise. If point was not at an opening parenthesis at all,
936 NIL is returned and point is not moved. If point was at an opening
937 parenthesis but no matching closing parenthesis was found, point is moved
938 to LIMIT."
939 (if (nor (eq (char-after) ?\{)
940 (eq (char-after) ?\"))
942 (save-restriction
943 (narrow-to-region (point-min) limit)
944 (condition-case nil
945 (progn
946 (forward-list)
947 ;; all of ebib expects that point moves to the closing
948 ;; parenthesis, not right after it, so we adjust.
949 (forward-char -1)
950 t) ; return t because a matching brace was found
951 (error (progn
952 (goto-char (point-max)) ; point-max because the narrowing is still in effect
953 nil))))))
955 (defun ebib-match-quote-forward (limit)
956 "Moves to the closing double quote matching the quote at POINT.
958 Does not search/move beyond LIMIT. Returns T if a matching quote was found,
959 NIL otherwise. If point was not at a double quote at all, NIL is returned
960 and point is not moved. If point was at a quote but no matching closing
961 quote was found, point is moved to LIMIT."
962 (when (eq (char-after (point)) ?\") ; make sure we're on a double quote.
963 (while
964 (progn
965 (forward-char) ; we need to move forward because we're on a double quote.
966 (skip-chars-forward "^\"" limit) ; find the next double quote.
967 (and (eq (char-before) ?\\) ; if it's preceded by a backslash,
968 (< (point) limit)))) ; and we're still below LIMIT, keep on searching.
969 (eq (char-after (point)) ?\"))) ; return T or NIL based on whether we've found a quote.
971 (defun ebib-insert-entry (entry-key fields db &optional sort timestamp)
972 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
974 Optional argument SORT indicates whether the KEYS-LIST must be
975 sorted after insertion. Default is NIL. Optional argument
976 TIMESTAMP indicates whether a timestamp is to be added to the
977 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
978 must also be set to T."
979 (when (and timestamp ebib-use-timestamp)
980 (puthash 'timestamp (from-raw (format-time-string ebib-timestamp-format)) fields))
981 (puthash entry-key fields (edb-database db))
982 (ebib-set-modified t db)
983 (setf (edb-n-entries db) (1+ (edb-n-entries db)))
984 (setf (edb-keys-list db)
985 (if sort
986 (sort (cons entry-key (edb-keys-list db)) 'string<)
987 (cons entry-key (edb-keys-list db)))))
989 (defun ebib-insert-string (abbr string db &optional sort)
990 "Stores the @STRING definition defined by ABBR and STRING into DB.
992 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
993 after insertion. When loading or merging a file, for example, it is more
994 economic to sort KEYS-LIST manually after all entries in the file have been
995 added."
996 (puthash abbr (from-raw string) (edb-strings db))
997 (ebib-set-modified t db)
998 (setf (edb-strings-list db)
999 (if sort
1000 (sort (cons abbr (edb-strings-list db)) 'string<)
1001 (cons abbr (edb-strings-list db)))))
1003 (defmacro ebib-cur-entry-key ()
1004 "Returns the key of the current entry in EBIB-CUR-DB."
1005 `(car (edb-cur-entry ebib-cur-db)))
1007 (defun ebib-search-key-in-buffer (entry-key)
1008 "Searches ENTRY-KEY in the index buffer.
1010 Moves point to the first character of the key and returns point."
1011 (goto-char (point-min))
1012 (search-forward entry-key)
1013 (beginning-of-line)
1014 (point))
1016 ;; when we sort entries, we either use string< on the entry keys, or
1017 ;; ebib-entry<, if the user has defined a sort order.
1019 (defun ebib-entry< (x y)
1020 "Returns T if entry X is smaller than entry Y.
1022 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1023 and Y should be keys of entries in the current database."
1024 (let* ((sort-list ebib-sort-order)
1025 (sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1026 (sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1027 (while (and sort-list
1028 (string= sortstring-x sortstring-y))
1029 (setq sort-list (cdr sort-list))
1030 (setq sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1031 (setq sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1032 (if (and sortstring-x sortstring-y)
1033 (string< sortstring-x sortstring-y)
1034 (string< x y))))
1036 (defun ebib-get-sortstring (entry-key sortkey-list)
1037 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1039 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1040 is a list of fields that are considered in order for the sort value."
1041 (let ((sort-string nil))
1042 (while (and sortkey-list
1043 (null (setq sort-string (gethash (car sortkey-list)
1044 (ebib-retrieve-entry entry-key ebib-cur-db)))))
1045 (setq sortkey-list (cdr sortkey-list)))
1046 sort-string))
1048 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1049 ;; main program execution ;;
1050 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1052 (defun ebib ()
1053 "Ebib, a BibTeX database manager."
1054 (interactive)
1055 (if (or (equal (window-buffer) ebib-index-buffer)
1056 (equal (window-buffer) ebib-entry-buffer))
1057 (error "Ebib already active")
1058 (unless ebib-initialized
1059 (ebib-init)
1060 (if ebib-preload-bib-files
1061 (mapc #'(lambda (file)
1062 (ebib-load-bibtex-file file))
1063 ebib-preload-bib-files)))
1064 ;; we save the current window configuration.
1065 (setq ebib-saved-window-config (current-window-configuration))
1066 ;; create the window configuration we want for ebib.
1067 (delete-other-windows)
1068 (switch-to-buffer ebib-index-buffer)
1069 (let* ((keys-window (selected-window))
1070 (entry-window (split-window keys-window ebib-index-window-size)))
1071 (set-window-buffer entry-window ebib-entry-buffer))))
1073 (defun ebib-init ()
1074 "Initialises Ebib.
1076 This function sets all variables to their initial values, creates the
1077 buffers and reads the rc file."
1078 (setq ebib-cur-entry-hash nil
1079 ebib-current-field nil
1080 ebib-minibuf-hist nil
1081 ebib-saved-window-config nil)
1082 (put 'timestamp 'ebib-hidden t)
1083 (when (file-readable-p "~/.ebibrc")
1084 (load "~/.ebibrc"))
1085 (ebib-create-buffers)
1086 (setq ebib-index-highlight (ebib-make-highlight 1 1 ebib-index-buffer))
1087 (setq ebib-fields-highlight (ebib-make-highlight 1 1 ebib-entry-buffer))
1088 (setq ebib-strings-highlight (ebib-make-highlight 1 1 ebib-strings-buffer))
1089 (setq ebib-initialized t))
1091 (defun ebib-create-buffers ()
1092 "Creates the buffers for Ebib."
1093 ;; first we create a buffer for multiline editing. this one does *not*
1094 ;; have a name beginning with a space, because undo-info is normally
1095 ;; present in an edit buffer.
1096 (setq ebib-multiline-buffer (get-buffer-create "*Ebib-edit*"))
1097 (set-buffer ebib-multiline-buffer)
1098 (ebib-multiline-edit-mode)
1099 ;; then we create a buffer to hold the fields of the current entry.
1100 (setq ebib-entry-buffer (get-buffer-create " *Ebib-entry*"))
1101 (set-buffer ebib-entry-buffer)
1102 (ebib-entry-mode)
1103 ;; then we create a buffer to hold the @STRING definitions
1104 (setq ebib-strings-buffer (get-buffer-create " *Ebib-strings*"))
1105 (set-buffer ebib-strings-buffer)
1106 (ebib-strings-mode)
1107 ;; then we create the help buffer
1108 (setq ebib-help-buffer (get-buffer-create " *Ebib-help*"))
1109 (set-buffer ebib-help-buffer)
1110 (ebib-help-mode)
1111 ;; and lastly we create a buffer for the entry keys.
1112 (setq ebib-index-buffer (get-buffer-create " none"))
1113 (set-buffer ebib-index-buffer)
1114 (ebib-index-mode))
1116 (defun ebib-quit ()
1117 "Quits Ebib.
1119 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1120 (interactive)
1121 (when (if (ebib-modified-p)
1122 (yes-or-no-p "There are modified databases. Quit anyway? ")
1123 (y-or-n-p "Quit Ebib? "))
1124 (kill-buffer ebib-entry-buffer)
1125 (kill-buffer ebib-index-buffer)
1126 (kill-buffer ebib-multiline-buffer)
1127 (setq ebib-databases nil
1128 ebib-index-buffer nil
1129 ebib-entry-buffer nil
1130 ebib-initialized nil
1131 ebib-index-highlight nil
1132 ebib-fields-highlight nil
1133 ebib-strings-highlight nil
1134 ebib-export-filename nil)
1135 (set-window-configuration ebib-saved-window-config)
1136 (message "")))
1138 (defun ebib-kill-emacs-query-function ()
1139 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1140 killed and the database has been modified."
1141 (if (not (ebib-modified-p))
1143 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1144 (progn
1145 (ebib-save-all-databases)
1147 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1149 (add-hook 'kill-emacs-query-functions 'ebib-kill-emacs-query-function)
1151 ;;;;;;;;;;;;;;;;
1152 ;; index-mode ;;
1153 ;;;;;;;;;;;;;;;;
1155 (eval-and-compile
1156 (define-prefix-command 'ebib-prefix-map)
1157 (suppress-keymap ebib-prefix-map)
1158 (defvar ebib-prefixed-functions '(ebib-delete-entry
1159 ebib-latex-entries
1160 ebib-mark-entry
1161 ebib-print-entries
1162 ebib-export-entry
1163 ebib-push-entry-key)))
1165 ;; macro to redefine key bindings.
1167 (defmacro ebib-key (buffer key &optional command)
1168 (cond
1169 ((eq buffer 'index)
1170 (let ((one `(define-key ebib-index-mode-map ,key (quote ,command)))
1171 (two (when (or (null command)
1172 (member command ebib-prefixed-functions))
1173 `(define-key ebib-prefix-map ,key (quote ,command)))))
1174 (if two
1175 `(progn ,one ,two)
1176 one)))
1177 ((eq buffer 'entry)
1178 `(define-key ebib-entry-mode-map ,key (quote ,command)))
1179 ((eq buffer 'strings)
1180 `(define-key ebib-strings-mode-map ,key (quote ,command)))
1181 ((eq buffer 'mark-prefix)
1182 `(progn
1183 (define-key ebib-index-mode-map (format "%c" ebib-prefix-key) nil)
1184 (define-key ebib-index-mode-map ,key 'ebib-prefix-map)
1185 (setq ebib-prefix-key (string-to-char ,key))))))
1187 (defvar ebib-index-mode-map
1188 (let ((map (make-keymap)))
1189 (suppress-keymap map)
1190 map)
1191 "Keymap for the ebib index buffer.")
1193 ;; we define the keys with ebib-key rather than with define-key, because
1194 ;; that automatically sets up ebib-prefix-map as well.
1195 (ebib-key index [up] ebib-prev-entry)
1196 (ebib-key index [down] ebib-next-entry)
1197 (ebib-key index [right] ebib-next-database)
1198 (ebib-key index [left] ebib-prev-database)
1199 (ebib-key index [prior] ebib-index-scroll-down)
1200 (ebib-key index [next] ebib-index-scroll-up)
1201 (ebib-key index [home] ebib-goto-first-entry)
1202 (ebib-key index [end] ebib-goto-last-entry)
1203 (ebib-key index [return] ebib-select-entry)
1204 (ebib-key index " " ebib-index-scroll-up)
1205 (ebib-key index "/" ebib-search)
1206 (ebib-key index "&" ebib-virtual-db-and)
1207 (ebib-key index "|" ebib-virtual-db-or)
1208 (ebib-key index "~" ebib-virtual-db-not)
1209 (ebib-key index ";" ebib-prefix-map)
1210 (ebib-key index "a" ebib-add-entry)
1211 (ebib-key index "b" ebib-index-scroll-down)
1212 (ebib-key index "c" ebib-close-database)
1213 (ebib-key index "C" ebib-customize)
1214 (ebib-key index "d" ebib-delete-entry)
1215 (ebib-key index "e" ebib-edit-entry)
1216 (ebib-key index "E" ebib-edit-keyname)
1217 (ebib-key index "f" ebib-print-filename)
1218 (ebib-key index "F" ebib-follow-crossref)
1219 (ebib-key index "g" ebib-goto-first-entry)
1220 (ebib-key index "G" ebib-goto-last-entry)
1221 (ebib-key index "h" ebib-index-help)
1222 (ebib-key index "H" ebib-toggle-hidden)
1223 (ebib-key index "j" ebib-next-entry)
1224 (ebib-key index "J" ebib-switch-to-database)
1225 (ebib-key index "k" ebib-prev-entry)
1226 (ebib-key index "L" ebib-latex-entries)
1227 (ebib-key index "m" ebib-mark-entry)
1228 (ebib-key index "M" ebib-merge-bibtex-file)
1229 (ebib-key index "n" ebib-search-next)
1230 (ebib-key index [(control n)] ebib-next-entry)
1231 (ebib-key index [(meta n)] ebib-index-scroll-up)
1232 (ebib-key index "o" ebib-load-bibtex-file)
1233 (ebib-key index "p" ebib-push-entry-key)
1234 (ebib-key index [(control p)] ebib-prev-entry)
1235 (ebib-key index [(meta p)] ebib-index-scroll-down)
1236 (ebib-key index "P" ebib-print-entries)
1237 (ebib-key index "r" ebib-edit-preamble)
1238 (ebib-key index "q" ebib-quit)
1239 (ebib-key index "s" ebib-save-current-database)
1240 (ebib-key index "S" ebib-save-all-databases)
1241 (ebib-key index "t" ebib-edit-strings)
1242 (ebib-key index "u" ebib-browse-url)
1243 (ebib-key index "V" ebib-print-filter)
1244 (ebib-key index "w" ebib-write-database)
1245 (ebib-key index "x" ebib-export-entry)
1246 (ebib-key index "\C-xb" ebib-lower)
1247 (ebib-key index "\C-xk" ebib-quit)
1248 (ebib-key index "X" ebib-export-preamble)
1249 (ebib-key index "z" ebib-lower)
1251 (defun ebib-switch-to-database-nth (key)
1252 (interactive (list (if (featurep 'xemacs)
1253 (event-key last-command-event)
1254 last-command-event)))
1255 (ebib-switch-to-database (- (if (featurep 'xemacs)
1256 (char-to-int key)
1257 key) 48)))
1259 (mapc #'(lambda (key)
1260 (define-key ebib-index-mode-map (format "%d" key)
1261 'ebib-switch-to-database-nth))
1262 '(1 2 3 4 5 6 7 8 9))
1264 (define-derived-mode ebib-index-mode
1265 fundamental-mode "Ebib-index"
1266 "Major mode for the Ebib index buffer."
1267 (setq buffer-read-only t))
1269 (defun ebib-fill-index-buffer ()
1270 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1272 If EBIB-CUR-DB is nil, the buffer is just erased and its name set to \"none\"."
1273 (set-buffer ebib-index-buffer)
1274 (let ((buffer-read-only nil))
1275 (erase-buffer)
1276 (if ebib-cur-db
1277 (progn
1278 ;; we may call this function when there are no entries in the
1279 ;; database. if so, we don't need to do this:
1280 (when (edb-cur-entry ebib-cur-db)
1281 (mapcar #'(lambda (entry)
1282 (insert entry)
1283 (when (member entry (edb-marked-entries ebib-cur-db))
1284 (let ((beg (save-excursion
1285 (beginning-of-line)
1286 (point))))
1287 (add-text-properties beg (point) `(face ,ebib-marked-face))))
1288 (insert "\n"))
1289 (edb-keys-list ebib-cur-db))
1290 (goto-char (point-min))
1291 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
1292 (beginning-of-line)
1293 (ebib-set-index-highlight))
1294 (set-buffer-modified-p (edb-modified ebib-cur-db))
1295 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1296 (length (member ebib-cur-db ebib-databases)))))
1297 (edb-name ebib-cur-db))))
1298 (rename-buffer " none"))))
1300 (defun ebib-customize ()
1301 (interactive)
1302 (ebib-lower)
1303 (customize-group 'ebib))
1305 (defun ebib-load-bibtex-file (&optional file)
1306 "Loads a BibTeX file into ebib."
1307 (interactive)
1308 (unless file
1309 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1310 (setq ebib-cur-db (ebib-create-new-database))
1311 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1312 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1313 ;; first, we empty the buffers
1314 (ebib-erase-buffer ebib-index-buffer)
1315 (ebib-erase-buffer ebib-entry-buffer)
1316 (if (file-readable-p file)
1317 ;; if the user entered the name of an existing file, we load it
1318 ;; by putting it in a buffer and then parsing it.
1319 (with-temp-buffer
1320 (insert-file-contents file)
1321 ;; if the user makes any changes, we'll want to create a back-up.
1322 (setf (edb-make-backup ebib-cur-db) t)
1323 (let ((result (ebib-find-bibtex-entries nil)))
1324 (setf (edb-n-entries ebib-cur-db) (car result))
1325 (when (edb-keys-list ebib-cur-db)
1326 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1327 (when (edb-strings-list ebib-cur-db)
1328 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1329 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1330 ;; and fill the buffers. note that filling a buffer also makes
1331 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1332 ;; later.
1333 (ebib-set-modified nil)
1334 (ebib-fill-entry-buffer)
1335 ;; and now we tell the user the result
1336 (message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1337 (car result)
1338 (cadr result)
1339 (if (caddr result)
1341 "no"))))
1342 ;; if the file does not exist, we need to issue a message.
1343 (message "(New file)"))
1344 ;; what we have to do in *any* case, is fill the index buffer. (this
1345 ;; even works if there are no keys in the database, e.g. when the
1346 ;; user opened a new file or if no BibTeX entries were found.
1347 (ebib-fill-index-buffer))
1349 (defun ebib-merge-bibtex-file ()
1350 "Merges a BibTeX file into the database."
1351 (interactive)
1352 (unless (edb-virtual ebib-cur-db)
1353 (if (not ebib-cur-db)
1354 (error "No database loaded. Use `o' to open a database")
1355 (let ((file (read-file-name "File to merge: ")))
1356 (with-temp-buffer
1357 (insert-file-contents file)
1358 (let ((n (ebib-find-bibtex-entries t)))
1359 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1360 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1361 (when (edb-strings-list ebib-cur-db)
1362 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1363 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1364 (ebib-fill-entry-buffer)
1365 (ebib-fill-index-buffer)
1366 (ebib-set-modified t)
1367 (message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1368 (car n)
1369 (cadr n)
1370 (if (caddr n)
1372 "no"))))))))
1374 (defun ebib-find-bibtex-entries (timestamp)
1375 "Finds the BibTeX entries in the current buffer.
1377 The search is started at the beginnig of the buffer. All entries found are
1378 stored in the hash table DATABASE of EBIB-CUR-DB. Returns a three-element
1379 list: the first element is the number of entries found, the second the
1380 number of @STRING definitions, and the third is T or NIL, indicating
1381 whether a @PREAMBLE was found.
1383 TIMESTAMP indicates whether a timestamp is to be added to each
1384 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1385 is set to T."
1386 (modify-syntax-entry ?\" "w")
1387 (let ((n-entries 0)
1388 (n-strings 0)
1389 (preamble nil))
1390 (goto-char (point-min))
1391 (while (re-search-forward "^@" nil t) ; find the next entry
1392 (let ((beg (point)))
1393 (when (looking-at-goto-end (concat ebib-bibtex-identifier "[\(\{]"))
1394 (let ((entry-type (downcase (buffer-substring-no-properties beg (1- (point))))))
1395 (cond
1396 ((equal entry-type "string") ; string and preamble must be treated differently
1397 (if (ebib-read-string)
1398 (setq n-strings (1+ n-strings))))
1399 ((equal entry-type "preamble") (ebib-read-preamble)
1400 (setq preamble t))
1401 ((equal entry-type "comment") (ebib-match-paren-forward (point-max))) ; ignore comments
1402 ((gethash (intern-soft entry-type) ebib-entry-types-hash) ; if the entry type has been defined
1403 (if (ebib-read-entry entry-type timestamp)
1404 (setq n-entries (1+ n-entries))))
1405 (t (message "Unknown entry type `%s'. Skipping." entry-type) ; we found something we don't know
1406 (ebib-match-paren-forward (point-max))))))))
1407 (list n-entries n-strings preamble)))
1409 (defun ebib-read-string ()
1410 "Reads the @STRING definition beginning at the line POINT is on.
1412 If a proper abbreviation and string are found, they are stored in the
1413 database. Returns the string if one was read, nil otherwise."
1414 (let ((limit (save-excursion ; we find the matching end parenthesis
1415 (backward-char)
1416 (ebib-match-paren-forward (point-max))
1417 (point))))
1418 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1419 (let ((beg (point)))
1420 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1421 (if-str (abbr (buffer-substring-no-properties beg (point)))
1422 (progn
1423 (skip-chars-forward "^\"{" limit)
1424 (let ((beg (point)))
1425 (if-str (string (cond
1426 ((eq (char-after) ?\" ) (if (ebib-match-quote-forward limit)
1427 (buffer-substring-no-properties beg (1+ (point)))
1428 nil))
1429 ((eq (char-after) ?\{ ) (if (ebib-match-paren-forward limit)
1430 (buffer-substring-no-properties beg (1+ (point)))
1431 nil))
1432 (t nil)))
1433 (if (member abbr (edb-strings-list ebib-cur-db))
1434 (message (format "@STRING definition `%s' duplicated" abbr))
1435 (ebib-insert-string abbr string ebib-cur-db))))))))))
1437 (defun ebib-read-preamble ()
1438 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1440 If there was already another @PREAMBLE definition, the new one is added to
1441 the existing one with a hash sign `#' between them."
1442 (let ((beg (point)))
1443 (forward-char -1)
1444 (ebib-match-paren-forward (point-max))
1445 (let ((text (buffer-substring-no-properties beg (point))))
1446 (if (edb-preamble ebib-cur-db)
1447 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1448 (setf (edb-preamble ebib-cur-db) text)))))
1450 (defun ebib-read-entry (entry-type &optional timestamp)
1451 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1453 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1454 otherwise. Optional argument TIMESTAMP indicates whether a
1455 timestamp is to be added. (Whether a timestamp is actually added,
1456 also depends on EBIB-USE-TIMESTAMP.)"
1457 (let ((entry-limit (save-excursion
1458 (backward-char)
1459 (ebib-match-paren-forward (point-max))
1460 (point)))
1461 (beg (progn
1462 (skip-chars-forward " \n\t\f") ; note the space!
1463 (point))))
1464 (when (looking-at-goto-end (concat "\\("
1465 ebib-bibtex-identifier
1466 "\\)[ \t\n\f]*,")
1467 1) ; this delimits the entry key
1468 (let ((entry-key (buffer-substring-no-properties beg (point))))
1469 (if (member entry-key (edb-keys-list ebib-cur-db))
1470 (message "Entry `%s' duplicated " entry-key)
1471 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1472 (when fields ; if fields were found, we store them, and return T.
1473 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1474 t)))))))
1476 (defun ebib-find-bibtex-fields (entry-type limit)
1477 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1479 Returns a hash table containing all the fields and values, or NIL if none
1480 were found. ENTRY-TYPE is the type of the entry, which will be recorded in
1481 the hash table. Before the search starts, POINT is moved back to the
1482 beginning of the line."
1483 (beginning-of-line)
1484 ;; we assign a function to fn in order to avoid putting the test on
1485 ;; ebib-allow-identical-fields in the while loop, where it would get
1486 ;; tested with every field being read.
1487 (let ((fn (if (not ebib-allow-identical-fields)
1488 (symbol-function 'puthash)
1489 #'(lambda (field-type field-contents fields)
1490 (let ((existing-contents (gethash field-type fields)))
1491 (puthash field-type (if existing-contents
1492 (from-raw (concat (to-raw existing-contents)
1493 "; "
1494 (to-raw field-contents)))
1495 field-contents)
1496 fields))))))
1497 (let ((fields (make-hash-table :size 15)))
1498 (while (progn
1499 (skip-chars-forward "^," limit) ; we must move to the next comma,
1500 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1501 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1502 (let ((beg (point)))
1503 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1504 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1505 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1506 (let ((field-contents (ebib-get-field-contents limit)))
1507 (when field-contents
1508 (funcall fn field-type field-contents fields))))))))
1509 (when (> (hash-table-count fields) 0)
1510 (puthash 'type* entry-type fields)
1511 fields))))
1513 (defun ebib-get-field-contents (limit)
1514 "Gets the contents of a BibTeX field.
1516 LIMIT indicates the end of the entry, beyond which the function will not
1517 search."
1518 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1519 (let ((beg (point)))
1520 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1522 (defun ebib-find-end-of-field (limit)
1523 "Moves POINT to the end of a field's contents and returns POINT.
1525 The contents of a field is delimited by a comma or by the closing brace of
1526 the entry. The latter is at position LIMIT."
1527 (while (and (not (eq (char-after) ?\,))
1528 (< (point) limit))
1529 (cond
1530 ((eq (char-after) ?\{) (ebib-match-paren-forward limit))
1531 ((eq (char-after) ?\") (ebib-match-quote-forward limit)))
1532 (forward-char 1))
1533 (if (= (point) limit)
1534 (skip-chars-backward " \n\t\f"))
1535 (point))
1537 (defun ebib-lower ()
1538 "Hides the Ebib buffers, but does not delete them."
1539 (interactive)
1540 (if (nor (equal (window-buffer) ebib-index-buffer)
1541 (equal (window-buffer) ebib-entry-buffer)
1542 (equal (window-buffer) ebib-strings-buffer)
1543 (equal (window-buffer) ebib-multiline-buffer)
1544 (equal (window-buffer) ebib-help-buffer))
1545 (error "Ebib is not active ")
1546 (set-window-configuration ebib-saved-window-config)
1547 (bury-buffer ebib-entry-buffer)
1548 (bury-buffer ebib-index-buffer)
1549 (bury-buffer ebib-multiline-buffer)
1550 (bury-buffer ebib-strings-buffer)
1551 (bury-buffer ebib-help-buffer)))
1553 (defun ebib-prev-entry ()
1554 "Moves to the previous BibTeX entry."
1555 (interactive)
1556 (ebib-execute-when
1557 ((entries)
1558 ;; if the current entry is the first entry,
1559 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1560 (beep) ; just beep.
1561 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1562 (1+ (length (edb-cur-entry ebib-cur-db)))))
1563 (goto-char (ebib-highlight-start ebib-index-highlight))
1564 (forward-line -1)
1565 (ebib-set-index-highlight)
1566 (ebib-fill-entry-buffer)))
1567 ((default)
1568 (beep))))
1570 (defun ebib-next-entry ()
1571 "Moves to the next BibTeX entry."
1572 (interactive)
1573 (ebib-execute-when
1574 ((entries)
1575 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1576 (beep) ; just beep.
1577 (setf (edb-cur-entry ebib-cur-db)
1578 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1579 (goto-char (ebib-highlight-start ebib-index-highlight))
1580 (forward-line 1)
1581 (ebib-set-index-highlight)
1582 (ebib-fill-entry-buffer)))
1583 ((default)
1584 (beep))))
1586 (defun ebib-add-entry ()
1587 "Adds a new entry to the database."
1588 (interactive)
1589 (ebib-execute-when
1590 ((real-db)
1591 (if-str (entry-key (read-string "New entry key: "))
1592 (progn
1593 (if (member entry-key (edb-keys-list ebib-cur-db))
1594 (error "Key already exists")
1595 (set-buffer ebib-index-buffer)
1596 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1597 (with-buffer-writable
1598 (insert (format "%s\n" entry-key))) ; add the entry in the buffer.
1599 (forward-line -1) ; move one line up to position the cursor on the new entry.
1600 (ebib-set-index-highlight)
1601 (let ((fields (make-hash-table)))
1602 (puthash 'type* ebib-default-type fields)
1603 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1604 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1605 (ebib-fill-entry-buffer)
1606 (ebib-edit-entry)
1607 (ebib-set-modified t)))))
1608 ((no-database)
1609 (error "No database open. Use `o' to open a database first"))
1610 ((default)
1611 (beep))))
1613 (defun ebib-close-database ()
1614 "Closes the current BibTeX database."
1615 (interactive)
1616 (ebib-execute-when
1617 ((database)
1618 (when (if (edb-modified ebib-cur-db)
1619 (yes-or-no-p "Database modified. Close it anyway? ")
1620 (y-or-n-p "Close database? "))
1621 (let ((to-be-deleted ebib-cur-db)
1622 (new-db (next-elem ebib-cur-db ebib-databases)))
1623 (setq ebib-databases (delete to-be-deleted ebib-databases))
1624 (if ebib-databases ; do we still have another database loaded?
1625 (progn
1626 (setq ebib-cur-db (or new-db
1627 (last1 ebib-databases)))
1628 (unless (edb-cur-entry ebib-cur-db)
1629 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1630 (ebib-fill-entry-buffer)
1631 (ebib-fill-index-buffer))
1632 ;; otherwise, we have to clean up a little and empty all the buffers.
1633 (setq ebib-cur-db nil)
1634 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1635 (set-buffer (car buf))
1636 (with-buffer-writable
1637 (erase-buffer))
1638 (ebib-delete-highlight (cadr buf)))
1639 (list (list ebib-entry-buffer ebib-fields-highlight)
1640 (list ebib-index-buffer ebib-index-highlight)
1641 (list ebib-strings-buffer ebib-strings-highlight)))
1642 ;; multiline edit buffer
1643 (set-buffer ebib-multiline-buffer)
1644 (with-buffer-writable
1645 (erase-buffer))
1646 (set-buffer ebib-index-buffer)
1647 (rename-buffer " none"))
1648 (message "Database closed."))))))
1650 (defun ebib-goto-first-entry ()
1651 "Moves to the first BibTeX entry in the database."
1652 (interactive)
1653 (ebib-execute-when
1654 ((entries)
1655 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1656 (set-buffer ebib-index-buffer)
1657 (goto-char (point-min))
1658 (ebib-set-index-highlight)
1659 (ebib-fill-entry-buffer))
1660 ((default)
1661 (beep))))
1663 (defun ebib-goto-last-entry ()
1664 "Moves to the last entry in the BibTeX database."
1665 (interactive)
1666 (ebib-execute-when
1667 ((entries)
1668 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
1669 (set-buffer ebib-index-buffer)
1670 (goto-line (edb-n-entries ebib-cur-db))
1671 (ebib-set-index-highlight)
1672 (ebib-fill-entry-buffer))
1673 ((default)
1674 (beep))))
1676 (defun ebib-edit-entry ()
1677 "Edits the current BibTeX entry."
1678 (interactive)
1679 (ebib-execute-when
1680 ((real-db entries)
1681 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1682 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
1683 (other-window 1)
1684 (switch-to-buffer ebib-entry-buffer)
1685 (goto-char (ebib-highlight-end ebib-fields-highlight)))
1686 ((default)
1687 (beep))))
1689 (defun ebib-edit-keyname ()
1690 "Change the key of a BibTeX entry."
1691 (interactive)
1692 (ebib-execute-when
1693 ((real-db entries)
1694 (let ((cur-keyname (ebib-cur-entry-key)))
1695 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
1696 cur-keyname))
1697 (if (member new-keyname (edb-keys-list ebib-cur-db))
1698 (error (format "Key `%s' already exists" new-keyname))
1699 (unless (string= cur-keyname new-keyname)
1700 (let ((fields (ebib-retrieve-entry cur-keyname ebib-cur-db))
1701 (marked (member cur-keyname (edb-marked-entries ebib-cur-db))))
1702 (ebib-remove-entry-from-db cur-keyname ebib-cur-db)
1703 (ebib-remove-key-from-buffer cur-keyname)
1704 (ebib-insert-entry new-keyname fields ebib-cur-db t nil)
1705 (setf (edb-cur-entry ebib-cur-db) (member new-keyname (edb-keys-list ebib-cur-db)))
1706 (sort-in-buffer (edb-n-entries ebib-cur-db) new-keyname)
1707 (with-buffer-writable
1708 (insert (format "%s\n" new-keyname))) ; add the entry in the buffer.
1709 (forward-line -1) ; move one line up to position the cursor on the new entry.
1710 (ebib-set-index-highlight)
1711 (ebib-set-modified t)
1712 (when marked (ebib-mark-entry))))))))
1713 ((default)
1714 (beep))))
1716 (defun ebib-mark-entry ()
1717 "Mark (or unmark) the current entry."
1718 (interactive)
1719 (if (ebib-called-with-prefix)
1720 (ebib-execute-when
1721 ((marked-entries)
1722 (setf (edb-marked-entries ebib-cur-db) nil)
1723 (ebib-fill-index-buffer))
1724 ((entries)
1725 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
1726 (ebib-fill-index-buffer))
1727 ((default)
1728 (beep)))
1729 (ebib-execute-when
1730 ((entries)
1731 (set-buffer ebib-index-buffer)
1732 (with-buffer-writable
1733 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
1734 (progn
1735 (setf (edb-marked-entries ebib-cur-db)
1736 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
1737 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
1738 (ebib-highlight-end ebib-index-highlight)
1739 '(face ,ebib-marked-face)))
1740 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
1741 (edb-marked-entries ebib-cur-db))
1742 'string<))
1743 (add-text-properties (ebib-highlight-start ebib-index-highlight)
1744 (ebib-highlight-end ebib-index-highlight)
1745 `(face ,ebib-marked-face)))))
1746 ((default)
1747 (beep)))))
1749 (defun ebib-index-scroll-down ()
1750 "Move one page up in the database."
1751 (interactive)
1752 (ebib-execute-when
1753 ((entries)
1754 (scroll-down)
1755 (ebib-select-entry))
1756 ((default)
1757 (beep))))
1759 (defun ebib-index-scroll-up ()
1760 "Move one page down in the database."
1761 (interactive)
1762 (ebib-execute-when
1763 ((entries)
1764 (scroll-up)
1765 (ebib-select-entry))
1766 ((default)
1767 (beep))))
1769 (defun ebib-format-entry (key db timestamp)
1770 "Format entry KEY from database DB into the current buffer in BibTeX format.
1772 If TIMESTAMP is T, a timestamp is added to the entry if EBIB-USE-TIMESTAMP is T."
1773 (let ((entry (ebib-retrieve-entry key db)))
1774 (when entry
1775 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
1776 (maphash #'(lambda (key value)
1777 (unless (or (eq key 'type*)
1778 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
1779 (insert (format "\t%s = %s,\n" key value))))
1780 entry)
1781 (if (and timestamp ebib-use-timestamp)
1782 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
1783 (delete-char -2)) ; the final ",\n" must be deleted
1784 (insert "\n}\n\n"))))
1786 (defun ebib-format-strings (db)
1787 "Format the @STRING commands in database DB."
1788 (maphash #'(lambda (key value)
1789 (insert (format "@STRING{%s = %s}\n" key value)))
1790 (edb-strings db))
1791 (insert "\n"))
1793 (defun ebib-compare-xrefs (x y)
1794 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
1796 (defun ebib-format-database (db)
1797 "Writes database DB into the current buffer in BibTeX format."
1798 (when (edb-preamble db)
1799 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
1800 (ebib-format-strings db)
1801 (let ((sorted-list (copy-list (edb-keys-list db))))
1802 (cond
1803 (ebib-save-xrefs-first
1804 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
1805 (ebib-sort-order
1806 (setq sorted-list (sort sorted-list 'ebib-entry<))))
1807 (mapc #'(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
1809 (defun ebib-save-database (db)
1810 "Saves the database DB."
1811 (ebib-execute-when
1812 ((real-db)
1813 (when (and (edb-make-backup db)
1814 (file-exists-p (edb-filename db)))
1815 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
1816 (setf (edb-make-backup db) nil))
1817 (with-temp-buffer
1818 (ebib-format-database db)
1819 (write-region (point-min) (point-max) (edb-filename db)))
1820 (ebib-set-modified nil db))))
1822 (defun ebib-write-database ()
1823 "Writes the current database to a different file.
1825 Can also be used to change a virtual database into a real one."
1826 (interactive)
1827 (ebib-execute-when
1828 ((database)
1829 (if-str (new-filename (read-file-name "Save to file: " "~/"))
1830 (progn
1831 (with-temp-buffer
1832 (ebib-format-database ebib-cur-db)
1833 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
1834 ;; if SAFE-WRITE-REGION was cancelled by the user because he
1835 ;; didn't want to overwrite an already existing file with his
1836 ;; new database, it throws an error, so the next lines will not
1837 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
1838 ;; (EDB-NAME DB).
1839 (setf (edb-filename ebib-cur-db) new-filename)
1840 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
1841 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1842 (length (member ebib-cur-db ebib-databases)))))
1843 (edb-name ebib-cur-db)))
1844 (ebib-execute-when
1845 ((virtual-db)
1846 (setf (edb-virtual ebib-cur-db) nil)
1847 (setf (edb-database ebib-cur-db)
1848 (let ((new-db (make-hash-table :test 'equal)))
1849 (mapc #'(lambda (key)
1850 (let ((entry (gethash key (edb-database ebib-cur-db))))
1851 (when entry
1852 (puthash key (copy-hash-table entry) new-db))))
1853 (edb-keys-list ebib-cur-db))
1854 new-db))))
1855 (ebib-set-modified nil))))
1856 ((default)
1857 (beep))))
1859 (defun ebib-save-current-database ()
1860 "Saves the current database."
1861 (interactive)
1862 (ebib-execute-when
1863 ((real-db)
1864 (if (not (edb-modified ebib-cur-db))
1865 (message "No changes need to be saved.")
1866 (ebib-save-database ebib-cur-db)))
1867 ((virtual-db)
1868 (error "Cannot save a virtual database. Use `w' to write to a file."))))
1870 (defun ebib-save-all-databases ()
1871 "Saves all currently open databases if they were modified."
1872 (interactive)
1873 (ebib-execute-when
1874 ((database)
1875 (mapc #'(lambda (db)
1876 (when (edb-modified db)
1877 (ebib-save-database db)))
1878 ebib-databases)
1879 (message "All databases saved."))))
1881 (defun ebib-print-filename ()
1882 "Displays the filename of the current database in the minibuffer."
1883 (interactive)
1884 (message (edb-filename ebib-cur-db)))
1886 (defun ebib-follow-crossref ()
1887 "Goes to the entry mentioned in the crossref field of the current entry."
1888 (interactive)
1889 (let ((new-cur-entry (to-raw (gethash 'crossref
1890 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
1891 (setf (edb-cur-entry ebib-cur-db)
1892 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
1893 (edb-cur-entry ebib-cur-db))))
1894 (ebib-fill-entry-buffer)
1895 (ebib-fill-index-buffer))
1897 (defun ebib-toggle-hidden ()
1898 (interactive)
1899 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
1900 (ebib-fill-entry-buffer))
1902 (defun ebib-delete-entry ()
1903 "Deletes the current entry from the database."
1904 (interactive)
1905 (if (ebib-called-with-prefix)
1906 (ebib-execute-when
1907 ((real-db marked-entries)
1908 (when (y-or-n-p "Delete all marked entries? ")
1909 (mapc #'(lambda (entry)
1910 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
1911 (edb-marked-entries ebib-cur-db))
1912 (message "Marked entries deleted.")
1913 (ebib-set-modified t)
1914 (ebib-fill-entry-buffer)
1915 (ebib-fill-index-buffer)))
1916 ((default)
1917 (beep)))
1918 (ebib-execute-when
1919 ((real-db entries)
1920 (let ((cur-entry (ebib-cur-entry-key)))
1921 (when (y-or-n-p (format "Delete %s? " cur-entry))
1922 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
1923 (ebib-remove-key-from-buffer cur-entry)
1924 (ebib-fill-entry-buffer)
1925 (ebib-set-modified t)
1926 (message (format "Entry `%s' deleted." cur-entry)))))
1927 ((default)
1928 (beep)))))
1930 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
1931 "Removes ENTRY-KEY from DB.
1933 Optional argument NEW-CUR-ENTRY is the key of the entry that is to become
1934 the new current entry. It it is NIL, the entry after the deleted one
1935 becomes the new current entry. If it is T, the current entry is not
1936 changed."
1937 (remhash entry-key (edb-database db))
1938 (setf (edb-n-entries db) (1- (edb-n-entries db)))
1939 (cond
1940 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
1941 ((stringp new-cur-entry) t)
1942 (t (setq new-cur-entry (ebib-cur-entry-key))))
1943 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
1944 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
1945 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
1946 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
1947 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
1949 (defun ebib-remove-key-from-buffer (entry-key)
1950 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
1951 (with-buffer-writable
1952 (let ((beg (ebib-search-key-in-buffer entry-key)))
1953 (forward-line 1)
1954 (delete-region beg (point))))
1955 (ebib-execute-when
1956 ((entries)
1957 (ebib-search-key-in-buffer (ebib-cur-entry-key))
1958 (ebib-set-index-highlight))))
1960 (defun ebib-select-entry ()
1961 "Makes the entry at (point) the current entry."
1962 (interactive)
1963 (ebib-execute-when
1964 ((entries)
1965 (beginning-of-line)
1966 (let ((beg (point)))
1967 (let* ((key (save-excursion
1968 (end-of-line)
1969 (buffer-substring-no-properties beg (point))))
1970 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
1971 (when new-cur-entry
1972 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
1973 (ebib-set-index-highlight)
1974 (ebib-fill-entry-buffer)))))
1975 ((default)
1976 (beep))))
1978 (defun ebib-export-entry (prefix)
1979 "Copies entries to another database.
1981 The prefix argument indicates which database to copy the entry
1982 to. If no prefix argument is present, a filename is asked to
1983 which the entry is appended."
1984 (interactive "P")
1985 (let ((num (ebib-prefix prefix)))
1986 (if (ebib-called-with-prefix)
1987 (ebib-export-marked-entries num)
1988 (ebib-export-single-entry num))))
1990 (defun ebib-export-single-entry (num)
1991 "Copies the current entry to another database.
1993 NUM indicates which database to copy the entry to. If it is NIL,
1994 a filename is asked to which the entry is appended."
1995 (ebib-execute-when
1996 ((real-db entries)
1997 (if num
1998 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
1999 #'(lambda (db)
2000 (let ((entry-key (ebib-cur-entry-key)))
2001 (if (member entry-key (edb-keys-list db))
2002 (error "Entry key `%s' already exists in database %d" entry-key num)
2003 (ebib-insert-entry entry-key
2004 (copy-hash-table (ebib-retrieve-entry entry-key
2005 ebib-cur-db))
2006 db t t)
2007 ;; if this is the first entry in the target DB,
2008 ;; its CUR-ENTRY must be set!
2009 (when (null (edb-cur-entry db))
2010 (setf (edb-cur-entry db) (edb-keys-list db)))
2011 t)))) ; we must return T, WHEN does not always do this.
2012 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2013 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2014 #'(lambda ()
2015 (insert "\n")
2016 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2017 ((default)
2018 (beep))))
2020 (defun ebib-export-marked-entries (num)
2021 "Copies the marked entries to another database.
2023 NUM indicates which database to copy the entry to. If it is NIL,
2024 a filename is asked to which the entry is appended."
2025 (ebib-execute-when
2026 ((real-db marked-entries)
2027 (if num
2028 (ebib-export-to-db
2029 num "Entries copied to database %d."
2030 #'(lambda (db)
2031 (mapc #'(lambda (entry-key)
2032 (if (member entry-key (edb-keys-list db))
2033 (error "Entry key `%s' already exists in database %d" entry-key num)
2034 (ebib-insert-entry entry-key
2035 (copy-hash-table (ebib-retrieve-entry entry-key
2036 ebib-cur-db))
2037 db t t)))
2038 (edb-marked-entries ebib-cur-db))
2039 ;; if the target DB was empty before, its CUR-ENTRY must be set!
2040 (when (null (edb-cur-entry db))
2041 (setf (edb-cur-entry db) (edb-keys-list db)))
2042 t)) ; we must return T, WHEN does not always do this.
2043 (ebib-export-to-file "Export to file: " "Entries exported to %s."
2044 #'(lambda ()
2045 (mapc #'(lambda (entry-key)
2046 (insert "\n")
2047 (ebib-format-entry entry-key ebib-cur-db t))
2048 (edb-marked-entries ebib-cur-db))))))
2049 ((default)
2050 (beep))))
2052 (defun ebib-search ()
2053 "Search the current Ebib database.
2055 The search is conducted with STRING-MATCH and can therefore be a regexp.
2056 Searching starts with the current entry."
2057 (interactive)
2058 (ebib-execute-when
2059 ((entries)
2060 (if-str (search-str (read-string "Search database for: "))
2061 (progn
2062 (setq ebib-search-string search-str)
2063 ;; first we search the current entry
2064 (if (ebib-search-in-entry ebib-search-string
2065 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2066 (ebib-fill-entry-buffer ebib-search-string)
2067 ;; if the search string wasn't found in the current entry, we continue searching.
2068 (ebib-search-next)))))
2069 ((default)
2070 (beep))))
2072 (defun ebib-search-next ()
2073 "Search the next occurrence of EBIB-SEARCH-STRING.
2075 Searching starts at the entry following the current entry. If a match is
2076 found, the matching entry is shown and becomes the new current entry."
2077 (interactive)
2078 (ebib-execute-when
2079 ((entries)
2080 (if (null ebib-search-string)
2081 (message "No search string")
2082 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2083 (while (and cur-search-entry
2084 (null (ebib-search-in-entry ebib-search-string
2085 (gethash (car cur-search-entry)
2086 (edb-database ebib-cur-db)))))
2087 (setq cur-search-entry (cdr cur-search-entry)))
2088 (if (null cur-search-entry)
2089 (message (format "`%s' not found" ebib-search-string))
2090 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2091 (set-buffer ebib-index-buffer)
2092 (goto-char (point-min))
2093 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
2094 (beginning-of-line)
2095 (ebib-set-index-highlight)
2096 (ebib-fill-entry-buffer ebib-search-string)))))
2097 ((default)
2098 (beep))))
2100 (defun ebib-search-in-entry (search-str entry &optional field)
2101 "Searches one entry of the ebib database.
2103 Returns a list of fields in ENTRY that match the regexp SEARCH-STR,
2104 or NIL if no matches were found. If FIELD is given, only that
2105 field is searched."
2106 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2107 (result nil))
2108 (if field
2109 (let ((value (gethash field entry)))
2110 (when (and (stringp value) ; the type* field has a symbol as value
2111 (string-match search-str value))
2112 (setq result (list field))))
2113 (maphash #'(lambda (field value)
2114 (when (and (stringp value) ; the type* field has a symbol as value
2115 (string-match search-str value))
2116 (setq result (cons field result))))
2117 entry))
2118 result))
2120 (defun ebib-edit-strings ()
2121 "Edits the @STRING definitions in the database."
2122 (interactive)
2123 (ebib-execute-when
2124 ((real-db)
2125 (ebib-fill-strings-buffer)
2126 (other-window 1)
2127 (switch-to-buffer ebib-strings-buffer)
2128 (goto-char (point-min)))
2129 ((default)
2130 (beep))))
2132 (defun ebib-edit-preamble ()
2133 "Edits the @PREAMBLE definition in the database."
2134 (interactive)
2135 (ebib-execute-when
2136 ((real-db)
2137 (other-window 1) ; we want the multiline edit buffer to appear in the lower window
2138 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2139 ((default)
2140 (beep))))
2142 (defun ebib-export-preamble (prefix)
2143 "Export the @PREAMBLE definition.
2145 If a prefix argument is given, it is taken as the database to
2146 export the preamble to. If the goal database already has a
2147 preamble, the new preamble will be appended to it. If no prefix
2148 argument is given, the user is asked to enter a filename to which
2149 the preamble is appended."
2150 (interactive "P")
2151 (ebib-execute-when
2152 ((real-db)
2153 (if (null (edb-preamble ebib-cur-db))
2154 (error "No @PREAMBLE defined")
2155 (let ((num (ebib-prefix prefix)))
2156 (if num
2157 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2158 #'(lambda (db)
2159 (let ((text (edb-preamble ebib-cur-db)))
2160 (if (edb-preamble db)
2161 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2162 (setf (edb-preamble db) text)))))
2163 (ebib-export-to-file "Export @PREAMBLE to file: "
2164 "@PREAMBLE exported to %s"
2165 #'(lambda ()
2166 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2167 ((default)
2168 (beep))))
2170 (defun ebib-print-entries ()
2171 "Creates a LaTeX file listing the entries.
2173 Either prints the entire database, or the marked entries."
2174 (interactive)
2175 (ebib-execute-when
2176 ((entries)
2177 (let ((entries (if (ebib-called-with-prefix)
2178 (edb-marked-entries ebib-cur-db)
2179 (edb-keys-list ebib-cur-db))))
2180 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2181 ebib-print-tempfile
2182 (read-file-name "Use temp file: " "~/" nil nil)))
2183 (progn
2184 (with-temp-buffer
2185 (insert "\\documentclass{article}\n\n")
2186 (when ebib-print-preamble
2187 (mapc #'(lambda (string)
2188 (insert (format "%s\n" string)))
2189 ebib-print-preamble))
2190 (insert "\n\\begin{document}\n\n")
2191 (mapc #'(lambda (entry-key)
2192 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2193 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2194 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2195 entry-key (symbol-name (gethash 'type* entry))))
2196 (insert "\\hline\n")
2197 (mapc #'(lambda (field)
2198 (if-str (value (gethash field entry))
2199 (when (or (not (multiline-p value))
2200 ebib-print-multiline)
2201 (insert (format "%s: & %s\\\\\n"
2202 field (to-raw value))))))
2203 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2204 (insert "\\end{tabular}\n\n")
2205 (insert "\\bigskip\n\n"))
2206 entries)
2207 (insert "\\end{document}\n")
2208 (write-region (point-min) (point-max) tempfile))
2209 (ebib-lower)
2210 (find-file tempfile)))))
2211 ((default)
2212 (beep))))
2214 (defun ebib-latex-entries ()
2215 "Creates a LaTeX file that \\nocite's entries from the database.
2217 Operates either on all entries or on the marked entries."
2218 (interactive)
2219 (ebib-execute-when
2220 ((real-db entries)
2221 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2222 ebib-print-tempfile
2223 (read-file-name "Use temp file: " "~/" nil nil)))
2224 (progn
2225 (with-temp-buffer
2226 (insert "\\documentclass{article}\n\n")
2227 (when ebib-print-preamble
2228 (mapc #'(lambda (string)
2229 (insert (format "%s\n" string)))
2230 ebib-latex-preamble))
2231 (insert "\n\\begin{document}\n\n")
2232 (if (ebib-called-with-prefix)
2233 (mapc #'(lambda (entry)
2234 (insert (format "\\nocite{%s}\n" entry)))
2235 (edb-marked-entries ebib-cur-db))
2236 (insert "\\nocite{*}\n"))
2237 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2238 (insert "\\end{document}\n")
2239 (write-region (point-min) (point-max) tempfile))
2240 (ebib-lower)
2241 (find-file tempfile))))
2242 ((default)
2243 (beep))))
2245 (defun ebib-switch-to-database (num)
2246 (interactive "NSwitch to database number: ")
2247 (let ((new-db (nth (1- num) ebib-databases)))
2248 (if new-db
2249 (progn
2250 (setq ebib-cur-db new-db)
2251 (ebib-fill-entry-buffer)
2252 (ebib-fill-index-buffer))
2253 (error "Database %d does not exist" num))))
2255 (defun ebib-next-database ()
2256 (interactive)
2257 (ebib-execute-when
2258 ((database)
2259 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2260 (unless new-db
2261 (setq new-db (car ebib-databases)))
2262 (setq ebib-cur-db new-db)
2263 (ebib-fill-entry-buffer)
2264 (ebib-fill-index-buffer)))))
2266 (defun ebib-prev-database ()
2267 (interactive)
2268 (ebib-execute-when
2269 ((database)
2270 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2271 (unless new-db
2272 (setq new-db (last1 ebib-databases)))
2273 (setq ebib-cur-db new-db)
2274 (ebib-fill-entry-buffer)
2275 (ebib-fill-index-buffer)))))
2277 (defun ebib-browse-url (num)
2278 "Ask a browser to load the URL in the standard URL field.
2280 The standard URL field may contain more than one URL, if they're
2281 whitespace-separated. In that case, a numeric prefix argument
2282 specifies which URL to choose.
2284 By \"standard URL field\" is meant the field defined in the
2285 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2286 is `url'."
2287 (interactive "p")
2288 (ebib-execute-when
2289 ((entries)
2290 (let ((url (to-raw (gethash ebib-standard-url-field
2291 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2292 (when (not (and url
2293 (ebib-call-browser url num)))
2294 (error "No url found in field `%s'" ebib-standard-url-field))))
2295 ((default)
2296 (beep))))
2298 (defun ebib-call-browser (urls n)
2299 "Passes the Nth url in URLS to a browser.
2301 URLs must be a string of whitespace-separated urls."
2302 (let ((url (nth (1- n)
2303 (let ((start 0)
2304 (result nil))
2305 (while (string-match ebib-url-regexp urls start)
2306 (add-to-list 'result (match-string 0 urls) t)
2307 (setq start (match-end 0)))
2308 result))))
2309 (cond
2310 ((string-match "\\\\url{\\(.*?\\)}" url)
2311 (setq url (match-string 1 url)))
2312 ((string-match ebib-url-regexp url)
2313 (setq url (match-string 0 url)))
2314 (t (setq url nil)))
2315 (when url
2316 (if (not (string= ebib-browser-command ""))
2317 (start-process "Ebib-browser" nil ebib-browser-command url)
2318 (browse-url url)))))
2320 (defun ebib-virtual-db-and (not)
2321 "Filter entries into a virtual database.
2323 If the current database is a virtual database already, perform a
2324 logical AND on the entries."
2325 (interactive "p")
2326 (ebib-execute-when
2327 ((entries)
2328 (ebib-filter-to-virtual-db 'and not))
2329 ((default)
2330 (beep))))
2332 (defun ebib-virtual-db-or (not)
2333 "Filter entries into a virtual database.
2335 If the current database is a virtual database already, perform a
2336 logical OR with the entries in the original database."
2337 (interactive "p")
2338 (ebib-execute-when
2339 ((entries)
2340 (ebib-filter-to-virtual-db 'or not))
2341 ((default)
2342 (beep))))
2344 (defun ebib-virtual-db-not ()
2345 "Negates the current virtual database."
2346 (interactive)
2347 (ebib-execute-when
2348 ((virtual-db)
2349 (setf (edb-virtual ebib-cur-db)
2350 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2351 (cadr (edb-virtual ebib-cur-db))
2352 `(not ,(edb-virtual ebib-cur-db))))
2353 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2354 (ebib-fill-entry-buffer)
2355 (ebib-fill-index-buffer))
2356 ((default)
2357 (beep))))
2359 (defun ebib-filter-to-virtual-db (bool not)
2360 "Filters the current database to a virtual database.
2362 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2363 a logical `not' is applied to the selection."
2364 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2365 (if (< not 0) "(not " "")
2366 (if (< not 0) ")" ""))
2367 (cons '("any" 0)
2368 (mapcar #'(lambda (x)
2369 (cons (symbol-name x) 0))
2370 (append ebib-unique-field-list ebib-additional-fields)))
2371 nil t)))
2372 (setq field (intern-soft field))
2373 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2374 (if (< not 0) "(not " "")
2375 field
2376 (if (< not 0) ")" "")))))
2377 (ebib-execute-when
2378 ((virtual-db)
2379 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2380 ,(if (>= not 0)
2381 `(contains ,field ,regexp)
2382 `(not (contains ,field ,regexp))))))
2383 ((real-db)
2384 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2385 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2386 (ebib-fill-entry-buffer)
2387 (ebib-fill-index-buffer))))
2389 (defun ebib-create-virtual-db (field regexp not)
2390 "Creates a virtual database based on EBIB-CUR-DB."
2391 ;; a virtual database is a database whose edb-virtual field contains an
2392 ;; expression that selects entries. this function only sets that
2393 ;; expression, it does not actually filter the entries.
2394 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2395 (setf (edb-virtual new-db) (if (>= not 0)
2396 `(contains ,field ,regexp)
2397 `(not (contains ,field ,regexp))))
2398 (setf (edb-filename new-db) nil)
2399 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2400 (setf (edb-modified new-db) nil)
2401 (setf (edb-make-backup new-db) nil)
2402 new-db))
2404 (defmacro contains (field regexp)
2405 ;; This is a hack: CONTAINS depends on the variable ENTRY being set to an
2406 ;; actual Ebib entry for its operation. The point of this macro is to
2407 ;; facilitate defining filters for virtual databases. It enables us to
2408 ;; define filters of the form:
2410 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2412 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2414 (defun ebib-run-filter (filter db)
2415 "Run FILTER on DB"
2416 (setf (edb-keys-list db)
2417 (sort (let ((result nil))
2418 (maphash #'(lambda (key value)
2419 (let ((entry value)) ; this is necessary for actually running the filter
2420 (when (eval filter)
2421 (setq result (cons key result)))))
2422 (edb-database db))
2423 result)
2424 'string<))
2425 (setf (edb-n-entries db) (length (edb-keys-list db)))
2426 (setf (edb-cur-entry db) (edb-keys-list db)))
2428 (defun ebib-print-filter (num)
2429 "Display the filter of the current virtual database.
2431 With any prefix argument, reapplies the filter to the
2432 database. This can be useful when the source database was
2433 modified."
2434 (interactive "P")
2435 (ebib-execute-when
2436 ((virtual-db)
2437 (when num
2438 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2439 (ebib-fill-entry-buffer)
2440 (ebib-fill-index-buffer))
2441 (message "%S" (edb-virtual ebib-cur-db)))
2442 ((default)
2443 (beep))))
2445 (defun ebib-index-help ()
2446 "Displays the help message for the index buffer."
2447 (interactive)
2448 (other-window 1)
2449 (ebib-display-help ebib-index-buffer))
2451 (defun ebib-push-entry-key (prefix)
2452 "Pushes the current entry to a LaTeX buffer.
2454 The user is prompted for the buffer to push the entry into."
2455 (interactive "p")
2456 (let ((called-with-prefix (ebib-called-with-prefix)))
2457 (ebib-execute-when
2458 ((entries)
2459 (let ((buffer (read-buffer "Push entry(ies) to buffer: " ebib-push-buffer t)))
2460 (when buffer
2461 (setq ebib-push-buffer buffer)
2462 (let ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
2463 "{%s}"))
2464 (latex-arg (if called-with-prefix
2465 (when (edb-marked-entries ebib-cur-db)
2466 (mapconcat #'(lambda (x) x)
2467 (edb-marked-entries ebib-cur-db)
2468 ","))
2469 (car (edb-cur-entry ebib-cur-db)))))
2470 (save-excursion
2471 (set-buffer buffer)
2472 (insert (format latex-cmd latex-arg)))
2473 (message "Pushed entries to buffer %s" buffer)))))
2474 ((default)
2475 (beep)))))
2477 ;;;;;;;;;;;;;;;;
2478 ;; entry-mode ;;
2479 ;;;;;;;;;;;;;;;;
2481 (defvar ebib-entry-mode-map
2482 (let ((map (make-keymap)))
2483 (suppress-keymap map)
2484 (define-key map [up] 'ebib-prev-field)
2485 (define-key map [down] 'ebib-next-field)
2486 (define-key map [prior] 'ebib-goto-prev-set)
2487 (define-key map [next] 'ebib-goto-next-set)
2488 (define-key map [home] 'ebib-goto-first-field)
2489 (define-key map [end] 'ebib-goto-last-field)
2490 (define-key map " " 'ebib-goto-next-set)
2491 (define-key map "b" 'ebib-goto-prev-set)
2492 (define-key map "c" 'ebib-copy-field-contents)
2493 (define-key map "d" 'ebib-delete-field-contents)
2494 (define-key map "e" 'ebib-edit-field)
2495 (define-key map "g" 'ebib-goto-first-field)
2496 (define-key map "G" 'ebib-goto-last-field)
2497 (define-key map "h" 'ebib-entry-help)
2498 (define-key map "j" 'ebib-next-field)
2499 (define-key map "k" 'ebib-prev-field)
2500 (define-key map "l" 'ebib-edit-multiline-field)
2501 (define-key map [(control n)] 'ebib-next-field)
2502 (define-key map [(meta n)] 'ebib-goto-prev-set)
2503 (define-key map [(control p)] 'ebib-prev-field)
2504 (define-key map [(meta p)] 'ebib-goto-next-set)
2505 (define-key map "q" 'ebib-quit-entry-buffer)
2506 (define-key map "r" 'ebib-toggle-raw)
2507 (define-key map "s" 'ebib-insert-abbreviation)
2508 (define-key map "u" 'ebib-browse-url-in-field)
2509 (define-key map "x" 'ebib-cut-field-contents)
2510 (define-key map "\C-xb" 'undefined)
2511 (define-key map "\C-xk" 'undefined)
2512 (define-key map "y" 'ebib-yank-field-contents)
2513 map)
2514 "Keymap for the Ebib entry buffer.")
2516 (define-derived-mode ebib-entry-mode
2517 fundamental-mode "Ebib-entry"
2518 "Major mode for the Ebib entry buffer."
2519 (setq buffer-read-only t)
2520 (setq truncate-lines t))
2522 (defun ebib-quit-entry-buffer ()
2523 "Quit editing the entry."
2524 (interactive)
2525 (other-window 1))
2527 (defun ebib-find-visible-field (field direction)
2528 "Finds the first visible field before or after FIELD.
2530 If DIRECTION is negative, search the preceding fields, otherwise
2531 search the succeeding fields. If FIELD is visible itself, return
2532 that. If there is no preceding/following visible field, return
2533 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
2534 (when ebib-hide-hidden-fields
2535 (let ((fn (if (>= direction 0)
2536 'next-elem
2537 'prev-elem)))
2538 (while (and field
2539 (get field 'ebib-hidden))
2540 (setq field (funcall fn field ebib-cur-entry-fields)))))
2541 field)
2543 (defun ebib-prev-field ()
2544 "Move to the previous field."
2545 (interactive)
2546 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
2547 (if (null new-field)
2548 (beep)
2549 (setq ebib-current-field new-field)
2550 (ebib-move-to-field ebib-current-field -1))))
2552 (defun ebib-next-field ()
2553 "Move to the next field."
2554 (interactive)
2555 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
2556 (if (null new-field)
2557 (when (interactive-p) ; i call this function after editing a field,
2558 ; and we don't want a beep then
2559 (beep))
2560 (setq ebib-current-field new-field)
2561 (ebib-move-to-field ebib-current-field 1))))
2563 (defun ebib-goto-first-field ()
2564 "Move to the first field."
2565 (interactive)
2566 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
2567 (if (null new-field)
2568 (beep)
2569 (setq ebib-current-field new-field)
2570 (ebib-move-to-field ebib-current-field -1))))
2572 (defun ebib-goto-last-field ()
2573 "Move to the last field."
2574 (interactive)
2575 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
2576 (if (null new-field)
2577 (beep)
2578 (setq ebib-current-field new-field)
2579 (ebib-move-to-field ebib-current-field 1))))
2581 (defun ebib-goto-next-set ()
2582 "Move to the next set of fields."
2583 (interactive)
2584 (cond
2585 ((eq ebib-current-field 'type*) (ebib-next-field))
2586 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
2587 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2588 (obl-fields (ebib-get-obl-fields entry-type))
2589 (opt-fields (ebib-get-opt-fields entry-type))
2590 (new-field nil))
2591 (when (member ebib-current-field obl-fields)
2592 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
2593 ;; new-field is nil if there are no opt-fields
2594 (when (or (member ebib-current-field opt-fields)
2595 (null new-field))
2596 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
2597 (if (null new-field)
2598 (ebib-goto-last-field) ; if there was no further set to go to,
2599 ; go to the last field of the current set
2600 (setq ebib-current-field new-field)
2601 (ebib-move-to-field ebib-current-field 1))))))
2603 (defun ebib-goto-prev-set ()
2604 "Move to the previous set of fields."
2605 (interactive)
2606 (unless (eq ebib-current-field 'type*)
2607 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2608 (obl-fields (ebib-get-obl-fields entry-type))
2609 (opt-fields (ebib-get-opt-fields entry-type))
2610 (new-field nil))
2611 (if (member ebib-current-field obl-fields)
2612 (ebib-goto-first-field)
2613 (when (member ebib-current-field ebib-additional-fields)
2614 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
2615 (when (or (member ebib-current-field opt-fields)
2616 (null new-field))
2617 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
2618 (if (null new-field)
2619 (ebib-goto-first-field)
2620 (setq ebib-current-field new-field)
2621 (ebib-move-to-field ebib-current-field -1))))))
2623 (defun ebib-edit-entry-type ()
2624 "Edits the type of an entry."
2625 ;; we want to put the completion buffer in the lower window. for this
2626 ;; reason, we need to switch to the other window before calling
2627 ;; completing-read. but in order to make sure that we return to the
2628 ;; entry buffer and not the index buffer when the user presses C-g, we
2629 ;; need to do this in an unwind-protect.
2630 (unwind-protect
2631 (progn
2632 (other-window 1)
2633 (let ((collection (ebib-create-collection ebib-entry-types-hash)))
2634 (if-str (new-type (completing-read "type: " collection nil t))
2635 (progn
2636 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
2637 (ebib-fill-entry-buffer)
2638 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2639 (ebib-set-modified t)))))
2640 (other-window 1)))
2642 (defun ebib-edit-crossref ()
2643 "Edits the crossref field."
2644 (unwind-protect
2645 (progn
2646 (other-window 1)
2647 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
2648 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
2649 (progn
2650 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
2651 (ebib-set-modified t)))))
2652 (other-window 1)
2653 ;; we now redisplay the entire entry buffer, so that the crossref'ed
2654 ;; fields show up. this also puts the cursor back on the type field.
2655 (ebib-fill-entry-buffer)))
2657 ;; we should modify ebib-edit-field, so that it calls the appropriate
2658 ;; helper function, which asks the user for the new value and just returns
2659 ;; that. storing it should then be done by ebib-edit-field, no matter what
2660 ;; sort of field the user edits.
2662 (defun ebib-edit-field ()
2663 "Edits a field of a BibTeX entry."
2664 (interactive)
2665 (cond
2666 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
2667 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
2668 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
2670 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
2671 (raw nil))
2672 (if (multiline-p init-contents)
2673 (ebib-edit-multiline-field)
2674 (when init-contents
2675 (if (raw-p init-contents)
2676 (setq raw t)
2677 (setq init-contents (to-raw init-contents))))
2678 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
2679 (if init-contents
2680 (cons init-contents 0)
2681 nil)
2682 ebib-minibuf-hist))
2683 (puthash ebib-current-field (if raw
2684 new-contents
2685 (concat "{" new-contents "}"))
2686 ebib-cur-entry-hash)
2687 (remhash ebib-current-field ebib-cur-entry-hash))
2688 (ebib-redisplay-current-field)
2689 ;; we move to the next field, but only if ebib-edit-field was
2690 ;; called interactively, otherwise we get a strange bug in
2691 ;; ebib-toggle-raw...
2692 (if (interactive-p) (ebib-next-field))
2693 (ebib-set-modified t))))))
2695 (defun ebib-browse-url-in-field (num)
2696 "Browse the url in the current field.
2698 The field may contain a whitespace-separated set of urls. The
2699 prefix argument indicates which url is to be sent to the
2700 browser."
2701 (interactive "p")
2702 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2703 (if (not (and urls
2704 (ebib-call-browser urls num)))
2705 (error "No url found in field `%s'" ebib-current-field))))
2707 (defun ebib-copy-field-contents ()
2708 "Copies the contents of the current field to the kill ring."
2709 (interactive)
2710 (unless (eq ebib-current-field 'type*)
2711 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2712 (when (stringp contents)
2713 (kill-new contents)
2714 (message "Field contents copied.")))))
2716 (defun ebib-cut-field-contents ()
2717 "Kills the contents of the current field. The killed text is put in the kill ring."
2718 (interactive)
2719 (unless (eq ebib-current-field 'type*)
2720 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2721 (when (stringp contents)
2722 (remhash ebib-current-field ebib-cur-entry-hash)
2723 (kill-new contents)
2724 (ebib-redisplay-current-field)
2725 (ebib-set-modified t)
2726 (message "Field contents killed.")))))
2728 (defun ebib-yank-field-contents (arg)
2729 "Inserts the last killed text into the current field.
2731 If the current field already has a contents, nothing is inserted,
2732 unless the previous command was also ebib-yank-field-contents,
2733 then the field contents is replaced with the previous yank. That
2734 is, multiple use of this command functions like the combination
2735 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
2736 (interactive "P")
2737 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
2738 (eq ebib-current-field 'crossref)
2739 (unless (eq last-command 'ebib-yank-field-contents)
2740 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
2741 (progn
2742 (setq this-command t)
2743 (beep))
2744 (let ((new-contents (current-kill (cond
2745 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
2748 ((eq arg '-) -2)
2749 (t (1- arg))))))
2750 (when new-contents
2751 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
2752 (ebib-redisplay-current-field)
2753 (ebib-set-modified t)))))
2755 (defun ebib-delete-field-contents ()
2756 "Deletes the contents of the current field. The deleted text is not put
2757 in the kill ring."
2758 (interactive)
2759 (if (eq ebib-current-field 'type*)
2760 (beep)
2761 (remhash ebib-current-field ebib-cur-entry-hash)
2762 (ebib-redisplay-current-field)
2763 (ebib-set-modified t)
2764 (message "Field contents deleted.")))
2766 (defun ebib-toggle-raw ()
2767 "Toggles the raw status of the current field contents."
2768 (interactive)
2769 (unless (or (eq ebib-current-field 'type*)
2770 (eq ebib-current-field 'crossref))
2771 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2772 (if (not contents) ; if there is no value,
2773 (progn
2774 (ebib-edit-field) ; the user can enter one, which we must then make raw
2775 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
2776 (when new-contents
2777 ;; note: we don't have to check for empty string, since that is
2778 ;; already done in ebib-edit-field
2779 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
2780 (if (raw-p contents)
2781 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
2782 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
2783 (ebib-redisplay-current-field)
2784 (ebib-set-modified t))))
2786 (defun ebib-edit-multiline-field ()
2787 "Edits the current field in multiline-mode."
2788 (interactive)
2789 (unless (or (eq ebib-current-field 'type*)
2790 (eq ebib-current-field 'crossref))
2791 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
2792 (if (raw-p text)
2793 (setq ebib-multiline-raw t)
2794 (setq text (to-raw text))
2795 (setq ebib-multiline-raw nil))
2796 (ebib-multiline-edit 'fields text))))
2798 (defun ebib-insert-abbreviation ()
2799 "Insert an abbreviation from the ones defined in the database."
2800 (interactive)
2801 (if (gethash ebib-current-field ebib-cur-entry-hash)
2802 (beep)
2803 (when (edb-strings-list ebib-cur-db)
2804 (unwind-protect
2805 (progn
2806 (other-window 1)
2807 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
2808 (string (completing-read "Abbreviation to insert: " collection nil t)))
2809 (when string
2810 (puthash ebib-current-field string ebib-cur-entry-hash)
2811 (ebib-set-modified t))))
2812 (other-window 1)
2813 ;; we can't do this earlier, because we would be writing to the index buffer...
2814 (ebib-redisplay-current-field)
2815 (ebib-next-field)))))
2817 (defun ebib-entry-help ()
2818 "Displays the help message for the entry buffer."
2819 (interactive)
2820 (ebib-display-help ebib-entry-buffer))
2822 ;;;;;;;;;;;;;;;;;;
2823 ;; strings-mode ;;
2824 ;;;;;;;;;;;;;;;;;;
2826 (defvar ebib-strings-mode-map
2827 (let ((map (make-keymap)))
2828 (suppress-keymap map)
2829 (define-key map [up] 'ebib-prev-string)
2830 (define-key map [down] 'ebib-next-string)
2831 (define-key map [prior] 'ebib-strings-page-up)
2832 (define-key map [next] 'ebib-strings-page-down)
2833 (define-key map [home] 'ebib-goto-first-string)
2834 (define-key map [end] 'ebib-goto-last-string)
2835 (define-key map " " 'ebib-strings-page-down)
2836 (define-key map "a" 'ebib-add-string)
2837 (define-key map "b" 'ebib-strings-page-up)
2838 (define-key map "c" 'ebib-copy-string-contents)
2839 (define-key map "d" 'ebib-delete-string)
2840 (define-key map "e" 'ebib-edit-string)
2841 (define-key map "g" 'ebib-goto-first-string)
2842 (define-key map "G" 'ebib-goto-last-string)
2843 (define-key map "h" 'ebib-strings-help)
2844 (define-key map "j" 'ebib-next-string)
2845 (define-key map "k" 'ebib-prev-string)
2846 (define-key map "l" 'ebib-edit-multiline-string)
2847 (define-key map [(control n)] 'ebib-next-string)
2848 (define-key map [(meta n)] 'ebib-strings-page-down)
2849 (define-key map [(control p)] 'ebib-prev-string)
2850 (define-key map [(meta p)] 'ebib-strings-page-up)
2851 (define-key map "q" 'ebib-quit-strings-buffer)
2852 (define-key map "x" 'ebib-export-string)
2853 (define-key map "X" 'ebib-export-all-strings)
2854 (define-key map "\C-xb" 'disabled)
2855 (define-key map "\C-xk" 'disabled)
2856 map)
2857 "Keymap for the ebib strings buffer.")
2859 (define-derived-mode ebib-strings-mode
2860 fundamental-mode "Ebib-strings"
2861 "Major mode for the Ebib strings buffer."
2862 (setq buffer-read-only t)
2863 (setq truncate-lines t))
2865 (defun ebib-quit-strings-buffer ()
2866 "Quit editing the @STRING definitions."
2867 (interactive)
2868 (switch-to-buffer ebib-entry-buffer)
2869 (other-window 1))
2871 (defun ebib-prev-string ()
2872 "Move to the previous string."
2873 (interactive)
2874 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
2875 (beep)
2876 ;; go to the beginnig of the highlight and move upward one line.
2877 (goto-char (ebib-highlight-start ebib-strings-highlight))
2878 (forward-line -1)
2879 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2880 (ebib-set-strings-highlight)))
2882 (defun ebib-next-string ()
2883 "Move to the next string."
2884 (interactive)
2885 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2886 (when (interactive-p) (beep))
2887 (goto-char (ebib-highlight-start ebib-strings-highlight))
2888 (forward-line 1)
2889 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2890 (ebib-set-strings-highlight)))
2892 (defun ebib-goto-first-string ()
2893 "Move to the first string."
2894 (interactive)
2895 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
2896 (goto-char (point-min))
2897 (ebib-set-strings-highlight))
2899 (defun ebib-goto-last-string ()
2900 "Move to the last string."
2901 (interactive)
2902 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2903 (goto-char (point-max))
2904 (forward-line -1)
2905 (ebib-set-strings-highlight))
2907 (defun ebib-strings-page-up ()
2908 "Moves 10 entries up in the database."
2909 (interactive)
2910 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
2911 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
2912 (if (<= (- number-of-strings remaining-number-of-strings) 10)
2913 (ebib-goto-first-string)
2914 (setq ebib-current-string (nth
2915 (- number-of-strings remaining-number-of-strings 10)
2916 (edb-strings-list ebib-cur-db)))
2917 (goto-char (ebib-highlight-start ebib-strings-highlight))
2918 (forward-line -10)
2919 (ebib-set-strings-highlight)))
2920 (message ebib-current-string))
2922 (defun ebib-strings-page-down ()
2923 "Moves 10 entries down in the database."
2924 (interactive)
2925 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
2926 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
2927 (if (<= remaining-number-of-strings 10)
2928 (ebib-goto-last-string)
2929 (setq ebib-current-string (nth
2930 (- number-of-strings remaining-number-of-strings -10)
2931 (edb-strings-list ebib-cur-db)))
2932 (goto-char (ebib-highlight-start ebib-strings-highlight))
2933 (forward-line 10)
2934 (ebib-set-strings-highlight)))
2935 (message ebib-current-string))
2937 (defun ebib-fill-strings-buffer ()
2938 "Fills the strings buffer with the @STRING definitions."
2939 (set-buffer ebib-strings-buffer)
2940 (with-buffer-writable
2941 (erase-buffer)
2942 (dolist (elem (edb-strings-list ebib-cur-db))
2943 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
2944 (insert (format "%-18s %s\n" elem
2945 (if (multiline-p str)
2946 (concat "+" (first-line str))
2947 (concat " " str)))))))
2948 (goto-char (point-min))
2949 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
2950 (ebib-set-strings-highlight)
2951 (set-buffer-modified-p nil))
2953 (defun ebib-edit-string ()
2954 "Edits the value of an @STRING definition
2956 When the user enters an empty string, the value is not changed."
2957 (interactive)
2958 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
2959 (if (multiline-p init-contents)
2960 (ebib-edit-multiline-string)
2961 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
2962 (if init-contents
2963 (cons init-contents 0)
2964 nil)
2965 ebib-minibuf-hist))
2966 (progn
2967 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
2968 (ebib-redisplay-current-string)
2969 (ebib-next-string)
2970 (ebib-set-modified t))
2971 (error "@STRING definition cannot be empty")))))
2973 (defun ebib-copy-string-contents ()
2974 "Copies the contents of the current string to the kill ring."
2975 (interactive)
2976 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
2977 (kill-new contents)
2978 (message "String value copied.")))
2980 (defun ebib-delete-string ()
2981 "Deletes the current @STRING definition from the database."
2982 (interactive)
2983 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
2984 (remhash ebib-current-string (edb-strings ebib-cur-db))
2985 (with-buffer-writable
2986 (let ((beg (progn
2987 (goto-char (ebib-highlight-start ebib-strings-highlight))
2988 (point))))
2989 (forward-line 1)
2990 (delete-region beg (point))))
2991 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
2992 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
2993 (when (null new-cur-string) ; deleted the last string
2994 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
2995 (forward-line -1))
2996 (setq ebib-current-string new-cur-string))
2997 (ebib-set-strings-highlight)
2998 (ebib-set-modified t)
2999 (message "@STRING definition deleted.")))
3001 (defun ebib-add-string ()
3002 "Creates a new @STRING definition."
3003 (interactive)
3004 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3005 (progn
3006 (if (member new-abbr (edb-strings-list ebib-cur-db))
3007 (error (format "%s already exists" new-abbr)))
3008 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3009 (progn
3010 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3011 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3012 (with-buffer-writable
3013 (insert (format "%-19s %s\n" new-abbr new-string)))
3014 (forward-line -1)
3015 (ebib-set-strings-highlight)
3016 (setq ebib-current-string new-abbr)
3017 (ebib-set-modified t))))))
3019 (defun ebib-export-string (prefix)
3020 "Exports the current @STRING.
3022 The prefix argument indicates which database to copy the string
3023 to. If no prefix argument is present, a filename is asked to
3024 which the string is appended."
3025 (interactive "P")
3026 (let ((num (ebib-prefix prefix)))
3027 (if num
3028 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3029 #'(lambda (db)
3030 (let ((abbr ebib-current-string)
3031 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3032 (if (member abbr (edb-strings-list db))
3033 (error "@STRING definition already exists in database %d" num)
3034 (ebib-insert-string abbr string db t)))))
3035 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3036 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3037 #'(lambda ()
3038 (insert (format "\n@string{%s = %s}\n"
3039 ebib-current-string
3040 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3042 (defun ebib-export-all-strings (prefix)
3043 "Export all @STRING definitions.
3045 If a prefix argument is given, it is taken as the database to copy the
3046 definitions to. Without prefix argument, asks for a file to append them
3047 to."
3048 (interactive "P")
3049 (when ebib-current-string ; there is always a current string, unless there are no strings
3050 (let ((num (ebib-prefix prefix)))
3051 (if num
3052 (ebib-export-to-db
3053 num "All @STRING definitions copied to database %d"
3054 #'(lambda (db)
3055 (mapc #'(lambda (abbr)
3056 (if (member abbr (edb-strings-list db))
3057 (message "@STRING definition `%s' already exists in database %d" abbr num)
3058 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3059 (edb-strings-list ebib-cur-db))))
3060 (ebib-export-to-file "Export all @STRING definitions to file: "
3061 "All @STRING definitions exported to %s"
3062 #'(lambda ()
3063 (insert (format "\n")) ; to keep things tidy.
3064 (ebib-format-strings ebib-cur-db)))))))
3066 (defun ebib-edit-multiline-string ()
3067 "Edits the current string in multiline-mode."
3068 (interactive)
3069 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3071 (defun ebib-strings-help ()
3072 "Displays the help message for the strings buffer."
3073 (interactive)
3074 (ebib-display-help ebib-strings-buffer))
3076 ;;;;;;;;;;;;;;;;;;;;;;;;;
3077 ;; multiline edit mode ;;
3078 ;;;;;;;;;;;;;;;;;;;;;;;;;
3080 (define-derived-mode ebib-multiline-edit-mode
3081 text-mode "Ebib-edit"
3082 "Major mode for editing multiline strings in Ebib."
3083 ;; we redefine some basic keys because we need them to leave this buffer.
3084 (local-set-key "\C-xb" 'ebib-leave-multiline-edit)
3085 (local-set-key "\C-x\C-s" 'ebib-save-from-multiline-edit)
3086 (local-set-key "\C-xk" 'ebib-cancel-multiline-edit))
3088 (defun ebib-multiline-edit (type &optional starttext)
3089 "Switches to Ebib's multiline edit buffer.
3091 STARTTEXT is a string that contains the initial text of the buffer."
3092 ;; note: the buffer is put in the currently active window!
3093 (switch-to-buffer ebib-multiline-buffer)
3094 (erase-buffer)
3095 (setq ebib-editing type)
3096 (when starttext
3097 (insert starttext)
3098 (goto-char (point-min))
3099 (set-buffer-modified-p nil)))
3101 (defun ebib-leave-multiline-edit ()
3102 "Quits the multiline edit buffer."
3103 (interactive)
3104 (ebib-store-multiline-text)
3105 (cond
3106 ((eq ebib-editing 'preamble)
3107 (switch-to-buffer ebib-entry-buffer)
3108 (other-window 1)) ; we have to switch back to the index buffer window
3109 ((eq ebib-editing 'fields)
3110 (switch-to-buffer ebib-entry-buffer)
3111 (ebib-redisplay-current-field)
3112 (ebib-next-field))
3113 ((eq ebib-editing 'strings)
3114 (switch-to-buffer ebib-strings-buffer)
3115 (ebib-redisplay-current-string)
3116 (ebib-next-string)))
3117 (message "Text stored."))
3119 (defun ebib-save-from-multiline-edit ()
3120 "Stores the text being edited in the multiline edit buffer and then saves the database."
3121 (interactive)
3122 (ebib-store-multiline-text)
3123 (ebib-save-database ebib-cur-db)
3124 (set-buffer-modified-p nil))
3126 (defun ebib-store-multiline-text ()
3127 "Stores the text being edited in the multiline edit buffer."
3128 ;; first make sure the buffer has no unbalanced braces. they would
3129 ;; confuse ebib if the file is loaded the next time.
3130 (check-parens)
3131 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3132 (cond
3133 ((eq ebib-editing 'preamble)
3134 (if (equal text "")
3135 (setf (edb-preamble ebib-cur-db) nil)
3136 (setf (edb-preamble ebib-cur-db) text)))
3137 ((eq ebib-editing 'fields)
3138 (if (equal text "")
3139 (remhash ebib-current-field ebib-cur-entry-hash)
3140 (when (not ebib-multiline-raw)
3141 (setq text (from-raw text)))
3142 (puthash ebib-current-field text ebib-cur-entry-hash)))
3143 ((eq ebib-editing 'strings)
3144 (if (equal text "")
3145 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3146 ;; MESSAGE, but we also do not switch back to the strings
3147 ;; buffer. this may not be so bad, actually, because the user
3148 ;; may want to change his edit.
3149 (error "@STRING definition cannot be empty ")
3150 (setq text (from-raw text)) ; strings cannot be raw
3151 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3152 (ebib-set-modified t))
3154 (defun ebib-cancel-multiline-edit ()
3155 "Quits the multiline edit buffer and discards the changes."
3156 (interactive)
3157 (catch 'no-cancel
3158 (when (buffer-modified-p)
3159 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3160 (throw 'no-cancel nil)))
3161 (cond
3162 ((eq ebib-editing 'fields)
3163 (switch-to-buffer ebib-entry-buffer)
3164 (ebib-redisplay-current-field)) ; we have to do this, because the
3165 ; user may have saved with C-x C-s
3166 ; before
3167 ((eq ebib-editing 'strings)
3168 (switch-to-buffer ebib-strings-buffer)
3169 (ebib-redisplay-current-string))
3170 ((eq ebib-editing 'preamble)
3171 (switch-to-buffer ebib-entry-buffer)
3172 (other-window 1)))))
3174 ;;;;;;;;;;;;;;;;;;;;
3175 ;; ebib-help-mode ;;
3176 ;;;;;;;;;;;;;;;;;;;;
3178 (defvar ebib-help-mode-map
3179 (let ((map (make-keymap)))
3180 (suppress-keymap map)
3181 (define-key map " " 'scroll-up)
3182 (define-key map "b" 'scroll-down)
3183 (define-key map "q" 'ebib-quit-help-buffer)
3184 map)
3185 "Keymap for the ebib help buffer.")
3187 (define-derived-mode ebib-help-mode
3188 fundamental-mode "Ebib-help"
3189 "Major mode for the Ebib help buffer."
3190 (setq buffer-read-only t)
3191 (local-set-key "\C-xb" 'ebib-quit-help-buffer)
3192 (local-set-key "\C-xk" 'ebib-quit-help-buffer))
3194 (defun ebib-display-help (buffer)
3195 "Shows the help message for Ebib-buffer BUFFER."
3196 (switch-to-buffer ebib-help-buffer)
3197 (setq ebib-before-help buffer)
3198 (with-buffer-writable
3199 (erase-buffer)
3200 (cond
3201 ((eq buffer ebib-index-buffer) (insert ebib-index-buffer-help))
3202 ((eq buffer ebib-entry-buffer) (insert ebib-entry-buffer-help))
3203 ((eq buffer ebib-strings-buffer) (insert ebib-strings-buffer-help)))
3204 (goto-char (point-min))))
3206 (defun ebib-quit-help-buffer ()
3207 "Exits the help buffer."
3208 (interactive)
3209 (cond
3210 ((eq ebib-before-help ebib-index-buffer)
3211 (switch-to-buffer ebib-entry-buffer)
3212 (other-window 1))
3213 ((eq ebib-before-help ebib-entry-buffer)
3214 (switch-to-buffer ebib-entry-buffer))
3215 ((eq ebib-before-help ebib-strings-buffer)
3216 (switch-to-buffer ebib-strings-buffer))))
3218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3219 ;; functions for non-Ebib buffers ;;
3220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3222 (defun ebib-import ()
3223 "Searches for BibTeX entries in the current buffer.
3225 The entries are added to the current database (i.e. the database that was
3226 active when Ebib was lowered. Works on the whole buffer, or on the region
3227 if it is active."
3228 (interactive)
3229 (if (not ebib-cur-db)
3230 (error "No database loaded. Use `o' to open a database")
3231 (if (edb-virtual ebib-cur-db)
3232 (error "Cannot import to a virtual database")
3233 (save-excursion
3234 (save-restriction
3235 (if (region-active)
3236 (narrow-to-region (region-beginning)
3237 (region-end)))
3238 (let ((buffer (current-buffer)))
3239 (with-temp-buffer
3240 (insert-buffer-substring buffer)
3241 (let ((n (ebib-find-bibtex-entries t)))
3242 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3243 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3244 (when (edb-strings-list ebib-cur-db)
3245 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3246 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3247 (ebib-fill-entry-buffer)
3248 (ebib-fill-index-buffer)
3249 (ebib-set-modified t)
3250 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3251 (car n)
3252 (cadr n)
3253 (if (caddr n)
3255 "no")))))))))))
3257 (defun ebib-get-db-from-filename (filename)
3258 "Returns the database struct associated with FILENAME."
3259 (catch 'found
3260 (mapc #'(lambda (db)
3261 (if (string= (file-name-nondirectory (edb-filename db)) filename)
3262 (throw 'found db)))
3263 ebib-databases)
3264 nil))
3266 (defun ebib-get-local-databases ()
3267 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3269 Each .bib file is a string holding the name of the .bib
3270 file. This function simply searches the current LaTeX file or its
3271 master file for a \\bibliography command and returns the file(s)
3272 given in its argument. If no \\bibliography command is found,
3273 returns the symbol NONE."
3274 (let ((texfile-buffer (current-buffer))
3275 texfile)
3276 ;; if AucTeX's TeX-master is used and set to a string, we must
3277 ;; search that file for a \bibliography command, as it's more
3278 ;; likely to be in there than in the file we're in.
3279 (and (boundp 'TeX-master)
3280 (stringp TeX-master)
3281 (setq texfile (ensure-extension TeX-master "tex")))
3282 (with-temp-buffer
3283 (if (and texfile (file-readable-p texfile))
3284 (insert-file-contents texfile)
3285 (insert-buffer-substring texfile-buffer))
3286 (save-excursion
3287 (goto-char (point-min))
3288 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3289 (mapcar #'(lambda (file)
3290 (ensure-extension file "bib"))
3291 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3292 'none)))))
3294 (defun ebib-insert-bibtex-key (prefix)
3295 "Inserts a BibTeX key at POINT.
3297 The user is prompted for a BibTeX key and has to choose one from
3298 the database(s) associated with the current LaTeX file, or from
3299 the current database if there is no \\bibliography command. Tab
3300 completion works."
3301 (interactive "p")
3302 (ebib-execute-when
3303 ((database)
3304 (or ebib-local-bibtex-filenames
3305 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3306 (let (collection)
3307 (if (eq ebib-local-bibtex-filenames 'none)
3308 (if (null (edb-cur-entry ebib-cur-db))
3309 (error "No entries found in current database")
3310 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3311 (mapc #'(lambda (file)
3312 (let ((db (ebib-get-db-from-filename file)))
3313 (cond
3314 ((null db)
3315 (message "Database %s not loaded" file))
3316 ((null (edb-cur-entry db))
3317 (message "No entries in database %s" file))
3318 (t (setq collection (append (ebib-create-collection (edb-database db))
3319 collection))))))
3320 ebib-local-bibtex-filenames))
3321 (if collection
3322 (let* ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
3323 "{%s}"))
3324 (key (completing-read (format "Insert \"%s\" with key: " latex-cmd)
3325 collection nil t nil ebib-minibuf-hist)))
3326 (when key
3327 (insert (format (or (cdr (assoc prefix ebib-insertion-strings))
3328 "{%s}") key)))))))
3329 ((default)
3330 (error "No database loaded"))))
3332 (defun ebib-entry-summary ()
3333 "Shows the fields of the key at POINT.
3335 The key is searched in the database associated with the LaTeX file, or in
3336 the current database if no \\bibliography command can be found."
3337 (interactive)
3338 (ebib-execute-when
3339 ((database)
3340 (or ebib-local-bibtex-filenames
3341 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3342 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3343 entry)
3344 (if (eq ebib-local-bibtex-filenames 'none)
3345 (if (not (member key (edb-keys-list ebib-cur-db)))
3346 (error "`%s' is not in the current database" key)
3347 (setq entry (gethash key (edb-database ebib-cur-db))))
3348 (setq entry
3349 (catch 'found
3350 (mapc #'(lambda (file)
3351 (let ((db (ebib-get-db-from-filename file)))
3352 (if (null db)
3353 (message "Database %s not loaded" file)
3354 (if (member key (edb-keys-list db))
3355 (throw 'found (gethash key (edb-database db)))))))
3356 ebib-local-bibtex-filenames)
3357 nil)))
3358 (if (null entry)
3359 (error "Entry `%s' not found" key)
3360 (with-output-to-temp-buffer "*Help*"
3361 (ebib-format-fields entry 'princ)))))
3362 ((default)
3363 (error "No database(s) loaded"))))
3365 (provide 'ebib)
3367 ;; we put these at the end, because they seem to mess up Emacs'
3368 ;; syntax highlighting.
3370 (setq ebib-index-buffer-help
3371 "Ebib index buffer -- command key overview
3373 Note: command keys are case-sensitive.
3375 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3377 cursor movement:
3378 [Up], k, C-p: go to the previous entry
3379 [Down], j, C-n: go to the next entry
3380 [Home], g: go to the first entry
3381 [End], G: go to the last entry
3382 [PgUp], b, M-p: scroll up
3383 [PgDn], [Space], M-n: scroll down
3385 editing:
3386 e: edit the current entry
3387 E: edit the current entry's name
3388 a: add a new entry
3389 d: delete the current entry
3390 ;-d: deletes all marked entries
3391 t: edit the @STRING definitions
3392 r: edit the @PREAMBLE definition
3393 m: (un)mark the current entry
3394 ;-m: unmarks all marked entries
3396 searching:
3397 /: search the database
3398 n: find the next occurrence of the search string
3399 C-s: search for a key (incrementally)
3400 [return]: select the entry under the cursor (use after C-s)
3401 F: follow the crossref field
3402 &: filter the current database with a logical AND
3403 |: filter the current database with a logical OR
3404 ~: filter the current database with a logical NOT
3405 V: show the current filter (with prefix argument:
3406 reapply current filter)
3408 file handling:
3409 o: open a database
3410 c: close the database
3411 s: save the database
3412 S: save all databases
3413 w: save the database under a different name
3414 M: merge another database
3415 x: export the current entry to another file
3416 (with prefix argument N: copy to database N)
3417 ;-x: export the marked entry to another file
3418 (with prefix argument N: copy to database N)
3419 X: export the @PREAMBLE definition to another file
3420 (with prefix argument N: copy to database N)
3421 f: print the full filename in the minibuffer
3423 databases:
3424 1-9: switch to database 1-9
3425 J: switch to another database (accepts prefix argument)
3426 [right], [left]: switch to previous/next database
3427 L: LaTeX the database
3428 ;-L: LaTeX the marked entries
3429 P: print the database
3430 ;-P: print the marked entries
3432 general:
3433 u: extract URL from the `url' field and send it to a browser
3434 p: push the current entry to a LaTeX buffer
3435 ;-p: push marked entries to a LaTeX buffer
3436 C: customise Ebib
3437 z: put Ebib in the background
3438 q: quit Ebib
3439 h: show this help page
3442 (setq ebib-entry-buffer-help
3443 "Ebib entry buffer -- command key overview
3445 Note: command keys are case-sensitive.
3447 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3449 cursor movement:
3450 [Up], k, C-p: go to the previous field
3451 [Down], j, C-n: go to the next field
3452 [Home], g: go to the first field
3453 [End], G: go to the last field
3454 [PgUp], b, M-p: go to the previous group of fields
3455 [PgDn], [Space], M-n: go to the next group of fields
3457 editing:
3458 e: edit the value of the current field
3459 c: copy the value of the current field (value is put into the kill ring)
3460 x: kill the value of the current field (value is put into the kill ring)
3461 y: yank the most recently copied/cut string
3462 d: delete the value of the current entry
3463 r: toggle the \"rawness\" status of the current field
3464 l: edit the current field as multi-line
3465 s: insert an @STRING abbreviation into the current field
3467 general:
3468 u: extract URL and send it to a browser
3469 q: quit the entry buffer and return to the index buffer
3470 h: show this help page
3473 (setq ebib-strings-buffer-help
3474 "Ebib strings buffer -- command key overview
3476 Note: command keys are case-sensitive.
3478 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3480 cursor movement:
3481 [Up], k, C-p: go to the previous @STRING definition
3482 [Down], j, C-n: go to the next @STRING definition
3483 [Home], g: go to the first @STRING definition
3484 [End], G: go to the last @STRING definition
3485 [PgUp], b, M-p: scroll 10 @STRING definitions up
3486 [PgDn], [Space], M-n: scroll 10 @STRING definitions down
3488 editing:
3489 e: edit the value of the current @STRING definition
3490 c: copy the value of the current @STRING definition
3491 d: delete the current @STRING definition
3492 a: add an @STRING definition
3493 l: edit the current @STRING as multi-line
3495 exporting:
3496 x: export the current @STRING definition to another file
3497 (with prefix argument N: copy to database N)
3498 X: export all @STRING definitions to another file
3499 (with prefix argument N: copy to database N)
3501 general:
3502 q: quit the strings buffer and return to the index buffer
3503 h: show this help page
3506 ;;; ebib ends here