summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/titlesec.el
blob: c3af0382285c6648e12718c3c3adef9c08f3ad1d (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
;;; titlesec.el --- AUCTeX style for `titlesec.sty' (v2.11)  -*- lexical-binding: t; -*-

;; Copyright (C) 2016--2022 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 `titlesec.sty' (v2.11) from
;; 2019/07/16.  `titlesec.sty' is part of TeXLive.

;;; Code:

(require 'tex)
(require 'latex)

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

(defvar LaTeX-titlesec-key-val-options
  '(("page" ("even" "odd"))
    ("numberless"))
  "Key=value options for \"\\titleformat\" and \"\\titlespacing\"
macros provided by \"titlesec.sty\".")

(defvar LaTeX-titlesec-section-command-list
  '("part"
    "chapter"
    "section"
    "subsection"
    "subsubsection"
    "paragraph"
    "subparagraph")
  "List of sectioning commands available in \"titlesec.sty\".")

(defvar LaTeX-titlesec-section-shape-list
  '("hang"
    "block"
    "display"
    "runin"
    "leftmargin"
    "rightmargin"
    "drop"
    "wrap"
    "frame")
  "List of sectioning shapes available for \"\\titleformat\" command.")

(defun LaTeX-titlesec-section-command-list ()
  "Remove \"chapter\" from variable
`LaTeX-titlesec-section-command-list' and return the remainder.
Removal is based on the return value of function
`LaTeX-largest-level'.  Items returned are prefixed with
`TeX-esc'."
  (mapcar (lambda (elt) (concat TeX-esc elt))
          (if (< (LaTeX-largest-level) 2)
              (symbol-value 'LaTeX-titlesec-section-command-list)
            (remove "chapter" LaTeX-titlesec-section-command-list))))

(defun LaTeX-arg-titlesec-titlespec (optional)
  "Insert the first argument of \"\\titleformat\" and \"\\titlespacing\".
Depending on returned value of function `LaTeX-largest-level',
append a \"name\" key with corresponding values to
`LaTeX-titlesec-key-val-options'.  The values are retrieved from
`LaTeX-titlesec-section-command-list'.  The values of this list
are also added stand-alone as keys.  If OPTIONAL is non-nil,
insert the argument in brackets."
  (let ((keyvals
         (TeX-read-key-val
          optional
          (append
           `(("name"
              ,(mapcar (lambda (elt) (concat TeX-esc elt))
                       (if (< (LaTeX-largest-level) 2)
                           (symbol-value 'LaTeX-titlesec-section-command-list)
                         (remove "chapter" LaTeX-titlesec-section-command-list)))))
           (mapcar #'list
                   (mapcar (lambda (elt) (concat TeX-esc elt))
                           (if (< (LaTeX-largest-level) 2)
                               (symbol-value 'LaTeX-titlesec-section-command-list)
                             (remove "chapter" LaTeX-titlesec-section-command-list))))
           LaTeX-titlesec-key-val-options)
          "Sectioning command")))
    (TeX-argument-insert keyvals optional)))

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

   ;; Load "titleps.el" when "pagestyles" package option is given
   (when (LaTeX-provided-package-options-member "titlesec" "pagestyles")
     (TeX-run-style-hooks "titleps"))

   (TeX-add-symbols
    ;; 2.4. Tools
    '("titlelabel" t)

    ;; \titleformat*{<command>}{<format>}
    '("titleformat*" (LaTeX-arg-titlesec-titlespec) t)

    ;; 3. Advanced Interface
    ;; \titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]
    '("titleformat"
      (LaTeX-arg-titlesec-titlespec)
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Shape")
                    LaTeX-titlesec-section-shape-list]
      (TeX-arg-conditional (y-or-n-p "With optional after-code? ")
                           (4 [nil])
                           (4)))

    '("chaptertitlename" 0)

    ;; 3.2. Spacing
    ;; \titlespacing{<command>}{<left>}{<before-sep>}{<after-sep>}[<right-sep>]
    '("titlespacing"
      (LaTeX-arg-titlesec-titlespec)
      (TeX-arg-length "Left margin")
      (TeX-arg-length "Before vertical space")
      (TeX-arg-length "Space between title and text")
      [TeX-arg-length "Right margin"])

    '("titlespacing*"
      (LaTeX-arg-titlesec-titlespec)
      (TeX-arg-length "Left margin")
      (TeX-arg-length "Before vertical space")
      (TeX-arg-length "Space between title and text")
      [TeX-arg-length "Right margin"])

    ;; 3.3. Spacing related tools
    '("filright"  0)
    '("filcenter" 0)
    '("filleft"   0)
    '("fillast"   0)
    '("filinner"  0)
    '("filouter"  0)
    '("wordsep"   0)
    '("nostruts"  0)

    ;; 3.4. Rules
    '("titleline"
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Alignment")
                    '("l" "r" "c")]
      t)

    '("titlerule" [TeX-arg-length "Rule height"])

    '("titlerule*" [TeX-arg-length "Box width"] "Text")

    '("iftitlemeasuring" 2)

    ;; 3.5. Page styles
    '("assignpagestyle"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Sectioning command")
                    (LaTeX-titlesec-section-command-list))
      (TeX-arg-pagestyle))

    ;; 3.9. Creating new levels and changing the class
    '("titleclass"
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Sectioning command")
                    (LaTeX-titlesec-section-command-list))
      (TeX-arg-eval completing-read
                    (TeX-argument-prompt nil nil "Class")
                    '("page" "top" "straight"))
      [TeX-arg-eval completing-read
                    (TeX-argument-prompt t nil "Super level command")
                    (LaTeX-titlesec-section-command-list)]) )

   ;; Don't increase indent at \iftitlemeasuring:
   (add-to-list 'LaTeX-indent-begin-exceptions-list "iftitlemeasuring" t)
   (LaTeX-indent-commands-regexp-make)

   ;; 3.4. Rules: A variant of \titleline to be used only with calcwidth
   (when (LaTeX-provided-package-options-member "titlesec" "calcwidth")
     (TeX-add-symbols
      '("titleline*"
        (TeX-arg-eval completing-read
                      (TeX-argument-prompt nil nil "Alignment")
                      '("l" "r" "c"))
        t)))

   ;; The length of the longest line is returned in \titlewidth
   (LaTeX-add-lengths "titlewidth"
                      "titlewidthlast"
                      "titlewidthfirst")

   ;; Fontification: We only add macros which are used at top level;
   ;; most of macros definded above are intended to be used in
   ;; arguments of \titleformat
   (when (and (featurep 'font-latex)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("titlelabel"        "{")
                                ;;
                                ;; \titleformat comes in 2 flavors:
                                ;; with *, it takes only 2 mandatory
                                ;; argument; w/o *, a lot more.  It is
                                ;; not (yet) possible to realize this
                                ;; behaviour within font-latex.  Hence
                                ;; we reduce the fontification to the
                                ;; first 2 mandatory arguments and
                                ;; ignore the rest.  *[ are optional anyway.
                                ("titleformat"       "*{[{")
                                ("titlespacing"      "*{{{{[")
                                ("iftitlemeasuring"  "{{")
                                ("assignpagestyle"   "{{")
                                ("titleclass"        "{[{["))
                              'function)))
 TeX-dialect)

(defvar LaTeX-titlesec-package-options
  '(;; 2.1. Format
    "rm" "sf" "tt" "md" "bf" "up" "it" "sl" "sc"
    "big" "medium" "small" "tiny"
    "raggedleft" "center" "raggedright"

    ;; 2.2. Spacing
    "compact"

    ;; 2.3. Uppercase
    "uppercase"

    ;; 3.3. Spacing related tools
    "indentafter"   "noindentafter"
    "rigidchapters" "rubberchapters"
    "bottomtitles"  "nobottomtitles" "nobottomtitles*"
    "aftersep"      "largestsep"     "pageatnewline"
    "nostruts"

    ;; 3.4. Rules
    "calcwidth"

    ;; 3.7. Other Package Options
    "explicit"
    "newparttoc"
    "oldparttoc"
    "clearempty"
    "toctitles"
    "newlinetospace"
    "loadonly"

    ;; 5. titleps and Page Styles
    "pagestyles")
  "Package options for the titlesec package.")

;;; titlesec.el ends here