NB. Standard jade library (defined in j locale)
NB. 
NB. Should not require modification.
NB.
NB. requires file: scripts.js  (which may be modified)
NB.
NB. ALPH           valid alphabetic characters in names
NB. JDIR           J directory
NB. PATHSEP        path separator \ or /
NB. PROJECT        project script files
NB. PUBLIC         public script files
NB. SOURCE         source script files (typically set in project files)
NB. buildproject   build project names
NB. buildpublic    build public names
NB. buildsource    build source names
NB. edit           edit name
NB. load           load script file
NB. loadactive     load active script window
NB. open           open script file
NB. require        load script file if not already loaded
NB. save           save current definitions to a script file
NB. scriptmake     make single script file from several files
NB. scripts        access script definitions
NB. selactive      selections for active script window
NB.
NB. subroutines defined here:
NB. j_build3       called by j_builder
NB. j_builder      build names utility
NB. j_getnames     get names utility
NB. j_getscripts   get script names
NB. j_globaldefs   global definitions in text
NB. j_info         display info message
NB. j_js           default extension to .js
NB. j_load         load scripts
NB. j_openfiles    open files utility
NB. j_origin       find script of origin
NB. j_pathsep      convert path separator 
NB. j_tolower      convert to lowercase

ALPH=: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
PROJECT=:  i.0 2
PUBLIC=:   i.0 3
SOURCE=:   i.0 3

NB. platform dependent definitions:
3 : 0''
j=. 9!:12''
PATHSEP=: ':'
j_pathsep=: ]

NB. if. j e. 4 5 do.
NB.   PATHSEP=: '/'
NB.   j_pathsep=: '/'&((# i.@#)@(=&'\')@]})
NB. else.
NB.   PATHSEP=: '\'
NB.   j_pathsep=: ]
NB. end.

if. j e. 2 6 do.
  j_info=: wdinfo
else.
  j_info=: ] NB. 1!:2&2
end.
''
)

