1 ;;; splitidx.el --- AUCTeX style for `splitidx.sty' (v1.2a)
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
5 ;; Author: Arash Esbati <arash@gnu.org>
6 ;; Maintainer: auctex-devel@gnu.org
10 ;; This file is part of AUCTeX.
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)
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.
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
29 ;; This file adds support for `splitidx.sty' (v1.2a) from 2013/04/09.
30 ;; `splitidx.sty' is part of TeXLive.
32 ;; The main index macro provided by `splitidx.sty' is
34 ;; \sindex[<shortcut>]{<index-entry>}
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:
41 ;; (setq reftex-index-macros '(("\\sindex[]{*}" 1 ?s "" nil t)))
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".
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:
55 ;; \documentclass{article}
56 ;; \usepackage[idxcommands]{splitidx}
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
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
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))
77 ;; Or you can do this in your tex file:
79 ;; %%% Local Variables:
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))
91 ;; Needed for auto-parsing.
94 ;; Setup parsing for \newindex:
95 (TeX-auto-add-type "splitidx-newindex" "LaTeX" "splitidx-newindices")
97 (defvar LaTeX-splitidx-newindex-regex
98 `(,(concat "\\\\new\\(?:protected\\)?index"
99 "\\(?:\\[[^]]*\\]\\)?"
101 1 LaTeX-auto-splitidx-newindex)
102 "Matches the argument of `\\newindex' from `splitidx.sty'.")
104 (defun LaTeX-splitidx-auto-prepare ()
105 "Clear `LaTeX-auto-splitidx-newindex' before parsing."
106 (setq LaTeX-auto-splitidx-newindex nil))
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)))
117 (when (and (featurep 'font-latex)
118 (eq TeX-install-font-lock 'font-latex-setup))
119 (font-latex-add-keywords `((,elt "{"))
121 ;; Prepare for parsing
122 (add-to-list 'LaTeX-auto-regexp-list
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 "}")) )))
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)
149 (let ((shortcut (TeX-read-string
150 (TeX-argument-prompt optional nil "Short cut"))))
151 (LaTeX-add-splitidx-newindices shortcut)
152 (format "%s" shortcut)))))
154 ;; 3.2 Marking up index entries
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) ]
162 ;; 3.4 Customizing index entries
164 (TeX-arg-eval completing-read
165 (TeX-argument-prompt optional nil "Short cut")
166 (LaTeX-splitidx-newindex-list))
169 '("AtNextWriteToIndex"
170 (TeX-arg-eval completing-read
171 (TeX-argument-prompt optional nil "Short cut")
172 (LaTeX-splitidx-newindex-list))
175 ;; 3.6 Preventing premature expansion of index entries
176 '("newprotectedindex"
180 (let ((shortcut (TeX-read-string
181 (TeX-argument-prompt optional nil "Short cut"))))
182 (LaTeX-add-splitidx-newindices shortcut)
183 (format "%s" shortcut)))))
185 ;; 3.7 Including the generated indices in your document
187 [ TeX-arg-eval completing-read
188 (TeX-argument-prompt optional nil "Short cut")
189 (LaTeX-splitidx-newindex-list) ]
195 [ TeX-arg-eval completing-read
196 (TeX-argument-prompt optional nil "Short cut")
197 (LaTeX-splitidx-newindex-list) ]
200 '("printsubindex*" 0)
203 [ TeX-arg-eval completing-read
204 (TeX-argument-prompt optional nil "Short cut")
205 (LaTeX-splitidx-newindex-list) ]
208 '("useindexpreamble" [ TeX-arg-macro ])
212 '("extendtheindex" 4) )
214 ;; Add splitidx to the parser.
215 (TeX-auto-add-regexp LaTeX-splitidx-newindex-regex)
217 ;; Borrowed from index.el
218 (add-to-list 'LaTeX-auto-regexp-list
219 `(,(concat "\\\\sindex\\(?:\\[[^{}]*\\]\\)?"
220 "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
221 "}[^}{]*\\)*}[^}{]*\\)*\\)}")
222 1 LaTeX-auto-index-entry))
224 ;; Completion for \sindex entries
225 (add-to-list 'TeX-complete-list
226 '("\\\\sindex\\(\\[[^][{}]*\\]\\)?{\\([^{}\n\r]*\\)"
227 2 LaTeX-index-entry-list "}"))
229 ;; Completion for the |see macro
230 (add-to-list 'TeX-complete-list
231 '("|see{\\([^{}\n\r]*\\)" 1 LaTeX-index-entry-list))
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))))
238 ;; 3.2 Marking up index entries
239 ;; \index should be an alias for \sindex
240 (when (LaTeX-provided-package-options-member "splitidx" "useindex")
243 [TeX-arg-eval completing-read
244 (TeX-argument-prompt optional nil "Short cut")
245 (LaTeX-splitidx-newindex-list) ]
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
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" "[{"))
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 "{"))
276 ;; Add defined <entry> to `LaTeX-index-entry-list'
277 (add-to-list 'LaTeX-auto-regexp-list
281 "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
282 "}[^}{]*\\)*}[^}{]*\\)*\\)}")
283 1 LaTeX-auto-index-entry)) ))
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" "{{{{"))
296 (font-latex-add-keywords '(("sindex" "[{"))
300 (defvar LaTeX-splitidx-package-options
307 "Prompt for package options for the splitidx package.")
309 ;;; splitidx.el ends here