all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: npostavs@users.sourceforge.net
Cc: 25777@debbugs.gnu.org
Subject: bug#25777: 25.1; [PATCH] `rectangle--pos-cols' should not move point
Date: Sun, 26 Feb 2017 22:24:59 -0800 (PST)	[thread overview]
Message-ID: <ed7e3c3f-a498-4e6d-b1a6-6e7165b23323@default> (raw)
In-Reply-To: <87poi4e7mv.fsf@users.sourceforge.net>

> > As I mentioned, this is a regression.  I think the bug and the patch
> > are pretty self-explanatory, but if you feel you need a recipe to
> > show that this causes cursor movement then let me know.
> 
> Yes, an example would be useful.  Perhaps it's the callers of
> rectangle--pos-cols that should use save-excursion.

I can certainly wrap all calls of `rectangle--pos-cols' with
`save-excursion'.  But that should not be necessary.  There
is no reason for that function to leave point moved.  It's
only aim in moving point is to do so temporarily.

Point is moved in Emacs 25, and it was not moved before.
It's a gratuitous regression.  `rectangle--pos-cols' does
not benefit in any way by leaving point moved.

Anyway, thanks for looking at this.  Here is a recipe.  You
can try to make it more minimal if you like, but it is pretty
simple and it shows the problem easily enough.

Download library `modeline-posn.el', from here:
https://www.emacswiki.org/emacs/download/modeline-posn.el

Evaluate these 3 variables: option `modelinepos-style' and
defvars `modelinepos-region-acting-on' and `modelinepos-rect-p'.

Evaluate this:

(defadvice rectangle-mark-mode
 (after bind-modelinepos-region-acting-on activate)
    (setq modelinepos-region-acting-on  rectangle-mark-mode
          modelinepos-rect-p            rectangle-mark-mode)  
    (redisplay 'force))

And evaluate the setq-default sexp for `mode-line-position'
(the one for Emacs > 22).

In buffer *scratch*:

M-x show-paren-mode RET
M-x set-variable RET show-paren-style RET mixed RET

Type this at the beginning of a line at the end of the buffer
(in *scratch*):

() RET () RET

So you see this, with the cursor (_) at eob, i.e., after the
final newline:

()
()
_

Then do `C-x SPC C-p'.  The cursor moves briefly up one line
and then immediately jumps back to where mark was, at the end
of the buffer.  It should stay where `C-p' put it, on the
second left paren.  The problem seems to come from an error
being raised in the context of the show-paren code.

An easier way to repro it is to just load file modeline-posn.el
and then do what I described starting with "In buffer *scratch:".
In that case you will see the intended effect of the library
when the region is active in `rectangle-mark-mode': The mode
line says how many rows & columns are in the rectangular region.
Of course, for the recipe the cursor will not move, but stays
stuck at eob, so the mode line just reads "0 rows, 0 cols".
Actually, you can see it briefly change to "1 rows, 0 cols"
and then change back to "0 rows, 0 cols".

To debug this, since I could not use the debugger (the code
is initiated while updating the mode-line during redisplay),
I had to just insert calls to `message' etc.  I commented
out parts of `modelinepos-style', to find the part that
matters.  Commenting out this, and replacing it by 999,
prevents the problem:

(let ((rpc (rectangle--pos-cols (region-beginning)
                                (region-end))))
  (abs (- (car rpc) (cdr rpc))))

I then replaced just the (abs...) with this:

(message "RPC: %S" rpc)
999

That printed (0 . 0) for RPC, but the problem was reproduced.
That told me that the problem came from just calling
`rectangle--pos-cols'.  Changing the above to just this
removed the problem:

(let ((rpc '(0 . 0))) (abs (- (car rpc) (cdr rpc))))

I checked the code of `rectangle--pos-cols' and saw that
it now does `goto-char' (and it did not do so before
Emacs 25.1).  Wrapping it in `save-excursion' fixed the
problem, without any negative effect on the function
(it does not need point to remain moved).

Such code should not gratuitously add cursor motion.
Other `goto-char' calls were also added, in other parts
of the same file, for Emacs 25.  Perhaps the person who
updated the code did not think about the consequence.

Point is not moved by the code in order to leave it in
a different place.  It is changed only for a temporary
effect.  That point-moving code should be wrapped in
`save-excursion'.  My advice is to check the other
such bare `goto-char' occurrences that were added in
Emacs 25.  I'm guessing that none of them need to leave
point moved.

Code should be able to call `rectangle--pos-cols' just
for its value, without getting side effects like cursor
movement.  If `rectangle--pos-cols' needed, or intended,
to leave point moved somewhere for some reason then yes,
of course, any calling code that doesn't want that should
be responsible for wrapping the calls with `save-excursion'.
But that's not the case: There is no reason for the function
to have the side effect of leaving point in a different position.





  reply	other threads:[~2017-02-27  6:24 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-17 17:51 bug#25777: 25.1; [PATCH] `rectangle--pos-cols' should not move point Drew Adams
2017-02-19 17:38 ` Drew Adams
2017-02-27  1:37   ` npostavs
2017-02-27  6:24     ` Drew Adams [this message]
2017-02-27 13:44       ` npostavs
2017-02-27 17:51         ` Drew Adams
2017-02-27 18:50           ` Noam Postavsky
2017-02-27 19:21             ` Drew Adams
2017-02-27 19:47               ` Noam Postavsky
2017-02-27 20:35                 ` Drew Adams
2017-02-28  4:57           ` npostavs
2017-02-28 15:11             ` Drew Adams
2017-03-02  1:21               ` npostavs
2017-03-02  2:32                 ` Drew Adams
2017-03-02 18:13                   ` Drew Adams
2017-03-03  2:09                     ` npostavs
2017-03-03  6:29                       ` Drew Adams
2017-03-03 13:28                         ` npostavs
2017-03-03 16:44                           ` Drew Adams
2017-03-03 18:16                             ` Noam Postavsky
2017-03-03 19:17                               ` Drew Adams
2019-06-24 17:10             ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ed7e3c3f-a498-4e6d-b1a6-6e7165b23323@default \
    --to=drew.adams@oracle.com \
    --cc=25777@debbugs.gnu.org \
    --cc=npostavs@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.