summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/splitidx.el
blob: edf52c28471dc7e0c35e30d2fa1f0a6f73ce8260 (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
315
;;; splitidx.el --- AUCTeX style for `splitidx.sty' (v1.2a)  -*- lexical-binding: t; -*-

;; Copyright (C) 2016, 2018, 2020 Free Software Foundation, Inc.

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2015-09-19
;; Keywords: tex

;; This file is part of AUCTeX.

;; AUCTeX is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; AUCTeX is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with AUCTeX; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;; This file adds support for `splitidx.sty' (v1.2a) from 2013/04/09.
;; `splitidx.sty' is part of TeXLive.

;; The main index macro provided by `splitidx.sty' is
;;
;;     \sindex[<shortcut>]{<index-entry>}
;;
;; where <shortcut> identifies the target where <index-entry> is going
;; into.  This style checks if RefTeX is loaded and adds "\sindex" to
;; index macros known by RefTeX; this is described in RefTeX manual
;; section 5.5 Defining Index Macros:
;;
;;     (setq reftex-index-macros '(("\\sindex[]{*}" 1 ?s "" nil t)))
;;
;; To make things more user friendly, this style does not hard-code
;; the key `?s' and offers a customizable variable called
;; `LaTeX-splitidx-sindex-reftex-quick-id-key'.  Customize this
;; variable to another unique key if you have other preference.  Also
;; note the integer 1 after "\\sindex[]{*}".  This tells RefTeX to
;; look in first optional argument to find out which index the entry
;; belongs to.  If omitted, RefTeX puts the index entry into "idx".

;; `splitidx.sty' provides also the option `idxcommands' where a
;; command with the name of the <shortcut> is defined for each
;; declared index.  An example from splitidx.pdf:
;;
;;     \documentclass{article}
;;     \usepackage[idxcommands]{splitidx}
;;
;;     \newindex[General Index]{idx}       % Name and shortcut of the 1st index
;;     \newindex[Index of Animals]{ani}    % ... 2nd index
;;     \newindex[Index of Fruits]{fru}     % ... 3rd index
;;     \newindex[Index of Vegetables]{veg} % ... 4th index
;;     ...
;;
;; Now four index commands \idx, \ani, \fru and \veg are available.
;; This style adds these commands to AUCTeX and RefTeX (check function
;; `LaTeX-splitidx-auto-cleanup').  The only thing missing is adding
;; these commands to `reftex-index-macros' since the unique keys must
;; be set by user.  To get full advantage from RefTeX, remember that
;; ?i, ?I, and ?g are reserved and set something like this in your
;; init-file:
;;
;;     (add-to-list 'reftex-index-macros '("\\idx" "idx" ?x "" nil t))
;;     (add-to-list 'reftex-index-macros '("\\ani" "ani" ?a "" nil t))
;;     (add-to-list 'reftex-index-macros '("\\fru" "fru" ?f "" nil t))
;;     (add-to-list 'reftex-index-macros '("\\veg" "veg" ?v "" nil t))
;;
;; Or you can do this in your tex file:
;;
;;     %%% Local Variables:
;;     %%% mode: latex
;;     %%% TeX-master: t
;;     %%% eval: (add-to-list 'reftex-index-macros '("\\idx" "idx" ?x "" nil t))
;;     %%% eval: (add-to-list 'reftex-index-macros '("\\ani" "ani" ?a "" nil t))
;;     %%% eval: (add-to-list 'reftex-index-macros '("\\fru" "fru" ?f "" nil t))
;;     %%% eval: (add-to-list 'reftex-index-macros '("\\veg" "veg" ?v "" nil t))
;;     %%% End:
;;

;;; Code:

;; Needed for auto-parsing:
(require 'tex)
(require 'latex)

;; Silence the compiler:
(declare-function font-latex-add-keywords
                  "font-latex"
                  (keywords class))

;; Setup parsing for \newindex:
(TeX-auto-add-type "splitidx-newindex" "LaTeX" "splitidx-newindices")

(defvar LaTeX-splitidx-newindex-regex
  `(,(concat "\\\\new\\(?:protected\\)?index"
             "\\(?:\\[[^]]*\\]\\)?"
             "{\\([^}]+\\)}")
    1 LaTeX-auto-splitidx-newindex)
  "Matches the argument of `\\newindex' from `splitidx.sty'.")

(defun LaTeX-splitidx-auto-prepare ()
  "Clear `LaTeX-auto-splitidx-newindex' before parsing."
  (setq LaTeX-auto-splitidx-newindex nil))

(defun LaTeX-splitidx-auto-cleanup ()
  "Process parsed results for \"splitidx.sty\"."
  (when (LaTeX-provided-package-options-member "splitidx" "idxcommands")
    (dolist (elt (mapcar #'car (LaTeX-splitidx-newindex-list)))
      ;; Make every element available as a command
      (TeX-add-symbols `(,elt TeX-arg-index))
      ;; Add new macros's to `ispell-tex-skip-alist': skip one argument
      (TeX-ispell-skip-setcar `((,elt ispell-tex-arg-end)))
      ;; font-locking
      (when (and (featurep 'font-latex)
                 (eq TeX-install-font-lock 'font-latex-setup))
        (font-latex-add-keywords `((,elt  "{"))
                                 'reference))
      ;; Prepare for parsing
      (add-to-list 'LaTeX-auto-regexp-list
                   `(,(concat
                       "\\\\"
                       elt
                       "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
                       "}[^}{]*\\)*}[^}{]*\\)*\\)}")
                     1 LaTeX-auto-index-entry))
      ;; Cater for completion
      (add-to-list 'TeX-complete-list
                   `(,(concat "\\\\" elt "{\\([^{}\n\r]*\\)")
                     1 LaTeX-index-entry-list "}")) )))

(add-hook 'TeX-auto-prepare-hook #'LaTeX-splitidx-auto-prepare t)
(add-hook 'TeX-auto-cleanup-hook #'LaTeX-splitidx-auto-cleanup t)
(add-hook 'TeX-update-style-hook #'TeX-auto-parse t)

(TeX-add-style-hook
 "splitidx"
 (lambda ()

   (TeX-add-symbols

    ;; 3.1 Setup
    '("newindex"
      [ "Index name" ]
      (TeX-arg-eval
       (lambda ()
         (let ((shortcut (TeX-read-string
                          (TeX-argument-prompt nil nil "Short cut"))))
           (LaTeX-add-splitidx-newindices shortcut)
           (format "%s" shortcut)))))

    ;; 3.2 Marking up index entries
    '("sindex"
      ;; I don't use `[ TeX-arg-index-tag ]' here
      [ TeX-arg-eval completing-read
        (TeX-argument-prompt t nil "Short cut")
        (LaTeX-splitidx-newindex-list) ]
      TeX-arg-index)

    ;; 3.4 Customizing index entries
    '("AtWriteToIndex"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Short cut")
                    (LaTeX-splitidx-newindex-list))
      t)

    '("AtNextWriteToIndex"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Short cut")
                    (LaTeX-splitidx-newindex-list))
      t)

    ;; 3.6 Preventing premature expansion of index entries
    '("newprotectedindex"
      [ "Index name" ]
      (TeX-arg-eval
       (lambda ()
         (let ((shortcut (TeX-read-string
                          (TeX-argument-prompt nil nil "Short cut"))))
           (LaTeX-add-splitidx-newindices shortcut)
           (format "%s" shortcut)))))

    ;; 3.7 Including the generated indices in your document
    '("printindex"
      [ TeX-arg-eval completing-read
        (TeX-argument-prompt t nil "Short cut")
        (LaTeX-splitidx-newindex-list) ]
      [ "Index name" ])

    '("printindex*" 0)

    '("printsubindex"
      [ TeX-arg-eval completing-read
        (TeX-argument-prompt t nil "Short cut")
        (LaTeX-splitidx-newindex-list) ]
      [ "Index name" ])

    '("printsubindex*" 0)

    '("setindexpreamble"
      [ TeX-arg-eval completing-read
        (TeX-argument-prompt t nil "Short cut")
        (LaTeX-splitidx-newindex-list) ]
      t)

    '("useindexpreamble" [ TeX-arg-macro ])

    '("indexshortcut" 0)

    '("extendtheindex" 4) )

   ;; Add splitidx to the parser.
   (TeX-auto-add-regexp LaTeX-splitidx-newindex-regex)

   ;; Borrowed from index.el
   (add-to-list 'LaTeX-auto-regexp-list
                `(,(concat "\\\\sindex\\(?:\\[[^{}]*\\]\\)?"
                           "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
                           "}[^}{]*\\)*}[^}{]*\\)*\\)}")
                  1 LaTeX-auto-index-entry))

   ;; Completion for \sindex entries
   (add-to-list 'TeX-complete-list
                '("\\\\sindex\\(\\[[^][{}]*\\]\\)?{\\([^{}\n\r]*\\)"
                  2 LaTeX-index-entry-list "}"))

   ;; Completion for the |see macro
   (add-to-list 'TeX-complete-list
                '("|see{\\([^{}\n\r]*\\)" 1 LaTeX-index-entry-list))

   ;; Tell RefTeX with `reftex-add-index-macros'
   (when (fboundp 'reftex-add-index-macros)
     (reftex-add-index-macros
      `(("\\sindex[]{*}" 1 ,LaTeX-splitidx-sindex-reftex-quick-id-key "" nil t))))

   ;; 3.2 Marking up index entries
   ;; \index should be an alias for \sindex
   (when (LaTeX-provided-package-options-member "splitidx" "useindex")
     (TeX-add-symbols
      '("index"
        [TeX-arg-eval completing-read
                      (TeX-argument-prompt t nil "Short cut")
                      (LaTeX-splitidx-newindex-list) ]
        (TeX-arg-index)))
     ;; Tell RefTeX to look in the optional arg. for the index short cut
     (when (fboundp 'reftex-add-index-macros)
       (reftex-add-index-macros '(("\\index[]{*}" 1 ?i "" nil t))))
     (add-to-list 'LaTeX-auto-regexp-list
                  `(,(concat
                      "\\\\index\\(?:\\[[^{}]*\\]\\)?"
                      "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
                      "}[^}{]*\\)*}[^}{]*\\)*\\)}")
                    1 LaTeX-auto-index-entry))
     (when (and (featurep 'font-latex)
                (eq TeX-install-font-lock 'font-latex-setup))
       (font-latex-add-keywords '(("index" "[{"))
                                'reference)))

   ;; 3.5 Automatic custom index commands
   ;; With package option `idxcommands', one can write \foo{<entry>}
   ;; instead of \sindex[foo]{<entry>}
   (when (and (LaTeX-provided-package-options-member "splitidx" "idxcommands")
              (LaTeX-splitidx-newindex-list))
     (dolist (elt (mapcar #'car (LaTeX-splitidx-newindex-list)))
       ;; Make every `foo' available as a command
       (TeX-add-symbols `(,elt TeX-arg-index))
       ;; Add new macros's to `ispell-tex-skip-alist': skip one argument
       (TeX-ispell-skip-setcar `((,elt ispell-tex-arg-end)))
       ;; Cater for font-locking
       (when (and (featurep 'font-latex)
                  (eq TeX-install-font-lock 'font-latex-setup))
         (font-latex-add-keywords `((,elt  "{"))
                                  'reference))
       ;; Add defined <entry> to `LaTeX-index-entry-list'
       (add-to-list 'LaTeX-auto-regexp-list
                    `(,(concat
                        "\\\\"
                        elt
                        "{\\([^}{]*\\({[^}{]*\\({[^}{]*\\({[^}{]*}[^}{]*\\)*"
                        "}[^}{]*\\)*}[^}{]*\\)*\\)}")
                      1 LaTeX-auto-index-entry)) ))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("newindex"           "[{")
                                ("AtWriteToIndex"     "{{")
                                ("AtNextWriteToIndex" "{{")
                                ("newprotectedindex"  "[{")
                                ("setindexpreamble"   "[{")
                                ("useindexpreamble"   "[")
                                ("extendtheindex"     "{{{{"))
                              'function)
     (font-latex-add-keywords '(("sindex"             "[{"))
                              'reference)))
 TeX-dialect)

(defvar LaTeX-splitidx-package-options
  '("makeindex"
    "useindex"
    "allintoone"
    "split"
    "protected"
    "idxcommands")
  "Prompt for package options for the splitidx package.")

;;; splitidx.el ends here