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