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