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