j=. 1!:41''
JDIR=:  j,(0=#j)#1!:40''
JDIR=: JDIR,PATHSEP #~ PATHSEP ~: {:JDIR

buildpublic=:  3 : 'PUBLIC=: j_builder y.'
buildproject=: 3 : 'PROJECT=: ,&(<''j'')@}:"1 j_builder y.'
buildsource=: 3 : 0
'' buildsource y.
:
if. #p=. x. do.
  p=. (-. (2#PATHSEP) E. p) # p
  p=. p,PATHSEP #~ PATHSEP ~: {:p
end.
empty SOURCE=: (#~ [: ~: {."1) SOURCE,~p j_builder y.
)

NB. edit   
edit=: 3 : 0
'ns'=. y.
file=. {.,j_getscripts_j_ s
try. dat=. 1!:1 file
catch. 'file read error: ',,>file return.
end.
dat=. (2&({.!.(<'''')))@;:;._2 dat,LF
ndx=. dat i. n;'=:'
if. ndx=#dat do. 
  ndx=. dat i. (}:(2<:+/\.n='_')#n);'=:' 
end.
if. ndx=#dat do. 
  j_info 'not found: ',":n return.
end.
empty wd 'smsel ',EAV,(>file),EAV,';smopen;smscroll ',(":ndx),';smfocus;'
)

NB. load    - load scripts
NB. respects local definitions
load=: 3 : 0
NB. load scripts
NB. e.g      load 'graph'   - load graph script
NB.    'loc' load 'graph'   - load with default locale: loc
NB. if script names are found in SOURCE, PUBLIC, or PROJECT, 
NB. the locale given there is used. If not found, the locale 
NB. used is the left argument, or base locale if not given.
0 load y.
:
x. j_load j_getscripts y.
)

loadactive=: 3 : 0
NB. load active script window   (does nothing if jx window)
0 loadactive y.
:
a=. wd 'qsmact'
if. '.jx' -: _3{.a do. empty'' return. end.
wd 'smselact;smsave;'
x. load a
)

NB. open   - open scripts
NB. e.g.  open 'graph'     - open graph script
open=: 3 : 0 
f=. ,}:"1 j_getscripts y.
b=. j_exist"0 f
if. 0 e. b do.
  j_info 'not found: ',;LF&,&.>(b=0)#f
end.
if. #f=. b#f do.j_openfiles f end.
)

NB. require    - load scripts if not already loaded
require=: 3 : 0
0 require y.
:
s=. j_getscripts y.
b=. -. ({."1 s) e. 4!:3''
x. j_load b#s
)

NB. save
NB. 
NB. saves current definitions in a script file
NB. 
NB. form: to save loc
NB.
NB.   to = destination file (any previous contents are overwritten)
NB.   loc = locale names
NB.      if empty, save all locales
NB.      otherwise it is a boxed list of one or more locale names
NB.    
NB. e.g. 
NB.   'my.js' save '';'z'    NB. save base and z locales in my.js
NB.   'my.js' save ''        NB. save all locales in my.js

save=: 4 : 0
x.=. boxopen x.
'' 1!:2 x.

if. 0=#y. do. y.=. 4!:1[6 end.

y.=. ,&.>cutopen y.
y.=. (y.e.4!:1[6)#y.

if. 0=#y. do. empty'' return. end.

n99=. -. & ('x.';'y.';'n99';'s99')
s99=. [: ; (, ([: '=:'&, [: ,&(10{a.) 5!:5@<)) &.>

while. #y. do.
  (toHOST s99 (n99 ".'namelist_',(>{.y.),'_ 0 1 2 3'),&.><'_',(>{.y.),'_') 1!:3 x.
  y.=. }.y.
end.

empty ''
)

NB. scriptmake
NB. newfile scriptmake sourcefiles
NB. returns number of characters written, or error message
scriptmake=: 4 : 0
y=. j_getscripts y.
txt=. ''
while. #y do.
  f=. {.{.y
  try. dat=. 1!:1 f
  catch. 'file read error: ',>f return. end.
  txt=. txt,LF,dat
  y=. }.y
end.
txt=. toJ txt,LF
txt=. <;._2 txt
NB. remove comments:
f=. #~ -.@(('NB.'&-:)@(3&{.)&>)
txt=. f txt
f=. 'NB.'&E. <: ~:/\@(''''&=)
g=. (,&CRLF)@(#~ *./\@f)
txt=. ;g each txt
txt 1!:2 }.0;x.
#txt
)

NB. scripts
scripts=: 3 : 0
NB.   scripts ''       short directory
NB.   scripts 'v'      verbose directory
NB.   scripts 'e'      edit directory
NB.   scripts 'reset'  reset directory
if. 0=#y. do.
  a=. list 0{"1 SOURCE
  b=. list 0{"1 PUBLIC
  c=. list 0{"1 PROJECT
  ((*#a)#a,' '),b,(*#c)#' ',c
elseif. 'reset'-:y. do.
  0!:0 <'main\scripts.js'
  SOURCE=: 0#SOURCE
elseif. 'v'e.y. do.
  dir=. SOURCE,PUBLIC,PROJECT
  a=. >0{"1 dir
  b=. >1{"1 dir
  c=. >2{"1 dir
  a /:~ a,.' ',.b,.' ',.c
elseif. 'e'e.y. do.
  open 'main\scripts.js'
elseif. 1 do.
  'invalid argument to scripts: ',,":y.
end.
)

NB. selactive
NB. selection box for active script window
selactive=: 3 : 0
fl=. wd 'qsmact'
if. '.js' -: _3{.fl do.
  'nx'=. j_globaldefs wd 'smselact;smread'
  'st'=. wdselect 'select';n
  if. s do. wd 'smscroll ',(":t{x),';' end.
end.
empty''
)

NB. j_ utilities:

NB. j_build3 - used by j_builder:
dnb=. {.~ i.&1@('NB.'&E.)
cut=. [: ((= ' '"_) <;._2 ]) ,&' '
dle=. -.&(<'')
d01=. ((1 }~) ({~ *@#@>@(1&{)))  NB. use 0 if 1 is empty
chop=. 0 1 2&(<@:({"1)"0 2)
j_build3=: chop@:(d01@(3&{.)@dle@cut@dnb &>) f.

NB. j_builder
NB. utility used to build names globals
NB. x. = optional default path
NB. y. = file names
j_builder=: 3 : 0
'' j_builder y.
:
if. (0 e. $y.) +. 16 < 3!:0 y. do. y. return. end.
y=. (<'') -.~ <;._2 y.
'sdl'=. j_build3 y
path=.  ,~ #&x.@(-.@(PATHSEP&e.))
drive=. ,~ #&JDIR@(-.@(':'&e.))
name=.  , #&'.js'@(-.@('.'&e.))@(-.&'''')
w=. j_pathsep@j_tolower@drive@path@name &.> d
s,.w,.l
)

NB. j_exist
j_exist=:  3 : 'try. 1 [ 1!:11 y.,<0 0 catch. 0 end.'

NB. j_getnames        - j_getnames namelist
NB. converts any short names to full names
NB. converts path separator 
NB. defaults file extension to .js
NB. returns 2 column matrix: names;locales
NB. defined in j locale
j_getnames=: 4 : 0
if. 0=#y=. y. do. i.0 2 return. end.
if. 0=#y=. cutopen y do. i.0 2 return. end.
y=. j_pathsep@j_tolower@(-.&' ')&.>y
ndx=. <.0.5*(,}:"1 x.) i. y
hit=. ndx<#x.
ind=. hit#i.#hit
ndx=. hit#ndx
x=. ndx{x.
y=. (1{"1 x) ind } y
y=. y,.(2{"1 x) ind } (#y)#<''
)

j_getscripts=: 3 : 0
(SOURCE,PUBLIC,PROJECT) j_getnames y.
)

NB. j_globaldefs 
NB. find global definitions in text
NB. i.e. lines starting:  name=: ...
NB. returns (boxed names);line numbers
j_globaldefs=: 3 : 0
dat=. <;._2 y.,LF
f=. [: (#~ (<'=:')&-:@{:) [: 2&{. ;:
def=. f &.> dat
b=. *#&>def
nms=. {.&>b#def
ndx=. b#i.#b
ind=. /:>nms
(ind{nms);<ind{ndx
)

NB. j_js     - default extension to .js
j_js=: , [: #&'.js' [: -. '.'&e.

NB. j_load   - load scripts
j_load=: 4 : 0
while. #y. do.
  'fl'=. {.y.
  try. 1!:11 f;0 0
  catch. j_info 'file read error: ',f return. end.
  l=. >(0-:x.){x.;l
  n=. 'l99_',l,'_'
  (3 : (n,'=:0!:0',LF,n,'<y.',LF,'4!:55<''',n,'''')) f
  y.=. }.y.
end.
empty''
)

NB. j_openfiles  - open files utility
j_openfiles=: 3 : 0
empty wd ; ('smsel ',EAV)&,@(,&(EAV,';smopen;smfocus;')) &.> y.
)

NB. j_origin     - find script of origin
j_origin=: 3 : 0
((<''),4!:3 '') {~ >: 4!:4 cutopen y.
)

NB. j_tolower
j=. '(y.i.~''ABCDEFGHIJKLMNOPQRSTUVWXYZ'',a.)'
j=.    j,'{''abcdefghijklmnopqrstuvwxyz'',a.'
j_tolower=: 13 : j 

NB. define PUBLIC and PROJECT:
0!:0 <JDIR,'main',PATHSEP,'scripts.js'
