Improve the smartparens' setup and add sp-eval-expression'

This commit is contained in:
Gerard Vermeulen 2024-06-14 21:06:13 +02:00
parent 8aa7843b42
commit 30f444a4a1

View File

@ -5624,13 +5624,15 @@ following links show how to put the documentation to practical use:
3. [[https://lists.gnu.org/archive/html/help-gnu-emacs/2014-07/msg00135.html][How to enable smartparens in the minibuffer after eval-expression]] explains 3. [[https://lists.gnu.org/archive/html/help-gnu-emacs/2014-07/msg00135.html][How to enable smartparens in the minibuffer after eval-expression]] explains
how the machinery after the first and after later usages of =eval-expression= how the machinery after the first and after later usages of =eval-expression=
differ and discusses options how to handle those differences. differ and discusses options how to handle those differences.
Listing [[lst:configure-smartparens]] aims to configure [[https://github.com/Fuco1/smartparens][smartparens]] for Go, LaTeX, Listing [[lst:setup-smartparens]] aims to setup [[https://github.com/Fuco1/smartparens][smartparens]] for Go, LaTeX, Lisp
Lisp dialects, Org, and Python. Execute src_emacs-lisp[:results dialects, Org, and Python. Execute src_emacs-lisp[:results
none]{(sp-cheat-sheet)} for short documentation taking into account the none]{(sp-cheat-sheet)} for short documentation taking into account the
overridden key bindings in listing [[lst:configure-smartparens]]. Table overridden key bindings in listing [[lst:setup-smartparens]]. Table
[[tab:smartparens-commands-and-bindings]] lists commands with key bindings taken in [[tab:smartparens-commands-and-bindings]] lists commands with key bindings taken in
order from src_emacs-lisp[:results none]{(sp-cheat-sheet)} that takes the order from src_emacs-lisp[:results none]{(sp-cheat-sheet)} that takes the
overrides of listing [[lst:configure-smartparens]] into account. overrides of listing [[lst:setup-smartparens]] into account. Finally, listing
[[lst:sp-eval-expression]] defines an alternative to =eval-expression= enabling
=smartparens-strict-mode= and =font-lock-mode=.
Despite the provocative post [[https://andreyorst.gitlab.io/posts/2022-02-20-what-if-structural-editing-was-a-mistake/]["What if structural editing was a mistake?"]], Despite the provocative post [[https://andreyorst.gitlab.io/posts/2022-02-20-what-if-structural-editing-was-a-mistake/]["What if structural editing was a mistake?"]],
[[https://github.com/Fuco1/smartparens][smartparens]] is one of my favorite packages. In particular, [[https://github.com/Fuco1/smartparens][smartparens]] handles [[https://github.com/Fuco1/smartparens][smartparens]] is one of my favorite packages. In particular, [[https://github.com/Fuco1/smartparens][smartparens]] handles
@ -5640,34 +5642,31 @@ inside source blocks) correctly. Therefore, [[https://smartparens.readthedocs.i
matching pairs immediately in front or after point in such files correctly, matching pairs immediately in front or after point in such files correctly,
contrary to for instance [[https://github.com/Fanael/rainbow-delimiters#readme][rainbow-delimiters]]. contrary to for instance [[https://github.com/Fanael/rainbow-delimiters#readme][rainbow-delimiters]].
#+caption[Configure =smartparens=]: #+caption[Setup =smartparens=]:
#+caption: Configure =smartparens=. #+caption: Setup =smartparens=.
#+name: lst:configure-smartparens #+name: lst:setup-smartparens
#+begin_src emacs-lisp -n :results silent #+begin_src emacs-lisp -n :results silent
(when (and (ensure-package-installation 'smartparens) (when (ensure-package-installation 'smartparens)
;; Require `smartparens-config' instead of `smartparens' to ;; GAV: Documentation says to require `smartparens-config'.
;; disable pairing of the quote character for lisp modes. (require 'smartparens-config)
(require 'smartparens-config nil 'noerror))
(setopt sp-base-key-bindings 'sp (setopt sp-base-key-bindings 'sp
sp-override-key-bindings '(("C-(" . sp-backward-slurp-sexp) sp-override-key-bindings '(("C-(" . sp-backward-slurp-sexp)
("C-)" . sp-forward-slurp-sexp) ("C-)" . sp-forward-slurp-sexp)
("C-M-(" . sp-backward-barf-sexp) ("C-M-(" . sp-backward-barf-sexp)
("C-M-)" . sp-forward-barf-sexp))) ("C-M-)" . sp-forward-barf-sexp)))
(when (fboundp 'smartparens-mode) (add-hook 'conf-toml-mode-hook #'smartparens-mode)
(dolist (symbol '(conf-toml-mode-hook prog-mode-hook text-mode-hook)) (add-hook 'prog-mode-hook #'smartparens-mode)
(add-hook symbol #'smartparens-mode))) (add-hook 'text-mode-hook #'smartparens-mode)
(when (fboundp 'smartparens-strict-mode) (add-hook 'emacs-lisp-mode-hook #'smartparens-strict-mode)
(dolist (symbol '(emacs-lisp-mode-hook (add-hook 'go-ts-mode-hook #'smartparens-strict-mode)
go-ts-mode-hook (add-hook 'ielm-mode-hook #'smartparens-strict-mode)
ielm-mode-hook (add-hook 'inferior-python-mode-hook #'smartparens-strict-mode)
inferior-python-mode-hook (add-hook 'lisp-data-mode-hook #'smartparens-strict-mode)
lisp-data-mode-hook (add-hook 'lisp-mode-hook #'smartparens-strict-mode)
lisp-mode-hook (add-hook 'python-mode-hook #'smartparens-strict-mode)
python-mode-hook (add-hook 'sly-mrepl-mode-hook #'smartparens-strict-mode)
sly-mrepl-mode-hook))
(add-hook symbol #'smartparens-strict-mode)))
(when (fboundp 'go-ts-mode) (when (fboundp 'go-ts-mode)
;; Stolen from `smartparens-go': ;; Stolen from `smartparens-go':
@ -5728,6 +5727,34 @@ contrary to for instance [[https://github.com/Fanael/rainbow-delimiters#readme][
| sp-mark-sexp | {{{kbd(C-M-SPC)}}} | | | sp-mark-sexp | {{{kbd(C-M-SPC)}}} | |
|--------------------------------+----------------------------+----------| |--------------------------------+----------------------------+----------|
#+caption[Define =sp-eval-expression= with =smartparens= support]:
#+caption: Define =sp-eval-expression= enabling =smartparens-strict-mode=
#+caption: and =font-lock-mode=.
#+name: lst:sp-eval-expression
#+begin_src emacs-lisp -n :results silent
(with-eval-after-load 'smartparens
;; https://lists.gnu.org/archive/html/help-gnu-emacs/2014-07/msg00135.html
;; GAV: Reuse `read--expresssion-map' instead of defining my own map.
(defun sp--read-expression (prompt &optional initial-contents)
(let ((minibuffer-completing-symbol t))
(minibuffer-with-setup-hook
(lambda ()
(emacs-lisp-mode) ; Enables `smartparens-strict-mode' too.
(use-local-map read--expression-map)
(font-lock-mode t))
(read-from-minibuffer prompt initial-contents
read--expression-map nil
'read-expression-history))))
(defun sp-eval-expression (expression &optional arg)
"Evaluate EXPRESSION with `smartparens' support."
(interactive (list (read (sp--read-expression "SP eval: "))
current-prefix-arg))
(if arg
(insert (pp-to-string (eval expression lexical-binding)))
(pp-display-expression (eval expression lexical-binding)
"*SP Eval Output*"))))
#+end_src
** [[https://github.com/davidshepherd7/electric-operator#readme][Electric operators]] ** [[https://github.com/davidshepherd7/electric-operator#readme][Electric operators]]
:PROPERTIES: :PROPERTIES: