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

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

;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
;; Maintainer: auctex-devel@gnu.org
;; Created: 2004-10-13
;; 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 `url.sty' v3.4 dated 2013-09-16.

;;; Code:

(require 'tex)
(require 'latex)

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

(declare-function font-latex-set-syntactic-keywords
                  "font-latex")

;; Setup for \DeclareUrlCommand:
(TeX-auto-add-type "url-DeclareUrlCommand" "LaTeX")

(defvar LaTeX-url-DeclareUrlCommand-regexp
  `(,(concat
      "\\\\DeclareUrlCommand"
      "[ \n\r\t]*"
      "{?"
      "\\\\\\([a-zA-Z]+\\)"
      "}?")
    1 LaTeX-auto-url-DeclareUrlCommand)
  "Matches the argument of `\\DeclareUrlCommand' from `url' package.")

(defun LaTeX-url-DeclareUrlCommand-prepare ()
  "Process macros parsed from `\\DeclareUrlCommand'."
  ;; \DeclareUrlCommand\abc{settings}: makes \abc{ } like \url{ } with
  ;; settings.
  (when (LaTeX-url-DeclareUrlCommand-list)
    (dolist (cmd (mapcar #'car (LaTeX-url-DeclareUrlCommand-list)))
      (TeX-add-symbols `(,cmd TeX-arg-verb-delim-or-brace))
      (add-to-list 'LaTeX-verbatim-macros-with-delims-local cmd)
      (add-to-list 'LaTeX-verbatim-macros-with-braces-local cmd))
    ;; Fontification
    (when (and (fboundp 'font-latex-add-keywords)
               (fboundp 'font-latex-set-syntactic-keywords)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords
       (mapcar (lambda (cmd)
                 (list cmd  ""))
               (mapcar #'car (LaTeX-url-DeclareUrlCommand-list)))
       'reference)
      ;; Tell font-lock about the update.
      (font-latex-set-syntactic-keywords))))

;; Setup for \urldef:
(TeX-auto-add-type "url-urldef" "LaTeX")

(defvar LaTeX-url-urldef-regexp
  `(,(concat
      "\\\\urldef"
      "[ \n\r\t]*"
      "{?"
      "\\\\\\([a-zA-Z]+\\)"
      "}?")
    1 LaTeX-auto-url-urldef)
  "Matches the argument of `\\urldef' from `url' package.")

(defun LaTeX-url-urldef-prepare ()
  "Process macros parsed from `\\urldef'."
  (when (LaTeX-url-urldef-list)
    (mapc #'TeX-add-symbols (mapcar #'car (LaTeX-url-urldef-list)))
    ;; Fontification
    (when (and (fboundp 'font-latex-add-keywords)
               (eq TeX-install-font-lock 'font-latex-setup))
      (font-latex-add-keywords
       (mapcar (lambda (cmd)
                 (list cmd ""))
               (mapcar #'car (LaTeX-url-urldef-list)))
       'reference))))

(defun LaTeX-url-auto-prepare ()
  "Clear `LaTeX-auto-url-*' before parsing."
  (setq LaTeX-auto-url-DeclareUrlCommand nil
        LaTeX-auto-url-urldef            nil))

(defun LaTeX-url-auto-cleanup ()
  "Process parsed elements from url.sty."
  (LaTeX-url-DeclareUrlCommand-prepare)
  (LaTeX-url-urldef-prepare))

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

(defun TeX-arg-url-urlstyle (optional &optional prompt)
  "Prompt for style used in \\urlstyle with completion.
If OPTIONAL is non-nil, indicate it in the minibuffer during the
query and insert the result in brackets.  PROMPT replaces the
standard one."
  (TeX-argument-insert
   (completing-read (TeX-argument-prompt optional prompt "Style")
                    '("rm" "same" "sf" "tt"))
   optional))

(defun TeX-arg-url-DeclareUrlCommand (optional &optional prompt)
  "Prompt for arguments of \\DeclareUrlCommand with completion.
If OPTIONAL is non-nil, indicate it in the minibuffer during the
query and insert the result in brackets.  PROMPT replaces the
standard one."
  (let ((cmd (TeX-read-string
              (TeX-argument-prompt optional prompt "Command: \\" t)))
        (style (completing-read
                (TeX-argument-prompt optional prompt "Style")
                '("rm" "same" "sf" "tt"))))
    (insert TeX-esc cmd TeX-grop)
    (when (and style (not (string= style "")))
      (insert TeX-esc "urlstyle" TeX-grop style TeX-grcl))
    (insert TeX-grcl)
    (LaTeX-add-url-DeclareUrlCommands cmd)
    (LaTeX-url-DeclareUrlCommand-prepare)))

(defun TeX-arg-url-urldef (optional &optional prompt)
  "Prompt for arguments of \\urldef with completion."
  (let ((cmd (TeX-read-string
              (TeX-argument-prompt optional prompt "Command: \\" t))))
    (TeX-argument-insert cmd optional TeX-esc)
    (LaTeX-add-url-urldefs cmd)
    (LaTeX-url-urldef-prepare))
  (TeX-insert-macro
   (completing-read
    (TeX-argument-prompt optional prompt "Macro: \\" t)
    (append (mapcar #'car (LaTeX-url-DeclareUrlCommand-list))
            '("url")))))

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

   ;; Add url to the parser
   (TeX-auto-add-regexp LaTeX-url-DeclareUrlCommand-regexp)
   (TeX-auto-add-regexp LaTeX-url-urldef-regexp)

   ;; New symbols
   (TeX-add-symbols
    ;; Macros for defining new styles, changing font, linebreaks etc.
    "Url"
    "UrlBigBreakPenalty"
    "UrlBigBreaks"
    "UrlBreakPenalty"
    "UrlBreaks"
    "UrlFont"
    "UrlLeft"
    "UrlNoBreaks"
    "UrlOrds"
    "UrlRight"
    "UrlSpecials"

    ;; "hyperref" redefines \url so that the argument is only in
    ;; braces.  We check here if hyperref is loaded:
    '("url" (TeX-arg-conditional (member "hyperref" (TeX-style-list))
                                 ("Url")
                                 ((TeX-arg-verb-delim-or-brace "Url"))))

    '("urldef" TeX-arg-url-urldef)

    '("urlstyle" TeX-arg-url-urlstyle))

   ;; For '\path', use the facilities provided by this style.  Also
   ;; don't add "path" for fontification below since
   ;; `LaTeX-url-DeclareUrlCommand-prepare' takes care of it.
   (LaTeX-add-url-DeclareUrlCommands "path")
   (LaTeX-url-DeclareUrlCommand-prepare)

   ;; Don't do the same for '\url' because hyperref.el has some code
   ;; to remove "url" from `LaTeX-verbatim-macros-with-delims-local',
   ;; but we check here as well if "hyperref" is already loaded:
   (unless (member "hyperref" (TeX-style-list))
     (add-to-list 'LaTeX-verbatim-macros-with-delims-local "url"))
   (add-to-list 'LaTeX-verbatim-macros-with-braces-local "url")

   ;; Fontification
   (when (and (fboundp 'font-latex-add-keywords)
              (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("url" ""))
                              'reference)
     ;; Don't fontify the second argument of 'DeclareUrlCommand' since
     ;; it gets the `font-latex-verbatim-face' from the first
     ;; argument.  Same applies also to '\urldef' where we don't
     ;; fontify any arguments:
     (font-latex-add-keywords '(("DeclareUrlCommand" "\\")
                                ("urldef"            "")
                                ("urlstyle"          "{"))
                              'function)
     ;; Tell font-lock about the update.
     (font-latex-set-syntactic-keywords)))
 TeX-dialect)

(defvar LaTeX-url-package-options '("hyphens" "obeyspaces" "spaces"
                                    "allowmove" "lowtilde")
  "Package options for the url package.")

;;; url.el ends here