From f9e488cb0ce5dd4f75c723a5239d96686b0d6906 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 28 Mar 2016 21:39:13 -0400 Subject: [PATCH] Move smartparens configs to modules --- core/core-editor.el | 57 ++----------------------------- core/defuns/defuns-smartparens.el | 11 ++++++ modules/defuns/defuns-lua.el | 10 ++++++ modules/defuns/defuns-org.el | 7 ++++ modules/module-cc.el | 12 +++++++ modules/module-lua.el | 18 +++++++--- modules/module-org.el | 15 +++++++- modules/module-php.el | 8 +++++ modules/module-sh.el | 9 ++++- modules/module-text.el | 4 +++ modules/module-web.el | 8 +++++ 11 files changed, 98 insertions(+), 61 deletions(-) create mode 100644 core/defuns/defuns-smartparens.el create mode 100644 modules/defuns/defuns-lua.el diff --git a/core/core-editor.el b/core/core-editor.el index 9460ec645..8e78fbcb2 100644 --- a/core/core-editor.el +++ b/core/core-editor.el @@ -237,6 +237,7 @@ enable multiple minor modes for the same regexp.") (add-hook 'evil-replace-state-exit-hook 'turn-on-smartparens-mode) ;; Auto-close more conservatively + (sp-pair "'" nil :unless '(sp-point-after-word-p)) (sp-pair "{" nil :post-handlers '(("||\n[i]" "RET") ("| " " ")) :unless '(sp-point-before-word-p sp-point-before-same-p)) (sp-pair "(" nil :post-handlers '(("||\n[i]" "RET") ("| " " ")) @@ -244,64 +245,12 @@ enable multiple minor modes for the same regexp.") (sp-pair "[" nil :post-handlers '(("| " " ")) :unless '(sp-point-before-word-p sp-point-before-same-p)) - (defun sp-point-in-string-p (id action context) - (when (eq action 'insert) - (sp-point-in-string))) - - (defun sp-insert-yasnippet (id action context) - (forward-char -1) - (if (sp-point-after-bol-p id action context) - (yas-expand-from-trigger-key) - (forward-char))) - + (sp-local-pair 'css-mode "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC"))) (sp-local-pair '(sh-mode markdown-mode) "`" "`" :unless '(sp-point-before-word-p sp-point-before-same-p)) - (sp-local-pair 'markdown-mode "```" "```" :post-handlers '(("||\n[i]" "RET")) :unless '(sp-point-before-word-p sp-point-before-same-p)) - - (sp-local-pair '(scss-mode css-mode) "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC"))) - - (sp-with-modes '(sh-mode) - (sp-local-pair "case" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert)) - (sp-local-pair "if" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert)) - (sp-local-pair "for" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert)) - (sp-local-pair "elif" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert)) - (sp-local-pair "while" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert))) - - (sp-with-modes '(c-mode c++-mode objc-mode php-mode java-mode) - (sp-local-pair "/*" "*/" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) - - ;; Doxygen blocks - (sp-local-pair "/**" "*/" :post-handlers '(("||\n[i]" "RET") ("||\n[i]" "SPC"))) - (sp-local-pair "/*!" "*/" :post-handlers '(("||\n[i]" "RET") ("[d-1]< | " "SPC")))) - - (defun sp--org-skip-asterisk (ms mb me) - (or (and (= (line-beginning-position) mb) - (eq 32 (char-after (1+ mb)))) - (and (= (1+ (line-beginning-position)) me) - (eq 32 (char-after me))))) - - (sp-with-modes '(org-mode) - (sp-local-pair "*" "*" :unless '(sp-point-after-word-p sp-point-at-bol-p) :skip-match 'sp--org-skip-asterisk) - (sp-local-pair "_" "_" :unless '(sp-point-before-word-p sp-point-after-word-p)) - (sp-local-pair "/" "/" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) - (sp-local-pair "~" "~" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) - (sp-local-pair "=" "=" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) - - (sp-local-pair "\\[" "\\]" :post-handlers '(("| " "SPC"))) - (sp-local-pair "\\(" "\\)" :post-handlers '(("| " "SPC"))) - (sp-local-pair "$$" "$$" :post-handlers '((:add " | ")) :unless '(sp-point-at-bol-p)) - (sp-local-pair "{" nil)) ;; Markup languages (sp-with-modes '(xml-mode nxml-mode php-mode) - (sp-local-pair "" :post-handlers '(("| " "SPC")))) - (sp-with-modes '(web-mode php-mode) - (sp-local-pair "" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) - (sp-local-pair "" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))) - (sp-with-modes '(web-mode) - (sp-local-pair "{{!--" "--}}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) - (sp-local-pair "<%" "%>" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) - (sp-local-pair "{!!" "!!}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) - (sp-local-pair "{#" "#}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))))) + (sp-local-pair "" :post-handlers '(("| " "SPC"))))) (use-package help-fns+ ; Improved help commands :commands (describe-buffer describe-command describe-file diff --git a/core/defuns/defuns-smartparens.el b/core/defuns/defuns-smartparens.el new file mode 100644 index 000000000..3ab61b136 --- /dev/null +++ b/core/defuns/defuns-smartparens.el @@ -0,0 +1,11 @@ +;;; defuns-smartparens.el + +;;;###autoload +(defun narf/sp-insert-yasnippet (id action context) + (forward-char -1) + (if (sp-point-after-bol-p id action context) + (yas-expand-from-trigger-key) + (forward-char))) + +(provide 'defuns-smartparens) +;;; defuns-smartparens.el ends here diff --git a/modules/defuns/defuns-lua.el b/modules/defuns/defuns-lua.el new file mode 100644 index 000000000..0fd845bd2 --- /dev/null +++ b/modules/defuns/defuns-lua.el @@ -0,0 +1,10 @@ +;;; defuns-lua.el + +;;;###autoload +(defun narf-inf-lua () + (interactive) + (lua-start-process "lua" "lua") + (pop-to-buffer lua-process-buffer)) + +(provide 'defuns-lua) +;;; defuns-lua.el ends here diff --git a/modules/defuns/defuns-org.el b/modules/defuns/defuns-org.el index f69f5314e..fcf69a929 100644 --- a/modules/defuns/defuns-org.el +++ b/modules/defuns/defuns-org.el @@ -350,5 +350,12 @@ re-align the table if necessary. (Necessary because org-mode has a (end evil-visual-end)) (org-insert-link nil link (when (and beg end) (buffer-substring-no-properties beg end))))) +;;;###autoload +(defun narf/sp-org-skip-asterisk (ms mb me) + (or (and (= (line-beginning-position) mb) + (eq 32 (char-after (1+ mb)))) + (and (= (1+ (line-beginning-position)) me) + (eq 32 (char-after me))))) + (provide 'defuns-org) ;;; defuns-org.el ends here diff --git a/modules/module-cc.el b/modules/module-cc.el index d183e3636..885ab788b 100644 --- a/modules/module-cc.el +++ b/modules/module-cc.el @@ -27,6 +27,18 @@ c-tab-always-indent nil c-electric-flag nil) + (defun narf/sp-point-is-template-p (id action context) + (and (sp-in-code-p id action context) + (sp-point-after-word-p id action context))) + + (sp-local-pair 'c++-mode "<" ">" :when '(narf/sp-point-is-template-p)) + (sp-with-modes '(c-mode c++-mode objc-mode java-mode) + (sp-local-pair "/*" "*/" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + + ;; Doxygen blocks + (sp-local-pair "/**" "*/" :post-handlers '(("||\n[i]" "RET") ("||\n[i]" "SPC"))) + (sp-local-pair "/*!" "*/" :post-handlers '(("||\n[i]" "RET") ("[d-1]< | " "SPC")))) + ;; C/C++ Settings (add-hook! (c-mode c++-mode) (electric-indent-local-mode +1) diff --git a/modules/module-lua.el b/modules/module-lua.el index 8847098e4..d0b4adf8c 100644 --- a/modules/module-lua.el +++ b/modules/module-lua.el @@ -4,18 +4,26 @@ :mode "\\.lua$" :interpreter "lua" :init - (define-repl! lua-mode narf-inf-lua) + (define-repl! lua-mode narf/inf-lua) (add-hook! lua-mode 'flycheck-mode) (after! company-dict (add-to-list 'company-dict-minor-mode-list 'love-mode)) - (add-hook! lua-mode (electric-indent-local-mode +1) (setq narf-electric-indent-words '("else" "end"))) + :config + (sp-with-modes '(lua-mode) + ;; disable defaults + (sp-local-pair "if" nil :actions :rem) + (sp-local-pair "while" nil :actions :rem) + (sp-local-pair "function" nil :actions :rem) - (defun narf-inf-lua () - (lua-start-process "lua" "lua") - (pop-to-buffer lua-process-buffer))) + (sp-local-pair "then " " end") + (sp-local-pair "do " " end") + (sp-local-pair "then" "end" :when '(("RET")) :post-handlers '("||\n[i]")) + (sp-local-pair "do" "end" :when '(("RET")) :post-handlers '("||\n[i]")) + + (sp-local-pair "function" "end" :post-handlers '((" |()\n[i]\n" "RET") ("|() " "SPC"))))) (define-minor-mode love-mode "Buffer local minor mode for Love2D" diff --git a/modules/module-org.el b/modules/module-org.el index 5a4a51c99..ea953f912 100644 --- a/modules/module-org.el +++ b/modules/module-org.el @@ -194,7 +194,20 @@ (define-text-object! "/" "/" "/") (define-text-object! "_" "_" "_") (define-text-object! "=" "=" "=") - (define-text-object! "~" "~" "~"))) + (define-text-object! "~" "~" "~")) + + ;; smartparens config + (sp-with-modes '(org-mode) + (sp-local-pair "*" "*" :unless '(sp-point-after-word-p sp-point-at-bol-p) :skip-match 'narf/sp-org-skip-asterisk) + (sp-local-pair "_" "_" :unless '(sp-point-before-word-p sp-point-after-word-p)) + (sp-local-pair "/" "/" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) + (sp-local-pair "~" "~" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) + (sp-local-pair "=" "=" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC"))) + + (sp-local-pair "\\[" "\\]" :post-handlers '(("| " "SPC"))) + (sp-local-pair "\\(" "\\)" :post-handlers '(("| " "SPC"))) + (sp-local-pair "$$" "$$" :post-handlers '((:add " | ")) :unless '(sp-point-at-bol-p)) + (sp-local-pair "{" nil))) (defun narf|org-keybinds () (define-key org-mode-map (kbd "RET") nil) diff --git a/modules/module-php.el b/modules/module-php.el index c2be8f112..61e2083cc 100644 --- a/modules/module-php.el +++ b/modules/module-php.el @@ -23,6 +23,14 @@ (load (concat php-extras-eldoc-functions-file ".el")) (message "PHP eldoc updated!")))) + (sp-with-modes '(php-mode) + (sp-local-pair "/*" "*/" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + (sp-local-pair "/**" "*/" :post-handlers '(("||\n[i]" "RET") ("||\n[i]" "SPC"))) + (sp-local-pair "" :post-handlers '(("||\n[i]" "RET") ("| " "SPC") ("| " "="))) + (sp-local-pair "" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + (sp-local-pair "" :when '(("RET")) :post-handlers '("||\n[i]")) + (sp-local-pair "" :when '(("RET")) :post-handlers '("||\n[i]"))) + (use-package php-refactor-mode :init (add-hook! php-mode '(turn-on-eldoc-mode emr-initialize php-refactor-mode)) diff --git a/modules/module-sh.el b/modules/module-sh.el index 81470c79c..4a552ef5e 100644 --- a/modules/module-sh.el +++ b/modules/module-sh.el @@ -24,7 +24,14 @@ (require 'company-shell) ;; Fontify variables in strings - (add-hook 'sh-mode-hook 'narf|sh-extra-font-lock-activate)) + (add-hook 'sh-mode-hook 'narf|sh-extra-font-lock-activate) + + (sp-with-modes '(sh-mode) + (sp-local-pair "case" "" :when '(("SPC")) :post-handlers '((:add narf/sp-insert-yasnippet)) :actions '(insert)) + (sp-local-pair "if" "" :when '(("SPC")) :post-handlers '((:add narf/sp-insert-yasnippet)) :actions '(insert)) + (sp-local-pair "for" "" :when '(("SPC")) :post-handlers '((:add narf/sp-insert-yasnippet)) :actions '(insert)) + (sp-local-pair "elif" "" :when '(("SPC")) :post-handlers '((:add narf/sp-insert-yasnippet)) :actions '(insert)) + (sp-local-pair "while" "" :when '(("SPC")) :post-handlers '((:add narf/sp-insert-yasnippet)) :actions '(insert)))) (provide 'module-sh) ;;; module-sh.el ends here diff --git a/modules/module-text.el b/modules/module-text.el index 965aa9942..388c5f9f2 100644 --- a/modules/module-text.el +++ b/modules/module-text.el @@ -10,6 +10,10 @@ :init (add-hook! markdown-mode 'narf|enable-hard-wrap) :config + (sp-local-pair 'markdown-mode "```" "```" + :post-handlers '(("||\n[i]" "RET")) + :unless '(sp-point-before-word-p sp-point-before-same-p)) + (map! (:map markdown-mode-map "" nil "" nil diff --git a/modules/module-web.el b/modules/module-web.el index 689f524a4..24986e64c 100644 --- a/modules/module-web.el +++ b/modules/module-web.el @@ -17,6 +17,8 @@ (setq-default css-indent-offset 2) (setq scss-compile-at-save nil) :config + (sp-local-pair 'scss-mode "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC"))) + (map! :map scss-mode-map :n "M-r" 'narf/web-refresh-browser (:leader @@ -60,6 +62,12 @@ (setq web-mode-enable-auto-pairing nil web-mode-enable-auto-quoting nil) :config + (sp-with-modes '(web-mode) + (sp-local-pair "{{!--" "--}}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + (sp-local-pair "<%" "%>" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + (sp-local-pair "{!!" "!!}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))) + (sp-local-pair "{#" "#}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))) + (after! web-beautify (add-hook! web-mode (setenv "jsbeautify_indent_size" "4")) (map! :map web-mode-map :m "gQ" 'web-beautify-html))