-
Notifications
You must be signed in to change notification settings - Fork 137
/
meow-thing.el
363 lines (297 loc) · 12.6 KB
/
meow-thing.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
;;; meow-thing.el --- Calculate bounds of thing in Meow -*- lexical-binding: t -*-
;; 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
;; 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.
(require 'cl-lib)
(require 'subr-x)
(require 'meow-var)
(require 'meow-util)
(declare-function meow--visual-line-end-position "meow-command")
(declare-function meow--visual-line-beginning-position "meow-command")
(defun meow--bounds-of-symbol ()
(when-let* ((bounds (bounds-of-thing-at-point meow-symbol-thing)))
(let ((beg (car bounds))
(end (cdr bounds)))
(save-mark-and-excursion
(goto-char end)
(if (not (looking-at-p "\\s)"))
(while (looking-at-p " \\|,")
(goto-char (cl-incf end)))
(goto-char beg)
(while (looking-back " \\|," 1)
(goto-char (cl-decf beg))))
(cons beg end)))))
(defun meow--bounds-of-string-1 ()
"Return the bounds of the string under the cursor.
The thing `string' is not available in Emacs 27.'"
(if (version< emacs-version "28")
(when (meow--in-string-p)
(let (beg end)
(save-mark-and-excursion
(while (meow--in-string-p)
(backward-char 1))
(setq beg (point)))
(save-mark-and-excursion
(while (meow--in-string-p)
(forward-char 1))
(setq end (point)))
(cons beg end)))
(bounds-of-thing-at-point 'string)))
(defun meow--inner-of-symbol ()
(bounds-of-thing-at-point meow-symbol-thing))
(defun meow--bounds-of-string (&optional inner)
(when-let* ((bounds (meow--bounds-of-string-1)))
(let ((beg (car bounds))
(end (cdr bounds)))
(cons
(save-mark-and-excursion
(goto-char beg)
(funcall (if inner #'skip-syntax-forward #'skip-syntax-backward) "\"|")
(point))
(save-mark-and-excursion
(goto-char end)
(funcall (if inner #'skip-syntax-backward #'skip-syntax-forward) "\"|")
(point))))))
(defun meow--inner-of-string ()
(meow--bounds-of-string t))
(defun meow--inner-of-window ()
(cons (window-start) (window-end)))
(defun meow--inner-of-line ()
(cons (save-mark-and-excursion (back-to-indentation) (point))
(line-end-position)))
(defun meow--inner-of-visual-line ()
(cons (meow--visual-line-beginning-position)
(meow--visual-line-end-position)))
;;; Registry
(defvar meow--thing-registry nil
"Thing registry.
This is a plist mapping from thing to (inner-fn . bounds-fn).
Both inner-fn and bounds-fn returns a cons of (start . end) for that thing.")
(defun meow--thing-register (thing inner-fn bounds-fn)
"Register INNER-FN and BOUNDS-FN to a THING."
(setq meow--thing-registry
(plist-put meow--thing-registry
thing
(cons inner-fn bounds-fn))))
(defun meow--thing-syntax-function (syntax)
(cons
(save-mark-and-excursion
(when (use-region-p)
(goto-char (region-beginning)))
(skip-syntax-backward (cdr syntax))
(point))
(save-mark-and-excursion
(when (use-region-p)
(goto-char (region-end)))
(skip-syntax-forward (cdr syntax))
(point))))
(defun meow--thing-regexp-function (b-re f-re near)
(let ((beg (save-mark-and-excursion
(when (use-region-p)
(goto-char (region-beginning)))
(when (re-search-backward b-re nil t)
(if near (match-end 0) (point)))))
(end (save-mark-and-excursion
(when (use-region-p)
(goto-char (region-end)))
(when (re-search-forward f-re nil t)
(if near (match-beginning 0) (point))))))
(when (and beg end)
(cons beg end))))
(defun meow--thing-parse-pair-search (push-token pop-token back near)
(let* ((search-fn (if back #'re-search-backward #'re-search-forward))
(match-fn (if back #'match-end #'match-beginning))
(cmp-fn (if back #'> #'<))
(push-next-pos nil)
(pop-next-pos nil)
(push-pos (save-mark-and-excursion
(when (funcall search-fn push-token nil t)
(setq push-next-pos (point))
(if near (funcall match-fn 0) (point)))))
(pop-pos (save-mark-and-excursion
(when (funcall search-fn pop-token nil t)
(setq pop-next-pos (point))
(if near (funcall match-fn 0) (point))))))
(cond
((and (not pop-pos) (not push-pos))
nil)
((not pop-pos)
(goto-char push-next-pos)
(cons 'push push-pos))
((not push-pos)
(goto-char pop-next-pos)
(cons 'pop pop-pos))
((funcall cmp-fn push-pos pop-pos)
(goto-char push-next-pos)
(cons 'push push-pos))
(t
(goto-char pop-next-pos)
(cons 'pop pop-pos)))))
(defun meow--thing-pair-function (push-token pop-token near)
(let* ((found nil)
(depth 0)
(beg (save-mark-and-excursion
(prog1
(let ((case-fold-search nil))
(while (and (<= depth 0)
(setq found (meow--thing-parse-pair-search push-token pop-token t near)))
(let ((push-or-pop (car found)))
(if (eq 'push push-or-pop)
(cl-incf depth)
(cl-decf depth))))
(when (> depth 0) (cdr found)))
(setq depth 0
found nil))))
(end (save-mark-and-excursion
(let ((case-fold-search nil))
(while (and (>= depth 0)
(setq found (meow--thing-parse-pair-search push-token pop-token nil near)))
(let ((push-or-pop (car found)))
(if (eq 'push push-or-pop)
(cl-incf depth)
(cl-decf depth))))
(when (< depth 0) (cdr found))))))
(when (and beg end)
(cons beg end))))
(defun meow--thing-make-syntax-function (x)
(lambda () (meow--thing-syntax-function x)))
(defun meow--thing-make-regexp-function (x near)
(let* ((b-re (cadr x))
(f-re (caddr x)))
(lambda () (meow--thing-regexp-function b-re f-re near))))
(defun meow--thing-make-pair-function (x near)
(let* ((push-token (let ((tokens (cadr x)))
(string-join (mapcar #'regexp-quote tokens) "\\|")))
(pop-token (let ((tokens (caddr x)))
(string-join (mapcar #'regexp-quote tokens) "\\|"))))
(lambda () (meow--thing-pair-function push-token pop-token near))))
(defun meow--thing-make-pair-regexp-function (x near)
(let* ((push-token (let ((tokens (cadr x)))
(string-join tokens "\\|")))
(pop-token (let ((tokens (caddr x)))
(string-join tokens "\\|"))))
(lambda () (meow--thing-pair-function push-token pop-token near))))
(defun meow--thing-parse-multi (xs near)
(let ((chained-fns (mapcar (lambda (x) (meow--thing-parse x near)) xs)))
(lambda ()
(let ((fns chained-fns)
ret)
(while (and fns (not ret))
(setq ret (funcall (car fns))
fns (cdr fns)))
ret))))
(defun meow--thing-parse (x near)
(cond
((functionp x)
x)
((symbolp x)
(lambda () (bounds-of-thing-at-point x)))
((equal 'syntax (car x))
(meow--thing-make-syntax-function x))
((equal 'regexp (car x))
(meow--thing-make-regexp-function x near))
((equal 'pair (car x))
(meow--thing-make-pair-function x near))
((equal 'pair-regexp (car x))
(meow--thing-make-pair-regexp-function x near))
((listp x)
(meow--thing-parse-multi x near))
(t
(lambda ()
(message "Meow: THING definition broken")
(cons (point) (point))))))
(defun meow-thing-register (thing inner bounds)
"Register a THING with INNER and BOUNDS.
Argument THING should be symbol, which specified in `meow-char-thing-table'.
Argument INNER and BOUNDS support following expressions:
EXPR ::= FUNCTION | SYMBOL | SYNTAX-EXPR | REGEXP-EXPR
| PAIRED-EXPR | MULTI-EXPR
SYNTAX-EXPR ::= (syntax . STRING)
REGEXP-EXPR ::= (regexp STRING STRING)
PAIRED-EXPR ::= (pair TOKENS TOKENS)
PAIRED-REGEXP-EXPR ::= (pair-regexp TOKENS-REGEXP TOKENS-REGEXP)
MULTI-EXPR ::= (EXPR ...)
TOKENS ::= (STRING ...)
FUNCTION is a function receives no arguments, return a cons which
the car is the beginning of thing, and the cdr is the end of
thing.
SYMBOL is a symbol represent a builtin thing.
Example: url
(meow-thing-register \\='url \\='url \\='url)
SYNTAX-EXPR contains a syntax description used by `skip-syntax-forward'
Example: non-whitespaces
(meow-thing-register \\='non-whitespace
\\='(syntax . \"^-\")
\\='(syntax . \"^-\"))
You can find the description for syntax in current buffer with
\\[describe-syntax].
REGEXP-EXPR contains two regexps, the first is used for
beginning, the second is used for end. For inner/beginning/end
function, the point of near end of match will be used. For
bounds function, the point of far end of match will be used.
Example: quoted
(meow-thing-register \\='quoted
\\='(regexp \"\\=`\" \"\\=`\\\\|\\='\")
\\='(regexp \"\\=`\" \"\\=`\\\\|\\='\"))
PAIR-EXPR contains two string token lists. The tokens in first
list are used for finding beginning, the tokens in second list
are used for finding end. A depth variable will be used while
searching, thus only matched pair will be found.
Example: do/end block
(meow-thing-register \\='do/end
\\='(pair (\"do\") (\"end\"))
\\='(pair (\"do\") (\"end\")))
PAIR-REGEXP-EXPR contains two regexp lists. The regexp in first
list are used for finding beginning, the regexp in second list
are used for finding end. A depth variable will be used while
searching, thus only matched pair will be found.
Example: The inner block of `{}` will ignore newlines and spaces
after \\='{\\=' before \\='}\\='.
(meow-thing-register \\='code-block
\\='(pair-regexp (\"{[\\n\\t ]*\") (\"[\\n\\t ]*}\") )
\\='(pair (\"{\") (\"}\")))"
(let ((inner-fn (meow--thing-parse inner t))
(bounds-fn (meow--thing-parse bounds nil)))
(meow--thing-register thing inner-fn bounds-fn)))
(meow-thing-register 'round
'(pair ("(") (")"))
'(pair ("(") (")")))
(meow-thing-register 'square
'(pair ("[") ("]"))
'(pair ("[") ("]")))
(meow-thing-register 'curly
'(pair ("{") ("}"))
'(pair ("{") ("}")))
(meow-thing-register 'paragraph 'paragraph 'paragraph)
(meow-thing-register 'sentence 'sentence 'sentence)
(meow-thing-register 'buffer 'buffer 'buffer)
(meow-thing-register 'defun 'defun 'defun)
(meow-thing-register meow-symbol-thing #'meow--inner-of-symbol #'meow--bounds-of-symbol)
(meow-thing-register 'string #'meow--inner-of-string #'meow--bounds-of-string)
(meow-thing-register 'window #'meow--inner-of-window #'meow--inner-of-window)
(meow-thing-register 'line #'meow--inner-of-line 'line)
(meow-thing-register 'visual-line #'meow--inner-of-visual-line #'meow--inner-of-visual-line)
(defun meow--parse-inner-of-thing-char (ch)
(when-let* ((ch-to-thing (assoc ch meow-char-thing-table)))
(meow--parse-range-of-thing (cdr ch-to-thing) t)))
(defun meow--parse-bounds-of-thing-char (ch)
(when-let* ((ch-to-thing (assoc ch meow-char-thing-table)))
(meow--parse-range-of-thing (cdr ch-to-thing) nil)))
(defun meow--parse-range-of-thing (thing inner)
"Parse either inner or bounds of THING. If INNER is non-nil then parse inner."
(when-let* ((bounds-fn-pair (plist-get meow--thing-registry thing)))
(if inner
(funcall (car bounds-fn-pair))
(funcall (cdr bounds-fn-pair)))))
(provide 'meow-thing)
;;; meow-thing.el ends here