init-smartparens.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; init-smartparens.el --- Smartparens Configuration File -*- lexical-binding: t -*-
  2. ;;; Commentary:
  3. ;;; Code:
  4. (use-package paredit
  5. :commands (paredit-semicolon
  6. paredit-comment-dwim
  7. paredit-close-round
  8. paredit-close-square
  9. paredit-close-curly))
  10. (use-package smartparens
  11. :diminish
  12. :custom
  13. (sp-base-key-bindings 'paredit)
  14. (sp-autoskip-closing-pair 'always)
  15. (sp-hybrid-kill-entire-symbol t)
  16. (sp-hybrid-kill-excessive-whitespace nil)
  17. :hook (elpaca-after-init . (lambda ()
  18. (smartparens-global-strict-mode)
  19. (show-smartparens-global-mode)
  20. (setq sp-paredit-bindings (delete '("M-?" . sp-convolute-sexp) sp-paredit-bindings))
  21. (require 'smartparens-config)
  22. (sp-use-paredit-bindings)))
  23. :config
  24. (sp-pair "\"" "\"" :wrap "M-\"")
  25. ;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
  26. (defun duplicate-sexp-after-point ()
  27. "Duplicates the content of the line that is after the point."
  28. (interactive)
  29. ;; skips to the next sexp
  30. (while (looking-at " ")
  31. (forward-char))
  32. (set-mark-command nil)
  33. ;; while we find sexps we move forward on the line
  34. (while (and (<= (point) (car (bounds-of-thing-at-point 'sexp)))
  35. (not (= (point) (line-end-position))))
  36. (forward-sexp)
  37. (while (looking-at " ")
  38. (forward-char)))
  39. (kill-ring-save (mark) (point))
  40. ;; go to the next line and copy the sexps we encountered
  41. (sp-newline)
  42. (set-mark-command nil)
  43. (yank)
  44. (exchange-point-and-mark))
  45. ;; From https://github.com/bodil/emacs.d/blob/master/bodil/bodil-paredit.el
  46. ;; Inverse M-(
  47. (defun wrap-round-from-behind ()
  48. "Wrap the previous sexp before the point with ()."
  49. (interactive)
  50. (forward-sexp -1)
  51. (sp-wrap-round)
  52. (insert " ")
  53. (forward-char -1))
  54. (defun kill-around-sexp ()
  55. "Kill everything in the current list, except the current expression.
  56. Equivalent to raising then wrapping."
  57. (interactive)
  58. (let ((paren-char (save-excursion
  59. (sp-backward-up-sexp)
  60. (following-char))))
  61. (sp-raise-sexp)
  62. (cond ((= paren-char ?\() (sp-wrap-round))
  63. ((= paren-char ?\{) (sp-wrap-curly))
  64. ((= paren-char ?\[) (sp-wrap-square))
  65. (t (error "Not in a list")))))
  66. (defun sp-mark-sexp-advice (origin &rest args)
  67. (if (sp--raw-argument-p (car args))
  68. (push-mark
  69. (save-excursion
  70. (sp-end-of-sexp)
  71. (point))
  72. nil t)
  73. (apply origin args)))
  74. (advice-add 'sp-mark-sexp :around 'sp-mark-sexp-advice)
  75. (defun sp-fwd-sexp (arg)
  76. (interactive "^P")
  77. (if (sp--raw-argument-p arg)
  78. (sp-end-of-sexp)
  79. (call-interactively 'sp-forward-sexp arg)))
  80. (defun sp-bwd-sexp (arg)
  81. (interactive "^P")
  82. (if (sp--raw-argument-p arg)
  83. (sp-beginning-of-sexp)
  84. (call-interactively 'sp-backward-sexp arg)))
  85. (unbind-key "M-?" 'smartparens-mode-map)
  86. (unbind-key "M-?" 'sp-keymap)
  87. :bind (:map smartparens-mode-map
  88. ("C-M-?" . sp-convolute-sexp)
  89. ([remap mark-sexp] . sp-mark-sexp)
  90. ("C-M-k" . sp-kill-sexp)
  91. ("M-K" . kill-sexp)
  92. ([remap sp-forward-sexp] . sp-fwd-sexp)
  93. ([remap sp-backward-sexp] . sp-bwd-sexp)
  94. ("M-[" . sp-wrap-square)
  95. ("C-M-{" . sp-wrap-curly)
  96. ("M-W" . sp-copy-sexp)
  97. (")" . paredit-close-round)
  98. ("]" . paredit-close-square)
  99. ("}" . paredit-close-curly)
  100. (";" . paredit-semicolon)
  101. ("M-;" . paredit-comment-dwim)
  102. ("M-q" . sp-indent-defun)
  103. ("C-j" . sp-newline)
  104. ("M-R" . kill-around-sexp)
  105. ("M-D" . sp-unwrap-sexp)
  106. ("C-M-S-d" . sp-backward-unwrap-sexp)
  107. ("C-c C-S-d" . duplicate-sexp-after-point)
  108. ("C-c M-(" . wrap-round-from-behind)))
  109. (provide 'init-smartparens)
  110. ;;; init-smartparens.el ends here