forked from joostkremers/ebib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebib.el
3168 lines (2956 loc) · 137 KB
/
ebib.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ebib.el --- a BibTeX database manager
;; Copyright (c) 2003-2014 Joost Kremers
;; All rights reserved.
;; Author: Joost Kremers <joostkremers@fastmail.fm>
;; Maintainer: Joost Kremers <joostkremers@fastmail.fm>
;; Created: 2003
;; Version: 1.15
;; Keywords: text bibtex
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. The name of the author may not be used to endorse or promote products
;; derived from this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE,
;; DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;; Commentary:
;; Ebib is a BibTeX database manager that runs in GNU Emacs. With Ebib, you
;; can create and manage .bib-files, all within Emacs. It supports @string
;; and @preamble definitions, multi-line field values, searching, and
;; integration with Emacs' (La)TeX mode.
;; See the Ebib manual for usage and installation instructions.
;; The latest release version of Ebib, contact information and mailing list
;; can be found at <http://joostkremers.github.io/ebib>. Development
;; sources can be found at <https://github.com/joostkremers/ebib>.
;;; Code:
(eval-and-compile
(if (string< (format "%d.%d" emacs-major-version emacs-minor-version) "24.3")
(progn
(require 'cl)
(defalias 'cl-caddr 'caddr)
(defalias 'cl-defstruct 'defstruct)
(defalias 'cl-do 'do)
(defalias 'cl-dolist 'dolist)
(defalias 'cl-flet 'flet)
(defalias 'cl-incf 'incf)
(defalias 'cl-labels 'labels)
(defalias 'cl-loop 'loop)
(defalias 'cl-multiple-value-bind 'multiple-value-bind)
(defalias 'cl-multiple-value-setq 'multiple-value-setq)
(defalias 'cl-remove-if 'remove-if)
(defalias 'cl-values 'values))
(require 'cl-lib)))
(require 'easymenu)
(require 'bibtex)
(require 'pp)
(require 'ebib-utils)
(require 'ebib-db)
(require 'ebib-filters)
(require 'ebib-keywords)
(defun ebib-display-buffer-reuse-window (buffer alist)
"Display BUFFER in an existing Ebib buffer.
If BUFFER is the index buffer, simply switch to the window
displaying it. (This function should not be called if there is a
chance the index buffer is not visible.) For any other buffer,
find a window displaying an Ebib buffer other than the index
buffer, switch to that window and display BUFFER. If no window
can be found, return NIL.
Note, The argument ALIST has no function."
(let (window)
(cond
;; the index buffer can only be displayed in its dedicated window.
((eq buffer (cdr (assoc 'index ebib-buffer-alist)))
(setq window (get-buffer-window buffer)))
;; if ebib-layout isn't full, the multiline buffer should not be
;; displayed in an Ebib buffer.
((and (eq buffer (cdr (assoc 'multiline ebib-buffer-alist)))
(not (eq ebib-layout 'full)))
(setq window nil))
(t (let ((buffers (delq nil (mapcar #'(lambda (bf)
(unless (eq (car bf) 'index)
(cdr bf)))
ebib-buffer-alist))))
(while (and buffers
(not (get-buffer-window (car buffers))))
(setq buffers (cdr buffers)))
(setq window (get-buffer-window (car buffers))))))
(when window
(select-window window)
(with-ebib-window-nondedicated
(switch-to-buffer buffer))
window)))
(defun ebib-display-buffer-largest-window (buffer alist)
"Display BUFFER in the largest non-dedicated window.
The argument ALIST has no function."
(unless ebib-popup-entry-window
(let ((window (get-largest-window)))
(select-window window)
(switch-to-buffer buffer)
window)))
(defun ebib-pop-to-buffer (buffer)
"Select or create a window to display BUFFER and display it.
BUFFER is a symbol indicating the buffer to switch to. It can be
'index, 'entry, 'strings, 'log or 'multiline.
If BUFFER is 'index, switch to the index window or signal an
error if there is no window displaying the index buffer.
For any other buffer, if there is a visible Ebib buffer other
than the index buffer, switch to its window and display BUFFER.
If there is no Ebib window, use the largest non-dedicated window
or, if `ebib-layout' is set to `popup', pop up a new window. If
all else fails, pop up a new frame."
(if (or (not (eq buffer 'index))
(get-buffer-window (cdr (assoc 'index ebib-buffer-alist))))
(pop-to-buffer (cdr (assoc buffer ebib-buffer-alist))
'((ebib-display-buffer-reuse-window
ebib-display-buffer-largest-window
display-buffer-pop-up-window
display-buffer-pop-up-frame))
t)
(error "Ebib is lowered. Use `M-x ebib' to restart")))
(defun ebib-display-entry (entry-key)
"Display ENTRY-KEY in the index buffer at POINT."
(with-current-buffer (cdr (assoc 'index ebib-buffer-alist))
(with-ebib-buffer-writable
(insert (format "%-30s %s\n"
entry-key
(if ebib-index-display-fields
(mapconcat #'(lambda (field)
(or (ebib-db-get-field-value field entry-key ebib-cur-db 'noerror 'unbraced)
""))
ebib-index-display-fields
" ") ; separator for mapconcat
""))))))
(defun ebib-redisplay-current-field ()
"Redisplay the contents of the current field in the entry buffer."
(with-current-buffer (cdr (assoc 'entry ebib-buffer-alist))
;; If the `crossref' field has changed, we need to redisplay the entire entry.
(if (eq ebib-current-field 'crossref)
(progn
(ebib-fill-entry-buffer)
(setq ebib-current-field 'crossref)
(re-search-forward "^crossref")
(ebib-set-fields-highlight))
(with-ebib-buffer-writable
(goto-char (ebib-highlight-start ebib-fields-highlight))
(let ((beg (point)))
(end-of-line)
(delete-region beg (point)))
(insert (propertize (format "%-17s " (symbol-name ebib-current-field)) 'face 'ebib-field-face)
(ebib-get-field-highlighted ebib-current-field (ebib-cur-entry-key)))
(ebib-set-fields-highlight)))))
(defun ebib-redisplay-current-string ()
"Redisplay the current string definition in the strings buffer."
(with-current-buffer (cdr (assoc 'strings ebib-buffer-alist))
(with-ebib-buffer-writable
(let ((val (ebib-db-get-string ebib-current-string ebib-cur-db nil 'unbraced)))
(goto-char (ebib-highlight-start ebib-strings-highlight))
(let ((beg (point)))
(end-of-line)
(delete-region beg (point)))
(insert (format "%-18s %s" ebib-current-string
(if (ebib-multiline-p val)
(concat "+" (ebib-first-line val))
(concat " " val))))
(ebib-set-strings-highlight)))))
(defun ebib-move-to-field (field direction)
"Move the fields overlay to the line containing FIELD.
If DIRECTION is positive, searches forward, if DIRECTION is
negative, searches backward. If DIRECTION is 1 or -1, searches
from POINT, if DIRECTION is 2 or -2, searches from beginning or
end of buffer. If FIELD is not found in the entry buffer, the
overlay is not moved. FIELD must be a symbol."
;; Note: this function does NOT change the value of `ebib-current-field'!
(with-current-buffer (cdr (assoc 'entry ebib-buffer-alist))
(if (eq field '=type=)
(goto-char (point-min))
(cl-multiple-value-bind (fn start limit) (if (>= direction 0)
(cl-values 're-search-forward (point-min) (point-max))
(cl-values 're-search-backward (point-max) (point-min)))
;; make sure we can get back to our original position, if the field
;; cannot be found in the buffer:
(let ((current-pos (point)))
(when (eq (logand direction 1) 0) ; if direction is even
(goto-char start))
(unless (funcall fn (concat "^" (symbol-name field)) limit t)
(goto-char current-pos)))))
(ebib-set-fields-highlight)))
(defun ebib-get-xref-field (field entry-type)
"Return the field from which FIELD inherits in ENTRY-TYPE.
For BibTeX, a field simply inherits from the same field in the
cross-referenced entry. BibLaTeX, however, allows complex
inheritance relations, so that e.g., the field `booktitle' may
inherit from the field `title' in the cross-referenced entry.
The inheritance scheme is stored in `ebib-biblatex-inheritance'."
(let ((inheritances (or (cadr (assq entry-type ebib-biblatex-inheritance))
(cadr (assq 'all ebib-biblatex-inheritance)))))
(or (cadr (assq field inheritances))
field)))
(defun ebib-get-field-highlighted (field key &optional match-str db)
"Return the contents of FIELD in entry KEY in DB with MATCH-STR highlighted."
;; Note: we need to work on a copy of the string, otherwise the highlights
;; are made to the string as stored in the database. Hence copy-sequence.
(or db (setq db ebib-cur-db))
(let* ((case-fold-search t)
(value (ebib-db-get-field-value field key db 'noerror nil 'xref))
(string (if (car value)
(copy-sequence (car value))))
(xref (cadr value))
(raw " ")
(multiline " ")
(matched nil))
;; we have to do a couple of things now:
;; - remove {} or "" around the string, if they're there
;; - search for match-str
;; - properly adjust the string if it's multiline
;; but all this is not necessary if there was no string
(when string
(if xref
(setq string (propertize string 'face 'ebib-crossref-face 'fontified t)))
(if (ebib-db-unbraced-p string)
(setq raw "*")
(setq string (ebib-db-unbrace string))) ; we have to make the string look nice
(when match-str
(cl-multiple-value-setq (string matched) (ebib-match-all-in-string match-str string)))
(when (ebib-multiline-p string)
;; IIUC PROPERTIZE shouldn't be necessary here, as the variable
;; multiline is local and therefore the object it refers to should
;; be GC'ed when the function returns. But for some reason, the
;; plus sign is persistent, and if it's been highlighted as the
;; result of a search, it stays that way.
(setq multiline (propertize "+" 'face nil))
(setq string (ebib-first-line string)))
(when (and matched
(string= multiline "+"))
(add-text-properties 0 1 '(face highlight) multiline)))
(concat raw multiline string)))
(defun ebib-format-fields (key &optional match-str db)
"Format the fields of entry KEY in DB.
The fields are inserted in the current buffer with their values.
If MATCH-STR is provided, then when it is present in the value,
it is highlighted. DB defaults to the current database."
(or db
(setq db ebib-cur-db))
(let* ((entry (ebib-db-get-entry key db))
(entry-type (cdr (assoc '=type= entry)))
(obl-fields (ebib-get-obl-fields entry-type))
(opt-fields (ebib-get-opt-fields entry-type)))
(insert (format "%-19s %s\n" (propertize "type" 'face 'ebib-field-face) entry-type))
(mapc #'(lambda (fields)
(insert "\n")
(mapcar #'(lambda (field)
(unless (and (get field 'ebib-hidden)
ebib-hide-hidden-fields)
(insert (propertize (format "%-17s " field) 'face 'ebib-field-face))
(insert (or
(ebib-get-field-highlighted field key match-str)
""))
(insert "\n")))
fields))
(list obl-fields opt-fields ebib-additional-fields))))
(defun ebib-redisplay ()
"Redisplay the index and entry buffers."
(ebib-fill-index-buffer)
(ebib-fill-entry-buffer))
(defun ebib-fill-index-buffer ()
"Fill the index buffer with the list of keys in `ebib-cur-db'.
If `ebib-cur-db' is nil, the buffer is just erased and its name set
to \"none\". This function sets `ebib-cur-keys-list'."
(with-current-buffer (cdr (assoc 'index ebib-buffer-alist))
;; First set the modification flag, so that it's still correct after
;; with-ebib-buffer-writable.
(when ebib-cur-db
(set-buffer-modified-p (ebib-db-modified-p ebib-cur-db)))
(with-ebib-buffer-writable
(erase-buffer)
(if (not ebib-cur-db)
(rename-buffer " none")
(setq ebib-cur-keys-list (ebib-list-keys))
;; Set a header line if there is a filter.
(setq header-line-format (ebib-filters-pp-filter (ebib-db-get-filter ebib-cur-db)))
;; We may call this function when there are no entries in the
;; database. If so, we don't need to do this:
(when (ebib-cur-entry-key)
;; It may be that no entry satisfies the filter.
(if (not ebib-cur-keys-list)
(message "No entries matching the filter")
;; Make sure the current entry is among the visible entries.
(unless (member (ebib-cur-entry-key) ebib-cur-keys-list)
(ebib-db-set-current-entry-key (car ebib-cur-keys-list) ebib-cur-db))
(mapc #'(lambda (entry)
(ebib-display-entry entry)
(when (member entry (ebib-db-list-marked-entries ebib-cur-db 'nosort))
(save-excursion
(forward-line -1)
(ebib-display-mark t))))
ebib-cur-keys-list)
(goto-char (point-min))
(re-search-forward (format "^%s " (ebib-cur-entry-key)))
(beginning-of-line)
(ebib-set-index-highlight)))
(rename-buffer (concat (format " %d:" (1+ (- (length ebib-databases)
(length (member ebib-cur-db ebib-databases)))))
(ebib-db-get-filename ebib-cur-db 'shortened)))))))
(defun ebib-list-keys ()
"Return a list of entry keys in the current database.
If a filter is active, only the keys of entries that match the
filter are returned. The returned list is sorted."
(when ebib-cur-db
(if (ebib-db-get-filter ebib-cur-db)
(ebib-filters-run-filter ebib-cur-db)
(ebib-db-list-keys ebib-cur-db))))
(defun ebib-display-mark (mark &optional beg end)
"Highlight/unhighlight an entry.
Add/remove `ebib-marked-face` to the region between BEG and END,
or to the entry point is on if these are omitted. If MARK is t,
`ebib-marked-face is added, if nil, it is removed. NB: if BEG and
END are omitted, this function changes point."
(unless (and beg end)
(beginning-of-line)
(setq beg (point))
(skip-chars-forward "^ ")
(setq end (point)))
(if mark
(add-text-properties beg end '(face ebib-marked-face))
(remove-text-properties beg end '(face ebib-marked-face))))
(defun ebib-fill-entry-buffer (&optional match-str)
"Fill the entry buffer with the fields of the current entry.
MATCH-STRING is a regexp that will be highlighted when it occurs
in the field contents."
(with-current-buffer (cdr (assoc 'entry ebib-buffer-alist))
(with-ebib-buffer-writable
(erase-buffer)
(when ebib-cur-keys-list ; are there entries being displayed?
(ebib-format-fields (ebib-cur-entry-key) match-str)
(setq ebib-current-field '=type=)
(goto-char (point-min))))))
;;;###autoload
(defun ebib (&optional file key)
"Ebib, a BibTeX database manager.
Optional argument FILE is a file to load. If FILE is already
loaded, switch to it. If KEY is given, jump to it."
(interactive)
;; First do some stuff in the buffer from which Ebib was called.
;; Save the buffer from which Ebib is called.
(setq ebib-buffer-before (current-buffer))
;; Set the push buffer to the current buffer.
(setq ebib-push-buffer (current-buffer))
;; See if there are local databases.
(or ebib-local-bibtex-filenames
(setq ebib-local-bibtex-filenames (ebib-get-local-databases)))
(or key (setq key (ebib-read-string-at-point "][^\"@\\&$#%',={} \t\n\f"))) ; See if there's a key at point.
;; Initialize Ebib if required.
(ebib-init)
;; Set up the windows.
(ebib-setup-windows)
;; See if we have a file.
(if file
(ebib-load-bibtex-file-internal (ebib-locate-bibfile file (append ebib-bib-search-dirs (list default-directory)))))
;; See if we have a key; ebib-cur-keys-list must be set for this to work.
(or ebib-cur-keys-list (setq ebib-cur-keys-list (ebib-list-keys)))
(if key
(ebib-find-and-set-key key (buffer-local-value 'ebib-local-bibtex-filenames ebib-buffer-before)))
(ebib-redisplay))
;;;###autoload
(defun ebib-open-org-link (key)
"Open Ebib and jump to KEY.
This is for use in Org-mode links."
(ebib nil key))
(defun ebib-find-and-set-key (key files)
"Make KEY the current entry.
FILES is a list of BibTeX files in which KEY is searched. If
FILES is `none', only the current database is searched."
(when ebib-databases
(if (eq files 'none)
(unless (member key ebib-cur-keys-list)
(setq key nil))
(let ((database (catch 'found
(mapc #'(lambda (file)
(let ((db (ebib-get-db-from-filename file)))
(if (and db (member key (ebib-db-list-keys db 'nosort)))
(throw 'found db))))
files)
nil))) ; We must return nil if the key wasn't found anywhere.
(if (null database)
(setq key nil)
(setq ebib-cur-db database))))
(if key
(ebib-db-set-current-entry-key key ebib-cur-db))))
(defun ebib-read-string-at-point (chars)
"Read a string at POINT delimited by CHARS and return it.
CHARS is a string of characters that should not occur in the string."
(save-excursion
(skip-chars-backward (concat "^" chars))
(let ((beg (point)))
(ebib-looking-at-goto-end (concat "[^" chars "]*"))
(buffer-substring-no-properties beg (point)))))
(defun ebib-init ()
"Initialise Ebib.
This function sets all variables to their initial values, creates
the buffers, reads the rc file and loads the files in
`ebib-preload-bib-files'."
(unless ebib-initialized
(setq ebib-current-field nil
ebib-saved-window-config nil)
(put 'timestamp 'ebib-hidden t)
(ebib-create-buffers)
(if (and ebib-keywords-file
(file-name-directory ebib-keywords-file)) ; returns nil if there is no directory part
(add-to-list 'ebib-keywords-files-alist (list (file-name-directory ebib-keywords-file)
(ebib-read-file-to-list ebib-keywords-file) nil)))
(setq ebib-keywords-list-per-session (copy-tree ebib-keywords-list))
(ebib-filters-load-file ebib-filters-default-file)
(setq ebib-index-highlight (ebib-make-highlight 1 1 (cdr (assoc 'index ebib-buffer-alist))))
(setq ebib-fields-highlight (ebib-make-highlight 1 1 (cdr (assoc 'entry ebib-buffer-alist))))
(setq ebib-strings-highlight (ebib-make-highlight 1 1 (cdr (assoc 'strings ebib-buffer-alist))))
(add-hook 'kill-emacs-query-functions 'ebib-kill-emacs-query-function)
(load ebib-rc-file t)
(if ebib-preload-bib-files
(mapc #'(lambda (file)
(ebib-load-bibtex-file-internal (or (locate-file file ebib-bib-search-dirs)
file)))
ebib-preload-bib-files))
(setq ebib-initialized t)))
(defun ebib-setup-windows ()
"Create Ebib's window configuration in the current frame."
;; If the index buffer is visible, just switch to it.
(let ((index-window (get-buffer-window (cdr (assoc 'index ebib-buffer-alist)))))
(if index-window
(select-window index-window)
;; Save the current window configuration.
(setq ebib-saved-window-config (current-window-configuration))
(cond
((eq ebib-layout 'full)
(delete-other-windows))
((eq ebib-layout 'custom)
(setq ebib-window-before (selected-window))
(let ((width (cond
((integerp ebib-width)
(- (window-total-width) ebib-width))
((floatp ebib-width)
(- (window-total-width) (truncate (* (window-total-width) ebib-width)))))))
(select-window (split-window (selected-window) width t)))))
(let* ((index-window (selected-window))
(entry-window (split-window index-window ebib-index-window-size
ebib-window-vertical-split)))
(switch-to-buffer (cdr (assoc 'index ebib-buffer-alist)))
(unless (eq ebib-layout 'index-only)
(set-window-buffer entry-window (cdr (assoc 'entry ebib-buffer-alist))))
(set-window-dedicated-p index-window t)
(if (eq ebib-layout 'custom)
(set-window-dedicated-p entry-window t))))))
(defun ebib-create-buffers ()
"Create the buffers for Ebib."
;; First we create a buffer for multiline editing. This one does *not*
;; have a name beginning with a space, because undo-info is normally
;; present in an edit buffer.
(add-to-list 'ebib-buffer-alist (cons 'multiline (get-buffer-create "*Ebib-edit*")))
(with-current-buffer (cdr (assoc 'multiline ebib-buffer-alist))
(funcall ebib-multiline-major-mode)
(ebib-multiline-mode t))
;; Then we create a buffer to hold the fields of the current entry.
(add-to-list 'ebib-buffer-alist (cons 'entry (get-buffer-create "*Ebib-entry*")))
(with-current-buffer (cdr (assoc 'entry ebib-buffer-alist))
(ebib-entry-mode)
(buffer-disable-undo))
;; Then we create a buffer to hold the @STRING definitions.
(add-to-list 'ebib-buffer-alist (cons 'strings (get-buffer-create "*Ebib-strings*")))
(with-current-buffer (cdr (assoc 'strings ebib-buffer-alist))
(ebib-strings-mode)
(buffer-disable-undo))
;; The log buffer.
(add-to-list 'ebib-buffer-alist (cons 'log (get-buffer-create "*Ebib-log*")))
(with-current-buffer (cdr (assoc 'log ebib-buffer-alist))
(erase-buffer)
(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")
(ebib-log-mode)
(buffer-disable-undo))
;; And lastly we create a buffer for the entry keys.
(add-to-list 'ebib-buffer-alist (cons 'index (get-buffer-create " none")))
(with-current-buffer (cdr (assoc 'index ebib-buffer-alist))
(ebib-index-mode)
(buffer-disable-undo)
(if ebib-index-mode-line
(setq mode-line-format ebib-index-mode-line))))
(defun ebib-quit ()
"Quit Ebib.
The Ebib buffers are killed, all variables except the keymaps are set to nil."
(interactive)
(when (if (ebib-modified-p)
(yes-or-no-p "There are modified databases. Quit anyway? ")
(y-or-n-p "Quit Ebib? "))
(ebib-keywords-save-all-new)
(ebib-filters-update-filters-file)
(mapc #'(lambda (buffer)
(kill-buffer buffer))
(mapcar #'cdr ebib-buffer-alist))
(setq ebib-databases nil
ebib-cur-db nil
ebib-buffer-alist nil
ebib-initialized nil
ebib-index-highlight nil
ebib-fields-highlight nil
ebib-strings-highlight nil
ebib-export-filename nil
ebib-window-before nil
ebib-buffer-before nil
ebib-cur-keys-list nil
ebib-keywords-files-alist nil
ebib-keywords-list-per-session nil
ebib-filters-alist nil)
(set-window-configuration ebib-saved-window-config)
(remove-hook 'kill-emacs-query-functions 'ebib-kill-emacs-query-function)
(message "")))
(defun ebib-kill-emacs-query-function ()
"Funktion to run if Emacs is killed.
Ask if the user wants to save any modified databases and added
keywords before Emacs is killed."
(when (or (not (ebib-modified-p))
(if (y-or-n-p "Save all unsaved Ebib databases? ")
(progn
(ebib-save-all-databases)
t)
(yes-or-no-p "Ebib holds modified databases. Kill anyway? ")))
(ebib-keywords-save-all-new)
t))
;;;;;;;;;;;;;;;;
;; index-mode ;;
;;;;;;;;;;;;;;;;
(eval-and-compile
(define-prefix-command 'ebib-prefix-map)
(suppress-keymap ebib-prefix-map))
;; macro to redefine key bindings.
(defmacro ebib-key (buffer key &optional command prefixed)
"Set KEY in BUFFER to COMMAND.
BUFFER is a symbol designating an Ebib buffer and can be `index',
`entry', `strings'. KEY is a standard Emacs key description as
passed to `define-key'. If COMMAND is nil, KEY is unbound. If
PREFIXED is non-nil, KEY is also bound in `ebib-prefix-map'.
BUFFER can also be `mark-prefix', in which case the prefix for
operating on marked entries is set to KEY; it can also be
`multiline', in which case the second character of the commands
in the multiline edit buffer is set to KEY. In either case,
COMMAND and PREFIXED are meaningless."
(cond
((eq buffer 'index)
(let ((one `(define-key ebib-index-mode-map ,key (quote ,command)))
(two (when (or prefixed
(null command))
`(define-key ebib-prefix-map ,key (quote ,command)))))
(if two
`(progn ,one ,two)
one)))
((eq buffer 'entry)
`(define-key ebib-entry-mode-map ,key (quote ,command)))
((eq buffer 'strings)
`(define-key ebib-strings-mode-map ,key (quote ,command)))
((eq buffer 'mark-prefix)
`(progn
(define-key ebib-index-mode-map (format "%c" ebib-prefix-key) nil)
(define-key ebib-index-mode-map ,key 'ebib-prefix-map)
(setq ebib-prefix-key (string-to-char ,key))))
((eq buffer 'multiline)
`(progn
(define-key ebib-multiline-mode-map "\C-c" nil)
(mapc #'(lambda (command)
(define-key ebib-multiline-mode-map (format "\C-c%s%c" ,key (car command)) (cdr command)))
'((?q . ebib-quit-multiline-edit-and-save)
(?c . ebib-cancel-multiline-edit)
(?s . ebib-save-from-multiline-edit)))
(setq ebib-multiline-key (string-to-char ,key))))))
(defvar ebib-index-mode-map
(let ((map (make-keymap)))
(suppress-keymap map)
map)
"Keymap for the ebib index buffer.")
;; We define the keys with ebib-key rather than with define-key, because
;; that way we can set up ebib-prefix-map as well.
(ebib-key index [up] ebib-prev-entry)
(ebib-key index [down] ebib-next-entry)
(ebib-key index [right] ebib-next-database)
(ebib-key index [left] ebib-prev-database)
(ebib-key index [prior] ebib-index-scroll-down)
(ebib-key index [next] ebib-index-scroll-up)
(ebib-key index [home] ebib-goto-first-entry)
(ebib-key index [end] ebib-goto-last-entry)
(ebib-key index [return] ebib-select-and-popup-entry)
(ebib-key index " " ebib-index-scroll-up)
(ebib-key index "/" ebib-search)
(ebib-key index "&" ebib-filters-logical-and)
(ebib-key index "|" ebib-filters-logical-or)
(ebib-key index "~" ebib-filters-logical-not)
(ebib-key index ";" ebib-prefix-map)
(ebib-key index "?" ebib-info)
(ebib-key index "a" ebib-add-entry)
(ebib-key index "A" ebib-show-annotation)
(ebib-key index "b" ebib-index-scroll-down)
(ebib-key index "c" ebib-index-c)
(ebib-key index "C" ebib-follow-crossref)
(ebib-key index "d" ebib-delete-entry t)
(ebib-key index "e" ebib-edit-entry)
(ebib-key index "E" ebib-edit-keyname)
(ebib-key index "f" ebib-view-file)
(ebib-key index "F" ebib-filters-map)
(ebib-key index "g" ebib-goto-first-entry)
(ebib-key index "G" ebib-goto-last-entry)
(ebib-key index "h" ebib-index-help)
(ebib-key index "i" ebib-browse-doi)
(ebib-key index "j" ebib-next-entry)
(ebib-key index "J" ebib-switch-to-database)
(ebib-key index "k" ebib-prev-entry)
(ebib-key index "K" ebib-generate-autokey)
(ebib-key index "l" ebib-show-log)
(ebib-key index "m" ebib-mark-entry t)
(ebib-key index "n" ebib-search-next)
(ebib-key index [(control n)] ebib-next-entry)
(ebib-key index [(meta n)] ebib-index-scroll-up)
(ebib-key index "o" ebib-load-bibtex-file)
(ebib-key index "p" ebib-push-bibtex-key t)
(ebib-key index [(control p)] ebib-prev-entry)
(ebib-key index [(meta p)] ebib-index-scroll-down)
(ebib-key index "P" ebib-edit-preamble)
(ebib-key index "q" ebib-quit)
(ebib-key index "r" ebib-reload-current-database)
(ebib-key index "R" ebib-reload-all-databases)
(ebib-key index "s" ebib-save-current-database)
(ebib-key index "S" ebib-edit-strings)
(ebib-key index "u" ebib-browse-url)
(ebib-key index "x" ebib-export-entry t)
(ebib-key index "\C-xb" ebib-leave-ebib-windows)
(ebib-key index "\C-xk" ebib-quit)
(ebib-key index "X" ebib-export-preamble)
(ebib-key index "z" ebib-leave-ebib-windows)
(ebib-key index "Z" ebib-lower)
(defun ebib-switch-to-database-nth (key)
"Switch to the NTH database.
This function is meant to be bound to the keys 1-9, whereby the
number is also the argument to the function."
(interactive (list (if (featurep 'xemacs)
(event-key last-command-event)
last-command-event)))
(ebib-switch-to-database (- (if (featurep 'xemacs)
(char-to-int key)
key) 48)))
(mapc #'(lambda (key)
(define-key ebib-index-mode-map (format "%d" key)
'ebib-switch-to-database-nth))
'(1 2 3 4 5 6 7 8 9))
(define-derived-mode ebib-index-mode
fundamental-mode "Ebib-index"
"Major mode for the Ebib index buffer."
(setq buffer-read-only t)
(setq cursor-type nil)
(setq truncate-lines t))
(easy-menu-define ebib-index-menu ebib-index-mode-map "Ebib index menu"
'("Ebib"
["Open Database..." ebib-load-bibtex-file t]
["Merge Database..." ebib-merge-bibtex-file (and ebib-cur-db (not (ebib-db-get-filter ebib-cur-db)))]
["Save Database" ebib-save-current-database (and ebib-cur-db
(ebib-db-modified-p ebib-cur-db))]
["Save All Databases" ebib-save-all-databases (ebib-modified-p)]
["Save Database As..." ebib-write-database ebib-cur-db]
["Close Database" ebib-close-database ebib-cur-db]
"--"
["Save New Keywords For Database" ebib-keywords-save-cur-db (ebib-keywords-new-p ebib-cur-db)]
["Save All New Keywords" ebib-keywords-save-all-new (ebib-keywords-new-p)]
"--"
("Entry"
["Add" ebib-add-entry (and ebib-cur-db (not (ebib-db-get-filter ebib-cur-db)))]
["Edit" ebib-edit-entry ebib-cur-keys-list]
["Delete" ebib-delete-entry (and ebib-cur-db
(ebib-cur-entry-key)
(not (ebib-db-get-filter ebib-cur-db)))])
["Edit Strings" ebib-edit-strings (and ebib-cur-db (not (ebib-db-get-filter ebib-cur-db)))]
["Edit Preamble" ebib-edit-preamble (and ebib-cur-db (not (ebib-db-get-filter ebib-cur-db)))]
"--"
["Open URL" ebib-browse-url (ebib-db-get-field-value ebib-standard-url-field (ebib-cur-entry-key) ebib-cur-db 'noerror)]
["Open DOI" ebib-browse-doi (ebib-db-get-field-value ebib-standard-doi-field (ebib-cur-entry-key) ebib-cur-db 'noerror)]
["View File" ebib-view-file (ebib-db-get-field-value ebib-standard-file-field (ebib-cur-entry-key) ebib-cur-db 'noerror)]
("Print Entries"
["As Bibliography" ebib-latex-entries (and ebib-cur-db (not (ebib-db-get-filter ebib-cur-db)))]
["As Index Cards" ebib-print-entries ebib-cur-db]
["Print Multiline Fields" ebib-toggle-print-multiline :enable t
:style toggle :selected ebib-print-multiline]
["Print Cards on Separate Pages" ebib-toggle-print-newpage :enable t
:style toggle :selected ebib-print-newpage])
"--"
("Options"
["Show Hidden Fields" ebib-toggle-hidden :enable t
:style toggle :selected (not ebib-hide-hidden-fields)]
["Use Timestamp" ebib-toggle-timestamp :enable t
:style toggle :selected ebib-use-timestamp]
["Save Cross-Referenced Entries First" ebib-toggle-xrefs-first :enable t
:style toggle :selected ebib-save-xrefs-first]
["Allow Identical Fields" ebib-toggle-identical-fields :enable t
:style toggle :selected ebib-allow-identical-fields]
["Full Layout" ebib-toggle-layout :enable t
:style toggle :selected (eq ebib-layout 'full)]
["Modify Entry Types" ebib-customize-entry-types t]
["Customize Ebib" ebib-customize t])
["View Log Buffer" ebib-show-log t]
["Lower Ebib" ebib-lower t]
["Quit" ebib-quit t]
["Help on Ebib" ebib-info t]))
(easy-menu-add ebib-index-menu ebib-index-mode-map)
(defun ebib-customize ()
"Switch to Ebib's customisation group."
(interactive)
(ebib-lower)
(customize-group 'ebib))
(defun ebib-customize-entry-types ()
"Customize `ebib-entry-types'."
(interactive)
(ebib-lower)
(customize-variable 'ebib-entry-types))
(defun ebib-load-bibtex-file (&optional file)
"Open a BibTeX file."
(interactive)
(unless file
(setq file (ebib-ensure-extension (read-file-name "File to open: " "~/") (car ebib-bibtex-extensions))))
(ebib-load-bibtex-file-internal file)
(ebib-redisplay))
(defun ebib-load-bibtex-file-internal (file)
"Helper function for `ebib-load-bibtex-file'.
Note: it is assumed that FILE is a fully expanded filename."
(let ((db (ebib-get-db-from-filename file)))
(if db ; FILE is already open in Ebib.
(setq ebib-cur-db db)
(setq ebib-cur-db (ebib-create-new-database))
(ebib-db-set-filename file ebib-cur-db)
(setq ebib-log-error nil) ; we haven't found any errors
(ebib-log 'log "%s: Opening file %s" (format-time-string "%d %b %Y, %H:%M:%S") file)
(if (file-exists-p file)
(progn
;; load the entries in the file
(ebib-load-entries file ebib-cur-db)
;; If the user makes any changes, we'll want to create a back-up.
(ebib-db-set-backup t ebib-cur-db)
(ebib-db-set-current-entry-key t ebib-cur-db)
(ebib-set-modified nil))
;; if the file does not exist, we need to issue a message.
(ebib-log 'message "(New file)"))
;; add keywords for the new database
(ebib-keywords-load-keywords ebib-cur-db)
(if ebib-keywords-files-alist
(ebib-log 'log "Using keywords from %s." (ebib-keywords-get-file ebib-cur-db))
(ebib-log 'log "Using general keyword list.")))))
(defun ebib-reload-current-database ()
"Reload the current database from disk."
(interactive)
(ebib-execute-when
((entries)
(when (yes-or-no-p "Reload current database from file ")
(ebib-reload-database ebib-cur-db)
(ebib-set-modified nil)
(ebib-redisplay)
(message "Database reloaded")))
((default) (beep))))
(defun ebib-reload-all-databases ()
"Reload all databases from disk."
(interactive)
(when (yes-or-no-p "Reload all databases from file ")
(mapc #'(lambda (db)
(ebib-reload-database db)
(ebib-set-modified nil db))
ebib-databases)
(ebib-redisplay)))
(defun ebib-reload-database (db)
"Reload database DB from disk."
(let ((file (ebib-db-get-filename db)))
;; first clear out some variables
(ebib-db-clear-database db)
;; then load the file
(ebib-log 'log "%s: Reloading file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") file)
(ebib-db-set-filename file db)
(ebib-load-entries file db)
(ebib-db-set-current-entry-key t db)))
(defun ebib-merge-bibtex-file ()
"Merge a BibTeX file into the current database."
(interactive)
(ebib-execute-when
((real-db)
(let ((file (expand-file-name (read-file-name "File to merge: "))))
(if (not (file-readable-p file))
(error "No such file: %s" file)
(setq ebib-log-error nil) ; we haven't found any errors (yet)
(ebib-log 'log "%s: Merging file %s" (format-time-string "%d-%b-%Y: %H:%M:%S") (ebib-db-get-filename ebib-cur-db))
(ebib-load-entries file ebib-cur-db)
(unless (ebib-cur-entry-key)
(ebib-db-set-current-entry-key t ebib-cur-db))
(ebib-redisplay)
(ebib-set-modified t))))
((default) (beep))))
(defun ebib-load-entries (file db)
"Load BibTeX entries from FILE into DB."
(with-temp-buffer
(with-syntax-table ebib-syntax-table
(insert-file-contents file)
(let ((result (ebib-find-bibtex-entries db nil)))
;; Log the results.
(ebib-log 'message "%d entries, %d @STRINGs and %s @PREAMBLE found in file."
(car result)
(cadr result)
(if (cl-caddr result)
"a"
"no"))
(when ebib-log-error
(message "%s found! Press `l' to check Ebib log buffer." (nth ebib-log-error '("Warnings" "Errors"))))))))
(defun ebib-find-bibtex-entries (db timestamp)
"Find the BibTeX entries in the current buffer.
The search is started at the beginnig of the buffer. All entries
found are stored in DB. Return value is a three-element list: the
first element is the number of entries found, the second the
number of @STRING definitions, and the third is T or NIL,
indicating whether a @PREAMBLE was found.
TIMESTAMP indicates whether a timestamp is to be added to each
entry. Note that a timestamp is only added if `ebib-use-timestamp'
is set to T."
(let ((n-entries 0)
(n-strings 0)
(preamble nil))
(goto-char (point-min))
(while (re-search-forward "^@" nil t) ; find the next entry
(let ((beg (point)))
(if (ebib-looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[[:space:]]*[\(\{]") 1)
(let ((entry-type (downcase (buffer-substring-no-properties beg (point)))))
(ebib-looking-at-goto-end "[[:space:]]*[\(\{]")
(cond
((equal entry-type "string") ; string and preamble must be treated differently
(if (ebib-read-string db)
(setq n-strings (1+ n-strings))))
((equal entry-type "preamble")
(when (ebib-read-preamble db)
(setq preamble t)))
((equal entry-type "comment") ; ignore comments
(ebib-log 'log "Comment at line %d ignored" (line-number-at-pos))
(ebib-match-paren-forward (point-max)))
((assoc (intern-soft entry-type) ebib-entry-types) ; if the entry type has been defined
(if (ebib-read-entry entry-type db timestamp)
(setq n-entries (1+ n-entries))))
;; anything else we report as an unknown entry type.
(t (ebib-log 'warning "Line %d: Unknown entry type `%s'. Skipping." (line-number-at-pos) entry-type)
(ebib-match-paren-forward (point-max)))))
(ebib-log 'error "Error: illegal entry type at line %d. Skipping" (line-number-at-pos)))))
(list n-entries n-strings preamble)))
(defun ebib-read-string (db)
"Read the @STRING definition beginning at the line POINT is on.
If a proper abbreviation and string are found, they are stored in
DB. Return the string if one was read, NIL otherwise."
(let ((limit (save-excursion ; we find the matching end parenthesis
(backward-char)
(ebib-match-paren-forward (point-max))
(point))))
(skip-chars-forward "\"#%'(),={} \n\t\f" limit)
(let ((beg (point)))
(if (ebib-looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
(ebib-ifstring (abbr (buffer-substring-no-properties beg (point)))
(progn
(skip-chars-forward "^\"{" limit)
(let ((beg (point)))
(ebib-ifstring (string (if (ebib-match-delim-forward limit)
(buffer-substring-no-properties beg (1+ (point)))
nil))
(unless (ebib-db-set-string abbr string db 'noerror)
(ebib-log 'warning (format "Line %d: @STRING definition `%s' duplicated. Skipping."
(line-number-at-pos) abbr)))))))
(ebib-log 'error "Error: illegal string identifier at line %d. Skipping" (line-number-at-pos))))))
(defun ebib-read-preamble (db)
"Read the @PREAMBLE definition and stores it in DB.
If there was already another @PREAMBLE definition, the new one is
added to the existing one with a hash sign `#' between them."
(let ((beg (point)))
(forward-char -1)
(when (ebib-match-paren-forward (point-max))
(ebib-db-set-preamble (buffer-substring-no-properties beg (point)) db 'append))))
(defun ebib-read-entry (entry-type db &optional timestamp)
"Read a BibTeX entry and store it in DB.
Returns the entry key if an entry was found, NIL otherwise.
Optional argument TIMESTAMP indicates whether a timestamp is to
be added. (Whether a timestamp is actually added, also depends on
`ebib-use-timestamp'.)"
(setq entry-type (intern-soft entry-type))
(let ((limit (save-excursion
(backward-char)
(ebib-match-paren-forward (point-max))
(point)))
(beg (progn
(skip-chars-forward " \n\t\f") ; note the space!
(point))))
(let (entry-key)
(if (ebib-looking-at-goto-end (concat "\\("
ebib-key-regexp
"\\)[ \t\n\f]*,")
1) ; this delimits the entry key
(progn ; if we found an entry key
(setq entry-key (buffer-substring-no-properties beg (point)))
(when (string= entry-key "") ; to be on the safe side
(setq entry-key (ebib-generate-tempkey db))
(ebib-log 'warning "Line %d: Temporary key generated for entry." (line-number-at-pos)))
(skip-chars-forward "^,")) ; move to the comma after the entry key
;; if there is no legal entry key, we create a temporary key and try to read the entry anyway.
(setq entry-key (ebib-generate-tempkey db))
(ebib-log 'warning "Line %d: No entry key; generating temporary key." (line-number-at-pos)))
(if (ebib-db-get-entry entry-key db 'noerror)
(ebib-log 'warning "Line %d: Entry `%s' duplicated. Skipping." (line-number-at-pos) entry-key)
(ebib-store-entry entry-key (list (cons '=type= entry-type)) db timestamp))
(cl-loop for field = (ebib-find-bibtex-field limit)
while field do
;; TODO We pass 'overwrite if `ebib-allow-identical-fields'
;; is nil in order to overwrite a possible timestamp. This
;; has to be handled better, though!
(ebib-db-set-field-value (car field) (cdr field) entry-key db (if ebib-allow-identical-fields
ebib-field-separator
'overwrite)
'as-is))
entry-key))) ; Return the entry key.
(defun ebib-find-bibtex-field (limit)
"Find the field after point.
Return a cons (FIELD . VALUE), or NIL if no field was found."
(skip-chars-forward "\"#%'(),={} \n\t\f" limit) ; move to the first char of the field name
(unless (>= (point) limit) ; if we haven't reached the end of the entry
(let ((beg (point)))
(if (ebib-looking-at-goto-end (concat "\\(" ebib-bibtex-identifier "\\)[ \t\n\f]*=") 1)
(let ((field-type (intern (downcase (buffer-substring-no-properties beg (point))))))
(skip-chars-forward "#%'(),=} \n\t\f" limit) ; move to the field contents
(let* ((beg (point))
(field-contents (buffer-substring-no-properties beg (ebib-find-end-of-field limit))))
(cons field-type field-contents)))
(ebib-log 'error "Error: illegal field name found at line %d. Skipping" (line-number-at-pos))))))
(defun ebib-find-end-of-field (limit)