emacs-devel
[Top][All Lists]
Advanced

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

Re: [patch] make electric-pair-mode smarter/more useful


From: João Távora
Subject: Re: [patch] make electric-pair-mode smarter/more useful
Date: Mon, 16 Dec 2013 14:21:47 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> I'm not completely set on using text-mode-syntax-table.  I think I can
> be convinced to try prog-mode-syntax-table.

Oh :) Read below I'll give the example you asked for :-)

>> The buffer situation "())", for example, has too many closers. But, to
> Maybe you could handle this by considering
>    (- (car (syntax-ppss))
>       (save-excursion (car (syntax-ppss (point-max))))
> But that doesn't solve the mixed-parens issue.

Interesting.

>> Your simplification for `electric-pair--looking-at-mismatched-string-p'
>> was also found to fail some tests
> It'd be useful to keep track of which ones.

Using this simplification:

  (save-excursion (eq char (nth 3 (syntax-ppss (point-max)))))

one gets 5 failures

   F electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-c++-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F 
electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-emacs-lisp-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-ruby-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F 
electric-pair-leave-unbalanced-quotes-alone-2-at-point-4-in-ruby-mode-in-comments
       With "#  "\"' ", try input " at point 4. Should become "#  ""\"' " and 
point at 5
   F 
electric-pair-leave-unbalanced-quotes-alone-at-point-4-in-ruby-mode-in-comments
       With "#  "' ", try input " at point 4. Should become "#  ""' " and point 
at 5

Using `electric-pair--syntax-ppss' fixes the last two but adds three
more (which are actually less becuase they're just mode-variations on the same 
test)

   F electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-c++-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F 
electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-emacs-lisp-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F electric-pair-inhibit-only-if-next-is-mismatched-at-point-1-in-ruby-mode
       With ""foo""bar", try input " at point 1. Should become """"foo""bar" 
and point at 2
   F 
electric-pair-inhibit-only-if-next-is-mismatched-at-point-3-in-ruby-mode-in-comments
       With "# "foo""bar", try input " at point 3. Should become "# 
"""foo""bar" and point at 4
   F 
electric-pair-inhibit-only-if-next-is-mismatched-at-point-4-in-c++-mode-in-comments
       With "// "foo""bar", try input " at point 4. Should become "// 
"""foo""bar" and point at 5
   F 
electric-pair-inhibit-only-if-next-is-mismatched-at-point-4-in-emacs-lisp-mode-in-comments
       With ";; "foo""bar", try input " at point 4. Should become ";; 
"""foo""bar" and point at 5

>> Here too, I've kind of changed my mind :-) Balancing can already be
>> turned off by customizing two variables:
>>   electric-pair-inhibit-predicate
>>   electric-pair-skip-self
> The thing is that the two kinda go together.  Having to tweak both
> together sounds like "coding" rather than "configuring".

Yeah, that's true.

> We could have the two functions check a new defcustom
> electric-pair-preserve-balance and if nil fallback on the old default.

Yes, that's what I meant, too. OK. So in the existing 2 variables custom
menu there will be basically 4 options:

- "always" or "never" (depending on whether it's pair or skip)
- "balance, maybe"    (the default we're discussing)
- "balance, always"
- "don't balance, be conservative"

>> Explained above again.  Using prog-mode-syntax-table allows me to get
>> some quote balancing in comments and strings.
> This is not really an example, let alone example*S*.  Which quotes?
> Why are they there?  Is it only for quotes?

OK. So Emacs -Q, M-x electric-pair-mode and then in the scratch buffer
go to some place in the comment's text and type a double quote. You get
it autopaired. If you type it again you get a skip. OK.

Now go back to the first quote and type it again. You get a skip. In my
opinion, not so nice. Delete the first quote, go back some words and
type another quote. You'll get an unbalanced string inside the
comment. Again not so nice.

Now if you do (setq electric-pair-text-syntax-table
prog-mode-syntax-table) and repeat the second paragraph, you'll get
results that I personally think are nicer.

BTW these are exactly the results that you mostly loose if you do the
`electric-pair--looking-at-mismatched-string-p' simplification above.

>> It also does when it detects it's in c-mode or c++-mode, since in my
>> testing syntax-ppss is sometimes broken there (tried in in
>> src/syntax.c)
>
> I'm not really surprised, sadly, but please report it as a bug (the fix
> is probably to make cc-mode use syntax-propertize-function, which is
> not a quick&easy fix since cc-mode currently sets the syntax-table in
> a very contorted way spread over various places and times).

OK.

> I don't understand why you'd want (not (or executing-kbd-macro
> noninteractive)) rather than any non-nil constant.  Where does this (not
> (or executing-kbd-macro noninteractive)) come from?

I read it in `called-interactively-p''s docstring... I mean, if one
calls `newline-and-indent' from lisp it shouldn't call newline with
interactive=t right?

>> +(put 'electric-pair-post-self-insert-function   'priority  20)
>> +(put 'electric-layout-post-self-insert-function 'priority  40)
>> +(put 'electric-indent-post-self-insert-function 'priority  60)
>> +(put 'blink-paren-post-self-insert-function     'priority 100)
> These belong next to the corresponding functions.

Do they? The relative order is between them, spreading them throughout
the buffer makes it difficult to read, doesn't it? I would agree to
spread if the priority spec was something like.

  (put 'electric-pair-post-self-insert-function
       'priority '(before electric-layout-post-self-insert-function))

and so on.

> Also, if you know why the order is this way, please add comments
> explaining it (I do know for blink-paren-post-self-insert-function and
> electric-indent-post-self-insert-function, but not sure why
> electric-layout-post-self-insert-function should come after
> electric-pair-post-self-insert-function rather than the opposite).

In js-mode layout rules, pairing must come before layout, there's a test
for that. Otherwise it was just a thumb rule. But OK, I can add comments
for the known dependencies.

>> +(defcustom electric-pair-skip-whitespace t
> This docstring still needs to be improved, because it still doesn't
> explain what really happens.  More specifically, which whitespace
> is skipped (before or after the skipped paren?).

OK.

> Other than those nitpicks, feel free to install those changes,

Hmmm, I don't know if I have write privs. I'll check. And I'll have to
check some bzr tutorials and README's.



reply via email to

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