-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
smartparens.el
9998 lines (8589 loc) · 392 KB
/
smartparens.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
;;; smartparens.el --- Automatic insertion, wrapping and paredit-like navigation with user defined pairs. -*- lexical-binding: t -*-
;; Copyright (C) 2012-2023 Matus Goljer
;; Author: Matus Goljer <[email protected]>
;; Maintainer: Matus Goljer <[email protected]>
;; Created: 17 Nov 2012
;; Version: 1.11.0
;; Keywords: abbrev convenience editing
;; URL: https://github.com/Fuco1/smartparens
;; This file is not part of GNU Emacs.
;;; License:
;; This file is part of Smartparens.
;; Smartparens 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.
;; Smartparens 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 Smartparens. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Smartparens is minor mode for Emacs that deals with parens pairs
;; and tries to be smart about it. It started as a unification effort
;; to combine functionality of several existing packages in a single,
;; compatible and extensible way to deal with parentheses, delimiters,
;; tags and the like. Some of these packages include autopair,
;; textmate, wrap-region, electric-pair-mode, paredit and others. With
;; the basic features found in other packages it also brings many
;; improvements as well as completely new features.
;; For a basic overview, see github readme at
;; https://github.com/Fuco1/smartparens
;; For the complete documentation visit the documentation wiki located
;; at https://github.com/Fuco1/smartparens/wiki
;; If you like this project, you can donate here:
;; https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CEYP5YVHDRX8C
;;; Code:
(eval-when-compile
(require 'subr-x) ; for `string-trim'
(require 'cl-lib))
(require 'dash)
(require 'thingatpt)
(require 'help-mode) ;; for help-xref-following #85
(require 'loadhist)
(declare-function cua-replace-region "cua-base") ; FIXME: remove this when we drop support for old emacs
(declare-function cua-delete-region "cua-base")
(declare-function cua--fallback "cua-base")
(declare-function package-version-join "package")
(declare-function package-desc-version "package")
(declare-function subword-kill "subword")
(declare-function subword-forward "subword")
(declare-function subword-backward "subword")
(declare-function hungry-delete-backward "hungry-delete")
(declare-function hungry-delete-forward "hungry-delete")
(declare-function evil-get-register "evil-common")
(declare-function evil-set-register "evil-common")
(eval-when-compile
(defvar evil-this-register)
(defvar package-alist)
(defvar sp-autoskip-closing-pair)
(defvar sp-show-enclosing-pair-commands)
(defvar show-smartparens-mode))
;;; backport for older emacsen
;; introduced in 24.3
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
;; Can't use backquote here, it's too early in the bootstrap.
(list 'progn (list 'defvar var val docstring)
(list 'make-variable-buffer-local (list 'quote var)))))
;;;###autoload
(defun sp-cheat-sheet (&optional arg)
"Generate a cheat sheet of all the smartparens interactive functions.
Without a prefix argument, print only the short documentation and examples.
With non-nil prefix argument ARG, show the full documentation for each function.
You can follow the links to the function or variable help page.
To get back to the full list, use \\[help-go-back].
You can use `beginning-of-defun' and `end-of-defun' to jump to
the previous/next entry.
Examples are fontified using the `font-lock-string-face' for
better orientation."
(interactive "P")
(setq arg (not arg))
(let ((do-not-display '(
smartparens-mode
smartparens-global-mode
turn-on-smartparens-mode
turn-off-smartparens-mode
sp-wrap-cancel
sp-remove-active-pair-overlay
sp-splice-sexp-killing-around ;; is aliased to `sp-raise-sexp'
show-smartparens-mode
show-smartparens-global-mode
turn-on-show-smartparens-mode
turn-off-show-smartparens-mode
))
(do-not-display-with-arg '(
sp-use-paredit-bindings
sp-use-smartparens-bindings
))
(commands (cl-loop for i in (cdr (feature-symbols 'smartparens))
if (and (eq (car-safe i) 'defun) (commandp (cdr i)))
collect (cdr i))))
(with-current-buffer (get-buffer-create "*Smartparens cheat sheet*")
(let ((standard-output (current-buffer))
(help-xref-following t))
(read-only-mode -1)
(erase-buffer)
(help-mode)
(smartparens-mode 1)
(help-setup-xref (list #'sp-cheat-sheet)
(called-interactively-p 'interactive))
(read-only-mode -1)
(--each (--remove (or (memq it do-not-display)
(and arg (memq it do-not-display-with-arg)))
commands)
(unless (equal (symbol-name it) "advice-compilation")
(let ((start (point)) kill-from)
(insert (propertize (symbol-name it) 'face 'font-lock-function-name-face))
(insert " is ")
(describe-function-1 it)
(save-excursion
(when arg
(goto-char start)
(forward-paragraph 1)
(forward-line 1)
(if (looking-at "^It is bound")
(forward-paragraph 2)
(forward-paragraph 1))
(setq kill-from (point))
(when (re-search-forward "^Examples:" nil t)
(delete-region kill-from
(save-excursion
(forward-line 1)
(point))))))
(insert (propertize (concat
"\n\n"
(make-string 72 ?―)
"\n\n") 'face 'font-lock-function-name-face)))))
(goto-char (point-min))
(while (re-search-forward "\\(->\\|\\)" nil t)
(let ((thing (bounds-of-thing-at-point 'line)))
(put-text-property (car thing) (cdr thing) 'face 'font-lock-string-face)))
(goto-char (point-min))
(while (re-search-forward "|" nil t)
(put-text-property (1- (point)) (point) 'face 'font-lock-warning-face))
(goto-char (point-min))
(while (re-search-forward "^It is bound to \\(.*?\\)\\." nil t)
(put-text-property (match-beginning 1) (match-end 1) 'face 'font-lock-keyword-face))
(goto-char (point-min))
(while (re-search-forward ";;.*?$" nil t)
(put-text-property (match-beginning 0) (match-end 0) 'face 'font-lock-comment-face))
(help-make-xrefs)
(goto-char (point-min))))
(pop-to-buffer "*Smartparens cheat sheet*")))
(defun sp-describe-system (starterkit)
"Describe user's system.
The output of this function can be used in bug reports."
(interactive
(list (completing-read "Starterkit/Distribution used: "
(list
"Spacemacs"
"Evil"
"Vanilla"
))))
(let ((text (format "- `smartparens` version: %s
- Active `major-mode`: `%s`
- Smartparens strict mode: %s
- Emacs version (`M-x emacs-version`): %s
- Starterkit/Distribution: %s
- OS: %s"
(--if-let (cadr (assoc 'smartparens package-alist))
(package-version-join (package-desc-version it))
"<Please specify manually>")
(symbol-name major-mode)
(bound-and-true-p smartparens-strict-mode)
(replace-regexp-in-string "\n" "" (emacs-version))
starterkit
(symbol-name system-type))))
(pop-to-buffer
(with-current-buffer (get-buffer-create "*sp-describe-system*")
(erase-buffer)
(insert "The content of the buffer underneath the line was
copied to your clipboard. You can also edit it in this buffer
and then copy the results manually.
------------------------------------------------
")
(insert text)
(current-buffer)))
(kill-new text)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Variables
(defvar-local sp-forward-bound-fn nil
"Function to restrict the forward search")
(defvar-local sp-backward-bound-fn nil
"Function to restrict the backward search")
(defun sp--get-forward-bound ()
"Get the bound to limit the forward search for looking for pairs.
If it returns nil, the original bound passed to the search
function will be considered."
(and sp-forward-bound-fn (funcall sp-forward-bound-fn)))
(defun sp--get-backward-bound ()
"Get the bound to limit the backward search for looking for pairs.
If it returns nil, the original bound passed to the search
function will be considered."
(and sp-backward-bound-fn (funcall sp-backward-bound-fn)))
(defvaralias 'sp-keymap 'smartparens-mode-map)
(make-obsolete-variable 'sp-keymap 'smartparens-mode-map "2015-01-01")
;;;###autoload
(defvar smartparens-mode-map (make-sparse-keymap)
"Keymap used for `smartparens-mode'.")
(defvar sp-paredit-bindings '(
("C-M-f" . sp-forward-sexp) ;; navigation
("C-M-b" . sp-backward-sexp)
("C-M-u" . sp-backward-up-sexp)
("C-M-d" . sp-down-sexp)
("C-M-p" . sp-backward-down-sexp)
("C-M-n" . sp-up-sexp)
("M-s" . sp-splice-sexp) ;; depth-changing commands
("M-<up>" . sp-splice-sexp-killing-backward)
("M-<down>" . sp-splice-sexp-killing-forward)
("M-r" . sp-splice-sexp-killing-around)
("M-(" . sp-wrap-round)
("C-)" . sp-forward-slurp-sexp) ;; barf/slurp
("C-<right>" . sp-forward-slurp-sexp)
("C-}" . sp-forward-barf-sexp)
("C-<left>" . sp-forward-barf-sexp)
("C-(" . sp-backward-slurp-sexp)
("C-M-<left>" . sp-backward-slurp-sexp)
("C-{" . sp-backward-barf-sexp)
("C-M-<right>" . sp-backward-barf-sexp)
("M-S" . sp-split-sexp) ;; misc
("M-j" . sp-join-sexp)
("M-?" . sp-convolute-sexp)
)
"Paredit inspired bindings.
Alist containing the default paredit bindings to corresponding
smartparens functions.")
(defun sp--populate-keymap (bindings)
"Populates the `smartparens-mode-map' from the BINDINGS alist."
(--each bindings
(define-key smartparens-mode-map (read-kbd-macro (car it)) (cdr it))))
;;;###autoload
(defun sp-use-paredit-bindings ()
"Initiate `smartparens-mode-map' with `sp-paredit-bindings'."
(interactive)
(sp--populate-keymap sp-paredit-bindings))
(defvar sp-smartparens-bindings '(
("C-M-f" . sp-forward-sexp)
("C-M-b" . sp-backward-sexp)
("C-M-d" . sp-down-sexp)
("C-M-a" . sp-backward-down-sexp)
("C-S-d" . sp-beginning-of-sexp)
("C-S-a" . sp-end-of-sexp)
("C-M-e" . sp-up-sexp)
("C-M-u" . sp-backward-up-sexp)
("C-M-n" . sp-next-sexp)
("C-M-p" . sp-previous-sexp)
("C-M-k" . sp-kill-sexp)
("C-M-w" . sp-copy-sexp)
("M-<delete>" . sp-unwrap-sexp)
("M-<backspace>" . sp-backward-unwrap-sexp)
("C-<right>" . sp-forward-slurp-sexp)
("C-<left>" . sp-forward-barf-sexp)
("C-M-<left>" . sp-backward-slurp-sexp)
("C-M-<right>" . sp-backward-barf-sexp)
("M-D" . sp-splice-sexp)
("C-M-<delete>" . sp-splice-sexp-killing-forward)
("C-M-<backspace>" . sp-splice-sexp-killing-backward)
("C-S-<backspace>" . sp-splice-sexp-killing-around)
("C-]" . sp-select-next-thing-exchange)
("C-M-]" . sp-select-next-thing)
("C-M-SPC" . sp-mark-sexp)
("M-F" . sp-forward-symbol)
("M-B" . sp-backward-symbol)
)
"Alist containing the default smartparens bindings.")
;;;###autoload
(defun sp-use-smartparens-bindings ()
"Initiate `smartparens-mode-map' with `sp-smartparens-bindings'."
(interactive)
(sp--populate-keymap sp-smartparens-bindings))
(defun sp--set-base-key-bindings (&optional symbol value)
"Set up the default keymap based on `sp-base-key-bindings'.
SYMBOL is the symbol being set, that is `sp-base-key-bindings'.
VALUE is the saved value (as a symbol), can be one of:
- sp
- paredit
This function is also used as a setter for this customize value."
(when symbol (set-default symbol value))
(cond
((eq value 'sp)
(sp-use-smartparens-bindings))
((eq value 'paredit)
(sp-use-paredit-bindings))))
(defun sp--update-override-key-bindings (&optional symbol value)
"Override the key bindings with values from `sp-override-key-bindings'.
SYMBOL is `sp-override-key-bindings', VALUE is the value being set.
This function is also used as a setter for this customize value."
(when symbol (set-default symbol value))
;; this also needs to reload the base set, if any is present.
(sp--set-base-key-bindings)
(sp--populate-keymap value))
(defcustom sp-base-key-bindings nil
"A default set of key bindings for commands provided by smartparens.
Paredit binding adds the bindings in `sp-paredit-bindings' to the
corresponding smartparens commands. It does not add bindings to
any other commands, or commands that do not have a paredit
counterpart.
Smartparens binding adds the bindings in
`sp-smartparens-bindings' to most common smartparens commands.
These are somewhat inspired by paredit, but in many cases differ.
Note that neither \"paredit\" nor \"smartparens\" bindings add a
binding for all the provided commands."
:type '(radio
(const :tag "Don't use any default set of bindings" nil)
(const :tag "Use smartparens set of bindings" sp)
(const :tag "Use paredit set of bindings" paredit))
:set 'sp--set-base-key-bindings
:group 'smartparens)
(defcustom sp-override-key-bindings nil
"An alist of bindings and commands that should override the base key set.
If you wish to override a binding from the base set, set the
value for the binding to the `kbd' recognizable string constant
and command to the command symbol you wish to bind there.
If you wish to disable a binding from the base set, set the value
for the command to nil.
Examples:
(\"C-M-f\" . sp-forward-sexp)
(\"C-<right>\" . nil)
See `sp-base-key-bindings'."
:type '(alist
:key-type string
:value-type symbol)
:set 'sp--update-override-key-bindings
:group 'smartparens)
(defvar-local sp-escape-char nil
"Character used to escape quotes inside strings.")
(defvar-local sp-comment-char nil
"Character used to start comments.")
(defvar-local sp-pair-list nil
"List of pairs for autoinsertion or wrapping.
Maximum length of opening or closing pair is
`sp-max-pair-length' characters.")
(defvar-local sp-local-pairs nil
"List of pair definitions used for current buffer.")
(defvar-local sp-last-operation nil
"Symbol holding the last successful operation.")
(cl-defstruct sp-state
"Smartparens state for the current buffer."
;; A "counter" to track delayed hook. When a pair is inserted, a
;; cons of the form (:next . pair) is stored. On the next
;; (immediately after insertion) invocation of post-command-hook, it
;; is changed to (:this . pair). When the `car' is :this, the
;; post-command-hook checks the delayed hooks for `pair' and
;; executes them, then reset the "counter".
delayed-hook
;; TODO
delayed-insertion
;; The last point checked by sp--syntax-ppss and its result, used for
;; memoization
last-syntax-ppss-point ;; a list (point point-min point-max)
last-syntax-ppss-result
;; Value of `sp-pair-list' for this buffer. Note that this might
;; differ from `sp-pair-list' which is often changed by dynamic
;; binding
pair-list
;; Value of `sp-local-pairs' for this buffer. Note that this might
;; differ from `sp-local-pairs' which is often changed by dynamic
;; binding
local-pairs
)
(defvar-local sp-state (make-sp-state)
"Smartparens state for the current buffer.")
;; TODO: get rid of this
(defvar-local sp-previous-point -1
"Location of point before last command.
This is only updated when some pair-overlay is active. Do not
rely on the value of this variable anywhere else!")
;; TODO: get rid of this
(defvar-local sp-wrap-point nil
"Save the value of point before attempt to wrap a region.
Used for restoring the original state if the wrapping is
cancelled.")
;; TODO: get rid of this
(defvar-local sp-wrap-mark nil
"Save the value of mark before attempt to wrap a region.
Used for restoring the original state if the wrapping is
cancelled.")
(defvar-local sp-last-inserted-characters ""
"Characters typed during the wrapping selection.
If wrapping is cancelled, these characters are re-inserted to the
location of point before the wrapping.")
(defvar-local sp-last-inserted-pair nil
"Last inserted pair.")
(defvar-local sp-delayed-pair nil
"The pair whose insertion is being delayed.
The insertion of this pair is delayed to be carried out in
`sp--post-command-hook-handler'. The format is (opening delim
. beg of the opening delim)")
(defvar-local sp-last-wrapped-region nil
"Information about the last wrapped region.
The format is the same as returned by `sp-get-sexp'.")
(defvar sp-point-inside-string nil
"Non-nil if point is inside a string.
Used to remember the state from before `self-insert-command' is
run.")
(defvar sp-buffer-modified-p nil
"Non-nil if buffer was modified before `pre-command-hook'.")
(defvar sp-pre-command-point nil
"Position of `point' before `this-command' gets executed.")
(defconst sp-max-pair-length 10
"Maximum length of an opening or closing delimiter.
Only the pairs defined by `sp-pair' are considered. Tag pairs
can be of any length.")
(defconst sp-max-prefix-length 100
"Maximum length of a pair prefix.
Because prefixes for pairs can be specified using regular
expressions, they can potentially be of arbitrary length. This
settings solves the problem where the parser would decide to
backtrack the entire buffer which would lock up Emacs.")
(defvar sp-pairs
'((t
.
((:open "\\\\(" :close "\\\\)" :actions (insert wrap autoskip navigate))
(:open "\\{" :close "\\}" :actions (insert wrap autoskip navigate))
(:open "\\(" :close "\\)" :actions (insert wrap autoskip navigate))
(:open "\\\"" :close "\\\"" :actions (insert wrap autoskip navigate))
(:open "\"" :close "\""
:actions (insert wrap autoskip navigate escape)
:unless (sp-in-string-quotes-p)
:post-handlers (sp-escape-wrapped-region sp-escape-quotes-after-insert))
(:open "'" :close "'"
:actions (insert wrap autoskip navigate escape)
:unless (sp-in-string-quotes-p sp-point-after-word-p)
:post-handlers (sp-escape-wrapped-region sp-escape-quotes-after-insert))
(:open "(" :close ")" :actions (insert wrap autoskip navigate))
(:open "[" :close "]" :actions (insert wrap autoskip navigate))
(:open "{" :close "}" :actions (insert wrap autoskip navigate))
(:open "`" :close "`" :actions (insert wrap autoskip navigate)))))
"List of pair definitions.
Maximum length of opening or closing pair is
`sp-max-pair-length' characters.")
(defvar sp-tags nil
"List of tag definitions. See `sp-local-tag' for more information.")
(defvar sp-prefix-tag-object nil
"If non-nil, only consider tags while searching for next thing.")
(defvar sp-prefix-pair-object nil
"If non-nil, only consider pairs while searching for next thing.
Pairs are defined as expressions delimited by pairs from
`sp-pair-list'.")
(defvar sp-prefix-symbol-object nil
"If non-nil, only consider symbols while searching for next thing.
Symbol is defined as a chunk of text recognized by
`sp-forward-symbol'.")
(define-obsolete-variable-alias 'sp--lisp-modes 'sp-lisp-modes "2015-11-08")
(defcustom sp-lisp-modes '(
cider-repl-mode
clojure-mode
clojurec-mode
clojurescript-mode
clojurex-mode
clojure-ts-mode
clojurescript-ts-mode
clojurec-ts-mode
common-lisp-mode
emacs-lisp-mode
eshell-mode
fennel-mode
fennel-repl-mode
geiser-repl-mode
gerbil-mode
inf-clojure-mode
inferior-emacs-lisp-mode
inferior-lisp-mode
inferior-scheme-mode
lisp-interaction-mode
lisp-mode
monroe-mode
racket-mode
racket-repl-mode
scheme-interaction-mode
scheme-mode
slime-repl-mode
sly-mrepl-mode
stumpwm-mode
)
"List of Lisp-related modes."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-clojure-modes '(
cider-repl-mode
clojure-mode
clojurec-mode
clojurescript-mode
clojurex-mode
clojure-ts-mode
clojurescript-ts-mode
clojurec-ts-mode
inf-clojure-mode
)
"List of Clojure-related modes."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-c-modes '(
c-mode
c++-mode
c-ts-mode
c++-ts-mode
)
"List of C-related modes."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-no-reindent-after-kill-modes '(
python-mode
python-ts-mode
coffee-mode
asm-mode
makefile-gmake-mode
haml-mode
yaml-mode
)
"List of modes that should not reindent after kill."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-no-reindent-after-kill-indent-line-functions
'(
insert-tab
)
"List of `indent-line-function's that should not reindent after kill."
:type '(repeat symbol)
:group 'smartparens)
(defvar sp--html-modes '(
sgml-mode
html-mode
rhtml-mode
nxhtml-mode
nxml-mode
web-mode
jinja2-mode
html-erb-mode
js-jsx-mode
js2-jsx-mode
rjsx-mode
tsx-ts-mode
)
"List of HTML modes.")
(defvar sp-message-alist
'((:unmatched-expression
"Search failed: there is an unmatched expression somewhere or we are at the beginning/end of file"
"Unmatched expression")
(:unbalanced-region
"Can not kill the region: the buffer would end up in an unbalanced state after deleting the active region"
"Killing the region would make the buffer unbalanced"
"Unbalanced region")
(:delimiter-in-string
"Ignored: opening or closing pair is inside a string or comment and matching pair is outside (or vice versa)")
(:no-matching-tag
"Search failed: no matching tag found"
"No matching tag")
(:invalid-context-prev
"Invalid context: previous h-sexp ends after the next one"
"Invalid context")
(:invalid-context-cur
"Invalid context: current h-sexp starts after the next one"
"Invalid context")
(:no-structure-found
"Previous sexp starts after current h-sexp or no structure was found"
"No valid structure found")
(:invalid-structure
"Ignored: this operation would result in invalid structure"
"Ignored because of invalid structure")
(:cant-slurp
"Ignored: we can not slurp without breaking strictly balanced expression"
"Can not slurp without breaking balance")
(:cant-slurp-context
"Ignored: we can not slurp into different context (comment -> code)"
"Can not slurp into different context")
(:cant-insert-closing-delimiter
"We can not insert unbalanced closing delimiter in strict mode"
"Can not insert unbalanced delimiter")
(:blank-sexp
"Point is in blank sexp, nothing to barf"
"Point is in blank sexp")
(:point-not-deep-enough
"Point has to be at least two levels deep to swap the enclosing delimiters"
"Point has to be at least two levels deep"
"Point not deep enough")
(:different-type
"The expressions to be joined are of different type"
"Expressions are of different type"))
"List of predefined messages to be displayed by `sp-message'.
Each element is a list consisting of a keyword and one or more
strings, which are chosen based on the `sp-message-width'
variable. If the latter is t, the first string is chosen as
default, which should be the most verbose option available.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customize & Mode definitions
(defgroup smartparens ()
"Smartparens minor mode."
:group 'editing
:prefix "sp-")
;;;###autoload
(define-minor-mode smartparens-mode
"Toggle smartparens mode.
You can enable pre-set bindings by customizing
`sp-base-key-bindings' variable. The current content of
`smartparens-mode-map' is:
\\{smartparens-mode-map}"
:init-value nil
:lighter (" SP" (:eval (if smartparens-strict-mode "/s" "")))
:group 'smartparens
:keymap smartparens-mode-map
(if smartparens-mode
(progn
(sp--init)
(add-hook 'self-insert-uses-region-functions 'sp-wrap--can-wrap-p nil 'local)
;; Unfortunately, some modes rebind "inserting" keys to their
;; own handlers but do not hand over the insertion back to
;; `self-insert-command', rather, they insert via `insert'.
;; Therefore, we need to call this handler in
;; `post-command-hook' too (inside
;; `sp--post-command-hook-handler'). The list
;; `sp--special-self-insert-commands' specifies which commands
;; to handle specially.
(add-hook 'post-self-insert-hook 'sp--post-self-insert-hook-handler nil 'local)
(add-hook 'pre-command-hook 'sp--save-pre-command-state nil 'local)
(add-hook 'post-command-hook 'sp--post-command-hook-handler nil 'local)
(run-hooks 'smartparens-enabled-hook))
(when smartparens-strict-mode
(smartparens-strict-mode -1))
(remove-hook 'self-insert-uses-region-functions 'sp-wrap--can-wrap-p 'local)
(remove-hook 'post-self-insert-hook 'sp--post-self-insert-hook-handler 'local)
(remove-hook 'pre-command-hook 'sp--save-pre-command-state 'local)
(remove-hook 'post-command-hook 'sp--post-command-hook-handler 'local)
(run-hooks 'smartparens-disabled-hook)))
(defvar smartparens-strict-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap delete-char] 'sp-delete-char)
(define-key map [remap delete-forward-char] 'sp-delete-char)
(define-key map [remap backward-delete-char-untabify] 'sp-backward-delete-char)
(define-key map [remap backward-delete-char] 'sp-backward-delete-char)
(define-key map [remap delete-backward-char] 'sp-backward-delete-char)
(define-key map [remap kill-word] 'sp-kill-word)
(define-key map [remap kill-line] 'sp-kill-hybrid-sexp)
(define-key map [remap backward-kill-word] 'sp-backward-kill-word)
(define-key map [remap kill-region] 'sp-kill-region)
(define-key map [remap delete-region] 'sp-delete-region)
(define-key map [remap kill-whole-line] 'sp-kill-whole-line)
map)
"Keymap used for `smartparens-strict-mode'.")
;;;###autoload
(define-minor-mode smartparens-strict-mode
"Toggle the strict smartparens mode.
When strict mode is active, `delete-char', `kill-word' and their
backward variants will skip over the pair delimiters in order to
keep the structure always valid (the same way as `paredit-mode'
does). This is accomplished by remapping them to
`sp-delete-char' and `sp-kill-word'. There is also function
`sp-kill-symbol' that deletes symbols instead of words, otherwise
working exactly the same (it is not bound to any key by default).
When strict mode is active, this is indicated with \"/s\"
after the smartparens indicator in the mode list."
:init-value nil
:group 'smartparens
(if smartparens-strict-mode
(progn
(unless smartparens-mode
(smartparens-mode 1))
(unless (assq 'smartparens-strict-mode minor-mode-overriding-map-alist)
(push `(smartparens-strict-mode . ,smartparens-strict-mode-map)
minor-mode-overriding-map-alist))
(put 'sp-backward-delete-char 'delete-selection 'sp--delete-selection-supersede-p)
(put 'sp-delete-char 'delete-selection 'sp--delete-selection-supersede-p)
(add-hook 'self-insert-uses-region-functions 'sp--self-insert-uses-region-strict-p nil 'local)
(setq sp-autoskip-closing-pair 'always))
(setq minor-mode-overriding-map-alist
(assq-delete-all 'smartparens-strict-mode minor-mode-overriding-map-alist))
(put 'sp-backward-delete-char 'delete-selection 'supersede)
(put 'sp-delete-char 'delete-selection 'supersede)
(remove-hook 'self-insert-uses-region-functions 'sp--self-insert-uses-region-strict-p 'local)
(let ((std-val (car (plist-get (symbol-plist 'sp-autoskip-closing-pair) 'standard-value)))
(saved-val (car (plist-get (symbol-plist 'sp-autoskip-closing-pair) 'saved-value))))
(setq sp-autoskip-closing-pair (eval (or saved-val std-val))))))
;;;###autoload
(define-globalized-minor-mode smartparens-global-strict-mode
smartparens-strict-mode
turn-on-smartparens-strict-mode
:group 'smartparens)
(defcustom sp-ignore-modes-list '(
minibuffer-mode
minibuffer-inactive-mode
)
"Modes where smartparens mode is inactive if allowed globally."
:type '(repeat symbol)
:group 'smartparens)
;;;###autoload
(defun turn-on-smartparens-strict-mode ()
"Turn on `smartparens-strict-mode'."
(interactive)
(when (sp--should-turn-on-p)
(smartparens-strict-mode 1)))
;;;###autoload
(defun turn-off-smartparens-strict-mode ()
"Turn off `smartparens-strict-mode'."
(interactive)
(smartparens-strict-mode -1))
(make-obsolete 'turn-off-smartparens-strict-mode
"this function is marked for deletion."
"2024-04-03")
(defun sp--init ()
"Initialize the buffer local smartparens state.
This includes pair bindings and other buffer local variables
that depend on the active `major-mode'."
(setq sp-state (make-sp-state))
;; setup local pair replacements
(sp--update-local-pairs)
;; set the escape char
(dotimes (char 256)
(unless sp-escape-char
(when (= ?\\ (char-syntax char))
(setq sp-escape-char (string char))))
(unless sp-comment-char
(when (= ?< (char-syntax char))
(setq sp-comment-char (string char)))))
;; in case no escape syntax is found, just assume the backspace
(unless sp-escape-char (setq sp-escape-char "\\")))
(defun sp--maybe-init ()
"Initialize the buffer if it is not already initialized.
See `sp--init'."
(unless sp-pair-list
(sp--init)))
(defun sp--remove-local-pair (open)
"Remove OPEN from `sp-local-pairs'."
(setq sp-local-pairs
(--remove (equal (plist-get it :open) open)
sp-local-pairs)))
(defun sp--update-sp-pair-list ()
"Update `sp-pair-list' according to current value of `sp-local-pairs'."
(setq sp-pair-list
(->> sp-local-pairs
(--map (cons (plist-get it :open) (plist-get it :close)))
(-sort (lambda (x y) (> (length (car x)) (length (car y))))))))
(defun sp--update-local-pairs ()
"Update local pairs after change or at mode initialization.
This command loads all the parent major mode definitions and
merges them into current buffer's `sp-local-pairs'."
;; Combine all the definitions from the most ancient parent to the
;; most recent parent
(-each (nreverse
(--unfold (when it
(cons it (get it 'derived-mode-parent)))
major-mode))
#'sp-update-local-pairs))
(defun sp-update-local-pairs (configuration)
"Update `sp-local-pairs' with CONFIGURATION.
The pairs are only updated in current buffer not in all buffers
with the same major mode! If you want to update all buffers of
the specific major-modes use `sp-local-pair'.
CONFIGURATION can be a symbol to be looked up in `sp-pairs' or a
property list corresponding to the arguments of `sp-local-pair'
or a list of such property lists."
(setq sp-local-pairs
(cond
((symbolp configuration)
(sp--merge-pair-configurations (cdr (assq configuration sp-pairs))))
((plist-member configuration :open)
(sp--merge-pair-configurations (list configuration)))
(t
(sp--merge-pair-configurations configuration))))
;; Keep only those which have non-nil :actions
(setq sp-local-pairs (--filter (plist-get it :actions) sp-local-pairs))
;; update the `sp-pair-list'. This is a list only containing
;; (open.close) cons pairs for easier querying. We also must order
;; it by length of opening delimiter in descending order (first
;; value is the longest)
(sp--update-sp-pair-list)
(setf (sp-state-local-pairs sp-state) sp-local-pairs)
(setf (sp-state-pair-list sp-state) sp-pair-list))
(defmacro sp-with-buffers-using-mode (mode &rest body)
"Execute BODY in every existing buffer using `major-mode' MODE."
(declare (indent 1))
`(--each (buffer-list)
(with-current-buffer it
(when (derived-mode-p ,mode)
,@body))))
(defun sp--update-local-pairs-everywhere (&rest modes)
"Run `sp--update-local-pairs' in all buffers.
This is necessary to update all the buffer-local definitions. If
MODES is non-nil, only update buffers with `major-mode' equal to
MODES."
(setq modes (-flatten modes))
(--each (buffer-list)
(with-current-buffer it
(when (and smartparens-mode
(or (not modes)
(--any? (derived-mode-p it) modes)))
(sp--update-local-pairs)))))
(defcustom smartparens-enabled-hook nil
"Called after `smartparens-mode' is turned on."
:type 'hook
:group 'smartparens)
(defcustom smartparens-disabled-hook nil
"Called after `smartparens-mode' is turned off."
:type 'hook
:group 'smartparens)
;;;###autoload
(define-globalized-minor-mode smartparens-global-mode
smartparens-mode
turn-on-smartparens-mode)
(defun sp--should-turn-on-p ()
"Return non-nil if `smartparens-global-mode' should be turned on.
The same check is also used for
`smartparens-global-strict-mode'."
(not (or (member major-mode sp-ignore-modes-list)
(and (not (derived-mode-p 'comint-mode))
(eq (get major-mode 'mode-class) 'special)))))
;;;###autoload
(defun turn-on-smartparens-mode ()
"Turn on `smartparens-mode'.
This function is used to turn on `smartparens-global-mode'.
By default `smartparens-global-mode' ignores buffers with
`mode-class' set to special, but only if they are also not comint
buffers.
Additionally, buffers on `sp-ignore-modes-list' are ignored.
You can still turn on smartparens in these mode manually (or
in mode's startup-hook etc.) by calling `smartparens-mode'."
(interactive)
(when (sp--should-turn-on-p)
(smartparens-mode 1)))
;;;###autoload
(defun turn-off-smartparens-mode ()
"Turn off `smartparens-mode'."
(interactive)
(smartparens-mode -1))
(make-obsolete 'turn-off-smartparens-mode
"this function is marked for deletion."
"2024-04-03")
;; insert custom
(defcustom sp-autoinsert-pair t
"If non-nil, autoinsert pairs. See `sp-insert-pair'."
:type 'boolean
:group 'smartparens)