Author Topic: _save_pos2()/_restore_pos2() seems to not be working  (Read 2394 times)

rowbearto

  • Senior Community Member
  • Posts: 2336
  • Hero Points: 132
_save_pos2()/_restore_pos2() seems to not be working
« on: November 05, 2017, 02:20:08 PM »
I wrote the following macro:

Code: [Select]
_command void python_paste()name_info(',' VSARG2_REQUIRES_EDITORCTL)
{
    _save_pos2(auto p2);
    _insert_text("#\n");
    paste();
    _restore_pos2(p2);
    delete_line();
}

This is to be used when editing python scripts for pasting when the cursor is at a particular position and getting the indentation as desired.

It types a comment ("#") + newline at the cursor, then pastes in a buffer, then it should go back and delete the "#\n".

It doesn't work (the "#\n" is not deleted) when using _save_pos2() and _restore_pos2(). I thought I should use _save_pos2() and _restore_pos2() because the documentation for save_pos says:

Quote
DO NOT use this function if you plan to modify the buffer before calling the restore_pos function.  Use the _save_pos2 function instead.

As I am modifying the buffer with the paste() call, that is why I first tried using _save_pos2()/_restore_pos2().

So then I tried using save_pos()/restore_pos(), and this works properly, it is deleting the python comment:

Code: [Select]
_command void python_paste()name_info(',' VSARG2_REQUIRES_EDITORCTL)
{
    save_pos(auto p2);
    _insert_text("#\n");
    paste();
    restore_pos(p2);
    delete_line();
}

So why is save_pos()/restore_pos() working here when a buffer is modified, but _save_pos2()/_restore_pos2() is not working? Is there a bug with _save_pos2()/_restore_pos2()? Or does documentation need an update?

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6937
  • Hero Points: 531
Re: _save_pos2()/_restore_pos2() seems to not be working
« Reply #1 on: November 07, 2017, 04:05:52 PM »
save_pos and restore_pos work fine because you are modifying data AFTER the position that was saved. This situation will always work.

_save_pos2() isn't working for you because the marker is getting moved due to the fact that you inserted some data at the beginning of the saved position. I suspect some older versions of SlickEdit didn't do this but were supposed to.

Code: [Select]
_command void python_paste()name_info(',' VSARG2_REQUIRES_EDITORCTL)
{
    _insert_text("#\n");
    up();_end_line();left();
    // Mark at the begining where we inserted.
    _save_pos2(auto p2);
    down();_begin_line();
    paste();
    _restore_pos2(p2);
    delete_line();
}

rowbearto

  • Senior Community Member
  • Posts: 2336
  • Hero Points: 132
Re: _save_pos2()/_restore_pos2() seems to not be working
« Reply #2 on: November 08, 2017, 01:54:48 PM »
OK, this explains it.

I think that you may want to add into the documentation for _save_pos2() that the saved position moves when doing _insert_text() but does not move when doing things like up(), end_line(), left(), down(), begin_line(), etc. This was not obvious. May also want to reference this post.

May also want to add into the documentation for save_pos() that it can still be used when modifying data AFTER the position was saved and that it is not impacted by any of: _insert_text, up(), end_line(), left(), down(), begin_line(), etc.

Rob
« Last Edit: November 08, 2017, 02:44:24 PM by rowbearto »