forked from manateelazycat/thing-edit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thing-edit.el
executable file
·614 lines (543 loc) · 18 KB
/
thing-edit.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
;;; thing-edit.el --- Extension thing edit
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2008, 2009, Andy Stewart, all rights reserved.
;; Copyright (C) 2014, Arthur Miller <[email protected]>, all rights reserved.
;; Created: 2008-06-08 00:42:07
;; Version: 1.2.2
;; Last-Updated: 2014-04-09 23:31:18
;; URL: http://www.emacswiki.org/emacs/download/thing-edit.el
;; Keywords: thingatpt, edit
;; Compatibility: GNU Emacs 23.0.60.1
;; 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:
;;
;; `thingatpt'
;;
;;; Commentary:
;;
;; This package is some useful functions that base on `thingatpt.el'.
;; Those function can copy or paste special data object quickly
;; and don't need to move cursor.
;; Just binding your like keystroke to those functions.
;;
;; thing-paste-sexp paste regular expression around cursor.
;; thing-copy-sexp copy regular expression around cursor.
;;
;; thing-paste-email paste email string around cursor
;; thing-copy-email copy email string around cursor.
;;
;; thing-paste-filename paste filename string around cursor.
;; thing-copy-filename copy filename string around cursor.
;;
;; thing-paste-url paste url string around cursor.
;; thing-copy-url copy url string around cursor.
;;
;; thing-paste-word paste word string around cursor.
;; thing-copy-word copy word string around cursor.
;;
;; thing-paste-symbol paste symbol string around cursor.
;; thing-copy-symbol copy symbol string around cursor.
;;
;; thing-paste-defun paste function string around cursor.
;; thing-copy-defun copy function string around cursor.
;;
;; thing-paste-list paste list string around cursor.
;; thing-copy-list copy list string around cursor.
;;
;; thing-paste-sentence paste sentence string around cursor.
;; thing-copy-sentence copy sentence string around cursor.
;;
;; thing-paste-whitespace paste whitespace string around cursor.
;; thing-copy-whitespace copy whitespace string around cursor.
;;
;; thing-paste-page paste page string around cursor.
;; thing-copy-page copy page string around cursor.
;;
;; thing-paste-line paste current line.
;; thing-copy-line copy current line.
;;
;; thing-paste-to-line-end paste string to end of line.
;; thing-copy-to-line-end copy string to end of line.
;;
;; thing-paste-to-line-beginning paste string to beginning of line.
;; thing-copy-to-line-beginning copy string to beginning of line.
;;
;; thing-paste-comment paste comment.
;; thing-copy-comment copy comment.
;;
;; thing-paste-paragrap paste paragraph around cursor.
;; thing-copy-paragrap copy paragraph around cursor.
;;
;;; Installation:
;;
;; Copy thing-edit.el to your load-path and add to your ~/.emacs
;;
;; (require 'thing-edit)
;;
;; No more need
;;; Change log:
;;
;; 2018/12/20
;; * add `thing-replace-xxx' function. These functions can replace current thing
;; with the content of `kill-ring'.
;;
;; 2014/04/09
;; * Merge Arthur's new functions `thing-copy-paragraph' and `thing-paste-paragraph', thanks!
;; * Merge Arthur's autoload patch, thanks a lot!
;;
;; 2009/01/13
;; * Add many functions.
;;
;; 2009/01/09
;; * Move functions `thing-paste-parentheses' and `thing-copy-parentheses'
;; to file `thing-edit-extension.el', avoid this package depend `paredit'.
;;
;; 2008/09/26
;; * Rebuild program framework, make code more clearly.
;;
;; 2008/07/28
;; * Modified function `lazy-search-mark-object' use paredit's function to
;; select content between parenthesis.
;;
;; 2008/06/19
;; * Modified search method of `thing-copy-parentheses' and `thing-paste-parentheses'.
;;
;; 2008/06/08
;; * Add edit of `url', `filename', `email', `sexp'
;; * And ignore `defun', `page', `whitespace', `list' `sentence'.
;; * Add basal function that apply thingatpt library.
;; * Complete that `word', `symbol', `line', `line-to-beg', `line-to-end'
;; * Comment is not use `thingatpt', but operate is allied.
;;
;;; Acknowledgments:
;;
;; Not yet
;;
;;; TODO
;;
;; None
;;
;;; Require
(require 'thingatpt)
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Customize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgroup thing-edit nil
"Thing edit."
:group 'term)
(defcustom thing-edit-show-message-p t
"Set this option to nil if want thing-edit work silencely.
Default is nil."
:type 'boolean
:group 'thing-edit)
(defun thing-edit-internal (object-beg object-end &optional kill-conditional)
"A fast edit complexes object.
Argument OBJECT-BEG the begin position that object.
Argument OBJECT-END the end position of object.
Optional argument KILL-CONDITIONAL default is do copy handle, if KILL-CONDITIONAL is non-nil do paste handle."
(interactive)
(if kill-conditional
(progn
(if thing-edit-show-message-p
(message "%s pasted." (buffer-substring object-beg object-end)))
(kill-region object-beg object-end))
(if thing-edit-show-message-p
(message "%s copied." (buffer-substring object-beg object-end)))
(kill-ring-save object-beg object-end)))
(defun thing-edit (thing &optional kill-conditional)
"This function is a simple interface for `thing-edit-internal'.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
(save-excursion
(thing-edit-internal (beginning-of-thing thing)
(end-of-thing thing)
kill-conditional)))
(defun thing-replace-internal (object-beg object-end)
"A fast replace complexes object.
Argument OBJECT-BEG the begin position that object.
Argument OBJECT-END the end position of object."
(interactive)
(progn
(goto-char object-beg)
(delete-char (- object-end object-beg))
(yank)))
(defun thing-replace (thing)
"This function is a simple interface for `thing-replace-internal'"
(save-excursion
(thing-replace-internal (beginning-of-thing thing)
(end-of-thing thing))))
;;;###autoload
(defun thing-paste-sexp ()
"Paste regular expression at current point."
(interactive)
(thing-edit 'sexp t))
;;;###autoload
(defun thing-copy-sexp (kill-conditional)
"Copy regular expression at current point.
With the universal argument, the text will also be killed."
(interactive "P")
(if kill-conditional
(thing-edit 'sexp t)
(thing-edit 'sexp)))
;;;###autoload
(defun thing-replace-sexp ()
"Replace regular expression at current point with the content of kill-ring."
(interactive)
(thing-replace 'sexp))
;;;###autoload
(defun thing-paste-email ()
"Paste email at current point."
(interactive)
(thing-edit 'email t))
;;;###autoload
(defun thing-copy-email (kill-conditional)
"Copy email at current point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'email t)
(thing-edit 'email)))
;;;###autoload
(defun thing-replace-email ()
"Replace email at current point with the content kill ring."
(interactive)
(thing-replace 'email))
;;;###autoload
(defun thing-paste-filename ()
"Paste filename at current point."
(interactive)
(thing-edit 'filename t))
;;;###autoload
(defun thing-copy-filename (kill-conditional)
"Copy filename at current point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'filename t)
(thing-edit 'filename)))
;;;###autoload
(defun thing-replace-filename ()
"Replace filename at current point with kill ring."
(interactive)
(thing-replace 'filename))
;;;###autoload
(defun thing-paste-url ()
"Paste url at current point."
(interactive)
(thing-edit 'url t))
;;;###autoload
(defun thing-copy-url (kill-conditional)
"Copy url at current point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'url t)
(thing-edit 'url)))
;;;###autoload
(defun thing-replace-url ()
"Replace url at current point with kill ring."
(interactive)
(thing-replace 'url))
;;;###autoload
(defun thing-paste-word ()
"Paste words at point."
(interactive)
(thing-edit 'word t))
;;;###autoload
(defun thing-copy-word (kill-conditional)
"Copy words at point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'word t)
(thing-edit 'word)))
;;;###autoload
(defun thing-replace-word ()
"Replace words at point with kill ring."
(interactive)
(thing-replace 'word))
;;;###autoload
(defun thing-paste-symbol ()
"Paste symbol around point."
(interactive)
(thing-edit 'symbol t))
;;;###autoload
(defun thing-copy-symbol (kill-conditional)
"Copy symbol around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'symbol t)
(thing-edit 'symbol)))
;;;###autoload
(defun thing-replace-symbol ()
"Replace symbol around point with kill ring."
(interactive)
(thing-replace 'symbol))
;;;###autoload
(defun thing-paste-line ()
"Paste current line into Kill-Ring without mark the line."
(interactive)
(thing-edit 'line t))
;;;###autoload
(defun thing-copy-line (kill-conditional)
"Copy current line into Kill-Ring without mark the line.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'line t)
(thing-edit 'line)))
;;;###autoload
(defun thing-replace-line ()
"Replace current line with kill ring"
(interactive)
(thing-replace 'line))
;;;###autoload
(defun thing-copy-paragraph (kill-conditional)
"Copy current paragraph around the point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'paragraph t)
(thing-edit 'paragraph)))
;;;###autoload
(defun thing-replace-paragraph ()
"Replace current paragraph around the point with the content of kill ring."
(interactive)
(thing-replace 'paragraph))
;;;###autoload
(defun thing-paste-paragraph (&optional kill-conditional)
"Paste current paragraph around the point"
(interactive)
(thing-edit 'paragraph t)
)
;;;###autoload
(defun thing-paste-defun ()
"Paste function around point."
(interactive)
(thing-edit 'defun t))
;;;###autoload
(defun thing-copy-defun (kill-conditional)
"Paste function around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'defun t)
(thing-edit 'defun)))
;;;###autoload
(defun thing-replace-defun ()
"Replace function around point with the content of kill ring."
(interactive)
(thing-replace 'defun))
;;;###autoload
(defun thing-paste-list ()
"Paste list around point."
(interactive)
(thing-edit 'list t))
;;;###autoload
(defun thing-copy-list (kill-conditional)
"Paste list around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'list t)
(thing-edit 'list)))
;;;###autoload
(defun thing-replace-list ()
"Replace list around point with the content of kill ring."
(interactive)
(thing-replace 'list))
;;;###autoload
(defun thing-paste-sentence ()
"Paste sentence around point."
(interactive)
(thing-edit 'sentence t))
;;;###autoload
(defun thing-copy-sentence (kill-conditional)
"Paste sentence around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'sentence t)
(thing-edit 'sentence)))
;;;###autoload
(defun thing-replace-sentence ()
"Replace sentence around point with the content of currnt line."
(interactive)
(thing-replace 'sentence))
;;;###autoload
(defun thing-paste-whitespace ()
"Paste whitespace around point."
(interactive)
(thing-edit 'whitespace t))
;;;###autoload
(defun thing-copy-whitespace (kill-conditional)
"Paste whitespace around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'whitespace t)
(thing-edit 'whitespace)))
;;;###autoload
(defun thing-replace-whitespace ()
"Replace whitespace around point with the content of currnt line."
(interactive)
(thing-replace 'whitespace))
;;;###autoload
(defun thing-paste-page ()
"Paste page around point."
(interactive)
(thing-edit 'page t))
;;;###autoload
(defun thing-copy-page (kill-conditional)
"Paste page around point.
With the universal argument, the text will also be killed"
(interactive "P")
(if kill-conditional
(thing-edit 'page t)
(thing-edit 'page)))
;;;###autoload
(defun thing-replace-page ()
"Replace page around point with the content of currnt line."
(interactive)
(thing-replace 'page))
;; Below function is not base on thingatpt, but it's effect like above function.
;; So i add to this package.
;;;###autoload
(defun thing-paste-to-line-end ()
"Paste content from current point to line end."
(interactive)
(thing-copy-to-line-end t))
;;;###autoload
(defun thing-copy-to-line-end (&optional kill-conditional)
"Copy content from current point to line end.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
(interactive)
(save-excursion
(thing-edit-internal (point)
(line-end-position)
kill-conditional)))
;;;###autoload
(defun thing-paste-to-line-beginning ()
"Paste content from current point to line beginning."
(interactive)
(thing-copy-to-line-beginning t))
;;;###autoload
(defun thing-copy-to-line-beginning (&optional kill-conditional)
"Copy content from current point tot line beginning.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
(interactive)
(save-excursion
(thing-edit-internal (line-beginning-position)
(point)
kill-conditional)))
;;;###autoload
(defun thing-paste-comment ()
"Paste the comment around line.
If mark is active, it can paste all comment that in mark."
(interactive)
(thing-copy-comment t))
;;;###autoload
(defun thing-copy-comment (&optional kill-conditional)
"Copy the comment around line.
If mark is active, it can copy all comment that in mark.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
(interactive)
(let ((beg (line-beginning-position))
(end (line-end-position)))
(when mark-active
(setq beg (region-beginning))
(setq end (region-end))
(deactivate-mark))
(save-excursion
(setq end (copy-marker end))
(goto-char beg)
(while (< (point) end)
(if (comment-search-forward end t)
(if kill-conditional
(call-interactively 'comment-kill)
(call-interactively 'comment-copy))
(goto-char end))))))
(defun thing-paste-parentheses ()
"Paste content in match parentheses."
(interactive)
(thing-copy-parentheses t))
(defun thing-copy-parentheses (kill-conditional)
"Copy content in match parentheses.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
(interactive "P")
(save-excursion
(if (thing-edit-in-string-p)
(thing-edit-internal
(1+ (car (thing-edit-string-start+end-points)))
(cdr (thing-edit-string-start+end-points))
kill-conditional)
(thing-edit-internal
(progn
(backward-up-list)
(forward-char +1)
(point))
(progn
(up-list)
(forward-char -1)
(point))
kill-conditional))))
(defun thing-replace-parentheses ()
"Replace content in match parentheses with the content of currnt line."
(interactive)
(save-excursion
(if (thing-edit-in-string-p)
(thing-replace-internal
(1+ (car (thing-edit-string-start+end-points)))
(cdr (thing-edit-string-start+end-points)))
(thing-replace-internal
(progn
(backward-up-list)
(forward-char +1)
(point))
(progn
(up-list)
(forward-char -1)
(point))))))
(defun thing-edit-in-string-p (&optional state)
(or (nth 3 (or state (thing-edit-current-parse-state)))
(and
(eq (get-text-property (point) 'face) 'font-lock-string-face)
(eq (get-text-property (- (point) 1) 'face) 'font-lock-string-face))
(and
(eq (get-text-property (point) 'face) 'font-lock-doc-face)
(eq (get-text-property (- (point) 1) 'face) 'font-lock-doc-face))
))
(defun thing-edit-string-start+end-points (&optional state)
"Return a cons of the points of open and close quotes of the string.
The string is determined from the parse state STATE, or the parse state
from the beginning of the defun to the point.
This assumes that `thing-edit-in-string-p' has already returned true, i.e.
that the point is already within a string."
(save-excursion
(let ((start (nth 8 (or state (thing-edit-current-parse-state)))))
(goto-char start)
(forward-sexp 1)
(cons start (1- (point))))))
(defun thing-edit-current-parse-state ()
"Return parse state of point from beginning of defun."
(let ((point (point)))
(beginning-of-defun)
(parse-partial-sexp (point) point)))
(provide 'thing-edit)
;;; thing-edit.el ends here