Subject: bug#22761: Surprising interaction between font-lock, invisible text, and point (self-insert-command and insert behave differently)

From: Clément Pit--Claudel <[email-masked]>
Date: Mon, 22 Feb 2016 12:36:42 -0500

On 02/22/2016 11:38 AM, Eli Zaretskii wrote:
>> Cc: [email-masked] From: Clément Pit--Claudel
>> <[email-masked]> Date: Mon, 22 Feb 2016 11:03:03
>> -0500
>> 
>> On 02/22/2016 10:53 AM, Eli Zaretskii wrote:
>>>> From: Clément Pit--Claudel <[email-masked]> Date:
>>>> Sun, 21 Feb 2016 19:19:17 -0500
>>>> 
>>>> The problem happens when editing ‘a_foo_b’ to replace ‘foo’ by 
>>>> ‘bar’ (to get ‘a_bar_b’). Removing ‘foo’ leaves ‘__’ in the
>>>> buffer, which font-lock recognizes: the whole string gets
>>>> displayed as ‘ab’. Even then, the point is still between the
>>>> two underscores (it hasn't moved after font-lock added the
>>>> invisible property to the underscores). Pressing ‘b’ to start
>>>> inserting ‘bar’ works fine (I end up with ‘a_b_c’), except for
>>>> one thing: the point gets moved after the second underscore.
>>>> Thus, pressing ‘ar’ to complete ‘bar’ ends up inserting
>>>> ‘a_b_arb’.
>>> 
>>> Did you try binding global-disable-point-adjustment to a non-nil
>>>  value?
>> 
>> I imagine that it would work, but wouldn't it break other parts of
>> Emacs? The mode to which I'm adding this subscripts feature also
>> uses hideshow, for example, and I do want the point to be moved out
>> of invisible sections then.
> 
> If it works, perhaps you could bind disable-point-adjustment (not
> the global variable) in the insertion commands.
> 
> In general, insertion in the middle of invisible text is tricky at 
> best.  So perhaps you should rethink how you handle this situation
> -- you could, for example, temporarily remove the entire invisible 
> portion, until the insertion is complete.

Thanks, this is a good idea. It will also be more intuitive for users, probably.
I tried to follow the strategy that prettify-symbols-unprettify-at-point uses, but I kept running into issues; using an overlay seems to work better. The issue described in my OP is still there (when the buffer text is ‘__’ and an ‘a’ is added in the middle (yielding ‘_a_’) the overlay is removed, and Emacs moves the point after the second underscore (probably before fontification kicks in and removes the invisible property?). Still I'm better off, because I can detect that case and protect against it using disable-point-adjustment.

Cheers,
Clément.