summaryrefslogtreecommitdiff
path: root/elpa/auctex-13.1.3/style/color.el
blob: a98b01bd4838d1855392e5468d3222af7511f678 (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
;;; color.el --- AUCTeX style for `color.sty' (v1.3d)  -*- lexical-binding: t; -*-

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

;; Author: Arash Esbati <arash@gnu.org>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2015-01-16
;; 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 `color.sty' (v1.3d) from 2022/01/06.
;; `color.sty' is part of TeXLive.

;; Many thanks to Tassilo Horn for his percetive comments on
;; implementation of this style and testing.

;;; Code:

;; Needed for compiling `LaTeX-check-insert-macro-default-style':
(require 'latex)

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

(defvar LaTeX-color-colour-models
  '("cmyk" "gray" "named" "rgb")
  "List of color models provided by `color.sty'.")

(defvar LaTeX-color-dvipsnames-colors
  '("Apricot"        "Aquamarine"      "Bittersweet"  "Black"
    "Blue"           "BlueGreen"       "BlueViolet"   "BrickRed"
    "Brown"          "BurntOrange"     "CadetBlue"    "CarnationPink"
    "Cerulean"       "CornflowerBlue"  "Cyan"         "Dandelion"
    "DarkOrchid"     "Emerald"         "ForestGreen"  "Fuchsia"
    "Goldenrod"      "Gray"            "Green"        "GreenYellow"
    "JungleGreen"    "Lavender"        "LimeGreen"    "Magenta"
    "Mahogany"       "Maroon"          "Melon"        "MidnightBlue"
    "Mulberry"       "NavyBlue"        "OliveGreen"   "Orange"
    "OrangeRed"      "Orchid"          "Peach"        "Periwinkle"
    "PineGreen"      "Plum"            "ProcessBlue"  "Purple"
    "RawSienna"      "Red"             "RedOrange"    "RedViolet"
    "Rhodamine"      "RoyalBlue"       "RoyalPurple"  "RubineRed"
    "Salmon"         "SeaGreen"        "Sepia"        "SkyBlue"
    "SpringGreen"    "Tan"             "TealBlue"     "Thistle"
    "Turquoise"      "Violet"          "VioletRed"    "White"
    "WildStrawberry" "Yellow"          "YellowGreen"  "YellowOrange")
  "List of colors defined by package option `dvipsnames' from `color.sty'.")

;; Needed for auto-parsing.
(require 'tex)

;; Plug \definecolor into the parser
(TeX-auto-add-type "color-definecolor" "LaTeX")

(defvar LaTeX-color-definecolor-regexp
  '("\\\\definecolor{\\([^}]+\\)}" 1 LaTeX-auto-color-definecolor)
  "Matches the argument of \\definecolor from color package.")

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

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

(defun TeX-arg-color-definecolor (optional &optional prompt)
  "Insert arguments of `\\definecolor' from `color.sty'."
  ;; \definecolor{<name>}{<model>}{<color spec>}
  ;; Ask for <name>, add to our list and insert it
  (let ((colorname (TeX-read-string "Color name: ")))
    (LaTeX-add-color-definecolors colorname)
    (TeX-argument-insert colorname optional))
  ;; Ask and insert <model>
  (let ((model (completing-read
                (TeX-argument-prompt optional prompt "Color model")
                (if (not (or (LaTeX-provided-package-options-member "color" "dvips")
                             (LaTeX-provided-package-options-member "color" "dvipsnames")))
                    (remove "named" LaTeX-color-colour-models)
                  LaTeX-color-colour-models))))
    (TeX-argument-insert model optional)
    ;; Depending on <model>, ask for <color spec> and insert it
    (cond (;; <cmyk> model
           (string-equal model "cmyk")
           (let ((cyan    (TeX-read-string "Value Cyan (between 0,1): "))
                 (magenta (TeX-read-string "Value Magenta (between 0,1): "))
                 (yellow  (TeX-read-string "Value Yellow (between 0,1): "))
                 (black   (TeX-read-string "Value Black (between 0,1): ")))
             (TeX-argument-insert
              (concat cyan "," magenta "," yellow "," black) optional)))
          ;; <rgb> model
          ((string-equal model "rgb")
           (let ((red   (TeX-read-string "Value Red (between 0,1): "))
                 (green (TeX-read-string "Value Green (between 0,1): "))
                 (blue  (TeX-read-string "Value Blue (between 0,1): ")))
             (TeX-argument-insert
              (concat red "," green "," blue) optional)))
          ;; <gray> model
          ((string-equal model "gray")
           (let ((grayness (TeX-read-string "Value Gray (between 0,1): ")))
             (TeX-argument-insert grayness optional)))
          ;; <named> model takes the dvipsnames
          ((string-equal model "named")
           (let ((color (completing-read "Named Color: "
                                         LaTeX-color-dvipsnames-colors)))
             (TeX-argument-insert color optional))))))

(defun TeX-arg-color (optional &optional prompt)
  "Insert arguments of various color commands from `color.sty'."
  ;; \color{<name>} or \color[<model>]{<color spec>} First, ask for
  ;; <model>.  This happens depending on the values of
  ;; `TeX-insert-macro-default-style' and if `current-prefix-arg'.
  ;; `named' is removed here from completion if package option is not
  ;; given.
  (let* ((TeX-last-optional-rejected nil)
         (model (LaTeX-check-insert-macro-default-style
                 (completing-read
                  (TeX-argument-prompt t prompt "Color model")
                  (if (not (or (LaTeX-provided-package-options-member "color" "dvips")
                               (LaTeX-provided-package-options-member "color" "dvipsnames")))
                      (remove "named" LaTeX-color-colour-models)
                    LaTeX-color-colour-models)))))
    ;; If <model> is non-nil because of 'mandatory-args-only and not
    ;; an empty string, then insert it
    (if (and model (not (string-equal model "")))
        (progn
          (insert (concat LaTeX-optop model LaTeX-optcl))
          (cond (;; <cmyk> model
                 (string-equal model "cmyk")
                 (let ((cyan    (TeX-read-string "Value Cyan (between 0,1): "))
                       (magenta (TeX-read-string "Value Magenta (between 0,1): "))
                       (yellow  (TeX-read-string "Value Yellow (between 0,1): "))
                       (black   (TeX-read-string "Value Black (between 0,1): ")))
                   (TeX-argument-insert
                    (concat cyan "," magenta "," yellow "," black) optional)))
                ;; <rgb> model
                ((string-equal model "rgb")
                 (let ((red   (TeX-read-string "Value Red (between 0,1): "))
                       (green (TeX-read-string "Value Green (between 0,1): "))
                       (blue  (TeX-read-string "Value Blue (between 0,1): ")))
                   (TeX-argument-insert
                    (concat red "," green "," blue) optional)))
                ;; <gray> model
                ((string-equal model "gray")
                 (let ((grayness (TeX-read-string "Value Gray (between 0,1): ")))
                   (TeX-argument-insert grayness optional)))
                ;; <named> model; allowed are dvipsnames.
                ((string-equal model "named")
                 (let ((color (completing-read "Named Color: "
                                               LaTeX-color-dvipsnames-colors)))
                   (TeX-argument-insert color optional)))))
      ;; if empty, ask for <name> with completion
      (let ((color (completing-read
                    (TeX-argument-prompt optional prompt "Color name")
                    (LaTeX-color-definecolor-list))))
        (TeX-argument-insert color optional)))))

(defun TeX-arg-color-fcolorbox (optional &optional prompt)
  "Insert arguments of `\\fcolorbox' from `color.sty'. "
  ;; \fcolorbox{<frame color name>}{<box color name>}{<text>} or
  ;; \fcolorbox[<model>]{<frame color spec>}{<box color spec>}{<text>}
  ;; First, ask for <model> depending on
  ;; `TeX-insert-macro-default-style' and `current-prefix-arg'.
  ;; Remove `named' if necessary.
  (let* ((TeX-last-optional-rejected nil)
         (model (LaTeX-check-insert-macro-default-style
                 (completing-read
                  (TeX-argument-prompt t prompt "Color model")
                  (if (not (or (LaTeX-provided-package-options-member "color" "dvips")
                               (LaTeX-provided-package-options-member "color" "dvipsnames")))
                      (remove "named" LaTeX-color-colour-models)
                    LaTeX-color-colour-models)))))
    ;; If <model> is non-nil because of 'mandatory-args-only and not
    ;; an empty string, then insert [<model>] and cater for 2
    ;; mandatory args.
    (if (and model (not (string-equal model "")))
        (progn
          (insert (concat LaTeX-optop model LaTeX-optcl))
          (cond (;; <cmyk> model
                 (string-equal model "cmyk")
                 (let ((cyan    (TeX-read-string "Frame value Cyan (between 0,1): "))
                       (magenta (TeX-read-string "Frame value Magenta (between 0,1): "))
                       (yellow  (TeX-read-string "Frame value Yellow (between 0,1): "))
                       (black   (TeX-read-string "Frame value Black (between 0,1): ")))
                   (TeX-argument-insert
                    (concat cyan "," magenta "," yellow "," black) optional))
                 (let ((cyan    (TeX-read-string "Box value Cyan (between 0,1): "))
                       (magenta (TeX-read-string "Box value Magenta (between 0,1): "))
                       (yellow  (TeX-read-string "Box value Yellow (between 0,1): "))
                       (black   (TeX-read-string "Box value Black (between 0,1): ")))
                   (TeX-argument-insert
                    (concat cyan "," magenta "," yellow "," black) optional)))
                ;; <rgb> model
                ((string-equal model "rgb")
                 (let ((red   (TeX-read-string "Frame value Red (between 0,1): "))
                       (green (TeX-read-string "Frame value Green (between 0,1): "))
                       (blue  (TeX-read-string "Frame value Blue (between 0,1): ")))
                   (TeX-argument-insert
                    (concat red "," green "," blue) optional))
                 (let ((red   (TeX-read-string "Box value Red (between 0,1): "))
                       (green (TeX-read-string "Box value Green (between 0,1): "))
                       (blue  (TeX-read-string "box value Blue (between 0,1): ")))
                   (TeX-argument-insert
                    (concat red "," green "," blue) optional)))
                ;; <gray> model
                ((string-equal model "gray")
                 (let ((grayness (TeX-read-string "Frame value Gray (between 0,1): ")))
                   (TeX-argument-insert grayness optional))
                 (let ((grayness (TeX-read-string "Box value Gray (between 0,1): ")))
                   (TeX-argument-insert grayness optional)))
                ;; <named> model; allowed are dvipsnames.
                ((string-equal model "named")
                 (let ((color (completing-read "Frame named Color: "
                                               LaTeX-color-dvipsnames-colors)))
                   (TeX-argument-insert color optional))
                 (let ((color (completing-read "Box named Color: "
                                               LaTeX-color-dvipsnames-colors)))
                   (TeX-argument-insert color optional)))))
      ;; if empty, ask for {<frame color spce>}{<box color name>} with completion
      (let ((frame-color (completing-read
                          (TeX-argument-prompt optional prompt "Frame color name")
                          (LaTeX-color-definecolor-list)))
            (box-color   (completing-read
                          (TeX-argument-prompt optional prompt "Box color name")
                          (LaTeX-color-definecolor-list))))
        (TeX-argument-insert frame-color optional)
        (TeX-argument-insert box-color   optional)))))

(TeX-add-style-hook
 "color"
 (lambda ()
   ;; Add color to the parser.
   (TeX-auto-add-regexp LaTeX-color-definecolor-regexp)

   ;; Add list of colors which are always available.
   (LaTeX-add-color-definecolors
    "black" "blue" "cyan" "green" "magenta" "red" "white" "yellow")

   ;; Add dvips colors in conjunction with `usenames'.
   (when (and (LaTeX-provided-package-options-member "color" "usenames")
              (or (LaTeX-provided-package-options-member "color" "dvips")
                  (LaTeX-provided-package-options-member "color" "dvipsnames")))
     (apply #'LaTeX-add-color-definecolors LaTeX-color-dvipsnames-colors))

   (unless (member "xcolor" (TeX-style-list))
     (TeX-add-symbols
      ;; \definecolor{<name>}{<model>}{<color spec>}
      '("definecolor" TeX-arg-color-definecolor)

      ;; \color{<name>} or \color[<model>]{<color spec>}
      '("color" TeX-arg-color)

      ;; \textcolor{<name>}{<text>} or
      ;; \textcolor[<model>]{<color spec>}{<text>}
      '("textcolor" TeX-arg-color "Text")

      ;; \mathcolor{<name>}{<math>} or
      ;; \mathcolor[<model>]{<color spec>}{<math>}
      '("mathcolor" TeX-arg-color "Math")

      ;; \pagecolor{<name>} or
      ;; \pagecolor[<model>]{<color spec>}
      '("pagecolor" TeX-arg-color)

      ;; \nopagecolor
      '("nopagecolor" 0)

      ;; \colorbox{<name>}{<text>} or
      ;; \colorbox[<model>]{<color spec>}{<text>}
      '("colorbox" TeX-arg-color "Text")

      ;; \fcolorbox{<frame color name>}{<box color name>}{<text>} or
      ;; \fcolorbox[<model>]{<frame color spec>}{<box color spec>}{<text>}
      '("fcolorbox" TeX-arg-color-fcolorbox "Text"))

     ;; Fontification
     (when (and (featurep 'font-latex)
                (eq TeX-install-font-lock 'font-latex-setup))
       (font-latex-add-keywords '(("color"         "[{")
                                  ("pagecolor"     "[{"))
                                'type-declaration)
       (font-latex-add-keywords '(("textcolor"     "[{{")
                                  ("colorbox"      "[{{" )
                                  ("fcolorbox"     "[{{{"))
                                'type-command)
       (font-latex-add-keywords '(("definecolor"    "{{{"))
                                'function))))
 TeX-dialect)

(defvar LaTeX-color-package-options
  '("debugshow" "dvipdf" "dvipdfm" "dvipdfmx" "dvips" "dvipsnames"
    "dvipsone" "dvisvgm" "dviwin" "dviwindo" "emtex" "luatex"
    "monochrome" "nodvipsnames" "nosetpagesize" "oztex" "pctex32"
    "pctexhp" "pctexps" "pctexwin" "pdftex" "setpagesize" "tcidvi"
    "textures" "truetex" "usenames" "vtex" "xdvi" "xetex")
  "Package options for the color package.")

;;; color.el ends here