Annotation of src/usr.bin/vim/main.c, Revision 1.3
1.3 ! downsj 1: /* $OpenBSD: main.c,v 1.2 1996/09/21 06:23:05 downsj Exp $ */
1.1 downsj 2: /* vi:set ts=4 sw=4:
3: *
4: * VIM - Vi IMproved by Bram Moolenaar
5: *
6: * Do ":help uganda" in Vim to read copying and usage conditions.
7: * Do ":help credits" in Vim to see a list of people who contributed.
8: */
9:
10: #define EXTERN
11: #include "vim.h"
12: #include "globals.h"
13: #include "proto.h"
14: #include "option.h"
15:
16: #ifdef SPAWNO
17: # include <spawno.h> /* special MSDOS swapping library */
18: #endif
19:
20: static void usage __PARMS((int, char_u *));
21: static int stdout_notty = FALSE; /* is stdout not a terminal? */
22:
23: /*
24: * Types of usage message required. These must match the array of error
25: * messages in usage().
26: */
27: #define USAGE_UNKNOWN_OPTION 0
28: #define USAGE_TOO_MANY_ARGS 1
29: #define USAGE_ARG_MISSING 2
30: #define USAGE_GARBAGE 3
31:
32: static void
33: usage(n, str)
34: int n;
35: char_u *str;
36: {
37: register int i;
38: static char_u *(use[]) = {(char_u *)"[file ..]",
39: (char_u *)"-t tag",
40: (char_u *)"-e [errorfile]"};
41: static char_u *(errors[]) = {(char_u *)"Unknown option",
42: (char_u *)"Too many arguments",
43: (char_u *)"Argument missing after",
44: (char_u *)"Garbage after option",
45: };
46:
47: #if defined(UNIX) || defined(__EMX__)
48: reset_signals(); /* kill us with CTRL-C here, if you like */
49: #endif
50:
51: fprintf(stderr, longVersion);
52: fprintf(stderr, "\n");
53: fprintf(stderr, (char *)errors[n]);
54: if (str != NULL)
55: fprintf(stderr, ": \"%s\"", str);
56: fprintf(stderr, "\nusage:");
57: for (i = 0; ; ++i)
58: {
59: fprintf(stderr, " vim [options] ");
60: fprintf(stderr, (char *)use[i]);
61: if (i == (sizeof(use) / sizeof(char_u *)) - 1)
62: break;
63: fprintf(stderr, "\n or:");
64: }
65:
66: fprintf(stderr, "\n\nOptions:\n");
67: #ifdef USE_GUI
68: fprintf(stderr, " -g\t\t\tRun using GUI\n");
69: fprintf(stderr, " -f\t\t\tForeground: Don't fork when starting GUI\n");
70: #endif
71: fprintf(stderr, " -R or -v\t\tReadonly mode (view mode)\n");
72: fprintf(stderr, " -b\t\t\tBinary mode\n");
73: fprintf(stderr, " -l\t\t\tLisp mode\n");
74: fprintf(stderr, " -n\t\t\tNo swap file, use memory only\n");
75: fprintf(stderr, " -r\t\t\tList swap files\n");
76: fprintf(stderr, " -r (with file name)\tRecover crashed session\n");
77: fprintf(stderr, " -L\t\t\tSame as -r\n");
78: #ifdef AMIGA
79: fprintf(stderr, " -x\t\t\tDon't use newcli to open window\n");
80: fprintf(stderr, " -d <device>\t\tUse <device> for I/O\n");
81: #endif
82: #ifdef RIGHTLEFT
83: fprintf(stderr, " -H\t\t\tstart in Hebrew mode\n");
84: #endif
85: fprintf(stderr, " -T <terminal>\tSet terminal type to <terminal>\n");
86: fprintf(stderr, " -o[N]\t\tOpen N windows (default: one for each file)\n");
87: fprintf(stderr, " +\t\t\tStart at end of file\n");
88: fprintf(stderr, " +<lnum>\t\tStart at line <lnum>\n");
89: fprintf(stderr, " -c <command>\t\tExecute <command> first\n");
90: fprintf(stderr, " -s <scriptin>\tRead commands from script file <scriptin>\n");
91: fprintf(stderr, " -w <scriptout>\tAppend commands to script file <scriptout>\n");
92: fprintf(stderr, " -W <scriptout>\tWrite commands to script file <scriptout>\n");
93: fprintf(stderr, " -u <vimrc>\t\tUse <vimrc> instead of any .vimrc\n");
94: fprintf(stderr, " -i <viminfo>\t\tUse <viminfo> instead of .viminfo\n");
95: fprintf(stderr, " --\t\t\tEnd of options\n");
96:
97: #ifdef USE_GUI_X11
98: # ifdef USE_GUI_MOTIF
99: fprintf(stderr, "\nOptions recognised by gvim (Motif version):\n");
100: # else
101: # ifdef USE_GUI_ATHENA
102: fprintf(stderr, "\nOptions recognised by gvim (Athena version):\n");
103: # endif /* USE_GUI_ATHENA */
104: # endif /* USE_GUI_MOTIF */
105: fprintf(stderr, " -display <display>\tRun vim on <display>\n");
106: fprintf(stderr, " -iconic\t\tStart vim iconified\n");
107: # if 0
108: fprintf(stderr, " -name <name>\t\tUse resource as if vim was <name>\n");
109: fprintf(stderr, "\t\t\t (Unimplemented)\n");
110: # endif
111: fprintf(stderr, " -background <color>\tUse <color> for the background (also: -bg)\n");
112: fprintf(stderr, " -foreground <color>\tUse <color> for normal text (also: -fg)\n");
113: fprintf(stderr, " -bold <color>\tUse <color> for bold text\n");
114: fprintf(stderr, " -italic <color>\tUse <color> for italic text\n");
115: fprintf(stderr, " -underline <color>\tUse <color> for underlined text (also: -ul)\n");
116: fprintf(stderr, " -cursor <color>\tUse <color> for cursor\n");
117: fprintf(stderr, " -font <font>\t\tUse <font> for normal text (also: -fn)\n");
118: fprintf(stderr, " -boldfont <font>\tUse <font> for bold text\n");
119: fprintf(stderr, " -italicfont <font>\tUse <font> for italic text\n");
120: fprintf(stderr, " -geometry <geom>\tUse <geom> for initial geometry (also: -geom)\n");
121: fprintf(stderr, " -borderwidth <width>\tUse a border width of <width> (also: -bw)\n");
122: fprintf(stderr, " -scrollbarwidth <width>\tUse a scrollbar width of <width> (also: -sw)\n");
123: fprintf(stderr, " -menuheight <height>\tUse a menu bar height of <height> (also: -mh)\n");
124: fprintf(stderr, " -reverse\t\tUse reverse video (also: -rv)\n");
125: fprintf(stderr, " +reverse\t\tDon't use reverse video (also: +rv)\n");
126: fprintf(stderr, " -xrm <resource>\tSet the specified resource\n");
127: #endif /* USE_GUI_X11 */
128:
129: mch_windexit(1);
130: }
131:
132: #ifdef HAVE_LOCALE_H
133: # include <locale.h>
134: #endif
135:
136: void
137: main(argc, argv)
138: int argc;
139: char **argv;
140: {
141: char_u *initstr; /* init string from the environment */
142: char_u *term = NULL; /* specified terminal name */
143: char_u *fname = NULL; /* file name from command line */
144: char_u *command = NULL; /* command from + or -c option */
145: char_u *tagname = NULL; /* tag from -t option */
146: char_u *use_vimrc = NULL; /* vimrc from -u option */
147: int c;
148: int doqf = 0;
149: int i;
150: int bin_mode = FALSE; /* -b option used */
151: int vi_mode = FALSE; /* run as vi */
152: int window_count = 1; /* number of windows to use */
153: int arg_idx = 0; /* index for arg_files[] */
154: int check_version = FALSE; /* check .vimrc version number */
155: int argv_idx; /* index in argv[n][] */
1.3 ! downsj 156: int invoked_as_ex = FALSE; /* argv[0] is "ex" */
1.1 downsj 157:
158: #if defined(MSDOS) || defined(WIN32) || defined(OS2)
159: static struct initmap
160: {
161: char_u *arg;
162: int mode;
163: } initmappings[] =
164: {
165: /* normal and visual mode */
166: #ifdef MSDOS
167: {(char_u *)"\316w H", NORMAL+VISUAL}, /* CTRL-HOME is 'H' */
168: {(char_u *)"\316u L", NORMAL+VISUAL}, /* CTRL-END is 'L' */
169: {(char_u *)"\316\204 1G", NORMAL+VISUAL}, /* CTRL-PageUp is '1G' */
170: {(char_u *)"\316v G", NORMAL+VISUAL}, /* CTRL-PageDown is 'G' */
171: #else /* WIN32 */
172: /* Use the Windows (CUA) keybindings */
173: {(char_u *)"\316w 1G", NORMAL+VISUAL}, /* CTRL-HOME is '1G' */
174: {(char_u *)"\316u G$", NORMAL+VISUAL}, /* CTRL-END is 'G$' */
175: {(char_u *)"\316\204 H", NORMAL+VISUAL}, /* CTRL-PageUp is 'H' */
176: {(char_u *)"\316v L$", NORMAL+VISUAL}, /* CTRL-PageDown is 'L$' */
177: {(char_u *)"\316s B", NORMAL+VISUAL}, /* CTRL-Left is 'B' */
178: {(char_u *)"\316t W", NORMAL+VISUAL}, /* CTRL-Right is 'W' */
179: #endif /* WIN32 */
180:
181: /* insert mode */
182: #ifdef MSDOS
183: {(char_u *)"\316w \017H", INSERT}, /* CTRL-HOME is '^OH' */
184: {(char_u *)"\316u \017L", INSERT}, /* CTRL-END is '^OL' */
185: {(char_u *)"\316\204 \017\061G", INSERT}, /* CTRL-PageUp is '^O1G' */
186: {(char_u *)"\316v \017G", INSERT}, /* CTRL-PageDown is '^OG' */
187: #else /* WIN32 */
188: /* Use the Windows (CUA) keybindings */
189: {(char_u *)"\316w \017\061G", INSERT}, /* CTRL-HOME is '^O1G' */
190: {(char_u *)"\316u \017G\017$", INSERT}, /* CTRL-END is '^OG^O$' */
191: {(char_u *)"\316\204 \017H",INSERT}, /* CTRL-PageUp is '^OH'*/
192: {(char_u *)"\316v \017L\017$", INSERT}, /* CTRL-PageDown ='^OL^O$'*/
193: {(char_u *)"\316s \017B", INSERT}, /* CTRL-Left is '^OB' */
194: {(char_u *)"\316t \017W", INSERT}, /* CTRL-Right is '^OW' */
195: #endif /* WIN32 */
196: };
197: #endif
198:
199: #ifdef __EMX__
200: _wildcard(&argc, &argv);
201: #endif
202:
203: #ifdef HAVE_LOCALE_H
204: setlocale(LC_ALL, ""); /* for ctype() and the like */
205: #endif
206:
207: #ifdef USE_GUI
208: gui_prepare(&argc, argv); /* Prepare for possibly starting GUI sometime */
209: #endif
210:
211: /*
212: * Check if we have an interactive window.
213: * On the Amiga: If there is no window, we open one with a newcli command
214: * (needed for :! to * work). mch_check_win() will also handle the -d argument.
215: */
216: stdout_notty = (mch_check_win(argc, argv) == FAIL);
217:
218: /*
219: * allocate the first window and buffer. Can't do anything without it
220: */
221: if ((curwin = win_alloc(NULL)) == NULL ||
222: (curbuf = buflist_new(NULL, NULL, 1L, FALSE)) == NULL)
223: mch_windexit(0);
224: curwin->w_buffer = curbuf;
225: screen_start(); /* don't know where cursor is yet */
226:
227: /*
228: * Allocate space for the generic buffers (needed for set_init_1()).
229: */
230: if ((IObuff = alloc(IOSIZE)) == NULL ||
231: (NameBuff = alloc(MAXPATHL)) == NULL)
232: mch_windexit(0);
233:
234: /*
235: * Set the default values for the options.
236: * First find out the home directory, needed to expand "~" in options.
237: */
238: init_homedir(); /* find real value of $HOME */
239: set_init_1();
240:
241: /*
242: * If the executable is called "view" we start in readonly mode.
243: */
244: if (STRCMP(gettail((char_u *)argv[0]), (char_u *)"view") == 0)
245: {
246: readonlymode = TRUE;
247: curbuf->b_p_ro = TRUE;
248: if (p_uc) /* if we are doing any updating.. */
249: p_uc = 10000; /* ..don't update very often */
250: }
251:
252: /*
1.3 ! downsj 253: * If the executable is called "ex" we start in ex mode.
! 254: */
! 255:
! 256: if (STRCMP(gettail((char_u *)argv[0]), (char_u *)"ex") == 0)
! 257: {
! 258: invoked_as_ex = TRUE;
! 259: }
! 260:
! 261: /*
1.1 downsj 262: * If the executable is called "gvim" we run the GUI version.
263: */
264: if (STRCMP(gettail((char_u *)argv[0]), (char_u *)"gvim") == 0)
265: {
266: #ifdef USE_GUI
267: gui.starting = TRUE;
268: #else
269: fprintf(stderr, (char *)e_nogvim);
270: mch_windexit(2);
271: #endif
272: }
273:
274: /*
275: * If the executable is called "vi" we switch to compat mode.
276: */
277: if (STRCMP(gettail((char_u *)argv[0]), (char_u *)"vi") == 0)
278: {
279: vi_mode = TRUE;
280: }
281:
282: ++argv;
283: /*
284: * Process the command line arguments
285: * '+{command}' execute command
286: * '-b' binary
287: * '-c {command}' execute command
288: * '-d {device}' device (for Amiga)
289: * '-f' Don't fork when starging GUI. (if USE_GUI defined)
290: * '-g' Run with GUI. (if USE_GUI defined)
291: * '-H' Start in right-left mode
292: * '-i viminfo' use instead of p_viminfo
293: * '-n' no .vim file
294: * '-o[N]' open N windows (default: number of files)
295: * '-r' recovery mode
296: * '-L' recovery mode
297: * '-s scriptin' read from script file
298: * '-T terminal' terminal name
299: * '-u vimrc' read initializations from a file
300: * '-v' view or Readonly mode
301: * '-R' view or Readonly mode
302: * '-w scriptout' write to script file (append)
303: * '-W scriptout' write to script file (overwrite)
304: * '-x' open window directly, not with newcli
305: */
306: argv_idx = 1; /* active option letter is argv[0][argv_idx] */
307:
308: while (argc > 1 && ((c = argv[0][0]) == '+' || (c == '-' &&
309: vim_strchr((char_u *)"bcdfgHilLnorRsTuvwWx",
310: c = argv[0][argv_idx]) != NULL)))
311: {
312: ++argv_idx; /* advance to next option letter by default */
313: switch (c)
314: {
315: case '+': /* + or +{number} or +/{pat} or +{command} */
316: argv_idx = -1; /* skip to next argument */
317: if (argv[0][1] == NUL)
318: command = (char_u *)"$";
319: else
320: command = (char_u *)&(argv[0][1]);
321: break;
322:
323: case 'b':
324: bin_mode = TRUE; /* postpone to after reading .exrc files */
325: break;
326:
327: #ifdef USE_GUI
328: case 'f':
329: gui.dofork = FALSE; /* don't fork() when starting GUI */
330: break;
331: #endif
332:
333: case 'g':
334: #ifdef USE_GUI
335: gui.starting = TRUE; /* start GUI a bit later */
336: #else
337: fprintf(stderr, (char *)e_nogvim);
338: mch_windexit(2);
339: #endif
340: break;
341:
342: case 'H': /* start in Hebrew mode: rl + hkmap set */
343: #ifdef RIGHTLEFT
344: curwin->w_p_rl = p_hkmap = TRUE;
345: #else
346: fprintf(stderr, (char *)e_nohebrew);
347: mch_windexit(2);
348: #endif
349: break;
350:
351: case 'l': /* -l: lisp mode, 'lisp' and 'showmatch' on */
352: curbuf->b_p_lisp = TRUE;
353: p_sm = TRUE;
354: break;
355:
356: case 'n':
357: p_uc = 0;
358: break;
359:
360: case 'o':
361: window_count = 0; /* default: open window for each file */
362: if (isdigit(argv[0][argv_idx]))
363: {
364: window_count = atoi(&(argv[0][argv_idx]));
365: while (isdigit(argv[0][argv_idx]))
366: ++argv_idx;
367: }
368: break;
369:
370: case 'r':
371: case 'L':
372: recoverymode = 1;
373: break;
374:
375: case 'v':
376: case 'R':
377: readonlymode = TRUE;
378: curbuf->b_p_ro = TRUE;
379: if (p_uc) /* if we are doing any updating.. */
380: p_uc = 10000; /* ..don't update very often */
381: break;
382:
383: case 'x':
384: break; /* This is ignored as it is handled in mch_check_win() */
385:
386:
387: case 'w':
388: if (isdigit(argv[0][argv_idx])) /* -w{number}; set window height */
389: {
390: argv_idx = -1;
391: break; /* not implemented, ignored */
392: }
393: /* FALLTHROUGH */
394:
395: default: /* options with argument */
396: /*
397: * Check there's no garbage immediately after the option letter.
398: */
399: if (argv[0][argv_idx] != NUL)
400: usage(USAGE_GARBAGE, (char_u *)argv[0]);
401:
402: --argc;
403: if (argc < 2)
404: usage(USAGE_ARG_MISSING, (char_u *)argv[0]);
405: ++argv;
406: argv_idx = -1;
407:
408: switch (c)
409: {
410: case 'c': /* -c {command} */
411: command = (char_u *)argv[0];
412: break;
413:
414: /* case 'd': This is ignored as it is handled in mch_check_win() */
415:
416: case 'i': /* -i {viminfo} */
417: use_viminfo = (char_u *)argv[0];
418: break;
419:
420: case 's': /* -s {scriptin} */
421: if (scriptin[0] != NULL)
422: {
423: fprintf(stderr,
424: "Attempt to open script file again: \"%s %s\"\n",
425: argv[-1], argv[0]);
426: mch_windexit(2);
427: }
428: if ((scriptin[0] = fopen(argv[0], READBIN)) == NULL)
429: {
430: fprintf(stderr, "Cannot open \"%s\" for reading\n", argv[0]);
431: mch_windexit(2);
432: }
433: break;
434:
435: /*
436: * The -T term option is always available and when HAVE_TERMLIB is supported
437: * it overrides the environment variable TERM.
438: */
439: case 'T': /* -T {terminal} */
440: term = (char_u *)argv[0];
441: break;
442:
443: case 'u': /* -u {vimrc} */
444: use_vimrc = (char_u *)argv[0];
445: break;
446:
447: case 'w': /* -w {scriptout} (append) */
448: case 'W': /* -W {scriptout} (overwrite) */
449: if (scriptout != NULL)
450: {
451: fprintf(stderr,
452: "Attempt to open script file again: \"%s %s\"\n",
453: argv[-1], argv[0]);
454: mch_windexit(2);
455: }
456: if ((scriptout = fopen(argv[0],
457: c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
458: {
459: fprintf(stderr, "cannot open \"%s\" for output\n", argv[0]);
460: mch_windexit(2);
461: }
462: break;
463: }
464: }
465: /*
466: * If there are no more letters after the current "-", go to next
467: * argument. argv_idx is set to -1 when the current argument is to be
468: * skipped.
469: */
470: if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
471: {
472: --argc;
473: ++argv;
474: argv_idx = 1;
475: }
476: }
477:
478: /* note that we may use mch_windexit() before mch_windinit()! */
479: mch_windinit(); /* inits Rows and Columns */
480: /*
481: * Set the default values for the options that use Rows and Columns.
482: */
483: set_init_2();
484:
485: firstwin->w_height = Rows - 1;
486: cmdline_row = Rows - 1;
487:
488: /*
489: * Process the other command line arguments.
490: * -e[errorfile] quickfix mode
491: * -t[tagname] jump to tag
492: * [--] [file ..] file names
493: */
494: if (argc > 1)
495: {
496: if (argv[0][0] == '-' && (argv[0][1] != '-' || argv[0][2] != NUL))
497: {
498: switch (argv[0][1])
499: {
500: case 'e': /* -e QuickFix mode */
501: switch (argc)
502: {
503: case 2:
504: if (argv[0][2]) /* -eerrorfile */
505: p_ef = (char_u *)argv[0] + 2;
506: break; /* -e */
507:
508: case 3: /* -e errorfile */
509: if (argv[0][2] != NUL)
510: usage(USAGE_GARBAGE, (char_u *)argv[0]);
511: ++argv;
512: p_ef = (char_u *)argv[0];
513: break;
514:
515: default: /* argc > 3: too many arguments */
516: usage(USAGE_TOO_MANY_ARGS, NULL);
517: }
518: doqf = 1;
519: break;
520:
521: case 't': /* -t tag or -ttag */
522: switch (argc)
523: {
524: case 2:
525: if (argv[0][2]) /* -ttag */
526: {
527: tagname = (char_u *)argv[0] + 2;
528: break;
529: }
530: usage(USAGE_ARG_MISSING, (char_u *)argv[0]);
531: break;
532:
533: case 3: /* -t tag */
534: if (argv[0][2] != NUL) /* also -ttag?! */
535: usage(USAGE_GARBAGE, (char_u *)argv[0]);
536: ++argv;
537: tagname = (char_u *)argv[0];
538: break;
539:
540: default: /* argc > 3: too many arguments */
541: usage(USAGE_TOO_MANY_ARGS, NULL);
542: }
543: break;
544:
545: default:
546: usage(USAGE_UNKNOWN_OPTION, (char_u *)argv[0]);
547: }
548: }
549: else /* must be a file name */
550: {
551: /*
552: * Skip a single "--" argument, used in front of a file name that
553: * starts with '-'.
554: */
555: if (argc > 2 && STRCMP(argv[0], "--") == 0)
556: {
557: ++argv;
558: --argc;
559: }
560:
561: #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
562: if (ExpandWildCards(argc - 1, (char_u **)argv, &arg_count,
563: &arg_files, TRUE, TRUE) == OK && arg_count != 0)
564: {
565: fname = arg_files[0];
566: arg_exp = TRUE;
567: }
568: #else
569: arg_files = (char_u **)argv;
570: arg_count = argc - 1;
571: fname = (char_u *)argv[0];
572: #endif
573: if (arg_count > 1)
574: {
575: printf("%d files to edit\n", arg_count);
576: screen_start(); /* don't know where cursor is now */
577: }
578: }
579: }
580:
581: RedrawingDisabled = TRUE;
582:
583: /*
584: * When listing swap file names, don't do cursor positioning et. al.
585: */
586: if (recoverymode && fname == NULL)
587: full_screen = FALSE;
588:
589: #ifdef USE_GUI
590: /*
591: * We don't want to open the GUI window until after we've read .vimrc,
592: * otherwise we don't know what font we will use, and hence we don't know
593: * what size the window should be. So if there are errors in the .vimrc
594: * file, they will have to go to the terminal -- webb
595: */
596: if (gui.starting)
597: full_screen = FALSE;
598: #endif
599:
600: /*
601: * Now print a warning if stdout is not a terminal.
602: */
603: if (full_screen && (stdout_notty || mch_check_input() == FAIL))
604: {
605: if (stdout_notty)
606: fprintf(stderr, "Vim: Warning: Output is not to a terminal\n");
607: if (mch_check_input() == FAIL)
608: fprintf(stderr, "Vim: Warning: Input is not from a terminal\n");
609: mch_delay(2000L, TRUE);
610: screen_start(); /* don't know where cursor is now */
611: }
612:
613: curbuf->b_nwindows = 1; /* there is one window */
614: win_init(curwin); /* init current window */
615: init_yank(); /* init yank buffers */
616: if (full_screen)
617: termcapinit(term); /* set terminal name and get terminal
618: capabilities */
619: screenclear(); /* clear screen (just inits screen structures,
620: because starting is TRUE) */
621:
622: if (full_screen)
623: msg_start(); /* in case a mapping or error message is printed */
624: msg_scroll = TRUE;
625: no_wait_return = TRUE;
626:
627: #if defined(MSDOS) || defined(WIN32) || defined(OS2)
628: /*
629: * Default mapping for some often used keys.
630: * Need to put string in allocated memory, because do_map() will modify it.
631: */
632: for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i)
633: {
634: initstr = strsave(initmappings[i].arg);
635: if (initstr != NULL)
636: {
637: do_map(0, initstr, initmappings[i].mode);
638: vim_free(initstr);
639: }
640: }
641: #endif
642:
643: /*
644: * If -u option give, use only the initializations from that file and nothing
645: * else.
646: */
647: if (use_vimrc != NULL)
648: {
649: if (STRCMP(use_vimrc, "NONE") != 0)
650: {
651: if (do_source(use_vimrc, FALSE) == OK)
652: check_version = TRUE;
653: else
654: EMSG2("Cannot read from \"%s\"", use_vimrc);
655: }
656: }
657: else
658: {
659:
660: /*
661: * get system wide defaults (for unix)
662: */
663: #if defined(HAVE_CONFIG_H) || defined(OS2)
664: if (do_source(((vi_mode == TRUE) ? sys_compatrc_fname
665: : sys_vimrc_fname), TRUE) == OK)
666: check_version = TRUE;
667: #endif
668:
669: /*
670: * Try to read initialization commands from the following places:
671: * - environment variable VIMINIT
672: * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc for Unix)
673: * - environment variable EXINIT
674: * - user exrc file (s:.exrc for Amiga, ~/.exrc for Unix)
675: * The first that exists is used, the rest is ignored.
676: */
677: if ((initstr = vim_getenv((char_u *)"VIMINIT")) != NULL &&
678: *initstr != NUL)
679: {
680: sourcing_name = (char_u *)"VIMINIT";
681: do_cmdline(initstr, TRUE, TRUE);
682: sourcing_name = NULL;
683: }
684: else if (do_source((char_u *)USR_VIMRC_FILE, TRUE) == FAIL)
685: {
686: if ((initstr = vim_getenv((char_u *)"EXINIT")) != NULL)
687: {
688: sourcing_name = (char_u *)"EXINIT";
689: do_cmdline(initstr, TRUE, TRUE);
690: sourcing_name = NULL;
691: }
692: else
693: (void)do_source((char_u *)USR_EXRC_FILE, FALSE);
694: }
695: else
696: check_version = TRUE;
697:
698: /*
699: * Read initialization commands from ".vimrc" or ".exrc" in current
700: * directory. This is only done if the 'exrc' option is set.
701: * Because of security reasons we disallow shell and write commands now,
702: * except for unix if the file is owned by the user or 'secure' option has
703: * been reset in environmet of global ".exrc" or ".vimrc".
704: * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
705: * sys_vimrc_fname.
706: */
707: if (p_exrc)
708: {
709: #ifdef UNIX
710: {
711: struct stat s;
712:
713: /* if ".vimrc" file is not owned by user, set 'secure' mode */
714: if (stat(VIMRC_FILE, &s) || s.st_uid != getuid())
715: secure = p_secure;
716: }
717: #else
718: secure = p_secure;
719: #endif
720:
721: i = FAIL;
722: if (fullpathcmp((char_u *)USR_VIMRC_FILE,
723: (char_u *)VIMRC_FILE) != FPC_SAME
724: #if defined(HAVE_CONFIG_H) || defined(OS2)
725: && fullpathcmp(((vi_mode == TRUE) ? sys_compatrc_fname : sys_vimrc_fname),
726: (char_u *)VIMRC_FILE) != FPC_SAME
727: #endif
728: )
729: i = do_source((char_u *)VIMRC_FILE, TRUE);
730: #ifdef UNIX
731: if (i == FAIL)
732: {
733: struct stat s;
734:
735: /* if ".exrc" is not owned by user set 'secure' mode */
736: if (stat(EXRC_FILE, &s) || s.st_uid != getuid())
737: secure = p_secure;
738: else
739: secure = 0;
740: }
741: else
742: check_version = TRUE;
743: #endif
744: if (i == FAIL && fullpathcmp((char_u *)USR_EXRC_FILE,
745: (char_u *)EXRC_FILE) != FPC_SAME)
746: (void)do_source((char_u *)EXRC_FILE, FALSE);
747: }
748: }
749:
750: /*
751: * Recovery mode without a file name: List swap files.
752: * This uses the 'dir' option, therefore it must be after the
753: * initializations.
754: */
755: if (recoverymode && fname == NULL)
756: {
757: recover_names(NULL, TRUE, 0);
758: mch_windexit(0);
759: }
760:
761: /*
762: * Set a few option defaults after reading .vimrc files:
763: * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
764: */
765: set_init_3();
766:
767: #ifdef USE_GUI
768: if (gui.starting)
769: {
770: gui_start();
771: full_screen = TRUE;
772: }
773: #endif
774:
775: /*
776: * If we read a .vimrc but it does not contain a "version 4.0" command,
777: * give the user a pointer to the help for the new version.
778: */
779: if (check_version && found_version == 0)
780: {
781: MSG("This is Vim version 4.0.");
782: MSG("No \":version 4.0\" command found in any .vimrc.");
783: MSG("Use \":help version\" for info about this new version.");
784: }
785:
786: #ifdef VIMINFO
787: /*
788: * Read in registers, history etc, but not marks, from the viminfo file
789: */
790: if (*p_viminfo != NUL)
791: read_viminfo(NULL, TRUE, FALSE, FALSE);
792: #endif /* VIMINFO */
793:
794: #ifdef SPAWNO /* special MSDOS swapping library */
795: init_SPAWNO("", SWAP_ANY);
796: #endif
797:
798: if (bin_mode) /* -b option used */
799: {
800: set_options_bin(curbuf->b_p_bin, 1);
801: curbuf->b_p_bin = 1; /* binary file I/O */
802: }
803:
1.2 downsj 804: /*
805: * "-e errorfile": Load the error file now.
806: * If the error file can't be read, exit before doing anything else.
807: */
808: if (doqf && qf_init() == FAIL) /* if reading error file fails: exit */
809: {
810: outchar('\n');
811: mch_windexit(3);
812: }
813:
1.1 downsj 814: /* Don't set the file name if there was a command in .vimrc that already
815: * loaded the file */
816: if (curbuf->b_filename == NULL)
817: {
818: (void)setfname(fname, NULL, TRUE); /* includes maketitle() */
819: ++arg_idx; /* used first argument name */
820: }
821:
822: if (window_count == 0)
823: window_count = arg_count;
824: if (window_count > 1)
825: {
826: /* Don't change the windows if there was a command in .vimrc that
827: * already split some windows */
828: if (firstwin->w_next == NULL)
829: window_count = make_windows(window_count);
830: else
831: window_count = win_count();
832: }
833: else
834: window_count = 1;
835:
836: /*
837: * Start putting things on the screen.
838: * Scroll screen down before drawing over it
839: * Clear screen now, so file message will not be cleared.
840: */
841: starting = FALSE;
842: no_wait_return = FALSE;
843: msg_scroll = FALSE;
844: #ifdef USE_GUI
845: /*
846: * This seems to be required to make callbacks to be called now, instead
847: * of after things have been put on the screen, which then may be deleted
848: * when getting a resize callback.
849: */
850: if (gui.in_use)
851: gui_mch_wait_for_chars(50);
852: #endif
853:
854: /*
855: * When done something that is not allowed or error message call wait_return.
856: * This must be done before starttermcap(), because it may switch to another
857: * screen. It must be done after settmode(1), because we want to react on a
858: * single key stroke.
859: * Call settmode and starttermcap here, so the T_KS and T_TI may be defined
860: * by termcapinit and redifined in .exrc.
861: */
862: settmode(1);
863: if (secure == 2 || need_wait_return || msg_didany)
864: wait_return(TRUE);
865:
866: starttermcap(); /* start termcap if not done by wait_return() */
867: #ifdef USE_MOUSE
868: setmouse(); /* may start using the mouse */
869: #endif
870: if (scroll_region)
871: scroll_region_reset(); /* In case Rows changed */
872:
873: secure = 0;
874:
875: scroll_start();
1.3 ! downsj 876:
! 877: if (!invoked_as_ex) {
! 878: screenclear(); /* clear screen */
! 879: }
1.1 downsj 880:
881: no_wait_return = TRUE;
882:
883: if (recoverymode) /* do recover */
884: {
885: msg_scroll = TRUE; /* scroll message up */
886: ml_recover();
887: msg_scroll = FALSE;
888: if (curbuf->b_ml.ml_mfp == NULL) /* failed */
889: getout(1);
1.2 downsj 890: do_modelines(); /* do modelines */
891: }
892: else
893: {
894: /*
895: * Open a buffer for windows that don't have one yet.
896: * Commands in the .vimrc might have loaded a file or split the window.
897: * Watch out for autocommands that delete a window.
898: */
899: #ifdef AUTOCMD
900: /*
901: * Don't execute Win/Buf Enter/Leave autocommands here
902: */
903: ++autocmd_no_enter;
904: ++autocmd_no_leave;
905: #endif
906: for (curwin = firstwin; curwin != NULL; curwin = curwin->w_next)
907: {
908: curbuf = curwin->w_buffer;
909: if (curbuf->b_ml.ml_mfp == NULL)
910: {
911: (void)open_buffer(); /* create memfile and read file */
912: #ifdef AUTOCMD
913: curwin = firstwin; /* start again */
914: #endif
1.3 ! downsj 915: if (invoked_as_ex) { /* move to end of file if running as
! 916: ex */
! 917: curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
! 918: }
1.2 downsj 919: }
920: mch_breakcheck();
921: if (got_int)
922: {
923: (void)vgetc(); /* only break the file loading, not the rest */
924: break;
925: }
926: }
1.1 downsj 927: #ifdef AUTOCMD
1.2 downsj 928: --autocmd_no_enter;
929: --autocmd_no_leave;
1.1 downsj 930: #endif
1.2 downsj 931: curwin = firstwin;
932: curbuf = curwin->w_buffer;
1.1 downsj 933: }
934:
1.2 downsj 935: #ifdef AUTOCMD
936: apply_autocmds(EVENT_BUFENTER, NULL, NULL);
937: #endif
1.1 downsj 938: setpcmark();
939:
940: /*
941: * When started with "-e errorfile" jump to first error now.
942: */
943: if (doqf)
1.2 downsj 944: qf_jump(0, 0, FALSE);
1.1 downsj 945:
946: /*
947: * If opened more than one window, start editing files in the other windows.
948: * Make_windows() has already opened the windows.
949: */
1.2 downsj 950: #ifdef AUTOCMD
951: /*
952: * Don't execute Win/Buf Enter/Leave autocommands here
953: */
954: ++autocmd_no_enter;
955: ++autocmd_no_leave;
956: #endif
1.1 downsj 957: for (i = 1; i < window_count; ++i)
958: {
959: if (curwin->w_next == NULL) /* just checking */
960: break;
961: win_enter(curwin->w_next, FALSE);
962:
963: /* Only open the file if there is no file in this window yet (that can
964: * happen when .vimrc contains ":sall") */
965: if (curbuf == firstwin->w_buffer || curbuf->b_filename == NULL)
966: {
967: curwin->w_arg_idx = arg_idx;
968: /* edit file from arg list, if there is one */
969: (void)do_ecmd(0, arg_idx < arg_count ? arg_files[arg_idx] : NULL,
1.3 ! downsj 970: NULL, NULL, (linenr_t)0, ECMD_HIDE);
1.1 downsj 971: if (arg_idx == arg_count - 1)
972: arg_had_last = TRUE;
973: ++arg_idx;
974: }
975: mch_breakcheck();
976: if (got_int)
977: {
978: (void)vgetc(); /* only break the file loading, not the rest */
979: break;
980: }
981: }
1.2 downsj 982: #ifdef AUTOCMD
983: --autocmd_no_enter;
984: #endif
1.1 downsj 985: win_enter(firstwin, FALSE); /* back to first window */
1.2 downsj 986: #ifdef AUTOCMD
987: --autocmd_no_leave;
988: #endif
1.1 downsj 989: if (window_count > 1)
990: win_equal(curwin, FALSE); /* adjust heights */
991:
992: /*
993: * If there are more file names in the argument list than windows,
994: * put the rest of the names in the buffer list.
995: */
996: while (arg_idx < arg_count)
997: (void)buflist_add(arg_files[arg_idx++]);
998:
999: /*
1000: * Need to jump to the tag before executing the '-c command'.
1001: * Makes "vim -c '/return' -t main" work.
1002: */
1003: if (tagname)
1004: {
1005: STRCPY(IObuff, "ta ");
1006: STRCAT(IObuff, tagname);
1007: do_cmdline(IObuff, TRUE, TRUE);
1008: }
1009:
1010: if (command)
1011: {
1012: /*
1013: * We start commands on line 0, make "vim +/pat file" match a
1014: * pattern on line 1.
1015: */
1016: curwin->w_cursor.lnum = 0;
1017: sourcing_name = (char_u *)"command line";
1018: do_cmdline(command, TRUE, TRUE);
1019: sourcing_name = NULL;
1020: }
1021:
1022: RedrawingDisabled = FALSE;
1023: redraw_later(NOT_VALID);
1024: no_wait_return = FALSE;
1025:
1026: /* start in insert mode */
1027: if (p_im)
1028: need_start_insertmode = TRUE;
1029:
1030: /*
1031: * main command loop
1032: */
1033: for (;;)
1034: {
1035: if (stuff_empty())
1036: {
1037: if (need_check_timestamps)
1038: check_timestamps();
1039: if (need_wait_return) /* if wait_return still needed ... */
1040: wait_return(FALSE); /* ... call it now */
1041: if (need_start_insertmode)
1042: {
1043: need_start_insertmode = FALSE;
1044: stuffReadbuff((char_u *)"i"); /* start insert mode next */
1045: /* skip the fileinfo message now, because it would be shown
1046: * after insert mode finishes! */
1047: need_fileinfo = FALSE;
1048: }
1049: }
1050: dont_wait_return = FALSE;
1051: if (got_int)
1052: {
1053: (void)vgetc(); /* flush all buffers */
1054: got_int = FALSE;
1055: }
1056: adjust_cursor(); /* put cursor on an existing line */
1057: msg_scroll = FALSE;
1058: quit_more = FALSE;
1059: keep_help_flag = FALSE;
1060: /*
1061: * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1062: * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1063: * update cursor and redraw.
1064: */
1.3 ! downsj 1065: if (skip_redraw || invoked_as_ex)
1.1 downsj 1066: skip_redraw = FALSE;
1067: else if (do_redraw || stuff_empty())
1068: {
1069: cursupdate(); /* Figure out where the cursor is based
1070: on curwin->w_cursor. */
1071: #ifdef SLEEP_IN_EMSG
1072: if (need_sleep) /* sleep before redrawing */
1073: {
1074: mch_delay(1000L, TRUE);
1075: need_sleep = FALSE;
1076: }
1077: #endif
1078: if (VIsual_active)
1079: update_curbuf(INVERTED);/* update inverted part */
1080: if (must_redraw)
1081: updateScreen(must_redraw);
1082: else if (redraw_cmdline)
1083: showmode();
1084: if (keep_msg != NULL)
1085: {
1086: if (keep_msg_highlight)
1087: {
1088: (void)set_highlight(keep_msg_highlight);
1089: msg_highlight = TRUE;
1090: }
1091: msg(keep_msg); /* display message after redraw */
1092: }
1093: if (need_fileinfo) /* used after jumping to a tag */
1094: {
1095: fileinfo(did_cd, TRUE, FALSE);
1096: need_fileinfo = FALSE;
1097: }
1098:
1099: emsg_on_display = FALSE; /* can delete error message now */
1100: msg_didany = FALSE; /* reset lines_left in msg_start() */
1101: do_redraw = FALSE;
1102: showruler(FALSE);
1103:
1104: setcursor();
1105: cursor_on();
1.3 ! downsj 1106: }
! 1107:
! 1108: /*
! 1109: * if we're invoked as ex, do a round of ex commands before
! 1110: * going on to normal mode
! 1111: */
! 1112:
! 1113: if (invoked_as_ex) {
! 1114: do_exmode();
! 1115:
! 1116: cursupdate();
! 1117: updateScreen(TRUE);
! 1118: showmode();
! 1119: setcursor();
! 1120: cursor_on();
! 1121:
! 1122: invoked_as_ex = FALSE;
1.1 downsj 1123: }
1124:
1125: /*
1126: * get and execute a normal mode command
1127: */
1128: normal();
1129: }
1130: /*NOTREACHED*/
1131: }
1132:
1133: void
1134: getout(r)
1135: int r;
1136: {
1137: exiting = TRUE;
1138:
1139: /* Position the cursor on the last screen line, below all the text */
1140: #ifdef USE_GUI
1141: if (!gui.in_use)
1142: #endif
1143: windgoto((int)Rows - 1, 0);
1144:
1145: #ifdef AUTOCMD
1146: apply_autocmds(EVENT_VIMLEAVE, NULL, NULL);
1147:
1148: /* Position the cursor again, the autocommands may have moved it */
1149: # ifdef USE_GUI
1150: if (!gui.in_use)
1151: # endif
1152: windgoto((int)Rows - 1, 0);
1153: #endif
1154:
1155: #ifdef VIMINFO
1.2 downsj 1156: msg_didany = FALSE;
1.1 downsj 1157: /* Write out the registers, history, marks etc, to the viminfo file */
1158: if (*p_viminfo != NUL)
1159: write_viminfo(NULL, FALSE);
1.2 downsj 1160: if (msg_didany) /* make the user read the error message */
1161: {
1162: no_wait_return = FALSE;
1163: wait_return(FALSE);
1164: }
1.1 downsj 1165: #endif /* VIMINFO */
1166:
1167: mch_windexit(r);
1168: }