Larry
unread,Apr 15, 2013, 11:41:27 AM4/15/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to GoogleSemWare TSE, tse...@freelists.org
/*** C2fa.s - this source is attached
C2fa was written with TSEPro 4.4, but works with TSE 2.5 on up.
This macro compares two selected files for unique lines. Unique lines
are those that occur in the first file but not in the second file, and
are lines that occur in the second file but not in the first file.
Once these lines are found, they are placed into a report file with the
line number and which file they came from, and then sorted by line
number.
There is no pretense that C2fa does what the Potpourri macro CmpFiles
does, but C2fa does provide a different approach to the problem.
How To Use:
===============
1. Make note of the assigned execution key below, compile into the TSE
/mac directory.
2. Execute the macro.
3. The first prompt asks for the filename of the first file to be
compared. The prompt has an F1 Help key and other custom keys to make
it easy to select the file.
4. The second prompt asks for the second file to be compared and offers
its own help screen and the same custom keys for finding the file.
5. Once the second file has been selected, the macro finds the unique
lines in each file and places them into a report file.
6. When finished, the macro shows a summary display and exits.
7. After successful execution and the creation of the report file, the
macro offers three useful assigned keys for navigating between the
three files:
a. CtrlShift SpaceBar: switch between the two compared files.
b. CtrlShift BackSpace: while in the report file, pressing this key
will jump to the file and line where the current cursor line in
the report file came from.
c. CtrlShift Tab: jumps to the report file.
The macro also keeps a custom history of the files that you have already
compared.
Enjoy.
***/
// C2fa.s - compare two files for differences and produce a "report"
string rev[40]="C2faRev1016"
// assigned execution key:
<ctrlshift c><a> execmacro("C2fa")
/***
{This Revision:}
1016- 20130415- safe save - published
1015- 20130415- finished the helps, added alt-o for overview, ctrlshift
tab to jump to report
1014- 20130415- safe save
1013- 20130415- write the helps and msg and clean up
1012- 20130414- safe save
1002- 2009 12 07 @ 3:12 pm- renamed and put into C2fa
1001- 2009 12 07 @ 3:12 pm- derived from jigcompare.s
-------------------
***/
// local globals
string fn1[255]="" // original file 1 name
string fn2[255]="" // original file 2 line
integer tb1=0 // file 1 buffer id
integer tb2=0 // gile 2 buffer id
integer tb3=0 // report file buffer id
integer nl1=0 // numlines file 1
integer nl2=0 // numlines file 2
string xfil[255]="" // picked file for prompts
integer bad=0 // loop exit flag
integer nf=0 // number of loaded files for List Open
integer c2fa_hist=0 // custom c2fa file history list
helpdef overview
title="Overview for the C2fa Macro"
""
"Macro Name: Compare 2 Files A"
"Macro Version: C2faRev1016"
""
"Assigned Keys:"
"<CtrlShift c><a> execute macro"
"<CtrlShift SpaceBar> switch between Fn1 and Fn2 after execution"
"<CtrlShift BackSpace> jump to file and line number from report file"
"<ctrlshift tab> jump to the report file"
""
"This macro compares two files for unique lines and produces a report"
"about lines in File 1 not found in File 2, and lines in File 2 not found"
"in File 1."
""
"The usual two files to compare would be a text file and its backup, such"
"as filename.txt and filename.bak. Any kinds of text files can be"
"compared, even program source files."
""
"The report produced by this macro is a list of all unique lines found in"
"both files. The list is formatted into three columns: line numbers,"
"source file, and the line text. The two source files are identified with"
"the short names: Fn1 for the first, Fn2 for the second."
""
"Two accessory assigned keys are also available after the report has been"
"generated and the macro remains loaded:"
"<CtrlShift SpaceBar> toggles between the two files"
"<CtrlShift BackSpace> with the macro still loaded, when the cursor line"
"<alt o> show this Overview screen"
" is on a line in the report, pressing this key will"
" automatically go to that line in that file."
""
"(Recompiling or purging the macro will defeat those two keys.)"
""
"Once the chosen file appears in the prompt, press Enter to accept it."
""
"The macro now proceeds to find the unique lines in both files and"
"produces the report. While it is working, a status message is presented"
"on the statusline."
""
"Quickness of the macro will vary depending on how large the files are"
"and how many unique lines it finds. On a 12,000 line pair of files with"
"a known 44 unique lines, it takes about 4 seconds on my computer."
""
"When finished, editing can take place. Using the two assigned keys"
"CtrlShift SpaceBar and CtrlShift BackSpace, finding and editing the"
"changes can be done more easily."
""
"Note: This macro does NOT test for line sequence. If the only difference"
"between two files is the sequence of lines, then these will not be"
"reported. File swapping would be the best way to identify such"
"differences (aided using CtrlShift SpaceBar, of course)."
""
"Finally, the macro makes NO changes to the original two files. Any"
"editing to be done must be done manually."
""
end
helpdef file1help
title="Choosing File 1 Help (C2faRev1016)"
""
"This first C2fa prompt asks you for the filename of the first of the two"
"files you wish to compare with each other. It does not matter which of"
"the two files is first."
""
"Entering the filename:"
"1. Type in the filename. If the file is in the file ring, it is"
" accepted. If the file exists on disk, it will be loaded and accepted."
""
"2. Use the F2 key to list all of the files currently loaded in TSE's"
" file ring. Select a filename and press Enter. The filename appears in"
" the prompt."
""
"3. Use the F5 and F6 keys to move through the file ring, F5 to previous"
" files, and F6 to next files. The screen is updated for each file and"
" that file's filename appears in the prompt."
""
"While at the prompt, you can also use the F1 key for this help, and the"
"Alt-o key to show the macro Overview."
""
"Once the chosen file appears in the prompt, press Enter to accept it."
""
"The next prompt asks for the SECOND file to compare."
""
end
helpdef file2help
title="Choosing File 2 Help (C2faRev1016)"
""
"This second C2fa prompt asks you for the filename of the second of the"
"two files you wish to compare with each other."
""
"Entering the filename:"
"1. Type in the filename. If the file is in the file ring, it is"
" accepted. If the file exists on disk, it will be loaded and accepted."
""
"2. Use the F2 key to list all of the files currently loaded in TSE's"
" file ring. Select a filename and press Enter. The filename appears in"
" the prompt."
""
"3. Use the F5 and F6 keys to move through the file ring, F5 to previous"
" files, and F6 to next files. The screen is updated for each file and"
" that file's filename appears in the prompt."
""
"While at the prompt, you can also use the F1 key for this help, and the"
"Alt-o key to show the macro Overview."
""
"Once the chosen file appears in the prompt, press Enter to accept it."
""
"The macro now proceeds to find the unique lines in both files and"
"produces the report. While it is working, a status message is presented"
"on the statusline."
""
end
// create list of open files and get a selected filename
proc mcreatelist()
integer tb=0
integer i=0
pushposition()
tb=createbuffer("fnx",_hidden_)
emptybuffer()
popposition()
pushposition()
for i=1 to nf+1
addline(currfilename(),tb)
nextfile(_dontload_)
endfor
gotobufferid(tb)
begfile()
if currlinelen()==0
delline()
endif
set(y1,query(windowy1))
list("Select a file and press Enter or ESC to close",query(windowcols))
if query(key) in <enter>,<greyenter>,<leftbtn>
xfil=gettext(1,currlinelen())
addhistorystr(xfil,c2fa_hist)
popposition()
begline()
deltoeol()
inserttext(xfil)
else
popposition()
endif
abandonfile(tb)
end
// first prompt custom keys
keydef c2fa1keys
<f1> quickhelp(file1help)
<f2> mcreatelist() if xfil<>"" fn1=xfil endif // list open files
<f5> bad=1 pushkey(<escape>) // prev file
<f6> bad=2 pushkey(<escape>) // next file
<alt o> quickhelp(overview)
end
// first prompt startup
proc c2fa1Startup()
Enable(c2fa1Keys)
UnHook(c2fa1Startup)
windowfooter("{F1}=Help {F2}=List Open {F5}=PrevFile {F6}=NextFile {alt
o}-Overview")
end
// second prompt custom keys
keydef c2fa2keys
<f1> quickhelp(file2help)
<f2> mcreatelist() if xfil<>"" fn2=xfil endif // list open files
<f5> bad=1 pushkey(<escape>) // prev file
<f6> bad=2 pushkey(<escape>) // next file
<alt o> quickhelp(overview)
end
// second prompt startup
proc c2fa2Startup()
Enable(c2fa2Keys)
UnHook(c2fa2Startup)
windowfooter("{F1}=Help {F2}=List Open {F5}=PrevFile {F6}=NextFile {alt
o}-Overciew")
end
// return to file and line from report file
proc mback()
string a[20]=""
integer b=0
// get the line number
b=val(gettext(1,20))
// get either fn1 or fn2 buffer id tb1 or tb2 and go to it
if lfind("Fn[12]","gcx")
a=gettext(currpos(),3)
iif(a=="Fn1",gotobufferid(tb1),gotobufferid(tb2))
gotoline(b)
scrolltocenter()
begline()
else
warn("Neither Fn1 or Fn2 found. Nothing to do.")
endif
end
proc main()
string a[255]="" // get each text line
integer i=0
integer numdif1=0 // # file 1 differences
integer numdif2=0 // # file 2 differences
integer tb=0 // report buffer id
// set the history
c2fa_hist=getfreehistory("C2fa:Compare_Files")
// current filename
fn1=currfilename()
// number of loaded files
nf=numfiles()
// prompt to select first file
repeat
bad=0
Hook(_PROMPT_STARTUP_, c2fa1Startup)
set(y1,query(windowrows)/2-3)
ask("Choose the FIRST File to Compare, ESC=Quit",fn1,c2fa_hist)
unHook(c2fa1Startup)
// quit or repeat
if query(key)==<escape> and bad==0
warn("Cancelled.")
goto ending
endif
// previous/next file movement
case bad
when 1 prevfile() updatedisplay() fn1=currfilename()
when 2 nextfile() updatedisplay() fn1=currfilename()
endcase
i=i+1 // loop count
until bad==0 or i>10
editfile(fn1)
updatedisplay()
nl1=numlines()
// prompt to select second file
repeat
bad=0
Hook(_PROMPT_STARTUP_, c2fa2Startup)
set(y1,query(windowrows)/2)
ask("Choose SECOND File to Compare, ESC=Quit",fn2,c2fa_hist)
unHook(c2fa2Startup)
if query(key)==<escape> and bad==0
warn("Cancelled.")
goto ending
endif
case bad
when 1 prevfile() updatedisplay() fn2=currfilename()
when 2 nextfile() updatedisplay() fn2=currfilename()
endcase
i=i+1
until bad==0 or i>20
editfile(fn2)
nl2=numlines()
// set the buffer id's
if fn1<>fn2
tb1=getbufferid(fn1)
tb2=getbufferid(fn2)
else
warn("The two files cannot be the same. Cancelled.")
goto ending
endif
// identify unique lines in both fn1 and fn2
if tb2
tb3=editfile("c2fa.dat")
emptybuffer()
// get unique lines in fn1 not in fn2
for i=1 to nl1
gotobufferid(tb1) // fn1
gotoline(i)
if currlinelen()
a=gettext(1,currlinelen())
gotobufferid(tb2) // fn2
if lfind(a,"g^")==0
gotobufferid(tb1) // go to fn1...
begline() // ...to get line number
addline(format(str(currline()):length(str(nl1)):" ")+" Fn1 "+a,tb3)
numdif1=numdif1+1 // fn1 differences count
message("Finding unique lines in Fn1: "+str(numdif1))
endif
gotobufferid(tb1) // fn1
endif
endfor
// get unique lines in fn2 not in fn1
for i=1 to nl2
gotobufferid(tb2) // fn2
gotoline(i)
if currlinelen()
a=gettext(1,currlinelen())
gotobufferid(tb1) // fn1
if lfind(a,"g^")==0
gotobufferid(tb2) // go to fn2...
begline() // ...to get line number
addline(format(str(currline()):length(str(nl2)):" ")+" Fn2 "+a,tb3)
numdif2=numdif2+1 // fn2 differences count
message("Finding unique lines in Fn2: "+str(numdif2))
endif
gotobufferid(tb2)
endif
endfor
// go to top of fn1 and fn2
begfile()
gotobufferid(tb1)
begfile()
// sort the report
gotobufferid(tb3)
begfile()
markcolumn(1,1,numlines(),9)
execmacro("sort")
unmarkblock()
begfile()
updatedisplay()
// show the report
tb=createtempbuffer()
addline()
addline("The differences between the two iles:")
addline("File fn1: "+fn1)
addline("File fn2: "+fn2)
addline()
addline("File fn1 Differences: "+str(numdif1))
addline("File fn2 Differences: "+str(numdif2))
addline("")
addline("File fn1 Lines: "+str(nl1))
addline("File fn2 Lines: "+str(nl2))
addline()
addline("Remember the three accessory assigned keys after successful
execution:")
addline("CtrlShift SpaceBar: switch between Fn1 and Fn2")
addline("CtrlShift BackSpace: go to file and line number of line in report
file")
addline("CtrlShhift Tab: jump to the report file")
addline("")
list("("+rev+") Two File Compare Summary",query(windowcols))
abandonfile(tb)
endif
goto ending
ending:
end
<ctrlshift spacebar>
iif(getbufferid()==tb1,gotobufferid(tb2),gotobufferid(tb1))
<ctrlshift backspace> mback()
<ctrlshift tab> gotobufferid(tb3)
<alt o> quickhelp(overview)