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