3 ;; Copyright (c) 2003-2007 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 keywords timestamp
)
50 "*Holds a list of the additional fields."
52 :type
'(repeat (symbol :tag
"Field")))
54 (defcustom ebib-index-window-size
10
55 "*The number of lines used for the keys buffer window."
59 (defcustom ebib-index-display-fields nil
60 "*Holds a list of the fields to display in the index buffer."
62 :type
'(repeat (symbol :tag
"Index Field")))
64 (defcustom ebib-insertion-strings
'((0 .
"%s")
66 "*The string to insert when calling EBIB-INSERT-BIBTEX-KEY.
67 The directive \"%s\" is replaced with the entry key."
69 :type
'(repeat (cons :tag
"Insertion string" (integer :tag
"Number") (string))))
71 (defcustom ebib-sort-order nil
72 "*The fields on which the BibTeX entries are to be sorted in the .bib file.
73 Sorting is done on different sort levels, and each sort level contains one
76 :type
'(repeat (repeat :tag
"Sort level" (symbol :tag
"Sort field"))))
78 (defcustom ebib-save-xrefs-first nil
79 "*If true, entries with a crossref field will be saved first in the .bib-file.
80 Setting this option has unpredictable results for the sort order
81 of entries, so it is not compatible with setting the Sort Order option."
85 (defface ebib-crossref-face
'((t (:foreground
"red")))
86 "*Face used to indicate values inherited from crossreferenced entries."
89 (defface ebib-marked-face
(if (featurep 'xemacs
)
90 '((t (:foreground
"white" :background
"red")))
91 '((t (:inverse-video t
))))
92 "*Face to indicate marked entries."
95 (defcustom ebib-use-timestamp nil
96 "*If true, new entries will get a time stamp.
97 The time stamp will be stored in a field \"timestamp\" that can
98 be made visible with the `H' command in the index buffer."
102 (defcustom ebib-timestamp-format
"%a %b %e %T %Y"
103 "*Format of the time string used in the timestamp.
104 The format is given directly to FORMAT-TIME-STRING, see the
105 documentation of that function for details."
109 (defcustom ebib-standard-url-field
'url
110 "*Standard field to store urls in.
111 In the index buffer, the command ebib-browse-url can be used to
112 send a url to a browser. This option sets the field from which
113 this command extracts the url."
117 (defcustom ebib-url-regexp
"\\\\url{\\(.*\\)}\\|https?://[^ '<>\"\n\t\f]+"
118 "*Regexp to extract urls."
122 (defcustom ebib-browser-command
""
123 "*Command to call the browser with.
124 GNU/Emacs has a function call-browser, which is used if this
127 :type
'(string :tag
"Browser command"))
129 (defcustom ebib-print-preamble nil
130 "*Preamble used for the LaTeX file for printing the database.
131 Each string is added to the preamble on a separate line."
133 :type
'(repeat (string :tag
"Add to preamble")))
135 (defcustom ebib-print-multiline nil
136 "*If set, multiline fields are included when printing the database."
140 (defcustom ebib-latex-preamble
'("\\bibliographystyle{plain}")
141 "*Preamble used for the LaTeX file for BibTeXing the database.
142 Each string is added to the preamble on a separate line."
144 :type
'(repeat (string :tag
"Add to preamble")))
146 (defcustom ebib-print-tempfile
""
147 "*Temporary file for use with EBIB-PRINT-DATABASE and EBIB-LATEX-DATABASE."
151 (defcustom ebib-allow-identical-fields nil
152 "*If set, Ebib handles multiple occurrences of a field gracefully."
156 (defvar ebib-entry-types-hash
(make-hash-table)
157 "Holds the hash table containing the entry type definitions.")
158 (defvar ebib-unique-field-list nil
159 "Holds a list of all field names.")
161 (defmacro add-to-listq
(listvar element
&optional append fn
)
162 (if (or (featurep 'xemacs
)
163 (string< emacs-version
"22"))
164 `(add-to-list (quote ,listvar
) ,element
,append
)
165 `(add-to-list (quote ,listvar
) ,element
,append
,fn
)))
167 (defun ebib-set-entry-types-hash (var value
)
168 "Sets EBIB-ENTRY-TYPES-HASH on the basis of EBIB-ENTRY-TYPES"
169 (set-default var value
)
170 (clrhash ebib-entry-types-hash
)
171 (setq ebib-unique-field-list nil
)
172 (mapc #'(lambda (entry)
173 (puthash (car entry
) (cdr entry
) ebib-entry-types-hash
)
174 (mapc #'(lambda (field)
175 (add-to-listq ebib-unique-field-list field t
'eq
))
177 (mapc #'(lambda (field)
178 (add-to-listq ebib-unique-field-list field t
'eq
))
182 (defcustom ebib-entry-types
183 '((article ;; name of entry type
184 (author title journal year
) ;; obligatory fields
185 (volume number pages month note
)) ;; optional fields
188 (author title publisher year
)
189 (editor volume number series address edition month note
))
193 (author howpublished address month year note
))
196 (author title chapter pages publisher year
)
197 (editor volume series address edition month note
))
200 (author title booktitle publisher year
)
201 (editor volume number series type chapter pages address edition month note
))
204 (author title booktitle year
)
205 (editor pages organization publisher address month note
))
209 (author organization address edition month year note
))
213 (title author howpublished month year note
))
216 (author title school year
)
217 (address month note
))
220 (author title school year
)
221 (address month note
))
225 (editor publisher organization address month note
))
228 (author title institution year
)
229 (type number address month note
))
235 "List of entry type definitions for Ebib"
237 :type
'(repeat (list :tag
"Entry type" (symbol :tag
"Name")
238 (repeat :tag
"Obligatory fields" (symbol :tag
"Field"))
239 (repeat :tag
"Optional fields" (symbol :tag
"Field"))))
240 :set
'ebib-set-entry-types-hash
)
242 ;; generic for all databases
244 ;; constants and variables that are set only once
245 (defconst ebib-bibtex-identifier
"[^\"#%'(),={} \t\n\f]*" "Regex describing a licit BibTeX identifier.")
246 (defconst ebib-version
"==VERSION==")
247 (defvar ebib-initialized nil
"T if Ebib has been initialized.")
249 ;; buffers and highlights
250 (defvar ebib-index-buffer nil
"The index buffer.")
251 (defvar ebib-entry-buffer nil
"The entry buffer.")
252 (defvar ebib-strings-buffer nil
"The strings buffer.")
253 (defvar ebib-multiline-buffer nil
"Buffer for editing multiline strings.")
254 (defvar ebib-help-buffer nil
"Buffer showing Ebib help.")
255 (defvar ebib-log-buffer nil
"Buffer showing warnings and errors during loading of .bib files")
256 (defvar ebib-index-highlight nil
"Highlight to mark the current entry.")
257 (defvar ebib-fields-highlight nil
"Highlight to mark the current field.")
258 (defvar ebib-strings-highlight nil
"Highlight to mark the current string.")
260 ;; general bookkeeping
261 (defvar ebib-minibuf-hist nil
"Holds the minibuffer history for Ebib")
262 (defvar ebib-saved-window-config nil
"Stores the window configuration when Ebib is called.")
263 (defvar ebib-export-filename nil
"Filename to export entries to.")
264 (defvar ebib-push-buffer nil
"Buffer to push entries to.")
265 (defvar ebib-search-string nil
"Stores the last search string.")
266 (defvar ebib-editing nil
"Indicates what the user is editing.
267 Its value can be 'strings, 'fields, or 'preamble.")
268 (defvar ebib-multiline-raw nil
"Indicates whether the multiline text being edited is raw.")
269 (defvar ebib-before-help nil
"Stores the buffer the user was in when he displayed the help message.")
270 (defvar ebib-log-error nil
"Indicates whether an error was logged.")
271 (defvar ebib-local-bibtex-filenames nil
"A buffer-local variable holding a list of the name(s) of that buffer's .bib file")
272 (make-variable-buffer-local 'ebib-local-bibtex-filenames
)
273 (defvar ebib-syntax-table
(make-syntax-table) "Syntax table used for reading .bib files.")
274 (modify-syntax-entry ?\
[ "." ebib-syntax-table
)
275 (modify-syntax-entry ?\
] "." ebib-syntax-table
)
276 (modify-syntax-entry ?\
( "." ebib-syntax-table
)
277 (modify-syntax-entry ?\
) "." ebib-syntax-table
)
278 (modify-syntax-entry ?
\" "w" ebib-syntax-table
)
282 ;; each database is represented by a struct
284 (database (make-hash-table :test
'equal
)) ; hashtable containing the database itself
285 (keys-list nil
) ; sorted list of the keys in the database
286 (cur-entry nil
) ; sublist of KEYS-LIST that starts with the current entry
287 (marked-entries nil
) ; list of marked entries
288 (n-entries 0) ; number of entries stored in this database
289 (strings (make-hash-table :test
'equal
)) ; hashtable with the @STRING definitions
290 (strings-list nil
) ; sorted list of the @STRING abbreviations
291 (preamble nil
) ; string with the @PREAMBLE definition
292 (filename nil
) ; name of the BibTeX file that holds this database
293 (name nil
) ; name of the database
294 (modified nil
) ; has this database been modified?
295 (make-backup nil
) ; do we need to make a backup of the .bib file?
296 (virtual nil
)) ; is this a virtual database?
298 ;; the master list and the current database
299 (defvar ebib-databases nil
"List of structs containing the databases")
300 (defvar ebib-cur-db nil
"The database that is currently active")
302 ;;;;;; bookkeeping required when editing field values or @STRING definitions
304 (defvar ebib-hide-hidden-fields t
"If set to T, hidden fields are not shown.")
306 ;; these two variables are set when the user enters the entry buffer
307 (defvar ebib-cur-entry-hash nil
"The hash table containing the data of the current entry.")
308 (defvar ebib-cur-entry-fields nil
"The fields of the type of the current entry.")
310 ;; and these two are set by EBIB-FILL-ENTRY-BUFFER and EBIB-FILL-STRINGS-BUFFER, respectively
311 (defvar ebib-current-field nil
"The current field.")
312 (defvar ebib-current-string nil
"The current @STRING definition.")
314 ;; we define these variables here, but only give them a value at the end of
315 ;; the file, because the long strings mess up Emacs' syntax highlighting.
316 (defvar ebib-index-buffer-help nil
"Help for the index buffer.")
317 (defvar ebib-entry-buffer-help nil
"Help for the entry buffer.")
318 (defvar ebib-strings-buffer-help nil
"Help for the strings buffer.")
320 ;; the prefix key is stored in a variable so that the user can customise it.
321 (defvar ebib-prefix-key ?\
;)
323 ;; this is an AucTeX variable, but we want to check its value, so let's
324 ;; keep the compiler from complaining.
328 ;; this is to keep XEmacs from complaining.
330 (if (featurep 'xemacs
)
331 (defvar mark-active
)))
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 ;; useful macros and functions ;;
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 (defmacro nor
(&rest args
)
338 "Returns T if none of its arguments are true."
341 ;; we sometimes (often, in fact ;-) need to do something with a string, but
342 ;; take special action (or do nothing) if that string is empty. IF-STR
343 ;; makes that easier:
345 (defmacro if-str
(bindvar then
&rest else
)
346 "Execute THEN only if STRING is nonempty.
347 Format: (if-str (var value) then-form [else-forms]) VAR is bound
348 to VALUE, which is evaluated. If VAR is a nonempty string,
349 THEN-FORM is executed. If VAR is either \"\" or nil, ELSE-FORM is
350 executed. Returns the value of THEN or of ELSE."
352 `(let ,(list bindvar
)
353 (if (nor (null ,(car bindvar
))
354 (equal ,(car bindvar
) ""))
358 (defmacro last1
(lst &optional n
)
359 "Returns the last (or Nth last) element of LST."
360 `(car (last ,lst
,n
)))
362 ;; we sometimes need to walk through lists. these functions yield the
363 ;; element directly preceding or following ELEM in LIST. in order to work
364 ;; properly, ELEM must be unique in LIST, obviously. if ELEM is the
365 ;; first/last element of LIST, or if it is not contained in LIST at all,
366 ;; the result is nil.
367 (defun next-elem (elem list
)
368 (cadr (member elem list
)))
370 (defun prev-elem (elem list
)
371 (if (or (equal elem
(car list
))
372 (not (member elem list
)))
374 (last1 list
(1+ (length (member elem list
))))))
376 (defun read-string-at-point (chars)
377 "Reads a string at POINT delimited by CHARS and returns it.
378 CHARS is a string of characters that should not occur in the string."
380 (skip-chars-backward (concat "^" chars
))
382 (looking-at-goto-end (concat "[^" chars
"]*"))
383 (buffer-substring-no-properties beg
(point)))))
385 (defun remove-from-string (string remove
)
386 "Returns a copy of STRING with all the occurrences of REMOVE taken out.
387 REMOVE can be a regex."
388 (apply 'concat
(split-string string remove
)))
390 (defun in-string (char string
)
391 "Returns T if CHAR is in STRING, otherwise NIL."
393 (do ((len (length string
))
396 (if (eq char
(aref string i
))
399 (defun ensure-extension (string ext
)
400 "Makes sure STRING has the extension EXT, by appending it if necessary.
401 EXT should be an extension without the dot."
402 (if (string-match (concat "\\." ext
"$") string
)
404 (concat string
"." ext
)))
406 (defmacro with-buffer-writable
(&rest body
)
407 "Makes the current buffer writable and executes the commands in BODY.
408 After BODY is executed, the buffer modified flag is unset."
409 (declare (indent defun
))
411 (let ((buffer-read-only nil
))
413 (set-buffer-modified-p nil
)))
415 (defmacro safe-write-region
(start end filename
&optional append visit lockname mustbenew
)
416 "XEmacs does not have the MUSTBENEW argument, so this is a way to implement it."
417 (if (featurep 'xemacs
)
418 `(if (and (file-exists-p ,filename
)
419 (not (y-or-n-p (format "File %s already exists; overwrite anyway? " ,filename
))))
420 (error "File %s exist" ,filename
)
421 (write-region ,start
,end
,filename
,append
,visit
,lockname
))
422 `(write-region ,start
,end
,filename
,append
,visit
,lockname
,mustbenew
)))
424 (defun symbol-or-string (x)
425 "Returns the symbol-name of X if X is a symbol, otherwise return X.
426 Much like SYMBOL-NAME, except it does not throw an error if X is
432 ;; XEmacs doesn't know about propertize...
433 (if (not (fboundp 'propertize
))
434 (defun propertize (string &rest properties
)
435 "Return a copy of STRING with text properties added.
436 First argument is the string to copy. Remaining arguments form a
437 sequence of PROPERTY VALUE pairs for text properties to add to
439 (let ((new-string (copy-sequence string
)))
440 (add-text-properties 0 (length new-string
) properties new-string
)
443 (defun region-active ()
444 (if (featurep 'xemacs
)
448 ;; RAW-P determines if STRING is raw. note that we cannot do this by
449 ;; simply checking whether STRING begins with { and ends with } (or
450 ;; begins and ends with "), because something like "{abc} # D # {efg}"
451 ;; would then be incorrectly recognised as non-raw. so we need to do
452 ;; the following: take out everything that is between braces or
453 ;; quotes, and see if anything is left. if there is, the original
454 ;; string was raw, otherwise it was not.
456 ;; so i first check whether the string begins with { or ". if not, we
457 ;; certainly have a raw string. (RAW-P recognises this through the default
458 ;; clause of the COND.) if the first character is { or ", we first take out
459 ;; every occurrence of backslash-escaped { and } or ", so that the rest of
460 ;; the function does not get confused over them.
462 ;; then, if the first character is {, i use REMOVE-FROM-STRING to take out
463 ;; every occurrence of the regex "{[^{]*?}", which translates to "the
464 ;; smallest string that starts with { and ends with }, and does not contain
465 ;; another {. IOW, it takes out the innermost braces and their
466 ;; contents. because braces may be embedded, we have to repeat this step
467 ;; until no more balanced braces are found in the string. (note that it
468 ;; would be unwise to check for just the occurrence of { or }, because that
469 ;; would throw RAW-P in an infinite loop if a string contains an unbalanced
472 ;; for strings beginning with " i do the same, except that it is not
473 ;; necessary to repeat this in a WHILE loop, for the simple reason that
474 ;; strings surrounded with double quotes cannot be embedded; i.e.,
475 ;; "ab"cd"ef" is not a valid (BibTeX) string, while {ab{cd}ef} is.
477 ;; note: because these strings are to be fed to BibTeX and ultimately
478 ;; (La)TeX, it might seem that we don't need to worry about strings
479 ;; containing unbalanced braces, because (La)TeX would choke on them. but
480 ;; the user may inadvertently enter such a string, and we therefore need to
481 ;; be able to handle it. (alternatively, we could perform a check on
482 ;; strings and warn the user.)
484 (defun raw-p (string)
485 "Non-nil if STRING is raw."
486 (when (stringp string
)
488 ((eq (string-to-char string
) ?\
{)
489 ;; we remove all occurrences of `\{' and of `\}' from the string:
490 (let ((clear-str (remove-from-string (remove-from-string string
"[\\][{]")
492 (while (and (in-string ?\
{ clear-str
) (in-string ?\
} clear-str
))
493 (setq clear-str
(remove-from-string clear-str
"{[^{]*?}")))
494 (> (length clear-str
) 0)))
495 ((eq (string-to-char string
) ?
\")
496 (let ((clear-str (remove-from-string string
"[\\][\"]"))) ; remove occurrences of `\"'
497 (setq clear-str
(remove-from-string clear-str
"\"[^\"]*?\""))
498 (> (length clear-str
) 0)))
501 (defun to-raw (string)
502 "Converts a string to its raw counterpart."
503 (if (and (stringp string
)
504 (not (raw-p string
)))
505 (substring string
1 -
1)
508 (defun from-raw (string)
509 "Converts a raw string to a non-raw one."
511 (concat "{" string
"}")
514 (defun multiline-p (string)
515 "True if STRING is multiline."
517 (string-match "\n" string
)))
519 (defun first-line (string)
520 "Returns the first line of a multi-line string."
521 (string-match "\n" string
)
522 (substring string
0 (match-beginning 0)))
524 (defun sort-in-buffer (limit str
)
525 "Moves POINT to the right position to insert STR in a buffer with lines sorted A-Z."
530 (goto-char (point-min))
532 (setq middle
(/ (+ lower upper
1) 2))
533 (goto-line middle
) ; if this turns out to be where we need to be,
534 (beginning-of-line) ; this puts POINT at the right spot.
535 (> (- upper lower
) 1)) ; if upper and lower differ by only 1, we have found the
536 ; position to insert the entry in.
540 (if (string< (buffer-substring-no-properties beg
(point)) str
)
542 (setq upper middle
)))))))))
544 (defun match-all (match-str string
)
545 "Highlights all the matches of MATCH-STR in STRING.
546 The return value is a list of two elements: the first is the
547 modified string, the second either t or nil, indicating whether a
548 match was found at all."
549 (do ((counter 0 (match-end 0)))
550 ((not (string-match match-str string counter
)) (values string
(not (= counter
0))))
551 (add-text-properties (match-beginning 0) (match-end 0) '(face highlight
) string
)))
553 (defun looking-at-goto-end (str &optional match
)
554 "Like LOOKING-AT but moves point to the end of the matching string.
555 MATCH acts just like the argument to MATCH-END, and defaults to 0."
556 (or match
(setq match
0))
557 (let ((case-fold-search t
))
559 (goto-char (match-end match
)))))
561 ;; this needs to be wrapped in an eval-and-compile, to keep Emacs from
562 ;; complaining that ebib-execute-helper isn't defined when it compiles
563 ;; ebib-execute-when.
565 (defun ebib-execute-helper (env)
566 "Helper function for EBIB-EXECUTE-WHEN."
570 (edb-cur-entry ebib-cur-db
)))
571 ((eq env
'marked-entries
)
573 (edb-marked-entries ebib-cur-db
)))
578 (not (edb-virtual ebib-cur-db
))))
579 ((eq env
'virtual-db
)
581 (edb-virtual ebib-cur-db
)))
582 ((eq env
'no-database
)
586 (defmacro ebib-execute-when
(&rest forms
)
587 "Macro to facilitate writing Ebib functions.
588 This functions essentially like a COND clause: the basic format
589 is (ebib-execute-when FORMS ...), where each FORM is built up
590 as (ENVIRONMENTS BODY). ENVIRONMENTS is a list of symbols (not
591 quoted) that specify under which conditions BODY is to be
592 executed. Valid symbols are:
594 entries: execute when there are entries in the database,
595 marked-entries: execute when there are marked entries in the database,
596 database: execute if there is a database,
597 no-database: execute if there is no database,
598 real-db: execute when there is a database and it is real,
599 virtual-db: execute when there is a database and it is virtual,
600 default: execute if all else fails.
602 Just like with COND, only one form is actually executed, the
603 first one that matches. If ENVIRONMENT contains more than one
604 condition, BODY is executed if they all match (i.e., the
605 conditions are AND'ed.)"
606 (declare (indent defun
))
608 ,@(mapcar #'(lambda (form)
609 (cons (if (= 1 (length (car form
)))
610 (ebib-execute-helper (caar form
))
611 `(and ,@(mapcar #'(lambda (env)
612 (ebib-execute-helper env
))
617 ;; the numeric prefix argument is 1 if the user gave no prefix argument at
618 ;; all. the raw prefix argument is not always a number. so we need to do
619 ;; our own conversion.
620 (defun ebib-prefix (num)
624 (defun ebib-called-with-prefix ()
625 "Returns T if the command was called with a prefix key."
626 (if (featurep 'xemacs
)
627 (member (character-to-event ebib-prefix-key
) (append (this-command-keys) nil
))
628 (member (event-convert-list (list ebib-prefix-key
))
629 (append (this-command-keys-vector) nil
))))
631 (defmacro ebib-export-to-db
(num message copy-fn
)
632 "Exports data to another database.
633 NUM is the number of the database to which the data is to be copied.
635 MESSAGE is a string displayed in the echo area if the export was
636 succesful. It must contain a %d directive, which is used to
637 display the database number to which the entry was exported.
639 COPY-FN is the function that actually copies the relevant
640 data. It must take as argument the database to which the data is
641 to be copied. COPY-FN must return T if the copying was
642 successful, and NIL otherwise."
643 `(let ((goal-db (nth (1- ,num
) ebib-databases
)))
646 (error "Database %d does not exist" ,num
))
647 ((edb-virtual goal-db
)
648 (error "Database %d is virtual" ,num
))
649 (t (when (funcall ,copy-fn goal-db
)
650 (ebib-set-modified t goal-db
)
651 (message ,message
,num
))))))
653 (defmacro ebib-export-to-file
(prompt-string message insert-fn
)
654 "Exports data to a file.
655 PROMPT-STRING is the string that is used to ask for the filename
656 to export to. INSERT-FN must insert the data to be exported into
657 the current buffer: it is called within a WITH-TEMP-BUFFER, whose
658 contents is appended to the file the user enters.
660 MESSAGE is shown in the echo area when the export was
661 successful. It must contain a %s directive, which is used to
662 display the actual filename."
663 `(let ((insert-default-directory (not ebib-export-filename
)))
664 (if-str (filename (read-file-name
665 ,prompt-string
"~/" nil nil ebib-export-filename
))
668 (append-to-file (point-min) (point-max) filename
)
669 (setq ebib-export-filename filename
)))))
671 (defun ebib-get-obl-fields (entry-type)
672 "Returns the obligatory fields of ENTRY-TYPE."
673 (car (gethash entry-type ebib-entry-types-hash
)))
675 (defun ebib-get-opt-fields (entry-type)
676 "Returns the optional fields of ENTRY-TYPE."
677 (cadr (gethash entry-type ebib-entry-types-hash
)))
679 (defun ebib-get-all-fields (entry-type)
680 "Returns all the fields of ENTRY-TYPE."
681 (cons 'type
* (append (ebib-get-obl-fields entry-type
)
682 (ebib-get-opt-fields entry-type
)
683 ebib-additional-fields
)))
685 (defun ebib-erase-buffer (buffer)
687 (with-buffer-writable
690 (defun ebib-make-highlight (begin end buffer
)
692 (if (featurep 'xemacs
)
694 (setq highlight
(make-extent begin end buffer
))
695 (set-extent-face highlight
'highlight
))
697 (setq highlight
(make-overlay begin end buffer
))
698 (overlay-put highlight
'face
'highlight
)))
701 (defun ebib-move-highlight (highlight begin end buffer
)
702 (if (featurep 'xemacs
)
703 (set-extent-endpoints highlight begin end buffer
)
704 (move-overlay highlight begin end buffer
)))
706 (defun ebib-highlight-start (highlight)
707 (if (featurep 'xemacs
)
708 (extent-start-position highlight
)
709 (overlay-start highlight
)))
711 (defun ebib-highlight-end (highlight)
712 (if (featurep 'xemacs
)
713 (extent-end-position highlight
)
714 (overlay-end highlight
)))
716 (defun ebib-delete-highlight (highlight)
717 (if (featurep 'xemacs
)
718 (detach-extent highlight
)
719 (delete-overlay highlight
)))
721 (defun ebib-set-index-highlight ()
722 (set-buffer ebib-index-buffer
)
725 (if ebib-index-display-fields
727 (skip-chars-forward "^ "))
728 (ebib-move-highlight ebib-index-highlight beg
(point) ebib-index-buffer
)))
730 (defun ebib-set-fields-highlight ()
731 (set-buffer ebib-entry-buffer
)
734 (looking-at-goto-end "[^ \t\n\f]*")
735 (ebib-move-highlight ebib-fields-highlight beg
(point) ebib-entry-buffer
)))
737 (defun ebib-set-strings-highlight ()
738 (set-buffer ebib-strings-buffer
)
741 (looking-at-goto-end "[^ \t\n\f]*")
742 (ebib-move-highlight ebib-strings-highlight beg
(point) ebib-strings-buffer
)))
744 (defun ebib-display-entry (entry-key)
745 "Displays ENTRY-KEY in the index buffer at POINT."
746 (set-buffer ebib-index-buffer
)
747 (insert (format "%-30s %s\n"
749 (if ebib-index-display-fields
750 (let ((cur-entry-hash (ebib-retrieve-entry entry-key ebib-cur-db
)))
751 (mapconcat #'(lambda (field)
753 (to-raw (gethash field cur-entry-hash
))
755 ebib-index-display-fields
759 (defun ebib-redisplay-current-field ()
760 "Redisplays the contents of the current field in the entry buffer."
761 (set-buffer ebib-entry-buffer
)
762 (if (eq ebib-current-field
'crossref
)
763 (ebib-fill-entry-buffer)
764 (with-buffer-writable
765 (goto-char (ebib-highlight-start ebib-fields-highlight
))
768 (delete-region beg
(point)))
769 (insert (format "%-17s " (symbol-name ebib-current-field
))
770 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash
))
771 (ebib-set-fields-highlight))))
773 (defun ebib-redisplay-current-string ()
774 "Redisplays the current string definition in the strings buffer."
775 (set-buffer ebib-strings-buffer
)
776 (with-buffer-writable
777 (let ((str (to-raw (gethash ebib-current-string
(edb-strings ebib-cur-db
)))))
778 (goto-char (ebib-highlight-start ebib-strings-highlight
))
781 (delete-region beg
(point)))
782 (insert (format "%-18s %s" ebib-current-string
783 (if (multiline-p str
)
784 (concat "+" (first-line str
))
786 (ebib-set-strings-highlight))))
788 (defun ebib-move-to-field (field direction
)
789 "Moves the fields overlay to the line containing FIELD.
790 If DIRECTION is positive, searches forward, if DIRECTION is
791 negative, searches backward. If DIRECTION is 1 or -1, searches
792 from POINT, if DIRECTION is 2 or -2, searches from beginning or
793 end of buffer. If FIELD is not found in the entry buffer, the
794 overlay is not moved. FIELD must be a symbol."
796 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
798 (set-buffer ebib-entry-buffer
)
799 (if (eq field
'type
*)
800 (goto-char (point-min))
801 (multiple-value-bind (fn start limit
) (if (>= direction
0)
802 (values 're-search-forward
(point-min) (point-max))
803 (values 're-search-backward
(point-max) (point-min)))
804 ;; make sure we can get back to our original position, if the field
805 ;; cannot be found in the buffer:
806 (let ((current-pos (point)))
807 (when (evenp direction
)
809 (unless (funcall fn
(concat "^" (symbol-name field
)) limit t
)
810 (goto-char current-pos
)))))
811 (ebib-set-fields-highlight))
813 (defun ebib-create-collection (hashtable)
814 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
815 The keys of HASHTABLE must be either symbols or strings."
817 (maphash #'(lambda (x y
)
818 (setq result
(cons (cons (symbol-or-string x
)
824 (defmacro ebib-retrieve-entry
(entry-key db
)
825 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
826 `(gethash ,entry-key
(edb-database ,db
)))
828 (defun ebib-get-field-highlighted (field current-entry
&optional match-str
)
829 ;; note: we need to work on a copy of the string, otherwise the highlights
830 ;; are made to the string as stored in the database. hence copy-sequence.
831 (let ((case-fold-search t
)
832 (string (copy-sequence (gethash field current-entry
)))
836 ;; we have to do a couple of things now:
837 ;; - remove {} or "" around the string, if they're there
838 ;; - search for match-str
839 ;; - properly adjust the string if it's multiline
840 ;; but all this is not necessary if there was no string
842 (let* ((xref (to-raw (gethash 'crossref current-entry
)))
843 (xref-entry (ebib-retrieve-entry xref ebib-cur-db
)))
845 (setq string
(gethash field xref-entry
))
847 (setq string
(propertize (to-raw string
) 'face
'ebib-crossref-face
'fontified t
))
851 (setq string
(to-raw string
))) ; we have to make the string look nice
853 (multiple-value-setq (string matched
) (match-all match-str string
)))
854 (when (multiline-p string
)
855 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
856 ;; multiline is local and therefore the object it refers to should
857 ;; be GC'ed when the function returns. but for some reason, the
858 ;; plus sign is persistent, and if it's been highlighted as the
859 ;; result of a search, it stays that way.
860 (setq multiline
(propertize "+" 'face nil
))
861 (setq string
(first-line string
))))
863 (string= multiline
"+"))
864 (add-text-properties 0 1 '(face highlight
) multiline
))
865 (concat raw multiline string
)))
867 (defun ebib-format-fields (entry fn
&optional match-str
)
868 (let* ((entry-type (gethash 'type
* entry
))
869 (obl-fields (ebib-get-obl-fields entry-type
))
870 (opt-fields (ebib-get-opt-fields entry-type
)))
871 (funcall fn
(format "%-19s %s\n" "type" entry-type
))
872 (mapc #'(lambda (fields)
874 (mapcar #'(lambda (field)
875 (unless (and (get field
'ebib-hidden
)
876 ebib-hide-hidden-fields
)
877 (funcall fn
(format "%-17s " field
))
879 (ebib-get-field-highlighted field entry match-str
)
883 (list obl-fields opt-fields ebib-additional-fields
))))
885 (defun ebib-fill-entry-buffer (&optional match-str
)
886 "Fills the entry buffer with the fields of the current entry.
887 MATCH-STRING is a regexp that will be highlighted when it occurs in the
889 (set-buffer ebib-entry-buffer
)
890 (with-buffer-writable
892 (when (and ebib-cur-db
; do we have a database?
893 (edb-keys-list ebib-cur-db
) ; does it contain entries?
894 (gethash (car (edb-cur-entry ebib-cur-db
))
895 (edb-database ebib-cur-db
))) ; does the current entry exist?
896 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db
))
897 (edb-database ebib-cur-db
)) 'insert match-str
)
898 (setq ebib-current-field
'type
*)
899 (goto-char (point-min))
900 (ebib-set-fields-highlight))))
902 (defun ebib-set-modified (mod &optional db
)
903 "Sets the modified flag of the database DB to MOD.
904 If DB is nil, it defaults to the current database, and the
905 modified flag of the index buffer is also (re)set. MOD must be
908 (setq db ebib-cur-db
))
909 (setf (edb-modified db
) mod
)
910 (when (eq db ebib-cur-db
)
912 (set-buffer ebib-index-buffer
)
913 (set-buffer-modified-p mod
))))
915 (defun ebib-modified-p ()
916 "Checks if any of the databases in Ebib were modified.
917 Returns the first modified database, or NIL if none was modified."
918 (let ((db (car ebib-databases
)))
920 (not (edb-modified db
)))
921 (setq db
(next-elem db ebib-databases
)))
924 (defun ebib-create-new-database (&optional db
)
925 "Creates a new database instance and returns it.
926 If DB is set to a database, the new database is a copy of DB."
931 (setq ebib-databases
(append ebib-databases
(list new-db
)))
934 (defun ebib-match-paren-forward (limit)
935 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
936 This function handles parentheses () and braces {}. Does not
937 search/move beyond LIMIT. Returns T if a matching parenthesis was
938 found, NIL otherwise. If point was not at an opening parenthesis
939 at all, NIL is returned and point is not moved. If point was at
940 an opening parenthesis but no matching closing parenthesis was
941 found, an error is logged and point is moved one character
942 forward to allow parsing to continue."
944 ((eq (char-after) ?\
{)
945 (ebib-match-brace-forward limit
))
946 ((eq (char-after) ?\
()
947 ;; we wrap this in a condition-case because we need to log the error
948 ;; message outside of the save-restriction, otherwise we get the wrong
952 (narrow-to-region (point) limit
)
953 ;; this is really a hack. we want to allow unbalanced parentheses in
954 ;; field values (bibtex does), so we cannot use forward-list
955 ;; here. for the same reason, looking for the matching paren by hand
956 ;; is pretty complicated. however, balanced parentheses can only be
957 ;; used to enclose entire entries (or @STRINGs or @PREAMBLEs) so we
958 ;; can be pretty sure we'll find it right before the next @ at the
959 ;; start of a line, or right before the end of the file.
960 (re-search-forward "^@" nil
0)
961 (skip-chars-backward "@ \n\t\f")
963 (if (eq (char-after) ?\
))
965 (goto-char (1+ (point-min)))
967 (error (ebib-log 'error
"Error in line %d: Matching closing parenthesis not found!" (line-number-at-pos))
971 (defun ebib-match-delim-forward (limit)
972 "Moves forward to the closing delimiter matching the opening delimiter at POINT.
973 This function handles braces {} and double quotes \"\". Does not
974 search/move beyond LIMIT. Returns T if a matching delimiter was
975 found, NIL otherwise. If point was not at an opening delimiter at
976 all, NIL is returned and point is not moved. If point was at an
977 opening delimiter but no matching closing delimiter was found, an
978 error is logged and point is moved one character forward to allow
979 parsing to continue."
981 ((eq (char-after) ?
\")
982 (ebib-match-quote-forward limit
))
983 ((eq (char-after) ?\
{)
984 (ebib-match-brace-forward limit
))
987 (defun ebib-match-brace-forward (limit)
988 "Moves forward to the closing brace matching the opening brace at POINT.
989 Does not search/move beyond LIMIT. Returns T if a matching brace
990 was found, NIL otherwise. If point was not at an opening brace at
991 all, NIL is returned and point is not moved. If point was at an
992 opening brace but no matching closing brace was found, an error
993 is logged and point is moved one character forward to allow
994 parsing to continue."
995 (when (eq (char-after) ?\
{) ; make sure we're really on a brace, otherwise return nil
998 (narrow-to-region (point) limit
)
1001 ;; all of ebib expects that point moves to the closing
1002 ;; parenthesis, not right after it, so we adjust.
1004 t
)) ; return t because a matching brace was found
1006 (ebib-log 'error
"Error in line %d: Matching closing brace not found!" (line-number-at-pos))
1010 (defun ebib-match-quote-forward (limit)
1011 "Moves to the closing double quote matching the quote at POINT.
1012 Does not search/move beyond LIMIT. Returns T if a matching quote
1013 was found, NIL otherwise. If point was not at a double quote at
1014 all, NIL is returned and point is not moved. If point was at a
1015 quote but no matching closing quote was found, an error is logged
1016 and point is moved one character forward to allow parsing to
1018 (when (eq (char-after (point)) ?
\") ; make sure we're on a double quote.
1021 (narrow-to-region (point) limit
)
1023 (forward-char) ; move forward because we're on a double quote
1024 (skip-chars-forward "^\"") ; search the next double quote
1025 (eq (char-before) ?
\\))) ; if it's preceded by a backslash, keep on searching
1026 (or (eq (char-after) ?
\")
1028 (goto-char (1+ (point-min)))
1030 (error (ebib-log 'error
"Error in line %d: Matching closing quote not found!" (line-number-at-pos))
1033 (defun ebib-insert-entry (entry-key fields db
&optional sort timestamp
)
1034 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
1035 Optional argument SORT indicates whether the KEYS-LIST must be
1036 sorted after insertion. Default is NIL. Optional argument
1037 TIMESTAMP indicates whether a timestamp is to be added to the
1038 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
1039 must also be set to T."
1040 (when (and timestamp ebib-use-timestamp
)
1041 (puthash 'timestamp
(from-raw (format-time-string ebib-timestamp-format
)) fields
))
1042 (puthash entry-key fields
(edb-database db
))
1043 (ebib-set-modified t db
)
1044 (setf (edb-n-entries db
) (1+ (edb-n-entries db
)))
1045 (setf (edb-keys-list db
)
1047 (sort (cons entry-key
(edb-keys-list db
)) 'string
<)
1048 (cons entry-key
(edb-keys-list db
)))))
1050 (defun ebib-insert-string (abbr string db
&optional sort
)
1051 "Stores the @STRING definition defined by ABBR and STRING into DB.
1052 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
1053 after insertion. When loading or merging a file, for example, it is more
1054 economic to sort KEYS-LIST manually after all entries in the file have been
1056 (puthash abbr
(from-raw string
) (edb-strings db
))
1057 (ebib-set-modified t db
)
1058 (setf (edb-strings-list db
)
1060 (sort (cons abbr
(edb-strings-list db
)) 'string
<)
1061 (cons abbr
(edb-strings-list db
)))))
1063 (defmacro ebib-cur-entry-key
()
1064 "Returns the key of the current entry in EBIB-CUR-DB."
1065 `(car (edb-cur-entry ebib-cur-db
)))
1067 (defun ebib-search-key-in-buffer (entry-key)
1068 "Searches ENTRY-KEY in the index buffer.
1070 Moves point to the first character of the key and returns point."
1071 (goto-char (point-min))
1072 (search-forward entry-key
)
1076 ;; when we sort entries, we either use string< on the entry keys, or
1077 ;; ebib-entry<, if the user has defined a sort order.
1079 (defun ebib-entry< (x y
)
1080 "Returns T if entry X is smaller than entry Y.
1081 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1082 and Y should be keys of entries in the current database."
1083 (let* ((sort-list ebib-sort-order
)
1084 (sortstring-x (to-raw (ebib-get-sortstring x
(car sort-list
))))
1085 (sortstring-y (to-raw (ebib-get-sortstring y
(car sort-list
)))))
1086 (while (and sort-list
1087 (string= sortstring-x sortstring-y
))
1088 (setq sort-list
(cdr sort-list
))
1089 (setq sortstring-x
(to-raw (ebib-get-sortstring x
(car sort-list
))))
1090 (setq sortstring-y
(to-raw (ebib-get-sortstring y
(car sort-list
)))))
1091 (if (and sortstring-x sortstring-y
)
1092 (string< sortstring-x sortstring-y
)
1095 (defun ebib-get-sortstring (entry-key sortkey-list
)
1096 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1097 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1098 is a list of fields that are considered in order for the sort value."
1099 (let ((sort-string nil
))
1100 (while (and sortkey-list
1101 (null (setq sort-string
(gethash (car sortkey-list
)
1102 (ebib-retrieve-entry entry-key ebib-cur-db
)))))
1103 (setq sortkey-list
(cdr sortkey-list
)))
1106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1107 ;; main program execution ;;
1108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1112 "Ebib, a BibTeX database manager."
1114 (if (or (equal (window-buffer) ebib-index-buffer
)
1115 (equal (window-buffer) ebib-entry-buffer
))
1116 (error "Ebib already active")
1117 ;; we save the buffer from which ebib is called
1118 (setq ebib-push-buffer
(current-buffer))
1119 (unless ebib-initialized
1121 (if ebib-preload-bib-files
1122 (mapc #'(lambda (file)
1123 (ebib-load-bibtex-file file
))
1124 ebib-preload-bib-files
)))
1125 ;; we save the current window configuration.
1126 (setq ebib-saved-window-config
(current-window-configuration))
1127 ;; create the window configuration we want for ebib.
1128 (delete-other-windows)
1129 (switch-to-buffer ebib-index-buffer
)
1130 (let* ((keys-window (selected-window))
1131 (entry-window (split-window keys-window ebib-index-window-size
)))
1132 (set-window-buffer entry-window ebib-entry-buffer
))))
1136 This function sets all variables to their initial values, creates the
1137 buffers and reads the rc file."
1138 (setq ebib-cur-entry-hash nil
1139 ebib-current-field nil
1140 ebib-minibuf-hist nil
1141 ebib-saved-window-config nil
)
1142 (put 'timestamp
'ebib-hidden t
)
1143 (load "~/.ebibrc" t
)
1144 (ebib-create-buffers)
1145 (setq ebib-index-highlight
(ebib-make-highlight 1 1 ebib-index-buffer
))
1146 (setq ebib-fields-highlight
(ebib-make-highlight 1 1 ebib-entry-buffer
))
1147 (setq ebib-strings-highlight
(ebib-make-highlight 1 1 ebib-strings-buffer
))
1148 (setq ebib-initialized t
))
1150 (defun ebib-create-buffers ()
1151 "Creates the buffers for Ebib."
1152 ;; first we create a buffer for multiline editing. this one does *not*
1153 ;; have a name beginning with a space, because undo-info is normally
1154 ;; present in an edit buffer.
1155 (setq ebib-multiline-buffer
(get-buffer-create "*Ebib-edit*"))
1156 (set-buffer ebib-multiline-buffer
)
1157 (ebib-multiline-edit-mode)
1158 ;; then we create a buffer to hold the fields of the current entry.
1159 (setq ebib-entry-buffer
(get-buffer-create " *Ebib-entry*"))
1160 (set-buffer ebib-entry-buffer
)
1162 ;; then we create a buffer to hold the @STRING definitions
1163 (setq ebib-strings-buffer
(get-buffer-create " *Ebib-strings*"))
1164 (set-buffer ebib-strings-buffer
)
1166 ;; then we create the help buffer
1167 (setq ebib-help-buffer
(get-buffer-create " *Ebib-help*"))
1168 (set-buffer ebib-help-buffer
)
1171 (setq ebib-log-buffer
(get-buffer-create " *Ebib-log*"))
1172 (set-buffer ebib-log-buffer
)
1174 (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")
1176 ;; and lastly we create a buffer for the entry keys.
1177 (setq ebib-index-buffer
(get-buffer-create " none"))
1178 (set-buffer ebib-index-buffer
)
1183 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1185 (when (if (ebib-modified-p)
1186 (yes-or-no-p "There are modified databases. Quit anyway? ")
1187 (y-or-n-p "Quit Ebib? "))
1188 (kill-buffer ebib-entry-buffer
)
1189 (kill-buffer ebib-index-buffer
)
1190 (kill-buffer ebib-strings-buffer
)
1191 (kill-buffer ebib-multiline-buffer
)
1192 (kill-buffer ebib-help-buffer
)
1193 (kill-buffer ebib-log-buffer
)
1194 (setq ebib-databases nil
1195 ebib-index-buffer nil
1196 ebib-entry-buffer nil
1197 ebib-initialized nil
1198 ebib-index-highlight nil
1199 ebib-fields-highlight nil
1200 ebib-strings-highlight nil
1201 ebib-export-filename nil
)
1202 (set-window-configuration ebib-saved-window-config
)
1205 (defun ebib-kill-emacs-query-function ()
1206 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1207 killed and the database has been modified."
1208 (if (not (ebib-modified-p))
1210 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1212 (ebib-save-all-databases)
1214 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1216 (add-hook 'kill-emacs-query-functions
'ebib-kill-emacs-query-function
)
1223 (define-prefix-command 'ebib-prefix-map
)
1224 (suppress-keymap ebib-prefix-map
)
1225 (defvar ebib-prefixed-functions
'(ebib-delete-entry
1230 ebib-push-entry-key
)))
1232 ;; macro to redefine key bindings.
1234 (defmacro ebib-key
(buffer key
&optional command
)
1237 (let ((one `(define-key ebib-index-mode-map
,key
(quote ,command
)))
1238 (two (when (or (null command
)
1239 (member command ebib-prefixed-functions
))
1240 `(define-key ebib-prefix-map
,key
(quote ,command
)))))
1245 `(define-key ebib-entry-mode-map
,key
(quote ,command
)))
1246 ((eq buffer
'strings
)
1247 `(define-key ebib-strings-mode-map
,key
(quote ,command
)))
1248 ((eq buffer
'mark-prefix
)
1250 (define-key ebib-index-mode-map
(format "%c" ebib-prefix-key
) nil
)
1251 (define-key ebib-index-mode-map
,key
'ebib-prefix-map
)
1252 (setq ebib-prefix-key
(string-to-char ,key
))))))
1254 (defvar ebib-index-mode-map
1255 (let ((map (make-keymap)))
1256 (suppress-keymap map
)
1258 "Keymap for the ebib index buffer.")
1260 ;; we define the keys with ebib-key rather than with define-key, because
1261 ;; that automatically sets up ebib-prefix-map as well.
1262 (ebib-key index
[up] ebib-prev-entry)
1263 (ebib-key index [down] ebib-next-entry)
1264 (ebib-key index [right] ebib-next-database)
1265 (ebib-key index [left] ebib-prev-database)
1266 (ebib-key index [prior] ebib-index-scroll-down)
1267 (ebib-key index [next] ebib-index-scroll-up)
1268 (ebib-key index [home] ebib-goto-first-entry)
1269 (ebib-key index [end] ebib-goto-last-entry)
1270 (ebib-key index [return] ebib-select-entry)
1271 (ebib-key index " " ebib-index-scroll-up)
1272 (ebib-key index "/" ebib-search)
1273 (ebib-key index "&" ebib-virtual-db-and)
1274 (ebib-key index "|" ebib-virtual-db-or)
1275 (ebib-key index "~" ebib-virtual-db-not)
1276 (ebib-key index ";" ebib-prefix-map)
1277 (ebib-key index "a" ebib-add-entry)
1278 (ebib-key index "b" ebib-index-scroll-down)
1279 (ebib-key index "c" ebib-close-database)
1280 (ebib-key index "C" ebib-customize)
1281 (ebib-key index "d" ebib-delete-entry)
1282 (ebib-key index "e" ebib-edit-entry)
1283 (ebib-key index "E" ebib-edit-keyname)
1284 (ebib-key index "f" ebib-print-filename)
1285 (ebib-key index "F" ebib-follow-crossref)
1286 (ebib-key index "g" ebib-goto-first-entry)
1287 (ebib-key index "G" ebib-goto-last-entry)
1288 (ebib-key index "h" ebib-index-help)
1289 (ebib-key index "H" ebib-toggle-hidden)
1290 (ebib-key index "j" ebib-next-entry)
1291 (ebib-key index "J" ebib-switch-to-database)
1292 (ebib-key index "k" ebib-prev-entry)
1293 (ebib-key index "l" ebib-show-log)
1294 (ebib-key index "L" ebib-latex-entries)
1295 (ebib-key index "m" ebib-mark-entry)
1296 (ebib-key index "M" ebib-merge-bibtex-file)
1297 (ebib-key index "n" ebib-search-next)
1298 (ebib-key index [(control n)] ebib-next-entry)
1299 (ebib-key index [(meta n)] ebib-index-scroll-up)
1300 (ebib-key index "o" ebib-load-bibtex-file)
1301 (ebib-key index "p" ebib-push-entry-key)
1302 (ebib-key index [(control p)] ebib-prev-entry)
1303 (ebib-key index [(meta p)] ebib-index-scroll-down)
1304 (ebib-key index "P" ebib-print-entries)
1305 (ebib-key index "r" ebib-edit-preamble)
1306 (ebib-key index "q" ebib-quit)
1307 (ebib-key index "s" ebib-save-current-database)
1308 (ebib-key index "S" ebib-save-all-databases)
1309 (ebib-key index "t" ebib-edit-strings)
1310 (ebib-key index "u" ebib-browse-url)
1311 (ebib-key index "V" ebib-print-filter)
1312 (ebib-key index "w" ebib-write-database)
1313 (ebib-key index "x" ebib-export-entry)
1314 (ebib-key index "\C-xb" ebib-lower)
1315 (ebib-key index "\C-xk" ebib-quit)
1316 (ebib-key index "X" ebib-export-preamble)
1317 (ebib-key index "z" ebib-lower)
1319 (defun ebib-switch-to-database-nth (key)
1320 (interactive (list (if (featurep 'xemacs)
1321 (event-key last-command-event)
1322 last-command-event)))
1323 (ebib-switch-to-database (- (if (featurep 'xemacs)
1327 (mapc #'(lambda (key)
1328 (define-key ebib-index-mode-map (format "%d" key)
1329 'ebib-switch-to-database-nth))
1330 '(1 2 3 4 5 6 7 8 9))
1332 (define-derived-mode ebib-index-mode
1333 fundamental-mode "Ebib-index"
1334 "Major mode for the Ebib index buffer."
1335 (setq buffer-read-only t)
1336 (setq truncate-lines t))
1338 (defun ebib-fill-index-buffer ()
1339 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1340 If EBIB-CUR-DB is nil, the buffer is just erased and its name set
1342 (set-buffer ebib-index-buffer)
1343 (let ((buffer-read-only nil))
1347 ;; we may call this function when there are no entries in the
1348 ;; database. if so, we don't need to do this:
1349 (when (edb-cur-entry ebib-cur-db)
1350 (mapc #'(lambda (entry)
1351 (ebib-display-entry entry)
1352 (when (member entry (edb-marked-entries ebib-cur-db))
1357 (skip-chars-forward "^ ")
1358 (add-text-properties beg (point) '(face ebib-marked-face))))))
1359 (edb-keys-list ebib-cur-db))
1360 (goto-char (point-min))
1361 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
1363 (ebib-set-index-highlight))
1364 (set-buffer-modified-p (edb-modified ebib-cur-db))
1365 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1366 (length (member ebib-cur-db ebib-databases)))))
1367 (edb-name ebib-cur-db))))
1368 (rename-buffer " none"))))
1370 (defun ebib-customize ()
1371 "Switches to Ebib's customisation group."
1374 (customize-group 'ebib))
1376 (defun ebib-log (type format-string &rest args)
1377 "Writes a message to Ebib's log buffer.
1378 TYPE (a symbol) is the type of message. It can be LOG, which
1379 writes the message to the log buffer only; MESSAGE, which writes
1380 the message to the log buffer and outputs it with the function
1381 MESSAGE; WARNING, which logs the message and sets the variable
1382 EBIB-LOG-ERROR to 0; or ERROR, which logs the message and sets
1383 the variable EBIB-LOG-ERROR to 1. The latter two can be used to
1384 signal the user to check the log for warnings or errors."
1386 (set-buffer ebib-log-buffer)
1389 (or ebib-log-error ; if ebib-error-log is already set to 1, we don't want to overwrite it!
1390 (setq ebib-log-error 0)))
1392 (setq ebib-log-error 1))
1394 (apply 'message format-string args)))
1395 (insert (apply 'format (concat format-string "\n") args))))
1397 (defun ebib-load-bibtex-file (&optional file)
1398 "Loads a BibTeX file into ebib."
1401 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1402 (setq ebib-cur-db (ebib-create-new-database))
1403 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1404 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1405 (setq ebib-log-error nil) ; we haven't found any errors
1406 (ebib-log 'log "%s: Opening file %s" (format-time-string "%d %b %Y, %H:%M:%S") (edb-filename ebib-cur-db))
1407 ;; first, we empty the buffers
1408 (ebib-erase-buffer ebib-index-buffer)
1409 (ebib-erase-buffer ebib-entry-buffer)
1410 (if (file-readable-p file)
1411 ;; if the user entered the name of an existing file, we load it
1412 ;; by putting it in a buffer and then parsing it.
1414 (with-syntax-table ebib-syntax-table
1415 (insert-file-contents file)
1416 ;; if the user makes any changes, we'll want to create a back-up.
1417 (setf (edb-make-backup ebib-cur-db) t)
1418 (let ((result (ebib-find-bibtex-entries nil)))
1419 (setf (edb-n-entries ebib-cur-db) (car result))
1420 (when (edb-keys-list ebib-cur-db)
1421 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1422 (when (edb-strings-list ebib-cur-db)
1423 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1424 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1425 ;; and fill the buffers. note that filling a buffer also makes
1426 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1428 (ebib-set-modified nil)
1429 (ebib-fill-entry-buffer)
1430 ;; and now we tell the user the result
1431 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1437 ;; if the file does not exist, we need to issue a message.
1438 (ebib-log 'message "(New file)"))
1439 ;; what we have to do in *any* case, is fill the index buffer. (this
1440 ;; even works if there are no keys in the database, e.g. when the
1441 ;; user opened a new file or if no BibTeX entries were found.
1442 (ebib-fill-index-buffer)
1443 (when ebib-log-error
1444 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1445 (ebib-log 'log "\n\f\n"))
1447 (defun ebib-merge-bibtex-file ()
1448 "Merges a BibTeX file into the database."
1450 (unless (edb-virtual ebib-cur-db)
1451 (if (not ebib-cur-db)
1452 (error "No database loaded. Use `o' to open a database")
1453 (let ((file (read-file-name "File to merge: ")))
1454 (setq ebib-log-error nil) ; we haven't found any errors
1455 (ebib-log 'log "%s: Merging file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") (edb-filename ebib-cur-db))
1457 (with-syntax-table ebib-syntax-table
1458 (insert-file-contents file)
1459 (let ((n (ebib-find-bibtex-entries t)))
1460 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1461 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1462 (when (edb-strings-list ebib-cur-db)
1463 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1464 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1465 (ebib-fill-entry-buffer)
1466 (ebib-fill-index-buffer)
1467 (ebib-set-modified t)
1468 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1474 (when ebib-log-error
1475 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1476 (ebib-log 'log "\n\f\n"))))))))
1478 (defun ebib-find-bibtex-entries (timestamp)
1479 "Finds the BibTeX entries in the current buffer.
1480 The search is started at the beginnig of the buffer. All entries
1481 found are stored in the hash table DATABASE of
1482 EBIB-CUR-DB. Returns a three-element list: the first element is
1483 the number of entries found, the second the number of @STRING
1484 definitions, and the third is T or NIL, indicating whether a
1485 @PREAMBLE was found.
1487 TIMESTAMP indicates whether a timestamp is to be added to each
1488 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1493 (goto-char (point-min))
1494 (while (re-search-forward "^@" nil t) ; find the next entry
1495 (let ((beg (point)))
1496 (when (looking-at-goto-end (concat ebib-bibtex-identifier "[\(\{]"))
1497 (let ((entry-type (downcase (buffer-substring-no-properties beg (1- (point))))))
1499 ((equal entry-type "string") ; string and preamble must be treated differently
1500 (if (ebib-read-string)
1501 (setq n-strings (1+ n-strings))))
1502 ((equal entry-type "preamble")
1503 (when (ebib-read-preamble)
1505 ((equal entry-type "comment") ; ignore comments
1506 (ebib-log 'log "Comment at line %d ignored" (line-number-at-pos))
1507 (ebib-match-paren-forward (point-max)))
1508 ((gethash (intern-soft entry-type) ebib-entry-types-hash) ; if the entry type has been defined
1509 (if (ebib-read-entry entry-type timestamp)
1510 (setq n-entries (1+ n-entries))))
1511 ;; anything else we report as an unknown entry type.
1512 (t (ebib-log 'warning "Line %d: Unknown entry type `%s'. Skipping." (line-number-at-pos) entry-type)
1513 (ebib-match-paren-forward (point-max))))))))
1514 (list n-entries n-strings preamble)))
1516 (defun ebib-read-string ()
1517 "Reads the @STRING definition beginning at the line POINT is on.
1518 If a proper abbreviation and string are found, they are stored in the
1519 database. Returns the string if one was read, nil otherwise."
1520 (let ((limit (save-excursion ; we find the matching end parenthesis
1522 (ebib-match-paren-forward (point-max))
1524 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1525 (let ((beg (point)))
1526 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1527 (if-str (abbr (buffer-substring-no-properties beg (point)))
1529 (skip-chars-forward "^\"{" limit)
1530 (let ((beg (point)))
1531 (if-str (string (if (ebib-match-delim-forward limit)
1532 (buffer-substring-no-properties beg (1+ (point)))
1534 (if (member abbr (edb-strings-list ebib-cur-db))
1535 (ebib-log 'warning (format "Line %d: @STRING definition `%s' duplicated. Skipping."
1536 (line-number-at-pos) abbr))
1537 (ebib-insert-string abbr string ebib-cur-db))))))))))
1539 (defun ebib-read-preamble ()
1540 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1541 If there was already another @PREAMBLE definition, the new one is
1542 added to the existing one with a hash sign `#' between them."
1543 (let ((beg (point)))
1545 (when (ebib-match-paren-forward (point-max))
1546 (let ((text (buffer-substring-no-properties beg (point))))
1547 (if (edb-preamble ebib-cur-db)
1548 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1549 (setf (edb-preamble ebib-cur-db) text))))))
1551 (defun ebib-read-entry (entry-type &optional timestamp)
1552 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1553 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1554 otherwise. Optional argument TIMESTAMP indicates whether a
1555 timestamp is to be added. (Whether a timestamp is actually added,
1556 also depends on EBIB-USE-TIMESTAMP.)"
1557 (let ((entry-limit (save-excursion
1559 (ebib-match-paren-forward (point-max))
1562 (skip-chars-forward " \n\t\f") ; note the space!
1564 (when (looking-at-goto-end (concat "\\("
1565 ebib-bibtex-identifier
1567 1) ; this delimits the entry key
1568 (let ((entry-key (buffer-substring-no-properties beg (point))))
1569 (if (member entry-key (edb-keys-list ebib-cur-db))
1570 (ebib-log 'warning "Line %d: Entry `%s' duplicated. Skipping." (line-number-at-pos) entry-key)
1571 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1572 (when fields ; if fields were found, we store them, and return T.
1573 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1576 (defun ebib-find-bibtex-fields (entry-type limit)
1577 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1578 Returns a hash table containing all the fields and values, or NIL
1579 if none were found. ENTRY-TYPE is the type of the entry, which
1580 will be recorded in the hash table. Before the search starts,
1581 POINT is moved back to the beginning of the line."
1583 ;; we assign a function to fn in order to avoid putting the test on
1584 ;; ebib-allow-identical-fields in the while loop, where it would get
1585 ;; tested with every field being read.
1586 (let ((fn (if (not ebib-allow-identical-fields)
1587 (symbol-function 'puthash)
1588 #'(lambda (field-type field-contents fields)
1589 (let ((existing-contents (gethash field-type fields)))
1590 (puthash field-type (if existing-contents
1591 (from-raw (concat (to-raw existing-contents)
1593 (to-raw field-contents)))
1596 (let ((fields (make-hash-table :size 15)))
1598 (skip-chars-forward "^," limit) ; we must move to the next comma,
1599 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1600 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1601 (let ((beg (point)))
1602 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1603 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1604 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1605 (let ((field-contents (ebib-get-field-contents limit)))
1606 (when field-contents
1607 (funcall fn field-type field-contents fields))))))))
1608 (when (> (hash-table-count fields) 0)
1609 (puthash 'type* entry-type fields)
1612 (defun ebib-get-field-contents (limit)
1613 "Gets the contents of a BibTeX field.
1614 LIMIT indicates the end of the entry, beyond which the function will not
1616 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1617 (let ((beg (point)))
1618 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1620 (defun ebib-find-end-of-field (limit)
1621 "Moves POINT to the end of a field's contents and returns POINT.
1622 The contents of a field is delimited by a comma or by the closing brace of
1623 the entry. The latter is at position LIMIT."
1624 (while (and (not (eq (char-after) ?\,))
1626 (ebib-match-delim-forward limit) ; check if we're on a delimiter and if so, jump to the matching closing delimiter
1628 (if (= (point) limit)
1629 (skip-chars-backward " \n\t\f"))
1632 (defun ebib-lower ()
1633 "Hides the Ebib buffers, but does not delete them."
1635 (if (nor (equal (window-buffer) ebib-index-buffer)
1636 (equal (window-buffer) ebib-entry-buffer)
1637 (equal (window-buffer) ebib-strings-buffer)
1638 (equal (window-buffer) ebib-multiline-buffer)
1639 (equal (window-buffer) ebib-help-buffer)
1640 (equal (window-buffer) ebib-log-buffer))
1641 (error "Ebib is not active ")
1642 (set-window-configuration ebib-saved-window-config)
1643 (bury-buffer ebib-entry-buffer)
1644 (bury-buffer ebib-index-buffer)
1645 (bury-buffer ebib-multiline-buffer)
1646 (bury-buffer ebib-strings-buffer)
1647 (bury-buffer ebib-help-buffer)
1648 (bury-buffer ebib-log-buffer)))
1650 (defun ebib-prev-entry ()
1651 "Moves to the previous BibTeX entry."
1655 ;; if the current entry is the first entry,
1656 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1658 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1659 (1+ (length (edb-cur-entry ebib-cur-db)))))
1660 (goto-char (ebib-highlight-start ebib-index-highlight))
1662 (ebib-set-index-highlight)
1663 (ebib-fill-entry-buffer)))
1667 (defun ebib-next-entry ()
1668 "Moves to the next BibTeX entry."
1672 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1674 (setf (edb-cur-entry ebib-cur-db)
1675 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1676 (goto-char (ebib-highlight-start ebib-index-highlight))
1678 (ebib-set-index-highlight)
1679 (ebib-fill-entry-buffer)))
1683 (defun ebib-add-entry ()
1684 "Adds a new entry to the database."
1688 (if-str (entry-key (read-string "New entry key: "))
1690 (if (member entry-key (edb-keys-list ebib-cur-db))
1691 (error "Key already exists")
1692 (set-buffer ebib-index-buffer)
1693 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1694 (with-buffer-writable
1695 (insert (format "%s \n" entry-key))) ; add the entry in the buffer.
1696 (forward-line -1) ; move one line up to position the cursor on the new entry.
1697 (ebib-set-index-highlight)
1698 (let ((fields (make-hash-table)))
1699 (puthash 'type* ebib-default-type fields)
1700 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1701 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1702 (ebib-fill-entry-buffer)
1704 (ebib-set-modified t)))))
1706 (error "No database open. Use `o' to open a database first"))
1710 (defun ebib-close-database ()
1711 "Closes the current BibTeX database."
1715 (when (if (edb-modified ebib-cur-db)
1716 (yes-or-no-p "Database modified. Close it anyway? ")
1717 (y-or-n-p "Close database? "))
1718 (let ((to-be-deleted ebib-cur-db)
1719 (new-db (next-elem ebib-cur-db ebib-databases)))
1720 (setq ebib-databases (delete to-be-deleted ebib-databases))
1721 (if ebib-databases ; do we still have another database loaded?
1723 (setq ebib-cur-db (or new-db
1724 (last1 ebib-databases)))
1725 (unless (edb-cur-entry ebib-cur-db)
1726 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1727 (ebib-fill-entry-buffer)
1728 (ebib-fill-index-buffer))
1729 ;; otherwise, we have to clean up a little and empty all the buffers.
1730 (setq ebib-cur-db nil)
1731 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1732 (set-buffer (car buf))
1733 (with-buffer-writable
1735 (ebib-delete-highlight (cadr buf)))
1736 (list (list ebib-entry-buffer ebib-fields-highlight)
1737 (list ebib-index-buffer ebib-index-highlight)
1738 (list ebib-strings-buffer ebib-strings-highlight)))
1739 ;; multiline edit buffer
1740 (set-buffer ebib-multiline-buffer)
1741 (with-buffer-writable
1743 (set-buffer ebib-index-buffer)
1744 (rename-buffer " none"))
1745 (message "Database closed."))))))
1747 (defun ebib-goto-first-entry ()
1748 "Moves to the first BibTeX entry in the database."
1752 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1753 (set-buffer ebib-index-buffer)
1754 (goto-char (point-min))
1755 (ebib-set-index-highlight)
1756 (ebib-fill-entry-buffer))
1760 (defun ebib-goto-last-entry ()
1761 "Moves to the last entry in the BibTeX database."
1765 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
1766 (set-buffer ebib-index-buffer)
1767 (goto-line (edb-n-entries ebib-cur-db))
1768 (ebib-set-index-highlight)
1769 (ebib-fill-entry-buffer))
1773 (defun ebib-edit-entry ()
1774 "Edits the current BibTeX entry."
1778 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1779 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
1781 (switch-to-buffer ebib-entry-buffer)
1782 (goto-char (ebib-highlight-end ebib-fields-highlight)))
1786 (defun ebib-edit-keyname ()
1787 "Change the key of a BibTeX entry."
1791 (let ((cur-keyname (ebib-cur-entry-key)))
1792 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
1794 (if (member new-keyname (edb-keys-list ebib-cur-db))
1795 (error (format "Key `%s' already exists" new-keyname))
1796 (unless (string= cur-keyname new-keyname)
1797 (let ((fields (ebib-retrieve-entry cur-keyname ebib-cur-db))
1798 (marked (member cur-keyname (edb-marked-entries ebib-cur-db))))
1799 (ebib-remove-entry-from-db cur-keyname ebib-cur-db)
1800 (ebib-remove-key-from-buffer cur-keyname)
1801 (ebib-insert-entry new-keyname fields ebib-cur-db t nil)
1802 (setf (edb-cur-entry ebib-cur-db) (member new-keyname (edb-keys-list ebib-cur-db)))
1803 (sort-in-buffer (edb-n-entries ebib-cur-db) new-keyname)
1804 (with-buffer-writable
1805 (ebib-display-entry new-keyname))
1806 (forward-line -1) ; move one line up to position the cursor on the new entry.
1807 (ebib-set-index-highlight)
1808 (ebib-set-modified t)
1809 (when marked (ebib-mark-entry))))))))
1813 (defun ebib-mark-entry ()
1814 "Mark (or unmark) the current entry."
1816 (if (ebib-called-with-prefix)
1819 (setf (edb-marked-entries ebib-cur-db) nil)
1820 (ebib-fill-index-buffer))
1822 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
1823 (ebib-fill-index-buffer))
1828 (set-buffer ebib-index-buffer)
1829 (with-buffer-writable
1830 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
1832 (setf (edb-marked-entries ebib-cur-db)
1833 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
1834 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
1835 (ebib-highlight-end ebib-index-highlight)
1836 '(face ebib-marked-face)))
1837 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
1838 (edb-marked-entries ebib-cur-db))
1840 (add-text-properties (ebib-highlight-start ebib-index-highlight)
1841 (ebib-highlight-end ebib-index-highlight)
1842 '(face ebib-marked-face)))))
1846 (defun ebib-index-scroll-down ()
1847 "Move one page up in the database."
1852 (ebib-select-entry))
1856 (defun ebib-index-scroll-up ()
1857 "Move one page down in the database."
1862 (ebib-select-entry))
1866 (defun ebib-format-entry (key db timestamp)
1867 "Format entry KEY from database DB into the current buffer in BibTeX format.
1868 If TIMESTAMP is T, a timestamp is added to the entry if
1869 EBIB-USE-TIMESTAMP is T."
1870 (let ((entry (ebib-retrieve-entry key db)))
1872 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
1873 (maphash #'(lambda (key value)
1874 (unless (or (eq key 'type*)
1875 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
1876 (insert (format "\t%s = %s,\n" key value))))
1878 (if (and timestamp ebib-use-timestamp)
1879 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
1880 (delete-char -2)) ; the final ",\n" must be deleted
1881 (insert "\n}\n\n"))))
1883 (defun ebib-format-strings (db)
1884 "Format the @STRING commands in database DB."
1885 (maphash #'(lambda (key value)
1886 (insert (format "@STRING{%s = %s}\n" key value)))
1890 (defun ebib-compare-xrefs (x y)
1891 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
1893 (defun ebib-format-database (db)
1894 "Writes database DB into the current buffer in BibTeX format."
1895 (when (edb-preamble db)
1896 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
1897 (ebib-format-strings db)
1898 (let ((sorted-list (copy-list (edb-keys-list db))))
1900 (ebib-save-xrefs-first
1901 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
1903 (setq sorted-list (sort sorted-list 'ebib-entry<))))
1904 (mapc #'(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
1906 (defun ebib-save-database (db)
1907 "Saves the database DB."
1910 (when (and (edb-make-backup db)
1911 (file-exists-p (edb-filename db)))
1912 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
1913 (setf (edb-make-backup db) nil))
1915 (ebib-format-database db)
1916 (write-region (point-min) (point-max) (edb-filename db)))
1917 (ebib-set-modified nil db))))
1919 (defun ebib-write-database ()
1920 "Writes the current database to a different file.
1921 Can also be used to change a virtual database into a real one."
1925 (if-str (new-filename (read-file-name "Save to file: " "~/"))
1928 (ebib-format-database ebib-cur-db)
1929 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
1930 ;; if SAFE-WRITE-REGION was cancelled by the user because he
1931 ;; didn't want to overwrite an already existing file with his
1932 ;; new database, it throws an error, so the next lines will not
1933 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
1935 (setf (edb-filename ebib-cur-db) new-filename)
1936 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
1937 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1938 (length (member ebib-cur-db ebib-databases)))))
1939 (edb-name ebib-cur-db)))
1942 (setf (edb-virtual ebib-cur-db) nil)
1943 (setf (edb-database ebib-cur-db)
1944 (let ((new-db (make-hash-table :test 'equal)))
1945 (mapc #'(lambda (key)
1946 (let ((entry (gethash key (edb-database ebib-cur-db))))
1948 (puthash key (copy-hash-table entry) new-db))))
1949 (edb-keys-list ebib-cur-db))
1951 (ebib-set-modified nil))))
1955 (defun ebib-save-current-database ()
1956 "Saves the current database."
1960 (if (not (edb-modified ebib-cur-db))
1961 (message "No changes need to be saved.")
1962 (ebib-save-database ebib-cur-db)))
1964 (error "Cannot save a virtual database. Use `w' to write to a file."))))
1966 (defun ebib-save-all-databases ()
1967 "Saves all currently open databases if they were modified."
1971 (mapc #'(lambda (db)
1972 (when (edb-modified db)
1973 (ebib-save-database db)))
1975 (message "All databases saved."))))
1977 (defun ebib-print-filename ()
1978 "Displays the filename of the current database in the minibuffer."
1980 (message (edb-filename ebib-cur-db)))
1982 (defun ebib-follow-crossref ()
1983 "Goes to the entry mentioned in the crossref field of the current entry."
1985 (let ((new-cur-entry (to-raw (gethash 'crossref
1986 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
1987 (setf (edb-cur-entry ebib-cur-db)
1988 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
1989 (edb-cur-entry ebib-cur-db))))
1990 (ebib-fill-entry-buffer)
1991 (ebib-fill-index-buffer))
1993 (defun ebib-toggle-hidden ()
1995 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
1996 (ebib-fill-entry-buffer))
1998 (defun ebib-delete-entry ()
1999 "Deletes the current entry from the database."
2001 (if (ebib-called-with-prefix)
2003 ((real-db marked-entries)
2004 (when (y-or-n-p "Delete all marked entries? ")
2005 (mapc #'(lambda (entry)
2006 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
2007 (edb-marked-entries ebib-cur-db))
2008 (message "Marked entries deleted.")
2009 (ebib-set-modified t)
2010 (ebib-fill-entry-buffer)
2011 (ebib-fill-index-buffer)))
2016 (let ((cur-entry (ebib-cur-entry-key)))
2017 (when (y-or-n-p (format "Delete %s? " cur-entry))
2018 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
2019 (ebib-remove-key-from-buffer cur-entry)
2020 (ebib-fill-entry-buffer)
2021 (ebib-set-modified t)
2022 (message (format "Entry `%s' deleted." cur-entry)))))
2026 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
2027 "Removes ENTRY-KEY from DB.
2028 Optional argument NEW-CUR-ENTRY is the key of the entry that is
2029 to become the new current entry. It it is NIL, the entry after
2030 the deleted one becomes the new current entry. If it is T, the
2031 current entry is not changed."
2032 (remhash entry-key (edb-database db))
2033 (setf (edb-n-entries db) (1- (edb-n-entries db)))
2035 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
2036 ((stringp new-cur-entry) t)
2037 (t (setq new-cur-entry (ebib-cur-entry-key))))
2038 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
2039 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
2040 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
2041 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
2042 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
2044 (defun ebib-remove-key-from-buffer (entry-key)
2045 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
2046 (with-buffer-writable
2047 (let ((beg (ebib-search-key-in-buffer entry-key)))
2049 (delete-region beg (point))))
2052 (ebib-search-key-in-buffer (ebib-cur-entry-key))
2053 (ebib-set-index-highlight))))
2055 (defun ebib-select-entry ()
2056 "Makes the entry at (point) the current entry."
2061 (let ((beg (point)))
2062 (let* ((key (save-excursion
2063 (skip-chars-forward "^ ")
2064 (buffer-substring-no-properties beg (point))))
2065 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
2067 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
2068 (ebib-set-index-highlight)
2069 (ebib-fill-entry-buffer)))))
2073 (defun ebib-export-entry (prefix)
2074 "Copies entries to another database.
2075 The prefix argument indicates which database to copy the entry
2076 to. If no prefix argument is present, a filename is asked to
2077 which the entry is appended."
2079 (let ((num (ebib-prefix prefix)))
2080 (if (ebib-called-with-prefix)
2081 (ebib-export-marked-entries num)
2082 (ebib-export-single-entry num))))
2084 (defun ebib-export-single-entry (num)
2085 "Copies the current entry to another database.
2086 NUM indicates which database to copy the entry to. If it is NIL,
2087 a filename is asked to which the entry is appended."
2091 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
2093 (let ((entry-key (ebib-cur-entry-key)))
2094 (if (member entry-key (edb-keys-list db))
2095 (error "Entry key `%s' already exists in database %d" entry-key num)
2096 (ebib-insert-entry entry-key
2097 (copy-hash-table (ebib-retrieve-entry entry-key
2100 ;; if this is the first entry in the target DB,
2101 ;; its CUR-ENTRY must be set!
2102 (when (null (edb-cur-entry db))
2103 (setf (edb-cur-entry db) (edb-keys-list db)))
2104 t)))) ; we must return T, WHEN does not always do this.
2105 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2106 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2109 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2113 (defun ebib-export-marked-entries (num)
2114 "Copies the marked entries to another database.
2115 NUM indicates which database to copy the entry to. If it is NIL,
2116 a filename is asked to which the entry is appended."
2118 ((real-db marked-entries)
2121 num "Entries copied to database %d."
2123 (mapc #'(lambda (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 (edb-marked-entries ebib-cur-db))
2131 ;; if the target DB was empty before, 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 "Export to file: " "Entries exported to %s."
2137 (mapc #'(lambda (entry-key)
2139 (ebib-format-entry entry-key ebib-cur-db t))
2140 (edb-marked-entries ebib-cur-db))))))
2144 (defun ebib-search ()
2145 "Search the current Ebib database.
2146 The search is conducted with STRING-MATCH and can therefore be a
2147 regexp. Searching starts with the current entry."
2151 (if-str (search-str (read-string "Search database for: "))
2153 (setq ebib-search-string search-str)
2154 ;; first we search the current entry
2155 (if (ebib-search-in-entry ebib-search-string
2156 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2157 (ebib-fill-entry-buffer ebib-search-string)
2158 ;; if the search string wasn't found in the current entry, we continue searching.
2159 (ebib-search-next)))))
2163 (defun ebib-search-next ()
2164 "Search the next occurrence of EBIB-SEARCH-STRING.
2165 Searching starts at the entry following the current entry. If a
2166 match is found, the matching entry is shown and becomes the new
2171 (if (null ebib-search-string)
2172 (message "No search string")
2173 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2174 (while (and cur-search-entry
2175 (null (ebib-search-in-entry ebib-search-string
2176 (gethash (car cur-search-entry)
2177 (edb-database ebib-cur-db)))))
2178 (setq cur-search-entry (cdr cur-search-entry)))
2179 (if (null cur-search-entry)
2180 (message (format "`%s' not found" ebib-search-string))
2181 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2182 (set-buffer ebib-index-buffer)
2183 (goto-char (point-min))
2184 (re-search-forward (format "^%s$" (ebib-cur-entry-key)))
2186 (ebib-set-index-highlight)
2187 (ebib-fill-entry-buffer ebib-search-string)))))
2191 (defun ebib-search-in-entry (search-str entry &optional field)
2192 "Searches one entry of the ebib database.
2193 Returns a list of fields in ENTRY that match the regexp
2194 SEARCH-STR, or NIL if no matches were found. If FIELD is given,
2195 only that field is searched."
2196 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2199 (let ((value (gethash field entry)))
2200 (when (and (stringp value) ; the type* field has a symbol as value
2201 (string-match search-str value))
2202 (setq result (list field))))
2203 (maphash #'(lambda (field value)
2204 (when (and (stringp value) ; the type* field has a symbol as value
2205 (string-match search-str value))
2206 (setq result (cons field result))))
2210 (defun ebib-edit-strings ()
2211 "Edits the @STRING definitions in the database."
2215 (ebib-fill-strings-buffer)
2217 (switch-to-buffer ebib-strings-buffer)
2218 (goto-char (point-min)))
2222 (defun ebib-edit-preamble ()
2223 "Edits the @PREAMBLE definition in the database."
2227 (other-window 1) ; we want the multiline edit buffer to appear in the lower window
2228 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2232 (defun ebib-export-preamble (prefix)
2233 "Export the @PREAMBLE definition.
2234 If a prefix argument is given, it is taken as the database to
2235 export the preamble to. If the goal database already has a
2236 preamble, the new preamble will be appended to it. If no prefix
2237 argument is given, the user is asked to enter a filename to which
2238 the preamble is appended."
2242 (if (null (edb-preamble ebib-cur-db))
2243 (error "No @PREAMBLE defined")
2244 (let ((num (ebib-prefix prefix)))
2246 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2248 (let ((text (edb-preamble ebib-cur-db)))
2249 (if (edb-preamble db)
2250 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2251 (setf (edb-preamble db) text)))))
2252 (ebib-export-to-file "Export @PREAMBLE to file: "
2253 "@PREAMBLE exported to %s"
2255 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2259 (defun ebib-print-entries ()
2260 "Creates a LaTeX file listing the entries.
2261 Either prints the entire database, or the marked entries."
2265 (let ((entries (if (ebib-called-with-prefix)
2266 (edb-marked-entries ebib-cur-db)
2267 (edb-keys-list ebib-cur-db))))
2268 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2270 (read-file-name "Use temp file: " "~/" nil nil)))
2273 (insert "\\documentclass{article}\n\n")
2274 (when ebib-print-preamble
2275 (mapc #'(lambda (string)
2276 (insert (format "%s\n" string)))
2277 ebib-print-preamble))
2278 (insert "\n\\begin{document}\n\n")
2279 (mapc #'(lambda (entry-key)
2280 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2281 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2282 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2283 entry-key (symbol-name (gethash 'type* entry))))
2284 (insert "\\hline\n")
2285 (mapc #'(lambda (field)
2286 (if-str (value (gethash field entry))
2287 (when (or (not (multiline-p value))
2288 ebib-print-multiline)
2289 (insert (format "%s: & %s\\\\\n"
2290 field (to-raw value))))))
2291 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2292 (insert "\\end{tabular}\n\n")
2293 (insert "\\bigskip\n\n"))
2295 (insert "\\end{document}\n")
2296 (write-region (point-min) (point-max) tempfile))
2298 (find-file tempfile)))))
2302 (defun ebib-latex-entries ()
2303 "Creates a LaTeX file that \\nocite's entries from the database.
2304 Operates either on all entries or on the marked entries."
2308 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2310 (read-file-name "Use temp file: " "~/" nil nil)))
2313 (insert "\\documentclass{article}\n\n")
2314 (when ebib-print-preamble
2315 (mapc #'(lambda (string)
2316 (insert (format "%s\n" string)))
2317 ebib-latex-preamble))
2318 (insert "\n\\begin{document}\n\n")
2319 (if (ebib-called-with-prefix)
2320 (mapc #'(lambda (entry)
2321 (insert (format "\\nocite{%s}\n" entry)))
2322 (edb-marked-entries ebib-cur-db))
2323 (insert "\\nocite{*}\n"))
2324 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2325 (insert "\\end{document}\n")
2326 (write-region (point-min) (point-max) tempfile))
2328 (find-file tempfile))))
2332 (defun ebib-switch-to-database (num)
2333 (interactive "NSwitch to database number: ")
2334 (let ((new-db (nth (1- num) ebib-databases)))
2337 (setq ebib-cur-db new-db)
2338 (ebib-fill-entry-buffer)
2339 (ebib-fill-index-buffer))
2340 (error "Database %d does not exist" num))))
2342 (defun ebib-next-database ()
2346 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2348 (setq new-db (car ebib-databases)))
2349 (setq ebib-cur-db new-db)
2350 (ebib-fill-entry-buffer)
2351 (ebib-fill-index-buffer)))))
2353 (defun ebib-prev-database ()
2357 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2359 (setq new-db (last1 ebib-databases)))
2360 (setq ebib-cur-db new-db)
2361 (ebib-fill-entry-buffer)
2362 (ebib-fill-index-buffer)))))
2364 (defun ebib-browse-url (num)
2365 "Ask a browser to load the URL in the standard URL field.
2366 The standard URL field may contain more than one URL, if they're
2367 whitespace-separated. In that case, a numeric prefix argument
2368 specifies which URL to choose.
2370 By \"standard URL field\" is meant the field defined in the
2371 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2376 (let ((url (to-raw (gethash ebib-standard-url-field
2377 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2379 (ebib-call-browser url num)))
2380 (error "No url found in field `%s'" ebib-standard-url-field))))
2384 (defun ebib-call-browser (urls n)
2385 "Passes the Nth url in URLS to a browser.
2386 URLs must be a string of whitespace-separated urls."
2387 (let ((url (nth (1- n)
2390 (while (string-match ebib-url-regexp urls start)
2391 (add-to-list 'result (match-string 0 urls) t)
2392 (setq start (match-end 0)))
2395 ((string-match "\\\\url{\\(.*?\\)}" url)
2396 (setq url (match-string 1 url)))
2397 ((string-match ebib-url-regexp url)
2398 (setq url (match-string 0 url)))
2401 (if (not (string= ebib-browser-command ""))
2402 (start-process "Ebib-browser" nil ebib-browser-command url)
2403 (browse-url url)))))
2405 (defun ebib-virtual-db-and (not)
2406 "Filter entries into a virtual database.
2407 If the current database is a virtual database already, perform a
2408 logical AND on the entries."
2412 (ebib-filter-to-virtual-db 'and not))
2416 (defun ebib-virtual-db-or (not)
2417 "Filter entries into a virtual database.
2418 If the current database is a virtual database already, perform a
2419 logical OR with the entries in the original database."
2423 (ebib-filter-to-virtual-db 'or not))
2427 (defun ebib-virtual-db-not ()
2428 "Negates the current virtual database."
2432 (setf (edb-virtual ebib-cur-db)
2433 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2434 (cadr (edb-virtual ebib-cur-db))
2435 `(not ,(edb-virtual ebib-cur-db))))
2436 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2437 (ebib-fill-entry-buffer)
2438 (ebib-fill-index-buffer))
2442 (defun ebib-filter-to-virtual-db (bool not)
2443 "Filters the current database to a virtual database.
2444 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2445 a logical `not' is applied to the selection."
2446 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2447 (if (< not 0) "(not " "")
2448 (if (< not 0) ")" ""))
2450 (mapcar #'(lambda (x)
2451 (cons (symbol-name x) 0))
2452 (append ebib-unique-field-list ebib-additional-fields)))
2454 (setq field (intern-soft field))
2455 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2456 (if (< not 0) "(not " "")
2458 (if (< not 0) ")" "")))))
2461 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2463 `(contains ,field ,regexp)
2464 `(not (contains ,field ,regexp))))))
2466 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2467 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2468 (ebib-fill-entry-buffer)
2469 (ebib-fill-index-buffer))))
2471 (defun ebib-create-virtual-db (field regexp not)
2472 "Creates a virtual database based on EBIB-CUR-DB."
2473 ;; a virtual database is a database whose edb-virtual field contains an
2474 ;; expression that selects entries. this function only sets that
2475 ;; expression, it does not actually filter the entries.
2476 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2477 (setf (edb-virtual new-db) (if (>= not 0)
2478 `(contains ,field ,regexp)
2479 `(not (contains ,field ,regexp))))
2480 (setf (edb-filename new-db) nil)
2481 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2482 (setf (edb-modified new-db) nil)
2483 (setf (edb-make-backup new-db) nil)
2486 (defmacro contains (field regexp)
2487 ;; This is a hack: CONTAINS depends on the variable ENTRY being set to an
2488 ;; actual Ebib entry for its operation. The point of this macro is to
2489 ;; facilitate defining filters for virtual databases. It enables us to
2490 ;; define filters of the form:
2492 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2494 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2496 (defun ebib-run-filter (filter db)
2498 (setf (edb-keys-list db)
2499 (sort (let ((result nil))
2500 (maphash #'(lambda (key value)
2501 (let ((entry value)) ; this is necessary for actually running the filter
2503 (setq result (cons key result)))))
2507 (setf (edb-n-entries db) (length (edb-keys-list db)))
2508 (setf (edb-cur-entry db) (edb-keys-list db)))
2510 (defun ebib-print-filter (num)
2511 "Display the filter of the current virtual database.
2512 With any prefix argument, reapplies the filter to the
2513 database. This can be useful when the source database was
2519 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2520 (ebib-fill-entry-buffer)
2521 (ebib-fill-index-buffer))
2522 (message "%S" (edb-virtual ebib-cur-db)))
2526 (defun ebib-index-help ()
2527 "Displays the help message for the index buffer."
2530 (ebib-display-help ebib-index-buffer))
2532 (defun ebib-show-log ()
2533 "Display the contents of the log buffer."
2536 (switch-to-buffer ebib-log-buffer))
2538 (defun ebib-push-entry-key (prefix)
2539 "Pushes the current entry to a LaTeX buffer.
2540 The user is prompted for the buffer to push the entry into."
2542 (let ((called-with-prefix (ebib-called-with-prefix)))
2545 (let ((buffer (read-buffer "Push entry(ies) to buffer: " ebib-push-buffer t)))
2547 (setq ebib-push-buffer buffer)
2548 (let ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
2550 (latex-arg (if called-with-prefix
2551 (when (edb-marked-entries ebib-cur-db)
2552 (mapconcat #'(lambda (x) x)
2553 (edb-marked-entries ebib-cur-db)
2555 (car (edb-cur-entry ebib-cur-db)))))
2558 (insert (format latex-cmd latex-arg)))
2559 (message "Pushed entries to buffer %s" buffer)))))
2567 (defvar ebib-entry-mode-map
2568 (let ((map (make-keymap)))
2569 (suppress-keymap map)
2570 (define-key map [up] 'ebib-prev-field
)
2571 (define-key map
[down] 'ebib-next-field)
2572 (define-key map [prior] 'ebib-goto-prev-set)
2573 (define-key map [next] 'ebib-goto-next-set)
2574 (define-key map [home] 'ebib-goto-first-field)
2575 (define-key map [end] 'ebib-goto-last-field)
2576 (define-key map " " 'ebib-goto-next-set)
2577 (define-key map "b" 'ebib-goto-prev-set)
2578 (define-key map "c" 'ebib-copy-field-contents)
2579 (define-key map "d" 'ebib-delete-field-contents)
2580 (define-key map "e" 'ebib-edit-field)
2581 (define-key map "g" 'ebib-goto-first-field)
2582 (define-key map "G" 'ebib-goto-last-field)
2583 (define-key map "h" 'ebib-entry-help)
2584 (define-key map "j" 'ebib-next-field)
2585 (define-key map "k" 'ebib-prev-field)
2586 (define-key map "l" 'ebib-edit-multiline-field)
2587 (define-key map [(control n)] 'ebib-next-field)
2588 (define-key map [(meta n)] 'ebib-goto-prev-set)
2589 (define-key map [(control p)] 'ebib-prev-field)
2590 (define-key map [(meta p)] 'ebib-goto-next-set)
2591 (define-key map "q" 'ebib-quit-entry-buffer)
2592 (define-key map "r" 'ebib-toggle-raw)
2593 (define-key map "s" 'ebib-insert-abbreviation)
2594 (define-key map "u" 'ebib-browse-url-in-field)
2595 (define-key map "x" 'ebib-cut-field-contents)
2596 (define-key map "\C-xb" 'undefined)
2597 (define-key map "\C-xk" 'undefined)
2598 (define-key map "y" 'ebib-yank-field-contents)
2600 "Keymap for the Ebib entry buffer.")
2602 (define-derived-mode ebib-entry-mode
2603 fundamental-mode "Ebib-entry"
2604 "Major mode for the Ebib entry buffer."
2605 (setq buffer-read-only t)
2606 (setq truncate-lines t))
2608 (defun ebib-quit-entry-buffer ()
2609 "Quit editing the entry."
2613 (defun ebib-find-visible-field (field direction)
2614 "Finds the first visible field before or after FIELD.
2615 If DIRECTION is negative, search the preceding fields, otherwise
2616 search the succeeding fields. If FIELD is visible itself, return
2617 that. If there is no preceding/following visible field, return
2618 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
2619 (when ebib-hide-hidden-fields
2620 (let ((fn (if (>= direction 0)
2624 (get field 'ebib-hidden))
2625 (setq field (funcall fn field ebib-cur-entry-fields)))))
2628 (defun ebib-prev-field ()
2629 "Move to the previous field."
2631 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
2632 (if (null new-field)
2634 (setq ebib-current-field new-field)
2635 (ebib-move-to-field ebib-current-field -1))))
2637 (defun ebib-next-field ()
2638 "Move to the next field."
2640 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
2641 (if (null new-field)
2642 (when (interactive-p) ; i call this function after editing a field,
2643 ; and we don't want a beep then
2645 (setq ebib-current-field new-field)
2646 (ebib-move-to-field ebib-current-field 1))))
2648 (defun ebib-goto-first-field ()
2649 "Move to the first field."
2651 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
2652 (if (null new-field)
2654 (setq ebib-current-field new-field)
2655 (ebib-move-to-field ebib-current-field -1))))
2657 (defun ebib-goto-last-field ()
2658 "Move to the last field."
2660 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
2661 (if (null new-field)
2663 (setq ebib-current-field new-field)
2664 (ebib-move-to-field ebib-current-field 1))))
2666 (defun ebib-goto-next-set ()
2667 "Move to the next set of fields."
2670 ((eq ebib-current-field 'type*) (ebib-next-field))
2671 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
2672 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2673 (obl-fields (ebib-get-obl-fields entry-type))
2674 (opt-fields (ebib-get-opt-fields entry-type))
2676 (when (member ebib-current-field obl-fields)
2677 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
2678 ;; new-field is nil if there are no opt-fields
2679 (when (or (member ebib-current-field opt-fields)
2681 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
2682 (if (null new-field)
2683 (ebib-goto-last-field) ; if there was no further set to go to,
2684 ; go to the last field of the current set
2685 (setq ebib-current-field new-field)
2686 (ebib-move-to-field ebib-current-field 1))))))
2688 (defun ebib-goto-prev-set ()
2689 "Move to the previous set of fields."
2691 (unless (eq ebib-current-field 'type*)
2692 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2693 (obl-fields (ebib-get-obl-fields entry-type))
2694 (opt-fields (ebib-get-opt-fields entry-type))
2696 (if (member ebib-current-field obl-fields)
2697 (ebib-goto-first-field)
2698 (when (member ebib-current-field ebib-additional-fields)
2699 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
2700 (when (or (member ebib-current-field opt-fields)
2702 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
2703 (if (null new-field)
2704 (ebib-goto-first-field)
2705 (setq ebib-current-field new-field)
2706 (ebib-move-to-field ebib-current-field -1))))))
2708 (defun ebib-edit-entry-type ()
2709 "Edits the type of an entry."
2710 ;; we want to put the completion buffer in the lower window. for this
2711 ;; reason, we need to switch to the other window before calling
2712 ;; completing-read. but in order to make sure that we return to the
2713 ;; entry buffer and not the index buffer when the user presses C-g, we
2714 ;; need to do this in an unwind-protect.
2718 (let ((collection (ebib-create-collection ebib-entry-types-hash)))
2719 (if-str (new-type (completing-read "type: " collection nil t))
2721 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
2722 (ebib-fill-entry-buffer)
2723 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2724 (ebib-set-modified t)))))
2727 (defun ebib-edit-crossref ()
2728 "Edits the crossref field."
2732 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
2733 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
2735 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
2736 (ebib-set-modified t)))))
2738 ;; we now redisplay the entire entry buffer, so that the crossref'ed
2739 ;; fields show up. this also puts the cursor back on the type field.
2740 (ebib-fill-entry-buffer)))
2742 ;; we should modify ebib-edit-field, so that it calls the appropriate
2743 ;; helper function, which asks the user for the new value and just returns
2744 ;; that. storing it should then be done by ebib-edit-field, no matter what
2745 ;; sort of field the user edits.
2747 (defun ebib-edit-field ()
2748 "Edits a field of a BibTeX entry."
2751 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
2752 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
2753 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
2755 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
2757 (if (multiline-p init-contents)
2758 (ebib-edit-multiline-field)
2760 (if (raw-p init-contents)
2762 (setq init-contents (to-raw init-contents))))
2763 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
2765 (cons init-contents 0)
2768 (puthash ebib-current-field (if raw
2770 (concat "{" new-contents "}"))
2771 ebib-cur-entry-hash)
2772 (remhash ebib-current-field ebib-cur-entry-hash))
2773 (ebib-redisplay-current-field)
2774 ;; we move to the next field, but only if ebib-edit-field was
2775 ;; called interactively, otherwise we get a strange bug in
2776 ;; ebib-toggle-raw...
2777 (if (interactive-p) (ebib-next-field))
2778 (ebib-set-modified t))))))
2780 (defun ebib-browse-url-in-field (num)
2781 "Browse the url in the current field.
2782 The field may contain a whitespace-separated set of urls. The
2783 prefix argument indicates which url is to be sent to the
2786 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2788 (ebib-call-browser urls num)))
2789 (error "No url found in field `%s'" ebib-current-field))))
2791 (defun ebib-copy-field-contents ()
2792 "Copies the contents of the current field to the kill ring."
2794 (unless (eq ebib-current-field 'type*)
2795 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2796 (when (stringp contents)
2798 (message "Field contents copied.")))))
2800 (defun ebib-cut-field-contents ()
2801 "Kills the contents of the current field. The killed text is put in the kill ring."
2803 (unless (eq ebib-current-field 'type*)
2804 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2805 (when (stringp contents)
2806 (remhash ebib-current-field ebib-cur-entry-hash)
2808 (ebib-redisplay-current-field)
2809 (ebib-set-modified t)
2810 (message "Field contents killed.")))))
2812 (defun ebib-yank-field-contents (arg)
2813 "Inserts the last killed text into the current field.
2814 If the current field already has a contents, nothing is inserted,
2815 unless the previous command was also ebib-yank-field-contents,
2816 then the field contents is replaced with the previous yank. That
2817 is, multiple use of this command functions like the combination
2818 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
2820 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
2821 (eq ebib-current-field 'crossref)
2822 (unless (eq last-command 'ebib-yank-field-contents)
2823 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
2825 (setq this-command t)
2827 (let ((new-contents (current-kill (cond
2828 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
2834 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
2835 (ebib-redisplay-current-field)
2836 (ebib-set-modified t)))))
2838 (defun ebib-delete-field-contents ()
2839 "Deletes the contents of the current field.
2840 The deleted text is not put in the kill ring."
2842 (if (eq ebib-current-field 'type*)
2844 (remhash ebib-current-field ebib-cur-entry-hash)
2845 (ebib-redisplay-current-field)
2846 (ebib-set-modified t)
2847 (message "Field contents deleted.")))
2849 (defun ebib-toggle-raw ()
2850 "Toggles the raw status of the current field contents."
2852 (unless (or (eq ebib-current-field 'type*)
2853 (eq ebib-current-field 'crossref))
2854 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2855 (if (not contents) ; if there is no value,
2857 (ebib-edit-field) ; the user can enter one, which we must then make raw
2858 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
2860 ;; note: we don't have to check for empty string, since that is
2861 ;; already done in ebib-edit-field
2862 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
2863 (if (raw-p contents)
2864 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
2865 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
2866 (ebib-redisplay-current-field)
2867 (ebib-set-modified t))))
2869 (defun ebib-edit-multiline-field ()
2870 "Edits the current field in multiline-mode."
2872 (unless (or (eq ebib-current-field 'type*)
2873 (eq ebib-current-field 'crossref))
2874 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
2876 (setq ebib-multiline-raw t)
2877 (setq text (to-raw text))
2878 (setq ebib-multiline-raw nil))
2879 (ebib-multiline-edit 'fields text))))
2881 (defun ebib-insert-abbreviation ()
2882 "Insert an abbreviation from the ones defined in the database."
2884 (if (gethash ebib-current-field ebib-cur-entry-hash)
2886 (when (edb-strings-list ebib-cur-db)
2890 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
2891 (string (completing-read "Abbreviation to insert: " collection nil t)))
2893 (puthash ebib-current-field string ebib-cur-entry-hash)
2894 (ebib-set-modified t))))
2896 ;; we can't do this earlier, because we would be writing to the index buffer...
2897 (ebib-redisplay-current-field)
2898 (ebib-next-field)))))
2900 (defun ebib-entry-help ()
2901 "Displays the help message for the entry buffer."
2903 (ebib-display-help ebib-entry-buffer))
2909 (defvar ebib-strings-mode-map
2910 (let ((map (make-keymap)))
2911 (suppress-keymap map)
2912 (define-key map [up] 'ebib-prev-string)
2913 (define-key map [down] 'ebib-next-string
)
2914 (define-key map
[prior] 'ebib-strings-page-up)
2915 (define-key map [next] 'ebib-strings-page-down)
2916 (define-key map [home] 'ebib-goto-first-string)
2917 (define-key map [end] 'ebib-goto-last-string)
2918 (define-key map " " 'ebib-strings-page-down)
2919 (define-key map "a" 'ebib-add-string)
2920 (define-key map "b" 'ebib-strings-page-up)
2921 (define-key map "c" 'ebib-copy-string-contents)
2922 (define-key map "d" 'ebib-delete-string)
2923 (define-key map "e" 'ebib-edit-string)
2924 (define-key map "g" 'ebib-goto-first-string)
2925 (define-key map "G" 'ebib-goto-last-string)
2926 (define-key map "h" 'ebib-strings-help)
2927 (define-key map "j" 'ebib-next-string)
2928 (define-key map "k" 'ebib-prev-string)
2929 (define-key map "l" 'ebib-edit-multiline-string)
2930 (define-key map [(control n)] 'ebib-next-string)
2931 (define-key map [(meta n)] 'ebib-strings-page-down)
2932 (define-key map [(control p)] 'ebib-prev-string)
2933 (define-key map [(meta p)] 'ebib-strings-page-up)
2934 (define-key map "q" 'ebib-quit-strings-buffer)
2935 (define-key map "x" 'ebib-export-string)
2936 (define-key map "X" 'ebib-export-all-strings)
2937 (define-key map "\C-xb" 'disabled)
2938 (define-key map "\C-xk" 'disabled)
2940 "Keymap for the ebib strings buffer.")
2942 (define-derived-mode ebib-strings-mode
2943 fundamental-mode "Ebib-strings"
2944 "Major mode for the Ebib strings buffer."
2945 (setq buffer-read-only t)
2946 (setq truncate-lines t))
2948 (defun ebib-quit-strings-buffer ()
2949 "Quit editing the @STRING definitions."
2951 (switch-to-buffer ebib-entry-buffer)
2954 (defun ebib-prev-string ()
2955 "Move to the previous string."
2957 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
2959 ;; go to the beginnig of the highlight and move upward one line.
2960 (goto-char (ebib-highlight-start ebib-strings-highlight))
2962 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2963 (ebib-set-strings-highlight)))
2965 (defun ebib-next-string ()
2966 "Move to the next string."
2968 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2969 (when (interactive-p) (beep))
2970 (goto-char (ebib-highlight-start ebib-strings-highlight))
2972 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
2973 (ebib-set-strings-highlight)))
2975 (defun ebib-goto-first-string ()
2976 "Move to the first string."
2978 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
2979 (goto-char (point-min))
2980 (ebib-set-strings-highlight))
2982 (defun ebib-goto-last-string ()
2983 "Move to the last string."
2985 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
2986 (goto-char (point-max))
2988 (ebib-set-strings-highlight))
2990 (defun ebib-strings-page-up ()
2991 "Moves 10 entries up in the database."
2993 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
2994 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
2995 (if (<= (- number-of-strings remaining-number-of-strings) 10)
2996 (ebib-goto-first-string)
2997 (setq ebib-current-string (nth
2998 (- number-of-strings remaining-number-of-strings 10)
2999 (edb-strings-list ebib-cur-db)))
3000 (goto-char (ebib-highlight-start ebib-strings-highlight))
3002 (ebib-set-strings-highlight)))
3003 (message ebib-current-string))
3005 (defun ebib-strings-page-down ()
3006 "Moves 10 entries down in the database."
3008 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3009 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3010 (if (<= remaining-number-of-strings 10)
3011 (ebib-goto-last-string)
3012 (setq ebib-current-string (nth
3013 (- number-of-strings remaining-number-of-strings -10)
3014 (edb-strings-list ebib-cur-db)))
3015 (goto-char (ebib-highlight-start ebib-strings-highlight))
3017 (ebib-set-strings-highlight)))
3018 (message ebib-current-string))
3020 (defun ebib-fill-strings-buffer ()
3021 "Fills the strings buffer with the @STRING definitions."
3022 (set-buffer ebib-strings-buffer)
3023 (with-buffer-writable
3025 (dolist (elem (edb-strings-list ebib-cur-db))
3026 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
3027 (insert (format "%-18s %s\n" elem
3028 (if (multiline-p str)
3029 (concat "+" (first-line str))
3030 (concat " " str)))))))
3031 (goto-char (point-min))
3032 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3033 (ebib-set-strings-highlight)
3034 (set-buffer-modified-p nil))
3036 (defun ebib-edit-string ()
3037 "Edits the value of an @STRING definition
3038 When the user enters an empty string, the value is not changed."
3040 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3041 (if (multiline-p init-contents)
3042 (ebib-edit-multiline-string)
3043 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
3045 (cons init-contents 0)
3049 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
3050 (ebib-redisplay-current-string)
3052 (ebib-set-modified t))
3053 (error "@STRING definition cannot be empty")))))
3055 (defun ebib-copy-string-contents ()
3056 "Copies the contents of the current string to the kill ring."
3058 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
3060 (message "String value copied.")))
3062 (defun ebib-delete-string ()
3063 "Deletes the current @STRING definition from the database."
3065 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
3066 (remhash ebib-current-string (edb-strings ebib-cur-db))
3067 (with-buffer-writable
3069 (goto-char (ebib-highlight-start ebib-strings-highlight))
3072 (delete-region beg (point))))
3073 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
3074 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
3075 (when (null new-cur-string) ; deleted the last string
3076 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
3078 (setq ebib-current-string new-cur-string))
3079 (ebib-set-strings-highlight)
3080 (ebib-set-modified t)
3081 (message "@STRING definition deleted.")))
3083 (defun ebib-add-string ()
3084 "Creates a new @STRING definition."
3086 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3088 (if (member new-abbr (edb-strings-list ebib-cur-db))
3089 (error (format "%s already exists" new-abbr)))
3090 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3092 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3093 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3094 (with-buffer-writable
3095 (insert (format "%-19s %s\n" new-abbr new-string)))
3097 (ebib-set-strings-highlight)
3098 (setq ebib-current-string new-abbr)
3099 (ebib-set-modified t))))))
3101 (defun ebib-export-string (prefix)
3102 "Exports the current @STRING.
3103 The prefix argument indicates which database to copy the string
3104 to. If no prefix argument is present, a filename is asked to
3105 which the string is appended."
3107 (let ((num (ebib-prefix prefix)))
3109 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3111 (let ((abbr ebib-current-string)
3112 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3113 (if (member abbr (edb-strings-list db))
3114 (error "@STRING definition already exists in database %d" num)
3115 (ebib-insert-string abbr string db t)))))
3116 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3117 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3119 (insert (format "\n@string{%s = %s}\n"
3121 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3123 (defun ebib-export-all-strings (prefix)
3124 "Export all @STRING definitions.
3125 If a prefix argument is given, it is taken as the database to
3126 copy the definitions to. Without prefix argument, asks for a file
3129 (when ebib-current-string ; there is always a current string, unless there are no strings
3130 (let ((num (ebib-prefix prefix)))
3133 num "All @STRING definitions copied to database %d"
3135 (mapc #'(lambda (abbr)
3136 (if (member abbr (edb-strings-list db))
3137 (message "@STRING definition `%s' already exists in database %d" abbr num)
3138 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3139 (edb-strings-list ebib-cur-db))))
3140 (ebib-export-to-file "Export all @STRING definitions to file: "
3141 "All @STRING definitions exported to %s"
3143 (insert (format "\n")) ; to keep things tidy.
3144 (ebib-format-strings ebib-cur-db)))))))
3146 (defun ebib-edit-multiline-string ()
3147 "Edits the current string in multiline-mode."
3149 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3151 (defun ebib-strings-help ()
3152 "Displays the help message for the strings buffer."
3154 (ebib-display-help ebib-strings-buffer))
3156 ;;;;;;;;;;;;;;;;;;;;;;;;;
3157 ;; multiline edit mode ;;
3158 ;;;;;;;;;;;;;;;;;;;;;;;;;
3160 (define-derived-mode ebib-multiline-edit-mode
3161 text-mode "Ebib-edit"
3162 "Major mode for editing multiline strings in Ebib."
3163 ;; we redefine some basic keys because we need them to leave this buffer.
3164 (local-set-key "\C-xb" 'ebib-leave-multiline-edit)
3165 (local-set-key "\C-x\C-s" 'ebib-save-from-multiline-edit)
3166 (local-set-key "\C-xk" 'ebib-cancel-multiline-edit))
3168 (defun ebib-multiline-edit (type &optional starttext)
3169 "Switches to Ebib's multiline edit buffer.
3170 STARTTEXT is a string that contains the initial text of the buffer."
3171 ;; note: the buffer is put in the currently active window!
3172 (switch-to-buffer ebib-multiline-buffer)
3174 (setq ebib-editing type)
3177 (goto-char (point-min))
3178 (set-buffer-modified-p nil)))
3180 (defun ebib-leave-multiline-edit ()
3181 "Quits the multiline edit buffer."
3183 (ebib-store-multiline-text)
3185 ((eq ebib-editing 'preamble)
3186 (switch-to-buffer ebib-entry-buffer)
3187 (other-window 1)) ; we have to switch back to the index buffer window
3188 ((eq ebib-editing 'fields)
3189 (switch-to-buffer ebib-entry-buffer)
3190 (ebib-redisplay-current-field)
3192 ((eq ebib-editing 'strings)
3193 (switch-to-buffer ebib-strings-buffer)
3194 (ebib-redisplay-current-string)
3195 (ebib-next-string)))
3196 (message "Text stored."))
3198 (defun ebib-save-from-multiline-edit ()
3199 "Stores the text being edited in the multiline edit buffer and then saves the database."
3201 (ebib-store-multiline-text)
3202 (ebib-save-database ebib-cur-db)
3203 (set-buffer-modified-p nil))
3205 (defun ebib-store-multiline-text ()
3206 "Stores the text being edited in the multiline edit buffer."
3207 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3209 ((eq ebib-editing 'preamble)
3211 (setf (edb-preamble ebib-cur-db) nil)
3212 (setf (edb-preamble ebib-cur-db) text)))
3213 ((eq ebib-editing 'fields)
3215 (remhash ebib-current-field ebib-cur-entry-hash)
3216 (when (not ebib-multiline-raw)
3217 (setq text (from-raw text)))
3218 (puthash ebib-current-field text ebib-cur-entry-hash)))
3219 ((eq ebib-editing 'strings)
3221 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3222 ;; MESSAGE, but we also do not switch back to the strings
3223 ;; buffer. this may not be so bad, actually, because the user
3224 ;; may want to change his edit.
3225 (error "@STRING definition cannot be empty ")
3226 (setq text (from-raw text)) ; strings cannot be raw
3227 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3228 (ebib-set-modified t))
3230 (defun ebib-cancel-multiline-edit ()
3231 "Quits the multiline edit buffer and discards the changes."
3234 (when (buffer-modified-p)
3235 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3236 (throw 'no-cancel nil)))
3238 ((eq ebib-editing 'fields)
3239 (switch-to-buffer ebib-entry-buffer)
3240 (ebib-redisplay-current-field)) ; we have to do this, because the
3241 ; user may have saved with C-x C-s
3243 ((eq ebib-editing 'strings)
3244 (switch-to-buffer ebib-strings-buffer)
3245 (ebib-redisplay-current-string))
3246 ((eq ebib-editing 'preamble)
3247 (switch-to-buffer ebib-entry-buffer)
3248 (other-window 1)))))
3250 ;;;;;;;;;;;;;;;;;;;;
3251 ;; ebib-help-mode ;;
3252 ;;;;;;;;;;;;;;;;;;;;
3254 (defvar ebib-help-mode-map
3255 (let ((map (make-keymap)))
3256 (suppress-keymap map)
3257 (define-key map " " 'scroll-up)
3258 (define-key map "b" 'scroll-down)
3259 (define-key map "q" 'ebib-quit-help-buffer)
3261 "Keymap for the ebib help buffer.")
3263 (define-derived-mode ebib-help-mode
3264 fundamental-mode "Ebib-help"
3265 "Major mode for the Ebib help buffer."
3266 (setq buffer-read-only t)
3267 (local-set-key "\C-xb" 'ebib-quit-help-buffer)
3268 (local-set-key "\C-xk" 'ebib-quit-help-buffer))
3270 (defun ebib-display-help (buffer)
3271 "Shows the help message for Ebib-buffer BUFFER."
3272 (switch-to-buffer ebib-help-buffer)
3273 (setq ebib-before-help buffer)
3274 (with-buffer-writable
3277 ((eq buffer ebib-index-buffer) (insert ebib-index-buffer-help))
3278 ((eq buffer ebib-entry-buffer) (insert ebib-entry-buffer-help))
3279 ((eq buffer ebib-strings-buffer) (insert ebib-strings-buffer-help)))
3280 (goto-char (point-min))))
3282 (defun ebib-quit-help-buffer ()
3283 "Exits the help buffer."
3286 ((eq ebib-before-help ebib-index-buffer)
3287 (switch-to-buffer ebib-entry-buffer)
3289 ((eq ebib-before-help ebib-entry-buffer)
3290 (switch-to-buffer ebib-entry-buffer))
3291 ((eq ebib-before-help ebib-strings-buffer)
3292 (switch-to-buffer ebib-strings-buffer))))
3298 (defvar ebib-log-mode-map
3299 (let ((map (make-keymap)))
3300 (suppress-keymap map)
3301 (define-key map " " 'scroll-up)
3302 (define-key map "b" 'scroll-down)
3303 (define-key map "q" 'ebib-quit-log-buffer)
3305 "Keymap for the ebib log buffer.")
3307 (define-derived-mode ebib-log-mode
3308 fundamental-mode "Ebib-log"
3309 "Major mode for the Ebib log buffer."
3310 (local-set-key "\C-xb" 'ebib-quit-log-buffer)
3311 (local-set-key "\C-xk" 'ebib-quit-log-buffer))
3313 (defun ebib-quit-log-buffer ()
3314 "Exits the log buffer."
3316 (switch-to-buffer ebib-entry-buffer)
3319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3320 ;; functions for non-Ebib buffers ;;
3321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3323 (defun ebib-import ()
3324 "Searches for BibTeX entries in the current buffer.
3325 The entries are added to the current database (i.e. the database
3326 that was active when Ebib was lowered. Works on the whole buffer,
3327 or on the region if it is active."
3329 (if (not ebib-cur-db)
3330 (error "No database loaded. Use `o' to open a database")
3331 (if (edb-virtual ebib-cur-db)
3332 (error "Cannot import to a virtual database")
3333 (with-syntax-table ebib-syntax-table
3337 (narrow-to-region (region-beginning)
3339 (let ((buffer (current-buffer)))
3341 (insert-buffer-substring buffer)
3342 (let ((n (ebib-find-bibtex-entries t)))
3343 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3344 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3345 (when (edb-strings-list ebib-cur-db)
3346 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3347 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3348 (ebib-fill-entry-buffer)
3349 (ebib-fill-index-buffer)
3350 (ebib-set-modified t)
3351 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3358 (defun ebib-get-db-from-filename (filename)
3359 "Returns the database struct associated with FILENAME."
3361 (mapc #'(lambda (db)
3362 (if (string= (file-name-nondirectory (edb-filename db)) filename)
3367 (defun ebib-get-local-databases ()
3368 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3369 Each .bib file is a string holding the name of the .bib
3370 file. This function simply searches the current LaTeX file or its
3371 master file for a \\bibliography command and returns the file(s)
3372 given in its argument. If no \\bibliography command is found,
3373 returns the symbol NONE."
3374 (let ((texfile-buffer (current-buffer))
3376 ;; if AucTeX's TeX-master is used and set to a string, we must
3377 ;; search that file for a \bibliography command, as it's more
3378 ;; likely to be in there than in the file we're in.
3379 (and (boundp 'TeX-master)
3380 (stringp TeX-master)
3381 (setq texfile (ensure-extension TeX-master "tex")))
3383 (if (and texfile (file-readable-p texfile))
3384 (insert-file-contents texfile)
3385 (insert-buffer-substring texfile-buffer))
3387 (goto-char (point-min))
3388 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3389 (mapcar #'(lambda (file)
3390 (ensure-extension file "bib"))
3391 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3394 (defun ebib-insert-bibtex-key (prefix)
3395 "Inserts a BibTeX key at POINT.
3396 The user is prompted for a BibTeX key and has to choose one from
3397 the database(s) associated with the current LaTeX file, or from
3398 the current database if there is no \\bibliography command. Tab
3403 (or ebib-local-bibtex-filenames
3404 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3406 (if (eq ebib-local-bibtex-filenames 'none)
3407 (if (null (edb-cur-entry ebib-cur-db))
3408 (error "No entries found in current database")
3409 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3410 (mapc #'(lambda (file)
3411 (let ((db (ebib-get-db-from-filename file)))
3414 (message "Database %s not loaded" file))
3415 ((null (edb-cur-entry db))
3416 (message "No entries in database %s" file))
3417 (t (setq collection (append (ebib-create-collection (edb-database db))
3419 ebib-local-bibtex-filenames))
3421 (let* ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
3423 (key (completing-read (format "Insert \"%s\" with key: " latex-cmd)
3424 collection nil t nil ebib-minibuf-hist)))
3426 (insert (format (or (cdr (assoc prefix ebib-insertion-strings))
3429 (error "No database loaded"))))
3431 (defun ebib-entry-summary ()
3432 "Shows the fields of the key at POINT.
3433 The key is searched in the database associated with the LaTeX
3434 file, or in the current database if no \\bibliography command can
3439 (or ebib-local-bibtex-filenames
3440 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3441 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3443 (if (eq ebib-local-bibtex-filenames 'none)
3444 (if (not (member key (edb-keys-list ebib-cur-db)))
3445 (error "`%s' is not in the current database" key)
3446 (setq entry (gethash key (edb-database ebib-cur-db))))
3449 (mapc #'(lambda (file)
3450 (let ((db (ebib-get-db-from-filename file)))
3452 (message "Database %s not loaded" file)
3453 (if (member key (edb-keys-list db))
3454 (throw 'found (gethash key (edb-database db)))))))
3455 ebib-local-bibtex-filenames)
3458 (error "Entry `%s' not found" key)
3459 (with-output-to-temp-buffer "*Help*"
3460 (ebib-format-fields entry 'princ)))))
3462 (error "No database(s) loaded"))))
3466 ;; we put these at the end, because they seem to mess up Emacs'
3467 ;; syntax highlighting.
3469 (setq ebib-index-buffer-help
3470 "Ebib index buffer -- command key overview
3472 Note: command keys are case-sensitive.
3474 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3477 [Up], k, C-p: go to the previous entry
3478 [Down], j, C-n: go to the next entry
3479 [Home], g: go to the first entry
3480 [End], G: go to the last entry
3481 [PgUp], b, M-p: scroll up
3482 [PgDn], [Space], M-n: scroll down
3485 e: edit the current entry
3486 E: edit the current entry's name
3488 d: delete the current entry
3489 ;-d: deletes all marked entries
3490 t: edit the @STRING definitions
3491 r: edit the @PREAMBLE definition
3492 m: (un)mark the current entry
3493 ;-m: unmarks all marked entries
3496 /: search the database
3497 n: find the next occurrence of the search string
3498 C-s: search for a key (incrementally)
3499 [return]: select the entry under the cursor (use after C-s)
3500 F: follow the crossref field
3501 &: filter the current database with a logical AND
3502 |: filter the current database with a logical OR
3503 ~: filter the current database with a logical NOT
3504 V: show the current filter (with prefix argument:
3505 reapply current filter)
3509 c: close the database
3510 s: save the database
3511 S: save all databases
3512 w: save the database under a different name
3513 M: merge another database
3514 x: export the current entry to another file
3515 (with prefix argument N: copy to database N)
3516 ;-x: export the marked entry to another file
3517 (with prefix argument N: copy to database N)
3518 X: export the @PREAMBLE definition to another file
3519 (with prefix argument N: copy to database N)
3520 f: print the full filename in the minibuffer
3523 1-9: switch to database 1-9
3524 J: switch to another database (accepts prefix argument)
3525 [right], [left]: switch to previous/next database
3526 L: LaTeX the database
3527 ;-L: LaTeX the marked entries
3528 P: print the database
3529 ;-P: print the marked entries
3532 u: extract URL from the `url' field and send it to a browser
3533 p: push the current entry to a LaTeX buffer
3534 ;-p: push marked entries to a LaTeX buffer
3536 z: put Ebib in the background
3538 h: show this help page
3541 (setq ebib-entry-buffer-help
3542 "Ebib entry buffer -- command key overview
3544 Note: command keys are case-sensitive.
3546 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3549 [Up], k, C-p: go to the previous field
3550 [Down], j, C-n: go to the next field
3551 [Home], g: go to the first field
3552 [End], G: go to the last field
3553 [PgUp], b, M-p: go to the previous group of fields
3554 [PgDn], [Space], M-n: go to the next group of fields
3557 e: edit the value of the current field
3558 c: copy the value of the current field (value is put into the kill ring)
3559 x: kill the value of the current field (value is put into the kill ring)
3560 y: yank the most recently copied/cut string
3561 d: delete the value of the current entry
3562 r: toggle the \"rawness\" status of the current field
3563 l: edit the current field as multi-line
3564 s: insert an @STRING abbreviation into the current field
3567 u: extract URL and send it to a browser
3568 q: quit the entry buffer and return to the index buffer
3569 h: show this help page
3572 (setq ebib-strings-buffer-help
3573 "Ebib strings buffer -- command key overview
3575 Note: command keys are case-sensitive.
3577 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3580 [Up], k, C-p: go to the previous @STRING definition
3581 [Down], j, C-n: go to the next @STRING definition
3582 [Home], g: go to the first @STRING definition
3583 [End], G: go to the last @STRING definition
3584 [PgUp], b, M-p: scroll 10 @STRING definitions up
3585 [PgDn], [Space], M-n: scroll 10 @STRING definitions down
3588 e: edit the value of the current @STRING definition
3589 c: copy the value of the current @STRING definition
3590 d: delete the current @STRING definition
3591 a: add an @STRING definition
3592 l: edit the current @STRING as multi-line
3595 x: export the current @STRING definition to another file
3596 (with prefix argument N: copy to database N)
3597 X: export all @STRING definitions to another file
3598 (with prefix argument N: copy to database N)
3601 q: quit the strings buffer and return to the index buffer
3602 h: show this help page