Sublime Forum

[SOLUTION] Undo and Redo to not move the cursor or the viewport

#1

It is well known, and not loved, that Undo and Redo returns to the current cursor position. While this “feature” may be working as designed, some users–arguably many–consider it a bug, a bug to which there is apparently still no workaround, as Soft Undo and Soft Redo also zoom to the cursor.

So that one may see undone and redone changes independent of the current cursor position, how about a setting or settings to not zoom to the cursor on undo and redo?

0 Likes

Undo and redo
#2

Can you describe the problem, because a workaround may be there

0 Likes

#3

Too see the full range of behavior:

  1. Have over 3 times as many lines as are visible in the viewport.
  2. Make an edit in around the middle line.
  3. Move the cursor so the edited line is not in the viewport.
  4. Undo. The cursor and viewport move to the positions they were in at step 2.
  5. Move the cursor so the edited line is not in the viewport.
  6. Redo. The cursor and viewport move to the positions they were in at step 2.

When undoing and redoing, the cursor and the viewport should not move.

0 Likes

#4

This should workaround the situation, you save it as (main menu bar - preferences - browse pacakges - user folder) something like Packages/User/UndoWithoutMovingCursorListener.py

Basically, get_regions has a bug that you cannot get the regions after doing undo, so what I did was to fold the thing, and unfold it after undo. That way I can restore the cursors in the correct position. But because of this bug, you lose any fold you have.

https://dl.dropboxusercontent.com/u/9303546/SublimeText/commands/undo_or_redo_without_moving_cursor_listener.py

0 Likes

#5

This seems to partially work. It also 1. shouldn’t recenter the view on the previous cursor position and 2. redo isn’t incorporated.

Bug: Undoing when the cursor is on the last line makes the cursor disappear such that nothing can be typed unless you click somewhere.

0 Likes

#6

I forgot about redo, there I updated with redo support and out of bounds ugly hack check https://dl.dropboxusercontent.com/u/9303546/SublimeText/commands/undo_or_redo_without_moving_cursor_listener.py

About

shouldn’t recenter the view on the previous cursor position and

idk what you mean, here screen does not move.

0 Likes

#7

The update fixes the previous bounds bug but it still doesn’t work quite right.

Bugs:

  • Redo moves the cursor and it may scroll the view.
  • Undo doesn’t move the cursor, unless you Undo and Redo and then Undo again on the same line, and it may scroll the view.

I’ve even disabled many packages just in case of any conflicts. Windows 3103.

0 Likes

#8

There I fixed the viewport moving when the cursor is in another page than the position you are undoing/redoing, about other issues, please give a step by step way to repro.

0 Likes

#9

Undo doesn’t move the display but it disappears the cursor; when you then arrow the cursor, it appears where it should be.

Redo moves the cursor and, if applicable, the viewport.

0 Likes

#10

i dont understand

0 Likes

#11

What do you not understand?

The objectives are 1. to make Undo and Redo never move the cursor to a different line (because, out of the box, they do), and 2. to make Undo and Redo never change which lines are visible, or in view (the viewport).

The current state of your fix–which I’m grateful you are creating–only partially accomplishes the first objective.

0 Likes

#12

Yes, we agree, It works for me. cursor does not move and screen stay in the same position even if undoing something in a non visible area of the screen. So, if something does not work, please check with a portable version of ST, and be sure you have the latest ST and “plugin” installed. After that, if you have any problem. Describe it completely, please. Seomthing like, Have text “aaa” move cursor to line 4 place cursor in column 9 then … etc, etc Because it seems to me we are not seeing the same thing. So maybe describing a very specific case can help debug the issue if any.

To clarify your test case in item2 works ok to me. It does what you desire in this thread.

0 Likes

#13

ignore. sdf sd fdsf

0 Likes

#14

OK for some reason, in my setup works as you expect. But on a vanilla ST it does not work as we expect. I found another bug in the selection ST API, the ST API has too many bugs. I found like three bugs in a row just now.

0 Likes

#15

ok, there I updated with more workarounds and hopefully will work as we expect. Please let me know.

0 Likes

#16

Bugs in the ST API, 'eh? That ought to get the attention of JPS. :smiley:

The latest update doesn’t seem to change the behavior at all from the default, on Windows 3103 x64 and on vanilla x64 portable.

If it still may be of use, here is my test case:

  1. Vertically size ST to 30 lines
  2. Have 90 populated lines (I used a 5-unique-line repeated pattern with line 5 blank)
  3. Center the viewport and place the cursor on line 44
  4. Do a find and replace of the unique part of line 44 (regex or not)
  5. Scroll until line 1 is visible
  6. Place the cursor on one of the visible lines
  7. Undo
  8. Scroll until line 90 is visible
  9. Place the cursor on one of the visible lines
  10. Redo

Currently, steps 7 and 9 recenter the viewport on and move the cursor back to line 44, which the objectives are to stop.

And how did you spice up your ST? :slightly_smiling:

0 Likes

#17

Quite challenging,

  1. Opened portable vanilla ST
  2. I did make sure I have latest the plugin and nothing more (no other packages, no preferences changed)
  3. Restarted
  4. Did you test case
  5. It works, no recenter of viewport and no move of the cursor.

Here have screen recorder with what I did
https://dl.dropboxusercontent.com/u/9303546/SublimeText/commands/open_file_with_custom_find_command.webm

I think is safe to assume ST is not mature enough.

  1. get_regions does not work after undo/redo
  2. sel() does not update the cursor position on the screen
  3. sel() lies with the cursor positions, as is reporting other position than the actually position
  4. set_viewport_position, does not set the viewport.
0 Likes

#18

Unfortunate conclusion about the ST API but excellent workarounds! Thank you! However, Undo and Redo on the last line moves the cursor up one line.

Although one difference in our test case executions is that I use Replace All, this doesn’t seem to matter.

Now, I finally noticed that my Redo wasn’t working because I had remapped it from the default redo_or_repeat to redo, and the plugin wasn’t listening for redo. So, I added all redo and undo variants and it seems to work in every case in my installed instance but not at all on vanilla:

if command_name == ‘undo’ or command_name == ‘soft_undo’ or command_name == ‘redo’ or command_name == ‘soft_redo’ or command_name == ‘redo_or_repeat’:

0 Likes

#19

Did you try to put your code in a TextCommand ?
If I remember well there’s some issue with refreshing the view when you do this kind of thinks out of a begin_edit/end_edit block.

0 Likes

#20

idk… maybe works… yes I trust you, but this is supposed to work out of the box…why is there if does not works? Also I have kinda similar problems in other packages that does not use the on_after_something commands… bufferscroll is for example a package that discovered many bugs, and the bugs are there in same shape after many years… whats the point of creating an API if remains broken… something that is supposed to be done with a few lines, needs from many silly and ugly(I like this word, because I’m ugly too) [have you read my code?] workarounds, is very frustrating. …

Thanks also @ParticleMan Im gonna add these similar commands too, glad you got it working. :confused:

0 Likes