Allow multiple .bib files in \bibliography command
[ebib.git] / src / ebib.el
blobad4d32e2f408fa71605fe7742d9dfef165917d13
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 (with-buffer-writable
750 (goto-char (ebib-highlight-start ebib-fields-highlight))
751 (let ((beg (point)))
752 (end-of-line)
753 (delete-region beg (point)))
754 (insert (format "%-17s " (symbol-name ebib-current-field))
755 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash))
756 (ebib-set-fields-highlight)))
758 (defun ebib-redisplay-current-string ()
759 "Redisplays the current string definition in the strings buffer."
760 (set-buffer ebib-strings-buffer)
761 (with-buffer-writable
762 (let ((str (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
763 (goto-char (ebib-highlight-start ebib-strings-highlight))
764 (let ((beg (point)))
765 (end-of-line)
766 (delete-region beg (point)))
767 (insert (format "%-18s %s" ebib-current-string
768 (if (multiline-p str)
769 (concat "+" (first-line str))
770 (concat " " str))))
771 (ebib-set-strings-highlight))))
773 (defun ebib-move-to-field (field direction)
774 "Moves the fields overlay to the line containing FIELD.
776 If DIRECTION is positive, searches forward, if DIRECTION is
777 negative, searches backward. If DIRECTION is 1 or -1, searches
778 from POINT, if DIRECTION is 2 or -2, searches from beginning or
779 end of buffer. If FIELD is not found in the entry buffer, the
780 overlay is not moved. FIELD must be a symbol."
782 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
784 (set-buffer ebib-entry-buffer)
785 (if (eq field 'type*)
786 (goto-char (point-min))
787 (multiple-value-bind (fn start limit) (if (>= direction 0)
788 (values 're-search-forward (point-min) (point-max))
789 (values 're-search-backward (point-max) (point-min)))
790 ;; make sure we can get back to our original position, if the field
791 ;; cannot be found in the buffer:
792 (let ((current-pos (point)))
793 (when (evenp direction)
794 (goto-char start))
795 (unless (funcall fn (concat "^" (symbol-name field)) limit t)
796 (goto-char current-pos)))))
797 (ebib-set-fields-highlight))
799 (defun ebib-create-collection (hashtable)
800 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
802 The keys of HASHTABLE must be either symbols or strings."
803 (let ((result nil))
804 (maphash '(lambda (x y)
805 (setq result (cons (cons (symbol-or-string x)
807 result)))
808 hashtable)
809 result))
811 (defmacro ebib-retrieve-entry (entry-key db)
812 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
813 `(gethash ,entry-key (edb-database ,db)))
815 (defun ebib-get-field-highlighted (field current-entry &optional match-str)
816 ;; note: we need to work on a copy of the string, otherwise the highlights
817 ;; are made to the string as stored in the database. hence copy-sequence.
818 (let ((case-fold-search t)
819 (string (copy-sequence (gethash field current-entry)))
820 (raw " ")
821 (multiline " ")
822 (matched nil))
823 ;; we have to do a couple of things now:
824 ;; - remove {} or "" around the string, if they're there
825 ;; - search for match-str
826 ;; - properly adjust the string if it's multiline
827 ;; but all this is not necessary if there was no string
828 (if (null string)
829 (let ((xref (to-raw (gethash 'crossref current-entry))))
830 (when xref
831 (setq string (gethash field (ebib-retrieve-entry xref ebib-cur-db)))
832 (if string
833 (setq string (propertize (to-raw string) 'face 'ebib-crossref-face 'fontified t))
834 (setq string ""))))
835 (if (raw-p string)
836 (setq raw "*")
837 (setq string (to-raw string))) ; we have to make the string look nice
838 (when match-str
839 (multiple-value-setq (string matched) (match-all match-str string)))
840 (when (multiline-p string)
841 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
842 ;; multiline is local and therefore the object it refers to should
843 ;; be GC'ed when the function returns. but for some reason, the
844 ;; plus sign is persistent, and if it's been highlighted as the
845 ;; result of a search, it stays that way.
846 (setq multiline (propertize "+" 'face nil))
847 (setq string (first-line string))))
848 (when (and matched
849 (string= multiline "+"))
850 (add-text-properties 0 1 '(face highlight) multiline))
851 (concat raw multiline string)))
853 (defun ebib-format-fields (entry fn &optional match-str)
854 (let* ((entry-type (gethash 'type* entry))
855 (obl-fields (ebib-get-obl-fields entry-type))
856 (opt-fields (ebib-get-opt-fields entry-type)))
857 (funcall fn (format "%-19s %s\n" "type" entry-type))
858 (mapc '(lambda (fields)
859 (funcall fn "\n")
860 (mapcar '(lambda (field)
861 (unless (and (get field 'ebib-hidden)
862 ebib-hide-hidden-fields)
863 (funcall fn (format "%-17s " field))
864 (funcall fn (or
865 (ebib-get-field-highlighted field entry match-str)
866 ""))
867 (funcall fn "\n")))
868 fields))
869 (list obl-fields opt-fields ebib-additional-fields))))
871 (defun ebib-fill-entry-buffer (&optional match-str)
872 "Fills the entry buffer with the fields of the current entry.
874 MATCH-STRING is a regexp that will be highlighted when it occurs in the
875 field contents."
876 (set-buffer ebib-entry-buffer)
877 (with-buffer-writable
878 (erase-buffer)
879 (when (and ebib-cur-db ; do we have a database?
880 (edb-keys-list ebib-cur-db) ; does it contain entries?
881 (gethash (car (edb-cur-entry ebib-cur-db))
882 (edb-database ebib-cur-db))) ; does the current entry exist?
883 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db))
884 (edb-database ebib-cur-db)) 'insert match-str)
885 (setq ebib-current-field 'type*)
886 (goto-char (point-min))
887 (ebib-set-fields-highlight))))
889 (defun ebib-set-modified (mod &optional db)
890 "Sets the modified flag of the database DB to MOD.
892 If DB is nil, it defaults to the current database, and the modified flag of
893 the index buffer is also (re)set. MOD must be either T or NIL."
894 (unless db
895 (setq db ebib-cur-db))
896 (setf (edb-modified db) mod)
897 (when (eq db ebib-cur-db)
898 (save-excursion
899 (set-buffer ebib-index-buffer)
900 (set-buffer-modified-p mod))))
902 (defun ebib-modified-p ()
903 "Checks if any of the databases in Ebib were modified.
905 Returns the first modified database, or NIL if none was modified."
906 (let ((db (car ebib-databases)))
907 (while (and db
908 (not (edb-modified db)))
909 (setq db (next-elem db ebib-databases)))
910 db))
912 (defun ebib-create-new-database (&optional db)
913 "Creates a new database instance and returns it.
915 If DB is set to a database, the new database is a copy of DB."
916 (let ((new-db
917 (if (edb-p db)
918 (copy-edb db)
919 (make-edb))))
920 (setq ebib-databases (append ebib-databases (list new-db)))
921 new-db))
923 ;; the next two functions are used in loading the database. a database is
924 ;; loaded by reading the raw file into a temp buffer and then reading all
925 ;; the entries in it. for this we need to be able to search a matching
926 ;; parenthesis and a matching double quote.
928 (defun ebib-match-paren-forward (limit)
929 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
931 Does not search/move beyond LIMIT. Returns T if a matching parenthesis was
932 found, NIL otherwise. If point was not at an opening parenthesis at all,
933 NIL is returned and point is not moved. If point was at an opening
934 parenthesis but no matching closing parenthesis was found, point is moved
935 to LIMIT."
936 (if (nor (eq (char-after) ?\{)
937 (eq (char-after) ?\"))
939 (save-restriction
940 (narrow-to-region (point-min) limit)
941 (condition-case nil
942 (progn
943 (forward-list)
944 ;; all of ebib expects that point moves to the closing
945 ;; parenthesis, not right after it, so we adjust.
946 (forward-char -1)
947 t) ; return t because a matching brace was found
948 (error (progn
949 (goto-char (point-max)) ; point-max because the narrowing is still in effect
950 nil))))))
952 (defun ebib-match-quote-forward (limit)
953 "Moves to the closing double quote matching the quote at POINT.
955 Does not search/move beyond LIMIT. Returns T if a matching quote was found,
956 NIL otherwise. If point was not at a double quote at all, NIL is returned
957 and point is not moved. If point was at a quote but no matching closing
958 quote was found, point is moved to LIMIT."
959 (when (eq (char-after (point)) ?\") ; make sure we're on a double quote.
960 (while
961 (progn
962 (forward-char) ; we need to move forward because we're on a double quote.
963 (skip-chars-forward "^\"" limit) ; find the next double quote.
964 (and (eq (char-before) ?\\) ; if it's preceded by a backslash,
965 (< (point) limit)))) ; and we're still below LIMIT, keep on searching.
966 (eq (char-after (point)) ?\"))) ; return T or NIL based on whether we've found a quote.
968 (defun ebib-insert-entry (entry-key fields db &optional sort timestamp)
969 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
971 Optional argument SORT indicates whether the KEYS-LIST must be
972 sorted after insertion. Default is NIL. Optional argument
973 TIMESTAMP indicates whether a timestamp is to be added to the
974 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
975 must also be set to T."
976 (when (and timestamp ebib-use-timestamp)
977 (puthash 'timestamp (from-raw (format-time-string ebib-timestamp-format)) fields))
978 (puthash entry-key fields (edb-database db))
979 (ebib-set-modified t db)
980 (setf (edb-n-entries db) (1+ (edb-n-entries db)))
981 (setf (edb-keys-list db)
982 (if sort
983 (sort (cons entry-key (edb-keys-list db)) 'string<)
984 (cons entry-key (edb-keys-list db)))))
986 (defun ebib-insert-string (abbr string db &optional sort)
987 "Stores the @STRING definition defined by ABBR and STRING into DB.
989 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
990 after insertion. When loading or merging a file, for example, it is more
991 economic to sort KEYS-LIST manually after all entries in the file have been
992 added."
993 (puthash abbr (from-raw string) (edb-strings db))
994 (ebib-set-modified t db)
995 (setf (edb-strings-list db)
996 (if sort
997 (sort (cons abbr (edb-strings-list db)) 'string<)
998 (cons abbr (edb-strings-list db)))))
1000 (defmacro ebib-cur-entry-key ()
1001 "Returns the key of the current entry in EBIB-CUR-DB."
1002 `(car (edb-cur-entry ebib-cur-db)))
1004 (defun ebib-search-key-in-buffer (entry-key)
1005 "Searches ENTRY-KEY in the index buffer.
1007 Moves point to the first character of the key and returns point."
1008 (goto-char (point-min))
1009 (search-forward entry-key)
1010 (beginning-of-line)
1011 (point))
1013 ;; when we sort entries, we either use string< on the entry keys, or
1014 ;; ebib-entry<, if the user has defined a sort order.
1016 (defun ebib-entry< (x y)
1017 "Returns T if entry X is smaller than entry Y.
1019 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1020 and Y should be keys of entries in the current database."
1021 (let* ((sort-list ebib-sort-order)
1022 (sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1023 (sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1024 (while (and sort-list
1025 (string= sortstring-x sortstring-y))
1026 (setq sort-list (cdr sort-list))
1027 (setq sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1028 (setq sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1029 (if (and sortstring-x sortstring-y)
1030 (string< sortstring-x sortstring-y)
1031 (string< x y))))
1033 (defun ebib-get-sortstring (entry-key sortkey-list)
1034 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1036 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1037 is a list of fields that are considered in order for the sort value."
1038 (let ((sort-string nil))
1039 (while (and sortkey-list
1040 (null (setq sort-string (gethash (car sortkey-list)
1041 (ebib-retrieve-entry entry-key ebib-cur-db)))))
1042 (setq sortkey-list (cdr sortkey-list)))
1043 sort-string))
1045 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1046 ;; main program execution ;;
1047 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1049 (defun ebib ()
1050 "Ebib, a BibTeX database manager."
1051 (interactive)
1052 (if (or (equal (window-buffer) ebib-index-buffer)
1053 (equal (window-buffer) ebib-entry-buffer))
1054 (error "Ebib already active")
1055 (unless ebib-initialized
1056 (ebib-init)
1057 (if ebib-preload-bib-files
1058 (mapc '(lambda (file)
1059 (ebib-load-bibtex-file file))
1060 ebib-preload-bib-files)))
1061 ;; we save the current window configuration.
1062 (setq ebib-saved-window-config (current-window-configuration))
1063 ;; create the window configuration we want for ebib.
1064 (delete-other-windows)
1065 (switch-to-buffer ebib-index-buffer)
1066 (let* ((keys-window (selected-window))
1067 (entry-window (split-window keys-window ebib-index-window-size)))
1068 (set-window-buffer entry-window ebib-entry-buffer))))
1070 (defun ebib-init ()
1071 "Initialises Ebib.
1073 This function sets all variables to their initial values, creates the
1074 buffers and reads the rc file."
1075 (setq ebib-cur-entry-hash nil
1076 ebib-current-field nil
1077 ebib-minibuf-hist nil
1078 ebib-saved-window-config nil)
1079 (put 'timestamp 'ebib-hidden t)
1080 (when (file-readable-p "~/.ebibrc")
1081 (load "~/.ebibrc"))
1082 (ebib-create-buffers)
1083 (setq ebib-index-highlight (ebib-make-highlight 1 1 ebib-index-buffer))
1084 (setq ebib-fields-highlight (ebib-make-highlight 1 1 ebib-entry-buffer))
1085 (setq ebib-strings-highlight (ebib-make-highlight 1 1 ebib-strings-buffer))
1086 (setq ebib-initialized t))
1088 (defun ebib-create-buffers ()
1089 "Creates the buffers for Ebib."
1090 ;; first we create a buffer for multiline editing. this one does *not*
1091 ;; have a name beginning with a space, because undo-info is normally
1092 ;; present in an edit buffer.
1093 (setq ebib-multiline-buffer (get-buffer-create "*Ebib-edit*"))
1094 (set-buffer ebib-multiline-buffer)
1095 (ebib-multiline-edit-mode)
1096 ;; then we create a buffer to hold the fields of the current entry.
1097 (setq ebib-entry-buffer (get-buffer-create " *Ebib-entry*"))
1098 (set-buffer ebib-entry-buffer)
1099 (ebib-entry-mode)
1100 ;; then we create a buffer to hold the @STRING definitions
1101 (setq ebib-strings-buffer (get-buffer-create " *Ebib-strings*"))
1102 (set-buffer ebib-strings-buffer)
1103 (ebib-strings-mode)
1104 ;; then we create the help buffer
1105 (setq ebib-help-buffer (get-buffer-create " *Ebib-help*"))
1106 (set-buffer ebib-help-buffer)
1107 (ebib-help-mode)
1108 ;; and lastly we create a buffer for the entry keys.
1109 (setq ebib-index-buffer (get-buffer-create " none"))
1110 (set-buffer ebib-index-buffer)
1111 (ebib-index-mode))
1113 (defun ebib-quit ()
1114 "Quits Ebib.
1116 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1117 (interactive)
1118 (when (if (ebib-modified-p)
1119 (yes-or-no-p "There are modified databases. Quit anyway? ")
1120 (y-or-n-p "Quit Ebib? "))
1121 (kill-buffer ebib-entry-buffer)
1122 (kill-buffer ebib-index-buffer)
1123 (kill-buffer ebib-multiline-buffer)
1124 (setq ebib-databases nil
1125 ebib-index-buffer nil
1126 ebib-entry-buffer nil
1127 ebib-initialized nil
1128 ebib-index-highlight nil
1129 ebib-fields-highlight nil
1130 ebib-strings-highlight nil
1131 ebib-export-filename nil)
1132 (set-window-configuration ebib-saved-window-config)
1133 (message "")))
1135 (defun ebib-kill-emacs-query-function ()
1136 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1137 killed and the database has been modified."
1138 (if (not (ebib-modified-p))
1140 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1141 (progn
1142 (ebib-save-all-databases)
1144 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1146 (add-hook 'kill-emacs-query-functions 'ebib-kill-emacs-query-function)
1148 ;;;;;;;;;;;;;;;;
1149 ;; index-mode ;;
1150 ;;;;;;;;;;;;;;;;
1152 (eval-and-compile
1153 (define-prefix-command 'ebib-prefix-map)
1154 (suppress-keymap ebib-prefix-map)
1155 (defvar ebib-prefixed-functions '(ebib-delete-entry
1156 ebib-latex-entries
1157 ebib-mark-entry
1158 ebib-print-entries
1159 ebib-export-entry
1160 ebib-push-entry-key)))
1162 ;; macro to redefine key bindings.
1164 (defmacro ebib-key (buffer key &optional command)
1165 (cond
1166 ((eq buffer 'index)
1167 (let ((one `(define-key ebib-index-mode-map ,key (quote ,command)))
1168 (two (when (or (null command)
1169 (member command ebib-prefixed-functions))
1170 `(define-key ebib-prefix-map ,key (quote ,command)))))
1171 (if two
1172 `(progn ,one ,two)
1173 one)))
1174 ((eq buffer 'entry)
1175 `(define-key ebib-entry-mode-map ,key (quote ,command)))
1176 ((eq buffer 'strings)
1177 `(define-key ebib-strings-mode-map ,key (quote ,command)))
1178 ((eq buffer 'mark-prefix)
1179 `(progn
1180 (define-key ebib-index-mode-map (format "%c" ebib-prefix-key) nil)
1181 (define-key ebib-index-mode-map ,key 'ebib-prefix-map)
1182 (setq ebib-prefix-key (string-to-char ,key))))))
1184 (defvar ebib-index-mode-map
1185 (let ((map (make-keymap)))
1186 (suppress-keymap map)
1187 map)
1188 "Keymap for the ebib index buffer.")
1190 ;; we define the keys with ebib-key rather than with define-key, because
1191 ;; that automatically sets up ebib-prefix-map as well.
1192 (ebib-key index [up] ebib-prev-entry)
1193 (ebib-key index [down] ebib-next-entry)
1194 (ebib-key index [right] ebib-next-database)
1195 (ebib-key index [left] ebib-prev-database)
1196 (ebib-key index [prior] ebib-index-scroll-down)
1197 (ebib-key index [next] ebib-index-scroll-up)
1198 (ebib-key index [home] ebib-goto-first-entry)
1199 (ebib-key index [end] ebib-goto-last-entry)
1200 (ebib-key index [return] ebib-select-entry)
1201 (ebib-key index " " ebib-index-scroll-up)
1202 (ebib-key index "/" ebib-search)
1203 (ebib-key index "&" ebib-virtual-db-and)
1204 (ebib-key index "|" ebib-virtual-db-or)
1205 (ebib-key index "~" ebib-virtual-db-not)
1206 (ebib-key index ";" ebib-prefix-map)
1207 (ebib-key index "a" ebib-add-entry)
1208 (ebib-key index "b" ebib-index-scroll-down)
1209 (ebib-key index "c" ebib-close-database)
1210 (ebib-key index "C" ebib-customize)
1211 (ebib-key index "d" ebib-delete-entry)
1212 (ebib-key index "e" ebib-edit-entry)
1213 (ebib-key index "E" ebib-edit-keyname)
1214 (ebib-key index "f" ebib-print-filename)
1215 (ebib-key index "F" ebib-follow-crossref)
1216 (ebib-key index "g" ebib-goto-first-entry)
1217 (ebib-key index "G" ebib-goto-last-entry)
1218 (ebib-key index "h" ebib-index-help)
1219 (ebib-key index "H" ebib-toggle-hidden)
1220 (ebib-key index "j" ebib-next-entry)
1221 (ebib-key index "J" ebib-switch-to-database)
1222 (ebib-key index "k" ebib-prev-entry)
1223 (ebib-key index "L" ebib-latex-entries)
1224 (ebib-key index "m" ebib-mark-entry)
1225 (ebib-key index "M" ebib-merge-bibtex-file)
1226 (ebib-key index "n" ebib-search-next)
1227 (ebib-key index [(control n)] ebib-next-entry)
1228 (ebib-key index [(meta n)] ebib-index-scroll-up)
1229 (ebib-key index "o" ebib-load-bibtex-file)
1230 (ebib-key index "p" ebib-push-entry-key)
1231 (ebib-key index [(control p)] ebib-prev-entry)
1232 (ebib-key index [(meta p)] ebib-index-scroll-down)
1233 (ebib-key index "P" ebib-print-entries)
1234 (ebib-key index "r" ebib-edit-preamble)
1235 (ebib-key index "q" ebib-quit)
1236 (ebib-key index "s" ebib-save-current-database)
1237 (ebib-key index "S" ebib-save-all-databases)
1238 (ebib-key index "t" ebib-edit-strings)
1239 (ebib-key index "u" ebib-browse-url)
1240 (ebib-key index "V" ebib-print-filter)
1241 (ebib-key index "w" ebib-write-database)
1242 (ebib-key index "x" ebib-export-entry)
1243 (ebib-key index "\C-xb" ebib-lower)
1244 (ebib-key index "\C-xk" ebib-quit)
1245 (ebib-key index "X" ebib-export-preamble)
1246 (ebib-key index "z" ebib-lower)
1248 (defun ebib-switch-to-database-nth (key)
1249 (interactive (list (if (featurep 'xemacs)
1250 (event-key last-command-event)
1251 last-command-event)))
1252 (ebib-switch-to-database (- (if (featurep 'xemacs)
1253 (char-to-int key)
1254 key) 48)))
1256 (mapc #'(lambda (key)
1257 (define-key ebib-index-mode-map (format "%d" key)
1258 'ebib-switch-to-database-nth))
1259 '(1 2 3 4 5 6 7 8 9))
1261 (define-derived-mode ebib-index-mode
1262 fundamental-mode "Ebib-index"
1263 "Major mode for the Ebib index buffer."
1264 (setq buffer-read-only t))
1266 (defun ebib-fill-index-buffer ()
1267 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1269 If EBIB-CUR-DB is nil, the buffer is just erased and its name set to \"none\"."
1270 (set-buffer ebib-index-buffer)
1271 (let ((buffer-read-only nil))
1272 (erase-buffer)
1273 (if ebib-cur-db
1274 (progn
1275 ;; we may call this function when there are no entries in the
1276 ;; database. if so, we don't need to do this:
1277 (when (edb-cur-entry ebib-cur-db)
1278 (mapcar '(lambda (entry)
1279 (insert entry)
1280 (when (member entry (edb-marked-entries ebib-cur-db))
1281 (let ((beg (save-excursion
1282 (beginning-of-line)
1283 (point))))
1284 (add-text-properties beg (point) `(face ,ebib-marked-face))))
1285 (insert "\n"))
1286 (edb-keys-list ebib-cur-db))
1287 (goto-char (point-min))
1288 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
1289 (beginning-of-line)
1290 (ebib-set-index-highlight))
1291 (set-buffer-modified-p (edb-modified ebib-cur-db))
1292 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1293 (length (member ebib-cur-db ebib-databases)))))
1294 (edb-name ebib-cur-db))))
1295 (rename-buffer " none"))))
1297 (defun ebib-customize ()
1298 (interactive)
1299 (ebib-lower)
1300 (customize-group 'ebib))
1302 (defun ebib-load-bibtex-file (&optional file)
1303 "Loads a BibTeX file into ebib."
1304 (interactive)
1305 (unless file
1306 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1307 (setq ebib-cur-db (ebib-create-new-database))
1308 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1309 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1310 ;; first, we empty the buffers
1311 (ebib-erase-buffer ebib-index-buffer)
1312 (ebib-erase-buffer ebib-entry-buffer)
1313 (if (file-readable-p file)
1314 ;; if the user entered the name of an existing file, we load it
1315 ;; by putting it in a buffer and then parsing it.
1316 (with-temp-buffer
1317 (insert-file-contents file)
1318 ;; if the user makes any changes, we'll want to create a back-up.
1319 (setf (edb-make-backup ebib-cur-db) t)
1320 (let ((result (ebib-find-bibtex-entries nil)))
1321 (setf (edb-n-entries ebib-cur-db) (car result))
1322 (when (edb-keys-list ebib-cur-db)
1323 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1324 (when (edb-strings-list ebib-cur-db)
1325 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1326 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1327 ;; and fill the buffers. note that filling a buffer also makes
1328 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1329 ;; later.
1330 (ebib-set-modified nil)
1331 (ebib-fill-entry-buffer)
1332 ;; and now we tell the user the result
1333 (message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1334 (car result)
1335 (cadr result)
1336 (if (caddr result)
1338 "no"))))
1339 ;; if the file does not exist, we need to issue a message.
1340 (message "(New file)"))
1341 ;; what we have to do in *any* case, is fill the index buffer. (this
1342 ;; even works if there are no keys in the database, e.g. when the
1343 ;; user opened a new file or if no BibTeX entries were found.
1344 (ebib-fill-index-buffer))
1346 (defun ebib-merge-bibtex-file ()
1347 "Merges a BibTeX file into the database."
1348 (interactive)
1349 (unless (edb-virtual ebib-cur-db)
1350 (if (not ebib-cur-db)
1351 (error "No database loaded. Use `o' to open a database")
1352 (let ((file (read-file-name "File to merge: ")))
1353 (with-temp-buffer
1354 (insert-file-contents file)
1355 (let ((n (ebib-find-bibtex-entries t)))
1356 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1357 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1358 (when (edb-strings-list ebib-cur-db)
1359 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1360 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1361 (ebib-fill-entry-buffer)
1362 (ebib-fill-index-buffer)
1363 (ebib-set-modified t)
1364 (message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1365 (car n)
1366 (cadr n)
1367 (if (caddr n)
1369 "no"))))))))
1371 (defun ebib-find-bibtex-entries (timestamp)
1372 "Finds the BibTeX entries in the current buffer.
1374 The search is started at the beginnig of the buffer. All entries found are
1375 stored in the hash table DATABASE of EBIB-CUR-DB. Returns a three-element
1376 list: the first element is the number of entries found, the second the
1377 number of @STRING definitions, and the third is T or NIL, indicating
1378 whether a @PREAMBLE was found.
1380 TIMESTAMP indicates whether a timestamp is to be added to each
1381 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1382 is set to T."
1383 (modify-syntax-entry ?\" "w")
1384 (let ((n-entries 0)
1385 (n-strings 0)
1386 (preamble nil))
1387 (goto-char (point-min))
1388 (while (re-search-forward "^@" nil t) ; find the next entry
1389 (let ((beg (point)))
1390 (when (looking-at-goto-end (concat ebib-bibtex-identifier "[\(\{]"))
1391 (let ((entry-type (downcase (buffer-substring-no-properties beg (1- (point))))))
1392 (cond
1393 ((equal entry-type "string") ; string and preamble must be treated differently
1394 (if (ebib-read-string)
1395 (setq n-strings (1+ n-strings))))
1396 ((equal entry-type "preamble") (ebib-read-preamble)
1397 (setq preamble t))
1398 ((equal entry-type "comment") (ebib-match-paren-forward (point-max))) ; ignore comments
1399 ((gethash (intern-soft entry-type) ebib-entry-types-hash) ; if the entry type has been defined
1400 (if (ebib-read-entry entry-type timestamp)
1401 (setq n-entries (1+ n-entries))))
1402 (t (message "Unknown entry type `%s'. Skipping." entry-type) ; we found something we don't know
1403 (ebib-match-paren-forward (point-max))))))))
1404 (list n-entries n-strings preamble)))
1406 (defun ebib-read-string ()
1407 "Reads the @STRING definition beginning at the line POINT is on.
1409 If a proper abbreviation and string are found, they are stored in the
1410 database. Returns the string if one was read, nil otherwise."
1411 (let ((limit (save-excursion ; we find the matching end parenthesis
1412 (backward-char)
1413 (ebib-match-paren-forward (point-max))
1414 (point))))
1415 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1416 (let ((beg (point)))
1417 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1418 (if-str (abbr (buffer-substring-no-properties beg (point)))
1419 (progn
1420 (skip-chars-forward "^\"{" limit)
1421 (let ((beg (point)))
1422 (if-str (string (cond
1423 ((eq (char-after) ?\" ) (if (ebib-match-quote-forward limit)
1424 (buffer-substring-no-properties beg (1+ (point)))
1425 nil))
1426 ((eq (char-after) ?\{ ) (if (ebib-match-paren-forward limit)
1427 (buffer-substring-no-properties beg (1+ (point)))
1428 nil))
1429 (t nil)))
1430 (if (member abbr (edb-strings-list ebib-cur-db))
1431 (message (format "@STRING definition `%s' duplicated" abbr))
1432 (ebib-insert-string abbr string ebib-cur-db))))))))))
1434 (defun ebib-read-preamble ()
1435 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1437 If there was already another @PREAMBLE definition, the new one is added to
1438 the existing one with a hash sign `#' between them."
1439 (let ((beg (point)))
1440 (forward-char -1)
1441 (ebib-match-paren-forward (point-max))
1442 (let ((text (buffer-substring-no-properties beg (point))))
1443 (if (edb-preamble ebib-cur-db)
1444 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1445 (setf (edb-preamble ebib-cur-db) text)))))
1447 (defun ebib-read-entry (entry-type &optional timestamp)
1448 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1450 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1451 otherwise. Optional argument TIMESTAMP indicates whether a
1452 timestamp is to be added. (Whether a timestamp is actually added,
1453 also depends on EBIB-USE-TIMESTAMP.)"
1454 (let ((entry-limit (save-excursion
1455 (backward-char)
1456 (ebib-match-paren-forward (point-max))
1457 (point)))
1458 (beg (progn
1459 (skip-chars-forward " \n\t\f") ; note the space!
1460 (point))))
1461 (when (looking-at-goto-end (concat "\\("
1462 ebib-bibtex-identifier
1463 "\\)[ \t\n\f]*,")
1464 1) ; this delimits the entry key
1465 (let ((entry-key (buffer-substring-no-properties beg (point))))
1466 (if (member entry-key (edb-keys-list ebib-cur-db))
1467 (message "Entry `%s' duplicated " entry-key)
1468 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1469 (when fields ; if fields were found, we store them, and return T.
1470 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1471 t)))))))
1473 (defun ebib-find-bibtex-fields (entry-type limit)
1474 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1476 Returns a hash table containing all the fields and values, or NIL if none
1477 were found. ENTRY-TYPE is the type of the entry, which will be recorded in
1478 the hash table. Before the search starts, POINT is moved back to the
1479 beginning of the line."
1480 (beginning-of-line)
1481 ;; we assign a function to fn in order to avoid putting the test on
1482 ;; ebib-allow-identical-fields in the while loop, where it would get
1483 ;; tested with every field being read.
1484 (let ((fn (if (not ebib-allow-identical-fields)
1485 (symbol-function 'puthash)
1486 #'(lambda (field-type field-contents fields)
1487 (let ((existing-contents (gethash field-type fields)))
1488 (puthash field-type (if existing-contents
1489 (from-raw (concat (to-raw existing-contents)
1490 "; "
1491 (to-raw field-contents)))
1492 field-contents)
1493 fields))))))
1494 (let ((fields (make-hash-table :size 15)))
1495 (while (progn
1496 (skip-chars-forward "^," limit) ; we must move to the next comma,
1497 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1498 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1499 (let ((beg (point)))
1500 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1501 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1502 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1503 (let ((field-contents (ebib-get-field-contents limit)))
1504 (when field-contents
1505 (funcall fn field-type field-contents fields))))))))
1506 (when (> (hash-table-count fields) 0)
1507 (puthash 'type* entry-type fields)
1508 fields))))
1510 (defun ebib-get-field-contents (limit)
1511 "Gets the contents of a BibTeX field.
1513 LIMIT indicates the end of the entry, beyond which the function will not
1514 search."
1515 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1516 (let ((beg (point)))
1517 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1519 (defun ebib-find-end-of-field (limit)
1520 "Moves POINT to the end of a field's contents and returns POINT.
1522 The contents of a field is delimited by a comma or by the closing brace of
1523 the entry. The latter is at position LIMIT."
1524 (while (and (not (eq (char-after) ?\,))
1525 (< (point) limit))
1526 (cond
1527 ((eq (char-after) ?\{) (ebib-match-paren-forward limit))
1528 ((eq (char-after) ?\") (ebib-match-quote-forward limit)))
1529 (forward-char 1))
1530 (if (= (point) limit)
1531 (skip-chars-backward " \n\t\f"))
1532 (point))
1534 (defun ebib-lower ()
1535 "Hides the Ebib buffers, but does not delete them."
1536 (interactive)
1537 (if (nor (equal (window-buffer) ebib-index-buffer)
1538 (equal (window-buffer) ebib-entry-buffer)
1539 (equal (window-buffer) ebib-strings-buffer)
1540 (equal (window-buffer) ebib-multiline-buffer)
1541 (equal (window-buffer) ebib-help-buffer))
1542 (error "Ebib is not active ")
1543 (set-window-configuration ebib-saved-window-config)
1544 (bury-buffer ebib-entry-buffer)
1545 (bury-buffer ebib-index-buffer)
1546 (bury-buffer ebib-multiline-buffer)
1547 (bury-buffer ebib-strings-buffer)
1548 (bury-buffer ebib-help-buffer)))
1550 (defun ebib-prev-entry ()
1551 "Moves to the previous BibTeX entry."
1552 (interactive)
1553 (ebib-execute-when
1554 ((entries)
1555 ;; if the current entry is the first entry,
1556 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1557 (beep) ; just beep.
1558 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1559 (1+ (length (edb-cur-entry ebib-cur-db)))))
1560 (goto-char (ebib-highlight-start ebib-index-highlight))
1561 (forward-line -1)
1562 (ebib-set-index-highlight)
1563 (ebib-fill-entry-buffer)))
1564 ((default)
1565 (beep))))
1567 (defun ebib-next-entry ()
1568 "Moves to the next BibTeX entry."
1569 (interactive)
1570 (ebib-execute-when
1571 ((entries)
1572 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1573 (beep) ; just beep.
1574 (setf (edb-cur-entry ebib-cur-db)
1575 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1576 (goto-char (ebib-highlight-start ebib-index-highlight))
1577 (forward-line 1)
1578 (ebib-set-index-highlight)
1579 (ebib-fill-entry-buffer)))
1580 ((default)
1581 (beep))))
1583 (defun ebib-add-entry ()
1584 "Adds a new entry to the database."
1585 (interactive)
1586 (ebib-execute-when
1587 ((real-db)
1588 (if-str (entry-key (read-string "New entry key: "))
1589 (progn
1590 (if (member entry-key (edb-keys-list ebib-cur-db))
1591 (error "Key already exists")
1592 (set-buffer ebib-index-buffer)
1593 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1594 (with-buffer-writable
1595 (insert (format "%s\n" entry-key))) ; add the entry in the buffer.
1596 (forward-line -1) ; move one line up to position the cursor on the new entry.
1597 (ebib-set-index-highlight)
1598 (let ((fields (make-hash-table)))
1599 (puthash 'type* ebib-default-type fields)
1600 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1601 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1602 (ebib-fill-entry-buffer)
1603 (ebib-edit-entry)
1604 (ebib-set-modified t)))))
1605 ((no-database)
1606 (error "No database open. Use `o' to open a database first"))
1607 ((default)
1608 (beep))))
1610 (defun ebib-close-database ()
1611 "Closes the current BibTeX database."
1612 (interactive)
1613 (ebib-execute-when
1614 ((database)
1615 (when (if (edb-modified ebib-cur-db)
1616 (yes-or-no-p "Database modified. Close it anyway? ")
1617 (y-or-n-p "Close database? "))
1618 (let ((to-be-deleted ebib-cur-db)
1619 (new-db (next-elem ebib-cur-db ebib-databases)))
1620 (setq ebib-databases (delete to-be-deleted ebib-databases))
1621 (if ebib-databases ; do we still have another database loaded?
1622 (progn
1623 (setq ebib-cur-db (or new-db
1624 (last1 ebib-databases)))
1625 (unless (edb-cur-entry ebib-cur-db)
1626 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1627 (ebib-fill-entry-buffer)
1628 (ebib-fill-index-buffer))
1629 ;; otherwise, we have to clean up a little and empty all the buffers.
1630 (setq ebib-cur-db nil)
1631 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1632 (set-buffer (car buf))
1633 (with-buffer-writable
1634 (erase-buffer))
1635 (ebib-delete-highlight (cadr buf)))
1636 (list (list ebib-entry-buffer ebib-fields-highlight)
1637 (list ebib-index-buffer ebib-index-highlight)
1638 (list ebib-strings-buffer ebib-strings-highlight)))
1639 ;; multiline edit buffer
1640 (set-buffer ebib-multiline-buffer)
1641 (with-buffer-writable
1642 (erase-buffer))
1643 (set-buffer ebib-index-buffer)
1644 (rename-buffer " none"))
1645 (message "Database closed."))))))
1647 (defun ebib-goto-first-entry ()
1648 "Moves to the first BibTeX entry in the database."
1649 (interactive)
1650 (ebib-execute-when
1651 ((entries)
1652 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1653 (set-buffer ebib-index-buffer)
1654 (goto-char (point-min))
1655 (ebib-set-index-highlight)
1656 (ebib-fill-entry-buffer))
1657 ((default)
1658 (beep))))
1660 (defun ebib-goto-last-entry ()
1661 "Moves to the last entry in the BibTeX database."
1662 (interactive)
1663 (ebib-execute-when
1664 ((entries)
1665 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
1666 (set-buffer ebib-index-buffer)
1667 (goto-line (edb-n-entries ebib-cur-db))
1668 (ebib-set-index-highlight)
1669 (ebib-fill-entry-buffer))
1670 ((default)
1671 (beep))))
1673 (defun ebib-edit-entry ()
1674 "Edits the current BibTeX entry."
1675 (interactive)
1676 (ebib-execute-when
1677 ((real-db entries)
1678 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1679 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
1680 (other-window 1)
1681 (switch-to-buffer ebib-entry-buffer)
1682 (goto-char (ebib-highlight-end ebib-fields-highlight)))
1683 ((default)
1684 (beep))))
1686 (defun ebib-edit-keyname ()
1687 "Change the key of a BibTeX entry."
1688 (interactive)
1689 (ebib-execute-when
1690 ((real-db entries)
1691 (let ((cur-keyname (ebib-cur-entry-key)))
1692 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
1693 cur-keyname))
1694 (if (member new-keyname (edb-keys-list ebib-cur-db))
1695 (error (format "Key `%s' already exists" new-keyname))
1696 (unless (string= cur-keyname new-keyname)
1697 (let ((fields (ebib-retrieve-entry cur-keyname ebib-cur-db))
1698 (marked (member cur-keyname (edb-marked-entries ebib-cur-db))))
1699 (ebib-remove-entry-from-db cur-keyname ebib-cur-db)
1700 (ebib-remove-key-from-buffer cur-keyname)
1701 (ebib-insert-entry new-keyname fields ebib-cur-db t nil)
1702 (setf (edb-cur-entry ebib-cur-db) (member new-keyname (edb-keys-list ebib-cur-db)))
1703 (sort-in-buffer (edb-n-entries ebib-cur-db) new-keyname)
1704 (with-buffer-writable
1705 (insert (format "%s\n" new-keyname))) ; add the entry in the buffer.
1706 (forward-line -1) ; move one line up to position the cursor on the new entry.
1707 (ebib-set-index-highlight)
1708 (ebib-set-modified t)
1709 (when marked (ebib-mark-entry))))))))
1710 ((default)
1711 (beep))))
1713 (defun ebib-mark-entry ()
1714 "Mark (or unmark) the current entry."
1715 (interactive)
1716 (if (ebib-called-with-prefix)
1717 (ebib-execute-when
1718 ((marked-entries)
1719 (setf (edb-marked-entries ebib-cur-db) nil)
1720 (ebib-fill-index-buffer))
1721 ((entries)
1722 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
1723 (ebib-fill-index-buffer))
1724 ((default)
1725 (beep)))
1726 (ebib-execute-when
1727 ((entries)
1728 (set-buffer ebib-index-buffer)
1729 (with-buffer-writable
1730 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
1731 (progn
1732 (setf (edb-marked-entries ebib-cur-db)
1733 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
1734 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
1735 (ebib-highlight-end ebib-index-highlight)
1736 '(face ,ebib-marked-face)))
1737 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
1738 (edb-marked-entries ebib-cur-db))
1739 'string<))
1740 (add-text-properties (ebib-highlight-start ebib-index-highlight)
1741 (ebib-highlight-end ebib-index-highlight)
1742 `(face ,ebib-marked-face)))))
1743 ((default)
1744 (beep)))))
1746 (defun ebib-index-scroll-down ()
1747 "Move one page up in the database."
1748 (interactive)
1749 (ebib-execute-when
1750 ((entries)
1751 (scroll-down)
1752 (ebib-select-entry))
1753 ((default)
1754 (beep))))
1756 (defun ebib-index-scroll-up ()
1757 "Move one page down in the database."
1758 (interactive)
1759 (ebib-execute-when
1760 ((entries)
1761 (scroll-up)
1762 (ebib-select-entry))
1763 ((default)
1764 (beep))))
1766 (defun ebib-format-entry (key db timestamp)
1767 "Format entry KEY from database DB into the current buffer in BibTeX format.
1769 If TIMESTAMP is T, a timestamp is added to the entry if EBIB-USE-TIMESTAMP is T."
1770 (let ((entry (ebib-retrieve-entry key db)))
1771 (when entry
1772 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
1773 (maphash '(lambda (key value)
1774 (unless (or (eq key 'type*)
1775 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
1776 (insert (format "\t%s = %s,\n" key value))))
1777 entry)
1778 (if (and timestamp ebib-use-timestamp)
1779 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
1780 (delete-char -2)) ; the final ",\n" must be deleted
1781 (insert "\n}\n\n"))))
1783 (defun ebib-format-strings (db)
1784 "Format the @STRING commands in database DB."
1785 (maphash '(lambda (key value)
1786 (insert (format "@STRING{%s = %s}\n" key value)))
1787 (edb-strings db))
1788 (insert "\n"))
1790 (defun ebib-compare-xrefs (x y)
1791 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
1793 (defun ebib-format-database (db)
1794 "Writes database DB into the current buffer in BibTeX format."
1795 (when (edb-preamble db)
1796 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
1797 (ebib-format-strings db)
1798 (let ((sorted-list (copy-list (edb-keys-list db))))
1799 (cond
1800 (ebib-save-xrefs-first
1801 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
1802 (ebib-sort-order
1803 (setq sorted-list (sort sorted-list 'ebib-entry<))))
1804 (mapc '(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
1806 (defun ebib-save-database (db)
1807 "Saves the database DB."
1808 (ebib-execute-when
1809 ((real-db)
1810 (when (and (edb-make-backup db)
1811 (file-exists-p (edb-filename db)))
1812 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
1813 (setf (edb-make-backup db) nil))
1814 (with-temp-buffer
1815 (ebib-format-database db)
1816 (write-region (point-min) (point-max) (edb-filename db)))
1817 (ebib-set-modified nil db))))
1819 (defun ebib-write-database ()
1820 "Writes the current database to a different file.
1822 Can also be used to change a virtual database into a real one."
1823 (interactive)
1824 (ebib-execute-when
1825 ((database)
1826 (if-str (new-filename (read-file-name "Save to file: " "~/"))
1827 (progn
1828 (with-temp-buffer
1829 (ebib-format-database ebib-cur-db)
1830 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
1831 ;; if SAFE-WRITE-REGION was cancelled by the user because he
1832 ;; didn't want to overwrite an already existing file with his
1833 ;; new database, it throws an error, so the next lines will not
1834 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
1835 ;; (EDB-NAME DB).
1836 (setf (edb-filename ebib-cur-db) new-filename)
1837 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
1838 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1839 (length (member ebib-cur-db ebib-databases)))))
1840 (edb-name ebib-cur-db)))
1841 (ebib-execute-when
1842 ((virtual-db)
1843 (setf (edb-virtual ebib-cur-db) nil)
1844 (setf (edb-database ebib-cur-db)
1845 (let ((new-db (make-hash-table :test 'equal)))
1846 (mapc '(lambda (key)
1847 (let ((entry (gethash key (edb-database ebib-cur-db))))
1848 (when entry
1849 (puthash key (copy-hash-table entry) new-db))))
1850 (edb-keys-list ebib-cur-db))
1851 new-db))))
1852 (ebib-set-modified nil))))
1853 ((default)
1854 (beep))))
1856 (defun ebib-save-current-database ()
1857 "Saves the current database."
1858 (interactive)
1859 (ebib-execute-when
1860 ((real-db)
1861 (if (not (edb-modified ebib-cur-db))
1862 (message "No changes need to be saved.")
1863 (ebib-save-database ebib-cur-db)))
1864 ((virtual-db)
1865 (error "Cannot save a virtual database. Use `w' to write to a file."))))
1867 (defun ebib-save-all-databases ()
1868 "Saves all currently open databases if they were modified."
1869 (interactive)
1870 (ebib-execute-when
1871 ((database)
1872 (mapc #'(lambda (db)
1873 (when (edb-modified db)
1874 (ebib-save-database db)))
1875 ebib-databases)
1876 (message "All databases saved."))))
1878 (defun ebib-print-filename ()
1879 "Displays the filename of the current database in the minibuffer."
1880 (interactive)
1881 (message (edb-filename ebib-cur-db)))
1883 (defun ebib-follow-crossref ()
1884 "Goes to the entry mentioned in the crossref field of the current entry."
1885 (interactive)
1886 (let ((new-cur-entry (to-raw (gethash 'crossref
1887 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
1888 (setf (edb-cur-entry ebib-cur-db)
1889 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
1890 (edb-cur-entry ebib-cur-db))))
1891 (ebib-fill-entry-buffer)
1892 (ebib-fill-index-buffer))
1894 (defun ebib-toggle-hidden ()
1895 (interactive)
1896 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
1897 (ebib-fill-entry-buffer))
1899 (defun ebib-delete-entry ()
1900 "Deletes the current entry from the database."
1901 (interactive)
1902 (if (ebib-called-with-prefix)
1903 (ebib-execute-when
1904 ((real-db marked-entries)
1905 (when (y-or-n-p "Delete all marked entries? ")
1906 (mapc '(lambda (entry)
1907 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
1908 (edb-marked-entries ebib-cur-db))
1909 (message "Marked entries deleted.")
1910 (ebib-set-modified t)
1911 (ebib-fill-entry-buffer)
1912 (ebib-fill-index-buffer)))
1913 ((default)
1914 (beep)))
1915 (ebib-execute-when
1916 ((real-db entries)
1917 (let ((cur-entry (ebib-cur-entry-key)))
1918 (when (y-or-n-p (format "Delete %s? " cur-entry))
1919 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
1920 (ebib-remove-key-from-buffer cur-entry)
1921 (ebib-fill-entry-buffer)
1922 (ebib-set-modified t)
1923 (message (format "Entry `%s' deleted." cur-entry)))))
1924 ((default)
1925 (beep)))))
1927 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
1928 "Removes ENTRY-KEY from DB.
1930 Optional argument NEW-CUR-ENTRY is the key of the entry that is to become
1931 the new current entry. It it is NIL, the entry after the deleted one
1932 becomes the new current entry. If it is T, the current entry is not
1933 changed."
1934 (remhash entry-key (edb-database db))
1935 (setf (edb-n-entries db) (1- (edb-n-entries db)))
1936 (cond
1937 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
1938 ((stringp new-cur-entry) t)
1939 (t (setq new-cur-entry (ebib-cur-entry-key))))
1940 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
1941 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
1942 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
1943 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
1944 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
1946 (defun ebib-remove-key-from-buffer (entry-key)
1947 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
1948 (with-buffer-writable
1949 (let ((beg (ebib-search-key-in-buffer entry-key)))
1950 (forward-line 1)
1951 (delete-region beg (point))))
1952 (ebib-execute-when
1953 ((entries)
1954 (ebib-search-key-in-buffer (ebib-cur-entry-key))
1955 (ebib-set-index-highlight))))
1957 (defun ebib-select-entry ()
1958 "Makes the entry at (point) the current entry."
1959 (interactive)
1960 (ebib-execute-when
1961 ((entries)
1962 (beginning-of-line)
1963 (let ((beg (point)))
1964 (let* ((key (save-excursion
1965 (end-of-line)
1966 (buffer-substring-no-properties beg (point))))
1967 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
1968 (when new-cur-entry
1969 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
1970 (ebib-set-index-highlight)
1971 (ebib-fill-entry-buffer)))))
1972 ((default)
1973 (beep))))
1975 (defun ebib-export-entry (prefix)
1976 "Copies entries to another database.
1978 The prefix argument indicates which database to copy the entry
1979 to. If no prefix argument is present, a filename is asked to
1980 which the entry is appended."
1981 (interactive "P")
1982 (let ((num (ebib-prefix prefix)))
1983 (if (ebib-called-with-prefix)
1984 (ebib-export-marked-entries num)
1985 (ebib-export-single-entry num))))
1987 (defun ebib-export-single-entry (num)
1988 "Copies the current entry to another database.
1990 NUM indicates which database to copy the entry to. If it is NIL,
1991 a filename is asked to which the entry is appended."
1992 (ebib-execute-when
1993 ((real-db entries)
1994 (if num
1995 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
1996 '(lambda (db)
1997 (let ((entry-key (ebib-cur-entry-key)))
1998 (if (member entry-key (edb-keys-list db))
1999 (error "Entry key `%s' already exists in database %d" entry-key num)
2000 (ebib-insert-entry entry-key
2001 (copy-hash-table (ebib-retrieve-entry entry-key
2002 ebib-cur-db))
2003 db t t)
2004 ;; if this is the first entry in the target DB,
2005 ;; its CUR-ENTRY must be set!
2006 (when (null (edb-cur-entry db))
2007 (setf (edb-cur-entry db) (edb-keys-list db)))
2008 t)))) ; we must return T, WHEN does not always do this.
2009 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2010 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2011 '(lambda ()
2012 (insert "\n")
2013 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2014 ((default)
2015 (beep))))
2017 (defun ebib-export-marked-entries (num)
2018 "Copies the marked entries to another database.
2020 NUM indicates which database to copy the entry to. If it is NIL,
2021 a filename is asked to which the entry is appended."
2022 (ebib-execute-when
2023 ((real-db marked-entries)
2024 (if num
2025 (ebib-export-to-db
2026 num "Entries copied to database %d."
2027 '(lambda (db)
2028 (mapc '(lambda (entry-key)
2029 (if (member entry-key (edb-keys-list db))
2030 (error "Entry key `%s' already exists in database %d" entry-key num)
2031 (ebib-insert-entry entry-key
2032 (copy-hash-table (ebib-retrieve-entry entry-key
2033 ebib-cur-db))
2034 db t t)))
2035 (edb-marked-entries ebib-cur-db))
2036 ;; if the target DB was empty before, its CUR-ENTRY must be set!
2037 (when (null (edb-cur-entry db))
2038 (setf (edb-cur-entry db) (edb-keys-list db)))
2039 t)) ; we must return T, WHEN does not always do this.
2040 (ebib-export-to-file "Export to file: " "Entries exported to %s."
2041 '(lambda ()
2042 (mapc '(lambda (entry-key)
2043 (insert "\n")
2044 (ebib-format-entry entry-key ebib-cur-db t))
2045 (edb-marked-entries ebib-cur-db))))))
2046 ((default)
2047 (beep))))
2050 (defun ebib-search ()
2051 "Search the current Ebib database.
2053 The search is conducted with STRING-MATCH and can therefore be a regexp.
2054 Searching starts with the current entry."
2055 (interactive)
2056 (ebib-execute-when
2057 ((entries)
2058 (if-str (search-str (read-string "Search database for: "))
2059 (progn
2060 (setq ebib-search-string search-str)
2061 ;; first we search the current entry
2062 (if (ebib-search-in-entry ebib-search-string
2063 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2064 (ebib-fill-entry-buffer ebib-search-string)
2065 ;; if the search string wasn't found in the current entry, we continue searching.
2066 (ebib-search-next)))))
2067 ((default)
2068 (beep))))
2070 (defun ebib-search-next ()
2071 "Search the next occurrence of EBIB-SEARCH-STRING.
2073 Searching starts at the entry following the current entry. If a match is
2074 found, the matching entry is shown and becomes the new current entry."
2075 (interactive)
2076 (ebib-execute-when
2077 ((entries)
2078 (if (null ebib-search-string)
2079 (message "No search string")
2080 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2081 (while (and cur-search-entry
2082 (null (ebib-search-in-entry ebib-search-string
2083 (gethash (car cur-search-entry)
2084 (edb-database ebib-cur-db)))))
2085 (setq cur-search-entry (cdr cur-search-entry)))
2086 (if (null cur-search-entry)
2087 (message (format "`%s' not found" ebib-search-string))
2088 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2089 (set-buffer ebib-index-buffer)
2090 (goto-char (point-min))
2091 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
2092 (beginning-of-line)
2093 (ebib-set-index-highlight)
2094 (ebib-fill-entry-buffer ebib-search-string)))))
2095 ((default)
2096 (beep))))
2098 (defun ebib-search-in-entry (search-str entry &optional field)
2099 "Searches one entry of the ebib database.
2101 Returns a list of fields in ENTRY that match the regexp SEARCH-STR,
2102 or NIL if no matches were found. If FIELD is given, only that
2103 field is searched."
2104 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2105 (result nil))
2106 (if field
2107 (let ((value (gethash field entry)))
2108 (when (and (stringp value) ; the type* field has a symbol as value
2109 (string-match search-str value))
2110 (setq result (list field))))
2111 (maphash '(lambda (field value)
2112 (when (and (stringp value) ; the type* field has a symbol as value
2113 (string-match search-str value))
2114 (setq result (cons field result))))
2115 entry))
2116 result))
2118 (defun ebib-edit-strings ()
2119 "Edits the @STRING definitions in the database."
2120 (interactive)
2121 (ebib-execute-when
2122 ((real-db)
2123 (ebib-fill-strings-buffer)
2124 (other-window 1)
2125 (switch-to-buffer ebib-strings-buffer)
2126 (goto-char (point-min)))
2127 ((default)
2128 (beep))))
2130 (defun ebib-edit-preamble ()
2131 "Edits the @PREAMBLE definition in the database."
2132 (interactive)
2133 (ebib-execute-when
2134 ((real-db)
2135 (other-window 1) ; we want the multiline edit buffer to appear in the lower window
2136 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2137 ((default)
2138 (beep))))
2140 (defun ebib-export-preamble (prefix)
2141 "Export the @PREAMBLE definition.
2143 If a prefix argument is given, it is taken as the database to
2144 export the preamble to. If the goal database already has a
2145 preamble, the new preamble will be appended to it. If no prefix
2146 argument is given, the user is asked to enter a filename to which
2147 the preamble is appended."
2148 (interactive "P")
2149 (ebib-execute-when
2150 ((real-db)
2151 (if (null (edb-preamble ebib-cur-db))
2152 (error "No @PREAMBLE defined")
2153 (let ((num (ebib-prefix prefix)))
2154 (if num
2155 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2156 '(lambda (db)
2157 (let ((text (edb-preamble ebib-cur-db)))
2158 (if (edb-preamble db)
2159 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2160 (setf (edb-preamble db) text)))))
2161 (ebib-export-to-file "Export @PREAMBLE to file: "
2162 "@PREAMBLE exported to %s"
2163 '(lambda ()
2164 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2165 ((default)
2166 (beep))))
2168 (defun ebib-print-entries ()
2169 "Creates a LaTeX file listing the entries.
2171 Either prints the entire database, or the marked entries."
2172 (interactive)
2173 (ebib-execute-when
2174 ((entries)
2175 (let ((entries (if (ebib-called-with-prefix)
2176 (edb-marked-entries ebib-cur-db)
2177 (edb-keys-list ebib-cur-db))))
2178 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2179 ebib-print-tempfile
2180 (read-file-name "Use temp file: " "~/" nil nil)))
2181 (progn
2182 (with-temp-buffer
2183 (insert "\\documentclass{article}\n\n")
2184 (when ebib-print-preamble
2185 (mapc '(lambda (string)
2186 (insert (format "%s\n" string)))
2187 ebib-print-preamble))
2188 (insert "\n\\begin{document}\n\n")
2189 (mapc '(lambda (entry-key)
2190 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2191 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2192 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2193 entry-key (symbol-name (gethash 'type* entry))))
2194 (insert "\\hline\n")
2195 (mapc '(lambda (field)
2196 (if-str (value (gethash field entry))
2197 (when (or (not (multiline-p value))
2198 ebib-print-multiline)
2199 (insert (format "%s: & %s\\\\\n"
2200 field (to-raw value))))))
2201 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2202 (insert "\\end{tabular}\n\n")
2203 (insert "\\bigskip\n\n"))
2204 entries)
2205 (insert "\\end{document}\n")
2206 (write-region (point-min) (point-max) tempfile))
2207 (ebib-lower)
2208 (find-file tempfile)))))
2209 ((default)
2210 (beep))))
2212 (defun ebib-latex-entries ()
2213 "Creates a LaTeX file that \\nocite's entries from the database.
2215 Operates either on all entries or on the marked entries."
2216 (interactive)
2217 (ebib-execute-when
2218 ((real-db entries)
2219 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2220 ebib-print-tempfile
2221 (read-file-name "Use temp file: " "~/" nil nil)))
2222 (progn
2223 (with-temp-buffer
2224 (insert "\\documentclass{article}\n\n")
2225 (when ebib-print-preamble
2226 (mapc '(lambda (string)
2227 (insert (format "%s\n" string)))
2228 ebib-latex-preamble))
2229 (insert "\n\\begin{document}\n\n")
2230 (if (ebib-called-with-prefix)
2231 (mapc '(lambda (entry)
2232 (insert (format "\\nocite{%s}\n" entry)))
2233 (edb-marked-entries ebib-cur-db))
2234 (insert "\\nocite{*}\n"))
2235 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2236 (insert "\\end{document}\n")
2237 (write-region (point-min) (point-max) tempfile))
2238 (ebib-lower)
2239 (find-file tempfile))))
2240 ((default)
2241 (beep))))
2243 (defun ebib-switch-to-database (num)
2244 (interactive "NSwitch to database number: ")
2245 (let ((new-db (nth (1- num) ebib-databases)))
2246 (if new-db
2247 (progn
2248 (setq ebib-cur-db new-db)
2249 (ebib-fill-entry-buffer)
2250 (ebib-fill-index-buffer))
2251 (error "Database %d does not exist" num))))
2253 (defun ebib-next-database ()
2254 (interactive)
2255 (ebib-execute-when
2256 ((database)
2257 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2258 (unless new-db
2259 (setq new-db (car ebib-databases)))
2260 (setq ebib-cur-db new-db)
2261 (ebib-fill-entry-buffer)
2262 (ebib-fill-index-buffer)))))
2264 (defun ebib-prev-database ()
2265 (interactive)
2266 (ebib-execute-when
2267 ((database)
2268 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2269 (unless new-db
2270 (setq new-db (last1 ebib-databases)))
2271 (setq ebib-cur-db new-db)
2272 (ebib-fill-entry-buffer)
2273 (ebib-fill-index-buffer)))))
2275 (defun ebib-browse-url (num)
2276 "Ask a browser to load the URL in the standard URL field.
2278 The standard URL field may contain more than one URL, if they're
2279 whitespace-separated. In that case, a numeric prefix argument
2280 specifies which URL to choose.
2282 By \"standard URL field\" is meant the field defined in the
2283 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2284 is `url'."
2285 (interactive "p")
2286 (ebib-execute-when
2287 ((entries)
2288 (let ((url (to-raw (gethash ebib-standard-url-field
2289 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2290 (when (not (and url
2291 (ebib-call-browser url num)))
2292 (error "No url found in field `%s'" ebib-standard-url-field))))
2293 ((default)
2294 (beep))))
2296 (defun ebib-call-browser (urls n)
2297 "Passes the Nth url in URLS to a browser.
2299 URLs must be a string of whitespace-separated urls."
2300 (let ((url (nth (1- n)
2301 (let ((start 0)
2302 (result nil))
2303 (while (string-match ebib-url-regexp urls start)
2304 (add-to-list 'result (match-string 0 urls) t)
2305 (setq start (match-end 0)))
2306 result))))
2307 (cond
2308 ((string-match "\\\\url{\\(.*?\\)}" url)
2309 (setq url (match-string 1 url)))
2310 ((string-match ebib-url-regexp url)
2311 (setq url (match-string 0 url)))
2312 (t (setq url nil)))
2313 (when url
2314 (if (not (string= ebib-browser-command ""))
2315 (start-process "Ebib-browser" nil ebib-browser-command url)
2316 (browse-url url)))))
2318 (defun ebib-virtual-db-and (not)
2319 "Filter entries into a virtual database.
2321 If the current database is a virtual database already, perform a
2322 logical AND on the entries."
2323 (interactive "p")
2324 (ebib-execute-when
2325 ((entries)
2326 (ebib-filter-to-virtual-db 'and not))
2327 ((default)
2328 (beep))))
2330 (defun ebib-virtual-db-or (not)
2331 "Filter entries into a virtual database.
2333 If the current database is a virtual database already, perform a
2334 logical OR with the entries in the original database."
2335 (interactive "p")
2336 (ebib-execute-when
2337 ((entries)
2338 (ebib-filter-to-virtual-db 'or not))
2339 ((default)
2340 (beep))))
2342 (defun ebib-virtual-db-not ()
2343 "Negates the current virtual database."
2344 (interactive)
2345 (ebib-execute-when
2346 ((virtual-db)
2347 (setf (edb-virtual ebib-cur-db)
2348 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2349 (cadr (edb-virtual ebib-cur-db))
2350 `(not ,(edb-virtual ebib-cur-db))))
2351 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2352 (ebib-fill-entry-buffer)
2353 (ebib-fill-index-buffer))
2354 ((default)
2355 (beep))))
2357 (defun ebib-filter-to-virtual-db (bool not)
2358 "Filters the current database to a virtual database.
2360 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2361 a logical `not' is applied to the selection."
2362 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2363 (if (< not 0) "(not " "")
2364 (if (< not 0) ")" ""))
2365 (cons '("any" 0)
2366 (mapcar '(lambda (x)
2367 (cons (symbol-name x) 0))
2368 (append ebib-unique-field-list ebib-additional-fields)))
2369 nil t)))
2370 (setq field (intern-soft field))
2371 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2372 (if (< not 0) "(not " "")
2373 field
2374 (if (< not 0) ")" "")))))
2375 (ebib-execute-when
2376 ((virtual-db)
2377 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2378 ,(if (>= not 0)
2379 `(contains ,field ,regexp)
2380 `(not (contains ,field ,regexp))))))
2381 ((real-db)
2382 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2383 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2384 (ebib-fill-entry-buffer)
2385 (ebib-fill-index-buffer))))
2387 (defun ebib-create-virtual-db (field regexp not)
2388 "Creates a virtual database based on EBIB-CUR-DB."
2389 ;; a virtual database is a database whose edb-virtual field contains an
2390 ;; expression that selects entries. this function only sets that
2391 ;; expression, it does not actually filter the entries.
2392 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2393 (setf (edb-virtual new-db) (if (>= not 0)
2394 `(contains ,field ,regexp)
2395 `(not (contains ,field ,regexp))))
2396 (setf (edb-filename new-db) nil)
2397 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2398 (setf (edb-modified new-db) nil)
2399 (setf (edb-make-backup new-db) nil)
2400 new-db))
2402 (defmacro contains (field regexp)
2403 ;; This is a hack: CONTAINS depends on the variable ENTRY being set to an
2404 ;; actual Ebib entry for its operation. The point of this macro is to
2405 ;; facilitate defining filters for virtual databases. It enables us to
2406 ;; define filters of the form:
2408 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2410 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2412 (defun ebib-run-filter (filter db)
2413 "Run FILTER on DB"
2414 (setf (edb-keys-list db)
2415 (sort (let ((result nil))
2416 (maphash '(lambda (key value)
2417 (let ((entry value)) ; this is necessary for actually running the filter
2418 (when (eval filter)
2419 (setq result (cons key result)))))
2420 (edb-database db))
2421 result)
2422 'string<))
2423 (setf (edb-n-entries db) (length (edb-keys-list db)))
2424 (setf (edb-cur-entry db) (edb-keys-list db)))
2426 (defun ebib-print-filter (num)
2427 "Display the filter of the current virtual database.
2429 With any prefix argument, reapplies the filter to the
2430 database. This can be useful when the source database was
2431 modified."
2432 (interactive "P")
2433 (ebib-execute-when
2434 ((virtual-db)
2435 (when num
2436 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2437 (ebib-fill-entry-buffer)
2438 (ebib-fill-index-buffer))
2439 (message "%S" (edb-virtual ebib-cur-db)))
2440 ((default)
2441 (beep))))
2443 (defun ebib-index-help ()
2444 "Displays the help message for the index buffer."
2445 (interactive)
2446 (other-window 1)
2447 (ebib-display-help ebib-index-buffer))
2449 (defun ebib-push-entry-key (prefix)
2450 "Pushes the current entry to a LaTeX buffer.
2452 The user is prompted for the buffer to push the entry into."
2453 (interactive "p")
2454 (let ((called-with-prefix (ebib-called-with-prefix)))
2455 (ebib-execute-when
2456 ((entries)
2457 (let ((buffer (read-buffer "Push entry(ies) to buffer: " ebib-push-buffer t)))
2458 (when buffer
2459 (setq ebib-push-buffer buffer)
2460 (let ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
2461 "{%s}"))
2462 (latex-arg (if called-with-prefix
2463 (when (edb-marked-entries ebib-cur-db)
2464 (mapconcat #'(lambda (x) x)
2465 (edb-marked-entries ebib-cur-db)
2466 ","))
2467 (car (edb-cur-entry ebib-cur-db)))))
2468 (save-excursion
2469 (set-buffer buffer)
2470 (insert (format latex-cmd latex-arg)))
2471 (message "Pushed entries to buffer %s" buffer)))))
2472 ((default)
2473 (beep)))))
2475 ;;;;;;;;;;;;;;;;
2476 ;; entry-mode ;;
2477 ;;;;;;;;;;;;;;;;
2479 (defvar ebib-entry-mode-map
2480 (let ((map (make-keymap)))
2481 (suppress-keymap map)
2482 (define-key map [up] 'ebib-prev-field)
2483 (define-key map [down] 'ebib-next-field)
2484 (define-key map [prior] 'ebib-goto-prev-set)
2485 (define-key map [next] 'ebib-goto-next-set)
2486 (define-key map [home] 'ebib-goto-first-field)
2487 (define-key map [end] 'ebib-goto-last-field)
2488 (define-key map " " 'ebib-goto-next-set)
2489 (define-key map "b" 'ebib-goto-prev-set)
2490 (define-key map "c" 'ebib-copy-field-contents)
2491 (define-key map "d" 'ebib-delete-field-contents)
2492 (define-key map "e" 'ebib-edit-field)
2493 (define-key map "g" 'ebib-goto-first-field)
2494 (define-key map "G" 'ebib-goto-last-field)
2495 (define-key map "h" 'ebib-entry-help)
2496 (define-key map "j" 'ebib-next-field)
2497 (define-key map "k" 'ebib-prev-field)
2498 (define-key map "l" 'ebib-edit-multiline-field)
2499 (define-key map [(control n)] 'ebib-next-field)
2500 (define-key map [(meta n)] 'ebib-goto-prev-set)
2501 (define-key map [(control p)] 'ebib-prev-field)
2502 (define-key map [(meta p)] 'ebib-goto-next-set)
2503 (define-key map "q" 'ebib-quit-entry-buffer)
2504 (define-key map "r" 'ebib-toggle-raw)
2505 (define-key map "s" 'ebib-insert-abbreviation)
2506 (define-key map "u" 'ebib-browse-url-in-field)
2507 (define-key map "x" 'ebib-cut-field-contents)
2508 (define-key map "\C-xb" 'undefined)
2509 (define-key map "\C-xk" 'undefined)
2510 (define-key map "y" 'ebib-yank-field-contents)
2511 map)
2512 "Keymap for the Ebib entry buffer.")
2514 (define-derived-mode ebib-entry-mode
2515 fundamental-mode "Ebib-entry"
2516 "Major mode for the Ebib entry buffer."
2517 (setq buffer-read-only t)
2518 (setq truncate-lines t))
2520 (defun ebib-quit-entry-buffer ()
2521 "Quit editing the entry."
2522 (interactive)
2523 (other-window 1))
2525 (defun ebib-find-visible-field (field direction)
2526 "Finds the first visible field before or after FIELD.
2528 If DIRECTION is negative, search the preceding fields, otherwise
2529 search the succeeding fields. If FIELD is visible itself, return
2530 that. If there is no preceding/following visible field, return
2531 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
2532 (when ebib-hide-hidden-fields
2533 (let ((fn (if (>= direction 0)
2534 'next-elem
2535 'prev-elem)))
2536 (while (and field
2537 (get field 'ebib-hidden))
2538 (setq field (funcall fn field ebib-cur-entry-fields)))))
2539 field)
2541 (defun ebib-prev-field ()
2542 "Move to the previous field."
2543 (interactive)
2544 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
2545 (if (null new-field)
2546 (beep)
2547 (setq ebib-current-field new-field)
2548 (ebib-move-to-field ebib-current-field -1))))
2550 (defun ebib-next-field ()
2551 "Move to the next field."
2552 (interactive)
2553 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
2554 (if (null new-field)
2555 (when (interactive-p) ; i call this function after editing a field,
2556 ; and we don't want a beep then
2557 (beep))
2558 (setq ebib-current-field new-field)
2559 (ebib-move-to-field ebib-current-field 1))))
2561 (defun ebib-goto-first-field ()
2562 "Move to the first field."
2563 (interactive)
2564 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
2565 (if (null new-field)
2566 (beep)
2567 (setq ebib-current-field new-field)
2568 (ebib-move-to-field ebib-current-field -1))))
2570 (defun ebib-goto-last-field ()
2571 "Move to the last field."
2572 (interactive)
2573 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
2574 (if (null new-field)
2575 (beep)
2576 (setq ebib-current-field new-field)
2577 (ebib-move-to-field ebib-current-field 1))))
2579 (defun ebib-goto-next-set ()
2580 "Move to the next set of fields."
2581 (interactive)
2582 (cond
2583 ((eq ebib-current-field 'type*) (ebib-next-field))
2584 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
2585 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2586 (obl-fields (ebib-get-obl-fields entry-type))
2587 (opt-fields (ebib-get-opt-fields entry-type))
2588 (new-field nil))
2589 (when (member ebib-current-field obl-fields)
2590 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
2591 ;; new-field is nil if there are no opt-fields
2592 (when (or (member ebib-current-field opt-fields)
2593 (null new-field))
2594 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
2595 (if (null new-field)
2596 (ebib-goto-last-field) ; if there was no further set to go to,
2597 ; go to the last field of the current set
2598 (setq ebib-current-field new-field)
2599 (ebib-move-to-field ebib-current-field 1))))))
2601 (defun ebib-goto-prev-set ()
2602 "Move to the previous set of fields."
2603 (interactive)
2604 (unless (eq ebib-current-field 'type*)
2605 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2606 (obl-fields (ebib-get-obl-fields entry-type))
2607 (opt-fields (ebib-get-opt-fields entry-type))
2608 (new-field nil))
2609 (if (member ebib-current-field obl-fields)
2610 (ebib-goto-first-field)
2611 (when (member ebib-current-field ebib-additional-fields)
2612 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
2613 (when (or (member ebib-current-field opt-fields)
2614 (null new-field))
2615 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
2616 (if (null new-field)
2617 (ebib-goto-first-field)
2618 (setq ebib-current-field new-field)
2619 (ebib-move-to-field ebib-current-field -1))))))
2621 (defun ebib-edit-entry-type ()
2622 "Edits the type of an entry."
2623 ;; we want to put the completion buffer in the lower window. for this
2624 ;; reason, we need to switch to the other window before calling
2625 ;; completing-read. but in order to make sure that we return to the
2626 ;; entry buffer and not the index buffer when the user presses C-g, we
2627 ;; need to do this in an unwind-protect.
2628 (unwind-protect
2629 (progn
2630 (other-window 1)
2631 (let ((collection (ebib-create-collection ebib-entry-types-hash)))
2632 (if-str (new-type (completing-read "type: " collection nil t))
2633 (progn
2634 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
2635 (ebib-fill-entry-buffer)
2636 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2637 (ebib-set-modified t)))))
2638 (other-window 1)))
2640 (defun ebib-edit-crossref ()
2641 "Edits the crossref field."
2642 (unwind-protect
2643 (progn
2644 (other-window 1)
2645 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
2646 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
2647 (progn
2648 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
2649 (ebib-set-modified t)))))
2650 (other-window 1)
2651 ;; we now redisplay the entire entry buffer, so that the crossref'ed
2652 ;; fields show up. this also puts the cursor back on the type field.
2653 (ebib-fill-entry-buffer)))
2655 ;; we should modify ebib-edit-field, so that it calls the appropriate
2656 ;; helper function, which asks the user for the new value and just returns
2657 ;; that. storing it should then be done by ebib-edit-field, no matter what
2658 ;; sort of field the user edits.
2660 (defun ebib-edit-field ()
2661 "Edits a field of a BibTeX entry."
2662 (interactive)
2663 (cond
2664 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
2665 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
2666 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
2668 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
2669 (raw nil))
2670 (if (multiline-p init-contents)
2671 (ebib-edit-multiline-field)
2672 (when init-contents
2673 (if (raw-p init-contents)
2674 (setq raw t)
2675 (setq init-contents (to-raw init-contents))))
2676 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
2677 (if init-contents
2678 (cons init-contents 0)
2679 nil)
2680 ebib-minibuf-hist))
2681 (puthash ebib-current-field (if raw
2682 new-contents
2683 (concat "{" new-contents "}"))
2684 ebib-cur-entry-hash)
2685 (remhash ebib-current-field ebib-cur-entry-hash))
2686 (ebib-redisplay-current-field)
2687 ;; we move to the next field, but only if ebib-edit-field was
2688 ;; called interactively, otherwise we get a strange bug in
2689 ;; ebib-toggle-raw...
2690 (if (interactive-p) (ebib-next-field))
2691 (ebib-set-modified t))))))
2693 (defun ebib-browse-url-in-field (num)
2694 "Browse the url in the current field.
2696 The field may contain a whitespace-separated set of urls. The
2697 prefix argument indicates which url is to be sent to the
2698 browser."
2699 (interactive "p")
2700 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2701 (if (not (and urls
2702 (ebib-call-browser urls num)))
2703 (error "No url found in field `%s'" ebib-current-field))))
2705 (defun ebib-copy-field-contents ()
2706 "Copies the contents of the current field to the kill ring."
2707 (interactive)
2708 (unless (eq ebib-current-field 'type*)
2709 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2710 (when (stringp contents)
2711 (kill-new contents)
2712 (message "Field contents copied.")))))
2714 (defun ebib-cut-field-contents ()
2715 "Kills the contents of the current field. The killed text is put in the kill ring."
2716 (interactive)
2717 (unless (eq ebib-current-field 'type*)
2718 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2719 (when (stringp contents)
2720 (remhash ebib-current-field ebib-cur-entry-hash)
2721 (kill-new contents)
2722 (ebib-redisplay-current-field)
2723 (ebib-set-modified t)
2724 (message "Field contents killed.")))))
2726 (defun ebib-yank-field-contents (arg)
2727 "Inserts the last killed text into the current field.
2729 If the current field already has a contents, nothing is inserted,
2730 unless the previous command was also ebib-yank-field-contents,
2731 then the field contents is replaced with the previous yank. That
2732 is, multiple use of this command functions like the combination
2733 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
2734 (interactive "P")
2735 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
2736 (eq ebib-current-field 'crossref)
2737 (unless (eq last-command 'ebib-yank-field-contents)
2738 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
2739 (progn
2740 (setq this-command t)
2741 (beep))
2742 (let ((new-contents (current-kill (cond
2743 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
2746 ((eq arg '-) -2)
2747 (t (1- arg))))))
2748 (when new-contents
2749 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
2750 (ebib-redisplay-current-field)
2751 (ebib-set-modified t)))))
2753 (defun ebib-delete-field-contents ()
2754 "Deletes the contents of the current field. The deleted text is not put
2755 in the kill ring."
2756 (interactive)
2757 (if (eq ebib-current-field 'type*)
2758 (beep)
2759 (remhash ebib-current-field ebib-cur-entry-hash)
2760 (ebib-redisplay-current-field)
2761 (ebib-set-modified t)
2762 (message "Field contents deleted.")))
2764 (defun ebib-toggle-raw ()
2765 "Toggles the raw status of the current field contents."
2766 (interactive)
2767 (unless (or (eq ebib-current-field 'type*)
2768 (eq ebib-current-field 'crossref))
2769 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2770 (if (not contents) ; if there is no value,
2771 (progn
2772 (ebib-edit-field) ; the user can enter one, which we must then make raw
2773 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
2774 (when new-contents
2775 ;; note: we don't have to check for empty string, since that is
2776 ;; already done in ebib-edit-field
2777 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
2778 (if (raw-p contents)
2779 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
2780 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
2781 (ebib-redisplay-current-field)
2782 (ebib-set-modified t))))
2784 (defun ebib-edit-multiline-field ()
2785 "Edits the current field in multiline-mode."
2786 (interactive)
2787 (unless (or (eq ebib-current-field 'type*)
2788 (eq ebib-current-field 'crossref))
2789 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
2790 (if (raw-p text)
2791 (setq ebib-multiline-raw t)
2792 (setq text (to-raw text))
2793 (setq ebib-multiline-raw nil))
2794 (ebib-multiline-edit 'fields text))))
2796 (defun ebib-insert-abbreviation ()
2797 "Insert an abbreviation from the ones defined in the database."
2798 (interactive)
2799 (if (gethash ebib-current-field ebib-cur-entry-hash)
2800 (beep)
2801 (when (edb-strings-list ebib-cur-db)
2802 (unwind-protect
2803 (progn
2804 (other-window 1)
2805 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
2806 (string (completing-read "Abbreviation to insert: " collection nil t)))
2807 (when string
2808 (puthash ebib-current-field string ebib-cur-entry-hash)
2809 (ebib-set-modified t))))
2810 (other-window 1)
2811 ;; we can't do this earlier, because we would be writing to the index buffer...
2812 (ebib-redisplay-current-field)
2813 (ebib-next-field)))))
2815 (defun ebib-entry-help ()
2816 "Displays the help message for the entry buffer."
2817 (interactive)
2818 (ebib-display-help ebib-entry-buffer))
2820 ;;;;;;;;;;;;;;;;;;
2821 ;; strings-mode ;;
2822 ;;;;;;;;;;;;;;;;;;
2824 (defvar ebib-strings-mode-map
2825 (let ((map (make-keymap)))
2826 (suppress-keymap map)
2827 (define-key map [up] 'ebib-prev-string)
2828 (define-key map [down] 'ebib-next-string)
2829 (define-key map [prior] 'ebib-strings-page-up)
2830 (define-key map [next] 'ebib-strings-page-down)
2831 (define-key map [home] 'ebib-goto-first-string)
2832 (define-key map [end] 'ebib-goto-last-string)
2833 (define-key map " " 'ebib-strings-page-down)
2834 (define-key map "a" 'ebib-add-string)
2835 (define-key map "b" 'ebib-strings-page-up)
2836 (define-key map "c" 'ebib-copy-string-contents)
2837 (define-key map "d" 'ebib-delete-string)
2838 (define-key map "e" 'ebib-edit-string)
2839 (define-key map "g" 'ebib-goto-first-string)
2840 (define-key map "G" 'ebib-goto-last-string)
2841 (define-key map "h" 'ebib-strings-help)
2842 (define-key map "j" 'ebib-next-string)
2843 (define-key map "k" 'ebib-prev-string)
2844 (define-key map "l" 'ebib-edit-multiline-string)
2845 (define-key map [(control n)] 'ebib-next-string)
2846 (define-key map [(meta n)] 'ebib-strings-page-down)
2847 (define-key map [(control p)] 'ebib-prev-string)
2848 (define-key map [(meta p)] 'ebib-strings-page-up)
2849 (define-key map "q" 'ebib-quit-strings-buffer)
2850 (define-key map "x" 'ebib-export-string)
2851 (define-key map "X" 'ebib-export-all-strings)
2852 (define-key map "\C-xb" 'disabled)
2853 (define-key map "\C-xk" 'disabled)
2854 map)
2855 "Keymap for the ebib strings buffer.")
2857 (define-derived-mode ebib-strings-mode
2858 fundamental-mode "Ebib-strings"
2859 "Major mode for the Ebib strings buffer."
2860 (setq buffer-read-only t)
2861 (setq truncate-lines t))
2863 (defun ebib-quit-strings-buffer ()
2864 "Quit editing the @STRING definitions."
2865 (interactive)
2866 (switch-to-buffer ebib-entry-buffer)
2867 (other-window 1))
2869 (defun ebib-prev-string ()
2870 "Move to the previous string."
2871 (interactive)
2872 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
2873 (beep)
2874 ;; go to the beginnig of the highlight and move upward one line.
2875 (goto-char (ebib-highlight-start ebib-strings-highlight))
2876 (forward-line -1)
2877 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2878 (ebib-set-strings-highlight)))
2880 (defun ebib-next-string ()
2881 "Move to the next string."
2882 (interactive)
2883 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2884 (when (interactive-p) (beep))
2885 (goto-char (ebib-highlight-start ebib-strings-highlight))
2886 (forward-line 1)
2887 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2888 (ebib-set-strings-highlight)))
2890 (defun ebib-goto-first-string ()
2891 "Move to the first string."
2892 (interactive)
2893 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
2894 (goto-char (point-min))
2895 (ebib-set-strings-highlight))
2897 (defun ebib-goto-last-string ()
2898 "Move to the last string."
2899 (interactive)
2900 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2901 (goto-char (point-max))
2902 (forward-line -1)
2903 (ebib-set-strings-highlight))
2905 (defun ebib-strings-page-up ()
2906 "Moves 10 entries up in the database."
2907 (interactive)
2908 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
2909 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
2910 (if (<= (- number-of-strings remaining-number-of-strings) 10)
2911 (ebib-goto-first-string)
2912 (setq ebib-current-string (nth
2913 (- number-of-strings remaining-number-of-strings 10)
2914 (edb-strings-list ebib-cur-db)))
2915 (goto-char (ebib-highlight-start ebib-strings-highlight))
2916 (forward-line -10)
2917 (ebib-set-strings-highlight)))
2918 (message ebib-current-string))
2920 (defun ebib-strings-page-down ()
2921 "Moves 10 entries down in the database."
2922 (interactive)
2923 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
2924 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
2925 (if (<= remaining-number-of-strings 10)
2926 (ebib-goto-last-string)
2927 (setq ebib-current-string (nth
2928 (- number-of-strings remaining-number-of-strings -10)
2929 (edb-strings-list ebib-cur-db)))
2930 (goto-char (ebib-highlight-start ebib-strings-highlight))
2931 (forward-line 10)
2932 (ebib-set-strings-highlight)))
2933 (message ebib-current-string))
2935 (defun ebib-fill-strings-buffer ()
2936 "Fills the strings buffer with the @STRING definitions."
2937 (set-buffer ebib-strings-buffer)
2938 (with-buffer-writable
2939 (erase-buffer)
2940 (dolist (elem (edb-strings-list ebib-cur-db))
2941 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
2942 (insert (format "%-18s %s\n" elem
2943 (if (multiline-p str)
2944 (concat "+" (first-line str))
2945 (concat " " str)))))))
2946 (goto-char (point-min))
2947 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
2948 (ebib-set-strings-highlight)
2949 (set-buffer-modified-p nil))
2951 (defun ebib-edit-string ()
2952 "Edits the value of an @STRING definition
2954 When the user enters an empty string, the value is not changed."
2955 (interactive)
2956 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
2957 (if (multiline-p init-contents)
2958 (ebib-edit-multiline-string)
2959 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
2960 (if init-contents
2961 (cons init-contents 0)
2962 nil)
2963 ebib-minibuf-hist))
2964 (progn
2965 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
2966 (ebib-redisplay-current-string)
2967 (ebib-next-string)
2968 (ebib-set-modified t))
2969 (error "@STRING definition cannot be empty")))))
2971 (defun ebib-copy-string-contents ()
2972 "Copies the contents of the current string to the kill ring."
2973 (interactive)
2974 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
2975 (kill-new contents)
2976 (message "String value copied.")))
2978 (defun ebib-delete-string ()
2979 "Deletes the current @STRING definition from the database."
2980 (interactive)
2981 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
2982 (remhash ebib-current-string (edb-strings ebib-cur-db))
2983 (with-buffer-writable
2984 (let ((beg (progn
2985 (goto-char (ebib-highlight-start ebib-strings-highlight))
2986 (point))))
2987 (forward-line 1)
2988 (delete-region beg (point))))
2989 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
2990 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
2991 (when (null new-cur-string) ; deleted the last string
2992 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
2993 (forward-line -1))
2994 (setq ebib-current-string new-cur-string))
2995 (ebib-set-strings-highlight)
2996 (ebib-set-modified t)
2997 (message "@STRING definition deleted.")))
2999 (defun ebib-add-string ()
3000 "Creates a new @STRING definition."
3001 (interactive)
3002 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3003 (progn
3004 (if (member new-abbr (edb-strings-list ebib-cur-db))
3005 (error (format "%s already exists" new-abbr)))
3006 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3007 (progn
3008 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3009 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3010 (with-buffer-writable
3011 (insert (format "%-19s %s\n" new-abbr new-string)))
3012 (forward-line -1)
3013 (ebib-set-strings-highlight)
3014 (setq ebib-current-string new-abbr)
3015 (ebib-set-modified t))))))
3017 (defun ebib-export-string (prefix)
3018 "Exports the current @STRING.
3020 The prefix argument indicates which database to copy the string
3021 to. If no prefix argument is present, a filename is asked to
3022 which the string is appended."
3023 (interactive "P")
3024 (let ((num (ebib-prefix prefix)))
3025 (if num
3026 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3027 '(lambda (db)
3028 (let ((abbr ebib-current-string)
3029 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3030 (if (member abbr (edb-strings-list db))
3031 (error "@STRING definition already exists in database %d" num)
3032 (ebib-insert-string abbr string db t)))))
3033 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3034 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3035 '(lambda ()
3036 (insert (format "\n@string{%s = %s}\n"
3037 ebib-current-string
3038 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3040 (defun ebib-export-all-strings (prefix)
3041 "Export all @STRING definitions.
3043 If a prefix argument is given, it is taken as the database to copy the
3044 definitions to. Without prefix argument, asks for a file to append them
3045 to."
3046 (interactive "P")
3047 (when ebib-current-string ; there is always a current string, unless there are no strings
3048 (let ((num (ebib-prefix prefix)))
3049 (if num
3050 (ebib-export-to-db
3051 num "All @STRING definitions copied to database %d"
3052 '(lambda (db)
3053 (mapc #'(lambda (abbr)
3054 (if (member abbr (edb-strings-list db))
3055 (message "@STRING definition `%s' already exists in database %d" abbr num)
3056 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3057 (edb-strings-list ebib-cur-db))))
3058 (ebib-export-to-file "Export all @STRING definitions to file: "
3059 "All @STRING definitions exported to %s"
3060 '(lambda ()
3061 (insert (format "\n")) ; to keep things tidy.
3062 (ebib-format-strings ebib-cur-db)))))))
3064 (defun ebib-edit-multiline-string ()
3065 "Edits the current string in multiline-mode."
3066 (interactive)
3067 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3069 (defun ebib-strings-help ()
3070 "Displays the help message for the strings buffer."
3071 (interactive)
3072 (ebib-display-help ebib-strings-buffer))
3074 ;;;;;;;;;;;;;;;;;;;;;;;;;
3075 ;; multiline edit mode ;;
3076 ;;;;;;;;;;;;;;;;;;;;;;;;;
3078 (define-derived-mode ebib-multiline-edit-mode
3079 text-mode "Ebib-edit"
3080 "Major mode for editing multiline strings in Ebib."
3081 ;; we redefine some basic keys because we need them to leave this buffer.
3082 (local-set-key "\C-xb" 'ebib-leave-multiline-edit)
3083 (local-set-key "\C-x\C-s" 'ebib-save-from-multiline-edit)
3084 (local-set-key "\C-xk" 'ebib-cancel-multiline-edit))
3086 (defun ebib-multiline-edit (type &optional starttext)
3087 "Switches to Ebib's multiline edit buffer.
3089 STARTTEXT is a string that contains the initial text of the buffer."
3090 ;; note: the buffer is put in the currently active window!
3091 (switch-to-buffer ebib-multiline-buffer)
3092 (erase-buffer)
3093 (setq ebib-editing type)
3094 (when starttext
3095 (insert starttext)
3096 (goto-char (point-min))
3097 (set-buffer-modified-p nil)))
3099 (defun ebib-leave-multiline-edit ()
3100 "Quits the multiline edit buffer."
3101 (interactive)
3102 (ebib-store-multiline-text)
3103 (cond
3104 ((eq ebib-editing 'preamble)
3105 (switch-to-buffer ebib-entry-buffer)
3106 (other-window 1)) ; we have to switch back to the index buffer window
3107 ((eq ebib-editing 'fields)
3108 (switch-to-buffer ebib-entry-buffer)
3109 (ebib-redisplay-current-field)
3110 (ebib-next-field))
3111 ((eq ebib-editing 'strings)
3112 (switch-to-buffer ebib-strings-buffer)
3113 (ebib-redisplay-current-string)
3114 (ebib-next-string)))
3115 (message "Text stored."))
3117 (defun ebib-save-from-multiline-edit ()
3118 "Stores the text being edited in the multiline edit buffer and then saves the database."
3119 (interactive)
3120 (ebib-store-multiline-text)
3121 (ebib-save-database ebib-cur-db)
3122 (set-buffer-modified-p nil))
3124 (defun ebib-store-multiline-text ()
3125 "Stores the text being edited in the multiline edit buffer."
3126 ;; first make sure the buffer has no unbalanced braces. they would
3127 ;; confuse ebib if the file is loaded the next time.
3128 (check-parens)
3129 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3130 (cond
3131 ((eq ebib-editing 'preamble)
3132 (if (equal text "")
3133 (setf (edb-preamble ebib-cur-db) nil)
3134 (setf (edb-preamble ebib-cur-db) text)))
3135 ((eq ebib-editing 'fields)
3136 (if (equal text "")
3137 (remhash ebib-current-field ebib-cur-entry-hash)
3138 (when (not ebib-multiline-raw)
3139 (setq text (from-raw text)))
3140 (puthash ebib-current-field text ebib-cur-entry-hash)))
3141 ((eq ebib-editing 'strings)
3142 (if (equal text "")
3143 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3144 ;; MESSAGE, but we also do not switch back to the strings
3145 ;; buffer. this may not be so bad, actually, because the user
3146 ;; may want to change his edit.
3147 (error "@STRING definition cannot be empty ")
3148 (setq text (from-raw text)) ; strings cannot be raw
3149 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3150 (ebib-set-modified t))
3152 (defun ebib-cancel-multiline-edit ()
3153 "Quits the multiline edit buffer and discards the changes."
3154 (interactive)
3155 (catch 'no-cancel
3156 (when (buffer-modified-p)
3157 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3158 (throw 'no-cancel nil)))
3159 (cond
3160 ((eq ebib-editing 'fields)
3161 (switch-to-buffer ebib-entry-buffer)
3162 (ebib-redisplay-current-field)) ; we have to do this, because the
3163 ; user may have saved with C-x C-s
3164 ; before
3165 ((eq ebib-editing 'strings)
3166 (switch-to-buffer ebib-strings-buffer)
3167 (ebib-redisplay-current-string))
3168 ((eq ebib-editing 'preamble)
3169 (switch-to-buffer ebib-entry-buffer)
3170 (other-window 1)))))
3172 ;;;;;;;;;;;;;;;;;;;;
3173 ;; ebib-help-mode ;;
3174 ;;;;;;;;;;;;;;;;;;;;
3176 (defvar ebib-help-mode-map
3177 (let ((map (make-keymap)))
3178 (suppress-keymap map)
3179 (define-key map " " 'scroll-up)
3180 (define-key map "b" 'scroll-down)
3181 (define-key map "q" 'ebib-quit-help-buffer)
3182 map)
3183 "Keymap for the ebib help buffer.")
3185 (define-derived-mode ebib-help-mode
3186 fundamental-mode "Ebib-help"
3187 "Major mode for the Ebib help buffer."
3188 (setq buffer-read-only t)
3189 (local-set-key "\C-xb" 'ebib-quit-help-buffer)
3190 (local-set-key "\C-xk" 'ebib-quit-help-buffer))
3192 (defun ebib-display-help (buffer)
3193 "Shows the help message for Ebib-buffer BUFFER."
3194 (switch-to-buffer ebib-help-buffer)
3195 (setq ebib-before-help buffer)
3196 (with-buffer-writable
3197 (erase-buffer)
3198 (cond
3199 ((eq buffer ebib-index-buffer) (insert ebib-index-buffer-help))
3200 ((eq buffer ebib-entry-buffer) (insert ebib-entry-buffer-help))
3201 ((eq buffer ebib-strings-buffer) (insert ebib-strings-buffer-help)))
3202 (goto-char (point-min))))
3204 (defun ebib-quit-help-buffer ()
3205 "Exits the help buffer."
3206 (interactive)
3207 (cond
3208 ((eq ebib-before-help ebib-index-buffer)
3209 (switch-to-buffer ebib-entry-buffer)
3210 (other-window 1))
3211 ((eq ebib-before-help ebib-entry-buffer)
3212 (switch-to-buffer ebib-entry-buffer))
3213 ((eq ebib-before-help ebib-strings-buffer)
3214 (switch-to-buffer ebib-strings-buffer))))
3216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3217 ;; functions for non-Ebib buffers ;;
3218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3220 (defun ebib-import ()
3221 "Searches for BibTeX entries in the current buffer.
3223 The entries are added to the current database (i.e. the database that was
3224 active when Ebib was lowered. Works on the whole buffer, or on the region
3225 if it is active."
3226 (interactive)
3227 (if (not ebib-cur-db)
3228 (error "No database loaded. Use `o' to open a database")
3229 (if (edb-virtual ebib-cur-db)
3230 (error "Cannot import to a virtual database")
3231 (save-excursion
3232 (save-restriction
3233 (if (region-active)
3234 (narrow-to-region (region-beginning)
3235 (region-end)))
3236 (let ((text (buffer-string)))
3237 (with-temp-buffer
3238 (insert text)
3239 (let ((n (ebib-find-bibtex-entries t)))
3240 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3241 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3242 (when (edb-strings-list ebib-cur-db)
3243 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3244 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3245 (ebib-fill-entry-buffer)
3246 (ebib-fill-index-buffer)
3247 (ebib-set-modified t)
3248 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3249 (car n)
3250 (cadr n)
3251 (if (caddr n)
3253 "no")))))))))))
3255 (defun ebib-get-db-from-filename (filename)
3256 "Returns the database struct associated with FILENAME."
3257 (catch 'found
3258 (mapc #'(lambda (db)
3259 (if (string= (file-name-nondirectory (edb-filename db)) filename)
3260 (throw 'found db)))
3261 ebib-databases)
3262 nil))
3264 (defun ebib-get-local-databases ()
3265 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3267 Each .bib file is a string holding the name of the .bib
3268 file. This function simply searches the current LaTeX file or its
3269 master file for a \\bibliography command and returns the file(s)
3270 given in its argument. If no \\bibliography command is found,
3271 returns the symbol NONE."
3272 (let ((texfile-buffer (current-buffer))
3273 texfile
3274 bibfiles)
3275 ;; if AucTeX's TeX-master is used and set to a string, we must
3276 ;; search that file for a \bibliography command, as it's more
3277 ;; likely to be in there than in the file we're in.
3278 (and (boundp 'TeX-master)
3279 (stringp TeX-master)
3280 (setq texfile (ensure-extension TeX-master "tex")))
3281 (with-temp-buffer
3282 (if (and texfile (file-readable-p texfile))
3283 (insert-file-contents texfile)
3284 (insert-buffer-substring texfile-buffer))
3285 (save-excursion
3286 (goto-char (point-min))
3287 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3288 (mapcar #'(lambda (file)
3289 (ensure-extension file "bib"))
3290 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3291 'none)))))
3293 (defun ebib-insert-bibtex-key (prefix)
3294 "Inserts a BibTeX key at POINT.
3296 The user is prompted for a BibTeX key and has to choose one from
3297 the database(s) associated with the current LaTeX file, or from
3298 the current database if there is no \\bibliography command. Tab
3299 completion works."
3300 (interactive "p")
3301 (ebib-execute-when
3302 ((database)
3303 (or ebib-local-bibtex-filenames
3304 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3305 (let (collection)
3306 (if (eq ebib-local-bibtex-filenames 'none)
3307 (if (null (edb-cur-entry ebib-cur-db))
3308 (error "No entries found in current database")
3309 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3310 (mapc #'(lambda (file)
3311 (let ((db (ebib-get-db-from-filename file)))
3312 (cond
3313 ((null db)
3314 (message "Database %s not loaded" file))
3315 ((null (edb-cur-entry db))
3316 (message "No entries in database %s" file))
3317 (t (setq collection (append (ebib-create-collection (edb-database db))
3318 collection))))))
3319 ebib-local-bibtex-filenames))
3320 (if collection
3321 (let* ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
3322 "{%s}"))
3323 (key (completing-read (format "Insert \"%s\" with key: " latex-cmd)
3324 collection nil t nil ebib-minibuf-hist)))
3325 (when key
3326 (insert (format (or (cdr (assoc prefix ebib-insertion-strings))
3327 "{%s}") key)))))))
3328 ((default)
3329 (error "No database loaded"))))
3331 (defun ebib-entry-summary ()
3332 "Shows the fields of the key at POINT.
3334 The key is searched in the database associated with the LaTeX file, or in
3335 the current database if no \\bibliography command can be found."
3336 (interactive)
3337 (ebib-execute-when
3338 ((database)
3339 (or ebib-local-bibtex-filenames
3340 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3341 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3342 entry)
3343 (if (eq ebib-local-bibtex-filenames 'none)
3344 (if (not (member key (edb-keys-list ebib-cur-db)))
3345 (error "`%s' is not in the current database" key)
3346 (setq entry (gethash key (edb-database ebib-cur-db))))
3347 (setq entry
3348 (catch 'found
3349 (mapc #'(lambda (file)
3350 (let ((db (ebib-get-db-from-filename file)))
3351 (if (null db)
3352 (message "Database %s not loaded" file)
3353 (if (member key (edb-keys-list db))
3354 (throw 'found (gethash key (edb-database db)))))))
3355 ebib-local-bibtex-filenames)
3356 nil)))
3357 (if (null entry)
3358 (error "Entry `%s' not found" key)
3359 (with-output-to-temp-buffer "*Help*"
3360 (ebib-format-fields entry 'princ)))))
3361 ((default)
3362 (error "No database(s) loaded"))))
3364 (provide 'ebib)
3366 ;; we put these at the end, because they seem to mess up Emacs'
3367 ;; syntax highlighting.
3369 (setq ebib-index-buffer-help
3370 "Ebib index buffer -- command key overview
3372 Note: command keys are case-sensitive.
3374 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3376 cursor movement:
3377 [Up], k, C-p: go to the previous entry
3378 [Down], j, C-n: go to the next entry
3379 [Home], g: go to the first entry
3380 [End], G: go to the last entry
3381 [PgUp], b, M-p: scroll up
3382 [PgDn], [Space], M-n: scroll down
3384 editing:
3385 e: edit the current entry
3386 E: edit the current entry's name
3387 a: add a new entry
3388 d: delete the current entry
3389 ;-d: deletes all marked entries
3390 t: edit the @STRING definitions
3391 r: edit the @PREAMBLE definition
3392 m: (un)mark the current entry
3393 ;-m: unmarks all marked entries
3395 searching:
3396 /: search the database
3397 n: find the next occurrence of the search string
3398 C-s: search for a key (incrementally)
3399 [return]: select the entry under the cursor (use after C-s)
3400 F: follow the crossref field
3401 &: filter the current database with a logical AND
3402 |: filter the current database with a logical OR
3403 ~: filter the current database with a logical NOT
3404 V: show the current filter (with prefix argument:
3405 reapply current filter)
3407 file handling:
3408 o: open a database
3409 c: close the database
3410 s: save the database
3411 S: save all databases
3412 w: save the database under a different name
3413 M: merge another database
3414 x: export the current entry to another file
3415 (with prefix argument N: copy to database N)
3416 ;-x: export the marked entry to another file
3417 (with prefix argument N: copy to database N)
3418 X: export the @PREAMBLE definition to another file
3419 (with prefix argument N: copy to database N)
3420 f: print the full filename in the minibuffer
3422 databases:
3423 1-9: switch to database 1-9
3424 J: switch to another database (accepts prefix argument)
3425 [right], [left]: switch to previous/next database
3426 L: LaTeX the database
3427 ;-L: LaTeX the marked entries
3428 P: print the database
3429 ;-P: print the marked entries
3431 general:
3432 u: extract URL from the `url' field and send it to a browser
3433 p: push the current entry to a LaTeX buffer
3434 ;-p: push marked entries to a LaTeX buffer
3435 C: customise Ebib
3436 z: put Ebib in the background
3437 q: quit Ebib
3438 h: show this help page
3441 (setq ebib-entry-buffer-help
3442 "Ebib entry buffer -- command key overview
3444 Note: command keys are case-sensitive.
3446 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3448 cursor movement:
3449 [Up], k, C-p: go to the previous field
3450 [Down], j, C-n: go to the next field
3451 [Home], g: go to the first field
3452 [End], G: go to the last field
3453 [PgUp], b, M-p: go to the previous group of fields
3454 [PgDn], [Space], M-n: go to the next group of fields
3456 editing:
3457 e: edit the value of the current field
3458 c: copy the value of the current field (value is put into the kill ring)
3459 x: kill the value of the current field (value is put into the kill ring)
3460 y: yank the most recently copied/cut string
3461 d: delete the value of the current entry
3462 r: toggle the \"rawness\" status of the current field
3463 l: edit the current field as multi-line
3464 s: insert an @STRING abbreviation into the current field
3466 general:
3467 u: extract URL and send it to a browser
3468 q: quit the entry buffer and return to the index buffer
3469 h: show this help page
3472 (setq ebib-strings-buffer-help
3473 "Ebib strings buffer -- command key overview
3475 Note: command keys are case-sensitive.
3477 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3479 cursor movement:
3480 [Up], k, C-p: go to the previous @STRING definition
3481 [Down], j, C-n: go to the next @STRING definition
3482 [Home], g: go to the first @STRING definition
3483 [End], G: go to the last @STRING definition
3484 [PgUp], b, M-p: scroll 10 @STRING definitions up
3485 [PgDn], [Space], M-n: scroll 10 @STRING definitions down
3487 editing:
3488 e: edit the value of the current @STRING definition
3489 c: copy the value of the current @STRING definition
3490 d: delete the current @STRING definition
3491 a: add an @STRING definition
3492 l: edit the current @STRING as multi-line
3494 exporting:
3495 x: export the current @STRING definition to another file
3496 (with prefix argument N: copy to database N)
3497 X: export all @STRING definitions to another file
3498 (with prefix argument N: copy to database N)
3500 general:
3501 q: quit the strings buffer and return to the index buffer
3502 h: show this help page
3505 ;;; ebib ends here