-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththingopt.el
More file actions
275 lines (239 loc) · 8.63 KB
/
thingopt.el
File metadata and controls
275 lines (239 loc) · 8.63 KB
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
;;; thingopt.el --- Thing at Point optional utilities
;; Copyright (C) 2008-2015 Tomohiro Matsuyama
;; Author: Tomohiro Matsuyama <m2ym.pub@gmail.com>
;; Keywords: convenience
;; 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:
;; TODO documentation
;; TODO forward-string by syntax (?)
;;; Code:
(eval-when-compile
(require 'cl))
(require 'thingatpt)
(defvar thing-list-cache nil)
(defvar upward-mark-thing-index 0)
(defvar upward-mark-thing-trial 0)
(defvar upward-mark-thing-original-position)
(defvar upward-mark-thing-list '(string symbol (up-list . *))
"List of types of things to mark for `upward-mark-thing'.")
(defvar upward-isearch-thing-list
'(email url word symbol string filename)
"Like `upward-mark-thing-list', but for `upward-isearch-thing'.")
(defun thingp (thing)
(or (get thing 'bounds-of-thing-at-point)
(get thing 'forward-op)
(get thing 'beginning-op)
(get thing 'end-op)
(fboundp (intern-soft (concat "forward-" (symbol-name thing))))))
(defun list-thing ()
(let (things)
(mapatoms
(lambda (atom)
(if (thingp atom)
(push atom things))))
things))
(defun read-thing ()
(or thing-list-cache
(setq thing-list-cache (list-thing)))
(completing-read "Thing: " (mapcar 'list thing-list-cache)
nil nil nil nil "sexp"))
;;;###autoload
(defun kill-thing (thing)
(interactive (list (read-thing)))
(if (stringp thing)
(setq thing (intern thing)))
(let ((bounds (bounds-of-thing-at-point thing)))
(if bounds
(kill-region (car bounds) (cdr bounds)))))
;;;###autoload
(defun copy-thing (thing)
(interactive (list (read-thing)))
(if (stringp thing)
(setq thing (intern thing)))
(let ((bounds (bounds-of-thing-at-point thing)))
(if bounds
(copy-region-as-kill (car bounds) (cdr bounds)))))
;;;###autoload
(defun mark-thing (thing)
(interactive (list (read-thing)))
(if (stringp thing)
(setq thing (intern thing)))
(let ((bounds (bounds-of-thing-at-point thing)))
(when bounds
(goto-char (car bounds))
(push-mark (cdr bounds) nil transient-mark-mode)
(setq deactivate-mark nil))))
(defun reset-upward-bounds-of-thing ()
"Helper function for `upward-bounds-of-thing'."
(setq upward-bounds-of-thing-index 0
upward-bounds-of-thing-trial 0
upward-bounds-of-thing-original-position (point)
upward-bounds-of-thing-prev nil))
(defun upward-bounds-of-thing (list-of-things)
"Finds the location of the ends of a thing. LIST-OF-THINGS is
a list of types of things. Repeatedly calling this will take
successive types from the list and return the bounds of that
thing at point."
(interactive)
(let ((len (length list-of-things))
(index upward-bounds-of-thing-index)
bounds)
(while (and (null bounds)
(< index len))
(let ((thing (nth index list-of-things))
(limit '*))
(if (consp thing)
(setq limit (cdr thing)
thing (car thing)))
(setq bounds (bounds-of-thing-at-point thing))
(if (not (or (null bounds)
(and (not (eq limit '*)) (>= upward-bounds-of-thing-trial limit))
(eq (car bounds) (cdr bounds))
(and upward-bounds-of-thing-prev
(equal bounds upward-bounds-of-thing-prev))))
(setq upward-bounds-of-thing-name (format "%s" (symbol-name thing)))
(setq bounds nil
index (mod (1+ index) len)
upward-bounds-of-thing-index (mod (1+ upward-bounds-of-thing-index) len)
upward-bounds-of-thing-trial 0)
(goto-char upward-bounds-of-thing-original-position))))
(when bounds
(setq upward-bounds-of-thing-trial (1+ upward-bounds-of-thing-trial))
(setq upward-bounds-of-thing-prev bounds)
bounds)))
;;;###autoload
(defun upward-mark-thing ()
"Marks the first type of thing in `upward-mark-thing-list' at
point. When called successively, it marks successive types of
things in `upward-mark-thing-list'. It is recommended to put
smaller things (e.g. word, symbol) before larger
things (e.g. list, paragraph) in `upward-mark-thing-list'. When
this is called enough times to get to the end of the list, it
wraps back to the first type of thing."
(interactive)
(unless (eq last-command this-command)
(reset-upward-bounds-of-thing))
(let ((bounds (upward-bounds-of-thing upward-mark-thing-list)))
(when bounds
(message "%s" upward-bounds-of-thing-name)
(goto-char (car bounds))
(push-mark (cdr bounds) t 'activate)
(setq deactivate-mark nil))))
;;;###autoload
(defun upward-isearch-thing ()
"Much like `upward-mark-thing', but adds THING to the isearch string.
This should be invoked while isearch is active. Clobbers the current isearch string."
(interactive)
(unless (eq last-command this-command)
(reset-upward-bounds-of-thing))
(let ((bounds (upward-bounds-of-thing upward-isearch-thing-list)))
(when bounds
(setq isearch-initial-string (buffer-substring-no-properties
(car bounds) (cdr bounds)))
(setq isearch-string isearch-initial-string
isearch-message isearch-initial-string)
(isearch-update)
(isearch-highlight (car bounds) (cdr bounds)))))
(defun define-thing-commands ()
(dolist (thing (list-thing))
(dolist (op '(mark kill copy))
(let ((symbol (intern (format "%s-%s" op thing))))
(if (and (fboundp symbol)
(not (get symbol 'thingopt)))
(setq symbol (intern (format "%s-%s*" op thing))))
(put symbol 'thingopt t)
(fset symbol `(lambda () (interactive) (,(intern (format "%s-thing" op)) ',thing)))))))
(defvar kill-thing-map
'((?w . word)
(?e . sexp)
(?s . symbol)
(?t . sentence)
(?p . paragraph)
(?f . defun)
(?F . filename)
(?l . list)
(?L . up-list)
(?S . string)
(?U . url)
(?P . page)))
(defun kill-region-dwim-1 (function)
(if (and transient-mark-mode mark-active)
(call-interactively function)
(let* ((c (read-char))
(thing (assoc-default c kill-thing-map))
(bounds (if thing (bounds-of-thing-at-point thing))))
(cond
(bounds
(funcall function (car bounds) (cdr bounds))
(message "Saved %s." thing))
(thing
(message "There is no %s here." thing))
(t
(message "Nothing here."))))))
;;;###autoload
(defun kill-region-dwim ()
(interactive)
(kill-region-dwim-1 #'kill-region))
;;;###autoload
(defun kill-ring-save-dwim ()
(interactive)
(kill-region-dwim-1 #'kill-ring-save))
(defun string-face-p (face)
(let (result)
(or (consp face)
(setq face (list face)))
(while (and face (null result))
(if (memq (car face) '(font-lock-string-face font-lock-doc-face))
(setq result t)
(setq face (cdr face))))
result))
(defun forward-string (&optional arg)
(interactive "p")
(if (null arg)
(setq arg 1))
(ignore-errors
(cond
((> arg 0)
(dotimes (i arg)
(while (and (re-search-forward "\\s\"")
(string-face-p (get-text-property (point) 'face))))))
((< arg 0)
(dotimes (i (- arg))
(while (and (re-search-backward "\\s\"")
(string-face-p (get-text-property (1- (point)) 'face)))))))))
(defun backward-string (&optional arg)
(interactive "p")
(forward-string (- (or arg 1))))
(defun bounds-of-up-list-at-point ()
(ignore-errors
(save-excursion
(let ((pos (scan-lists (point) -1 1)))
(goto-char pos)
(forward-list)
(cons pos (point))))))
(put 'up-list 'bounds-of-thing-at-point
(symbol-function 'bounds-of-up-list-at-point))
(defun forward-defun (&optional arg)
(interactive "p")
(if (null arg)
(setq arg 1))
(ignore-errors
(cond
((< arg 0)
(beginning-of-defun (- arg)))
((> arg 0)
(end-of-defun arg)))))
(defun backward-defun (&optional arg)
(interactive "p")
(forward-defun (- (or arg 1))))
(provide 'thingopt)
;;; thingopt.el ends here