3 ;; Copyright (c) 2003-2008 Joost Kremers
4 ;; All rights reserved.
6 ;; Redistribution and use in source and binary forms, with or without
7 ;; modification, are permitted provided that the following conditions
10 ;; 1. Redistributions of source code must retain the above copyright
11 ;; notice, this list of conditions and the following disclaimer.
12 ;; 2. Redistributions in binary form must reproduce the above copyright
13 ;; notice, this list of conditions and the following disclaimer in the
14 ;; documentation and/or other materials provided with the distribution.
15 ;; 3. The name of the author may not be used to endorse or promote products
16 ;; derived from this software without specific prior written permission.
18 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 ;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 ;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 ;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE,
24 ;; DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 ;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ;;;;;;;;;;;;;;;;;;;;;;
32 ;; global variables ;;
33 ;;;;;;;;;;;;;;;;;;;;;;
37 (defgroup ebib nil
"Ebib: a BibTeX database manager" :group
'Tex
)
39 (defcustom ebib-default-type
'article
40 "*The default type for a newly created BibTeX entry."
44 (defcustom ebib-preload-bib-files nil
45 "*List of .bib files to load automatically when Ebib starts."
47 :type
'(repeat (file :must-match t
)))
49 (defcustom ebib-additional-fields
'(crossref url annote abstract
50 keywords file timestamp
)
51 "*Holds a list of the additional fields."
53 :type
'(repeat (symbol :tag
"Field")))
55 (defcustom ebib-layout
'full
57 Full width: Ebib occupies the entire Emacs frame.
59 Specify width: Ebib occupies the right side of the Emacs frame,
60 with the left side free for another window."
62 :type
'(choice (const :tag
"Full width" full
)
63 (integer :tag
"Specify width")))
65 (defcustom ebib-index-window-size
10
66 "*The number of lines used for the keys buffer window."
70 (defcustom ebib-index-display-fields nil
71 "*Holds a list of the fields to display in the index buffer."
73 :type
'(repeat (symbol :tag
"Index Field")))
75 (defcustom ebib-insertion-strings
'((0 .
"%s")
77 "*The string to insert when calling EBIB-INSERT-BIBTEX-KEY.
78 The directive \"%s\" is replaced with the entry key."
80 :type
'(repeat (cons :tag
"Insertion string" (integer :tag
"Number") (string))))
82 (defcustom ebib-sort-order nil
83 "*The fields on which the BibTeX entries are to be sorted in the .bib file.
84 Sorting is done on different sort levels, and each sort level contains one
87 :type
'(repeat (repeat :tag
"Sort level" (symbol :tag
"Sort field"))))
89 (defcustom ebib-save-xrefs-first nil
90 "*If true, entries with a crossref field will be saved first in the .bib-file.
91 Setting this option has unpredictable results for the sort order
92 of entries, so it is not compatible with setting the Sort Order option."
96 (defface ebib-crossref-face
'((t (:foreground
"red")))
97 "*Face used to indicate values inherited from crossreferenced entries."
100 (defface ebib-marked-face
(if (featurep 'xemacs
)
101 '((t (:foreground
"white" :background
"red")))
102 '((t (:inverse-video t
))))
103 "*Face to indicate marked entries."
106 (defcustom ebib-use-timestamp nil
107 "*If true, new entries will get a time stamp.
108 The time stamp will be stored in a field \"timestamp\" that can
109 be made visible with the `H' command in the index buffer."
113 (defcustom ebib-timestamp-format
"%a %b %e %T %Y"
114 "*Format of the time string used in the timestamp.
115 The format is given directly to FORMAT-TIME-STRING, see the
116 documentation of that function for details."
120 (defcustom ebib-standard-url-field
'url
121 "*Standard field to store urls in.
122 In the index buffer, the command ebib-browse-url can be used to
123 send a url to a browser. This option sets the field from which
124 this command extracts the url."
128 (defcustom ebib-url-regexp
"\\\\url{\\(.*\\)}\\|https?://[^ '<>\"\n\t\f]+"
129 "*Regular expression to extract urls."
133 (defcustom ebib-browser-command
""
134 "*Command to call the browser with.
135 GNU/Emacs has a function call-browser, which is used if this
138 :type
'(string :tag
"Browser command"))
140 (defcustom ebib-standard-file-field
'file
141 "*Standard field to store filenames in.
142 In the index buffer, the command ebib-view-file can be used to
143 view a file externally. This option sets the field from which
144 this command extracts the filename."
148 (defcustom ebib-file-associations
'(("pdf" .
"xpdf")
150 "*List of file associations.
151 Lists file extensions together with external programs to handle
152 files with those extensions. If the external program is left
153 blank, Ebib tries to handle the file internally in
154 Emacs (e.g. with doc-view-mode)."
156 :type
'(repeat (cons :tag
"File association"
157 (string :tag
"Extension") (string :tag
"Command"))))
159 (defcustom ebib-file-regexp
"[^?|\\:*<>\" \n\t\f]+"
160 "*Regular expression to match filenames."
164 (defcustom ebib-file-search-dirs
'("~")
165 "*List of directories to search for files."
167 :type
'(repeat :tag
"Search directories" (string :tag
"Directory")))
169 (defcustom ebib-print-preamble nil
170 "*Preamble used for the LaTeX file for printing the database.
171 Each string is added to the preamble on a separate line."
173 :type
'(repeat (string :tag
"Add to preamble")))
175 (defcustom ebib-print-multiline nil
176 "*If set, multiline fields are included when printing the database."
180 (defcustom ebib-latex-preamble
'("\\bibliographystyle{plain}")
181 "*Preamble used for the LaTeX file for BibTeXing the database.
182 Each string is added to the preamble on a separate line."
184 :type
'(repeat (string :tag
"Add to preamble")))
186 (defcustom ebib-print-tempfile
""
187 "*Temporary file for use with EBIB-PRINT-DATABASE and EBIB-LATEX-DATABASE."
191 (defcustom ebib-allow-identical-fields nil
192 "*If set, Ebib handles multiple occurrences of a field gracefully."
196 (defvar ebib-entry-types-hash
(make-hash-table)
197 "Holds the hash table containing the entry type definitions.")
198 (defvar ebib-unique-field-list nil
199 "Holds a list of all field names.")
201 (defmacro add-to-listq
(listvar element
&optional append fn
)
202 (if (or (featurep 'xemacs
)
203 (string< emacs-version
"22"))
204 `(add-to-list (quote ,listvar
) ,element
,append
)
205 `(add-to-list (quote ,listvar
) ,element
,append
,fn
)))
207 (defun ebib-set-entry-types-hash (var value
)
208 "Sets EBIB-ENTRY-TYPES-HASH on the basis of EBIB-ENTRY-TYPES"
209 (set-default var value
)
210 (clrhash ebib-entry-types-hash
)
211 (setq ebib-unique-field-list nil
)
212 (mapc #'(lambda (entry)
213 (puthash (car entry
) (cdr entry
) ebib-entry-types-hash
)
214 (mapc #'(lambda (field)
215 (add-to-listq ebib-unique-field-list field t
'eq
))
217 (mapc #'(lambda (field)
218 (add-to-listq ebib-unique-field-list field t
'eq
))
222 (defcustom ebib-entry-types
223 '((article ;; name of entry type
224 (author title journal year
) ;; obligatory fields
225 (volume number pages month note
)) ;; optional fields
228 (author title publisher year
)
229 (editor volume number series address edition month note
))
233 (author howpublished address month year note
))
236 (author title chapter pages publisher year
)
237 (editor volume series address edition month note
))
240 (author title booktitle publisher year
)
241 (editor volume number series type chapter pages address edition month note
))
244 (author title booktitle year
)
245 (editor pages organization publisher address month note
))
249 (author organization address edition month year note
))
253 (title author howpublished month year note
))
256 (author title school year
)
257 (address month note
))
260 (author title school year
)
261 (address month note
))
265 (editor publisher organization address month note
))
268 (author title institution year
)
269 (type number address month note
))
275 "List of entry type definitions for Ebib"
277 :type
'(repeat (list :tag
"Entry type" (symbol :tag
"Name")
278 (repeat :tag
"Obligatory fields" (symbol :tag
"Field"))
279 (repeat :tag
"Optional fields" (symbol :tag
"Field"))))
280 :set
'ebib-set-entry-types-hash
)
282 ;; generic for all databases
284 ;; constants and variables that are set only once
285 (defconst ebib-bibtex-identifier
"[^\"#%'(),={} \t\n\f]*" "Regex describing a licit BibTeX identifier.")
286 (defconst ebib-version
"==VERSION==")
287 (defvar ebib-initialized nil
"T if Ebib has been initialized.")
289 ;; buffers and highlights
290 (defvar ebib-index-buffer nil
"The index buffer.")
291 (defvar ebib-entry-buffer nil
"The entry buffer.")
292 (defvar ebib-strings-buffer nil
"The strings buffer.")
293 (defvar ebib-multiline-buffer nil
"Buffer for editing multiline strings.")
294 (defvar ebib-help-buffer nil
"Buffer showing Ebib help.")
295 (defvar ebib-log-buffer nil
"Buffer showing warnings and errors during loading of .bib files")
296 (defvar ebib-index-highlight nil
"Highlight to mark the current entry.")
297 (defvar ebib-fields-highlight nil
"Highlight to mark the current field.")
298 (defvar ebib-strings-highlight nil
"Highlight to mark the current string.")
300 ;; general bookkeeping
301 (defvar ebib-minibuf-hist nil
"Holds the minibuffer history for Ebib")
302 (defvar ebib-saved-window-config nil
"Stores the window configuration when Ebib is called.")
303 (defvar ebib-pre-ebib-window nil
"The window that was active when Ebib was called.")
304 (defvar ebib-pre-multiline-buffer nil
"The buffer in the window before switching to the multiline edit buffer.")
305 (defvar ebib-export-filename nil
"Filename to export entries to.")
306 (defvar ebib-push-buffer nil
"Buffer to push entries to.")
307 (defvar ebib-search-string nil
"Stores the last search string.")
308 (defvar ebib-editing nil
"Indicates what the user is editing.
309 Its value can be 'strings, 'fields, or 'preamble.")
310 (defvar ebib-multiline-raw nil
"Indicates whether the multiline text being edited is raw.")
311 (defvar ebib-before-help nil
"Stores the buffer the user was in when he displayed the help message.")
312 (defvar ebib-log-error nil
"Indicates whether an error was logged.")
313 (defvar ebib-local-bibtex-filenames nil
"A buffer-local variable holding a list of the name(s) of that buffer's .bib file")
314 (make-variable-buffer-local 'ebib-local-bibtex-filenames
)
315 (defvar ebib-syntax-table
(make-syntax-table) "Syntax table used for reading .bib files.")
316 (modify-syntax-entry ?\
[ "." ebib-syntax-table
)
317 (modify-syntax-entry ?\
] "." ebib-syntax-table
)
318 (modify-syntax-entry ?\
( "." ebib-syntax-table
)
319 (modify-syntax-entry ?\
) "." ebib-syntax-table
)
320 (modify-syntax-entry ?
\" "w" ebib-syntax-table
)
324 ;; each database is represented by a struct
326 (database (make-hash-table :test
'equal
)) ; hashtable containing the database itself
327 (keys-list nil
) ; sorted list of the keys in the database
328 (cur-entry nil
) ; sublist of KEYS-LIST that starts with the current entry
329 (marked-entries nil
) ; list of marked entries
330 (n-entries 0) ; number of entries stored in this database
331 (strings (make-hash-table :test
'equal
)) ; hashtable with the @STRING definitions
332 (strings-list nil
) ; sorted list of the @STRING abbreviations
333 (preamble nil
) ; string with the @PREAMBLE definition
334 (filename nil
) ; name of the BibTeX file that holds this database
335 (name nil
) ; name of the database
336 (modified nil
) ; has this database been modified?
337 (make-backup nil
) ; do we need to make a backup of the .bib file?
338 (virtual nil
)) ; is this a virtual database?
340 ;; the master list and the current database
341 (defvar ebib-databases nil
"List of structs containing the databases")
342 (defvar ebib-cur-db nil
"The database that is currently active")
344 ;;;;;; bookkeeping required when editing field values or @STRING definitions
346 (defvar ebib-hide-hidden-fields t
"If set to T, hidden fields are not shown.")
348 ;; these two variables are set when the user enters the entry buffer
349 (defvar ebib-cur-entry-hash nil
"The hash table containing the data of the current entry.")
350 (defvar ebib-cur-entry-fields nil
"The fields of the type of the current entry.")
352 ;; and these two are set by EBIB-FILL-ENTRY-BUFFER and EBIB-FILL-STRINGS-BUFFER, respectively
353 (defvar ebib-current-field nil
"The current field.")
354 (defvar ebib-current-string nil
"The current @STRING definition.")
356 ;; we define these variables here, but only give them a value at the end of
357 ;; the file, because the long strings mess up Emacs' syntax highlighting.
358 (defvar ebib-index-buffer-help nil
"Help for the index buffer.")
359 (defvar ebib-entry-buffer-help nil
"Help for the entry buffer.")
360 (defvar ebib-strings-buffer-help nil
"Help for the strings buffer.")
362 ;; the prefix key is stored in a variable so that the user can customise it.
363 (defvar ebib-prefix-key ?\
;)
365 ;; this is an AucTeX variable, but we want to check its value, so let's
366 ;; keep the compiler from complaining.
370 ;; this is to keep XEmacs from complaining.
372 (if (featurep 'xemacs
)
373 (defvar mark-active
)))
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376 ;; useful macros and functions ;;
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 (defmacro nor
(&rest args
)
380 "Returns T if none of its arguments are true."
383 ;; we sometimes (often, in fact ;-) need to do something with a string, but
384 ;; take special action (or do nothing) if that string is empty. IF-STR
385 ;; makes that easier:
387 (defmacro if-str
(bindvar then
&rest else
)
388 "Execute THEN only if STRING is nonempty.
389 Format: (if-str (var value) then-form [else-forms]) VAR is bound
390 to VALUE, which is evaluated. If VAR is a nonempty string,
391 THEN-FORM is executed. If VAR is either \"\" or nil, ELSE-FORM is
392 executed. Returns the value of THEN or of ELSE."
394 `(let ,(list bindvar
)
395 (if (nor (null ,(car bindvar
))
396 (equal ,(car bindvar
) ""))
400 (defmacro last1
(lst &optional n
)
401 "Returns the last (or Nth last) element of LST."
402 `(car (last ,lst
,n
)))
404 ;; we sometimes need to walk through lists. these functions yield the
405 ;; element directly preceding or following ELEM in LIST. in order to work
406 ;; properly, ELEM must be unique in LIST, obviously. if ELEM is the
407 ;; first/last element of LIST, or if it is not contained in LIST at all,
408 ;; the result is nil.
409 (defun next-elem (elem list
)
410 (cadr (member elem list
)))
412 (defun prev-elem (elem list
)
413 (if (or (equal elem
(car list
))
414 (not (member elem list
)))
416 (last1 list
(1+ (length (member elem list
))))))
418 (defun read-string-at-point (chars)
419 "Reads a string at POINT delimited by CHARS and returns it.
420 CHARS is a string of characters that should not occur in the string."
422 (skip-chars-backward (concat "^" chars
))
424 (looking-at-goto-end (concat "[^" chars
"]*"))
425 (buffer-substring-no-properties beg
(point)))))
427 (defun remove-from-string (string remove
)
428 "Returns a copy of STRING with all the occurrences of REMOVE taken out.
429 REMOVE can be a regex."
430 (apply 'concat
(split-string string remove
)))
432 (defun in-string (char string
)
433 "Returns T if CHAR is in STRING, otherwise NIL."
435 (do ((len (length string
))
438 (if (eq char
(aref string i
))
441 (defun ensure-extension (string ext
)
442 "Makes sure STRING has the extension EXT, by appending it if necessary.
443 EXT should be an extension without the dot."
444 (if (string-match (concat "\\." ext
"$") string
)
446 (concat string
"." ext
)))
448 (defmacro with-buffer-writable
(&rest body
)
449 "Makes the current buffer writable and executes the commands in BODY.
450 After BODY is executed, the buffer modified flag is unset."
451 (declare (indent defun
))
453 (let ((buffer-read-only nil
))
455 (set-buffer-modified-p nil
)))
457 (defmacro safe-write-region
(start end filename
&optional append visit lockname mustbenew
)
458 "XEmacs does not have the MUSTBENEW argument, so this is a way to implement it."
459 (if (featurep 'xemacs
)
460 `(if (and (file-exists-p ,filename
)
461 (not (y-or-n-p (format "File %s already exists; overwrite anyway? " ,filename
))))
462 (error "File %s exist" ,filename
)
463 (write-region ,start
,end
,filename
,append
,visit
,lockname
))
464 `(write-region ,start
,end
,filename
,append
,visit
,lockname
,mustbenew
)))
466 (defun symbol-or-string (x)
467 "Returns the symbol-name of X if X is a symbol, otherwise return X.
468 Much like SYMBOL-NAME, except it does not throw an error if X is
474 ;; XEmacs doesn't know about propertize...
475 (if (not (fboundp 'propertize
))
476 (defun propertize (string &rest properties
)
477 "Return a copy of STRING with text properties added.
478 First argument is the string to copy. Remaining arguments form a
479 sequence of PROPERTY VALUE pairs for text properties to add to
481 (let ((new-string (copy-sequence string
)))
482 (add-text-properties 0 (length new-string
) properties new-string
)
485 (defun region-active ()
486 (if (featurep 'xemacs
)
490 ;; RAW-P determines if STRING is raw. note that we cannot do this by
491 ;; simply checking whether STRING begins with { and ends with } (or
492 ;; begins and ends with "), because something like "{abc} # D # {efg}"
493 ;; would then be incorrectly recognised as non-raw. so we need to do
494 ;; the following: take out everything that is between braces or
495 ;; quotes, and see if anything is left. if there is, the original
496 ;; string was raw, otherwise it was not.
498 ;; so i first check whether the string begins with { or ". if not, we
499 ;; certainly have a raw string. (RAW-P recognises this through the default
500 ;; clause of the COND.) if the first character is { or ", we first take out
501 ;; every occurrence of backslash-escaped { and } or ", so that the rest of
502 ;; the function does not get confused over them.
504 ;; then, if the first character is {, i use REMOVE-FROM-STRING to take out
505 ;; every occurrence of the regex "{[^{]*?}", which translates to "the
506 ;; smallest string that starts with { and ends with }, and does not contain
507 ;; another {. IOW, it takes out the innermost braces and their
508 ;; contents. because braces may be embedded, we have to repeat this step
509 ;; until no more balanced braces are found in the string. (note that it
510 ;; would be unwise to check for just the occurrence of { or }, because that
511 ;; would throw RAW-P in an infinite loop if a string contains an unbalanced
514 ;; for strings beginning with " i do the same, except that it is not
515 ;; necessary to repeat this in a WHILE loop, for the simple reason that
516 ;; strings surrounded with double quotes cannot be embedded; i.e.,
517 ;; "ab"cd"ef" is not a valid (BibTeX) string, while {ab{cd}ef} is.
519 ;; note: because these strings are to be fed to BibTeX and ultimately
520 ;; (La)TeX, it might seem that we don't need to worry about strings
521 ;; containing unbalanced braces, because (La)TeX would choke on them. but
522 ;; the user may inadvertently enter such a string, and we therefore need to
523 ;; be able to handle it. (alternatively, we could perform a check on
524 ;; strings and warn the user.)
526 (defun raw-p (string)
527 "Non-nil if STRING is raw."
528 (when (stringp string
)
530 ((eq (string-to-char string
) ?\
{)
531 ;; we remove all occurrences of `\{' and of `\}' from the string:
532 (let ((clear-str (remove-from-string (remove-from-string string
"[\\][{]")
534 (while (and (in-string ?\
{ clear-str
) (in-string ?\
} clear-str
))
535 (setq clear-str
(remove-from-string clear-str
"{[^{]*?}")))
536 (> (length clear-str
) 0)))
537 ((eq (string-to-char string
) ?
\")
538 (let ((clear-str (remove-from-string string
"[\\][\"]"))) ; remove occurrences of `\"'
539 (setq clear-str
(remove-from-string clear-str
"\"[^\"]*?\""))
540 (> (length clear-str
) 0)))
543 (defun to-raw (string)
544 "Converts a string to its raw counterpart."
545 (if (and (stringp string
)
546 (not (raw-p string
)))
547 (substring string
1 -
1)
550 (defun from-raw (string)
551 "Converts a raw string to a non-raw one."
553 (concat "{" string
"}")
556 (defun multiline-p (string)
557 "True if STRING is multiline."
559 (string-match "\n" string
)))
561 (defun first-line (string)
562 "Returns the first line of a multi-line string."
563 (string-match "\n" string
)
564 (substring string
0 (match-beginning 0)))
566 (defun sort-in-buffer (limit str
)
567 "Moves POINT to the right position to insert STR in a buffer with lines sorted A-Z."
572 (goto-char (point-min))
574 (setq middle
(/ (+ lower upper
1) 2))
575 (goto-line middle
) ; if this turns out to be where we need to be,
576 (beginning-of-line) ; this puts POINT at the right spot.
577 (> (- upper lower
) 1)) ; if upper and lower differ by only 1, we have found the
578 ; position to insert the entry in.
582 (if (string< (buffer-substring-no-properties beg
(point)) str
)
584 (setq upper middle
)))))))))
586 (defun match-all (match-str string
)
587 "Highlights all the matches of MATCH-STR in STRING.
588 The return value is a list of two elements: the first is the
589 modified string, the second either t or nil, indicating whether a
590 match was found at all."
591 (do ((counter 0 (match-end 0)))
592 ((not (string-match match-str string counter
)) (values string
(not (= counter
0))))
593 (add-text-properties (match-beginning 0) (match-end 0) '(face highlight
) string
)))
595 (defun looking-at-goto-end (str &optional match
)
596 "Like LOOKING-AT but moves point to the end of the matching string.
597 MATCH acts just like the argument to MATCH-END, and defaults to 0."
598 (or match
(setq match
0))
599 (let ((case-fold-search t
))
601 (goto-char (match-end match
)))))
603 ;; this needs to be wrapped in an eval-and-compile, to keep Emacs from
604 ;; complaining that ebib-execute-helper isn't defined when it compiles
605 ;; ebib-execute-when.
607 (defun ebib-execute-helper (env)
608 "Helper function for EBIB-EXECUTE-WHEN."
612 (edb-cur-entry ebib-cur-db
)))
613 ((eq env
'marked-entries
)
615 (edb-marked-entries ebib-cur-db
)))
620 (not (edb-virtual ebib-cur-db
))))
621 ((eq env
'virtual-db
)
623 (edb-virtual ebib-cur-db
)))
624 ((eq env
'no-database
)
628 (defmacro ebib-execute-when
(&rest forms
)
629 "Macro to facilitate writing Ebib functions.
630 This functions essentially like a COND clause: the basic format
631 is (ebib-execute-when FORMS ...), where each FORM is built up
632 as (ENVIRONMENTS BODY). ENVIRONMENTS is a list of symbols (not
633 quoted) that specify under which conditions BODY is to be
634 executed. Valid symbols are:
636 entries: execute when there are entries in the database,
637 marked-entries: execute when there are marked entries in the database,
638 database: execute if there is a database,
639 no-database: execute if there is no database,
640 real-db: execute when there is a database and it is real,
641 virtual-db: execute when there is a database and it is virtual,
642 default: execute if all else fails.
644 Just like with COND, only one form is actually executed, the
645 first one that matches. If ENVIRONMENT contains more than one
646 condition, BODY is executed if they all match (i.e., the
647 conditions are AND'ed.)"
648 (declare (indent defun
))
650 ,@(mapcar #'(lambda (form)
651 (cons (if (= 1 (length (car form
)))
652 (ebib-execute-helper (caar form
))
653 `(and ,@(mapcar #'(lambda (env)
654 (ebib-execute-helper env
))
659 ;; the numeric prefix argument is 1 if the user gave no prefix argument at
660 ;; all. the raw prefix argument is not always a number. so we need to do
661 ;; our own conversion.
662 (defun ebib-prefix (num)
666 (defun ebib-called-with-prefix ()
667 "Returns T if the command was called with a prefix key."
668 (if (featurep 'xemacs
)
669 (member (character-to-event ebib-prefix-key
) (append (this-command-keys) nil
))
670 (member (event-convert-list (list ebib-prefix-key
))
671 (append (this-command-keys-vector) nil
))))
673 (defmacro ebib-export-to-db
(num message copy-fn
)
674 "Exports data to another database.
675 NUM is the number of the database to which the data is to be copied.
677 MESSAGE is a string displayed in the echo area if the export was
678 succesful. It must contain a %d directive, which is used to
679 display the database number to which the entry was exported.
681 COPY-FN is the function that actually copies the relevant
682 data. It must take as argument the database to which the data is
683 to be copied. COPY-FN must return T if the copying was
684 successful, and NIL otherwise."
685 `(let ((goal-db (nth (1- ,num
) ebib-databases
)))
688 (error "Database %d does not exist" ,num
))
689 ((edb-virtual goal-db
)
690 (error "Database %d is virtual" ,num
))
691 (t (when (funcall ,copy-fn goal-db
)
692 (ebib-set-modified t goal-db
)
693 (message ,message
,num
))))))
695 (defmacro ebib-export-to-file
(prompt-string message insert-fn
)
696 "Exports data to a file.
697 PROMPT-STRING is the string that is used to ask for the filename
698 to export to. INSERT-FN must insert the data to be exported into
699 the current buffer: it is called within a WITH-TEMP-BUFFER, whose
700 contents is appended to the file the user enters.
702 MESSAGE is shown in the echo area when the export was
703 successful. It must contain a %s directive, which is used to
704 display the actual filename."
705 `(let ((insert-default-directory (not ebib-export-filename
)))
706 (if-str (filename (read-file-name
707 ,prompt-string
"~/" nil nil ebib-export-filename
))
710 (append-to-file (point-min) (point-max) filename
)
711 (setq ebib-export-filename filename
)))))
713 (defun ebib-temp-window ()
715 (if (eq ebib-layout
'full
)
716 (get-buffer-window ebib-entry-buffer
)
717 ebib-pre-ebib-window
))
719 (defun ebib-get-obl-fields (entry-type)
720 "Returns the obligatory fields of ENTRY-TYPE."
721 (car (gethash entry-type ebib-entry-types-hash
)))
723 (defun ebib-get-opt-fields (entry-type)
724 "Returns the optional fields of ENTRY-TYPE."
725 (cadr (gethash entry-type ebib-entry-types-hash
)))
727 (defun ebib-get-all-fields (entry-type)
728 "Returns all the fields of ENTRY-TYPE."
729 (cons 'type
* (append (ebib-get-obl-fields entry-type
)
730 (ebib-get-opt-fields entry-type
)
731 ebib-additional-fields
)))
733 (defun ebib-erase-buffer (buffer)
735 (with-buffer-writable
738 (defun ebib-make-highlight (begin end buffer
)
740 (if (featurep 'xemacs
)
742 (setq highlight
(make-extent begin end buffer
))
743 (set-extent-face highlight
'highlight
))
745 (setq highlight
(make-overlay begin end buffer
))
746 (overlay-put highlight
'face
'highlight
)))
749 (defun ebib-move-highlight (highlight begin end buffer
)
750 (if (featurep 'xemacs
)
751 (set-extent-endpoints highlight begin end buffer
)
752 (move-overlay highlight begin end buffer
)))
754 (defun ebib-highlight-start (highlight)
755 (if (featurep 'xemacs
)
756 (extent-start-position highlight
)
757 (overlay-start highlight
)))
759 (defun ebib-highlight-end (highlight)
760 (if (featurep 'xemacs
)
761 (extent-end-position highlight
)
762 (overlay-end highlight
)))
764 (defun ebib-delete-highlight (highlight)
765 (if (featurep 'xemacs
)
766 (detach-extent highlight
)
767 (delete-overlay highlight
)))
769 (defun ebib-set-index-highlight ()
770 (set-buffer ebib-index-buffer
)
773 (if ebib-index-display-fields
775 (skip-chars-forward "^ "))
776 (ebib-move-highlight ebib-index-highlight beg
(point) ebib-index-buffer
)))
778 (defun ebib-set-fields-highlight ()
779 (set-buffer ebib-entry-buffer
)
782 (looking-at-goto-end "[^ \t\n\f]*")
783 (ebib-move-highlight ebib-fields-highlight beg
(point) ebib-entry-buffer
)))
785 (defun ebib-set-strings-highlight ()
786 (set-buffer ebib-strings-buffer
)
789 (looking-at-goto-end "[^ \t\n\f]*")
790 (ebib-move-highlight ebib-strings-highlight beg
(point) ebib-strings-buffer
)))
792 (defun ebib-display-entry (entry-key)
793 "Displays ENTRY-KEY in the index buffer at POINT."
794 (set-buffer ebib-index-buffer
)
795 (insert (format "%-30s %s\n"
797 (if ebib-index-display-fields
798 (let ((cur-entry-hash (ebib-retrieve-entry entry-key ebib-cur-db
)))
799 (mapconcat #'(lambda (field)
801 (to-raw (gethash field cur-entry-hash
))
803 ebib-index-display-fields
807 (defun ebib-redisplay-current-field ()
808 "Redisplays the contents of the current field in the entry buffer."
809 (set-buffer ebib-entry-buffer
)
810 (if (eq ebib-current-field
'crossref
)
812 (ebib-fill-entry-buffer)
813 (setq ebib-current-field
'crossref
)
814 (re-search-forward "^crossref")
815 (ebib-set-fields-highlight))
816 (with-buffer-writable
817 (goto-char (ebib-highlight-start ebib-fields-highlight
))
820 (delete-region beg
(point)))
821 (insert (format "%-17s " (symbol-name ebib-current-field
))
822 (ebib-get-field-highlighted ebib-current-field ebib-cur-entry-hash
))
823 (ebib-set-fields-highlight))))
825 (defun ebib-redisplay-current-string ()
826 "Redisplays the current string definition in the strings buffer."
827 (set-buffer ebib-strings-buffer
)
828 (with-buffer-writable
829 (let ((str (to-raw (gethash ebib-current-string
(edb-strings ebib-cur-db
)))))
830 (goto-char (ebib-highlight-start ebib-strings-highlight
))
833 (delete-region beg
(point)))
834 (insert (format "%-18s %s" ebib-current-string
835 (if (multiline-p str
)
836 (concat "+" (first-line str
))
838 (ebib-set-strings-highlight))))
840 (defun ebib-move-to-field (field direction
)
841 "Moves the fields overlay to the line containing FIELD.
842 If DIRECTION is positive, searches forward, if DIRECTION is
843 negative, searches backward. If DIRECTION is 1 or -1, searches
844 from POINT, if DIRECTION is 2 or -2, searches from beginning or
845 end of buffer. If FIELD is not found in the entry buffer, the
846 overlay is not moved. FIELD must be a symbol."
848 ;;Note: this function does NOT change the value of EBIB-CURRENT-FIELD!
850 (set-buffer ebib-entry-buffer
)
851 (if (eq field
'type
*)
852 (goto-char (point-min))
853 (multiple-value-bind (fn start limit
) (if (>= direction
0)
854 (values 're-search-forward
(point-min) (point-max))
855 (values 're-search-backward
(point-max) (point-min)))
856 ;; make sure we can get back to our original position, if the field
857 ;; cannot be found in the buffer:
858 (let ((current-pos (point)))
859 (when (evenp direction
)
861 (unless (funcall fn
(concat "^" (symbol-name field
)) limit t
)
862 (goto-char current-pos
)))))
863 (ebib-set-fields-highlight))
865 (defun ebib-create-collection (hashtable)
866 "Creates a list from the keys in HASHTABLE that can be used as COLLECTION in COMPLETING-READ.
867 The keys of HASHTABLE must be either symbols or strings."
869 (maphash #'(lambda (x y
)
870 (setq result
(cons (cons (symbol-or-string x
)
876 (defmacro ebib-retrieve-entry
(entry-key db
)
877 "Returns the hash table of the fields stored in DB under ENTRY-KEY."
878 `(gethash ,entry-key
(edb-database ,db
)))
880 (defun ebib-get-field-highlighted (field current-entry
&optional match-str
)
881 ;; note: we need to work on a copy of the string, otherwise the highlights
882 ;; are made to the string as stored in the database. hence copy-sequence.
883 (let ((case-fold-search t
)
884 (string (copy-sequence (gethash field current-entry
)))
888 ;; we have to do a couple of things now:
889 ;; - remove {} or "" around the string, if they're there
890 ;; - search for match-str
891 ;; - properly adjust the string if it's multiline
892 ;; but all this is not necessary if there was no string
894 (let* ((xref (to-raw (gethash 'crossref current-entry
)))
895 (xref-entry (ebib-retrieve-entry xref ebib-cur-db
)))
897 (setq string
(gethash field xref-entry
))
899 (setq string
(propertize (to-raw string
) 'face
'ebib-crossref-face
'fontified t
))
903 (setq string
(to-raw string
))) ; we have to make the string look nice
905 (multiple-value-setq (string matched
) (match-all match-str string
)))
906 (when (multiline-p string
)
907 ;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
908 ;; multiline is local and therefore the object it refers to should
909 ;; be GC'ed when the function returns. but for some reason, the
910 ;; plus sign is persistent, and if it's been highlighted as the
911 ;; result of a search, it stays that way.
912 (setq multiline
(propertize "+" 'face nil
))
913 (setq string
(first-line string
))))
915 (string= multiline
"+"))
916 (add-text-properties 0 1 '(face highlight
) multiline
))
917 (concat raw multiline string
)))
919 (defun ebib-format-fields (entry fn
&optional match-str
)
920 (let* ((entry-type (gethash 'type
* entry
))
921 (obl-fields (ebib-get-obl-fields entry-type
))
922 (opt-fields (ebib-get-opt-fields entry-type
)))
923 (funcall fn
(format "%-19s %s\n" "type" entry-type
))
924 (mapc #'(lambda (fields)
926 (mapcar #'(lambda (field)
927 (unless (and (get field
'ebib-hidden
)
928 ebib-hide-hidden-fields
)
929 (funcall fn
(format "%-17s " field
))
931 (ebib-get-field-highlighted field entry match-str
)
935 (list obl-fields opt-fields ebib-additional-fields
))))
937 (defun ebib-fill-entry-buffer (&optional match-str
)
938 "Fills the entry buffer with the fields of the current entry.
939 MATCH-STRING is a regexp that will be highlighted when it occurs in the
941 (set-buffer ebib-entry-buffer
)
942 (with-buffer-writable
944 (when (and ebib-cur-db
; do we have a database?
945 (edb-keys-list ebib-cur-db
) ; does it contain entries?
946 (gethash (car (edb-cur-entry ebib-cur-db
))
947 (edb-database ebib-cur-db
))) ; does the current entry exist?
948 (ebib-format-fields (gethash (car (edb-cur-entry ebib-cur-db
))
949 (edb-database ebib-cur-db
)) 'insert match-str
)
950 (setq ebib-current-field
'type
*)
951 (goto-char (point-min))
952 (ebib-set-fields-highlight)
953 (skip-chars-forward "^ "))))
955 (defun ebib-set-modified (mod &optional db
)
956 "Sets the modified flag of the database DB to MOD.
957 If DB is nil, it defaults to the current database, and the
958 modified flag of the index buffer is also (re)set. MOD must be
961 (setq db ebib-cur-db
))
962 (setf (edb-modified db
) mod
)
963 (when (eq db ebib-cur-db
)
965 (set-buffer ebib-index-buffer
)
966 (set-buffer-modified-p mod
))))
968 (defun ebib-modified-p ()
969 "Checks if any of the databases in Ebib were modified.
970 Returns the first modified database, or NIL if none was modified."
971 (let ((db (car ebib-databases
)))
973 (not (edb-modified db
)))
974 (setq db
(next-elem db ebib-databases
)))
977 (defun ebib-create-new-database (&optional db
)
978 "Creates a new database instance and returns it.
979 If DB is set to a database, the new database is a copy of DB."
984 (setq ebib-databases
(append ebib-databases
(list new-db
)))
987 (defun ebib-match-paren-forward (limit)
988 "Moves forward to the closing parenthesis matching the opening parenthesis at POINT.
989 This function handles parentheses () and braces {}. Does not
990 search/move beyond LIMIT. Returns T if a matching parenthesis was
991 found, NIL otherwise. If point was not at an opening parenthesis
992 at all, NIL is returned and point is not moved. If point was at
993 an opening parenthesis but no matching closing parenthesis was
994 found, an error is logged and point is moved one character
995 forward to allow parsing to continue."
997 ((eq (char-after) ?\
{)
998 (ebib-match-brace-forward limit
))
999 ((eq (char-after) ?\
()
1000 ;; we wrap this in a condition-case because we need to log the error
1001 ;; message outside of the save-restriction, otherwise we get the wrong
1005 (narrow-to-region (point) limit
)
1006 ;; this is really a hack. we want to allow unbalanced parentheses in
1007 ;; field values (bibtex does), so we cannot use forward-list
1008 ;; here. for the same reason, looking for the matching paren by hand
1009 ;; is pretty complicated. however, balanced parentheses can only be
1010 ;; used to enclose entire entries (or @STRINGs or @PREAMBLEs) so we
1011 ;; can be pretty sure we'll find it right before the next @ at the
1012 ;; start of a line, or right before the end of the file.
1013 (re-search-forward "^@" nil
0)
1014 (skip-chars-backward "@ \n\t\f")
1016 (if (eq (char-after) ?\
))
1018 (goto-char (1+ (point-min)))
1020 (error (ebib-log 'error
"Error in line %d: Matching closing parenthesis not found!" (line-number-at-pos))
1024 (defun ebib-match-delim-forward (limit)
1025 "Moves forward to the closing delimiter matching the opening delimiter at POINT.
1026 This function handles braces {} and double quotes \"\". Does not
1027 search/move beyond LIMIT. Returns T if a matching delimiter was
1028 found, NIL otherwise. If point was not at an opening delimiter at
1029 all, NIL is returned and point is not moved. If point was at an
1030 opening delimiter but no matching closing delimiter was found, an
1031 error is logged and point is moved one character forward to allow
1032 parsing to continue."
1034 ((eq (char-after) ?
\")
1035 (ebib-match-quote-forward limit
))
1036 ((eq (char-after) ?\
{)
1037 (ebib-match-brace-forward limit
))
1040 (defun ebib-match-brace-forward (limit)
1041 "Moves forward to the closing brace matching the opening brace at POINT.
1042 Does not search/move beyond LIMIT. Returns T if a matching brace
1043 was found, NIL otherwise. If point was not at an opening brace at
1044 all, NIL is returned and point is not moved. If point was at an
1045 opening brace but no matching closing brace was found, an error
1046 is logged and point is moved one character forward to allow
1047 parsing to continue."
1048 (when (eq (char-after) ?\
{) ; make sure we're really on a brace, otherwise return nil
1051 (narrow-to-region (point) limit
)
1054 ;; all of ebib expects that point moves to the closing
1055 ;; parenthesis, not right after it, so we adjust.
1057 t
)) ; return t because a matching brace was found
1059 (ebib-log 'error
"Error in line %d: Matching closing brace not found!" (line-number-at-pos))
1063 (defun ebib-match-quote-forward (limit)
1064 "Moves to the closing double quote matching the quote at POINT.
1065 Does not search/move beyond LIMIT. Returns T if a matching quote
1066 was found, NIL otherwise. If point was not at a double quote at
1067 all, NIL is returned and point is not moved. If point was at a
1068 quote but no matching closing quote was found, an error is logged
1069 and point is moved one character forward to allow parsing to
1071 (when (eq (char-after (point)) ?
\") ; make sure we're on a double quote.
1074 (narrow-to-region (point) limit
)
1076 (forward-char) ; move forward because we're on a double quote
1077 (skip-chars-forward "^\"") ; search the next double quote
1078 (eq (char-before) ?
\\))) ; if it's preceded by a backslash, keep on searching
1079 (or (eq (char-after) ?
\")
1081 (goto-char (1+ (point-min)))
1083 (error (ebib-log 'error
"Error in line %d: Matching closing quote not found!" (line-number-at-pos))
1086 (defun ebib-insert-entry (entry-key fields db
&optional sort timestamp
)
1087 "Stores the entry defined by ENTRY-KEY and FIELDS into DB.
1088 Optional argument SORT indicates whether the KEYS-LIST must be
1089 sorted after insertion. Default is NIL. Optional argument
1090 TIMESTAMP indicates whether a timestamp is to be added to the
1091 entry. Note that for a timestamp to be added, EBIB-USE-TIMESTAMP
1092 must also be set to T."
1093 (when (and timestamp ebib-use-timestamp
)
1094 (puthash 'timestamp
(from-raw (format-time-string ebib-timestamp-format
)) fields
))
1095 (puthash entry-key fields
(edb-database db
))
1096 (ebib-set-modified t db
)
1097 (setf (edb-n-entries db
) (1+ (edb-n-entries db
)))
1098 (setf (edb-keys-list db
)
1100 (sort (cons entry-key
(edb-keys-list db
)) 'string
<)
1101 (cons entry-key
(edb-keys-list db
)))))
1103 (defun ebib-insert-string (abbr string db
&optional sort
)
1104 "Stores the @STRING definition defined by ABBR and STRING into DB.
1105 Optional argument SORT indicates whether the STRINGS-LIST must be sorted
1106 after insertion. When loading or merging a file, for example, it is more
1107 economic to sort KEYS-LIST manually after all entries in the file have been
1109 (puthash abbr
(from-raw string
) (edb-strings db
))
1110 (ebib-set-modified t db
)
1111 (setf (edb-strings-list db
)
1113 (sort (cons abbr
(edb-strings-list db
)) 'string
<)
1114 (cons abbr
(edb-strings-list db
)))))
1116 (defmacro ebib-cur-entry-key
()
1117 "Returns the key of the current entry in EBIB-CUR-DB."
1118 `(car (edb-cur-entry ebib-cur-db
)))
1120 (defun ebib-search-key-in-buffer (entry-key)
1121 "Searches ENTRY-KEY in the index buffer.
1123 Moves point to the first character of the key and returns point."
1124 (goto-char (point-min))
1125 (search-forward entry-key
)
1129 ;; when we sort entries, we either use string< on the entry keys, or
1130 ;; ebib-entry<, if the user has defined a sort order.
1132 (defun ebib-entry< (x y
)
1133 "Returns T if entry X is smaller than entry Y.
1134 The entries are compared based on the fields listed in EBIB-SORT-ORDER. X
1135 and Y should be keys of entries in the current database."
1136 (let* ((sort-list ebib-sort-order
)
1137 (sortstring-x (to-raw (ebib-get-sortstring x
(car sort-list
))))
1138 (sortstring-y (to-raw (ebib-get-sortstring y
(car sort-list
)))))
1139 (while (and sort-list
1140 (string= sortstring-x sortstring-y
))
1141 (setq sort-list
(cdr sort-list
))
1142 (setq sortstring-x
(to-raw (ebib-get-sortstring x
(car sort-list
))))
1143 (setq sortstring-y
(to-raw (ebib-get-sortstring y
(car sort-list
)))))
1144 (if (and sortstring-x sortstring-y
)
1145 (string< sortstring-x sortstring-y
)
1148 (defun ebib-get-sortstring (entry-key sortkey-list
)
1149 "Returns the field value on which the entry ENTRY-KEY is to be sorted.
1150 ENTRY-KEY must be the key of an entry in the current database. SORTKEY-LIST
1151 is a list of fields that are considered in order for the sort value."
1152 (let ((sort-string nil
))
1153 (while (and sortkey-list
1154 (null (setq sort-string
(gethash (car sortkey-list
)
1155 (ebib-retrieve-entry entry-key ebib-cur-db
)))))
1156 (setq sortkey-list
(cdr sortkey-list
)))
1159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1160 ;; main program execution ;;
1161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1165 "Ebib, a BibTeX database manager."
1167 (if (or (equal (window-buffer) ebib-index-buffer
)
1168 (equal (window-buffer) ebib-entry-buffer
))
1169 (error "Ebib already active")
1170 ;; we save the buffer from which ebib is called
1171 (setq ebib-push-buffer
(current-buffer))
1172 ;; initialize ebib if required
1173 (unless ebib-initialized
1175 (if ebib-preload-bib-files
1176 (mapc #'(lambda (file)
1177 (ebib-load-bibtex-file file
))
1178 ebib-preload-bib-files
)))
1179 ;; if ebib is visible, we just switch to the index buffer
1180 (let ((index-window (get-buffer-window ebib-index-buffer
)))
1182 (select-window index-window nil
)
1183 (ebib-setup-windows)))))
1185 (defun ebib-setup-windows ()
1186 "Create the window configuration we want for ebib in the
1188 ;; we save the current window configuration.
1189 (setq ebib-saved-window-config
(current-window-configuration))
1190 (unless (eq ebib-layout
'full
)
1191 (setq ebib-pre-ebib-window
(selected-window))
1192 (let ((ebib-window (split-window (selected-window) (- (window-width) ebib-layout
) t
)))
1193 (select-window ebib-window nil
)))
1194 (let* ((index-window (selected-window))
1195 (entry-window (split-window index-window ebib-index-window-size
)))
1196 (switch-to-buffer ebib-index-buffer
)
1197 (set-window-buffer entry-window ebib-entry-buffer
)
1198 (unless (eq ebib-layout
'full
)
1199 (set-window-dedicated-p index-window t
)
1200 (set-window-dedicated-p entry-window t
))))
1204 This function sets all variables to their initial values, creates the
1205 buffers and reads the rc file."
1206 (setq ebib-cur-entry-hash nil
1207 ebib-current-field nil
1208 ebib-minibuf-hist nil
1209 ebib-saved-window-config nil
)
1210 (put 'timestamp
'ebib-hidden t
)
1211 (load "~/.ebibrc" t
)
1212 (ebib-create-buffers)
1213 (setq ebib-index-highlight
(ebib-make-highlight 1 1 ebib-index-buffer
))
1214 (setq ebib-fields-highlight
(ebib-make-highlight 1 1 ebib-entry-buffer
))
1215 (setq ebib-strings-highlight
(ebib-make-highlight 1 1 ebib-strings-buffer
))
1216 (setq ebib-initialized t
))
1218 (defun ebib-create-buffers ()
1219 "Creates the buffers for Ebib."
1220 ;; first we create a buffer for multiline editing. this one does *not*
1221 ;; have a name beginning with a space, because undo-info is normally
1222 ;; present in an edit buffer.
1223 (setq ebib-multiline-buffer
(get-buffer-create "*Ebib-edit*"))
1224 (set-buffer ebib-multiline-buffer
)
1225 (ebib-multiline-edit-mode)
1226 ;; then we create a buffer to hold the fields of the current entry.
1227 (setq ebib-entry-buffer
(get-buffer-create " *Ebib-entry*"))
1228 (set-buffer ebib-entry-buffer
)
1230 ;; then we create a buffer to hold the @STRING definitions
1231 (setq ebib-strings-buffer
(get-buffer-create " *Ebib-strings*"))
1232 (set-buffer ebib-strings-buffer
)
1234 ;; then we create the help buffer
1235 (setq ebib-help-buffer
(get-buffer-create " *Ebib-help*"))
1236 (set-buffer ebib-help-buffer
)
1239 (setq ebib-log-buffer
(get-buffer-create " *Ebib-log*"))
1240 (set-buffer ebib-log-buffer
)
1242 (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")
1244 ;; and lastly we create a buffer for the entry keys.
1245 (setq ebib-index-buffer
(get-buffer-create " none"))
1246 (set-buffer ebib-index-buffer
)
1251 The Ebib buffers are killed, all variables except the keymaps are set to nil."
1253 (when (if (ebib-modified-p)
1254 (yes-or-no-p "There are modified databases. Quit anyway? ")
1255 (y-or-n-p "Quit Ebib? "))
1256 (mapc #'(lambda (buffer)
1257 (kill-buffer buffer
))
1258 (list ebib-entry-buffer
1261 ebib-multiline-buffer
1264 (setq ebib-databases nil
1265 ebib-index-buffer nil
1266 ebib-entry-buffer nil
1267 ebib-initialized nil
1268 ebib-index-highlight nil
1269 ebib-fields-highlight nil
1270 ebib-strings-highlight nil
1271 ebib-export-filename nil
1272 ebib-pre-ebib-window nil
)
1273 (set-window-configuration ebib-saved-window-config
)
1276 (defun ebib-kill-emacs-query-function ()
1277 "Ask if the user wants to save the database loaded in Ebib when Emacs is
1278 killed and the database has been modified."
1279 (if (not (ebib-modified-p))
1281 (if (y-or-n-p "Save all unsaved Ebib databases? ")
1283 (ebib-save-all-databases)
1285 (yes-or-no-p "Ebib database was modified. Kill anyway? "))))
1287 (add-hook 'kill-emacs-query-functions
'ebib-kill-emacs-query-function
)
1294 (define-prefix-command 'ebib-prefix-map
)
1295 (suppress-keymap ebib-prefix-map
)
1296 (defvar ebib-prefixed-functions
'(ebib-delete-entry
1301 ebib-push-entry-key
)))
1303 ;; macro to redefine key bindings.
1305 (defmacro ebib-key
(buffer key
&optional command
)
1308 (let ((one `(define-key ebib-index-mode-map
,key
(quote ,command
)))
1309 (two (when (or (null command
)
1310 (member command ebib-prefixed-functions
))
1311 `(define-key ebib-prefix-map
,key
(quote ,command
)))))
1316 `(define-key ebib-entry-mode-map
,key
(quote ,command
)))
1317 ((eq buffer
'strings
)
1318 `(define-key ebib-strings-mode-map
,key
(quote ,command
)))
1319 ((eq buffer
'mark-prefix
)
1321 (define-key ebib-index-mode-map
(format "%c" ebib-prefix-key
) nil
)
1322 (define-key ebib-index-mode-map
,key
'ebib-prefix-map
)
1323 (setq ebib-prefix-key
(string-to-char ,key
))))))
1325 (defvar ebib-index-mode-map
1326 (let ((map (make-keymap)))
1327 (suppress-keymap map
)
1329 "Keymap for the ebib index buffer.")
1331 ;; we define the keys with ebib-key rather than with define-key, because
1332 ;; that automatically sets up ebib-prefix-map as well.
1333 (ebib-key index
[up] ebib-prev-entry)
1334 (ebib-key index [down] ebib-next-entry)
1335 (ebib-key index [right] ebib-next-database)
1336 (ebib-key index [left] ebib-prev-database)
1337 (ebib-key index [prior] ebib-index-scroll-down)
1338 (ebib-key index [next] ebib-index-scroll-up)
1339 (ebib-key index [home] ebib-goto-first-entry)
1340 (ebib-key index [end] ebib-goto-last-entry)
1341 (ebib-key index [return] ebib-select-entry)
1342 (ebib-key index " " ebib-index-scroll-up)
1343 (ebib-key index "/" ebib-search)
1344 (ebib-key index "&" ebib-virtual-db-and)
1345 (ebib-key index "|" ebib-virtual-db-or)
1346 (ebib-key index "~" ebib-virtual-db-not)
1347 (ebib-key index ";" ebib-prefix-map)
1348 (ebib-key index "a" ebib-add-entry)
1349 (ebib-key index "b" ebib-index-scroll-down)
1350 (ebib-key index "c" ebib-close-database)
1351 (ebib-key index "C" ebib-customize)
1352 (ebib-key index "d" ebib-delete-entry)
1353 (ebib-key index "e" ebib-edit-entry)
1354 (ebib-key index "E" ebib-edit-keyname)
1355 (ebib-key index "f" ebib-view-file)
1356 (ebib-key index "F" ebib-follow-crossref)
1357 (ebib-key index "g" ebib-goto-first-entry)
1358 (ebib-key index "G" ebib-goto-last-entry)
1359 (ebib-key index "h" ebib-index-help)
1360 (ebib-key index "H" ebib-toggle-hidden)
1361 (ebib-key index "j" ebib-next-entry)
1362 (ebib-key index "J" ebib-switch-to-database)
1363 (ebib-key index "k" ebib-prev-entry)
1364 (ebib-key index "l" ebib-show-log)
1365 (ebib-key index "L" ebib-latex-entries)
1366 (ebib-key index "m" ebib-mark-entry)
1367 (ebib-key index "M" ebib-merge-bibtex-file)
1368 (ebib-key index "n" ebib-search-next)
1369 (ebib-key index [(control n)] ebib-next-entry)
1370 (ebib-key index [(meta n)] ebib-index-scroll-up)
1371 (ebib-key index "o" ebib-load-bibtex-file)
1372 (ebib-key index "p" ebib-push-entry-key)
1373 (ebib-key index [(control p)] ebib-prev-entry)
1374 (ebib-key index [(meta p)] ebib-index-scroll-down)
1375 (ebib-key index "P" ebib-print-entries)
1376 (ebib-key index "r" ebib-edit-preamble)
1377 (ebib-key index "q" ebib-quit)
1378 (ebib-key index "s" ebib-save-current-database)
1379 (ebib-key index "S" ebib-save-all-databases)
1380 (ebib-key index "t" ebib-edit-strings)
1381 (ebib-key index "u" ebib-browse-url)
1382 (ebib-key index "V" ebib-print-filter)
1383 (ebib-key index "w" ebib-write-database)
1384 (ebib-key index "x" ebib-export-entry)
1385 (ebib-key index "\C-xb" ebib-leave-ebib-windows)
1386 (ebib-key index "\C-xk" ebib-quit)
1387 (ebib-key index "X" ebib-export-preamble)
1388 (ebib-key index "z" ebib-leave-ebib-windows)
1389 (ebib-key index "Z" ebib-lower)
1391 (defun ebib-switch-to-database-nth (key)
1392 (interactive (list (if (featurep 'xemacs)
1393 (event-key last-command-event)
1394 last-command-event)))
1395 (ebib-switch-to-database (- (if (featurep 'xemacs)
1399 (mapc #'(lambda (key)
1400 (define-key ebib-index-mode-map (format "%d" key)
1401 'ebib-switch-to-database-nth))
1402 '(1 2 3 4 5 6 7 8 9))
1404 (define-derived-mode ebib-index-mode
1405 fundamental-mode "Ebib-index"
1406 "Major mode for the Ebib index buffer."
1407 (setq buffer-read-only t)
1408 (setq truncate-lines t))
1410 (defun ebib-fill-index-buffer ()
1411 "Fills the index buffer with the list of keys in EBIB-CUR-DB.
1412 If EBIB-CUR-DB is nil, the buffer is just erased and its name set
1414 (set-buffer ebib-index-buffer)
1415 (let ((buffer-read-only nil))
1419 ;; we may call this function when there are no entries in the
1420 ;; database. if so, we don't need to do this:
1421 (when (edb-cur-entry ebib-cur-db)
1422 (mapc #'(lambda (entry)
1423 (ebib-display-entry entry)
1424 (when (member entry (edb-marked-entries ebib-cur-db))
1429 (skip-chars-forward "^ ")
1430 (add-text-properties beg (point) '(face ebib-marked-face))))))
1431 (edb-keys-list ebib-cur-db))
1432 (goto-char (point-min))
1433 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
1435 (ebib-set-index-highlight))
1436 (set-buffer-modified-p (edb-modified ebib-cur-db))
1437 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
1438 (length (member ebib-cur-db ebib-databases)))))
1439 (edb-name ebib-cur-db))))
1440 (rename-buffer " none"))))
1442 (defun ebib-customize ()
1443 "Switches to Ebib's customisation group."
1446 (customize-group 'ebib))
1448 (defun ebib-log (type format-string &rest args)
1449 "Writes a message to Ebib's log buffer.
1450 TYPE (a symbol) is the type of message. It can be LOG, which
1451 writes the message to the log buffer only; MESSAGE, which writes
1452 the message to the log buffer and outputs it with the function
1453 MESSAGE; WARNING, which logs the message and sets the variable
1454 EBIB-LOG-ERROR to 0; or ERROR, which logs the message and sets
1455 the variable EBIB-LOG-ERROR to 1. The latter two can be used to
1456 signal the user to check the log for warnings or errors."
1458 (set-buffer ebib-log-buffer)
1461 (or ebib-log-error ; if ebib-error-log is already set to 1, we don't want to overwrite it!
1462 (setq ebib-log-error 0)))
1464 (setq ebib-log-error 1))
1466 (apply 'message format-string args)))
1467 (insert (apply 'format (concat format-string "\n") args))))
1469 (defun ebib-load-bibtex-file (&optional file)
1470 "Loads a BibTeX file into ebib."
1473 (setq file (ensure-extension (read-file-name "File to open: " "~/") "bib")))
1474 (setq ebib-cur-db (ebib-create-new-database))
1475 (setf (edb-filename ebib-cur-db) (expand-file-name file))
1476 (setf (edb-name ebib-cur-db) (file-name-nondirectory (edb-filename ebib-cur-db)))
1477 (setq ebib-log-error nil) ; we haven't found any errors
1478 (ebib-log 'log "%s: Opening file %s" (format-time-string "%d %b %Y, %H:%M:%S") (edb-filename ebib-cur-db))
1479 ;; first, we empty the buffers
1480 (ebib-erase-buffer ebib-index-buffer)
1481 (ebib-erase-buffer ebib-entry-buffer)
1482 (if (file-readable-p file)
1483 ;; if the user entered the name of an existing file, we load it
1484 ;; by putting it in a buffer and then parsing it.
1486 (with-syntax-table ebib-syntax-table
1487 (insert-file-contents file)
1488 ;; if the user makes any changes, we'll want to create a back-up.
1489 (setf (edb-make-backup ebib-cur-db) t)
1490 (let ((result (ebib-find-bibtex-entries nil)))
1491 (setf (edb-n-entries ebib-cur-db) (car result))
1492 (when (edb-keys-list ebib-cur-db)
1493 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<)))
1494 (when (edb-strings-list ebib-cur-db)
1495 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1496 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1497 ;; and fill the buffers. note that filling a buffer also makes
1498 ;; that buffer active. therefore we do EBIB-FILL-INDEX-BUFFER
1500 (ebib-set-modified nil)
1501 (ebib-fill-entry-buffer)
1502 ;; and now we tell the user the result
1503 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1509 ;; if the file does not exist, we need to issue a message.
1510 (ebib-log 'message "(New file)"))
1511 ;; what we have to do in *any* case, is fill the index buffer. (this
1512 ;; even works if there are no keys in the database, e.g. when the
1513 ;; user opened a new file or if no BibTeX entries were found.
1514 (ebib-fill-index-buffer)
1515 (when ebib-log-error
1516 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1517 (ebib-log 'log "\n\f\n"))
1519 (defun ebib-merge-bibtex-file ()
1520 "Merges a BibTeX file into the database."
1522 (unless (edb-virtual ebib-cur-db)
1523 (if (not ebib-cur-db)
1524 (error "No database loaded. Use `o' to open a database")
1525 (let ((file (read-file-name "File to merge: ")))
1526 (setq ebib-log-error nil) ; we haven't found any errors
1527 (ebib-log 'log "%s: Merging file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") (edb-filename ebib-cur-db))
1529 (with-syntax-table ebib-syntax-table
1530 (insert-file-contents file)
1531 (let ((n (ebib-find-bibtex-entries t)))
1532 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
1533 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
1534 (when (edb-strings-list ebib-cur-db)
1535 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
1536 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1537 (ebib-fill-entry-buffer)
1538 (ebib-fill-index-buffer)
1539 (ebib-set-modified t)
1540 (ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
1546 (when ebib-log-error
1547 (message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))
1548 (ebib-log 'log "\n\f\n"))))))))
1550 (defun ebib-find-bibtex-entries (timestamp)
1551 "Finds the BibTeX entries in the current buffer.
1552 The search is started at the beginnig of the buffer. All entries
1553 found are stored in the hash table DATABASE of
1554 EBIB-CUR-DB. Returns a three-element list: the first element is
1555 the number of entries found, the second the number of @STRING
1556 definitions, and the third is T or NIL, indicating whether a
1557 @PREAMBLE was found.
1559 TIMESTAMP indicates whether a timestamp is to be added to each
1560 entry. Note that a timestamp is only added if EBIB-USE-TIMESTAMP
1565 (goto-char (point-min))
1566 (while (re-search-forward "^@" nil t) ; find the next entry
1567 (let ((beg (point)))
1568 (when (looking-at-goto-end (concat ebib-bibtex-identifier "[\(\{]"))
1569 (let ((entry-type (downcase (buffer-substring-no-properties beg (1- (point))))))
1571 ((equal entry-type "string") ; string and preamble must be treated differently
1572 (if (ebib-read-string)
1573 (setq n-strings (1+ n-strings))))
1574 ((equal entry-type "preamble")
1575 (when (ebib-read-preamble)
1577 ((equal entry-type "comment") ; ignore comments
1578 (ebib-log 'log "Comment at line %d ignored" (line-number-at-pos))
1579 (ebib-match-paren-forward (point-max)))
1580 ((gethash (intern-soft entry-type) ebib-entry-types-hash) ; if the entry type has been defined
1581 (if (ebib-read-entry entry-type timestamp)
1582 (setq n-entries (1+ n-entries))))
1583 ;; anything else we report as an unknown entry type.
1584 (t (ebib-log 'warning "Line %d: Unknown entry type `%s'. Skipping." (line-number-at-pos) entry-type)
1585 (ebib-match-paren-forward (point-max))))))))
1586 (list n-entries n-strings preamble)))
1588 (defun ebib-read-string ()
1589 "Reads the @STRING definition beginning at the line POINT is on.
1590 If a proper abbreviation and string are found, they are stored in the
1591 database. Returns the string if one was read, nil otherwise."
1592 (let ((limit (save-excursion ; we find the matching end parenthesis
1594 (ebib-match-paren-forward (point-max))
1596 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1597 (let ((beg (point)))
1598 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1599 (if-str (abbr (buffer-substring-no-properties beg (point)))
1601 (skip-chars-forward "^\"{" limit)
1602 (let ((beg (point)))
1603 (if-str (string (if (ebib-match-delim-forward limit)
1604 (buffer-substring-no-properties beg (1+ (point)))
1606 (if (member abbr (edb-strings-list ebib-cur-db))
1607 (ebib-log 'warning (format "Line %d: @STRING definition `%s' duplicated. Skipping."
1608 (line-number-at-pos) abbr))
1609 (ebib-insert-string abbr string ebib-cur-db))))))))))
1611 (defun ebib-read-preamble ()
1612 "Reads the @PREAMBLE definition and stores it in EBIB-PREAMBLE.
1613 If there was already another @PREAMBLE definition, the new one is
1614 added to the existing one with a hash sign `#' between them."
1615 (let ((beg (point)))
1617 (when (ebib-match-paren-forward (point-max))
1618 (let ((text (buffer-substring-no-properties beg (point))))
1619 (if (edb-preamble ebib-cur-db)
1620 (setf (edb-preamble ebib-cur-db) (concat (edb-preamble ebib-cur-db) "\n# " text))
1621 (setf (edb-preamble ebib-cur-db) text))))))
1623 (defun ebib-read-entry (entry-type &optional timestamp)
1624 "Reads a BibTeX entry and stores it in DATABASE of EBIB-CUR-DB.
1625 Returns the new EBIB-KEYS-LIST if an entry was found, nil
1626 otherwise. Optional argument TIMESTAMP indicates whether a
1627 timestamp is to be added. (Whether a timestamp is actually added,
1628 also depends on EBIB-USE-TIMESTAMP.)"
1629 (let ((entry-limit (save-excursion
1631 (ebib-match-paren-forward (point-max))
1634 (skip-chars-forward " \n\t\f") ; note the space!
1636 (when (looking-at-goto-end (concat "\\("
1637 ebib-bibtex-identifier
1639 1) ; this delimits the entry key
1640 (let ((entry-key (buffer-substring-no-properties beg (point))))
1641 (if (member entry-key (edb-keys-list ebib-cur-db))
1642 (ebib-log 'warning "Line %d: Entry `%s' duplicated. Skipping." (line-number-at-pos) entry-key)
1643 (let ((fields (ebib-find-bibtex-fields (intern-soft entry-type) entry-limit)))
1644 (when fields ; if fields were found, we store them, and return T.
1645 (ebib-insert-entry entry-key fields ebib-cur-db nil timestamp)
1648 (defun ebib-find-bibtex-fields (entry-type limit)
1649 "Finds the fields of the BibTeX entry that starts on the line POINT is on.
1650 Returns a hash table containing all the fields and values, or NIL
1651 if none were found. ENTRY-TYPE is the type of the entry, which
1652 will be recorded in the hash table. Before the search starts,
1653 POINT is moved back to the beginning of the line."
1655 ;; we assign a function to fn in order to avoid putting the test on
1656 ;; ebib-allow-identical-fields in the while loop, where it would get
1657 ;; tested with every field being read.
1658 (let ((fn (if (not ebib-allow-identical-fields)
1659 (symbol-function 'puthash)
1660 #'(lambda (field-type field-contents fields)
1661 (let ((existing-contents (gethash field-type fields)))
1662 (puthash field-type (if existing-contents
1663 (from-raw (concat (to-raw existing-contents)
1665 (to-raw field-contents)))
1668 (let ((fields (make-hash-table :size 15)))
1670 (skip-chars-forward "^," limit) ; we must move to the next comma,
1671 (eq (char-after) ?,)) ; and make sure we are really on a comma.
1672 (skip-chars-forward "\"#%'(),={} \n\t\f" limit)
1673 (let ((beg (point)))
1674 (when (looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
1675 (let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
1676 (unless (eq field-type 'type*) ; the 'type*' key holds the entry type, so we can't use it
1677 (let ((field-contents (ebib-get-field-contents limit)))
1678 (when field-contents
1679 (funcall fn field-type field-contents fields))))))))
1680 (when (> (hash-table-count fields) 0)
1681 (puthash 'type* entry-type fields)
1684 (defun ebib-get-field-contents (limit)
1685 "Gets the contents of a BibTeX field.
1686 LIMIT indicates the end of the entry, beyond which the function will not
1688 (skip-chars-forward "#%'(),=} \n\t\f" limit)
1689 (let ((beg (point)))
1690 (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
1692 (defun ebib-find-end-of-field (limit)
1693 "Moves POINT to the end of a field's contents and returns POINT.
1694 The contents of a field is delimited by a comma or by the closing brace of
1695 the entry. The latter is at position LIMIT."
1696 (while (and (not (eq (char-after) ?\,))
1698 (ebib-match-delim-forward limit) ; check if we're on a delimiter and if so, jump to the matching closing delimiter
1700 (if (= (point) limit)
1701 (skip-chars-backward " \n\t\f"))
1704 (defun ebib-leave-ebib-windows ()
1705 "Leaves the Ebib windows, lowering them if necessary."
1709 (defun ebib-lower (&optional soft)
1710 "Hides the Ebib windows.
1711 If optional argument SOFT is non-nil, just switch to a non-Ebib
1712 buffer if Ebib is not occupying the entire frame."
1714 (unless (member (window-buffer) (list ebib-index-buffer
1717 ebib-multiline-buffer
1720 (error "Ebib is not active "))
1722 (not (eq ebib-layout 'full)))
1723 (select-window ebib-pre-ebib-window nil)
1724 (set-window-configuration ebib-saved-window-config))
1725 (mapc #'(lambda (buffer)
1726 (bury-buffer buffer))
1727 (list ebib-index-buffer
1730 ebib-multiline-buffer
1734 (defun ebib-prev-entry ()
1735 "Moves to the previous BibTeX entry."
1739 ;; if the current entry is the first entry,
1740 (if (eq (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1742 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)
1743 (1+ (length (edb-cur-entry ebib-cur-db)))))
1744 (goto-char (ebib-highlight-start ebib-index-highlight))
1746 (ebib-set-index-highlight)
1747 (ebib-fill-entry-buffer)))
1751 (defun ebib-next-entry ()
1752 "Moves to the next BibTeX entry."
1756 (if (= (length (edb-cur-entry ebib-cur-db)) 1) ; if we're on the last entry,
1758 (setf (edb-cur-entry ebib-cur-db)
1759 (last (edb-keys-list ebib-cur-db) (1- (length (edb-cur-entry ebib-cur-db)))))
1760 (goto-char (ebib-highlight-start ebib-index-highlight))
1762 (ebib-set-index-highlight)
1763 (ebib-fill-entry-buffer)))
1767 (defun ebib-add-entry ()
1768 "Adds a new entry to the database."
1772 (if-str (entry-key (read-string "New entry key: "))
1774 (if (member entry-key (edb-keys-list ebib-cur-db))
1775 (error "Key already exists")
1776 (set-buffer ebib-index-buffer)
1777 (sort-in-buffer (1+ (edb-n-entries ebib-cur-db)) entry-key)
1778 (with-buffer-writable
1779 (ebib-display-entry entry-key))
1780 (forward-line -1) ; move one line up to position the cursor on the new entry.
1781 (ebib-set-index-highlight)
1782 (let ((fields (make-hash-table)))
1783 (puthash 'type* ebib-default-type fields)
1784 (ebib-insert-entry entry-key fields ebib-cur-db t t))
1785 (setf (edb-cur-entry ebib-cur-db) (member entry-key (edb-keys-list ebib-cur-db)))
1786 (ebib-fill-entry-buffer)
1788 (ebib-set-modified t)))))
1790 (error "No database open. Use `o' to open a database first"))
1794 (defun ebib-close-database ()
1795 "Closes the current BibTeX database."
1799 (when (if (edb-modified ebib-cur-db)
1800 (yes-or-no-p "Database modified. Close it anyway? ")
1801 (y-or-n-p "Close database? "))
1802 (let ((to-be-deleted ebib-cur-db)
1803 (new-db (next-elem ebib-cur-db ebib-databases)))
1804 (setq ebib-databases (delete to-be-deleted ebib-databases))
1805 (if ebib-databases ; do we still have another database loaded?
1807 (setq ebib-cur-db (or new-db
1808 (last1 ebib-databases)))
1809 (unless (edb-cur-entry ebib-cur-db)
1810 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db)))
1811 (ebib-fill-entry-buffer)
1812 (ebib-fill-index-buffer))
1813 ;; otherwise, we have to clean up a little and empty all the buffers.
1814 (setq ebib-cur-db nil)
1815 (mapc #'(lambda (buf) ; this is just to avoid typing almost the same thing three times...
1816 (set-buffer (car buf))
1817 (with-buffer-writable
1819 (ebib-delete-highlight (cadr buf)))
1820 (list (list ebib-entry-buffer ebib-fields-highlight)
1821 (list ebib-index-buffer ebib-index-highlight)
1822 (list ebib-strings-buffer ebib-strings-highlight)))
1823 ;; multiline edit buffer
1824 (set-buffer ebib-multiline-buffer)
1825 (with-buffer-writable
1827 (set-buffer ebib-index-buffer)
1828 (rename-buffer " none"))
1829 (message "Database closed."))))))
1831 (defun ebib-goto-first-entry ()
1832 "Moves to the first BibTeX entry in the database."
1836 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
1837 (set-buffer ebib-index-buffer)
1838 (goto-char (point-min))
1839 (ebib-set-index-highlight)
1840 (ebib-fill-entry-buffer))
1844 (defun ebib-goto-last-entry ()
1845 "Moves to the last entry in the BibTeX database."
1849 (setf (edb-cur-entry ebib-cur-db) (last (edb-keys-list ebib-cur-db)))
1850 (set-buffer ebib-index-buffer)
1851 (goto-line (edb-n-entries ebib-cur-db))
1852 (ebib-set-index-highlight)
1853 (ebib-fill-entry-buffer))
1857 (defun ebib-edit-entry ()
1858 "Edits the current BibTeX entry."
1862 (setq ebib-cur-entry-hash (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
1863 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
1864 (select-window (get-buffer-window ebib-entry-buffer) nil)
1865 ;; (switch-to-buffer ebib-entry-buffer)
1866 (goto-char (ebib-highlight-end ebib-fields-highlight)))
1870 (defun ebib-edit-keyname ()
1871 "Change the key of a BibTeX entry."
1875 (let ((cur-keyname (ebib-cur-entry-key)))
1876 (if-str (new-keyname (read-string (format "Change `%s' to: " cur-keyname)
1878 (if (member new-keyname (edb-keys-list ebib-cur-db))
1879 (error (format "Key `%s' already exists" new-keyname))
1880 (unless (string= cur-keyname new-keyname)
1881 (let ((fields (ebib-retrieve-entry cur-keyname ebib-cur-db))
1882 (marked (member cur-keyname (edb-marked-entries ebib-cur-db))))
1883 (ebib-remove-entry-from-db cur-keyname ebib-cur-db)
1884 (ebib-remove-key-from-buffer cur-keyname)
1885 (ebib-insert-entry new-keyname fields ebib-cur-db t nil)
1886 (setf (edb-cur-entry ebib-cur-db) (member new-keyname (edb-keys-list ebib-cur-db)))
1887 (sort-in-buffer (edb-n-entries ebib-cur-db) new-keyname)
1888 (with-buffer-writable
1889 (ebib-display-entry new-keyname))
1890 (forward-line -1) ; move one line up to position the cursor on the new entry.
1891 (ebib-set-index-highlight)
1892 (ebib-set-modified t)
1893 (when marked (ebib-mark-entry))))))))
1897 (defun ebib-mark-entry ()
1898 "Mark (or unmark) the current entry."
1900 (if (ebib-called-with-prefix)
1903 (setf (edb-marked-entries ebib-cur-db) nil)
1904 (ebib-fill-index-buffer))
1906 (setf (edb-marked-entries ebib-cur-db) (copy-sequence (edb-keys-list ebib-cur-db)))
1907 (ebib-fill-index-buffer))
1912 (set-buffer ebib-index-buffer)
1913 (with-buffer-writable
1914 (if (member (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db))
1916 (setf (edb-marked-entries ebib-cur-db)
1917 (delete (ebib-cur-entry-key) (edb-marked-entries ebib-cur-db)))
1918 (remove-text-properties (ebib-highlight-start ebib-index-highlight)
1919 (ebib-highlight-end ebib-index-highlight)
1920 '(face ebib-marked-face)))
1921 (setf (edb-marked-entries ebib-cur-db) (sort (cons (ebib-cur-entry-key)
1922 (edb-marked-entries ebib-cur-db))
1924 (add-text-properties (ebib-highlight-start ebib-index-highlight)
1925 (ebib-highlight-end ebib-index-highlight)
1926 '(face ebib-marked-face)))))
1930 (defun ebib-index-scroll-down ()
1931 "Move one page up in the database."
1936 (ebib-select-entry))
1940 (defun ebib-index-scroll-up ()
1941 "Move one page down in the database."
1946 (ebib-select-entry))
1950 (defun ebib-format-entry (key db timestamp)
1951 "Format entry KEY from database DB into the current buffer in BibTeX format.
1952 If TIMESTAMP is T, a timestamp is added to the entry if
1953 EBIB-USE-TIMESTAMP is T."
1954 (let ((entry (ebib-retrieve-entry key db)))
1956 (insert (format "@%s{%s,\n" (gethash 'type* entry) key))
1957 (maphash #'(lambda (key value)
1958 (unless (or (eq key 'type*)
1959 (and (eq key 'timestamp) timestamp ebib-use-timestamp))
1960 (insert (format "\t%s = %s,\n" key value))))
1962 (if (and timestamp ebib-use-timestamp)
1963 (insert (format "\ttimestamp = {%s}" (format-time-string ebib-timestamp-format)))
1964 (delete-char -2)) ; the final ",\n" must be deleted
1965 (insert "\n}\n\n"))))
1967 (defun ebib-format-strings (db)
1968 "Format the @STRING commands in database DB."
1969 (maphash #'(lambda (key value)
1970 (insert (format "@STRING{%s = %s}\n" key value)))
1974 (defun ebib-compare-xrefs (x y)
1975 (gethash 'crossref (ebib-retrieve-entry x ebib-cur-db)))
1977 (defun ebib-format-database (db)
1978 "Writes database DB into the current buffer in BibTeX format."
1979 (when (edb-preamble db)
1980 (insert (format "@PREAMBLE{%s}\n\n" (edb-preamble db))))
1981 (ebib-format-strings db)
1982 (let ((sorted-list (copy-list (edb-keys-list db))))
1984 (ebib-save-xrefs-first
1985 (setq sorted-list (sort sorted-list 'ebib-compare-xrefs)))
1987 (setq sorted-list (sort sorted-list 'ebib-entry<))))
1988 (mapc #'(lambda (key) (ebib-format-entry key db nil)) sorted-list)))
1990 (defun ebib-save-database (db)
1991 "Saves the database DB."
1994 (when (and (edb-make-backup db)
1995 (file-exists-p (edb-filename db)))
1996 (rename-file (edb-filename db) (concat (edb-filename db) "~") t)
1997 (setf (edb-make-backup db) nil))
1999 (ebib-format-database db)
2000 (write-region (point-min) (point-max) (edb-filename db)))
2001 (ebib-set-modified nil db))))
2003 (defun ebib-write-database ()
2004 "Writes the current database to a different file.
2005 Can also be used to change a virtual database into a real one."
2009 (if-str (new-filename (read-file-name "Save to file: " "~/"))
2012 (ebib-format-database ebib-cur-db)
2013 (safe-write-region (point-min) (point-max) new-filename nil nil nil t))
2014 ;; if SAFE-WRITE-REGION was cancelled by the user because he
2015 ;; didn't want to overwrite an already existing file with his
2016 ;; new database, it throws an error, so the next lines will not
2017 ;; be executed. hence we can safely set (EDB-FILENAME DB) and
2019 (setf (edb-filename ebib-cur-db) new-filename)
2020 (setf (edb-name ebib-cur-db) (file-name-nondirectory new-filename))
2021 (rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
2022 (length (member ebib-cur-db ebib-databases)))))
2023 (edb-name ebib-cur-db)))
2026 (setf (edb-virtual ebib-cur-db) nil)
2027 (setf (edb-database ebib-cur-db)
2028 (let ((new-db (make-hash-table :test 'equal)))
2029 (mapc #'(lambda (key)
2030 (let ((entry (gethash key (edb-database ebib-cur-db))))
2032 (puthash key (copy-hash-table entry) new-db))))
2033 (edb-keys-list ebib-cur-db))
2035 (ebib-set-modified nil))))
2039 (defun ebib-save-current-database ()
2040 "Saves the current database."
2044 (if (not (edb-modified ebib-cur-db))
2045 (message "No changes need to be saved.")
2046 (ebib-save-database ebib-cur-db)))
2048 (error "Cannot save a virtual database. Use `w' to write to a file."))))
2050 (defun ebib-save-all-databases ()
2051 "Saves all currently open databases if they were modified."
2055 (mapc #'(lambda (db)
2056 (when (edb-modified db)
2057 (ebib-save-database db)))
2059 (message "All databases saved."))))
2061 (defun ebib-print-filename ()
2062 "Displays the filename of the current database in the minibuffer."
2064 (message (edb-filename ebib-cur-db)))
2066 (defun ebib-follow-crossref ()
2067 "Goes to the entry mentioned in the crossref field of the current entry."
2069 (let ((new-cur-entry (to-raw (gethash 'crossref
2070 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2071 (setf (edb-cur-entry ebib-cur-db)
2072 (or (member new-cur-entry (edb-keys-list ebib-cur-db))
2073 (edb-cur-entry ebib-cur-db))))
2074 (ebib-fill-entry-buffer)
2075 (ebib-fill-index-buffer))
2077 (defun ebib-toggle-hidden ()
2079 (setq ebib-hide-hidden-fields (not ebib-hide-hidden-fields))
2080 (ebib-fill-entry-buffer))
2082 (defun ebib-delete-entry ()
2083 "Deletes the current entry from the database."
2085 (if (ebib-called-with-prefix)
2087 ((real-db marked-entries)
2088 (when (y-or-n-p "Delete all marked entries? ")
2089 (mapc #'(lambda (entry)
2090 (ebib-remove-entry-from-db entry ebib-cur-db (not (string= entry (ebib-cur-entry-key)))))
2091 (edb-marked-entries ebib-cur-db))
2092 (message "Marked entries deleted.")
2093 (ebib-set-modified t)
2094 (ebib-fill-entry-buffer)
2095 (ebib-fill-index-buffer)))
2100 (let ((cur-entry (ebib-cur-entry-key)))
2101 (when (y-or-n-p (format "Delete %s? " cur-entry))
2102 (ebib-remove-entry-from-db cur-entry ebib-cur-db)
2103 (ebib-remove-key-from-buffer cur-entry)
2104 (ebib-fill-entry-buffer)
2105 (ebib-set-modified t)
2106 (message (format "Entry `%s' deleted." cur-entry)))))
2110 (defun ebib-remove-entry-from-db (entry-key db &optional new-cur-entry)
2111 "Removes ENTRY-KEY from DB.
2112 Optional argument NEW-CUR-ENTRY is the key of the entry that is
2113 to become the new current entry. It it is NIL, the entry after
2114 the deleted one becomes the new current entry. If it is T, the
2115 current entry is not changed."
2116 (remhash entry-key (edb-database db))
2117 (setf (edb-n-entries db) (1- (edb-n-entries db)))
2119 ((null new-cur-entry) (setq new-cur-entry (cadr (edb-cur-entry db))))
2120 ((stringp new-cur-entry) t)
2121 (t (setq new-cur-entry (ebib-cur-entry-key))))
2122 (setf (edb-keys-list db) (delete entry-key (edb-keys-list db)))
2123 (setf (edb-marked-entries db) (delete entry-key (edb-marked-entries db)))
2124 (setf (edb-cur-entry db) (member new-cur-entry (edb-keys-list db)))
2125 (unless (edb-cur-entry db) ; if (edb-cur-entry db) is nil, we deleted the last entry.
2126 (setf (edb-cur-entry db) (last (edb-keys-list db)))))
2128 (defun ebib-remove-key-from-buffer (entry-key)
2129 "Removes ENTRY-KEY from the index buffer and highlights the current entry."
2130 (with-buffer-writable
2131 (let ((beg (ebib-search-key-in-buffer entry-key)))
2133 (delete-region beg (point))))
2136 (ebib-search-key-in-buffer (ebib-cur-entry-key))
2137 (ebib-set-index-highlight))))
2139 (defun ebib-select-entry ()
2140 "Makes the entry at (point) the current entry."
2145 (let ((beg (point)))
2146 (let* ((key (save-excursion
2147 (skip-chars-forward "^ ")
2148 (buffer-substring-no-properties beg (point))))
2149 (new-cur-entry (member key (edb-keys-list ebib-cur-db))))
2151 (setf (edb-cur-entry ebib-cur-db) new-cur-entry)
2152 (ebib-set-index-highlight)
2153 (ebib-fill-entry-buffer)))))
2157 (defun ebib-export-entry (prefix)
2158 "Copies entries to another database.
2159 The prefix argument indicates which database to copy the entry
2160 to. If no prefix argument is present, a filename is asked to
2161 which the entry is appended."
2163 (let ((num (ebib-prefix prefix)))
2164 (if (ebib-called-with-prefix)
2165 (ebib-export-marked-entries num)
2166 (ebib-export-single-entry num))))
2168 (defun ebib-export-single-entry (num)
2169 "Copies the current entry to another database.
2170 NUM indicates which database to copy the entry to. If it is NIL,
2171 a filename is asked to which the entry is appended."
2175 (ebib-export-to-db num (format "Entry `%s' copied to database %%d." (ebib-cur-entry-key))
2177 (let ((entry-key (ebib-cur-entry-key)))
2178 (if (member entry-key (edb-keys-list db))
2179 (error "Entry key `%s' already exists in database %d" entry-key num)
2180 (ebib-insert-entry entry-key
2181 (copy-hash-table (ebib-retrieve-entry entry-key
2184 ;; if this is the first entry in the target DB,
2185 ;; its CUR-ENTRY must be set!
2186 (when (null (edb-cur-entry db))
2187 (setf (edb-cur-entry db) (edb-keys-list db)))
2188 t)))) ; we must return T, WHEN does not always do this.
2189 (ebib-export-to-file (format "Export `%s' to file: " (ebib-cur-entry-key))
2190 (format "Entry `%s' exported to %%s." (ebib-cur-entry-key))
2193 (ebib-format-entry (ebib-cur-entry-key) ebib-cur-db t)))))
2197 (defun ebib-export-marked-entries (num)
2198 "Copies the marked entries to another database.
2199 NUM indicates which database to copy the entry to. If it is NIL,
2200 a filename is asked to which the entry is appended."
2202 ((real-db marked-entries)
2205 num "Entries copied to database %d."
2207 (mapc #'(lambda (entry-key)
2208 (if (member entry-key (edb-keys-list db))
2209 (error "Entry key `%s' already exists in database %d" entry-key num)
2210 (ebib-insert-entry entry-key
2211 (copy-hash-table (ebib-retrieve-entry entry-key
2214 (edb-marked-entries ebib-cur-db))
2215 ;; if the target DB was empty before, its CUR-ENTRY must be set!
2216 (when (null (edb-cur-entry db))
2217 (setf (edb-cur-entry db) (edb-keys-list db)))
2218 t)) ; we must return T, WHEN does not always do this.
2219 (ebib-export-to-file "Export to file: " "Entries exported to %s."
2221 (mapc #'(lambda (entry-key)
2223 (ebib-format-entry entry-key ebib-cur-db t))
2224 (edb-marked-entries ebib-cur-db))))))
2228 (defun ebib-search ()
2229 "Search the current Ebib database.
2230 The search is conducted with STRING-MATCH and can therefore be a
2231 regexp. Searching starts with the current entry."
2235 (if-str (search-str (read-string "Search database for: "))
2237 (setq ebib-search-string search-str)
2238 ;; first we search the current entry
2239 (if (ebib-search-in-entry ebib-search-string
2240 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db))
2241 (ebib-fill-entry-buffer ebib-search-string)
2242 ;; if the search string wasn't found in the current entry, we continue searching.
2243 (ebib-search-next)))))
2247 (defun ebib-search-next ()
2248 "Search the next occurrence of EBIB-SEARCH-STRING.
2249 Searching starts at the entry following the current entry. If a
2250 match is found, the matching entry is shown and becomes the new
2255 (if (null ebib-search-string)
2256 (message "No search string")
2257 (let ((cur-search-entry (cdr (edb-cur-entry ebib-cur-db))))
2258 (while (and cur-search-entry
2259 (null (ebib-search-in-entry ebib-search-string
2260 (gethash (car cur-search-entry)
2261 (edb-database ebib-cur-db)))))
2262 (setq cur-search-entry (cdr cur-search-entry)))
2263 (if (null cur-search-entry)
2264 (message (format "`%s' not found" ebib-search-string))
2265 (setf (edb-cur-entry ebib-cur-db) cur-search-entry)
2266 (set-buffer ebib-index-buffer)
2267 (goto-char (point-min))
2268 (re-search-forward (format "^%s " (ebib-cur-entry-key)))
2270 (ebib-set-index-highlight)
2271 (ebib-fill-entry-buffer ebib-search-string)))))
2275 (defun ebib-search-in-entry (search-str entry &optional field)
2276 "Searches one entry of the ebib database.
2277 Returns a list of fields in ENTRY that match the regexp
2278 SEARCH-STR, or NIL if no matches were found. If FIELD is given,
2279 only that field is searched."
2280 (let ((case-fold-search t) ; we want to ensure a case-insensitive search
2283 (let ((value (gethash field entry)))
2284 (when (and (stringp value) ; the type* field has a symbol as value
2285 (string-match search-str value))
2286 (setq result (list field))))
2287 (maphash #'(lambda (field value)
2288 (when (and (stringp value) ; the type* field has a symbol as value
2289 (string-match search-str value))
2290 (setq result (cons field result))))
2294 (defun ebib-edit-strings ()
2295 "Edits the @STRING definitions in the database."
2299 (ebib-fill-strings-buffer)
2300 (select-window (get-buffer-window ebib-entry-buffer) nil)
2301 (set-window-dedicated-p (selected-window) nil)
2302 (switch-to-buffer ebib-strings-buffer)
2303 (unless (eq ebib-layout 'full)
2304 (set-window-dedicated-p (selected-window) t))
2305 (goto-char (point-min)))
2309 (defun ebib-edit-preamble ()
2310 "Edits the @PREAMBLE definition in the database."
2314 (select-window (ebib-temp-window) nil)
2315 (ebib-multiline-edit 'preamble (edb-preamble ebib-cur-db)))
2319 (defun ebib-export-preamble (prefix)
2320 "Export the @PREAMBLE definition.
2321 If a prefix argument is given, it is taken as the database to
2322 export the preamble to. If the goal database already has a
2323 preamble, the new preamble will be appended to it. If no prefix
2324 argument is given, the user is asked to enter a filename to which
2325 the preamble is appended."
2329 (if (null (edb-preamble ebib-cur-db))
2330 (error "No @PREAMBLE defined")
2331 (let ((num (ebib-prefix prefix)))
2333 (ebib-export-to-db num "@PREAMBLE copied to database %d"
2335 (let ((text (edb-preamble ebib-cur-db)))
2336 (if (edb-preamble db)
2337 (setf (edb-preamble db) (concat (edb-preamble db) "\n# " text))
2338 (setf (edb-preamble db) text)))))
2339 (ebib-export-to-file "Export @PREAMBLE to file: "
2340 "@PREAMBLE exported to %s"
2342 (insert (format "\n@preamble{%s}\n\n" (edb-preamble ebib-cur-db)))))))))
2346 (defun ebib-print-entries ()
2347 "Creates a LaTeX file listing the entries.
2348 Either prints the entire database, or the marked entries."
2352 (let ((entries (if (ebib-called-with-prefix)
2353 (edb-marked-entries ebib-cur-db)
2354 (edb-keys-list ebib-cur-db))))
2355 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2357 (read-file-name "Use temp file: " "~/" nil nil)))
2360 (insert "\\documentclass{article}\n\n")
2361 (when ebib-print-preamble
2362 (mapc #'(lambda (string)
2363 (insert (format "%s\n" string)))
2364 ebib-print-preamble))
2365 (insert "\n\\begin{document}\n\n")
2366 (mapc #'(lambda (entry-key)
2367 (insert "\\begin{tabular}{p{0.2\\textwidth}p{0.8\\textwidth}}\n")
2368 (let ((entry (ebib-retrieve-entry entry-key ebib-cur-db)))
2369 (insert (format "\\multicolumn{2}{l}{\\texttt{%s (%s)}}\\\\\n"
2370 entry-key (symbol-name (gethash 'type* entry))))
2371 (insert "\\hline\n")
2372 (mapc #'(lambda (field)
2373 (if-str (value (gethash field entry))
2374 (when (or (not (multiline-p value))
2375 ebib-print-multiline)
2376 (insert (format "%s: & %s\\\\\n"
2377 field (to-raw value))))))
2378 (cdr (ebib-get-all-fields (gethash 'type* entry)))))
2379 (insert "\\end{tabular}\n\n")
2380 (insert "\\bigskip\n\n"))
2382 (insert "\\end{document}\n")
2383 (write-region (point-min) (point-max) tempfile))
2385 (find-file tempfile)))))
2389 (defun ebib-latex-entries ()
2390 "Creates a LaTeX file that \\nocite's entries from the database.
2391 Operates either on all entries or on the marked entries."
2395 (if-str (tempfile (if (not (string= "" ebib-print-tempfile))
2397 (read-file-name "Use temp file: " "~/" nil nil)))
2400 (insert "\\documentclass{article}\n\n")
2401 (when ebib-print-preamble
2402 (mapc #'(lambda (string)
2403 (insert (format "%s\n" string)))
2404 ebib-latex-preamble))
2405 (insert "\n\\begin{document}\n\n")
2406 (if (ebib-called-with-prefix)
2407 (mapc #'(lambda (entry)
2408 (insert (format "\\nocite{%s}\n" entry)))
2409 (edb-marked-entries ebib-cur-db))
2410 (insert "\\nocite{*}\n"))
2411 (insert (format "\n\\bibliography{%s}\n\n" (expand-file-name (edb-filename ebib-cur-db))))
2412 (insert "\\end{document}\n")
2413 (write-region (point-min) (point-max) tempfile))
2415 (find-file tempfile))))
2419 (defun ebib-switch-to-database (num)
2420 (interactive "NSwitch to database number: ")
2421 (let ((new-db (nth (1- num) ebib-databases)))
2424 (setq ebib-cur-db new-db)
2425 (ebib-fill-entry-buffer)
2426 (ebib-fill-index-buffer))
2427 (error "Database %d does not exist" num))))
2429 (defun ebib-next-database ()
2433 (let ((new-db (next-elem ebib-cur-db ebib-databases)))
2435 (setq new-db (car ebib-databases)))
2436 (setq ebib-cur-db new-db)
2437 (ebib-fill-entry-buffer)
2438 (ebib-fill-index-buffer)))))
2440 (defun ebib-prev-database ()
2444 (let ((new-db (prev-elem ebib-cur-db ebib-databases)))
2446 (setq new-db (last1 ebib-databases)))
2447 (setq ebib-cur-db new-db)
2448 (ebib-fill-entry-buffer)
2449 (ebib-fill-index-buffer)))))
2451 (defun ebib-browse-url (num)
2452 "Ask a browser to load the URL in the standard URL field.
2453 The standard URL field may contain more than one URL, if they're
2454 whitespace-separated. In that case, a numeric prefix argument
2455 specifies which URL to choose.
2457 By \"standard URL field\" is meant the field defined in the
2458 customisation variable EBIB-STANDARD-URL-FIELD. Its default value
2463 (let ((url (to-raw (gethash ebib-standard-url-field
2464 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2466 (ebib-call-browser url num)))
2467 (error "No url found in field `%s'" ebib-standard-url-field))))
2471 (defun ebib-call-browser (urls n)
2472 "Passes the Nth url in URLS to a browser.
2473 URLs must be a string of whitespace-separated urls."
2474 (let ((url (nth (1- n)
2477 (while (string-match ebib-url-regexp urls start)
2478 (add-to-list 'result (match-string 0 urls) t)
2479 (setq start (match-end 0)))
2482 ((string-match "\\\\url{\\(.*?\\)}" url)
2483 (setq url (match-string 1 url)))
2484 ((string-match ebib-url-regexp url)
2485 (setq url (match-string 0 url)))
2488 (if (not (string= ebib-browser-command ""))
2489 (start-process "Ebib-browser" nil ebib-browser-command url)
2490 (browse-url url)))))
2492 (defun ebib-view-file (num)
2493 "View a file in the standard file field.
2494 The standard file field may contain more than one filename, if
2495 they're whitespace-separated. In that case, a numeric prefix
2496 argument specifies which file to choose.
2498 By \"standard file field\" is meant the field defined in the
2499 customisation variable EBIB-STANDARD-FILE-FIELD. Its default
2504 (let ((filename (to-raw (gethash ebib-standard-file-field
2505 (ebib-retrieve-entry (ebib-cur-entry-key) ebib-cur-db)))))
2506 (when (not (and filename
2507 (ebib-call-file-viewer filename num)))
2508 (error "No valid filename found in field `%s'" ebib-standard-file-field))))
2512 (defun ebib-call-file-viewer (files n)
2513 "Passes the Nth file in FILES to an external viewer.
2514 FILESs must be a string of whitespace-separated filenames."
2515 (let* ((file (nth (1- n)
2518 (while (string-match ebib-file-regexp files start)
2519 (add-to-list 'result (match-string 0 files) t)
2520 (setq start (match-end 0)))
2522 (ext (last1 (split-string file "\\."))))
2523 (let ((file-full-path (locate-file file ebib-file-search-dirs)))
2524 (when file-full-path
2525 (if-str (viewer (cdr (assoc ext ebib-file-associations)))
2526 (start-process (concat "ebib " ext " viewer process") nil viewer file-full-path)
2528 (find-file file-full-path))))))
2530 (defun ebib-virtual-db-and (not)
2531 "Filter entries into a virtual database.
2532 If the current database is a virtual database already, perform a
2533 logical AND on the entries."
2537 (ebib-filter-to-virtual-db 'and not))
2541 (defun ebib-virtual-db-or (not)
2542 "Filter entries into a virtual database.
2543 If the current database is a virtual database already, perform a
2544 logical OR with the entries in the original database."
2548 (ebib-filter-to-virtual-db 'or not))
2552 (defun ebib-virtual-db-not ()
2553 "Negates the current virtual database."
2557 (setf (edb-virtual ebib-cur-db)
2558 (if (eq (car (edb-virtual ebib-cur-db)) 'not)
2559 (cadr (edb-virtual ebib-cur-db))
2560 `(not ,(edb-virtual ebib-cur-db))))
2561 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2562 (ebib-fill-entry-buffer)
2563 (ebib-fill-index-buffer))
2567 (defun ebib-filter-to-virtual-db (bool not)
2568 "Filters the current database to a virtual database.
2569 BOOL is the operator to be used, either `and' or `or'. If NOT<0,
2570 a logical `not' is applied to the selection."
2571 (let ((field (completing-read (format "Filter: %s(contains <field> <regexp>)%s. Enter field: "
2572 (if (< not 0) "(not " "")
2573 (if (< not 0) ")" ""))
2575 (mapcar #'(lambda (x)
2576 (cons (symbol-name x) 0))
2577 (append ebib-unique-field-list ebib-additional-fields)))
2579 (setq field (intern-soft field))
2580 (let ((regexp (read-string (format "Filter: %s(contains %s <regexp>)%s. Enter regexp: "
2581 (if (< not 0) "(not " "")
2583 (if (< not 0) ")" "")))))
2586 (setf (edb-virtual ebib-cur-db) `(,bool ,(edb-virtual ebib-cur-db)
2588 `(contains ,field ,regexp)
2589 `(not (contains ,field ,regexp))))))
2591 (setq ebib-cur-db (ebib-create-virtual-db field regexp not))))
2592 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2593 (ebib-fill-entry-buffer)
2594 (ebib-fill-index-buffer))))
2596 (defun ebib-create-virtual-db (field regexp not)
2597 "Creates a virtual database based on EBIB-CUR-DB."
2598 ;; a virtual database is a database whose edb-virtual field contains an
2599 ;; expression that selects entries. this function only sets that
2600 ;; expression, it does not actually filter the entries.
2601 (let ((new-db (ebib-create-new-database ebib-cur-db)))
2602 (setf (edb-virtual new-db) (if (>= not 0)
2603 `(contains ,field ,regexp)
2604 `(not (contains ,field ,regexp))))
2605 (setf (edb-filename new-db) nil)
2606 (setf (edb-name new-db) (concat "V:" (edb-name new-db)))
2607 (setf (edb-modified new-db) nil)
2608 (setf (edb-make-backup new-db) nil)
2611 (defmacro contains (field regexp)
2612 ;; This is a hack: CONTAINS depends on the variable ENTRY being set to an
2613 ;; actual Ebib entry for its operation. The point of this macro is to
2614 ;; facilitate defining filters for virtual databases. It enables us to
2615 ;; define filters of the form:
2617 ;; (and (not (contains author "Chomsky")) (contains year "1995"))
2619 `(ebib-search-in-entry ,regexp entry ,(unless (eq field 'any) `(quote ,field))))
2621 (defun ebib-run-filter (filter db)
2623 (setf (edb-keys-list db)
2624 (sort (let ((result nil))
2625 (maphash #'(lambda (key value)
2626 (let ((entry value)) ; this is necessary for actually running the filter
2628 (setq result (cons key result)))))
2632 (setf (edb-n-entries db) (length (edb-keys-list db)))
2633 (setf (edb-cur-entry db) (edb-keys-list db)))
2635 (defun ebib-print-filter (num)
2636 "Display the filter of the current virtual database.
2637 With any prefix argument, reapplies the filter to the
2638 database. This can be useful when the source database was
2644 (ebib-run-filter (edb-virtual ebib-cur-db) ebib-cur-db)
2645 (ebib-fill-entry-buffer)
2646 (ebib-fill-index-buffer))
2647 (message "%S" (edb-virtual ebib-cur-db)))
2651 (defun ebib-index-help ()
2652 "Displays the help message for the index buffer."
2655 (ebib-display-help ebib-index-buffer))
2657 (defun ebib-show-log ()
2658 "Display the contents of the log buffer."
2661 (switch-to-buffer ebib-log-buffer))
2663 (defun ebib-push-entry-key (prefix)
2664 "Pushes the current entry to a LaTeX buffer.
2665 The user is prompted for the buffer to push the entry into."
2667 (let ((called-with-prefix (ebib-called-with-prefix)))
2670 (let ((buffer (read-buffer "Push entry(ies) to buffer: " ebib-push-buffer t)))
2672 (setq ebib-push-buffer buffer)
2673 (let ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
2675 (latex-arg (if called-with-prefix
2676 (when (edb-marked-entries ebib-cur-db)
2677 (mapconcat #'(lambda (x) x)
2678 (edb-marked-entries ebib-cur-db)
2680 (car (edb-cur-entry ebib-cur-db)))))
2683 (insert (format latex-cmd latex-arg)))
2684 (message "Pushed entries to buffer %s" buffer)))))
2692 (defvar ebib-entry-mode-map
2693 (let ((map (make-keymap)))
2694 (suppress-keymap map)
2695 (define-key map [up] 'ebib-prev-field
)
2696 (define-key map
[down] 'ebib-next-field)
2697 (define-key map [prior] 'ebib-goto-prev-set)
2698 (define-key map [next] 'ebib-goto-next-set)
2699 (define-key map [home] 'ebib-goto-first-field)
2700 (define-key map [end] 'ebib-goto-last-field)
2701 (define-key map " " 'ebib-goto-next-set)
2702 (define-key map "b" 'ebib-goto-prev-set)
2703 (define-key map "c" 'ebib-copy-field-contents)
2704 (define-key map "d" 'ebib-delete-field-contents)
2705 (define-key map "e" 'ebib-edit-field)
2706 (define-key map "f" 'ebib-view-file-in-field)
2707 (define-key map "g" 'ebib-goto-first-field)
2708 (define-key map "G" 'ebib-goto-last-field)
2709 (define-key map "h" 'ebib-entry-help)
2710 (define-key map "j" 'ebib-next-field)
2711 (define-key map "k" 'ebib-prev-field)
2712 (define-key map "l" 'ebib-edit-multiline-field)
2713 (define-key map [(control n)] 'ebib-next-field)
2714 (define-key map [(meta n)] 'ebib-goto-prev-set)
2715 (define-key map [(control p)] 'ebib-prev-field)
2716 (define-key map [(meta p)] 'ebib-goto-next-set)
2717 (define-key map "q" 'ebib-quit-entry-buffer)
2718 (define-key map "r" 'ebib-toggle-raw)
2719 (define-key map "s" 'ebib-insert-abbreviation)
2720 (define-key map "u" 'ebib-browse-url-in-field)
2721 (define-key map "x" 'ebib-cut-field-contents)
2722 (define-key map "\C-xb" 'undefined)
2723 (define-key map "\C-xk" 'undefined)
2724 (define-key map "y" 'ebib-yank-field-contents)
2726 "Keymap for the Ebib entry buffer.")
2728 (define-derived-mode ebib-entry-mode
2729 fundamental-mode "Ebib-entry"
2730 "Major mode for the Ebib entry buffer."
2731 (setq buffer-read-only t)
2732 (setq truncate-lines t))
2734 (defun ebib-quit-entry-buffer ()
2735 "Quit editing the entry."
2737 (select-window (get-buffer-window ebib-index-buffer) nil))
2739 (defun ebib-find-visible-field (field direction)
2740 "Finds the first visible field before or after FIELD.
2741 If DIRECTION is negative, search the preceding fields, otherwise
2742 search the succeeding fields. If FIELD is visible itself, return
2743 that. If there is no preceding/following visible field, return
2744 NIL. If EBIB-HIDE-HIDDEN-FIELDS is NIL, return FIELD."
2745 (when ebib-hide-hidden-fields
2746 (let ((fn (if (>= direction 0)
2750 (get field 'ebib-hidden))
2751 (setq field (funcall fn field ebib-cur-entry-fields)))))
2754 (defun ebib-prev-field ()
2755 "Move to the previous field."
2757 (let ((new-field (ebib-find-visible-field (prev-elem ebib-current-field ebib-cur-entry-fields) -1)))
2758 (if (null new-field)
2760 (setq ebib-current-field new-field)
2761 (ebib-move-to-field ebib-current-field -1))))
2763 (defun ebib-next-field ()
2764 "Move to the next field."
2766 (let ((new-field (ebib-find-visible-field (next-elem ebib-current-field ebib-cur-entry-fields) 1)))
2767 (if (null new-field)
2768 (when (interactive-p) ; i call this function after editing a field,
2769 ; and we don't want a beep then
2771 (setq ebib-current-field new-field)
2772 (ebib-move-to-field ebib-current-field 1))))
2774 (defun ebib-goto-first-field ()
2775 "Move to the first field."
2777 (let ((new-field (ebib-find-visible-field (car ebib-cur-entry-fields) 1)))
2778 (if (null new-field)
2780 (setq ebib-current-field new-field)
2781 (ebib-move-to-field ebib-current-field -1))))
2783 (defun ebib-goto-last-field ()
2784 "Move to the last field."
2786 (let ((new-field (ebib-find-visible-field (last1 ebib-cur-entry-fields) -1)))
2787 (if (null new-field)
2789 (setq ebib-current-field new-field)
2790 (ebib-move-to-field ebib-current-field 1))))
2792 (defun ebib-goto-next-set ()
2793 "Move to the next set of fields."
2796 ((eq ebib-current-field 'type*) (ebib-next-field))
2797 ((member ebib-current-field ebib-additional-fields) (ebib-goto-last-field))
2798 (t (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2799 (obl-fields (ebib-get-obl-fields entry-type))
2800 (opt-fields (ebib-get-opt-fields entry-type))
2802 (when (member ebib-current-field obl-fields)
2803 (setq new-field (ebib-find-visible-field (car opt-fields) 1)))
2804 ;; new-field is nil if there are no opt-fields
2805 (when (or (member ebib-current-field opt-fields)
2807 (setq new-field (ebib-find-visible-field (car ebib-additional-fields) 1)))
2808 (if (null new-field)
2809 (ebib-goto-last-field) ; if there was no further set to go to,
2810 ; go to the last field of the current set
2811 (setq ebib-current-field new-field)
2812 (ebib-move-to-field ebib-current-field 1))))))
2814 (defun ebib-goto-prev-set ()
2815 "Move to the previous set of fields."
2817 (unless (eq ebib-current-field 'type*)
2818 (let* ((entry-type (gethash 'type* ebib-cur-entry-hash))
2819 (obl-fields (ebib-get-obl-fields entry-type))
2820 (opt-fields (ebib-get-opt-fields entry-type))
2822 (if (member ebib-current-field obl-fields)
2823 (ebib-goto-first-field)
2824 (when (member ebib-current-field ebib-additional-fields)
2825 (setq new-field (ebib-find-visible-field (last1 opt-fields) -1)))
2826 (when (or (member ebib-current-field opt-fields)
2828 (setq new-field (ebib-find-visible-field (last1 obl-fields) -1)))
2829 (if (null new-field)
2830 (ebib-goto-first-field)
2831 (setq ebib-current-field new-field)
2832 (ebib-move-to-field ebib-current-field -1))))))
2834 (defun ebib-edit-entry-type ()
2835 ;; we don't want the completion buffer to be shown in the index window,
2836 ;; so we need to switch to an appropriate window first. we do this in an
2837 ;; unwind-protect to make sure we always get back to the entry buffer.
2840 (if (eq ebib-layout 'full)
2842 (select-window ebib-pre-ebib-window) nil)
2843 (let ((collection (ebib-create-collection ebib-entry-types-hash)))
2844 (if-str (new-type (completing-read "type: " collection nil t))
2846 (puthash 'type* (intern-soft new-type) ebib-cur-entry-hash)
2847 (ebib-fill-entry-buffer)
2848 (setq ebib-cur-entry-fields (ebib-get-all-fields (gethash 'type* ebib-cur-entry-hash)))
2849 (ebib-set-modified t)))))
2850 (select-window (get-buffer-window ebib-entry-buffer) nil)))
2852 (defun ebib-edit-crossref ()
2853 "Edits the crossref field."
2854 ;; we don't want the completion buffer to be shown in the index window,
2855 ;; so we need to switch to an appropriate window first. we do this in an
2856 ;; unwind-protect to make sure we always get back to the entry buffer.
2859 (if (eq ebib-layout 'full)
2861 (select-window ebib-pre-ebib-window) nil)
2862 (let ((collection (ebib-create-collection (edb-database ebib-cur-db))))
2863 (if-str (key (completing-read "Key to insert in `crossref': " collection nil t))
2865 (puthash 'crossref (from-raw key) ebib-cur-entry-hash)
2866 (ebib-set-modified t)))))
2867 (select-window (get-buffer-window ebib-entry-buffer) nil)
2868 ;; we now redisplay the entire entry buffer, so that the crossref'ed
2869 ;; fields show up. this also puts the cursor back on the type field.
2870 (ebib-fill-entry-buffer)
2871 (setq ebib-current-field 'crossref)
2872 (re-search-forward "^crossref")
2873 (ebib-set-fields-highlight)))
2875 ;; we should modify ebib-edit-field, so that it calls the appropriate
2876 ;; helper function, which asks the user for the new value and just returns
2877 ;; that. storing it should then be done by ebib-edit-field, no matter what
2878 ;; sort of field the user edits.
2880 (defun ebib-edit-field ()
2881 "Edits a field of a BibTeX entry."
2884 ((eq ebib-current-field 'type*) (ebib-edit-entry-type))
2885 ((eq ebib-current-field 'crossref) (ebib-edit-crossref))
2886 ((eq ebib-current-field 'annote) (ebib-edit-multiline-field))
2888 (let ((init-contents (gethash ebib-current-field ebib-cur-entry-hash))
2890 (if (multiline-p init-contents)
2891 (ebib-edit-multiline-field)
2893 (if (raw-p init-contents)
2895 (setq init-contents (to-raw init-contents))))
2896 (if-str (new-contents (read-string (format "%s: " (symbol-name ebib-current-field))
2898 (cons init-contents 0)
2901 (puthash ebib-current-field (if raw
2903 (concat "{" new-contents "}"))
2904 ebib-cur-entry-hash)
2905 (remhash ebib-current-field ebib-cur-entry-hash))
2906 (ebib-redisplay-current-field)
2907 ;; we move to the next field, but only if ebib-edit-field was
2908 ;; called interactively, otherwise we get a strange bug in
2909 ;; ebib-toggle-raw...
2910 (if (interactive-p) (ebib-next-field))
2911 (ebib-set-modified t))))))
2913 (defun ebib-browse-url-in-field (num)
2914 "Browse a url in the current field.
2915 The field may contain a whitespace-separated set of urls. The
2916 prefix argument indicates which url is to be sent to the
2919 (let ((urls (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2921 (ebib-call-browser urls num)))
2922 (error "No url found in field `%s'" ebib-current-field))))
2924 (defun ebib-view-file-in-field (num)
2925 "View a file in the current field.
2926 The field may contain a whitespace-separated set of
2927 filenames. The prefix argument indicates which file is to be
2930 (let ((files (to-raw (gethash ebib-current-field ebib-cur-entry-hash))))
2932 (ebib-call-file-viewer files num)))
2933 (error "No valid filename found in field `%s'" ebib-current-field))))
2935 (defun ebib-copy-field-contents ()
2936 "Copies the contents of the current field to the kill ring."
2938 (unless (eq ebib-current-field 'type*)
2939 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2940 (when (stringp contents)
2942 (message "Field contents copied.")))))
2944 (defun ebib-cut-field-contents ()
2945 "Kills the contents of the current field. The killed text is put in the kill ring."
2947 (unless (eq ebib-current-field 'type*)
2948 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2949 (when (stringp contents)
2950 (remhash ebib-current-field ebib-cur-entry-hash)
2952 (ebib-redisplay-current-field)
2953 (ebib-set-modified t)
2954 (message "Field contents killed.")))))
2956 (defun ebib-yank-field-contents (arg)
2957 "Inserts the last killed text into the current field.
2958 If the current field already has a contents, nothing is inserted,
2959 unless the previous command was also ebib-yank-field-contents,
2960 then the field contents is replaced with the previous yank. That
2961 is, multiple use of this command functions like the combination
2962 of C-y/M-y. Prefix arguments also work the same as with C-y/M-y."
2964 (if (or (eq ebib-current-field 'type*) ; we cannot yank into the type* or crossref fields
2965 (eq ebib-current-field 'crossref)
2966 (unless (eq last-command 'ebib-yank-field-contents)
2967 (gethash ebib-current-field ebib-cur-entry-hash))) ; nor into a field already filled
2969 (setq this-command t)
2971 (let ((new-contents (current-kill (cond
2972 ((listp arg) (if (eq last-command 'ebib-yank-field-contents)
2978 (puthash ebib-current-field new-contents ebib-cur-entry-hash)
2979 (ebib-redisplay-current-field)
2980 (ebib-set-modified t)))))
2982 (defun ebib-delete-field-contents ()
2983 "Deletes the contents of the current field.
2984 The deleted text is not put in the kill ring."
2986 (if (eq ebib-current-field 'type*)
2988 (remhash ebib-current-field ebib-cur-entry-hash)
2989 (ebib-redisplay-current-field)
2990 (ebib-set-modified t)
2991 (message "Field contents deleted.")))
2993 (defun ebib-toggle-raw ()
2994 "Toggles the raw status of the current field contents."
2996 (unless (or (eq ebib-current-field 'type*)
2997 (eq ebib-current-field 'crossref))
2998 (let ((contents (gethash ebib-current-field ebib-cur-entry-hash)))
2999 (if (not contents) ; if there is no value,
3001 (ebib-edit-field) ; the user can enter one, which we must then make raw
3002 (let ((new-contents (gethash ebib-current-field ebib-cur-entry-hash)))
3004 ;; note: we don't have to check for empty string, since that is
3005 ;; already done in ebib-edit-field
3006 (puthash ebib-current-field (to-raw new-contents) ebib-cur-entry-hash))))
3007 (if (raw-p contents)
3008 (puthash ebib-current-field (from-raw contents) ebib-cur-entry-hash)
3009 (puthash ebib-current-field (to-raw contents) ebib-cur-entry-hash)))
3010 (ebib-redisplay-current-field)
3011 (ebib-set-modified t))))
3013 (defun ebib-edit-multiline-field ()
3014 "Edits the current field in multiline-mode."
3016 (unless (or (eq ebib-current-field 'type*)
3017 (eq ebib-current-field 'crossref))
3018 (let ((text (gethash ebib-current-field ebib-cur-entry-hash)))
3020 (setq ebib-multiline-raw t)
3021 (setq text (to-raw text))
3022 (setq ebib-multiline-raw nil))
3023 (select-window (ebib-temp-window) nil)
3024 (ebib-multiline-edit 'fields text))))
3026 (defun ebib-insert-abbreviation ()
3027 "Insert an abbreviation from the ones defined in the database."
3029 (if (gethash ebib-current-field ebib-cur-entry-hash)
3031 (when (edb-strings-list ebib-cur-db)
3035 (let* ((collection (ebib-create-collection (edb-strings ebib-cur-db)))
3036 (string (completing-read "Abbreviation to insert: " collection nil t)))
3038 (puthash ebib-current-field string ebib-cur-entry-hash)
3039 (ebib-set-modified t))))
3041 ;; we can't do this earlier, because we would be writing to the index buffer...
3042 (ebib-redisplay-current-field)
3043 (ebib-next-field)))))
3045 (defun ebib-entry-help ()
3046 "Displays the help message for the entry buffer."
3048 (ebib-display-help ebib-entry-buffer))
3054 (defvar ebib-strings-mode-map
3055 (let ((map (make-keymap)))
3056 (suppress-keymap map)
3057 (define-key map [up] 'ebib-prev-string)
3058 (define-key map [down] 'ebib-next-string
)
3059 (define-key map
[prior] 'ebib-strings-page-up)
3060 (define-key map [next] 'ebib-strings-page-down)
3061 (define-key map [home] 'ebib-goto-first-string)
3062 (define-key map [end] 'ebib-goto-last-string)
3063 (define-key map " " 'ebib-strings-page-down)
3064 (define-key map "a" 'ebib-add-string)
3065 (define-key map "b" 'ebib-strings-page-up)
3066 (define-key map "c" 'ebib-copy-string-contents)
3067 (define-key map "d" 'ebib-delete-string)
3068 (define-key map "e" 'ebib-edit-string)
3069 (define-key map "g" 'ebib-goto-first-string)
3070 (define-key map "G" 'ebib-goto-last-string)
3071 (define-key map "h" 'ebib-strings-help)
3072 (define-key map "j" 'ebib-next-string)
3073 (define-key map "k" 'ebib-prev-string)
3074 (define-key map "l" 'ebib-edit-multiline-string)
3075 (define-key map [(control n)] 'ebib-next-string)
3076 (define-key map [(meta n)] 'ebib-strings-page-down)
3077 (define-key map [(control p)] 'ebib-prev-string)
3078 (define-key map [(meta p)] 'ebib-strings-page-up)
3079 (define-key map "q" 'ebib-quit-strings-buffer)
3080 (define-key map "x" 'ebib-export-string)
3081 (define-key map "X" 'ebib-export-all-strings)
3082 (define-key map "\C-xb" 'disabled)
3083 (define-key map "\C-xk" 'disabled)
3085 "Keymap for the ebib strings buffer.")
3087 (define-derived-mode ebib-strings-mode
3088 fundamental-mode "Ebib-strings"
3089 "Major mode for the Ebib strings buffer."
3090 (setq buffer-read-only t)
3091 (setq truncate-lines t))
3093 (defun ebib-quit-strings-buffer ()
3094 "Quit editing the @STRING definitions."
3096 (set-window-dedicated-p (selected-window) nil)
3097 (switch-to-buffer ebib-entry-buffer)
3098 (unless (eq ebib-layout 'full)
3099 (set-window-dedicated-p (selected-window) t))
3100 (select-window (get-buffer-window ebib-index-buffer) nil))
3102 (defun ebib-prev-string ()
3103 "Move to the previous string."
3105 (if (equal ebib-current-string (car (edb-strings-list ebib-cur-db))) ; if we're on the first string
3107 ;; go to the beginnig of the highlight and move upward one line.
3108 (goto-char (ebib-highlight-start ebib-strings-highlight))
3110 (setq ebib-current-string (prev-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3111 (ebib-set-strings-highlight)))
3113 (defun ebib-next-string ()
3114 "Move to the next string."
3116 (if (equal ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3117 (when (interactive-p) (beep))
3118 (goto-char (ebib-highlight-start ebib-strings-highlight))
3120 (setq ebib-current-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db)))
3121 (ebib-set-strings-highlight)))
3123 (defun ebib-goto-first-string ()
3124 "Move to the first string."
3126 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3127 (goto-char (point-min))
3128 (ebib-set-strings-highlight))
3130 (defun ebib-goto-last-string ()
3131 "Move to the last string."
3133 (setq ebib-current-string (last1 (edb-strings-list ebib-cur-db)))
3134 (goto-char (point-max))
3136 (ebib-set-strings-highlight))
3138 (defun ebib-strings-page-up ()
3139 "Moves 10 entries up in the database."
3141 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3142 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3143 (if (<= (- number-of-strings remaining-number-of-strings) 10)
3144 (ebib-goto-first-string)
3145 (setq ebib-current-string (nth
3146 (- number-of-strings remaining-number-of-strings 10)
3147 (edb-strings-list ebib-cur-db)))
3148 (goto-char (ebib-highlight-start ebib-strings-highlight))
3150 (ebib-set-strings-highlight)))
3151 (message ebib-current-string))
3153 (defun ebib-strings-page-down ()
3154 "Moves 10 entries down in the database."
3156 (let ((number-of-strings (length (edb-strings-list ebib-cur-db)))
3157 (remaining-number-of-strings (length (member ebib-current-string (edb-strings-list ebib-cur-db)))))
3158 (if (<= remaining-number-of-strings 10)
3159 (ebib-goto-last-string)
3160 (setq ebib-current-string (nth
3161 (- number-of-strings remaining-number-of-strings -10)
3162 (edb-strings-list ebib-cur-db)))
3163 (goto-char (ebib-highlight-start ebib-strings-highlight))
3165 (ebib-set-strings-highlight)))
3166 (message ebib-current-string))
3168 (defun ebib-fill-strings-buffer ()
3169 "Fills the strings buffer with the @STRING definitions."
3170 (set-buffer ebib-strings-buffer)
3171 (with-buffer-writable
3173 (dolist (elem (edb-strings-list ebib-cur-db))
3174 (let ((str (to-raw (gethash elem (edb-strings ebib-cur-db)))))
3175 (insert (format "%-18s %s\n" elem
3176 (if (multiline-p str)
3177 (concat "+" (first-line str))
3178 (concat " " str)))))))
3179 (goto-char (point-min))
3180 (setq ebib-current-string (car (edb-strings-list ebib-cur-db)))
3181 (ebib-set-strings-highlight)
3182 (set-buffer-modified-p nil))
3184 (defun ebib-edit-string ()
3185 "Edits the value of an @STRING definition
3186 When the user enters an empty string, the value is not changed."
3188 (let ((init-contents (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3189 (if (multiline-p init-contents)
3190 (ebib-edit-multiline-string)
3191 (if-str (new-contents (read-string (format "%s: " ebib-current-string)
3193 (cons init-contents 0)
3197 (puthash ebib-current-string (from-raw new-contents) (edb-strings ebib-cur-db))
3198 (ebib-redisplay-current-string)
3200 (ebib-set-modified t))
3201 (error "@STRING definition cannot be empty")))))
3203 (defun ebib-copy-string-contents ()
3204 "Copies the contents of the current string to the kill ring."
3206 (let ((contents (gethash ebib-current-string (edb-strings ebib-cur-db))))
3208 (message "String value copied.")))
3210 (defun ebib-delete-string ()
3211 "Deletes the current @STRING definition from the database."
3213 (when (y-or-n-p (format "Delete @STRING definition %s? " ebib-current-string))
3214 (remhash ebib-current-string (edb-strings ebib-cur-db))
3215 (with-buffer-writable
3217 (goto-char (ebib-highlight-start ebib-strings-highlight))
3220 (delete-region beg (point))))
3221 (let ((new-cur-string (next-elem ebib-current-string (edb-strings-list ebib-cur-db))))
3222 (setf (edb-strings-list ebib-cur-db) (delete ebib-current-string (edb-strings-list ebib-cur-db)))
3223 (when (null new-cur-string) ; deleted the last string
3224 (setq new-cur-string (last1 (edb-strings-list ebib-cur-db)))
3226 (setq ebib-current-string new-cur-string))
3227 (ebib-set-strings-highlight)
3228 (ebib-set-modified t)
3229 (message "@STRING definition deleted.")))
3231 (defun ebib-add-string ()
3232 "Creates a new @STRING definition."
3234 (if-str (new-abbr (read-string "New @STRING abbreviation: "))
3236 (if (member new-abbr (edb-strings-list ebib-cur-db))
3237 (error (format "%s already exists" new-abbr)))
3238 (if-str (new-string (read-string (format "Value for %s: " new-abbr)))
3240 (ebib-insert-string new-abbr new-string ebib-cur-db t)
3241 (sort-in-buffer (length (edb-strings-list ebib-cur-db)) new-abbr)
3242 (with-buffer-writable
3243 (insert (format "%-19s %s\n" new-abbr new-string)))
3245 (ebib-set-strings-highlight)
3246 (setq ebib-current-string new-abbr)
3247 (ebib-set-modified t))))))
3249 (defun ebib-export-string (prefix)
3250 "Exports the current @STRING.
3251 The prefix argument indicates which database to copy the string
3252 to. If no prefix argument is present, a filename is asked to
3253 which the string is appended."
3255 (let ((num (ebib-prefix prefix)))
3257 (ebib-export-to-db num (format "@STRING definition `%s' copied to database %%d" ebib-current-string)
3259 (let ((abbr ebib-current-string)
3260 (string (gethash ebib-current-string (edb-strings ebib-cur-db))))
3261 (if (member abbr (edb-strings-list db))
3262 (error "@STRING definition already exists in database %d" num)
3263 (ebib-insert-string abbr string db t)))))
3264 (ebib-export-to-file (format "Export @STRING definition `%s' to file: " ebib-current-string)
3265 (format "@STRING definition `%s' exported to %%s" ebib-current-string)
3267 (insert (format "\n@string{%s = %s}\n"
3269 (gethash ebib-current-string (edb-strings ebib-cur-db)))))))))
3271 (defun ebib-export-all-strings (prefix)
3272 "Export all @STRING definitions.
3273 If a prefix argument is given, it is taken as the database to
3274 copy the definitions to. Without prefix argument, asks for a file
3277 (when ebib-current-string ; there is always a current string, unless there are no strings
3278 (let ((num (ebib-prefix prefix)))
3281 num "All @STRING definitions copied to database %d"
3283 (mapc #'(lambda (abbr)
3284 (if (member abbr (edb-strings-list db))
3285 (message "@STRING definition `%s' already exists in database %d" abbr num)
3286 (ebib-insert-string abbr (gethash abbr (edb-strings ebib-cur-db)) db t)))
3287 (edb-strings-list ebib-cur-db))))
3288 (ebib-export-to-file "Export all @STRING definitions to file: "
3289 "All @STRING definitions exported to %s"
3291 (insert (format "\n")) ; to keep things tidy.
3292 (ebib-format-strings ebib-cur-db)))))))
3294 (defun ebib-edit-multiline-string ()
3295 "Edits the current string in multiline-mode."
3297 (select-window (ebib-temp-window) nil)
3298 (ebib-multiline-edit 'string (to-raw (gethash ebib-current-string (edb-strings ebib-cur-db)))))
3300 (defun ebib-strings-help ()
3301 "Displays the help message for the strings buffer."
3303 (ebib-display-help ebib-strings-buffer))
3305 ;;;;;;;;;;;;;;;;;;;;;;;;;
3306 ;; multiline edit mode ;;
3307 ;;;;;;;;;;;;;;;;;;;;;;;;;
3309 (define-derived-mode ebib-multiline-edit-mode
3310 text-mode "Ebib-edit"
3311 "Major mode for editing multiline strings in Ebib."
3312 ;; we redefine some basic keys because we need them to leave this buffer.
3313 (local-set-key "\C-xb" 'ebib-quit-multiline-edit)
3314 (local-set-key "\C-x\C-s" 'ebib-save-from-multiline-edit)
3315 (local-set-key "\C-xk" 'ebib-cancel-multiline-edit))
3317 (defun ebib-multiline-edit (type &optional starttext)
3318 "Switches to Ebib's multiline edit buffer.
3319 STARTTEXT is a string that contains the initial text of the buffer."
3320 ;; note: the buffer is put in the currently active window!
3321 (setq ebib-pre-multiline-buffer (current-buffer))
3322 (switch-to-buffer ebib-multiline-buffer)
3323 (set-buffer-modified-p nil)
3325 (setq ebib-editing type)
3328 (goto-char (point-min))
3329 (set-buffer-modified-p nil)))
3331 (defun ebib-quit-multiline-edit ()
3332 "Quits the multiline edit buffer, saving the text."
3334 (ebib-store-multiline-text)
3335 (ebib-leave-multiline-edit-buffer)
3337 ((eq ebib-editing 'fields)
3339 ((eq ebib-editing 'strings)
3340 (ebib-next-string)))
3341 (message "Text stored."))
3343 (defun ebib-cancel-multiline-edit ()
3344 "Quits the multiline edit buffer and discards the changes."
3347 (when (buffer-modified-p)
3348 (unless (y-or-n-p "Text has been modified. Abandon changes? ")
3349 (throw 'no-cancel nil)))
3350 (ebib-leave-multiline-edit-buffer)))
3352 (defun ebib-leave-multiline-edit-buffer ()
3353 "Leaves the multiline edit buffer.
3354 Restores the previous buffer in the window that the multiline
3355 edit buffer was shown in."
3356 (switch-to-buffer ebib-pre-multiline-buffer)
3358 ((eq ebib-editing 'preamble)
3359 (select-window (get-buffer-window ebib-index-buffer) nil))
3360 ((eq ebib-editing 'fields)
3361 ;; in full-frame layout, select-window isn't necessary, but it doesn't hurt either.
3362 (select-window (get-buffer-window ebib-entry-buffer) nil)
3363 (ebib-redisplay-current-field))
3364 ((eq ebib-editing 'strings)
3365 ;; in full-frame layout, select-window isn't necessary, but it doesn't hurt either.
3366 (select-window (get-buffer-window ebib-strings-buffer) nil)
3367 (ebib-redisplay-current-string))))
3369 (defun ebib-save-from-multiline-edit ()
3370 "Stores the text being edited in the multiline edit buffer and then saves the database."
3372 (ebib-store-multiline-text)
3373 (ebib-save-database ebib-cur-db)
3374 (set-buffer-modified-p nil))
3376 (defun ebib-store-multiline-text ()
3377 "Stores the text being edited in the multiline edit buffer."
3378 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
3380 ((eq ebib-editing 'preamble)
3382 (setf (edb-preamble ebib-cur-db) nil)
3383 (setf (edb-preamble ebib-cur-db) text)))
3384 ((eq ebib-editing 'fields)
3386 (remhash ebib-current-field ebib-cur-entry-hash)
3387 (when (not ebib-multiline-raw)
3388 (setq text (from-raw text)))
3389 (puthash ebib-current-field text ebib-cur-entry-hash)))
3390 ((eq ebib-editing 'strings)
3392 ;; with ERROR, we avoid execution of EBIB-SET-MODIFIED and
3393 ;; MESSAGE, but we also do not switch back to the strings
3394 ;; buffer. this may not be so bad, actually, because the user
3395 ;; may want to change his edit.
3396 (error "@STRING definition cannot be empty ")
3397 (setq text (from-raw text)) ; strings cannot be raw
3398 (puthash ebib-current-string text (edb-strings ebib-cur-db))))))
3399 (ebib-set-modified t))
3401 ;;;;;;;;;;;;;;;;;;;;
3402 ;; ebib-help-mode ;;
3403 ;;;;;;;;;;;;;;;;;;;;
3405 (defvar ebib-help-mode-map
3406 (let ((map (make-keymap)))
3407 (suppress-keymap map)
3408 (define-key map " " 'scroll-up)
3409 (define-key map "b" 'scroll-down)
3410 (define-key map "q" 'ebib-quit-help-buffer)
3412 "Keymap for the ebib help buffer.")
3414 (define-derived-mode ebib-help-mode
3415 fundamental-mode "Ebib-help"
3416 "Major mode for the Ebib help buffer."
3417 (setq buffer-read-only t)
3418 (local-set-key "\C-xb" 'ebib-quit-help-buffer)
3419 (local-set-key "\C-xk" 'ebib-quit-help-buffer))
3421 (defun ebib-display-help (buffer)
3422 "Shows the help message for Ebib-buffer BUFFER."
3423 (switch-to-buffer ebib-help-buffer)
3424 (setq ebib-before-help buffer)
3425 (with-buffer-writable
3428 ((eq buffer ebib-index-buffer) (insert ebib-index-buffer-help))
3429 ((eq buffer ebib-entry-buffer) (insert ebib-entry-buffer-help))
3430 ((eq buffer ebib-strings-buffer) (insert ebib-strings-buffer-help)))
3431 (goto-char (point-min))))
3433 (defun ebib-quit-help-buffer ()
3434 "Exits the help buffer."
3437 ((eq ebib-before-help ebib-index-buffer)
3438 (switch-to-buffer ebib-entry-buffer)
3440 ((eq ebib-before-help ebib-entry-buffer)
3441 (switch-to-buffer ebib-entry-buffer))
3442 ((eq ebib-before-help ebib-strings-buffer)
3443 (switch-to-buffer ebib-strings-buffer))))
3449 (defvar ebib-log-mode-map
3450 (let ((map (make-keymap)))
3451 (suppress-keymap map)
3452 (define-key map " " 'scroll-up)
3453 (define-key map "b" 'scroll-down)
3454 (define-key map "q" 'ebib-quit-log-buffer)
3456 "Keymap for the ebib log buffer.")
3458 (define-derived-mode ebib-log-mode
3459 fundamental-mode "Ebib-log"
3460 "Major mode for the Ebib log buffer."
3461 (local-set-key "\C-xb" 'ebib-quit-log-buffer)
3462 (local-set-key "\C-xk" 'ebib-quit-log-buffer))
3464 (defun ebib-quit-log-buffer ()
3465 "Exits the log buffer."
3467 (switch-to-buffer ebib-entry-buffer)
3470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3471 ;; functions for non-Ebib buffers ;;
3472 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3474 (defun ebib-import ()
3475 "Searches for BibTeX entries in the current buffer.
3476 The entries are added to the current database (i.e. the database
3477 that was active when Ebib was lowered. Works on the whole buffer,
3478 or on the region if it is active."
3480 (if (not ebib-cur-db)
3481 (error "No database loaded. Use `o' to open a database")
3482 (if (edb-virtual ebib-cur-db)
3483 (error "Cannot import to a virtual database")
3484 (with-syntax-table ebib-syntax-table
3488 (narrow-to-region (region-beginning)
3490 (let ((buffer (current-buffer)))
3492 (insert-buffer-substring buffer)
3493 (let ((n (ebib-find-bibtex-entries t)))
3494 (setf (edb-keys-list ebib-cur-db) (sort (edb-keys-list ebib-cur-db) 'string<))
3495 (setf (edb-n-entries ebib-cur-db) (length (edb-keys-list ebib-cur-db)))
3496 (when (edb-strings-list ebib-cur-db)
3497 (setf (edb-strings-list ebib-cur-db) (sort (edb-strings-list ebib-cur-db) 'string<)))
3498 (setf (edb-cur-entry ebib-cur-db) (edb-keys-list ebib-cur-db))
3499 (ebib-fill-entry-buffer)
3500 (ebib-fill-index-buffer)
3501 (ebib-set-modified t)
3502 (message (format "%d entries, %d @STRINGs and %s @PREAMBLE found in buffer."
3509 (defun ebib-get-db-from-filename (filename)
3510 "Returns the database struct associated with FILENAME."
3512 (mapc #'(lambda (db)
3513 (if (string= (file-name-nondirectory (edb-filename db)) filename)
3518 (defun ebib-get-local-databases ()
3519 "Returns a list of .bib files associated with the file in the current LaTeX buffer.
3520 Each .bib file is a string holding the name of the .bib
3521 file. This function simply searches the current LaTeX file or its
3522 master file for a \\bibliography command and returns the file(s)
3523 given in its argument. If no \\bibliography command is found,
3524 returns the symbol NONE."
3525 (let ((texfile-buffer (current-buffer))
3527 ;; if AucTeX's TeX-master is used and set to a string, we must
3528 ;; search that file for a \bibliography command, as it's more
3529 ;; likely to be in there than in the file we're in.
3530 (and (boundp 'TeX-master)
3531 (stringp TeX-master)
3532 (setq texfile (ensure-extension TeX-master "tex")))
3534 (if (and texfile (file-readable-p texfile))
3535 (insert-file-contents texfile)
3536 (insert-buffer-substring texfile-buffer))
3538 (goto-char (point-min))
3539 (if (re-search-forward "\\\\bibliography{\\(.*?\\)}" nil t)
3540 (mapcar #'(lambda (file)
3541 (ensure-extension file "bib"))
3542 (split-string (buffer-substring-no-properties (match-beginning 1) (match-end 1)) ",[ ]*"))
3545 (defun ebib-insert-bibtex-key (prefix)
3546 "Inserts a BibTeX key at POINT.
3547 The user is prompted for a BibTeX key and has to choose one from
3548 the database(s) associated with the current LaTeX file, or from
3549 the current database if there is no \\bibliography command. Tab
3554 (or ebib-local-bibtex-filenames
3555 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3557 (if (eq ebib-local-bibtex-filenames 'none)
3558 (if (null (edb-cur-entry ebib-cur-db))
3559 (error "No entries found in current database")
3560 (setq collection (ebib-create-collection (edb-database ebib-cur-db))))
3561 (mapc #'(lambda (file)
3562 (let ((db (ebib-get-db-from-filename file)))
3565 (message "Database %s not loaded" file))
3566 ((null (edb-cur-entry db))
3567 (message "No entries in database %s" file))
3568 (t (setq collection (append (ebib-create-collection (edb-database db))
3570 ebib-local-bibtex-filenames))
3572 (let* ((latex-cmd (or (cdr (assoc prefix ebib-insertion-strings))
3574 (key (completing-read (format "Insert \"%s\" with key: " latex-cmd)
3575 collection nil t nil ebib-minibuf-hist)))
3577 (insert (format (or (cdr (assoc prefix ebib-insertion-strings))
3580 (error "No database loaded"))))
3582 (defun ebib-entry-summary ()
3583 "Shows the fields of the key at POINT.
3584 The key is searched in the database associated with the LaTeX
3585 file, or in the current database if no \\bibliography command can
3590 (or ebib-local-bibtex-filenames
3591 (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3592 (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3594 (if (eq ebib-local-bibtex-filenames 'none)
3595 (if (not (member key (edb-keys-list ebib-cur-db)))
3596 (error "`%s' is not in the current database" key)
3597 (setq entry (gethash key (edb-database ebib-cur-db))))
3600 (mapc #'(lambda (file)
3601 (let ((db (ebib-get-db-from-filename file)))
3603 (message "Database %s not loaded" file)
3604 (if (member key (edb-keys-list db))
3605 (throw 'found (gethash key (edb-database db)))))))
3606 ebib-local-bibtex-filenames)
3609 (error "Entry `%s' not found" key)
3610 (with-output-to-temp-buffer "*Help*"
3611 (ebib-format-fields entry 'princ)))))
3613 (error "No database(s) loaded"))))
3615 ;; (defun ebib-entry-summary ()
3616 ;; "Shows the fields of the key at POINT.
3617 ;; The key is searched in the database associated with the LaTeX
3618 ;; file, or in the current database if no \\bibliography command can
3621 ;; (ebib-execute-when
3623 ;; (or ebib-local-bibtex-filenames
3624 ;; (setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
3625 ;; (let ((key (read-string-at-point "\"#%'(),={} \n\t\f"))
3627 ;; (if (eq ebib-local-bibtex-filenames 'none)
3628 ;; (if (not (member key (edb-keys-list ebib-cur-db)))
3629 ;; (error "`%s' is not in the current database" key)
3630 ;; (setq entry (gethash key (edb-database ebib-cur-db))))
3633 ;; (mapc #'(lambda (file)
3634 ;; (let ((db (ebib-get-db-from-filename file)))
3636 ;; (message "Database %s not loaded" file)
3637 ;; (if (member key (edb-keys-list db))
3638 ;; (throw 'found (gethash key (edb-database db)))))))
3639 ;; ebib-local-bibtex-filenames)
3642 ;; (error "Entry `%s' not found" key)
3643 ;; (with-current-buffer " *Ebib-entry*"
3644 ;; (with-buffer-writable
3646 ;; (ebib-format-fields entry 'insert))))))
3648 ;; (error "No database(s) loaded"))))
3652 ;; we put these at the end, because they seem to mess up Emacs'
3653 ;; syntax highlighting.
3655 (setq ebib-index-buffer-help
3656 "Ebib index buffer -- command key overview
3658 Note: command keys are case-sensitive.
3660 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3663 [Up], k, C-p: go to the previous entry
3664 [Down], j, C-n: go to the next entry
3665 [Home], g: go to the first entry
3666 [End], G: go to the last entry
3667 [PgUp], b, M-p: scroll up
3668 [PgDn], [Space], M-n: scroll down
3671 e: edit the current entry
3672 E: edit the current entry's name
3674 d: delete the current entry
3675 ;-d: deletes all marked entries
3676 t: edit the @STRING definitions
3677 r: edit the @PREAMBLE definition
3678 m: (un)mark the current entry
3679 ;-m: unmarks all marked entries
3682 /: search the database
3683 n: find the next occurrence of the search string
3684 C-s: search for a key (incrementally)
3685 [return]: select the entry under the cursor (use after C-s)
3686 F: follow the crossref field
3687 &: filter the current database with a logical AND
3688 |: filter the current database with a logical OR
3689 ~: filter the current database with a logical NOT
3690 V: show the current filter (with prefix argument:
3691 reapply current filter)
3695 c: close the database
3696 s: save the database
3697 S: save all databases
3698 w: save the database under a different name
3699 M: merge another database
3700 x: export the current entry to another file
3701 (with prefix argument N: copy to database N)
3702 ;-x: export the marked entry to another file
3703 (with prefix argument N: copy to database N)
3704 X: export the @PREAMBLE definition to another file
3705 (with prefix argument N: copy to database N)
3706 f: print the full filename in the minibuffer
3709 1-9: switch to database 1-9
3710 J: switch to another database (accepts prefix argument)
3711 [right], [left]: switch to previous/next database
3712 L: LaTeX the database
3713 ;-L: LaTeX the marked entries
3714 P: print the database
3715 ;-P: print the marked entries
3718 u: extract URL from the `url' field and send it to a browser
3719 p: push the current entry to a LaTeX buffer
3720 ;-p: push marked entries to a LaTeX buffer
3722 z: put Ebib in the background
3724 h: show this help page
3727 (setq ebib-entry-buffer-help
3728 "Ebib entry buffer -- command key overview
3730 Note: command keys are case-sensitive.
3732 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3735 [Up], k, C-p: go to the previous field
3736 [Down], j, C-n: go to the next field
3737 [Home], g: go to the first field
3738 [End], G: go to the last field
3739 [PgUp], b, M-p: go to the previous group of fields
3740 [PgDn], [Space], M-n: go to the next group of fields
3743 e: edit the value of the current field
3744 c: copy the value of the current field (value is put into the kill ring)
3745 x: kill the value of the current field (value is put into the kill ring)
3746 y: yank the most recently copied/cut string
3747 d: delete the value of the current entry
3748 r: toggle the \"rawness\" status of the current field
3749 l: edit the current field as multi-line
3750 s: insert an @STRING abbreviation into the current field
3753 u: extract URL and send it to a browser
3754 q: quit the entry buffer and return to the index buffer
3755 h: show this help page
3758 (setq ebib-strings-buffer-help
3759 "Ebib strings buffer -- command key overview
3761 Note: command keys are case-sensitive.
3763 (Press C-v to scroll down, M-v to scroll up, `q' to quit.)
3766 [Up], k, C-p: go to the previous @STRING definition
3767 [Down], j, C-n: go to the next @STRING definition
3768 [Home], g: go to the first @STRING definition
3769 [End], G: go to the last @STRING definition
3770 [PgUp], b, M-p: scroll 10 @STRING definitions up
3771 [PgDn], [Space], M-n: scroll 10 @STRING definitions down
3774 e: edit the value of the current @STRING definition
3775 c: copy the value of the current @STRING definition
3776 d: delete the current @STRING definition
3777 a: add an @STRING definition
3778 l: edit the current @STRING as multi-line
3781 x: export the current @STRING definition to another file
3782 (with prefix argument N: copy to database N)
3783 X: export all @STRING definitions to another file
3784 (with prefix argument N: copy to database N)
3787 q: quit the strings buffer and return to the index buffer
3788 h: show this help page