summaryrefslogtreecommitdiff
path: root/support/emacs-modes/swiftex/hilit-swiftex.el
blob: cc0ae5979e2bfc654ad1dcfaa3e86d2af06a2b0b (plain)
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
;;; hilit-swiftex.el -- enhancements for highlighting swiftex and doctex
;;;                      buffers using hilit19.el or hl319.el.
;;;
;;; This file was based on hilit-LaTeX.el v1.05 by Peter Galbraith
;;; (rhogee@bathybius.meteo.mcgill.ca).

;;; to do:  we need to reset the patterns explicitly whenever we
;;;         change modes, since we need to eval variables; or can we use
;;;         functions?

;;; need to distinguish doctex from swiftex.

(require 'hilit19)
(provide 'hilit-swiftex)

(hilit-set-mode-patterns
 '(doctex-mode)
 '(
   ;; outline heading
   ("^\\^\\^A\\*+.*$" nil comment)
   ;; comment
   ("^\\^\\^A.*$" nil comment)
   ;; label
   ("\\\\item\\(\\[[^]]*\\]\\)?" nil label)
   ;; formula
   ("\\\\("  "\\\\)" formula)           ; \( \)
   ("[^\\\\]\\\\\\[" "\\\\\\]" formula) ; \[ \] but not \\[len]
   ("[^$\\]\\($\\($[^$]*\\$\\|[^$]*\\)\\$\\)" 1 formula) ; '$...$' or '$$...$$'
   ;;   ^^ but not \$102.54 is expensive for a car wash.
   ;; crossref
   ("|[^|\n]*|" nil crossref)
   (hilit-bracket-region 
   ;       (\\(no\\)?cite  -> changed to any cite commands that users make up.
    "\\\\\\([^ ]*cite\\|\\(page\\)?ref\\|eqref\\|label\\|index\\|glossary\\){"
    crossref)
   ;; bold
   (hilit-bracket-wysiwyg "{\\\\bfseries" bold)
   (hilit-bracket-region "\\\\textbf{" bold)
   ;; italic
   (hilit-bracket-wysiwyg "{\\\\\\(em\\|itshape\\|slshape\\)" italic)
   (hilit-bracket-region "\\\\emph{" italic)
   (hilit-bracket-region "\\\\text\\(it\\|sl\\){" italic)
   ;; include
   (hilit-bracket-region  "\\\\\\(include\\*?\\|input\\|bibliography\\){" include)
   ;; defun
   ("\\\\ReserveCS{?\\\\[a-zA-Z-@]+" defun)
   ;; FIX, many things don't necessarily have {'s after them.
   ;; FIX: global and long;; see doctex
   (hilit-bracket-region "\\\\new\\(let\\|tokens\\|length\\|theorem\\|counter\\){" defun)
   ;; FIX want to have matching braces...
   (hilit-bracket-region "\\\\NewNameDef\\*?{" defun)
;; causes error!
   (hilit-bracket-region "\\(\\\\long\\|\\\\global\\)+\\\\def[^}]*{" defun)
   ;; keyword
   (hilit-bracket-region "\\\\\\(v\\|h\\)space\\(\*\\)?{" keyword)
   (hilit-bracket-region "\\\\text\\(tt\\|sf\\|rm\\){" keyword)
   (hilit-bracket-region "\\\\footnote\\(mark\\|text\\)?{" keyword)
   (hilit-bracket-region 
    "\\\\\\(sub\\)*\\(paragraph\\|section\\)\\(\*\\|\\[.*\\]\\)?{" keyword)
   (hilit-bracket-region 
    "\\\\\\(chapter\\|part\\)\\(\*\\|\\[.*\\]\\)?{" keyword)
   ("\\\\[a-z]+box" nil keyword)
   ;; define
   ("\\\\\\(setlength\\|settowidth\\|addtolength\\|setcounter\\|addtocounter\\)" nil define)
   (hilit-bracket-region "\\\\\\(title\\|author\\|date\\|thanks\\){" define)
   ;; decl
   ("\\\\\\(scshape\\|bfseries\\|em\\|itshape\\|rmfamily\\|sffamily\\|slshape\\|ttfamily\\)\\b" nil decl)
   ("``" "''" string)
   ("\\\\\\(raggedright\\|makeindex\\|makeglossary\\|maketitle\\)\\b" nil
    decl)
   (hilit-bracket-region 
    "\\\\\\(pagestyle\\|thispagestyle\\|pagenumbering\\|numberwithin\\){" decl)
   (hilit-bracket-region "\\\\documentclass\\(\\[.*\\]\\)?{" decl)
   (hilit-bracket-region "\\\\\\(begin\\|end\\|nofiles\\|includeonly\\){" 
                         decl)
   ("\\\\\\(normalsize\\|small\\|footnotesize\\|scriptsize\\|tiny\\|large\\|Large\\|LARGE\\|huge\\|Huge\\)\\b" nil decl)
   ("\\\\\\(smaller\\|larger\\)\\(\\[[^]]*\\]\\)?" nil decl)
   (hilit-bracket-region "\\\\relsize{"  decl)
   ("\\\\\\(appendix\\|tableofcontents\\|listoffigures\\|listoftables\\)\\b"
    nil decl)
   ;; label
   (hilit-bracket-wysiwyg "{\\\\scshape" label)
   (hilit-bracket-region "\\\\caption\\(\\[[^]]*\\]\\)?{" label)
))

;; avoid message while byte-compiling
(eval-when-compile (defvar hilit-on-the-fly-in-use))
(hilit-set-mode-patterns
 '(swiftex-mode)
 '(
   ;; outline heading
   ("^%\\*+.*$" nil comment)
   ("\\(^\\|[^\\]\\)\\(%.*\\)$" 2 comment) ; comments
   ;; label
   ("\\\\item\\(\\[[^]]*\\]\\)?" nil label)
   ;; formula
   ("\\\\("  "\\\\)" formula)           ; \( \)
   ("[^\\\\]\\\\\\[" "\\\\\\]" formula) ; \[ \] but not \\[len]
   ("[^$\\]\\($\\($[^$]*\\$\\|[^$]*\\)\\$\\)" 1 formula) ; '$...$' or '$$...$$'
   ;;   ^^ but not \$102.54 is expensive for a car wash.
   ;; crossref
   ("|[^|\n]*|" nil crossref)
   (hilit-bracket-region 
   ;       (\\(no\\)?cite  -> changed to any cite commands that users make up.
    "\\\\\\([^ ]*cite\\|\\(page\\)?ref\\|eqref\\|label\\|index\\|glossary\\){"
    crossref)
   ;; bold
   (hilit-bracket-wysiwyg "{\\\\bfseries" bold)
   (hilit-bracket-region "\\\\textbf{" bold)
   ;; italic
   (hilit-bracket-wysiwyg "{\\\\\\(em\\|itshape\\|slshape\\)" italic)
   (hilit-bracket-region "\\\\emph{" italic)
   (hilit-bracket-region "\\\\text\\(it\\|sl\\){" italic)
   ;; include
   (hilit-bracket-region  "\\\\\\(include\\*?\\|input\\|bibliography\\){" include)
   ;; defun
   ("\\\\ReserveCS{?\\\\[a-zA-Z-@]+" defun)
   ;; FIX, many things don't necessarily have {'s after them.
   ;; FIX: global and long ; ak, all messed up.
   (hilit-bracket-region 
    "\\\\\\(re\\)?new\\(environment\\|command\\|let\\)\\\\[.\n]*{" defun)
   (hilit-bracket-region "\\\\new\\(let\\|tokens\\|length\\|theorem\\|counter\\){" defun)
   ;; FIX want to have matching braces...
   (hilit-bracket-region "\\\\NewNameDef\\*?{" defun)
;; changed; see \\long in doctex...
   ;; keyword
   (hilit-bracket-region "\\\\\\(v\\|h\\)space\\(\*\\)?{" keyword)
   (hilit-bracket-region "\\\\text\\(tt\\|sf\\|rm\\){" keyword)
   (hilit-bracket-region "\\\\footnote\\(mark\\|text\\)?{" keyword)
   (hilit-bracket-region 
    "\\\\\\(sub\\)*\\(paragraph\\|section\\)\\(\*\\|\\[.*\\]\\)?{" keyword)
   (hilit-bracket-region 
    "\\\\\\(chapter\\|part\\)\\(\*\\|\\[.*\\]\\)?{" keyword)
   ("\\\\[a-z]+box" nil keyword)
   ;; define
   ("\\\\\\(setlength\\|settowidth\\|addtolength\\|setcounter\\|addtocounter\\)" nil define)
   (hilit-bracket-region "\\\\\\(title\\|author\\|date\\|thanks\\){" define)
   ;; decl
   ("\\\\\\(scshape\\|bfseries\\|em\\|itshape\\|rmfamily\\|sffamily\\|slshape\\|ttfamily\\)\\b" nil decl)
   ("``" "''" string)
   ("\\\\\\(raggedright\\|makeindex\\|makeglossary\\|maketitle\\)\\b" nil
    decl)
   (hilit-bracket-region 
    "\\\\\\(pagestyle\\|thispagestyle\\|pagenumbering\\|numberwithin\\){" decl)
   (hilit-bracket-region "\\\\documentclass\\(\\[.*\\]\\)?{" decl)
   (hilit-bracket-region "\\\\\\(begin\\|end\\|nofiles\\|includeonly\\){" 
                         decl)
   ("\\\\\\(normalsize\\|small\\|footnotesize\\|scriptsize\\|tiny\\|large\\|Large\\|LARGE\\|huge\\|Huge\\)\\b" nil decl)
   ("\\\\\\(smaller\\|larger\\)\\(\\[[^]]*\\]\\)?" nil decl)
   (hilit-bracket-region "\\\\relsize{"  decl)
   ("\\\\\\(appendix\\|tableofcontents\\|listoffigures\\|listoftables\\)\\b"
    nil decl)
   ;; label
   (hilit-bracket-wysiwyg "{\\\\scshape" label)
   (hilit-bracket-region "\\\\caption\\(\\[[^]]*\\]\\)?{" label)
))

;; define decl label keyword bold italic comment string include

;; from the hilit-src
    ;; and anotherone for LaTeX
;;    (crossref	  DarkGoldenrod	    Goldenrod	       underline)
;;    (formula	  Goldenrod	    DarkGoldenrod      underline)
;; 

;   ;; comments
;   ("[^\\]%.*$" nil comment)

;   ;; the following two match \foo[xx]{xx} or \foo*{xx} or \foo{xx}
;   ("\\\\\\(sub\\)*\\(paragraph\\|section\\)\\(\*\\|\\[.*\\]\\)?{" "}"
;    keyword)
;   ("\\\\\\(chapter\\|part\\)\\(\*\\|\\[.*\\]\\)?{" "}" keyword)
;   ("\\\\footnote\\(mark\\|text\\)?{" "}" keyword)
;   ("\\\\[a-z]+box" nil keyword)
;   ("\\\\\\(v\\|h\\)space\\(\*\\)?{" "}" keyword)

;   ;; (re-)define new commands/environments/counters
;   ("\\\\\\(re\\)?new\\(environment\\|command\\){" "}" defun)
;   ("\\\\new\\(length\\|theorem\\|counter\\){" "}" defun)

;   ;; various declarations/definitions
;   ("\\\\\\(setlength\\|settowidth\\|addtolength\\|setcounter\\|addtocounter\\)" nil define)
;   ("\\\\\\(title\\|author\\|date\\|thanks\\){" "}" define)

;   ("\\\\documentstyle\\(\\[.*\\]\\)?{" "}" decl)
;   ("\\\\\\(begin\\|end\\|nofiles\\|includeonly\\){" "}" decl)
;   ("\\\\\\(raggedright\\|makeindex\\|makeglossary\\|maketitle\\)\\b" nil
;    decl)
;   ("\\\\\\(pagestyle\\|thispagestyle\\|pagenumbering\\){" "}" decl)
;   ("\\\\\\(normalsize\\|small\\|footnotesize\\|scriptsize\\|tiny\\|large\\|Large\\|LARGE\\|huge\\|Huge\\)\\b" nil decl)
;   ("\\\\\\(appendix\\|tableofcontents\\|listoffigures\\|listoftables\\)\\b"
;    nil decl)
;   ("\\\\\\(bf\\|em\\|it\\|rm\\|sf\\|sl\\|ss\\|tt\\)\\b" nil decl)

;   ;; label-like things
;   ("\\\\item\\(\\[[^]]*\\]\\)?" nil label)
;   ("\\\\caption\\(\\[[^]]*\\]\\)?{" "}" label)

;   ;; formulas
;   ("[^\\]\\\\("  "\\\\)" formula)                   ; \( \)
;   ("[^\\]\\\\\\[" "\\\\\\]" formula)                ; \[ \]
;   ("[^\\$]\\($\\($[^$]*\\$\\|[^$]*\\)\\$\\)" 1 formula) ; '$...$' or '$$...$$'
   
;   ;; things that bring in external files
;   ("\\\\\\(include\\|input\\|bibliography\\){" "}" include)

;   ;; "wysiwyg" emphasis -- these don't work with nested expressions
;   ;; ("{\\\\\\(em\\|it\\|sl\\)" "}" italic)
;   ;; ("{\\\\bf" "}" bold)

;   ("``" "''" string)

;   ;; things that do some sort of cross-reference
;   ("\\\\\\(\\(no\\)?cite\\|\\(page\\)?ref\\|label\\|index\\|glossary\\){" "}" crossref)
;   ))

 (defun hilit-bracket-region (open)
  "Find region within curly brackets for hilit pattern.
ARG is pattern for beginning of pattern and ends with {.
It cannot be white space.  
Patterns ends simply at the matching closing bracket.

Used for patterns like:
 \\chapter{stuff with matching {} ending with }"
  (if (re-search-forward open nil t)
      (let ((here (point))
            (the-start (match-beginning 0)))
        (backward-char 1)               ; point is on bracket
        (if hilit-on-the-fly-in-use
            ;; if hl319's on-the-fly hilighting is in use then we can't use 
            ;; forward-list because it uses the built-in scan-lists and will 
            ;; return an error if there is no matching bracket yet.
            (re-search-forward "}" 
                               (progn (save-excursion (end-of-line)(point))) 
                               1)
          (forward-list 1))
        (cons the-start (point)))))

(defun hilit-inside-bracket-region (open)
  "Find region within curly brackets for hilit pattern.
ARG is pattern for beginning of pattern and ends with {.
It cannot be white space.  
Patterns ends simply at the matching closing bracket.

Used for patterns like:
 \\textbf{only stuff within bracket is highlited}"
  (if (re-search-forward open nil t)
      (let ((the-start (point)))
        (backward-char 1)               ; point is on bracket
        (if hilit-on-the-fly-in-use
            (re-search-forward "}" 
                               (progn (save-excursion (end-of-line)(point))) 1)
          (forward-list 1))
        (backward-char 1)
        (cons the-start (point)))))

(defun hilit-inside-environment (open)
  "Find region within curly brackets for hilit pattern.
ARG is pattern for \\begin{something}."
  (if (re-search-forward open nil t)
      (let ((the-start (progn (forward-line 1)(point))) ;Start on next line
            (the-end))
        (re-search-backward "\\\\begin{\\([^}]+\\)}" nil t)
        (setq the-end (buffer-substring (match-beginning 1)(match-end 1)))
        (if hilit-on-the-fly-in-use     ;Search no more than 10 lines away
            (if (search-forward (concat "\\end{" the-end "}") 
                                (save-excursion (forward-line 10)(point)) t)
                (cons the-start (progn (forward-line -1)(point)))
              (cons the-start the-start))
          (if (search-forward (concat "\\end{" the-end "}") nil t)
              (cons the-start (match-beginning 0))
            (end-of-line)               ;Move off the \begin{}
            (cons the-start the-start))))))

(defun hilit-bracket-wysiwyg (open)
  "Find region within curly brackets for hilit pattern.
ARG is pattern for beginning of pattern and starts with {.
Patterns ends simply at the matching closing bracket.

Used for patterns like:
 {\em stuff with matching {} ending with }"
  (if (re-search-forward open nil t)
      (let ((the-start (match-beginning 0)))
        (goto-char the-start)
        (if hilit-on-the-fly-in-use
            (re-search-forward "}" 
                               (progn (save-excursion (end-of-line)(point))) 1)
          (forward-list 1))
        (cons the-start (point)))))


(defvar hilit-AmSLaTeX-commands nil
  "*Set to t if you want to hightlight AmSLaTeX commands.")

(defvar hilit-multilingual-strings nil
  "*Set to t if you want to hightlight multilingual quoted strings.
Highlights:  \"`german\"', \"< french \">, << french >> and + 8-bit french ;.")

(defvar hilit-LaTeX-commands t
  "*Set to nil if you don't want to highlight unknown LaTeX commands")

(defvar hilit-on-the-fly-in-use nil
  "Used by hilit-LaTeX to determine if on-the-fly hilighting is in use")

;; I need to modify hl319.el's on-the-fly highlighter so that it
;; tells me on-the-fly highlighting is in use.
(defun hilit-rehighlight-changed-lines (st en len)
  "Quietly rehighlight just this line.
Useful as an after change hook in VM/gnus summary buffers and dired buffers."
  (save-match-data
    (let ((hilit-on-the-fly-in-use t))
      ;; (> (- en st) hilit-max-change-rehighlight)
      (hilit-rehighlight-region st en 'quietly))))