summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/doc.el
blob: 63c6580c07cdcee1ad6119b423fc8c05f690374f (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
;;; doc.el --- AUCTeX style for `doc.sty'  -*- lexical-binding: t; -*-

;; Copyright (C) 2004-2022  Free Software Foundation, Inc.

;; Author: Frank Küster <frank@kuesterei.ch>
;; Maintainer: auctex-devel@gnu.org
;; 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 `doc.sty' (v3.0h) dated 2022/06/01.

;;; Code:

(require 'tex)
(require 'latex)

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

(defun LaTeX-env-doc-no-comment (environment)
  "Insert ENVIRONMENT and make sure there is no commented empty line inside."
  (LaTeX-insert-environment environment)
  (unless (TeX-active-mark)
    (when (save-excursion
            (beginning-of-line)
            (looking-at (concat "[ \t]+$\\|[ \t]*"
                                TeX-comment-start-regexp "+[ \t]*$")))
      (delete-region (line-beginning-position) (line-end-position))
      (indent-according-to-mode))))

(defun LaTeX-env-doc-commented (environment)
  "Insert ENVIRONMENT and make sure all parts are in comments.
This functions search for the begin and the end of the inserted
environment and makes sure those parts are in comments.  The same
applies also to the point inside after the insertion."
  (LaTeX-insert-environment
   environment
   (if (string= environment "macro")
       ;; For 'macro' environment, elements will start with a
       ;; backslash, so we insert them initially:
       (progn
         (let ((opt (mapconcat #'identity
                               (TeX-completing-read-multiple
                                (TeX-argument-prompt t nil "Suppress option")
                                '("noindex" "noprint"))
                               ","))
               (mac (TeX-read-string
                     (TeX-argument-prompt nil nil "Macro(s)")
                     TeX-esc)))
           (concat (when (and opt (not (string= opt "")))
                     (format "[%s]" opt))
                   (format "{%s}" mac))))
     ;; For other environments, we don't know about the elements,
     ;; so do nothing.  For 'environment', we adjust the prompt in
     ;; minibuffer.
     (let ((opt (mapconcat #'identity
                           (TeX-completing-read-multiple
                            (TeX-argument-prompt t nil "Suppress option")
                            '("noindex" "noprint"))
                           ","))
           (env (TeX-read-string
                 (TeX-argument-prompt nil nil
                                      (if (string= environment "environment")
                                          "Environment(s)"
                                        "Element(s)")))))
       (concat
        (when (and opt (not (string= opt "")))
          (format "[%s]" opt))
        (format "{%s}" env)))))
  ;; Now make sure everything is commented:
  (let ((p (point-marker))
        (active-mark (and (TeX-active-mark)
                          (not (eq (mark) (point)))))
        (func (lambda ()
                (if (TeX-in-line-comment)
                    (indent-according-to-mode)
                  (delete-horizontal-space)
                  (beginning-of-line)
                  (insert "%")
                  (indent-according-to-mode)))))
    ;; Go to the start of the env we have inserted:
    (search-backward (concat "\\begin" TeX-grop environment TeX-grcl)
                     (if active-mark nil (line-beginning-position 0))
                     t)
    ;; If the line is not commented, insert %
    (funcall func)
    (goto-char p)
    ;; Do the same for the end of the environment
    (search-forward (concat "\\end" TeX-grop environment TeX-grcl)
                    (if active-mark nil (line-end-position 2))
                    t)
    (goto-char (match-beginning 0))
    (funcall func)
    ;; Finally for where we started and clean up only when region was
    ;; not active:
    (goto-char p)
    (unless active-mark (funcall func))
    (set-marker p nil)))

(defun LaTeX-doc-after-insert-macrocode (env start end)
  "Make sure the macrocode environment is properly formatted after insertion."
  (when (TeX-member env '("macrocode" "macrocode*")
                    #'string-equal)
    (save-excursion
      (goto-char end)
      (skip-chars-backward " \t")
      (when (bolp)
        (insert "%")
        (indent-according-to-mode))
      (goto-char start)
      (skip-chars-backward " \t")
      (when (bolp)
        (insert "%")
        (indent-according-to-mode)))))

(defvar LaTeX-doc-newdocelement-key-val-options
  '(("macrolike" ("true" "false"))
    ("envlike" ("true" "false"))
    ("toplevel" ("true" "false"))
    ("notoplevel" ("true" "false"))
    ("idxtype")
    ("printtype")
    ("idxgroup")
    ("noindex" ("true" "false"))
    ("noprint" ("true" "false")))
  "Key=value options for '\\NewDocElement' macro.")

;; Setup for \NewDocElement:

(TeX-auto-add-type "doc-NewDocElement" "LaTeX")

(defvar LaTeX-doc-NewDocElement-regexp
  `(,(concat  "^[ \t%]*"
              "\\\\NewDocElement"
              "[ \t\n\r%]*"
              "\\(?:"
              (LaTeX-extract-key-value-label 'none)
              "\\)?"
              "[ \t\n\r%]*"
              "{\\([^}]+\\)}"
              "[ \t\n\r%]*"
              "{\\([^}]+\\)}")
    (1 2) LaTeX-auto-doc-NewDocElement)
  "Matches the arguments of '\\NewDocElement' from doc package.
AUCTeX parser doesn't look for text parts commented out.
Therefore, the regexp in this variable explicitly looks for a
percent sign at the beginning of a line before
'\\NewDocElement'.")

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

(defun LaTeX-doc-auto-cleanup ()
  "Process elements defined with '\\NewDocElement'."
  (when (LaTeX-doc-NewDocElement-list)
    ;; Make sure `docTeX-indent-inner-fixed' is local:
    (make-local-variable 'docTeX-indent-inner-fixed)

    ;; \NewDocElement[<options>]{<element-name>}{<env-name>} defines:
    ;; 1. \Describe<element-name>[<options>]{<element>}
    ;; 2. \begin{<env-name>}[<options>]{<element>}
    ;; 3. \PrintDescribe<element-name>{<element>}
    ;; 4. \Print<element-name>Name
    (let (macs)
      (dolist (elt (LaTeX-doc-NewDocElement-list))
        (let ((eltname (car elt))
              (envname (cadr elt)))
          (TeX-add-symbols
           ;; Cater for \Describe<eltname>[options]{<elements query>}
           `(,(concat "Describe" eltname)
             [TeX-arg-eval completing-read
                           (TeX-argument-prompt t nil "Suppress option")
                           '("noindex" "noprint")]
             "Element")

           ;; Cater for \PrintDescribe<eltname>{<elements query>}
           `(,(concat "PrintDescribe" eltname) "Element")

           ;; Cater for \Print<eltname>Name
           (concat "Print" eltname "Name"))

          ;; Add the \Describe<element-name> to macs
          (push (concat "Describe" eltname) macs)

          ;; Cater for \begin{<envname>}[options]{<elements query>}
          (LaTeX-add-environments
           `(,envname LaTeX-env-doc-commented))

          ;; Make sure we have fixed inner indent for our environments:
          (add-to-list 'docTeX-indent-inner-fixed
                       `(,(concat (regexp-quote TeX-esc)
                                  "\\(begin\\|end\\)[ \t]*"
                                  (regexp-quote TeX-grop)
                                  envname
                                  (regexp-quote TeX-grcl))
                         0 nil)
                       t)
          ;; Add fontification:
          (when (and (featurep 'font-latex)
                     (eq TeX-install-font-lock 'font-latex-setup))
            (font-latex-add-keywords `((,(concat "Describe" eltname) "[|{\\" ))
                                     'variable))))

      ;; Let \Describe<element-name> stay in their own lines:
      (LaTeX-paragraph-commands-add-locally macs))))

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

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

   ;; Add doc to the parser:
   (TeX-auto-add-regexp LaTeX-doc-NewDocElement-regexp)

   (add-hook 'LaTeX-after-insert-env-hook #'LaTeX-doc-after-insert-macrocode
             nil t)

   (LaTeX-add-environments
    ;; 2.3 General conventions
    '("macrocode"  LaTeX-env-doc-no-comment)
    '("macrocode*" LaTeX-env-doc-no-comment)

    ;; 2.5 Describing the definition of macros and environments
    '("macro"       LaTeX-env-doc-commented)
    '("environment" LaTeX-env-doc-commented)

    '("theglossary" LaTeX-env-item))

   (TeX-add-symbols
    ;; 2.1 The driver file
    '("DocInput"
      (TeX-arg-eval
       (lambda ()
         (let ((file (file-relative-name
                      (read-file-name
                       "File to input: " nil nil nil nil
                       (lambda (x)
                         (or (file-directory-p x)
                             (string-match "\\.\\(fdd\\|dtx\\)\\'" x))))
                      (TeX-master-directory))))
           (format "%s" file)))))

    '("IndexInput"
      (TeX-arg-eval
       (lambda ()
         (let ((file (file-relative-name
                      (read-file-name
                       "File to input: " nil nil nil nil
                       (lambda (x)
                         (or (file-directory-p x)
                             (string-match "\\.\\(tex\\|ltx\\|fdd\\|dtx\\)\\'" x))))
                      (TeX-master-directory))))
           (format "%s" file)))))

    ;; 2.2 Package options
    '("SetupDoc" (TeX-arg-eval mapconcat #'identity
                               (TeX-completing-read-multiple
                                (TeX-argument-prompt nil nil "Options")
                                LaTeX-doc-package-options)
                               ","))

    ;; 2.4 Describing the usage of macros and environments
    '("DescribeMacro"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Suppress option")
                    '("noindex" "noprint")]
      (TeX-arg-eval
       (lambda ()
         (let ((name (TeX-read-string
                      (TeX-argument-prompt nil nil "Macro")
                      TeX-esc)))
           (format "%s" name)))))
    '("DescribeEnv"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Suppress option")
                    '("noindex" "noprint")]
      "Environment")

    ;; 2.5 Describing the definition of macros and environments
    "MacroFont"

    ;; 2.6 Formatting names in the margin
    '("PrintDescribeMacro" "Element")
    '("PrintDescribeEnv"   "Element")
    "PrintMacroName"
    "PrintEnvName"

    ;; 2.7 Providing further documentation items
    '("NewDocElement"
      [TeX-arg-key-val LaTeX-doc-newdocelement-key-val-options]
      "Element name" "Environment name")

    ;; 2.8 Displaying sample code verbatim
    ;; "verbatim" environment and "verb" macro are provided by
    ;; latex.el, so we don't add them here again.

    ;; 2.9 Using a special escape character
    '("SpecialEscapechar" "Character")

    ;; 2.10 Cross-referencing all macros used
    "DisableCrossrefs"
    "EnableCrossrefs"
    ;; We don't fontify the next macro since it is a one-liner anyway
    '("DoNotIndex" t)
    "CodelineIndex"
    "PageIndex"
    "theCodelineNo"
    "CodelineNumbered"

    ;; 2.11 Producing the actual index entries
    "actualchar"
    "quotechar"
    "encapchar"
    "levelchar"

    "SpecialMainMacroIndex"
    "SpecialMainEnvIndex"
    "SpecialMacroIndex"
    "SpecialEnvIndex"
    "SpecialIndex"
    "SpecialShortIndex"
    "SortIndex"
    "verbatimchar"

    "subitem"
    "subsubitem"
    "indexspace"
    "efill"
    "pfill"

    ;; 2.12 Setting the index entries: theindex environment is
    ;; provided by latex.el.
    "PrintIndex"
    '("IndexPrologue" t)
    "IndexParms"
    "main"
    "usage"
    "code"

    ;; 2.13 Changing the default values of style parameters
    "DocstyleParms"

    ;; 2.14 Short input of verbatim text pieces: These macros are
    ;; provided by 'shortvrb.el' which is run later

    ;; 2.15 Additional bells and whistles
    "Web"
    "AmSTeX"
    "BibTeX"
    "SliTeX"
    "PlainTeX"
    '("meta" "Text")
    "OnlyDescription"
    '("StopEventually" t)
    '("MaybeStop" t)
    "Finale"
    "AlsoImplementation"
    "IndexInput"
    '("changes" "version" TeX-arg-date t)
    "generalname"
    "RecordChanges"
    "PrintChanges"
    "GlossaryPrologue"
    "GlossaryParms"
    "bslash"
    "MakePrivateLetters"
    "DontCheckModules"
    "CheckModules"
    "Module"
    '("AltMacroFont" t)

    ;; 5.1 makeindex bugs
    "PercentIndex"
    ;; 5.2 File transmission issues
    '("CheckSum" t)
    '("CharacterTable" t))

   (TeX-run-style-hooks "shortvrb")

   (LaTeX-add-lengths "MacrocodeTopsep" "MacroTopsep" "MacroIndent"
                      "IndexMin" "GlossaryMin")
   (LaTeX-add-counters "IndexColumns" "GlossaryColumns" "StandardModuleDepth")

   ;; Macros which should be on their own line:
   (LaTeX-paragraph-commands-add-locally '("DescribeEnv"
                                           "DescribeMacro"
                                           "changes"))

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("SetupDoc"           "{")
                                ("NewDocElement"      "[{{")
                                ("RenewDocElement"    "[{{")
                                ("SpecialEscapechar"  "{")
                                ("DisableCrossrefs"   "")
                                ("EnableCrossrefs"    "")
                                ("CodelineIndex"      "")
                                ("PageIndex"          "")
                                ("CodelineNumbered"   "")
                                ("PrintIndex"         "")
                                ("IndexPrologue"      "")
                                ("AmSTeX"             "")
                                ("BibTeX"             "")
                                ("SliTeX"             "")
                                ("PlainTeX"           "")
                                ("OnlyDescription"    "")
                                ("StopEventually"     "")
                                ("MaybeStop"          "")
                                ("Finale"             "")
                                ("AlsoImplementation" "")
                                ("changes"            "{{{")
                                ("PrintChanges"       "")
                                ("RecordChanges"      ""))
                              'function)
     (font-latex-add-keywords '(("DescribeMacro" "[|{\\")
                                ("DescribeEnv"   "[{"))
                              'variable)
     (font-latex-add-keywords '(("meta"       "{"))
                              'textual)
     (font-latex-add-keywords '(("DocInput"   "{")
                                ("DocInclude" "{" )
                                ("IndexInput" "{"))
                              'reference)))
 TeX-dialect)

(defvar LaTeX-doc-package-options
  '("hyperref" "nohyperref"
    "multicol" "nomulticol"
    "debugshow"
    "noindex" "noprint"
    "reportchangedates")
  "Package options for the doc package.")

;; Local Variables:
;; coding: utf-8
;; End:

;;; doc.el ends here