Uniquify keys
[ebib.git] / src / ebib.el
blobe8717826f041398c120b554cee24d4e479ac3989
1 ;; Ebib v==VERSION==
2 ;;
3 ;; Copyright (c) 2003-2011 Joost Kremers
4 ;; All rights reserved.
5 ;;
6 ;; Redistribution and use in source and binary forms, with or without
7 ;; modification, are permitted provided that the following conditions
8 ;; are met:
9 ;;
10 ;; 1. Redistributions of source code must retain the above copyright
11 ;; notice, this list of conditions and the following disclaimer.
12 ;; 2. Redistributions in binary form must reproduce the above copyright
13 ;; notice, this list of conditions and the following disclaimer in the
14 ;; documentation and/or other materials provided with the distribution.
15 ;; 3. The name of the author may not be used to endorse or promote products
16 ;; derived from this software without specific prior written permission.
18 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 ;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 ;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE,
24 ;; DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 ;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (eval-when-compile
30 (require 'cl))
31 (require 'easymenu)
32 (require 'bibtex)
34 (declare-function bibtex-generate-autokey "bibtex" nil)
36 ;;;;;;;;;;;;;;;;;;;;;;
37 ;; global variables ;;
38 ;;;;;;;;;;;;;;;;;;;;;;
40 ;; user customisation
42 (defgroup ebib nil "Ebib: a BibTeX database manager" :group 'Tex)
44 (defcustom ebib-default-type 'article
45 "*The default type for a newly created BibTeX entry."
46 :group 'ebib
47 :type 'symbol)
49 (defcustom ebib-preload-bib-files nil
50 "*List of .bib files to load automatically when Ebib starts."
51 :group 'ebib
52 :type '(repeat (file :must-match t)))
54 (defcustom ebib-additional-fields '(crossref url annote abstract
55 keywords file timestamp)
56 "*List of the additional fields."
57 :group 'ebib
58 :type '(repeat (symbol :tag "Field")))
60 (defcustom ebib-layout 'full
61 "*Ebib window layout.
62 Full width: Ebib occupies the entire Emacs frame.
63 Custom width: Ebib occupies the right side of the Emacs frame,
64 with the left side free for another window."
65 :group 'ebib
66 :type '(choice (const :tag "Full width" full)
67 (const :tag "Custom width" custom)))
69 (defcustom ebib-width 80
70 "*Width of the Ebib windows.
71 Only takes effect if EBIB-LAYOUT is set to CUSTOM."
72 :group 'ebib
73 :type 'integer)
75 (defcustom ebib-index-window-size 10
76 "*The number of lines used for the index buffer window."
77 :group 'ebib
78 :type 'integer)
80 (defcustom ebib-index-display-fields nil
81 "*List of the fields to display in the index buffer."
82 :group 'ebib
83 :type '(repeat (symbol :tag "Index Field")))
85 (defcustom ebib-uniquify-keys nil
86 "*Create unique keys.
87 If set, Ebib will not complain about duplicate keys but will
88 instead create a unique key by adding an identifier to it.
89 Identifiers are created from consecutive letters of the
90 alphabet, starting with `b'."
91 :group 'ebib
92 :type 'boolean)
94 (defcustom ebib-autogenerate-keys nil
95 "*If set, Ebib generates key automatically.
96 Uses the function BIBTEX-GENERATE-AUTOKEY, see there for
97 customization options."
98 :group 'ebib
99 :type 'boolean)
101 (defcustom ebib-insertion-commands '(("cite" 1 nil))
102 "*A list of commands that can be used to insert an entry into a (La)TeX buffer.
103 For use with EBIB-INSERT-BIBTEX-KEY and EBIB-PUSH-BIBTEX-KEY."
104 :group 'ebib
105 :type '(repeat (list :tag "Citation command" (string :tag "Command")
106 (integer :tag "Optional arguments")
107 (choice (const :tag "Standard command" nil)
108 (const :tag "Multicite command" t)))))
110 (defcustom ebib-multiline-major-mode 'text-mode
111 "*The major mode of the multiline edit buffer."
112 :group 'ebib
113 :type '(function :tag "Mode function"))
115 (defcustom ebib-sort-order nil
116 "*The fields on which the BibTeX entries are to be sorted in the .bib file.
117 Sorting is done on different sort levels, and each sort level contains one
118 or more sort keys."
119 :group 'ebib
120 :type '(repeat (repeat :tag "Sort level" (symbol :tag "Sort field"))))
122 (defcustom ebib-save-xrefs-first nil
123 "*If true, entries with a crossref field will be saved first in the .bib-file.
124 Setting this option has unpredictable results for the sort order
125 of entries, so it is not compatible with setting the Sort Order option."
126 :group 'ebib
127 :type 'boolean)
129 (defface ebib-crossref-face '((t (:foreground "red")))
130 "*Face used to indicate values inherited from crossreferenced entries."
131 :group 'ebib)
133 (defface ebib-marked-face (if (featurep 'xemacs)
134 '((t (:foreground "white" :background "red")))
135 '((t (:inverse-video t))))
136 "*Face to indicate marked entries."
137 :group 'ebib)
139 (defcustom ebib-use-timestamp nil
140 "*If true, new entries will get a time stamp.
141 The time stamp will be stored in a field \"timestamp\" that can
142 be made visible with the command \\[ebib-toggle-hidden] in the
143 index buffer."
144 :group 'ebib
145 :type 'boolean)
147 (defcustom ebib-timestamp-format "%a %b %e %T %Y"
148 "*Format of the time string used in the timestamp.
149 The format is passed unmodified to FORMAT-TIME-STRING, see the
150 documentation of that function for details."
151 :group 'ebib
152 :type 'string)
154 (defcustom ebib-standard-url-field 'url
155 "*Standard field to store urls in.
156 In the index buffer, the command ebib-browse-url can be used to
157 send a url to a browser. This option sets the field from which
158 this command extracts the url."
159 :group 'ebib
160 :type 'symbol)
162 (defcustom ebib-url-regexp "\\\\url{\\(.*\\)}\\|https?://[^ '<>\"\n\t\f]+"
163 "*Regular expression to extract urls."
164 :group 'ebib
165 :type 'string)
167 (defcustom ebib-browser-command ""
168 "*Command to call the browser with.
169 GNU/Emacs has a function call-browser, which is used if this
170 option is unset."
171 :group 'ebib
172 :type '(string :tag "Browser command"))
174 (defcustom ebib-standard-file-field 'file
175 "*Standard field to store filenames in.
176 In the index buffer, the command ebib-view-file can be used to
177 view a file externally. This option sets the field from which
178 this command extracts the filename."
179 :group 'ebib
180 :type 'symbol)
182 (defcustom ebib-file-associations '(("pdf" . "xpdf")
183 ("ps" . "gv"))
184 "*List of file associations.
185 Lists file extensions together with external programs to handle
186 files with those extensions. If the external program is left
187 blank, Ebib tries to handle the file internally in
188 Emacs (e.g. with doc-view-mode)."
189 :group 'ebib
190 :type '(repeat (cons :tag "File association"
191 (string :tag "Extension") (string :tag "Command"))))
193 (defcustom ebib-file-regexp "[^?|\\:*<>\" \n\t\f]+"
194 "*Regular expression to match filenames."
195 :group 'ebib
196 :type 'string)
198 (defcustom ebib-file-search-dirs '("~")
199 "*List of directories to search for files."
200 :group 'ebib
201 :type '(repeat :tag "Search directories" (string :tag "Directory")))
203 (defcustom ebib-print-preamble nil
204 "*Preamble used for the LaTeX file for printing the database.
205 Each string is added to the preamble on a separate line."
206 :group 'ebib
207 :type '(repeat (string :tag "Add to preamble")))
209 (defcustom ebib-print-newpage nil
210 "*If set, each entry is printed on a separate page."
211 :group 'ebib
212 :type 'boolean)
214 (defcustom ebib-print-multiline nil
215 "*If set, multiline fields are included when printing the database."
216 :group 'ebib
217 :type 'boolean)
219 (defcustom ebib-latex-preamble '("\\bibliographystyle{plain}")
220 "*Preamble used for the LaTeX file for BibTeXing the database.
221 Each string is added to the preamble on a separate line."
222 :group 'ebib
223 :type '(repeat (string :tag "Add to preamble")))
225 (defcustom ebib-print-tempfile ""
226 "*Temporary file for use with EBIB-PRINT-DATABASE and EBIB-LATEX-DATABASE."
227 :group 'ebib
228 :type '(file))
230 (defcustom ebib-allow-identical-fields nil
231 "*If set, Ebib handles multiple occurrences of a field gracefully."
232 :group 'ebib
233 :type 'boolean)
235 (defvar ebib-unique-field-list nil
236 "Holds a list of all field names.")
238 (defmacro add-to-listq (listvar element &optional append fn)
239 (if (or (featurep 'xemacs)
240 (string< emacs-version "22"))
241 `(add-to-list (quote ,listvar) ,element ,append)
242 `(add-to-list (quote ,listvar) ,element ,append ,fn)))
244 (defun ebib-set-unique-field-list (var value)
245 "Sets EBIB-UNIQUE-FIELD-LIST on the basis of EBIB-ENTRY-TYPES"
246 (set-default var value)
247 (setq ebib-unique-field-list nil)
248 (mapc #'(lambda (entry)
249 (mapc #'(lambda (field)
250 (add-to-listq ebib-unique-field-list field t 'eq))
251 (cadr entry))
252 (mapc #'(lambda (field)
253 (add-to-listq ebib-unique-field-list field t 'eq))
254 (caddr entry)))
255 value))
257 (defcustom ebib-entry-types
258 '((article ;; name of entry type
259 (author title journal year) ;; obligatory fields
260 (volume number pages month note)) ;; optional fields
262 (book
263 (author title publisher year)
264 (editor volume number series address edition month note))
266 (booklet
267 (title)
268 (author howpublished address month year note))
270 (inbook
271 (author title chapter pages publisher year)
272 (editor volume series address edition month note))
274 (incollection
275 (author title booktitle publisher year)
276 (editor volume number series type chapter pages address edition month note))
278 (inproceedings
279 (author title booktitle year)
280 (editor pages organization publisher address month note))
282 (manual
283 (title)
284 (author organization address edition month year note))
286 (misc
288 (title author howpublished month year note))
290 (mastersthesis
291 (author title school year)
292 (address month note))
294 (phdthesis
295 (author title school year)
296 (address month note))
298 (proceedings
299 (title year)
300 (editor publisher organization address month note))
302 (techreport
303 (author title institution year)
304 (type number address month note))
306 (unpublished
307 (author title note)
308 (month year)))
310 "List of entry type definitions for Ebib"
311 :group 'ebib
312 :type '(repeat (list :tag "Entry type" (symbol :tag "Name")
313 (repeat :tag "Obligatory fields" (symbol :tag "Field"))
314 (repeat :tag "Optional fields" (symbol :tag "Field"))))
315 :set 'ebib-set-unique-field-list)
317 ;; generic for all databases
319 ;; constants and variables that are set only once
320 (defconst ebib-bibtex-identifier "[^\"#%'(),={} \t\n\f]*" "Regexp describing a licit BibTeX identifier.")
321 (defconst ebib-version "==VERSION==")
322 (defvar ebib-initialized nil "T if Ebib has been initialized.")
324 ;; buffers and highlights
325 (defvar ebib-index-buffer nil "The index buffer.")
326 (defvar ebib-entry-buffer nil "The entry buffer.")
327 (defvar ebib-strings-buffer nil "The strings buffer.")
328 (defvar ebib-multiline-buffer nil "Buffer for editing multiline strings.")
329 (defvar ebib-log-buffer nil "Buffer showing warnings and errors during loading of .bib files")
330 (defvar ebib-index-highlight nil "Highlight to mark the current entry.")
331 (defvar ebib-fields-highlight nil "Highlight to mark the current field.")
332 (defvar ebib-strings-highlight nil "Highlight to mark the current string.")
334 ;; general bookkeeping
335 (defvar ebib-minibuf-hist nil "Holds the minibuffer history for Ebib")
336 (defvar ebib-saved-window-config nil "Stores the window configuration when Ebib is called.")
337 (defvar ebib-pre-ebib-window nil "The window that was active when Ebib was called.")
338 (defvar ebib-pre-multiline-buffer nil "The buffer in the window before switching to the multiline edit buffer.")
339 (defvar ebib-export-filename nil "Filename to export entries to.")
340 (defvar ebib-push-buffer nil "Buffer to push entries to.")
341 (defvar ebib-search-string nil "Stores the last search string.")
342 (defvar ebib-editing nil "Indicates what the user is editing.
343 Its value can be 'strings, 'fields, or 'preamble.")
344 (defvar ebib-multiline-raw nil "Indicates whether the multiline text being edited is raw.")
345 (defvar ebib-log-error nil "Indicates whether an error was logged.")
346 (defvar ebib-local-bibtex-filenames nil
347 "A buffer-local variable holding a list of the name(s) of that buffer's .bib file(s)")
348 (make-variable-buffer-local 'ebib-local-bibtex-filenames)
349 (defvar ebib-syntax-table (make-syntax-table) "Syntax table used for reading .bib files.")
350 (modify-syntax-entry ?\[ "." ebib-syntax-table)
351 (modify-syntax-entry ?\] "." ebib-syntax-table)
352 (modify-syntax-entry ?\( "." ebib-syntax-table)
353 (modify-syntax-entry ?\) "." ebib-syntax-table)
354 (modify-syntax-entry ?\" "w" ebib-syntax-table)
356 ;; the databases
358 ;; each database is represented by a struct
359 (defstruct edb
360 (database (make-hash-table :test 'equal)) ; hashtable containing the database itself
361 (keys-list nil) ; sorted list of the keys in the database
362 (cur-entry nil) ; sublist of KEYS-LIST that starts with the current entry
363 (marked-entries nil) ; list of marked entries
364 (n-entries 0) ; number of entries stored in this database
365 (strings (make-hash-table :test 'equal)) ; hashtable with the @STRING definitions
366 (strings-list nil) ; sorted list of the @STRING abbreviations
367 (preamble nil) ; string with the @PREAMBLE definition
368 (filename nil) ; name of the BibTeX file that holds this database
369 (name nil) ; name of the database
370 (modified nil) ; has this database been modified?
371 (make-backup nil) ; do we need to make a backup of the .bib file?
372 (virtual nil)) ; is this a virtual database?
374 ;; the master list and the current database
375 (defvar ebib-databases nil "List of structs containing the databases.")
376 (defvar ebib-cur-db nil "The database that is currently active.")
378 ;;;;;; bookkeeping required when editing field values or @STRING definitions
380 (defvar ebib-hide-hidden-fields t "If set to T, hidden fields are not shown.")
382 ;; this variable is set when the user enters the entry buffer
383 (defvar ebib-cur-entry-fields nil "The fields of the type of the current entry.")
385 ;; these two are set by EBIB-FILL-ENTRY-BUFFER
386 (defvar ebib-cur-entry-hash nil "The hash table containing the data of the current entry.")
387 (defvar ebib-current-field nil "The current field.")
389 ;; and this one by EBIB-FILL-STRINGS-BUFFER
390 (defvar ebib-current-string nil "The current @STRING definition.")
392 ;; the prefix key and the multiline key are stored in a variable so that the
393 ;; user can customise them.
394 (defvar ebib-prefix-key ?\;)
395 (defvar ebib-multiline-key ?\|)
397 ;; this is an AucTeX variable, but we want to check its value, so let's
398 ;; keep the compiler from complaining.
399 (eval-when-compile
400 (defvar TeX-master))
402 ;; this is to keep XEmacs from complaining.
403 (eval-when-compile
404 (if (featurep 'xemacs)
405 (defvar mark-active)))
407 ;; XEmacs has line-number, not line-number-at-pos.
408 (eval-and-compile
409 (if (featurep 'xemacs)
410 (defalias 'line-number-at-pos 'line-number)))
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;; useful macros and functions ;;
414 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
416 (defmacro nor (&rest args)
417 "Returns T if none of its arguments are true."
418 `(not (or ,@args)))
420 ;; we sometimes (often, in fact ;-) need to do something with a string, but
421 ;; take special action (or do nothing) if that string is empty. IF-STR
422 ;; makes that easier:
424 (defmacro if-str (bindvar then &rest else)
425 "Execute THEN only if STRING is nonempty.
426 Format: (if-str (var value) then-form [else-forms]) VAR is bound
427 to VALUE, which is evaluated. If VAR is a nonempty string,
428 THEN-FORM is executed. If VAR is either \"\" or nil, ELSE-FORM is
429 executed. Returns the value of THEN or of ELSE."
430 (declare (indent 2))
431 `(let ,(list bindvar)
432 (if (nor (null ,(car bindvar))
433 (equal ,(car bindvar) ""))
434 ,then
435 ,@else)))
437 (defmacro last1 (lst &optional n)
438 "Returns the last (or Nth last) element of LST."
439 `(car (last ,lst ,n)))
441 ;; we sometimes need to walk through lists. these functions yield the
442 ;; element directly preceding or following ELEM in LIST. in order to work
443 ;; properly, ELEM must be unique in LIST, obviously. if ELEM is the
444 ;; first/last element of LIST, or if it is not contained in LIST at all,
445 ;; the result is nil.
446 (defun next-elem (elem list)
447 (cadr (member elem list)))
449 (defun prev-elem (elem list)
450 (if (or (equal elem (car list))
451 (not (member elem list)))
453 (last1 list (1+ (length (member elem list))))))
455 (defun read-string-at-point (chars)
456 "Reads a string at POINT delimited by CHARS and returns it.
457 CHARS is a string of characters that should not occur in the string."
458 (save-excursion
459 (skip-chars-backward (concat "^" chars))
460 (let ((beg (point)))
461 (looking-at-goto-end (concat "[^" chars "]*"))
462 (buffer-substring-no-properties beg (point)))))
464 (defun remove-from-string (string remove)
465 "Returns a copy of STRING with all the occurrences of REMOVE taken out.
466 REMOVE can be a regex."
467 (apply 'concat (split-string string remove)))
469 (defun in-string (char string)
470 "Returns T if CHAR is in STRING, otherwise NIL."
471 (catch 'found
472 (do ((len (length string))
473 (i 0 (1+ i)))
474 ((= i len) nil)
475 (if (eq char (aref string i))
476 (throw 'found t)))))
478 (defun ensure-extension (string ext)
479 "Makes sure STRING has the extension EXT, by appending it if necessary.
480 EXT should be an extension without the dot."
481 (if (string-match (concat "\\." ext "$") string)
482 string
483 (concat string "." ext)))
485 (defmacro with-buffer-writable (&rest body)
486 "Makes the current buffer writable and executes the commands in BODY.
487 After BODY is executed, the buffer modified flag is unset."
488 (declare (indent defun))
489 `(unwind-protect
490 (let ((buffer-read-only nil))
491 ,@body)
492 (set-buffer-modified-p nil)))
494 (defmacro safe-write-region (start end filename &optional append visit lockname mustbenew)
495 "XEmacs does not have the MUSTBENEW argument, so this is a way to implement it."
496 (if (featurep 'xemacs)
497 `(if (and (file-exists-p ,filename)
498 (not (y-or-n-p (format "File %s already exists; overwrite anyway? " ,filename))))
499 (error "File %s exist" ,filename)
500 (write-region ,start ,end ,filename ,append ,visit ,lockname))
501 `(write-region ,start ,end ,filename ,append ,visit ,lockname ,mustbenew)))
503 (defun symbol-or-string (x)
504 "Returns the symbol-name of X if X is a symbol, otherwise return X.
505 Much like SYMBOL-NAME, except it does not throw an error if X is
506 not a symbol."
507 (if (symbolp x)
508 (symbol-name x)
511 ;; XEmacs doesn't know about propertize...
512 (if (not (fboundp 'propertize))
513 (defun propertize (string &rest properties)
514 "Return a copy of STRING with text properties added.
515 First argument is the string to copy. Remaining arguments form a
516 sequence of PROPERTY VALUE pairs for text properties to add to
517 the result."
518 (let ((new-string (copy-sequence string)))
519 (add-text-properties 0 (length new-string) properties new-string)
520 new-string)))
522 (defun region-active ()
523 (if (featurep 'xemacs)
524 (region-active-p)
525 mark-active))
527 ;; RAW-P determines if STRING is raw. note that we cannot do this by
528 ;; simply checking whether STRING begins with { and ends with } (or
529 ;; begins and ends with "), because something like "{abc} # D # {efg}"
530 ;; would then be incorrectly recognised as non-raw. so we need to do
531 ;; the following: take out everything that is between braces or
532 ;; quotes, and see if anything is left. if there is, the original
533 ;; string was raw, otherwise it was not.
535 ;; so i first check whether the string begins with { or ". if not, we
536 ;; certainly have a raw string. (RAW-P recognises this through the default
537 ;; clause of the COND.) if the first character is { or ", we first take out
538 ;; every occurrence of backslash-escaped { and } or ", so that the rest of
539 ;; the function does not get confused over them.
541 ;; then, if the first character is {, i use REMOVE-FROM-STRING to take out
542 ;; every occurrence of the regex "{[^{]*?}", which translates to "the
543 ;; smallest string that starts with { and ends with }, and does not contain
544 ;; another {. IOW, it takes out the innermost braces and their
545 ;; contents. because braces may be embedded, we have to repeat this step
546 ;; until no more balanced braces are found in the string. (note that it
547 ;; would be unwise to check for just the occurrence of { or }, because that
548 ;; would throw RAW-P in an infinite loop if a string contains an unbalanced
549 ;; brace.)
551 ;; for strings beginning with " i do the same, except that it is not
552 ;; necessary to repeat this in a WHILE loop, for the simple reason that
553 ;; strings surrounded with double quotes cannot be embedded; i.e.,
554 ;; "ab"cd"ef" is not a valid (BibTeX) string, while {ab{cd}ef} is.
556 ;; note: because these strings are to be fed to BibTeX and ultimately
557 ;; (La)TeX, it might seem that we don't need to worry about strings
558 ;; containing unbalanced braces, because (La)TeX would choke on them. but
559 ;; the user may inadvertently enter such a string, and we therefore need to
560 ;; be able to handle it. (alternatively, we could perform a check on
561 ;; strings and warn the user.)
563 (defun raw-p (string)
564 "Non-nil if STRING is raw."
565 (when (stringp string)
566 (cond
567 ((eq (string-to-char string) ?\{)
568 ;; we remove all occurrences of `\{' and of `\}' from the string:
569 (let ((clear-str (remove-from-string (remove-from-string string "[\\][{]")
570 "[\\][}]")))
571 (while (and (in-string ?\{ clear-str) (in-string ?\} clear-str))
572 (setq clear-str (remove-from-string clear-str "{[^{]*?}")))
573 (> (length clear-str) 0)))
574 ((eq (string-to-char string) ?\")
575 (let ((clear-str (remove-from-string string "[\\][\"]"))) ; remove occurrences of `\"'
576 (setq clear-str (remove-from-string clear-str "\"[^\"]*?\""))
577 (> (length clear-str) 0)))
578 (t t))))
580 (defun to-raw (string)
581 "Converts a string to its raw counterpart."
582 (if (and (stringp string)
583 (not (raw-p string)))
584 (substring string 1 -1)
585 string))
587 (defun from-raw (string)
588 "Converts a raw string to a non-raw one."
589 (if (raw-p string)
590 (concat "{" string "}")
591 string))
593 (defun multiline-p (string)
594 "True if STRING is multiline."
595 (if (stringp string)
596 (string-match "\n" string)))
598 (defun first-line (string)
599 "Returns the first line of a multi-line string."
600 (string-match "\n" string)
601 (substring string 0 (match-beginning 0)))
603 (defun sort-in-buffer (limit str)
604 "Moves POINT to the right position to insert STR in a buffer with lines sorted A-Z."
605 (let ((upper limit)
606 middle)
607 (when (> limit 0)
608 (let ((lower 0))
609 (goto-char (point-min))
610 (while (progn
611 (setq middle (/ (+ lower upper 1) 2))
612 (goto-char (point-min))
613 (forward-line (1- middle)) ; if this turns out to be where we need to be,
614 (beginning-of-line) ; this puts POINT at the right spot.
615 (> (- upper lower) 1)) ; if upper and lower differ by only 1, we have found the
616 ; position to insert the entry in.
617 (save-excursion
618 (let ((beg (point)))
619 (end-of-line)
620 (if (string< (buffer-substring-no-properties beg (point)) str)
621 (setq lower middle)
622 (setq upper middle)))))))))
624 (defun match-all (match-str string)
625 "Highlights all the matches of MATCH-STR in STRING.
626 The return value is a list of two elements: the first is the
627 modified string, the second either t or nil, indicating whether a
628 match was found at all."
629 (do ((counter 0 (match-end 0)))
630 ((not (string-match match-str string counter)) (values string (not (= counter 0))))
631 (add-text-properties (match-beginning 0) (match-end 0) '(face highlight) string)))
633 (defun looking-at-goto-end (str &optional match)
634 "Like LOOKING-AT but moves point to the end of the matching string.
635 MATCH acts just like the argument to MATCH-END, and defaults to 0."
636 (or match (setq match 0))
637 (let ((case-fold-search t))
638 (if (looking-at str)
639 (goto-char (match-end match)))))
641 ;; this needs to be wrapped in an eval-and-compile, to keep Emacs from
642 ;; complaining that ebib-execute-helper isn't defined when it compiles
643 ;; ebib-execute-when.
644 (eval-and-compile
645 (defun ebib-execute-helper (env)
646 "Helper function for EBIB-EXECUTE-WHEN."
647 (cond
648 ((eq env 'entries)
649 '(and ebib-cur-db
650 (edb-cur-entry ebib-cur-db)))
651 ((eq env 'marked-entries)
652 '(and ebib-cur-db
653 (edb-marked-entries ebib-cur-db)))
654 ((eq env 'database)
655 'ebib-cur-db)
656 ((eq env 'real-db)
657 '(and ebib-cur-db
658 (not (edb-virtual ebib-cur-db))))
659 ((eq env 'virtual-db)
660 '(and ebib-cur-db
661 (edb-virtual ebib-cur-db)))
662 ((eq env 'no-database)
663 '(not ebib-cur-db))
664 (t t))))
666 (defmacro ebib-execute-when (&rest forms)
667 "Macro to facilitate writing Ebib functions.
668 This functions essentially like a COND clause: the basic format
669 is (ebib-execute-when FORMS ...), where each FORM is built up
670 as (ENVIRONMENTS BODY). ENVIRONMENTS is a list of symbols (not
671 quoted) that specify under which conditions BODY is to be
672 executed. Valid symbols are:
674 entries: execute when there are entries in the database,
675 marked-entries: execute when there are marked entries in the database,
676 database: execute if there is a database,
677 no-database: execute if there is no database,
678 real-db: execute when there is a database and it is real,
679 virtual-db: execute when there is a database and it is virtual,
680 default: execute if all else fails.
682 Just like with COND, only one form is actually executed, the
683 first one that matches. If ENVIRONMENT contains more than one
684 condition, BODY is executed if they all match (i.e., the
685 conditions are AND'ed.)"
686 (declare (indent defun))
687 `(cond
688 ,@(mapcar #'(lambda (form)
689 (cons (if (= 1 (length (car form)))
690 (ebib-execute-helper (caar form))
691 `(and ,@(mapcar #'(lambda (env)
692 (ebib-execute-helper env))
693 (car form))))
694 (cdr form)))
695 forms)))
697 ;; the numeric prefix argument is 1 if the user gave no prefix argument at
698 ;; all. the raw prefix argument is not always a number. so we need to do
699 ;; our own conversion.
700 (defun ebib-prefix (num)
701 (when (numberp num)
702 num))
704 (defun ebib-called-with-prefix ()
705 "Returns T if the command was called with a prefix key."
706 (if (featurep 'xemacs)
707 (member (character-to-event ebib-prefix-key) (append (this-command-keys) nil))
708 (member (event-convert-list (list ebib-prefix-key))
709 (append (this-command-keys-vector) nil))))
711 (defmacro ebib-export-to-db (num message copy-fn)
712 "Exports data to another database.
713 NUM is the number of the database to which the data is to be copied.
715 MESSAGE is a string displayed in the echo area if the export was
716 succesful. It must contain a %d directive, which is used to
717 display the database number to which the entry was exported.
719 COPY-FN is the function that actually copies the relevant
720 data. It must take as argument the database to which the data is
721 to be copied. COPY-FN must return T if the copying was
722 successful, and NIL otherwise."
723 (let ((goal-db (make-symbol "goal-db")))
724 `(let ((,goal-db (nth (1- ,num) ebib-databases)))
725 (cond
726 ((not ,goal-db)
727 (error "Database %d does not exist" ,num))
728 ((edb-virtual ,goal-db)
729 (error "Database %d is virtual" ,num))
730 (t (when (funcall ,copy-fn ,goal-db)
731 (ebib-set-modified t ,goal-db)
732 (message ,message ,num)))))))
734 (defmacro ebib-cur-entry-key ()
735 "Returns the key of the current entry in EBIB-CUR-DB."
736 `(car (edb-cur-entry ebib-cur-db)))
738 (defmacro ebib-export-to-file (prompt-string message insert-fn)
739 "Exports data to a file.
740 PROMPT-STRING is the string that is used to ask for the filename
741 to export to. INSERT-FN must insert the data to be exported into
742 the current buffer: it is called within a WITH-TEMP-BUFFER, whose
743 contents is appended to the file the user enters.
745 MESSAGE is shown in the echo area when the export was
746 successful. It must contain a %s directive, which is used to
747 display the actual filename."
748 (let ((filename (make-symbol "filename")))
749 `(let ((insert-default-directory (not ebib-export-filename)))
750 (if-str (,filename (read-file-name
751 ,prompt-string "~/" nil nil ebib-export-filename))
752 (with-temp-buffer
753 (funcall ,insert-fn)
754 (append-to-file (point-min) (point-max) ,filename)
755 (setq ebib-export-filename ,filename))))))
757 (defun ebib-temp-window ()
758 "Returns a window to be used for temporary use."
759 (if (eq ebib-layout 'full)
760 (get-buffer-window ebib-entry-buffer)
761 ebib-pre-ebib-window))
763 (defun ebib-get-obl-fields (entry-type)
764 "Returns the obligatory fields of ENTRY-TYPE."
765 (nth 1 (assoc entry-type ebib-entry-types)))
767 (defun ebib-get-opt-fields (entry-type)
768 "Returns the optional fields of ENTRY-TYPE."
769 (nth 2 (assoc entry-type ebib-entry-types)))
771 (defun ebib-get-all-fields (entry-type)
772 "Returns all the fields of ENTRY-TYPE."
773 (cons 'type* (append (ebib-get-obl-fields entry-type)
774 (ebib-get-opt-fields entry-type)
775 ebib-additional-fields)))
777 (defmacro ebib-retrieve-entry (entry-key db)
778 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
779 `(gethash ,entry-key (edb-database ,db)))
781 (defun ebib-erase-buffer (buffer)
782 (set-buffer buffer)
783 (with-buffer-writable
784 (erase-buffer)))
786 (defun ebib-make-highlight (begin end buffer)
787 (let (highlight)
788 (if (featurep 'xemacs)
789 (progn
790 (setq highlight (make-extent begin end buffer))
791 (set-extent-face highlight 'highlight))
792 (progn
793 (setq highlight (make-overlay begin end buffer))
794 (overlay-put highlight 'face 'highlight)))
795 highlight))
797 (defun ebib-move-highlight (highlight begin end buffer)
798 (if (featurep 'xemacs)
799 (set-extent-endpoints highlight begin end buffer)
800 (move-overlay highlight begin end buffer)))
802 (defun ebib-highlight-start (highlight)
803 (if (featurep 'xemacs)
804 (extent-start-position highlight)
805 (overlay-start highlight)))
807 (defun ebib-highlight-end (highlight)
808 (if (featurep 'xemacs)
809 (extent-end-position highlight)
810 (overlay-end highlight)))
812 (defun ebib-delete-highlight (highlight)
813 (if (featurep 'xemacs)
814 (detach-extent highlight)
815 (delete-overlay highlight)))
817 (defun ebib-set-index-highlight ()
818 (set-buffer ebib-index-buffer)
819 (beginning-of-line)
820 (let ((beg (point)))
821 (if ebib-index-display-fields
822 (end-of-line)
823 (skip-chars-forward "^ "))
824 (ebib-move-highlight ebib-index-highlight beg (point) ebib-index-buffer)
825 (beginning-of-line)))
827 (defun ebib-set-fields-highlight ()
828 (set-buffer ebib-entry-buffer)
829 (beginning-of-line)
830 (let ((beg (point)))
831 (looking-at-goto-end "[^ \t\n\f]*")
832 (ebib-move-highlight ebib-fields-highlight beg (point) ebib-entry-buffer)
833 (beginning-of-line)))
835 (defun ebib-set-strings-highlight ()
836 (set-buffer ebib-strings-buffer)
837 (beginning-of-line)
838 (let ((beg (point)))
839 (looking-at-goto-end "[^ \t\n\f]*")
840 (ebib-move-highlight ebib-strings-highlight beg (point) ebib-strings-buffer)
841 (beginning-of-line)))
843 (defun ebib-display-entry (entry-key)
844 "Displays ENTRY-KEY in the index buffer at POINT."
845 (set-buffer ebib-index-buffer)
846 (insert (format "%-30s %s\n"
847 entry-key
848 (if ebib-index-display-fields
849 (let ((cur-entry-hash (ebib-retrieve-entry entry-key ebib-cur-db)))
850 (mapconcat #'(lambda (field)
852 (to-raw (gethash field cur-entry-hash))
853 ""))
854 ebib-index-display-fields
855 "; "))
856 ""))))
858 (defun ebib-redisplay-current-field ()
859 "Redisplays the contents of the current field in the entry buffer."
860 (set-buffer ebib-entry-buffer)
861 (if (eq ebib-current-field 'crossref)
862 (progn
863 (ebib-fill-entry-buffer)
864 (setq ebib-current-field 'crossref)
865 (re-search-forward "^crossref")
866 (ebib-set-fields-highlight))
867 (with-buffer-writable
868 (goto-char (ebib-highlight-start ebib-fields-highlight))
869 (let ((beg (point)))
870 (end-of-line)
871 (delete-region beg (point)))
872 (insert (format "%-17s " (symbol-name ebib-current-field))
873 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash))
874 (ebib-set-fields-highlight))))
876 (defun ebib-redisplay-current-string ()
877 "Redisplays the current string definition in the strings buffer."
878 (set-buffer ebib-strings-buffer)
879 (with-buffer-writable
880 (let ((str (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
881 (goto-char (ebib-highlight-start ebib-strings-highlight))
882 (let ((beg (point)))
883 (end-of-line)
884 (delete-region beg (point)))
885 (insert (format "%-18s %s" ebib-current-string
886 (if (multiline-p str)
887 (concat "+" (first-line str))
888 (concat " " str))))
889 (ebib-set-strings-highlight))))
891 (defun ebib-move-to-field (field direction)
892 "Moves the fields overlay to the line containing FIELD.
893 If DIRECTION is positive, searches forward, if DIRECTION is
894 negative, searches backward. If DIRECTION is 1 or -1, searches
895 from POINT, if DIRECTION is 2 or -2, searches from beginning or
896 end of buffer. If FIELD is not found in the entry buffer, the
897 overlay is not moved. FIELD must be a symbol."
899 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
901 (set-buffer ebib-entry-buffer)
902 (if (eq field 'type*)
903 (goto-char (point-min))
904 (multiple-value-bind (fn start limit) (if (>= direction 0)
905 (values 're-search-forward (point-min) (point-max))
906 (values 're-search-backward (point-max) (point-min)))
907 ;; make sure we can get back to our original position, if the field
908 ;; cannot be found in the buffer:
909 (let ((current-pos (point)))
910 (when (eq (logand direction 1) 0) ; if direction is even
911 (goto-char start))
912 (unless (funcall fn (concat "^" (symbol-name field)) limit t)
913 (goto-char current-pos)))))
914 (ebib-set-fields-highlight))
916 (defun ebib-create-collection (hashtable)
917 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
918 The keys of HASHTABLE must be either symbols or strings."
919 (let ((result nil))
920 (maphash #'(lambda (x y)
921 (setq result (cons (cons (symbol-or-string x)
923 result)))
924 hashtable)
925 result))
927 (defun ebib-get-field-value (field entry-key &optional db)
928 "Returns the value of FIELD in the entry ENTRY-KEY in DB.
929 Actually returns a list with the value of FIELD as the first
930 element. The second element is T if the value was obtained from a
931 cross-referenced entry.
933 If DB is nil, it defaults to the current database."
934 (unless db
935 (setq db ebib-cur-db))
936 (let ((entry (ebib-retrieve-entry entry-key db)))
937 (if entry
939 (let ((val (copy-sequence (gethash field entry))))
940 (if val
941 (values val nil)))
942 (let ((xref (ebib-retrieve-entry (to-raw (gethash 'crossref entry)) db)))
943 (if xref
944 (values (gethash field xref) t))))
945 (values nil nil))))
947 (defun ebib-get-field-highlighted (field current-entry &optional match-str)
948 ;; note: we need to work on a copy of the string, otherwise the highlights
949 ;; are made to the string as stored in the database. hence copy-sequence.
950 (let ((case-fold-search t)
951 (string (copy-sequence (gethash field current-entry)))
952 (raw " ")
953 (multiline " ")
954 (matched nil))
955 ;; we have to do a couple of things now:
956 ;; - remove {} or "" around the string, if they're there
957 ;; - search for match-str
958 ;; - properly adjust the string if it's multiline
959 ;; but all this is not necessary if there was no string
960 (if (null string)
961 (let* ((xref (to-raw (gethash 'crossref current-entry)))
962 (xref-entry (ebib-retrieve-entry xref ebib-cur-db)))
963 (when xref-entry
964 (setq string (copy-sequence (gethash field xref-entry)))
965 (if string
966 (setq string (propertize (to-raw string) 'face 'ebib-crossref-face 'fontified t))
967 (setq string ""))))
968 (if (raw-p string)
969 (setq raw "*")
970 (setq string (to-raw string))) ; we have to make the string look nice
971 (when match-str
972 (multiple-value-setq (string matched) (match-all match-str string))))
973 (when (multiline-p string)
974 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
975 ;; multiline is local and therefore the object it refers to should
976 ;; be GC'ed when the function returns. but for some reason, the
977 ;; plus sign is persistent, and if it's been highlighted as the
978 ;; result of a search, it stays that way.
979 (setq multiline (propertize "+" 'face nil))
980 (setq string (first-line string)))
981 (when (and matched
982 (string= multiline "+"))
983 (add-text-properties 0 1 '(face highlight) multiline))
984 (concat raw multiline string)))
986 (defun ebib-format-fields (entry fn &optional match-str)
987 (let* ((entry-type (gethash 'type* entry))
988 (obl-fields (ebib-get-obl-fields entry-type))
989 (opt-fields (ebib-get-opt-fields entry-type)))
990 (funcall fn (format "%-19s %s\n" "type" entry-type))
991 (mapc #'(lambda (fields)
992 (funcall fn "\n")
993 (mapcar #'(lambda (field)
994 (unless (and (get field 'ebib-hidden)
995 ebib-hide-hidden-fields)
996 (funcall fn (format "%-17s " field))
997 (funcall fn (or
998 (ebib-get-field-highlighted field entry match-str)
999 ""))
1000 (funcall fn "\n")))
1001 fields))
1002 (list obl-fields opt-fields ebib-additional-fields))))
1004 (defun ebib-fill-entry-buffer (&optional match-str)
1005 "Fills the entry buffer with the fields of the current entry.
1006 MATCH-STRING is a regexp that will be highlighted when it occurs in the
1007 field contents."
1008 (set-buffer ebib-entry-buffer)
1009 (with-buffer-writable
1010 (erase-buffer)
1011 (when (and ebib-cur-db ; do we have a database?
1012 (edb-keys-list ebib-cur-db) ; does it contain entries?
1013 (gethash (car (edb-cur-entry ebib-cur-db))
1014 (edb-database ebib-cur-db))) ; does the current entry exist?
1015 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db))
1016 (edb-database ebib-cur-db)) 'insert match-str)
1017 (setq ebib-current-field 'type*)
1018 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1019 (goto-char (point-min))
1020 (ebib-set-fields-highlight))))
1021 ;; (skip-chars-forward "^ "))))
1023 (defun ebib-set-modified (mod &optional db)
1024 "Sets the modified flag of the database DB to MOD.
1025 If DB is nil, it defaults to the current database, and the
1026 modified flag of the index buffer is also (re)set. MOD must be
1027 either T or NIL."
1028 (unless db
1029 (setq db ebib-cur-db))
1030 (setf (edb-modified db) mod)
1031 (when (eq db ebib-cur-db)
1032 (with-current-buffer ebib-index-buffer
1033 (set-buffer-modified-p mod))))
1035 (defun ebib-modified-p ()
1036 "Checks if any of the databases in Ebib were modified.
1037 Returns the first modified database, or NIL if none was modified."
1038 (let ((db (car ebib-databases)))
1039 (while (and db
1040 (not (edb-modified db)))
1041 (setq db (next-elem db ebib-databases)))
1042 db))
1044 (defun ebib-create-new-database (&optional db)
1045 "Creates a new database instance and returns it.
1046 If DB is set to a database, the new database is a copy of DB."
1047 (let ((new-db
1048 (if (edb-p db)
1049 (copy-edb db)
1050 (make-edb))))
1051 (setq ebib-databases (append ebib-databases (list new-db)))
1052 new-db))
1054 (defun ebib-match-paren-forward (limit)
1055 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
1056 This function handles parentheses () and braces {}. Does not
1057 search/move beyond LIMIT. Returns T if a matching parenthesis was
1058 found, NIL otherwise. If point was not at an opening parenthesis
1059 at all, NIL is returned and point is not moved. If point was at
1060 an opening parenthesis but no matching closing parenthesis was
1061 found, an error is logged and point is moved one character
1062 forward to allow parsing to continue."
1063 (cond
1064 ((eq (char-after) ?\{)
1065 (ebib-match-brace-forward limit))
1066 ((eq (char-after) ?\()
1067 ;; we wrap this in a condition-case because we need to log the error
1068 ;; message outside of the save-restriction, otherwise we get the wrong
1069 ;; line number.
1070 (condition-case nil
1071 (save-restriction
1072 (narrow-to-region (point) limit)
1073 ;; this is really a hack. we want to allow unbalanced parentheses in
1074 ;; field values (bibtex does), so we cannot use forward-list
1075 ;; here. for the same reason, looking for the matching paren by hand
1076 ;; is pretty complicated. however, balanced parentheses can only be
1077 ;; used to enclose entire entries (or @STRINGs or @PREAMBLEs) so we
1078 ;; can be pretty sure we'll find it right before the next @ at the
1079 ;; start of a line, or right before the end of the file.
1080 (re-search-forward "^@" nil 0)
1081 (skip-chars-backward "@ \n\t\f")
1082 (forward-char -1)
1083 (if (eq (char-after) ?\))
1085 (goto-char (1+ (point-min)))
1086 (error "")))
1087 (error (ebib-log 'error "Error in line %d: Matching closing parenthesis not found!" (line-number-at-pos))
1088 nil)))
1089 (t nil)))
1091 (defun ebib-match-delim-forward (limit)
1092 "Moves forward to the closing delimiter matching the opening delimiter at POINT.
1093 This function handles braces {} and double quotes \"\". Does not
1094 search/move beyond LIMIT. Returns T if a matching delimiter was
1095 found, NIL otherwise. If point was not at an opening delimiter at
1096 all, NIL is returned and point is not moved. If point was at an
1097 opening delimiter but no matching closing delimiter was found, an
1098 error is logged and point is moved one character forward to allow
1099 parsing to continue."
1100 (cond
1101 ((eq (char-after) ?\")
1102 (ebib-match-quote-forward limit))
1103 ((eq (char-after) ?\{)
1104 (ebib-match-brace-forward limit))
1105 (t nil)))
1107 (defun ebib-match-brace-forward (limit)
1108 "Moves forward to the closing brace matching the opening brace at POINT.
1109 Does not search/move beyond LIMIT. Returns T if a matching brace
1110 was found, NIL otherwise. If point was not at an opening brace at
1111 all, NIL is returned and point is not moved. If point was at an
1112 opening brace but no matching closing brace was found, an error
1113 is logged and point is moved one character forward to allow
1114 parsing to continue."
1115 (when (eq (char-after) ?\{) ; make sure we're really on a brace, otherwise return nil
1116 (condition-case nil
1117 (save-restriction
1118 (narrow-to-region (point) limit)
1119 (progn
1120 (forward-list)
1121 ;; all of ebib expects that point moves to the closing
1122 ;; parenthesis, not right after it, so we adjust.
1123 (forward-char -1)
1124 t)) ; return t because a matching brace was found
1125 (error (progn
1126 (ebib-log 'error "Error in line %d: Matching closing brace not found!" (line-number-at-pos))
1127 (forward-char 1)
1128 nil)))))
1130 (defun ebib-match-quote-forward (limit)
1131 "Moves to the closing double quote matching the quote at POINT.
1132 Does not search/move beyond LIMIT. Returns T if a matching quote
1133 was found, NIL otherwise. If point was not at a double quote at
1134 all, NIL is returned and point is not moved. If point was at a
1135 quote but no matching closing quote was found, an error is logged
1136 and point is moved one character forward to allow parsing to
1137 continue."
1138 (when (eq (char-after (point)) ?\") ; make sure we're on a double quote.
1139 (condition-case nil
1140 (save-restriction
1141 (narrow-to-region (point) limit)
1142 (while (progn
1143 (forward-char) ; move forward because we're on a double quote
1144 (skip-chars-forward "^\"") ; search the next double quote
1145 (eq (char-before) ?\\))) ; if it's preceded by a backslash, keep on searching
1146 (or (eq (char-after) ?\")
1147 (progn
1148 (goto-char (1+ (point-min)))
1149 (error ""))))
1150 (error (ebib-log 'error "Error in line %d: Matching closing quote not found!" (line-number-at-pos))
1151 nil))))
1153 (defun ebib-insert-entry (entry-key fields db &optional sort timestamp)
1154 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
1155 Optional argument SORT indicates whether the KEYS-LIST must be
1156 sorted after insertion. Default is NIL. Optional argument
1157 TIMESTAMP indicates whether a timestamp is to be added to the
1158 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
1159 must also be set to T."
1160 (when (and timestamp ebib-use-timestamp)
1161 (puthash 'timestamp (from-raw (format-time-string ebib-timestamp-format)) fields))
1162 (puthash entry-key fields (edb-database db))
1163 (ebib-set-modified t db)
1164 (setf (edb-n-entries db) (1+ (edb-n-entries db)))
1165 (setf (edb-keys-list db)
1166 (if sort
1167 (sort (cons entry-key (edb-keys-list db)) 'string<)
1168 (cons entry-key (edb-keys-list db)))))
1170 (defun ebib-insert-string (abbr string db &optional sort)
1171 "Stores the @STRING definition defined by ABBR and STRING into DB.
1172 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
1173 after insertion. When loading or merging a file, for example, it is more
1174 economic to sort KEYS-LIST manually after all entries in the file have been
1175 added."
1176 (puthash abbr (from-raw string) (edb-strings db))
1177 (ebib-set-modified t db)
1178 (setf (edb-strings-list db)
1179 (if sort
1180 (sort (cons abbr (edb-strings-list db)) 'string<)
1181 (cons abbr (edb-strings-list db)))))
1183 (defun ebib-search-key-in-buffer (entry-key)
1184 "Searches ENTRY-KEY in the index buffer.
1185 Moves point to the first character of the key and returns point."
1186 (goto-char (point-min))
1187 (re-search-forward (concat "^" entry-key))
1188 (beginning-of-line)
1189 (point))
1191 ;; when we sort entries, we either use string< on the entry keys, or
1192 ;; ebib-entry<, if the user has defined a sort order.
1194 (defun ebib-entry< (x y)
1195 "Returns T if entry X is smaller than entry Y.
1196 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1197 and Y should be keys of entries in the current database."
1198 (let* ((sort-list ebib-sort-order)
1199 (sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1200 (sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1201 (while (and sort-list
1202 (string= sortstring-x sortstring-y))
1203 (setq sort-list (cdr sort-list))
1204 (setq sortstring-x (to-raw (ebib-get-sortstring x (car sort-list))))
1205 (setq sortstring-y (to-raw (ebib-get-sortstring y (car sort-list)))))
1206 (if (and sortstring-x sortstring-y)
1207 (string< sortstring-x sortstring-y)
1208 (string< x y))))
1210 (defun ebib-get-sortstring (entry-key sortkey-list)
1211 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1212 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1213 is a list of fields that are considered in order for the sort value."
1214 (let ((sort-string nil))
1215 (while (and sortkey-list
1216 (null (setq sort-string (gethash (car sortkey-list)
1217 (ebib-retrieve-entry entry-key ebib-cur-db)))))
1218 (setq sortkey-list (cdr sortkey-list)))
1219 sort-string))
1221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1222 ;; main program execution ;;
1223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1225 ;;;###autoload
1226 (defun ebib (&optional key)
1227 "Ebib, a BibTeX database manager.
1228 Optional argument key specifies the entry of the current database
1229 that is to be displayed."
1230 (interactive)
1231 (if (or (equal (window-buffer) ebib-index-buffer)
1232 (equal (window-buffer) ebib-entry-buffer))
1233 (error "Ebib already active")
1234 ;; we save the buffer from which ebib is called
1235 (setq ebib-push-buffer (current-buffer))
1236 ;; initialize ebib if required
1237 (unless ebib-initialized
1238 (ebib-init)
1239 (if ebib-preload-bib-files
1240 (mapc #'(lambda (file)
1241 (ebib-load-bibtex-file file))
1242 ebib-preload-bib-files)))
1243 ;; if ebib is visible, we just switch to the index buffer
1244 (let ((index-window (get-buffer-window ebib-index-buffer)))
1245 (if index-window
1246 (select-window index-window nil)
1247 (ebib-setup-windows)))
1248 ;; if ebib is called with an argument, we look for it
1249 (when key
1250 (let ((exists? (member key (edb-keys-list ebib-cur-db))))
1251 (if exists?
1252 (progn
1253 (setf (edb-cur-entry ebib-cur-db) exists?)
1254 (set-buffer ebib-index-buffer)
1255 (goto-char (point-min))
1256 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
1257 (ebib-select-entry))
1258 (message "No entry `%s' in current database " key))))))
1260 (defun ebib-setup-windows ()
1261 "Creates the window configuration we want for Ebib in the
1262 current window."
1263 ;; we save the current window configuration.
1264 (setq ebib-saved-window-config (current-window-configuration))
1265 (if (eq ebib-layout 'full)
1266 (delete-other-windows)
1267 (setq ebib-pre-ebib-window (selected-window))
1268 (let ((ebib-window (split-window (selected-window) (- (window-width) ebib-width) t)))
1269 (select-window ebib-window nil)))
1270 (let* ((index-window (selected-window))
1271 (entry-window (split-window index-window ebib-index-window-size)))
1272 (switch-to-buffer ebib-index-buffer)
1273 (set-window-buffer entry-window ebib-entry-buffer)
1274 (unless (eq ebib-layout 'full)
1275 (set-window-dedicated-p index-window t)
1276 (set-window-dedicated-p entry-window t))))
1278 (defun ebib-init ()
1279 "Initialises Ebib.
1280 This function sets all variables to their initial values, creates the
1281 buffers and reads the rc file."
1282 (setq ebib-cur-entry-hash nil
1283 ebib-current-field nil
1284 ebib-minibuf-hist nil
1285 ebib-saved-window-config nil)
1286 (put 'timestamp 'ebib-hidden t)
1287 (load "~/.ebibrc" t)
1288 (ebib-create-buffers)
1289 (setq ebib-index-highlight (ebib-make-highlight 1 1 ebib-index-buffer))
1290 (setq ebib-fields-highlight (ebib-make-highlight 1 1 ebib-entry-buffer))
1291 (setq ebib-strings-highlight (ebib-make-highlight 1 1 ebib-strings-buffer))
1292 (setq ebib-initialized t))
1294 (defun ebib-create-buffers ()
1295 "Creates the buffers for Ebib."
1296 ;; first we create a buffer for multiline editing. this one does *not*
1297 ;; have a name beginning with a space, because undo-info is normally
1298 ;; present in an edit buffer.
1299 (setq ebib-multiline-buffer (get-buffer-create "*Ebib-edit*"))
1300 (set-buffer ebib-multiline-buffer)
1301 (funcall ebib-multiline-major-mode)
1302 (ebib-multiline-mode t)
1303 ;; then we create a buffer to hold the fields of the current entry.
1304 (setq ebib-entry-buffer (get-buffer-create " *Ebib-entry*"))
1305 (set-buffer ebib-entry-buffer)
1306 (ebib-entry-mode)
1307 ;; then we create a buffer to hold the @STRING definitions
1308 (setq ebib-strings-buffer (get-buffer-create " *Ebib-strings*"))
1309 (set-buffer ebib-strings-buffer)
1310 (ebib-strings-mode)
1311 ;; the log buffer
1312 (setq ebib-log-buffer (get-buffer-create " *Ebib-log*"))
1313 (set-buffer ebib-log-buffer)
1314 (erase-buffer)
1315 (insert "Ebib log messages\n\n(Press C-v or SPACE to scroll down, M-v or `b' to scroll up, `q' to quit.)\n\n\n")
1316 (ebib-log-mode)
1317 ;; and lastly we create a buffer for the entry keys.
1318 (setq ebib-index-buffer (get-buffer-create " none"))
1319 (set-buffer ebib-index-buffer)
1320 (ebib-index-mode))
1322 (defun ebib-quit ()
1323 "Quits Ebib.
1324 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1325 (interactive)
1326 (when (if (ebib-modified-p)
1327 (yes-or-no-p "There are modified databases. Quit anyway? ")
1328 (y-or-n-p "Quit Ebib? "))
1329 (mapc #'(lambda (buffer)
1330 (kill-buffer buffer))
1331 (list ebib-entry-buffer
1332 ebib-index-buffer
1333 ebib-strings-buffer
1334 ebib-multiline-buffer
1335 ebib-log-buffer))
1336 (setq ebib-databases nil
1337 ebib-index-buffer nil
1338 ebib-entry-buffer nil
1339 ebib-initialized nil
1340 ebib-index-highlight nil
1341 ebib-fields-highlight nil
1342 ebib-strings-highlight nil
1343 ebib-export-filename nil
1344 ebib-pre-ebib-window nil)
1345 (set-window-configuration ebib-saved-window-config)
1346 (message "")))
1348 (defun ebib-kill-emacs-query-function ()
1349 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1350 killed and the database has been modified."
1351 (if (not (ebib-modified-p))
1353 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1354 (progn
1355 (ebib-save-all-databases)
1357 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1359 (add-hook 'kill-emacs-query-functions 'ebib-kill-emacs-query-function)
1361 ;;;;;;;;;;;;;;;;
1362 ;; index-mode ;;
1363 ;;;;;;;;;;;;;;;;
1365 (eval-and-compile
1366 (define-prefix-command 'ebib-prefix-map)
1367 (suppress-keymap ebib-prefix-map)
1368 (defvar ebib-prefixed-functions '(ebib-delete-entry
1369 ebib-latex-entries
1370 ebib-mark-entry
1371 ebib-print-entries
1372 ebib-push-bibtex-key
1373 ebib-export-entry)))
1375 ;; macro to redefine key bindings.
1377 (defmacro ebib-key (buffer key &optional command)
1378 (cond
1379 ((eq buffer 'index)
1380 (let ((one `(define-key ebib-index-mode-map ,key (quote ,command)))
1381 (two (when (or (null command)
1382 (member command ebib-prefixed-functions))
1383 `(define-key ebib-prefix-map ,key (quote ,command)))))
1384 (if two
1385 `(progn ,one ,two)
1386 one)))
1387 ((eq buffer 'entry)
1388 `(define-key ebib-entry-mode-map ,key (quote ,command)))
1389 ((eq buffer 'strings)
1390 `(define-key ebib-strings-mode-map ,key (quote ,command)))
1391 ((eq buffer 'mark-prefix)
1392 `(progn
1393 (define-key ebib-index-mode-map (format "%c" ebib-prefix-key) nil)
1394 (define-key ebib-index-mode-map ,key 'ebib-prefix-map)
1395 (setq ebib-prefix-key (string-to-char ,key))))
1396 ((eq buffer 'multiline)
1397 `(progn
1398 (define-key ebib-multiline-mode-map "\C-c" nil)
1399 (mapc #'(lambda (command)
1400 (define-key ebib-multiline-mode-map (format "\C-c%s%c" ,key (car command)) (cdr command)))
1401 '((?q . ebib-quit-multiline-edit)
1402 (?c . ebib-cancel-multiline-edit)
1403 (?s . ebib-save-from-multiline-edit)))
1404 (setq ebib-multiline-key (string-to-char ,key))))))
1406 (defvar ebib-index-mode-map
1407 (let ((map (make-keymap)))
1408 (suppress-keymap map)
1409 map)
1410 "Keymap for the ebib index buffer.")
1412 ;; we define the keys with ebib-key rather than with define-key, because
1413 ;; that automatically sets up ebib-prefix-map as well.
1414 (ebib-key index [up] ebib-prev-entry)
1415 (ebib-key index [down] ebib-next-entry)
1416 (ebib-key index [right] ebib-next-database)
1417 (ebib-key index [left] ebib-prev-database)
1418 (ebib-key index [prior] ebib-index-scroll-down)
1419 (ebib-key index [next] ebib-index-scroll-up)
1420 (ebib-key index [home] ebib-goto-first-entry)
1421 (ebib-key index [end] ebib-goto-last-entry)
1422 (ebib-key index [return] ebib-select-entry)
1423 (ebib-key index " " ebib-index-scroll-up)
1424 (ebib-key index "/" ebib-search)
1425 (ebib-key index "&" ebib-virtual-db-and)
1426 (ebib-key index "|" ebib-virtual-db-or)
1427 (ebib-key index "~" ebib-virtual-db-not)
1428 (ebib-key index ";" ebib-prefix-map)
1429 (ebib-key index "a" ebib-add-entry)
1430 (ebib-key index "b" ebib-index-scroll-down)
1431 (ebib-key index "c" ebib-close-database)
1432 (ebib-key index "d" ebib-delete-entry)
1433 (ebib-key index "e" ebib-edit-entry)
1434 (ebib-key index "E" ebib-edit-keyname)
1435 (ebib-key index "f" ebib-view-file)
1436 (ebib-key index "F" ebib-follow-crossref)
1437 (ebib-key index "g" ebib-goto-first-entry)
1438 (ebib-key index "G" ebib-goto-last-entry)
1439 (ebib-key index "h" ebib-index-help)
1440 (ebib-key index "j" ebib-next-entry)
1441 (ebib-key index "J" ebib-switch-to-database)
1442 (ebib-key index "k" ebib-prev-entry)
1443 (ebib-key index "K" ebib-generate-autokey)
1444 (ebib-key index "l" ebib-show-log)
1445 (ebib-key index "m" ebib-mark-entry)
1446 (ebib-key index "n" ebib-search-next)
1447 (ebib-key index "N" ebib-search-crossref)
1448 (ebib-key index [(control n)] ebib-next-entry)
1449 (ebib-key index [(meta n)] ebib-index-scroll-up)
1450 (ebib-key index "o" ebib-load-bibtex-file)
1451 (ebib-key index "p" ebib-push-bibtex-key)
1452 (ebib-key index [(control p)] ebib-prev-entry)
1453 (ebib-key index [(meta p)] ebib-index-scroll-down)
1454 (ebib-key index "P" ebib-edit-preamble)
1455 (ebib-key index "q" ebib-quit)
1456 (ebib-key index "s" ebib-save-current-database)
1457 (ebib-key index "S" ebib-edit-strings)
1458 (ebib-key index "u" ebib-browse-url)
1459 (ebib-key index "V" ebib-print-filter)
1460 (ebib-key index "x" ebib-export-entry)
1461 (ebib-key index "\C-xb" ebib-leave-ebib-windows)
1462 (ebib-key index "\C-xk" ebib-quit)
1463 (ebib-key index "X" ebib-export-preamble)
1464 (ebib-key index "z" ebib-leave-ebib-windows)
1465 (ebib-key index "Z" ebib-lower)
1467 (defun ebib-switch-to-database-nth (key)
1468 (interactive (list (if (featurep 'xemacs)
1469 (event-key last-command-event)
1470 last-command-event)))
1471 (ebib-switch-to-database (- (if (featurep 'xemacs)
1472 (char-to-int key)
1473 key) 48)))
1475 (mapc #'(lambda (key)
1476 (define-key ebib-index-mode-map (format "%d" key)
1477 'ebib-switch-to-database-nth))
1478 '(1 2 3 4 5 6 7 8 9))
1480 (define-derived-mode ebib-index-mode
1481 fundamental-mode "Ebib-index"
1482 "Major mode for the Ebib index buffer."
1483 (setq buffer-read-only t)
1484 (setq truncate-lines t))
1486 (easy-menu-define ebib-index-menu ebib-index-mode-map "Ebib index menu"
1487 '("Ebib"
1488 ["Open Database..." ebib-load-bibtex-file t]
1489 ["Merge Database..." ebib-merge-bibtex-file (and ebib-cur-db (not (edb-virtual ebib-cur-db)))]
1490 ["Save Database" ebib-save-current-database (and ebib-cur-db
1491 (edb-modified ebib-cur-db))]
1492 ["Save All Databases" ebib-save-all-databases (ebib-modified-p)]
1493 ["Save Database As..." ebib-write-database ebib-cur-db]
1494 ["Close Database" ebib-close-database ebib-cur-db]
1495 "--"
1496 ("Entry"
1497 ["Add" ebib-add-entry (and ebib-cur-db (not (edb-virtual ebib-cur-db)))]
1498 ["Edit" ebib-edit-entry (and ebib-cur-db
1499 (edb-cur-entry ebib-cur-db)
1500 (not (edb-virtual ebib-cur-db)))]
1501 ["Delete" ebib-delete-entry (and ebib-cur-db
1502 (edb-cur-entry ebib-cur-db)
1503 (not (edb-virtual ebib-cur-db)))])
1504 ["Edit Strings" ebib-edit-strings (and ebib-cur-db (not (edb-virtual ebib-cur-db)))]
1505 ["Edit Preamble" ebib-edit-preamble (and ebib-cur-db (not (edb-virtual ebib-cur-db)))]
1506 "--"
1507 ["Open URL" ebib-browse-url (gethash ebib-standard-url-field ebib-cur-entry-hash)]
1508 ["View File" ebib-view-file (gethash ebib-standard-file-field ebib-cur-entry-hash)]
1509 ("Print Entries"
1510 ["As Bibliography" ebib-latex-entries (and ebib-cur-db (not (edb-virtual ebib-cur-db)))]
1511 ["As Index Cards" ebib-print-entries ebib-cur-db]
1512 ["Print Multiline Fields" ebib-toggle-print-multiline :enable t
1513 :style toggle :selected ebib-print-multiline]
1514 ["Print Cards on Separate Pages" ebib-toggle-print-newpage :enable t
1515 :style toggle :selected ebib-print-newpage])
1516 "--"
1517 ("Options"
1518 ["Show Hidden Fields" ebib-toggle-hidden :enable t
1519 :style toggle :selected (not ebib-hide-hidden-fields)]
1520 ["Use Timestamp" ebib-toggle-timestamp :enable t
1521 :style toggle :selected ebib-use-timestamp]
1522 ["Save Cross-Referenced Entries First" ebib-toggle-xrefs-first :enable t
1523 :style toggle :selected ebib-save-xrefs-first]
1524 ["Allow Identical Fields" ebib-toggle-identical-fields :enable t
1525 :style toggle :selected ebib-allow-identical-fields]
1526 ["Full Layout" ebib-toggle-layout :enable t
1527 :style toggle :selected (eq ebib-layout 'full)]
1528 ["Modify Entry Types" ebib-customize-entry-types t]
1529 ["Customize Ebib" ebib-customize t])
1530 ["View Log Buffer" ebib-show-log t]
1531 ["Lower Ebib" ebib-lower t]
1532 ["Quit" ebib-quit t]
1533 ["Help on Ebib" ebib-info t]))
1535 (easy-menu-add ebib-index-menu ebib-index-mode-map)
1537 (defun ebib-fill-index-buffer ()
1538 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1539 If EBIB-CUR-DB is nil, the buffer is just erased and its name set
1540 to \"none\"."
1541 (set-buffer ebib-index-buffer)
1542 (let ((buffer-read-only nil))
1543 (erase-buffer)
1544 (if ebib-cur-db
1545 (progn
1546 ;; we may call this function when there are no entries in the
1547 ;; database. if so, we don't need to do this:
1548 (when (edb-cur-entry ebib-cur-db)
1549 (mapc #'(lambda (entry)
1550 (ebib-display-entry entry)
1551 (when (member entry (edb-marked-entries ebib-cur-db))
1552 (save-excursion
1553 (let ((beg (progn
1554 (beginning-of-line)
1555 (forward-line -1)
1556 (point))))
1557 (skip-chars-forward "^ ")
1558 (add-text-properties beg (point) '(face ebib-marked-face))))))
1559 (edb-keys-list ebib-cur-db))
1560 (goto-char (point-min))
1561 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
1562 (beginning-of-line)
1563 (ebib-set-index-highlight))
1564 (set-buffer-modified-p (edb-modified ebib-cur-db))
1565 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1566 (length (member ebib-cur-db ebib-databases)))))
1567 (edb-name ebib-cur-db))))
1568 (rename-buffer " none"))))
1570 (defun ebib-customize ()
1571 "Switches to Ebib's customisation group."
1572 (interactive)
1573 (ebib-lower)
1574 (customize-group 'ebib))
1576 (defun ebib-customize-entry-types ()
1577 "Customizes EBIB-ENTRY-TYPES."
1578 (interactive)
1579 (ebib-lower)
1580 (customize-variable 'ebib-entry-types))
1582 (defun ebib-log (type format-string &rest args)
1583 "Writes a message to Ebib's log buffer.
1584 TYPE (a symbol) is the type of message. It can be LOG, which
1585 writes the message to the log buffer only; MESSAGE, which writes
1586 the message to the log buffer and outputs it with the function
1587 MESSAGE; WARNING, which logs the message and sets the variable
1588 EBIB-LOG-ERROR to 0; or ERROR, which logs the message and sets
1589 the variable EBIB-LOG-ERROR to 1. The latter two can be used to
1590 signal the user to check the log for warnings or errors."
1591 (with-current-buffer ebib-log-buffer
1592 (cond
1593 ((eq type 'warning)
1594 (or ebib-log-error ; if ebib-error-log is already set to 1, we don't want to overwrite it!
1595 (setq ebib-log-error 0)))
1596 ((eq type 'error)
1597 (setq ebib-log-error 1))
1598 ((eq type 'message)
1599 (apply 'message format-string args)))
1600 (insert (apply 'format (concat format-string "\n") args))))
1602 (defun ebib-load-bibtex-file (&optional file)
1603 "Loads a BibTeX file into Ebib."
1604 (interactive)
1605 (unless file
1606 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1607 (setq ebib-cur-db (ebib-create-new-database))
1608 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1609 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1610 (setq ebib-log-error nil) ; we haven't found any errors
1611 (ebib-log 'log "%s: Opening file %s" (format-time-string "%d %b %Y, %H:%M:%S") (edb-filename ebib-cur-db))
1612 ;; first, we empty the buffers
1613 (ebib-erase-buffer ebib-index-buffer)
1614 (ebib-erase-buffer ebib-entry-buffer)
1615 (if (file-readable-p file)
1616 ;; if the user entered the name of an existing file, we load it
1617 ;; by putting it in a buffer and then parsing it.
1618 (with-temp-buffer
1619 (with-syntax-table ebib-syntax-table
1620 (insert-file-contents file)
1621 ;; if the user makes any changes, we'll want to create a back-up.
1622 (setf (edb-make-backup ebib-cur-db) t)
1623 (let ((result (ebib-find-bibtex-entries nil)))
1624 (setf (edb-n-entries ebib-cur-db) (car result))
1625 (when (edb-keys-list ebib-cur-db)
1626 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1627 (when (edb-strings-list ebib-cur-db)
1628 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1629 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1630 ;; and fill the buffers. note that filling a buffer also makes
1631 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1632 ;; later.
1633 (ebib-set-modified nil)
1634 (ebib-fill-entry-buffer)
1635 ;; and now we tell the user the result
1636 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1637 (car result)
1638 (cadr result)
1639 (if (caddr result)
1641 "no")))))
1642 ;; if the file does not exist, we need to issue a message.
1643 (ebib-log 'message "(New file)"))
1644 ;; what we have to do in *any* case, is fill the index buffer. (this
1645 ;; even works if there are no keys in the database, e.g. when the
1646 ;; user opened a new file or if no BibTeX entries were found.
1647 (ebib-fill-index-buffer)
1648 (when ebib-log-error
1649 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1650 (ebib-log 'log "\n\f\n"))
1652 (defun ebib-merge-bibtex-file ()
1653 "Merges a BibTeX file into the database."
1654 (interactive)
1655 (unless (edb-virtual ebib-cur-db)
1656 (if (not ebib-cur-db)
1657 (error "No database loaded. Use `o' to open a database")
1658 (let ((file (read-file-name "File to merge: ")))
1659 (setq ebib-log-error nil) ; we haven't found any errors
1660 (ebib-log 'log "%s: Merging file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") (edb-filename ebib-cur-db))
1661 (with-temp-buffer
1662 (with-syntax-table ebib-syntax-table
1663 (insert-file-contents file)
1664 (let ((n (ebib-find-bibtex-entries t)))
1665 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1666 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1667 (when (edb-strings-list ebib-cur-db)
1668 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1669 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1670 (ebib-fill-entry-buffer)
1671 (ebib-fill-index-buffer)
1672 (ebib-set-modified t)
1673 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1674 (car n)
1675 (cadr n)
1676 (if (caddr n)
1678 "no"))
1679 (when ebib-log-error
1680 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1681 (ebib-log 'log "\n\f\n"))))))))
1683 (defun ebib-find-bibtex-entries (timestamp)
1684 "Finds the BibTeX entries in the current buffer.
1685 The search is started at the beginnig of the buffer. All entries
1686 found are stored in the hash table DATABASE of
1687 EBIB-CUR-DB. Returns a three-element list: the first element is
1688 the number of entries found, the second the number of @STRING
1689 definitions, and the third is T or NIL, indicating whether a
1690 @PREAMBLE was found.
1692 TIMESTAMP indicates whether a timestamp is to be added to each
1693 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1694 is set to T."
1695 (let ((n-entries 0)
1696 (n-strings 0)
1697 (preamble nil))
1698 (goto-char (point-min))
1699 (while (re-search-forward "^@" nil t) ; find the next entry
1700 (let ((beg (point)))
1701 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[[:space:]]*[\(\{]") 1)
1702 (let ((entry-type (downcase (buffer-substring-no-properties beg (point)))))
1703 (looking-at-goto-end "[[:space:]]*[\(\{]")
1704 (cond
1705 ((equal entry-type "string") ; string and preamble must be treated differently
1706 (if (ebib-read-string)
1707 (setq n-strings (1+ n-strings))))
1708 ((equal entry-type "preamble")
1709 (when (ebib-read-preamble)
1710 (setq preamble t)))
1711 ((equal entry-type "comment") ; ignore comments
1712 (ebib-log 'log "Comment at line %d ignored" (line-number-at-pos))
1713 (ebib-match-paren-forward (point-max)))
1714 ((assoc (intern-soft entry-type) ebib-entry-types) ; if the entry type has been defined
1715 (if (ebib-read-entry entry-type timestamp)
1716 (setq n-entries (1+ n-entries))))
1717 ;; anything else we report as an unknown entry type.
1718 (t (ebib-log 'warning "Line %d: Unknown entry type `%s'. Skipping." (line-number-at-pos) entry-type)
1719 (ebib-match-paren-forward (point-max))))))))
1720 (list n-entries n-strings preamble)))
1722 (defun ebib-read-string ()
1723 "Reads the @STRING definition beginning at the line POINT is on.
1724 If a proper abbreviation and string are found, they are stored in the
1725 database. Returns the string if one was read, nil otherwise."
1726 (let ((limit (save-excursion ; we find the matching end parenthesis
1727 (backward-char)
1728 (ebib-match-paren-forward (point-max))
1729 (point))))
1730 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1731 (let ((beg (point)))
1732 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1733 (if-str (abbr (buffer-substring-no-properties beg (point)))
1734 (progn
1735 (skip-chars-forward "^\"{" limit)
1736 (let ((beg (point)))
1737 (if-str (string (if (ebib-match-delim-forward limit)
1738 (buffer-substring-no-properties beg (1+ (point)))
1739 nil))
1740 (if (member abbr (edb-strings-list ebib-cur-db))
1741 (ebib-log 'warning (format "Line %d: @STRING definition `%s' duplicated. Skipping."
1742 (line-number-at-pos) abbr))
1743 (ebib-insert-string abbr string ebib-cur-db))))))))))
1745 (defun ebib-read-preamble ()
1746 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1747 If there was already another @PREAMBLE definition, the new one is
1748 added to the existing one with a hash sign `#' between them."
1749 (let ((beg (point)))
1750 (forward-char -1)
1751 (when (ebib-match-paren-forward (point-max))
1752 (let ((text (buffer-substring-no-properties beg (point))))
1753 (if (edb-preamble ebib-cur-db)
1754 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1755 (setf (edb-preamble ebib-cur-db) text))))))
1757 (defun ebib-read-entry (entry-type &optional timestamp)
1758 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1759 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1760 otherwise. Optional argument TIMESTAMP indicates whether a
1761 timestamp is to be added. (Whether a timestamp is actually added,
1762 also depends on EBIB-USE-TIMESTAMP.)"
1763 (let ((entry-limit (save-excursion
1764 (backward-char)
1765 (ebib-match-paren-forward (point-max))
1766 (point)))
1767 (beg (progn
1768 (skip-chars-forward " \n\t\f") ; note the space!
1769 (point))))
1770 (when (looking-at-goto-end (concat "\\("
1771 ebib-bibtex-identifier
1772 "\\)[ \t\n\f]*,")
1773 1) ; this delimits the entry key
1774 (let ((entry-key (buffer-substring-no-properties beg (point))))
1775 (if (member entry-key (edb-keys-list ebib-cur-db))
1776 (ebib-log 'warning "Line %d: Entry `%s' duplicated. Skipping." (line-number-at-pos) entry-key)
1777 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1778 (when fields ; if fields were found, we store them, and return T.
1779 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1780 t)))))))
1782 (defun ebib-find-bibtex-fields (entry-type limit)
1783 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1784 Returns a hash table containing all the fields and values, or NIL
1785 if none were found. ENTRY-TYPE is the type of the entry, which
1786 will be recorded in the hash table. Before the search starts,
1787 POINT is moved back to the beginning of the line."
1788 (beginning-of-line)
1789 ;; we assign a function to fn in order to avoid putting the test on
1790 ;; ebib-allow-identical-fields in the while loop, where it would get
1791 ;; tested with every field being read.
1792 (let ((fn (if (not ebib-allow-identical-fields)
1793 (symbol-function 'puthash)
1794 #'(lambda (field-type field-contents fields)
1795 (let ((existing-contents (gethash field-type fields)))
1796 (puthash field-type (if existing-contents
1797 (from-raw (concat (to-raw existing-contents)
1798 "; "
1799 (to-raw field-contents)))
1800 field-contents)
1801 fields))))))
1802 (let ((fields (make-hash-table :size 15)))
1803 (while (progn
1804 (skip-chars-forward "^," limit) ; we must move to the next comma,
1805 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1806 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1807 (let ((beg (point)))
1808 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1809 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1810 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1811 (let ((field-contents (ebib-read-field-contents limit)))
1812 (when field-contents
1813 (funcall fn field-type field-contents fields))))))))
1814 (when (> (hash-table-count fields) 0)
1815 (puthash 'type* entry-type fields)
1816 fields))))
1818 (defun ebib-read-field-contents (limit)
1819 "Reads the contents of a BibTeX field from a .bib file.
1820 LIMIT indicates the end of the entry, beyond which the function will not
1821 search."
1822 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1823 (let ((beg (point)))
1824 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1826 (defun ebib-find-end-of-field (limit)
1827 "Moves POINT to the end of a field's contents and returns POINT.
1828 The contents of a field is delimited by a comma or by the closing brace of
1829 the entry. The latter is at position LIMIT."
1830 (while (and (not (eq (char-after) ?\,))
1831 (< (point) limit))
1832 (ebib-match-delim-forward limit) ; check if we're on a delimiter and if so, jump to the matching closing delimiter
1833 (forward-char 1))
1834 (if (= (point) limit)
1835 (skip-chars-backward " \n\t\f"))
1836 (point))
1838 (defun ebib-leave-ebib-windows ()
1839 "Leaves the Ebib windows, lowering them if necessary."
1840 (interactive)
1841 (ebib-lower t))
1843 (defun ebib-lower (&optional soft)
1844 "Hides the Ebib windows.
1845 If optional argument SOFT is non-nil, just switch to a non-Ebib
1846 buffer if Ebib is not occupying the entire frame."
1847 (interactive)
1848 (unless (member (window-buffer) (list ebib-index-buffer
1849 ebib-entry-buffer
1850 ebib-strings-buffer
1851 ebib-multiline-buffer
1852 ebib-log-buffer))
1853 (error "Ebib is not active "))
1854 (if (and soft
1855 (not (eq ebib-layout 'full)))
1856 (select-window ebib-pre-ebib-window nil)
1857 (set-window-configuration ebib-saved-window-config))
1858 (mapc #'(lambda (buffer)
1859 (bury-buffer buffer))
1860 (list ebib-index-buffer
1861 ebib-entry-buffer
1862 ebib-strings-buffer
1863 ebib-multiline-buffer
1864 ebib-log-buffer)))
1866 (defun ebib-prev-entry ()
1867 "Moves to the previous BibTeX entry."
1868 (interactive)
1869 (ebib-execute-when
1870 ((entries)
1871 ;; if the current entry is the first entry,
1872 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1873 (beep) ; just beep.
1874 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1875 (1+ (length (edb-cur-entry ebib-cur-db)))))
1876 (goto-char (ebib-highlight-start ebib-index-highlight))
1877 (forward-line -1)
1878 (ebib-set-index-highlight)
1879 (ebib-fill-entry-buffer)))
1880 ((default)
1881 (beep))))
1883 (defun ebib-next-entry ()
1884 "Moves to the next BibTeX entry."
1885 (interactive)
1886 (ebib-execute-when
1887 ((entries)
1888 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1889 (beep) ; just beep.
1890 (setf (edb-cur-entry ebib-cur-db)
1891 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1892 (goto-char (ebib-highlight-start ebib-index-highlight))
1893 (forward-line 1)
1894 (ebib-set-index-highlight)
1895 (ebib-fill-entry-buffer)))
1896 ((default)
1897 (beep))))
1899 (defun ebib-add-entry ()
1900 "Adds a new entry to the database."
1901 (interactive)
1902 (ebib-execute-when
1903 ((real-db)
1904 (if-str (entry-key (if ebib-autogenerate-keys
1905 "<new-entry>"
1906 (read-string "New entry key: ")))
1907 (progn
1908 (if (member entry-key (edb-keys-list ebib-cur-db))
1909 (if ebib-uniquify-keys
1910 (setq entry-key (ebib-uniquify-key entry-key))
1911 (error "Key already exists")))
1912 (set-buffer ebib-index-buffer)
1913 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1914 ;; we create the hash table *before* the call to
1915 ;; ebib-display-entry, because that function refers to the
1916 ;; hash table if ebib-index-display-fields is set.
1917 (let ((fields (make-hash-table)))
1918 (puthash 'type* ebib-default-type fields)
1919 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1920 (with-buffer-writable
1921 (ebib-display-entry entry-key))
1922 (forward-line -1) ; move one line up to position the cursor on the new entry.
1923 (ebib-set-index-highlight)
1924 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1925 (ebib-fill-entry-buffer)
1926 (ebib-edit-entry)
1927 (ebib-set-modified t))))
1928 ((no-database)
1929 (error "No database open. Use `o' to open a database first"))
1930 ((default)
1931 (beep))))
1933 (defun ebib-uniquify-key (key)
1934 "Creates a unique key from KEY."
1935 (let* ((suffix ?b)
1936 (unique-key (concat key (list suffix))))
1937 (while (member unique-key (edb-keys-list ebib-cur-db))
1938 (setq suffix (1+ suffix))
1939 (setq unique-key (concat key (list suffix))))
1940 unique-key))
1942 (defun ebib-generate-autokey ()
1943 "Automatically generate a key for the current entry.
1944 This function uses the function BIBTEX-GENERATE-AUTOKEY to
1945 generate the key, see that function's documentation for details."
1946 (interactive)
1947 (ebib-execute-when
1948 ((real-db entries)
1949 (let ((new-key
1950 (with-temp-buffer
1951 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db nil)
1952 (let ((x-ref (ebib-get-field-value 'crossref (ebib-cur-entry-key))))
1953 (if x-ref
1954 (ebib-format-entry (to-raw (car x-ref)) ebib-cur-db nil)))
1955 (goto-char (point-min))
1956 (bibtex-generate-autokey))))
1957 (if (equal new-key "")
1958 (error (format "Cannot create key"))
1959 (ebib-update-keyname new-key))))
1960 ((default)
1961 (beep))))
1963 (defun ebib-close-database ()
1964 "Closes the current BibTeX database."
1965 (interactive)
1966 (ebib-execute-when
1967 ((database)
1968 (when (if (edb-modified ebib-cur-db)
1969 (yes-or-no-p "Database modified. Close it anyway? ")
1970 (y-or-n-p "Close database? "))
1971 (let ((to-be-deleted ebib-cur-db)
1972 (new-db (next-elem ebib-cur-db ebib-databases)))
1973 (setq ebib-databases (delete to-be-deleted ebib-databases))
1974 (if ebib-databases ; do we still have another database loaded?
1975 (progn
1976 (setq ebib-cur-db (or new-db
1977 (last1 ebib-databases)))
1978 (unless (edb-cur-entry ebib-cur-db)
1979 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1980 (ebib-fill-entry-buffer)
1981 (ebib-fill-index-buffer))
1982 ;; otherwise, we have to clean up a little and empty all the buffers.
1983 (setq ebib-cur-db nil)
1984 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1985 (set-buffer (car buf))
1986 (with-buffer-writable
1987 (erase-buffer))
1988 (ebib-delete-highlight (cadr buf)))
1989 (list (list ebib-entry-buffer ebib-fields-highlight)
1990 (list ebib-index-buffer ebib-index-highlight)
1991 (list ebib-strings-buffer ebib-strings-highlight)))
1992 ;; multiline edit buffer
1993 (set-buffer ebib-multiline-buffer)
1994 (with-buffer-writable
1995 (erase-buffer))
1996 (set-buffer ebib-index-buffer)
1997 (rename-buffer " none"))
1998 (message "Database closed."))))))
2000 (defun ebib-goto-first-entry ()
2001 "Moves to the first BibTeX entry in the database."
2002 (interactive)
2003 (ebib-execute-when
2004 ((entries)
2005 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
2006 (set-buffer ebib-index-buffer)
2007 (goto-char (point-min))
2008 (ebib-set-index-highlight)
2009 (ebib-fill-entry-buffer))
2010 ((default)
2011 (beep))))
2013 (defun ebib-goto-last-entry ()
2014 "Moves to the last entry in the BibTeX database."
2015 (interactive)
2016 (ebib-execute-when
2017 ((entries)
2018 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
2019 (set-buffer ebib-index-buffer)
2020 (goto-char (point-min))
2021 (forward-line (1- (edb-n-entries ebib-cur-db)))
2022 (ebib-set-index-highlight)
2023 (ebib-fill-entry-buffer))
2024 ((default)
2025 (beep))))
2027 (defun ebib-edit-entry ()
2028 "Edits the current BibTeX entry."
2029 (interactive)
2030 (ebib-execute-when
2031 ((real-db entries)
2032 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2033 (select-window (get-buffer-window ebib-entry-buffer) nil))
2034 ((default)
2035 (beep))))
2037 (defun ebib-edit-keyname ()
2038 "Changes the key of a BibTeX entry."
2039 (interactive)
2040 (ebib-execute-when
2041 ((real-db entries)
2042 (let ((cur-keyname (ebib-cur-entry-key)))
2043 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
2044 cur-keyname))
2045 (ebib-update-keyname new-keyname))))
2046 ((default)
2047 (beep))))
2049 (defun ebib-update-keyname (new-key)
2050 "Changes the key of the current BibTeX entry to NEW-KEY."
2051 (if (member new-key (edb-keys-list ebib-cur-db))
2052 (if ebib-uniquify-keys
2053 (setq new-key (ebib-uniquify-key new-key))
2054 (error (format "Key `%s' already exists" new-key))))
2055 (let ((cur-key (ebib-cur-entry-key)))
2056 (unless (string= cur-key new-key)
2057 (let ((fields (ebib-retrieve-entry cur-key ebib-cur-db))
2058 (marked (member cur-key (edb-marked-entries ebib-cur-db))))
2059 (ebib-remove-entry-from-db cur-key ebib-cur-db)
2060 (ebib-remove-key-from-buffer cur-key)
2061 (ebib-insert-entry new-key fields ebib-cur-db t nil)
2062 (setf (edb-cur-entry ebib-cur-db) (member new-key (edb-keys-list ebib-cur-db)))
2063 (sort-in-buffer (edb-n-entries ebib-cur-db) new-key)
2064 (with-buffer-writable
2065 (ebib-display-entry new-key))
2066 (forward-line -1) ; move one line up to position the cursor on the new entry.
2067 (ebib-set-index-highlight)
2068 (ebib-set-modified t)
2069 (when marked (ebib-mark-entry))))))
2071 (defun ebib-mark-entry ()
2072 "Marks or unmarks the current entry."
2073 (interactive)
2074 (if (ebib-called-with-prefix)
2075 (ebib-execute-when
2076 ((marked-entries)
2077 (setf (edb-marked-entries ebib-cur-db) nil)
2078 (ebib-fill-index-buffer)
2079 (message "All entries unmarked"))
2080 ((entries)
2081 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
2082 (ebib-fill-index-buffer)
2083 (message "All entries marked"))
2084 ((default)
2085 (beep)))
2086 (ebib-execute-when
2087 ((entries)
2088 (set-buffer ebib-index-buffer)
2089 (with-buffer-writable
2090 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
2091 (progn
2092 (setf (edb-marked-entries ebib-cur-db)
2093 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
2094 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
2095 (ebib-highlight-end ebib-index-highlight)
2096 '(face ebib-marked-face)))
2097 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
2098 (edb-marked-entries ebib-cur-db))
2099 'string<))
2100 (add-text-properties (ebib-highlight-start ebib-index-highlight)
2101 (ebib-highlight-end ebib-index-highlight)
2102 '(face ebib-marked-face)))))
2103 ((default)
2104 (beep)))))
2106 (defun ebib-index-scroll-down ()
2107 "Moves one page up in the database."
2108 (interactive)
2109 (ebib-execute-when
2110 ((entries)
2111 (scroll-down)
2112 (ebib-select-entry))
2113 ((default)
2114 (beep))))
2116 (defun ebib-index-scroll-up ()
2117 "Moves one page down in the database."
2118 (interactive)
2119 (ebib-execute-when
2120 ((entries)
2121 (scroll-up)
2122 (ebib-select-entry))
2123 ((default)
2124 (beep))))
2126 (defun ebib-format-entry (key db timestamp)
2127 "Formats entry KEY from database DB into the current buffer in BibTeX format.
2128 If TIMESTAMP is T, a timestamp is added to the entry if
2129 EBIB-USE-TIMESTAMP is T."
2130 (let ((entry (ebib-retrieve-entry key db)))
2131 (when entry
2132 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
2133 (maphash #'(lambda (key value)
2134 (unless (or (eq key 'type*)
2135 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
2136 (insert (format "\t%s = %s,\n" key value))))
2137 entry)
2138 (if (and timestamp ebib-use-timestamp)
2139 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
2140 (delete-char -2)) ; the final ",\n" must be deleted
2141 (insert "\n}\n\n"))))
2143 (defun ebib-format-strings (db)
2144 "Formats the @STRING commands in database DB."
2145 (maphash #'(lambda (key value)
2146 (insert (format "@STRING{%s = %s}\n" key value)))
2147 (edb-strings db))
2148 (insert "\n"))
2150 (defun ebib-compare-xrefs (x y)
2151 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
2153 (defun ebib-format-database (db)
2154 "Writes database DB into the current buffer in BibTeX format."
2155 (when (edb-preamble db)
2156 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
2157 (ebib-format-strings db)
2158 (let ((sorted-list (copy-tree (edb-keys-list db))))
2159 (cond
2160 (ebib-save-xrefs-first
2161 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
2162 (ebib-sort-order
2163 (setq sorted-list (sort sorted-list 'ebib-entry<))))
2164 (mapc #'(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
2166 (defun ebib-save-database (db)
2167 "Saves the database DB."
2168 (ebib-execute-when
2169 ((real-db)
2170 (when (and (edb-make-backup db)
2171 (file-exists-p (edb-filename db)))
2172 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
2173 (setf (edb-make-backup db) nil))
2174 (with-temp-buffer
2175 (ebib-format-database db)
2176 (write-region (point-min) (point-max) (edb-filename db)))
2177 (ebib-set-modified nil db))))
2179 (defun ebib-write-database ()
2180 "Writes the current database to a different file.
2181 Can also be used to change a virtual database into a real one."
2182 (interactive)
2183 (ebib-execute-when
2184 ((database)
2185 (if-str (new-filename (read-file-name "Save to file: " "~/"))
2186 (progn
2187 (with-temp-buffer
2188 (ebib-format-database ebib-cur-db)
2189 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
2190 ;; if SAFE-WRITE-REGION was cancelled by the user because he
2191 ;; didn't want to overwrite an already existing file with his
2192 ;; new database, it throws an error, so the next lines will not
2193 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
2194 ;; (EDB-NAME DB).
2195 (setf (edb-filename ebib-cur-db) new-filename)
2196 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
2197 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
2198 (length (member ebib-cur-db ebib-databases)))))
2199 (edb-name ebib-cur-db)))
2200 (ebib-execute-when
2201 ((virtual-db)
2202 (setf (edb-virtual ebib-cur-db) nil)
2203 (setf (edb-database ebib-cur-db)
2204 (let ((new-db (make-hash-table :test 'equal)))
2205 (mapc #'(lambda (key)
2206 (let ((entry (gethash key (edb-database ebib-cur-db))))
2207 (when entry
2208 (puthash key (copy-hash-table entry) new-db))))
2209 (edb-keys-list ebib-cur-db))
2210 new-db))))
2211 (ebib-set-modified nil))))
2212 ((default)
2213 (beep))))
2215 (defun ebib-save-current-database ()
2216 "Saves the current database."
2217 (interactive)
2218 (ebib-execute-when
2219 ((real-db)
2220 (if (not (edb-modified ebib-cur-db))
2221 (message "No changes need to be saved.")
2222 (ebib-save-database ebib-cur-db)))
2223 ((virtual-db)
2224 (error "Cannot save a virtual database. Use `w' to write to a file."))))
2226 (defun ebib-save-all-databases ()
2227 "Saves all currently open databases if they were modified."
2228 (interactive)
2229 (ebib-execute-when
2230 ((database)
2231 (mapc #'(lambda (db)
2232 (when (edb-modified db)
2233 (ebib-save-database db)))
2234 ebib-databases)
2235 (message "All databases saved."))))
2237 (defun ebib-print-filename ()
2238 "Displays the filename of the current database in the minibuffer."
2239 (interactive)
2240 (message (edb-filename ebib-cur-db)))
2242 (defun ebib-follow-crossref ()
2243 "Goes to the entry mentioned in the crossref field of the current entry."
2244 (interactive)
2245 (let ((new-cur-entry (to-raw (gethash 'crossref
2246 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2247 (setf (edb-cur-entry ebib-cur-db)
2248 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
2249 (edb-cur-entry ebib-cur-db))))
2250 (ebib-fill-entry-buffer)
2251 (ebib-fill-index-buffer))
2253 (defun ebib-toggle-hidden ()
2254 "Toggle viewing hidden fields."
2255 (interactive)
2256 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
2257 (ebib-fill-entry-buffer))
2259 (defun ebib-toggle-timestamp ()
2260 "Toggle using timestamp for new entries."
2261 (interactive)
2262 (setq ebib-use-timestamp (not ebib-use-timestamp)))
2264 (defun ebib-toggle-xrefs-first ()
2265 "Toggle saving of crossreferenced entries first."
2266 (interactive)
2267 (setq ebib-save-xrefs-first (not ebib-save-xrefs-first)))
2269 (defun ebib-toggle-identical-fields ()
2270 "Toggle whether Ebib allows identical fields when opening a .bib file."
2271 (interactive)
2272 (setq ebib-allow-identical-fields (not ebib-allow-identical-fields)))
2274 (defun ebib-toggle-layout ()
2275 "Toggles the Ebib layout."
2276 (interactive)
2277 (if (eq ebib-layout 'full)
2278 (setq ebib-layout 'custom)
2279 (setq ebib-layout 'full))
2280 (ebib-lower)
2281 (ebib))
2283 (defun ebib-toggle-print-newpage ()
2284 "Toggle whether index cards are printed with a newpage after each card."
2285 (interactive)
2286 (setq ebib-print-newpage (not ebib-print-newpage)))
2288 (defun ebib-toggle-print-multiline ()
2289 "Toggle whether multiline fields are printed."
2290 (interactive)
2291 (setq ebib-print-multiline (not ebib-print-multiline)))
2293 (defun ebib-delete-entry ()
2294 "Deletes the current entry from the database."
2295 (interactive)
2296 (if (ebib-called-with-prefix)
2297 (ebib-execute-when
2298 ((real-db marked-entries)
2299 (when (y-or-n-p "Delete all marked entries? ")
2300 (mapc #'(lambda (entry)
2301 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
2302 (edb-marked-entries ebib-cur-db))
2303 (message "Marked entries deleted.")
2304 (ebib-set-modified t)
2305 (ebib-fill-entry-buffer)
2306 (ebib-fill-index-buffer)))
2307 ((default)
2308 (beep)))
2309 (ebib-execute-when
2310 ((real-db entries)
2311 (let ((cur-entry (ebib-cur-entry-key)))
2312 (when (y-or-n-p (format "Delete %s? " cur-entry))
2313 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
2314 (ebib-remove-key-from-buffer cur-entry)
2315 (ebib-fill-entry-buffer)
2316 (ebib-set-modified t)
2317 (message (format "Entry `%s' deleted." cur-entry)))))
2318 ((default)
2319 (beep)))))
2321 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
2322 "Removes ENTRY-KEY from DB.
2323 Optional argument NEW-CUR-ENTRY is the key of the entry that is
2324 to become the new current entry. It it is NIL, the entry after
2325 the deleted one becomes the new current entry. If it is T, the
2326 current entry is not changed."
2327 (remhash entry-key (edb-database db))
2328 (setf (edb-n-entries db) (1- (edb-n-entries db)))
2329 (cond
2330 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
2331 ((stringp new-cur-entry) t)
2332 (t (setq new-cur-entry (ebib-cur-entry-key))))
2333 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
2334 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
2335 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
2336 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
2337 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
2339 (defun ebib-remove-key-from-buffer (entry-key)
2340 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
2341 (with-buffer-writable
2342 (let ((beg (ebib-search-key-in-buffer entry-key)))
2343 (forward-line 1)
2344 (delete-region beg (point))))
2345 (ebib-execute-when
2346 ((entries)
2347 (ebib-search-key-in-buffer (ebib-cur-entry-key))
2348 (ebib-set-index-highlight))))
2350 (defun ebib-select-entry ()
2351 "Makes the entry at POINT the current entry."
2352 (interactive)
2353 (ebib-execute-when
2354 ((entries)
2355 (beginning-of-line)
2356 (let ((beg (point)))
2357 (let* ((key (save-excursion
2358 (skip-chars-forward "^ ")
2359 (buffer-substring-no-properties beg (point))))
2360 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
2361 (when new-cur-entry
2362 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
2363 (ebib-set-index-highlight)
2364 (ebib-fill-entry-buffer)))))
2365 ((default)
2366 (beep))))
2368 (defun ebib-export-entry (prefix)
2369 "Copies entries to another database.
2370 The prefix argument indicates which database to copy the entry
2371 to. If no prefix argument is present, a filename is asked to
2372 which the entry is appended."
2373 (interactive "P")
2374 (let ((num (ebib-prefix prefix)))
2375 (if (ebib-called-with-prefix)
2376 (ebib-export-marked-entries num)
2377 (ebib-export-single-entry num))))
2379 (defun ebib-export-single-entry (num)
2380 "Copies the current entry to another database.
2381 NUM indicates which database to copy the entry to. If it is NIL,
2382 a filename is asked to which the entry is appended."
2383 (ebib-execute-when
2384 ((real-db entries)
2385 (if num
2386 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
2387 #'(lambda (db)
2388 (let ((entry-key (ebib-cur-entry-key)))
2389 (if (member entry-key (edb-keys-list db))
2390 (error "Entry key `%s' already exists in database %d" entry-key num)
2391 (ebib-insert-entry entry-key
2392 (copy-hash-table (ebib-retrieve-entry entry-key
2393 ebib-cur-db))
2394 db t t)
2395 ;; if this is the first entry in the target DB,
2396 ;; its CUR-ENTRY must be set!
2397 (when (null (edb-cur-entry db))
2398 (setf (edb-cur-entry db) (edb-keys-list db)))
2399 t)))) ; we must return T, WHEN does not always do this.
2400 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2401 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2402 #'(lambda ()
2403 (insert "\n")
2404 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2405 ((default)
2406 (beep))))
2408 (defun ebib-export-marked-entries (num)
2409 "Copies the marked entries to another database.
2410 NUM indicates which database to copy the entry to. If it is NIL,
2411 a filename is asked to which the entry is appended."
2412 (ebib-execute-when
2413 ((real-db marked-entries)
2414 (if num
2415 (ebib-export-to-db
2416 num "Entries copied to database %d."
2417 #'(lambda (db)
2418 (mapc #'(lambda (entry-key)
2419 (if (member entry-key (edb-keys-list db))
2420 (error "Entry key `%s' already exists in database %d" entry-key num)
2421 (ebib-insert-entry entry-key
2422 (copy-hash-table (ebib-retrieve-entry entry-key
2423 ebib-cur-db))
2424 db t t)))
2425 (edb-marked-entries ebib-cur-db))
2426 ;; if the target DB was empty before, its CUR-ENTRY must be set!
2427 (when (null (edb-cur-entry db))
2428 (setf (edb-cur-entry db) (edb-keys-list db)))
2429 t)) ; we must return T, WHEN does not always do this.
2430 (ebib-export-to-file "Export to file: " "Entries exported to %s."
2431 #'(lambda ()
2432 (mapc #'(lambda (entry-key)
2433 (insert "\n")
2434 (ebib-format-entry entry-key ebib-cur-db t))
2435 (edb-marked-entries ebib-cur-db))))))
2436 ((default)
2437 (beep))))
2439 (defun ebib-search ()
2440 "Search the current Ebib database.
2441 The search is conducted with STRING-MATCH and can therefore be a
2442 regexp. Searching starts with the current entry."
2443 (interactive)
2444 (ebib-execute-when
2445 ((entries)
2446 (if-str (search-str (read-string "Search database for: "))
2447 (progn
2448 (setq ebib-search-string search-str)
2449 ;; first we search the current entry
2450 (if (ebib-search-in-entry ebib-search-string
2451 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2452 (ebib-fill-entry-buffer ebib-search-string)
2453 ;; if the search string wasn't found in the current entry, we continue searching.
2454 (ebib-search-next)))))
2455 ((default)
2456 (beep))))
2458 (defun ebib-search-next ()
2459 "Searches the next occurrence of EBIB-SEARCH-STRING.
2460 Searching starts at the entry following the current entry. If a
2461 match is found, the matching entry is shown and becomes the new
2462 current entry."
2463 (interactive)
2464 (ebib-execute-when
2465 ((entries)
2466 (if (null ebib-search-string)
2467 (message "No search string")
2468 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2469 (while (and cur-search-entry
2470 (null (ebib-search-in-entry ebib-search-string
2471 (gethash (car cur-search-entry)
2472 (edb-database ebib-cur-db)))))
2473 (setq cur-search-entry (cdr cur-search-entry)))
2474 (if (null cur-search-entry)
2475 (message (format "`%s' not found" ebib-search-string))
2476 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2477 (set-buffer ebib-index-buffer)
2478 (goto-char (point-min))
2479 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
2480 (beginning-of-line)
2481 (ebib-set-index-highlight)
2482 (ebib-fill-entry-buffer ebib-search-string)))))
2483 ((default)
2484 (beep))))
2486 (defun ebib-search-in-entry (search-str entry &optional field)
2487 "Searches one entry of the ebib database.
2488 Returns a list of fields in ENTRY that match the regexp
2489 SEARCH-STR, or NIL if no matches were found. If FIELD is given,
2490 only that field is searched."
2491 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2492 (result nil))
2493 (if field
2494 (let ((value (gethash field entry)))
2495 (when (and (stringp value) ; the type* field has a symbol as value
2496 (string-match search-str value))
2497 (setq result (list field))))
2498 (maphash #'(lambda (field value)
2499 (when (and (stringp value) ; the type* field has a symbol as value
2500 (string-match search-str value))
2501 (setq result (cons field result))))
2502 entry))
2503 result))
2505 (defun ebib-search-crossref ()
2506 "Searches the database for the key of the current entry.
2507 This is useful in searching the entries that cross-reference the
2508 current entry."
2509 (interactive)
2510 (setq ebib-search-string (car (edb-cur-entry ebib-cur-db)))
2511 (ebib-search-next))
2513 (defun ebib-edit-strings ()
2514 "Edits the @STRING definitions in the database."
2515 (interactive)
2516 (ebib-execute-when
2517 ((real-db)
2518 (ebib-fill-strings-buffer)
2519 (select-window (get-buffer-window ebib-entry-buffer) nil)
2520 (set-window-dedicated-p (selected-window) nil)
2521 (switch-to-buffer ebib-strings-buffer)
2522 (unless (eq ebib-layout 'full)
2523 (set-window-dedicated-p (selected-window) t))
2524 (goto-char (point-min)))
2525 ((default)
2526 (beep))))
2528 (defun ebib-edit-preamble ()
2529 "Edits the @PREAMBLE definition in the database."
2530 (interactive)
2531 (ebib-execute-when
2532 ((real-db)
2533 (select-window (ebib-temp-window) nil)
2534 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2535 ((default)
2536 (beep))))
2538 (defun ebib-export-preamble (prefix)
2539 "Exports the @PREAMBLE definition.
2540 If a prefix argument is given, it is taken as the database to
2541 export the preamble to. If the goal database already has a
2542 preamble, the new preamble will be appended to it. If no prefix
2543 argument is given, the user is asked to enter a filename to which
2544 the preamble is appended."
2545 (interactive "P")
2546 (ebib-execute-when
2547 ((real-db)
2548 (if (null (edb-preamble ebib-cur-db))
2549 (error "No @PREAMBLE defined")
2550 (let ((num (ebib-prefix prefix)))
2551 (if num
2552 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2553 #'(lambda (db)
2554 (let ((text (edb-preamble ebib-cur-db)))
2555 (if (edb-preamble db)
2556 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2557 (setf (edb-preamble db) text)))))
2558 (ebib-export-to-file "Export @PREAMBLE to file: "
2559 "@PREAMBLE exported to %s"
2560 #'(lambda ()
2561 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2562 ((default)
2563 (beep))))
2565 (defun ebib-print-entries ()
2566 "Creates a LaTeX file listing the entries.
2567 Either prints the entire database, or the marked entries."
2568 (interactive)
2569 (ebib-execute-when
2570 ((entries)
2571 (let ((entries (or (when (or (ebib-called-with-prefix)
2572 (equal '(menu-bar) (elt (this-command-keys-vector) 0)))
2573 (edb-marked-entries ebib-cur-db))
2574 (edb-keys-list ebib-cur-db))))
2575 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2576 ebib-print-tempfile
2577 (read-file-name "Use temp file: " "~/" nil nil)))
2578 (progn
2579 (with-temp-buffer
2580 (insert "\\documentclass{article}\n\n")
2581 (when ebib-print-preamble
2582 (mapc #'(lambda (string)
2583 (insert (format "%s\n" string)))
2584 ebib-print-preamble))
2585 (insert "\n\\begin{document}\n\n")
2586 (mapc #'(lambda (entry-key)
2587 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2588 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2589 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2590 entry-key (symbol-name (gethash 'type* entry))))
2591 (insert "\\hline\n")
2592 (mapc #'(lambda (field)
2593 (if-str (value (gethash field entry))
2594 (when (or (not (multiline-p value))
2595 ebib-print-multiline)
2596 (insert (format "%s: & %s\\\\\n"
2597 field (to-raw value))))))
2598 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2599 (insert "\\end{tabular}\n\n")
2600 (insert (if ebib-print-newpage
2601 "\\newpage\n\n"
2602 "\\bigskip\n\n")))
2603 entries)
2604 (insert "\\end{document}\n")
2605 (write-region (point-min) (point-max) tempfile))
2606 (ebib-lower)
2607 (find-file tempfile)))))
2608 ((default)
2609 (beep))))
2611 (defun ebib-latex-entries ()
2612 "Creates a LaTeX file that \\nocites entries from the database.
2613 Operates either on all entries or on the marked entries."
2614 (interactive)
2615 (ebib-execute-when
2616 ((real-db entries)
2617 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2618 ebib-print-tempfile
2619 (read-file-name "Use temp file: " "~/" nil nil)))
2620 (progn
2621 (with-temp-buffer
2622 (insert "\\documentclass{article}\n\n")
2623 (when ebib-print-preamble
2624 (mapc #'(lambda (string)
2625 (insert (format "%s\n" string)))
2626 ebib-latex-preamble))
2627 (insert "\n\\begin{document}\n\n")
2628 (if (and (or (ebib-called-with-prefix)
2629 (equal '(menu-bar) (elt (this-command-keys-vector) 0)))
2630 (edb-marked-entries ebib-cur-db))
2631 (mapc #'(lambda (entry)
2632 (insert (format "\\nocite{%s}\n" entry)))
2633 (edb-marked-entries ebib-cur-db))
2634 (insert "\\nocite{*}\n"))
2635 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2636 (insert "\\end{document}\n")
2637 (write-region (point-min) (point-max) tempfile))
2638 (ebib-lower)
2639 (find-file tempfile))))
2640 ((default)
2641 (beep))))
2643 (defun ebib-switch-to-database (num)
2644 (interactive "NSwitch to database number: ")
2645 (let ((new-db (nth (1- num) ebib-databases)))
2646 (if new-db
2647 (progn
2648 (setq ebib-cur-db new-db)
2649 (ebib-fill-entry-buffer)
2650 (ebib-fill-index-buffer))
2651 (error "Database %d does not exist" num))))
2653 (defun ebib-next-database ()
2654 (interactive)
2655 (ebib-execute-when
2656 ((database)
2657 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2658 (unless new-db
2659 (setq new-db (car ebib-databases)))
2660 (setq ebib-cur-db new-db)
2661 (ebib-fill-entry-buffer)
2662 (ebib-fill-index-buffer)))))
2664 (defun ebib-prev-database ()
2665 (interactive)
2666 (ebib-execute-when
2667 ((database)
2668 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2669 (unless new-db
2670 (setq new-db (last1 ebib-databases)))
2671 (setq ebib-cur-db new-db)
2672 (ebib-fill-entry-buffer)
2673 (ebib-fill-index-buffer)))))
2675 (defun ebib-browse-url (num)
2676 "Asks a browser to load the URL in the standard URL field.
2677 The standard URL field may contain more than one URL, if they're
2678 whitespace-separated. In that case, a numeric prefix argument
2679 specifies which URL to choose.
2681 By \"standard URL field\" is meant the field defined in the
2682 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2683 is `url'."
2684 (interactive "p")
2685 (ebib-execute-when
2686 ((entries)
2687 (let ((url (to-raw (gethash ebib-standard-url-field
2688 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2689 (when (not (and url
2690 (ebib-call-browser url num)))
2691 (error "No url found in field `%s'" ebib-standard-url-field))))
2692 ((default)
2693 (beep))))
2695 (defun ebib-call-browser (urls n)
2696 "Passes the Nth url in URLS to a browser.
2697 URLs must be a string of whitespace-separated urls."
2698 (let ((url (nth (1- n)
2699 (let ((start 0)
2700 (result nil))
2701 (while (string-match ebib-url-regexp urls start)
2702 (add-to-list 'result (match-string 0 urls) t)
2703 (setq start (match-end 0)))
2704 result))))
2705 (cond
2706 ((string-match "\\\\url{\\(.*?\\)}" url)
2707 (setq url (match-string 1 url)))
2708 ((string-match ebib-url-regexp url)
2709 (setq url (match-string 0 url)))
2710 (t (setq url nil)))
2711 (when url
2712 (if (not (string= ebib-browser-command ""))
2713 (start-process "Ebib-browser" nil ebib-browser-command url)
2714 (browse-url url)))))
2716 (defun ebib-view-file (num)
2717 "Views a file in the standard file field.
2718 The standard file field may contain more than one filename, if
2719 they're whitespace-separated. In that case, a numeric prefix
2720 argument specifies which file to choose.
2722 By \"standard file field\" is meant the field defined in the
2723 customisation variable EBIB-STANDARD-FILE-FIELD. Its default
2724 value is `file'."
2725 (interactive "p")
2726 (ebib-execute-when
2727 ((entries)
2728 (let ((filename (to-raw (gethash ebib-standard-file-field
2729 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2730 (when (not (and filename
2731 (ebib-call-file-viewer filename num)))
2732 (error "No valid filename found in field `%s'" ebib-standard-file-field))))
2733 ((default)
2734 (beep))))
2736 (defun ebib-call-file-viewer (files n)
2737 "Passes the Nth file in FILES to an external viewer.
2738 FILES must be a string of whitespace-separated filenames."
2739 (let* ((file (nth (1- n)
2740 (let ((start 0)
2741 (result nil))
2742 (while (string-match ebib-file-regexp files start)
2743 (add-to-list 'result (match-string 0 files) t)
2744 (setq start (match-end 0)))
2745 result)))
2746 (ext (file-name-extension file)))
2747 (let ((file-full-path (locate-file file ebib-file-search-dirs)))
2748 (when file-full-path
2749 (if-str (viewer (cdr (assoc ext ebib-file-associations)))
2750 (start-process (concat "ebib " ext " viewer process") nil viewer file-full-path)
2751 (ebib-lower)
2752 (find-file file-full-path))))))
2754 (defun ebib-virtual-db-and (not)
2755 "Filters entries into a virtual database.
2756 If the current database is a virtual database already, perform a
2757 logical AND on the entries."
2758 (interactive "p")
2759 (ebib-execute-when
2760 ((entries)
2761 (ebib-filter-to-virtual-db 'and not))
2762 ((default)
2763 (beep))))
2765 (defun ebib-virtual-db-or (not)
2766 "Filters entries into a virtual database.
2767 If the current database is a virtual database already, perform a
2768 logical OR with the entries in the original database."
2769 (interactive "p")
2770 (ebib-execute-when
2771 ((entries)
2772 (ebib-filter-to-virtual-db 'or not))
2773 ((default)
2774 (beep))))
2776 (defun ebib-virtual-db-not ()
2777 "Negates the current virtual database."
2778 (interactive)
2779 (ebib-execute-when
2780 ((virtual-db)
2781 (setf (edb-virtual ebib-cur-db)
2782 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2783 (cadr (edb-virtual ebib-cur-db))
2784 `(not ,(edb-virtual ebib-cur-db))))
2785 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2786 (ebib-fill-entry-buffer)
2787 (ebib-fill-index-buffer))
2788 ((default)
2789 (beep))))
2791 (defun ebib-filter-to-virtual-db (bool not)
2792 "Filters the current database to a virtual database.
2793 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2794 a logical `not' is applied to the selection."
2795 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2796 (if (< not 0) "(not " "")
2797 (if (< not 0) ")" ""))
2798 (cons '("any" 0)
2799 (mapcar #'(lambda (x)
2800 (cons (symbol-name x) 0))
2801 (append ebib-unique-field-list ebib-additional-fields)))
2802 nil t)))
2803 (setq field (intern-soft field))
2804 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2805 (if (< not 0) "(not " "")
2806 field
2807 (if (< not 0) ")" "")))))
2808 (ebib-execute-when
2809 ((virtual-db)
2810 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2811 ,(if (>= not 0)
2812 `(contains ,field ,regexp)
2813 `(not (contains ,field ,regexp))))))
2814 ((real-db)
2815 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2816 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2817 (ebib-fill-entry-buffer)
2818 (ebib-fill-index-buffer))))
2820 (defun ebib-create-virtual-db (field regexp not)
2821 "Creates a virtual database based on EBIB-CUR-DB."
2822 ;; a virtual database is a database whose edb-virtual field contains an
2823 ;; expression that selects entries. this function only sets that
2824 ;; expression, it does not actually filter the entries.
2825 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2826 (setf (edb-virtual new-db) (if (>= not 0)
2827 `(contains ,field ,regexp)
2828 `(not (contains ,field ,regexp))))
2829 (setf (edb-filename new-db) nil)
2830 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2831 (setf (edb-modified new-db) nil)
2832 (setf (edb-make-backup new-db) nil)
2833 new-db))
2835 (defmacro contains (field regexp)
2836 ;; Note: the variable ENTRY is *not* bound in this macro! The function
2837 ;; calling CONTAINS *must* set ENTRY to an actual Ebib entry. The point
2838 ;; of this macro is to facilitate defining filters for virtual databases.
2839 ;; It enables us to define filters of the form:
2841 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2843 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2845 (defun ebib-run-filter (filter db)
2846 "Runs FILTER on DB"
2847 (setf (edb-keys-list db)
2848 (sort (let ((result nil))
2849 (maphash #'(lambda (key value)
2850 (let ((entry value)) ; this is necessary for actually running the filter
2851 (when (eval filter)
2852 (setq result (cons key result)))))
2853 (edb-database db))
2854 result)
2855 'string<))
2856 (setf (edb-n-entries db) (length (edb-keys-list db)))
2857 (setf (edb-cur-entry db) (edb-keys-list db)))
2859 (defun ebib-print-filter (num)
2860 "Displays the filter of the current virtual database.
2861 With any prefix argument, reapplies the filter to the
2862 database. This can be useful when the source database was
2863 modified."
2864 (interactive "P")
2865 (ebib-execute-when
2866 ((virtual-db)
2867 (when num
2868 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2869 (ebib-fill-entry-buffer)
2870 (ebib-fill-index-buffer))
2871 (message "%S" (edb-virtual ebib-cur-db)))
2872 ((default)
2873 (beep))))
2875 (defun ebib-show-log ()
2876 "Displays the contents of the log buffer."
2877 (interactive)
2878 (select-window (get-buffer-window ebib-entry-buffer) nil)
2879 (set-window-dedicated-p (selected-window) nil)
2880 (switch-to-buffer ebib-log-buffer)
2881 (unless (eq ebib-layout 'full)
2882 (set-window-dedicated-p (selected-window) t)))
2884 (defun ebib-create-citation-command (command n-opt-args key mode)
2885 "Create a LaTeX citation command with COMMAND and KEY.
2886 N-OPT-ARGS is the number of optional arguments COMMAND takes. A
2887 backslash is prefixed to COMMAND if none is present (and COMMAND
2888 is non-empty). MODE is the major mode of the push buffer. If this
2889 is 'org-mode, the citation command is formatted as an org-mode
2890 link. Any other value created a standard TeX citation."
2891 (let ((opt-args (loop for i from 1 to n-opt-args
2892 collect (read-from-minibuffer (format "Cite %s with optional argument %d: " key i)))))
2893 (while (equal (car opt-args) "") ; empty args at the beginning of the list don't need
2894 (setq opt-args (cdr opt-args))) ; to be included.
2895 (if (eq mode 'org-mode)
2896 (format "[[%s:%s][%s]]"
2897 command key
2898 (if opt-args ; if the user provided an optional argument,
2899 (car opt-args) ; it is included as the link description,t
2900 key)) ; otherwise the key becomes the description.
2901 (format "%s%s%s{%s}"
2902 (if (or (string= command "")
2903 (= (aref command 0) 92))
2904 "" "\\") ; add a backslash if the user didn't type one.
2905 command
2906 (mapconcat #'(lambda (str)
2907 (format "[%s]" str))
2908 opt-args "")
2909 key))))
2911 (defun ebib-push-bibtex-key ()
2912 "Pushes the current entry to a LaTeX buffer.
2913 The user is prompted for the buffer to push the entry into."
2914 (interactive)
2915 (let ((called-with-prefix (ebib-called-with-prefix)))
2916 (ebib-execute-when
2917 ((entries)
2918 (let ((buffer (read-buffer (if called-with-prefix
2919 "Push marked entries to buffer: "
2920 "Push entry to buffer: ")
2921 ebib-push-buffer t)))
2922 (when buffer
2923 (setq ebib-push-buffer buffer)
2924 (let (insert-string)
2925 (if-str (command (completing-read "Command to use: " ebib-insertion-commands
2926 nil nil nil ebib-minibuf-hist))
2927 (let* ((n-opt-args (or (cadr (assoc command ebib-insertion-commands))
2929 (allow-mult-args (caddr (assoc command ebib-insertion-commands)))
2930 (key (if (and called-with-prefix ; if there are marked entries...
2931 (edb-marked-entries ebib-cur-db))
2932 (if allow-mult-args ; and the command allows multiple separate args
2933 (car (edb-marked-entries ebib-cur-db)) ; just take the first one here
2934 (mapconcat #'(lambda (x) x) ; otherwise take all the marked entries
2935 (edb-marked-entries ebib-cur-db)
2936 ","))
2937 (car (edb-cur-entry ebib-cur-db)))) ; if there are no marked entries, take the current entry
2938 (first (ebib-create-citation-command command n-opt-args key (buffer-local-value 'major-mode (get-buffer buffer))))
2939 (rest (when (and called-with-prefix ; if we have marked entries...
2940 (edb-marked-entries ebib-cur-db)
2941 allow-mult-args) ; and a command that allows multiple separate arguments
2942 (mapcar #'(lambda (key) ; we need to process the rest of the entries
2943 (ebib-create-citation-command "" n-opt-args key (buffer-local-value 'major-mode (get-buffer buffer))))
2944 (cdr (edb-marked-entries ebib-cur-db))))))
2945 (setq insert-string (apply #'concat first rest)))
2946 ;; if the user did not provide a comand, we just insert the
2947 ;; current entry, or the marked entries, separated by commas:
2948 (setq insert-string
2949 (if (and called-with-prefix
2950 (edb-marked-entries ebib-cur-db))
2951 (mapconcat #'(lambda (x) x) ; otherwise take all the marked entries
2952 (edb-marked-entries ebib-cur-db)
2953 ",")
2954 (car (edb-cur-entry ebib-cur-db)))))
2955 (when insert-string
2956 (with-current-buffer buffer
2957 (insert insert-string))
2958 (message "Pushed entries to buffer %s" buffer))))))
2959 ((default)
2960 (beep)))))
2962 (defun ebib-index-help ()
2963 "Shows the info node of Ebib's index buffer."
2964 (interactive)
2965 (ebib-lower)
2966 (info "(ebib) The Index Buffer"))
2968 (defun ebib-info ()
2969 "Shows Ebib's info node."
2970 (interactive)
2971 (ebib-lower)
2972 (info "(ebib)"))
2974 ;;;;;;;;;;;;;;;;
2975 ;; entry-mode ;;
2976 ;;;;;;;;;;;;;;;;
2978 (defvar ebib-entry-mode-map
2979 (let ((map (make-keymap)))
2980 (suppress-keymap map)
2981 (define-key map [up] 'ebib-prev-field)
2982 (define-key map [down] 'ebib-next-field)
2983 (define-key map [prior] 'ebib-goto-prev-set)
2984 (define-key map [next] 'ebib-goto-next-set)
2985 (define-key map [home] 'ebib-goto-first-field)
2986 (define-key map [end] 'ebib-goto-last-field)
2987 (define-key map [return] 'ebib-edit-field)
2988 (define-key map " " 'ebib-goto-next-set)
2989 (define-key map "b" 'ebib-goto-prev-set)
2990 (define-key map "c" 'ebib-copy-field-contents)
2991 (define-key map "d" 'ebib-delete-field-contents)
2992 (define-key map "e" 'ebib-edit-field)
2993 (define-key map "f" 'ebib-view-file-in-field)
2994 (define-key map "g" 'ebib-goto-first-field)
2995 (define-key map "G" 'ebib-goto-last-field)
2996 (define-key map "h" 'ebib-entry-help)
2997 (define-key map "j" 'ebib-next-field)
2998 (define-key map "k" 'ebib-prev-field)
2999 (define-key map "l" 'ebib-edit-multiline-field)
3000 (define-key map [(control n)] 'ebib-next-field)
3001 (define-key map [(meta n)] 'ebib-goto-prev-set)
3002 (define-key map [(control p)] 'ebib-prev-field)
3003 (define-key map [(meta p)] 'ebib-goto-next-set)
3004 (define-key map "q" 'ebib-quit-entry-buffer)
3005 (define-key map "r" 'ebib-toggle-raw)
3006 (define-key map "s" 'ebib-insert-abbreviation)
3007 (define-key map "u" 'ebib-browse-url-in-field)
3008 (define-key map "x" 'ebib-cut-field-contents)
3009 (define-key map "\C-xb" 'undefined)
3010 (define-key map "\C-xk" 'undefined)
3011 (define-key map "y" 'ebib-yank-field-contents)
3012 map)
3013 "Keymap for the Ebib entry buffer.")
3015 (define-derived-mode ebib-entry-mode
3016 fundamental-mode "Ebib-entry"
3017 "Major mode for the Ebib entry buffer."
3018 (setq buffer-read-only t)
3019 (setq truncate-lines t))
3021 (defun ebib-quit-entry-buffer ()
3022 "Quits editing the entry.
3023 If the key of the current entry is <new-entry>, a new key is
3024 automatically generated using BIBTEX-GENERATE-AUTOKEY."
3025 (interactive)
3026 (select-window (get-buffer-window ebib-index-buffer) nil)
3027 (if (equal (ebib-cur-entry-key) "<new-entry>")
3028 (ebib-generate-autokey)))
3030 (defun ebib-find-visible-field (field direction)
3031 "Finds the first visible field before or after FIELD.
3032 If DIRECTION is negative, search the preceding fields, otherwise
3033 search the succeeding fields. If FIELD is visible itself, return
3034 that. If there is no preceding/following visible field, return
3035 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
3036 (when ebib-hide-hidden-fields
3037 (let ((fn (if (>= direction 0)
3038 'next-elem
3039 'prev-elem)))
3040 (while (and field
3041 (get field 'ebib-hidden))
3042 (setq field (funcall fn field ebib-cur-entry-fields)))))
3043 field)
3045 (defun ebib-prev-field ()
3046 "Moves to the previous field."
3047 (interactive)
3048 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
3049 (if (null new-field)
3050 (beep)
3051 (setq ebib-current-field new-field)
3052 (ebib-move-to-field ebib-current-field -1))))
3054 (defun ebib-next-field ()
3055 "Moves to the next field."
3056 (interactive)
3057 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
3058 (if (null new-field)
3059 (when (interactive-p) ; i call this function after editing a field,
3060 ; and we don't want a beep then
3061 (beep))
3062 (setq ebib-current-field new-field)
3063 (ebib-move-to-field ebib-current-field 1))))
3065 (defun ebib-goto-first-field ()
3066 "Moves to the first field."
3067 (interactive)
3068 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
3069 (if (null new-field)
3070 (beep)
3071 (setq ebib-current-field new-field)
3072 (ebib-move-to-field ebib-current-field -1))))
3074 (defun ebib-goto-last-field ()
3075 "Moves to the last field."
3076 (interactive)
3077 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
3078 (if (null new-field)
3079 (beep)
3080 (setq ebib-current-field new-field)
3081 (ebib-move-to-field ebib-current-field 1))))
3083 (defun ebib-goto-next-set ()
3084 "Moves to the next set of fields."
3085 (interactive)
3086 (cond
3087 ((eq ebib-current-field 'type*) (ebib-next-field))
3088 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
3089 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
3090 (obl-fields (ebib-get-obl-fields entry-type))
3091 (opt-fields (ebib-get-opt-fields entry-type))
3092 (new-field nil))
3093 (when (member ebib-current-field obl-fields)
3094 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
3095 ;; new-field is nil if there are no opt-fields
3096 (when (or (member ebib-current-field opt-fields)
3097 (null new-field))
3098 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
3099 (if (null new-field)
3100 (ebib-goto-last-field) ; if there was no further set to go to,
3101 ; go to the last field of the current set
3102 (setq ebib-current-field new-field)
3103 (ebib-move-to-field ebib-current-field 1))))))
3105 (defun ebib-goto-prev-set ()
3106 "Moves to the previous set of fields."
3107 (interactive)
3108 (unless (eq ebib-current-field 'type*)
3109 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
3110 (obl-fields (ebib-get-obl-fields entry-type))
3111 (opt-fields (ebib-get-opt-fields entry-type))
3112 (new-field nil))
3113 (if (member ebib-current-field obl-fields)
3114 (ebib-goto-first-field)
3115 (when (member ebib-current-field ebib-additional-fields)
3116 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
3117 (when (or (member ebib-current-field opt-fields)
3118 (null new-field))
3119 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
3120 (if (null new-field)
3121 (ebib-goto-first-field)
3122 (setq ebib-current-field new-field)
3123 (ebib-move-to-field ebib-current-field -1))))))
3125 (defun ebib-edit-entry-type ()
3126 "Edits the entry type."
3127 ;; we don't want the completion buffer to be shown in the index window,
3128 ;; so we need to switch to an appropriate window first. we do this in an
3129 ;; unwind-protect to make sure we always get back to the entry buffer.
3130 (unwind-protect
3131 (progn
3132 (if (eq ebib-layout 'full)
3133 (other-window 1)
3134 (select-window ebib-pre-ebib-window) nil)
3135 (if-str (new-type (completing-read "type: " ebib-entry-types nil t))
3136 (progn
3137 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
3138 (ebib-fill-entry-buffer)
3139 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
3140 (ebib-set-modified t))))
3141 (select-window (get-buffer-window ebib-entry-buffer) nil)))
3143 (defun ebib-edit-crossref ()
3144 "Edits the crossref field."
3145 ;; we don't want the completion buffer to be shown in the index window,
3146 ;; so we need to switch to an appropriate window first. we do this in an
3147 ;; unwind-protect to make sure we always get back to the entry buffer.
3148 (unwind-protect
3149 (progn
3150 (if (eq ebib-layout 'full)
3151 (other-window 1)
3152 (select-window ebib-pre-ebib-window) nil)
3153 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
3154 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
3155 (progn
3156 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
3157 (ebib-set-modified t)))))
3158 (select-window (get-buffer-window ebib-entry-buffer) nil)
3159 ;; we now redisplay the entire entry buffer, so that the crossref'ed
3160 ;; fields show up. this also puts the cursor back on the type field.
3161 (ebib-fill-entry-buffer)
3162 (setq ebib-current-field 'crossref)
3163 (re-search-forward "^crossref")
3164 (ebib-set-fields-highlight)))
3166 ;; we should modify ebib-edit-field, so that it calls the appropriate
3167 ;; helper function, which asks the user for the new value and just returns
3168 ;; that. storing it should then be done by ebib-edit-field, no matter what
3169 ;; sort of field the user edits.
3171 (defun ebib-edit-field ()
3172 "Edits a field of a BibTeX entry."
3173 (interactive)
3174 (cond
3175 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
3176 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
3177 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
3179 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
3180 (raw nil))
3181 (if (multiline-p init-contents)
3182 (ebib-edit-multiline-field)
3183 (when init-contents
3184 (if (raw-p init-contents)
3185 (setq raw t)
3186 (setq init-contents (to-raw init-contents))))
3187 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
3188 (if init-contents
3189 (cons init-contents 0)
3190 nil)
3191 ebib-minibuf-hist))
3192 (puthash ebib-current-field (if raw
3193 new-contents
3194 (concat "{" new-contents "}"))
3195 ebib-cur-entry-hash)
3196 (remhash ebib-current-field ebib-cur-entry-hash))
3197 (ebib-redisplay-current-field)
3198 ;; we move to the next field, but only if ebib-edit-field was
3199 ;; called interactively, otherwise we get a strange bug in
3200 ;; ebib-toggle-raw...
3201 (if (interactive-p) (ebib-next-field))
3202 (ebib-set-modified t))))))
3204 (defun ebib-browse-url-in-field (num)
3205 "Browses a URL in the current field.
3206 The field may contain a whitespace-separated set of URLs. The
3207 prefix argument indicates which URL is to be sent to the
3208 browser."
3209 (interactive "p")
3210 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
3211 (if (not (and urls
3212 (ebib-call-browser urls num)))
3213 (error "No url found in field `%s'" ebib-current-field))))
3215 (defun ebib-view-file-in-field (num)
3216 "Views a file in the current field.
3217 The field may contain a whitespace-separated set of
3218 filenames. The prefix argument indicates which file is to be
3219 viewed."
3220 (interactive "p")
3221 (let ((files (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
3222 (if (not (and files
3223 (ebib-call-file-viewer files num)))
3224 (error "No valid filename found in field `%s'" ebib-current-field))))
3226 (defun ebib-copy-field-contents ()
3227 "Copies the contents of the current field to the kill ring."
3228 (interactive)
3229 (unless (eq ebib-current-field 'type*)
3230 (let ((contents (car (ebib-get-field-value ebib-current-field (ebib-cur-entry-key)))))
3231 (when (stringp contents)
3232 (kill-new contents)
3233 (message "Field contents copied.")))))
3235 (defun ebib-cut-field-contents ()
3236 "Kills the contents of the current field. The killed text is put in the kill ring."
3237 (interactive)
3238 (unless (eq ebib-current-field 'type*)
3239 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
3240 (when (stringp contents)
3241 (remhash ebib-current-field ebib-cur-entry-hash)
3242 (kill-new contents)
3243 (ebib-redisplay-current-field)
3244 (ebib-set-modified t)
3245 (message "Field contents killed.")))))
3247 (defun ebib-yank-field-contents (arg)
3248 "Inserts the last killed text into the current field.
3249 If the current field already has a contents, nothing is inserted,
3250 unless the previous command was also ebib-yank-field-contents,
3251 then the field contents is replaced with the previous yank. That
3252 is, multiple uses of this command function like the combination
3253 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
3254 (interactive "P")
3255 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
3256 (eq ebib-current-field 'crossref)
3257 (unless (eq last-command 'ebib-yank-field-contents)
3258 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
3259 (progn
3260 (setq this-command t)
3261 (beep))
3262 (let ((new-contents (current-kill (cond
3263 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
3266 ((eq arg '-) -2)
3267 (t (1- arg))))))
3268 (when new-contents
3269 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
3270 (ebib-redisplay-current-field)
3271 (ebib-set-modified t)))))
3273 (defun ebib-delete-field-contents ()
3274 "Deletes the contents of the current field.
3275 The deleted text is not put in the kill ring."
3276 (interactive)
3277 (if (eq ebib-current-field 'type*)
3278 (beep)
3279 (when (y-or-n-p "Delete field contents? ")
3280 (remhash ebib-current-field ebib-cur-entry-hash)
3281 (ebib-redisplay-current-field)
3282 (ebib-set-modified t)
3283 (message "Field contents deleted."))))
3285 (defun ebib-toggle-raw ()
3286 "Toggles the raw status of the current field contents."
3287 (interactive)
3288 (unless (or (eq ebib-current-field 'type*)
3289 (eq ebib-current-field 'crossref))
3290 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
3291 (if (not contents) ; if there is no value,
3292 (progn
3293 (ebib-edit-field) ; the user can enter one, which we must then make raw
3294 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
3295 (when new-contents
3296 ;; note: we don't have to check for empty string, since that is
3297 ;; already done in ebib-edit-field
3298 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
3299 (if (raw-p contents)
3300 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
3301 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
3302 (ebib-redisplay-current-field)
3303 (ebib-set-modified t))))
3305 (defun ebib-edit-multiline-field ()
3306 "Edits the current field in multiline-mode."
3307 (interactive)
3308 (unless (or (eq ebib-current-field 'type*)
3309 (eq ebib-current-field 'crossref))
3310 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
3311 (if (raw-p text)
3312 (setq ebib-multiline-raw t)
3313 (setq text (to-raw text))
3314 (setq ebib-multiline-raw nil))
3315 (select-window (ebib-temp-window) nil)
3316 (ebib-multiline-edit 'fields text))))
3318 (defun ebib-insert-abbreviation ()
3319 "Inserts an abbreviation from the ones defined in the database."
3320 (interactive)
3321 (if (gethash ebib-current-field ebib-cur-entry-hash)
3322 (beep)
3323 (when (edb-strings-list ebib-cur-db)
3324 (unwind-protect
3325 (progn
3326 (other-window 1)
3327 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
3328 (string (completing-read "Abbreviation to insert: " collection nil t)))
3329 (when string
3330 (puthash ebib-current-field string ebib-cur-entry-hash)
3331 (ebib-set-modified t))))
3332 (other-window 1)
3333 ;; we can't do this earlier, because we would be writing to the index buffer...
3334 (ebib-redisplay-current-field)
3335 (ebib-next-field)))))
3337 (defun ebib-entry-help ()
3338 "Shows the info node for Ebib's entry buffer."
3339 (interactive)
3340 (ebib-lower)
3341 (info "(ebib) The Entry Buffer"))
3343 ;;;;;;;;;;;;;;;;;;
3344 ;; strings-mode ;;
3345 ;;;;;;;;;;;;;;;;;;
3347 (defvar ebib-strings-mode-map
3348 (let ((map (make-keymap)))
3349 (suppress-keymap map)
3350 (define-key map [up] 'ebib-prev-string)
3351 (define-key map [down] 'ebib-next-string)
3352 (define-key map [prior] 'ebib-strings-page-up)
3353 (define-key map [next] 'ebib-strings-page-down)
3354 (define-key map [home] 'ebib-goto-first-string)
3355 (define-key map [end] 'ebib-goto-last-string)
3356 (define-key map " " 'ebib-strings-page-down)
3357 (define-key map "a" 'ebib-add-string)
3358 (define-key map "b" 'ebib-strings-page-up)
3359 (define-key map "c" 'ebib-copy-string-contents)
3360 (define-key map "d" 'ebib-delete-string)
3361 (define-key map "e" 'ebib-edit-string)
3362 (define-key map "g" 'ebib-goto-first-string)
3363 (define-key map "G" 'ebib-goto-last-string)
3364 (define-key map "h" 'ebib-strings-help)
3365 (define-key map "j" 'ebib-next-string)
3366 (define-key map "k" 'ebib-prev-string)
3367 (define-key map "l" 'ebib-edit-multiline-string)
3368 (define-key map [(control n)] 'ebib-next-string)
3369 (define-key map [(meta n)] 'ebib-strings-page-down)
3370 (define-key map [(control p)] 'ebib-prev-string)
3371 (define-key map [(meta p)] 'ebib-strings-page-up)
3372 (define-key map "q" 'ebib-quit-strings-buffer)
3373 (define-key map "x" 'ebib-export-string)
3374 (define-key map "X" 'ebib-export-all-strings)
3375 (define-key map "\C-xb" 'disabled)
3376 (define-key map "\C-xk" 'disabled)
3377 map)
3378 "Keymap for the ebib strings buffer.")
3380 (define-derived-mode ebib-strings-mode
3381 fundamental-mode "Ebib-strings"
3382 "Major mode for the Ebib strings buffer."
3383 (setq buffer-read-only t)
3384 (setq truncate-lines t))
3386 (defun ebib-quit-strings-buffer ()
3387 "Quits editing the @STRING definitions."
3388 (interactive)
3389 (set-window-dedicated-p (selected-window) nil)
3390 (switch-to-buffer ebib-entry-buffer)
3391 (unless (eq ebib-layout 'full)
3392 (set-window-dedicated-p (selected-window) t))
3393 (select-window (get-buffer-window ebib-index-buffer) nil))
3395 (defun ebib-prev-string ()
3396 "Moves to the previous string."
3397 (interactive)
3398 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
3399 (beep)
3400 ;; go to the beginnig of the highlight and move upward one line.
3401 (goto-char (ebib-highlight-start ebib-strings-highlight))
3402 (forward-line -1)
3403 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3404 (ebib-set-strings-highlight)))
3406 (defun ebib-next-string ()
3407 "Moves to the next string."
3408 (interactive)
3409 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3410 (when (interactive-p) (beep))
3411 (goto-char (ebib-highlight-start ebib-strings-highlight))
3412 (forward-line 1)
3413 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3414 (ebib-set-strings-highlight)))
3416 (defun ebib-goto-first-string ()
3417 "Moves to the first string."
3418 (interactive)
3419 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3420 (goto-char (point-min))
3421 (ebib-set-strings-highlight))
3423 (defun ebib-goto-last-string ()
3424 "Moves to the last string."
3425 (interactive)
3426 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3427 (goto-char (point-max))
3428 (forward-line -1)
3429 (ebib-set-strings-highlight))
3431 (defun ebib-strings-page-up ()
3432 "Moves 10 entries up in the database."
3433 (interactive)
3434 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3435 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3436 (if (<= (- number-of-strings remaining-number-of-strings) 10)
3437 (ebib-goto-first-string)
3438 (setq ebib-current-string (nth
3439 (- number-of-strings remaining-number-of-strings 10)
3440 (edb-strings-list ebib-cur-db)))
3441 (goto-char (ebib-highlight-start ebib-strings-highlight))
3442 (forward-line -10)
3443 (ebib-set-strings-highlight)))
3444 (message ebib-current-string))
3446 (defun ebib-strings-page-down ()
3447 "Moves 10 entries down in the database."
3448 (interactive)
3449 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3450 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3451 (if (<= remaining-number-of-strings 10)
3452 (ebib-goto-last-string)
3453 (setq ebib-current-string (nth
3454 (- number-of-strings remaining-number-of-strings -10)
3455 (edb-strings-list ebib-cur-db)))
3456 (goto-char (ebib-highlight-start ebib-strings-highlight))
3457 (forward-line 10)
3458 (ebib-set-strings-highlight)))
3459 (message ebib-current-string))
3461 (defun ebib-fill-strings-buffer ()
3462 "Fills the strings buffer with the @STRING definitions."
3463 (set-buffer ebib-strings-buffer)
3464 (with-buffer-writable
3465 (erase-buffer)
3466 (dolist (elem (edb-strings-list ebib-cur-db))
3467 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
3468 (insert (format "%-18s %s\n" elem
3469 (if (multiline-p str)
3470 (concat "+" (first-line str))
3471 (concat " " str)))))))
3472 (goto-char (point-min))
3473 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3474 (ebib-set-strings-highlight)
3475 (set-buffer-modified-p nil))
3477 (defun ebib-edit-string ()
3478 "Edits the value of an @STRING definition
3479 When the user enters an empty string, the value is not changed."
3480 (interactive)
3481 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3482 (if (multiline-p init-contents)
3483 (ebib-edit-multiline-string)
3484 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
3485 (if init-contents
3486 (cons init-contents 0)
3487 nil)
3488 ebib-minibuf-hist))
3489 (progn
3490 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
3491 (ebib-redisplay-current-string)
3492 (ebib-next-string)
3493 (ebib-set-modified t))
3494 (error "@STRING definition cannot be empty")))))
3496 (defun ebib-copy-string-contents ()
3497 "Copies the contents of the current string to the kill ring."
3498 (interactive)
3499 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
3500 (kill-new contents)
3501 (message "String value copied.")))
3503 (defun ebib-delete-string ()
3504 "Deletes the current @STRING definition from the database."
3505 (interactive)
3506 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
3507 (remhash ebib-current-string (edb-strings ebib-cur-db))
3508 (with-buffer-writable
3509 (let ((beg (progn
3510 (goto-char (ebib-highlight-start ebib-strings-highlight))
3511 (point))))
3512 (forward-line 1)
3513 (delete-region beg (point))))
3514 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
3515 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
3516 (when (null new-cur-string) ; deleted the last string
3517 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
3518 (forward-line -1))
3519 (setq ebib-current-string new-cur-string))
3520 (ebib-set-strings-highlight)
3521 (ebib-set-modified t)
3522 (message "@STRING definition deleted.")))
3524 (defun ebib-add-string ()
3525 "Creates a new @STRING definition."
3526 (interactive)
3527 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3528 (progn
3529 (if (member new-abbr (edb-strings-list ebib-cur-db))
3530 (error (format "%s already exists" new-abbr)))
3531 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3532 (progn
3533 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3534 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3535 (with-buffer-writable
3536 (insert (format "%-19s %s\n" new-abbr new-string)))
3537 (forward-line -1)
3538 (ebib-set-strings-highlight)
3539 (setq ebib-current-string new-abbr)
3540 (ebib-set-modified t))))))
3542 (defun ebib-export-string (prefix)
3543 "Exports the current @STRING.
3544 The prefix argument indicates which database to copy the string
3545 to. If no prefix argument is present, a filename is asked to
3546 which the string is appended."
3547 (interactive "P")
3548 (let ((num (ebib-prefix prefix)))
3549 (if num
3550 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3551 #'(lambda (db)
3552 (let ((abbr ebib-current-string)
3553 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3554 (if (member abbr (edb-strings-list db))
3555 (error "@STRING definition already exists in database %d" num)
3556 (ebib-insert-string abbr string db t)))))
3557 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3558 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3559 #'(lambda ()
3560 (insert (format "\n@string{%s = %s}\n"
3561 ebib-current-string
3562 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3564 (defun ebib-export-all-strings (prefix)
3565 "Exports all @STRING definitions.
3566 If a prefix argument is given, it is taken as the database to
3567 copy the definitions to. Without prefix argument, asks for a file
3568 to append them to."
3569 (interactive "P")
3570 (when ebib-current-string ; there is always a current string, unless there are no strings
3571 (let ((num (ebib-prefix prefix)))
3572 (if num
3573 (ebib-export-to-db
3574 num "All @STRING definitions copied to database %d"
3575 #'(lambda (db)
3576 (mapc #'(lambda (abbr)
3577 (if (member abbr (edb-strings-list db))
3578 (message "@STRING definition `%s' already exists in database %d" abbr num)
3579 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3580 (edb-strings-list ebib-cur-db))))
3581 (ebib-export-to-file "Export all @STRING definitions to file: "
3582 "All @STRING definitions exported to %s"
3583 #'(lambda ()
3584 (insert (format "\n")) ; to keep things tidy.
3585 (ebib-format-strings ebib-cur-db)))))))
3587 (defun ebib-edit-multiline-string ()
3588 "Edits the current string in multiline-mode."
3589 (interactive)
3590 (select-window (ebib-temp-window) nil)
3591 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3593 (defun ebib-strings-help ()
3594 "Shows the info node on Ebib's strings buffer."
3595 (interactive)
3596 (ebib-lower)
3597 (info "(ebib) The Strings Buffer"))
3599 ;;;;;;;;;;;;;;;;;;;;
3600 ;; multiline edit ;;
3601 ;;;;;;;;;;;;;;;;;;;;
3603 (define-minor-mode ebib-multiline-mode
3604 "Minor mode for Ebib's multiline edit buffer."
3605 :init-value nil :lighter nil :global nil
3606 :keymap '(("\C-c|q" . ebib-quit-multiline-edit)
3607 ("\C-c|c" . ebib-cancel-multiline-edit)
3608 ("\C-c|s" . ebib-save-from-multiline-edit)
3609 ("\C-c|h" . ebib-multiline-help)))
3611 (easy-menu-define ebib-multiline-menu ebib-multiline-mode-map "Ebib multiline menu"
3612 '("Ebib"
3613 ["Store Text and Exit" ebib-quit-multiline-edit t]
3614 ["Cancel Edit" ebib-cancel-multiline-edit t]
3615 ["Save Text" ebib-save-from-multiline-edit t]
3616 ["Help" ebib-multiline-help t]))
3618 (easy-menu-add ebib-multiline-menu ebib-multiline-mode-map)
3620 (defun ebib-multiline-edit (type &optional starttext)
3621 "Switches to Ebib's multiline edit buffer.
3622 STARTTEXT is a string that contains the initial text of the buffer."
3623 ;; note: the buffer is put in the currently active window!
3624 (setq ebib-pre-multiline-buffer (current-buffer))
3625 (switch-to-buffer ebib-multiline-buffer)
3626 (set-buffer-modified-p nil)
3627 (erase-buffer)
3628 (setq ebib-editing type)
3629 (when starttext
3630 (insert starttext)
3631 (goto-char (point-min))
3632 (set-buffer-modified-p nil)))
3634 (defun ebib-quit-multiline-edit ()
3635 "Quits the multiline edit buffer, saving the text."
3636 (interactive)
3637 (ebib-store-multiline-text)
3638 (ebib-leave-multiline-edit-buffer)
3639 (cond
3640 ((eq ebib-editing 'fields)
3641 (ebib-next-field))
3642 ((eq ebib-editing 'strings)
3643 (ebib-next-string)))
3644 (message "Text stored."))
3646 (defun ebib-cancel-multiline-edit ()
3647 "Quits the multiline edit buffer and discards the changes."
3648 (interactive)
3649 (catch 'no-cancel
3650 (when (buffer-modified-p)
3651 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3652 (throw 'no-cancel nil)))
3653 (ebib-leave-multiline-edit-buffer)))
3655 (defun ebib-leave-multiline-edit-buffer ()
3656 "Leaves the multiline edit buffer.
3657 Restores the previous buffer in the window that the multiline
3658 edit buffer was shown in."
3659 (switch-to-buffer ebib-pre-multiline-buffer)
3660 (cond
3661 ((eq ebib-editing 'preamble)
3662 (select-window (get-buffer-window ebib-index-buffer) nil))
3663 ((eq ebib-editing 'fields)
3664 ;; in full-frame layout, select-window isn't necessary, but it doesn't hurt either.
3665 (select-window (get-buffer-window ebib-entry-buffer) nil)
3666 (ebib-redisplay-current-field))
3667 ((eq ebib-editing 'strings)
3668 ;; in full-frame layout, select-window isn't necessary, but it doesn't hurt either.
3669 (select-window (get-buffer-window ebib-strings-buffer) nil)
3670 (ebib-redisplay-current-string))))
3672 (defun ebib-save-from-multiline-edit ()
3673 "Saves the database from within the multiline edit buffer.
3674 The text being edited is stored before saving the database."
3675 (interactive)
3676 (ebib-store-multiline-text)
3677 (ebib-save-database ebib-cur-db)
3678 (set-buffer-modified-p nil))
3680 (defun ebib-store-multiline-text ()
3681 "Stores the text being edited in the multiline edit buffer."
3682 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3683 (cond
3684 ((eq ebib-editing 'preamble)
3685 (if (equal text "")
3686 (setf (edb-preamble ebib-cur-db) nil)
3687 (setf (edb-preamble ebib-cur-db) text)))
3688 ((eq ebib-editing 'fields)
3689 (if (equal text "")
3690 (remhash ebib-current-field ebib-cur-entry-hash)
3691 (when (not ebib-multiline-raw)
3692 (setq text (from-raw text)))
3693 (puthash ebib-current-field text ebib-cur-entry-hash)))
3694 ((eq ebib-editing 'strings)
3695 (if (equal text "")
3696 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3697 ;; MESSAGE, but we also do not switch back to the strings
3698 ;; buffer. this may not be so bad, actually, because the user
3699 ;; may want to change his edit.
3700 (error "@STRING definition cannot be empty ")
3701 (setq text (from-raw text)) ; strings cannot be raw
3702 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3703 (ebib-set-modified t))
3705 (defun ebib-multiline-help ()
3706 "Show the info node on Ebib's multiline edit buffer."
3707 (interactive)
3708 (ebib-lower)
3709 (info "(ebib) The Multiline Edit Buffer"))
3711 ;;;;;;;;;;;;;;;;;;;
3712 ;; ebib-log-mode ;;
3713 ;;;;;;;;;;;;;;;;;;;
3715 (defvar ebib-log-mode-map
3716 (let ((map (make-keymap)))
3717 (suppress-keymap map)
3718 (define-key map " " 'scroll-up)
3719 (define-key map "b" 'scroll-down)
3720 (define-key map "q" 'ebib-quit-log-buffer)
3721 map)
3722 "Keymap for the ebib log buffer.")
3724 (define-derived-mode ebib-log-mode
3725 fundamental-mode "Ebib-log"
3726 "Major mode for the Ebib log buffer."
3727 (local-set-key "\C-xb" 'ebib-quit-log-buffer)
3728 (local-set-key "\C-xk" 'ebib-quit-log-buffer))
3730 (defun ebib-quit-log-buffer ()
3731 "Exits the log buffer."
3732 (interactive)
3733 (set-window-dedicated-p (selected-window) nil)
3734 (switch-to-buffer ebib-entry-buffer)
3735 (unless (eq ebib-layout 'full)
3736 (set-window-dedicated-p (selected-window) t))
3737 (select-window (get-buffer-window ebib-index-buffer) nil))
3739 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3740 ;; functions for non-Ebib buffers ;;
3741 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3743 (defun ebib-import ()
3744 "Searches for BibTeX entries in the current buffer.
3745 The entries are added to the current database (i.e. the database
3746 that was active when Ebib was lowered. Works on the whole buffer,
3747 or on the region if it is active."
3748 (interactive)
3749 (if (not ebib-cur-db)
3750 (error "No database loaded. Use `o' to open a database")
3751 (if (edb-virtual ebib-cur-db)
3752 (error "Cannot import to a virtual database")
3753 (with-syntax-table ebib-syntax-table
3754 (save-excursion
3755 (save-restriction
3756 (if (region-active)
3757 (narrow-to-region (region-beginning)
3758 (region-end)))
3759 (let ((buffer (current-buffer)))
3760 (with-temp-buffer
3761 (insert-buffer-substring buffer)
3762 (let ((n (ebib-find-bibtex-entries t)))
3763 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3764 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3765 (when (edb-strings-list ebib-cur-db)
3766 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3767 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3768 (ebib-fill-entry-buffer)
3769 (ebib-fill-index-buffer)
3770 (ebib-set-modified t)
3771 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3772 (car n)
3773 (cadr n)
3774 (if (caddr n)
3776 "no"))))))))))))
3778 (defun ebib-get-db-from-filename (filename)
3779 "Returns the database struct associated with FILENAME."
3780 (when (file-name-absolute-p filename)
3781 (setq filename (expand-file-name filename))) ; expand ~, . and ..
3782 (catch 'found
3783 (mapc #'(lambda (db)
3784 (if (string= filename
3785 ; if filename is absolute, we want to compare to the
3786 ; absolute filename of the database, otherwise we
3787 ; should use only the non-directory component.
3788 (if (file-name-absolute-p filename)
3789 (edb-filename db)
3790 (file-name-nondirectory (edb-filename db))))
3791 (throw 'found db)))
3792 ebib-databases)
3793 nil))
3795 (defun ebib-get-local-databases ()
3796 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3797 Each element in the list is a string holding the name of the .bib
3798 file. This function simply searches the current LaTeX file or its
3799 master file for a \\bibliography command and returns the file(s)
3800 given in its argument. If no \\bibliography command is found,
3801 returns the symbol NONE."
3802 (let ((texfile-buffer (current-buffer))
3803 texfile)
3804 ;; if AucTeX's TeX-master is used and set to a string, we must
3805 ;; search that file for a \bibliography command, as it's more
3806 ;; likely to be in there than in the file we're in.
3807 (and (boundp 'TeX-master)
3808 (stringp TeX-master)
3809 (setq texfile (ensure-extension TeX-master "tex")))
3810 (with-temp-buffer
3811 (if (and texfile (file-readable-p texfile))
3812 (insert-file-contents texfile)
3813 (insert-buffer-substring texfile-buffer))
3814 (save-excursion
3815 (goto-char (point-min))
3816 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3817 (mapcar #'(lambda (file)
3818 (ensure-extension file "bib"))
3819 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3820 'none)))))
3822 (defun ebib-insert-bibtex-key ()
3823 "Inserts a BibTeX key at POINT.
3824 The user is prompted for a BibTeX key and has to choose one from
3825 the database(s) associated with the current LaTeX file, or from
3826 the current database if there is no \\bibliography command. Tab
3827 completion works."
3828 (interactive)
3829 (ebib-execute-when
3830 ((database)
3831 (or ebib-local-bibtex-filenames
3832 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3833 (let (collection)
3834 (if (eq ebib-local-bibtex-filenames 'none)
3835 (if (null (edb-cur-entry ebib-cur-db))
3836 (error "No entries found in current database")
3837 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3838 (mapc #'(lambda (file)
3839 (let ((db (ebib-get-db-from-filename file)))
3840 (cond
3841 ((null db)
3842 (message "Database %s not loaded" file))
3843 ((null (edb-cur-entry db))
3844 (message "No entries in database %s" file))
3845 (t (setq collection (append (ebib-create-collection (edb-database db))
3846 collection))))))
3847 ebib-local-bibtex-filenames))
3848 (when collection
3849 (let* ((key (completing-read "Key to insert: " collection nil t nil ebib-minibuf-hist))
3850 (insert-string (if-str (command (completing-read "Command to use: " ebib-insertion-commands
3851 nil nil nil ebib-minibuf-hist))
3852 (ebib-create-citation-command command
3854 (cadr (assoc command ebib-insertion-commands))
3857 (buffer-local-value 'major-mode (current-buffer)))
3858 key)))
3859 (when insert-string
3860 (insert insert-string))))))
3861 ((default)
3862 (error "No database loaded"))))
3864 (defun ebib-entry-summary ()
3865 "Shows the fields of the key at POINT.
3866 The key is searched in the database associated with the LaTeX
3867 file, or in the current database if no \\bibliography command can
3868 be found."
3869 (interactive)
3870 (ebib-execute-when
3871 ((database)
3872 (or ebib-local-bibtex-filenames
3873 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3874 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3875 entry
3876 database)
3877 (if (eq ebib-local-bibtex-filenames 'none)
3878 (if (not (member key (edb-keys-list ebib-cur-db)))
3879 (error "Entry `%s' is not in the current database" key)
3880 (setq entry (gethash key (edb-database ebib-cur-db)))
3881 (setq database ebib-cur-db))
3882 (multiple-value-setq (entry database)
3883 (catch 'found
3884 (mapc #'(lambda (file)
3885 (let ((db (ebib-get-db-from-filename file)))
3886 (if (null db)
3887 (message "Database %s not loaded" file)
3888 (if (member key (edb-keys-list db))
3889 (throw 'found (values (gethash key (edb-database db)) db))))))
3890 ebib-local-bibtex-filenames)
3891 (list nil nil))))
3892 (if (null entry)
3893 (error "Entry `%s' not found" key)
3894 (let ((index-window (get-buffer-window ebib-index-buffer)))
3895 (if (not index-window)
3896 (with-help-window "*Help*"
3897 (ebib-format-fields entry 'princ))
3898 (with-selected-window index-window
3899 (setq ebib-cur-db database)
3900 (ebib-fill-index-buffer)
3901 (setf (edb-cur-entry ebib-cur-db) (member key (edb-keys-list ebib-cur-db)))
3902 (goto-char (point-min))
3903 (re-search-forward (format "^%s " key))
3904 (beginning-of-line)
3905 (ebib-set-index-highlight))
3906 (ebib-fill-entry-buffer))))))
3907 ((default)
3908 (error "No database(s) loaded"))))
3910 (provide 'ebib)
3912 ;;; ebib ends here