Menu

select whole word from caret position

NppExec
Oren Farhi
2011-01-26
2013-05-21
  • Oren Farhi

    Oren Farhi - 2011-01-26

    hi.
    I want to use SCI command which selects a whole word from the current caret position - actually-  imitating double click behavior on a word.
    any ideas how do that with SCI?
    moreover, I want to be able to use the word for later use and add more character to it.
    Thanks.

     
  • DV

    DV - 2011-01-29

    I believe it must be something like this: 
     
    // position of caret (or start of selection) 
    SCI_SENDMSG SCI_GETSELECTIONSTART 
    set pos = $(MSG_RESULT) 
    // start position of nearest word 
    SCI_SENDMSG SCI_WORDSTARTPOSITION $(pos) 1  
    set word_start = $(MSG_RESULT) 
    // end position of nearest word 
    SCI_SENDMSG SCI_WORDENDPOSITION $(word_start) 1 
    set word_end = $(MSG_RESULT) 
    // set selection 
    SCI_SENDMSG SCI_SETSEL $(word_start) $(word_end) 
     

     
  • Oren Farhi

    Oren Farhi - 2011-02-03

    thanks DV!
    now i only need to get selected text and i can't seem to do that with: SCI_GETSELTEXT.
    I don't understand the explanation in scintillaDoc.
    Please assist.

    Thanks :-)

     
  • DV

    DV - 2011-02-04

    As "help sci_sendmsg" says, NppExec reserves a buffer of (4 MB)*sizeof(char) for @"<str>". So you don't need to care about the buffer allocation when requesting text from Scintilla. (Additionally, if the buffer of 4*1024*1024 symbols is not OK for you, you can specify the value of 'SendMsg_MaxBufLength' - see "NppExec_TechInfo.txt"). 
    So, to get the text from Scintilla, you just need to specify a "pointer to string" in terms of NppExec which has the following form:
     
    @"initial text of the string" 
     
    I.e. 
     
    // get selected text 
    SCI_SENDMSG SCI_GETSELTEXT 4000000 @"" 
    // do something with selected text stored in $(MSG_LPARAM) 
    echo $(MSG_LPARAM) 
     
    Explanation: 
    We have the following description of SCI_GETSELTEXT: 
    SCI_GETSELTEXT(<unused>, char *text) 
    It means the first parameter of SCI_SENDMSG should be SCI_GETSELTEXT, 
    the second parameter - WPARAM - is unused integer value, 
    and the third parameter - LPARAM - is a pointer to string buffer. 
    So we pass 4000000 as unused WPARAM (just in case, if Scintilla will require it in future as buffer size - you can specify up to 4*1024*1024-1) and @"" as LPARAM (it means NppExec creates a string buffer of 4*1024*1024 symbols and passes it to Scintilla). 
    As a result, $(MSG_LPARAM) will contain the selected text.

     
  • Oren Farhi

    Oren Farhi - 2011-02-06

    Hi dv.
    thanks again for helping me out.
    is there a page where there are examples of using SCI_SENDMSG commands?
    I'm lost with how to use other commands.

    I want to imitate this behavior:
    1. select a whole word in the cursor position - save it in a variable "SEARCH_CLASS"
    2. add the characters " =" to the "SEARCH_CLASS".
    3. search for the term in SEARCH_CLASS variable.

    I wanted to learn how to use SCI commands through this.

    Thanks.

     
  • DV

    DV - 2011-02-06

    In NppExec's Console, type: 
     
    help sci_sendmsg 
     
    and also: 
     
    help npp_sendmsg 
     
    and also: 
     
    help help 
     
    In NppExec's Manual: see "4.6.2. Calculating" (at the end) and "4.6.6. Grab Console output to new editor pane". 
    IMHO (in my horrific opinion) it's enough to understand the idea, especially together with the examples above in this topic.

     
  • Oren Farhi

    Oren Farhi - 2011-02-07

    hi dv.
    the manual is very details. However, I couldn't find any reference for performing a search or a reference for sepcial sci messages such as SCI_FINDTEXT - which suppose to get a second parameter *ttf (type of sci_texttofind).

     
  • DV

    DV - 2011-02-07

    Yes, SCI_FINDTEXT requires a structure as LPARAM, so this message can not be handled by NppExec. Instead, you can use pairs of SCI_SEARCHANCHOR and SCI_SEARCHNEXT - as described inside ScintillaDoc.html. As for WPARAM of SCI_SEARCHNEXT, you can use 0 or set the sum of the search-flags e.g.: 
    set searchFlags ~ SCFIND_WHOLEWORD + SCFIND_MATCHCASE

     
  • Oren Farhi

    Oren Farhi - 2011-02-07

    Hi dv.
    sorry for nudging - i can't seem to succeed in it:
    SCI_SENDMSG SCI_SEARCHANCHOR
    SCI_SENDMSG SCI_SEARCHNEXT "$(MSG_LPARAM)" 0

    I'm missing with these to complete my script:
    2. add the characters " =" to the "SEARCH_CLASS".
    3. search for the term in SEARCH_CLASS variable and go to the result line

     
  • DV

    DV - 2011-02-08

    As "help sci_sendmsg" says: 

    This command sets the following variables:
        $(MSG_RESULT) - LRESULT as int
        $(MSG_WPARAM) - int or string for @<int> or @"<str>", otherwise empty
        $(MSG_LPARAM) - int or string for @<int> or @"<str>", otherwise empty

    It means that each SCI_SENDMSG (or NPP_SENDMSG) either sets or empties these three variables. So, if you want to keep the value (in our case it is MSG_LPARAM), you should assign (copy) it to another user variable e.g. 
     
    SCI_SENDMSG SCI_GETSELTEXT 4000000 @"" 
    set SELECTED_TEXT = $(MSG_LPARAM) 
     
    Stupid? Yes, but if we would keep the values of MSG_LRESULT, WPARAM and LPARAM between different calls to SCI_SENDMSG, it would be incorrect with respect to SendMessage behaviour. (You actually send a new message, and I think it should affect the three return variables). 
    Now, to add " =", you just use 
     
    $(SELECTED_TEXT) " =" 
     
    to continue with searching.

     
  • Oren Farhi

    Oren Farhi - 2011-02-08

    hi dv.
    I managed to handle with adding the characters( my mistake in the reply when adding step 2).
    however, i cannot handle the SCI search operation as I tried with:
    SCI_SENDMSG SCI_SEARCHANCHOR
    SCI_SENDMSG SCI_SEARCHNEXT "$(MSG_LPARAM)" 0

     
  • DV

    DV - 2011-02-08

    Seems either my explanations are bad or you are completely frustrating by NppExec. 
    OK, here is the final part of the script which searches for a selected word plus tariling " =". 
     
    // get selected text 
    SCI_SENDMSG SCI_GETSELTEXT 4000000 @"" 
    set text2find = $(MSG_LPARAM) = 
    // set selection after current selected text 
    SCI_SENDMSG SCI_GETSELECTIONEND 
    SCI_SENDMSG SCI_SETSEL $(MSG_RESULT) $(MSG_RESULT) 
    // search for the text 
    SCI_SENDMSG SCI_SEARCHANCHOR  
    SCI_SENDMSG SCI_SEARCHNEXT 0 "$(text2find)" 
     

     
  • Oren Farhi

    Oren Farhi - 2011-02-08

    dv - thank you very much.
    you've been very helpful thus far.
    it's the integration of scintilla commands and NPPEXE that is hard (for me) to understand.

    This is the final script which finds a javascript class declaration in the current file:

    NPP_CONSOLE 0
    // position of caret (or start of selection)  
    set jsClassPreviousLine = $(CURRENT_LINE)
    SCI_SENDMSG SCI_GETSELECTIONSTART  
    set pos = $(MSG_RESULT)  
    // start position of nearest word  
    SCI_SENDMSG SCI_WORDSTARTPOSITION $(pos) 1   
    set word_start = $(MSG_RESULT)  
    // end position of nearest word  
    SCI_SENDMSG SCI_WORDENDPOSITION $(word_start) 1  
    set word_end = $(MSG_RESULT)  
    // set selection  
    SCI_SENDMSG SCI_SETSEL $(word_start) $(word_end)
    // get selected text
    SCI_SENDMSG SCI_GETSELTEXT 4000000 @""
    set text2find = $(MSG_LPARAM)
    // set selection after current selected text
    SCI_SENDMSG SCI_GETSELECTIONEND
    SCI_SENDMSG SCI_SETSEL $(MSG_RESULT) $(MSG_RESULT)
    // search for the text
    SCI_SENDMSG SCI_GOTOLINE 0
    SCI_SENDMSG SCI_SEARCHANCHOR
    SCI_SENDMSG SCI_SEARCHNEXT 0 "$(text2find) ="
    SCI_SENDMSG SCI_SCROLLCARET
    

    Thanks again.

     

Log in to post a comment.