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
|
;;; mm-editing.el --- Text editing configuation -*- lexical-binding: t; -*-
;;; Delete Region When Typing
(use-package delsel
:hook (after-init . delete-selection-mode))
^L
;;; Force Spaces For Alignment
(defun mm-editing-force-space-indentation (function &rest arguments)
"Call FUNCTION with ARGUMENTS in an environment in which
`indent-tabs-mode' is nil."
(let (indent-tabs-mode)
(apply function arguments)))
(dolist (command #'(align
c-backslash-region
comment-dwim
makefile-backslash-region))
(advice-add command :around #'mm-editing-force-space-indentation))
^L
;;; Indentation Settings
(setq-default
tab-width 4
indent-tabs-mode t)
(defvar mm-editing-indentation-settings-alist
'((c-mode . (:extras c-basic-offset))
(c-ts-mode . (:extras c-ts-mode-indent-offset))
(css-mode . (:extras css-indent-offset))
(emacs-lisp-mode . (:width 8 :spaces t)) ; GNU code uses 8-column tabs
(go-mod-ts-mode . (:extras go-ts-mode-indent-offset))
(go-ts-mode . (:extras go-ts-mode-indent-offset))
(gsp-ts-mode . (:width 2 :extras gsp-ts-mode-indent-rules))
(helpful-mode . (:width 8)) ; GNU code uses 8-column tabs
(lisp-data-mode . (:spaces t))
(lisp-interaction-mode . (:spaces t))
(lisp-mode . (:spaces t))
(mhtml-mode . (:extras sgml-basic-offset))
(org-mode . (:spaces t))
(python-mode . (:extras python-indent-offset))
(python-ts-mode . (:extras python-indent-offset))
(sgml-mode . (:extras sgml-basic-offset))
(sh-mode . (:extras sh-basic-offset))
(vimscript-ts-mode . (:extras vimscript-ts-mode-indent-level)))
"Alist of indentation settings.
Each pair in this alist is of the form (MODE . SETTINGS) where MODE
specifies the mode for which the given SETTINGS should apply.
SETTINGS is a plist of one-or-more of the following keys:
`:spaces' -- If nil force tabs for indentation, if non-nil for spaces
for indentation. If this key is not provided then the
value of `indent-tabs-mode' is used.
`:width' -- Specifies a non-negative number to be used as the tab
width and indentation offset. If this key is not
provided then the default value of `tab-width' is used.
`:extras' -- A list of mode-specific variables which control
indentation settings that need to be set for
configurations to properly be applied.")
(defun mm-editing-set-indentation-settings ()
"Set indentation settings for the current major mode.
The indentation settings are set based on the configured values in
`mm-editing-indentation-settings-alist'."
(let* ((plist (alist-get major-mode mm-editing-indentation-settings-alist))
(spaces (plist-member plist :spaces))
(width (plist-member plist :width))
(extras (plist-member plist :extras)))
;; Some modes like ‘python-mode’ explicitly set ‘tab-width’ and
;; ‘indent-tabs-mode’ so we must override them explicitly.
(setq-local
indent-tabs-mode (if spaces
(not (cadr spaces))
(default-value 'indent-tabs-mode))
tab-width (if width
(cadr width)
(default-value 'tab-width)))
(when extras
(setq extras (cadr extras))
(when (symbolp extras)
(setq extras (list extras)))
(dolist (extra extras)
(set extra tab-width)))))
(add-hook 'after-change-major-mode-hook #'mm-editing-set-indentation-settings)
(defun mm-editing-set-tabsize ()
"Set the tabsize for the current buffer.
If the current buffer’s major mode requires setting additional variables,
those should be listed in `mm-editing-indentation-settings'."
(interactive)
(let* ((prompt-default (default-value 'tab-width))
(prompt (format-prompt "Tabsize" prompt-default))
(tabsize (mm-as-number (read-string prompt nil nil prompt-default))))
(setq-local tab-width tabsize)
(when-let* ((plist (alist-get major-mode mm-editing-indentation-settings))
(extras (plist-get plist :extras)))
(dolist (extra (if (symbolp extras)
(list extras)
extras))
(set (make-local-variable extra) tabsize)))))
^L
;;; Make Tab Not Suck
(use-package indent
:custom
(tab-always-indent t))
(use-package electric
:config
(setq-default electric-indent-inhibit t))
^L
;;; Multiple Cursors
(defmacro mm--define-mc-marking-command (name search-function noun)
(let ((noun-symbol (intern noun)))
`(defun ,name (beg end ,noun-symbol)
,(format "Mark all occurances of %s between BEG and END.
If called interactively with an active region then all matches in the
region are marked, otherwise all matches in the buffer are marked."
(upcase noun))
(interactive
(list (or (use-region-beginning) (point-min))
(or (use-region-end) (point-max))
(read-string
(format-prompt ,(concat "Match " noun) nil))))
(require 'multiple-cursors)
(if (string-empty-p ,noun-symbol)
(message "Command aborted")
(catch 'mm--no-match
(mc/remove-fake-cursors)
(goto-char beg)
(let (did-match-p)
(while (,search-function ,noun-symbol end :noerror)
(setq did-match-p t)
(push-mark (match-beginning 0))
(exchange-point-and-mark)
(mc/create-fake-cursor-at-point)
(goto-char (mark)))
(unless did-match-p
(message "No match for `%s'" ,noun-symbol)
(throw 'mm--no-match nil)))
(when-let ((first (mc/furthest-cursor-before-point)))
(mc/pop-state-from-overlay first))
(if (> (mc/num-cursors) 1)
(multiple-cursors-mode 1)
(multiple-cursors-mode 0)))))))
(mm--define-mc-marking-command
mm-mark-all-in-region search-forward "string")
(mm--define-mc-marking-command
mm-mark-all-in-region-regexp re-search-forward "regexp")
(use-package multiple-cursors
:ensure t
:defer 2
:bind (("C->" . #'mc/mark-next-like-this)
("C-<" . #'mc/mark-previous-like-this)
("C-M-<" . #'mc/mark-all-like-this-dwim)
("C-M->" . #'mc/edit-lines)
("C-$" . #'mm-mark-all-in-region)
("M-$" . #'mm-mark-all-in-region-regexp))
:init
(with-eval-after-load 'multiple-cursors-core
(dolist (command #'(backward-delete-char
capitalize-dwim
delete-backward-char
delete-forward-char
downcase-dwim
upcase-dwim))
(add-to-list 'mc/cmds-to-run-for-all command))
(dolist (command #'(helpful-callable
helpful-key
helpful-symbol
helpful-variable))
(add-to-list 'mc/cmds-to-run-once command))
(add-to-list 'mc/unsupported-minor-modes #'corfu-mode))
:config
(keymap-unset mc/keymap "<return>" :remove))
^L
;;; Increment Numbers
(use-package increment
:bind (("C-c i i" . #'increment-number-at-point)
("C-c i d" . #'decrement-number-at-point))
:commands (increment-number-at-point decrement-number-at-point))
^L
;;; Surround With Delimeters
(defun mm-editing-surround-with-spaces (char)
"Surrounds region or current symbol with a pair defined by CHAR.
This is the same as `surround-insert' except it pads the contents of the
surround with spaces."
(interactive
(list (char-to-string (read-char "Character: "))))
(let* ((pair (surround--make-pair char))
(left (car pair))
(right (cdr pair))
(bounds (surround--infer-bounds t)))
(save-excursion
(goto-char (cdr bounds))
(insert " " right)
(goto-char (car bounds))
(insert left " "))
(when (eq (car bounds) (point))
(forward-char))))
;; TODO: Implement this manually
(use-package surround
:ensure t
:bind-keymap ("M-'" . surround-keymap)
:bind (:map surround-keymap
("S" . #'mm-editing-surround-with-spaces))
:config
(dolist (pair '(("‘" . "’")
("“" . "”")
("»" . "«")
("⟮" . "⟯")))
(push pair surround-pairs))
(make-variable-buffer-local 'surround-pairs)
(add-hook 'emacs-lisp-mode-hook
(defun mm-editing-add-elisp-quotes-pair ()
(push '("`" . "'") surround-pairs))))
^L
;;; Emmet Mode
(defun mm-editing-emmet-dwim (arg)
"Do-What-I-Mean Emmet expansion.
If the region is active then the region will be surrounded by an emmet
expansion read from the minibuffer. Otherwise the emmet expression
before point is expanded. When provided a prefix argument the behaviour
is as described by `emmet-expand-line'."
(interactive "P")
(if (region-active-p)
(call-interactively #'emmet-wrap-with-markup)
(emmet-expand-line arg)))
(use-package emmet-mode
:ensure t
:bind ("C-," . mm-editing-emmet-dwim)
:custom
(emmet-self-closing-tag-style ""))
^L
;;; Additional Major Modes
(use-package csv-mode
:ensure t)
^L
;;; Mode-Specific Configurations
(use-package make-mode
:custom
(makefile-backslash-column 80))
(provide 'mm-editing)
|