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