forked from SonjayaJetBrain/animated-gogglases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac-math.el
180 lines (161 loc) · 6.04 KB
/
ac-math.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
;;; ac-math.el --- Auto-complete sources for input of mathematical symbols and latex tags
;;
;; Copyright (C) 2011-2013, Vitalie Spinu
;; Author: Vitalie Spinu
;; URL: https://github.com/vitoshka/ac-math
;; Keywords: latex, auto-complete, Unicode, symbols
;; Version: 1.1
;; Package-Requires: ((auto-complete "1.4") (math-symbol-lists "1.0"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This file is *NOT* part of GNU Emacs.
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;; Features that might be required by this library:
;;
;; auto-complete http://cx4a.org/software/auto-complete/
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;
;; This add-on defines three ac-sources for the
;; *[auto-complete](https://github.com/auto-complete)* package:
;;
;; * ac-source-latex-commands - input latex commands
;; * ac-source-math-latex - input math latex tags (by default, active only in math environments in latex modes)
;; * ac-source-math-unicode - input of unicode symbols (by default, active everywhere except math environments)
;;
;; Start math completion by typing the prefix "\" key. Select the completion
;; type RET (`ac-complete`). Completion on TAB (`ac-expand`) is not that great
;; as you will see dummy characters, but it's usable.
;;
;; (See https://github.com/vitoshka/ac-math#readme for more)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'math-symbol-lists)
(require 'auto-complete)
(defgroup ac-math nil
"Auto completion."
:group 'auto-complete
:prefix "ac-math")
(defcustom ac-math-unicode-in-math-p nil
"Set this to t if unicode in math latex environments is needed."
:group 'ac-math)
(defcustom ac-math-prefix-regexp "\\\\\\(.*\\)"
"Regexp matching the prefix of the ac-math symbol."
:group 'ac-math)
(defvar ac-math--dummy " ")
(defun ac-math--make-candidates (alist &optional unicode)
"Build a list of math symbols ready to be used in ac source.
Each element is a cons cell (SYMB . VALUE) where SYMB is the
string to be displayed during the completion and the VALUE is the
actually value inserted on RET completion. If UNICODE is non-nil
the value of VALUE is the unicode character else it's the latex
command."
(delq nil
(mapcar
#'(lambda (el)
(let* ((symb (substring (nth 1 el) 1))
;; (sep (propertize ac-math--dummy 'display ""))
(sep ac-math--dummy)
(ch (and (nth 2 el) (decode-char 'ucs (nth 2 el))))
(uni-symb (and ch (char-to-string ch)))
(uni-string (concat sep uni-symb)))
(unless (and unicode (null uni-symb))
(cons (concat symb uni-string)
(if unicode
uni-symb
symb)))))
alist)))
<<<<<<< HEAD
=======
;;;###autoload
>>>>>>> 5aa4cd9 (Add autoloads)
(defconst ac-math-symbols-latex
(delete-dups
(append (ac-math--make-candidates math-symbol-list-basic)
(ac-math--make-candidates math-symbol-list-extended)))
"List of math completion candidates.")
<<<<<<< HEAD
=======
;;;###autoload
>>>>>>> 5aa4cd9 (Add autoloads)
(defconst ac-math-symbols-unicode
(delete-dups
(append (ac-math--make-candidates math-symbol-list-basic t)
(ac-math--make-candidates math-symbol-list-extended t)))
"List of math completion candidates.")
(defun ac-math-action-latex (&optional del-backward)
"Function to be used in ac action property.
Deletes the unicode symbol from the end of the completed
string. If DEL-BACKWARD is non-nil, delete the name of the symbol
instead."
(let ((pos (point))
(start-dummy (save-excursion
(re-search-backward ac-math--dummy (point-at-bol) 'no-error)))
(end-dummy (match-end 0))
(inhibit-point-motion-hooks t))
(when start-dummy
(if del-backward
(when end-dummy
(goto-char start-dummy)
(when (re-search-backward ac-math-prefix-regexp)
(delete-region (match-beginning 0) end-dummy))
(forward-word 1))
(delete-region start-dummy (point))))))
(defun ac-math-action-unicode ()
(ac-math-action-latex 'backward))
(defun ac-math-latex-math-face-p ()
(let ((face (get-text-property (point) 'face)))
(if (consp face)
(eq (car face) 'font-latex-math-face)
(eq face 'font-latex-math-face))))
(defun ac-math-candidates-latex ()
(when (ac-math-latex-math-face-p)
ac-math-symbols-latex))
(defun ac-math-candidates-unicode ()
(when (or ac-math-unicode-in-math-p
(not (ac-math-latex-math-face-p)))
ac-math-symbols-unicode))
(defun ac-math-prefix ()
"Return the location of the start of the current symbol.
Uses `ac-math-prefix-regexp'."
(when (re-search-backward ac-math-prefix-regexp (point-at-bol) 'no-error)
(match-beginning 1)))
;;;###autoload
(defvar ac-source-latex-commands
'((candidates . math-symbol-list-latex-commands)
(symbol . "c")
(prefix . ac-math-prefix)))
;;;###autoload
(defvar ac-source-math-latex
'((candidates . ac-math-candidates-latex)
(symbol . "l")
(prefix . ac-math-prefix)
(action . ac-math-action-latex)))
;;;###autoload
(defvar ac-source-math-unicode
'((candidates . ac-math-candidates-unicode)
(symbol . "u")
(prefix . ac-math-prefix)
(action . ac-math-action-unicode)))
(provide 'ac-math)
;;; ac-math.el ends here