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