]> code.delx.au - gnu-emacs-elpa/blob - sotlisp.el
Add a few more abbrevs, suggested by Oleh
[gnu-emacs-elpa] / sotlisp.el
1 ;;; sotlisp.el --- Write lisp at the speed of thought. -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/speed-of-thought-lisp
7 ;; Keywords: convenience, lisp
8 ;; Package-Requires: ((emacs "24.1"))
9 ;; Version: 1.4
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; This defines a new global minor-mode `speed-of-thought-mode', which
27 ;; activates locally on any supported buffer. Currently, only
28 ;; `emacs-lisp-mode' buffers are supported.
29 ;;
30 ;; The mode is quite simple, and is composed of two parts:
31 ;;
32 ;;; Abbrevs
33 ;;
34 ;; A large number of abbrevs which expand function
35 ;; initials to their name. A few examples:
36 ;;
37 ;; - wcb -> with-current-buffer
38 ;; - i -> insert
39 ;; - r -> require '
40 ;; - a -> and
41 ;;
42 ;; However, these are defined in a way such that they ONLY expand in a
43 ;; place where you would use a function, so hitting SPC after "(r"
44 ;; expands to "(require '", but hitting SPC after "(delete-region r"
45 ;; will NOT expand the `r', because that's obviously not a function.
46 ;; Furtheromre, "#'r" will expand to "#'require" (note how it ommits
47 ;; that extra quote, since it would be useless here).
48 ;;
49 ;;; Commands
50 ;;
51 ;; It also defines 4 commands, which really fit into this "follow the
52 ;; thought-flow" way of writing. The bindings are as follows, I
53 ;; understand these don't fully adhere to conventions, and I'd
54 ;; appreciate suggestions on better bindings.
55 ;;
56 ;; - M-RET :: Break line, and insert "()" with point in the middle.
57 ;; - C-RET :: Do `forward-up-list', then do M-RET.
58 ;;
59 ;; Hitting RET followed by a `(' was one of the most common key sequences
60 ;; for me while writing elisp, so giving it a quick-to-hit key was a
61 ;; significant improvement.
62 ;;
63 ;; - C-c f :: Find function under point. If it is not defined, create a
64 ;; definition for it below the current function and leave point inside.
65 ;; - C-c v :: Same, but for variable.
66 ;;
67 ;; With these commands, you just write your code as you think of it. Once
68 ;; you hit a "stop-point" of sorts in your tought flow, you hit `C-c f/v`
69 ;; on any undefined functions/variables, write their definitions, and hit
70 ;; `C-u C-SPC` to go back to the main function.
71 ;;
72 ;;; Small Example
73 ;;
74 ;; With the above (assuming you use something like paredit or
75 ;; electric-pair-mode), if you write:
76 ;;
77 ;; ( w t b M-RET i SPC text
78 ;;
79 ;; You get
80 ;;
81 ;; (with-temp-buffer (insert text))
82
83 \f
84 ;;; Code:
85
86 ;;; Predicates
87 (defun sotlisp--auto-paired-p ()
88 "Non-nil if this buffer auto-inserts parentheses."
89 (or (bound-and-true-p electric-pair-mode)
90 (bound-and-true-p paredit-mode)
91 (bound-and-true-p smartparens-mode)))
92
93 (defun sotlisp--looking-back (regexp)
94 (string-match
95 (concat regexp "\\'")
96 (buffer-substring (line-beginning-position) (point))))
97
98 (defun sotlisp--function-form-p ()
99 "Non-nil if point is at the start of a sexp.
100 Specially, avoids matching inside argument lists."
101 (and (eq (char-before) ?\()
102 (not (sotlisp--looking-back "(\\(defun\\s-+.*\\|lambda\\s-+\\)("))
103 (not (string-match (rx (syntax symbol)) (string last-command-event)))))
104
105 (defun sotlisp--function-quote-p ()
106 "Non-nil if point is at a sharp-quote."
107 (ignore-errors
108 (save-excursion
109 (forward-char -2)
110 (looking-at-p "#'"))))
111
112 (defun sotlisp--code-p ()
113 (save-excursion
114 (let ((r (point)))
115 (beginning-of-defun)
116 (let ((pps (parse-partial-sexp (point) r)))
117 (not (or (elt pps 3)
118 (elt pps 4)))))))
119
120 (defun sotlisp--function-p ()
121 "Non-nil if point is at reasonable place for a function name.
122 Returns non-nil if, after moving backwards by a sexp, either
123 `sotlisp--function-form-p' or `sotlisp--function-quote-p' return
124 non-nil."
125 (save-excursion
126 (ignore-errors
127 (skip-chars-backward (rx alnum))
128 (and (sotlisp--code-p)
129 (or (sotlisp--function-form-p)
130 (sotlisp--function-quote-p))))))
131
132 (defun sotlisp--whitespace-p ()
133 "Non-nil if current `self-insert'ed char is whitespace."
134 (sotlisp--whitespace-char-p last-command-event))
135 (make-obsolete 'sotlisp--whitespace-p 'sotlisp--whitespace-char-p "1.2")
136
137 (defun sotlisp--whitespace-char-p (char)
138 "Non-nil if CHAR is has whitespace syntax."
139 (ignore-errors
140 (string-match (rx space) (string char))))
141
142 \f
143 ;;; Expansion logic
144 (defvar sotlisp--needs-moving nil
145 "Will `sotlisp--move-to-$' move point after insertion?")
146
147 (defun sotlisp--move-to-$ ()
148 "Move backwards until `$' and delete it.
149 Point is left where the `$' char was. Does nothing if variable
150 `sotlisp-mode' is nil."
151 (when (bound-and-true-p speed-of-thought-mode)
152 (when sotlisp--needs-moving
153 (setq sotlisp--needs-moving nil)
154 (skip-chars-backward "^\\$")
155 (delete-char -1))))
156
157 (add-hook 'post-command-hook #'sotlisp--move-to-$ 'append)
158
159 (defun sotlisp--maybe-skip-closing-paren ()
160 "Move past `)' if variable `electric-pair-mode' is enabled."
161 (when (and (char-after ?\))
162 (sotlisp--auto-paired-p))
163 (forward-char 1)))
164
165 (defun sotlisp--post-expansion-cleanup ()
166 "Do some processing conditioned on the expansion done.
167 If the command that triggered the expansion was a whitespace
168 char, perform the steps below and return t.
169
170 If the expansion ended in a $, delete it and call
171 `sotlisp--maybe-skip-closing-paren'.
172 If it ended in a space and there's a space ahead, delete the
173 space ahead."
174 ;; Inform `expand-abbrev' that `self-insert-command' should not
175 ;; trigger, by returning non-nil on SPC.
176 (when (sotlisp--whitespace-char-p last-command-event)
177 ;; And maybe move out of closing paren if expansion ends with $.
178 (if (eq (char-before) ?$)
179 (progn (delete-char -1)
180 (setq sotlisp--needs-moving nil)
181 (sotlisp--maybe-skip-closing-paren))
182 (when (and (sotlisp--whitespace-char-p (char-after))
183 (sotlisp--whitespace-char-p (char-before)))
184 (delete-char 1)))
185 t))
186
187 (defvar sotlisp--function-table (make-hash-table :test #'equal)
188 "Table where function abbrev expansions are stored.")
189
190 (defun sotlisp--expand-function ()
191 "Expand the function abbrev before point.
192 See `sotlisp-define-function-abbrev'."
193 (let ((r (point)))
194 (skip-chars-backward (rx alnum))
195 (let* ((name (buffer-substring (point) r))
196 (expansion (gethash name sotlisp--function-table)))
197 (if (not expansion)
198 (progn (goto-char r) nil)
199 (delete-region (point) r)
200 (if (sotlisp--function-quote-p)
201 ;; After #' use the simple expansion.
202 (insert (sotlisp--simplify-function-expansion expansion))
203 ;; Inside a form, use the full expansion.
204 (insert expansion)
205 (when (string-match "\\$" expansion)
206 (setq sotlisp--needs-moving t)))
207 ;; Must be last.
208 (sotlisp--post-expansion-cleanup)))))
209
210 (put 'sotlisp--expand-function 'no-self-insert t)
211
212 (defun sotlisp--simplify-function-expansion (expansion)
213 "Take a substring of EXPANSION up to first space.
214 The space char is not included. Any \"$\" are also removed."
215 (replace-regexp-in-string
216 "\\$" ""
217 (substring expansion 0 (string-match " " expansion))))
218
219 \f
220 ;;; Abbrev definitions
221 (defconst sotlisp--default-function-abbrevs
222 '(
223 ("a" . "and ")
224 ("ah" . "add-hook '")
225 ("atl" . "add-to-list '")
226 ("bb" . "bury-buffer")
227 ("bc" . "forward-char -1")
228 ("bfn" . "buffer-file-name")
229 ("bl" . "buffer-list$")
230 ("blp" . "buffer-live-p ")
231 ("bn" . "buffer-name")
232 ("bod" . "beginning-of-defun")
233 ("bol" . "forward-line 0$")
234 ("bp" . "boundp '")
235 ("bs" . "buffer-string$")
236 ("bsn" . "buffer-substring-no-properties")
237 ("bss" . "buffer-substring ")
238 ("bw" . "forward-word -1")
239 ("c" . "concat ")
240 ("ca" . "char-after$")
241 ("cb" . "current-buffer$")
242 ("cc" . "condition-case er\n$\n(error nil)")
243 ("ci" . "call-interactively ")
244 ("cip" . "called-interactively-p 'any")
245 ("csv" . "customize-save-variable '")
246 ("d" . "delete-char 1")
247 ("dc" . "delete-char 1")
248 ("dcu" . "defcustom $ t\n \"\"\n :type 'boolean")
249 ("df" . "defun $ ()\n \"\"\n ")
250 ("dfa" . "defface $ \n '((t))\n \"\"\n ")
251 ("dfc" . "defcustom $ t\n \"\"\n :type 'boolean")
252 ("dff" . "defface $ \n '((t))\n \"\"\n ")
253 ("dfv" . "defvar $ t\n \"\"")
254 ("dk" . "define-key ")
255 ("dl" . "dolist (it $)")
256 ("dmp" . "derived-mode-p '")
257 ("dm" . "defmacro $ ()\n \"\"\n ")
258 ("dr" . "delete-region ")
259 ("dv" . "defvar $ t\n \"\"")
260 ("e" . "error \"$\"")
261 ("efn" . "expand-file-name ")
262 ("eol" . "end-of-line")
263 ("f" . "format \"$\"")
264 ("fb" . "fboundp '")
265 ("fbp" . "fboundp '")
266 ("fc" . "forward-char 1")
267 ("ff" . "find-file ")
268 ("fl" . "forward-line 1")
269 ("fp" . "functionp ")
270 ("frp" . "file-readable-p ")
271 ("fs" . "forward-sexp 1")
272 ("fu" . "funcall ")
273 ("fw" . "forward-word 1")
274 ("g" . "goto-char ")
275 ("gc" . "goto-char ")
276 ("gsk" . "global-set-key ")
277 ("i" . "insert ")
278 ("ie" . "ignore-errors ")
279 ("ii" . "interactive")
280 ("il" . "if-let (($))")
281 ("ir" . "indent-region ")
282 ("jcl" . "justify-current-line ")
283 ("jl" . "delete-indentation")
284 ("jos" . "just-one-space")
285 ("jr" . "json-read$")
286 ("jtr" . "jump-to-register ")
287 ("k" . "kbd \"$\"")
288 ("kb" . "kill-buffer")
289 ("kn" . "kill-new ")
290 ("kp" . "keywordp ")
291 ("l" . "lambda ($)")
292 ("la" . "looking-at \"$\"")
293 ("lap" . "looking-at-p \"$\"")
294 ("lb" . "looking-back \"$\"")
295 ("lbp" . "line-beginning-position")
296 ("lep" . "line-end-position")
297 ("let" . "let (($))")
298 ("lp" . "listp ")
299 ("m" . "message \"$%s\"")
300 ("mb" . "match-beginning 0")
301 ("mc" . "mapcar ")
302 ("mct" . "mapconcat ")
303 ("me" . "match-end 0")
304 ("ms" . "match-string 0")
305 ("msn" . "match-string-no-properties 0")
306 ("msnp" . "match-string-no-properties 0")
307 ("msp" . "match-string-no-properties 0")
308 ("mt" . "mapconcat ")
309 ("n" . "not ")
310 ("nai" . "newline-and-indent$")
311 ("nl" . "forward-line 1")
312 ("np" . "numberp ")
313 ("ntr" . "narrow-to-region ")
314 ("ow" . "other-window 1")
315 ("p" . "point$")
316 ("pm" . "point-marker$")
317 ("pa" . "point-max$")
318 ("pg" . "plist-get ")
319 ("pi" . "point-min$")
320 ("pz" . "propertize ")
321 ("r" . "require '")
322 ("ra" . "use-region-p$")
323 ("rap" . "use-region-p$")
324 ("rb" . "region-beginning")
325 ("re" . "region-end")
326 ("rh" . "remove-hook '")
327 ("rm" . "replace-match \"$\"")
328 ("ro" . "regexp-opt ")
329 ("rq" . "regexp-quote ")
330 ("rris" . "replace-regexp-in-string ")
331 ("rrs" . "replace-regexp-in-string ")
332 ("rs" . "while (search-forward $ nil t)\n(replace-match \"\") nil t)")
333 ("rsb" . "re-search-backward \"$\" nil 'noerror")
334 ("rsf" . "re-search-forward \"$\" nil 'noerror")
335 ("s" . "setq ")
336 ("sb" . "search-backward $ nil 'noerror")
337 ("sbr" . "search-backward-regexp $ nil 'noerror")
338 ("scb" . "skip-chars-backward \"$\\r\\n[:blank:]\"")
339 ("scf" . "skip-chars-forward \"$\\r\\n[:blank:]\"")
340 ("se" . "save-excursion")
341 ("sf" . "search-forward $ nil 'noerror")
342 ("sfr" . "search-forward-regexp $ nil 'noerror")
343 ("sic" . "self-insert-command")
344 ("sl" . "string<")
345 ("sm" . "string-match \"$\"")
346 ("smd" . "save-match-data")
347 ("sn" . "symbol-name ")
348 ("sp" . "stringp ")
349 ("sq" . "string= ")
350 ("sr" . "save-restriction")
351 ("ss" . "substring ")
352 ("ssn" . "substring-no-properties ")
353 ("ssnp" . "substring-no-properties ")
354 ("stb" . "switch-to-buffer ")
355 ("sw" . "selected-window$")
356 ("syp" . "symbolp ")
357 ("tap" . "thing-at-point 'symbol")
358 ("u" . "unless ")
359 ("ul" . "up-list")
360 ("up" . "unwind-protect\n(progn $)")
361 ("urp" . "use-region-p$")
362 ("w" . "when ")
363 ("wcb" . "with-current-buffer ")
364 ("wf" . "write-file ")
365 ("wh" . "while ")
366 ("wl" . "when-let (($))")
367 ("we" . "window-end")
368 ("ws" . "window-start")
369 ("wsw" . "with-selected-window ")
370 ("wtb" . "with-temp-buffer")
371 ("wtf" . "with-temp-file ")
372 )
373 "Alist of (ABBREV . EXPANSION) used by `sotlisp'.")
374
375 (defun sotlisp-define-function-abbrev (name expansion)
376 "Define a function abbrev expanding NAME to EXPANSION.
377 This abbrev will only be expanded in places where a function name is
378 sensible. Roughly, this is right after a `(' or a `#''.
379
380 If EXPANSION is any string, it doesn't have to be the just the
381 name of a function. In particular:
382 - if it contains a `$', this char will not be inserted and
383 point will be moved to its position after expansion.
384 - if it contains a space, only a substring of it up to the
385 first space is inserted when expanding after a `#'' (this is done
386 by defining two different abbrevs).
387
388 For instance, if one defines
389 (sotlisp-define-function-abbrev \"d\" \"delete-char 1\")
390
391 then triggering `expand-abbrev' after \"d\" expands in the
392 following way:
393 (d => (delete-char 1
394 #'d => #'delete-char"
395 (define-abbrev emacs-lisp-mode-abbrev-table
396 name t #'sotlisp--expand-function
397 ;; Don't override user abbrevs
398 :system t
399 ;; Only expand in function places.
400 :enable-function #'sotlisp--function-p)
401 (puthash name expansion sotlisp--function-table))
402
403 (defun sotlisp-erase-all-abbrevs ()
404 "Undefine all abbrevs defined by `sotlisp'."
405 (interactive)
406 (maphash (lambda (x _) (define-abbrev emacs-lisp-mode-abbrev-table x nil))
407 sotlisp--function-table))
408
409 (defun sotlisp-define-all-abbrevs ()
410 "Define all abbrevs in `sotlisp--default-function-abbrevs'."
411 (interactive)
412 (mapc (lambda (x) (sotlisp-define-function-abbrev (car x) (cdr x)))
413 sotlisp--default-function-abbrevs))
414
415 \f
416 ;;; The global minor-mode
417 (defvar speed-of-thought-turn-on-hook '()
418 "Hook run once when `speed-of-thought-mode' is enabled.
419 Note that `speed-of-thought-mode' is global, so this is not run
420 on every buffer.
421
422 See `sotlisp-turn-on-everywhere' for an example of what a
423 function in this hook should do.")
424
425 (defvar speed-of-thought-turn-off-hook '()
426 "Hook run once when `speed-of-thought-mode' is disabled.
427 Note that `speed-of-thought-mode' is global, so this is not run
428 on every buffer.
429
430 See `sotlisp-turn-on-everywhere' for an example of what a
431 function in this hook should do.")
432
433 ;;;###autoload
434 (define-minor-mode speed-of-thought-mode
435 nil nil nil nil
436 :global t
437 (run-hooks (if speed-of-thought-mode
438 'speed-of-thought-turn-on-hook
439 'speed-of-thought-turn-off-hook)))
440
441 ;;;###autoload
442 (defun speed-of-thought-hook-in (on off)
443 "Add functions ON and OFF to `speed-of-thought-mode' hooks.
444 If `speed-of-thought-mode' is already on, call ON."
445 (add-hook 'speed-of-thought-turn-on-hook on)
446 (add-hook 'speed-of-thought-turn-off-hook off)
447 (when speed-of-thought-mode (funcall on)))
448
449 \f
450 ;;; The local minor-mode
451 (define-minor-mode sotlisp-mode
452 nil nil " SoT"
453 `(([M-return] . sotlisp-newline-and-parentheses)
454 ([C-return] . sotlisp-downlist-newline-and-parentheses)
455 (,(kbd "C-M-;") . ,(if (boundp 'comment-or-uncomment-sexp)
456 #'comment-or-uncomment-sexp
457 #'sotlisp-comment-or-uncomment-sexp))
458 ("\C-cf" . sotlisp-find-or-define-function)
459 ("\C-cv" . sotlisp-find-or-define-variable)))
460
461 (defun sotlisp-turn-on-everywhere ()
462 "Call-once function to turn on sotlisp everywhere.
463 Calls `sotlisp-mode' on all `emacs-lisp-mode' buffers, and sets
464 up a hook and abbrevs."
465 (add-hook 'emacs-lisp-mode-hook #'sotlisp-mode)
466 (sotlisp-define-all-abbrevs)
467 (mapc (lambda (b)
468 (with-current-buffer b
469 (when (derived-mode-p 'emacs-lisp-mode)
470 (sotlisp-mode 1))))
471 (buffer-list)))
472
473 (defun sotlisp-turn-off-everywhere ()
474 "Call-once function to turn off sotlisp everywhere.
475 Removes `sotlisp-mode' from all `emacs-lisp-mode' buffers, and
476 removes hooks and abbrevs."
477 (remove-hook 'emacs-lisp-mode-hook #'sotlisp-mode)
478 (sotlisp-erase-all-abbrevs)
479 (mapc (lambda (b)
480 (with-current-buffer b
481 (when (derived-mode-p 'emacs-lisp-mode)
482 (sotlisp-mode -1))))
483 (buffer-list)))
484
485 (speed-of-thought-hook-in #'sotlisp-turn-on-everywhere #'sotlisp-turn-off-everywhere)
486
487 \f
488 ;;; Commands
489 (defun sotlisp-newline-and-parentheses ()
490 "`newline-and-indent' then insert a pair of parentheses."
491 (interactive)
492 (point)
493 (ignore-errors (expand-abbrev))
494 (newline-and-indent)
495 (insert "()")
496 (forward-char -1))
497
498 (defun sotlisp-downlist-newline-and-parentheses ()
499 "`up-list', `newline-and-indent', then insert a parentheses pair."
500 (interactive)
501 (ignore-errors (expand-abbrev))
502 (up-list)
503 (newline-and-indent)
504 (insert "()")
505 (forward-char -1))
506
507 (defun sotlisp--find-in-buffer (r s)
508 "Find the string (concat R (regexp-quote S)) somewhere in this buffer."
509 (let ((l (save-excursion
510 (goto-char (point-min))
511 (save-match-data
512 (when (search-forward-regexp (concat r (regexp-quote s) "\\_>")
513 nil :noerror)
514 (match-beginning 0))))))
515 (when l
516 (push-mark)
517 (goto-char l)
518 l)))
519
520 (defun sotlisp--beginning-of-defun ()
521 "`push-mark' and move above this defun."
522 (push-mark)
523 (beginning-of-defun)
524 (when (looking-back "^;;;###autoload\\s-*\n")
525 (forward-line -1)))
526
527 (defun sotlisp--function-at-point ()
528 "Return name of `function-called-at-point'."
529 (if (save-excursion
530 (ignore-errors (forward-sexp -1)
531 (looking-at-p "#'")))
532 (thing-at-point 'symbol)
533 (let ((fcap (function-called-at-point)))
534 (if fcap
535 (symbol-name fcap)
536 (thing-at-point 'symbol)))))
537
538 (defun sotlisp-find-or-define-function (&optional prefix)
539 "If symbol under point is a defined function, go to it, otherwise define it.
540 Essentially `find-function' on steroids.
541
542 If you write in your code the name of a function you haven't
543 defined yet, just place point on its name and hit \\[sotlisp-find-or-define-function]
544 and a defun will be inserted with point inside it. After that,
545 you can just hit `pop-mark' to go back to where you were.
546 With a PREFIX argument, creates a `defmacro' instead.
547
548 If the function under point is already defined this just calls
549 `find-function', with one exception:
550 if there's a defun (or equivalent) for this function in the
551 current buffer, we go to that even if it's not where the
552 global definition comes from (this is useful if you're
553 writing an Emacs package that also happens to be installed
554 through package.el).
555
556 With a prefix argument, defines a `defmacro' instead of a `defun'."
557 (interactive "P")
558 (let ((name (sotlisp--function-at-point)))
559 (unless (and name (sotlisp--find-in-buffer "(def\\(un\\|macro\\|alias\\) " name))
560 (let ((name-s (intern-soft name)))
561 (if (fboundp name-s)
562 (find-function name-s)
563 (sotlisp--beginning-of-defun)
564 (insert "(def" (if prefix "macro" "un")
565 " " name " (")
566 (save-excursion (insert ")\n \"\"\n )\n\n")))))))
567
568 (defun sotlisp-find-or-define-variable (&optional prefix)
569 "If symbol under point is a defined variable, go to it, otherwise define it.
570 Essentially `find-variable' on steroids.
571
572 If you write in your code the name of a variable you haven't
573 defined yet, place point on its name and hit \\[sotlisp-find-or-define-variable]
574 and a `defcustom' will be created with point inside. After that,
575 you can just `pop-mark' to go back to where you were. With a
576 PREFIX argument, creates a `defvar' instead.
577
578 If the variable under point is already defined this just calls
579 `find-variable', with one exception:
580 if there's a defvar (or equivalent) for this variable in the
581 current buffer, we go to that even if it's not where the
582 global definition comes from (this is useful if you're
583 writing an Emacs package that also happens to be installed
584 through package.el).
585
586 With a prefix argument, defines a `defvar' instead of a `defcustom'."
587 (interactive "P")
588 (let ((name (symbol-name (variable-at-point t))))
589 (unless (sotlisp--find-in-buffer "(def\\(custom\\|const\\|var\\) " name)
590 (unless (and (symbolp (variable-at-point))
591 (ignore-errors (find-variable (variable-at-point)) t))
592 (let ((name (thing-at-point 'symbol)))
593 (sotlisp--beginning-of-defun)
594 (insert "(def" (if prefix "var" "custom")
595 " " name " t")
596 (save-excursion
597 (insert "\n \"\""
598 (if prefix "" "\n :type 'boolean")
599 ")\n\n")))))))
600
601 \f
602 ;;; Comment sexp
603 (defun sotlisp-uncomment-sexp (&optional n)
604 "Uncomment a sexp around point."
605 (interactive "P")
606 (let* ((initial-point (point-marker))
607 (inhibit-field-text-motion t)
608 (p)
609 (end (save-excursion
610 (when (elt (syntax-ppss) 4)
611 (re-search-backward comment-start-skip
612 (line-beginning-position)
613 t))
614 (setq p (point-marker))
615 (comment-forward (point-max))
616 (point-marker)))
617 (beg (save-excursion
618 (forward-line 0)
619 (while (and (not (bobp))
620 (= end (save-excursion
621 (comment-forward (point-max))
622 (point))))
623 (forward-line -1))
624 (goto-char (line-end-position))
625 (re-search-backward comment-start-skip
626 (line-beginning-position)
627 t)
628 (ignore-errors
629 (while (looking-at comment-start-skip)
630 (forward-char -1))
631 (unless (looking-at "[\n\r[:blank]]")
632 (forward-char 1)))
633 (point-marker))))
634 (unless (= beg end)
635 (uncomment-region beg end)
636 (goto-char p)
637 ;; Indentify the "top-level" sexp inside the comment.
638 (ignore-errors
639 (while (>= (point) beg)
640 (backward-prefix-chars)
641 (skip-chars-backward "\r\n[:blank:]")
642 (setq p (point-marker))
643 (backward-up-list)))
644 ;; Re-comment everything before it.
645 (ignore-errors
646 (comment-region beg p))
647 ;; And everything after it.
648 (goto-char p)
649 (forward-sexp (or n 1))
650 (skip-chars-forward "\r\n[:blank:]")
651 (if (< (point) end)
652 (ignore-errors
653 (comment-region (point) end))
654 ;; If this is a closing delimiter, pull it up.
655 (goto-char end)
656 (skip-chars-forward "\r\n[:blank:]")
657 (when (eq 5 (car (syntax-after (point))))
658 (delete-indentation))))
659 ;; Without a prefix, it's more useful to leave point where
660 ;; it was.
661 (unless n
662 (goto-char initial-point))))
663
664 (defun sotlisp--comment-sexp-raw ()
665 "Comment the sexp at point or ahead of point."
666 (pcase (or (bounds-of-thing-at-point 'sexp)
667 (save-excursion
668 (skip-chars-forward "\r\n[:blank:]")
669 (bounds-of-thing-at-point 'sexp)))
670 (`(,l . ,r)
671 (goto-char r)
672 (skip-chars-forward "\r\n[:blank:]")
673 (save-excursion
674 (comment-region l r))
675 (skip-chars-forward "\r\n[:blank:]"))))
676
677 (defun sotlisp-comment-or-uncomment-sexp (&optional n)
678 "Comment the sexp at point and move past it.
679 If already inside (or before) a comment, uncomment instead.
680 With a prefix argument N, (un)comment that many sexps."
681 (interactive "P")
682 (if (or (elt (syntax-ppss) 4)
683 (< (save-excursion
684 (skip-chars-forward "\r\n[:blank:]")
685 (point))
686 (save-excursion
687 (comment-forward 1)
688 (point))))
689 (sotlisp-uncomment-sexp n)
690 (dotimes (_ (or n 1))
691 (sotlisp--comment-sexp-raw))))
692
693 (provide 'sotlisp)
694 ;;; sotlisp.el ends here
695