-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr-font-lock.el
234 lines (209 loc) · 8.95 KB
/
r-font-lock.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
;;; r-font-lock.el --- Font lock support R mode -*- lexical-binding: t; -*-
;;
;; Copyright © 2016 Vitalie Spinu, Lionel Henry
;; Author: Vitalie Spinu <[email protected]>
;; Created: 3 Jul 2016
;;
;; This file is not part of GNU Emacs.
;;
;;; License:
;;
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;;
;;; Code:
(defgroup r-font-lock nil
"Font Lock customization used in r-mode."
:group 'r
:prefix "r-")
(defvar r-font-lock-available-features
'((comments "Comments")
(strings "Strings")
(backquotes "Backquoted Vars")
(modifiers "Modifiers" :keyword r-keyword:modifiers)
(keywords "Keywords" :keyword r-keyword:keywords)
(constants "Constants" :keyword r-keyword:constants)
(F&T "F & T" :keyword r-keyword:F&T)
(fun-calls "Function Calls" :keyword r-keyword:fun-calls)
(fun-defs "Function Definitions" :keyword r-keyword:fun-defs)
(ops-assign "Assign Operators" :keyword r-keyword:assign-ops)
(ops-other "Operators" :keyword r-keyword:operators)
(ops-= "=" :keyword r-keyword:=)
(%op% "%op%")
(numbers "Numbers" :keyword r-keyword:numbers)
(delimiters "Delimiters ()[]{}" :keyword r-keyword:delimiters))
"An alist of available font-lock features for the R mode.
Each element of the alist is a list of the form (FEATURE NAME
ACTIVE [REST ...]). FEATURE is a symbol naming the feature. NAME
is the human visible name of the features (for use in completion
and menus). ACTIVE is a Boolean value indicating if the feature
is active or not. TYPE is the type of the feature, currently
either nil for plain options and :keyword for Font Lock
keywords. The optional REST list is type specific. For :keyword
type, it consists of the variable name that contains the
respective font lock keyword.")
(defcustom r-font-lock-features
'(comments strings modifiers keywords constants F&T fun-calls fun-defs ops-assign %op%)
"Active font lock features in R mode."
:group 'r-font-lock
:type `(set ,@(cl-loop for (k v) in r-font-lock-available-features
collect `(const :tag ,v ,k))))
(defvar-local r-font-lock-keywords nil
"Currently active font lock keywords in R mode buffers.")
(defvar r-backquoted-face 'r-backquoted-face)
(defface r-backquoted-face
'((default (:inherit font-lock-variable-name-face)))
"Font Lock face for backquoted names."
:group 'r-font-lock)
(defvar r-operator-face 'r-operator-face)
(defface r-operator-face
'((default (:inherit font-lock-type-face)))
"Font Lock face used to highlight operators in r-mode buffers."
:group 'r-font-lock)
(defvar r-function-call-face 'r-function-call-face)
(defface r-function-call-face
'((default (:slant normal :inherit font-lock-function-name-face)))
"Font Lock face used to highlight function calls in ess buffers."
:group 'r-font-lock)
(defvar r-numbers-face 'r-numbers-face)
(defface r-numbers-face
'((default (:slant normal :inherit font-lock-type-face)))
"Font Lock face used to highlight numbers in R mode buffers."
:group 'r-font-lock)
(defvar r-delimiters-face 'r-delimiters-face)
(defface r-delimiters-face
'((default (:slant normal :inherit font-lock-type-face)))
"Font Lock face used to highlight numbers in R mode buffers."
:group 'r-font-lock)
(defvar r-keyword:keywords
(let ((keywords '("while" "for" "in" "repeat" "if" "else" "switch" "break" "next" "function"
"return" "message" "warning" "stop")))
(cons (concat "\\<" (regexp-opt keywords 'enc-paren) "\\>") 'font-lock-keyword-face)))
(defvar r-keyword:modifiers
(let ((modifyiers '("library" "attach" "detach" "source" "require")))
(cons (concat "\\<" (regexp-opt modifyiers 'enc-paren) "\\>") 'font-lock-builtin-face))
"Font Lock keyword for R modifiers")
(defvar r-keyword:constants
(let ((consts '("TRUE" "FALSE" "NA" "NULL" "Inf" "NaN"
"NA_integer_" "NA_real_" "NA_complex_" "NA_character_")))
(cons (concat "\\<" (regexp-opt consts 'enc-paren) "\\>") 'font-lock-constant-face))
"Font Lock constants keyword.")
(defvar r-keyword:F&T
(cons "\\b[FT]\\b" 'font-lock-constant-face)
"Font Lock keyword for T and F symbols in R mode.")
(defvar r-keyword:fun-calls
(cons "\\(\\sw+\\)[\t ]*(" '(1 'r-function-call-face keep))
"Font Lock for R's function calls.")
(defvar r-keyword:fun-defs
(let ((fun-regexp "\\(\\sw+\\)[ \t]*\\(<-\\)[ \t\n]*function\\b"))
(cons fun-regexp '(1 font-lock-function-name-face nil)))
"Font Lock keyword for R function definitions.")
(defvar r-keyword:assign-ops
(let ((ops '("<<-" "<-" "->" "->>")))
(cons (regexp-opt ops) 'font-lock-keyword-face))
"Font Lock keywords for R assign operators")
(defvar r-keyword:operators
(cons "[-=+></%]+" 'r-operator-face)
"Font Lock keyword for R's operators.")
(defvar r-keyword:=
(cons "=" 'r-operator-face)
"Font Lock keyword for = in R mode.")
(defvar r-keyword:numbers
(cons "\\b[0-9]*[.eE]?[0-9]+[eEL]?\\b" 'r-numbers-face)
"Font Lock for R's numbers.")
(defvar r-keyword:delimiters
(cons "\\s(\\|\\s)" 'r-delimiters-face)
"Font Lock keyword for R's parenthesis.")
(defun r--extract-fl-keywords ()
"Return active font lock keywords.
Use active features listed in `r-font-lock-keywords' to extract
keywords from `r-font-lock-available-features'."
(cl-loop for (name _ type sym) in r-font-lock-available-features
when (and (eq type :keyword) (memq name r-font-lock-features))
collect (symbol-value sym)))
(defun r--toggle-fl-keyword (name)
(interactive)
(if (memq name r-font-lock-features)
(setq r-font-lock-features (delq name r-font-lock-features))
(push name r-font-lock-features))
;; refresh font-lock defaults in all necessary buffers
(let ((mode major-mode)
(kwds (r--extract-fl-keywords)))
(mapc (lambda (b)
(with-current-buffer b
(when (eq major-mode mode)
(setq r-font-lock-keywords kwds)
(font-lock-refresh-defaults))))
(buffer-list))))
(defun r--generate-fl-submenu (menu)
"Generate font-lock submenu."
(append (cl-loop for (name desc-name) in r-font-lock-available-features
collect `[,desc-name
(lambda () (interactive)
(r--toggle-fl-keyword ',name))
:style toggle
:enable t
:selected ,(not (null (memq name r-font-lock-features)))])
(list "-----"
["Save to custom" (lambda () (interactive)
(customize-save-variable 'r-font-lock-features
r-font-lock-features))
t])))
(defun r-font-lock-syntactic-face-function (state)
"Function used as `font-lock-syntactic-face-function'.
See `font-lock-syntactic-face-function' for the definition of STATE."
(let ((string-end (save-excursion
(and (nth 3 state)
(r-goto-char (nth 8 state))
(r-forward-sexp)
(point)))))
(when (eq (nth 3 state) ?`)
(put-text-property (nth 8 state) string-end 'ess-r-backquoted t))
(cond
((eq (nth 3 state) ?%)
(if (memq '%op% r-font-lock-features)
'font-lock-keyword-face
'default))
((save-excursion
(and (r-goto-char string-end)
(r-looking-at "<-")
(r-goto-char (match-end 0))
(r-looking-at "function\\b")))
(if (memq 'fun-defs r-font-lock-features)
'font-lock-function-name-face
'default))
((save-excursion
(and (r-goto-char string-end)
(r-looking-at "(")))
(if (memq 'fun-calls r-font-lock-features)
'ess-function-call-face
'default))
((eq (nth 3 state) ?`)
(if (memq 'backquotes r-font-lock-features)
'r-backquoted-face
'default))
((nth 3 state)
'font-lock-string-face
(if (memq 'strings r-font-lock-features)
'font-lock-string-face
'default))
(t
(if (memq 'comments r-font-lock-features)
'font-lock-comment-face
'default)))))
(provide 'r-font-lock)
;;; r-font-lock.el ends here