Subj : ... To : All From : Robert Todd Date : Thu Dec 07 2000 01:54 am FORUM: (104) comp.lang.pascal.borland MSG#: 14134 FROM: NAT.MEAD@NTLWORLD.COM DATE: 22 Nov 2000, 12:12p TO: ALL SUBJECT: source - ed - line editor { heavily commented source of a line editor. This program is expecially for all the edlin freaks out there! its a hard to use (if your a lamer), non-gui, line editor with no frills thrown in for good measure. it'll edit and save any file regardless of its attributes. (ie. readonly) only edits upto 200 lines, although re-compiling for protected mode will allow loads more (why would you?). I threw this together expecially for the pascal newsgroups so newbies can read some pascal source thats quite easy. if you hate edlin with a vengance then you'll want to kill this. personally i prefer edlin to win95+, in my opionion edlin is far better! if anyone has command interpreter pascal sources *please* may i have a copy! ofcourse, theres absolutly no warentee. Code^Bandit. email me at: adam.mead^#^ntlworld.com spam will be ignored, i dont want any pizzas, double glazing, health ins. or anything else, although a new motorbike would be nice (since some idiot woman drove her car out infront of me. tsk!, women drivers!) } program ed; {command line, line editor} uses crt,dos; {crt for the readkey, dos for file functions} const maxlines = 200; {can adjust higher for fpc} type TLines = array[1..maxlines] of string; {[maxlines] strings of 255chars} var tfile : text; {textfile} input : string; {a string of 255chars max} lines : tlines; {this is the TLines array} i,x : integer; {temp vars used by any} ti : integer; {set to total number of lines in array} ch : char; {the key the user presses at prompt} loaded : boolean; {was the file loaded at load?} attr : word; {this is the files origional attributes} procedure clear; {clears the whole array} begin for i:=1 to maxlines do lines[i]:=''; {sets every line in array to nothing} i:=0; {sets var i to 0} ti:=0; {sets total number of lines to 0} end; procedure list; {display lines from array} var toline : integer; {line to stop displaying at} begin write('from: '); {$i-} readln(i); {$i+} {gets line num to display from} if i < 1 then i:=1; {to low a number?, set var i to 1} if i > ti then i:=ti; {number above total lines?, set to total lines} toline:=i+18; {toline set to line num+18 lines} if toline>ti then toline:=ti; {toline > total lines?, set toline to total} if ti > 0 then for x:=i to toline do writeln('[',x,'] ',lines[x]); {writelns lines[x] and inc(x) until it gets to toline} end; procedure edit; {edit a line} begin write('line: '); {$i-} readln(i); {$i+} {get line number to edit} if (i < 1) or (i > maxlines) then exit; {not a valid line number?, exit procedure} writeln(i,'>',lines[i]); {show the line number and the line itself} write('->'); {show the editing prompt} readln(input); {reads the newline input from user} if input <> '/q' then {if the newline is /q then abort edit} begin {else set the new line to var input} lines[i]:=input; if ti < i then ti:=i; {makes sure that theres at least one line now} end; end; procedure deletel; {deletes a line} begin write('line: '); {$i-} readln(i); {$i+} {get line number} if (i < 1) or (i > maxlines) then exit; {not allowed?, exit procedure} for x:=i to ti do {repeats from line number until got to max lines} begin if x < ti then lines[x]:=lines[x+1]; {sets lines[x] to the line after} end; lines[ti]:=''; {clears the last line in the array} ti:=ti-1; {decriments total number of lines} end; procedure insline; {inserts a line before num} begin write('line: '); {$i-} readln(i); {$i+} {gets line number to insert a line before} if (i < 1) or (i=maxlines) then exit; {if its not in allowed then exit procedure} for x:=ti+1 downto i+1 do {does it backwards} begin lines[x]:=lines[x-1]; {sets the line after x to the one before} end; lines[i]:=''; {clears the new line} inc(ti); {adds 1 to the total number of lines} end; procedure save; {saves array to file} begin close(tfile); {closes the file so we can work on it in other ways } getfattr(tfile,attr); {gets the files attributes, ie: read-only, hidden..} setfattr(tfile,archive); {sets it to archive, so we can write to it if r.o.} rewrite(tfile); {rewrites the file (clearing it)} append(tfile); {appends onto the end of it (i dont think this is needed)} for x:=1 to ti do writeln(tfile, lines[x]); {writes each line of array to} writeln('saved.'); {^^ the file} close(tfile); {closes the file so we can reset its attrs} setfattr(tfile, attr); {sets the files attrbutes back to origional} reset(tfile); {resets it for reading (we close it later)} end; procedure truncate; {truncates array to num lines} begin write('line: '); {gets line number to truncate from (inc itself)} {$i-} readln(i); {$i+} {as above} if (i < 1) or (i > ti) then exit; {if it aint within the allowed then exit} for x:=i to ti do {does x (inputed line) till ti (total lines)} begin lines[x]:=''; {blanks out lines[x]} end; if i > 1 then ti:=i-1 else ti:=1; {sets number of lines total to new num} end; procedure help; {ch='?'} begin writeln; writeln('ED - for all the edlin freaks! - v469.223.24á (c) 1994-2000 Code^Bandit.'); writeln; writeln('E - Edit line. /q on its own aborts'); writeln('I - Insert Line before line'); writeln('D - Delete line'); writeln('C - Clear the lot'); writeln('L - List file from line'); writeln('T - Truncate file line - eof'); writeln('S - Save to file'); writeln; end; begin {main} assign(output,''); {these two lines means it writes via dos and not to} rewrite(output); {video mem/bios directly, good for ansi stuff} ti:=0; {sets a global varible} if paramcount = 1 then {if command line paramiters =1 then.....} begin clear; {clears the array so we dont get no errors} loaded:=false; {we didnt load from command line} assign(tfile, paramstr(1)); {assigns the cmd line filename to tfile} {$i-} reset(tfile); {$i+} {resets it so we can read from it} if ioresult <> 0 then {cant reset, file exist?} begin {$i-} rewrite(tfile); {$i+} {create a new one} if ioresult <> 0 then {oh dear, invalid filename prehaps?} begin writeln('could not open/create file'); halt(99); {bombs out on errorlevel 99} end; reset(tfile); {we just created a file so lets reset it} writeln('new file created.'); ti:=1; {sets var ti to 1 (ti is the number of lines total in array} end else loaded:=true; {if fileexisted then we loaded it} if loaded then begin repeat inc(ti); readln(tfile, lines[ti]); {reads the lines into the array} until (eof(tfile)) or (ti=maxlines); {until theres no more or we got to max} end; repeat {main loop} write(ti,' :'); {writes total lines followed by prompt} ch:=readkey; {waits for a keypress, then reads it into var ch} writeln(ch); {writes it to screen} if ch in ['l','e','c','d','i','s','q','t','?'] then {if its any of these} case ch of {then do one of the following..} 'l' : list; {list lines..} 'e' : edit; {edit a line} 'c' : clear; {clear the array} 'd' : deletel; {delete a line} 'i' : insline; {insert a line} 's' : save; {save array to file} 't' : truncate; {truncate array to num lines..} '?' : help; {not much help :-) } end; {end of case} until ch='q'; {main loop until ch='q'} close(tfile); {close the file} end else {else no paramiters at command line} begin writeln('no file specifed to edit/create.'); halt(98); {bomb out with errorlevel 98} end; end. {very end of program} .... Platinum Xpress & Wildcat!..... Nice!!!! --- Platinum Xpress/Win/WINServer v3.0pr5 * Origin: Fido/3407 - MS Online - msol.dtdns.net (1:3407/4) .