/sys/doc/ Documentation archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

wm/edit



This diff adds some simple stuff to wm/edit:
	1.  `Search:Line No.'  
	2.  `File:New'
	3.  `File:Save Snarf' Save text previously snarf'd with cut or
copy to a file.  If file exists, optionally append to it.

Also fixes a few irritants:  prompt to save current file before opening
a new one and before exiting the app;  check whether target already
exists for `Save as' and `Save' [new file];  update titlebar with
fully qualified filename after `Save as'.

Apologies for any spurious formatting of diff by Netscrape.

Steve


18a19,22
> include "lib.m";
> 	str: String;
> 	workdir: Workdir;
> 
37a42
> 	".m.file.menu add command -label New -command {send c new}",
40a46,47
> 	".m.file.menu add separator",
> 	".m.file.menu add command -label {Save snarf} -command {send c snarfsave}",
47a55
> 	".m.search.menu add command -label {Line No.} -command {send c goto}",
51a60,61
> 	".b.t tag configure lineno -relief raised -borderwidth 2 -bg white",
> 	".b.t tag bind lineno <ButtonPress-1> {send c cleartags}",
66a77,78
> 	str = load String String->PATH;
> 	workdir = load Workdir Workdir->PATH;
98d109
< 
103a115
> 	newcurfile();
124a137,144
> 			"goto" =>
> 				do_goto();
> 			"new" =>
> 				do_new();
> 			"snarfsave" =>
> 				do_snarfsave();
> 			"cleartags" =>
> 				tk->cmd(ed, ".b.t tag remove lineno 1.0 end");
127c147,148
< 			if(s == "exit")
---
> 			if(s == "exit"){
> 				save_ask();
128a150
> 			}
148a171
> 	savecurf := 0;
152c175,179
< 			break;
---
> 			break;	
> 		if(savecurf == 0){
> 			save_ask();
> 			savecurf = 1;
> 		}
172c199
< 		if(prompt || curfile == "")
---
> 		if(prompt || curfile == ""){
174c201,205
< 		if(savetfile(fname, contents))
---
> 			if(isfile(fname) == "exists")
> 				if(tklib->dialog(ed,fname+" already
exists",0,"Cancel"::"Overwrite"::nil) == 0)
> 					break;
> 		}
> 		if(savetfile(fname, contents, 1))
248c279
< savetfile(path: string, contents: string): int
---
> savetfile(path: string, contents: string, chg_curfile: int): int
261c292,297
< 	curfile = path;
---
> 	if(chg_curfile){
> 		curfile = fullpath(path);
> 		newcurfile();
> 	}
> 	return 1;
> }
262a299,348
> wd: string;
> fullpath(file: string) : string
> {
> 	if(wd == nil) 
> 		wd = workdir->init(); 
> 	case file[0] {
> 		'/' =>
> 			return file;
> 		'.' =>
> 			if(file[1] == '/')
> 				return wd + file[1:];
> 		* =>
> 			return wd + "/" + file;
> 	}
> 	# deal with ../
> 	for(j := 0; file[j:j+3] == "../"; j+=3)
> 		;
> 	(n, ls) := sys->tokenize(wd, "/");
> 	path: string;
> 	for(n -= j / 3; n > 0; n--){
> 		path += "/" + hd ls;
> 		ls = tl ls;
> 	}
> 	return path + "/" + file[j:];
> }
> 
> isfile(path: string) : string
> {
> 	(is, d) := sys->stat(path);
> 	if(is == -1)
> 		return "no";
> 	if(d.mode & Sys->CHDIR)
> 		return "dir";
> 	return "exists";
> }
> 
> appendtfile(file: string, s: string) : int
> {
> 	fd := sys->open(file, Sys->OWRITE);
> 	if(fd == nil)
> 		return 0;
> 	if(sys->seek(fd, 0, Sys->SEEKEND) < 0)
> 		return 0;
> 	buf := array of byte s;
> 	n := len buf;
> 	i := sys->write(fd, buf, n);
> 	if(i != n){
> 		sys->print("Only wrote %d of %d bytes: %r\n", i, n);
> 		return 0;
> 	}
264a351,353
> 	
> do_snarfsave()
> {
265a355,382
> 	if (snarf == ""){
> 		tklib->notice(ed, "No snarf to save");
> 		return;
> 	}
> loop: for(;;){
> 		file := tklib->getstring(ed, "Save to ");
> 		if(file == "")
> 			break;
> 		case isfile(file) {
> 		"dir" =>
> 			tklib->notice(ed, file+" is a directory");
> 			continue;
> 		"exists" =>
> 			if(! tklib->dialog(ed, file+" already exists.", 0,
"Cancel"::"Append"::nil))
> 				break loop;
> 			if(appendtfile(file, snarf))
> 				break loop;
> 		"no" =>
> 			if(savetfile(file, snarf, 0))
> 				break loop;
> 		}
> 		# error on appendtfile or savetfile
> 		if(!tklib->dialog(ed, "Can't save file "+file,0,"Cancel"::"Try another
file"::nil))
> 			break loop;
> 	}
> 
> }
> 					
268c385,449
< 	tk->cmd(ed, ".m.filename configure -text '" + curfile);
---
> 	t: string;
> 	if(curfile == "")
> 		t = "(new file)";
> 	else
> 		t = curfile;
> 	tk->cmd(ed, ".m.filename configure -text '" + t);
> }
> 
> do_goto()
> {
> 	line, end: string;
> 	for(;;){
> 		if((line = tklib->getstring(ed, "Goto")) == nil)
> 			return;	
> 		(nl, suf) := str->toint(line, 10);	
> 		if(nl < 1 || suf != ""){
> 			if(! tklib->dialog(ed, "Bad line number", 0, "Cancel"::"Reenter"::nil))
> 				return;
> 		}else{
> 			end = tk->cmd(ed, ".b.t index end");
> 			if(end == "1.0")
> 				return;
> 			(endline, endchar) := str->splitl(end, ".");
> 			(ne, bar) := str->toint(endline, 10);
> 
> 			if(endchar == ".0") #can't set tag 
> 				ne--;
> 			if(nl > ne)
> 				line = string(ne);
> 			tk->cmd(ed, ".b.t tag remove lineno 1.0 end");
> 			tk->cmd(ed, ".b.t mark set insert "+line+".0"); 
> 			s := tk->cmd(ed, ".b.t get {insert linestart} {insert lineend}");
> 
> 			epos: string;
> 			if(s[0] == '\n')    #{insert lineend} wraps if line is a bare '\n'
> 				epos = line + ".1";
> 			else
> 				epos = line + "."+string(len s);	
> 			tk->cmd(ed, ".b.t tag add lineno "+line+".0 "+epos);
> 			tk->cmd(ed, ".b.t see "+line+".0");
> 			break;
> 		}
> 	}
> }
> 
> save_ask()
> {
> 	file: string;
> 	if(tk->cmd(ed, ".b.t index end") == "1.0")
> 		return; #empty file
> 	file = curfile;
> 	if(file == nil)
> 		file = "(new file)";
> 	if(tklib->dialog(ed, "Save "+file+"?", 0, "Yes"::"No"::nil) == 0)
> 		do_save(0);
> }
> 
> do_new()
> {
> 	if(tk->cmd(ed, ".b.t index end") == "1.0")
> 		return;
> 	save_ask();
> 	tk->cmd(ed, ".b.t delete 1.0 end");
> 	curfile = "";
> 	newcurfile();