forked from manateelazycat/awesome-pair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawesome-pair.el
1474 lines (1348 loc) · 49.4 KB
/
awesome-pair.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; awesome-pair.el --- Auto parenthesis pairing with syntax table
;; Filename: awesome-pair.el
;; Description: Auto parenthesis pairing with syntax table
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2018-11-11 09:27:58
;; Version: 3.4
;; Last-Updated: 2020-02-27 13:29:54
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/awesome-pair.el
;; Keywords:
;; Compatibility: GNU Emacs 27.0.50
;;
;; Features that might be required by this library:
;;
;; `subr-x' `thingatpt'
;;
;;; 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, 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.
;;; Commentary:
;;
;; Auto parenthesis pairing with syntax table.
;;
;; I'm a big fans of paredit.el, I have used the paredit.el more than ten years.
;; But paredit.el not very good for web programming, so I think it's time to write my own plugin.
;;
;; Thanks Taylor R. Campbell, you surprise me how wonderful Emacs is.
;;
;;; Installation:
;;
;; Put awesome-pair.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'awesome-pair)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET awesome-pair RET
;;
;;; Change log:
;;
;; 2020/02/27
;; * Make `awesome-pair-wrap-round' and `awesome-pair-equal' works with script area of html file.
;;
;; 2019/12/23
;; * Re-implement `awesome-pair-in-attribute-p' and fix bug of kill line in web-mode.
;;
;; 2019/08/25
;; * Jump to internal parenthesis start position.
;;
;; 2019/08/23
;; * Fix #28 "Unbalanced parentheses" error.
;;
;; 2019/08/20
;; * Fix #23 "Unbalanced parentheses" error
;;
;; 2019/08/18
;; * Don't kill rest string if cursor position at end tag before.
;;
;; 2019/08/10
;; * Try to kill element if cursor in element area.
;;
;; 2019/07/20
;; * Don't test unbalance parentheses when press `awesome-pair-close-round' in markdown-mode.
;;
;; 2019/07/17
;; * Rewrite `awesome-pair-missing-close', make `awesome-pair-fix-unbalanced-parentheses' can fix unbalance parentheses automatically.
;;
;; 2019/06/28
;; * Make `awesome-pair-in-comment-p' work with `web-mode-comment-face'.
;;
;; 2019/06/25
;; * Improve `awesome-pair-wrap-round' that only wrap Tag in Vue template area.
;;
;; 2019/06/10
;; * Fix `awesome-pair-web-mode-element-wrap' wrap area when region is active.
;;
;; 2019/06/04
;; * Improve `awesome-pair-web-mode-element-wrap' when wrap region area that don't need do return operation after warp tag.
;;
;; 2019/05/28
;; * Fix `awesome-pair-kill' error when kill string in *.vue file.
;;
;; 2019/05/14
;; * When edit *.vue file, just insert double quote after equal when point in template area.
;;
;; 2019/05/10
;; * Add `thingatpt' depend.
;;
;; 2019/03/29
;; * Insert a closing parenthesis in the string of the JS file.
;;
;; 2019/03/20
;; * Don't insert quote after equal if cursor in curly parenthesis.
;;
;; 2019/03/16
;; * Make `awesome-pair-wrap-double-quote' and `awesome-pair-unwrap' support web-mode.
;; * Add new command `awesome-pair-space'.
;;
;; 2019/03/12
;; * Add new command `awesome-pair-equal'.
;;
;; 2019/02/27
;; * Don't insert \" in string that wrap by `...` when current mode is golang.
;;
;; 2019/02/09
;; * Insert ) directly in sh-mode for case ... in syntax.
;;
;; 2019/02/07
;; * Don't insert \ before " if cursor at comment area.
;; * Fix `integer-or-marker-p' error when run `awesome-pair-*-delete-in-string' functions in Python language.
;;
;; 2019/01/30
;; * Fix 'wrong type character-p' error when call `awesome-pair-forward-delete' in beginning of buffer.
;; * Add docs of `awesome-pair-forward-delete'.
;;
;; 2019/01/29
;; * Fixed bug where `awesome-pair-jump-out-pair-and-newline' function did not clean unnecessary whitespaces sometimes.
;;
;; 2019/01/09
;; * Just indent parent expression after unwrap pair when in lisp like language.
;;
;; 2018/12/27
;; * Just clean unnecessary whitespace before close parenthesis when in lisp like language.
;;
;; 2018/12/09
;; * Fix bug of `awesome-pair-in-string-p' when cursor at left side of string.
;;
;; 2018/12/02
;; * Use `get-text-property' improve algorithm of `awesome-pair-in-string-p' and `awesome-pair-in-commit-p'
;;
;; 2018/11/30
;; * Fix `awesome-pair-kill-line-in-string' won't work with golang string.
;;
;; 2018/11/23
;; * Make `awesome-pair-kill-line-in-string' support single quote string.
;;
;; 2018/11/11
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'subr-x)
(require 'thingatpt)
;;; Code:
(defvar awesome-pair-mode-map (make-sparse-keymap)
"Keymap for the awesome-pair minor mode.")
(define-minor-mode awesome-pair-mode
"Minor mode for auto parenthesis pairing with syntax table.
\\<awesome-pair-mode-map>"
)
(defmacro awesome-pair-ignore-errors (body)
`(ignore-errors
,body
t))
;;;;;;;;;;;;;;;;; Interactive functions ;;;;;;;;;;;;;;;;;;;;;;
(defun awesome-pair-open-round ()
(interactive)
(cond
((region-active-p)
(awesome-pair-wrap-round))
((and (awesome-pair-in-string-p)
(derived-mode-p 'js-mode))
(insert "()")
(backward-char))
((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert "("))
(t
(insert "()")
(backward-char))
))
(defun awesome-pair-open-curly ()
(interactive)
(cond
((region-active-p)
(awesome-pair-wrap-curly))
((and (awesome-pair-in-string-p)
(derived-mode-p 'js-mode))
(insert "{}")
(backward-char))
((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert "{"))
(t
(cond ((derived-mode-p 'ruby-mode)
(insert "{ }")
(backward-char 2))
(t
(insert "{}")
(backward-char)))
)
))
(defun awesome-pair-open-bracket ()
(interactive)
(cond
((region-active-p)
(awesome-pair-wrap-bracket))
((and (awesome-pair-in-string-p)
(derived-mode-p 'js-mode))
(insert "[]")
(backward-char))
((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert "["))
(t
(insert "[]")
(backward-char))
))
(defun awesome-pair-fix-unbalanced-parentheses ()
(interactive)
(let ((close (awesome-pair-missing-close)))
(if close
(cond ((eq ?\) (matching-paren close))
(insert ")"))
((eq ?\} (matching-paren close))
(insert "}"))
((eq ?\] (matching-paren close))
(insert "]")))
(up-list))))
(defun awesome-pair-close-round ()
(interactive)
(cond ((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert ")"))
;; Insert ) directly in sh-mode for case ... in syntax.
((or
(derived-mode-p 'sh-mode)
(derived-mode-p 'markdown-mode))
(insert ")"))
(t
(awesome-pair-fix-unbalanced-parentheses))))
(defun awesome-pair-close-curly ()
(interactive)
(cond ((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert "}"))
(t
(awesome-pair-fix-unbalanced-parentheses))))
(defun awesome-pair-close-bracket ()
(interactive)
(cond ((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(insert "]"))
(t
(awesome-pair-fix-unbalanced-parentheses))))
(defun awesome-pair-double-quote ()
(interactive)
(cond ((region-active-p)
(awesome-pair-wrap-double-quote))
((awesome-pair-in-string-p)
(cond
((and (derived-mode-p 'python-mode)
(and (eq (char-before) ?\") (eq (char-after) ?\")))
(insert "\"\"")
(backward-char))
;; When current mode is golang.
;; Don't insert \" in string that wrap by `...`
((and (derived-mode-p 'go-mode)
(equal (save-excursion (nth 3 (awesome-pair-current-parse-state))) 96))
(insert "\""))
(t
(insert "\\\""))))
((awesome-pair-in-comment-p)
(insert "\""))
(t
(insert "\"\"")
(backward-char))
))
(defun awesome-pair-space (arg)
"Wrap space around cursor if cursor in blank parenthesis.
input: {|} (press <SPACE> at |)
output: { | }
input: [|] (press <SPACE> at |)
output: [ | ]
"
(interactive "p")
(if (> arg 1)
(self-insert-command arg)
(cond ((or (awesome-pair-in-comment-p)
(awesome-pair-in-string-p))
(insert " "))
((or (and (equal (char-after) ?\} )
(equal (char-before) ?\{ ))
(and (equal (char-after) ?\] )
(equal (char-before) ?\[ )))
(insert " ")
(backward-char 1))
(t
(insert " ")))))
(defun awesome-pair-match-paren (arg)
"Go to the matching parenthesis if on parenthesis, otherwise insert %."
(interactive "p")
(cond ((or (awesome-pair-in-comment-p)
(awesome-pair-in-string-p))
(self-insert-command (or arg 1)))
((looking-at "\\s\(\\|\\s\{\\|\\s\[")
(forward-list))
((looking-back "\\s\)\\|\\s\}\\|\\s\\]")
(backward-list))
(t
(cond
;; Enhancement the automatic jump of web-mode.
((derived-mode-p 'web-mode)
(awesome-pair-web-mode-match-paren))
(t
(self-insert-command (or arg 1))))
)))
(defun awesome-pair-web-mode-match-paren ()
(require 'sgml-mode)
(cond ((looking-at "<")
(sgml-skip-tag-forward 1))
((looking-back ">")
(sgml-skip-tag-backward 1))
(t (self-insert-command (or arg 1)))))
(defun awesome-pair-backward-delete ()
(interactive)
(cond ((awesome-pair-in-string-p)
(awesome-pair-backward-delete-in-string))
((awesome-pair-in-comment-p)
(backward-delete-char 1))
((awesome-pair-after-close-pair-p)
(if (and (derived-mode-p 'sh-mode)
(eq ?\) (char-before)))
(delete-char -1)
(awesome-pair-backward-movein-or-delete-close-pair)))
((awesome-pair-in-empty-pair-p)
(awesome-pair-backward-delete-in-pair))
((not (awesome-pair-after-open-pair-p))
(backward-delete-char 1))
))
(defun awesome-pair-forward-delete ()
(interactive)
(cond ((awesome-pair-in-string-p)
(awesome-pair-forward-delete-in-string))
((awesome-pair-in-comment-p)
(delete-char 1))
((awesome-pair-before-open-pair-p)
(awesome-pair-forward-movein-or-delete-open-pair))
((awesome-pair-in-empty-pair-p)
(awesome-pair-backward-delete-in-pair))
((and (derived-mode-p 'sh-mode)
(awesome-pair-before-close-pair-p)
(eq ?\) (char-after)))
(delete-char 1))
((not (awesome-pair-before-close-pair-p))
(delete-char 1))
))
(defun awesome-pair-kill ()
"Intelligent soft kill.
When inside of code, kill forward S-expressions on the line, but respecting delimeters.
When in a string, kill to the end of the string.
When in comment, kill to the end of the line."
(interactive)
(cond ((derived-mode-p 'web-mode)
(awesome-pair-web-mode-kill))
((derived-mode-p 'ruby-mode)
(awesome-pair-ruby-mode-kill))
(t
(awesome-pair-common-mode-kill))))
(defun awesome-pair-backward-kill ()
"Intelligent soft kill.
When inside of code, kill backward S-expressions on the line, but respecting delimiters.
When in a string, kill to the beginning of the string.
When in comment, kill to the beginning of the line."
(interactive)
(cond ((derived-mode-p 'web-mode)
(awesome-pair-web-mode-backward-kill))
((derived-mode-p 'ruby-mode)
(awesome-pair-ruby-mode-backward-kill))
(t
(awesome-pair-common-mode-backward-kill))))
(defun awesome-pair-wrap-round ()
(interactive)
(cond
;; If in *.Vue file
;; In template area, call `awesome-pair-web-mode-element-wrap'
;; Otherwise, call `awesome-pair-wrap-round-pair'
((and (buffer-file-name) (string-equal (file-name-extension (buffer-file-name)) "vue"))
(if (awesome-pair-vue-in-template-area)
(awesome-pair-web-mode-element-wrap)
(awesome-pair-wrap-round-pair)))
;; If is `web-mode' but not in *.Vue file, call `awesome-pair-web-mode-element-wrap'
((derived-mode-p 'web-mode)
(if (awesome-pair-in-script-area)
(awesome-pair-wrap-round-pair)
(awesome-pair-web-mode-element-wrap)))
;; Otherwise call `awesome-pair-wrap-round-pair'
(t
(awesome-pair-wrap-round-pair))
))
(defun awesome-pair-wrap-round-pair ()
(cond ((region-active-p)
(awesome-pair-wrap-region "(" ")"))
((awesome-pair-in-string-p)
(let ((string-bound (awesome-pair-string-start+end-points)))
(awesome-pair-wrap (car string-bound) (1+ (cdr string-bound))
"(" ")")))
((awesome-pair-in-comment-p)
(awesome-pair-wrap (beginning-of-thing 'symbol) (end-of-thing 'symbol)
"(" ")"))
(t
(awesome-pair-wrap (beginning-of-thing 'sexp) (end-of-thing 'sexp)
"(" ")")))
;; Indent wrap area.
(awesome-pair-indent-parenthesis-area)
;; Jump to internal parenthesis start position.
(up-list)
(awesome-pair-match-paren 1)
(forward-char)
)
(defun awesome-pair-wrap-bracket ()
(interactive)
(cond ((region-active-p)
(awesome-pair-wrap-region "[" "]"))
((awesome-pair-in-string-p)
(let ((string-bound (awesome-pair-string-start+end-points)))
(awesome-pair-wrap (car string-bound) (1+ (cdr string-bound))
"[" "]")))
((awesome-pair-in-comment-p)
(awesome-pair-wrap (beginning-of-thing 'symbol) (end-of-thing 'symbol)
"[" "]"))
(t
(awesome-pair-wrap (beginning-of-thing 'sexp) (end-of-thing 'sexp)
"[" "]")))
;; Indent wrap area.
(awesome-pair-indent-parenthesis-area)
;; Jump to internal parenthesis start position.
(up-list)
(awesome-pair-match-paren 1)
(forward-char))
(defun awesome-pair-wrap-curly ()
(interactive)
(cond ((region-active-p)
(awesome-pair-wrap-region "{" "}"))
((awesome-pair-in-string-p)
(let ((string-bound (awesome-pair-string-start+end-points)))
(awesome-pair-wrap (car string-bound) (1+ (cdr string-bound))
"{" "}")))
((awesome-pair-in-comment-p)
(awesome-pair-wrap (beginning-of-thing 'symbol) (end-of-thing 'symbol)
"{" "}"))
(t
(awesome-pair-wrap (beginning-of-thing 'sexp) (end-of-thing 'sexp)
"{" "}")))
;; Forward to jump in parenthesis.
(forward-char))
(defun awesome-pair-wrap-double-quote ()
(interactive)
(cond ((and (region-active-p)
(awesome-pair-in-string-p))
(cond ((and (derived-mode-p 'go-mode)
(equal (save-excursion (nth 3 (awesome-pair-current-parse-state))) 96))
(awesome-pair-wrap-region "\"" "\""))
(t
(awesome-pair-wrap-region "\\\"" "\\\""))))
((region-active-p)
(awesome-pair-wrap-region "\"" "\""))
((awesome-pair-in-string-p)
(goto-char (1+ (cdr (awesome-pair-string-start+end-points)))))
((awesome-pair-in-comment-p)
(awesome-pair-wrap (beginning-of-thing 'symbol) (end-of-thing 'symbol)
"\"" "\""))
(t
(awesome-pair-wrap (beginning-of-thing 'sexp) (end-of-thing 'sexp)
"\"" "\"")))
;; Forward to jump in parenthesis.
(forward-char))
(defun awesome-pair-unwrap (&optional argument)
(interactive "P")
(cond ((derived-mode-p 'web-mode)
(awesome-pair-web-mode-element-unwrap))
((awesome-pair-in-string-p)
(awesome-pair-splice-string argument))
(t
(save-excursion
(awesome-pair-kill-surrounding-sexps-for-splice argument)
(backward-up-list)
(save-excursion
(forward-sexp)
(backward-delete-char 1))
(delete-char 1)
;; Try to indent parent expression after unwrap pair.
;; This feature just enable in lisp-like language.
(when (or
(derived-mode-p 'lisp-mode)
(derived-mode-p 'emacs-lisp-mode))
(ignore-errors
(backward-up-list)
(indent-sexp)))))))
(defun awesome-pair-jump-out-pair-and-newline ()
(interactive)
(cond ((awesome-pair-in-string-p)
(goto-char (1+ (cdr (awesome-pair-string-start+end-points))))
(newline-and-indent))
(t
;; Just do when have `up-list' in next step.
(if (awesome-pair-ignore-errors (save-excursion (up-list)))
(let (up-list-point)
(if (awesome-pair-is-blank-line-p)
;; Clean current line first if current line is blank line.
(awesome-pair-kill-current-line)
;; Move out of current parentheses and newline.
(up-list)
(setq up-list-point (point))
(newline-and-indent)
;; Try to clean unnecessary whitespace before close parenthesis.
;; This feature just enable in lisp-like language.
(when (or
(derived-mode-p 'lisp-mode)
(derived-mode-p 'emacs-lisp-mode))
(save-excursion
(goto-char up-list-point)
(backward-char)
(when (awesome-pair-only-whitespaces-before-cursor-p)
(awesome-pair-delete-whitespace-around-cursor))))))
;; Try to clean blank line if no pair can jump out.
(if (awesome-pair-is-blank-line-p)
(awesome-pair-kill-current-line))))))
(defun awesome-pair-jump-left ()
"To left of previous match parentheses."
(interactive)
(cond
;; Jump out of string if cursor in string area.
((awesome-pair-in-string-p)
(goto-char (car (awesome-pair-string-start+end-points))))
;; Jump to previous pair.
(t
(backward-char 1)
(while (not (looking-at "\\(['\"<({]\\|[[]\\)")) (backward-char 1)))))
(defun awesome-pair-jump-right ()
"To right of next match parentheses."
(interactive)
(cond
;; Jump out of string if cursor in string area.
((awesome-pair-in-string-p)
(goto-char (+ (cdr (awesome-pair-string-start+end-points)) 1)))
;; Jump to next pair.
(t
(while (not (looking-at "\\(['\">)}]\\|]\\)")) (forward-char 1))
(forward-char 1))))
(defun awesome-pair-delete-whitespace-before-cursor ()
(kill-region (save-excursion
(search-backward-regexp "[^ \t\n]" nil t)
(forward-char)
(point))
(point)))
(defun awesome-pair-delete-whitespace-around-cursor ()
(kill-region (save-excursion
(search-backward-regexp "[^ \t\n]" nil t)
(forward-char)
(point))
(save-excursion
(search-forward-regexp "[^ \t\n]" nil t)
(backward-char)
(point))))
(defun awesome-pair-kill-current-line ()
(kill-region (beginning-of-thing 'line) (end-of-thing 'line))
(back-to-indentation))
(defun awesome-pair-missing-close ()
(let ((start-point (point))
open)
(save-excursion
;; Get open tag.
(backward-up-list)
(setq open (char-after))
;; Jump to start position and use `check-parens' check unbalance paren.
(goto-char start-point)
(ignore-errors
(check-parens))
;; Return missing tag if point change after `check-parens'
;; Otherwhere return nil.
(if (equal start-point (point))
nil
open)
)))
(defun awesome-pair-backward-delete-in-pair ()
(backward-delete-char 1)
(delete-char 1))
(defun awesome-pair-backward-movein-or-delete-close-pair ()
(if (awesome-pair-ignore-errors (save-excursion (backward-sexp)))
(backward-char)
(backward-delete-char 1)))
(defun awesome-pair-forward-movein-or-delete-open-pair ()
(if (awesome-pair-ignore-errors (save-excursion (forward-sexp)))
(forward-char)
(delete-char 1)))
(defun awesome-pair-backward-delete-in-string ()
(let ((start+end (awesome-pair-string-start+end-points)))
(cond
;; Some language, such as Python, `awesome-pair-string-start+end-points' will return nil cause by `beginning-of-defun' retun nil.
;; This logical branch is handle this.
((not start+end)
;; First determine if it is in the string area?
(when (awesome-pair-in-string-p)
(let ((syn-before (char-syntax (char-before)))
(syn-after (char-syntax (char-after))))
(cond
;; Remove double quotes when the string is empty
((and (eq syn-before ?\" )
(eq syn-after ?\" ))
(backward-delete-char 1)
(delete-char 1))
;; If there is still content in the string and the double quotation marks are in front of the cursor,
;; no delete operation is performed.
((eq syn-before ?\" ))
;; If the cursor is not double quotes before and after, delete the previous character.
(t
(backward-delete-char 1))))))
((not (eq (1- (point)) (car start+end)))
(if (awesome-pair-in-string-escape-p)
(delete-char 1))
(backward-delete-char 1)
(if (awesome-pair-in-string-escape-p)
(backward-delete-char 1)))
((eq (point) (cdr start+end))
(backward-delete-char 1)
(delete-char 1)))))
(defun awesome-pair-forward-delete-in-string ()
(let ((start+end (awesome-pair-string-start+end-points)))
(cond
;; Some language, such as Python, `awesome-pair-string-start+end-points' will return nil cause by `beginning-of-defun' retun nil.
;; This logical branch is handle this.
((not start+end)
;; First determine if it is in the string area?
(when (awesome-pair-in-string-p)
(let ((syn-before (char-syntax (char-before)))
(syn-after (char-syntax (char-after))))
(cond
;; Remove double quotes when the string is empty
((and (eq syn-before ?\" )
(eq syn-after ?\" ))
(backward-delete-char 1)
(delete-char 1))
;; If there is still content in the string and the double quotation marks are after of the cursor,
;; no delete operation is performed.
((eq syn-after ?\" ))
;; If the cursor is not double quotes before and after, delete the previous character.
(t
(delete-char 1))))))
((not (eq (point) (cdr start+end)))
(cond ((awesome-pair-in-string-escape-p)
(delete-char -1))
((eq (char-after) ?\\ )
(delete-char +1)))
(delete-char +1))
((eq (1- (point)) (car start+end))
(delete-char -1)
(delete-char +1)))))
(defun awesome-pair-splice-string (argument)
(let ((original-point (point))
(start+end (awesome-pair-string-start+end-points)))
(let ((start (car start+end))
(end (cdr start+end)))
(let* ((escaped-string
(cond ((not (consp argument))
(buffer-substring (1+ start) end))
((= 4 (car argument))
(buffer-substring original-point end))
(t
(buffer-substring (1+ start) original-point))))
(unescaped-string
(awesome-pair-unescape-string escaped-string)))
(if (not unescaped-string)
(error "Unspliceable string.")
(save-excursion
(goto-char start)
(delete-region start (1+ end))
(insert unescaped-string))
(if (not (and (consp argument)
(= 4 (car argument))))
(goto-char (- original-point 1))))))))
(defun awesome-pair-point-at-sexp-start ()
(save-excursion
(forward-sexp)
(backward-sexp)
(point)))
(defun awesome-pair-point-at-sexp-end ()
(save-excursion
(backward-sexp)
(forward-sexp)
(point)))
(defun awesome-pair-point-at-sexp-boundary (n)
(cond ((< n 0) (awesome-pair-point-at-sexp-start))
((= n 0) (point))
((> n 0) (awesome-pair-point-at-sexp-end))))
(defun awesome-pair-kill-surrounding-sexps-for-splice (argument)
(cond ((or (awesome-pair-in-string-p)
(awesome-pair-in-comment-p))
(error "Invalid context for splicing S-expressions."))
((or (not argument) (eq argument 0)) nil)
((or (numberp argument) (eq argument '-))
(let* ((argument (if (eq argument '-) -1 argument))
(saved (awesome-pair-point-at-sexp-boundary (- argument))))
(goto-char saved)
(ignore-errors (backward-sexp argument))
(awesome-pair-hack-kill-region saved (point))))
((consp argument)
(let ((v (car argument)))
(if (= v 4)
(let ((end (point)))
(ignore-errors
(while (not (bobp))
(backward-sexp)))
(awesome-pair-hack-kill-region (point) end))
(let ((beginning (point)))
(ignore-errors
(while (not (eobp))
(forward-sexp)))
(awesome-pair-hack-kill-region beginning (point))))))
(t (error "Bizarre prefix argument `%s'." argument))))
(defun awesome-pair-unescape-string (string)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (and (not (eobp))
(search-forward "\\" nil t))
(delete-char -1)
(forward-char))
(condition-case condition
(progn (check-parens) (buffer-string))
(error nil))))
(defun awesome-pair-hack-kill-region (start end)
(let ((this-command nil)
(last-command nil))
(kill-region start end)))
(defun awesome-pair-kill-internal ()
(cond (current-prefix-arg
(kill-line (if (integerp current-prefix-arg)
current-prefix-arg
1)))
((awesome-pair-in-string-p)
(awesome-pair-kill-line-in-string))
((awesome-pair-in-single-quote-string-p)
(awesome-pair-kill-line-in-single-quote-string))
((or (awesome-pair-in-comment-p)
(save-excursion
(awesome-pair-skip-whitespace t (point-at-eol))
(or (eq (char-after) ?\; )
(eolp))))
(kill-line))
(t (awesome-pair-kill-sexps-on-line))))
(defun awesome-pair-backward-kill-internal ()
(cond (current-prefix-arg
(kill-line (if (integerp current-prefix-arg)
current-prefix-arg
1)))
((awesome-pair-in-string-p)
(awesome-pair-kill-line-backward-in-string))
((awesome-pair-in-single-quote-string-p)
(awesome-pair-kill-line-backward-in-single-quote-string))
((or (awesome-pair-in-comment-p)
(save-excursion
(awesome-pair-skip-whitespace nil (point-at-bol))
(bolp)))
(if (bolp) (awesome-pair-backward-delete)
(kill-line 0)))
(t (awesome-pair-kill-sexps-backward-on-line))))
(defun awesome-pair-kill-line-in-single-quote-string ()
(let ((sexp-end (save-excursion
(forward-sexp)
(backward-char)
(point))))
(kill-region (point) sexp-end)))
(defun awesome-pair-kill-line-backward-in-single-quote-string ()
(let ((sexp-beg (save-excursion
(backward-sexp)
(forward-char)
(point))))
(kill-region sexp-beg (point))))
(defun awesome-pair-kill-line-in-string ()
(cond ((save-excursion
(awesome-pair-skip-whitespace t (point-at-eol))
(eolp))
(kill-line))
(t
(save-excursion
(if (awesome-pair-in-string-escape-p)
(backward-char))
(let ((beginning (point)))
(while (save-excursion
(forward-char)
(awesome-pair-in-string-p))
(forward-char))
(kill-region beginning (point)))
))))
(defun awesome-pair-kill-line-backward-in-string ()
(cond ((save-excursion
(awesome-pair-skip-whitespace nil (point-at-bol))
(bolp))
(kill-line))
(t
(save-excursion
(if (awesome-pair-in-string-escape-p)
(forward-char))
(let ((beginning (point)))
(while (save-excursion
(backward-char)
(awesome-pair-in-string-p))
(backward-char))
(kill-region (point) beginning))
))))
(defun awesome-pair-skip-whitespace (trailing-p &optional limit)
(funcall (if trailing-p 'skip-chars-forward 'skip-chars-backward)
" \t\n"
limit))
(defun awesome-pair-kill-sexps-on-line ()
(if (awesome-pair-in-char-p)
(backward-char 2))
(let ((beginning (point))
(eol (point-at-eol)))
(let ((end-of-list-p (awesome-pair-forward-sexps-to-kill beginning eol)))
(if end-of-list-p (progn (up-list) (backward-char)))
(if kill-whole-line
(awesome-pair-kill-sexps-on-whole-line beginning)
(kill-region beginning
(if (and (not end-of-list-p)
(eq (point-at-eol) eol))
eol
(point)))))))
(defun awesome-pair-kill-sexps-backward-on-line ()
(if (awesome-pair-in-char-p)
(forward-char 1))
(let ((beginning (point))
(bol (point-at-bol)))
(let ((beg-of-list-p (awesome-pair-backward-sexps-to-kill beginning bol)))
(if beg-of-list-p (progn (up-list -1) (forward-char)))
(if kill-whole-line
(awesome-pair-kill-sexps-on-whole-line beginning)
(kill-region (if (and (not beg-of-list-p)
(eq (point-at-bol) bol))
bol
(point))
beginning)))))
(defun awesome-pair-forward-sexps-to-kill (beginning eol)
(let ((end-of-list-p nil)
(firstp t))
(catch 'return
(while t
(if (and kill-whole-line (eobp)) (throw 'return nil))
(save-excursion
(unless (awesome-pair-ignore-errors (forward-sexp))
(if (awesome-pair-ignore-errors (up-list))
(progn
(setq end-of-list-p (eq (point-at-eol) eol))
(throw 'return nil))))
(if (or (and (not firstp)
(not kill-whole-line)
(eobp))
(not (awesome-pair-ignore-errors (backward-sexp)))
(not (eq (point-at-eol) eol)))
(throw 'return nil)))
(forward-sexp)
(if (and firstp
(not kill-whole-line)
(eobp))
(throw 'return nil))
(setq firstp nil)))
end-of-list-p))
(defun awesome-pair-backward-sexps-to-kill (beginning bol)
(let ((beg-of-list-p nil)
(lastp t))
(catch 'return
(while t
(if (and kill-whole-line (bobp)) (throw 'return nil))
(save-excursion
(unless (awesome-pair-ignore-errors (backward-sexp))
(if (awesome-pair-ignore-errors (up-list -1))
(progn
(setq beg-of-list-p (eq (point-at-bol) bol))
(throw 'return nil))))
(if (or (and (not lastp)
(not kill-whole-line)
(bobp))
(not (awesome-pair-ignore-errors (forward-sexp)))
(not (eq (point-at-bol) bol)))
(throw 'return nil)))
(backward-sexp)
(if (and lastp
(not kill-whole-line)
(bobp))
(throw 'return nil))
(setq lastp nil)))
beg-of-list-p))
(defun awesome-pair-kill-sexps-on-whole-line (beginning)
(kill-region beginning
(or (save-excursion
(awesome-pair-skip-whitespace t)
(and (not (eq (char-after) ?\; ))
(point)))
(point-at-eol)))
(cond ((save-excursion (awesome-pair-skip-whitespace nil (point-at-bol))
(bolp))
(lisp-indent-line))
((eobp) nil)
((let ((syn-before (char-syntax (char-before)))