NB. jedit.ijs -- assigns some editing utilities to F-keys
NB.  F2 - show list of definitions
NB.  F4 - comment/decomment block
NB.  F5 - indent block
NB.  F6 - unindent block
NB.  F9 - select entire definition
NB.
NB. 17-Jun-1999  uses wd 'replace' and 'getscroll' features
NB.
cocurrent 'jedit'

NB.* Selection a replaces current selection with the result of u.
NB. The actual code for this adverb is selected according to
NB. the current version of J.
Selection=: 1 : (>(4.03>:".>{.<;._2 wd'qabout'){(0 :0);(0 :0))
txt=.wd 'smselact;smread'
oldscroll=.wd 'smgetscroll'
sel=:". wd 'smgetsel'
y.=.y. NB. very funny
in=. {.@] }. ({.~ }.)
wd 'smsetselect ',": ({.sel)+0, (# [ wd@('smreplace *'&,)) u. txt in sel
)
txt=.wd 'smselact;smread'
sel=.". wd 'smgetsel'
y.=.y. NB. very funny
in=. {.@] }. ({.~ }.)
'nt lot'=.(u.;#) txt in sel
b=.txt before sel
wd 'smwrite *', b, nt, txt after sel
wd 'smscroll ',":_2++/LF=b
wd 'smsetselect ',(":sel+0,lot-~#nt),' 0'
)

rts=:#~ +./\.@:(' '&~:)
NB.* EachLine a apply u. to each string (delimited by fret)
EachLine=: 1 : 0
last=.LF -. {:y.
(LF-.last),~ ; ([,(<LF)"_,])/ [`(rts@}: , {:)@.({: = CR"_)@u. each <;._2 y.,last
)


indent=: ' '&,
unindent=: (}.~ ' '"_ = {.)
comment=: ('NB.'&,@indent)`(unindent@(3&}.))@.([: {. 'NB.'&E.)


NB.* blockup v moves selection one line up
blockup=: 3 : 0
txt=.wd 'smselact;smread'
sel=.". wd 'smgetsel'
b=.txt before sel
n=.2+CR i:~ 2}.b
wd 'smwrite *', (n{.b), (txt in sel), (n}.b), txt after sel
wd 'smscroll ',":_3++/LF=b
wd 'smsetselect ',(":sel-2#n-~#b),' 0'
i. 0 0
)



NB.* blockdown v moves selection one line down
blockdown=: 3 : 0
txt=.wd 'smselact;smread'
sel=.". wd 'smgetsel'
n=.LF i.~ a=.txt after sel
wd 'smwrite *', (b=.txt before sel), (n{.a), (txt in sel), (n}.a)
NB. wd 'smscroll ',":_3++/LF=#b
wd 'smsetselect ',":sel+2#n
i. 0 0
)


NB.* selex v select explicitly defined entity
NB. by NSG, 1999
NB. usage:
NB.  a) tie this verb to function key (e.g. F9);
NB.  b) move your caret into explicitly defined entity;
NB.  c) press key (F9).
NB. Entire entity will be selected. Now it is ready for
NB. redefinition (Ctrl+E) or copying/erasing/moving.
NB. No warranty, do not eat.
selex=: 3 : 0
txt=.wd 'smselact;smread'
ln=.+/(lfs=.LF=txt){.~{.". wd 'smgetsel'        NB. current line
re=.17!:2 '^[[:alnum:]_]+[[:space:]]*=:.*(0|1|2|3|4|noun|verb|monad|dyad|adverb|conjunction)[[:space:]]+(define|:[[:space:]]*0)'
        NB. def begins, ends
fns=.(-:&(')',CR) - _1: ~: {.@{.@(re&(17!:0))) ;._1 LF, txt
17!:3 re
L1=._1 i:~ (>:ln){.fns          NB. first line of definition
L2=.L1+(L1}.fns) i. 1           NB. last line of definition
        NB. select lines
wd 'smsetselect ',":>:(i:&1 , i.&1, _1:)   (>:&L1 *. <:&L2)   +/\lfs
i. 0 0
)

NB.* deflist v shows all names defined globally in the current window
deflist=: 3 : 0
txt=.wd 'smselact;smread'
re=.17!:2 '^([[:alnum:]_]+)[[:space:]]*=:'
fns=.<@(({:@] {. {.@] }. [)`(''"_)@.(_1:={.@])  (1&{)@(re&(17!:0))) ;._1 LF, txt
lns=.(a:&~: # i.@#)fns
17!:3 re
'ok item'=.wdselect 'Definitions';<fns-.a:
if. ok do.
  wd 'smsetselect ', ":|.0,,~(>:item{lns)i.~ +/\LF=txt
end.
i. 0 0
)

wd 'smsetcmd 2 3 "All defs',TAB,'F2" "do_jedit_ ''deflist 0''" "Show all global definitions in script" ;'
wd 'smsetcmd 4 3 "NB.',TAB,'F4"      "do_jedit_ ''comment EachLine Selection 0''" "Comment/decomment selected block" ;'
wd 'smsetcmd 5 3 "Indent',TAB,'F5"   "do_jedit_ ''indent EachLine Selection 0''" "Indent selected block" ;'
wd 'smsetcmd 6 3 "Unindent',TAB,'F6" "do_jedit_ ''unindent EachLine Selection 0''" "Unindent block" ;'
NB. wd 'smsetcmd 7 3 "Block up',TAB,'F7" "do_jedit_ ''blockup 0''" help ;'
NB. wd 'smsetcmd 8 3 "Block dn',TAB,'F8" "do_jedit_ ''blockdown 0''" help ;'
wd 'smsetcmd 9 3 "Select def',TAB,'F9" "do_jedit_ ''selex 0''" "Select current explicit entity" ;'

NB. perl -ne "print if/^[a-zA-Z_0-9]+\s*=:\s*(1|2|3|4|verb|adverb|conjunction)\s+(def|:\s*0)\s*$/" *.*
NB. '^([[:alnum:]_]+)[[:space:]]*=:[[:space:]]*(1|2|3|4|verb|adverb|conjunction)[[:space:]]+(def|:[[:space:]]*0)[[:space:]]*$'