summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/l3doc.el
blob: e51e83a472e0ee322caa563e9a7c3096ec986b5c (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
;;; l3doc.el --- AUCTeX style for `l3doc.cls'  -*- lexical-binding: t; -*-

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

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2022-03-05
;; 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 `l3doc.cls' dated 2022/02/24.

;;; Code:

(require 'tex)
(require 'latex)

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

(defvar LaTeX-l3doc-cmd-key-val-options
  '(("index")
    ("noindex")
    ("module" ("TeX"))
    ("replace" ("true" "false")))
  "Key=value options for l3doc macros.")

(defun LaTeX-env-l3doc-function (environment)
  "Insert the ENVIRONMENT provided by l3doc.cls.
This function should be used for the environments 'function' and
'variable' provided by the l3doc class.  Also ask the user if a
'syntax' environment should be added as well."
  (let* ((time (format-time-string "%Y-%m-%d" (current-time)))
         (exp-flag (TeX-read-key-val t `(("added" (,time))
                                         ("updated" (,time))
                                         ("deprecated")
                                         ("tested")
                                         ("EXP")
                                         ("rEXP")
                                         ("TF")
                                         ("pTF")
                                         ("noTF")
                                         ("label")
                                         ("verb"))
                                     "Flags (k=v)"))
         (mac (TeX-read-string
               (TeX-argument-prompt nil nil "Macro(s)")
               TeX-esc))
         (active-mark (and (TeX-active-mark)
                           (not (eq (mark) (point)))))
         (elt-count 0)
         (count 1)
         (comment-func (lambda ()
                         (if (TeX-in-line-comment)
                             (indent-according-to-mode)
                           (delete-horizontal-space)
                           (beginning-of-line)
                           (insert "%")
                           (indent-according-to-mode))))
         p)
    (LaTeX-insert-environment environment
                              (concat
                               (unless (zerop (length exp-flag))
                                 (format "[%s]" exp-flag))
                               TeX-grop mac TeX-grcl))
    ;; Remember where we start:
    (setq p (point-marker))
    ;; Now make sure we have '%' everywhere, start at the beginning:
    (search-backward (concat TeX-esc "begin" TeX-grop environment TeX-grcl)
                     (if active-mark nil (line-beginning-position 0))
                     t)
    (funcall comment-func)
    ;; Now at the end:
    (goto-char p)
    (search-forward (concat TeX-esc "end" TeX-grop environment TeX-grcl)
                    (if active-mark nil (line-end-position 2))
                    t)
    (goto-char (match-beginning 0))
    (funcall comment-func)
    ;; Finally for where we started and clean up only when region was
    ;; not active:
    (goto-char p)
    (unless active-mark (funcall comment-func))
    (set-marker p nil)
    ;; Ask if we should insert a 'syntax' environment:
    (when (and (not active-mark)
               (y-or-n-p "Insert syntax environment? "))
      (LaTeX-environment-menu "syntax")
      (funcall comment-func)
      ;; Try to be smart: insert \cs{mac} into buffer.  First, delete
      ;; any whitespaces after the ',' if inserted:
      (setq mac (split-string
                 (replace-regexp-in-string "[[:blank:]]"
                                           ""
                                           mac)
                 "," t))
      ;; Count the number of elements for the number of linebreaks:
      (setq elt-count (length mac))
      ;; Now insert the functions wrapped in \cs:
      (save-excursion
        (dolist (elt mac)
          (insert TeX-esc "cs" TeX-grop (substring elt 1) TeX-grcl)
          (when (< count elt-count)
            (LaTeX-newline)
            (setq count (1+ count)))))
      ;; Now move to end of the first line:
      (end-of-line)
      (just-one-space))))

(defun LaTeX-item-l3doc-syntax ()
  "Insert line break macro on the last line.
For syntax environment from l3doc class."
  (save-excursion
    (end-of-line 0)
    (just-one-space)
    (TeX-insert-macro "\\")))

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

   ;; l3doc.cls loads shortvrb.sty and sets '|' and '"' as shorthands.
   ;; We append them to a local version of `LaTeX-shortvrb-chars'
   ;; before running the style hook for 'shortvrb' which is done
   ;; inside 'doc.el':
   (make-local-variable 'LaTeX-shortvrb-chars)
   (dolist (elt '(?| ?\"))
     (add-to-list 'LaTeX-shortvrb-chars elt t))

   (TeX-run-style-hooks "expl3" "doc" "ltx-base"
                        "article" "array" "amsmath" "booktabs"
                        "color" "colortbl" "hologo" "enumitem"
                        "textcomp" "csquotes" "fancyvrb" "verbatim"
                        "underscore")

   (TeX-add-symbols
    ;; 4.2 Partitioning documentation and implementation
    '("EnableDocumentation" 0)
    '("EnableImplementation" 0)
    '("DisableDocumentation" 0)
    '("DisableImplementation" 0)
    '("DocInputAgain" 0)

    ;; 4.3 General text markup
    '("cmd"
      [TeX-arg-key-val LaTeX-l3doc-cmd-key-val-options]
      TeX-arg-macro)
    '("cs"
      [TeX-arg-key-val LaTeX-l3doc-cmd-key-val-options]
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Macro")
                    (TeX-symbol-list)))
    '("tn"
      [TeX-arg-key-val LaTeX-l3doc-key-val-options]
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Macro")
                    (TeX-symbol-list)))

    ;; "meta" is provided by doc.el, so don't add here again
    '("Arg" "Argument")
    '("marg" "Mandatory argument")
    '("oarg" "Optional argument")
    '("parg" "Picture mode argument")

    '("file" "File name")
    '("env" LaTeX-env-args
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Environment")
                    (LaTeX-environment-list)))
    '("pkg" "Package name")
    '("cls" "Class name")

    '("NB" "Tag" t)

    '("DocInput"
      (TeX-arg-eval
       (lambda ()
         (let ((file (TeX-read-string
                      (format "File(s) to include (default %s): " (current-buffer))
                      nil nil (current-buffer))))
           (format "%s" file)))))

    ;; 4.5 Describing functions in the implementation
    '("TestFiles" "File(s)")
    '("UnitTested" 0)
    '("TestMissing" t))

   (LaTeX-add-environments
    '("documentation")
    '("implementation")
    '("NOTE" "Tag")
    '("function" LaTeX-env-l3doc-function)
    '("variable" LaTeX-env-l3doc-function)
    ;; Feature in 'syntax' environment: Hit 'M-RET' to insert '\\':
    '("syntax")
    '("texnote")
    '("arguments" LaTeX-env-item))

   ;; Do not indent the content of the 'documentation' and
   ;; 'implementation' environments; it is odd when major parts of the
   ;; document are indented.  Append them to a local version of
   ;; `LaTeX-document-regexp':
   (unless (string-match-p "\\<implementation\\>" LaTeX-document-regexp)
     (set (make-local-variable 'LaTeX-document-regexp)
          (concat LaTeX-document-regexp
                  "\\|documentation\\|implementation")))

   ;; Append syntax to `LaTeX-item-list' with `LaTeX-item-l3doc-syntax'
   (add-to-list 'LaTeX-item-list '("syntax" . LaTeX-item-l3doc-syntax) t)

   ;; Fontification
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("cmd"  "[{")
                                ("cs"   "[{")
                                ("Arg"  "{")
                                ("marg" "{")
                                ("oarg" "{")
                                ("parg" "{")
                                ("file" "{")
                                ("env"  "{")
                                ("pkg"  "{")
                                ("cls"  "{")
                                ("NB"   "{{"))
                              'textual)
     (font-latex-add-keywords '(("EnableDocumentation"   "")
                                ("EnableImplementation"  "")
                                ("DisableDocumentation"  "")
                                ("DisableImplementation" "")
                                ("DocInputAgain"         "")
                                ("TestFiles"             "{")
                                ("UnitTested"            "")
                                ("TestMissing"           "{"))
                              'function)
     (font-latex-add-keywords '("DocInput" "{")
                              'reference)))
 TeX-dialect)

(defvar LaTeX-l3doc-class-options
  (progn
    (TeX-load-style "article")
    (append (remove "a5paper" LaTeX-article-class-options)
            '("full" "onlydoc" "check" "nocheck" "checktest"
              "nochecktest" "kernel" "stdmodule" "cm-default"
              "lm-default" "cs-break-off" "cs-break-nohyphen"
              "show-notes" "hide-notes")))
  "Class options for the l3doc class.")

;;; l3doc.el ends here