[Top][All Lists]

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

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

From: Stefan Monnier
Subject: Re: CC Mode and electric-pair "problem".
Date: Wed, 20 Jun 2018 10:16:05 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> How about this idea: we add a new syntax flag to Emacs, ", which
> terminates any open string, the same way the syntax > terminates any
> open comment.  We could then set this syntax flag on newline.

To me this looks like adding a hack to patch over another.

I don't think the new behavior of unclosed strings in CC-mode is worse
than the old one, but I don't think it's really better either: it's just
different (in some cases it's better in others it's worse).

So the problem I see with it is that it brings complexity in the code
with no real improvement in terms of behavior.  The bad-interaction with
electric-pair shows that this complexity has a real immediate cost.
The suggestion above suggests that this complexity may bring in yet
more complexity.

Me not happy.

If the purpose of the change is to address use cases such as Clément's:
> Sorry for jumping in a bit late.  Does that mean that after the changed an
> unclosed quote will only cause refontification up to the end of the line?
> That would be a very nice improvement.  I don't use electric-pair-mode, and
> as things currently stand inserting an unmatched quote applies
> font-lock-string-face to the entire buffer, which is a bit annoying.

How 'bout taking an approach that will have much fewer side-effects:
Instead of adding the complexity at the low-level of syntax-tables
to make strings "magically" terminate at EOL, hook into

    when inserting a ", add a matching " at EOL if needed, or remove
    the " that we added at EOL earlier.

Something like (guaranteed 100% tested, of course.  No animals were harmed):

    (add-hook 'post-self-insert-hook
        (lambda ()
          (when (memq last-command-event '(?\" ?\'))
              (let ((pos (point))
                    (ppss (syntax-ppss (line-end-position))))
                (when (and (nth 3 ppss)        ;; EOL within a string
                           (not (nth 5 ppss))) ;; EOL not escaped
                  (if (and (> (point) pos)
                           (eq last-command-event (char-before)))
                      ;; Remove extraneous unmatched " at EOL.
                      (delete-region (1- (point)) (point))
                    (insert last-command-event)))))))
        'append 'local)

I used `append` to try and make it interact better with electric-pair-mode.


reply via email to

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