1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * session.c: session related functions 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_SESSION) || defined(PROTO) 17 18 static int did_lcd; // whether ":lcd" was produced for a session 19 20 /* 21 * Write a file name to the session file. 22 * Takes care of the "slash" option in 'sessionoptions' and escapes special 23 * characters. 24 * Returns FAIL if writing fails or out of memory. 25 */ 26 static int 27 ses_put_fname(FILE *fd, char_u *name, unsigned *flagp) 28 { 29 char_u *sname; 30 char_u *p; 31 int retval = OK; 32 33 sname = home_replace_save(NULL, name); 34 if (sname == NULL) 35 return FAIL; 36 37 if (*flagp & SSOP_SLASH) 38 { 39 // change all backslashes to forward slashes 40 for (p = sname; *p != NUL; MB_PTR_ADV(p)) 41 if (*p == '\\') 42 *p = '/'; 43 } 44 45 // escape special characters 46 p = vim_strsave_fnameescape(sname, FALSE); 47 vim_free(sname); 48 if (p == NULL) 49 return FAIL; 50 51 // write the result 52 if (fputs((char *)p, fd) < 0) 53 retval = FAIL; 54 55 vim_free(p); 56 return retval; 57 } 58 59 /* 60 * Write a buffer name to the session file. 61 * Also ends the line, if "add_eol" is TRUE. 62 * Returns FAIL if writing fails. 63 */ 64 static int 65 ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol) 66 { 67 char_u *name; 68 69 // Use the short file name if the current directory is known at the time 70 // the session file will be sourced. 71 // Don't do this for ":mkview", we don't know the current directory. 72 // Don't do this after ":lcd", we don't keep track of what the current 73 // directory is. 74 if (buf->b_sfname != NULL 75 && flagp == &ssop_flags 76 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)) 77 #ifdef FEAT_AUTOCHDIR 78 && !p_acd 79 #endif 80 && !did_lcd) 81 name = buf->b_sfname; 82 else 83 name = buf->b_ffname; 84 if (ses_put_fname(fd, name, flagp) == FAIL 85 || (add_eol && put_eol(fd) == FAIL)) 86 return FAIL; 87 return OK; 88 } 89 90 /* 91 * Write an argument list to the session file. 92 * Returns FAIL if writing fails. 93 */ 94 static int 95 ses_arglist( 96 FILE *fd, 97 char *cmd, 98 garray_T *gap, 99 int fullname, // TRUE: use full path name 100 unsigned *flagp) 101 { 102 int i; 103 char_u *buf = NULL; 104 char_u *s; 105 106 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL) 107 return FAIL; 108 if (put_line(fd, "%argdel") == FAIL) 109 return FAIL; 110 for (i = 0; i < gap->ga_len; ++i) 111 { 112 // NULL file names are skipped (only happens when out of memory). 113 s = alist_name(&((aentry_T *)gap->ga_data)[i]); 114 if (s != NULL) 115 { 116 if (fullname) 117 { 118 buf = alloc(MAXPATHL); 119 if (buf != NULL) 120 { 121 (void)vim_FullName(s, buf, MAXPATHL, FALSE); 122 s = buf; 123 } 124 } 125 if (fputs("$argadd ", fd) < 0 126 || ses_put_fname(fd, s, flagp) == FAIL 127 || put_eol(fd) == FAIL) 128 { 129 vim_free(buf); 130 return FAIL; 131 } 132 vim_free(buf); 133 } 134 } 135 return OK; 136 } 137 138 /* 139 * Return non-zero if window "wp" is to be stored in the Session. 140 */ 141 static int 142 ses_do_win(win_T *wp) 143 { 144 #ifdef FEAT_TERMINAL 145 if (bt_terminal(wp->w_buffer)) 146 return !term_is_finished(wp->w_buffer) 147 && (ssop_flags & SSOP_TERMINAL) 148 && term_should_restore(wp->w_buffer); 149 #endif 150 if (wp->w_buffer->b_fname == NULL 151 #ifdef FEAT_QUICKFIX 152 // When 'buftype' is "nofile" can't restore the window contents. 153 || bt_nofilename(wp->w_buffer) 154 #endif 155 ) 156 return (ssop_flags & SSOP_BLANK); 157 if (bt_help(wp->w_buffer)) 158 return (ssop_flags & SSOP_HELP); 159 return TRUE; 160 } 161 162 /* 163 * Return TRUE if frame "fr" has a window somewhere that we want to save in 164 * the Session. 165 */ 166 static int 167 ses_do_frame(frame_T *fr) 168 { 169 frame_T *frc; 170 171 if (fr->fr_layout == FR_LEAF) 172 return ses_do_win(fr->fr_win); 173 FOR_ALL_FRAMES(frc, fr->fr_child) 174 if (ses_do_frame(frc)) 175 return TRUE; 176 return FALSE; 177 } 178 179 /* 180 * Skip frames that don't contain windows we want to save in the Session. 181 * Returns NULL when there none. 182 */ 183 static frame_T * 184 ses_skipframe(frame_T *fr) 185 { 186 frame_T *frc; 187 188 FOR_ALL_FRAMES(frc, fr) 189 if (ses_do_frame(frc)) 190 break; 191 return frc; 192 } 193 194 /* 195 * Write commands to "fd" to recursively create windows for frame "fr", 196 * horizontally and vertically split. 197 * After the commands the last window in the frame is the current window. 198 * Returns FAIL when writing the commands to "fd" fails. 199 */ 200 static int 201 ses_win_rec(FILE *fd, frame_T *fr) 202 { 203 frame_T *frc; 204 int count = 0; 205 206 if (fr->fr_layout != FR_LEAF) 207 { 208 // Find first frame that's not skipped and then create a window for 209 // each following one (first frame is already there). 210 frc = ses_skipframe(fr->fr_child); 211 if (frc != NULL) 212 while ((frc = ses_skipframe(frc->fr_next)) != NULL) 213 { 214 // Make window as big as possible so that we have lots of room 215 // to split. 216 if (put_line(fd, "wincmd _ | wincmd |") == FAIL 217 || put_line(fd, fr->fr_layout == FR_COL 218 ? "split" : "vsplit") == FAIL) 219 return FAIL; 220 ++count; 221 } 222 223 // Go back to the first window. 224 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL 225 ? "%dwincmd k" : "%dwincmd h", count) < 0 226 || put_eol(fd) == FAIL)) 227 return FAIL; 228 229 // Recursively create frames/windows in each window of this column or 230 // row. 231 frc = ses_skipframe(fr->fr_child); 232 while (frc != NULL) 233 { 234 ses_win_rec(fd, frc); 235 frc = ses_skipframe(frc->fr_next); 236 // Go to next window. 237 if (frc != NULL && put_line(fd, "wincmd w") == FAIL) 238 return FAIL; 239 } 240 } 241 return OK; 242 } 243 244 static int 245 ses_winsizes( 246 FILE *fd, 247 int restore_size, 248 win_T *tab_firstwin) 249 { 250 int n = 0; 251 win_T *wp; 252 253 if (restore_size && (ssop_flags & SSOP_WINSIZE)) 254 { 255 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 256 { 257 if (!ses_do_win(wp)) 258 continue; 259 ++n; 260 261 // restore height when not full height 262 if (wp->w_height + wp->w_status_height < topframe->fr_height 263 && (fprintf(fd, 264 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)", 265 n, (long)wp->w_height, Rows / 2, Rows) < 0 266 || put_eol(fd) == FAIL)) 267 return FAIL; 268 269 // restore width when not full width 270 if (wp->w_width < Columns && (fprintf(fd, 271 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)", 272 n, (long)wp->w_width, Columns / 2, Columns) < 0 273 || put_eol(fd) == FAIL)) 274 return FAIL; 275 } 276 } 277 else 278 { 279 // Just equalise window sizes 280 if (put_line(fd, "wincmd =") == FAIL) 281 return FAIL; 282 } 283 return OK; 284 } 285 286 static int 287 put_view_curpos(FILE *fd, win_T *wp, char *spaces) 288 { 289 int r; 290 291 if (wp->w_curswant == MAXCOL) 292 r = fprintf(fd, "%snormal! $", spaces); 293 else 294 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1); 295 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK; 296 } 297 298 /* 299 * Write commands to "fd" to restore the view of a window. 300 * Caller must make sure 'scrolloff' is zero. 301 */ 302 static int 303 put_view( 304 FILE *fd, 305 win_T *wp, 306 int add_edit, // add ":edit" command to view 307 unsigned *flagp, // vop_flags or ssop_flags 308 int current_arg_idx, // current argument index of the window, 309 // use -1 if unknown 310 hashtab_T *terminal_bufs UNUSED) // already encountered terminal buffers, 311 // can be NULL 312 { 313 win_T *save_curwin; 314 int f; 315 int do_cursor; 316 int did_next = FALSE; 317 318 // Always restore cursor position for ":mksession". For ":mkview" only 319 // when 'viewoptions' contains "cursor". 320 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR); 321 322 // Local argument list. 323 if (wp->w_alist == &global_alist) 324 { 325 if (put_line(fd, "argglobal") == FAIL) 326 return FAIL; 327 } 328 else 329 { 330 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga, 331 flagp == &vop_flags 332 || !(*flagp & SSOP_CURDIR) 333 || wp->w_localdir != NULL, flagp) == FAIL) 334 return FAIL; 335 } 336 337 // Only when part of a session: restore the argument index. Some 338 // arguments may have been deleted, check if the index is valid. 339 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp) 340 && flagp == &ssop_flags) 341 { 342 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0 343 || put_eol(fd) == FAIL) 344 return FAIL; 345 did_next = TRUE; 346 } 347 348 // Edit the file. Skip this when ":next" already did it. 349 if (add_edit && (!did_next || wp->w_arg_idx_invalid)) 350 { 351 # ifdef FEAT_TERMINAL 352 if (bt_terminal(wp->w_buffer)) 353 { 354 if (term_write_session(fd, wp, terminal_bufs) == FAIL) 355 return FAIL; 356 } 357 else 358 # endif 359 // Load the file. 360 if (wp->w_buffer->b_ffname != NULL 361 # ifdef FEAT_QUICKFIX 362 && !bt_nofilename(wp->w_buffer) 363 # endif 364 ) 365 { 366 // Editing a file in this buffer: use ":edit file". 367 // This may have side effects! (e.g., compressed or network file). 368 // 369 // Note, if a buffer for that file already exists, use :badd to 370 // edit that buffer, to not lose folding information (:edit resets 371 // folds in other buffers) 372 if (fputs("if bufexists(\"", fd) < 0 373 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 374 || fputs("\") | buffer ", fd) < 0 375 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 376 || fputs(" | else | edit ", fd) < 0 377 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 378 || fputs(" | endif", fd) < 0 379 || put_eol(fd) == FAIL) 380 return FAIL; 381 } 382 else 383 { 384 // No file in this buffer, just make it empty. 385 if (put_line(fd, "enew") == FAIL) 386 return FAIL; 387 #ifdef FEAT_QUICKFIX 388 if (wp->w_buffer->b_ffname != NULL) 389 { 390 // The buffer does have a name, but it's not a file name. 391 if (fputs("file ", fd) < 0 392 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL) 393 return FAIL; 394 } 395 #endif 396 do_cursor = FALSE; 397 } 398 } 399 400 if (wp->w_alt_fnum) 401 { 402 buf_T *alt = buflist_findnr(wp->w_alt_fnum); 403 404 // Set the alternate file if the buffer is listed. 405 if ((flagp == &ssop_flags) 406 && alt != NULL 407 && alt->b_fname != NULL 408 && *alt->b_fname != NUL 409 && alt->b_p_bl 410 && (fputs("balt ", fd) < 0 411 || ses_fname(fd, alt, flagp, TRUE) == FAIL)) 412 return FAIL; 413 } 414 415 // Local mappings and abbreviations. 416 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 417 && makemap(fd, wp->w_buffer) == FAIL) 418 return FAIL; 419 420 // Local options. Need to go to the window temporarily. 421 // Store only local values when using ":mkview" and when ":mksession" is 422 // used and 'sessionoptions' doesn't include "options". 423 // Some folding options are always stored when "folds" is included, 424 // otherwise the folds would not be restored correctly. 425 save_curwin = curwin; 426 curwin = wp; 427 curbuf = curwin->w_buffer; 428 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 429 f = makeset(fd, OPT_LOCAL, 430 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS)); 431 #ifdef FEAT_FOLDING 432 else if (*flagp & SSOP_FOLDS) 433 f = makefoldset(fd); 434 #endif 435 else 436 f = OK; 437 curwin = save_curwin; 438 curbuf = curwin->w_buffer; 439 if (f == FAIL) 440 return FAIL; 441 442 #ifdef FEAT_FOLDING 443 // Save Folds when 'buftype' is empty and for help files. 444 if ((*flagp & SSOP_FOLDS) 445 && wp->w_buffer->b_ffname != NULL 446 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer))) 447 { 448 if (put_folds(fd, wp) == FAIL) 449 return FAIL; 450 } 451 #endif 452 453 // Set the cursor after creating folds, since that moves the cursor. 454 if (do_cursor) 455 { 456 457 // Restore the cursor line in the file and relatively in the 458 // window. Don't use "G", it changes the jumplist. 459 if (wp->w_height <= 0) 460 { 461 if (fprintf(fd, "let s:l = %ld", (long)wp->w_cursor.lnum) < 0) 462 return FAIL; 463 } 464 else if (fprintf(fd, 465 "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)", 466 (long)wp->w_cursor.lnum, 467 (long)(wp->w_cursor.lnum - wp->w_topline), 468 (long)wp->w_height / 2, (long)wp->w_height) < 0) 469 return FAIL; 470 471 if (put_eol(fd) == FAIL 472 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL 473 || put_line(fd, "keepjumps exe s:l") == FAIL 474 || put_line(fd, "normal! zt") == FAIL 475 || fprintf(fd, "keepjumps %ld", (long)wp->w_cursor.lnum) < 0 476 || put_eol(fd) == FAIL) 477 return FAIL; 478 // Restore the cursor column and left offset when not wrapping. 479 if (wp->w_cursor.col == 0) 480 { 481 if (put_line(fd, "normal! 0") == FAIL) 482 return FAIL; 483 } 484 else 485 { 486 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0) 487 { 488 if (fprintf(fd, 489 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)", 490 (long)wp->w_virtcol + 1, 491 (long)(wp->w_virtcol - wp->w_leftcol), 492 (long)wp->w_width / 2, (long)wp->w_width) < 0 493 || put_eol(fd) == FAIL 494 || put_line(fd, "if s:c > 0") == FAIL 495 || fprintf(fd, 496 " exe 'normal! ' . s:c . '|zs' . %ld . '|'", 497 (long)wp->w_virtcol + 1) < 0 498 || put_eol(fd) == FAIL 499 || put_line(fd, "else") == FAIL 500 || put_view_curpos(fd, wp, " ") == FAIL 501 || put_line(fd, "endif") == FAIL) 502 return FAIL; 503 } 504 else if (put_view_curpos(fd, wp, "") == FAIL) 505 return FAIL; 506 } 507 } 508 509 // Local directory, if the current flag is not view options or the "curdir" 510 // option is included. 511 if (wp->w_localdir != NULL 512 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR))) 513 { 514 if (fputs("lcd ", fd) < 0 515 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL 516 || put_eol(fd) == FAIL) 517 return FAIL; 518 did_lcd = TRUE; 519 } 520 521 return OK; 522 } 523 524 #ifdef FEAT_EVAL 525 static int 526 store_session_globals(FILE *fd) 527 { 528 hashtab_T *gvht = get_globvar_ht(); 529 hashitem_T *hi; 530 dictitem_T *this_var; 531 int todo; 532 char_u *p, *t; 533 534 todo = (int)gvht->ht_used; 535 for (hi = gvht->ht_array; todo > 0; ++hi) 536 { 537 if (!HASHITEM_EMPTY(hi)) 538 { 539 --todo; 540 this_var = HI2DI(hi); 541 if ((this_var->di_tv.v_type == VAR_NUMBER 542 || this_var->di_tv.v_type == VAR_STRING) 543 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 544 { 545 // Escape special characters with a backslash. Turn a LF and 546 // CR into \n and \r. 547 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv), 548 (char_u *)"\\\"\n\r"); 549 if (p == NULL) // out of memory 550 break; 551 for (t = p; *t != NUL; ++t) 552 if (*t == '\n') 553 *t = 'n'; 554 else if (*t == '\r') 555 *t = 'r'; 556 if ((fprintf(fd, "let %s = %c%s%c", 557 this_var->di_key, 558 (this_var->di_tv.v_type == VAR_STRING) ? '"' 559 : ' ', 560 p, 561 (this_var->di_tv.v_type == VAR_STRING) ? '"' 562 : ' ') < 0) 563 || put_eol(fd) == FAIL) 564 { 565 vim_free(p); 566 return FAIL; 567 } 568 vim_free(p); 569 } 570 #ifdef FEAT_FLOAT 571 else if (this_var->di_tv.v_type == VAR_FLOAT 572 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 573 { 574 float_T f = this_var->di_tv.vval.v_float; 575 int sign = ' '; 576 577 if (f < 0) 578 { 579 f = -f; 580 sign = '-'; 581 } 582 if ((fprintf(fd, "let %s = %c%f", 583 this_var->di_key, sign, f) < 0) 584 || put_eol(fd) == FAIL) 585 return FAIL; 586 } 587 #endif 588 } 589 } 590 return OK; 591 } 592 #endif 593 594 /* 595 * Write openfile commands for the current buffers to an .exrc file. 596 * Return FAIL on error, OK otherwise. 597 */ 598 static int 599 makeopens( 600 FILE *fd, 601 char_u *dirnow) // Current directory name 602 { 603 buf_T *buf; 604 int only_save_windows = TRUE; 605 int nr; 606 int restore_size = TRUE; 607 win_T *wp; 608 char_u *sname; 609 win_T *edited_win = NULL; 610 int tabnr; 611 int restore_stal = FALSE; 612 win_T *tab_firstwin; 613 frame_T *tab_topframe; 614 int cur_arg_idx = 0; 615 int next_arg_idx = 0; 616 int ret = FAIL; 617 #ifdef FEAT_TERMINAL 618 hashtab_T terminal_bufs; 619 620 hash_init(&terminal_bufs); 621 #endif 622 623 if (ssop_flags & SSOP_BUFFERS) 624 only_save_windows = FALSE; // Save ALL buffers 625 626 // Begin by setting the this_session variable, and then other 627 // sessionable variables. 628 #ifdef FEAT_EVAL 629 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL) 630 goto fail; 631 if (ssop_flags & SSOP_GLOBALS) 632 if (store_session_globals(fd) == FAIL) 633 goto fail; 634 #endif 635 636 // Close all windows and tabs but one. 637 if (put_line(fd, "silent only") == FAIL) 638 goto fail; 639 if ((ssop_flags & SSOP_TABPAGES) 640 && put_line(fd, "silent tabonly") == FAIL) 641 goto fail; 642 643 // Now a :cd command to the session directory or the current directory 644 if (ssop_flags & SSOP_SESDIR) 645 { 646 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')") 647 == FAIL) 648 goto fail; 649 } 650 else if (ssop_flags & SSOP_CURDIR) 651 { 652 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow); 653 if (sname == NULL 654 || fputs("cd ", fd) < 0 655 || ses_put_fname(fd, sname, &ssop_flags) == FAIL 656 || put_eol(fd) == FAIL) 657 { 658 vim_free(sname); 659 goto fail; 660 } 661 vim_free(sname); 662 } 663 664 // If there is an empty, unnamed buffer we will wipe it out later. 665 // Remember the buffer number. 666 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL) 667 goto fail; 668 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL) 669 goto fail; 670 if (put_line(fd, "endif") == FAIL) 671 goto fail; 672 673 // Now save the current files, current buffer first. 674 if (put_line(fd, "set shortmess=aoO") == FAIL) 675 goto fail; 676 677 // the global argument list 678 if (ses_arglist(fd, "argglobal", &global_alist.al_ga, 679 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL) 680 goto fail; 681 682 if (ssop_flags & SSOP_RESIZE) 683 { 684 // Note: after the restore we still check it worked! 685 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0 686 || put_eol(fd) == FAIL) 687 goto fail; 688 } 689 690 #ifdef FEAT_GUI 691 if (gui.in_use && (ssop_flags & SSOP_WINPOS)) 692 { 693 int x, y; 694 695 if (gui_mch_get_winpos(&x, &y) == OK) 696 { 697 // Note: after the restore we still check it worked! 698 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL) 699 goto fail; 700 } 701 } 702 #endif 703 704 // When there are two or more tabpages and 'showtabline' is 1 the tabline 705 // will be displayed when creating the next tab. That resizes the windows 706 // in the first tab, which may cause problems. Set 'showtabline' to 2 707 // temporarily to avoid that. 708 if (p_stal == 1 && first_tabpage->tp_next != NULL) 709 { 710 if (put_line(fd, "set stal=2") == FAIL) 711 goto fail; 712 restore_stal = TRUE; 713 } 714 715 // May repeat putting Windows for each tab, when "tabpages" is in 716 // 'sessionoptions'. 717 // Don't use goto_tabpage(), it may change directory and trigger 718 // autocommands. 719 tab_firstwin = firstwin; // first window in tab page "tabnr" 720 tab_topframe = topframe; 721 if ((ssop_flags & SSOP_TABPAGES)) 722 { 723 tabpage_T *tp; 724 725 // Similar to ses_win_rec() below, populate the tab pages first so 726 // later local options won't be copied to the new tabs. 727 FOR_ALL_TABPAGES(tp) 728 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL) 729 goto fail; 730 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL) 731 goto fail; 732 } 733 for (tabnr = 1; ; ++tabnr) 734 { 735 tabpage_T *tp = NULL; 736 int need_tabnext = FALSE; 737 int cnr = 1; 738 739 if ((ssop_flags & SSOP_TABPAGES)) 740 { 741 tp = find_tabpage(tabnr); 742 743 if (tp == NULL) 744 break; // done all tab pages 745 if (tp == curtab) 746 { 747 tab_firstwin = firstwin; 748 tab_topframe = topframe; 749 } 750 else 751 { 752 tab_firstwin = tp->tp_firstwin; 753 tab_topframe = tp->tp_topframe; 754 } 755 if (tabnr > 1) 756 need_tabnext = TRUE; 757 } 758 759 // Before creating the window layout, try loading one file. If this 760 // is aborted we don't end up with a number of useless windows. 761 // This may have side effects! (e.g., compressed or network file). 762 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 763 { 764 if (ses_do_win(wp) 765 && wp->w_buffer->b_ffname != NULL 766 && !bt_help(wp->w_buffer) 767 #ifdef FEAT_QUICKFIX 768 && !bt_nofilename(wp->w_buffer) 769 #endif 770 ) 771 { 772 if (need_tabnext && put_line(fd, "tabnext") == FAIL) 773 goto fail; 774 need_tabnext = FALSE; 775 776 if (fputs("edit ", fd) < 0 777 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE) 778 == FAIL) 779 goto fail; 780 if (!wp->w_arg_idx_invalid) 781 edited_win = wp; 782 break; 783 } 784 } 785 786 // If no file got edited create an empty tab page. 787 if (need_tabnext && put_line(fd, "tabnext") == FAIL) 788 goto fail; 789 790 if (tab_topframe->fr_layout != FR_LEAF) 791 { 792 // Save current window layout. 793 if (put_line(fd, "let s:save_splitbelow = &splitbelow") == FAIL 794 || put_line(fd, "let s:save_splitright = &splitright") 795 == FAIL) 796 goto fail; 797 if (put_line(fd, "set splitbelow splitright") == FAIL) 798 goto fail; 799 if (ses_win_rec(fd, tab_topframe) == FAIL) 800 goto fail; 801 if (put_line(fd, "let &splitbelow = s:save_splitbelow") == FAIL 802 || put_line(fd, "let &splitright = s:save_splitright") 803 == FAIL) 804 goto fail; 805 } 806 807 // Check if window sizes can be restored (no windows omitted). 808 // Remember the window number of the current window after restoring. 809 nr = 0; 810 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp)) 811 { 812 if (ses_do_win(wp)) 813 ++nr; 814 else 815 restore_size = FALSE; 816 if (curwin == wp) 817 cnr = nr; 818 } 819 820 if (tab_firstwin->w_next != NULL) 821 { 822 // Go to the first window. 823 if (put_line(fd, "wincmd t") == FAIL) 824 goto fail; 825 826 // If more than one window, see if sizes can be restored. 827 // First set 'winheight' and 'winwidth' to 1 to avoid the windows 828 // being resized when moving between windows. 829 // Do this before restoring the view, so that the topline and the 830 // cursor can be set. This is done again below. 831 // winminheight and winminwidth need to be set to avoid an error if 832 // the user has set winheight or winwidth. 833 if (put_line(fd, "let s:save_winminheight = &winminheight") == FAIL 834 || put_line(fd, "let s:save_winminwidth = &winminwidth") 835 == FAIL) 836 goto fail; 837 if (put_line(fd, "set winminheight=0") == FAIL 838 || put_line(fd, "set winheight=1") == FAIL 839 || put_line(fd, "set winminwidth=0") == FAIL 840 || put_line(fd, "set winwidth=1") == FAIL) 841 goto fail; 842 } 843 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL) 844 goto fail; 845 846 // Restore the tab-local working directory if specified 847 // Do this before the windows, so that the window-local directory can 848 // override the tab-local directory. 849 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR) 850 { 851 if (fputs("tcd ", fd) < 0 852 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL 853 || put_eol(fd) == FAIL) 854 goto fail; 855 did_lcd = TRUE; 856 } 857 858 // Restore the view of the window (options, file, cursor, etc.). 859 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 860 { 861 if (!ses_do_win(wp)) 862 continue; 863 if (put_view(fd, wp, wp != edited_win, &ssop_flags, cur_arg_idx, 864 #ifdef FEAT_TERMINAL 865 &terminal_bufs 866 #else 867 NULL 868 #endif 869 ) == FAIL) 870 goto fail; 871 if (nr > 1 && put_line(fd, "wincmd w") == FAIL) 872 goto fail; 873 next_arg_idx = wp->w_arg_idx; 874 } 875 876 // The argument index in the first tab page is zero, need to set it in 877 // each window. For further tab pages it's the window where we do 878 // "tabedit". 879 cur_arg_idx = next_arg_idx; 880 881 // Restore cursor to the current window if it's not the first one. 882 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 883 || put_eol(fd) == FAIL)) 884 goto fail; 885 886 // Restore window sizes again after jumping around in windows, because 887 // the current window has a minimum size while others may not. 888 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL) 889 goto fail; 890 891 // Don't continue in another tab page when doing only the current one 892 // or when at the last tab page. 893 if (!(ssop_flags & SSOP_TABPAGES)) 894 break; 895 } 896 897 if (ssop_flags & SSOP_TABPAGES) 898 { 899 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0 900 || put_eol(fd) == FAIL) 901 goto fail; 902 } 903 if (restore_stal && put_line(fd, "set stal=1") == FAIL) 904 goto fail; 905 906 // Now put the remaining buffers into the buffer list. 907 // This is near the end, so that when 'hidden' is set we don't create extra 908 // buffers. If the buffer was already created with another command the 909 // ":badd" will have no effect. 910 FOR_ALL_BUFFERS(buf) 911 { 912 if (!(only_save_windows && buf->b_nwindows == 0) 913 && !(buf->b_help && !(ssop_flags & SSOP_HELP)) 914 #ifdef FEAT_TERMINAL 915 // Skip terminal buffers: finished ones are not useful, others 916 // will be resurrected and result in a new buffer. 917 && !bt_terminal(buf) 918 #endif 919 && buf->b_fname != NULL 920 && buf->b_p_bl) 921 { 922 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L 923 : buf->b_wininfo->wi_fpos.lnum) < 0 924 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL) 925 goto fail; 926 } 927 } 928 929 // Wipe out an empty unnamed buffer we started in. 930 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0") 931 == FAIL) 932 goto fail; 933 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL) 934 goto fail; 935 if (put_line(fd, "endif") == FAIL) 936 goto fail; 937 if (put_line(fd, "unlet! s:wipebuf") == FAIL) 938 goto fail; 939 940 // Re-apply 'winheight', 'winwidth' and 'shortmess'. 941 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s", 942 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL) 943 goto fail; 944 if (tab_firstwin->w_next != NULL) 945 { 946 // Restore 'winminheight' and 'winminwidth'. 947 if (put_line(fd, "let &winminheight = s:save_winminheight") == FAIL 948 || put_line(fd, "let &winminwidth = s:save_winminwidth") == FAIL) 949 goto fail; 950 } 951 952 // Lastly, execute the x.vim file if it exists. 953 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL 954 || put_line(fd, "if filereadable(s:sx)") == FAIL 955 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL 956 || put_line(fd, "endif") == FAIL) 957 goto fail; 958 959 ret = OK; 960 fail: 961 #ifdef FEAT_TERMINAL 962 hash_clear_all(&terminal_bufs, 0); 963 #endif 964 return ret; 965 } 966 967 /* 968 * Get the name of the view file for the current buffer. 969 */ 970 static char_u * 971 get_view_file(int c) 972 { 973 int len = 0; 974 char_u *p, *s; 975 char_u *retval; 976 char_u *sname; 977 978 if (curbuf->b_ffname == NULL) 979 { 980 emsg(_(e_noname)); 981 return NULL; 982 } 983 sname = home_replace_save(NULL, curbuf->b_ffname); 984 if (sname == NULL) 985 return NULL; 986 987 // We want a file name without separators, because we're not going to make 988 // a directory. 989 // "normal" path separator -> "=+" 990 // "=" -> "==" 991 // ":" path separator -> "=-" 992 for (p = sname; *p; ++p) 993 if (*p == '=' || vim_ispathsep(*p)) 994 ++len; 995 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9); 996 if (retval != NULL) 997 { 998 STRCPY(retval, p_vdir); 999 add_pathsep(retval); 1000 s = retval + STRLEN(retval); 1001 for (p = sname; *p; ++p) 1002 { 1003 if (*p == '=') 1004 { 1005 *s++ = '='; 1006 *s++ = '='; 1007 } 1008 else if (vim_ispathsep(*p)) 1009 { 1010 *s++ = '='; 1011 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS) 1012 if (*p == ':') 1013 *s++ = '-'; 1014 else 1015 #endif 1016 *s++ = '+'; 1017 } 1018 else 1019 *s++ = *p; 1020 } 1021 *s++ = '='; 1022 *s++ = c; 1023 STRCPY(s, ".vim"); 1024 } 1025 1026 vim_free(sname); 1027 return retval; 1028 } 1029 1030 /* 1031 * ":loadview [nr]" 1032 */ 1033 void 1034 ex_loadview(exarg_T *eap) 1035 { 1036 char_u *fname; 1037 1038 fname = get_view_file(*eap->arg); 1039 if (fname != NULL) 1040 { 1041 do_source(fname, FALSE, DOSO_NONE, NULL); 1042 vim_free(fname); 1043 } 1044 } 1045 1046 # if defined(FEAT_GUI_GNOME) \ 1047 || (defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)) \ 1048 || defined(PROTO) 1049 /* 1050 * Generate a script that can be used to restore the current editing session. 1051 * Save the value of v:this_session before running :mksession in order to make 1052 * automagic session save fully transparent. Return TRUE on success. 1053 */ 1054 int 1055 write_session_file(char_u *filename) 1056 { 1057 char_u *escaped_filename; 1058 char *mksession_cmdline; 1059 unsigned int save_ssop_flags; 1060 int failed; 1061 1062 // Build an ex command line to create a script that restores the current 1063 // session if executed. Escape the filename to avoid nasty surprises. 1064 escaped_filename = vim_strsave_escaped(filename, escape_chars); 1065 if (escaped_filename == NULL) 1066 return FALSE; 1067 mksession_cmdline = alloc(10 + (int)STRLEN(escaped_filename) + 1); 1068 if (mksession_cmdline == NULL) 1069 { 1070 vim_free(escaped_filename); 1071 return FALSE; 1072 } 1073 strcpy(mksession_cmdline, "mksession "); 1074 STRCAT(mksession_cmdline, escaped_filename); 1075 vim_free(escaped_filename); 1076 1077 // Use a reasonable hardcoded set of 'sessionoptions' flags to avoid 1078 // unpredictable effects when the session is saved automatically. Also, 1079 // we definitely need SSOP_GLOBALS to be able to restore v:this_session. 1080 // Don't use SSOP_BUFFERS to prevent the buffer list from becoming 1081 // enormously large if the GNOME session feature is used regularly. 1082 save_ssop_flags = ssop_flags; 1083 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS 1084 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES); 1085 1086 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session"); 1087 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL); 1088 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session"); 1089 do_unlet((char_u *)"Save_VV_this_session", TRUE); 1090 1091 ssop_flags = save_ssop_flags; 1092 vim_free(mksession_cmdline); 1093 1094 // Reopen the file and append a command to restore v:this_session, 1095 // as if this save never happened. This is to avoid conflicts with 1096 // the user's own sessions. FIXME: It's probably less hackish to add 1097 // a "stealth" flag to 'sessionoptions' -- gotta ask Bram. 1098 if (!failed) 1099 { 1100 FILE *fd; 1101 1102 fd = open_exfile(filename, TRUE, APPENDBIN); 1103 1104 failed = (fd == NULL 1105 || put_line(fd, "let v:this_session = Save_VV_this_session") 1106 == FAIL 1107 || put_line(fd, "unlet Save_VV_this_session") == FAIL); 1108 1109 if (fd != NULL && fclose(fd) != 0) 1110 failed = TRUE; 1111 1112 if (failed) 1113 mch_remove(filename); 1114 } 1115 1116 return !failed; 1117 } 1118 # endif 1119 1120 #endif // FEAT_SESSION 1121 1122 #if defined(FEAT_SESSION) && defined(USE_CRNL) 1123 # define MKSESSION_NL 1124 static int mksession_nl = FALSE; // use NL only in put_eol() 1125 #endif 1126 1127 /* 1128 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession". 1129 */ 1130 void 1131 ex_mkrc(exarg_T *eap) 1132 { 1133 FILE *fd; 1134 int failed = FALSE; 1135 char_u *fname; 1136 #ifdef FEAT_BROWSE 1137 char_u *browseFile = NULL; 1138 #endif 1139 #ifdef FEAT_SESSION 1140 int view_session = FALSE; 1141 int using_vdir = FALSE; // using 'viewdir'? 1142 char_u *viewFile = NULL; 1143 unsigned *flagp; 1144 #endif 1145 1146 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview) 1147 { 1148 #ifdef FEAT_SESSION 1149 view_session = TRUE; 1150 #else 1151 ex_ni(eap); 1152 return; 1153 #endif 1154 } 1155 1156 #ifdef FEAT_SESSION 1157 // Use the short file name until ":lcd" is used. We also don't use the 1158 // short file name when 'acd' is set, that is checked later. 1159 did_lcd = FALSE; 1160 1161 // ":mkview" or ":mkview 9": generate file name with 'viewdir' 1162 if (eap->cmdidx == CMD_mkview 1163 && (*eap->arg == NUL 1164 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL))) 1165 { 1166 eap->forceit = TRUE; 1167 fname = get_view_file(*eap->arg); 1168 if (fname == NULL) 1169 return; 1170 viewFile = fname; 1171 using_vdir = TRUE; 1172 } 1173 else 1174 #endif 1175 if (*eap->arg != NUL) 1176 fname = eap->arg; 1177 else if (eap->cmdidx == CMD_mkvimrc) 1178 fname = (char_u *)VIMRC_FILE; 1179 #ifdef FEAT_SESSION 1180 else if (eap->cmdidx == CMD_mksession) 1181 fname = (char_u *)SESSION_FILE; 1182 #endif 1183 else 1184 fname = (char_u *)EXRC_FILE; 1185 1186 #ifdef FEAT_BROWSE 1187 if (cmdmod.cmod_flags & CMOD_BROWSE) 1188 { 1189 browseFile = do_browse(BROWSE_SAVE, 1190 # ifdef FEAT_SESSION 1191 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") : 1192 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") : 1193 # endif 1194 (char_u *)_("Save Setup"), 1195 fname, (char_u *)"vim", NULL, 1196 (char_u *)_(BROWSE_FILTER_MACROS), NULL); 1197 if (browseFile == NULL) 1198 goto theend; 1199 fname = browseFile; 1200 eap->forceit = TRUE; // since dialog already asked 1201 } 1202 #endif 1203 1204 #if defined(FEAT_SESSION) && defined(vim_mkdir) 1205 // When using 'viewdir' may have to create the directory. 1206 if (using_vdir && !mch_isdir(p_vdir)) 1207 vim_mkdir_emsg(p_vdir, 0755); 1208 #endif 1209 1210 fd = open_exfile(fname, eap->forceit, WRITEBIN); 1211 if (fd != NULL) 1212 { 1213 #ifdef FEAT_SESSION 1214 if (eap->cmdidx == CMD_mkview) 1215 flagp = &vop_flags; 1216 else 1217 flagp = &ssop_flags; 1218 #endif 1219 1220 #ifdef MKSESSION_NL 1221 // "unix" in 'sessionoptions': use NL line separator 1222 if (view_session && (*flagp & SSOP_UNIX)) 1223 mksession_nl = TRUE; 1224 #endif 1225 1226 // Write the version command for :mkvimrc 1227 if (eap->cmdidx == CMD_mkvimrc) 1228 (void)put_line(fd, "version 6.0"); 1229 1230 #ifdef FEAT_SESSION 1231 if (eap->cmdidx == CMD_mksession) 1232 { 1233 if (put_line(fd, "let SessionLoad = 1") == FAIL) 1234 failed = TRUE; 1235 } 1236 1237 if (eap->cmdidx != CMD_mkview) 1238 #endif 1239 { 1240 // Write setting 'compatible' first, because it has side effects. 1241 // For that same reason only do it when needed. 1242 if (p_cp) 1243 (void)put_line(fd, "if !&cp | set cp | endif"); 1244 else 1245 (void)put_line(fd, "if &cp | set nocp | endif"); 1246 } 1247 1248 #ifdef FEAT_SESSION 1249 if (!view_session 1250 || (eap->cmdidx == CMD_mksession 1251 && (*flagp & SSOP_OPTIONS))) 1252 #endif 1253 { 1254 int flags = OPT_GLOBAL; 1255 1256 #ifdef FEAT_SESSION 1257 if (eap->cmdidx == CMD_mksession && (*flagp & SSOP_SKIP_RTP)) 1258 flags |= OPT_SKIPRTP; 1259 #endif 1260 failed |= (makemap(fd, NULL) == FAIL 1261 || makeset(fd, flags, FALSE) == FAIL); 1262 } 1263 1264 #ifdef FEAT_SESSION 1265 if (!failed && view_session) 1266 { 1267 if (put_line(fd, "let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1") == FAIL) 1268 failed = TRUE; 1269 if (eap->cmdidx == CMD_mksession) 1270 { 1271 char_u *dirnow; // current directory 1272 1273 dirnow = alloc(MAXPATHL); 1274 if (dirnow == NULL) 1275 failed = TRUE; 1276 else 1277 { 1278 // Change to session file's dir. 1279 if (mch_dirname(dirnow, MAXPATHL) == FAIL 1280 || mch_chdir((char *)dirnow) != 0) 1281 *dirnow = NUL; 1282 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR)) 1283 { 1284 if (vim_chdirfile(fname, NULL) == OK) 1285 shorten_fnames(TRUE); 1286 } 1287 else if (*dirnow != NUL 1288 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL) 1289 { 1290 if (mch_chdir((char *)globaldir) == 0) 1291 shorten_fnames(TRUE); 1292 } 1293 1294 failed |= (makeopens(fd, dirnow) == FAIL); 1295 1296 // restore original dir 1297 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR) 1298 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL))) 1299 { 1300 if (mch_chdir((char *)dirnow) != 0) 1301 emsg(_(e_prev_dir)); 1302 shorten_fnames(TRUE); 1303 } 1304 vim_free(dirnow); 1305 } 1306 } 1307 else 1308 { 1309 failed |= (put_view(fd, curwin, !using_vdir, flagp, -1, NULL) 1310 == FAIL); 1311 } 1312 if (put_line(fd, "let &g:so = s:so_save | let &g:siso = s:siso_save") 1313 == FAIL) 1314 failed = TRUE; 1315 #ifdef FEAT_SEARCH_EXTRA 1316 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL) 1317 failed = TRUE; 1318 #endif 1319 if (put_line(fd, "doautoall SessionLoadPost") == FAIL) 1320 failed = TRUE; 1321 if (eap->cmdidx == CMD_mksession) 1322 { 1323 if (put_line(fd, "unlet SessionLoad") == FAIL) 1324 failed = TRUE; 1325 } 1326 } 1327 #endif 1328 if (put_line(fd, "\" vim: set ft=vim :") == FAIL) 1329 failed = TRUE; 1330 1331 failed |= fclose(fd); 1332 1333 if (failed) 1334 emsg(_(e_write)); 1335 #if defined(FEAT_SESSION) 1336 else if (eap->cmdidx == CMD_mksession) 1337 { 1338 // successful session write - set this_session var 1339 char_u *tbuf; 1340 1341 tbuf = alloc(MAXPATHL); 1342 if (tbuf != NULL) 1343 { 1344 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK) 1345 set_vim_var_string(VV_THIS_SESSION, tbuf, -1); 1346 vim_free(tbuf); 1347 } 1348 } 1349 #endif 1350 #ifdef MKSESSION_NL 1351 mksession_nl = FALSE; 1352 #endif 1353 } 1354 1355 #ifdef FEAT_BROWSE 1356 theend: 1357 vim_free(browseFile); 1358 #endif 1359 #ifdef FEAT_SESSION 1360 vim_free(viewFile); 1361 #endif 1362 } 1363 1364 #if (defined(FEAT_VIMINFO) || defined(FEAT_SESSION)) || defined(PROTO) 1365 var_flavour_T 1366 var_flavour(char_u *varname) 1367 { 1368 char_u *p = varname; 1369 1370 if (ASCII_ISUPPER(*p)) 1371 { 1372 while (*(++p)) 1373 if (ASCII_ISLOWER(*p)) 1374 return VAR_FLAVOUR_SESSION; 1375 return VAR_FLAVOUR_VIMINFO; 1376 } 1377 else 1378 return VAR_FLAVOUR_DEFAULT; 1379 } 1380 #endif 1381 1382 /* 1383 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession". 1384 * Return FAIL for a write error. 1385 */ 1386 int 1387 put_eol(FILE *fd) 1388 { 1389 if ( 1390 #ifdef USE_CRNL 1391 ( 1392 # ifdef MKSESSION_NL 1393 !mksession_nl && 1394 # endif 1395 (putc('\r', fd) < 0)) || 1396 #endif 1397 (putc('\n', fd) < 0)) 1398 return FAIL; 1399 return OK; 1400 } 1401 1402 /* 1403 * Write a line to "fd". 1404 * Return FAIL for a write error. 1405 */ 1406 int 1407 put_line(FILE *fd, char *s) 1408 { 1409 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL) 1410 return FAIL; 1411 return OK; 1412 } 1413