NB. built from project: ~Source\main\dirmatch\dirmatch
NB.

script_z_ '~system\packages\color\color16.ijs'
script_z_ '~system\main\compare.ijs'
script_z_ '~system\packages\winapi\dirbrowse.ijs'
script_z_ '~system\main\dll.ijs'
script_z_ '~system\main\files.ijs'
script_z_ '~system\main\gl2.ijs'
script_z_ '~system\classes\input\jfiletype.ijs'
script_z_ '~system\classes\grid\jsgrid.ijs'
script_z_ '~system\classes\view\jview.ijs'
script_z_ '~system\classes\grid\jzgrid.ijs'
script_z_ '~system\main\pack.ijs'
script_z_ '~system\packages\print\print.ijs'
script_z_ '~system\main\strings.ijs'

coclass 'jdirmatch'


coinsert 'j'
EMPTY=: i.0 0
LF2=: LF,LF
SEP=: PATHSEP

deb=: #~ (+. 1: |. (> </\))@(' '&~:)
dlb=: }.~ =&' ' i. 0:

isempty=: 0: e. $
intersect=: e. # [
last=: 1 : 'x. " 1'
pathname=: 3 : '(b#y.);(-.b=.+./\.y.=PATHSEP)#y.'
quote=: ''''&,@(,&'''')@(#~ >:@(=&''''))
subs=. 2 : 'x."_ I. @(e.&y.)@]} ]'
toblank=: ' ' subs '_'
tohyphen=: '_' subs ' '

todelim=: ; @: ((EAV&,) @ (,&EAV) @ , @ ": &.>)
tolist=: }.@;@:((10{a.)&,@,@":&.>)

termBS=: 3 : 'y., PATHSEP #~ (*#y.) *. PATHSEP~:_1{.y.'
termLF=: , (0: < #) # LF"_ -. _1&{.

fmtdir=: termBS @ dmlower
dminfo=: 3 : 0
jdm_show''
wdinfo 'Directory Match';y.
)
dmjoin=: 4 : 0
if. 0 = L. y. do.
  dat=. y.
else.
  dat=. }. , LF ,. (>tohyphen each {."1 y.) ,. ' ',. > {:"1 y.
end.
dat=. x.,'=: 0 : 0',LF,(termLF dat),')',LF
)
dmquery=: 4 : 0
jdm_show''
x. wdquery 'Directory Match';y.
)
dmsplit=: 3 : 0
if. 0 < L. y. do. y. return. end.
dat=. <;._2 y.
g=. i.&' '
h=. g (toblank@{. ; deb@}.) ]
res=. h &> dat
({."1 res) ; <{:"1 res
)
dmrep=: 4 : 0
dat=. y.
if. (0=#dat) +. 2=3!:0 dat do.
  if. LF e. dat do.
    dat=. dat, LF -. {:dat
    x.,'=: 0 : 0', LF, ; <;.2 dat,')',LF
  else.
    x.,'=: ',(quote dat),LF
  end.
else.
  x.,'=: ',(":dat),LF
end.
)
expandn=: 4 : 0
y. #~ (#y.)$>:(-x.){.0j1
)
filecopy=: 3 : 0
'source dest'=. y.
if. 0 = pathcreate fpath dest do. 0 return.
end.
if. IFWIN32 do.
  0 pick 'kernel32 CopyFileA i *c *c i' cd source;dest;0
else.
  datold=. fread :: 0: dest
  datnew=. fread :: 0: source
  if. -. datold -: datnew do.
    datnew fwrite dest
  end.
end.
1
)
fileread=: 1!:1
filecmp=: 4 : 0
src=. (< @: (1!:1))"0 x.
tar=. (< @: (1!:1))"0 y.
src = tar
)
fpath=: 3 : 'y.#~+./\.y.=PATHSEP'
makecompare=: 3 : 0
if. IGNORESEP do.
  compare=: compare_z_ & toJ
else.
  compare=: compare_z_
end.
i.0 0
)
pathcreate=: 3 : 0
p=. jhostpath y.
p=. p, PATHSEP_j_ -. {: p
if. # 1!:0 }: p do. 1 return. end.
p=. (p e. '/\') <;.2 p
t=. ''
f=. 1!:5 :: 0:
while. #p do.
  t=. (_1 { p), t
  p=. }: p
  if. # 1!:0 }: ; p do.
    p=. ; p
    while. #t do.
      p=. p, > 0 { t
      t=. }. t
      if. 0 = f < p do. 0 return. end.
    end.
    1 return.
  end.
end.
0
)
tolist3=: 3 : 0
}. , LF ,. listfmt y.
)
tolist6=: 3 : 0
dat=. ((2 * #y.),3) $ ,y.
}: tolist 2 expandn <"1 listfmt dat
)
listfmt=: 3 : 0
nm=. > 0 {"1 y.
ts=. tsfmt > 1 {"1 y.
ts=. ' ' ,. ts ,. ' '
sz=. ": ,. > 2 {"1 y.
nm ,. ts ,. sz
)
tsfmt=: 3 : 0
r=. }: $ y.
t=. 2 1 0 3 4 5 {"1 [ _6 [\ , 6 {."1 <. y.
d=. '--#::' 2 6 11 14 17 }"1 [ 2 4 5 3 3 3 ": t
mth=. _3[\'   JanFebMarAprMayJunJulAugSepOctNovDec'
d=. ,((1 {"1 t) { mth) 3 4 5 }"1 d
d=. '0' (I. d=' ') } d
d=. ' ' (I. d='#') } d
(r,20) $ d
)
wdshiver=: 3 : 0
a=. wd 'qformx'
wd 'pmovex ', ": 0 0 1 0 + 0 ". a
wd 'pmovex ', a
)
DMCFGFILE=: jpath '~config\dmconfig.ijs'

DMDIRS=: ''
DMSOURCEFM=: '' 
DMSOURCE=: ''
DMTARGET=: ''
DMFAVORITES=: i.0 2

MAXDMDIRS=: 12  

DMFOUNDALL=: DMFOUNDCONTENTS=: ''
NOTINSOURCE=: NOTINTARGET=: ''
DIFFTIME=: DIFFCONT=: i.0 6
DIFFTIMEST=: DIFFCONTST=: ''   

DMMSK=: ''
DMTREEX=: ''
DMTYPE=: ''          
DMTYPES=: 0 2$''
DMMAXSIZE=: 30000
DMTAB=: 'std'
DMSUBDIR=: 1
DMCONTENTS=: 0
DMTIMESTAMP=: 0

PJPROJ=: '' 

IGNORESEP=: 0
DMPATHSEP=: PATHSEP
termSEP=: , (0: < #) # DMPATHSEP"_ -. _1&{.
termdelSEP=: }.~ [: - 0: i.~ DMPATHSEP&= @ |.

HWNDP=: ''
PICON=: ''
contents=: '0'
dmputconfig=: 3 : 0
DMDIRS=: (MAXDMDIRS <. #DMDIRS) {. DMDIRS
dat=. 'NB. Directory Match configuration',LF
dat=. dat,LF,'DMDIRS' dmrep tolist DMDIRS
dat=. dat,LF,'DMFAVORITES' dmrep tolist ,DMFAVORITES
dat=. dat,LF,'DMLASTDIRS' dmrep tolist DMSOURCEFM;DMTARGET
dat=. dat,LF,'DMTYPE' dmrep DMTYPE
dat=. dat,LF,'DMTYPEXTS' dmjoin DMTYPES,.DMEXTS
dat=. toHOST dat
dat 1!:2 <DMCFGFILE
)
dmgetglobals=: 3 : 0
'DMTYPES DMEXTS'=: dmsplit DMTYPEXTS
DMDIRS=: <;._2 dmlower DMDIRS
if. # DMLASTDIRS do.
  'DMSOURCE DMTARGET'=: 2 {. LF cutopen DMLASTDIRS
else.
  DMSOURCE=: DMTARGET=: ''
end.
DMSOURCEFM=: DMSOURCE
DMFAVORITES=: _2 ,\ a: -.~ deb each <;._2 DMFAVORITES
)

getdir=: 3 : 0

r=. i.0 3
path=. y.
ifsvn=. '.svn' -: _4 {. path
if. ifsvn do.
  fl=. 1!:0 path,'\text-base\*'
  fl=. fl #~ (<'.svn-base') = _9 {. each {."1 fl
  path=. _4 }. path
  r=. r, (path&, each _9 }. each {."1 fl),.1 2{"1 fl
else.
  fl=. 1!:0 path,'*'
  if. #fl do.
    att=. > 4{"1 fl
    fl=. fl #~ ('h' ~: 1{"1 att) *. 'd' ~: 4{"1 att
  end.
  r=. r, (path&, each {."1 fl),.1 2{"1 fl
end.
if. DMSUBDIR do.
  dl=. 1!:0 path,'*'
  if. #dl do.
    att=. > 4{"1 dl
    dl=. dl #~ ('h' ~: 1{"1 att) *. 'd' = 4{"1 att
    dr=. {."1 dl
    dr=. dr -. DMTREEX	
    if. #dr do.
      dr=. (path&, @ ,&(SEP,ifsvn#'.svn')) each dr
      r=. r, ; getdir each dr
    end.
  end.
end.
if. #r do.
  r=. (dmlower L:0 {."1 r) 0 }"0 1 r
end.
if. -. (<,'*') e. DMX do.
  fls=. {."1 r
  ndx=. 1 + fls i: &> '.'
  msk=. (ndx }.each fls) e. DMX
  r=. msk # r
end.

sort r
)


DMDIRS=: 0 : 0
)

DMFAVORITES=: 0 : 0
)

DMLASTDIRS=: 0 : 0
)

DMTYPE=: 'SourceText'

DMTYPEXTS=: 0 : 0
Scripts    ijs
Labs       ijt
Text       txt
SourceText ijs, ijt, txt
HTML       htm, html
Plaintext  htm, html, ijs, ijt, txt
All        *
)

FED=: 0 : 0
pc fed owner;
xywh 2 3 55 9;cc z0 static;cn "Directory Pairs:";
xywh 0 12 141 127;cc favedit editm ws_border ws_hscroll ws_vscroll rightmove bottommove;
xywh 105 1 35 11;cc cancel button leftmove rightmove;cn "Close";
pas 0 0;
rem form end;
)
fed_run=: 3 : 0
pos=. wd 'qformx'
wd FED
wd 'set favedit *',tolist }: , DMFAVORITES ,. a:
wd 'setfocus favedit'
wdcenter pos
wd 'pshow'
)
fedclose=: 3 : 0
dat=. a: -.~ <;._2 favedit, LF
if. 1 = 2 | #dat do.
  dminfo 'Favorites not in pairs. There are ',(":#dat),' directories'
  return.
end.
DMFAVORITES=: _2 ,\ dat
wd 'pclose;psel ',HWNDP
wd 'pactive'
)
fed_cancel_button=: fed_cancel=: fed_close=: fedclose

coltypes=: 15 15
coltitle=: 'Source';'Target'
colwidth=: 100 100
colstretch=: 1 1
filltype=: 4
sortenable=: 1
FAV=: 0 : 0
pc fav closeok owner;pn "dirs";
xywh 2 5 41 9;cc s0 static;cn "Directories:";
xywh 0 17 190 80;cc grid isigraph ws_border ws_clipchildren rightmove bottommove;
xywh 129 4 34 11;cc cancel button leftmove rightmove;cn "Cancel";
xywh 165 4 34 11;cc ok button bs_defpushbutton leftmove rightmove;cn "&OK";
xywh 190 17 9 80;cc scrollbarv scrollbarv leftmove rightmove bottommove;
pas 0 0;pcenter;
rem form end;
)
fav_run=: 3 : 0
data=: dataraw=: DMFAVORITES
font1=: font0=: PROFONT
pos=. wd 'qformx'
wd FAV
wdfit ''
wdcenter pos
grid=: '' conew 'jsgrid'
init__grid''
wd 'setfocus grid'
wd 'pshow;'
)
fav_gethilite=: 3 : 0
0 pick hilite__grid { dataraw__grid
)
favclose=: 3 : 0
destroy__grid''
wd 'psel fav;pclose'
)
fav_cancel_button=: fav_cancel=: fav_close=: favclose
formselect=: 3 : 'wd''psel '',formhwnd'
fav_grid_size=: 3 : 'size__grid 0'
fav_grid_mbldown=: 3 : 'mbldown__grid sysdata'
fav_grid_mblup=: 3 : 'mblup__grid sysdata'
fav_grid_mbldbl=: 3 : 'mbldbl__grid sysdata'
fav_grid_mmove=: 3 : 'mmove__grid sysdata'
fav_grid_char=: 3 : 'char__grid sysdata;sysmodifiers'
fav_scrollbarv_button=: 3 : 0
scrollbarv__grid scrollbarv
wd'setfocus grid'
)
favorites=: 3 : 0
if. #DMFAVORITES do. fav_run'' end.
)
select_result=: 3 : 0
'src tar'=. deb each hilite__grid { dataraw__grid
if. -. (src;tar) -: source,target do.
  DMSOURCEFM=: src
  DMTARGET=: tar
  DMDIRS=: ~. dmlower each (src;tar), DMDIRS
  wd 'psel ',HWNDP
  dmshow''
end.
favclose''
)
fav_ok_button=: fav_enter=: select_result

getfoundname1=: 3 : 0
if. isempty dat=. getnames '' do. '' return. end.
'prev name next'=. dat
if. isempty name do. '' return. end.
if. prev -: name do.
  DMTARGET joindirname name
elseif. name -: next do.
  getsourcename1 name
elseif. (<name) e. {."1 NOTINSOURCE do.
  DMTARGET joindirname name
elseif. (<name) e. {."1 NOTINTARGET do.
  getsourcename1 name
elseif. do.
  ''
end.
)
getfoundname2=: 3 : 0
if. isempty dat=. getnames '' do. '' return. end.
'prev name next'=. dat
if. prev -: name do.
  'first second'=. prev;name
elseif. name -: next do.
  'first second'=. name;next
elseif. do.
  '' return.
end.
(getsourcename1 first) ; DMTARGET joindirname second
)
getignorenames=: 3 : 0
dat=. getnames ''
if. isempty 1 pick dat do. '' return. end.
dat -. a:
)
getnames=: 3 : 0
if. 0=#found do. '' return. end.
pos=. {.".found_select
ind=. 0, I. found=LF
ndx=. (ind >: pos) i: 0
ndx=. ndx + 0 1 2
dat=. <;._2 found,LF
dat=. a: , dat , a:
if. 0 e. ndx e. i. # dat do. '' return. end.
res=. getname1 each ndx { dat
if. 0=#;res do. '' end.
)
joindirname=: 4 : 0
(fmtdir x.), y.
)
getname1=: 3 : 0
dat=. deb |. y.
ndx=. I. dat = ' '
if. 2 < # ndx do.
  |. (1 + 2 { ndx) }. dat
else.
  ''
end.
)
remfound=: 3 : 0
fm=. <y.
dms=. jhostpath DMSOURCE, SEP

msk=. fm ~: dms&, each {."1 DIFFCONT
if. 0 e. msk do.
  DIFFCONT=: msk # DIFFCONT
  DIFFCONTST=: msk # DIFFCONTST
end.

msk=. fm ~: dms&, each {."1 DIFFTIME
if. 0 e. msk do.
  DIFFTIME=: msk # DIFFTIME
  DIFFTIMEST=: msk # DIFFTIMEST
end.

msk=. fm ~: dms&, each {."1 NOTINTARGET
if. 0 e. msk do.
  NOTINTARGET=: msk # NOTINTARGET
end.

match_fmt''
dmshowfind''
)

match=: 3 : 0
dmread''
if. isempty DMSOURCE do.
  dminfo 'Enter the source directory' return.
end.
if. isempty DMTARGET do.
  dminfo 'Enter the target directory' return.
end.
DMDIRS=. ~. dmlower each (DMSOURCE;DMTARGET), DMDIRS
matches 0
DMMAXMSG=: 0
dmshowfind''
)
matches=: 3 : 0
DMFOUNDCONTENTS=: DMFOUNDALL=: ''
match_do ''
match_fmt y.
)
matchclear=: 3 : 0
DMFOUNDCONTENTS=: DMFOUNDALL=: ''
DMMAXMSG=: 0
dmshowfind''
)
match_fmt=: 3 : 0
count=. {. y.,1  
DMFOUNDCONTENTS=: DMFOUNDALL=: ''

hit=. +/ (#DIFFCONT),(#DIFFTIME),(#NOTINSOURCE),#NOTINTARGET

if. 0 = hit do.
  dminfo count pick 'Contents match';'Contents now match'
  return.
end.

if. #DIFFCONT do.
  DMFOUNDCONTENTS=: 'different contents - source,target: ',LF,(tolist6 DIFFCONT),LF2
end.

txt=. ''
if. #NOTINSOURCE do.
  txt=. txt, 'not in source: ',LF,(tolist3 NOTINSOURCE),LF2
end.

if. #NOTINTARGET do.
  txt=. txt, 'not in target: ',LF,(tolist3 NOTINTARGET),LF2
end.

if. #DIFFTIME do.
  txt=. txt, 'different timestamp - source,target: ',LF,(tolist6 DIFFTIME),LF2
end.

DMFOUNDALL=: txt, DMFOUNDCONTENTS
)
match_do=: 3 : 0

NOTINSOURCE=: NOTINTARGET=: ''
DIFFTIME=: DIFFCONT=: i.0 6
DIFFTIMEST=: DIFFCONTST=: '' 
DMX=: ' ,' cutopen (DMTYPES i. boxxopen DMTYPE) pick DMEXTS

src=. fmtdir DMSOURCE
tar=. fmtdir DMTARGET

dx=. getdir src, (DMTAB-:'svn') # '.svn'
dy=. getdir tar

fx=. {."1 dx
fy=. {."1 dy

fx=. (#src) }. each fx
fy=. (#tar) }. each fy
dx=. fx ,. }."1 dx
dy=. fy ,. }."1 dy
NOTINTARGET=: (-. fx e. fy) # dx
NOTINSOURCE=: (-. fy e. fx) # dy

dx=. (fx e. fy) # dx
dy=. (fy e. fx) # dy
dxy=. dx ,. dy

if. 0 e. #dx do. return. end.
msk=. (2 {"1 dx) ~: 2 {"1 dy
if. 1 e. msk do.
  DIFFCONT=: msk # dxy
  msk=. -. msk
  dx=. msk # dx
  dy=. msk # dy
  dxy=. msk # dxy
end.
nms=. {."1 dx
cmp=. (src&, each nms) filecmp tar&, each nms 
DIFFCONT=: sort DIFFCONT, (cmp = 0) # dxy
DIFFCONTST=: (1 {"1 DIFFCONT) earlier 4 {"1 DIFFCONT
if. DMTIMESTAMP do.
  dx=. cmp # dx
  dy=. cmp # dy
  dxy=. cmp # dxy
  msk=. -. dx e. dy
  if. 1 e. msk do.
    DIFFTIME=: msk # dxy
    DIFFTIMEST=: (1 {"1 DIFFTIME) earlier 4 {"1 DIFFTIME
  end.
end.
)
earlier=: 4 : 0
_2 </ \ /: /: , x. ,. y.
)

getsourcename=: 3 : 0
if. (DMTAB-:'svn') do.
  y=. pathname &> y.
  px=. < '.svn',DMPATHSEP,'text-base',DMPATHSEP
  sx=. <'.svn-base'
  y=. ({."1 y) ,each px ,each ({:"1 y) ,each sx
else.
  y=. y.
end.
(<fmtdir DMSOURCE) ,each y
)
getsourcename1=: getsourcename&.<


PTOP=: 0
PSHOW=: 0

JDM=: 0 : 0
pc jdm;pn "Directory Match";
menupop "&File";
menu std "Source from Directory" "" "" "";
menu snp "Source from Snapshot" "" "" "";
menu svn "Source from Subversion" "" "" "";
menusep ;
menu exit "E&xit" "" "" "";
menupopz;
menupop "&Favorites";
menu select "&Select from favorites..." "" "" "";
menusep ;
menu edit "&Edit favorites..." "" "" "";
menusep ;
menu save "S&ave current files as favorites" "" "" "";
menupopz;
menupop "&View";
menu contents "&Contents only" "" "" "";
menusep ;
menu mignorest "&Ignore source files earlier than target" "" "" "";
menusep ;
menu mignorent "&Ignore target files not in source" "" "" "";
menusep ;
menu mignoresep "&Compare ignores line separators" "" "" "";
menupopz;
menupop "&Tools";
menu swap "&Swap source and target directories" "" "" "";
menusep ;
menu copyst "&Copy source files not in target" "" "" "";
menusep ;
menu copylt "&Copy source files &later than target" "" "" "";
menusep ;
menu copyat "&Copy &all source files" "" "" "";
menusep ;
menu delts "&Delete target files not in source" "" "" "";
menusep ;
menu print "&Print" "" "" "";
menupopz;
menupop "&Help";
menu help "&Help" "" "" "";
menupopz;
xywh 0 0 332 52;cc tabs tab rightmove;
xywh 0 57 332 1;cc s1 staticbox ss_etchedhorz rightmove;
xywh 0 60 275 150;cc found editm ws_hscroll ws_vscroll rightmove bottommove;
xywh 280 60 50 12;cc compare button leftmove rightmove;cn "Co&mpare";
xywh 280 72 50 12;cc compareall button leftmove rightmove;cn "Compare &All";
xywh 280 84 50 12;cc xdiff button leftmove rightmove;cn "E&xternal Diff";
xywh 280 96 50 12;cc open button leftmove rightmove;cn "&Open";
xywh 280 108 50 12;cc view button leftmove rightmove;cn "&View";
xywh 280 120 50 12;cc copy button leftmove rightmove;cn "&Copy";
xywh 280 132 50 12;cc ignore button leftmove rightmove;cn "&Ignore";
xywh 280 194 50 12;cc cancel button leftmove topmove rightmove bottommove;cn "Close";
xywh 292 196 40 12;cc hideme button leftmove topmove rightmove bottommove;
pas 0 0;pcenter;
rem form end;
)
dmrun=: 3 : 0

jdm_init''

if. wdishandle HWNDP do.
  wd 'psel ',HWNDP
  jdm_show''
  return.
end.

PSHOW=: 0
0!:0 :: ] <DMCFGFILE
dmgetglobals''
wd JDM
HWNDP=: wd 'qhwndp'
if. L. y. do.
  'src tar'=. ": each 2 $ y.
  DMSOURCEFM=: src
  DMTARGET=: tar
  DMDIRS=: ~. dmlower each (src;tar), DMDIRS
  index=. match=. 0
else.
  'index match'=. 2 {. y.,0
end.
DMTABGROUPS=: ;: 'std snp svn'
DMTAB=: index pick DMTABGROUPS
DMTABID=: 'jdm', DMTAB
DMLOADED=: <DMTABID

wd 'setshow tabs 0'
wd 'creategroup tabs'
(DMTABID,'_run')~''
wd 'creategroup'
wd 'setshow ',DMTABID,' 1'

wd 'set mignoresep ',":IGNORESEP
wd 'setshow hideme 0'  
wd 'setfont found ',FIXFONT
wd :: ] 'picon ',PICON
dmshow''
enablefound 0
wpset 'jdm'
if. match -: 1 do.
  (DMTABID,'_match')~''
end.
jdm_show''
)
jdm_init=: 3 : 0
if. IFWIN32 do.
  dmlower=: tolower
else.
  dmlower=: ]
end.

if. 0=4!:0 <'DIRTREEX' do.
  DMTREEX=: boxxopen DIRTREEX
end.
EMPTY
)
jdm_show=: 3 : 0
if. -. PSHOW do.
  wd 'pshow;pactive;ptop ',":PTOP
  PSHOW=: 1
end.
)
jdm_btypes_button=: 3 : 0
(type;<DMTYPES,.DMEXTS) conew 'jfiletype'
)
jdm_contents_button=: 3 : 0
DMCONTENTS=: ". contents
dmshowfind''
)
jdm_close=: 3 : 0
dmread''
dmputconfig''
wpsave 'jdm'
wd 'psel ',HWNDP
wd 'pclose'
)
jdm_cancel=: jdm_cancel_button=: jdm_close
jdm_exit_button=: jdm_close
jdm_enter=: jdm_edir_button=: jdm_etype_button=: jdm_find_button
jdm_help_button=: 3 : 0
htmlhelp 'user\directory_match.htm'
)
jdm_print_button=: 3 : 0
if. 0=#found do. dminfo 'Nothing to print'
else. prints found end.
)
dmread=: 3 : 0
DMCONTENTS=: 0 ". contents
)
jdm_swap=: 3 : 0
if. y. -: DMTAB do. return. end.
dmlast=. DMTABID
DMTABID=: 'jdm', DMTAB=: y.
if. -. DMTABID e. DMLOADED do.
  wd 'creategroup tabs'
  (DMTABID,'_run')~''
  wd 'creategroup'
end.
(DMTABID,'_show')~''
wdshiver''
wd 'setshow ',dmlast,' 0'
wd 'setshow ',DMTABID,' 1'
)

jdm_std_button=: jdm_swap bind 'std'
jdm_snp_button=: jdm_swap bind 'snp'
jdm_svn_button=: jdm_swap bind 'svn'
jdm_tctrl_fkey=: 3 : 0
PTOP=: ".ptop
wd 'ptop ',ptop
)
jdm_save_button=: 3 : 0
DMFAVORITES=: ~. DMFAVORITES, source ; target
dminfo 'Done.'
)
dmshow=: 3 : 0
DMALL=: a: -.~ ~. DMSOURCEFM;DMTARGET;DMDIRS
dup=. DMSOURCEFM -: DMTARGET
wd 'setenable swap ',": DMTAB -: 'std'
wd 'set contents ', ":DMCONTENTS
(DMTABID,'_show')~''
qd=. wd 'qd'
({."1 qd)=: {:"1 qd
)
dmshowfind=: 3 : 0
if. 0=#DMFOUNDALL do.
  wd 'set found'
  enablefound 0
  return.
end.
txt=. DMCONTENTS pick DMFOUNDALL;DMFOUNDCONTENTS
if. DMMAXMSG < DMMAXSIZE < #txt do.
  msg=. 'Found text is too large to display in full.'
  msg=. msg,LF,LF,'Save full text to file temp',SEP,'dm.txt?'
  if. 0=2 dmquery msg do.
    txt 1!:2 <jpath '~temp\dm.txt'
  end.
  txt=. (DMMAXSIZE {. txt),'...'
end.
DMMAXMSG=: 1
wd 'set found *',txt
enablefound 1
)
jdm_compare_button=: comparefiles
jdm_compareall_button=: compareallfiles
jdm_edit_button=: fed_run
jdm_match_button=: match
jdm_open_button=: openfile
jdm_swap_button=: swapdirs
jdm_view_button=: viewfile
jdm_select_button=: favorites
jdm_copy_button=: copy1
jdm_copyat_button=: copyall
jdm_copylt_button=: copylater
jdm_copyst_button=: copysource
jdm_delts_button=: deletetarget
jdm_ignore_button=: ignore
jdm_mignorent_button=: ignorent
jdm_mignoresep_button=: ignoresep
jdm_mignorest_button=: ignorest
jdm_xdiff_button=: xdiff

jdm_source_button=: jdm_target_button=: jdm_type_button=: match
filetype_result=: 3 : 0
'type filters'=. y.
DMTYPES=: {."1 filters
DMEXTS=: {:"1 filters
wd 'psel ',HWNDP
wd 'set type ',todelim DMTYPES
wd 'setselect type ',": DMTYPES i. <type
)

comparefiles=: 3 : 0
if. 0=#name=. getfoundname2'' do.
  dminfo 'Nothing to compare'
else.
  wdview fcompare name
end.
)
compareallfiles=: 3 : 0
nms=. {."1 DIFFCONT
res=. ''
for_n. nms do.
  src=. getsourcename1 > n
  tar=. (fmtdir DMTARGET), > n
  res=. res, fcompare src;tar
  res=. res, LF, LF
end.
res=. _2 }. res
if. 0=#res do.
  dminfo 'Nothing to compare'
else.
  wdview res
end.
)
xdiff=: 3 : 0
if. 0=#name=. getfoundname2'' do.
  dminfo 'Nothing to compare'
else.
  'a b'=. name
  wd 'winexec ',EAV,XDIFF,' "',a,'" "',b,'"',EAV
end.
)
copy1=: 3 : 0
if. isempty name=. getfoundname2'' do.
  name=. getfoundname1''
end.
if. isempty name do.
  txt=. 'No file selected'
  dminfo txt return.
end.

name=. boxopen name
if. 2 = #name do.
  'fm to'=. name
else.
  fm=. 0 pick name
  to=. (fmtdir DMTARGET), (#fmtdir DMSOURCE) }. fm
end.

fm=. jhostpath fm
to=. jhostpath to

svn=. (DMTAB-:'svn') # ' Subversion'
txt=. 'OK to copy from',svn,' source to target: ',LF,LF,fm

if. 0 = 0 dmquery txt do.
  res=. filecopy fm;to
  if. res = 0 do.
    txt=. 'Unable to copy file'
    dminfo txt
  else.
    remfound fm
  end.
else.
  dminfo 'Not done'
end.
)
copyall=: 3 : 0
not=. {."1 NOTINTARGET
dif=. ((DIFFCONTST=0) # {."1 DIFFCONT), (DIFFTIMEST=0) # {."1 DIFFTIME
fls=. not, dif
if. 0 = # fls do.
  dminfo 'No source files to copy' return.
end.
fm=. getsourcename fls
to=. (fmtdir DMTARGET)&, each fls
to docopy fm
)
copylater=: 3 : 0
dif=. DIFFCONT,DIFFTIME
msk=. (4 {"1 dif) earlier 1 {"1 dif
fls=. msk # {."1 dif
if. 0 = # fls do.
  dminfo 'No source files to copy' return.
end.
fm=. getsourcename fls
to=. (fmtdir DMTARGET)&, each fls
to docopy fm
)
copysource=: 3 : 0
if. 0 = # NOTINTARGET do.
  dminfo 'No source files to copy' return.
end.
fm=. getsourcename {."1 NOTINTARGET
to=. (fmtdir DMTARGET)&, each {."1 NOTINTARGET
to docopy fm
)
deletetarget=: 3 : 0
if. 0 = # NOTINSOURCE do.
  dminfo 'No target files to delete' return.
end.
nms=. (fmtdir DMTARGET)&, each {."1 NOTINSOURCE
if. 0 = 0 dmquery 'OK to delete:',LF,LF,tolist nms do.
  ferase nms
  match''
end.
)
docopy=: 4 : 0
to=. x.
fm=. y.
if. 0 = 0 dmquery 'OK to copy:',LF,LF,tolist fm do.
  res=. filecopy"1 fm ,. to
  if. 0 e. res do.
    txt=. 'Unable to copy:',LF,LF,tolist (res=0)#fm
    dminfo txt
  end.
  if. 1 e. res do.
    match''
  end.
else.
  dminfo 'Not done'
end.
)
enablefound=: 3 : 0
f=. <' ',": y.
hit=. (#DIFFCONT),(#DIFFTIME),(#NOTINSOURCE),#NOTINTARGET
nms=. 'copy ignore found open view print xdiff'
if. y. = 0 do.
  nms=. ;: nms , ' mignorest mignorent compare compareall contents copyat copylt copyst delts'
  cmd=. nms ,each f
else.
  cmd=. (;: nms) ,each f
  dd=. (#DIFFCONT) + #DIFFTIME
  cmd=. cmd, <'compare ',": 0 < dd
  cmd=. cmd, <'compareall ',": 0 < dd
  cmd=. cmd, <'contents ',": (0 < 0 { hit) *. 0 < +/ }. hit
  cmd=. cmd, <'copyat ',": 0 < (#NOTINTARGET) + dd
  cmd=. cmd, <'copylt ',": 0 < (#NOTINTARGET) + 0 e. DIFFTIMEST, DIFFCONTST
  cmd=. cmd, <'copyst ',": 0 < #NOTINTARGET
  cmd=. cmd, <'delts ',": 0 < #NOTINSOURCE
  cmd=. cmd, <'mignorest ',": 1 e. DIFFTIMEST, DIFFCONTST
  cmd=. cmd, <'mignorent ',": 0 < #NOTINSOURCE
  cmd=. cmd, <'xdiff ',": (*#XDIFF) *. 0 < dd
end.
wd }. ; ';setenable '&, each cmd
)
ignore=: 3 : 0
nms=. getignorenames''
if. isempty nms do.
  dminfo 'No file selected' return.
end.
msk=. -. ({."1 DIFFCONT) e. nms
DIFFCONT=: msk # DIFFCONT
DIFFCONTST=: msk # DIFFCONTST

msk=. -. ({."1 DIFFTIME) e. nms
DIFFTIME=: msk # DIFFTIME
DIFFTIMEST=: msk # DIFFTIMEST

NOTINSOURCE=: nms ignore1 NOTINSOURCE
NOTINTARGET=: nms ignore1 NOTINTARGET
match_fmt''
dmshowfind''
)
ignore1=: 4 : 0
y. #~ -. ({."1 y.) e. x.
)
ignorent=: 3 : 0
NOTINSOURCE=: 0 # NOTINSOURCE
match_fmt''
dmshowfind''
)
ignoresep=: 3 : 0
IGNORESEP=: -. IGNORESEP
wd 'set mignoresep ',":IGNORESEP
makecompare''
i.0 0
)
ignorest=: 3 : 0
if. -. 1 e. DIFFCONTST, DIFFTIMEST do. return. end.
if. #DIFFCONT do.
  DIFFCONT=: (-. DIFFCONTST) # DIFFCONT
  DIFFCONTST=: (#DIFFCONT) $ 0
end.
if. #DIFFTIME do.
  DIFFTIME=: (-. DIFFTIMEST) # DIFFTIME
  DIFFTIMEST=: (#DIFFTIME) $ 0
end.
match_fmt''
dmshowfind''
)
openfile=: 3 : 0
if. 0=#name=. getfoundname1'' do.
  dminfo 'No file selected'
else.
  try.
    smopen_jijs_ name
    smfocus_jijs_ ''
    
  catch. dminfo 'Unable to open: ',name
  end.
end.
)
swapdirs=: 3 : 0
dmread''
'DMSOURCEFM DMTARGET'=: DMTARGET ; DMSOURCEFM
if. #DMFOUNDALL do.
  'NOTINSOURCE NOTINTARGET'=: NOTINTARGET ;< NOTINSOURCE
  DIFFCONT=: 3 4 5 0 1 2 {"1 DIFFCONT
  DIFFTIME=: 3 4 5 0 1 2 {"1 DIFFTIME
  match_fmt''
end.
dmshow''
dmshowfind''
)
viewfile=: 3 : 0
if. 0=#name=. getfoundname1'' do.
  dminfo 'No file selected'
else.
  dat=. 1!:1 :: _1: < name
  if. dat -: _1 do.
    dminfo 'Unable to view: ',name
  else.
    name wdview dat
  end.
end.
)
view_result=: 3 : 0
wd 'psel ',HWNDP
wd 'setfocus found'
)

JDMSNP=: 0 : 0
pc jdmsnp;
xywh 4 6 30 11;cc s21 static;cn "Project:";
xywh 35 5 127 150;cc pproject combodrop ws_vscroll cbs_autohscroll rightmove;
xywh 166 7 10 9;cc bpproject button leftmove rightmove;cn ">>";
xywh 4 21 30 11;cc s22 static;cn "Source:";
xywh 35 20 127 150;cc psource combolist ws_vscroll cbs_autohscroll rightmove;
xywh 4 36 30 11;cc s23 static;cn "Target:";
xywh 35 35 127 150;cc ptarget combolist ws_vscroll rightmove;
xywh 181 6 92 10;cc ptimestamp checkbox leftmove rightmove;cn "Compare timestamps";
xywh 278 4 50 12;cc pmatch button leftmove rightmove;cn "Match";
pas 0 0;pcenter;
rem form end;
)
jdmsnp_run=: 3 : 0
wd JDMSNP
)
jdmsnp_bpproject_button=: 3 : 0
p=. PJPATH,PJPROJ
dir=. p,(0=#p) # jpath '~home'
txt=. dmlower 0 dirbrowse 'Select Folder';dir;0
if. (#txt) > txt -: dmlower psource do.
  PJPATH=: ''
  PJPROJ=: txt
  jdmsnp_getfiles''
  dmshow''
  matchclear''
end.
)
jdmsnp_pproject_select=: 3 : 0
PJPROJ=: pproject
jdmsnp_show''
)
jdmsnp_match=: 3 : 0
jdmsnp_read''
match''
)

jdmsnp_pmatch_button=: jdmsnp_match
jdmsnp_read=: 3 : 0
p=. PJPATH,PJPROJ,PATHSEP
DMSOURCEFM=: p,'.snp',PATHSEP,psource
if. ptarget -: 'Current' do.
  DMTARGET=: p
else.
  DMTARGET=: p,'.snp',PATHSEP,ptarget
end.
DMSOURCE=: DMSOURCEFM
DMTIMESTAMP=: 0 ". ptimestamp
DMTYPE=: 'All'
DMSUBDIR=: 0
)
jdmsnp_show=: 3 : 0
jdmsnp_getfiles''
wd 'set std 0;set snp 1;set svn 0'
wd 'set pproject ',todelim PJPROJS
wd 'setselect pproject ',": PJPROJS index <PJPROJ
wd 'set psource ',todelim PJSOURCES
wd 'setselect psource ',":PJSOURCES index <PJSOURCE
wd 'set ptarget ',todelim PJTARGETS
wd 'setselect ptarget ',":PJTARGETS index <PJTARGET
wd 'set ptimestamp ',":DMTIMESTAMP
wd 'setfocus pmatch'
)
jdmsnp_getfiles=: 3 : 0
if. isempty PJPROJ do.
  PJPROJS=: PJSOURCE=: PJSOURCES=: PJTARGET=: PJTARGETS=: ''
  if. -. (<'jproject') e. 18!:1 [ 0 do. return. end.
  if. 0 ~: 4!:0 <'PJFILES_jproject_' do. return. end.
  files=. PJFILES_jproject_
  file=. PROJECTFILE_jproject_
  PJPATH=: PIDPATH_jproject_
  len=. #PJPATH
  PJPROJ=: }: len }. 0 pick pathname file
  PJPROJS=: }: each len }.each {."1 pathname &> files
end.
PJSOURCES=: ss_list_jproject_ PJPATH,PJPROJ,PATHSEP
PJTARGETS=: (<'Current'),PJSOURCES
PJSOURCE=: >{. (PJSOURCES intersect <PJSOURCE), PJSOURCES
PJTARGET=: >{. (PJTARGETS intersect <PJTARGET), PJTARGETS
)

JDMSTD=: 0 : 0
pc jdmstd;
xywh 4 6 30 11;cc s0 static;cn "Source:";
xywh 35 5 127 150;cc source combodrop ws_vscroll cbs_autohscroll rightmove;
xywh 4 21 30 11;cc s1 static;cn "Target";
xywh 35 20 127 150;cc target combodrop ws_vscroll cbs_autohscroll rightmove;
xywh 4 36 30 11;cc s2 static;cn "Type:";
xywh 35 35 107 150;cc type combolist ws_vscroll cbs_autohscroll rightmove;
xywh 166 7 10 9;cc bsource button leftmove rightmove;cn ">>";
xywh 166 21 10 9;cc btarget button leftmove rightmove;cn ">>";
xywh 145 37 10 9;cc btypes button leftmove rightmove;cn ">>";
xywh 181 6 92 10;cc timestamp checkbox leftmove rightmove;cn "Compare timestamps";
xywh 181 21 92 10;cc subdir checkbox leftmove rightmove;cn "Include subdirectories";
xywh 278 4 50 12;cc match button leftmove rightmove;cn "Match";
pas 0 0;pcenter;
rem form end;
)
jdmstd_run=: 3 : 0
wd JDMSTD
)
jdmstd_btypes_button=: 3 : 0
(type;<DMTYPES,.DMEXTS) conew 'jfiletype'
)
jdmstd_bsource_button=: 3 : 0
dir=. DMSOURCE,(0=#DMSOURCE)#jpath '~home'
txt=. dmlower 0 dirbrowse 'Select Folder';dir;0
if. (#txt) > txt -: dmlower source do.
  DMSOURCE=: txt
  DMDIRS=: ~. (<txt), DMDIRS
  dmshow''
  matchclear''
end.
)
jdmstd_btarget_button=: 3 : 0
dir=. DMTARGET,(0=#DMTARGET)#jpath '~home'
txt=. dmlower 0 dirbrowse 'Select Folder';dir;0
if. (#txt) > txt -: dmlower target do.
  DMTARGET=: txt
  DMDIRS=: ~. (<txt), DMDIRS
  dmshow''
  matchclear''
end.
)
jdmstd_match=: 3 : 0
jdmstd_read''
match''
)

jdmstd_match_button=: jdmstd_match
jdmstd_read=: 3 : 0
DMSOURCEFM=: (0 < #source) # fullname source
DMTARGET=: (0 < #target) # fullname target
DMSOURCE=: (DMTAB-:'svn') pick DMSOURCEFM;DMTARGET
DMTIMESTAMP=: 0 ". timestamp
DMTYPE=: type
DMSUBDIR=: 0 ". subdir
)
jdmstd_show=: 3 : 0
txt=. todelim DMALL
wd 'set std 1;set snp 0;set svn 0'
wd 'set source ',txt
wd 'set target ',txt
if. 0 < # DMALL do.
  wd 'setselect source 0'
end.
if. 1 < # DMALL do.
  wd 'setselect target 1'
end.
wd 'set type ',todelim DMTYPES
wd 'setselect type ',": (#DMTYPES) | DMTYPES i. <DMTYPE
wd 'set timestamp ',":DMTIMESTAMP
wd 'set subdir ', ":DMSUBDIR
wd 'setfocus match'

)

JDMSVN=: 0 : 0
pc jdmsvn;
xywh 4 6 171 11;cc s10 static;cn "Source from Subversion";
xywh 4 21 30 11;cc s11 static;cn "Target";
xywh 35 20 127 150;cc vtarget combodrop ws_vscroll cbs_autohscroll rightmove;
xywh 4 36 30 11;cc s12 static;cn "Type:";
xywh 35 35 107 150;cc vtype combolist ws_vscroll cbs_autohscroll rightmove;
xywh 166 21 10 9;cc bvtarget button leftmove rightmove;cn ">>";
xywh 145 37 10 9;cc bvtypes button leftmove rightmove;cn ">>";
xywh 181 6 92 10;cc vtimestamp checkbox leftmove rightmove;cn "Compare timestamps";
xywh 181 21 92 10;cc vsubdir checkbox leftmove rightmove;cn "Include subdirectories";
xywh 278 4 50 12;cc vmatch button leftmove rightmove;cn "Match";
pas 0 0;pcenter;
rem form end;
)
jdmsvn_run=: 3 : 0
wd JDMSVN
)
jdmsvn_bvtypes_button=: 3 : 0
(type;<DMTYPES,.DMEXTS) conew 'jfiletype'
)
jdmsvn_bvtarget_button=: 3 : 0
dir=. DMTARGET,(0=#DMTARGET)#jpath '~home'
txt=. dmlower 0 dirbrowse 'Select Folder';dir;0
if. (#txt) > txt -: dmlower vtarget do.
  DMTARGET=: txt
  DMDIRS=: ~. (<txt), DMDIRS
  dmshow''
  matchclear''
end.
)
jdmsvn_vmatch=: 3 : 0
jdmsvn_read''
match''
)

jdmsvn_vmatch_button=: jdmsvn_vmatch
jdmsvn_read=: 3 : 0
DMTARGET=: (0 < #vtarget) # fullname vtarget
DMSOURCE=: (DMTAB-:'svn') pick DMSOURCEFM;DMTARGET
DMTIMESTAMP=: 0 ". timestamp
DMTYPE=: type
DMSUBDIR=: 0 ". subdir
)
jdmsvn_show=: 3 : 0
wd 'set std 0;set snp 0;set svn 1'
txt=. todelim DMALL
wd 'set vtarget ',txt
if. 1 < # DMALL do.
  wd 'setselect vtarget 1'
end.
wd 'set vtype ',todelim DMTYPES
wd 'setselect vtype ',": (#DMTYPES) | DMTYPES i. <DMTYPE
wd 'set vtimestamp ',":DMTIMESTAMP
wd 'set vsubdir ', ":DMSUBDIR
wd 'setfocus vmatch'
)


dirmatch_z_=: dmrun_jdirmatch_