AUCTeX Sync -- New Style Files
[packages] / xemacs-packages / auctex / style / splitidx.el
1 ;;; splitidx.el --- AUCTeX style for `splitidx.sty' (v1.2a)
2
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
4
5 ;; Author: Arash Esbati <arash@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Created: 2015-09-19
8 ;; Keywords: tex
9
10 ;; This file is part of AUCTeX.
11
12 ;; AUCTeX is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; AUCTeX is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with AUCTeX; see the file COPYING.  If not, write to the Free
24 ;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 ;; 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This file adds support for `splitidx.sty' (v1.2a) from 2013/04/09.
30 ;; `splitidx.sty' is part of TeXLive.
31
32 ;; The main index macro provided by `splitidx.sty' is
33 ;;
34 ;;     \sindex[<shortcut>]{<index-entry>}
35 ;;
36 ;; where <shortcut> identifies the target where <index-entry> is going
37 ;; into.  This style checks if RefTeX is loaded and adds "\sindex" to
38 ;; index macros known by RefTeX; this is described in RefTeX manual
39 ;; section 5.5 Defining Index Macros:
40 ;;
41 ;;     (setq reftex-index-macros '(("\\sindex[]{*}" 1 ?s "" nil t)))
42 ;;
43 ;; To make things more user friendly, this style does not hard-code
44 ;; the key `?s' and offers a customizable variable called
45 ;; `LaTeX-splitidx-sindex-reftex-quick-id-key'.  Customize this
46 ;; variable to another unique key if you have other preference.  Also
47 ;; note the integer 1 after "\\sindex[]{*}".  This tells RefTeX to
48 ;; look in first optional argument to find out which index the entry
49 ;; belongs to.  If omitted, RefTeX puts the index entry into "idx".
50
51 ;; `splitidx.sty' provides also the option `idxcommands' where a
52 ;; command with the name of the <shortcut> is defined for each
53 ;; declared index.  An example from splitidx.pdf:
54 ;;
55 ;;     \documentclass{article}
56 ;;     \usepackage[idxcommands]{splitidx}
57 ;;
58 ;;     \newindex[General Index]{idx}       % Name and shortcut of the 1st index
59 ;;     \newindex[Index of Animals]{ani}    % ... 2nd index
60 ;;     \newindex[Index of Fruits]{fru}     % ... 3rd index
61 ;;     \newindex[Index of Vegetables]{veg} % ... 4th index
62 ;;     ...
63 ;;
64 ;; Now four index commands \idx, \ani, \fru and \veg are available.
65 ;; This style adds these commands to AUCTeX and RefTeX (check function
66 ;; `LaTeX-splitidx-auto-cleanup').  The only thing missing is adding
67 ;; these commands to `reftex-index-macros' since the unique keys must
68 ;; be set by user.  To get full advantage from RefTeX, remember that
69 ;; ?i, ?I, and ?g are reserved and set something like this in your
70 ;; init-file:
71 ;;
72 ;;     (add-to-list 'reftex-index-macros '("\\idx" "idx" ?x "" nil t))
73 ;;     (add-to-list 'reftex-index-macros '("\\ani" "ani" ?a "" nil t))
74 ;;     (add-to-list 'reftex-index-macros '("\\fru" "fru" ?f "" nil t))
75 ;;     (add-to-list 'reftex-index-macros '("\\veg" "veg" ?v "" nil t))
76 ;;
77 ;; Or you can do this in your tex file:
78 ;;
79 ;;     %%% Local Variables:
80 ;;     %%% mode: latex
81 ;;     %%% TeX-master: t
82 ;;     %%% eval: (add-to-list 'reftex-index-macros '("\\idx" "idx" ?x "" nil t))
83 ;;     %%% eval: (add-to-list 'reftex-index-macros '("\\ani" "ani" ?a "" nil t))
84 ;;     %%% eval: (add-to-list 'reftex-index-macros '("\\fru" "fru" ?f "" nil t))
85 ;;     %%% eval: (add-to-list 'reftex-index-macros '("\\veg" "veg" ?v "" nil t))
86 ;;     %%% End:
87 ;;
88
89 ;;; Code:
90
91 ;; Needed for auto-parsing.
92 (require 'tex)
93
94 ;; Setup parsing for \newindex:
95 (TeX-auto-add-type "splitidx-newindex" "LaTeX" "splitidx-newindices")
96
97 (defvar LaTeX-splitidx-newindex-regex
98   `(,(concat "\\\\new\\(?:protected\\)?index"
99              "\\(?:\\[[^]]*\\]\\)?"
100              "{\\([^}]+\\)}")
101     1 LaTeX-auto-splitidx-newindex)
102   "Matches the argument of `\\newindex' from `splitidx.sty'.")
103
104 (defun LaTeX-splitidx-auto-prepare ()
105   "Clear `LaTeX-auto-splitidx-newindex' before parsing."
106   (setq LaTeX-auto-splitidx-newindex nil))
107
108 (defun LaTeX-splitidx-auto-cleanup ()
109   "Process parsed results for \"splitidx.sty\"."
110   (when (LaTeX-provided-package-options-member "splitidx" "idxcommands")
111     (dolist (elt (mapcar #'car (LaTeX-splitidx-newindex-list)))
112       ;; Make every element available as a command
113       (TeX-add-symbols `(,elt TeX-arg-index))
114       ;; Add new macros's to `ispell-tex-skip-alist': skip one argument
115       (TeX-ispell-skip-setcar `((,elt ispell-tex-arg-end)))
116       ;; font-locking
117       (when (and (featurep 'font-latex)
118                  (eq TeX-install-font-lock 'font-latex-setup))
119         (font-latex-add-keywords `((,elt  "{"))
120                                  'reference))
121       ;; Prepare for parsing
122       (add-to-list 'LaTeX-auto-regexp-list
123                    `(,(concat
124                        "\\\\"
125                        elt
126                        "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
127                        "}[^}{]*\\)*}[^}{]*\\)*\\)}")
128                      1 LaTeX-auto-index-entry))
129       ;; Cater for completion
130       (add-to-list 'TeX-complete-list
131                    `(,(concat "\\\\" elt "{\\([^{}\n\r]*\\)")
132                      1 LaTeX-index-entry-list "}")) )))
133
134 (add-hook 'TeX-auto-prepare-hook #'LaTeX-splitidx-auto-prepare t)
135 (add-hook 'TeX-auto-cleanup-hook #'LaTeX-splitidx-auto-cleanup t)
136 (add-hook 'TeX-update-style-hook #'TeX-auto-parse t)
137
138 (TeX-add-style-hook
139  "splitidx"
140  (lambda ()
141
142    (TeX-add-symbols
143
144     ;; 3.1 Setup
145     '("newindex"
146       [ "Index name" ]
147       (TeX-arg-eval
148        (lambda ()
149          (let ((shortcut (TeX-read-string
150                           (TeX-argument-prompt optional nil "Short cut"))))
151            (LaTeX-add-splitidx-newindices shortcut)
152            (format "%s" shortcut)))))
153
154     ;; 3.2 Marking up index entries
155     '("sindex"
156       ;; I don't use `[ TeX-arg-index-tag ]' here
157       [ TeX-arg-eval completing-read
158                      (TeX-argument-prompt optional nil "Short cut")
159                      (LaTeX-splitidx-newindex-list) ]
160       TeX-arg-index)
161
162     ;; 3.4 Customizing index entries
163     '("AtWriteToIndex"
164       (TeX-arg-eval completing-read
165                     (TeX-argument-prompt optional nil "Short cut")
166                     (LaTeX-splitidx-newindex-list))
167       t)
168
169     '("AtNextWriteToIndex"
170       (TeX-arg-eval completing-read
171                     (TeX-argument-prompt optional nil "Short cut")
172                     (LaTeX-splitidx-newindex-list))
173       t)
174
175     ;; 3.6 Preventing premature expansion of index entries
176     '("newprotectedindex"
177       [ "Index name" ]
178       (TeX-arg-eval
179        (lambda ()
180          (let ((shortcut (TeX-read-string
181                           (TeX-argument-prompt optional nil "Short cut"))))
182            (LaTeX-add-splitidx-newindices shortcut)
183            (format "%s" shortcut)))))
184
185     ;; 3.7 Including the generated indices in your document
186     '("printindex"
187       [ TeX-arg-eval completing-read
188                      (TeX-argument-prompt optional nil "Short cut")
189                      (LaTeX-splitidx-newindex-list) ]
190       [ "Index name" ])
191
192     '("printindex*" 0)
193
194     '("printsubindex"
195       [ TeX-arg-eval completing-read
196                      (TeX-argument-prompt optional nil "Short cut")
197                      (LaTeX-splitidx-newindex-list) ]
198       [ "Index name" ])
199
200     '("printsubindex*" 0)
201
202     '("setindexpreamble"
203       [ TeX-arg-eval completing-read
204                      (TeX-argument-prompt optional nil "Short cut")
205                      (LaTeX-splitidx-newindex-list) ]
206       t)
207
208     '("useindexpreamble" [ TeX-arg-macro ])
209
210     '("indexshortcut" 0)
211
212     '("extendtheindex" 4) )
213
214    ;; Add splitidx to the parser.
215    (TeX-auto-add-regexp LaTeX-splitidx-newindex-regex)
216
217    ;; Borrowed from index.el
218    (add-to-list 'LaTeX-auto-regexp-list
219                 `(,(concat "\\\\sindex\\(?:\\[[^{}]*\\]\\)?"
220                            "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
221                            "}[^}{]*\\)*}[^}{]*\\)*\\)}")
222                   1 LaTeX-auto-index-entry))
223
224    ;; Completion for \sindex entries
225    (add-to-list 'TeX-complete-list
226                 '("\\\\sindex\\(\\[[^][{}]*\\]\\)?{\\([^{}\n\r]*\\)"
227                   2 LaTeX-index-entry-list "}"))
228
229    ;; Completion for the |see macro
230    (add-to-list 'TeX-complete-list
231                 '("|see{\\([^{}\n\r]*\\)" 1 LaTeX-index-entry-list))
232
233    ;; Tell RefTeX with `reftex-add-index-macros'
234    (when (fboundp 'reftex-add-index-macros)
235      (reftex-add-index-macros
236       `(("\\sindex[]{*}" 1 ,LaTeX-splitidx-sindex-reftex-quick-id-key "" nil t))))
237
238    ;; 3.2 Marking up index entries
239    ;; \index should be an alias for \sindex
240    (when (LaTeX-provided-package-options-member "splitidx" "useindex")
241        (TeX-add-symbols
242         '("index"
243           [TeX-arg-eval completing-read
244                         (TeX-argument-prompt optional nil "Short cut")
245                         (LaTeX-splitidx-newindex-list) ]
246           (TeX-arg-index)))
247        ;; Tell RefTeX to look in the optional arg. for the index short cut
248        (when (fboundp 'reftex-add-index-macros)
249          (reftex-add-index-macros '(("\\index[]{*}" 1 ?i "" nil t))))
250        (add-to-list 'LaTeX-auto-regexp-list
251                     `(,(concat
252                         "\\\\index\\(?:\\[[^{}]*\\]\\)?"
253                         "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
254                         "}[^}{]*\\)*}[^}{]*\\)*\\)}")
255                       1 LaTeX-auto-index-entry))
256        (when (and (featurep 'font-latex)
257                   (eq TeX-install-font-lock 'font-latex-setup))
258          (font-latex-add-keywords '(("index" "[{"))
259                                   'reference)))
260
261    ;; 3.5 Automatic custom index commands
262    ;; With package option `idxcommands', one can write \foo{<entry>}
263    ;; instead of \sindex[foo]{<entry>}
264    (when (and (LaTeX-provided-package-options-member "splitidx" "idxcommands")
265               (LaTeX-splitidx-newindex-list))
266      (dolist (elt (mapcar #'car (LaTeX-splitidx-newindex-list)))
267        ;; Make every `foo' available as a command
268        (TeX-add-symbols `(,elt TeX-arg-index))
269        ;; Add new macros's to `ispell-tex-skip-alist': skip one argument
270        (TeX-ispell-skip-setcar `((,elt ispell-tex-arg-end)))
271        ;; Cater for font-locking
272        (when (and (featurep 'font-latex)
273                   (eq TeX-install-font-lock 'font-latex-setup))
274          (font-latex-add-keywords `((,elt  "{"))
275                                   'reference))
276        ;; Add defined <entry> to `LaTeX-index-entry-list'
277        (add-to-list 'LaTeX-auto-regexp-list
278                     `(,(concat
279                         "\\\\"
280                         elt
281                         "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
282                         "}[^}{]*\\)*}[^}{]*\\)*\\)}")
283                       1 LaTeX-auto-index-entry)) ))
284
285    ;; Fontification
286    (when (and (featurep 'font-latex)
287               (eq TeX-install-font-lock 'font-latex-setup))
288      (font-latex-add-keywords '(("newindex"           "[{")
289                                 ("AtWriteToIndex"     "{{")
290                                 ("AtNextWriteToIndex" "{{")
291                                 ("newprotectedindex"  "[{")
292                                 ("setindexpreamble"   "[{")
293                                 ("useindexpreamble"   "[")
294                                 ("extendtheindex"     "{{{{"))
295                               'function)
296      (font-latex-add-keywords '(("sindex"             "[{"))
297                               'reference)))
298  LaTeX-dialect)
299
300 (defvar LaTeX-splitidx-package-options
301   '("makeindex"
302     "useindex"
303     "allintoone"
304     "split"
305     "protected"
306     "idxcommands")
307   "Prompt for package options for the splitidx package.")
308
309 ;;; splitidx.el ends here