Author Topic: why do cua-select and select-word disagree on what word means ?  (Read 306 times)

jcelle

  • Community Member
  • Posts: 88
  • Hero Points: 4
why do cua-select and select-word disagree on what word means ?
« on: December 19, 2019, 09:41:17 pm »
Hello,
I have ctrl+shift+right bound to cua-select, hoping to select next word and extend word by word each time I press ctrl+shift+right again and again.
Problem is that the word selected is longer to what I expect.

If I bind ctrl+shift+right to select-word function instead of cua-select I get the exact word selected, but I can't extend my selection by pressing ctrl+shift+right again and again.

(eg. let's say cursor is a the beginning of this string: "foo</bold>"
- cua-select  selects 'foo</' (KO)
- select-word selects 'foo') (OK)

I was wondering if someone had some explanations on this lack of logic or could help me have the proper word selection of select-word while being able to extend the selection if I press the shortcut repeatedly.

Some other context info:
I have the 'next word style' set to 'Begin' ('End' is also available) because I expect ctrl+right/left to jump from beginning of word to beginning of word (no text selection here, just cursor movement). I suspect though this is affecting the cua-select computation of what the next word is.

My 'wordchars' is set to 'A-Za-z0-9'.

Thanks in advance and merry christmas all.
Jerome

Graeme

  • Senior Community Member
  • Posts: 2444
  • Hero Points: 322
Re: why do cua-select and select-word disagree on what word means ?
« Reply #1 on: December 20, 2019, 10:34:58 am »
I have my own macros for this.  You can probably play with the code below if it's not exactly what you want.

If you call the command select_char on the command line, then move the cursor with right-arrow or ctrl-right-arrow etc, you find that it creates a selection, then extends it as the cursor moves.

I have some cursor moving macros that are slightly different to next-word prev-word  - the "stop-on-all" macros step from token to token rather than word to word.
cursor-to-next-token
cursor-to-prev-token
cursor_to_next_token_stop_on_all
cursor_to_prev_token_stop_on_all

So then I have select_cursor_to_next_token() which starts a selection if there isn't one already and calls cursor_to_next_token().

Code: [Select]
_command void select_cursor_to_next_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   if ( !select_active() ) {
      select_char();
   }
   cursor_to_next_token();
}

If you want to try it, add the code below to something.e in your configuration folder and load using the load module command in the macro menu.  Unload with the unload module command.

BTW - I suspect that cua-select is consistent - default ctrl-right-arrow is the same as default ctrl-shift-right-arrow but the second one selects text.  cua-select is 492 lines of fairly complex looking code - would you believe.

Code: [Select]
#include "slick.sh"
#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)

//#import "window.e"
#include "se/ui/toolwindow.sh"


static boolean is_wordchar(_str s1)
{
   //return isalnum(s1) || (s1=='_');
   return pos('['p_word_chars']',s1,1,'R') > 0;
}


static boolean is_whitespace(_str s1)
{
   return (s1==' ') || (s1==\n) || (s1==\t) || (s1==\r) ;
}


/* cursor_to_next_token_stop_on_all
   - skips whitespace,
   - stops at start and end of a word,
   - stops on any other non whitespace char
*/
_command void cursor_to_next_token_stop_on_all() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   int lim = 0;
   if ( is_wordchar(get_text()) ) {
      while ( is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_right();
      }
   } else {
      cursor_right();
   }
   lim = 0;
   _str s1 = get_text();
   while ( is_whitespace(s1) ) {
      if (++lim > 2000)
         return;
      if ((s1==\n) || (s1==\r)) {
         begin_line();
         cursor_down();
      } else {
         cursor_right();
      }
      s1 = get_text();
   }
}


/* cursor_to_prev_token_stop_on_all
   - skips whitespace,
   - stops at start and end of a word,
   - stops on any other non whitespace char
*/
_command void cursor_to_prev_token_stop_on_all() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   int lim = 0;
   cursor_left();
   while ( is_whitespace(get_text()) ) {
      if (++lim > 2000)
         return;
      cursor_left();
      if (get_text()==\r) {
         return;
      }
   }
   lim = 0;
   if ( is_wordchar(get_text()) ) {
      while ( is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_left();
      }
      cursor_right();
   }
}


_command select_to_next_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   _select_char();
   cursor_to_next_token_stop_on_all();
   _select_char();
}


_command void select_to_prev_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   _select_char();
   cursor_to_prev_token_stop_on_all();
   _select_char();
}

/* cursor_to_next_token
   - stops at start and end of a word
*/
_command void cursor_to_next_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   int lim = 0;
   if ( is_wordchar(get_text()) ) {
      while ( is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_right();
      }
      return;
   } else {
      while ( !is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_right();
      }
      return;
   }
}


_command void select_cursor_to_next_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   if ( !select_active() ) {
      select_char();
   }
   cursor_to_next_token();
}



/* cursor_to_prev_token
   - stops at start and end of a word
*/
_command void cursor_to_prev_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   int lim = 0;
   cursor_left();
   if ( is_wordchar(get_text()) ) {
      while ( is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_left();
      }
      cursor_right();
      return;
   } else {
      while ( !is_wordchar(get_text()) ) {
         if (++lim > 2000)
            return;
         cursor_left();
      }
      cursor_right();
      return;
   }
}

_command void select_cursor_to_prev_token() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
{
   if ( !select_active() ) {
      select_char();
   }
   cursor_to_prev_token();
}


jcelle

  • Community Member
  • Posts: 88
  • Hero Points: 4
Re: why do cua-select and select-word disagree on what word means ?
« Reply #2 on: December 20, 2019, 03:27:48 pm »
Thanks Graeme, I am going to check this !

jcelle

  • Community Member
  • Posts: 88
  • Hero Points: 4
Re: why do cua-select and select-word disagree on what word means ?
« Reply #3 on: December 21, 2019, 10:40:42 am »
Unfortunately I am not able to use such macro in VSE standard edition.
I however found some workaround by switching to Vim emulation keyboard: here we seem to have the full set of word navigation commands, so exhaustive and useful.
Thanks again for answering me.