| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
/* Contains commands that deal with creating, selecting, killing and |
| 9 |
* listing buffers, and buffer modes, and find-file, etc. |
| 10 |
*/ |
| 11 |
#include "jove.h" |
| 12 |
#include "jctype.h" |
| 13 |
#include "disp.h" |
| 14 |
#include "ask.h" |
| 15 |
#include "extend.h" |
| 16 |
#include "fmt.h" |
| 17 |
#include "insert.h" |
| 18 |
#include "marks.h" |
| 19 |
#include "move.h" |
| 20 |
#include "proc.h" |
| 21 |
#include "wind.h" |
| 22 |
|
| 23 |
#ifdef IPROCS |
| 24 |
# include "fp.h" |
| 25 |
# include "iproc.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifdef MAC |
| 29 |
# include "mac.h" |
| 30 |
#else |
| 31 |
# include <sys/stat.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
char |
| 35 |
iobuff[LBSIZE], |
| 36 |
genbuf[LBSIZE], |
| 37 |
linebuf[LBSIZE]; |
| 38 |
|
| 39 |
private void |
| 40 |
setbname(Buffer *, const char *); |
| 41 |
|
| 42 |
private const char Mainbuf[] = "Main", |
| 43 |
NoName[] = "Sans un nom!"; |
| 44 |
|
| 45 |
Buffer |
| 46 |
*world = NULL, /* First in the list */ |
| 47 |
*curbuf = NULL, /* pointer into world for current buffer */ |
| 48 |
*lastbuf = NULL, /* Last buffer we were in so we have a default |
| 49 |
buffer during a select buffer. */ |
| 50 |
*perr_buf = NULL; /* Buffer with error messages */ |
| 51 |
|
| 52 |
/* Toggle BIT in the current buffer's minor mode flags. If argument is |
| 53 |
* supplied, a positive one always turns on the mode and zero argument |
| 54 |
* always turns it off. |
| 55 |
*/ |
| 56 |
void |
| 57 |
TogMinor(int bit) |
| 58 |
{ |
| 59 |
if (is_an_arg()) { |
| 60 |
if (arg_value() == 0) |
| 61 |
curbuf->b_minor &= ~bit; |
| 62 |
else |
| 63 |
curbuf->b_minor |= bit; |
| 64 |
} else |
| 65 |
curbuf->b_minor ^= bit; |
| 66 |
UpdModLine = YES; |
| 67 |
} |
| 68 |
|
| 69 |
/* Creates a new buffer, links it at the end of the buffer chain, and |
| 70 |
* returns it. |
| 71 |
*/ |
| 72 |
private Buffer *free_bufs = NULL; |
| 73 |
|
| 74 |
private Buffer * |
| 75 |
buf_alloc(void) |
| 76 |
{ |
| 77 |
Buffer *b, |
| 78 |
*lastbp; |
| 79 |
|
| 80 |
lastbp = NULL; |
| 81 |
for (b = world; b != NULL; b = b->b_next) |
| 82 |
lastbp = b; |
| 83 |
|
| 84 |
if (free_bufs != NULL) { |
| 85 |
b = free_bufs; |
| 86 |
free_bufs = b->b_next; |
| 87 |
} else { |
| 88 |
b = (Buffer *) emalloc(sizeof (Buffer)); |
| 89 |
} |
| 90 |
if (lastbp) |
| 91 |
lastbp->b_next = b; |
| 92 |
else |
| 93 |
world = b; |
| 94 |
b->b_first = NULL; |
| 95 |
b->b_next = NULL; |
| 96 |
#ifdef MAC |
| 97 |
b->Type = BUFFER; /* kludge, but simplifies menu handlers */ |
| 98 |
b->Name = NULL; |
| 99 |
#endif |
| 100 |
return b; |
| 101 |
} |
| 102 |
|
| 103 |
/* Make a buffer and initialize it. */ |
| 104 |
|
| 105 |
private Buffer * |
| 106 |
mak_buf(void) |
| 107 |
{ |
| 108 |
Buffer *newb; |
| 109 |
int i; |
| 110 |
|
| 111 |
newb = buf_alloc(); |
| 112 |
newb->b_fname = NULL; |
| 113 |
newb->b_name = copystr(NoName); |
| 114 |
newb->b_marks = NULL; |
| 115 |
newb->b_themark = 0; /* Index into markring */ |
| 116 |
/* No marks yet */ |
| 117 |
for (i = 0; i < NMARKS; i++) |
| 118 |
newb->b_markring[i] = NULL; |
| 119 |
newb->b_modified = newb->b_diverged = NO; |
| 120 |
newb->b_type = B_FILE; /* File until proven SCRATCH */ |
| 121 |
newb->b_minor = 0; |
| 122 |
newb->b_major = TEXTMODE; |
| 123 |
newb->b_first = NULL; |
| 124 |
newb->b_map = NULL; |
| 125 |
#ifdef IPROCS |
| 126 |
newb->b_process = NULL; |
| 127 |
#endif |
| 128 |
buf_clear(newb); |
| 129 |
#ifdef MAC |
| 130 |
Bufchange = YES; |
| 131 |
#endif |
| 132 |
return newb; |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
ReNamBuf(void) |
| 137 |
{ |
| 138 |
setbname(curbuf, ask_buf((Buffer *)NULL, ALLOW_NEW)); |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
FindFile(void) |
| 143 |
{ |
| 144 |
char fnamebuf[FILESIZE]; |
| 145 |
|
| 146 |
(void) ask_file((char *)NULL, curbuf->b_fname, fnamebuf); |
| 147 |
SetABuf(curbuf); |
| 148 |
SetBuf(do_find(curwind, fnamebuf, YES, NO)); |
| 149 |
if (curbuf->b_diverged) |
| 150 |
rbell(); /* slight hint of divergence */ |
| 151 |
} |
| 152 |
|
| 153 |
private void |
| 154 |
mkbuflist(const char **bnamp, const char **ebnamp) |
| 155 |
{ |
| 156 |
Buffer *b; |
| 157 |
|
| 158 |
for (b = world; b != NULL; b = b->b_next) { |
| 159 |
if (b->b_name != NULL) { |
| 160 |
*bnamp++ = b->b_name; |
| 161 |
if (bnamp >= ebnamp) { |
| 162 |
complain("too many buffers to list"); |
| 163 |
/* NOTREACHED */ |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
*bnamp = NULL; |
| 168 |
} |
| 169 |
|
| 170 |
const char * |
| 171 |
ask_buf(Buffer *def, int flags) |
| 172 |
{ |
| 173 |
const char *defname = def != NULL? def->b_name : (char *)NULL; |
| 174 |
const char *bnames[200]; |
| 175 |
const char *bname; |
| 176 |
int offset; |
| 177 |
char prompt[100]; |
| 178 |
|
| 179 |
/* The test for % in the next definition is a kludge to prevent |
| 180 |
* the default buffer name in the prompt string from provoking |
| 181 |
* unintended formatting. Ugh! The name will still be the default. |
| 182 |
*/ |
| 183 |
if (defname != NULL && strchr(defname, '%') == NULL) |
| 184 |
swritef(prompt, sizeof(prompt), ": %%f (default %s) ", defname); |
| 185 |
else |
| 186 |
jamstr(prompt, ProcFmt); |
| 187 |
mkbuflist(bnames, &bnames[elemsof(bnames)]); |
| 188 |
/* Secret bonus: if there is no default, ^R will insert curbuf's name. */ |
| 189 |
offset = complete(bnames, defname==NULL? curbuf->b_name : defname, prompt, |
| 190 |
flags | (defname == NULL? 0 : ALLOW_EMPTY)); |
| 191 |
if (offset < 0) |
| 192 |
bname = *Minibuf == '\0' && defname != NULL? defname : Minibuf; |
| 193 |
else |
| 194 |
bname = bnames[offset]; |
| 195 |
return bname; |
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
BufSelect(void) |
| 200 |
{ |
| 201 |
const char *bname; |
| 202 |
|
| 203 |
bname = ask_buf(lastbuf, ALLOW_OLD | ALLOW_INDEX | ALLOW_NEW); |
| 204 |
SetABuf(curbuf); |
| 205 |
SetBuf(do_select(curwind, bname)); |
| 206 |
} |
| 207 |
|
| 208 |
private void |
| 209 |
BufNSelect(int n) |
| 210 |
{ |
| 211 |
Buffer *b; |
| 212 |
|
| 213 |
for (b = world; b != NULL; b = b->b_next) { |
| 214 |
if (b->b_name != NULL) { |
| 215 |
n -= 1; |
| 216 |
if (n == 0) { |
| 217 |
SetABuf(curbuf); |
| 218 |
SetBuf(do_select(curwind, b->b_name)); |
| 219 |
return; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
complain("[No such buffer]"); |
| 224 |
/* NOTREACHED */ |
| 225 |
} |
| 226 |
|
| 227 |
void Buf1Select(void) { BufNSelect(1); } |
| 228 |
void Buf2Select(void) { BufNSelect(2); } |
| 229 |
void Buf3Select(void) { BufNSelect(3); } |
| 230 |
void Buf4Select(void) { BufNSelect(4); } |
| 231 |
void Buf5Select(void) { BufNSelect(5); } |
| 232 |
void Buf6Select(void) { BufNSelect(6); } |
| 233 |
void Buf7Select(void) { BufNSelect(7); } |
| 234 |
void Buf8Select(void) { BufNSelect(8); } |
| 235 |
void Buf9Select(void) { BufNSelect(9); } |
| 236 |
void Buf10Select(void) { BufNSelect(10); } |
| 237 |
|
| 238 |
private void |
| 239 |
delb_wind(Buffer *b) |
| 240 |
{ |
| 241 |
Window *w = fwind; |
| 242 |
const char |
| 243 |
*alt = lastbuf != NULL && lastbuf != b |
| 244 |
? lastbuf->b_name : b->b_next != NULL |
| 245 |
? b->b_next->b_name : Mainbuf; |
| 246 |
|
| 247 |
do { |
| 248 |
Window *next = w->w_next; |
| 249 |
|
| 250 |
if (w->w_bufp == b) { |
| 251 |
if (one_windp() || alt != Mainbuf) |
| 252 |
(void) do_select(w, alt); |
| 253 |
else |
| 254 |
del_wind(w); |
| 255 |
} |
| 256 |
w = next; |
| 257 |
} while (w != fwind || w->w_bufp == b); |
| 258 |
} |
| 259 |
|
| 260 |
private Buffer * |
| 261 |
getNMbuf(void) |
| 262 |
{ |
| 263 |
Buffer *delbuf = buf_exists(ask_buf(curbuf, |
| 264 |
ALLOW_OLD | ALLOW_INDEX)); |
| 265 |
|
| 266 |
if (delbuf->b_modified) |
| 267 |
confirm("%s modified, are you sure? ", delbuf->b_name); |
| 268 |
return delbuf; |
| 269 |
} |
| 270 |
|
| 271 |
void |
| 272 |
BufErase(void) |
| 273 |
{ |
| 274 |
Buffer *delbuf = getNMbuf(); |
| 275 |
|
| 276 |
buf_clear(delbuf); |
| 277 |
} |
| 278 |
|
| 279 |
/* Free a buffer structure. |
| 280 |
* The actual struct is preserved to reduce the damage |
| 281 |
* from dangling references to it. They seem to be pervasive. |
| 282 |
* We try to reset enough that a dangling reference will be useless. |
| 283 |
*/ |
| 284 |
|
| 285 |
private void |
| 286 |
kill_buf(Buffer *delbuf) |
| 287 |
{ |
| 288 |
#ifdef IPROCS |
| 289 |
untieDeadProcess(delbuf); /* check for lingering processes */ |
| 290 |
#endif |
| 291 |
|
| 292 |
/* Clean up windows associated with this buffer |
| 293 |
* before it becomes invalid. |
| 294 |
*/ |
| 295 |
delb_wind(delbuf); |
| 296 |
|
| 297 |
/* Resetting curbuf must be done after delb_wind since it can |
| 298 |
* have a side-effect of setting curbuf to delbuf. For the same |
| 299 |
* reason, delbuf must not be unlinked until after delb_wind. |
| 300 |
*/ |
| 301 |
if (curbuf == delbuf) |
| 302 |
curbuf = NULL; |
| 303 |
if (lastbuf == delbuf) |
| 304 |
lastbuf = curbuf; /* even if NULL */ |
| 305 |
if (curbuf == NULL) |
| 306 |
SetBuf(curwind->w_bufp); |
| 307 |
|
| 308 |
/* unlink the buffer */ |
| 309 |
if (world == delbuf) { |
| 310 |
world = delbuf->b_next; |
| 311 |
} else { |
| 312 |
Buffer *b; |
| 313 |
|
| 314 |
for (b = world; b->b_next != delbuf; b = b->b_next) |
| 315 |
; |
| 316 |
b->b_next = delbuf->b_next; |
| 317 |
} |
| 318 |
|
| 319 |
if (perr_buf == delbuf) { |
| 320 |
ErrFree(); |
| 321 |
perr_buf = NULL; |
| 322 |
} |
| 323 |
|
| 324 |
lfreelist(delbuf->b_first); |
| 325 |
delbuf->b_first = delbuf->b_dot = delbuf->b_last = NULL; |
| 326 |
if (delbuf->b_name != NULL) { |
| 327 |
free(delbuf->b_name); |
| 328 |
delbuf->b_name = NULL; |
| 329 |
} |
| 330 |
if (delbuf->b_fname != NULL) { |
| 331 |
free(delbuf->b_fname); |
| 332 |
delbuf->b_fname = NULL; |
| 333 |
} |
| 334 |
flush_marks(delbuf); |
| 335 |
delbuf->b_marks = NULL; |
| 336 |
DelObjRef((data_obj *)delbuf->b_map); |
| 337 |
delbuf->b_map = NULL; |
| 338 |
|
| 339 |
delbuf->b_next = free_bufs; |
| 340 |
free_bufs = delbuf; |
| 341 |
#ifdef MAC |
| 342 |
Bufchange = YES; |
| 343 |
delbuf->Name = NULL; /* ??? this cannot legitimately matter */ |
| 344 |
#endif |
| 345 |
} |
| 346 |
|
| 347 |
/* offer to kill some buffers */ |
| 348 |
|
| 349 |
void |
| 350 |
KillSome(void) |
| 351 |
{ |
| 352 |
Buffer *b, |
| 353 |
*next; |
| 354 |
|
| 355 |
for (b = world; b != NULL; b = next) { |
| 356 |
next = b->b_next; |
| 357 |
if (!yes_or_no_p(IsModified(b)? "Kill %s [modified]? " : "Kill %s? ", b->b_name)) |
| 358 |
continue; |
| 359 |
|
| 360 |
if (IsModified(b) |
| 361 |
&& yes_or_no_p("%s modified; should I save it? ", b->b_name)) |
| 362 |
{ |
| 363 |
Buffer *oldb = curbuf; |
| 364 |
|
| 365 |
SetBuf(b); |
| 366 |
SaveFile(); |
| 367 |
SetBuf(oldb); |
| 368 |
} |
| 369 |
kill_buf(b); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
void |
| 374 |
BufKill(void) |
| 375 |
{ |
| 376 |
kill_buf(getNMbuf()); |
| 377 |
} |
| 378 |
|
| 379 |
private const char *const TypeNames[] = { |
| 380 |
NULL, |
| 381 |
"Scratch", |
| 382 |
"File", |
| 383 |
"Process", |
| 384 |
}; |
| 385 |
|
| 386 |
void |
| 387 |
BufList(void) |
| 388 |
{ |
| 389 |
const char |
| 390 |
fmt[] = "%2s %5s %-8s %-1s%-1s %-*s %-s"; |
| 391 |
Buffer *b; |
| 392 |
int bcount = 1, /* To give each buffer a number */ |
| 393 |
buf_width = 11; |
| 394 |
jbool |
| 395 |
any_needsaving = NO, |
| 396 |
any_tempmodified = NO, |
| 397 |
any_ntbf = NO, |
| 398 |
any_diverged = NO; |
| 399 |
|
| 400 |
for (b = world; b != NULL; b = b->b_next) { |
| 401 |
buf_width = jmax(buf_width, (int)strlen(b->b_name)); |
| 402 |
if (b->b_type == B_FILE) |
| 403 |
any_needsaving |= IsModified(b); |
| 404 |
else |
| 405 |
any_tempmodified |= IsModified(b); |
| 406 |
any_ntbf |= b->b_ntbf; |
| 407 |
any_diverged |= b->b_diverged; |
| 408 |
} |
| 409 |
|
| 410 |
TOstart("Buffer list"); |
| 411 |
|
| 412 |
if (any_needsaving) |
| 413 |
Typeout("(* means buffer needs saving)"); |
| 414 |
if (any_tempmodified) |
| 415 |
Typeout("(+ means temporary buffer has been modified)"); |
| 416 |
if (any_ntbf) |
| 417 |
Typeout("(- means file hasn't been read yet)"); |
| 418 |
if (any_diverged) |
| 419 |
Typeout("(# means file has been changed since buffer was read or written)"); |
| 420 |
if (any_needsaving | any_tempmodified | any_ntbf | any_diverged) |
| 421 |
Typeout(NullStr); |
| 422 |
Typeout(fmt, "NO", "Lines", "Type", NullStr, NullStr, buf_width, "Name", "File"); |
| 423 |
Typeout(fmt, "--", "-----", "----", NullStr, NullStr, buf_width, "----", "----"); |
| 424 |
for (b = world; b != NULL; b = b->b_next) { |
| 425 |
char |
| 426 |
bnostr[10], |
| 427 |
linestr[10]; |
| 428 |
|
| 429 |
swritef(bnostr, sizeof(bnostr), "%d", bcount++); |
| 430 |
if (b->b_ntbf) |
| 431 |
strcpy(linestr, "?"); |
| 432 |
else |
| 433 |
swritef(linestr, sizeof(linestr), "%d", |
| 434 |
LinesTo(b->b_first, (LinePtr)NULL)); |
| 435 |
Typeout(fmt, bnostr, linestr, TypeNames[b->b_type], |
| 436 |
b->b_diverged ? "#" : NullStr, |
| 437 |
IsModified(b) |
| 438 |
? (b->b_type==B_FILE? "*" : "+") |
| 439 |
: b->b_ntbf ? "-" : NullStr, |
| 440 |
buf_width, /* For the * (variable length field) */ |
| 441 |
b->b_name, |
| 442 |
filename(b)); |
| 443 |
|
| 444 |
if (TOabort) |
| 445 |
break; |
| 446 |
} |
| 447 |
TOstop(); |
| 448 |
} |
| 449 |
|
| 450 |
private void |
| 451 |
bufname(Buffer *b) |
| 452 |
{ |
| 453 |
char tmp[100]; |
| 454 |
const char *cp; |
| 455 |
int try = 1; |
| 456 |
|
| 457 |
if (b->b_fname == NULL) { |
| 458 |
complain("[No file name]"); |
| 459 |
/* NOTREACHED */ |
| 460 |
} |
| 461 |
cp = jbasename(b->b_fname); |
| 462 |
jamstr(tmp, cp); |
| 463 |
while (buf_exists(tmp)) { |
| 464 |
swritef(tmp, sizeof(tmp), "%s.%d", cp, try); |
| 465 |
try += 1; |
| 466 |
} |
| 467 |
setbname(b, tmp); |
| 468 |
} |
| 469 |
|
| 470 |
void |
| 471 |
buf_clear(Buffer *b) |
| 472 |
{ |
| 473 |
lfreelist(b->b_first); |
| 474 |
b->b_first = b->b_dot = b->b_last = NULL; |
| 475 |
(void) listput(b, b->b_first); |
| 476 |
|
| 477 |
SavLine(b->b_dot, NullStr); |
| 478 |
b->b_char = 0; |
| 479 |
AllMarkReset(b, b->b_dot); |
| 480 |
if (b == curbuf) |
| 481 |
getDOT(); |
| 482 |
|
| 483 |
diverge(b, NO); |
| 484 |
if (b->b_modified) |
| 485 |
UpdModLine = YES; |
| 486 |
b->b_ntbf = b->b_modified = NO; |
| 487 |
|
| 488 |
#ifdef USE_INO |
| 489 |
b->b_dev = 0; |
| 490 |
b->b_ino = 0; |
| 491 |
#endif |
| 492 |
b->b_mtime = 0; |
| 493 |
} |
| 494 |
|
| 495 |
/* Returns pointer to buffer with name NAME, or if NAME is a string of digits |
| 496 |
* returns the buffer whose number equals those digits. Otherwise, returns |
| 497 |
* NULL. |
| 498 |
*/ |
| 499 |
Buffer * |
| 500 |
buf_exists(const char *name) |
| 501 |
{ |
| 502 |
Buffer *bp; |
| 503 |
|
| 504 |
if (name == NULL) |
| 505 |
return NULL; |
| 506 |
|
| 507 |
for (bp = world; bp != NULL; bp = bp->b_next) |
| 508 |
if (strcmp(bp->b_name, name) == 0) |
| 509 |
return bp; |
| 510 |
|
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
/* Returns buffer pointer with a file name NAME, if one is found. |
| 515 |
* If target is NULL, all buffers are searched; otherwise, only |
| 516 |
* target is examined. Match is by inode (to catch links) or |
| 517 |
* canonical name. |
| 518 |
* |
| 519 |
* Divergence is diligently searched for. In particular, all |
| 520 |
* buffers are examined to see if they match the specified file, |
| 521 |
* in name or inode. If they do, divergence is checked (i.e. loss |
| 522 |
* of underlying file, change in inode number, or modification |
| 523 |
* time different from last visit). |
| 524 |
* |
| 525 |
* DS_REUSE can be used to avoid redundant stat calls. Use it |
| 526 |
* with care! |
| 527 |
*/ |
| 528 |
|
| 529 |
jbool |
| 530 |
was_dir, /* do_stat found a directory */ |
| 531 |
was_file; /* do_stat found a (plain) file */ |
| 532 |
|
| 533 |
Buffer * |
| 534 |
do_stat(const char *name, Buffer *target, int flags) |
| 535 |
{ |
| 536 |
Buffer *b = NULL, |
| 537 |
*result = NULL; |
| 538 |
char fnamebuf[FILESIZE]; |
| 539 |
static struct stat |
| 540 |
stbuf; |
| 541 |
|
| 542 |
PathParse(name, fnamebuf); |
| 543 |
|
| 544 |
if ((flags & DS_REUSE) == 0) { |
| 545 |
if (stat(fnamebuf, &stbuf) < 0) { |
| 546 |
stbuf.st_mode = S_IFREG; |
| 547 |
#ifdef USE_INO |
| 548 |
stbuf.st_ino = 0; /* impossible number */ |
| 549 |
stbuf.st_dev = 0; |
| 550 |
#endif |
| 551 |
stbuf.st_mtime = 0; |
| 552 |
} |
| 553 |
was_dir = (stbuf.st_mode & S_IFMT) == S_IFDIR; |
| 554 |
was_file = stbuf.st_ino != 0 && (stbuf.st_mode & S_IFMT) == S_IFREG; |
| 555 |
} |
| 556 |
if ((flags & DS_DIR) == 0 && was_dir) { |
| 557 |
complain("[%s is a directory]", fnamebuf); |
| 558 |
/* NOTREACHED */ |
| 559 |
} |
| 560 |
if (flags & DS_SET) { |
| 561 |
if ((stbuf.st_mode & S_IFMT) == S_IFREG) { |
| 562 |
#ifdef USE_INO |
| 563 |
target->b_dev = stbuf.st_dev; |
| 564 |
target->b_ino = stbuf.st_ino; |
| 565 |
#endif |
| 566 |
target->b_mtime = stbuf.st_mtime; |
| 567 |
} else { |
| 568 |
#ifdef USE_INO |
| 569 |
target->b_dev = 0; |
| 570 |
target->b_ino = 0; |
| 571 |
#endif |
| 572 |
target->b_mtime = 0; |
| 573 |
} |
| 574 |
diverge(target, NO); |
| 575 |
} |
| 576 |
|
| 577 |
for (b = world; b != NULL; b = b->b_next) { |
| 578 |
#ifdef USE_INO |
| 579 |
if(b->b_ino != 0 && b->b_ino == stbuf.st_ino |
| 580 |
&& b->b_dev != 0 && b->b_dev == stbuf.st_dev) |
| 581 |
{ |
| 582 |
/* A buffer's inode got a match; check divergence: |
| 583 |
* - name is different & can't stat buffer's fname |
| 584 |
* - name is different & stat yields a different inode |
| 585 |
* - inode now refers to a non-file |
| 586 |
*/ |
| 587 |
struct stat stbchk; |
| 588 |
|
| 589 |
if ((strcmp(b->b_fname, fnamebuf) != 0 |
| 590 |
&& (stat(b->b_fname, &stbchk) == -1 |
| 591 |
|| b->b_ino != stbchk.st_ino || b->b_dev != stbchk.st_dev)) |
| 592 |
|| (stbuf.st_mode & S_IFMT) != S_IFREG) |
| 593 |
{ |
| 594 |
/* false match: buffer's file has lost its inode */ |
| 595 |
b->b_ino = 0; |
| 596 |
b->b_dev = 0; |
| 597 |
diverge(b, YES); |
| 598 |
continue; /* try again */ |
| 599 |
} |
| 600 |
/* true match -- check times */ |
| 601 |
if (b->b_mtime != stbuf.st_mtime) |
| 602 |
diverge(b, YES); |
| 603 |
if (target == NULL || target == b) |
| 604 |
result = b; |
| 605 |
} else if (b->b_fname != NULL && strcmp(b->b_fname, fnamebuf) == 0) { |
| 606 |
/* a name (but not inode) match */ |
| 607 |
if (b->b_ino != stbuf.st_ino |
| 608 |
|| (stbuf.st_mode & S_IFMT) != S_IFREG) |
| 609 |
{ |
| 610 |
/* one or the other has an inode */ |
| 611 |
b->b_ino = 0; |
| 612 |
b->b_dev = 0; |
| 613 |
diverge(b, YES); |
| 614 |
} |
| 615 |
if (target == NULL || target == b) |
| 616 |
result = b; |
| 617 |
} |
| 618 |
#else |
| 619 |
if (b->b_fname != NULL && strcmp(b->b_fname, fnamebuf) == 0) { |
| 620 |
/* a name match -- check times */ |
| 621 |
if (stbuf.st_mtime != b->b_mtime) |
| 622 |
diverge(b, YES); |
| 623 |
if (target == NULL || target == b) |
| 624 |
result = b; |
| 625 |
} |
| 626 |
#endif |
| 627 |
} |
| 628 |
return result; |
| 629 |
} |
| 630 |
|
| 631 |
private void |
| 632 |
setbname(Buffer *b, const char *name) |
| 633 |
{ |
| 634 |
UpdModLine = YES; /* Kludge ... but speeds things up considerably */ |
| 635 |
if (name != NULL) { |
| 636 |
b->b_name = strcpy(freealloc(b->b_name, strlen(name) + 1), |
| 637 |
name); |
| 638 |
} else { |
| 639 |
b->b_name = NULL; |
| 640 |
} |
| 641 |
#ifdef MAC |
| 642 |
Bufchange = YES; |
| 643 |
#endif |
| 644 |
} |
| 645 |
|
| 646 |
void |
| 647 |
setfname(Buffer *b, const char *name) |
| 648 |
{ |
| 649 |
char wholename[FILESIZE], |
| 650 |
oldname[FILESIZE], |
| 651 |
*oldptr = NULL; |
| 652 |
Buffer *save = curbuf; |
| 653 |
|
| 654 |
SetBuf(b); |
| 655 |
UpdModLine = YES; /* Kludge ... but speeds things up considerably */ |
| 656 |
if (b->b_fname != NULL) { |
| 657 |
oldptr = strcpy(oldname, b->b_fname); |
| 658 |
free(b->b_fname); |
| 659 |
b->b_fname = NULL; |
| 660 |
} |
| 661 |
if (name != NULL) { |
| 662 |
PathParse(name, wholename); |
| 663 |
curbuf->b_fname = copystr(wholename); |
| 664 |
} |
| 665 |
DoAutoExec(curbuf->b_fname, oldptr); |
| 666 |
|
| 667 |
/* until they're known, zero these */ |
| 668 |
#ifdef USE_INO |
| 669 |
curbuf->b_dev = 0; |
| 670 |
curbuf->b_ino = 0; |
| 671 |
#endif |
| 672 |
curbuf->b_mtime = 0; |
| 673 |
diverge(curbuf, NO); |
| 674 |
|
| 675 |
SetBuf(save); |
| 676 |
#ifdef MAC |
| 677 |
Bufchange = YES; |
| 678 |
#endif |
| 679 |
} |
| 680 |
|
| 681 |
/* Find the file `fname' into buf and put in in window `w' */ |
| 682 |
|
| 683 |
Buffer * |
| 684 |
do_find(Window *w, char *fname, jbool force, jbool do_macros) |
| 685 |
{ |
| 686 |
Buffer *b, |
| 687 |
*oldb = curbuf; |
| 688 |
|
| 689 |
b = do_stat(fname, (Buffer *)NULL, DS_NONE); |
| 690 |
if (b == NULL) { |
| 691 |
b = mak_buf(); |
| 692 |
setfname(b, fname); |
| 693 |
bufname(b); |
| 694 |
(void) do_stat(b->b_fname, b, DS_SET | DS_REUSE); |
| 695 |
|
| 696 |
b->b_ntbf = force; |
| 697 |
SetBuf(b); /* force => load the file */ |
| 698 |
if (w) |
| 699 |
tiewind(w, b); |
| 700 |
|
| 701 |
/* We now execute all pending macro text, on the |
| 702 |
* unwarranted presumption that it must have come |
| 703 |
* from an auto-execute-macro. If we didn't do this |
| 704 |
* here, it would get delayed until the current |
| 705 |
* command was finished, which may be too late in the |
| 706 |
* case of UNIX_cmdline, watch_input, find_tag, or |
| 707 |
* ErrParse. |
| 708 |
* |
| 709 |
* There are some SetBuf(b) calls within the dispatch, |
| 710 |
* but they do not cause reading of the file |
| 711 |
* because b->b_ntbf will be false at this point. |
| 712 |
* One consequence will be that the macro will be executed |
| 713 |
* on the unfilled buffer -- somewhat surprising! |
| 714 |
*/ |
| 715 |
if (do_macros) |
| 716 |
dispatch_macros(); |
| 717 |
|
| 718 |
/* ??? At this point, we make the rash assumption |
| 719 |
* that buffer oldb still exists, even though |
| 720 |
* the macro text could have deleted it. |
| 721 |
*/ |
| 722 |
b->b_ntbf = !force; |
| 723 |
/* |
| 724 |
* the file will be read when the user next mentions Buffer b |
| 725 |
*/ |
| 726 |
SetBuf(oldb); |
| 727 |
} else { |
| 728 |
if (force) { |
| 729 |
SetBuf(b); /* b->b_ntbf => load the file */ |
| 730 |
SetBuf(oldb); |
| 731 |
} |
| 732 |
if (w) |
| 733 |
tiewind(w, b); |
| 734 |
} |
| 735 |
return b; |
| 736 |
} |
| 737 |
|
| 738 |
/* set alternate buffer */ |
| 739 |
|
| 740 |
void |
| 741 |
SetABuf(Buffer *b) |
| 742 |
{ |
| 743 |
if (b != NULL) |
| 744 |
lastbuf = b; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
/* check to see if BP is a valid buffer pointer */ |
| 749 |
jbool |
| 750 |
valid_bp(Buffer *bp) |
| 751 |
{ |
| 752 |
Buffer *b; |
| 753 |
|
| 754 |
for (b = world; b != NULL; b = b->b_next) |
| 755 |
if (b == bp) |
| 756 |
return YES; |
| 757 |
return NO; |
| 758 |
} |
| 759 |
|
| 760 |
void |
| 761 |
SetBuf(Buffer *newbuf) |
| 762 |
{ |
| 763 |
if (newbuf == curbuf || newbuf == NULL) |
| 764 |
return; |
| 765 |
|
| 766 |
if (!valid_bp(newbuf)) { |
| 767 |
complain("Internal error: (0x%x) is not a valid buffer pointer!", newbuf); |
| 768 |
/* NOTREACHED */ |
| 769 |
} |
| 770 |
lsave(); |
| 771 |
curbuf = newbuf; |
| 772 |
getDOT(); |
| 773 |
/* do the read now ... */ |
| 774 |
if (curbuf->b_ntbf) |
| 775 |
read_file(curbuf->b_fname, NO); |
| 776 |
#ifdef MAC |
| 777 |
Modechange = YES; |
| 778 |
#endif |
| 779 |
} |
| 780 |
|
| 781 |
Buffer * |
| 782 |
do_select(Window *w, const char *name) |
| 783 |
{ |
| 784 |
Buffer *new; |
| 785 |
|
| 786 |
if ((new = buf_exists(name)) == NULL) { |
| 787 |
new = mak_buf(); |
| 788 |
setfname(new, (char *)NULL); |
| 789 |
setbname(new, name); |
| 790 |
} |
| 791 |
if (w != NULL) |
| 792 |
tiewind(w, new); |
| 793 |
return new; |
| 794 |
} |
| 795 |
|
| 796 |
void |
| 797 |
buf_init(void) |
| 798 |
{ |
| 799 |
SetBuf(do_select(curwind, Mainbuf)); |
| 800 |
} |
| 801 |
|
| 802 |
LinePtr |
| 803 |
lastline(LinePtr lp) |
| 804 |
{ |
| 805 |
LinePtr next; |
| 806 |
|
| 807 |
while ((next = lp->l_next) != NULL) |
| 808 |
lp = next; |
| 809 |
return lp; |
| 810 |
} |
| 811 |
|
| 812 |
LinePtr |
| 813 |
next_line(LinePtr line, long num) |
| 814 |
{ |
| 815 |
if (num < 0) |
| 816 |
return prev_line(line, -num); |
| 817 |
|
| 818 |
if (line != NULL) |
| 819 |
while (--num >= 0 && line->l_next != NULL) |
| 820 |
line = line->l_next; |
| 821 |
return line; |
| 822 |
} |
| 823 |
|
| 824 |
LinePtr |
| 825 |
prev_line(LinePtr line, long num) |
| 826 |
{ |
| 827 |
if (num < 0) |
| 828 |
return next_line(line, -num); |
| 829 |
|
| 830 |
if (line != NULL) |
| 831 |
while (--num >= 0 && line->l_prev != NULL) |
| 832 |
line = line->l_prev; |
| 833 |
return line; |
| 834 |
} |