81309
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
1 ;;; bibtex-style.el --- Major mode for BibTeX Style files
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
2
|
81311
|
3 ;; Copyright (C) 2005,2007 Free Software Foundation, Inc.
|
81309
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
4
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
6 ;; Keywords:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
7
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
8 ;; This file is free software; you can redistribute it and/or modify
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
9 ;; it under the terms of the GNU General Public License as published by
|
82161
|
10 ;; the Free Software Foundation; either version 3, or (at your option)
|
81309
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
11 ;; any later version.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
12
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
13 ;; This file is distributed in the hope that it will be useful,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
16 ;; GNU General Public License for more details.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
17
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
18 ;; You should have received a copy of the GNU General Public License
|
81311
|
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
21 ;; Boston, MA 02110-1301, USA.
|
81309
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
22
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
23 ;;; Commentary:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
24
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
25 ;; Done: font-lock, imenu, outline, commenting, indentation.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
26 ;; Todo: tab-completion.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
27 ;; Bugs:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
28
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
29 ;;; Code:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
30
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
31 (defvar bibtex-style-mode-syntax-table
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
32 (let ((st (make-syntax-table)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
33 (modify-syntax-entry ?% "<" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
34 (modify-syntax-entry ?\n ">" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
35 (modify-syntax-entry ?\{ "(}" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
36 (modify-syntax-entry ?\} "){" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
37 (modify-syntax-entry ?\" "\"" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
38 (modify-syntax-entry ?. "_" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
39 (modify-syntax-entry ?' "'" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
40 (modify-syntax-entry ?# "'" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
41 (modify-syntax-entry ?* "." st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
42 (modify-syntax-entry ?= "." st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
43 (modify-syntax-entry ?$ "_" st)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
44 st))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
45
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
46
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
47 (defconst bibtex-style-commands
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
48 '("ENTRY" "EXECUTE" "FUNCTION" "INTEGERS" "ITERATE" "MACRO" "READ"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
49 "REVERSE" "SORT" "STRINGS"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
50
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
51 (defconst bibtex-style-functions
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
52 ;; From http://www.eeng.dcu.ie/local-docs/btxdocs/btxhak/btxhak/node4.html.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
53 '("<" ">" "=" "+" "-" "*" ":="
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
54 "add.period$" "call.type$" "change.case$" "chr.to.int$" "cite$"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
55 "duplicate$" "empty$" "format.name$" "if$" "int.to.chr$" "int.to.str$"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
56 "missing$" "newline$" "num.names$" "pop$" "preamble$" "purify$" "quote$"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
57 "skip$" "stack$" "substring$" "swap$" "text.length$" "text.prefix$"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
58 "top$" "type$" "warning$" "while$" "width$" "write$"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
59
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
60 (defvar bibtex-style-font-lock-keywords
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
61 `((,(regexp-opt bibtex-style-commands 'words) . font-lock-keyword-face)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
62 ("\\w+\\$" . font-lock-keyword-face)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
63 ("\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
64 (2 font-lock-function-name-face))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
65
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
66 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.bst\\'" . bibtex-style-mode))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
67
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
68 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
69 (define-derived-mode bibtex-style-mode nil "BibStyle"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
70 "Major mode for editing BibTeX style files."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
71 (set (make-local-variable 'comment-start) "%")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
72 (set (make-local-variable 'outline-regexp) "^[a-z]")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
73 (set (make-local-variable 'imenu-generic-expression)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
74 '((nil "\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}" 2)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
75 (set (make-local-variable 'indent-line-function) 'bibtex-style-indent-line)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
76 (set (make-local-variable 'parse-sexp-ignore-comments) t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
77 (setq font-lock-defaults
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
78 '(bibtex-style-font-lock-keywords nil t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
79 ((?. . "w")))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
80
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
81 (defun bibtex-style-indent-line ()
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
82 "Indent current line of BibTeX Style code."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
83 (interactive)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
84 (let* ((savep (point))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
85 (indent (condition-case nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
86 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
87 (forward-line 0)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
88 (skip-chars-forward " \t")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
89 (if (>= (point) savep) (setq savep nil))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
90 (max (bibtex-style-calculate-indentation) 0))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
91 (error 0))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
92 (if savep
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
93 (save-excursion (indent-line-to indent))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
94 (indent-line-to indent))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
95
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
96 (defcustom bibtex-style-indent-basic 2
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
97 "Basic amount of indentation to use in BibTeX Style mode."
|
84578
|
98 :type 'integer
|
|
99 :group 'bibtex)
|
81309
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
100
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
101 (defun bibtex-style-calculate-indentation (&optional virt)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
102 (or
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
103 ;; Stick the first line at column 0.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
104 (and (= (point-min) (line-beginning-position)) 0)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
105 ;; Commands start at column 0.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
106 (and (looking-at (regexp-opt bibtex-style-commands 'words)) 0)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
107 ;; Trust the current indentation, if such info is applicable.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
108 (and virt (save-excursion (skip-chars-backward " \t{") (bolp))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
109 (current-column))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
110 ;; Put leading close-paren where the matching open brace would be.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
111 (and (looking-at "}")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
112 (condition-case nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
113 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
114 (up-list -1)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
115 (bibtex-style-calculate-indentation 'virt))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
116 (scan-error nil)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
117 ;; Align leading "if$" with previous command.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
118 (and (looking-at "if\\$")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
119 (condition-case nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
120 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
121 (backward-sexp 3)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
122 (bibtex-style-calculate-indentation 'virt))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
123 (scan-error
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
124 ;; There is no command before the "if$".
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
125 (condition-case nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
126 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
127 (up-list -1)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
128 (+ bibtex-style-indent-basic
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
129 (bibtex-style-calculate-indentation 'virt)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
130 (scan-error nil)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
131 ;; Right after an opening brace.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
132 (condition-case err (save-excursion (backward-sexp 1) nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
133 (scan-error (goto-char (nth 2 err))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
134 (+ bibtex-style-indent-basic
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
135 (bibtex-style-calculate-indentation 'virt))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
136 ;; Default, align with previous command.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
137 (let ((fai ;; First arm of an "if$".
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
138 (condition-case nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
139 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
140 (forward-sexp 2)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
141 (forward-comment (point-max))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
142 (looking-at "if\\$"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
143 (scan-error nil))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
144 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
145 (condition-case err
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
146 (while (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
147 (backward-sexp 1)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
148 (save-excursion (skip-chars-backward " \t{") (not (bolp)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
149 (scan-error nil))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
150 (+ (current-column)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
151 (if (or fai (looking-at "ENTRY")) bibtex-style-indent-basic 0))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
152
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
153
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
154 (provide 'bibtex-style)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
155 ;; arch-tag: b20ad41a-fd36-466e-8fd2-cc6137f9c55c
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
156 ;;; bibtex-style.el ends here
|