Mostly use smartparens

This commit is contained in:
David Morgan 2022-11-01 16:27:42 +00:00
parent 45c2459c8e
commit aba1ab3191
Signed by: djm
GPG Key ID: C171251002C200F2
6 changed files with 54 additions and 231 deletions

View File

@ -52,7 +52,6 @@
(require! 'init-smartparens)
(require! 'init-emacs-lisp)
(require! 'init-clojure)
(require! 'init-paredit)
(require! 'init-crux)
(require! 'init-lsp)
(require! 'init-git)

View File

@ -28,7 +28,7 @@
;; https://www.emacswiki.org/emacs/HippieExpand#h5o-9
(defadvice he-substitute-string (after he-paredit-fix)
"Remove extra paren when expanding line in paredit."
(if (and paredit-mode (equal (substring str -1) ")"))
(if (and (or smartparens-mode paredit-mode) (equal (substring str -1) ")"))
(progn (backward-delete-char 1) (forward-char))))
:bind
("C-M-/" . hippie-expand))

View File

@ -17,7 +17,6 @@
(t (list (line-beginning-position) (line-beginning-position 2)))))))
(crux-with-region-or-sexp-or-line sp-kill-region)
(crux-with-region-or-sexp-or-line paredit-kill-region)
(crux-with-region-or-buffer shell-command-on-region)
(crux-with-region-or-buffer indent-region)
(crux-with-region-or-buffer untabify)

View File

@ -1,191 +0,0 @@
;;; init-paredit.el --- Paredit Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;; add-hooks/add-lisp-hook based on https://github.com/bodil/emacs.d/blob/master/bodil/bodil-lisp.el
;;; Code:
(defvar-local lisp-modes
'(scheme-mode emacs-lisp-mode lisp-mode clojure-mode cider-repl-mode
eval-expression-minibuffer-setup ielm-mode lisp-interaction-mode))
(use-package emacs
:config
(defun add-hooks (modes func)
(dolist (mode modes)
(add-hook (intern (concat (symbol-name mode) "-hook")) func)))
(defun add-lisp-hook (func)
(add-hooks lisp-modes func)))
(use-package paredit
:diminish
:bind
(:map paredit-mode-map
([remap mark-sexp] . sp-mark-sexp)
("M-[" . paredit-smart-wrap-square)
("C-c M-{" . paredit-smart-wrap-curly)
("C-c M-<" . paredit-smart-wrap-angled)
([remap paredit-wrap-round] . paredit-smart-wrap-round)
([remap paredit-meta-doublequote] . paredit-smart-metadouble-quote)
([remap paredit-splice-sexp] . paredit-smart-splice-sexp)
("M-W" . paredit-copy-as-kill)
("C-S-k" . paredit-kill-region))
:config
(defmacro define-paredit-smart-wrap (name)
`(defun ,(intern (concat "paredit-smart-wrap-" name))
(&optional argument)
,(concat "Wrap the following S-expression, from the beginning of the current symbol.
See `paredit-wrap-sexp' for more details.
Falls back to smartparens in comments and strings.")
(interactive "P")
(if (or (paredit-in-string-p)
(paredit-in-comment-p)
(paredit-in-char-p))
(,(intern (concat "sp-wrap-" name)))
(beginning-of-thing 'symbol)
(,(intern (concat "paredit-wrap-" name)) argument))))
(define-paredit-smart-wrap "round")
(define-paredit-smart-wrap "curly")
(define-paredit-smart-wrap "square")
(define-paredit-smart-wrap "angled")
;; paredit-meta-doublequote is not like the wrap functions (but can act as one)
(defun paredit-smart-metadouble-quote (&optional n)
"Move to the end of the string.
If not in a string, act as `paredit-doublequote'; if not prefix argument
is specified and the region is not active or `transient-mark-mode' is
disabled, the default is to wrap one S-expression, however, not zero.
If wrapping, move to the beginning of the symbol first.
Falls back to smartparens in comments."
(interactive "P")
(if (paredit-in-comment-p)
(sp-wrap-double-quotation-marks)
(when (not (paredit-in-string-p))
(beginning-of-thing 'symbol))
(paredit-meta-doublequote n)))
(defmacro define-paredit-smart-sexp (name)
`(defun ,(intern (concat "paredit-smart-" name "-sexp"))
(&optional argument)
,(concat "Splice the list that the point is on by removing its delimiters.
This version falls back to the smartparens version in scenarios that paredit will
not handle.")
(interactive "P")
(if (or (paredit-in-string-p)
(paredit-in-comment-p)
(paredit-in-char-p))
(,(intern (concat "sp-" name "-sexp")))
(,(intern (concat "paredit-" name "-sexp")) argument))))
(define-paredit-smart-sexp "splice")
:init
;; From emacswiki - extreme barfage & slurpage
(defun paredit-barf-all-the-way-backward ()
(interactive)
(paredit-split-sexp)
(paredit-backward-down)
(paredit-splice-sexp))
(defun paredit-barf-all-the-way-forward ()
(interactive)
(paredit-split-sexp)
(paredit-forward-down)
(paredit-splice-sexp)
(if (eolp) (delete-horizontal-space)))
(defun paredit-slurp-all-the-way-backward ()
(interactive)
(catch 'done
(while (not (bobp))
(save-excursion
(paredit-backward-up)
(if (eq (char-before) ?\()
(throw 'done t)))
(paredit-backward-slurp-sexp))))
(defun paredit-slurp-all-the-way-forward ()
(interactive)
(catch 'done
(while (not (eobp))
(save-excursion
(paredit-forward-up)
(if (eq (char-after) ?\))
(throw 'done t)))
(paredit-forward-slurp-sexp))))
;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
;; Inverse M-(
(defun paredit-wrap-round-from-behind ()
(interactive)
(forward-sexp -1)
(paredit-wrap-round)
(insert " ")
(forward-char -1))
;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
;; Duplicate sexp
(defun paredit-duplicate-after-point ()
"Duplicates the content of the line that is after the point."
(interactive)
;; skips to the next sexp
(while (looking-at " ")
(forward-char))
(set-mark-command nil)
;; while we find sexps we move forward on the line
(while (and (<= (point) (car (bounds-of-thing-at-point 'sexp)))
(not (= (point) (line-end-position))))
(forward-sexp)
(while (looking-at " ")
(forward-char)))
(kill-ring-save (mark) (point))
;; go to the next line and copy the sexprs we encountered
(paredit-newline)
(set-mark-command nil)
(yank)
(exchange-point-and-mark))
(defun replace-paredit-binding (commands original-keys new-keys)
(when commands
(if (and (consp (car commands))
(equal (caar commands) original-keys))
(setcar (car commands) new-keys)
(replace-paredit-binding (cdr commands) original-keys new-keys))))
(defun paredit-commands-advice ()
;; Default paredit-convolute-sexp binding clashes with xref-find-references
(replace-paredit-binding paredit-commands "M-?" "C-c M-?")
(nconc paredit-commands
'("Extreme Barfage & Slurpage"
(("C-M-)")
paredit-slurp-all-the-way-forward
("(foo (bar |baz) quux zot)"
"(foo (bar |baz quux zot))")
("(a b ((c| d)) e f)"
"(a b ((c| d)) e f)"))
(("C-M-}" "M-F")
paredit-barf-all-the-way-forward
("(foo (bar |baz quux) zot)"
"(foo (bar|) baz quux zot)"))
(("C-M-(")
paredit-slurp-all-the-way-backward
("(foo bar (baz| quux) zot)"
"((foo bar baz| quux) zot)")
("(a b ((c| d)) e f)"
"(a b ((c| d)) e f)"))
(("C-M-{" "M-B")
paredit-barf-all-the-way-backward
("(foo (bar baz |quux) zot)"
"(foo bar baz (|quux) zot)")))
'("Extra"
(("C-c M-)")
paredit-wrap-round-from-behind
("(foo| bar baz" "((| foo) bar baz"))
(("C-c C-S-d")
paredit-duplicate-after-point
("|(foo)" "(foo)\n|(foo)"))))
;; Only need to do this once
(advice-remove 'paredit-define-keys 'paredit-commands-advice))
(advice-add 'paredit-define-keys :before 'paredit-commands-advice)
(add-lisp-hook #'turn-off-smartparens-mode)
(add-lisp-hook #'enable-paredit-mode))
(provide 'init-paredit)
;;; init-paredit.el ends here

View File

@ -2,53 +2,70 @@
;;; Commentary:
;;; Code:
(use-package paredit
:commands (paredit-semicolon paredit-comment-dwim))
(use-package smartparens
:diminish
:custom
(sp-base-key-bindings 'paredit)
(sp-autoskip-closing-pair 'always)
(sp-hybrid-kill-entire-symbol t)
(sp-hybrid-kill-excessive-whitespace t)
(sp-hybrid-kill-excessive-whitespace nil)
:hook (after-init . (lambda ()
(smartparens-global-strict-mode)
(show-smartparens-global-mode)
(setq sp-paredit-bindings (delete '("M-?" . sp-convolute-sexp) sp-paredit-bindings))
(require 'smartparens-config)
(sp-use-paredit-bindings)))
:config
(defun sp-wrap-double-quotation-marks ()
(sp-pair "\"" "\"" :wrap "M-\"")
;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
(defun duplicate-sexp-after-point ()
"Duplicates the content of the line that is after the point."
(interactive)
(sp-wrap-with-pair "\""))
;; https://github.com/syl20bnr/spacemacs/blob/develop/layers/+spacemacs/spacemacs-editing/funcs.el (spacemacs/smart-closing-parenthesis)
;; TODO can make things unbalanced
(defun sp-close-round ()
;; skips to the next sexp
(while (looking-at " ")
(forward-char))
(set-mark-command nil)
;; while we find sexps we move forward on the line
(while (and (<= (point) (car (bounds-of-thing-at-point 'sexp)))
(not (= (point) (line-end-position))))
(forward-sexp)
(while (looking-at " ")
(forward-char)))
(kill-ring-save (mark) (point))
;; go to the next line and copy the sexps we encountered
(sp-newline)
(set-mark-command nil)
(yank)
(exchange-point-and-mark))
;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
;; Inverse M-(
(defun wrap-round-from-behind ()
"Wrap the previous sexp before the point with ()."
(interactive)
(let* ((sp-navigate-close-if-unbalanced t)
(current-pos (point))
(current-line (line-number-at-pos current-pos))
next-pos next-line)
(save-excursion
(let ((buffer-undo-list)
(modified (buffer-modified-p)))
(unwind-protect
(progn
(sp-up-sexp)
(setq next-pos (point)
next-line (line-number-at-pos)))
(primitive-undo (length buffer-undo-list)
buffer-undo-list)
(set-buffer-modified-p modified))))
(cond
((and (= current-line next-line)
(not (= current-pos next-pos)))
(sp-up-sexp))
(t
(insert-char ?\))))))
(smartparens-global-strict-mode)
(show-smartparens-global-mode)
(require 'smartparens-config)
(sp-use-paredit-bindings)
(forward-sexp -1)
(sp-wrap-round)
(insert " ")
(forward-char -1))
(unbind-key "M-?" 'smartparens-mode-map)
:commands sp-local-pair
(unbind-key "M-?" 'sp-keymap)
:bind (:map smartparens-mode-map
("C-M-?" . sp-convolute-sexp)
;; (")" . sp-close-round)
("M-\"" . sp-wrap-double-quotation-marks)))
([remap mark-sexp] . sp-mark-sexp)
("M-[" . paredit-smart-wrap-square)
("C-c M-{" . sp-wrap-curly)
("M-W" . sp-copy-sexp)
(")" . paredit-close-round)
("]" . paredit-close-square)
("}" . paredit-close-curly)
(";" . paredit-semicolon)
("M-;" . paredit-comment-dwim)
("M-q" . sp-indent-defun)
("C-j" . sp-newline)
("C-c C-S-d" . duplicate-sexp-after-point)
("C-c M-(" . wrap-round-from-behind)))
(provide 'init-smartparens)
;;; init-smartparens.el ends here

View File

@ -220,8 +220,7 @@
:config
(defun eval-expr-minibuffer-setup ()
(local-set-key (kbd "<tab>") #'lisp-complete-symbol)
(set-syntax-table emacs-lisp-mode-syntax-table)
(paredit-mode)))
(set-syntax-table emacs-lisp-mode-syntax-table)))
(provide 'init-ui)
;;; init-ui.el ends here