Subject: Re: bug#21710: 25.0.50; self-insert-command before a folded outline inserts after the outline

From: Eli Zaretskii <[email-masked]>
Date: Wed, 21 Oct 2015 20:02:33 +0300

> From: Oleh Krehel <[email-masked]>
> Cc: [email-masked]
> Date: Wed, 21 Oct 2015 15:05:16 +0200
> 
> Eli Zaretskii <[email-masked]> writes:
> 
> > The reason for this is that on all header lines but the last inserting
> > the first 't' makes it invisible.
> 
> This isn't a good behavior. `self-insert-command' should not put text
> into an invisible region.

It doesn't.  It inserts the character at point.  The problem is that
the inserted character becomes invisible because the overlay becomes
applicable to it.

> "C-e" should complement this behavior, starting from:
> 
> * |foo...
> * bar...
> 
> pressing "C-e" should result in:
> 
> * foo|...
> * bar...

If that's what you want, you need to program that into the mode.  The
default effect of C-e is to put point one character before the newline
on the same visible line, which is _after_ the invisible text.  IOW,
this is not a bug, but perhaps a missing feature.

> And from the state above, pressing "C-d" should result in:
> (user-error "Overlay read-only"), instead of deleting hidden characters.

Once again, you need to program that.  By default, C-d just deletes
the character at point.

> > Then point adjustment kicks in, and moves point out of the
> > invisible region, so the second 't' is inserted at the end of the
> > sub-level text.
> 
> Can you point me to a place where I can read up on this point
> adjustment?

Some minimal Lisp-level documentation is in "(elisp) Adjusting Point.
For the gory details, see the function adjust_point_for_property in
keyboard.c.

> I'd like to help in fixing this bug, if it's not too complicated.

It's not a bug in point adjustment, strictly speaking: that part does
its job as intended.  The problem is with the fact that the inserted
character becomes invisible.

> So far, I'm looking at this code:
> 
> (defun outline-flag-region (from to flag)
>   "Hide or show lines from FROM to TO, according to FLAG.
> If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
>   (remove-overlays from to 'invisible 'outline)
>   (when flag
>     ;; We use `front-advance' here because the invisible text begins at the
>     ;; very end of the heading, before the newline, so text inserted at FROM
>     ;; belongs to the heading rather than to the entry.
>     (let ((o (make-overlay from to nil 'front-advance)))
>       (overlay-put o 'evaporate t)
>       (overlay-put o 'invisible 'outline)
>       (overlay-put o 'isearch-open-invisible
> 		   (or outline-isearch-open-invisible-function
> 		       'outline-isearch-open-invisible))))
>   ;; Seems only used by lazy-lock.  I.e. obsolete.
>   (run-hooks 'outline-view-change-hook))
> 
> The comment about `front-advance' implies that what I proposed above was
> the intended behavior, which is broken currently.

Then why does it work correctly on the last heading?