Fuco1 / smartparens

Minor mode for Emacs that deals with parens pairs and tries to be smart about it.
GNU General Public License v3.0
1.84k stars 194 forks source link

suggestion to add words of warning about cperl-mode to the wiki/documentation #1128

Closed c-alpha closed 8 months ago

c-alpha commented 2 years ago

cperl-mode is quite proactive about doing things automatically for you. It thus can, and often does, conflict with smartparens. I am thus proposing to add a few words of warning to the wiki and documentation about these possible interferences.

Thus, if when typing { in a Perl file, you get {}}, it's time to "tame cpel-mode". Here is the configuration I ended up using:

(use-package cperl-mode
  :init
  ;; cperl-mode is preferred to perl-mode
  ;; "Brevity is the soul of wit" <foo at acm.org>
  (defalias 'perl-mode 'cperl-mode)
  ;; un-bind the various parentheses as balancing is ensured by
  ;; smartparens, and these bindings would interfere
  :bind (:map cperl-mode-map
              ("{" . nil)
              ("[" . nil)
              ("(" . nil)
              ("}" . nil)
              ("]" . nil)
              (")" . nil))
  :custom
  (cperl-hairy t)
  (cperl-electric-parens nil "don't interfere with smartparens")
  (cperl-highlight-variables-indiscriminately t "highlight scalar variables, too"))

When you see similar "doubling-up issues" with other characters, check cperl-mode-map whether there are any further key bindings you might want to remove. Also see #323.

Hope this helps.

c-alpha commented 1 year ago

ping?

Fuco1 commented 1 year ago

latex-mode does the same and there are mechanisms in smartparens to deal with this so that it "just works". I'll triage it and it will be fixed at some point.

c-alpha commented 1 year ago

Cool, many thanks for acting on this, @Fuco1 ! ❤️ 👍

titanofold commented 1 year ago

If helpful, here's the entirety of what I used to get satisfactory behavior.

(with-eval-after-load 'cperl-mode
  (setq cperl-invalid-face nil
    cperl-indent-parens-as-block t
    cperl-close-paren-offset (- cperl-indent-level)
        cperl-font-lock t
        cperl-electric-lbrace-space nil
        cperl-electric-parens nil
        cperl-electric-linefeed t
        cperl-electric-keywords t
        cperl-info-on-command-no-prompt t
        cperl-clobber-lisp-bindings t
        cperl-lazy-help-time 5)

  (define-key cperl-mode-map "{" nil)

  (with-eval-after-load 'smartparens
    (defun my-sp-cperl-indent (id action context)
      (cond ((eq context 'comment)
             (cperl-indent-for-comment))
            ((eq context 'code)
             (if (eq action 'wrap)
                 (cperl-indent-region
                  (sp-get sp-last-wrapped-region :beg)
                  (sp-get sp-last-wrapped-region :end))
               (cperl-indent-line)))))

    (sp-local-pair 'cperl-mode "{" nil :post-handlers '(:add my-sp-cperl-indent))))

Of particular interest is not enabling cperl-hairy and instead enable the specific features. Still, even without cperl-electric-parens, { gets bound so it needs to be unbound.

I'm not really certain what cperl-indent-for-comment does as nothing seems to change with comment indenting, but that is my preference.

At any rate, I get the correct number of braces/parens, and everything is indented nicely as I edit.

Fuco1 commented 8 months ago

I added a page to the wiki with link back to this issue.