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