[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CC Mode and electric-pair "problem".

From: João Távora
Subject: Re: CC Mode and electric-pair "problem".
Date: Tue, 19 Jun 2018 02:35:03 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (darwin)

João Távora <address@hidden> writes:

>> It is what I said at the end of my previous post.  e-p-m assumes that
>> whitespace has "neutral" syntax.  When it doesn't (like here, with a
>> string-fence property), the scan-sexps doesn't work as desired.  I'm
>> convinced this could be changed.
> OK. I'll have a look (I admit to not having looked at that code in depth
> since I wrote it 5 years ago).  I was pretty much convinced it was
> flawless :-)

I looked at this code and decided its better to leave it.  I don't need
it to fix the problem at hand.  I may change this opinion, so I started
by pushing this change that we need regardless:

   commit 6353387835f6cb34765ac525ac3e9edf3239e589
       Electric-pair-mode lets modes choose how to skip whitespace
       * lisp/elec-pair.el (electric-pair-skip-whitespace-function): New 
buffer-local variable.
       (electric-pair-post-self-insert-function): Call it.

Then, I defined this function

    (defun c-mode-electric-skip-whitespace ()
      "CC-mode's way of skipping whitespace."
      (let ((saved (point))
            (in-comment (nth 4 (syntax-ppss))))
        ;; actually if you also skip backslash here, you'll skip/chomp
        ;; over newline escapes, which may be nice.
        (skip-chars-forward (apply #'string 
        (unless (or (not in-comment)
                    (nth 4 (syntax-ppss)))
          (goto-char saved))))

And added this to c-mode-common-hook:

   (add-hook 'c-mode-common-hook
             (lambda ()
               (setq-local electric-pair-skip-whitespace-function
               (add-function :around
                             (local 'electric-pair-skip-self)
                             (lambda (&rest r)
                               (let (terminator)
                                 (if (and (setq terminator
                                                (nth 3 (syntax-ppss)))
                                            (goto-char (1- (line-end-position)))
                                            (and (eq terminator
                                                     (nth 3 (syntax-ppss)))
                                                 (not (eq terminator
                                   (apply r)))))))

All e-p-m tests pass, though the detection of NL-terminated string is
very shady (but you probably have much better ways inside CC-mode to
detect them).  If your "fix-scan-sexps" idea above works (I don't
understand it) then the add-function won't be needed at all.

Let me know what you think,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]