Author Topic: How to get selection in a _str variable?  (Read 8119 times)

slickMS

  • Community Member
  • Posts: 9
  • Hero Points: 0
How to get selection in a _str variable?
« on: January 31, 2011, 05:09:02 PM »
Hi,

Can anyone show me how I can get the current section into a _str variable?
If possible this would work regardless of the type of selection, single-line, multiline
etc.

I use the following; code but this does NOT work when the selection spans more
than line?

Code: [Select]
static _str GetSelectionOrWordAtCursor()
{
  int first_col, last_col, buf_id;
  _str word, line;

  if (!_begin_select_compare() && !_end_select_compare())
  {  /* get text out of selection */
    _get_selinfo(first_col, last_col, buf_id);
    get_line(line);

    if (_select_type('', 'I'))
      ++last_col;

    if (_select_type() == 'LINE')
      word = line;
    else
      word = expand_tabs(line, first_col, last_col - first_col);

    _deselect();
  }
  else
  {
    deselect();
    word = cur_word(first_col, '', 1);
  }

  return word;
}

Thanks,
Mark
« Last Edit: January 31, 2011, 07:29:00 PM by slickMS »

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: How to get selection in a _str variable?
« Reply #1 on: January 31, 2011, 09:07:12 PM »
@slickMS: The original function is not intended to handle normal selections spanning mult. lines.
But you might use the example below to add this feature to your version.

Good luck, HS2

Code: [Select]
/**
 * retrieves selected text, first and last line number of an existing normal or line selection
 *
 * @param seltext    [out] selected text if any
 * @param bline      [out] line number begin of selection
 * @param eline      [out] line number end of selection
 * @param doDeselect [in]  clear current selection [default: true]
 *
 * @return  0  -> OK
 *          TEXT_NOT_SELECTED_RC
 */
static int hs2_get_sel_text( _str &seltext, int &bline, int &eline, boolean doDeselect = true )
{
   int rc = TEXT_NOT_SELECTED_RC;
   seltext = '';
   if ( select_active() )
   {
      typeless p; _save_pos2( p );

      long bpos, epos;
      _begin_select( '', false, true);
      bline = p_RLine; bpos = _QROffset();

      _end_select( '', false, true);
      eline = p_RLine; epos = _QROffset();

      _restore_pos2( p );
      if ( doDeselect ) deselect();   // optionally quit selection for convenience
      rc = 0;

      // say("bpos: " bpos " epos: " epos);
      seltext = get_text( (int)(epos - bpos), (int)bpos );
   }

   return rc;
}

slickMS

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: How to get selection in a _str variable?
« Reply #2 on: January 31, 2011, 09:21:19 PM »
Great!

Thanks; hs2. That seems to work fine!


MS

AGodfrey

  • Junior Community Member
  • Posts: 8
  • Hero Points: 0
Re: How to get selection in a _str variable?
« Reply #3 on: April 27, 2017, 09:03:30 PM »
Just a note for others who hit the problem I did: This technique doesn't work in a command that specifies VSARG2_REQUIRES_EDITORCTL.
'bline' ends up wrong, and is the same value as 'eline'. I don't know why this is; for me, VSARG2_REQUIRES_EDITORCTL was just a nice-to-have that I could do without.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: How to get selection in a _str variable?
« Reply #4 on: April 28, 2017, 12:12:45 AM »
Use both: VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL
I'm not sure why.
See https://community.slickedit.com/index.php/topic,13314.msg52764.html#msg52764