2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;; DESCRIPTION : alpha style for BibTeX files
6 ;; COPYRIGHT : (C) 2010 David MICHEL
8 ;; This software falls under the GNU general public license version 3 or later.
9 ;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
10 ;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 (texmacs-module (bibtex alpha)
15 (:use (bibtex bib-utils) (bibtex plain)))
17 (bib-define-style "alpha" "plain")
19 (define (format-label-year x)
20 (let* ((y (bib-field x "year"))
21 (l (string-length y)))
22 (if (<= l 2) y (substring y (- l 2) l))))
24 (define (format-label-names a)
28 (with von (bib-purify (bib-abbreviate
29 (list-ref (list-ref a 1) 2) "" ""))
31 (bib-prefix (list-ref (list-ref a 1) 3) 3)
32 (string-append von (bib-prefix
33 (list-ref (list-ref a 1) 3) 1)))))
39 (with von (bib-purify (bib-abbreviate
40 (list-ref (list-ref a i) 2)
42 (set! lab (string-append
44 (list-ref (list-ref a i) 3) 1)))))
46 (if (> n 5) (string-append pre "+") pre)))
48 (define (format-book-inbook-label n x)
49 (with key (list-ref x 2)
50 (if (bib-empty? x "author")
51 (if (bib-empty? x "editor")
55 (format-label-names (bib-field x "editor")))
56 (format-label-names (bib-field x "author")))))
58 (define (format-proceedings-misc-label ae n x)
59 (with key (list-ref x 2)
64 (format-label-names (bib-field x ae)))))
66 (define (format-label-prefix n x)
67 (let* ((doctype (list-ref x 1))
69 ((or (equal? doctype "book") (equal? doctype "inbook"))
70 (format-book-inbook-label n x))
71 ((equal? doctype "proceedings")
72 (format-proceedings-misc-label "editor" n x))
73 (else (format-proceedings-misc-label "author" n x)))))
74 (string-append pre (format-label-year x))))
76 (define bib-label-table `())
77 (define bib-key-table `())
79 (tm-define (bib-preprocessing t)
81 (set! bib-label-table (make-hash-table 100))
82 (set! bib-key-table (make-hash-table 100))
83 (do ((entry t (cdr entry)) (n 1 (+ n 1)))
85 (let* ((label (format-label-prefix 0 (car entry)))
86 (num (hash-ref bib-label-table label)))
87 (hash-set! bib-key-table (list-ref (car entry) 2) label)
89 (hash-set! bib-label-table label
90 (if (equal? num `()) `(1 2) `(,@num ,(+ 1 (length num)))))
91 (hash-set! bib-label-table label `())))))
93 (define (format-label n x)
94 (let* ((pre (hash-ref bib-key-table (list-ref x 2)))
95 (num (hash-ref bib-label-table pre)))
98 (hash-set! bib-label-table pre (cdr num))
99 (string-append pre (string (integer->char (+ 96 n))))))))
101 (tm-define (format-bibitem n x)
103 `(bibitem* ,(format-label n x)))
105 (define (invert-label l)
106 (with invert (lambda (c)
108 ((char-upper-case? c) (char-downcase c))
109 ((char-lower-case? c) (char-upcase c))
111 (string-map invert l)))
113 (tm-define (bib-sort-key x)
115 (let ((label (hash-ref bib-key-table (list-ref x 2)))
116 (lplain (bib-with-style "plain" bib-sort-key x)))
117 (string-append (invert-label label) " " lplain)))