Recent

Author Topic: Reselect Last Selection  (Read 2563 times)

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Reselect Last Selection
« on: July 02, 2025, 02:03:05 am »
I tried looking thru shortcuts and I might be missing it - or it doesn't exist.  I'm looking for a way to reselect the last selection in the editor.  Basically, if you've used Vim, the equivalent to gv.

I find if I perform a replace in a selection, the replace completes and the text is no longer selected.  But there are many times I want to perform multiple replaces and I have to reselect the text again.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12537
  • Debugger - SynEdit - and more
    • wiki
Re: Reselect Last Selection
« Reply #1 on: July 02, 2025, 02:08:28 am »
There indeed isn't currently such an option. Interesting idea though.

In the meantime, depending on what you replace, have you tried syncro edit?
https://wiki.lazarus.freepascal.org/New_IDE_features_since#Syncron-Edit
« Last Edit: July 02, 2025, 02:09:59 am by Martin_fr »

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #2 on: July 02, 2025, 03:47:22 am »
I find if I perform a replace in a selection, the replace completes and the text is no longer selected.  But there are many times I want to perform multiple replaces and I have to reselect the text again.
Maybe setting "Selection" to "Persistent block" in the Editor options might be a solution for you. 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19431
  • Glad to be alive.
Re: Reselect Last Selection
« Reply #3 on: July 02, 2025, 12:49:03 pm »
A undo buffer for ctrl-shift-e?
Any "programmer" that knows only one programming language is not a programmer

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12537
  • Debugger - SynEdit - and more
    • wiki
Re: Reselect Last Selection
« Reply #4 on: July 02, 2025, 12:58:30 pm »
Ideally "replace" or at least "replace in selection" should not remove the selection.

But, as it selects the first found instance when asking if it should replace, that does not work. For that to work a 2ndary selection would be needed.

Undo (normal undo) wont work either, even if it stored changes to selection => because the undo would first undo the replace.
When "replace" finds something, it FIRST removes/changes the selection, THEN changes the text. And undo goes in reverse order.

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Re: Reselect Last Selection
« Reply #5 on: July 02, 2025, 02:34:30 pm »
Maybe setting "Selection" to "Persistent block" in the Editor options might be a solution for you.

After doing the Replace All, selection still lost, but thanks for the suggestion.

BTW, I didn't state it in the original post, but I usually use Replace All in Selection.  I don't think that changes any answers, just mentioning it.

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #6 on: July 02, 2025, 03:07:46 pm »
After doing the Replace All, selection still lost, but thanks for the suggestion.
You're welcome.  I thought it was worth a shot.  Unfortunately it didn't work but, it didn't take much time to try it either.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12537
  • Debugger - SynEdit - and more
    • wiki
Re: Reselect Last Selection
« Reply #7 on: July 02, 2025, 05:04:21 pm »
There isn't a solution at the moment.

Well, you can probably write Pascal-script macros that set a bookmark at the start and end of the selection. And then another that sets the selection from those bookmarks. You need to spare 2 bookmarks though. And get into the Pascal script macros.

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #8 on: July 02, 2025, 07:06:04 pm »
Well, you can probably write Pascal-script macros that set a bookmark at the start and end of the selection. And then another that sets the selection from those bookmarks. You need to spare 2 bookmarks though. And get into the Pascal script macros.
That's an interesting idea.

Do you think that instead of using bookmarks, it might be sufficient to record the line numbers of the start and end of the selection ?

The only concern that comes to mind is if the text-replace operation modifies the original selection's number of lines but, I think that's a bit of a rare case ... is this a correct impression ?
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19431
  • Glad to be alive.
Re: Reselect Last Selection
« Reply #9 on: July 02, 2025, 07:09:23 pm »
Well, yes, correct. Store the diffs.
Any "programmer" that knows only one programming language is not a programmer

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #10 on: July 02, 2025, 07:21:23 pm »
Never mind.

I just remembered there is no way to pass data between macros, because of that the use of bookmarks is pretty much forced. 

ETA:

Actually, just thought of a hack that would allow sharing data between macros.  That would remove the need to use bookmarks.  I haven't tried it yet, so don't really know if it will work.  Something to do, hopefully sometime soon.

« Last Edit: July 02, 2025, 07:34:16 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #11 on: July 09, 2025, 11:59:04 pm »
I thought it would be fun to solve this problem using a macro... here is the macro:
Code: Pascal  [Select][+][-]
  1. var
  2.   BeginPt    : TPOINT;
  3.   EndPt      : TPOINT;
  4.  
  5.   BeginCaret : TPOINT;
  6.  
  7. begin
  8.   with BeginPt do
  9.   begin
  10.     x := 0;
  11.     y := 0;
  12.   end;
  13.  
  14.   with EndPt do
  15.   begin
  16.     x := 0;
  17.     y := 0;
  18.   end;
  19.  
  20.   with BeginCaret do
  21.   begin
  22.     x := BeginPt.x;
  23.     y := BeginPt.y;
  24.   end;
  25.  
  26.  
  27.   if Caller.SelAvail then
  28.   begin
  29.     BeginPt := Caller.BlockBegin;
  30.     EndPt   := Caller.BlockEnd;
  31.  
  32.     with BeginCaret do
  33.     begin
  34.       x := BeginPt.x;
  35.       y := BeginPt.y;
  36.     end;
  37.  
  38.     //ShowMessage('BeginPt.x: ' + IntToStr(BeginPt.x) + '  ' +
  39.     //            'BeginPt.y: ' + IntToStr(BeginPt.y) + '  ' +
  40.     //            'EndPt.x: '   + IntToStr(EndPt.x)   + '  ' +
  41.     //            'EndPt.y: '   + IntToStr(EndPt.y));
  42.   end;
  43.  
  44.   ecReplace;     // do the search and replace
  45.  
  46.   if BeginPt.x <> 0 then
  47.   begin
  48.     Caller.BlockBegin := BeginPt;
  49.     Caller.BlockEnd   := EndPt;
  50.  
  51.     Caller.CaretXY    := BeginCaret;
  52.   end;
  53. end.                              
  54.  
It's written in such a way that if nothing is selected the macro's action are the same as the actions done by the original replace command.

if there is text selected then, it does the replacement with the addition that the text _stays_ selected ready for a next replace operation to be done on it.  That way, it is not even necessary to re-select the text since it stays selected.

Two possible ways of using the macro are:

1. re-assign the current key combination assigned to the "replace" command to this macro (default is ctrl-r) if going that route, do it in two steps, first delete the key assigned to "replace" then assign it to the macro.  This because, if it's not deleted first, the key re-assignment eventually gets lost.  This way effectively replaces the current action with this macro's action.

2. assign any unused key combination to this macro.  This way, there are two ways of performing a replace operation, the original one and this new one that keeps the text selected ready for another replace operation.

HTH.

ETA:

Just for the record, the variable BeginCaret isn't really needed.  BeginPt has the same information.
« Last Edit: July 10, 2025, 12:19:38 am by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Re: Reselect Last Selection
« Reply #12 on: July 10, 2025, 03:26:22 am »
I tried it out - after researching how to support macros  :D

It would perfectly - minimal testing but nice.

Took me longer to find a good unused key combination than it did to actually save the macro

Thanks

440bx

  • Hero Member
  • *****
  • Posts: 6556
Re: Reselect Last Selection
« Reply #13 on: July 10, 2025, 04:09:47 am »
You're welcome.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018