ed

[hare] The standard editor
Log | Files | Refs | README | LICENSE

file.ha (865B)


      1 use errors;
      2 use fmt;
      3 use fs;
      4 use io;
      5 use os;
      6 use strings;
      7 
      8 fn edit(s: *Session, cmd: *Command, forced: bool) (void | Error) = {
      9 	assert_noaddrs(s, cmd.linenums)?;
     10 
     11 	if (!forced && s.buf.modified && !s.warned) {
     12 		s.warned = true;
     13 		return WarnBufferModified;
     14 	};
     15 
     16 	hist_clear(s.buf);
     17 	hist_newseq(s.buf);
     18 
     19 	const rd = os::open(filename(s, cmd, true)?)?; defer io::close(rd)!;
     20 
     21 	buf_deleteall(s.buf);
     22 	const (sz, _) = buf_read(s.buf, rd, 0)?;
     23 	s.buf.modified = false;
     24 
     25 	hist_discardseq(s.buf);
     26 
     27 	if (!s.suppressmode)
     28 		fmt::println(sz)!;
     29 
     30 	s.buf.cursor = len(s.buf.lines) - 1;
     31 };
     32 
     33 fn filename(s: *Session, cmd: *Command, remember: bool) (str | NoFilename) = {
     34 	if (cmd.arg1 != "") {
     35 		if (remember || s.buf.filename == "")
     36 			s.buf.filename = strings::dup(cmd.arg1);
     37 		return cmd.arg1;
     38 	};
     39 	if (s.buf.filename != "")
     40 		return s.buf.filename;
     41 
     42 	return NoFilename;
     43 };