-
Notifications
You must be signed in to change notification settings - Fork 3
/
smartparens.el
187 lines (155 loc) · 7.68 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
;;;;;;;;;
;; global
(require 'smartparens-config)
(add-hook 'minibuffer-setup-hook 'turn-on-smartparens-strict-mode)
;;;;;;;;;;;;;;;;;;;;;;;;
;; keybinding management
(define-key smartparens-mode-map (kbd "C-M-f") 'sp-forward-sexp)
(define-key smartparens-mode-map (kbd "C-M-b") 'sp-backward-sexp)
(define-key smartparens-mode-map (kbd "C-M-d") 'sp-down-sexp)
(define-key smartparens-mode-map (kbd "C-M-a") 'sp-backward-down-sexp)
(define-key smartparens-mode-map (kbd "C-S-d") 'sp-beginning-of-sexp)
(define-key smartparens-mode-map (kbd "C-S-a") 'sp-end-of-sexp)
(define-key smartparens-mode-map (kbd "C-M-e") 'sp-up-sexp)
(define-key smartparens-mode-map (kbd "C-M-u") 'sp-backward-up-sexp)
(define-key smartparens-mode-map (kbd "C-M-t") 'sp-transpose-sexp)
(define-key smartparens-mode-map (kbd "C-M-n") 'sp-next-sexp)
(define-key smartparens-mode-map (kbd "C-M-p") 'sp-previous-sexp)
(define-key smartparens-mode-map (kbd "C-M-k") 'sp-kill-sexp)
(define-key smartparens-mode-map (kbd "C-M-w") 'sp-copy-sexp)
(define-key smartparens-mode-map (kbd "M-<delete>") 'sp-unwrap-sexp)
(define-key smartparens-mode-map (kbd "M-<backspace>") 'sp-backward-unwrap-sexp)
(define-key smartparens-mode-map (kbd "C-<right>") 'sp-forward-slurp-sexp)
(define-key smartparens-mode-map (kbd "C-<left>") 'sp-forward-barf-sexp)
(define-key smartparens-mode-map (kbd "C-M-<left>") 'sp-backward-slurp-sexp)
(define-key smartparens-mode-map (kbd "C-M-<right>") 'sp-backward-barf-sexp)
(define-key smartparens-mode-map (kbd "M-D") 'sp-splice-sexp)
(define-key smartparens-mode-map (kbd "C-M-<delete>") 'sp-splice-sexp-killing-forward)
(define-key smartparens-mode-map (kbd "C-M-<backspace>") 'sp-splice-sexp-killing-backward)
(define-key smartparens-mode-map (kbd "C-S-<backspace>") 'sp-splice-sexp-killing-around)
(define-key smartparens-mode-map (kbd "C-]") 'sp-select-next-thing-exchange)
(define-key smartparens-mode-map (kbd "C-<left_bracket>") 'sp-select-previous-thing)
(define-key smartparens-mode-map (kbd "C-M-]") 'sp-select-next-thing)
(define-key smartparens-mode-map (kbd "M-F") 'sp-forward-symbol)
(define-key smartparens-mode-map (kbd "M-B") 'sp-backward-symbol)
(bind-key "C-c f" (lambda () (interactive) (sp-beginning-of-sexp 2)) smartparens-mode-map)
(bind-key "C-c b" (lambda () (interactive) (sp-beginning-of-sexp -2)) smartparens-mode-map)
(bind-key "C-M-s"
(defhydra smartparens-hydra ()
"Smartparens"
("d" sp-down-sexp "Down")
("e" sp-up-sexp "Up")
("u" sp-backward-up-sexp "Up")
("a" sp-backward-down-sexp "Down")
("f" sp-forward-sexp "Forward")
("b" sp-backward-sexp "Backward")
("k" sp-kill-sexp "Kill" :color blue)
("q" nil "Quit" :color blue))
smartparens-mode-map)
(bind-key "H-t" 'sp-prefix-tag-object smartparens-mode-map)
(bind-key "H-p" 'sp-prefix-pair-object smartparens-mode-map)
(bind-key "H-y" 'sp-prefix-symbol-object smartparens-mode-map)
(bind-key "H-h" 'sp-highlight-current-sexp smartparens-mode-map)
(bind-key "H-e" 'sp-prefix-save-excursion smartparens-mode-map)
(bind-key "H-s c" 'sp-convolute-sexp smartparens-mode-map)
(bind-key "H-s a" 'sp-absorb-sexp smartparens-mode-map)
(bind-key "H-s e" 'sp-emit-sexp smartparens-mode-map)
(bind-key "H-s p" 'sp-add-to-previous-sexp smartparens-mode-map)
(bind-key "H-s n" 'sp-add-to-next-sexp smartparens-mode-map)
(bind-key "H-s j" 'sp-join-sexp smartparens-mode-map)
(bind-key "H-s s" 'sp-split-sexp smartparens-mode-map)
(bind-key "H-s r" 'sp-rewrap-sexp smartparens-mode-map)
(defvar hyp-s-x-map)
(define-prefix-command 'hyp-s-x-map)
(bind-key "H-s x" hyp-s-x-map smartparens-mode-map)
(bind-key "H-s x x" 'sp-extract-before-sexp smartparens-mode-map)
(bind-key "H-s x a" 'sp-extract-after-sexp smartparens-mode-map)
(bind-key "H-s x s" 'sp-swap-enclosing-sexp smartparens-mode-map)
(bind-key "C-x C-t" 'sp-transpose-hybrid-sexp smartparens-mode-map)
(bind-key ";" 'sp-comment emacs-lisp-mode-map)
(bind-key [remap c-electric-backspace] 'sp-backward-delete-char smartparens-strict-mode-map)
;;;;;;;;;;;;;;;;;;
;; pair management
(sp-local-pair 'minibuffer-inactive-mode "'" nil :actions nil)
(bind-key "C-(" 'sp---wrap-with-40 minibuffer-local-map)
;;; markdown-mode
(sp-with-modes '(markdown-mode gfm-mode rst-mode)
(sp-local-pair "*" "*"
:wrap "C-*"
:unless '(sp-point-after-word-p sp-point-at-bol-p)
:post-handlers '(("[d1]" "SPC"))
:skip-match 'sp--gfm-skip-asterisk)
(sp-local-pair "**" "**")
(sp-local-pair "_" "_" :wrap "C-_" :unless '(sp-point-after-word-p)))
(defun sp--gfm-skip-asterisk (ms mb me)
(save-excursion
(goto-char mb)
(save-match-data (looking-at "^\\* "))))
;;; org-mode
(sp-with-modes 'org-mode
(sp-local-pair "*" "*" :actions '(insert wrap) :unless '(sp-point-after-word-p sp-point-at-bol-p) :wrap "C-*" :skip-match 'sp--org-skip-asterisk)
(sp-local-pair "_" "_" :unless '(sp-point-after-word-p) :wrap "C-_")
(sp-local-pair "/" "/" :unless '(sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
(sp-local-pair "~" "~" :unless '(sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
(sp-local-pair "=" "=" :unless '(sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
(sp-local-pair "«" "»"))
(defun sp--org-skip-asterisk (ms mb me)
(or (and (= (line-beginning-position) mb)
(eq 32 (char-after (1+ mb))))
(and (= (1+ (line-beginning-position)) me)
(eq 32 (char-after me)))))
;;; tex-mode latex-mode
(sp-with-modes '(tex-mode plain-tex-mode latex-mode)
(sp-local-tag "i" "\"<" "\">"))
;;; lisp modes
(sp-with-modes sp--lisp-modes
(sp-local-pair "(" nil
:wrap "C-("
:pre-handlers '(my-add-space-before-sexp-insertion)
:post-handlers '(my-add-space-after-sexp-insertion)))
(defun my-add-space-after-sexp-insertion (id action _context)
(when (eq action 'insert)
(save-excursion
(forward-char (sp-get-pair id :cl-l))
(when (or (eq (char-syntax (following-char)) ?w)
(looking-at (sp--get-opening-regexp)))
(insert " ")))))
(defun my-add-space-before-sexp-insertion (id action _context)
(when (eq action 'insert)
(save-excursion
(backward-char (length id))
(when (or (eq (char-syntax (preceding-char)) ?w)
(and (looking-back (sp--get-closing-regexp))
(not (eq (char-syntax (preceding-char)) ?'))))
(insert " ")))))
;;; C++
(sp-with-modes '(malabar-mode c++-mode)
(sp-local-pair "{" nil :post-handlers '(("||\n[i]" "RET"))))
(sp-local-pair 'c++-mode "/*" "*/" :post-handlers '((" | " "SPC")
("* ||\n[i]" "RET")))
;;; PHP
(sp-with-modes '(php-mode)
(sp-local-pair "/**" "*/" :post-handlers '(("| " "SPC")
(my-php-handle-docstring "RET")))
(sp-local-pair "/*." ".*/" :post-handlers '(("| " "SPC")))
(sp-local-pair "{" nil :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair "(" nil :prefix "\\(\\sw\\|\\s_\\)*"))
(defun my-php-handle-docstring (&rest _ignored)
(-when-let (line (save-excursion
(forward-line)
(thing-at-point 'line)))
(cond
((string-match-p "function" line)
(save-excursion
(insert "\n")
(let ((args (save-excursion
(forward-line)
(my-php-get-function-args))))
(--each args
(insert (format "* @param %s\n" it)))))
(insert "* "))
((string-match-p ".*class\\|interface" line)
(save-excursion (insert "\n*\n* @author\n"))
(insert "* ")))
(let ((o (sp--get-active-overlay)))
(indent-region (overlay-start o) (overlay-end o)))))