forked from monsanto/auto-complete-auctex
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcompany-auctex.el
334 lines (278 loc) · 11.9 KB
/
company-auctex.el
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
;;; company-auctex.el --- Company-mode auto-completion for AUCTeX
;; Copyright (C) 2012 Christopher Monsanto, 2014 Alexey Romanov
;; Author: Christopher Monsanto <[email protected]>, Alexey Romanov <[email protected]>
;; Version: 0.1
;; URL: https://github.com/alexeyr/company-auctex/
;; Package-Requires: ((yasnippet "0.8.0") (company "0.8.0") (auctex "11.87"))
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; To use:
;; (require 'company-auctex)
;; (company-auctex-init)
;; Feel free to contribute better documentation!
;;; Code:
(require 'tex)
(require 'latex)
(require 'company)
(require 'yasnippet)
(eval-when-compile (require 'cl-lib))
(defvar company-auctex-arg-lookup-table
'((TeX-arg-define-macro . ("\\MacroName"))
(TeX-arg-counter . ("Counter"))
(TeX-arg-define-counter . ("\\CounterName"))
(TeX-arg-file . ("Filename"))
(TeX-arg-bibliography . ("Filename"))
(TeX-arg-bibstyle . ("Style"))
(TeX-arg-environment . ("Environment"))
(TeX-arg-define-environment . ("EnvironmentName"))
(TeX-arg-size . ("(w, h)"))
(TeX-arg-ref . ("Name"))
(TeX-arg-index . ("Index"))
(TeX-arg-define-label . ("Label"))
(LaTeX-arg-section . ("Title"))
(LaTeX-arg-usepackage . (["opt1,..."] "Package"))
(LaTeX-env-label . nil)
(LaTeX-amsmath-env-aligned . (["htbp!"]))
(LaTeX-amsmath-env-alignat . (["# Columns"]))
(LaTeX-env-array . (["bct"] "lcrpmb|"))
(LaTeX-env-item . nil)
(LaTeX-env-document . nil)
(LaTeX-env-figure . (["htbp!"]))
(LaTeX-env-contents . ("Filename"))
(LaTeX-env-minipage . (["htbp!"] "Width"))
(LaTeX-env-list . ("Label" "\\itemsep,\\labelsep,..."))
(LaTeX-env-picture . ("(w, h)" "(x, y)"))
(LaTeX-env-tabular* . ("Width" ["htbp!"] "lcrpmb|><"))
(LaTeX-env-bib . ("WidestLabel"))
(TeX-arg-conditional . ([""]))
(2 . ("" ""))
(3 . ("" "" ""))
(4 . ("" "" "" ""))
(5 . ("" "" "" "" ""))
(6 . ("" "" "" "" "" ""))
(7 . ("" "" "" "" "" "" ""))
(8 . ("" "" "" "" "" "" "" ""))
(9 . ("" "" "" "" "" "" "" "" "")))
"Anything not in this table defaults to '(\"\").")
(defgroup company-auctex nil
"Completion backend for AUCTeX."
:prefix "company-auctex-"
:tag "Company AUCTeX"
:group 'company)
(defun car-or (item)
"Return car of ITEM if it's a cons, ITEM otherwise."
(or (car-safe item) item))
(defun company-auctex-lookup-arg (item)
(let ((arg (assoc (car-or item) company-auctex-arg-lookup-table)))
(if arg (cdr arg) '(""))))
(defun company-auctex-expand-arg-info (arg-info)
(cl-loop for item in arg-info
append (cond
((or (stringp item) (and (vectorp item) (stringp (elt item 0))))
(list item))
((vectorp item)
(cl-loop for item-2 in (company-auctex-lookup-arg (elt item 0))
collect [item-2]))
(t
(company-auctex-lookup-arg item)))))
(defun company-auctex-snippet-arg (arg)
(let* ((opt (vectorp arg))
(item (if opt (elt arg 0) arg))
(var (format "${%s}" item)))
(if opt
(concat "${[" var "]}")
(concat "{" var "}"))))
(defun company-auctex-prefix (regexp)
"Returns the prefix for matching given REGEXP."
(and (derived-mode-p 'latex-mode)
(when (looking-back regexp)
(match-string-no-properties 1))))
;; Macros
;;
(defun company-auctex--disable-yas ()
(yas-minor-mode -1)
(remove-hook 'yas-after-exit-snippet-hook #'company-auctex--disable-yas))
(defmacro company-auctex-with-yas (&rest body)
`(progn
(unless yas-minor-mode
(yas-minor-mode +1)
(add-hook 'yas-after-exit-snippet-hook #'company-auctex--disable-yas))
,@body))
(defun company-auctex-get-LaTeX-font-list (&optional mathp)
(delq nil (mapcar
(lambda (x)
(and (stringp x)
(not (string= x ""))
(not (string= x "}"))
(list (substring x 1 -1) t)))
(delete-dups
(mapcar (if mathp
(lambda (x) (nth 3 x))
#'cadr)
LaTeX-font-list)))))
(defun company-auctex-macro-snippet (arg-info)
(let ((count 1))
(apply 'concat
(cl-loop for item in (company-auctex-expand-arg-info arg-info)
collect (company-auctex-snippet-arg item)))))
(defun company-auctex-expand-args (str env)
(company-auctex-with-yas
(yas-expand-snippet (company-auctex-macro-snippet (assoc-default str env)))))
(defun company-auctex-macro-candidates (prefix)
(let ((comlist (mapcar (lambda (item) (car-or (car item)))
(append (TeX-symbol-list)
(LaTeX-length-list)
LaTeX-section-list
(company-auctex-get-LaTeX-font-list)))))
(all-completions prefix comlist)))
(defun company-auctex-macro-post-completion (candidate)
(company-auctex-expand-args candidate
(append (TeX-symbol-list)
(mapcar (lambda (item)
(list (car item) 'LaTeX-arg-section))
LaTeX-section-list)
(company-auctex-get-LaTeX-font-list))))
;;;###autoload
(defun company-auctex-macros (command &optional arg &rest ignored)
"company-auctex-macros backend"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-auctex-macros))
(prefix (company-auctex-prefix "\\\\\\([a-zA-Z]*\\)\\="))
(candidates (company-auctex-macro-candidates arg))
(post-completion (company-auctex-macro-post-completion arg))))
;; Symbols
;;
(defcustom company-auctex-symbol-math-symbol '("$" . "$")
"If non-nil, on insertion of a candidate of the
`company-auctex-symbol' backend, when outside math mode,
`company-auctex' will insert symbols for opening and closing
inline equation and leave point inside them.
If nil, the insertion of math symbols is disabled.
If equal to the symbol 'TeX-electric-math `company-auctex' will
use the same pair `TeX-insert-dollar' does.
Otherwise, this variable should be a cons cell whose CAR is the
string to insert before point, the CDR is the string to insert
after point. You can choose between \"$...$\" and
\"\\(...\\)\"."
:group 'company-auctex
:type '(choice (const :tag "No math symbol added" nil)
(const :tag "Use TeX-electric-math" TeX-electric-math)
(const :tag "$...$" ("$" . "$"))
(const :tag "\\(...\\)" ("\\(" . "\\)"))
(cons :tag "Other"
(string :tag "Insert before symbol")
(string :tag "Insert after symbol"))))
(defun company-auctex-math-all ()
(append LaTeX-math-list LaTeX-math-default))
(defun company-auctex-symbol-candidates (prefix)
(all-completions prefix (append (mapcar 'cadr (company-auctex-math-all))
(mapcar 'car (company-auctex-get-LaTeX-font-list t)))))
(defun company-auctex-symbol-post-completion (candidate)
(search-backward candidate)
(delete-region (1- (match-beginning 0)) (match-end 0))
(let ((math-symb (cond
((or (not company-auctex-symbol-math-symbol)
(consp company-auctex-symbol-math-symbol))
company-auctex-symbol-math-symbol)
((equal company-auctex-symbol-math-symbol
'TeX-electric-math)
TeX-electric-math)
(t ; fallback value, equal to default
'("$" . "$")))))
(if (texmathp)
(insert "\\" candidate)
(insert (or (car math-symb) "") "\\" candidate)
(save-excursion (insert (or (cdr math-symb) "")))))
(company-auctex-expand-args
candidate
(append (TeX-symbol-list) (company-auctex-get-LaTeX-font-list t))))
(defun company-auctex-symbol-annotation (candidate)
(let ((char (nth 2 (assoc candidate (mapcar 'cdr (company-auctex-math-all))))))
(and char (concat " " (char-to-string (decode-char 'ucs char))))))
;;;###autoload
(defun company-auctex-symbols (command &optional arg &rest ignored)
"company-auctex-symbols backend"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-auctex-symbols))
(prefix (company-auctex-prefix "\\\\\\([a-zA-Z]*\\)\\="))
(candidates (company-auctex-symbol-candidates arg))
(post-completion (company-auctex-symbol-post-completion arg))
(annotation (company-auctex-symbol-annotation arg))))
;; Environments
;;
(defcustom company-auctex-environment-prefix "beg"
"Prefix for auto-completing environments.")
(defun company-auctex-environment-candidates (prefix)
(let
((envlist
(mapcar (lambda (item)
(concat company-auctex-environment-prefix (car item)))
(LaTeX-environment-list))))
(all-completions prefix envlist)))
(defun company-auctex-environment-post-completion (candidate)
(search-backward candidate)
(delete-region (1- (match-beginning 0)) (match-end 0))
(let ((candidate
(substring candidate (length company-auctex-environment-prefix))))
(company-auctex-with-yas
(yas-expand-snippet
(format "\\begin{%s}%s\n$0\n\\end{%s}"
candidate
(company-auctex-macro-snippet
(assoc-default candidate (LaTeX-environment-list)))
candidate)))))
;;;###autoload
(defun company-auctex-environments (command &optional arg &rest ignored)
"company-auctex-environments backend"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-auctex-environments))
(prefix (company-auctex-prefix "\\\\\\([a-zA-Z]*\\)\\="))
(candidates (company-auctex-environment-candidates arg))
(post-completion (company-auctex-environment-post-completion arg))))
;; Refs
;;
(defun company-auctex-label-candidates (prefix)
(all-completions prefix (mapcar 'car (LaTeX-label-list))))
;;;###autoload
(defun company-auctex-labels (command &optional arg &rest ignored)
"company-auctex-labels backend"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-auctex-labels))
(prefix (company-auctex-prefix "\\\\\\(?:eq\\|auto\\|c\\)?ref{\\([^}]*\\)\\="))
(candidates (company-auctex-label-candidates arg))))
;; Bibs
;;
(defun company-auctex-bib-candidates (prefix)
(all-completions prefix (mapcar 'car (LaTeX-bibitem-list))))
;;;###autoload
(defun company-auctex-bibs (command &optional arg &rest ignored)
"company-auctex-bibs backend"
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-auctex-bibs))
(prefix (company-auctex-prefix "\\\\cite[^[{]*\\(?:\\[[^]]*\\]\\)?{\\([^},]*\\)\\="))
(candidates (company-auctex-bib-candidates arg))))
;; Merged backend
;;
;;;###autoload
(defun company-auctex-init ()
"Add backends provided by company-auctex to company-backends."
(add-to-list 'company-backends 'company-auctex-labels)
(add-to-list 'company-backends 'company-auctex-bibs)
(add-to-list 'company-backends
'(company-auctex-macros company-auctex-symbols company-auctex-environments)))
(provide 'company-auctex)
;;; company-auctex.el ends here