3 ;; Copyright (c) 2003-2008 Joost Kremers
4 ;; All rights reserved.
6 ;; Redistribution and use in source and binary forms, with or without
7 ;; modification, are permitted provided that the following conditions
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.
31 ;;;;;;;;;;;;;;;;;;;;;;
32 ;; global variables ;;
33 ;;;;;;;;;;;;;;;;;;;;;;
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."
44 (defcustom ebib-preload-bib-files nil
45 "*List of .bib files to load automatically when Ebib starts."
47 :type
'(repeat (file :must-match t
)))
49 (defcustom ebib-additional-fields
'(crossref url annote abstract
50 keywords file timestamp
)
51 "*Holds a list of the additional fields."
53 :type
'(repeat (symbol :tag
"Field")))
55 (defcustom ebib-index-window-size
10
56 "*The number of lines used for the keys buffer window."
60 (defcustom ebib-index-display-fields nil
61 "*Holds a list of the fields to display in the index buffer."
63 :type
'(repeat (symbol :tag
"Index Field")))
65 (defcustom ebib-insertion-strings
'((0 .
"%s")
67 "*The string to insert when calling EBIB-INSERT-BIBTEX-KEY.
68 The directive \"%s\" is replaced with the entry key."
70 :type
'(repeat (cons :tag
"Insertion string" (integer :tag
"Number") (string))))
72 (defcustom ebib-sort-order nil
73 "*The fields on which the BibTeX entries are to be sorted in the .bib file.
74 Sorting is done on different sort levels, and each sort level contains one
77 :type
'(repeat (repeat :tag
"Sort level" (symbol :tag
"Sort field"))))
79 (defcustom ebib-save-xrefs-first nil
80 "*If true, entries with a crossref field will be saved first in the .bib-file.
81 Setting this option has unpredictable results for the sort order
82 of entries, so it is not compatible with setting the Sort Order option."
86 (defface ebib-crossref-face
'((t (:foreground
"red")))
87 "*Face used to indicate values inherited from crossreferenced entries."
90 (defface ebib-marked-face
(if (featurep 'xemacs
)
91 '((t (:foreground
"white" :background
"red")))
92 '((t (:inverse-video t
))))
93 "*Face to indicate marked entries."
96 (defcustom ebib-use-timestamp nil
97 "*If true, new entries will get a time stamp.
98 The time stamp will be stored in a field \"timestamp\" that can
99 be made visible with the `H' command in the index buffer."
103 (defcustom ebib-timestamp-format
"%a %b %e %T %Y"
104 "*Format of the time string used in the timestamp.
105 The format is given directly to FORMAT-TIME-STRING, see the
106 documentation of that function for details."
110 (defcustom ebib-standard-url-field
'url
111 "*Standard field to store urls in.
112 In the index buffer, the command ebib-browse-url can be used to
113 send a url to a browser. This option sets the field from which
114 this command extracts the url."
118 (defcustom ebib-url-regexp
"\\\\url{\\(.*\\)}\\|https?://[^ '<>\"\n\t\f]+"
119 "*Regular expression to extract urls."
123 (defcustom ebib-browser-command
""
124 "*Command to call the browser with.
125 GNU/Emacs has a function call-browser, which is used if this
128 :type
'(string :tag
"Browser command"))
130 (defcustom ebib-standard-file-field
'file
131 "*Standard field to store filenames in.
132 In the index buffer, the command ebib-view-file can be used to
133 view a file externally. This option sets the field from which
134 this command extracts the filename."
138 (defcustom ebib-file-associations
'(("pdf" .
"xpdf")
140 "*List of file associations.
141 Lists file extensions together with external programs to handle
142 files with those extensions. If the external program is left
143 blank, Ebib tries to handle the file internally in
144 Emacs (e.g. with doc-view-mode)."
146 :type
'(repeat (cons :tag
"File association"
147 (string :tag
"Extension") (string :tag
"Command"))))
149 (defcustom ebib-file-regexp
"[^?|\\:*<>\" \n\t\f]+"
150 "*Regular expression to match filenames."
154 (defcustom ebib-file-search-dirs
'("~")
155 "*List of directories to search for files."
157 :type
'(repeat :tag
"Search directories" (string :tag
"Directory")))
159 (defcustom ebib-print-preamble nil
160 "*Preamble used for the LaTeX file for printing the database.
161 Each string is added to the preamble on a separate line."
163 :type
'(repeat (string :tag
"Add to preamble")))
165 (defcustom ebib-print-multiline nil
166 "*If set, multiline fields are included when printing the database."
170 (defcustom ebib-latex-preamble
'("\\bibliographystyle{plain}")
171 "*Preamble used for the LaTeX file for BibTeXing the database.
172 Each string is added to the preamble on a separate line."
174 :type
'(repeat (string :tag
"Add to preamble")))
176 (defcustom ebib-print-tempfile
""
177 "*Temporary file for use with EBIB-PRINT-DATABASE and EBIB-LATEX-DATABASE."
181 (defcustom ebib-allow-identical-fields nil
182 "*If set, Ebib handles multiple occurrences of a field gracefully."
186 (defvar ebib-entry-types-hash
(make-hash-table)
187 "Holds the hash table containing the entry type definitions.")
188 (defvar ebib-unique-field-list nil
189 "Holds a list of all field names.")
191 (defmacro add-to-listq
(listvar element
&optional append fn
)
192 (if (or (featurep 'xemacs
)
193 (string< emacs-version
"22"))
194 `(add-to-list (quote ,listvar
) ,element
,append
)
195 `(add-to-list (quote ,listvar
) ,element
,append
,fn
)))
197 (defun ebib-set-entry-types-hash (var value
)
198 "Sets EBIB-ENTRY-TYPES-HASH on the basis of EBIB-ENTRY-TYPES"
199 (set-default var value
)
200 (clrhash ebib-entry-types-hash
)
201 (setq ebib-unique-field-list nil
)
202 (mapc #'(lambda (entry)
203 (puthash (car entry
) (cdr entry
) ebib-entry-types-hash
)
204 (mapc #'(lambda (field)
205 (add-to-listq ebib-unique-field-list field t
'eq
))
207 (mapc #'(lambda (field)
208 (add-to-listq ebib-unique-field-list field t
'eq
))
212 (defcustom ebib-entry-types
213 '((article ;; name of entry type
214 (author title journal year
) ;; obligatory fields
215 (volume number pages month note
)) ;; optional fields
218 (author title publisher year
)
219 (editor volume number series address edition month note
))
223 (author howpublished address month year note
))
226 (author title chapter pages publisher year
)
227 (editor volume series address edition month note
))
230 (author title booktitle publisher year
)
231 (editor volume number series type chapter pages address edition month note
))
234 (author title booktitle year
)
235 (editor pages organization publisher address month note
))
239 (author organization address edition month year note
))
243 (title author howpublished month year note
))
246 (author title school year
)
247 (address month note
))
250 (author title school year
)
251 (address month note
))
255 (editor publisher organization address month note
))
258 (author title institution year
)
259 (type number address month note
))
265 "List of entry type definitions for Ebib"
267 :type
'(repeat (list :tag
"Entry type" (symbol :tag
"Name")
268 (repeat :tag
"Obligatory fields" (symbol :tag
"Field"))
269 (repeat :tag
"Optional fields" (symbol :tag
"Field"))))
270 :set
'ebib-set-entry-types-hash
)
272 ;; generic for all databases
274 ;; constants and variables that are set only once
275 (defconst ebib-bibtex-identifier
"[^\"#%'(),={} \t\n\f]*" "Regex describing a licit BibTeX identifier.")
276 (defconst ebib-version
"==VERSION==")
277 (defvar ebib-initialized nil
"T if Ebib has been initialized.")
279 ;; buffers and highlights
280 (defvar ebib-index-buffer nil
"The index buffer.")
281 (defvar ebib-entry-buffer nil
"The entry buffer.")
282 (defvar ebib-strings-buffer nil
"The strings buffer.")
283 (defvar ebib-multiline-buffer nil
"Buffer for editing multiline strings.")
284 (defvar ebib-help-buffer nil
"Buffer showing Ebib help.")
285 (defvar ebib-log-buffer nil
"Buffer showing warnings and errors during loading of .bib files")
286 (defvar ebib-index-highlight nil
"Highlight to mark the current entry.")
287 (defvar ebib-fields-highlight nil
"Highlight to mark the current field.")
288 (defvar ebib-strings-highlight nil
"Highlight to mark the current string.")
290 ;; general bookkeeping
291 (defvar ebib-minibuf-hist nil
"Holds the minibuffer history for Ebib")
292 (defvar ebib-saved-window-config nil
"Stores the window configuration when Ebib is called.")
293 (defvar ebib-export-filename nil
"Filename to export entries to.")
294 (defvar ebib-push-buffer nil
"Buffer to push entries to.")
295 (defvar ebib-search-string nil
"Stores the last search string.")
296 (defvar ebib-editing nil
"Indicates what the user is editing.
297 Its value can be 'strings, 'fields, or 'preamble.")
298 (defvar ebib-multiline-raw nil
"Indicates whether the multiline text being edited is raw.")
299 (defvar ebib-before-help nil
"Stores the buffer the user was in when he displayed the help message.")
300 (defvar ebib-log-error nil
"Indicates whether an error was logged.")
301 (defvar ebib-local-bibtex-filenames nil
"A buffer-local variable holding a list of the name(s) of that buffer's .bib file")
302 (make-variable-buffer-local 'ebib-local-bibtex-filenames
)
303 (defvar ebib-syntax-table
(make-syntax-table) "Syntax table used for reading .bib files.")
304 (modify-syntax-entry ?\
[ "." ebib-syntax-table
)
305 (modify-syntax-entry ?\
] "." ebib-syntax-table
)
306 (modify-syntax-entry ?\
( "." ebib-syntax-table
)
307 (modify-syntax-entry ?\
) "." ebib-syntax-table
)
308 (modify-syntax-entry ?
\" "w" ebib-syntax-table
)
312 ;; each database is represented by a struct
314 (database (make-hash-table :test
'equal
)) ; hashtable containing the database itself
315 (keys-list nil
) ; sorted list of the keys in the database
316 (cur-entry nil
) ; sublist of KEYS-LIST that starts with the current entry
317 (marked-entries nil
) ; list of marked entries
318 (n-entries 0) ; number of entries stored in this database
319 (strings (make-hash-table :test
'equal
)) ; hashtable with the @STRING definitions
320 (strings-list nil
) ; sorted list of the @STRING abbreviations
321 (preamble nil
) ; string with the @PREAMBLE definition
322 (filename nil
) ; name of the BibTeX file that holds this database
323 (name nil
) ; name of the database
324 (modified nil
) ; has this database been modified?
325 (make-backup nil
) ; do we need to make a backup of the .bib file?
326 (virtual nil
)) ; is this a virtual database?
328 ;; the master list and the current database
329 (defvar ebib-databases nil
"List of structs containing the databases")
330 (defvar ebib-cur-db nil
"The database that is currently active")
332 ;;;;;; bookkeeping required when editing field values or @STRING definitions
334 (defvar ebib-hide-hidden-fields t
"If set to T, hidden fields are not shown.")
336 ;; these two variables are set when the user enters the entry buffer
337 (defvar ebib-cur-entry-hash nil
"The hash table containing the data of the current entry.")
338 (defvar ebib-cur-entry-fields nil
"The fields of the type of the current entry.")
340 ;; and these two are set by EBIB-FILL-ENTRY-BUFFER and EBIB-FILL-STRINGS-BUFFER, respectively
341 (defvar ebib-current-field nil
"The current field.")
342 (defvar ebib-current-string nil
"The current @STRING definition.")
344 ;; we define these variables here, but only give them a value at the end of
345 ;; the file, because the long strings mess up Emacs' syntax highlighting.
346 (defvar ebib-index-buffer-help nil
"Help for the index buffer.")
347 (defvar ebib-entry-buffer-help nil
"Help for the entry buffer.")
348 (defvar ebib-strings-buffer-help nil
"Help for the strings buffer.")
350 ;; the prefix key is stored in a variable so that the user can customise it.
351 (defvar ebib-prefix-key ?\
;)
353 ;; this is an AucTeX variable, but we want to check its value, so let's
354 ;; keep the compiler from complaining.
358 ;; this is to keep XEmacs from complaining.
360 (if (featurep 'xemacs
)
361 (defvar mark-active
)))
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;; useful macros and functions ;;
365 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
367 (defmacro nor
(&rest args
)
368 "Returns T if none of its arguments are true."
371 ;; we sometimes (often, in fact ;-) need to do something with a string, but
372 ;; take special action (or do nothing) if that string is empty. IF-STR
373 ;; makes that easier:
375 (defmacro if-str
(bindvar then
&rest else
)
376 "Execute THEN only if STRING is nonempty.
377 Format: (if-str (var value) then-form [else-forms]) VAR is bound
378 to VALUE, which is evaluated. If VAR is a nonempty string,
379 THEN-FORM is executed. If VAR is either \"\" or nil, ELSE-FORM is
380 executed. Returns the value of THEN or of ELSE."
382 `(let ,(list bindvar
)
383 (if (nor (null ,(car bindvar
))
384 (equal ,(car bindvar
) ""))
388 (defmacro last1
(lst &optional n
)
389 "Returns the last (or Nth last) element of LST."
390 `(car (last ,lst
,n
)))
392 ;; we sometimes need to walk through lists. these functions yield the
393 ;; element directly preceding or following ELEM in LIST. in order to work
394 ;; properly, ELEM must be unique in LIST, obviously. if ELEM is the
395 ;; first/last element of LIST, or if it is not contained in LIST at all,
396 ;; the result is nil.
397 (defun next-elem (elem list
)
398 (cadr (member elem list
)))
400 (defun prev-elem (elem list
)
401 (if (or (equal elem
(car list
))
402 (not (member elem list
)))
404 (last1 list
(1+ (length (member elem list
))))))
406 (defun read-string-at-point (chars)
407 "Reads a string at POINT delimited by CHARS and returns it.
408 CHARS is a string of characters that should not occur in the string."
410 (skip-chars-backward (concat "^" chars
))
412 (looking-at-goto-end (concat "[^" chars
"]*"))
413 (buffer-substring-no-properties beg
(point)))))
415 (defun remove-from-string (string remove
)
416 "Returns a copy of STRING with all the occurrences of REMOVE taken out.
417 REMOVE can be a regex."
418 (apply 'concat
(split-string string remove
)))
420 (defun in-string (char string
)
421 "Returns T if CHAR is in STRING, otherwise NIL."
423 (do ((len (length string
))
426 (if (eq char
(aref string i
))
429 (defun ensure-extension (string ext
)
430 "Makes sure STRING has the extension EXT, by appending it if necessary.
431 EXT should be an extension without the dot."
432 (if (string-match (concat "\\." ext
"$") string
)
434 (concat string
"." ext
)))
436 (defmacro with-buffer-writable
(&rest body
)
437 "Makes the current buffer writable and executes the commands in BODY.
438 After BODY is executed, the buffer modified flag is unset."
439 (declare (indent defun
))
441 (let ((buffer-read-only nil
))
443 (set-buffer-modified-p nil
)))
445 (defmacro safe-write-region
(start end filename
&optional append visit lockname mustbenew
)
446 "XEmacs does not have the MUSTBENEW argument, so this is a way to implement it."
447 (if (featurep 'xemacs
)
448 `(if (and (file-exists-p ,filename
)
449 (not (y-or-n-p (format "File %s already exists; overwrite anyway? " ,filename
))))
450 (error "File %s exist" ,filename
)
451 (write-region ,start
,end
,filename
,append
,visit
,lockname
))
452 `(write-region ,start
,end
,filename
,append
,visit
,lockname
,mustbenew
)))
454 (defun symbol-or-string (x)
455 "Returns the symbol-name of X if X is a symbol, otherwise return X.
456 Much like SYMBOL-NAME, except it does not throw an error if X is
462 ;; XEmacs doesn't know about propertize...
463 (if (not (fboundp 'propertize
))
464 (defun propertize (string &rest properties
)
465 "Return a copy of STRING with text properties added.
466 First argument is the string to copy. Remaining arguments form a
467 sequence of PROPERTY VALUE pairs for text properties to add to
469 (let ((new-string (copy-sequence string
)))
470 (add-text-properties 0 (length new-string
) properties new-string
)
473 (defun region-active ()
474 (if (featurep 'xemacs
)
478 ;; RAW-P determines if STRING is raw. note that we cannot do this by
479 ;; simply checking whether STRING begins with { and ends with } (or
480 ;; begins and ends with "), because something like "{abc} # D # {efg}"
481 ;; would then be incorrectly recognised as non-raw. so we need to do
482 ;; the following: take out everything that is between braces or
483 ;; quotes, and see if anything is left. if there is, the original
484 ;; string was raw, otherwise it was not.
486 ;; so i first check whether the string begins with { or ". if not, we
487 ;; certainly have a raw string. (RAW-P recognises this through the default
488 ;; clause of the COND.) if the first character is { or ", we first take out
489 ;; every occurrence of backslash-escaped { and } or ", so that the rest of
490 ;; the function does not get confused over them.
492 ;; then, if the first character is {, i use REMOVE-FROM-STRING to take out
493 ;; every occurrence of the regex "{[^{]*?}", which translates to "the
494 ;; smallest string that starts with { and ends with }, and does not contain
495 ;; another {. IOW, it takes out the innermost braces and their
496 ;; contents. because braces may be embedded, we have to repeat this step
497 ;; until no more balanced braces are found in the string. (note that it
498 ;; would be unwise to check for just the occurrence of { or }, because that
499 ;; would throw RAW-P in an infinite loop if a string contains an unbalanced
502 ;; for strings beginning with " i do the same, except that it is not
503 ;; necessary to repeat this in a WHILE loop, for the simple reason that
504 ;; strings surrounded with double quotes cannot be embedded; i.e.,
505 ;; "ab"cd"ef" is not a valid (BibTeX) string, while {ab{cd}ef} is.
507 ;; note: because these strings are to be fed to BibTeX and ultimately
508 ;; (La)TeX, it might seem that we don't need to worry about strings
509 ;; containing unbalanced braces, because (La)TeX would choke on them. but
510 ;; the user may inadvertently enter such a string, and we therefore need to
511 ;; be able to handle it. (alternatively, we could perform a check on
512 ;; strings and warn the user.)
514 (defun raw-p (string)
515 "Non-nil if STRING is raw."
516 (when (stringp string
)
518 ((eq (string-to-char string
) ?\
{)
519 ;; we remove all occurrences of `\{' and of `\}' from the string:
520 (let ((clear-str (remove-from-string (remove-from-string string
"[\\][{]")
522 (while (and (in-string ?\
{ clear-str
) (in-string ?\
} clear-str
))
523 (setq clear-str
(remove-from-string clear-str
"{[^{]*?}")))
524 (> (length clear-str
) 0)))
525 ((eq (string-to-char string
) ?
\")
526 (let ((clear-str (remove-from-string string
"[\\][\"]"))) ; remove occurrences of `\"'
527 (setq clear-str
(remove-from-string clear-str
"\"[^\"]*?\""))
528 (> (length clear-str
) 0)))
531 (defun to-raw (string)
532 "Converts a string to its raw counterpart."
533 (if (and (stringp string
)
534 (not (raw-p string
)))
535 (substring string
1 -
1)
538 (defun from-raw (string)
539 "Converts a raw string to a non-raw one."
541 (concat "{" string
"}")
544 (defun multiline-p (string)
545 "True if STRING is multiline."
547 (string-match "\n" string
)))
549 (defun first-line (string)
550 "Returns the first line of a multi-line string."
551 (string-match "\n" string
)
552 (substring string
0 (match-beginning 0)))
554 (defun sort-in-buffer (limit str
)
555 "Moves POINT to the right position to insert STR in a buffer with lines sorted A-Z."
560 (goto-char (point-min))
562 (setq middle
(/ (+ lower upper
1) 2))
563 (goto-line middle
) ; if this turns out to be where we need to be,
564 (beginning-of-line) ; this puts POINT at the right spot.
565 (> (- upper lower
) 1)) ; if upper and lower differ by only 1, we have found the
566 ; position to insert the entry in.
570 (if (string< (buffer-substring-no-properties beg
(point)) str
)
572 (setq upper middle
)))))))))
574 (defun match-all (match-str string
)
575 "Highlights all the matches of MATCH-STR in STRING.
576 The return value is a list of two elements: the first is the
577 modified string, the second either t or nil, indicating whether a
578 match was found at all."
579 (do ((counter 0 (match-end 0)))
580 ((not (string-match match-str string counter
)) (values string
(not (= counter
0))))
581 (add-text-properties (match-beginning 0) (match-end 0) '(face highlight
) string
)))
583 (defun looking-at-goto-end (str &optional match
)
584 "Like LOOKING-AT but moves point to the end of the matching string.
585 MATCH acts just like the argument to MATCH-END, and defaults to 0."
586 (or match
(setq match
0))
587 (let ((case-fold-search t
))
589 (goto-char (match-end match
)))))
591 ;; this needs to be wrapped in an eval-and-compile, to keep Emacs from
592 ;; complaining that ebib-execute-helper isn't defined when it compiles
593 ;; ebib-execute-when.
595 (defun ebib-execute-helper (env)
596 "Helper function for EBIB-EXECUTE-WHEN."
600 (edb-cur-entry ebib-cur-db
)))
601 ((eq env
'marked-entries
)
603 (edb-marked-entries ebib-cur-db
)))
608 (not (edb-virtual ebib-cur-db
))))
609 ((eq env
'virtual-db
)
611 (edb-virtual ebib-cur-db
)))
612 ((eq env
'no-database
)
616 (defmacro ebib-execute-when
(&rest forms
)
617 "Macro to facilitate writing Ebib functions.
618 This functions essentially like a COND clause: the basic format
619 is (ebib-execute-when FORMS ...), where each FORM is built up
620 as (ENVIRONMENTS BODY). ENVIRONMENTS is a list of symbols (not
621 quoted) that specify under which conditions BODY is to be
622 executed. Valid symbols are:
624 entries: execute when there are entries in the database,
625 marked-entries: execute when there are marked entries in the database,
626 database: execute if there is a database,
627 no-database: execute if there is no database,
628 real-db: execute when there is a database and it is real,
629 virtual-db: execute when there is a database and it is virtual,
630 default: execute if all else fails.
632 Just like with COND, only one form is actually executed, the
633 first one that matches. If ENVIRONMENT contains more than one
634 condition, BODY is executed if they all match (i.e., the
635 conditions are AND'ed.)"
636 (declare (indent defun
))
638 ,@(mapcar #'(lambda (form)
639 (cons (if (= 1 (length (car form
)))
640 (ebib-execute-helper (caar form
))
641 `(and ,@(mapcar #'(lambda (env)
642 (ebib-execute-helper env
))
647 ;; the numeric prefix argument is 1 if the user gave no prefix argument at
648 ;; all. the raw prefix argument is not always a number. so we need to do
649 ;; our own conversion.
650 (defun ebib-prefix (num)
654 (defun ebib-called-with-prefix ()
655 "Returns T if the command was called with a prefix key."
656 (if (featurep 'xemacs
)
657 (member (character-to-event ebib-prefix-key
) (append (this-command-keys) nil
))
658 (member (event-convert-list (list ebib-prefix-key
))
659 (append (this-command-keys-vector) nil
))))
661 (defmacro ebib-export-to-db
(num message copy-fn
)
662 "Exports data to another database.
663 NUM is the number of the database to which the data is to be copied.
665 MESSAGE is a string displayed in the echo area if the export was
666 succesful. It must contain a %d directive, which is used to
667 display the database number to which the entry was exported.
669 COPY-FN is the function that actually copies the relevant
670 data. It must take as argument the database to which the data is
671 to be copied. COPY-FN must return T if the copying was
672 successful, and NIL otherwise."
673 `(let ((goal-db (nth (1- ,num
) ebib-databases
)))
676 (error "Database %d does not exist" ,num
))
677 ((edb-virtual goal-db
)
678 (error "Database %d is virtual" ,num
))
679 (t (when (funcall ,copy-fn goal-db
)
680 (ebib-set-modified t goal-db
)
681 (message ,message
,num
))))))
683 (defmacro ebib-export-to-file
(prompt-string message insert-fn
)
684 "Exports data to a file.
685 PROMPT-STRING is the string that is used to ask for the filename
686 to export to. INSERT-FN must insert the data to be exported into
687 the current buffer: it is called within a WITH-TEMP-BUFFER, whose
688 contents is appended to the file the user enters.
690 MESSAGE is shown in the echo area when the export was
691 successful. It must contain a %s directive, which is used to
692 display the actual filename."
693 `(let ((insert-default-directory (not ebib-export-filename
)))
694 (if-str (filename (read-file-name
695 ,prompt-string
"~/" nil nil ebib-export-filename
))
698 (append-to-file (point-min) (point-max) filename
)
699 (setq ebib-export-filename filename
)))))
701 (defun ebib-get-obl-fields (entry-type)
702 "Returns the obligatory fields of ENTRY-TYPE."
703 (car (gethash entry-type ebib-entry-types-hash
)))
705 (defun ebib-get-opt-fields (entry-type)
706 "Returns the optional fields of ENTRY-TYPE."
707 (cadr (gethash entry-type ebib-entry-types-hash
)))
709 (defun ebib-get-all-fields (entry-type)
710 "Returns all the fields of ENTRY-TYPE."
711 (cons 'type
* (append (ebib-get-obl-fields entry-type
)
712 (ebib-get-opt-fields entry-type
)
713 ebib-additional-fields
)))
715 (defun ebib-erase-buffer (buffer)
717 (with-buffer-writable
720 (defun ebib-make-highlight (begin end buffer
)
722 (if (featurep 'xemacs
)
724 (setq highlight
(make-extent begin end buffer
))
725 (set-extent-face highlight
'highlight
))
727 (setq highlight
(make-overlay begin end buffer
))
728 (overlay-put highlight
'face
'highlight
)))
731 (defun ebib-move-highlight (highlight begin end buffer
)
732 (if (featurep 'xemacs
)
733 (set-extent-endpoints highlight begin end buffer
)
734 (move-overlay highlight begin end buffer
)))
736 (defun ebib-highlight-start (highlight)
737 (if (featurep 'xemacs
)
738 (extent-start-position highlight
)
739 (overlay-start highlight
)))
741 (defun ebib-highlight-end (highlight)
742 (if (featurep 'xemacs
)
743 (extent-end-position highlight
)
744 (overlay-end highlight
)))
746 (defun ebib-delete-highlight (highlight)
747 (if (featurep 'xemacs
)
748 (detach-extent highlight
)
749 (delete-overlay highlight
)))
751 (defun ebib-set-index-highlight ()
752 (set-buffer ebib-index-buffer
)
755 (if ebib-index-display-fields
757 (skip-chars-forward "^ "))
758 (ebib-move-highlight ebib-index-highlight beg
(point) ebib-index-buffer
)))
760 (defun ebib-set-fields-highlight ()
761 (set-buffer ebib-entry-buffer
)
764 (looking-at-goto-end "[^ \t\n\f]*")
765 (ebib-move-highlight ebib-fields-highlight beg
(point) ebib-entry-buffer
)))
767 (defun ebib-set-strings-highlight ()
768 (set-buffer ebib-strings-buffer
)
771 (looking-at-goto-end "[^ \t\n\f]*")
772 (ebib-move-highlight ebib-strings-highlight beg
(point) ebib-strings-buffer
)))
774 (defun ebib-display-entry (entry-key)
775 "Displays ENTRY-KEY in the index buffer at POINT."
776 (set-buffer ebib-index-buffer
)
777 (insert (format "%-30s %s\n"
779 (if ebib-index-display-fields
780 (let ((cur-entry-hash (ebib-retrieve-entry entry-key ebib-cur-db
)))
781 (mapconcat #'(lambda (field)
783 (to-raw (gethash field cur-entry-hash
))
785 ebib-index-display-fields
789 (defun ebib-redisplay-current-field ()
790 "Redisplays the contents of the current field in the entry buffer."
791 (set-buffer ebib-entry-buffer
)
792 (if (eq ebib-current-field
'crossref
)
793 (ebib-fill-entry-buffer)
794 (with-buffer-writable
795 (goto-char (ebib-highlight-start ebib-fields-highlight
))
798 (delete-region beg
(point)))
799 (insert (format "%-17s " (symbol-name ebib-current-field
))
800 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash
))
801 (ebib-set-fields-highlight))))
803 (defun ebib-redisplay-current-string ()
804 "Redisplays the current string definition in the strings buffer."
805 (set-buffer ebib-strings-buffer
)
806 (with-buffer-writable
807 (let ((str (to-raw (gethash ebib-current-string
(edb-strings ebib-cur-db
)))))
808 (goto-char (ebib-highlight-start ebib-strings-highlight
))
811 (delete-region beg
(point)))
812 (insert (format "%-18s %s" ebib-current-string
813 (if (multiline-p str
)
814 (concat "+" (first-line str
))
816 (ebib-set-strings-highlight))))
818 (defun ebib-move-to-field (field direction
)
819 "Moves the fields overlay to the line containing FIELD.
820 If DIRECTION is positive, searches forward, if DIRECTION is
821 negative, searches backward. If DIRECTION is 1 or -1, searches
822 from POINT, if DIRECTION is 2 or -2, searches from beginning or
823 end of buffer. If FIELD is not found in the entry buffer, the
824 overlay is not moved. FIELD must be a symbol."
826 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
828 (set-buffer ebib-entry-buffer
)
829 (if (eq field
'type
*)
830 (goto-char (point-min))
831 (multiple-value-bind (fn start limit
) (if (>= direction
0)
832 (values 're-search-forward
(point-min) (point-max))
833 (values 're-search-backward
(point-max) (point-min)))
834 ;; make sure we can get back to our original position, if the field
835 ;; cannot be found in the buffer:
836 (let ((current-pos (point)))
837 (when (evenp direction
)
839 (unless (funcall fn
(concat "^" (symbol-name field
)) limit t
)
840 (goto-char current-pos
)))))
841 (ebib-set-fields-highlight))
843 (defun ebib-create-collection (hashtable)
844 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
845 The keys of HASHTABLE must be either symbols or strings."
847 (maphash #'(lambda (x y
)
848 (setq result
(cons (cons (symbol-or-string x
)
854 (defmacro ebib-retrieve-entry
(entry-key db
)
855 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
856 `(gethash ,entry-key
(edb-database ,db
)))
858 (defun ebib-get-field-highlighted (field current-entry
&optional match-str
)
859 ;; note: we need to work on a copy of the string, otherwise the highlights
860 ;; are made to the string as stored in the database. hence copy-sequence.
861 (let ((case-fold-search t
)
862 (string (copy-sequence (gethash field current-entry
)))
866 ;; we have to do a couple of things now:
867 ;; - remove {} or "" around the string, if they're there
868 ;; - search for match-str
869 ;; - properly adjust the string if it's multiline
870 ;; but all this is not necessary if there was no string
872 (let* ((xref (to-raw (gethash 'crossref current-entry
)))
873 (xref-entry (ebib-retrieve-entry xref ebib-cur-db
)))
875 (setq string
(gethash field xref-entry
))
877 (setq string
(propertize (to-raw string
) 'face
'ebib-crossref-face
'fontified t
))
881 (setq string
(to-raw string
))) ; we have to make the string look nice
883 (multiple-value-setq (string matched
) (match-all match-str string
)))
884 (when (multiline-p string
)
885 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
886 ;; multiline is local and therefore the object it refers to should
887 ;; be GC'ed when the function returns. but for some reason, the
888 ;; plus sign is persistent, and if it's been highlighted as the
889 ;; result of a search, it stays that way.
890 (setq multiline
(propertize "+" 'face nil
))
891 (setq string
(first-line string
))))
893 (string= multiline
"+"))
894 (add-text-properties 0 1 '(face highlight
) multiline
))
895 (concat raw multiline string
)))
897 (defun ebib-format-fields (entry fn
&optional match-str
)
898 (let* ((entry-type (gethash 'type
* entry
))
899 (obl-fields (ebib-get-obl-fields entry-type
))
900 (opt-fields (ebib-get-opt-fields entry-type
)))
901 (funcall fn
(format "%-19s %s\n" "type" entry-type
))
902 (mapc #'(lambda (fields)
904 (mapcar #'(lambda (field)
905 (unless (and (get field
'ebib-hidden
)
906 ebib-hide-hidden-fields
)
907 (funcall fn
(format "%-17s " field
))
909 (ebib-get-field-highlighted field entry match-str
)
913 (list obl-fields opt-fields ebib-additional-fields
))))
915 (defun ebib-fill-entry-buffer (&optional match-str
)
916 "Fills the entry buffer with the fields of the current entry.
917 MATCH-STRING is a regexp that will be highlighted when it occurs in the
919 (set-buffer ebib-entry-buffer
)
920 (with-buffer-writable
922 (when (and ebib-cur-db
; do we have a database?
923 (edb-keys-list ebib-cur-db
) ; does it contain entries?
924 (gethash (car (edb-cur-entry ebib-cur-db
))
925 (edb-database ebib-cur-db
))) ; does the current entry exist?
926 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db
))
927 (edb-database ebib-cur-db
)) 'insert match-str
)
928 (setq ebib-current-field
'type
*)
929 (goto-char (point-min))
930 (ebib-set-fields-highlight))))
932 (defun ebib-set-modified (mod &optional db
)
933 "Sets the modified flag of the database DB to MOD.
934 If DB is nil, it defaults to the current database, and the
935 modified flag of the index buffer is also (re)set. MOD must be
938 (setq db ebib-cur-db
))
939 (setf (edb-modified db
) mod
)
940 (when (eq db ebib-cur-db
)
942 (set-buffer ebib-index-buffer
)
943 (set-buffer-modified-p mod
))))
945 (defun ebib-modified-p ()
946 "Checks if any of the databases in Ebib were modified.
947 Returns the first modified database, or NIL if none was modified."
948 (let ((db (car ebib-databases
)))
950 (not (edb-modified db
)))
951 (setq db
(next-elem db ebib-databases
)))
954 (defun ebib-create-new-database (&optional db
)
955 "Creates a new database instance and returns it.
956 If DB is set to a database, the new database is a copy of DB."
961 (setq ebib-databases
(append ebib-databases
(list new-db
)))
964 (defun ebib-match-paren-forward (limit)
965 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
966 This function handles parentheses () and braces {}. Does not
967 search/move beyond LIMIT. Returns T if a matching parenthesis was
968 found, NIL otherwise. If point was not at an opening parenthesis
969 at all, NIL is returned and point is not moved. If point was at
970 an opening parenthesis but no matching closing parenthesis was
971 found, an error is logged and point is moved one character
972 forward to allow parsing to continue."
974 ((eq (char-after) ?\
{)
975 (ebib-match-brace-forward limit
))
976 ((eq (char-after) ?\
()
977 ;; we wrap this in a condition-case because we need to log the error
978 ;; message outside of the save-restriction, otherwise we get the wrong
982 (narrow-to-region (point) limit
)
983 ;; this is really a hack. we want to allow unbalanced parentheses in
984 ;; field values (bibtex does), so we cannot use forward-list
985 ;; here. for the same reason, looking for the matching paren by hand
986 ;; is pretty complicated. however, balanced parentheses can only be
987 ;; used to enclose entire entries (or @STRINGs or @PREAMBLEs) so we
988 ;; can be pretty sure we'll find it right before the next @ at the
989 ;; start of a line, or right before the end of the file.
990 (re-search-forward "^@" nil
0)
991 (skip-chars-backward "@ \n\t\f")
993 (if (eq (char-after) ?\
))
995 (goto-char (1+ (point-min)))
997 (error (ebib-log 'error
"Error in line %d: Matching closing parenthesis not found!" (line-number-at-pos))
1001 (defun ebib-match-delim-forward (limit)
1002 "Moves forward to the closing delimiter matching the opening delimiter at POINT.
1003 This function handles braces {} and double quotes \"\". Does not
1004 search/move beyond LIMIT. Returns T if a matching delimiter was
1005 found, NIL otherwise. If point was not at an opening delimiter at
1006 all, NIL is returned and point is not moved. If point was at an
1007 opening delimiter but no matching closing delimiter was found, an
1008 error is logged and point is moved one character forward to allow
1009 parsing to continue."
1011 ((eq (char-after) ?
\")
1012 (ebib-match-quote-forward limit
))
1013 ((eq (char-after) ?\
{)
1014 (ebib-match-brace-forward limit
))
1017 (defun ebib-match-brace-forward (limit)
1018 "Moves forward to the closing brace matching the opening brace at POINT.
1019 Does not search/move beyond LIMIT. Returns T if a matching brace
1020 was found, NIL otherwise. If point was not at an opening brace at
1021 all, NIL is returned and point is not moved. If point was at an
1022 opening brace but no matching closing brace was found, an error
1023 is logged and point is moved one character forward to allow
1024 parsing to continue."
1025 (when (eq (char-after) ?\
{) ; make sure we're really on a brace, otherwise return nil
1028 (narrow-to-region (point) limit
)
1031 ;; all of ebib expects that point moves to the closing
1032 ;; parenthesis, not right after it, so we adjust.
1034 t
)) ; return t because a matching brace was found
1036 (ebib-log 'error
"Error in line %d: Matching closing brace not found!" (line-number-at-pos))
1040 (defun ebib-match-quote-forward (limit)
1041 "Moves to the closing double quote matching the quote at POINT.
1042 Does not search/move beyond LIMIT. Returns T if a matching quote
1043 was found, NIL otherwise. If point was not at a double quote at
1044 all, NIL is returned and point is not moved. If point was at a
1045 quote but no matching closing quote was found, an error is logged
1046 and point is moved one character forward to allow parsing to
1048 (when (eq (char-after (point)) ?
\") ; make sure we're on a double quote.
1051 (narrow-to-region (point) limit
)
1053 (forward-char) ; move forward because we're on a double quote
1054 (skip-chars-forward "^\"") ; search the next double quote
1055 (eq (char-before) ?
\\))) ; if it's preceded by a backslash, keep on searching
1056 (or (eq (char-after) ?
\")
1058 (goto-char (1+ (point-min)))
1060 (error (ebib-log 'error
"Error in line %d: Matching closing quote not found!" (line-number-at-pos))
1063 (defun ebib-insert-entry (entry-key fields db
&optional sort timestamp
)
1064 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
1065 Optional argument SORT indicates whether the KEYS-LIST must be
1066 sorted after insertion. Default is NIL. Optional argument
1067 TIMESTAMP indicates whether a timestamp is to be added to the
1068 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
1069 must also be set to T."
1070 (when (and timestamp ebib-use-timestamp
)
1071 (puthash 'timestamp
(from-raw (format-time-string ebib-timestamp-format
)) fields
))
1072 (puthash entry-key fields
(edb-database db
))
1073 (ebib-set-modified t db
)
1074 (setf (edb-n-entries db
) (1+ (edb-n-entries db
)))
1075 (setf (edb-keys-list db
)
1077 (sort (cons entry-key
(edb-keys-list db
)) 'string
<)
1078 (cons entry-key
(edb-keys-list db
)))))
1080 (defun ebib-insert-string (abbr string db
&optional sort
)
1081 "Stores the @STRING definition defined by ABBR and STRING into DB.
1082 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
1083 after insertion. When loading or merging a file, for example, it is more
1084 economic to sort KEYS-LIST manually after all entries in the file have been
1086 (puthash abbr
(from-raw string
) (edb-strings db
))
1087 (ebib-set-modified t db
)
1088 (setf (edb-strings-list db
)
1090 (sort (cons abbr
(edb-strings-list db
)) 'string
<)
1091 (cons abbr
(edb-strings-list db
)))))
1093 (defmacro ebib-cur-entry-key
()
1094 "Returns the key of the current entry in EBIB-CUR-DB."
1095 `(car (edb-cur-entry ebib-cur-db
)))
1097 (defun ebib-search-key-in-buffer (entry-key)
1098 "Searches ENTRY-KEY in the index buffer.
1100 Moves point to the first character of the key and returns point."
1101 (goto-char (point-min))
1102 (search-forward entry-key
)
1106 ;; when we sort entries, we either use string< on the entry keys, or
1107 ;; ebib-entry<, if the user has defined a sort order.
1109 (defun ebib-entry< (x y
)
1110 "Returns T if entry X is smaller than entry Y.
1111 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1112 and Y should be keys of entries in the current database."
1113 (let* ((sort-list ebib-sort-order
)
1114 (sortstring-x (to-raw (ebib-get-sortstring x
(car sort-list
))))
1115 (sortstring-y (to-raw (ebib-get-sortstring y
(car sort-list
)))))
1116 (while (and sort-list
1117 (string= sortstring-x sortstring-y
))
1118 (setq sort-list
(cdr sort-list
))
1119 (setq sortstring-x
(to-raw (ebib-get-sortstring x
(car sort-list
))))
1120 (setq sortstring-y
(to-raw (ebib-get-sortstring y
(car sort-list
)))))
1121 (if (and sortstring-x sortstring-y
)
1122 (string< sortstring-x sortstring-y
)
1125 (defun ebib-get-sortstring (entry-key sortkey-list
)
1126 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1127 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1128 is a list of fields that are considered in order for the sort value."
1129 (let ((sort-string nil
))
1130 (while (and sortkey-list
1131 (null (setq sort-string
(gethash (car sortkey-list
)
1132 (ebib-retrieve-entry entry-key ebib-cur-db
)))))
1133 (setq sortkey-list
(cdr sortkey-list
)))
1136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1137 ;; main program execution ;;
1138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1142 "Ebib, a BibTeX database manager."
1144 (if (or (equal (window-buffer) ebib-index-buffer
)
1145 (equal (window-buffer) ebib-entry-buffer
))
1146 (error "Ebib already active")
1147 ;; we save the buffer from which ebib is called
1148 (setq ebib-push-buffer
(current-buffer))
1149 (unless ebib-initialized
1151 (if ebib-preload-bib-files
1152 (mapc #'(lambda (file)
1153 (ebib-load-bibtex-file file
))
1154 ebib-preload-bib-files
)))
1155 ;; we save the current window configuration.
1156 (setq ebib-saved-window-config
(current-window-configuration))
1157 ;; create the window configuration we want for ebib.
1158 (delete-other-windows)
1159 (switch-to-buffer ebib-index-buffer
)
1160 (let* ((keys-window (selected-window))
1161 (entry-window (split-window keys-window ebib-index-window-size
)))
1162 (set-window-buffer entry-window ebib-entry-buffer
))))
1166 This function sets all variables to their initial values, creates the
1167 buffers and reads the rc file."
1168 (setq ebib-cur-entry-hash nil
1169 ebib-current-field nil
1170 ebib-minibuf-hist nil
1171 ebib-saved-window-config nil
)
1172 (put 'timestamp
'ebib-hidden t
)
1173 (load "~/.ebibrc" t
)
1174 (ebib-create-buffers)
1175 (setq ebib-index-highlight
(ebib-make-highlight 1 1 ebib-index-buffer
))
1176 (setq ebib-fields-highlight
(ebib-make-highlight 1 1 ebib-entry-buffer
))
1177 (setq ebib-strings-highlight
(ebib-make-highlight 1 1 ebib-strings-buffer
))
1178 (setq ebib-initialized t
))
1180 (defun ebib-create-buffers ()
1181 "Creates the buffers for Ebib."
1182 ;; first we create a buffer for multiline editing. this one does *not*
1183 ;; have a name beginning with a space, because undo-info is normally
1184 ;; present in an edit buffer.
1185 (setq ebib-multiline-buffer
(get-buffer-create "*Ebib-edit*"))
1186 (set-buffer ebib-multiline-buffer
)
1187 (ebib-multiline-edit-mode)
1188 ;; then we create a buffer to hold the fields of the current entry.
1189 (setq ebib-entry-buffer
(get-buffer-create " *Ebib-entry*"))
1190 (set-buffer ebib-entry-buffer
)
1192 ;; then we create a buffer to hold the @STRING definitions
1193 (setq ebib-strings-buffer
(get-buffer-create " *Ebib-strings*"))
1194 (set-buffer ebib-strings-buffer
)
1196 ;; then we create the help buffer
1197 (setq ebib-help-buffer
(get-buffer-create " *Ebib-help*"))
1198 (set-buffer ebib-help-buffer
)
1201 (setq ebib-log-buffer
(get-buffer-create " *Ebib-log*"))
1202 (set-buffer ebib-log-buffer
)
1204 (insert "Ebib log messages\n\n(Press C-v or SPACE to scroll down, M-v or `b' to scroll up, `q' to quit.)\n\n\n")
1206 ;; and lastly we create a buffer for the entry keys.
1207 (setq ebib-index-buffer
(get-buffer-create " none"))
1208 (set-buffer ebib-index-buffer
)
1213 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1215 (when (if (ebib-modified-p)
1216 (yes-or-no-p "There are modified databases. Quit anyway? ")
1217 (y-or-n-p "Quit Ebib? "))
1218 (kill-buffer ebib-entry-buffer
)
1219 (kill-buffer ebib-index-buffer
)
1220 (kill-buffer ebib-strings-buffer
)
1221 (kill-buffer ebib-multiline-buffer
)
1222 (kill-buffer ebib-help-buffer
)
1223 (kill-buffer ebib-log-buffer
)
1224 (setq ebib-databases nil
1225 ebib-index-buffer nil
1226 ebib-entry-buffer nil
1227 ebib-initialized nil
1228 ebib-index-highlight nil
1229 ebib-fields-highlight nil
1230 ebib-strings-highlight nil
1231 ebib-export-filename nil
)
1232 (set-window-configuration ebib-saved-window-config
)
1235 (defun ebib-kill-emacs-query-function ()
1236 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1237 killed and the database has been modified."
1238 (if (not (ebib-modified-p))
1240 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1242 (ebib-save-all-databases)
1244 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1246 (add-hook 'kill-emacs-query-functions
'ebib-kill-emacs-query-function
)
1253 (define-prefix-command 'ebib-prefix-map
)
1254 (suppress-keymap ebib-prefix-map
)
1255 (defvar ebib-prefixed-functions
'(ebib-delete-entry
1260 ebib-push-entry-key
)))
1262 ;; macro to redefine key bindings.
1264 (defmacro ebib-key
(buffer key
&optional command
)
1267 (let ((one `(define-key ebib-index-mode-map
,key
(quote ,command
)))
1268 (two (when (or (null command
)
1269 (member command ebib-prefixed-functions
))
1270 `(define-key ebib-prefix-map
,key
(quote ,command
)))))
1275 `(define-key ebib-entry-mode-map
,key
(quote ,command
)))
1276 ((eq buffer
'strings
)
1277 `(define-key ebib-strings-mode-map
,key
(quote ,command
)))
1278 ((eq buffer
'mark-prefix
)
1280 (define-key ebib-index-mode-map
(format "%c" ebib-prefix-key
) nil
)
1281 (define-key ebib-index-mode-map
,key
'ebib-prefix-map
)
1282 (setq ebib-prefix-key
(string-to-char ,key
))))))
1284 (defvar ebib-index-mode-map
1285 (let ((map (make-keymap)))
1286 (suppress-keymap map
)
1288 "Keymap for the ebib index buffer.")
1290 ;; we define the keys with ebib-key rather than with define-key, because
1291 ;; that automatically sets up ebib-prefix-map as well.
1292 (ebib-key index
[up] ebib-prev-entry)
1293 (ebib-key index [down] ebib-next-entry)
1294 (ebib-key index [right] ebib-next-database)
1295 (ebib-key index [left] ebib-prev-database)
1296 (ebib-key index [prior] ebib-index-scroll-down)
1297 (ebib-key index [next] ebib-index-scroll-up)
1298 (ebib-key index [home] ebib-goto-first-entry)
1299 (ebib-key index [end] ebib-goto-last-entry)
1300 (ebib-key index [return] ebib-select-entry)
1301 (ebib-key index " " ebib-index-scroll-up)
1302 (ebib-key index "/" ebib-search)
1303 (ebib-key index "&" ebib-virtual-db-and)
1304 (ebib-key index "|" ebib-virtual-db-or)
1305 (ebib-key index "~" ebib-virtual-db-not)
1306 (ebib-key index ";" ebib-prefix-map)
1307 (ebib-key index "a" ebib-add-entry)
1308 (ebib-key index "b" ebib-index-scroll-down)
1309 (ebib-key index "c" ebib-close-database)
1310 (ebib-key index "C" ebib-customize)
1311 (ebib-key index "d" ebib-delete-entry)
1312 (ebib-key index "e" ebib-edit-entry)
1313 (ebib-key index "E" ebib-edit-keyname)
1314 (ebib-key index "f" ebib-view-file)
1315 (ebib-key index "F" ebib-follow-crossref)
1316 (ebib-key index "g" ebib-goto-first-entry)
1317 (ebib-key index "G" ebib-goto-last-entry)
1318 (ebib-key index "h" ebib-index-help)
1319 (ebib-key index "H" ebib-toggle-hidden)
1320 (ebib-key index "j" ebib-next-entry)
1321 (ebib-key index "J" ebib-switch-to-database)
1322 (ebib-key index "k" ebib-prev-entry)
1323 (ebib-key index "l" ebib-show-log)
1324 (ebib-key index "L" ebib-latex-entries)
1325 (ebib-key index "m" ebib-mark-entry)
1326 (ebib-key index "M" ebib-merge-bibtex-file)
1327 (ebib-key index "n" ebib-search-next)
1328 (ebib-key index [(control n)] ebib-next-entry)
1329 (ebib-key index [(meta n)] ebib-index-scroll-up)
1330 (ebib-key index "o" ebib-load-bibtex-file)
1331 (ebib-key index "p" ebib-push-entry-key)
1332 (ebib-key index [(control p)] ebib-prev-entry)
1333 (ebib-key index [(meta p)] ebib-index-scroll-down)
1334 (ebib-key index "P" ebib-print-entries)
1335 (ebib-key index "r" ebib-edit-preamble)
1336 (ebib-key index "q" ebib-quit)
1337 (ebib-key index "s" ebib-save-current-database)
1338 (ebib-key index "S" ebib-save-all-databases)
1339 (ebib-key index "t" ebib-edit-strings)
1340 (ebib-key index "u" ebib-browse-url)
1341 (ebib-key index "V" ebib-print-filter)
1342 (ebib-key index "w" ebib-write-database)
1343 (ebib-key index "x" ebib-export-entry)
1344 (ebib-key index "\C-xb" ebib-lower)
1345 (ebib-key index "\C-xk" ebib-quit)
1346 (ebib-key index "X" ebib-export-preamble)
1347 (ebib-key index "z" ebib-lower)
1349 (defun ebib-switch-to-database-nth (key)
1350 (interactive (list (if (featurep 'xemacs)
1351 (event-key last-command-event)
1352 last-command-event)))
1353 (ebib-switch-to-database (- (if (featurep 'xemacs)
1357 (mapc #'(lambda (key)
1358 (define-key ebib-index-mode-map (format "%d" key)
1359 'ebib-switch-to-database-nth))
1360 '(1 2 3 4 5 6 7 8 9))
1362 (define-derived-mode ebib-index-mode
1363 fundamental-mode "Ebib-index"
1364 "Major mode for the Ebib index buffer."
1365 (setq buffer-read-only t)
1366 (setq truncate-lines t))
1368 (defun ebib-fill-index-buffer ()
1369 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1370 If EBIB-CUR-DB is nil, the buffer is just erased and its name set
1372 (set-buffer ebib-index-buffer)
1373 (let ((buffer-read-only nil))
1377 ;; we may call this function when there are no entries in the
1378 ;; database. if so, we don't need to do this:
1379 (when (edb-cur-entry ebib-cur-db)
1380 (mapc #'(lambda (entry)
1381 (ebib-display-entry entry)
1382 (when (member entry (edb-marked-entries ebib-cur-db))
1387 (skip-chars-forward "^ ")
1388 (add-text-properties beg (point) '(face ebib-marked-face))))))
1389 (edb-keys-list ebib-cur-db))
1390 (goto-char (point-min))
1391 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
1393 (ebib-set-index-highlight))
1394 (set-buffer-modified-p (edb-modified ebib-cur-db))
1395 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1396 (length (member ebib-cur-db ebib-databases)))))
1397 (edb-name ebib-cur-db))))
1398 (rename-buffer " none"))))
1400 (defun ebib-customize ()
1401 "Switches to Ebib's customisation group."
1404 (customize-group 'ebib))
1406 (defun ebib-log (type format-string &rest args)
1407 "Writes a message to Ebib's log buffer.
1408 TYPE (a symbol) is the type of message. It can be LOG, which
1409 writes the message to the log buffer only; MESSAGE, which writes
1410 the message to the log buffer and outputs it with the function
1411 MESSAGE; WARNING, which logs the message and sets the variable
1412 EBIB-LOG-ERROR to 0; or ERROR, which logs the message and sets
1413 the variable EBIB-LOG-ERROR to 1. The latter two can be used to
1414 signal the user to check the log for warnings or errors."
1416 (set-buffer ebib-log-buffer)
1419 (or ebib-log-error ; if ebib-error-log is already set to 1, we don't want to overwrite it!
1420 (setq ebib-log-error 0)))
1422 (setq ebib-log-error 1))
1424 (apply 'message format-string args)))
1425 (insert (apply 'format (concat format-string "\n") args))))
1427 (defun ebib-load-bibtex-file (&optional file)
1428 "Loads a BibTeX file into ebib."
1431 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1432 (setq ebib-cur-db (ebib-create-new-database))
1433 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1434 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1435 (setq ebib-log-error nil) ; we haven't found any errors
1436 (ebib-log 'log "%s: Opening file %s" (format-time-string "%d %b %Y, %H:%M:%S") (edb-filename ebib-cur-db))
1437 ;; first, we empty the buffers
1438 (ebib-erase-buffer ebib-index-buffer)
1439 (ebib-erase-buffer ebib-entry-buffer)
1440 (if (file-readable-p file)
1441 ;; if the user entered the name of an existing file, we load it
1442 ;; by putting it in a buffer and then parsing it.
1444 (with-syntax-table ebib-syntax-table
1445 (insert-file-contents file)
1446 ;; if the user makes any changes, we'll want to create a back-up.
1447 (setf (edb-make-backup ebib-cur-db) t)
1448 (let ((result (ebib-find-bibtex-entries nil)))
1449 (setf (edb-n-entries ebib-cur-db) (car result))
1450 (when (edb-keys-list ebib-cur-db)
1451 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1452 (when (edb-strings-list ebib-cur-db)
1453 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1454 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1455 ;; and fill the buffers. note that filling a buffer also makes
1456 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1458 (ebib-set-modified nil)
1459 (ebib-fill-entry-buffer)
1460 ;; and now we tell the user the result
1461 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1467 ;; if the file does not exist, we need to issue a message.
1468 (ebib-log 'message "(New file)"))
1469 ;; what we have to do in *any* case, is fill the index buffer. (this
1470 ;; even works if there are no keys in the database, e.g. when the
1471 ;; user opened a new file or if no BibTeX entries were found.
1472 (ebib-fill-index-buffer)
1473 (when ebib-log-error
1474 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1475 (ebib-log 'log "\n\f\n"))
1477 (defun ebib-merge-bibtex-file ()
1478 "Merges a BibTeX file into the database."
1480 (unless (edb-virtual ebib-cur-db)
1481 (if (not ebib-cur-db)
1482 (error "No database loaded. Use `o' to open a database")
1483 (let ((file (read-file-name "File to merge: ")))
1484 (setq ebib-log-error nil) ; we haven't found any errors
1485 (ebib-log 'log "%s: Merging file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") (edb-filename ebib-cur-db))
1487 (with-syntax-table ebib-syntax-table
1488 (insert-file-contents file)
1489 (let ((n (ebib-find-bibtex-entries t)))
1490 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1491 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1492 (when (edb-strings-list ebib-cur-db)
1493 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1494 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1495 (ebib-fill-entry-buffer)
1496 (ebib-fill-index-buffer)
1497 (ebib-set-modified t)
1498 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1504 (when ebib-log-error
1505 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1506 (ebib-log 'log "\n\f\n"))))))))
1508 (defun ebib-find-bibtex-entries (timestamp)
1509 "Finds the BibTeX entries in the current buffer.
1510 The search is started at the beginnig of the buffer. All entries
1511 found are stored in the hash table DATABASE of
1512 EBIB-CUR-DB. Returns a three-element list: the first element is
1513 the number of entries found, the second the number of @STRING
1514 definitions, and the third is T or NIL, indicating whether a
1515 @PREAMBLE was found.
1517 TIMESTAMP indicates whether a timestamp is to be added to each
1518 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1523 (goto-char (point-min))
1524 (while (re-search-forward "^@" nil t) ; find the next entry
1525 (let ((beg (point)))
1526 (when (looking-at-goto-end (concat ebib-bibtex-identifier "[\(\{]"))
1527 (let ((entry-type (downcase (buffer-substring-no-properties beg (1- (point))))))
1529 ((equal entry-type "string") ; string and preamble must be treated differently
1530 (if (ebib-read-string)
1531 (setq n-strings (1+ n-strings))))
1532 ((equal entry-type "preamble")
1533 (when (ebib-read-preamble)
1535 ((equal entry-type "comment") ; ignore comments
1536 (ebib-log 'log "Comment at line %d ignored" (line-number-at-pos))
1537 (ebib-match-paren-forward (point-max)))
1538 ((gethash (intern-soft entry-type) ebib-entry-types-hash) ; if the entry type has been defined
1539 (if (ebib-read-entry entry-type timestamp)
1540 (setq n-entries (1+ n-entries))))
1541 ;; anything else we report as an unknown entry type.
1542 (t (ebib-log 'warning "Line %d: Unknown entry type `%s'. Skipping." (line-number-at-pos) entry-type)
1543 (ebib-match-paren-forward (point-max))))))))
1544 (list n-entries n-strings preamble)))
1546 (defun ebib-read-string ()
1547 "Reads the @STRING definition beginning at the line POINT is on.
1548 If a proper abbreviation and string are found, they are stored in the
1549 database. Returns the string if one was read, nil otherwise."
1550 (let ((limit (save-excursion ; we find the matching end parenthesis
1552 (ebib-match-paren-forward (point-max))
1554 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1555 (let ((beg (point)))
1556 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1557 (if-str (abbr (buffer-substring-no-properties beg (point)))
1559 (skip-chars-forward "^\"{" limit)
1560 (let ((beg (point)))
1561 (if-str (string (if (ebib-match-delim-forward limit)
1562 (buffer-substring-no-properties beg (1+ (point)))
1564 (if (member abbr (edb-strings-list ebib-cur-db))
1565 (ebib-log 'warning (format "Line %d: @STRING definition `%s' duplicated. Skipping."
1566 (line-number-at-pos) abbr))
1567 (ebib-insert-string abbr string ebib-cur-db))))))))))
1569 (defun ebib-read-preamble ()
1570 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1571 If there was already another @PREAMBLE definition, the new one is
1572 added to the existing one with a hash sign `#' between them."
1573 (let ((beg (point)))
1575 (when (ebib-match-paren-forward (point-max))
1576 (let ((text (buffer-substring-no-properties beg (point))))
1577 (if (edb-preamble ebib-cur-db)
1578 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1579 (setf (edb-preamble ebib-cur-db) text))))))
1581 (defun ebib-read-entry (entry-type &optional timestamp)
1582 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1583 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1584 otherwise. Optional argument TIMESTAMP indicates whether a
1585 timestamp is to be added. (Whether a timestamp is actually added,
1586 also depends on EBIB-USE-TIMESTAMP.)"
1587 (let ((entry-limit (save-excursion
1589 (ebib-match-paren-forward (point-max))
1592 (skip-chars-forward " \n\t\f") ; note the space!
1594 (when (looking-at-goto-end (concat "\\("
1595 ebib-bibtex-identifier
1597 1) ; this delimits the entry key
1598 (let ((entry-key (buffer-substring-no-properties beg (point))))
1599 (if (member entry-key (edb-keys-list ebib-cur-db))
1600 (ebib-log 'warning "Line %d: Entry `%s' duplicated. Skipping." (line-number-at-pos) entry-key)
1601 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1602 (when fields ; if fields were found, we store them, and return T.
1603 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1606 (defun ebib-find-bibtex-fields (entry-type limit)
1607 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1608 Returns a hash table containing all the fields and values, or NIL
1609 if none were found. ENTRY-TYPE is the type of the entry, which
1610 will be recorded in the hash table. Before the search starts,
1611 POINT is moved back to the beginning of the line."
1613 ;; we assign a function to fn in order to avoid putting the test on
1614 ;; ebib-allow-identical-fields in the while loop, where it would get
1615 ;; tested with every field being read.
1616 (let ((fn (if (not ebib-allow-identical-fields)
1617 (symbol-function 'puthash)
1618 #'(lambda (field-type field-contents fields)
1619 (let ((existing-contents (gethash field-type fields)))
1620 (puthash field-type (if existing-contents
1621 (from-raw (concat (to-raw existing-contents)
1623 (to-raw field-contents)))
1626 (let ((fields (make-hash-table :size 15)))
1628 (skip-chars-forward "^," limit) ; we must move to the next comma,
1629 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1630 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1631 (let ((beg (point)))
1632 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1633 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1634 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1635 (let ((field-contents (ebib-get-field-contents limit)))
1636 (when field-contents
1637 (funcall fn field-type field-contents fields))))))))
1638 (when (> (hash-table-count fields) 0)
1639 (puthash 'type* entry-type fields)
1642 (defun ebib-get-field-contents (limit)
1643 "Gets the contents of a BibTeX field.
1644 LIMIT indicates the end of the entry, beyond which the function will not
1646 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1647 (let ((beg (point)))
1648 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1650 (defun ebib-find-end-of-field (limit)
1651 "Moves POINT to the end of a field's contents and returns POINT.
1652 The contents of a field is delimited by a comma or by the closing brace of
1653 the entry. The latter is at position LIMIT."
1654 (while (and (not (eq (char-after) ?\,))
1656 (ebib-match-delim-forward limit) ; check if we're on a delimiter and if so, jump to the matching closing delimiter
1658 (if (= (point) limit)
1659 (skip-chars-backward " \n\t\f"))
1662 (defun ebib-lower ()
1663 "Hides the Ebib buffers, but does not delete them."
1665 (if (nor (equal (window-buffer) ebib-index-buffer)
1666 (equal (window-buffer) ebib-entry-buffer)
1667 (equal (window-buffer) ebib-strings-buffer)
1668 (equal (window-buffer) ebib-multiline-buffer)
1669 (equal (window-buffer) ebib-help-buffer)
1670 (equal (window-buffer) ebib-log-buffer))
1671 (error "Ebib is not active ")
1672 (set-window-configuration ebib-saved-window-config)
1673 (bury-buffer ebib-entry-buffer)
1674 (bury-buffer ebib-index-buffer)
1675 (bury-buffer ebib-multiline-buffer)
1676 (bury-buffer ebib-strings-buffer)
1677 (bury-buffer ebib-help-buffer)
1678 (bury-buffer ebib-log-buffer)))
1680 (defun ebib-prev-entry ()
1681 "Moves to the previous BibTeX entry."
1685 ;; if the current entry is the first entry,
1686 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1688 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1689 (1+ (length (edb-cur-entry ebib-cur-db)))))
1690 (goto-char (ebib-highlight-start ebib-index-highlight))
1692 (ebib-set-index-highlight)
1693 (ebib-fill-entry-buffer)))
1697 (defun ebib-next-entry ()
1698 "Moves to the next BibTeX entry."
1702 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1704 (setf (edb-cur-entry ebib-cur-db)
1705 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1706 (goto-char (ebib-highlight-start ebib-index-highlight))
1708 (ebib-set-index-highlight)
1709 (ebib-fill-entry-buffer)))
1713 (defun ebib-add-entry ()
1714 "Adds a new entry to the database."
1718 (if-str (entry-key (read-string "New entry key: "))
1720 (if (member entry-key (edb-keys-list ebib-cur-db))
1721 (error "Key already exists")
1722 (set-buffer ebib-index-buffer)
1723 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1724 (with-buffer-writable
1725 (ebib-display-entry entry-key))
1726 (forward-line -1) ; move one line up to position the cursor on the new entry.
1727 (ebib-set-index-highlight)
1728 (let ((fields (make-hash-table)))
1729 (puthash 'type* ebib-default-type fields)
1730 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1731 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1732 (ebib-fill-entry-buffer)
1734 (ebib-set-modified t)))))
1736 (error "No database open. Use `o' to open a database first"))
1740 (defun ebib-close-database ()
1741 "Closes the current BibTeX database."
1745 (when (if (edb-modified ebib-cur-db)
1746 (yes-or-no-p "Database modified. Close it anyway? ")
1747 (y-or-n-p "Close database? "))
1748 (let ((to-be-deleted ebib-cur-db)
1749 (new-db (next-elem ebib-cur-db ebib-databases)))
1750 (setq ebib-databases (delete to-be-deleted ebib-databases))
1751 (if ebib-databases ; do we still have another database loaded?
1753 (setq ebib-cur-db (or new-db
1754 (last1 ebib-databases)))
1755 (unless (edb-cur-entry ebib-cur-db)
1756 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1757 (ebib-fill-entry-buffer)
1758 (ebib-fill-index-buffer))
1759 ;; otherwise, we have to clean up a little and empty all the buffers.
1760 (setq ebib-cur-db nil)
1761 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1762 (set-buffer (car buf))
1763 (with-buffer-writable
1765 (ebib-delete-highlight (cadr buf)))
1766 (list (list ebib-entry-buffer ebib-fields-highlight)
1767 (list ebib-index-buffer ebib-index-highlight)
1768 (list ebib-strings-buffer ebib-strings-highlight)))
1769 ;; multiline edit buffer
1770 (set-buffer ebib-multiline-buffer)
1771 (with-buffer-writable
1773 (set-buffer ebib-index-buffer)
1774 (rename-buffer " none"))
1775 (message "Database closed."))))))
1777 (defun ebib-goto-first-entry ()
1778 "Moves to the first BibTeX entry in the database."
1782 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1783 (set-buffer ebib-index-buffer)
1784 (goto-char (point-min))
1785 (ebib-set-index-highlight)
1786 (ebib-fill-entry-buffer))
1790 (defun ebib-goto-last-entry ()
1791 "Moves to the last entry in the BibTeX database."
1795 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
1796 (set-buffer ebib-index-buffer)
1797 (goto-line (edb-n-entries ebib-cur-db))
1798 (ebib-set-index-highlight)
1799 (ebib-fill-entry-buffer))
1803 (defun ebib-edit-entry ()
1804 "Edits the current BibTeX entry."
1808 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1809 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
1811 (switch-to-buffer ebib-entry-buffer)
1812 (goto-char (ebib-highlight-end ebib-fields-highlight)))
1816 (defun ebib-edit-keyname ()
1817 "Change the key of a BibTeX entry."
1821 (let ((cur-keyname (ebib-cur-entry-key)))
1822 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
1824 (if (member new-keyname (edb-keys-list ebib-cur-db))
1825 (error (format "Key `%s' already exists" new-keyname))
1826 (unless (string= cur-keyname new-keyname)
1827 (let ((fields (ebib-retrieve-entry cur-keyname ebib-cur-db))
1828 (marked (member cur-keyname (edb-marked-entries ebib-cur-db))))
1829 (ebib-remove-entry-from-db cur-keyname ebib-cur-db)
1830 (ebib-remove-key-from-buffer cur-keyname)
1831 (ebib-insert-entry new-keyname fields ebib-cur-db t nil)
1832 (setf (edb-cur-entry ebib-cur-db) (member new-keyname (edb-keys-list ebib-cur-db)))
1833 (sort-in-buffer (edb-n-entries ebib-cur-db) new-keyname)
1834 (with-buffer-writable
1835 (ebib-display-entry new-keyname))
1836 (forward-line -1) ; move one line up to position the cursor on the new entry.
1837 (ebib-set-index-highlight)
1838 (ebib-set-modified t)
1839 (when marked (ebib-mark-entry))))))))
1843 (defun ebib-mark-entry ()
1844 "Mark (or unmark) the current entry."
1846 (if (ebib-called-with-prefix)
1849 (setf (edb-marked-entries ebib-cur-db) nil)
1850 (ebib-fill-index-buffer))
1852 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
1853 (ebib-fill-index-buffer))
1858 (set-buffer ebib-index-buffer)
1859 (with-buffer-writable
1860 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
1862 (setf (edb-marked-entries ebib-cur-db)
1863 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
1864 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
1865 (ebib-highlight-end ebib-index-highlight)
1866 '(face ebib-marked-face)))
1867 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
1868 (edb-marked-entries ebib-cur-db))
1870 (add-text-properties (ebib-highlight-start ebib-index-highlight)
1871 (ebib-highlight-end ebib-index-highlight)
1872 '(face ebib-marked-face)))))
1876 (defun ebib-index-scroll-down ()
1877 "Move one page up in the database."
1882 (ebib-select-entry))
1886 (defun ebib-index-scroll-up ()
1887 "Move one page down in the database."
1892 (ebib-select-entry))
1896 (defun ebib-format-entry (key db timestamp)
1897 "Format entry KEY from database DB into the current buffer in BibTeX format.
1898 If TIMESTAMP is T, a timestamp is added to the entry if
1899 EBIB-USE-TIMESTAMP is T."
1900 (let ((entry (ebib-retrieve-entry key db)))
1902 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
1903 (maphash #'(lambda (key value)
1904 (unless (or (eq key 'type*)
1905 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
1906 (insert (format "\t%s = %s,\n" key value))))
1908 (if (and timestamp ebib-use-timestamp)
1909 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
1910 (delete-char -2)) ; the final ",\n" must be deleted
1911 (insert "\n}\n\n"))))
1913 (defun ebib-format-strings (db)
1914 "Format the @STRING commands in database DB."
1915 (maphash #'(lambda (key value)
1916 (insert (format "@STRING{%s = %s}\n" key value)))
1920 (defun ebib-compare-xrefs (x y)
1921 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
1923 (defun ebib-format-database (db)
1924 "Writes database DB into the current buffer in BibTeX format."
1925 (when (edb-preamble db)
1926 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
1927 (ebib-format-strings db)
1928 (let ((sorted-list (copy-list (edb-keys-list db))))
1930 (ebib-save-xrefs-first
1931 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
1933 (setq sorted-list (sort sorted-list 'ebib-entry<))))
1934 (mapc #'(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
1936 (defun ebib-save-database (db)
1937 "Saves the database DB."
1940 (when (and (edb-make-backup db)
1941 (file-exists-p (edb-filename db)))
1942 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
1943 (setf (edb-make-backup db) nil))
1945 (ebib-format-database db)
1946 (write-region (point-min) (point-max) (edb-filename db)))
1947 (ebib-set-modified nil db))))
1949 (defun ebib-write-database ()
1950 "Writes the current database to a different file.
1951 Can also be used to change a virtual database into a real one."
1955 (if-str (new-filename (read-file-name "Save to file: " "~/"))
1958 (ebib-format-database ebib-cur-db)
1959 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
1960 ;; if SAFE-WRITE-REGION was cancelled by the user because he
1961 ;; didn't want to overwrite an already existing file with his
1962 ;; new database, it throws an error, so the next lines will not
1963 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
1965 (setf (edb-filename ebib-cur-db) new-filename)
1966 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
1967 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1968 (length (member ebib-cur-db ebib-databases)))))
1969 (edb-name ebib-cur-db)))
1972 (setf (edb-virtual ebib-cur-db) nil)
1973 (setf (edb-database ebib-cur-db)
1974 (let ((new-db (make-hash-table :test 'equal)))
1975 (mapc #'(lambda (key)
1976 (let ((entry (gethash key (edb-database ebib-cur-db))))
1978 (puthash key (copy-hash-table entry) new-db))))
1979 (edb-keys-list ebib-cur-db))
1981 (ebib-set-modified nil))))
1985 (defun ebib-save-current-database ()
1986 "Saves the current database."
1990 (if (not (edb-modified ebib-cur-db))
1991 (message "No changes need to be saved.")
1992 (ebib-save-database ebib-cur-db)))
1994 (error "Cannot save a virtual database. Use `w' to write to a file."))))
1996 (defun ebib-save-all-databases ()
1997 "Saves all currently open databases if they were modified."
2001 (mapc #'(lambda (db)
2002 (when (edb-modified db)
2003 (ebib-save-database db)))
2005 (message "All databases saved."))))
2007 (defun ebib-print-filename ()
2008 "Displays the filename of the current database in the minibuffer."
2010 (message (edb-filename ebib-cur-db)))
2012 (defun ebib-follow-crossref ()
2013 "Goes to the entry mentioned in the crossref field of the current entry."
2015 (let ((new-cur-entry (to-raw (gethash 'crossref
2016 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2017 (setf (edb-cur-entry ebib-cur-db)
2018 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
2019 (edb-cur-entry ebib-cur-db))))
2020 (ebib-fill-entry-buffer)
2021 (ebib-fill-index-buffer))
2023 (defun ebib-toggle-hidden ()
2025 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
2026 (ebib-fill-entry-buffer))
2028 (defun ebib-delete-entry ()
2029 "Deletes the current entry from the database."
2031 (if (ebib-called-with-prefix)
2033 ((real-db marked-entries)
2034 (when (y-or-n-p "Delete all marked entries? ")
2035 (mapc #'(lambda (entry)
2036 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
2037 (edb-marked-entries ebib-cur-db))
2038 (message "Marked entries deleted.")
2039 (ebib-set-modified t)
2040 (ebib-fill-entry-buffer)
2041 (ebib-fill-index-buffer)))
2046 (let ((cur-entry (ebib-cur-entry-key)))
2047 (when (y-or-n-p (format "Delete %s? " cur-entry))
2048 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
2049 (ebib-remove-key-from-buffer cur-entry)
2050 (ebib-fill-entry-buffer)
2051 (ebib-set-modified t)
2052 (message (format "Entry `%s' deleted." cur-entry)))))
2056 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
2057 "Removes ENTRY-KEY from DB.
2058 Optional argument NEW-CUR-ENTRY is the key of the entry that is
2059 to become the new current entry. It it is NIL, the entry after
2060 the deleted one becomes the new current entry. If it is T, the
2061 current entry is not changed."
2062 (remhash entry-key (edb-database db))
2063 (setf (edb-n-entries db) (1- (edb-n-entries db)))
2065 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
2066 ((stringp new-cur-entry) t)
2067 (t (setq new-cur-entry (ebib-cur-entry-key))))
2068 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
2069 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
2070 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
2071 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
2072 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
2074 (defun ebib-remove-key-from-buffer (entry-key)
2075 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
2076 (with-buffer-writable
2077 (let ((beg (ebib-search-key-in-buffer entry-key)))
2079 (delete-region beg (point))))
2082 (ebib-search-key-in-buffer (ebib-cur-entry-key))
2083 (ebib-set-index-highlight))))
2085 (defun ebib-select-entry ()
2086 "Makes the entry at (point) the current entry."
2091 (let ((beg (point)))
2092 (let* ((key (save-excursion
2093 (skip-chars-forward "^ ")
2094 (buffer-substring-no-properties beg (point))))
2095 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
2097 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
2098 (ebib-set-index-highlight)
2099 (ebib-fill-entry-buffer)))))
2103 (defun ebib-export-entry (prefix)
2104 "Copies entries to another database.
2105 The prefix argument indicates which database to copy the entry
2106 to. If no prefix argument is present, a filename is asked to
2107 which the entry is appended."
2109 (let ((num (ebib-prefix prefix)))
2110 (if (ebib-called-with-prefix)
2111 (ebib-export-marked-entries num)
2112 (ebib-export-single-entry num))))
2114 (defun ebib-export-single-entry (num)
2115 "Copies the current entry to another database.
2116 NUM indicates which database to copy the entry to. If it is NIL,
2117 a filename is asked to which the entry is appended."
2121 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
2123 (let ((entry-key (ebib-cur-entry-key)))
2124 (if (member entry-key (edb-keys-list db))
2125 (error "Entry key `%s' already exists in database %d" entry-key num)
2126 (ebib-insert-entry entry-key
2127 (copy-hash-table (ebib-retrieve-entry entry-key
2130 ;; if this is the first entry in the target DB,
2131 ;; its CUR-ENTRY must be set!
2132 (when (null (edb-cur-entry db))
2133 (setf (edb-cur-entry db) (edb-keys-list db)))
2134 t)))) ; we must return T, WHEN does not always do this.
2135 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2136 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2139 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2143 (defun ebib-export-marked-entries (num)
2144 "Copies the marked entries to another database.
2145 NUM indicates which database to copy the entry to. If it is NIL,
2146 a filename is asked to which the entry is appended."
2148 ((real-db marked-entries)
2151 num "Entries copied to database %d."
2153 (mapc #'(lambda (entry-key)
2154 (if (member entry-key (edb-keys-list db))
2155 (error "Entry key `%s' already exists in database %d" entry-key num)
2156 (ebib-insert-entry entry-key
2157 (copy-hash-table (ebib-retrieve-entry entry-key
2160 (edb-marked-entries ebib-cur-db))
2161 ;; if the target DB was empty before, its CUR-ENTRY must be set!
2162 (when (null (edb-cur-entry db))
2163 (setf (edb-cur-entry db) (edb-keys-list db)))
2164 t)) ; we must return T, WHEN does not always do this.
2165 (ebib-export-to-file "Export to file: " "Entries exported to %s."
2167 (mapc #'(lambda (entry-key)
2169 (ebib-format-entry entry-key ebib-cur-db t))
2170 (edb-marked-entries ebib-cur-db))))))
2174 (defun ebib-search ()
2175 "Search the current Ebib database.
2176 The search is conducted with STRING-MATCH and can therefore be a
2177 regexp. Searching starts with the current entry."
2181 (if-str (search-str (read-string "Search database for: "))
2183 (setq ebib-search-string search-str)
2184 ;; first we search the current entry
2185 (if (ebib-search-in-entry ebib-search-string
2186 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2187 (ebib-fill-entry-buffer ebib-search-string)
2188 ;; if the search string wasn't found in the current entry, we continue searching.
2189 (ebib-search-next)))))
2193 (defun ebib-search-next ()
2194 "Search the next occurrence of EBIB-SEARCH-STRING.
2195 Searching starts at the entry following the current entry. If a
2196 match is found, the matching entry is shown and becomes the new
2201 (if (null ebib-search-string)
2202 (message "No search string")
2203 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2204 (while (and cur-search-entry
2205 (null (ebib-search-in-entry ebib-search-string
2206 (gethash (car cur-search-entry)
2207 (edb-database ebib-cur-db)))))
2208 (setq cur-search-entry (cdr cur-search-entry)))
2209 (if (null cur-search-entry)
2210 (message (format "`%s' not found" ebib-search-string))
2211 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2212 (set-buffer ebib-index-buffer)
2213 (goto-char (point-min))
2214 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
2216 (ebib-set-index-highlight)
2217 (ebib-fill-entry-buffer ebib-search-string)))))
2221 (defun ebib-search-in-entry (search-str entry &optional field)
2222 "Searches one entry of the ebib database.
2223 Returns a list of fields in ENTRY that match the regexp
2224 SEARCH-STR, or NIL if no matches were found. If FIELD is given,
2225 only that field is searched."
2226 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2229 (let ((value (gethash field entry)))
2230 (when (and (stringp value) ; the type* field has a symbol as value
2231 (string-match search-str value))
2232 (setq result (list field))))
2233 (maphash #'(lambda (field value)
2234 (when (and (stringp value) ; the type* field has a symbol as value
2235 (string-match search-str value))
2236 (setq result (cons field result))))
2240 (defun ebib-edit-strings ()
2241 "Edits the @STRING definitions in the database."
2245 (ebib-fill-strings-buffer)
2247 (switch-to-buffer ebib-strings-buffer)
2248 (goto-char (point-min)))
2252 (defun ebib-edit-preamble ()
2253 "Edits the @PREAMBLE definition in the database."
2257 (other-window 1) ; we want the multiline edit buffer to appear in the lower window
2258 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2262 (defun ebib-export-preamble (prefix)
2263 "Export the @PREAMBLE definition.
2264 If a prefix argument is given, it is taken as the database to
2265 export the preamble to. If the goal database already has a
2266 preamble, the new preamble will be appended to it. If no prefix
2267 argument is given, the user is asked to enter a filename to which
2268 the preamble is appended."
2272 (if (null (edb-preamble ebib-cur-db))
2273 (error "No @PREAMBLE defined")
2274 (let ((num (ebib-prefix prefix)))
2276 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2278 (let ((text (edb-preamble ebib-cur-db)))
2279 (if (edb-preamble db)
2280 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2281 (setf (edb-preamble db) text)))))
2282 (ebib-export-to-file "Export @PREAMBLE to file: "
2283 "@PREAMBLE exported to %s"
2285 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2289 (defun ebib-print-entries ()
2290 "Creates a LaTeX file listing the entries.
2291 Either prints the entire database, or the marked entries."
2295 (let ((entries (if (ebib-called-with-prefix)
2296 (edb-marked-entries ebib-cur-db)
2297 (edb-keys-list ebib-cur-db))))
2298 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2300 (read-file-name "Use temp file: " "~/" nil nil)))
2303 (insert "\\documentclass{article}\n\n")
2304 (when ebib-print-preamble
2305 (mapc #'(lambda (string)
2306 (insert (format "%s\n" string)))
2307 ebib-print-preamble))
2308 (insert "\n\\begin{document}\n\n")
2309 (mapc #'(lambda (entry-key)
2310 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2311 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2312 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2313 entry-key (symbol-name (gethash 'type* entry))))
2314 (insert "\\hline\n")
2315 (mapc #'(lambda (field)
2316 (if-str (value (gethash field entry))
2317 (when (or (not (multiline-p value))
2318 ebib-print-multiline)
2319 (insert (format "%s: & %s\\\\\n"
2320 field (to-raw value))))))
2321 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2322 (insert "\\end{tabular}\n\n")
2323 (insert "\\bigskip\n\n"))
2325 (insert "\\end{document}\n")
2326 (write-region (point-min) (point-max) tempfile))
2328 (find-file tempfile)))))
2332 (defun ebib-latex-entries ()
2333 "Creates a LaTeX file that \\nocite's entries from the database.
2334 Operates either on all entries or on the marked entries."
2338 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2340 (read-file-name "Use temp file: " "~/" nil nil)))
2343 (insert "\\documentclass{article}\n\n")
2344 (when ebib-print-preamble
2345 (mapc #'(lambda (string)
2346 (insert (format "%s\n" string)))
2347 ebib-latex-preamble))
2348 (insert "\n\\begin{document}\n\n")
2349 (if (ebib-called-with-prefix)
2350 (mapc #'(lambda (entry)
2351 (insert (format "\\nocite{%s}\n" entry)))
2352 (edb-marked-entries ebib-cur-db))
2353 (insert "\\nocite{*}\n"))
2354 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2355 (insert "\\end{document}\n")
2356 (write-region (point-min) (point-max) tempfile))
2358 (find-file tempfile))))
2362 (defun ebib-switch-to-database (num)
2363 (interactive "NSwitch to database number: ")
2364 (let ((new-db (nth (1- num) ebib-databases)))
2367 (setq ebib-cur-db new-db)
2368 (ebib-fill-entry-buffer)
2369 (ebib-fill-index-buffer))
2370 (error "Database %d does not exist" num))))
2372 (defun ebib-next-database ()
2376 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2378 (setq new-db (car ebib-databases)))
2379 (setq ebib-cur-db new-db)
2380 (ebib-fill-entry-buffer)
2381 (ebib-fill-index-buffer)))))
2383 (defun ebib-prev-database ()
2387 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2389 (setq new-db (last1 ebib-databases)))
2390 (setq ebib-cur-db new-db)
2391 (ebib-fill-entry-buffer)
2392 (ebib-fill-index-buffer)))))
2394 (defun ebib-browse-url (num)
2395 "Ask a browser to load the URL in the standard URL field.
2396 The standard URL field may contain more than one URL, if they're
2397 whitespace-separated. In that case, a numeric prefix argument
2398 specifies which URL to choose.
2400 By \"standard URL field\" is meant the field defined in the
2401 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2406 (let ((url (to-raw (gethash ebib-standard-url-field
2407 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2409 (ebib-call-browser url num)))
2410 (error "No url found in field `%s'" ebib-standard-url-field))))
2414 (defun ebib-call-browser (urls n)
2415 "Passes the Nth url in URLS to a browser.
2416 URLs must be a string of whitespace-separated urls."
2417 (let ((url (nth (1- n)
2420 (while (string-match ebib-url-regexp urls start)
2421 (add-to-list 'result (match-string 0 urls) t)
2422 (setq start (match-end 0)))
2425 ((string-match "\\\\url{\\(.*?\\)}" url)
2426 (setq url (match-string 1 url)))
2427 ((string-match ebib-url-regexp url)
2428 (setq url (match-string 0 url)))
2431 (if (not (string= ebib-browser-command ""))
2432 (start-process "Ebib-browser" nil ebib-browser-command url)
2433 (browse-url url)))))
2435 (defun ebib-view-file (num)
2436 "View a file in the standard file field.
2437 The standard file field may contain more than one filename, if
2438 they're whitespace-separated. In that case, a numeric prefix
2439 argument specifies which file to choose.
2441 By \"standard file field\" is meant the field defined in the
2442 customisation variable EBIB-STANDARD-FILE-FIELD. Its default
2447 (let ((filename (to-raw (gethash ebib-standard-file-field
2448 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2449 (when (not (and filename
2450 (ebib-call-file-viewer filename num)))
2451 (error "No valid filename found in field `%s'" ebib-standard-file-field))))
2455 (defun ebib-call-file-viewer (files n)
2456 "Passes the Nth file in FILES to an external viewer.
2457 FILESs must be a string of whitespace-separated filenames."
2458 (let* ((file (nth (1- n)
2461 (while (string-match ebib-file-regexp files start)
2462 (add-to-list 'result (match-string 0 files) t)
2463 (setq start (match-end 0)))
2465 (ext (last1 (split-string file "\\."))))
2466 (let ((file-full-path (locate-file file ebib-file-search-dirs)))
2467 (when file-full-path
2468 (if-str (viewer (cdr (assoc ext ebib-file-associations)))
2469 (start-process (concat "ebib " ext " viewer process") nil viewer file-full-path)
2471 (find-file file-full-path))))))
2473 (defun ebib-virtual-db-and (not)
2474 "Filter entries into a virtual database.
2475 If the current database is a virtual database already, perform a
2476 logical AND on the entries."
2480 (ebib-filter-to-virtual-db 'and not))
2484 (defun ebib-virtual-db-or (not)
2485 "Filter entries into a virtual database.
2486 If the current database is a virtual database already, perform a
2487 logical OR with the entries in the original database."
2491 (ebib-filter-to-virtual-db 'or not))
2495 (defun ebib-virtual-db-not ()
2496 "Negates the current virtual database."
2500 (setf (edb-virtual ebib-cur-db)
2501 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2502 (cadr (edb-virtual ebib-cur-db))
2503 `(not ,(edb-virtual ebib-cur-db))))
2504 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2505 (ebib-fill-entry-buffer)
2506 (ebib-fill-index-buffer))
2510 (defun ebib-filter-to-virtual-db (bool not)
2511 "Filters the current database to a virtual database.
2512 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2513 a logical `not' is applied to the selection."
2514 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2515 (if (< not 0) "(not " "")
2516 (if (< not 0) ")" ""))
2518 (mapcar #'(lambda (x)
2519 (cons (symbol-name x) 0))
2520 (append ebib-unique-field-list ebib-additional-fields)))
2522 (setq field (intern-soft field))
2523 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2524 (if (< not 0) "(not " "")
2526 (if (< not 0) ")" "")))))
2529 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2531 `(contains ,field ,regexp)
2532 `(not (contains ,field ,regexp))))))
2534 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2535 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2536 (ebib-fill-entry-buffer)
2537 (ebib-fill-index-buffer))))
2539 (defun ebib-create-virtual-db (field regexp not)
2540 "Creates a virtual database based on EBIB-CUR-DB."
2541 ;; a virtual database is a database whose edb-virtual field contains an
2542 ;; expression that selects entries. this function only sets that
2543 ;; expression, it does not actually filter the entries.
2544 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2545 (setf (edb-virtual new-db) (if (>= not 0)
2546 `(contains ,field ,regexp)
2547 `(not (contains ,field ,regexp))))
2548 (setf (edb-filename new-db) nil)
2549 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2550 (setf (edb-modified new-db) nil)
2551 (setf (edb-make-backup new-db) nil)
2554 (defmacro contains (field regexp)
2555 ;; This is a hack: CONTAINS depends on the variable ENTRY being set to an
2556 ;; actual Ebib entry for its operation. The point of this macro is to
2557 ;; facilitate defining filters for virtual databases. It enables us to
2558 ;; define filters of the form:
2560 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2562 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2564 (defun ebib-run-filter (filter db)
2566 (setf (edb-keys-list db)
2567 (sort (let ((result nil))
2568 (maphash #'(lambda (key value)
2569 (let ((entry value)) ; this is necessary for actually running the filter
2571 (setq result (cons key result)))))
2575 (setf (edb-n-entries db) (length (edb-keys-list db)))
2576 (setf (edb-cur-entry db) (edb-keys-list db)))
2578 (defun ebib-print-filter (num)
2579 "Display the filter of the current virtual database.
2580 With any prefix argument, reapplies the filter to the
2581 database. This can be useful when the source database was
2587 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2588 (ebib-fill-entry-buffer)
2589 (ebib-fill-index-buffer))
2590 (message "%S" (edb-virtual ebib-cur-db)))
2594 (defun ebib-index-help ()
2595 "Displays the help message for the index buffer."
2598 (ebib-display-help ebib-index-buffer))
2600 (defun ebib-show-log ()
2601 "Display the contents of the log buffer."
2604 (switch-to-buffer ebib-log-buffer))
2606 (defun ebib-push-entry-key (prefix)
2607 "Pushes the current entry to a LaTeX buffer.
2608 The user is prompted for the buffer to push the entry into."
2610 (let ((called-with-prefix (ebib-called-with-prefix)))
2613 (let ((buffer (read-buffer "Push entry(ies) to buffer: " ebib-push-buffer t)))
2615 (setq ebib-push-buffer buffer)
2616 (let ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
2618 (latex-arg (if called-with-prefix
2619 (when (edb-marked-entries ebib-cur-db)
2620 (mapconcat #'(lambda (x) x)
2621 (edb-marked-entries ebib-cur-db)
2623 (car (edb-cur-entry ebib-cur-db)))))
2626 (insert (format latex-cmd latex-arg)))
2627 (message "Pushed entries to buffer %s" buffer)))))
2635 (defvar ebib-entry-mode-map
2636 (let ((map (make-keymap)))
2637 (suppress-keymap map)
2638 (define-key map [up] 'ebib-prev-field
)
2639 (define-key map
[down] 'ebib-next-field)
2640 (define-key map [prior] 'ebib-goto-prev-set)
2641 (define-key map [next] 'ebib-goto-next-set)
2642 (define-key map [home] 'ebib-goto-first-field)
2643 (define-key map [end] 'ebib-goto-last-field)
2644 (define-key map " " 'ebib-goto-next-set)
2645 (define-key map "b" 'ebib-goto-prev-set)
2646 (define-key map "c" 'ebib-copy-field-contents)
2647 (define-key map "d" 'ebib-delete-field-contents)
2648 (define-key map "e" 'ebib-edit-field)
2649 (define-key map "f" 'ebib-view-file-in-field)
2650 (define-key map "g" 'ebib-goto-first-field)
2651 (define-key map "G" 'ebib-goto-last-field)
2652 (define-key map "h" 'ebib-entry-help)
2653 (define-key map "j" 'ebib-next-field)
2654 (define-key map "k" 'ebib-prev-field)
2655 (define-key map "l" 'ebib-edit-multiline-field)
2656 (define-key map [(control n)] 'ebib-next-field)
2657 (define-key map [(meta n)] 'ebib-goto-prev-set)
2658 (define-key map [(control p)] 'ebib-prev-field)
2659 (define-key map [(meta p)] 'ebib-goto-next-set)
2660 (define-key map "q" 'ebib-quit-entry-buffer)
2661 (define-key map "r" 'ebib-toggle-raw)
2662 (define-key map "s" 'ebib-insert-abbreviation)
2663 (define-key map "u" 'ebib-browse-url-in-field)
2664 (define-key map "x" 'ebib-cut-field-contents)
2665 (define-key map "\C-xb" 'undefined)
2666 (define-key map "\C-xk" 'undefined)
2667 (define-key map "y" 'ebib-yank-field-contents)
2669 "Keymap for the Ebib entry buffer.")
2671 (define-derived-mode ebib-entry-mode
2672 fundamental-mode "Ebib-entry"
2673 "Major mode for the Ebib entry buffer."
2674 (setq buffer-read-only t)
2675 (setq truncate-lines t))
2677 (defun ebib-quit-entry-buffer ()
2678 "Quit editing the entry."
2682 (defun ebib-find-visible-field (field direction)
2683 "Finds the first visible field before or after FIELD.
2684 If DIRECTION is negative, search the preceding fields, otherwise
2685 search the succeeding fields. If FIELD is visible itself, return
2686 that. If there is no preceding/following visible field, return
2687 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
2688 (when ebib-hide-hidden-fields
2689 (let ((fn (if (>= direction 0)
2693 (get field 'ebib-hidden))
2694 (setq field (funcall fn field ebib-cur-entry-fields)))))
2697 (defun ebib-prev-field ()
2698 "Move to the previous field."
2700 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
2701 (if (null new-field)
2703 (setq ebib-current-field new-field)
2704 (ebib-move-to-field ebib-current-field -1))))
2706 (defun ebib-next-field ()
2707 "Move to the next field."
2709 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
2710 (if (null new-field)
2711 (when (interactive-p) ; i call this function after editing a field,
2712 ; and we don't want a beep then
2714 (setq ebib-current-field new-field)
2715 (ebib-move-to-field ebib-current-field 1))))
2717 (defun ebib-goto-first-field ()
2718 "Move to the first field."
2720 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
2721 (if (null new-field)
2723 (setq ebib-current-field new-field)
2724 (ebib-move-to-field ebib-current-field -1))))
2726 (defun ebib-goto-last-field ()
2727 "Move to the last field."
2729 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
2730 (if (null new-field)
2732 (setq ebib-current-field new-field)
2733 (ebib-move-to-field ebib-current-field 1))))
2735 (defun ebib-goto-next-set ()
2736 "Move to the next set of fields."
2739 ((eq ebib-current-field 'type*) (ebib-next-field))
2740 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
2741 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2742 (obl-fields (ebib-get-obl-fields entry-type))
2743 (opt-fields (ebib-get-opt-fields entry-type))
2745 (when (member ebib-current-field obl-fields)
2746 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
2747 ;; new-field is nil if there are no opt-fields
2748 (when (or (member ebib-current-field opt-fields)
2750 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
2751 (if (null new-field)
2752 (ebib-goto-last-field) ; if there was no further set to go to,
2753 ; go to the last field of the current set
2754 (setq ebib-current-field new-field)
2755 (ebib-move-to-field ebib-current-field 1))))))
2757 (defun ebib-goto-prev-set ()
2758 "Move to the previous set of fields."
2760 (unless (eq ebib-current-field 'type*)
2761 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2762 (obl-fields (ebib-get-obl-fields entry-type))
2763 (opt-fields (ebib-get-opt-fields entry-type))
2765 (if (member ebib-current-field obl-fields)
2766 (ebib-goto-first-field)
2767 (when (member ebib-current-field ebib-additional-fields)
2768 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
2769 (when (or (member ebib-current-field opt-fields)
2771 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
2772 (if (null new-field)
2773 (ebib-goto-first-field)
2774 (setq ebib-current-field new-field)
2775 (ebib-move-to-field ebib-current-field -1))))))
2777 (defun ebib-edit-entry-type ()
2778 "Edits the type of an entry."
2779 ;; we want to put the completion buffer in the lower window. for this
2780 ;; reason, we need to switch to the other window before calling
2781 ;; completing-read. but in order to make sure that we return to the
2782 ;; entry buffer and not the index buffer when the user presses C-g, we
2783 ;; need to do this in an unwind-protect.
2787 (let ((collection (ebib-create-collection ebib-entry-types-hash)))
2788 (if-str (new-type (completing-read "type: " collection nil t))
2790 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
2791 (ebib-fill-entry-buffer)
2792 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2793 (ebib-set-modified t)))))
2796 (defun ebib-edit-crossref ()
2797 "Edits the crossref field."
2801 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
2802 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
2804 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
2805 (ebib-set-modified t)))))
2807 ;; we now redisplay the entire entry buffer, so that the crossref'ed
2808 ;; fields show up. this also puts the cursor back on the type field.
2809 (ebib-fill-entry-buffer)))
2811 ;; we should modify ebib-edit-field, so that it calls the appropriate
2812 ;; helper function, which asks the user for the new value and just returns
2813 ;; that. storing it should then be done by ebib-edit-field, no matter what
2814 ;; sort of field the user edits.
2816 (defun ebib-edit-field ()
2817 "Edits a field of a BibTeX entry."
2820 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
2821 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
2822 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
2824 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
2826 (if (multiline-p init-contents)
2827 (ebib-edit-multiline-field)
2829 (if (raw-p init-contents)
2831 (setq init-contents (to-raw init-contents))))
2832 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
2834 (cons init-contents 0)
2837 (puthash ebib-current-field (if raw
2839 (concat "{" new-contents "}"))
2840 ebib-cur-entry-hash)
2841 (remhash ebib-current-field ebib-cur-entry-hash))
2842 (ebib-redisplay-current-field)
2843 ;; we move to the next field, but only if ebib-edit-field was
2844 ;; called interactively, otherwise we get a strange bug in
2845 ;; ebib-toggle-raw...
2846 (if (interactive-p) (ebib-next-field))
2847 (ebib-set-modified t))))))
2849 (defun ebib-browse-url-in-field (num)
2850 "Browse a url in the current field.
2851 The field may contain a whitespace-separated set of urls. The
2852 prefix argument indicates which url is to be sent to the
2855 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2857 (ebib-call-browser urls num)))
2858 (error "No url found in field `%s'" ebib-current-field))))
2860 (defun ebib-view-file-in-field (num)
2861 "View a file in the current field.
2862 The field may contain a whitespace-separated set of
2863 filenames. The prefix argument indicates which file is to be
2866 (let ((files (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2868 (ebib-call-file-viewer files num)))
2869 (error "No valid filename found in field `%s'" ebib-current-field))))
2871 (defun ebib-copy-field-contents ()
2872 "Copies the contents of the current field to the kill ring."
2874 (unless (eq ebib-current-field 'type*)
2875 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2876 (when (stringp contents)
2878 (message "Field contents copied.")))))
2880 (defun ebib-cut-field-contents ()
2881 "Kills the contents of the current field. The killed text is put in the kill ring."
2883 (unless (eq ebib-current-field 'type*)
2884 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2885 (when (stringp contents)
2886 (remhash ebib-current-field ebib-cur-entry-hash)
2888 (ebib-redisplay-current-field)
2889 (ebib-set-modified t)
2890 (message "Field contents killed.")))))
2892 (defun ebib-yank-field-contents (arg)
2893 "Inserts the last killed text into the current field.
2894 If the current field already has a contents, nothing is inserted,
2895 unless the previous command was also ebib-yank-field-contents,
2896 then the field contents is replaced with the previous yank. That
2897 is, multiple use of this command functions like the combination
2898 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
2900 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
2901 (eq ebib-current-field 'crossref)
2902 (unless (eq last-command 'ebib-yank-field-contents)
2903 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
2905 (setq this-command t)
2907 (let ((new-contents (current-kill (cond
2908 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
2914 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
2915 (ebib-redisplay-current-field)
2916 (ebib-set-modified t)))))
2918 (defun ebib-delete-field-contents ()
2919 "Deletes the contents of the current field.
2920 The deleted text is not put in the kill ring."
2922 (if (eq ebib-current-field 'type*)
2924 (remhash ebib-current-field ebib-cur-entry-hash)
2925 (ebib-redisplay-current-field)
2926 (ebib-set-modified t)
2927 (message "Field contents deleted.")))
2929 (defun ebib-toggle-raw ()
2930 "Toggles the raw status of the current field contents."
2932 (unless (or (eq ebib-current-field 'type*)
2933 (eq ebib-current-field 'crossref))
2934 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2935 (if (not contents) ; if there is no value,
2937 (ebib-edit-field) ; the user can enter one, which we must then make raw
2938 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
2940 ;; note: we don't have to check for empty string, since that is
2941 ;; already done in ebib-edit-field
2942 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
2943 (if (raw-p contents)
2944 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
2945 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
2946 (ebib-redisplay-current-field)
2947 (ebib-set-modified t))))
2949 (defun ebib-edit-multiline-field ()
2950 "Edits the current field in multiline-mode."
2952 (unless (or (eq ebib-current-field 'type*)
2953 (eq ebib-current-field 'crossref))
2954 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
2956 (setq ebib-multiline-raw t)
2957 (setq text (to-raw text))
2958 (setq ebib-multiline-raw nil))
2959 (ebib-multiline-edit 'fields text))))
2961 (defun ebib-insert-abbreviation ()
2962 "Insert an abbreviation from the ones defined in the database."
2964 (if (gethash ebib-current-field ebib-cur-entry-hash)
2966 (when (edb-strings-list ebib-cur-db)
2970 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
2971 (string (completing-read "Abbreviation to insert: " collection nil t)))
2973 (puthash ebib-current-field string ebib-cur-entry-hash)
2974 (ebib-set-modified t))))
2976 ;; we can't do this earlier, because we would be writing to the index buffer...
2977 (ebib-redisplay-current-field)
2978 (ebib-next-field)))))
2980 (defun ebib-entry-help ()
2981 "Displays the help message for the entry buffer."
2983 (ebib-display-help ebib-entry-buffer))
2989 (defvar ebib-strings-mode-map
2990 (let ((map (make-keymap)))
2991 (suppress-keymap map)
2992 (define-key map [up] 'ebib-prev-string)
2993 (define-key map [down] 'ebib-next-string
)
2994 (define-key map
[prior] 'ebib-strings-page-up)
2995 (define-key map [next] 'ebib-strings-page-down)
2996 (define-key map [home] 'ebib-goto-first-string)
2997 (define-key map [end] 'ebib-goto-last-string)
2998 (define-key map " " 'ebib-strings-page-down)
2999 (define-key map "a" 'ebib-add-string)
3000 (define-key map "b" 'ebib-strings-page-up)
3001 (define-key map "c" 'ebib-copy-string-contents)
3002 (define-key map "d" 'ebib-delete-string)
3003 (define-key map "e" 'ebib-edit-string)
3004 (define-key map "g" 'ebib-goto-first-string)
3005 (define-key map "G" 'ebib-goto-last-string)
3006 (define-key map "h" 'ebib-strings-help)
3007 (define-key map "j" 'ebib-next-string)
3008 (define-key map "k" 'ebib-prev-string)
3009 (define-key map "l" 'ebib-edit-multiline-string)
3010 (define-key map [(control n)] 'ebib-next-string)
3011 (define-key map [(meta n)] 'ebib-strings-page-down)
3012 (define-key map [(control p)] 'ebib-prev-string)
3013 (define-key map [(meta p)] 'ebib-strings-page-up)
3014 (define-key map "q" 'ebib-quit-strings-buffer)
3015 (define-key map "x" 'ebib-export-string)
3016 (define-key map "X" 'ebib-export-all-strings)
3017 (define-key map "\C-xb" 'disabled)
3018 (define-key map "\C-xk" 'disabled)
3020 "Keymap for the ebib strings buffer.")
3022 (define-derived-mode ebib-strings-mode
3023 fundamental-mode "Ebib-strings"
3024 "Major mode for the Ebib strings buffer."
3025 (setq buffer-read-only t)
3026 (setq truncate-lines t))
3028 (defun ebib-quit-strings-buffer ()
3029 "Quit editing the @STRING definitions."
3031 (switch-to-buffer ebib-entry-buffer)
3034 (defun ebib-prev-string ()
3035 "Move to the previous string."
3037 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
3039 ;; go to the beginnig of the highlight and move upward one line.
3040 (goto-char (ebib-highlight-start ebib-strings-highlight))
3042 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3043 (ebib-set-strings-highlight)))
3045 (defun ebib-next-string ()
3046 "Move to the next string."
3048 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3049 (when (interactive-p) (beep))
3050 (goto-char (ebib-highlight-start ebib-strings-highlight))
3052 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3053 (ebib-set-strings-highlight)))
3055 (defun ebib-goto-first-string ()
3056 "Move to the first string."
3058 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3059 (goto-char (point-min))
3060 (ebib-set-strings-highlight))
3062 (defun ebib-goto-last-string ()
3063 "Move to the last string."
3065 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3066 (goto-char (point-max))
3068 (ebib-set-strings-highlight))
3070 (defun ebib-strings-page-up ()
3071 "Moves 10 entries up in the database."
3073 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3074 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3075 (if (<= (- number-of-strings remaining-number-of-strings) 10)
3076 (ebib-goto-first-string)
3077 (setq ebib-current-string (nth
3078 (- number-of-strings remaining-number-of-strings 10)
3079 (edb-strings-list ebib-cur-db)))
3080 (goto-char (ebib-highlight-start ebib-strings-highlight))
3082 (ebib-set-strings-highlight)))
3083 (message ebib-current-string))
3085 (defun ebib-strings-page-down ()
3086 "Moves 10 entries down in the database."
3088 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3089 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3090 (if (<= remaining-number-of-strings 10)
3091 (ebib-goto-last-string)
3092 (setq ebib-current-string (nth
3093 (- number-of-strings remaining-number-of-strings -10)
3094 (edb-strings-list ebib-cur-db)))
3095 (goto-char (ebib-highlight-start ebib-strings-highlight))
3097 (ebib-set-strings-highlight)))
3098 (message ebib-current-string))
3100 (defun ebib-fill-strings-buffer ()
3101 "Fills the strings buffer with the @STRING definitions."
3102 (set-buffer ebib-strings-buffer)
3103 (with-buffer-writable
3105 (dolist (elem (edb-strings-list ebib-cur-db))
3106 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
3107 (insert (format "%-18s %s\n" elem
3108 (if (multiline-p str)
3109 (concat "+" (first-line str))
3110 (concat " " str)))))))
3111 (goto-char (point-min))
3112 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3113 (ebib-set-strings-highlight)
3114 (set-buffer-modified-p nil))
3116 (defun ebib-edit-string ()
3117 "Edits the value of an @STRING definition
3118 When the user enters an empty string, the value is not changed."
3120 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3121 (if (multiline-p init-contents)
3122 (ebib-edit-multiline-string)
3123 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
3125 (cons init-contents 0)
3129 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
3130 (ebib-redisplay-current-string)
3132 (ebib-set-modified t))
3133 (error "@STRING definition cannot be empty")))))
3135 (defun ebib-copy-string-contents ()
3136 "Copies the contents of the current string to the kill ring."
3138 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
3140 (message "String value copied.")))
3142 (defun ebib-delete-string ()
3143 "Deletes the current @STRING definition from the database."
3145 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
3146 (remhash ebib-current-string (edb-strings ebib-cur-db))
3147 (with-buffer-writable
3149 (goto-char (ebib-highlight-start ebib-strings-highlight))
3152 (delete-region beg (point))))
3153 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
3154 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
3155 (when (null new-cur-string) ; deleted the last string
3156 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
3158 (setq ebib-current-string new-cur-string))
3159 (ebib-set-strings-highlight)
3160 (ebib-set-modified t)
3161 (message "@STRING definition deleted.")))
3163 (defun ebib-add-string ()
3164 "Creates a new @STRING definition."
3166 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3168 (if (member new-abbr (edb-strings-list ebib-cur-db))
3169 (error (format "%s already exists" new-abbr)))
3170 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3172 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3173 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3174 (with-buffer-writable
3175 (insert (format "%-19s %s\n" new-abbr new-string)))
3177 (ebib-set-strings-highlight)
3178 (setq ebib-current-string new-abbr)
3179 (ebib-set-modified t))))))
3181 (defun ebib-export-string (prefix)
3182 "Exports the current @STRING.
3183 The prefix argument indicates which database to copy the string
3184 to. If no prefix argument is present, a filename is asked to
3185 which the string is appended."
3187 (let ((num (ebib-prefix prefix)))
3189 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3191 (let ((abbr ebib-current-string)
3192 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3193 (if (member abbr (edb-strings-list db))
3194 (error "@STRING definition already exists in database %d" num)
3195 (ebib-insert-string abbr string db t)))))
3196 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3197 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3199 (insert (format "\n@string{%s = %s}\n"
3201 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3203 (defun ebib-export-all-strings (prefix)
3204 "Export all @STRING definitions.
3205 If a prefix argument is given, it is taken as the database to
3206 copy the definitions to. Without prefix argument, asks for a file
3209 (when ebib-current-string ; there is always a current string, unless there are no strings
3210 (let ((num (ebib-prefix prefix)))
3213 num "All @STRING definitions copied to database %d"
3215 (mapc #'(lambda (abbr)
3216 (if (member abbr (edb-strings-list db))
3217 (message "@STRING definition `%s' already exists in database %d" abbr num)
3218 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3219 (edb-strings-list ebib-cur-db))))
3220 (ebib-export-to-file "Export all @STRING definitions to file: "
3221 "All @STRING definitions exported to %s"
3223 (insert (format "\n")) ; to keep things tidy.
3224 (ebib-format-strings ebib-cur-db)))))))
3226 (defun ebib-edit-multiline-string ()
3227 "Edits the current string in multiline-mode."
3229 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3231 (defun ebib-strings-help ()
3232 "Displays the help message for the strings buffer."
3234 (ebib-display-help ebib-strings-buffer))
3236 ;;;;;;;;;;;;;;;;;;;;;;;;;
3237 ;; multiline edit mode ;;
3238 ;;;;;;;;;;;;;;;;;;;;;;;;;
3240 (define-derived-mode ebib-multiline-edit-mode
3241 text-mode "Ebib-edit"
3242 "Major mode for editing multiline strings in Ebib."
3243 ;; we redefine some basic keys because we need them to leave this buffer.
3244 (local-set-key "\C-xb" 'ebib-leave-multiline-edit)
3245 (local-set-key "\C-x\C-s" 'ebib-save-from-multiline-edit)
3246 (local-set-key "\C-xk" 'ebib-cancel-multiline-edit))
3248 (defun ebib-multiline-edit (type &optional starttext)
3249 "Switches to Ebib's multiline edit buffer.
3250 STARTTEXT is a string that contains the initial text of the buffer."
3251 ;; note: the buffer is put in the currently active window!
3252 (switch-to-buffer ebib-multiline-buffer)
3254 (setq ebib-editing type)
3257 (goto-char (point-min))
3258 (set-buffer-modified-p nil)))
3260 (defun ebib-leave-multiline-edit ()
3261 "Quits the multiline edit buffer."
3263 (ebib-store-multiline-text)
3265 ((eq ebib-editing 'preamble)
3266 (switch-to-buffer ebib-entry-buffer)
3267 (other-window 1)) ; we have to switch back to the index buffer window
3268 ((eq ebib-editing 'fields)
3269 (switch-to-buffer ebib-entry-buffer)
3270 (ebib-redisplay-current-field)
3272 ((eq ebib-editing 'strings)
3273 (switch-to-buffer ebib-strings-buffer)
3274 (ebib-redisplay-current-string)
3275 (ebib-next-string)))
3276 (message "Text stored."))
3278 (defun ebib-save-from-multiline-edit ()
3279 "Stores the text being edited in the multiline edit buffer and then saves the database."
3281 (ebib-store-multiline-text)
3282 (ebib-save-database ebib-cur-db)
3283 (set-buffer-modified-p nil))
3285 (defun ebib-store-multiline-text ()
3286 "Stores the text being edited in the multiline edit buffer."
3287 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3289 ((eq ebib-editing 'preamble)
3291 (setf (edb-preamble ebib-cur-db) nil)
3292 (setf (edb-preamble ebib-cur-db) text)))
3293 ((eq ebib-editing 'fields)
3295 (remhash ebib-current-field ebib-cur-entry-hash)
3296 (when (not ebib-multiline-raw)
3297 (setq text (from-raw text)))
3298 (puthash ebib-current-field text ebib-cur-entry-hash)))
3299 ((eq ebib-editing 'strings)
3301 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3302 ;; MESSAGE, but we also do not switch back to the strings
3303 ;; buffer. this may not be so bad, actually, because the user
3304 ;; may want to change his edit.
3305 (error "@STRING definition cannot be empty ")
3306 (setq text (from-raw text)) ; strings cannot be raw
3307 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3308 (ebib-set-modified t))
3310 (defun ebib-cancel-multiline-edit ()
3311 "Quits the multiline edit buffer and discards the changes."
3314 (when (buffer-modified-p)
3315 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3316 (throw 'no-cancel nil)))
3318 ((eq ebib-editing 'fields)
3319 (switch-to-buffer ebib-entry-buffer)
3320 (ebib-redisplay-current-field)) ; we have to do this, because the
3321 ; user may have saved with C-x C-s
3323 ((eq ebib-editing 'strings)
3324 (switch-to-buffer ebib-strings-buffer)
3325 (ebib-redisplay-current-string))
3326 ((eq ebib-editing 'preamble)
3327 (switch-to-buffer ebib-entry-buffer)
3328 (other-window 1)))))
3330 ;;;;;;;;;;;;;;;;;;;;
3331 ;; ebib-help-mode ;;
3332 ;;;;;;;;;;;;;;;;;;;;
3334 (defvar ebib-help-mode-map
3335 (let ((map (make-keymap)))
3336 (suppress-keymap map)
3337 (define-key map " " 'scroll-up)
3338 (define-key map "b" 'scroll-down)
3339 (define-key map "q" 'ebib-quit-help-buffer)
3341 "Keymap for the ebib help buffer.")
3343 (define-derived-mode ebib-help-mode
3344 fundamental-mode "Ebib-help"
3345 "Major mode for the Ebib help buffer."
3346 (setq buffer-read-only t)
3347 (local-set-key "\C-xb" 'ebib-quit-help-buffer)
3348 (local-set-key "\C-xk" 'ebib-quit-help-buffer))
3350 (defun ebib-display-help (buffer)
3351 "Shows the help message for Ebib-buffer BUFFER."
3352 (switch-to-buffer ebib-help-buffer)
3353 (setq ebib-before-help buffer)
3354 (with-buffer-writable
3357 ((eq buffer ebib-index-buffer) (insert ebib-index-buffer-help))
3358 ((eq buffer ebib-entry-buffer) (insert ebib-entry-buffer-help))
3359 ((eq buffer ebib-strings-buffer) (insert ebib-strings-buffer-help)))
3360 (goto-char (point-min))))
3362 (defun ebib-quit-help-buffer ()
3363 "Exits the help buffer."
3366 ((eq ebib-before-help ebib-index-buffer)
3367 (switch-to-buffer ebib-entry-buffer)
3369 ((eq ebib-before-help ebib-entry-buffer)
3370 (switch-to-buffer ebib-entry-buffer))
3371 ((eq ebib-before-help ebib-strings-buffer)
3372 (switch-to-buffer ebib-strings-buffer))))
3378 (defvar ebib-log-mode-map
3379 (let ((map (make-keymap)))
3380 (suppress-keymap map)
3381 (define-key map " " 'scroll-up)
3382 (define-key map "b" 'scroll-down)
3383 (define-key map "q" 'ebib-quit-log-buffer)
3385 "Keymap for the ebib log buffer.")
3387 (define-derived-mode ebib-log-mode
3388 fundamental-mode "Ebib-log"
3389 "Major mode for the Ebib log buffer."
3390 (local-set-key "\C-xb" 'ebib-quit-log-buffer)
3391 (local-set-key "\C-xk" 'ebib-quit-log-buffer))
3393 (defun ebib-quit-log-buffer ()
3394 "Exits the log buffer."
3396 (switch-to-buffer ebib-entry-buffer)
3399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3400 ;; functions for non-Ebib buffers ;;
3401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3403 (defun ebib-import ()
3404 "Searches for BibTeX entries in the current buffer.
3405 The entries are added to the current database (i.e. the database
3406 that was active when Ebib was lowered. Works on the whole buffer,
3407 or on the region if it is active."
3409 (if (not ebib-cur-db)
3410 (error "No database loaded. Use `o' to open a database")
3411 (if (edb-virtual ebib-cur-db)
3412 (error "Cannot import to a virtual database")
3413 (with-syntax-table ebib-syntax-table
3417 (narrow-to-region (region-beginning)
3419 (let ((buffer (current-buffer)))
3421 (insert-buffer-substring buffer)
3422 (let ((n (ebib-find-bibtex-entries t)))
3423 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3424 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3425 (when (edb-strings-list ebib-cur-db)
3426 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3427 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3428 (ebib-fill-entry-buffer)
3429 (ebib-fill-index-buffer)
3430 (ebib-set-modified t)
3431 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3438 (defun ebib-get-db-from-filename (filename)
3439 "Returns the database struct associated with FILENAME."
3441 (mapc #'(lambda (db)
3442 (if (string= (file-name-nondirectory (edb-filename db)) filename)
3447 (defun ebib-get-local-databases ()
3448 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3449 Each .bib file is a string holding the name of the .bib
3450 file. This function simply searches the current LaTeX file or its
3451 master file for a \\bibliography command and returns the file(s)
3452 given in its argument. If no \\bibliography command is found,
3453 returns the symbol NONE."
3454 (let ((texfile-buffer (current-buffer))
3456 ;; if AucTeX's TeX-master is used and set to a string, we must
3457 ;; search that file for a \bibliography command, as it's more
3458 ;; likely to be in there than in the file we're in.
3459 (and (boundp 'TeX-master)
3460 (stringp TeX-master)
3461 (setq texfile (ensure-extension TeX-master "tex")))
3463 (if (and texfile (file-readable-p texfile))
3464 (insert-file-contents texfile)
3465 (insert-buffer-substring texfile-buffer))
3467 (goto-char (point-min))
3468 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3469 (mapcar #'(lambda (file)
3470 (ensure-extension file "bib"))
3471 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3474 (defun ebib-insert-bibtex-key (prefix)
3475 "Inserts a BibTeX key at POINT.
3476 The user is prompted for a BibTeX key and has to choose one from
3477 the database(s) associated with the current LaTeX file, or from
3478 the current database if there is no \\bibliography command. Tab
3483 (or ebib-local-bibtex-filenames
3484 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3486 (if (eq ebib-local-bibtex-filenames 'none)
3487 (if (null (edb-cur-entry ebib-cur-db))
3488 (error "No entries found in current database")
3489 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3490 (mapc #'(lambda (file)
3491 (let ((db (ebib-get-db-from-filename file)))
3494 (message "Database %s not loaded" file))
3495 ((null (edb-cur-entry db))
3496 (message "No entries in database %s" file))
3497 (t (setq collection (append (ebib-create-collection (edb-database db))
3499 ebib-local-bibtex-filenames))
3501 (let* ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
3503 (key (completing-read (format "Insert \"%s\" with key: " latex-cmd)
3504 collection nil t nil ebib-minibuf-hist)))
3506 (insert (format (or (cdr (assoc prefix ebib-insertion-strings))
3509 (error "No database loaded"))))
3511 (defun ebib-entry-summary ()
3512 "Shows the fields of the key at POINT.
3513 The key is searched in the database associated with the LaTeX
3514 file, or in the current database if no \\bibliography command can
3519 (or ebib-local-bibtex-filenames
3520 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3521 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3523 (if (eq ebib-local-bibtex-filenames 'none)
3524 (if (not (member key (edb-keys-list ebib-cur-db)))
3525 (error "`%s' is not in the current database" key)
3526 (setq entry (gethash key (edb-database ebib-cur-db))))
3529 (mapc #'(lambda (file)
3530 (let ((db (ebib-get-db-from-filename file)))
3532 (message "Database %s not loaded" file)
3533 (if (member key (edb-keys-list db))
3534 (throw 'found (gethash key (edb-database db)))))))
3535 ebib-local-bibtex-filenames)
3538 (error "Entry `%s' not found" key)
3539 (with-output-to-temp-buffer "*Help*"
3540 (ebib-format-fields entry 'princ)))))
3542 (error "No database(s) loaded"))))
3546 ;; we put these at the end, because they seem to mess up Emacs'
3547 ;; syntax highlighting.
3549 (setq ebib-index-buffer-help
3550 "Ebib index buffer -- command key overview
3552 Note: command keys are case-sensitive.
3554 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3557 [Up], k, C-p: go to the previous entry
3558 [Down], j, C-n: go to the next entry
3559 [Home], g: go to the first entry
3560 [End], G: go to the last entry
3561 [PgUp], b, M-p: scroll up
3562 [PgDn], [Space], M-n: scroll down
3565 e: edit the current entry
3566 E: edit the current entry's name
3568 d: delete the current entry
3569 ;-d: deletes all marked entries
3570 t: edit the @STRING definitions
3571 r: edit the @PREAMBLE definition
3572 m: (un)mark the current entry
3573 ;-m: unmarks all marked entries
3576 /: search the database
3577 n: find the next occurrence of the search string
3578 C-s: search for a key (incrementally)
3579 [return]: select the entry under the cursor (use after C-s)
3580 F: follow the crossref field
3581 &: filter the current database with a logical AND
3582 |: filter the current database with a logical OR
3583 ~: filter the current database with a logical NOT
3584 V: show the current filter (with prefix argument:
3585 reapply current filter)
3589 c: close the database
3590 s: save the database
3591 S: save all databases
3592 w: save the database under a different name
3593 M: merge another database
3594 x: export the current entry to another file
3595 (with prefix argument N: copy to database N)
3596 ;-x: export the marked entry to another file
3597 (with prefix argument N: copy to database N)
3598 X: export the @PREAMBLE definition to another file
3599 (with prefix argument N: copy to database N)
3600 f: print the full filename in the minibuffer
3603 1-9: switch to database 1-9
3604 J: switch to another database (accepts prefix argument)
3605 [right], [left]: switch to previous/next database
3606 L: LaTeX the database
3607 ;-L: LaTeX the marked entries
3608 P: print the database
3609 ;-P: print the marked entries
3612 u: extract URL from the `url' field and send it to a browser
3613 p: push the current entry to a LaTeX buffer
3614 ;-p: push marked entries to a LaTeX buffer
3616 z: put Ebib in the background
3618 h: show this help page
3621 (setq ebib-entry-buffer-help
3622 "Ebib entry buffer -- command key overview
3624 Note: command keys are case-sensitive.
3626 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3629 [Up], k, C-p: go to the previous field
3630 [Down], j, C-n: go to the next field
3631 [Home], g: go to the first field
3632 [End], G: go to the last field
3633 [PgUp], b, M-p: go to the previous group of fields
3634 [PgDn], [Space], M-n: go to the next group of fields
3637 e: edit the value of the current field
3638 c: copy the value of the current field (value is put into the kill ring)
3639 x: kill the value of the current field (value is put into the kill ring)
3640 y: yank the most recently copied/cut string
3641 d: delete the value of the current entry
3642 r: toggle the \"rawness\" status of the current field
3643 l: edit the current field as multi-line
3644 s: insert an @STRING abbreviation into the current field
3647 u: extract URL and send it to a browser
3648 q: quit the entry buffer and return to the index buffer
3649 h: show this help page
3652 (setq ebib-strings-buffer-help
3653 "Ebib strings buffer -- command key overview
3655 Note: command keys are case-sensitive.
3657 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3660 [Up], k, C-p: go to the previous @STRING definition
3661 [Down], j, C-n: go to the next @STRING definition
3662 [Home], g: go to the first @STRING definition
3663 [End], G: go to the last @STRING definition
3664 [PgUp], b, M-p: scroll 10 @STRING definitions up
3665 [PgDn], [Space], M-n: scroll 10 @STRING definitions down
3668 e: edit the value of the current @STRING definition
3669 c: copy the value of the current @STRING definition
3670 d: delete the current @STRING definition
3671 a: add an @STRING definition
3672 l: edit the current @STRING as multi-line
3675 x: export the current @STRING definition to another file
3676 (with prefix argument N: copy to database N)
3677 X: export all @STRING definitions to another file
3678 (with prefix argument N: copy to database N)
3681 q: quit the strings buffer and return to the index buffer
3682 h: show this help page