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