From 30f444a4a172d1bdb1cd2896e8965eef7634dd42 Mon Sep 17 00:00:00 2001 From: Gerard Vermeulen Date: Fri, 14 Jun 2024 21:06:13 +0200 Subject: [PATCH] Improve the `smartparens' setup and add `sp-eval-expression' --- README.org | 75 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/README.org b/README.org index 09f088e..02b4f33 100644 --- a/README.org +++ b/README.org @@ -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 how the machinery after the first and after later usages of =eval-expression= 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, -Lisp dialects, Org, and Python. Execute src_emacs-lisp[:results +Listing [[lst:setup-smartparens]] aims to setup [[https://github.com/Fuco1/smartparens][smartparens]] for Go, LaTeX, Lisp +dialects, Org, and Python. Execute src_emacs-lisp[:results 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 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?"]], [[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, contrary to for instance [[https://github.com/Fanael/rainbow-delimiters#readme][rainbow-delimiters]]. -#+caption[Configure =smartparens=]: -#+caption: Configure =smartparens=. -#+name: lst:configure-smartparens +#+caption[Setup =smartparens=]: +#+caption: Setup =smartparens=. +#+name: lst:setup-smartparens #+begin_src emacs-lisp -n :results silent -(when (and (ensure-package-installation 'smartparens) - ;; Require `smartparens-config' instead of `smartparens' to - ;; disable pairing of the quote character for lisp modes. - (require 'smartparens-config nil 'noerror)) +(when (ensure-package-installation 'smartparens) + ;; GAV: Documentation says to require `smartparens-config'. + (require 'smartparens-config) (setopt sp-base-key-bindings 'sp sp-override-key-bindings '(("C-(" . sp-backward-slurp-sexp) ("C-)" . sp-forward-slurp-sexp) ("C-M-(" . sp-backward-barf-sexp) ("C-M-)" . sp-forward-barf-sexp))) - (when (fboundp 'smartparens-mode) - (dolist (symbol '(conf-toml-mode-hook prog-mode-hook text-mode-hook)) - (add-hook symbol #'smartparens-mode))) + (add-hook 'conf-toml-mode-hook #'smartparens-mode) + (add-hook 'prog-mode-hook #'smartparens-mode) + (add-hook 'text-mode-hook #'smartparens-mode) - (when (fboundp 'smartparens-strict-mode) - (dolist (symbol '(emacs-lisp-mode-hook - go-ts-mode-hook - ielm-mode-hook - inferior-python-mode-hook - lisp-data-mode-hook - lisp-mode-hook - python-mode-hook - sly-mrepl-mode-hook)) - (add-hook symbol #'smartparens-strict-mode))) + (add-hook 'emacs-lisp-mode-hook #'smartparens-strict-mode) + (add-hook 'go-ts-mode-hook #'smartparens-strict-mode) + (add-hook 'ielm-mode-hook #'smartparens-strict-mode) + (add-hook 'inferior-python-mode-hook #'smartparens-strict-mode) + (add-hook 'lisp-data-mode-hook #'smartparens-strict-mode) + (add-hook 'lisp-mode-hook #'smartparens-strict-mode) + (add-hook 'python-mode-hook #'smartparens-strict-mode) + (add-hook 'sly-mrepl-mode-hook #'smartparens-strict-mode) (when (fboundp 'go-ts-mode) ;; 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)}}} | | |--------------------------------+----------------------------+----------| +#+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]] :PROPERTIES: