Scripts/Edit

From J Wiki
Jump to navigation Jump to search

The following script simplifies the editing of definitions. By positioning the cursor in a name it will find the name in the current path containing the definition and open its script for editing. If it appears in multiple locales then it asks which you want to edit.

This verb can be put on an F-key as shown at the bottom comment.

This needs to be enhanced to work with Project Manager, but it should not be too difficult to have it search the Project Manager scripts if available.

Anybody who wants to add this, feel free.

Contributed by Don Guinn <<Date(2007-01-27T19:53:19Z)>>

NB. myedit - Edit a name under/after cursor in active session window.

cocurrent 'myedit'

NB. If the word is not a valid editable name in the current path
NB. or is a name for ctrl-F1 then the word is passed to help instead
NB. of edit.
NB.
NB. The text in the active window is captured along with the cursor
NB. position.  If multiple characters are selected then the selection
NB. is passed to edit.  If not then the word that follows the cursor
NB. or the word that the cursor is within is passed to edit.
NB.
NB. First, the line containing the cursor is found by counting line
NB. feeds.  Then am unenterable character is inserted into the line
NB. at the cursor.  The line is then broken into tokens and the
NB. unenterable character is located.  If the tokens before this
NB. character are equal then that means that the word was not split
NB. into two tokens.
NB.
NB. The name "word" is made a global to see in case this fails.

3 : 0 'system\extras\util\help.ijs'
if. 3=4!:0<'jpath'
  do.   require jpath   '~',y
  else. require jsystempath y
end.
)

myedit =: 3 : 0
'txt se'=.getsmdata''          NB. Get script in active window.
's e'=.se
if. l=.e-s                     NB. Check for characters selected.
  do.                          NB. Characters selected.
    word=:>{.;:l{.s}.txt
  else.                        NB. No characters selected.
    r=.s
    txt=.txt,LF
    NB. Get the line containing the cursor.
    's e'=.i{~_1 0++/r>i=.0,(LF=txt)#i.#txt
    t=.(e-s){.s}.txt
    t=.t#~0j1^(t='''')+._2}.+.//.3#,:'NB.'E.t NB. blank NB.s & quotes.
    NB. Insert unusable character at cursor position.
    u=.{:a.
    r=.r-s
    t2=.(r{.t),u,r}.t
    NB. Tokenize line with and without unusable character.
    t=.;:t
    t2=.;:t2
    NB. Find position of unusable character in tokenized line.
    word=:>t{~p=.1 i.~t~:t2{.~#t
end.
word=:word}.~LF={.word    NB. Will have LF if cursor at first of line.
if. DICT_jhelp_ e.~<,word NB. If the word is a J primitive invoke help.
  do.   help_j_ word
  else.
    NB. Find the appropriate locale in which to search for the word.
    if. '_'~:{:word NB. If locale already specified leave it alone.
      do. if. +./n=.'__' E. word
        do. NB. If locative convert it to a locale name.
          locative=.word}.~2+n=.n#i.#n
          word=:n{.word
          y=.(locative,'_','_',~>y)~
        else. NB. Just a name.  Find what locale it is in.
          if. -.(<word)e.('nl_','_',~>y)~'' do.
            if. _1-:y=.sel_locale word do. return. end.
          end.
      end.
      word=:word,'_','_',~>y NB. Build name including locale.
    end.
    try. n=.0>4!:4 <word catch. n=.1 end. NB. Verify name is in the path.
    if. -.n do.
      try. edit`help_j_@.n word
        catch. n=.1
      end.
    end.
    if. n
      do. if. _1<4!:0 <word
        do.   msg=.'was not defined in a script file."'
        else. msg=.'not found."'
      end.
      wd 'mb "Edit/Help Error" "''',(>word),''' ',msg
    end.
end.
)

NB. Select desired locale when name appears in several locales.
NB. Input:
NB.   y - name to be edited.
NB. Return:
NB.   numeric _1 - cancel button selected.
NB.   text string - name of locale selected.
sel_locale =: 3 : 0
l=.''
for_n. conl'' do.
  if. (<y)e.nl__n'' do.
    l=.l,n
  end.
end.
if. 2>#l do. ,>l return. end.
wd 'pc edsel;pn *Select Locale to Edit'
txt=.'The name ''',y,''' appears is several locales.  Select the desired locale.'
wd 'xywh 6 6 100 30;cc label static;cn *',txt
wd 'xywh 6 36 100 50;cc sel listbox ws_vscroll rightmove bottommove'
NB. wd 'setfont label ',f=.getconfig_j_ 'SMFONT'
NB. wd 'setfont sel ',f
wd 'xywh 120 10 34 12;cc select button bs_defpushbutton leftmove rightmove;cn "Select"'
wd 'xywh 120 26 34 12; cc cancel button leftmove rightmove;cn "Cancel"'
wd 'set sel ',;:^:_1 l
wd 'setselect sel 0'
wd 'pas 3 3;pcenter'
wd 'pshow'
whilst. 'edsel_sel_select'-:sysevent=.>(<1;1){wdq do.
  wdq=.wd 'wait;q'
end.
wd 'pclose'
if. sysevent-:'edsel_cancel_button' do. _1 return. end.
>(<(({."1 wdq)i.<'sel');1){wdq
)

NB.*getsmdata - Get the information on the current J window.
NB.
NB. Input: None
NB. Return:
NB.   A 2 element list
NB.     0 - The text of the selected (active) window (smread).
NB.     1 - Start and end caret selection information (smgetsel).
getsmdata=: 3 : 0
smselact_jijs_''
(smread_jijs_;smgetsel_jijs_)''
)

NB. Define F2 to invoke my editor.  Must pass the current locale to myedit.
NB. jijs_f2_fkey_jijs_ =: myedit_myedit_