nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] Simplifying nano just a bit


From: Eitan Adler
Subject: [Nano-devel] Simplifying nano just a bit
Date: Mon, 31 Mar 2014 22:56:54 -0700

Hi all,

I believe the following patch may make nano users much happier when
they open their editor.  Please consider applying it.

commit 233a52cb669f8881f6038a05f982ca49001cc79e
Author: Eitan Adler <address@hidden>
Date:   Mon Mar 31 22:54:40 2014 -0700

    Simplify the user experienced when opening nano.

diff --git a/trunk/nano/src/nano.c b/trunk/nano/src/nano.c
index 22305bb..f9bd873 100644
--- a/trunk/nano/src/nano.c
+++ b/trunk/nano/src/nano.c
@@ -1,2756 +1,4150 @@
-/* $Id$ */
-/**************************************************************************
- *   nano.c                                                               *
- *                                                                        *
- *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,  *
- *   2008, 2009 Free Software Foundation, Inc.                            *
- *   This program is free software; you can redistribute it and/or modify *
- *   it under the terms of the GNU General Public License as published by *
- *   the Free Software Foundation; either version 3, or (at your option)  *
- *   any later version.                                                   *
- *                                                                        *
- *   This program is distributed in the hope that it will be useful, but  *
- *   WITHOUT ANY WARRANTY; without even the implied warranty of           *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
- *   General Public License for more details.                             *
- *                                                                        *
- *   You should have received a copy of the GNU General Public License    *
- *   along with this program; if not, write to the Free Software          *
- *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
- *   02110-1301, USA.                                                     *
- *                                                                        *
- **************************************************************************/
-
-#include "proto.h"
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <ctype.h>
-#include <locale.h>
-#include <time.h>
-#ifdef ENABLE_UTF8
-#include <langinfo.h>
-#endif
-#include <termios.h>
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#endif
-#ifndef NANO_TINY
-#include <sys/ioctl.h>
-#endif
-
-#ifndef DISABLE_MOUSE
-static int oldinterval = -1;
-       /* Used to store the user's original mouse click interval. */
-#endif
-#ifdef ENABLE_NANORC
-static bool no_rcfiles = FALSE;
-       /* Should we ignore all rcfiles? */
-#endif
-static struct termios oldterm;
-       /* The user's original terminal settings. */
-static struct sigaction act;
-       /* Used to set up all our fun signal handlers. */
-
-/* Create a new filestruct node.  Note that we do not set prevnode->next
- * to the new line. */
-filestruct *make_new_node(filestruct *prevnode)
-{
-    filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct));
+/* vi:set ts=8 sts=4 sw=4:
+ *
+ * VIM - Vi IMproved   by Bram Moolenaar
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */

-    newnode->data = NULL;
-    newnode->prev = prevnode;
-    newnode->next = NULL;
-    newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1;
+#define EXTERN
+#include "vim.h"

-#ifdef ENABLE_COLOR
-    newnode->multidata = NULL;
+#ifdef SPAWNO
+# include <spawno.h>           /* special MS-DOS swapping library */
 #endif

-    return newnode;
-}
-
-/* Make a copy of a filestruct node. */
-filestruct *copy_node(const filestruct *src)
-{
-    filestruct *dst;
-
-    assert(src != NULL);
-
-    dst = (filestruct *)nmalloc(sizeof(filestruct));
-
-    dst->data = mallocstrcpy(NULL, src->data);
-    dst->next = src->next;
-    dst->prev = src->prev;
-    dst->lineno = src->lineno;
-#ifdef ENABLE_COLOR
-    dst->multidata = NULL;
+#ifdef __CYGWIN__
+# ifndef WIN32
+#  include <cygwin/version.h>
+#  include <sys/cygwin.h>      /* for cygwin_conv_to_posix_path() and/or
+                                * cygwin_conv_path() */
+# endif
+# include <limits.h>
 #endif

-    return dst;
-}
-
-/* Splice a node into an existing filestruct. */
-void splice_node(filestruct *begin, filestruct *newnode, filestruct
-       *end)
-{
-    assert(newnode != NULL && begin != NULL);
+/* Maximum number of commands from + or -c arguments. */
+#define MAX_ARG_CMDS 10

-    newnode->next = end;
-    newnode->prev = begin;
-    begin->next = newnode;
-    if (end != NULL)
-       end->prev = newnode;
-}
+/* values for "window_layout" */
+#define WIN_HOR            1       /* "-o" horizontally split windows */
+#define        WIN_VER     2       /* "-O" vertically split windows */
+#define        WIN_TABS    3       /* "-p" windows on tab pages */

-/* Unlink a node from the rest of the filestruct. */
-void unlink_node(const filestruct *fileptr)
+/* Struct for various parameters passed between main() and other functions. */
+typedef struct
 {
-    assert(fileptr != NULL);
-
-    if (fileptr->prev != NULL)
-       fileptr->prev->next = fileptr->next;
-    if (fileptr->next != NULL)
-       fileptr->next->prev = fileptr->prev;
-}
-
-/* Delete a node from the filestruct. */
-void delete_node(filestruct *fileptr)
+    int                argc;
+    char       **argv;
+
+    int                evim_mode;              /* started as "evim" */
+    char_u     *use_vimrc;             /* vimrc from -u argument */
+
+    int                n_commands;                  /* no. of commands from + 
or -c */
+    char_u     *commands[MAX_ARG_CMDS];     /* commands from + or -c arg. */
+    char_u     cmds_tofree[MAX_ARG_CMDS];   /* commands that need free() */
+    int                n_pre_commands;              /* no. of commands from 
--cmd */
+    char_u     *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
+
+    int                edit_type;              /* type of editing to do */
+    char_u     *tagname;               /* tag from -t argument */
+#ifdef FEAT_QUICKFIX
+    char_u     *use_ef;                /* 'errorfile' from -q argument */
+#endif
+
+    int                want_full_screen;
+    int                stdout_isatty;          /* is stdout a terminal? */
+    char_u     *term;                  /* specified terminal name */
+#ifdef FEAT_CRYPT
+    int                ask_for_key;            /* -x argument */
+#endif
+    int                no_swap_file;           /* "-n" argument used */
+#ifdef FEAT_EVAL
+    int                use_debug_break_level;
+#endif
+#ifdef FEAT_WINDOWS
+    int                window_count;           /* number of windows to use */
+    int                window_layout;          /* 0, WIN_HOR, WIN_VER or 
WIN_TABS */
+#endif
+
+#ifdef FEAT_CLIENTSERVER
+    int                serverArg;              /* TRUE when argument for a 
server */
+    char_u     *serverName_arg;        /* cmdline arg for server name */
+    char_u     *serverStr;             /* remote server command */
+    char_u     *serverStrEnc;          /* encoding of serverStr */
+    char_u     *servername;            /* allocated name for our server */
+#endif
+#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
+    int                literal;                /* don't expand file names */
+#endif
+#ifdef MSWIN
+    int                full_path;              /* file name argument was full 
path */
+#endif
+#ifdef FEAT_DIFF
+    int                diff_mode;              /* start with 'diff' set */
+#endif
+} mparm_T;
+
+/* Values for edit_type. */
+#define EDIT_NONE   0      /* no edit type yet */
+#define EDIT_FILE   1      /* file name argument[s] given, use argument list */
+#define EDIT_STDIN  2      /* read file from stdin */
+#define EDIT_TAG    3      /* tag name argument given, use tagname */
+#define EDIT_QF            4       /* start in quickfix mode */
+
+#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
+static int file_owned __ARGS((char *fname));
+#endif
+static void mainerr __ARGS((int, char_u *));
+#ifndef NO_VIM_MAIN
+static void main_msg __ARGS((char *s));
+static void usage __ARGS((void));
+static int get_number_arg __ARGS((char_u *p, int *idx, int def));
+# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
+static void init_locale __ARGS((void));
+# endif
+static void parse_command_name __ARGS((mparm_T *parmp));
+static void early_arg_scan __ARGS((mparm_T *parmp));
+static void command_line_scan __ARGS((mparm_T *parmp));
+static void check_tty __ARGS((mparm_T *parmp));
+static void read_stdin __ARGS((void));
+static void create_windows __ARGS((mparm_T *parmp));
+# ifdef FEAT_WINDOWS
+static void edit_buffers __ARGS((mparm_T *parmp));
+# endif
+static void exe_pre_commands __ARGS((mparm_T *parmp));
+static void exe_commands __ARGS((mparm_T *parmp));
+static void source_startup_scripts __ARGS((mparm_T *parmp));
+static void main_start_gui __ARGS((void));
+# if defined(HAS_SWAP_EXISTS_ACTION)
+static void check_swap_exists_action __ARGS((void));
+# endif
+# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
+static void exec_on_server __ARGS((mparm_T *parmp));
+static void prepare_server __ARGS((mparm_T *parmp));
+static void cmdsrv_main __ARGS((int *argc, char **argv, char_u
*serverName_arg, char_u **serverStr));
+static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
+# endif
+#endif
+
+
+/*
+ * Different types of error messages.
+ */
+static char *(main_errors[]) =
 {
-    assert(fileptr != NULL && fileptr->data != NULL);
+    N_("Unknown option argument"),
+#define ME_UNKNOWN_OPTION      0
+    N_("Too many edit arguments"),
+#define ME_TOO_MANY_ARGS       1
+    N_("Argument missing after"),
+#define ME_ARG_MISSING         2
+    N_("Garbage after option argument"),
+#define ME_GARBAGE             3
+    N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
+#define ME_EXTRA_CMD           4
+    N_("Invalid argument for"),
+#define ME_INVALID_ARG         5
+};
+
+#ifndef PROTO          /* don't want a prototype for main() */
+#ifndef NO_VIM_MAIN    /* skip this for unittests */
+    int
+# ifdef VIMDLL
+_export
+# endif
+# ifdef FEAT_GUI_MSWIN
+#  ifdef __BORLANDC__
+_cdecl
+#  endif
+VimMain
+# else
+main
+# endif
+(argc, argv)
+    int                argc;
+    char       **argv;
+{
+    char_u     *fname = NULL;          /* file name from command line */
+    mparm_T    params;                 /* various parameters passed between
+                                        * main() and other functions. */
+#ifdef STARTUPTIME
+    int                i;
+#endif
+
+    /*
+     * Do any system-specific initialisations.  These can NOT use IObuff or
+     * NameBuff.  Thus emsg2() cannot be called!
+     */
+    mch_early_init();
+
+    /* Many variables are in "params" so that we can pass them to invoked
+     * functions without a lot of arguments.  "argc" and "argv" are also
+     * copied, so that they can be changed. */
+    vim_memset(&params, 0, sizeof(params));
+    params.argc = argc;
+    params.argv = argv;
+    params.want_full_screen = TRUE;
+#ifdef FEAT_EVAL
+    params.use_debug_break_level = -1;
+#endif
+#ifdef FEAT_WINDOWS
+    params.window_count = -1;
+#endif
+
+#ifdef FEAT_RUBY
+    {
+       int ruby_stack_start;
+       vim_ruby_init((void *)&ruby_stack_start);
+    }
+#endif

-    if (fileptr->data != NULL)
-       free(fileptr->data);
+#ifdef FEAT_TCL
+    vim_tcl_init(params.argv[0]);
+#endif

-#ifdef ENABLE_COLOR
-    if (fileptr->multidata)
-       free(fileptr->multidata);
+#ifdef MEM_PROFILE
+    atexit(vim_mem_profile_dump);
 #endif

-    free(fileptr);
-}
+#ifdef STARTUPTIME
+    for (i = 1; i < argc; ++i)
+    {
+       if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
+       {
+           time_fd = mch_fopen(argv[i + 1], "a");
+           TIME_MSG("--- VIM STARTING ---");
+           break;
+       }
+    }
+#endif
+    starttime = time(NULL);

-/* Duplicate a whole filestruct. */
-filestruct *copy_filestruct(const filestruct *src)
-{
-    filestruct *head, *copy;
+#ifdef __EMX__
+    _wildcard(&params.argc, &params.argv);
+#endif

-    assert(src != NULL);
+#ifdef FEAT_MBYTE
+    (void)mb_init();   /* init mb_bytelen_tab[] to ones */
+#endif
+#ifdef FEAT_EVAL
+    eval_init();       /* init global variables */
+#endif

-    copy = copy_node(src);
-    copy->prev = NULL;
-    head = copy;
-    src = src->next;
+#ifdef __QNXNTO__
+    qnx_init();                /* PhAttach() for clipboard, (and gui) */
+#endif

-    while (src != NULL) {
-       copy->next = copy_node(src);
-       copy->next->prev = copy;
-       copy = copy->next;
+#ifdef MAC_OS_CLASSIC
+    /* Prepare for possibly starting GUI sometime */
+    /* Macintosh needs this before any memory is allocated. */
+    gui_prepare(&params.argc, params.argv);
+    TIME_MSG("GUI prepared");
+#endif

-       src = src->next;
-    }
+    /* Init the table of Normal mode commands. */
+    init_normal_cmds();

-    copy->next = NULL;
+#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
+    make_version();    /* Construct the long version string. */
+#endif

-    return head;
-}
+    /*
+     * Allocate space for the generic buffers (needed for set_init_1() and
+     * EMSG2()).
+     */
+    if ((IObuff = alloc(IOSIZE)) == NULL
+           || (NameBuff = alloc(MAXPATHL)) == NULL)
+       mch_exit(0);
+    TIME_MSG("Allocated generic buffers");

-/* Free a filestruct. */
-void free_filestruct(filestruct *src)
-{
-    assert(src != NULL);
+#ifdef NBDEBUG
+    /* Wait a moment for debugging NetBeans.  Must be after allocating
+     * NameBuff. */
+    nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
+    nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
+    TIME_MSG("NetBeans debug wait");
+#endif

-    while (src->next != NULL) {
-       src = src->next;
-       delete_node(src->prev);
-    }
+#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
+    /*
+     * Setup to use the current locale (for ctype() and many other things).
+     * NOTE: Translated messages with encodings other than latin1 will not
+     * work until set_init_1() has been called!
+     */
+    init_locale();
+    TIME_MSG("locale set");
+#endif

-    delete_node(src);
-}
+#ifdef FEAT_GUI
+    gui.dofork = TRUE;             /* default is to use fork() */
+#endif

-/* Renumber all entries in a filestruct, starting with fileptr. */
-void renumber(filestruct *fileptr)
-{
-    ssize_t line;
+    /*
+     * Do a first scan of the arguments in "argv[]":
+     *   -display or --display
+     *   --server...
+     *   --socketid
+     *   --windowid
+     */
+    early_arg_scan(&params);

-    assert(fileptr != NULL);
+#ifdef FEAT_SUN_WORKSHOP
+    findYourself(params.argv[0]);
+#endif
+#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
+    /* Prepare for possibly starting GUI sometime */
+    gui_prepare(&params.argc, params.argv);
+    TIME_MSG("GUI prepared");
+#endif

-    line = (fileptr->prev == NULL) ? 0 : fileptr->prev->lineno;
+#ifdef FEAT_CLIPBOARD
+    clip_init(FALSE);          /* Initialise clipboard stuff */
+    TIME_MSG("clipboard setup");
+#endif

-    assert(fileptr != fileptr->next);
+    /*
+     * Check if we have an interactive window.
+     * On the Amiga: If there is no window, we open one with a newcli command
+     * (needed for :! to * work). mch_check_win() will also handle the -d or
+     * -dev argument.
+     */
+    params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
+    TIME_MSG("window checked");

-    for (; fileptr != NULL; fileptr = fileptr->next)
-       fileptr->lineno = ++line;
-}
+    /*
+     * Allocate the first window and buffer.
+     * Can't do anything without it, exit when it fails.
+     */
+    if (win_alloc_first() == FAIL)
+       mch_exit(0);

-/* Partition a filestruct so that it begins at (top, top_x) and ends at
- * (bot, bot_x). */
-partition *partition_filestruct(filestruct *top, size_t top_x,
-       filestruct *bot, size_t bot_x)
-{
-    partition *p;
-
-    assert(top != NULL && bot != NULL && openfile->fileage != NULL &&
openfile->filebot != NULL);
-
-    /* Initialize the partition. */
-    p = (partition *)nmalloc(sizeof(partition));
-
-    /* If the top and bottom of the partition are different from the top
-     * and bottom of the filestruct, save the latter and then set them
-     * to top and bot. */
-    if (top != openfile->fileage) {
-       p->fileage = openfile->fileage;
-       openfile->fileage = top;
-    } else
-       p->fileage = NULL;
-    if (bot != openfile->filebot) {
-       p->filebot = openfile->filebot;
-       openfile->filebot = bot;
-    } else
-       p->filebot = NULL;
-
-    /* Save the line above the top of the partition, detach the top of
-     * the partition from it, and save the text before top_x in
-     * top_data. */
-    p->top_prev = top->prev;
-    top->prev = NULL;
-    p->top_data = mallocstrncpy(NULL, top->data, top_x + 1);
-    p->top_data[top_x] = '\0';
-
-    /* Save the line below the bottom of the partition, detach the
-     * bottom of the partition from it, and save the text after bot_x in
-     * bot_data. */
-    p->bot_next = bot->next;
-    bot->next = NULL;
-    p->bot_data = mallocstrcpy(NULL, bot->data + bot_x);
-
-    /* Remove all text after bot_x at the bottom of the partition. */
-    null_at(&bot->data, bot_x);
-
-    /* Remove all text before top_x at the top of the partition. */
-    charmove(top->data, top->data + top_x, strlen(top->data) -
-       top_x + 1);
-    align(&top->data);
-
-    /* Return the partition. */
-    return p;
-}
+    init_yank();               /* init yank buffers */

-/* Unpartition a filestruct so that it begins at (fileage, 0) and ends
- * at (filebot, strlen(filebot->data)) again. */
-void unpartition_filestruct(partition **p)
-{
-    char *tmp;
-
-    assert(p != NULL && openfile->fileage != NULL &&
openfile->filebot != NULL);
-
-    /* Reattach the line above the top of the partition, and restore the
-     * text before top_x from top_data.  Free top_data when we're done
-     * with it. */
-    tmp = mallocstrcpy(NULL, openfile->fileage->data);
-    openfile->fileage->prev = (*p)->top_prev;
-    if (openfile->fileage->prev != NULL)
-       openfile->fileage->prev->next = openfile->fileage;
-    openfile->fileage->data = charealloc(openfile->fileage->data,
-       strlen((*p)->top_data) + strlen(openfile->fileage->data) + 1);
-    strcpy(openfile->fileage->data, (*p)->top_data);
-    free((*p)->top_data);
-    strcat(openfile->fileage->data, tmp);
-    free(tmp);
-
-    /* Reattach the line below the bottom of the partition, and restore
-     * the text after bot_x from bot_data.  Free bot_data when we're
-     * done with it. */
-    openfile->filebot->next = (*p)->bot_next;
-    if (openfile->filebot->next != NULL)
-       openfile->filebot->next->prev = openfile->filebot;
-    openfile->filebot->data = charealloc(openfile->filebot->data,
-       strlen(openfile->filebot->data) + strlen((*p)->bot_data) + 1);
-    strcat(openfile->filebot->data, (*p)->bot_data);
-    free((*p)->bot_data);
-
-    /* Restore the top and bottom of the filestruct, if they were
-     * different from the top and bottom of the partition. */
-    if ((*p)->fileage != NULL)
-       openfile->fileage = (*p)->fileage;
-    if ((*p)->filebot != NULL)
-       openfile->filebot = (*p)->filebot;
-
-    /* Uninitialize the partition. */
-    free(*p);
-    *p = NULL;
-}
+    alist_init(&global_alist); /* Init the argument list to empty. */

-/* Move all the text between (top, top_x) and (bot, bot_x) in the
- * current filestruct to a filestruct beginning with file_top and ending
- * with file_bot.  If no text is between (top, top_x) and (bot, bot_x),
- * don't do anything. */
-void move_to_filestruct(filestruct **file_top, filestruct **file_bot,
-       filestruct *top, size_t top_x, filestruct *bot, size_t bot_x)
-{
-    filestruct *top_save;
-    bool edittop_inside;
-#ifndef NANO_TINY
-    bool mark_inside = FALSE;
-    bool same_line = FALSE;
-#endif
-
-    assert(file_top != NULL && file_bot != NULL && top != NULL && bot != NULL);
-
-    /* If (top, top_x)-(bot, bot_x) doesn't cover any text, get out. */
-    if (top == bot && top_x == bot_x)
-       return;
-
-    /* Partition the filestruct so that it contains only the text from
-     * (top, top_x) to (bot, bot_x), keep track of whether the top of
-     * the edit window is inside the partition, and keep track of
-     * whether the mark begins inside the partition. */
-    filepart = partition_filestruct(top, top_x, bot, bot_x);
-    edittop_inside = (openfile->edittop->lineno >=
-       openfile->fileage->lineno && openfile->edittop->lineno <=
-       openfile->filebot->lineno);
-#ifndef NANO_TINY
-    if (openfile->mark_set) {
-       mark_inside = (openfile->mark_begin->lineno >=
-               openfile->fileage->lineno &&
-               openfile->mark_begin->lineno <=
-               openfile->filebot->lineno &&
-               (openfile->mark_begin != openfile->fileage ||
-               openfile->mark_begin_x >= top_x) &&
-               (openfile->mark_begin != openfile->filebot ||
-               openfile->mark_begin_x <= bot_x));
-       same_line = (openfile->mark_begin == openfile->fileage);
+    /*
+     * Set the default values for the options.
+     * NOTE: Non-latin1 translated messages are working only after this,
+     * because this is where "has_mbyte" will be set, which is used by
+     * msg_outtrans_len_attr().
+     * First find out the home directory, needed to expand "~" in options.
+     */
+    init_homedir();            /* find real value of $HOME */
+    set_init_1();
+    TIME_MSG("inits 1");
+
+#ifdef FEAT_EVAL
+    set_lang_var();            /* set v:lang and v:ctype */
+#endif
+
+#ifdef FEAT_CLIENTSERVER
+    /*
+     * Do the client-server stuff, unless "--servername ''" was used.
+     * This may exit Vim if the command was sent to the server.
+     */
+    exec_on_server(&params);
+#endif
+
+    /*
+     * Figure out the way to work from the command name argv[0].
+     * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
+     */
+    parse_command_name(&params);
+
+    /*
+     * Process the command line arguments.  File names are put in the global
+     * argument list "global_alist".
+     */
+    command_line_scan(&params);
+    TIME_MSG("parsing arguments");
+
+    /*
+     * On some systems, when we compile with the GUI, we always use it.  On Mac
+     * there is no terminal version, and on Windows we can't fork one off with
+     * :gui.
+     */
+#ifdef ALWAYS_USE_GUI
+    gui.starting = TRUE;
+#else
+# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
+    /*
+     * Check if the GUI can be started.  Reset gui.starting if not.
+     * Don't know about other systems, stay on the safe side and don't check.
+     */
+    if (gui.starting)
+    {
+       if (gui_init_check() == FAIL)
+       {
+           gui.starting = FALSE;
+
+           /* When running "evim" or "gvim -y" we need the menus, exit if we
+            * don't have them. */
+           if (params.evim_mode)
+               mch_exit(1);
+       }
     }
-#endif
-
-    /* Get the number of characters in the text, and subtract it from
-     * totsize. */
-    openfile->totsize -= get_totsize(top, bot);
-
-    if (*file_top == NULL) {
-       /* If file_top is empty, just move all the text directly into
-        * it.  This is equivalent to tacking the text in top onto the
-        * (lack of) text at the end of file_top. */
-       *file_top = openfile->fileage;
-       *file_bot = openfile->filebot;
-
-       /* Renumber starting with file_top. */
-       renumber(*file_top);
-    } else {
-       filestruct *file_bot_save = *file_bot;
-
-       /* Otherwise, tack the text in top onto the text at the end of
-        * file_bot. */
-       (*file_bot)->data = charealloc((*file_bot)->data,
-               strlen((*file_bot)->data) +
-               strlen(openfile->fileage->data) + 1);
-       strcat((*file_bot)->data, openfile->fileage->data);
-
-       /* Attach the line after top to the line after file_bot.  Then,
-        * if there's more than one line after top, move file_bot down
-        * to bot. */
-       (*file_bot)->next = openfile->fileage->next;
-       if ((*file_bot)->next != NULL) {
-           (*file_bot)->next->prev = *file_bot;
-           *file_bot = openfile->filebot;
+# endif
+#endif
+
+    if (GARGCOUNT > 0)
+    {
+#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
+       /*
+        * Expand wildcards in file names.
+        */
+       if (!params.literal)
+       {
+           /* Temporarily add '(' and ')' to 'isfname'.  These are valid
+            * filename characters but are excluded from 'isfname' to make
+            * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
+           do_cmdline_cmd((char_u *)":set isf+=(,)");
+           alist_expand(NULL, 0);
+           do_cmdline_cmd((char_u *)":set isf&");
        }
-
-       /* Renumber starting with the line after the original
-        * file_bot. */
-       if (file_bot_save->next != NULL)
-           renumber(file_bot_save->next);
+#endif
+       fname = alist_name(&GARGLIST[0]);
     }

-    /* Since the text has now been saved, remove it from the
-     * filestruct. */
-    openfile->fileage = (filestruct *)nmalloc(sizeof(filestruct));
-    openfile->fileage->data = mallocstrcpy(NULL, "");
-    openfile->filebot = openfile->fileage;
-
-#ifdef ENABLE_COLOR
-    openfile->fileage->multidata = NULL;
-#endif
-
-    /* Restore the current line and cursor position.  If the mark begins
-     * inside the partition, set the beginning of the mark to where the
-     * saved text used to start. */
-    openfile->current = openfile->fileage;
-    openfile->current_x = top_x;
-#ifndef NANO_TINY
-    if (mark_inside) {
-       openfile->mark_begin = openfile->current;
-       openfile->mark_begin_x = openfile->current_x;
-    } else if (same_line)
-       /* Update the content of this partially cut line. */
-       openfile->mark_begin = openfile->current;
-#endif
-
-    top_save = openfile->fileage;
-
-    /* Unpartition the filestruct so that it contains all the text
-     * again, minus the saved text. */
-    unpartition_filestruct(&filepart);
-
-    /* If the top of the edit window was inside the old partition, put
-     * it in range of current. */
-    if (edittop_inside)
-       edit_update(NONE);
-
-    /* Renumber starting with the beginning line of the old
-     * partition. */
-    renumber(top_save);
-
-    /* If the NO_NEWLINES flag isn't set, and the text doesn't end with
-     * a magicline, add a new magicline. */
-    if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
-       new_magicline();
-}
+#if defined(WIN32) && defined(FEAT_MBYTE)
+    {
+       extern void set_alist_count(void);

-/* Copy all the text from the filestruct beginning with file_top and
- * ending with file_bot to the current filestruct at the current cursor
- * position. */
-void copy_from_filestruct(filestruct *file_top, filestruct *file_bot)
-{
-    filestruct *top_save;
-    size_t current_x_save = openfile->current_x;
-    bool edittop_inside;
-#ifndef NANO_TINY
-    bool right_side_up = FALSE, single_line = FALSE;
+       /* Remember the number of entries in the argument list.  If it changes
+        * we don't react on setting 'encoding'. */
+       set_alist_count();
+    }
 #endif

-    assert(file_top != NULL && file_bot != NULL);
-
-#ifndef NANO_TINY
-    /* Keep track of whether the mark begins inside the partition and
-     * will need adjustment. */
-    if (openfile->mark_set) {
-       filestruct *top, *bot;
-       size_t top_x, bot_x;
-
-       mark_order((const filestruct **)&top, &top_x,
-               (const filestruct **)&bot, &bot_x, &right_side_up);
-
-       single_line = (top == bot);
+#ifdef MSWIN
+    if (GARGCOUNT == 1 && params.full_path)
+    {
+       /*
+        * If there is one filename, fully qualified, we have very probably
+        * been invoked from explorer, so change to the file's directory.
+        * Hint: to avoid this when typing a command use a forward slash.
+        * If the cd fails, it doesn't matter.
+        */
+       (void)vim_chdirfile(fname);
     }
 #endif
-
-    /* Partition the filestruct so that it contains no text, and keep
-     * track of whether the top of the edit window is inside the
-     * partition. */
-    filepart = partition_filestruct(openfile->current,
-       openfile->current_x, openfile->current, openfile->current_x);
-    edittop_inside = (openfile->edittop == openfile->fileage);
-
-    /* Put the top and bottom of the filestruct at copies of file_top
-     * and file_bot. */
-    openfile->fileage = copy_filestruct(file_top);
-    openfile->filebot = openfile->fileage;
-    while (openfile->filebot->next != NULL)
-       openfile->filebot = openfile->filebot->next;
-
-    /* Restore the current line and cursor position.  If the mark begins
-     * inside the partition, adjust the mark coordinates to compensate
-     * for the change in the current line. */
-    openfile->current = openfile->filebot;
-    openfile->current_x = strlen(openfile->filebot->data);
-    if (openfile->fileage == openfile->filebot) {
-#ifndef NANO_TINY
-       if (openfile->mark_set) {
-           openfile->mark_begin = openfile->current;
-           if (!right_side_up)
-               openfile->mark_begin_x += openfile->current_x;
+    TIME_MSG("expanding arguments");
+
+#ifdef FEAT_DIFF
+    if (params.diff_mode && params.window_count == -1)
+       params.window_count = 0;        /* open up to 3 windows */
+#endif
+
+    /* Don't redraw until much later. */
+    ++RedrawingDisabled;
+
+    /*
+     * When listing swap file names, don't do cursor positioning et. al.
+     */
+    if (recoverymode && fname == NULL)
+       params.want_full_screen = FALSE;
+
+    /*
+     * When certain to start the GUI, don't check capabilities of terminal.
+     * For GTK we can't be sure, but when started from the desktop it doesn't
+     * make sense to try using a terminal.
+     */
+#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
+    if (gui.starting
+# ifdef FEAT_GUI_GTK
+           && !isatty(2)
+# endif
+           )
+       params.want_full_screen = FALSE;
+#endif
+
+#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
+    /* When the GUI is started from Finder, need to display messages in a
+     * message box.  isatty(2) returns TRUE anyway, thus we need to check the
+     * name to know we're not started from a terminal. */
+    if (gui.starting && (!isatty(2) || strcmp("/dev/console",
ttyname(2)) == 0))
+    {
+       params.want_full_screen = FALSE;
+
+       /* Avoid always using "/" as the current directory.  Note that when
+        * started from Finder the arglist will be filled later in
+        * HandleODocAE() and "fname" will be NULL. */
+       if (getcwd((char *)NameBuff, MAXPATHL) != NULL
+                                               && STRCMP(NameBuff, "/") == 0)
+       {
+           if (fname != NULL)
+               (void)vim_chdirfile(fname);
+           else
+           {
+               expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
+               vim_chdir(NameBuff);
+           }
        }
-#endif
-       openfile->current_x += current_x_save;
     }
-#ifndef NANO_TINY
-    else if (openfile->mark_set) {
-       if (right_side_up) {
-           if (single_line)
-               /* Get the new data, stuff was inserted on the mark line. */
-               openfile->mark_begin = openfile->fileage;
-               /* The x is okay, it did not move. */
-       } else {
-           if (single_line) {
-               openfile->mark_begin = openfile->current;
-               openfile->mark_begin_x -= current_x_save;
-           } else
-               openfile->mark_begin_x -= openfile->current_x;
+#endif
+
+    /*
+     * mch_init() sets up the terminal (window) for use.  This must be
+     * done after resetting full_screen, otherwise it may move the cursor
+     * (MSDOS).
+     * Note that we may use mch_exit() before mch_init()!
+     */
+    mch_init();
+    TIME_MSG("shell init");
+
+#ifdef USE_XSMP
+    /*
+     * For want of anywhere else to do it, try to connect to xsmp here.
+     * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
+     * Hijacking -X 'no X connection' to also disable XSMP connection as that
+     * has a similar delay upon failure.
+     * Only try if SESSION_MANAGER is set to something non-null.
+     */
+    if (!x_no_connect)
+    {
+       char *p = getenv("SESSION_MANAGER");
+
+       if (p != NULL && *p != NUL)
+       {
+           xsmp_init();
+           TIME_MSG("xsmp init");
        }
     }
 #endif

-    /* Get the number of characters in the copied text, and add it to
-     * totsize. */
-    openfile->totsize += get_totsize(openfile->fileage,
-       openfile->filebot);
+    /*
+     * Print a warning if stdout is not a terminal.
+     */
+    check_tty(&params);

-    /* Update the current y-coordinate to account for the number of
-     * lines the copied text has, less one since the first line will be
-     * tacked onto the current line. */
-    openfile->current_y += openfile->filebot->lineno - 1;
+    /* This message comes before term inits, but after setting "silent_mode"
+     * when the input is not a tty. */
+    if (GARGCOUNT > 1 && !silent_mode)
+       printf(_("%d files to edit\n"), GARGCOUNT);

-    top_save = openfile->fileage;
-
-    /* If the top of the edit window is inside the partition, set it to
-     * where the copied text now starts. */
-    if (edittop_inside)
-       openfile->edittop = openfile->fileage;
-
-    /* Unpartition the filestruct so that it contains all the text
-     * again, plus the copied text. */
-    unpartition_filestruct(&filepart);
-
-    /* Renumber starting with the beginning line of the old
-     * partition. */
-    renumber(top_save);
+    if (params.want_full_screen && !silent_mode)
+    {
+       termcapinit(params.term);       /* set terminal name and get terminal
+                                  capabilities (will set full_screen) */
+       screen_start();         /* don't know where cursor is now */
+       TIME_MSG("Termcap init");
+    }

-    /* If the NO_NEWLINES flag isn't set, and the text doesn't end with
-     * a magicline, add a new magicline. */
-    if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
-       new_magicline();
+    /*
+     * Set the default values for the options that use Rows and Columns.
+     */
+    ui_get_shellsize();                /* inits Rows and Columns */
+    win_init_size();
+#ifdef FEAT_DIFF
+    /* Set the 'diff' option now, so that it can be checked for in a .vimrc
+     * file.  There is no buffer yet though. */
+    if (params.diff_mode)
+       diff_win_options(firstwin, FALSE);
+#endif
+
+    cmdline_row = Rows - p_ch;
+    msg_row = cmdline_row;
+    screenalloc(FALSE);                /* allocate screen buffers */
+    set_init_2();
+    TIME_MSG("inits 2");
+
+    msg_scroll = TRUE;
+    no_wait_return = TRUE;
+
+    init_mappings();           /* set up initial mappings */
+
+    init_highlight(TRUE, FALSE); /* set the default highlight groups */
+    TIME_MSG("init highlight");
+
+#ifdef FEAT_EVAL
+    /* Set the break level after the terminal is initialized. */
+    debug_break_level = params.use_debug_break_level;
+#endif
+
+#ifdef FEAT_MZSCHEME
+    /*
+     * Newer version of MzScheme (Racket) require earlier (trampolined)
+     * initialisation via scheme_main_setup.
+     * Implement this by initialising it as early as possible
+     * and splitting off remaining Vim main into vim_main2
+     */
+    {
+       /* Pack up preprocessed command line arguments.
+        * It is safe because Scheme does not access argc/argv. */
+       char *args[2];
+       args[0] = (char *)fname;
+       args[1] = (char *)&params;
+       return mzscheme_main(2, args);
+    }
 }
-
-/* Create a new openfilestruct node. */
-openfilestruct *make_new_opennode(void)
-{
-    openfilestruct *newnode =
-       (openfilestruct *)nmalloc(sizeof(openfilestruct));
-
-    newnode->filename = NULL;
-    newnode->fileage = NULL;
-    newnode->filebot = NULL;
-    newnode->edittop = NULL;
-    newnode->current = NULL;
-#ifndef NANO_TINY
-    newnode->current_stat = NULL;
-    newnode->last_action = OTHER;
-    newnode->lock_filename = NULL;
 #endif
+#endif /* NO_VIM_MAIN */

-    return newnode;
-}
-
-/* Splice a node into an existing openfilestruct. */
-void splice_opennode(openfilestruct *begin, openfilestruct *newnode,
-       openfilestruct *end)
+/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
+ * NO_VIM_MAIN is defined. */
+#ifdef FEAT_MZSCHEME
+    int
+vim_main2(int argc UNUSED, char **argv UNUSED)
 {
-    assert(newnode != NULL && begin != NULL);
-
-    newnode->next = end;
-    newnode->prev = begin;
-    begin->next = newnode;
+# ifndef NO_VIM_MAIN
+    char_u     *fname = (char_u *)argv[0];
+    mparm_T    params;

-    if (end != NULL)
-       end->prev = newnode;
+    memcpy(&params, argv[1], sizeof(params));
+# else
+    return 0;
 }
-
-/* Unlink a node from the rest of the openfilestruct, and delete it. */
-void unlink_opennode(openfilestruct *fileptr)
-{
-    assert(fileptr != NULL && fileptr->prev != NULL && fileptr->next
!= NULL && fileptr != fileptr->prev && fileptr != fileptr->next);
-
-    fileptr->prev->next = fileptr->next;
-    fileptr->next->prev = fileptr->prev;
-
-    delete_opennode(fileptr);
-}
-
-/* Delete a node from the openfilestruct. */
-void delete_opennode(openfilestruct *fileptr)
-{
-    assert(fileptr != NULL && fileptr->filename != NULL &&
fileptr->fileage != NULL);
-
-    free(fileptr->filename);
-    free_filestruct(fileptr->fileage);
-#ifndef NANO_TINY
-    if (fileptr->current_stat != NULL)
-       free(fileptr->current_stat);
+# endif
+#endif
+
+#ifndef NO_VIM_MAIN
+    /* Execute --cmd arguments. */
+    exe_pre_commands(&params);
+
+    /* Source startup scripts. */
+    source_startup_scripts(&params);
+
+#ifdef FEAT_EVAL
+    /*
+     * Read all the plugin files.
+     * Only when compiled with +eval, since most plugins need it.
+     */
+    if (p_lpl)
+    {
+# ifdef VMS    /* Somehow VMS doesn't handle the "**". */
+       source_runtime((char_u *)"plugin/*.vim", TRUE);
+# else
+       source_runtime((char_u *)"plugin/**/*.vim", TRUE);
+# endif
+       TIME_MSG("loading plugins");
+    }
 #endif

-    free(fileptr);
-}
-
-#ifdef DEBUG
-/* Deallocate all memory associated with this and later files, including
- * the lines of text. */
-void free_openfilestruct(openfilestruct *src)
-{
-    assert(src != NULL);
-
-    while (src != src->next) {
-       src = src->next;
-       delete_opennode(src->prev);
+#ifdef FEAT_DIFF
+    /* Decide about window layout for diff mode after reading vimrc. */
+    if (params.diff_mode && params.window_layout == 0)
+    {
+       if (diffopt_horizontal())
+           params.window_layout = WIN_HOR;     /* use horizontal split */
+       else
+           params.window_layout = WIN_VER;     /* use vertical split */
     }
-
-    delete_opennode(src);
-}
 #endif

-/* Display a warning about a key disabled in view mode. */
-void print_view_warning(void)
-{
-    statusbar(_("Key invalid in view mode"));
-}
-
-/* Make nano exit gracefully. */
-void finish(void)
-{
-    /* Blank the statusbar (and shortcut list, if applicable), and move
-     * the cursor to the last line of the screen. */
-    if (!ISSET(NO_HELP))
-       blank_bottombars();
-    else
-       blank_statusbar();
-    wrefresh(bottomwin);
-    endwin();
-
-    /* Restore the old terminal settings. */
-    tcsetattr(0, TCSANOW, &oldterm);
-
-#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
-    if (!no_rcfiles && ISSET(HISTORYLOG))
-       save_history();
-    if (!no_rcfiles && ISSET(POS_HISTORY)) {
-       update_poshistory(openfile->filename, openfile->current->lineno,
xplustabs()+1);
-       save_poshistory();
+    /*
+     * Recovery mode without a file name: List swap files.
+     * This uses the 'dir' option, therefore it must be after the
+     * initializations.
+     */
+    if (recoverymode && fname == NULL)
+    {
+       recover_names(NULL, TRUE, 0, NULL);
+       mch_exit(0);
     }
-#endif

-#ifdef DEBUG
-    thanks_for_all_the_fish();
+    /*
+     * Set a few option defaults after reading .vimrc files:
+     * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
+     */
+    set_init_3();
+    TIME_MSG("inits 3");
+
+    /*
+     * "-n" argument: Disable swap file by setting 'updatecount' to 0.
+     * Note that this overrides anything from a vimrc file.
+     */
+    if (params.no_swap_file)
+       p_uc = 0;
+
+#ifdef FEAT_FKMAP
+    if (curwin->w_p_rl && p_altkeymap)
+    {
+       p_hkmap = FALSE;        /* Reset the Hebrew keymap mode */
+# ifdef FEAT_ARABIC
+       curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
+# endif
+       p_fkmap = TRUE;         /* Set the Farsi keymap mode */
+    }
 #endif

-    /* Get out. */
-    exit(0);
-}
-
-/* Make nano die gracefully. */
-void die(const char *msg, ...)
-{
-    va_list ap;
-
-    endwin();
-
-    /* Restore the old terminal settings. */
-    tcsetattr(0, TCSANOW, &oldterm);
-
-    va_start(ap, msg);
-    vfprintf(stderr, msg, ap);
-    va_end(ap);
-
-    /* Save the current file buffer if it's been modified. */
-    if (openfile && openfile->modified) {
-       /* If we've partitioned the filestruct, unpartition it now. */
-       if (filepart != NULL)
-           unpartition_filestruct(&filepart);
-
-       die_save_file(openfile->filename
-#ifndef NANO_TINY
-               , openfile->current_stat
+#ifdef FEAT_GUI
+    if (gui.starting)
+    {
+#if defined(UNIX) || defined(VMS)
+       /* When something caused a message from a vimrc script, need to output
+        * an extra newline before the shell prompt. */
+       if (did_emsg || msg_didout)
+           putchar('\n');
 #endif
-               );
-    }
-
-#ifdef ENABLE_MULTIBUFFER
-    /* Save all of the other modified file buffers, if any. */
-    if (openfile != NULL) {
-       openfilestruct *tmp = openfile;

-       while (tmp != openfile->next) {
-           openfile = openfile->next;
+       gui_start();            /* will set full_screen to TRUE */
+       TIME_MSG("starting GUI");

-           /* Save the current file buffer if it's been modified. */
-           if (openfile->modified)
-               die_save_file(openfile->filename
-#ifndef NANO_TINY
-                       , openfile->current_stat
-#endif
-                       );
-       }
+       /* When running "evim" or "gvim -y" we need the menus, exit if we
+        * don't have them. */
+       if (!gui.in_use && params.evim_mode)
+           mch_exit(1);
     }
 #endif

-    /* Get out. */
-    exit(1);
-}
+#ifdef SPAWNO          /* special MSDOS swapping library */
+    init_SPAWNO("", SWAP_ANY);
+#endif

-/* Save the current file under the name spacified in die_filename, which
- * is modified to be unique if necessary. */
-void die_save_file(const char *die_filename
-#ifndef NANO_TINY
-       , struct stat *die_stat
+#ifdef FEAT_VIMINFO
+    /*
+     * Read in registers, history etc, but not marks, from the viminfo file.
+     * This is where v:oldfiles gets filled.
+     */
+    if (*p_viminfo != NUL)
+    {
+       read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
+       TIME_MSG("reading viminfo");
+    }
 #endif
-       )
-{
-    char *retval;
-    bool failed = TRUE;
-
-    /* If we're using restricted mode, don't write any emergency backup
-     * files, since that would allow reading from or writing to files
-     * not specified on the command line. */
-    if (ISSET(RESTRICTED))
-       return;
-
-    /* If we can't save, we have really bad problems, but we might as
-     * well try. */
-    if (*die_filename == '\0')
-       die_filename = "nano";
-
-    retval = get_next_filename(die_filename, ".save");
-    if (retval[0] != '\0')
-       failed = !write_file(retval, NULL, TRUE, OVERWRITE, TRUE);
-
-    if (!failed)
-       fprintf(stderr, _("\nBuffer written to %s\n"), retval);
-    else if (retval[0] != '\0')
-       fprintf(stderr, _("\nBuffer not written to %s: %s\n"), retval,
-               strerror(errno));
-    else
-       fprintf(stderr, _("\nBuffer not written: %s\n"),
-               _("Too many backup files?"));
-
-#ifndef NANO_TINY
-    /* Try and chmod/chown the save file to the values of the
original file, but
-       dont worry if it fails because we're supposed to be bailing as fast
-       as possible. */
-    if (die_stat) {
-       int shush;
-       shush = chmod(retval, die_stat->st_mode);
-       shush = chown(retval, die_stat->st_uid, die_stat->st_gid);
+#ifdef FEAT_EVAL
+    /* It's better to make v:oldfiles an empty list than NULL. */
+    if (get_vim_var_list(VV_OLDFILES) == NULL)
+       set_vim_var_list(VV_OLDFILES, list_alloc());
+#endif
+
+#ifdef FEAT_QUICKFIX
+    /*
+     * "-q errorfile": Load the error file now.
+     * If the error file can't be read, exit before doing anything else.
+     */
+    if (params.edit_type == EDIT_QF)
+    {
+       if (params.use_ef != NULL)
+           set_string_option_direct((char_u *)"ef", -1,
+                                          params.use_ef, OPT_FREE, SID_CARG);
+       vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
+       if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
+       {
+           out_char('\n');
+           mch_exit(3);
+       }
+       TIME_MSG("reading errorfile");
     }
 #endif

-    free(retval);
-}
-
-/* Initialize the three window portions nano uses. */
-void window_init(void)
-{
-    /* If the screen height is too small, get out. */
-    editwinrows = LINES - 5 + no_more_space() + no_help();
-    if (COLS < MIN_EDITOR_COLS || editwinrows < MIN_EDITOR_ROWS)
-       die(_("Window size is too small for nano...\n"));
-
-#ifndef DISABLE_WRAPJUSTIFY
-    /* Set up fill, based on the screen width. */
-    fill = wrap_at;
-    if (fill <= 0)
-       fill += COLS;
-    if (fill < 0)
-       fill = 0;
-#endif
-
-    if (topwin != NULL)
-       delwin(topwin);
-    if (edit != NULL)
-       delwin(edit);
-    if (bottomwin != NULL)
-       delwin(bottomwin);
-
-    /* Set up the windows. */
-    topwin = newwin(2 - no_more_space(), COLS, 0, 0);
-    edit = newwin(editwinrows, COLS, 2 - no_more_space(), 0);
-    bottomwin = newwin(3 - no_help(), COLS, editwinrows + (2 -
-       no_more_space()), 0);
-
-    /* Turn the keypad on for the windows, if necessary. */
-    if (!ISSET(REBIND_KEYPAD)) {
-       keypad(topwin, TRUE);
-       keypad(edit, TRUE);
-       keypad(bottomwin, TRUE);
+    /*
+     * Start putting things on the screen.
+     * Scroll screen down before drawing over it
+     * Clear screen now, so file message will not be cleared.
+     */
+    starting = NO_BUFFERS;
+    no_wait_return = FALSE;
+    if (!exmode_active)
+       msg_scroll = FALSE;
+
+#ifdef FEAT_GUI
+    /*
+     * This seems to be required to make callbacks to be called now, instead
+     * of after things have been put on the screen, which then may be deleted
+     * when getting a resize callback.
+     * For the Mac this handles putting files dropped on the Vim icon to
+     * global_alist.
+     */
+    if (gui.in_use)
+    {
+# ifdef FEAT_SUN_WORKSHOP
+       if (!usingSunWorkShop)
+# endif
+           gui_wait_for_chars(50L);
+       TIME_MSG("GUI delay");
     }
-}
-
-#ifndef DISABLE_MOUSE
-/* Disable mouse support. */
-void disable_mouse_support(void)
-{
-    mousemask(0, NULL);
-    mouseinterval(oldinterval);
-}
-
-/* Enable mouse support. */
-void enable_mouse_support(void)
-{
-    mousemask(ALL_MOUSE_EVENTS, NULL);
-    oldinterval = mouseinterval(50);
-}
-
-/* Initialize mouse support.  Enable it if the USE_MOUSE flag is set,
- * and disable it otherwise. */
-void mouse_init(void)
-{
-    if (ISSET(USE_MOUSE))
-       enable_mouse_support();
-    else
-       disable_mouse_support();
-}
-#endif /* !DISABLE_MOUSE */
-
-#ifdef HAVE_GETOPT_LONG
-#define print_opt(shortflag, longflag, desc)
print_opt_full(shortflag, longflag, desc)
-#else
-#define print_opt(shortflag, longflag, desc) print_opt_full(shortflag, desc)
 #endif

-/* Print one usage string to the screen.  This cuts down on duplicate
- * strings to translate, and leaves out the parts that shouldn't be
- * translatable (i.e. the flag names). */
-void print_opt_full(const char *shortflag
-#ifdef HAVE_GETOPT_LONG
-       , const char *longflag
+#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
+    qnx_clip_init();
 #endif
-       , const char *desc)
-{
-    printf(" %s\t", shortflag);
-    if (strlenpt(shortflag) < 8)
-       printf("\t");

-#ifdef HAVE_GETOPT_LONG
-    printf("%s\t", longflag);
-    if (strlenpt(longflag) < 8)
-       printf("\t\t");
-    else if (strlenpt(longflag) < 16)
-       printf("\t");
+#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
+    clip_init(TRUE);
 #endif

-    if (desc != NULL)
-       printf("%s", _(desc));
-    printf("\n");
-}
-
-/* Explain how to properly use nano and its command line options. */
-void usage(void)
-{
-    printf(_("Usage: nano [OPTIONS] [[+LINE,COLUMN] FILE]...\n\n"));
-    printf(
-#ifdef HAVE_GETOPT_LONG
-       _("Option\t\tGNU long option\t\tMeaning\n")
-#else
-       _("Option\t\tMeaning\n")
-#endif
-       );
-    print_opt("-h, -?", "--help", N_("Show this message"));
-    print_opt(_("+LINE,COLUMN"), "",
-       N_("Start at line LINE, column COLUMN"));
-#ifndef NANO_TINY
-    print_opt("-A", "--smarthome", N_("Enable smart home key"));
-    print_opt("-B", "--backup", N_("Save backups of existing files"));
-    print_opt(_("-C <dir>"), _("--backupdir=<dir>"),
-       N_("Directory for saving unique backup files"));
-#endif
-    print_opt("-D", "--boldtext",
-       N_("Use bold instead of reverse video text"));
-#ifndef NANO_TINY
-    print_opt("-E", "--tabstospaces",
-       N_("Convert typed tabs to spaces"));
-#endif
-#ifdef ENABLE_MULTIBUFFER
-    print_opt("-F", "--multibuffer", N_("Enable multiple file buffers"));
-#endif
-#ifdef ENABLE_NANORC
-#ifndef NANO_TINY
-    print_opt("-G", "--locking",
-       N_("Use (vim-style) lock files"));
-    print_opt("-H", "--historylog",
-       N_("Log & read search/replace string history"));
-#endif
-    print_opt("-I", "--ignorercfiles",
-       N_("Don't look at nanorc files"));
-#endif
-    print_opt("-K", "--rebindkeypad",
-       N_("Fix numeric keypad key confusion problem"));
-    print_opt("-L", "--nonewlines",
-       N_("Don't add newlines to the ends of files"));
-#ifndef NANO_TINY
-    print_opt("-N", "--noconvert",
-       N_("Don't convert files from DOS/Mac format"));
-#endif
-    print_opt("-O", "--morespace", N_("Use one more line for editing"));
-#ifndef NANO_TINY
-    print_opt("-P", "--poslog",
-       N_("Log & read location of cursor position"));
-#endif
-#ifndef DISABLE_JUSTIFY
-    print_opt(_("-Q <str>"), _("--quotestr=<str>"),
-       N_("Quoting string"));
-#endif
-    print_opt("-R", "--restricted", N_("Restricted mode"));
-#ifndef NANO_TINY
-    print_opt("-S", "--smooth",
-       N_("Scroll by line instead of half-screen"));
-#endif
-    print_opt(_("-T <#cols>"), _("--tabsize=<#cols>"),
-       N_("Set width of a tab to #cols columns"));
-#ifndef NANO_TINY
-    print_opt("-U", "--quickblank", N_("Do quick statusbar blanking"));
-#endif
-    print_opt("-V", "--version",
-       N_("Print version information and exit"));
-#ifndef NANO_TINY
-    print_opt("-W", "--wordbounds",
-       N_("Detect word boundaries more accurately"));
-#endif
-#ifdef ENABLE_COLOR
-    print_opt(_("-Y <str>"), _("--syntax=<str>"),
-       N_("Syntax definition to use for coloring"));
-#endif
-    print_opt("-c", "--const", N_("Constantly show cursor position"));
-    print_opt("-d", "--rebinddelete",
-       N_("Fix Backspace/Delete confusion problem"));
-#ifndef NANO_TINY
-    print_opt("-i", "--autoindent",
-       N_("Automatically indent new lines"));
-    print_opt("-k", "--cut", N_("Cut from cursor to end of line"));
-#endif
-    print_opt("-l", "--nofollow",
-       N_("Don't follow symbolic links, overwrite"));
-#ifndef DISABLE_MOUSE
-    print_opt("-m", "--mouse", N_("Enable the use of the mouse"));
-#endif
-#ifndef DISABLE_OPERATINGDIR
-    print_opt(_("-o <dir>"), _("--operatingdir=<dir>"),
-       N_("Set operating directory"));
-#endif
-    print_opt("-p", "--preserve",
-       N_("Preserve XON (^Q) and XOFF (^S) keys"));
-    print_opt("-q", "--quiet",
-       N_("Silently ignore startup issues like rc file errors"));
-#ifndef DISABLE_WRAPJUSTIFY
-    print_opt(_("-r <#cols>"), _("--fill=<#cols>"),
-       N_("Set wrapping point at column #cols"));
-#endif
-#ifndef DISABLE_SPELLER
-    print_opt(_("-s <prog>"), _("--speller=<prog>"),
-       N_("Enable alternate speller"));
-#endif
-    print_opt("-t", "--tempfile",
-       N_("Auto save on exit, don't prompt"));
-#ifndef NANO_TINY
-    print_opt("-u", "--undo", N_("Allow generic undo [EXPERIMENTAL]"));
-#endif
-
-    print_opt("-v", "--view", N_("View mode (read-only)"));
-#ifndef DISABLE_WRAPPING
-    print_opt("-w", "--nowrap", N_("Don't wrap long lines"));
-#endif
-    print_opt("-x", "--nohelp", N_("Don't show the two help lines"));
-    print_opt("-z", "--suspend", N_("Enable suspension"));
-#ifndef NANO_TINY
-    print_opt("-$", "--softwrap", N_("Enable soft line wrapping"));
-#endif
-
-    /* This is a special case. */
-    print_opt("-a, -b, -e,", "", NULL);
-    print_opt("-f, -g, -j", "", N_("(ignored, for Pico compatibility)"));
-
-    exit(0);
-}
-
-/* Display the current version of nano, the date and time it was
- * compiled, contact information for it, and the configuration options
- * it was compiled with. */
-void version(void)
-{
-    printf(_(" GNU nano version %s (compiled %s, %s)\n"), VERSION,
-       __TIME__, __DATE__);
-    printf(" (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,\n");
-    printf(" 2008, 2009 Free Software Foundation, Inc.\n");
-    printf(
-       _(" Email: address@hidden       Web: http://www.nano-editor.org/";));
-    printf(_("\n Compiled options:"));
-
-#ifdef DISABLE_BROWSER
-    printf(" --disable-browser");
-#endif
-#ifdef DISABLE_HELP
-    printf(" --disable-help");
-#endif
-#ifdef DISABLE_JUSTIFY
-    printf(" --disable-justify");
-#endif
-#ifdef DISABLE_MOUSE
-    printf(" --disable-mouse");
-#endif
-#ifndef ENABLE_NLS
-    printf(" --disable-nls");
-#endif
-#ifdef DISABLE_OPERATINGDIR
-    printf(" --disable-operatingdir");
-#endif
-#ifdef DISABLE_SPELLER
-    printf(" --disable-speller");
-#endif
-#ifdef DISABLE_TABCOMP
-    printf(" --disable-tabcomp");
-#endif
-#ifdef DISABLE_WRAPPING
-    printf(" --disable-wrapping");
-#endif
-#ifdef DISABLE_ROOTWRAPPING
-    printf(" --disable-wrapping-as-root");
-#endif
-#ifdef ENABLE_COLOR
-    printf(" --enable-color");
-#endif
-#ifdef DEBUG
-    printf(" --enable-debug");
-#endif
-#ifdef NANO_EXTRA
-    printf(" --enable-extra");
-#endif
-#ifdef ENABLE_MULTIBUFFER
-    printf(" --enable-multibuffer");
-#endif
-#ifdef ENABLE_NANORC
-    printf(" --enable-nanorc");
-#endif
-#ifdef NANO_TINY
-    printf(" --enable-tiny");
-#endif
-#ifdef ENABLE_UTF8
-    printf(" --enable-utf8");
-#endif
-#ifdef USE_SLANG
-    printf(" --with-slang");
+#ifdef FEAT_XCLIPBOARD
+    /* Start using the X clipboard, unless the GUI was started. */
+# ifdef FEAT_GUI
+    if (!gui.in_use)
+# endif
+    {
+       setup_term_clip();
+       TIME_MSG("setup clipboard");
+    }
 #endif
-    printf("\n");
-}
-
-/* Return 1 if the MORE_SPACE flag is set, and 0 otherwise.  This is
- * used to calculate the relative screen position while taking this flag
- * into account, since it adds one line to the edit window. */
-int no_more_space(void)
-{
-    return ISSET(MORE_SPACE) ? 1 : 0;
-}

-/* Return 2 if the NO_HELP flag is set, and 0 otherwise.  This is used
- * to calculate the relative screen position while taking this flag into
- * account, since it removes two lines from the edit window. */
-int no_help(void)
-{
-    return ISSET(NO_HELP) ? 2 : 0;
-}
-
-/* Indicate a disabled function on the statusbar. */
-void nano_disabled_msg(void)
-{
-    statusbar(_("Sorry, support for this function has been disabled"));
-}
-
-/* If the current file buffer has been modified, and the TEMP_FILE flag
- * isn't set, ask whether or not to save the file buffer.  If the
- * TEMP_FILE flag is set, save it unconditionally.  Then, if more than
- * one file buffer is open, close the current file buffer and switch to
- * the next one.  If only one file buffer is open, exit from nano. */
-void do_exit(void)
-{
-    int i;
-
-    /* If the file hasn't been modified, pretend the user chose not to
-     * save. */
-    if (!openfile->modified)
-       i = 0;
-    /* If the TEMP_FILE flag is set, pretend the user chose to save. */
-    else if (ISSET(TEMP_FILE))
-       i = 1;
-    /* Otherwise, ask the user whether or not to save. */
-    else
-       i = do_yesno_prompt(FALSE,
-               _("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) 
? "));
+#ifdef FEAT_CLIENTSERVER
+    /* Prepare for being a Vim server. */
+    prepare_server(&params);
+#endif
+
+    /*
+     * If "-" argument given: Read file from stdin.
+     * Do this before starting Raw mode, because it may change things that the
+     * writing end of the pipe doesn't like, e.g., in case stdin and stderr
+     * are the same terminal: "cat | vim -".
+     * Using autocommands here may cause trouble...
+     */
+    if (params.edit_type == EDIT_STDIN && !recoverymode)
+       read_stdin();
+
+#if defined(UNIX) || defined(VMS)
+    /* When switching screens and something caused a message from a vimrc
+     * script, need to output an extra newline on exit. */
+    if ((did_emsg || msg_didout) && *T_TI != NUL)
+       newline_on_exit = TRUE;
+#endif
+
+    /*
+     * When done something that is not allowed or error message call
+     * wait_return.  This must be done before starttermcap(), because it may
+     * switch to another screen. It must be done after settmode(TMODE_RAW),
+     * because we want to react on a single key stroke.
+     * Call settmode and starttermcap here, so the T_KS and T_TI may be
+     * defined by termcapinit and redefined in .exrc.
+     */
+    settmode(TMODE_RAW);
+    TIME_MSG("setting raw mode");
+
+    if (need_wait_return || msg_didany)
+    {
+       wait_return(TRUE);
+       TIME_MSG("waiting for return");
+    }

-#ifdef DEBUG
-    dump_filestruct(openfile->fileage);
+    starttermcap();        /* start termcap if not done by wait_return() */
+    TIME_MSG("start termcap");
+#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
+    may_req_ambiguous_char_width();
 #endif

-    /* If the user chose not to save, or if the user chose to save and
-     * the save succeeded, we're ready to exit. */
-    if (i == 0 || (i == 1 && do_writeout(TRUE))) {
-
-#ifndef NANO_TINY
-        if (ISSET(LOCKING) && openfile->lock_filename)
-            delete_lockfile(openfile->lock_filename);
+#ifdef FEAT_MOUSE
+    setmouse();                                /* may start using the mouse */
 #endif
+    if (scroll_region)
+       scroll_region_reset();          /* In case Rows changed */
+    scroll_start();    /* may scroll the screen to the right position */

-#ifdef ENABLE_MULTIBUFFER
-       /* Exit only if there are no more open file buffers. */
-       if (!close_buffer())
+    /*
+     * Don't clear the screen when starting in Ex mode, unless using the GUI.
+     */
+    if (exmode_active
+#ifdef FEAT_GUI
+                       && !gui.in_use
 #endif
-           finish();
-    /* If the user canceled, we go on. */
-    } else if (i != 1)
-       statusbar(_("Cancelled"));
-
-    shortcut_init(FALSE);
-    display_main_list();
-}
-
-/* Another placeholder for function mapping */
-void do_cancel(void)
-{
-    ;
-}
-
-static struct sigaction pager_oldaction, pager_newaction;  /*
Original and temporary handlers for SIGINT. */
-static bool pager_sig_failed = FALSE; /* Did sigaction() fail without
changing the signal handlers? */
-static bool pager_input_aborted = FALSE; /* Did someone invoke the
pager and abort it via ^C? */
-
-/* Things which need to be run regardless of whether
- * we finished the stdin pipe correctly or not. */
-void finish_stdin_pager(void)
-{
-    FILE *f;
-    int ttystdin;
-
-    /* Read whatever we did get from stdin. */
-    f = fopen("/dev/stdin", "rb");
-       if (f == NULL)
-        nperror("fopen");
-
-    read_file(f, 0, "stdin", TRUE, FALSE);
-    ttystdin = open("/dev/tty", O_RDONLY);
-    if (!ttystdin)
-        die(_("Couldn't reopen stdin from keyboard, sorry\n"));
-
-    dup2(ttystdin,0);
-    close(ttystdin);
-    if (!pager_input_aborted)
-       tcgetattr(0, &oldterm);
-    if (!pager_sig_failed && sigaction(SIGINT, &pager_oldaction, NULL) == -1)
-        nperror("sigaction");
-    terminal_init();
-    doupdate();
-}
-
-/* Cancel reading from stdin like a pager. */
-RETSIGTYPE cancel_stdin_pager(int signal)
-{
-    /* Currently do nothing, just handle the intr silently. */
-    pager_input_aborted = TRUE;
-}
-
-/* Let nano read stdin for the first file at least. */
-void stdin_pager(void)
-{
-    endwin();
-    if (!pager_input_aborted)
-       tcsetattr(0, TCSANOW, &oldterm);
-    fprintf(stderr, _("Reading from stdin, ^C to abort\n"));
-
-    /* Set things up so that Ctrl-C will cancel the new process. */
-    /* Enable interpretation of the special control keys so that
-     * we get SIGINT when Ctrl-C is pressed. */
-#ifndef NANO_TINY
-    enable_signals();
-#endif
-
-    if (sigaction(SIGINT, NULL, &pager_newaction) == -1) {
-       pager_sig_failed = TRUE;
-       nperror("sigaction");
-    } else {
-       pager_newaction.sa_handler = cancel_stdin_pager;
-       if (sigaction(SIGINT, &pager_newaction, &pager_oldaction) == -1) {
-           pager_sig_failed = TRUE;
-           nperror("sigaction");
-       }
+                                       )
+       must_redraw = CLEAR;
+    else
+    {
+       screenclear();                  /* clear screen */
+       TIME_MSG("clearing screen");
     }

-    open_buffer("", FALSE);
-    finish_stdin_pager();
-}
-
-/* Initialize the signal handlers. */
-void signal_init(void)
-{
-    /* Trap SIGINT and SIGQUIT because we want them to do useful
-     * things. */
-    memset(&act, 0, sizeof(struct sigaction));
-    act.sa_handler = SIG_IGN;
-    sigaction(SIGINT, &act, NULL);
-    sigaction(SIGQUIT, &act, NULL);
-
-    /* Trap SIGHUP and SIGTERM because we want to write the file out. */
-    act.sa_handler = handle_hupterm;
-    sigaction(SIGHUP, &act, NULL);
-    sigaction(SIGTERM, &act, NULL);
-
-#ifndef NANO_TINY
-    /* Trap SIGWINCH because we want to handle window resizes. */
-    act.sa_handler = handle_sigwinch;
-    sigaction(SIGWINCH, &act, NULL);
-    allow_pending_sigwinch(FALSE);
-#endif
-
-    /* Trap normal suspend (^Z) so we can handle it ourselves. */
-    if (!ISSET(SUSPEND)) {
-       act.sa_handler = SIG_IGN;
-       sigaction(SIGTSTP, &act, NULL);
-    } else {
-       /* Block all other signals in the suspend and continue handlers.
-        * If we don't do this, other stuff interrupts them! */
-       sigfillset(&act.sa_mask);
-
-       act.sa_handler = do_suspend;
-       sigaction(SIGTSTP, &act, NULL);
-
-       act.sa_handler = do_continue;
-       sigaction(SIGCONT, &act, NULL);
+#ifdef FEAT_CRYPT
+    if (params.ask_for_key)
+    {
+       (void)blowfish_self_test();
+       (void)get_crypt_key(TRUE, TRUE);
+       TIME_MSG("getting crypt key");
     }
-}
-
-/* Handler for SIGHUP (hangup) and SIGTERM (terminate). */
-RETSIGTYPE handle_hupterm(int signal)
-{
-    die(_("Received SIGHUP or SIGTERM\n"));
-}
-
-/* Handler for SIGTSTP (suspend). */
-RETSIGTYPE do_suspend(int signal)
-{
-
-    if (ISSET(RESTRICTED)) {
-        nano_disabled_msg();
-       return;
-    }
-
-#ifndef DISABLE_MOUSE
-    /* Turn mouse support off. */
-    disable_mouse_support();
 #endif

-    /* Move the cursor to the last line of the screen. */
-    move(LINES - 1, 0);
-    endwin();
-
-    /* Display our helpful message. */
-    printf(_("Use \"fg\" to return to nano.\n"));
-    fflush(stdout);
-
-    /* Restore the old terminal settings. */
-    tcsetattr(0, TCSANOW, &oldterm);
-
-    /* Trap SIGHUP and SIGTERM so we can properly deal with them while
-     * suspended. */
-    act.sa_handler = handle_hupterm;
-    sigaction(SIGHUP, &act, NULL);
-    sigaction(SIGTERM, &act, NULL);
-
-    /* Do what mutt does: send ourselves a SIGSTOP. */
-    kill(0, SIGSTOP);
-}
+    no_wait_return = TRUE;

-/* the subnfunc version */
-void do_suspend_void(void)
-{
-    if (ISSET(SUSPEND))
-       do_suspend(0);
-}
+    /*
+     * Create the requested number of windows and edit buffers in them.
+     * Also does recovery if "recoverymode" set.
+     */
+    create_windows(&params);
+    TIME_MSG("opening buffers");

-/* Handler for SIGCONT (continue after suspend). */
-RETSIGTYPE do_continue(int signal)
-{
-#ifndef DISABLE_MOUSE
-    /* Turn mouse support back on if it was on before. */
-    if (ISSET(USE_MOUSE))
-       enable_mouse_support();
+#ifdef FEAT_EVAL
+    /* clear v:swapcommand */
+    set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
 #endif

-#ifndef NANO_TINY
-    /* Perhaps the user resized the window while we slept.  Handle it,
-     * and restore the terminal to its previous state and update the
-     * screen in the process. */
-    handle_sigwinch(0);
-#else
-    /* Restore the terminal to its previous state. */
-    terminal_init();
+    /* Ex starts at last line of the file */
+    if (exmode_active)
+       curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;

-    /* Turn the cursor back on for sure. */
-    curs_set(1);
-
-    /* Redraw the contents of the windows that need it. */
-    blank_statusbar();
-    wnoutrefresh(bottomwin);
-    total_refresh();
+#ifdef FEAT_AUTOCMD
+    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
+    TIME_MSG("BufEnter autocommands");
 #endif
-}
+    setpcmark();

-#ifndef NANO_TINY
-/* Handler for SIGWINCH (window size change). */
-RETSIGTYPE handle_sigwinch(int signal)
-{
-    const char *tty = ttyname(0);
-    int fd, result = 0;
-    struct winsize win;
-
-    if (tty == NULL)
-       return;
-    fd = open(tty, O_RDWR);
-    if (fd == -1)
-       return;
-    result = ioctl(fd, TIOCGWINSZ, &win);
-    close(fd);
-    if (result == -1)
-       return;
-
-    /* We could check whether the COLS or LINES changed, and return
-     * otherwise.  However, COLS and LINES are curses global variables,
-     * and in some cases curses has already updated them.  But not in
-     * all cases.  Argh. */
-#ifdef REDEFINING_MACROS_OK
-    COLS = win.ws_col;
-    LINES = win.ws_row;
-#endif
-
-    /* If we've partitioned the filestruct, unpartition it now. */
-    if (filepart != NULL)
-       unpartition_filestruct(&filepart);
-
-#ifdef USE_SLANG
-    /* Slang curses emulation brain damage, part 1: If we just do what
-     * curses does here, it'll only work properly if the resize made the
-     * window smaller.  Do what mutt does: Leave and immediately reenter
-     * Slang screen management mode. */
-    SLsmg_reset_smg();
-    SLsmg_init_smg();
-#else
-    /* Do the equivalent of what Minimum Profit does: Leave and
-     * immediately reenter curses mode. */
-    endwin();
-    doupdate();
+#ifdef FEAT_QUICKFIX
+    /*
+     * When started with "-q errorfile" jump to first error now.
+     */
+    if (params.edit_type == EDIT_QF)
+    {
+       qf_jump(NULL, 0, 0, FALSE);
+       TIME_MSG("jump to first error");
+    }
 #endif

-    /* Restore the terminal to its previous state. */
-    terminal_init();
-
-    /* Turn the cursor back on for sure. */
-    curs_set(1);
-
-    /* Do the equivalent of what both mutt and Minimum Profit do:
-     * Reinitialize all the windows based on the new screen
-     * dimensions. */
-    window_init();
-
-    /* Redraw the contents of the windows that need it. */
-    blank_statusbar();
-    wnoutrefresh(bottomwin);
-    currmenu = MMAIN;
-    total_refresh();
-
-    /* Jump back to either main() or the unjustify routine in
-     * do_justify(). */
-    siglongjmp(jump_buf, 1);
-}
-
-/* If allow is TRUE, block any SIGWINCH signals that we get, so that we
- * can deal with them later.  If allow is FALSE, unblock any SIGWINCH
- * signals that we have, so that we can deal with them now. */
-void allow_pending_sigwinch(bool allow)
-{
-    sigset_t winch;
-    sigemptyset(&winch);
-    sigaddset(&winch, SIGWINCH);
-    sigprocmask(allow ? SIG_UNBLOCK : SIG_BLOCK, &winch, NULL);
-}
-#endif /* !NANO_TINY */
-
-#ifndef NANO_TINY
-/* Handle the global toggle specified in flag. */
-void do_toggle(int flag)
-{
-    bool enabled;
-    const char *desc;
-
-    TOGGLE(flag);
-
-    switch (flag) {
-#ifndef DISABLE_MOUSE
-       case USE_MOUSE:
-           mouse_init();
-           break;
+#ifdef FEAT_WINDOWS
+    /*
+     * If opened more than one window, start editing files in the other
+     * windows.
+     */
+    edit_buffers(&params);
 #endif
-       case MORE_SPACE:
-       case NO_HELP:
-           window_init();
-           total_refresh();
-           break;
-       case SUSPEND:
-           signal_init();
-           break;
-#ifdef ENABLE_NANORC
-       case WHITESPACE_DISPLAY:
-           titlebar(NULL);
-           edit_refresh();
-           break;
-#endif
-#ifdef ENABLE_COLOR
-       case NO_COLOR_SYNTAX:
-           edit_refresh();
-           break;
-#endif
-       case SOFTWRAP:
-           total_refresh();
-           break;
-    }

-    enabled = ISSET(flag);
+#ifdef FEAT_DIFF
+    if (params.diff_mode)
+    {
+       win_T   *wp;

-    if (flag == NO_HELP
-#ifndef DISABLE_WRAPPING
-       || flag == NO_WRAP
-#endif
-#ifdef ENABLE_COLOR
-       || flag == NO_COLOR_SYNTAX
+       /* set options in each window for "vimdiff". */
+       for (wp = firstwin; wp != NULL; wp = wp->w_next)
+           diff_win_options(wp, TRUE);
+    }
 #endif
-       )
-       enabled = !enabled;
-
-    desc = (char *) _(flagtostr(flag));
-    statusbar("%s %s", desc, enabled ? _("enabled") :
-       _("disabled"));
-}

-/* Bleh */
-void do_toggle_void(void)
-{
-;
-}
-#endif /* !NANO_TINY */
+    /*
+     * Shorten any of the filenames, but only when absolute.
+     */
+    shorten_fnames(FALSE);

-/* Disable extended input and output processing in our terminal
- * settings. */
-void disable_extended_io(void)
-{
-    struct termios term;
+    /*
+     * Need to jump to the tag before executing the '-c command'.
+     * Makes "vim -c '/return' -t main" work.
+     */
+    if (params.tagname != NULL)
+    {
+#if defined(HAS_SWAP_EXISTS_ACTION)
+       swap_exists_did_quit = FALSE;
+#endif

-    tcgetattr(0, &term);
-    term.c_lflag &= ~IEXTEN;
-    term.c_oflag &= ~OPOST;
-    tcsetattr(0, TCSANOW, &term);
-}
+       vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
+       do_cmdline_cmd(IObuff);
+       TIME_MSG("jumping to tag");

-/* Disable interpretation of the special control keys in our terminal
- * settings. */
-void disable_signals(void)
-{
-    struct termios term;
+#if defined(HAS_SWAP_EXISTS_ACTION)
+       /* If the user doesn't want to edit the file then we quit here. */
+       if (swap_exists_did_quit)
+           getout(1);
+#endif
+    }

-    tcgetattr(0, &term);
-    term.c_lflag &= ~ISIG;
-    tcsetattr(0, TCSANOW, &term);
-}
+    /* Execute any "+", "-c" and "-S" arguments. */
+    if (params.n_commands > 0)
+       exe_commands(&params);

-#ifndef NANO_TINY
-/* Enable interpretation of the special control keys in our terminal
- * settings. */
-void enable_signals(void)
-{
-    struct termios term;
+    RedrawingDisabled = 0;
+    redraw_all_later(NOT_VALID);
+    no_wait_return = FALSE;
+    starting = 0;

-    tcgetattr(0, &term);
-    term.c_lflag |= ISIG;
-    tcsetattr(0, TCSANOW, &term);
-}
+#ifdef FEAT_TERMRESPONSE
+    /* Requesting the termresponse is postponed until here, so that a "-c q"
+     * argument doesn't make it appear in the shell Vim was started from. */
+    may_req_termresponse();
 #endif

-/* Disable interpretation of the flow control characters in our terminal
- * settings. */
-void disable_flow_control(void)
-{
-    struct termios term;
+    /* start in insert mode */
+    if (p_im)
+       need_start_insertmode = TRUE;

-    tcgetattr(0, &term);
-    term.c_iflag &= ~IXON;
-    tcsetattr(0, TCSANOW, &term);
-}
+#ifdef FEAT_AUTOCMD
+    apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
+    TIME_MSG("VimEnter autocommands");
+#endif

-/* Enable interpretation of the flow control characters in our terminal
- * settings. */
-void enable_flow_control(void)
-{
-    struct termios term;
+#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
+    /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
+     * done after the clipboard is available and all initial commands that may
+     * modify the 'clipboard' setting have run; i.e. just before entering the
+     * main loop. */
+    {
+       int default_regname = 0;
+       adjust_clip_reg(&default_regname);
+       set_reg_var(default_regname);
+    }
+#endif

-    tcgetattr(0, &term);
-    term.c_iflag |= IXON;
-    tcsetattr(0, TCSANOW, &term);
-}
+#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
+    /* When a startup script or session file setup for diff'ing and
+     * scrollbind, sync the scrollbind now. */
+    if (curwin->w_p_diff && curwin->w_p_scb)
+    {
+       update_topline();
+       check_scrollbind((linenr_T)0, 0L);
+       TIME_MSG("diff scrollbinding");
+    }
+#endif

-/* Set up the terminal state.  Put the terminal in raw mode (read one
- * character at a time, disable the special control keys, and disable
- * the flow control characters), disable translation of carriage return
- * (^M) into newline (^J) so that we can tell the difference between the
- * Enter key and Ctrl-J, and disable echoing of characters as they're
- * typed.  Finally, disable extended input and output processing, and,
- * if we're not in preserve mode, reenable interpretation of the flow
- * control characters. */
-void terminal_init(void)
-{
-#ifdef USE_SLANG
-    /* Slang curses emulation brain damage, part 2: Slang doesn't
-     * implement raw(), nonl(), or noecho() properly, so there's no way
-     * to properly reinitialize the terminal using them.  We have to
-     * disable the special control keys and interpretation of the flow
-     * control characters using termios, save the terminal state after
-     * the first call, and restore it on subsequent calls. */
-    static struct termios newterm;
-    static bool newterm_set = FALSE;
-
-    if (!newterm_set) {
-#endif
-
-       raw();
-       nonl();
-       noecho();
-       disable_extended_io();
-       if (ISSET(PRESERVE))
-           enable_flow_control();
-
-       disable_signals();
-#ifdef USE_SLANG
-       if (!ISSET(PRESERVE))
-           disable_flow_control();
-
-       tcgetattr(0, &newterm);
-       newterm_set = TRUE;
-    } else
-       tcsetattr(0, TCSANOW, &newterm);
+#if defined(WIN3264) && !defined(FEAT_GUI_W32)
+    mch_set_winsize_now();         /* Allow winsize changes from now on */
 #endif
-}

-/* Read in a character, interpret it as a shortcut or toggle if
- * necessary, and return it.  Set meta_key to TRUE if the character is a
- * meta sequence, set func_key to TRUE if the character is a function
- * key, set s_or_t to TRUE if the character is a shortcut or toggle
- * key, set ran_func to TRUE if we ran a function associated with a
- * shortcut key, and set finished to TRUE if we're done after running
- * or trying to run a function associated with a shortcut key.  If
- * allow_funcs is FALSE, don't actually run any functions associated
- * with shortcut keys. */
-int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
-       *ran_func, bool *finished, bool allow_funcs)
-{
-    int input;
-       /* The character we read in. */
-    static int *kbinput = NULL;
-       /* The input buffer. */
-    static size_t kbinput_len = 0;
-       /* The length of the input buffer. */
-    bool cut_copy = FALSE;
-       /* Are we cutting or copying text? */
-    const sc *s;
-    bool have_shortcut;
-
-    *s_or_t = FALSE;
-    *ran_func = FALSE;
-    *finished = FALSE;
-
-    /* Read in a character. */
-    input = get_kbinput(edit, meta_key, func_key);
-
-#ifndef DISABLE_MOUSE
-    if (allow_funcs) {
-       /* If we got a mouse click and it was on a shortcut, read in the
-        * shortcut character. */
-       if (*func_key && input == KEY_MOUSE) {
-           if (do_mouse() == 1)
-               input = get_kbinput(edit, meta_key, func_key);
-           else {
-               *meta_key = FALSE;
-               *func_key = FALSE;
-               input = ERR;
-           }
+#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
+    /* When tab pages were created, may need to update the tab pages line and
+     * scrollbars.  This is skipped while creating them. */
+    if (first_tabpage->tp_next != NULL)
+    {
+       out_flush();
+       gui_init_which_components(NULL);
+       gui_update_scrollbars(TRUE);
+    }
+    need_mouse_correct = TRUE;
+#endif
+
+    /* If ":startinsert" command used, stuff a dummy command to be able to
+     * call normal_cmd(), which will then start Insert mode. */
+    if (restart_edit != 0)
+       stuffcharReadbuff(K_NOP);
+
+#ifdef FEAT_NETBEANS_INTG
+    if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
+    {
+# ifdef FEAT_GUI
+#  if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)  \
+               && !defined(FEAT_GUI_W32)
+       if (gui.in_use)
+       {
+           mch_errmsg(_("netbeans is not supported with this GUI\n"));
+           mch_exit(2);
        }
+#  endif
+# endif
+       /* Tell the client that it can start sending commands. */
+       netbeans_open(netbeansArg + 3, TRUE);
     }
 #endif

-    /* Check for a shortcut in the main list. */
-    s = get_shortcut(MMAIN, &input, meta_key, func_key);
+    TIME_MSG("before starting main loop");

-    /* If we got a shortcut from the main list, or a "universal"
-     * edit window shortcut, set have_shortcut to TRUE. */
-    have_shortcut = (s != NULL);
+    /*
+     * Call the main command loop.  This never returns.
+    */
+    main_loop(FALSE, FALSE);

-    /* If we got a non-high-bit control key, a meta key sequence, or a
-     * function key, and it's not a shortcut or toggle, throw it out. */
-    if (!have_shortcut) {
-       if (is_ascii_cntrl_char(input) || *meta_key || *func_key) {
-           statusbar(_("Unknown Command"));
-           beep();
-           *meta_key = FALSE;
-           *func_key = FALSE;
-           input = ERR;
-       }
+    return 0;
+}
+#endif /* NO_VIM_MAIN */
+#endif /* PROTO */
+
+/*
+ * Main loop: Execute Normal mode commands until exiting Vim.
+ * Also used to handle commands in the command-line window, until the window
+ * is closed.
+ * Also used to handle ":visual" command after ":global": execute Normal mode
+ * commands, return when entering Ex mode.  "noexmode" is TRUE then.
+ */
+    void
+main_loop(cmdwin, noexmode)
+    int                cmdwin;     /* TRUE when working in the command-line 
window */
+    int                noexmode;   /* TRUE when return on entering Ex mode */
+{
+    oparg_T    oa;                             /* operator arguments */
+    int                previous_got_int = FALSE;       /* "got_int" was TRUE */
+#ifdef FEAT_CONCEAL
+    linenr_T   conceal_old_cursor_line = 0;
+    linenr_T   conceal_new_cursor_line = 0;
+    int                conceal_update_lines = FALSE;
+#endif
+
+#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
+    /* Setup to catch a terminating error from the X server.  Just ignore
+     * it, restore the state and continue.  This might not always work
+     * properly, but at least we don't exit unexpectedly when the X server
+     * exits while Vim is running in a console. */
+    if (!cmdwin && !noexmode && SETJMP(x_jump_env))
+    {
+       State = NORMAL;
+       VIsual_active = FALSE;
+       got_int = TRUE;
+       need_wait_return = FALSE;
+       global_busy = FALSE;
+       exmode_active = 0;
+       skip_redraw = FALSE;
+       RedrawingDisabled = 0;
+       no_wait_return = 0;
+       vgetc_busy = 0;
+# ifdef FEAT_EVAL
+       emsg_skip = 0;
+# endif
+       emsg_off = 0;
+# ifdef FEAT_MOUSE
+       setmouse();
+# endif
+       settmode(TMODE_RAW);
+       starttermcap();
+       scroll_start();
+       redraw_later_clear();
     }
+#endif

-    if (allow_funcs) {
-       /* If we got a character, and it isn't a shortcut or toggle,
-        * it's a normal text character.  Display the warning if we're
-        * in view mode, or add the character to the input buffer if
-        * we're not. */
-       if (input != ERR && !have_shortcut) {
-           if (ISSET(VIEW_MODE))
-               print_view_warning();
-           else {
-               kbinput_len++;
-               kbinput = (int *)nrealloc(kbinput, kbinput_len *
-                       sizeof(int));
-               kbinput[kbinput_len - 1] = input;
-
+    clear_oparg(&oa);
+    while (!cmdwin
+#ifdef FEAT_CMDWIN
+           || cmdwin_result == 0
+#endif
+           )
+    {
+       if (stuff_empty())
+       {
+           did_check_timestamps = FALSE;
+           if (need_check_timestamps)
+               check_timestamps(FALSE);
+           if (need_wait_return)       /* if wait_return still needed ... */
+               wait_return(FALSE);     /* ... call it now */
+           if (need_start_insertmode && goto_im() && !VIsual_active)
+           {
+               need_start_insertmode = FALSE;
+               stuffReadbuff((char_u *)"i");   /* start insert mode next */
+               /* skip the fileinfo message now, because it would be shown
+                * after insert mode finishes! */
+               need_fileinfo = FALSE;
            }
        }

-       /* If we got a shortcut or toggle, or if there aren't any other
-        * characters waiting after the one we read in, we need to
-        * output all the characters in the input buffer if it isn't
-        * empty.  Note that it should be empty if we're in view
-        * mode. */
-        if (have_shortcut || get_key_buffer_len() == 0) {
-#ifndef DISABLE_WRAPPING
-           /* If we got a shortcut or toggle, and it's not the shortcut
-            * for verbatim input, turn off prepending of wrapped text. */
-           if (have_shortcut && (!have_shortcut || s == NULL || s->scfunc !=
-               do_verbatim_input))
-               wrap_reset();
-#endif
-
-           if (kbinput != NULL) {
-               /* Display all the characters in the input buffer at
-                * once, filtering out control characters. */
-               char *output = charalloc(kbinput_len + 1);
-               size_t i;
-
-               for (i = 0; i < kbinput_len; i++)
-                   output[i] = (char)kbinput[i];
-               output[i] = '\0';
-
-               do_output(output, kbinput_len, FALSE);
-
-               free(output);
-
-               /* Empty the input buffer. */
-               kbinput_len = 0;
-               free(kbinput);
-               kbinput = NULL;
+       /* Reset "got_int" now that we got back to the main loop.  Except when
+        * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
+        * the ":g" command.
+        * For ":g/pat/vi" we reset "got_int" when used once.  When used
+        * a second time we go back to Ex mode and abort the ":g" command. */
+       if (got_int)
+       {
+           if (noexmode && global_busy && !exmode_active && previous_got_int)
+           {
+               /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
+                * used and keep "got_int" set, so that it aborts ":g". */
+               exmode_active = EXMODE_NORMAL;
+               State = NORMAL;
+           }
+           else if (!global_busy || !exmode_active)
+           {
+               if (!quit_more)
+                   (void)vgetc();              /* flush all buffers */
+               got_int = FALSE;
            }
+           previous_got_int = TRUE;
        }
-
-       if (have_shortcut) {
-           switch (input) {
-               /* Handle the normal edit window shortcuts, setting
-                * ran_func to TRUE if we try to run their associated
-                * functions and setting finished to TRUE to indicate
-                * that we're done after running or trying to run their
-                * associated functions. */
-               default:
-                   /* If the function associated with this shortcut is
-                    * cutting or copying text, indicate this. */
-                   if (s->scfunc == do_cut_text_void
-#ifndef NANO_TINY
-                       || s->scfunc == do_copy_text || s->scfunc ==
-                       do_cut_till_end
-#endif
+       else
+           previous_got_int = FALSE;
+
+       if (!exmode_active)
+           msg_scroll = FALSE;
+       quit_more = FALSE;
+
+       /*
+        * If skip redraw is set (for ":" in wait_return()), don't redraw now.
+        * If there is nothing in the stuff_buffer or do_redraw is TRUE,
+        * update cursor and redraw.
+        */
+       if (skip_redraw || exmode_active)
+           skip_redraw = FALSE;
+       else if (do_redraw || stuff_empty())
+       {
+#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
+           /* Trigger CursorMoved if the cursor moved. */
+           if (!finish_op && (
+# ifdef FEAT_AUTOCMD
+                       has_cursormoved()
+# endif
+# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
+                       ||
+# endif
+# ifdef FEAT_CONCEAL
+                       curwin->w_p_cole > 0
+# endif
                        )
-                       cut_copy = TRUE;
-
-                   if (s->scfunc != 0) {
-                       const subnfunc *f = sctofunc((sc *) s);
-                       *ran_func = TRUE;
-                       if (ISSET(VIEW_MODE) && f && !f->viewok)
-                           print_view_warning();
-                       else {
-#ifndef NANO_TINY
-                           if (s->scfunc == do_toggle_void)
-                               do_toggle(s->toggle);
-                           else {
-#else
-                           {
-#endif
-                               s->scfunc();
-#ifdef ENABLE_COLOR
-                               if (f && !f->viewok && openfile->syntax != NULL
-                                       && openfile->syntax->nmultis > 0) {
-                                   reset_multis(openfile->current, FALSE);
-                               }
-#endif
-                               if (edit_refresh_needed) {
-#ifdef DEBUG
-                                   fprintf(stderr, "running edit_refresh() as
edit_refresh_needed is true\n");
-#endif
-                                   edit_refresh();
-                                   edit_refresh_needed = FALSE;
-                               }
-
-                           }
-                       }
-                   }
-                   *finished = TRUE;
-                   break;
+                && !equalpos(last_cursormoved, curwin->w_cursor))
+           {
+# ifdef FEAT_AUTOCMD
+               if (has_cursormoved())
+                   apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
+                                                              FALSE, curbuf);
+# endif
+# ifdef FEAT_CONCEAL
+               if (curwin->w_p_cole > 0)
+               {
+                   conceal_old_cursor_line = last_cursormoved.lnum;
+                   conceal_new_cursor_line = curwin->w_cursor.lnum;
+                   conceal_update_lines = TRUE;
+               }
+# endif
+               last_cursormoved = curwin->w_cursor;
            }
-       }
-    }
-
-    /* If we aren't cutting or copying text, blow away the text in the
-     * cutbuffer. */
-    if (!cut_copy)
-       cutbuffer_reset();
-
-    return input;
-}
-
-void xon_complaint(void)
-{
-    statusbar(_("XON ignored, mumble mumble"));
-}
-
-void xoff_complaint(void)
-{
-    statusbar(_("XOFF ignored, mumble mumble"));
-}
-
-
-#ifndef DISABLE_MOUSE
-/* Handle a mouse click on the edit window or the shortcut list. */
-int do_mouse(void)
-{
-    int mouse_x, mouse_y;
-    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
-
-    /* We can click on the edit window to move the cursor. */
-    if (retval == 0 && wmouse_trafo(edit, &mouse_y, &mouse_x, FALSE)) {
-       bool sameline;
-           /* Did they click on the line with the cursor?  If they
-            * clicked on the cursor, we set the mark. */
-       filestruct *current_save = openfile->current;
-       size_t current_x_save = openfile->current_x;
-       size_t pww_save = openfile->placewewant;
-
-       sameline = (mouse_y == openfile->current_y);
-
-#ifdef DEBUG
-           fprintf(stderr, "mouse_y = %d, current_y = %d\n", mouse_y,
openfile->current_y);
 #endif

-       if (ISSET(SOFTWRAP)) {
-           int i = 0;
-           for (openfile->current = openfile->edittop;
-                openfile->current->next && i < mouse_y;
-                openfile->current = openfile->current->next, i++) {
-               openfile->current_y = i;
-               i += strlenpt(openfile->current->data) / COLS;
+#ifdef FEAT_AUTOCMD
+           /* Trigger TextChanged if b_changedtick differs. */
+           if (!finish_op && has_textchanged()
+                   && last_changedtick != curbuf->b_changedtick)
+           {
+               if (last_changedtick_buf == curbuf)
+                   apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
+                                                              FALSE, curbuf);
+               last_changedtick_buf = curbuf;
+               last_changedtick = curbuf->b_changedtick;
            }
-
-#ifdef DEBUG
-           fprintf(stderr, "do_mouse(): moving to current_y = %d, i %d\n",
openfile->current_y, i);
-           fprintf(stderr, "            openfile->current->data =
\"%s\"\n", openfile->current->data);
 #endif

-           if (i > mouse_y) {
-               openfile->current = openfile->current->prev;
-               openfile->current_x = actual_x(openfile->current->data, mouse_x 
+
(mouse_y - openfile->current_y) * COLS);
-#ifdef DEBUG
-           fprintf(stderr, "do_mouse(): i > mouse_y, mouse_x = %d,
current_x to = %d\n", mouse_x, openfile->current_x);
-#endif
-           } else {
-               openfile->current_x = actual_x(openfile->current->data, 
mouse_x);
-#ifdef DEBUG
-           fprintf(stderr, "do_mouse(): i <= mouse_y, mouse_x = %d, setting
current_x to = %d\n", mouse_x, openfile->current_x);
+#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
+           /* Scroll-binding for diff mode may have been postponed until
+            * here.  Avoids doing it for every change. */
+           if (diff_need_scrollbind)
+           {
+               check_scrollbind((linenr_T)0, 0L);
+               diff_need_scrollbind = FALSE;
+           }
 #endif
+#if defined(FEAT_FOLDING)
+           /* Include a closed fold completely in the Visual area. */
+           foldAdjustVisual();
+#endif
+#ifdef FEAT_FOLDING
+           /*
+            * When 'foldclose' is set, apply 'foldlevel' to folds that don't
+            * contain the cursor.
+            * When 'foldopen' is "all", open the fold(s) under the cursor.
+            * This may mark the window for redrawing.
+            */
+           if (hasAnyFolding(curwin) && !char_avail())
+           {
+               foldCheckClose();
+               if (fdo_flags & FDO_ALL)
+                   foldOpenCursor();
            }
+#endif

-           openfile->placewewant = xplustabs();
-
-       } else {
-           /* Move to where the click occurred. */
-           for (; openfile->current_y < mouse_y && openfile->current !=
-                  openfile->filebot; openfile->current_y++)
-               openfile->current = openfile->current->next;
-           for (; openfile->current_y > mouse_y && openfile->current !=
-                  openfile->fileage; openfile->current_y--)
-               openfile->current = openfile->current->prev;
-
-           openfile->current_x = actual_x(openfile->current->data,
-               get_page_start(xplustabs()) + mouse_x);
-
-           openfile->placewewant = xplustabs();
-       }
+           /*
+            * Before redrawing, make sure w_topline is correct, and w_leftcol
+            * if lines don't wrap, and w_skipcol if lines wrap.
+            */
+           update_topline();
+           validate_cursor();
+
+           if (VIsual_active)
+               update_curbuf(INVERTED);/* update inverted part */
+           else if (must_redraw)
+               update_screen(0);
+           else if (redraw_cmdline || clear_cmdline)
+               showmode();
+#ifdef FEAT_WINDOWS
+           redraw_statuslines();
+#endif
+#ifdef FEAT_TITLE
+           if (need_maketitle)
+               maketitle();
+#endif
+           /* display message after redraw */
+           if (keep_msg != NULL)
+           {
+               char_u *p;
+
+               /* msg_attr_keep() will set keep_msg to NULL, must free the
+                * string here. */
+               p = keep_msg;
+               keep_msg = NULL;
+               msg_attr(p, keep_msg_attr);
+               vim_free(p);
+           }
+           if (need_fileinfo)          /* show file info after redraw */
+           {
+               fileinfo(FALSE, TRUE, FALSE);
+               need_fileinfo = FALSE;
+           }

-#ifndef NANO_TINY
-       /* Clicking where the cursor is toggles the mark, as does
-        * clicking beyond the line length with the cursor at the end of
-        * the line. */
-       if (sameline && openfile->current_x == current_x_save)
-           do_mark();
+           emsg_on_display = FALSE;    /* can delete error message now */
+           did_emsg = FALSE;
+           msg_didany = FALSE;         /* reset lines_left in msg_start() */
+           may_clear_sb_text();        /* clear scroll-back text on next msg */
+           showruler(FALSE);
+
+# if defined(FEAT_CONCEAL)
+           if (conceal_update_lines
+                   && (conceal_old_cursor_line != conceal_new_cursor_line
+                       || conceal_cursor_line(curwin)
+                       || need_cursor_line_redraw))
+           {
+               if (conceal_old_cursor_line != conceal_new_cursor_line
+                       && conceal_old_cursor_line
+                                               <= curbuf->b_ml.ml_line_count)
+                   update_single_line(curwin, conceal_old_cursor_line);
+               update_single_line(curwin, conceal_new_cursor_line);
+               curwin->w_valid &= ~VALID_CROW;
+           }
+# endif
+           setcursor();
+           cursor_on();
+
+           do_redraw = FALSE;
+
+#ifdef STARTUPTIME
+           /* Now that we have drawn the first screen all the startup stuff
+            * has been done, close any file for startup messages. */
+           if (time_fd != NULL)
+           {
+               TIME_MSG("first screen update");
+               TIME_MSG("--- VIM STARTED ---");
+               fclose(time_fd);
+               time_fd = NULL;
+           }
 #endif
-
-       edit_redraw(current_save, pww_save);
+       }
+#ifdef FEAT_GUI
+       if (need_mouse_correct)
+           gui_mouse_correct();
+#endif
+
+       /*
+        * Update w_curswant if w_set_curswant has been set.
+        * Postponed until here to avoid computing w_virtcol too often.
+        */
+       update_curswant();
+
+#ifdef FEAT_EVAL
+       /*
+        * May perform garbage collection when waiting for a character, but
+        * only at the very toplevel.  Otherwise we may be using a List or
+        * Dict internally somewhere.
+        * "may_garbage_collect" is reset in vgetc() which is invoked through
+        * do_exmode() and normal_cmd().
+        */
+       may_garbage_collect = (!cmdwin && !noexmode);
+#endif
+       /*
+        * If we're invoked as ex, do a round of ex commands.
+        * Otherwise, get and execute a normal mode command.
+        */
+       if (exmode_active)
+       {
+           if (noexmode)   /* End of ":global/path/visual" commands */
+               return;
+           do_exmode(exmode_active == EXMODE_VIM);
+       }
+       else
+           normal_cmd(&oa, TRUE);
     }
-
-    return retval;
 }
-#endif /* !DISABLE_MOUSE */

-#ifdef ENABLE_COLOR
-void alloc_multidata_if_needed(filestruct *fileptr)
-{
-    if (!fileptr->multidata)
-       fileptr->multidata = (short *) nmalloc(openfile->syntax->nmultis *
sizeof(short));
-}

-/* Precalculate the multi-line start and end regex info so we can
- * speed up rendering (with any hope at all...). */
-void precalc_multicolorinfo(void)
+#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
+/*
+ * Exit, but leave behind swap files for modified buffers.
+ */
+    void
+getout_preserve_modified(exitval)
+    int                exitval;
 {
-#ifdef DEBUG
-           fprintf(stderr, "entering precalc_multicolorinfo()\n");
+# if defined(SIGHUP) && defined(SIG_IGN)
+    /* Ignore SIGHUP, because a dropped connection causes a read error, which
+     * makes Vim exit and then handling SIGHUP causes various reentrance
+     * problems. */
+    signal(SIGHUP, SIG_IGN);
+# endif
+
+    ml_close_notmod();             /* close all not-modified buffers */
+    ml_sync_all(FALSE, FALSE);     /* preserve all swap files */
+    ml_close_all(FALSE);           /* close all memfiles, without deleting */
+    getout(exitval);               /* exit Vim properly */
+}
 #endif
-    if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) {
-       const colortype *tmpcolor = openfile->colorstrings;
-       regmatch_t startmatch, endmatch;
-       filestruct *fileptr, *endptr;
-       time_t last_check = time(NULL), cur_check = 0;
-
-       /* Let us get keypresses to see if the user is trying to
-        * start editing.  We may want to throw up a statusbar
-        * message before starting this later if it takes
-        * too long to do this routine.  For now silently
-        * abort if they hit a key. */
-       nodelay(edit, FALSE);

-       for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) {

-           /* If it's not a multi-line regex, amscray. */
-           if (tmpcolor->end == NULL)
-               continue;
-#ifdef DEBUG
-           fprintf(stderr, "working on color id %d\n", tmpcolor->id);
-#endif
-
-           for (fileptr = openfile->fileage; fileptr != NULL; fileptr =
fileptr->next) {
-               int startx = 0;
-               int nostart = 0;
-#ifdef DEBUG
-               fprintf(stderr, "working on lineno %lu\n", (unsigned long) 
fileptr->lineno);
+/* Exit properly */
+    void
+getout(exitval)
+    int                exitval;
+{
+#ifdef FEAT_AUTOCMD
+    buf_T      *buf;
+    win_T      *wp;
+    tabpage_T  *tp, *next_tp;
 #endif

-               alloc_multidata_if_needed(fileptr);
+    exiting = TRUE;

-               if ((cur_check = time(NULL)) - last_check > 1) {
-                   last_check = cur_check;
-                   if (wgetch(edit) != ERR)
-                       goto precalc_cleanup;
-               }
+    /* When running in Ex mode an error causes us to exit with a non-zero exit
+     * code.  POSIX requires this, although it's not 100% clear from the
+     * standard. */
+    if (exmode_active)
+       exitval += ex_exitval;

-               while ((nostart = regexec(tmpcolor->start, 
&fileptr->data[startx],
1, &startmatch, 0))  == 0) {
-                   /* Look for end, and start marking how many lines are
-                    * encompassed which should speed up rendering later. */
-                   startx += startmatch.rm_eo;
-#ifdef DEBUG
-                   fprintf(stderr, "match found at pos %d...", startx);
+    /* Position the cursor on the last screen line, below all the text */
+#ifdef FEAT_GUI
+    if (!gui.in_use)
 #endif
+       windgoto((int)Rows - 1, 0);

-                   /* Look on this line first for end. */
-                   if (regexec(tmpcolor->end, &fileptr->data[startx], 1,
&endmatch, 0)  == 0) {
-                       startx += endmatch.rm_eo;
-                       fileptr->multidata[tmpcolor->id] |= CSTARTENDHERE;
-#ifdef DEBUG
-                       fprintf(stderr, "end found on this line\n");
-#endif
-                       continue;
-                   }
-
-                   /* Nice, we didn't find the end regex on this line.  Let's
start looking for it. */
-                   for (endptr = fileptr->next; endptr != NULL; endptr = 
endptr->next) {
-
-#ifdef DEBUG
-                       fprintf(stderr, "advancing to line %lu to find 
end...\n",
(unsigned long) endptr->lineno);
+#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
+    /* Optionally print hashtable efficiency. */
+    hash_debug_results();
 #endif
-                       /* Check for keyboard input, again. */
-                       if ((cur_check = time(NULL)) - last_check > 1) {
-                           last_check = cur_check;
-                           if (wgetch(edit) != ERR)
-                               goto precalc_cleanup;
-                       }
-                       if (regexec(tmpcolor->end, endptr->data, 1, &endmatch, 
0) == 0)
-                          break;
-                   }

-                   if (endptr == NULL) {
-#ifdef DEBUG
-                       fprintf(stderr, "no end found, breaking out\n");
+#ifdef FEAT_GUI
+    msg_didany = FALSE;
 #endif
-                       break;
-                   }

-#ifdef DEBUG
-                   fprintf(stderr, "end found\n");
-#endif
-                   /* We found it, we found it, la la la la la.  Mark all
-                    * the lines in between and the end properly. */
-                   fileptr->multidata[tmpcolor->id] |= CENDAFTER;
-#ifdef DEBUG
-                   fprintf(stderr, "marking line %lu as CENDAFTER\n", (unsigned
long) fileptr->lineno);
-#endif
-                   for (fileptr = fileptr->next; fileptr != endptr; fileptr =
fileptr->next) {
-                       alloc_multidata_if_needed(fileptr);
-                       fileptr->multidata[tmpcolor->id] = CWHOLELINE;
-#ifdef DEBUG
-                       fprintf(stderr, "marking intermediary line %lu as 
CWHOLELINE\n",
(unsigned long) fileptr->lineno);
-#endif
-                   }
-                   alloc_multidata_if_needed(endptr);
-#ifdef DEBUG
-                   fprintf(stderr, "marking line %lu as BEGINBEFORE\n", 
(unsigned
long) fileptr->lineno);
-#endif
-                   endptr->multidata[tmpcolor->id] |= CBEGINBEFORE;
-                   /* We should be able to skip all the way to the line of the 
match.
-                    * This may introduce more bugs but it's the Right Thing to 
do. */
-                   fileptr = endptr;
-                   startx = endmatch.rm_eo;
-#ifdef DEBUG
-                   fprintf(stderr, "jumping to line %lu pos %d to continue\n",
(unsigned long) endptr->lineno, startx);
-#endif
-               }
-               if (nostart && startx == 0) {
-#ifdef DEBUG
-                   fprintf(stderr, "no start found on line %lu, continuing\n",
(unsigned long) fileptr->lineno);
-#endif
-                   fileptr->multidata[tmpcolor->id] = CNONE;
+#ifdef FEAT_AUTOCMD
+    if (get_vim_var_nr(VV_DYING) <= 1)
+    {
+       /* Trigger BufWinLeave for all windows, but only once per buffer. */
+# if defined FEAT_WINDOWS
+       for (tp = first_tabpage; tp != NULL; tp = next_tp)
+       {
+           next_tp = tp->tp_next;
+           for (wp = (tp == curtab)
+                   ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
+           {
+               if (wp->w_buffer == NULL)
+                   /* Autocmd must have close the buffer already, skip. */
                    continue;
+               buf = wp->w_buffer;
+               if (buf->b_changedtick != -1)
+               {
+                   apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
+                                                   buf->b_fname, FALSE, buf);
+                   buf->b_changedtick = -1;  /* note that we did it already */
+                   /* start all over, autocommands may mess up the lists */
+                   next_tp = first_tabpage;
+                   break;
                }
            }
        }
-    }
-precalc_cleanup:
-    nodelay(edit, FALSE);
-}
-#endif /* ENABLE_COLOR */
-
-/* The user typed output_len multibyte characters.  Add them to the edit
- * buffer, filtering out all ASCII control characters if allow_cntrls is
- * TRUE. */
-void do_output(char *output, size_t output_len, bool allow_cntrls)
-{
-    size_t current_len, orig_lenpt, i = 0;
-    char *char_buf = charalloc(mb_cur_max());
-    int char_buf_len;
-
-    assert(openfile->current != NULL && openfile->current->data != NULL);
-
-    current_len = strlen(openfile->current->data);
-    if (ISSET(SOFTWRAP))
-       orig_lenpt = strlenpt(openfile->current->data);
-
-    while (i < output_len) {
-       /* If allow_cntrls is TRUE, convert nulls and newlines
-        * properly. */
-       if (allow_cntrls) {
-           /* Null to newline, if needed. */
-           if (output[i] == '\0')
-               output[i] = '\n';
-           /* Newline to Enter, if needed. */
-           else if (output[i] == '\n') {
-               do_enter(FALSE);
-               i++;
-               continue;
+# else
+       apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
+                                                              FALSE, curbuf);
+# endif
+
+       /* Trigger BufUnload for buffers that are loaded */
+       for (buf = firstbuf; buf != NULL; buf = buf->b_next)
+           if (buf->b_ml.ml_mfp != NULL)
+           {
+               apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
+                                                                 FALSE, buf);
+               if (!buf_valid(buf))    /* autocmd may delete the buffer */
+                   break;
            }
-       }
-
-       /* Interpret the next multibyte character. */
-       char_buf_len = parse_mbchar(output + i, char_buf, NULL);
-
-       i += char_buf_len;
-
-       /* If allow_cntrls is FALSE, filter out an ASCII control
-        * character. */
-       if (!allow_cntrls && is_ascii_cntrl_char(*(output + i -
-               char_buf_len)))
-           continue;
-
-       /* If the NO_NEWLINES flag isn't set, when a character is
-        * added to the magicline, it means we need a new magicline. */
-       if (!ISSET(NO_NEWLINES) && openfile->filebot ==
-               openfile->current)
-           new_magicline();
-
-       /* More dangerousness fun =) */
-       openfile->current->data = charealloc(openfile->current->data,
-               current_len + (char_buf_len * 2));
-
-       assert(openfile->current_x <= current_len);
-
-       charmove(openfile->current->data + openfile->current_x +
-               char_buf_len, openfile->current->data +
-               openfile->current_x, current_len - openfile->current_x +
-               char_buf_len);
-       strncpy(openfile->current->data + openfile->current_x, char_buf,
-               char_buf_len);
-       current_len += char_buf_len;
-       openfile->totsize++;
-       set_modified();
-
-#ifndef NANO_TINY
-       update_undo(ADD);
+       apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
+    }
+#endif

-       /* Note that current_x has not yet been incremented. */
-       if (openfile->mark_set && openfile->current ==
-               openfile->mark_begin && openfile->current_x <
-               openfile->mark_begin_x)
-           openfile->mark_begin_x += char_buf_len;
+#ifdef FEAT_VIMINFO
+    if (*p_viminfo != NUL)
+       /* Write out the registers, history, marks etc, to the viminfo file */
+       write_viminfo(NULL, FALSE);
 #endif

-       openfile->current_x += char_buf_len;
+#ifdef FEAT_AUTOCMD
+    if (get_vim_var_nr(VV_DYING) <= 1)
+       apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
+#endif

-#ifndef DISABLE_WRAPPING
-       /* If we're wrapping text, we need to call edit_refresh(). */
-       if (!ISSET(NO_WRAP))
-           if (do_wrap(openfile->current, FALSE))
-               edit_refresh_needed = TRUE;
+#ifdef FEAT_PROFILE
+    profile_dump();
 #endif

-#ifdef ENABLE_COLOR
-       /* If color syntaxes are available and turned on, we need to
-        * call edit_refresh(). */
-       if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
-           edit_refresh_needed = TRUE;
+    if (did_emsg
+#ifdef FEAT_GUI
+           || (gui.in_use && msg_didany && p_verbose > 0)
 #endif
+           )
+    {
+       /* give the user a chance to read the (error) message */
+       no_wait_return = FALSE;
+       wait_return(FALSE);
     }

-    /* Well we might also need a full refresh if we've changed the
-     * line length to be a new multiple of COLS. */
-    if (ISSET(SOFTWRAP) && edit_refresh_needed == FALSE)
-       if (strlenpt(openfile->current->data) / COLS  != orig_lenpt / COLS)
-           edit_refresh_needed = TRUE;
-
-    free(char_buf);
+#ifdef FEAT_AUTOCMD
+    /* Position the cursor again, the autocommands may have moved it */
+# ifdef FEAT_GUI
+    if (!gui.in_use)
+# endif
+       windgoto((int)Rows - 1, 0);
+#endif

-    openfile->placewewant = xplustabs();
+#ifdef FEAT_LUA
+    lua_end();
+#endif
+#ifdef FEAT_MZSCHEME
+    mzscheme_end();
+#endif
+#ifdef FEAT_TCL
+    tcl_end();
+#endif
+#ifdef FEAT_RUBY
+    ruby_end();
+#endif
+#ifdef FEAT_PYTHON
+    python_end();
+#endif
+#ifdef FEAT_PYTHON3
+    python3_end();
+#endif
+#ifdef FEAT_PERL
+    perl_end();
+#endif
+#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
+    iconv_end();
+#endif
+#ifdef FEAT_NETBEANS_INTG
+    netbeans_end();
+#endif
+#ifdef FEAT_CSCOPE
+    cs_end();
+#endif
+#ifdef FEAT_EVAL
+    if (garbage_collect_at_exit)
+       garbage_collect();
+#endif

-#ifdef ENABLE_COLOR
-    reset_multis(openfile->current, FALSE);
-#endif
-    if (edit_refresh_needed == TRUE) {
-       edit_refresh();
-       edit_refresh_needed = FALSE;
-    } else
-       update_line(openfile->current, openfile->current_x);
+    mch_exit(exitval);
 }

-int main(int argc, char **argv)
+#ifndef NO_VIM_MAIN
+/*
+ * Get a (optional) count for a Vim argument.
+ */
+    static int
+get_number_arg(p, idx, def)
+    char_u     *p;         /* pointer to argument */
+    int                *idx;       /* index in argument, is incremented */
+    int                def;        /* default value */
 {
-    int optchr;
-    ssize_t startline = 1;
-       /* Line to try and start at. */
-    ssize_t startcol = 1;
-       /* Column to try and start at. */
-#ifndef DISABLE_WRAPJUSTIFY
-    bool fill_used = FALSE;
-       /* Was the fill option used? */
-#endif
-#ifdef ENABLE_MULTIBUFFER
-    bool old_multibuffer;
-       /* The old value of the multibuffer option, restored after we
-        * load all files on the command line. */
-#endif
-#ifdef HAVE_GETOPT_LONG
-    const struct option long_options[] = {
-       {"help", 0, NULL, 'h'},
-       {"boldtext", 0, NULL, 'D'},
-#ifdef ENABLE_MULTIBUFFER
-       {"multibuffer", 0, NULL, 'F'},
-#endif
-#ifdef ENABLE_NANORC
-       {"ignorercfiles", 0, NULL, 'I'},
-#endif
-       {"rebindkeypad", 0, NULL, 'K'},
-       {"nonewlines", 0, NULL, 'L'},
-       {"morespace", 0, NULL, 'O'},
-#ifndef DISABLE_JUSTIFY
-       {"quotestr", 1, NULL, 'Q'},
-#endif
-       {"restricted", 0, NULL, 'R'},
-       {"tabsize", 1, NULL, 'T'},
-       {"version", 0, NULL, 'V'},
-#ifdef ENABLE_COLOR
-       {"syntax", 1, NULL, 'Y'},
-#endif
-       {"const", 0, NULL, 'c'},
-       {"rebinddelete", 0, NULL, 'd'},
-       {"nofollow", 0, NULL, 'l'},
-#ifndef DISABLE_MOUSE
-       {"mouse", 0, NULL, 'm'},
-#endif
-#ifndef DISABLE_OPERATINGDIR
-       {"operatingdir", 1, NULL, 'o'},
-#endif
-       {"preserve", 0, NULL, 'p'},
-       {"quiet", 0, NULL, 'q'},
-#ifndef DISABLE_WRAPJUSTIFY
-       {"fill", 1, NULL, 'r'},
-#endif
-#ifndef DISABLE_SPELLER
-       {"speller", 1, NULL, 's'},
-#endif
-       {"tempfile", 0, NULL, 't'},
-       {"view", 0, NULL, 'v'},
-#ifndef DISABLE_WRAPPING
-       {"nowrap", 0, NULL, 'w'},
-#endif
-       {"nohelp", 0, NULL, 'x'},
-       {"suspend", 0, NULL, 'z'},
-#ifndef NANO_TINY
-       {"smarthome", 0, NULL, 'A'},
-       {"backup", 0, NULL, 'B'},
-       {"backupdir", 1, NULL, 'C'},
-       {"tabstospaces", 0, NULL, 'E'},
-       {"locking", 0, NULL, 'G'},
-       {"historylog", 0, NULL, 'H'},
-       {"noconvert", 0, NULL, 'N'},
-       {"poslog", 0, NULL, 'P'},
-       {"smooth", 0, NULL, 'S'},
-       {"quickblank", 0, NULL, 'U'},
-       {"undo", 0, NULL, 'u'},
-       {"wordbounds", 0, NULL, 'W'},
-       {"autoindent", 0, NULL, 'i'},
-       {"cut", 0, NULL, 'k'},
-       {"softwrap", 0, NULL, '$'},
-#endif
-       {NULL, 0, NULL, 0}
-    };
-#endif
-
-#ifdef ENABLE_UTF8
+    if (vim_isdigit(p[*idx]))
     {
-       /* If the locale set exists and uses UTF-8, we should use
-        * UTF-8. */
-       char *locale = setlocale(LC_ALL, "");
-
-       if (locale != NULL && (strcmp(nl_langinfo(CODESET),
-               "UTF-8") == 0)) {
-#ifdef USE_SLANG
-           SLutf8_enable(1);
-#endif
-           utf8_init();
-       }
+       def = atoi((char *)&(p[*idx]));
+       while (vim_isdigit(p[*idx]))
+           *idx = *idx + 1;
     }
-#else
+    return def;
+}
+
+#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
+/*
+ * Setup to use the current locale (for ctype() and many other things).
+ */
+    static void
+init_locale()
+{
     setlocale(LC_ALL, "");
+
+# ifdef FEAT_GUI_GTK
+    /* Tell Gtk not to change our locale settings. */
+    gtk_disable_setlocale();
+# endif
+# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
+    /* Make sure strtod() uses a decimal point, not a comma. */
+    setlocale(LC_NUMERIC, "C");
+# endif
+
+# ifdef WIN32
+    /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
+     * text while it's expecting text in the current locale.  This call avoids
+     * that. */
+    setlocale(LC_CTYPE, "C");
+# endif
+
+# ifdef FEAT_GETTEXT
+    {
+       int     mustfree = FALSE;
+       char_u  *p;
+
+#  ifdef DYNAMIC_GETTEXT
+       /* Initialize the gettext library */
+       dyn_libintl_init(NULL);
+#  endif
+       /* expand_env() doesn't work yet, because chartab[] is not initialized
+        * yet, call vim_getenv() directly */
+       p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
+       if (p != NULL && *p != NUL)
+       {
+           vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
+           bindtextdomain(VIMPACKAGE, (char *)NameBuff);
+       }
+       if (mustfree)
+           vim_free(p);
+       textdomain(VIMPACKAGE);
+    }
+# endif
+}
 #endif

-#ifdef ENABLE_NLS
-    bindtextdomain(PACKAGE, LOCALEDIR);
-    textdomain(PACKAGE);
+/*
+ * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
+ * If the executable name starts with "r" we disable shell commands.
+ * If the next character is "e" we run in Easy mode.
+ * If the next character is "g" we run the GUI version.
+ * If the next characters are "view" we start in readonly mode.
+ * If the next characters are "diff" or "vimdiff" we start in diff mode.
+ * If the next characters are "ex" we start in Ex mode.  If it's followed
+ * by "im" use improved Ex mode.
+ */
+    static void
+parse_command_name(parmp)
+    mparm_T    *parmp;
+{
+    char_u     *initstr;
+
+    initstr = gettail((char_u *)parmp->argv[0]);
+
+#ifdef MACOS_X_UNIX
+    /* An issue has been seen when launching Vim in such a way that
+     * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
+     * executable or a symbolic link of it. Until this issue is resolved
+     * we prohibit the GUI from being used.
+     */
+    if (STRCMP(initstr, parmp->argv[0]) == 0)
+       disallow_gui = TRUE;
+
+    /* TODO: On MacOS X default to gui if argv[0] ends in:
+     *       /Vim.app/Contents/MacOS/Vim */
 #endif

-#if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAPPING)
-    /* If we don't have rcfile support, --disable-wrapping-as-root is
-     * used, and we're root, turn wrapping off. */
-    if (geteuid() == NANO_ROOT_UID)
-       SET(NO_WRAP);
+#ifdef FEAT_EVAL
+    set_vim_var_string(VV_PROGNAME, initstr, -1);
 #endif

-    while ((optchr =
-#ifdef HAVE_GETOPT_LONG
-       getopt_long(argc, argv,
-               "h?ABC:DEFGHIKLNOPQ:RST:UVWY:abcdefgijklmo:pqr:s:tuvwxz$",
-               long_options, NULL)
+    if (TOLOWER_ASC(initstr[0]) == 'r')
+    {
+       restricted = TRUE;
+       ++initstr;
+    }
+
+    /* Use evim mode for "evim" and "egvim", not for "editor". */
+    if (TOLOWER_ASC(initstr[0]) == 'e'
+           && (TOLOWER_ASC(initstr[1]) == 'v'
+               || TOLOWER_ASC(initstr[1]) == 'g'))
+    {
+#ifdef FEAT_GUI
+       gui.starting = TRUE;
+#endif
+       parmp->evim_mode = TRUE;
+       ++initstr;
+    }
+
+    /* "gvim" starts the GUI.  Also accept "Gvim" for MS-Windows. */
+    if (TOLOWER_ASC(initstr[0]) == 'g')
+    {
+       main_start_gui();
+#ifdef FEAT_GUI
+       ++initstr;
+#endif
+    }
+
+    if (STRNICMP(initstr, "view", 4) == 0)
+    {
+       readonlymode = TRUE;
+       curbuf->b_p_ro = TRUE;
+       p_uc = 10000;                   /* don't update very often */
+       initstr += 4;
+    }
+    else if (STRNICMP(initstr, "vim", 3) == 0)
+       initstr += 3;
+
+    /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
+    if (STRICMP(initstr, "diff") == 0)
+    {
+#ifdef FEAT_DIFF
+       parmp->diff_mode = TRUE;
 #else
-       getopt(argc, argv,
-               "h?ABC:DEFGHIKLNOPQ:RST:UVWY:abcdefgijklmo:pqr:s:tuvwxz$")
+       mch_errmsg(_("This Vim was not compiled with the diff feature."));
+       mch_errmsg("\n");
+       mch_exit(2);
 #endif
-               ) != -1) {
-       switch (optchr) {
-           case 'a':
-           case 'b':
-           case 'e':
-           case 'f':
-           case 'g':
-           case 'j':
-               /* Pico compatibility flags. */
-               break;
-#ifndef NANO_TINY
-           case 'A':
-               SET(SMART_HOME);
-               break;
-           case 'B':
-               SET(BACKUP_FILE);
-               break;
-           case 'C':
-               backup_dir = mallocstrcpy(backup_dir, optarg);
-               break;
+    }
+
+    if (STRNICMP(initstr, "ex", 2) == 0)
+    {
+       if (STRNICMP(initstr + 2, "im", 2) == 0)
+           exmode_active = EXMODE_VIM;
+       else
+           exmode_active = EXMODE_NORMAL;
+       change_compatible(TRUE);        /* set 'compatible' */
+    }
+}
+
+/*
+ * Get the name of the display, before gui_prepare() removes it from
+ * argv[].  Used for the xterm-clipboard display.
+ *
+ * Also find the --server... arguments and --socketid and --windowid
+ */
+    static void
+early_arg_scan(parmp)
+    mparm_T    *parmp UNUSED;
+{
+#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
+       || !defined(FEAT_NETBEANS_INTG)
+    int                argc = parmp->argc;
+    char       **argv = parmp->argv;
+    int                i;
+
+    for (i = 1; i < argc; i++)
+    {
+       if (STRCMP(argv[i], "--") == 0)
+           break;
+# ifdef FEAT_XCLIPBOARD
+       else if (STRICMP(argv[i], "-display") == 0
+#  if defined(FEAT_GUI_GTK)
+               || STRICMP(argv[i], "--display") == 0
+#  endif
+               )
+       {
+           if (i == argc - 1)
+               mainerr_arg_missing((char_u *)argv[i]);
+           xterm_display = argv[++i];
+       }
+# endif
+# ifdef FEAT_CLIENTSERVER
+       else if (STRICMP(argv[i], "--servername") == 0)
+       {
+           if (i == argc - 1)
+               mainerr_arg_missing((char_u *)argv[i]);
+           parmp->serverName_arg = (char_u *)argv[++i];
+       }
+       else if (STRICMP(argv[i], "--serverlist") == 0)
+           parmp->serverArg = TRUE;
+       else if (STRNICMP(argv[i], "--remote", 8) == 0)
+       {
+           parmp->serverArg = TRUE;
+#  ifdef FEAT_GUI
+           if (strstr(argv[i], "-wait") != 0)
+               /* don't fork() when starting the GUI to edit files ourself */
+               gui.dofork = FALSE;
+#  endif
+       }
+# endif
+
+# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
+#  ifdef FEAT_GUI_W32
+       else if (STRICMP(argv[i], "--windowid") == 0)
+#  else
+       else if (STRICMP(argv[i], "--socketid") == 0)
+#  endif
+       {
+           long_u      id;
+           int         count;
+
+           if (i == argc - 1)
+               mainerr_arg_missing((char_u *)argv[i]);
+           if (STRNICMP(argv[i+1], "0x", 2) == 0)
+               count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
+           else
+               count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
+           if (count != 1)
+               mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
+           else
+#  ifdef FEAT_GUI_W32
+               win_socket_id = id;
+#  else
+               gtk_socket_id = id;
+#  endif
+           i++;
+       }
+# endif
+# ifdef FEAT_GUI_GTK
+       else if (STRICMP(argv[i], "--echo-wid") == 0)
+           echo_wid_arg = TRUE;
+# endif
+# ifndef FEAT_NETBEANS_INTG
+       else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
+       {
+           mch_errmsg(_("'-nb' cannot be used: not enabled at compile 
time\n"));
+           mch_exit(2);
+       }
+# endif
+
+    }
 #endif
-           case 'D':
-               SET(BOLD_TEXT);
-               break;
-#ifndef NANO_TINY
-           case 'E':
-               SET(TABS_TO_SPACES);
-               break;
+}
+
+/*
+ * Scan the command line arguments.
+ */
+    static void
+command_line_scan(parmp)
+    mparm_T    *parmp;
+{
+    int                argc = parmp->argc;
+    char       **argv = parmp->argv;
+    int                argv_idx;               /* index in argv[n][] */
+    int                had_minmin = FALSE;     /* found "--" argument */
+    int                want_argument;          /* option argument with 
argument */
+    int                c;
+    char_u     *p = NULL;
+    long       n;
+
+    --argc;
+    ++argv;
+    argv_idx = 1;          /* active option letter is argv[0][argv_idx] */
+    while (argc > 0)
+    {
+       /*
+        * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
+        */
+       if (argv[0][0] == '+' && !had_minmin)
+       {
+           if (parmp->n_commands >= MAX_ARG_CMDS)
+               mainerr(ME_EXTRA_CMD, NULL);
+           argv_idx = -1;          /* skip to next argument */
+           if (argv[0][1] == NUL)
+               parmp->commands[parmp->n_commands++] = (char_u *)"$";
+           else
+               parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
+       }
+
+       /*
+        * Optional argument.
+        */
+       else if (argv[0][0] == '-' && !had_minmin)
+       {
+           want_argument = FALSE;
+           c = argv[0][argv_idx++];
+#ifdef VMS
+           /*
+            * VMS only uses upper case command lines.  Interpret "-X" as "-x"
+            * and "-/X" as "-X".
+            */
+           if (c == '/')
+           {
+               c = argv[0][argv_idx++];
+               c = TOUPPER_ASC(c);
+           }
+           else
+               c = TOLOWER_ASC(c);
 #endif
-#ifdef ENABLE_MULTIBUFFER
-           case 'F':
-               SET(MULTIBUFFER);
+           switch (c)
+           {
+           case NUL:           /* "vim -"  read from stdin */
+                               /* "ex -" silent mode */
+               if (exmode_active)
+                   silent_mode = TRUE;
+               else
+               {
+                   if (parmp->edit_type != EDIT_NONE)
+                       mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
+                   parmp->edit_type = EDIT_STDIN;
+                   read_cmd_fd = 2;    /* read from stderr instead of stdin */
+               }
+               argv_idx = -1;          /* skip to next argument */
                break;
+
+           case '-':           /* "--" don't take any more option arguments */
+                               /* "--help" give help message */
+                               /* "--version" give version message */
+                               /* "--literal" take files literally */
+                               /* "--nofork" don't fork */
+                               /* "--noplugin[s]" skip plugins */
+                               /* "--cmd <cmd>" execute cmd before vimrc */
+               if (STRICMP(argv[0] + argv_idx, "help") == 0)
+                   usage();
+               else if (STRICMP(argv[0] + argv_idx, "version") == 0)
+               {
+                   Columns = 80;       /* need to init Columns */
+                   info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
+                   list_version();
+                   msg_putchar('\n');
+                   msg_didout = FALSE;
+                   mch_exit(0);
+               }
+               else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
+               {
+#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
+                   parmp->literal = TRUE;
 #endif
-#ifdef ENABLE_NANORC
-#ifndef NANO_TINY
-           case 'G':
-               SET(LOCKING);
-               break;
-           case 'H':
-               SET(HISTORYLOG);
-               break;
+               }
+               else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
+               {
+#ifdef FEAT_GUI
+                   gui.dofork = FALSE; /* don't fork() when starting GUI */
 #endif
-           case 'I':
-               no_rcfiles = TRUE;
-               break;
+               }
+               else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
+                   p_lpl = FALSE;
+               else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
+               {
+                   want_argument = TRUE;
+                   argv_idx += 3;
+               }
+               else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
+               {
+                   want_argument = TRUE;
+                   argv_idx += 11;
+               }
+#ifdef FEAT_CLIENTSERVER
+               else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
+                   ; /* already processed -- no arg */
+               else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
+                      || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
+               {
+                   /* already processed -- snatch the following arg */
+                   if (argc > 1)
+                   {
+                       --argc;
+                       ++argv;
+                   }
+               }
 #endif
-           case 'K':
-               SET(REBIND_KEYPAD);
+#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
+# ifdef FEAT_GUI_GTK
+               else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
+# else
+               else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
+# endif
+               {
+                   /* already processed -- snatch the following arg */
+                   if (argc > 1)
+                   {
+                       --argc;
+                       ++argv;
+                   }
+               }
+#endif
+#ifdef FEAT_GUI_GTK
+               else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
+               {
+                   /* already processed, skip */
+               }
+#endif
+               else
+               {
+                   if (argv[0][argv_idx])
+                       mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
+                   had_minmin = TRUE;
+               }
+               if (!want_argument)
+                   argv_idx = -1;      /* skip to next argument */
                break;
-           case 'L':
-               SET(NO_NEWLINES);
+
+           case 'A':           /* "-A" start in Arabic mode */
+#ifdef FEAT_ARABIC
+               set_option_value((char_u *)"arabic", 1L, NULL, 0);
+#else
+               mch_errmsg(_(e_noarabic));
+               mch_exit(2);
+#endif
                break;
-#ifndef NANO_TINY
-           case 'N':
-               SET(NO_CONVERT);
+
+           case 'b':           /* "-b" binary mode */
+               /* Needs to be effective before expanding file names, because
+                * for Win32 this makes us edit a shortcut file itself,
+                * instead of the file it links to. */
+               set_options_bin(curbuf->b_p_bin, 1, 0);
+               curbuf->b_p_bin = 1;        /* binary file I/O */
                break;
-#endif
-           case 'O':
-               SET(MORE_SPACE);
+
+           case 'C':           /* "-C"  Compatible */
+               change_compatible(TRUE);
                break;
-#ifndef NANO_TINY
-           case 'P':
-               SET(POS_HISTORY);
+
+           case 'e':           /* "-e" Ex mode */
+               exmode_active = EXMODE_NORMAL;
                break;
-#endif
-#ifndef DISABLE_JUSTIFY
-           case 'Q':
-               quotestr = mallocstrcpy(quotestr, optarg);
+
+           case 'E':           /* "-E" Improved Ex mode */
+               exmode_active = EXMODE_VIM;
                break;
+
+           case 'f':           /* "-f"  GUI: run in foreground.  Amiga: open
+                               window directly, not with newcli */
+#ifdef FEAT_GUI
+               gui.dofork = FALSE;     /* don't fork() when starting GUI */
 #endif
-           case 'R':
-               SET(RESTRICTED);
                break;
-#ifndef NANO_TINY
-           case 'S':
-               SET(SMOOTH_SCROLL);
+
+           case 'g':           /* "-g" start GUI */
+               main_start_gui();
                break;
+
+           case 'F':           /* "-F" start in Farsi mode: rl + fkmap set */
+#ifdef FEAT_FKMAP
+               p_fkmap = TRUE;
+               set_option_value((char_u *)"rl", 1L, NULL, 0);
+#else
+               mch_errmsg(_(e_nofarsi));
+               mch_exit(2);
 #endif
-           case 'T':
-               if (!parse_num(optarg, &tabsize) || tabsize <= 0) {
-                   fprintf(stderr, _("Requested tab size \"%s\" is invalid"), 
optarg);
-                   fprintf(stderr, "\n");
-                   exit(1);
-               }
-               break;
-#ifndef NANO_TINY
-           case 'U':
-               SET(QUICK_BLANK);
                break;
+
+           case 'h':           /* "-h" give help message */
+#ifdef FEAT_GUI_GNOME
+               /* Tell usage() to exit for "gvim". */
+               gui.starting = FALSE;
 #endif
-           case 'V':
-               version();
-               exit(0);
-#ifndef NANO_TINY
-           case 'W':
-               SET(WORD_BOUNDS);
+               usage();
                break;
+
+           case 'H':           /* "-H" start in Hebrew mode: rl + hkmap set */
+#ifdef FEAT_RIGHTLEFT
+               p_hkmap = TRUE;
+               set_option_value((char_u *)"rl", 1L, NULL, 0);
+#else
+               mch_errmsg(_(e_nohebrew));
+               mch_exit(2);
 #endif
-#ifdef ENABLE_COLOR
-           case 'Y':
-               syntaxstr = mallocstrcpy(syntaxstr, optarg);
                break;
+
+           case 'l':           /* "-l" lisp mode, 'lisp' and 'showmatch' on */
+#ifdef FEAT_LISP
+               set_option_value((char_u *)"lisp", 1L, NULL, 0);
+               p_sm = TRUE;
 #endif
-           case 'c':
-               SET(CONST_UPDATE);
                break;
-           case 'd':
-               SET(REBIND_DELETE);
+
+           case 'M':           /* "-M"  no changes or writing of files */
+               reset_modifiable();
+               /* FALLTHROUGH */
+
+           case 'm':           /* "-m"  no writing of files */
+               p_write = FALSE;
                break;
-#ifndef NANO_TINY
-           case 'i':
-               SET(AUTOINDENT);
+
+           case 'y':           /* "-y"  easy mode */
+#ifdef FEAT_GUI
+               gui.starting = TRUE;    /* start GUI a bit later */
+#endif
+               parmp->evim_mode = TRUE;
                break;
-           case 'k':
-               SET(CUT_TO_END);
+
+           case 'N':           /* "-N"  Nocompatible */
+               change_compatible(FALSE);
                break;
+
+           case 'n':           /* "-n" no swap file */
+#ifdef FEAT_NETBEANS_INTG
+               /* checking for "-nb", netbeans parameters */
+               if (argv[0][argv_idx] == 'b')
+               {
+                   netbeansArg = argv[0];
+                   argv_idx = -1;          /* skip to next argument */
+               }
+               else
 #endif
-           case 'l':
-               SET(NOFOLLOW_SYMLINKS);
-               break;
-#ifndef DISABLE_MOUSE
-           case 'm':
-               SET(USE_MOUSE);
+               parmp->no_swap_file = TRUE;
                break;
+
+           case 'p':           /* "-p[N]" open N tab pages */
+#ifdef TARGET_API_MAC_OSX
+               /* For some reason on MacOS X, an argument like:
+                  -psn_0_10223617 is passed in when invoke from Finder
+                  or with the 'open' command */
+               if (argv[0][argv_idx] == 's')
+               {
+                   argv_idx = -1; /* bypass full -psn */
+                   main_start_gui();
+                   break;
+               }
+#endif
+#ifdef FEAT_WINDOWS
+               /* default is 0: open window for each file */
+               parmp->window_count = get_number_arg((char_u *)argv[0],
+                                                               &argv_idx, 0);
+               parmp->window_layout = WIN_TABS;
 #endif
-#ifndef DISABLE_OPERATINGDIR
-           case 'o':
-               operating_dir = mallocstrcpy(operating_dir, optarg);
                break;
+
+           case 'o':           /* "-o[N]" open N horizontal split windows */
+#ifdef FEAT_WINDOWS
+               /* default is 0: open window for each file */
+               parmp->window_count = get_number_arg((char_u *)argv[0],
+                                                               &argv_idx, 0);
+               parmp->window_layout = WIN_HOR;
 #endif
-           case 'p':
-               SET(PRESERVE);
                break;
-           case 'q':
-               SET(QUIET);
+
+               case 'O':       /* "-O[N]" open N vertical split windows */
+#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
+               /* default is 0: open window for each file */
+               parmp->window_count = get_number_arg((char_u *)argv[0],
+                                                               &argv_idx, 0);
+               parmp->window_layout = WIN_VER;
+#endif
                break;
-#ifndef DISABLE_WRAPJUSTIFY
-           case 'r':
-               if (!parse_num(optarg, &wrap_at)) {
-                   fprintf(stderr, _("Requested fill size \"%s\" is invalid"), 
optarg);
-                   fprintf(stderr, "\n");
-                   exit(1);
+
+#ifdef FEAT_QUICKFIX
+           case 'q':           /* "-q" QuickFix mode */
+               if (parmp->edit_type != EDIT_NONE)
+                   mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
+               parmp->edit_type = EDIT_QF;
+               if (argv[0][argv_idx])          /* "-q{errorfile}" */
+               {
+                   parmp->use_ef = (char_u *)argv[0] + argv_idx;
+                   argv_idx = -1;
                }
-               fill_used = TRUE;
+               else if (argc > 1)              /* "-q {errorfile}" */
+                   want_argument = TRUE;
                break;
 #endif
-#ifndef DISABLE_SPELLER
+
+           case 'R':           /* "-R" readonly mode */
+               readonlymode = TRUE;
+               curbuf->b_p_ro = TRUE;
+               p_uc = 10000;                   /* don't update very often */
+               break;
+
+           case 'r':           /* "-r" recovery mode */
+           case 'L':           /* "-L" recovery mode */
+               recoverymode = 1;
+               break;
+
            case 's':
-               alt_speller = mallocstrcpy(alt_speller, optarg);
+               if (exmode_active)      /* "-s" silent (batch) mode */
+                   silent_mode = TRUE;
+               else            /* "-s {scriptin}" read from script file */
+                   want_argument = TRUE;
                break;
-#endif
-           case 't':
-               SET(TEMP_FILE);
+
+           case 't':           /* "-t {tag}" or "-t{tag}" jump to tag */
+               if (parmp->edit_type != EDIT_NONE)
+                   mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
+               parmp->edit_type = EDIT_TAG;
+               if (argv[0][argv_idx])          /* "-t{tag}" */
+               {
+                   parmp->tagname = (char_u *)argv[0] + argv_idx;
+                   argv_idx = -1;
+               }
+               else                            /* "-t {tag}" */
+                   want_argument = TRUE;
                break;
-#ifndef NANO_TINY
-           case 'u':
-               SET(UNDOABLE);
+
+#ifdef FEAT_EVAL
+           case 'D':           /* "-D"         Debugging */
+               parmp->use_debug_break_level = 9999;
                break;
 #endif
-           case 'v':
-               SET(VIEW_MODE);
+#ifdef FEAT_DIFF
+           case 'd':           /* "-d"         'diff' */
+# ifdef AMIGA
+               /* check for "-dev {device}" */
+               if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
+                   want_argument = TRUE;
+               else
+# endif
+                   parmp->diff_mode = TRUE;
                break;
-#ifndef DISABLE_WRAPPING
-           case 'w':
-               SET(NO_WRAP);
-               /* If both --fill and --nowrap are given on the
-                * command line, the last given option wins. */
-               fill_used = FALSE;
+#endif
+           case 'V':           /* "-V{N}"      Verbose level */
+               /* default is 10: a little bit verbose */
+               p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
+               if (argv[0][argv_idx] != NUL)
+               {
+                   set_option_value((char_u *)"verbosefile", 0L,
+                                            (char_u *)argv[0] + argv_idx, 0);
+                   argv_idx = (int)STRLEN(argv[0]);
+               }
                break;
+
+           case 'v':           /* "-v"  Vi-mode (as if called "vi") */
+               exmode_active = 0;
+#ifdef FEAT_GUI
+               gui.starting = FALSE;   /* don't start GUI */
 #endif
-           case 'x':
-               SET(NO_HELP);
                break;
-           case 'z':
-               SET(SUSPEND);
+
+           case 'w':           /* "-w{number}" set window height */
+                               /* "-w {scriptout}"     write to script */
+               if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
+               {
+                   n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
+                   set_option_value((char_u *)"window", n, NULL, 0);
+                   break;
+               }
+               want_argument = TRUE;
+               break;
+
+#ifdef FEAT_CRYPT
+           case 'x':           /* "-x"  encrypted reading/writing of files */
+               parmp->ask_for_key = TRUE;
+               break;
+#endif
+
+           case 'X':           /* "-X"  don't connect to X server */
+#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
+               x_no_connect = TRUE;
+#endif
                break;
-#ifndef NANO_TINY
-           case '$':
-               SET(SOFTWRAP);
+
+           case 'Z':           /* "-Z"  restricted mode */
+               restricted = TRUE;
                break;
+
+           case 'c':           /* "-c{command}" or "-c {command}" execute
+                                  command */
+               if (argv[0][argv_idx] != NUL)
+               {
+                   if (parmp->n_commands >= MAX_ARG_CMDS)
+                       mainerr(ME_EXTRA_CMD, NULL);
+                   parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
+                                                                  + argv_idx;
+                   argv_idx = -1;
+                   break;
+               }
+               /*FALLTHROUGH*/
+           case 'S':           /* "-S {file}" execute Vim script */
+           case 'i':           /* "-i {viminfo}" use for viminfo */
+#ifndef FEAT_DIFF
+           case 'd':           /* "-d {device}" device (for Amiga) */
 #endif
+           case 'T':           /* "-T {terminal}" terminal name */
+           case 'u':           /* "-u {vimrc}" vim inits file */
+           case 'U':           /* "-U {gvimrc}" gvim inits file */
+           case 'W':           /* "-W {scriptout}" overwrite */
+#ifdef FEAT_GUI_W32
+           case 'P':           /* "-P {parent title}" MDI parent */
+#endif
+               want_argument = TRUE;
+               break;
+
            default:
-               usage();
+               mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
+           }
+
+           /*
+            * Handle option arguments with argument.
+            */
+           if (want_argument)
+           {
+               /*
+                * Check for garbage immediately after the option letter.
+                */
+               if (argv[0][argv_idx] != NUL)
+                   mainerr(ME_GARBAGE, (char_u *)argv[0]);
+
+               --argc;
+               if (argc < 1 && c != 'S')  /* -S has an optional argument */
+                   mainerr_arg_missing((char_u *)argv[0]);
+               ++argv;
+               argv_idx = -1;
+
+               switch (c)
+               {
+               case 'c':       /* "-c {command}" execute command */
+               case 'S':       /* "-S {file}" execute Vim script */
+                   if (parmp->n_commands >= MAX_ARG_CMDS)
+                       mainerr(ME_EXTRA_CMD, NULL);
+                   if (c == 'S')
+                   {
+                       char    *a;
+
+                       if (argc < 1)
+                           /* "-S" without argument: use default session file
+                            * name. */
+                           a = SESSION_FILE;
+                       else if (argv[0][0] == '-')
+                       {
+                           /* "-S" followed by another option: use default
+                            * session file name. */
+                           a = SESSION_FILE;
+                           ++argc;
+                           --argv;
+                       }
+                       else
+                           a = argv[0];
+                       p = alloc((unsigned)(STRLEN(a) + 4));
+                       if (p == NULL)
+                           mch_exit(2);
+                       sprintf((char *)p, "so %s", a);
+                       parmp->cmds_tofree[parmp->n_commands] = TRUE;
+                       parmp->commands[parmp->n_commands++] = p;
+                   }
+                   else
+                       parmp->commands[parmp->n_commands++] =
+                                                           (char_u *)argv[0];
+                   break;
+
+               case '-':
+                   if (argv[-1][2] == 'c')
+                   {
+                       /* "--cmd {command}" execute command */
+                       if (parmp->n_pre_commands >= MAX_ARG_CMDS)
+                           mainerr(ME_EXTRA_CMD, NULL);
+                       parmp->pre_commands[parmp->n_pre_commands++] =
+                                                           (char_u *)argv[0];
+                   }
+                   /* "--startuptime <file>" already handled */
+                   break;
+
+           /*  case 'd':   -d {device} is handled in mch_check_win() for the
+            *              Amiga */
+
+#ifdef FEAT_QUICKFIX
+               case 'q':       /* "-q {errorfile}" QuickFix mode */
+                   parmp->use_ef = (char_u *)argv[0];
+                   break;
+#endif
+
+               case 'i':       /* "-i {viminfo}" use for viminfo */
+                   use_viminfo = (char_u *)argv[0];
+                   break;
+
+               case 's':       /* "-s {scriptin}" read from script file */
+                   if (scriptin[0] != NULL)
+                   {
+scripterror:
+                       mch_errmsg(_("Attempt to open script file again: \""));
+                       mch_errmsg(argv[-1]);
+                       mch_errmsg(" ");
+                       mch_errmsg(argv[0]);
+                       mch_errmsg("\"\n");
+                       mch_exit(2);
+                   }
+                   if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
+                   {
+                       mch_errmsg(_("Cannot open for reading: \""));
+                       mch_errmsg(argv[0]);
+                       mch_errmsg("\"\n");
+                       mch_exit(2);
+                   }
+                   if (save_typebuf() == FAIL)
+                       mch_exit(2);    /* out of memory */
+                   break;
+
+               case 't':       /* "-t {tag}" */
+                   parmp->tagname = (char_u *)argv[0];
+                   break;
+
+               case 'T':       /* "-T {terminal}" terminal name */
+                   /*
+                    * The -T term argument is always available and when
+                    * HAVE_TERMLIB is supported it overrides the environment
+                    * variable TERM.
+                    */
+#ifdef FEAT_GUI
+                   if (term_is_gui((char_u *)argv[0]))
+                       gui.starting = TRUE;    /* start GUI a bit later */
+                   else
+#endif
+                       parmp->term = (char_u *)argv[0];
+                   break;
+
+               case 'u':       /* "-u {vimrc}" vim inits file */
+                   parmp->use_vimrc = (char_u *)argv[0];
+                   break;
+
+               case 'U':       /* "-U {gvimrc}" gvim inits file */
+#ifdef FEAT_GUI
+                   use_gvimrc = (char_u *)argv[0];
+#endif
+                   break;
+
+               case 'w':       /* "-w {nr}" 'window' value */
+                               /* "-w {scriptout}" append to script file */
+                   if (vim_isdigit(*((char_u *)argv[0])))
+                   {
+                       argv_idx = 0;
+                       n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
+                       set_option_value((char_u *)"window", n, NULL, 0);
+                       argv_idx = -1;
+                       break;
+                   }
+                   /*FALLTHROUGH*/
+               case 'W':       /* "-W {scriptout}" overwrite script file */
+                   if (scriptout != NULL)
+                       goto scripterror;
+                   if ((scriptout = mch_fopen(argv[0],
+                                   c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
+                   {
+                       mch_errmsg(_("Cannot open for script output: \""));
+                       mch_errmsg(argv[0]);
+                       mch_errmsg("\"\n");
+                       mch_exit(2);
+                   }
+                   break;
+
+#ifdef FEAT_GUI_W32
+               case 'P':               /* "-P {parent title}" MDI parent */
+                   gui_mch_set_parent(argv[0]);
+                   break;
+#endif
+               }
+           }
        }
-    }

-    /* If the executable filename starts with 'r', enable restricted
-     * mode. */
-    if (*(tail(argv[0])) == 'r')
-       SET(RESTRICTED);
+       /*
+        * File name argument.
+        */
+       else
+       {
+           argv_idx = -1;          /* skip to next argument */
+
+           /* Check for only one type of editing. */
+           if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
+               mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
+           parmp->edit_type = EDIT_FILE;

-    /* If we're using restricted mode, disable suspending, backups, and
-     * reading rcfiles, since they all would allow reading from or
-     * writing to files not specified on the command line. */
-    if (ISSET(RESTRICTED)) {
-       UNSET(SUSPEND);
-       UNSET(BACKUP_FILE);
-#ifdef ENABLE_NANORC
-       no_rcfiles = TRUE;
+#ifdef MSWIN
+           /* Remember if the argument was a full path before changing
+            * slashes to backslashes. */
+           if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
+               parmp->full_path = TRUE;
+#endif
+
+           /* Add the file to the global argument list. */
+           if (ga_grow(&global_alist.al_ga, 1) == FAIL
+                   || (p = vim_strsave((char_u *)argv[0])) == NULL)
+               mch_exit(2);
+#ifdef FEAT_DIFF
+           if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
+                                     && !mch_isdir(alist_name(&GARGLIST[0])))
+           {
+               char_u      *r;
+
+               r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
+               if (r != NULL)
+               {
+                   vim_free(p);
+                   p = r;
+               }
+           }
+#endif
+#if defined(__CYGWIN32__) && !defined(WIN32)
+           /*
+            * If vim is invoked by non-Cygwin tools, convert away any
+            * DOS paths, so things like .swp files are created correctly.
+            * Look for evidence of non-Cygwin paths before we bother.
+            * This is only for when using the Unix files.
+            */
+           if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
+           {
+               char posix_path[PATH_MAX];
+
+# if CYGWIN_VERSION_DLL_MAJOR >= 1007
+               cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
+# else
+               cygwin_conv_to_posix_path(p, posix_path);
+# endif
+               vim_free(p);
+               p = vim_strsave((char_u *)posix_path);
+               if (p == NULL)
+                   mch_exit(2);
+           }
 #endif
+
+#ifdef USE_FNAME_CASE
+           /* Make the case of the file name match the actual file. */
+           fname_case(p, 0);
+#endif
+
+           alist_add(&global_alist, p,
+#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
+                   parmp->literal ? 2 : 0      /* add buffer nr after exp. */
+#else
+                   2           /* add buffer number now and use curbuf */
+#endif
+                   );
+
+#if defined(FEAT_MBYTE) && defined(WIN32)
+           {
+               /* Remember this argument has been added to the argument list.
+                * Needed when 'encoding' is changed. */
+               used_file_arg(argv[0], parmp->literal, parmp->full_path,
+# ifdef FEAT_DIFF
+                                                           parmp->diff_mode
+# else
+                                                           FALSE
+# endif
+                                                           );
+           }
+#endif
+       }
+
+       /*
+        * If there are no more letters after the current "-", go to next
+        * argument.  argv_idx is set to -1 when the current argument is to be
+        * skipped.
+        */
+       if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
+       {
+           --argc;
+           ++argv;
+           argv_idx = 1;
+       }
     }

-    /* Set up the shortcut lists.
-     * Need to do this before the rcfile. */
-    shortcut_init(FALSE);
+#ifdef FEAT_EVAL
+    /* If there is a "+123" or "-c" command, set v:swapcommand to the first
+     * one. */
+    if (parmp->n_commands > 0)
+    {
+       p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
+       if (p != NULL)
+       {
+           sprintf((char *)p, ":%s\r", parmp->commands[0]);
+           set_vim_var_string(VV_SWAPCOMMAND, p, -1);
+           vim_free(p);
+       }
+    }
+#endif
+}
+
+/*
+ * Print a warning if stdout is not a terminal.
+ * When starting in Ex mode and commands come from a file, set Silent mode.
+ */
+    static void
+check_tty(parmp)
+    mparm_T    *parmp;
+{
+    int                input_isatty;           /* is active input a terminal? 
*/

-/* We've read through the command line options.  Now back up the flags
- * and values that are set, and read the rcfile(s).  If the values
- * haven't changed afterward, restore the backed-up values. */
-#ifdef ENABLE_NANORC
-    if (!no_rcfiles) {
-#ifndef DISABLE_OPERATINGDIR
-       char *operating_dir_cpy = operating_dir;
+    input_isatty = mch_input_isatty();
+    if (exmode_active)
+    {
+       if (!input_isatty)
+           silent_mode = TRUE;
+    }
+    else if (parmp->want_full_screen && (!parmp->stdout_isatty ||
!input_isatty)
+#ifdef FEAT_GUI
+           /* don't want the delay when started from the desktop */
+           && !gui.starting
 #endif
-#ifndef DISABLE_WRAPJUSTIFY
-       ssize_t wrap_at_cpy = wrap_at;
+           )
+    {
+#ifdef NBDEBUG
+       /*
+        * This shouldn't be necessary. But if I run netbeans with the log
+        * output coming to the console and XOpenDisplay fails, I get vim
+        * trying to start with input/output to my console tty.  This fills my
+        * input buffer so fast I can't even kill the process in under 2
+        * minutes (and it beeps continuously the whole time :-)
+        */
+       if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
+       {
+           mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
+           exit(1);
+       }
 #endif
-#ifndef NANO_TINY
-       char *backup_dir_cpy = backup_dir;
+       if (!parmp->stdout_isatty)
+           mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
+       if (!input_isatty)
+           mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
+       out_flush();
+       if (scriptin[0] == NULL)
+           ui_delay(2000L, TRUE);
+       TIME_MSG("Warning delay");
+    }
+}
+
+/*
+ * Read text from stdin.
+ */
+    static void
+read_stdin()
+{
+    int            i;
+
+#if defined(HAS_SWAP_EXISTS_ACTION)
+    /* When getting the ATTENTION prompt here, use a dialog */
+    swap_exists_action = SEA_DIALOG;
 #endif
-#ifndef DISABLE_JUSTIFY
-       char *quotestr_cpy = quotestr;
+    no_wait_return = TRUE;
+    i = msg_didany;
+    set_buflisted(TRUE);
+    (void)open_buffer(TRUE, NULL, 0);  /* create memfile and read file */
+    no_wait_return = FALSE;
+    msg_didany = i;
+    TIME_MSG("reading stdin");
+#if defined(HAS_SWAP_EXISTS_ACTION)
+    check_swap_exists_action();
 #endif
-#ifndef DISABLE_SPELLER
-       char *alt_speller_cpy = alt_speller;
+#if !(defined(AMIGA) || defined(MACOS))
+    /*
+     * Close stdin and dup it from stderr.  Required for GPM to work
+     * properly, and for running external commands.
+     * Is there any other system that cannot do this?
+     */
+    close(0);
+    ignored = dup(2);
 #endif
-       ssize_t tabsize_cpy = tabsize;
-       unsigned flags_cpy[sizeof(flags) / sizeof(flags[0])];
-       size_t i;
+}
+
+/*
+ * Create the requested number of windows and edit buffers in them.
+ * Also does recovery if "recoverymode" set.
+ */
+    static void
+create_windows(parmp)
+    mparm_T    *parmp UNUSED;
+{
+#ifdef FEAT_WINDOWS
+    int                dorewind;
+    int                done = 0;

-       memcpy(flags_cpy, flags, sizeof(flags_cpy));
+    /*
+     * Create the number of windows that was requested.
+     */
+    if (parmp->window_count == -1)     /* was not set */
+       parmp->window_count = 1;
+    if (parmp->window_count == 0)
+       parmp->window_count = GARGCOUNT;
+    if (parmp->window_count > 1)
+    {
+       /* Don't change the windows if there was a command in .vimrc that
+        * already split some windows */
+       if (parmp->window_layout == 0)
+           parmp->window_layout = WIN_HOR;
+       if (parmp->window_layout == WIN_TABS)
+       {
+           parmp->window_count = make_tabpages(parmp->window_count);
+           TIME_MSG("making tab pages");
+       }
+       else if (firstwin->w_next == NULL)
+       {
+           parmp->window_count = make_windows(parmp->window_count,
+                                            parmp->window_layout == WIN_VER);
+           TIME_MSG("making windows");
+       }
+       else
+           parmp->window_count = win_count();
+    }
+    else
+       parmp->window_count = 1;
+#endif

-#ifndef DISABLE_OPERATINGDIR
-       operating_dir = NULL;
+    if (recoverymode)                  /* do recover */
+    {
+       msg_scroll = TRUE;              /* scroll message up */
+       ml_recover();
+       if (curbuf->b_ml.ml_mfp == NULL) /* failed */
+           getout(1);
+       do_modelines(0);                /* do modelines */
+    }
+    else
+    {
+       /*
+        * Open a buffer for windows that don't have one yet.
+        * Commands in the .vimrc might have loaded a file or split the window.
+        * Watch out for autocommands that delete a window.
+        */
+#ifdef FEAT_AUTOCMD
+       /*
+        * Don't execute Win/Buf Enter/Leave autocommands here
+        */
+       ++autocmd_no_enter;
+       ++autocmd_no_leave;
 #endif
-#ifndef NANO_TINY
-       backup_dir = NULL;
+#ifdef FEAT_WINDOWS
+       dorewind = TRUE;
+       while (done++ < 1000)
+       {
+           if (dorewind)
+           {
+               if (parmp->window_layout == WIN_TABS)
+                   goto_tabpage(1);
+               else
+                   curwin = firstwin;
+           }
+           else if (parmp->window_layout == WIN_TABS)
+           {
+               if (curtab->tp_next == NULL)
+                   break;
+               goto_tabpage(0);
+           }
+           else
+           {
+               if (curwin->w_next == NULL)
+                   break;
+               curwin = curwin->w_next;
+           }
+           dorewind = FALSE;
 #endif
-#ifndef DISABLE_JUSTIFY
-       quotestr = NULL;
+           curbuf = curwin->w_buffer;
+           if (curbuf->b_ml.ml_mfp == NULL)
+           {
+#ifdef FEAT_FOLDING
+               /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
+               if (p_fdls >= 0)
+                   curwin->w_p_fdl = p_fdls;
 #endif
-#ifndef DISABLE_SPELLER
-       alt_speller = NULL;
+#if defined(HAS_SWAP_EXISTS_ACTION)
+               /* When getting the ATTENTION prompt here, use a dialog */
+               swap_exists_action = SEA_DIALOG;
 #endif
+               set_buflisted(TRUE);

-       do_rcfile();
+               /* create memfile, read file */
+               (void)open_buffer(FALSE, NULL, 0);

-#ifdef DEBUG
-        fprintf(stderr, "After rebinding keys...\n");
-        print_sclist();
+#if defined(HAS_SWAP_EXISTS_ACTION)
+               if (swap_exists_action == SEA_QUIT)
+               {
+                   if (got_int || only_one_window())
+                   {
+                       /* abort selected or quit and only one window */
+                       did_emsg = FALSE;   /* avoid hit-enter prompt */
+                       getout(1);
+                   }
+                   /* We can't close the window, it would disturb what
+                    * happens next.  Clear the file name and set the arg
+                    * index to -1 to delete it later. */
+                   setfname(curbuf, NULL, NULL, FALSE);
+                   curwin->w_arg_idx = -1;
+                   swap_exists_action = SEA_NONE;
+               }
+               else
+                   handle_swap_exists(NULL);
 #endif
+#ifdef FEAT_AUTOCMD
+               dorewind = TRUE;                /* start again */
+#endif
+           }
+#ifdef FEAT_WINDOWS
+           ui_breakcheck();
+           if (got_int)
+           {
+               (void)vgetc();  /* only break the file loading, not the rest */
+               break;
+           }
+       }
+#endif
+#ifdef FEAT_WINDOWS
+       if (parmp->window_layout == WIN_TABS)
+           goto_tabpage(1);
+       else
+           curwin = firstwin;
+       curbuf = curwin->w_buffer;
+#endif
+#ifdef FEAT_AUTOCMD
+       --autocmd_no_enter;
+       --autocmd_no_leave;
+#endif
+    }
+}

-#ifndef DISABLE_OPERATINGDIR
-       if (operating_dir_cpy != NULL) {
-           free(operating_dir);
-           operating_dir = operating_dir_cpy;
+#ifdef FEAT_WINDOWS
+    /*
+     * If opened more than one window, start editing files in the other
+     * windows.  make_windows() has already opened the windows.
+     */
+    static void
+edit_buffers(parmp)
+    mparm_T    *parmp;
+{
+    int                arg_idx;                /* index in argument list */
+    int                i;
+    int                advance = TRUE;
+    win_T      *win;
+
+# ifdef FEAT_AUTOCMD
+    /*
+     * Don't execute Win/Buf Enter/Leave autocommands here
+     */
+    ++autocmd_no_enter;
+    ++autocmd_no_leave;
+# endif
+
+    /* When w_arg_idx is -1 remove the window (see create_windows()). */
+    if (curwin->w_arg_idx == -1)
+    {
+       win_close(curwin, TRUE);
+       advance = FALSE;
+    }
+
+    arg_idx = 1;
+    for (i = 1; i < parmp->window_count; ++i)
+    {
+       /* When w_arg_idx is -1 remove the window (see create_windows()). */
+       if (curwin->w_arg_idx == -1)
+       {
+           ++arg_idx;
+           win_close(curwin, TRUE);
+           advance = FALSE;
+           continue;
        }
+
+       if (advance)
+       {
+           if (parmp->window_layout == WIN_TABS)
+           {
+               if (curtab->tp_next == NULL)    /* just checking */
+                   break;
+               goto_tabpage(0);
+           }
+           else
+           {
+               if (curwin->w_next == NULL)     /* just checking */
+                   break;
+               win_enter(curwin->w_next, FALSE);
+           }
+       }
+       advance = TRUE;
+
+       /* Only open the file if there is no file in this window yet (that can
+        * happen when .vimrc contains ":sall"). */
+       if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
+       {
+           curwin->w_arg_idx = arg_idx;
+           /* Edit file from arg list, if there is one.  When "Quit" selected
+            * at the ATTENTION prompt close the window. */
+# ifdef HAS_SWAP_EXISTS_ACTION
+           swap_exists_did_quit = FALSE;
+# endif
+           (void)do_ecmd(0, arg_idx < GARGCOUNT
+                         ? alist_name(&GARGLIST[arg_idx]) : NULL,
+                         NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
+# ifdef HAS_SWAP_EXISTS_ACTION
+           if (swap_exists_did_quit)
+           {
+               /* abort or quit selected */
+               if (got_int || only_one_window())
+               {
+                   /* abort selected and only one window */
+                   did_emsg = FALSE;   /* avoid hit-enter prompt */
+                   getout(1);
+               }
+               win_close(curwin, TRUE);
+               advance = FALSE;
+           }
+# endif
+           if (arg_idx == GARGCOUNT - 1)
+               arg_had_last = TRUE;
+           ++arg_idx;
+       }
+       ui_breakcheck();
+       if (got_int)
+       {
+           (void)vgetc();      /* only break the file loading, not the rest */
+           break;
+       }
+    }
+
+    if (parmp->window_layout == WIN_TABS)
+       goto_tabpage(1);
+# ifdef FEAT_AUTOCMD
+    --autocmd_no_enter;
+# endif
+
+    /* make the first window the current window */
+    win = firstwin;
+#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
+    /* Avoid making a preview window the current window. */
+    while (win->w_p_pvw)
+    {
+       win = win->w_next;
+       if (win == NULL)
+       {
+           win = firstwin;
+           break;
+       }
+    }
+#endif
+    win_enter(win, FALSE);
+
+# ifdef FEAT_AUTOCMD
+    --autocmd_no_leave;
+# endif
+    TIME_MSG("editing files in windows");
+    if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
+       win_equal(curwin, FALSE, 'b');  /* adjust heights */
+}
+#endif /* FEAT_WINDOWS */
+
+/*
+ * Execute the commands from --cmd arguments "cmds[cnt]".
+ */
+    static void
+exe_pre_commands(parmp)
+    mparm_T    *parmp;
+{
+    char_u     **cmds = parmp->pre_commands;
+    int                cnt = parmp->n_pre_commands;
+    int                i;
+
+    if (cnt > 0)
+    {
+       curwin->w_cursor.lnum = 0; /* just in case.. */
+       sourcing_name = (char_u *)_("pre-vimrc command line");
+# ifdef FEAT_EVAL
+       current_SID = SID_CMDARG;
+# endif
+       for (i = 0; i < cnt; ++i)
+           do_cmdline_cmd(cmds[i]);
+       sourcing_name = NULL;
+# ifdef FEAT_EVAL
+       current_SID = 0;
+# endif
+       TIME_MSG("--cmd commands");
+    }
+}
+
+/*
+ * Execute "+", "-c" and "-S" arguments.
+ */
+    static void
+exe_commands(parmp)
+    mparm_T    *parmp;
+{
+    int                i;
+
+    /*
+     * We start commands on line 0, make "vim +/pat file" match a
+     * pattern on line 1.  But don't move the cursor when an autocommand
+     * with g`" was used.
+     */
+    msg_scroll = TRUE;
+    if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
+       curwin->w_cursor.lnum = 0;
+    sourcing_name = (char_u *)"command line";
+#ifdef FEAT_EVAL
+    current_SID = SID_CARG;
+#endif
+    for (i = 0; i < parmp->n_commands; ++i)
+    {
+       do_cmdline_cmd(parmp->commands[i]);
+       if (parmp->cmds_tofree[i])
+           vim_free(parmp->commands[i]);
+    }
+    sourcing_name = NULL;
+#ifdef FEAT_EVAL
+    current_SID = 0;
 #endif
-#ifndef DISABLE_WRAPJUSTIFY
-       if (fill_used)
-           wrap_at = wrap_at_cpy;
+    if (curwin->w_cursor.lnum == 0)
+       curwin->w_cursor.lnum = 1;
+
+    if (!exmode_active)
+       msg_scroll = FALSE;
+
+#ifdef FEAT_QUICKFIX
+    /* When started with "-q errorfile" jump to first error again. */
+    if (parmp->edit_type == EDIT_QF)
+       qf_jump(NULL, 0, 0, FALSE);
 #endif
-#ifndef NANO_TINY
-       if (backup_dir_cpy != NULL) {
-           free(backup_dir);
-           backup_dir = backup_dir_cpy;
+    TIME_MSG("executing command arguments");
+}
+
+/*
+ * Source startup scripts.
+ */
+    static void
+source_startup_scripts(parmp)
+    mparm_T    *parmp;
+{
+    int                i;
+
+    /*
+     * For "evim" source evim.vim first of all, so that the user can overrule
+     * any things he doesn't like.
+     */
+    if (parmp->evim_mode)
+    {
+       (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
+       TIME_MSG("source evim file");
+    }
+
+    /*
+     * If -u argument given, use only the initializations from that file and
+     * nothing else.
+     */
+    if (parmp->use_vimrc != NULL)
+    {
+       if (STRCMP(parmp->use_vimrc, "NONE") == 0
+                                    || STRCMP(parmp->use_vimrc, "NORC") == 0)
+       {
+#ifdef FEAT_GUI
+           if (use_gvimrc == NULL)         /* don't load gvimrc either */
+               use_gvimrc = parmp->use_vimrc;
+#endif
+           if (parmp->use_vimrc[2] == 'N')
+               p_lpl = FALSE;              /* don't load plugins either */
        }
+       else
+       {
+           if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
+               EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
+       }
+    }
+    else if (!silent_mode)
+    {
+#ifdef AMIGA
+       struct Process  *proc = (struct Process *)FindTask(0L);
+       APTR            save_winptr = proc->pr_WindowPtr;
+
+       /* Avoid a requester here for a volume that doesn't exist. */
+       proc->pr_WindowPtr = (APTR)-1L;
 #endif
-#ifndef DISABLE_JUSTIFY
-       if (quotestr_cpy != NULL) {
-           free(quotestr);
-           quotestr = quotestr_cpy;
+
+       /*
+        * Get system wide defaults, if the file name is defined.
+        */
+#ifdef SYS_VIMRC_FILE
+       (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
+#endif
+#ifdef MACOS_X
+       (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
+#endif
+
+       /*
+        * Try to read initialization commands from the following places:
+        * - environment variable VIMINIT
+        * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
+        * - second user vimrc file ($VIM/.vimrc for Dos)
+        * - environment variable EXINIT
+        * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
+        * - second user exrc file ($VIM/.exrc for Dos)
+        * The first that exists is used, the rest is ignored.
+        */
+       if (process_env((char_u *)"VIMINIT", TRUE) != OK)
+       {
+           if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
+#ifdef USR_VIMRC_FILE2
+               && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
+                                                          DOSO_VIMRC) == FAIL
+#endif
+#ifdef USR_VIMRC_FILE3
+               && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
+                                                          DOSO_VIMRC) == FAIL
+#endif
+#ifdef USR_VIMRC_FILE4
+               && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
+                                                          DOSO_VIMRC) == FAIL
+#endif
+               && process_env((char_u *)"EXINIT", FALSE) == FAIL
+               && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
+           {
+#ifdef USR_EXRC_FILE2
+               (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
+#endif
+           }
        }
+
+       /*
+        * Read initialization commands from ".vimrc" or ".exrc" in current
+        * directory.  This is only done if the 'exrc' option is set.
+        * Because of security reasons we disallow shell and write commands
+        * now, except for unix if the file is owned by the user or 'secure'
+        * option has been reset in environment of global ".exrc" or ".vimrc".
+        * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
+        * SYS_VIMRC_FILE.
+        */
+       if (p_exrc)
+       {
+#if defined(UNIX) || defined(VMS)
+           /* If ".vimrc" file is not owned by user, set 'secure' mode. */
+           if (!file_owned(VIMRC_FILE))
+#endif
+               secure = p_secure;
+
+           i = FAIL;
+           if (fullpathcmp((char_u *)USR_VIMRC_FILE,
+                                     (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
+#ifdef USR_VIMRC_FILE2
+                   && fullpathcmp((char_u *)USR_VIMRC_FILE2,
+                                     (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
+#endif
+#ifdef USR_VIMRC_FILE3
+                   && fullpathcmp((char_u *)USR_VIMRC_FILE3,
+                                     (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
+#endif
+#ifdef SYS_VIMRC_FILE
+                   && fullpathcmp((char_u *)SYS_VIMRC_FILE,
+                                     (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
 #endif
-#ifndef DISABLE_SPELLER
-       if (alt_speller_cpy != NULL) {
-           free(alt_speller);
-           alt_speller = alt_speller_cpy;
+                               )
+               i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
+
+           if (i == FAIL)
+           {
+#if defined(UNIX) || defined(VMS)
+               /* if ".exrc" is not owned by user set 'secure' mode */
+               if (!file_owned(EXRC_FILE))
+                   secure = p_secure;
+               else
+                   secure = 0;
+#endif
+               if (       fullpathcmp((char_u *)USR_EXRC_FILE,
+                                     (char_u *)EXRC_FILE, FALSE) != FPC_SAME
+#ifdef USR_EXRC_FILE2
+                       && fullpathcmp((char_u *)USR_EXRC_FILE2,
+                                     (char_u *)EXRC_FILE, FALSE) != FPC_SAME
+#endif
+                               )
+                   (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
+           }
        }
+       if (secure == 2)
+           need_wait_return = TRUE;
+       secure = 0;
+#ifdef AMIGA
+       proc->pr_WindowPtr = save_winptr;
+#endif
+    }
+    TIME_MSG("sourcing vimrc file(s)");
+}
+
+/*
+ * Setup to start using the GUI.  Exit with an error when not available.
+ */
+    static void
+main_start_gui()
+{
+#ifdef FEAT_GUI
+    gui.starting = TRUE;       /* start GUI a bit later */
+#else
+    mch_errmsg(_(e_nogvim));
+    mch_errmsg("\n");
+    mch_exit(2);
 #endif
-       if (tabsize_cpy != -1)
-           tabsize = tabsize_cpy;
+}

-       for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++)
-           flags[i] |= flags_cpy[i];
+#endif  /* NO_VIM_MAIN */
+
+/*
+ * Get an environment variable, and execute it as Ex commands.
+ * Returns FAIL if the environment variable was not executed, OK otherwise.
+ */
+    int
+process_env(env, is_viminit)
+    char_u     *env;
+    int                is_viminit; /* when TRUE, called for VIMINIT */
+{
+    char_u     *initstr;
+    char_u     *save_sourcing_name;
+    linenr_T   save_sourcing_lnum;
+#ifdef FEAT_EVAL
+    scid_T     save_sid;
+#endif
+
+    if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
+    {
+       if (is_viminit)
+           vimrc_found(NULL, NULL);
+       save_sourcing_name = sourcing_name;
+       save_sourcing_lnum = sourcing_lnum;
+       sourcing_name = env;
+       sourcing_lnum = 0;
+#ifdef FEAT_EVAL
+       save_sid = current_SID;
+       current_SID = SID_ENV;
+#endif
+       do_cmdline_cmd(initstr);
+       sourcing_name = save_sourcing_name;
+       sourcing_lnum = save_sourcing_lnum;
+#ifdef FEAT_EVAL
+       current_SID = save_sid;;
+#endif
+       return OK;
     }
-#ifdef DISABLE_ROOTWRAPPING
-    /* If we don't have any rcfiles, --disable-wrapping-as-root is used,
-     * and we're root, turn wrapping off. */
-    else if (geteuid() == NANO_ROOT_UID)
-       SET(NO_WRAP);
+    return FAIL;
+}
+
+#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
+/*
+ * Return TRUE if we are certain the user owns the file "fname".
+ * Used for ".vimrc" and ".exrc".
+ * Use both stat() and lstat() for extra security.
+ */
+    static int
+file_owned(fname)
+    char       *fname;
+{
+    struct stat s;
+# ifdef UNIX
+    uid_t      uid = getuid();
+# else  /* VMS */
+    uid_t      uid = ((getgid() << 16) | getuid());
+# endif
+
+    return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
+# ifdef HAVE_LSTAT
+           || mch_lstat(fname, &s) != 0 || s.st_uid != uid
+# endif
+           );
+}
 #endif
-#endif /* ENABLE_NANORC */

-#ifndef DISABLE_WRAPPING
-    /* Overwrite an rcfile "set nowrap" or --disable-wrapping-as-root
-       if a --fill option was given on the command line. */
-    if (fill_used)
-       UNSET(NO_WRAP);
+/*
+ * Give an error message main_errors["n"] and exit.
+ */
+    static void
+mainerr(n, str)
+    int                n;      /* one of the ME_ defines */
+    char_u     *str;   /* extra argument or NULL */
+{
+#if defined(UNIX) || defined(__EMX__) || defined(VMS)
+    reset_signals();           /* kill us with CTRL-C here, if you like */
 #endif

-    /* If we're using bold text instead of reverse video text, set it up
-     * now. */
-    if (ISSET(BOLD_TEXT))
-       reverse_attr = A_BOLD;
+    mch_errmsg(longVersion);
+    mch_errmsg("\n");
+    mch_errmsg(_(main_errors[n]));
+    if (str != NULL)
+    {
+       mch_errmsg(": \"");
+       mch_errmsg((char *)str);
+       mch_errmsg("\"");
+    }
+    mch_errmsg(_("\nMore info with: \"vim -h\"\n"));

-#ifndef NANO_TINY
-    /* Set up the search/replace history. */
-    history_init();
-#ifdef ENABLE_NANORC
-    if (!no_rcfiles) {
-       if (ISSET(HISTORYLOG) || ISSET(POS_HISTORY)) {
-           if (check_dotnano() == 0) {
-               UNSET(HISTORYLOG);
-               UNSET(POS_HISTORY);
-           }
-       }
-       if (ISSET(HISTORYLOG))
-           load_history();
-       if (ISSET(POS_HISTORY))
-           load_poshistory();
+    mch_exit(1);
+}
+
+    void
+mainerr_arg_missing(str)
+    char_u     *str;
+{
+    mainerr(ME_ARG_MISSING, str);
+}
+
+#ifndef NO_VIM_MAIN
+/*
+ * print a message with three spaces prepended and '\n' appended.
+ */
+    static void
+main_msg(s)
+    char *s;
+{
+    mch_msg("   ");
+    mch_msg(s);
+    mch_msg("\n");
+}
+
+/*
+ * Print messages for "vim -h" or "vim --help" and exit.
+ */
+    static void
+usage()
+{
+    int                i;
+    static char        *(use[]) =
+    {
+       N_("[file ..]       edit specified file(s)"),
+       N_("-               read text from stdin"),
+       N_("-t tag          edit file where tag is defined"),
+#ifdef FEAT_QUICKFIX
+       N_("-q [errorfile]  edit file with first error")
+#endif
+    };
+
+#if defined(UNIX) || defined(__EMX__) || defined(VMS)
+    reset_signals();           /* kill us with CTRL-C here, if you like */
+#endif
+
+    mch_msg(longVersion);
+    mch_msg(_("\n\nusage:"));
+    for (i = 0; ; ++i)
+    {
+       mch_msg(_(" vim [arguments] "));
+       mch_msg(_(use[i]));
+       if (i == (sizeof(use) / sizeof(char_u *)) - 1)
+           break;
+       mch_msg(_("\n   or:"));
     }
-#endif /* ENABLE_NANORC */
-#endif /* !NANO_TINY */
+#ifdef VMS
+    mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
+#endif

-#ifndef NANO_TINY
-    /* Set up the backup directory (unless we're using restricted mode,
-     * in which case backups are disabled, since they would allow
-     * reading from or writing to files not specified on the command
-     * line).  This entails making sure it exists and is a directory, so
-     * that backup files will be saved there. */
-    if (!ISSET(RESTRICTED))
-       init_backup_dir();
+    mch_msg(_("\n\nArguments:\n"));
+    main_msg(_("--\t\t\tOnly file names after this"));
+#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
+    main_msg(_("--literal\t\tDon't expand wildcards"));
+#endif
+#ifdef FEAT_OLE
+    main_msg(_("-register\t\tRegister this gvim for OLE"));
+    main_msg(_("-unregister\t\tUnregister gvim for OLE"));
+#endif
+#ifdef FEAT_GUI
+    main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
+    main_msg(_("-f  or  --nofork\tForeground: Don't fork when starting GUI"));
+#endif
+    main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
+    main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
+    main_msg(_("-E\t\t\tImproved Ex mode"));
+    main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
+#ifdef FEAT_DIFF
+    main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
+#endif
+    main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
+    main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
+    main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
+    main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
+    main_msg(_("-M\t\t\tModifications in text not allowed"));
+    main_msg(_("-b\t\t\tBinary mode"));
+#ifdef FEAT_LISP
+    main_msg(_("-l\t\t\tLisp mode"));
 #endif
+    main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
+    main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
+    main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to
fname]"));
+#ifdef FEAT_EVAL
+    main_msg(_("-D\t\t\tDebugging mode"));
+#endif
+    main_msg(_("-n\t\t\tNo swap file, use memory only"));
+    main_msg(_("-r\t\t\tList swap files and exit"));
+    main_msg(_("-r (with file name)\tRecover crashed session"));
+    main_msg(_("-L\t\t\tSame as -r"));
+#ifdef AMIGA
+    main_msg(_("-f\t\t\tDon't use newcli to open window"));
+    main_msg(_("-dev <device>\t\tUse <device> for I/O"));
+#endif
+#ifdef FEAT_ARABIC
+    main_msg(_("-A\t\t\tstart in Arabic mode"));
+#endif
+#ifdef FEAT_RIGHTLEFT
+    main_msg(_("-H\t\t\tStart in Hebrew mode"));
+#endif
+#ifdef FEAT_FKMAP
+    main_msg(_("-F\t\t\tStart in Farsi mode"));
+#endif
+    main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
+    main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
+#ifdef FEAT_GUI
+    main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
+#endif
+    main_msg(_("--noplugin\t\tDon't load plugin scripts"));
+#ifdef FEAT_WINDOWS
+    main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
+    main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
+    main_msg(_("-O[N]\t\tLike -o but split vertically"));
+#endif
+    main_msg(_("+\t\t\tStart at end of file"));
+    main_msg(_("+<lnum>\t\tStart at line <lnum>"));
+    main_msg(_("--cmd <command>\tExecute <command> before loading any
vimrc file"));
+    main_msg(_("-c <command>\t\tExecute <command> after loading the
first file"));
+    main_msg(_("-S <session>\t\tSource file <session> after loading
the first file"));
+    main_msg(_("-s <scriptin>\tRead Normal mode commands from file
<scriptin>"));
+    main_msg(_("-w <scriptout>\tAppend all typed commands to file
<scriptout>"));
+    main_msg(_("-W <scriptout>\tWrite all typed commands to file
<scriptout>"));
+#ifdef FEAT_CRYPT
+    main_msg(_("-x\t\t\tEdit encrypted files"));
+#endif
+#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
+# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
+    main_msg(_("-display <display>\tConnect vim to this particular X-server"));
+# endif
+    main_msg(_("-X\t\t\tDo not connect to X server"));
+#endif
+#ifdef FEAT_CLIENTSERVER
+    main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
+    main_msg(_("--remote-silent <files>  Same, don't complain if
there is no server"));
+    main_msg(_("--remote-wait <files>  As --remote but wait for files
to have been edited"));
+    main_msg(_("--remote-wait-silent <files>  Same, don't complain if
there is no server"));
+# ifdef FEAT_WINDOWS
+    main_msg(_("--remote-tab[-wait][-silent] <files>  As --remote but
use tab page per file"));
+# endif
+    main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
+    main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server
and print result"));
+    main_msg(_("--serverlist\t\tList available Vim server names and exit"));
+    main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
+#endif
+#ifdef STARTUPTIME
+    main_msg(_("--startuptime <file>\tWrite startup timing messages
to <file>"));
+#endif
+#ifdef FEAT_VIMINFO
+    main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
+#endif
+    main_msg(_("-h  or  --help\tPrint Help (this message) and exit"));
+    main_msg(_("--version\t\tPrint version information and exit"));

-#ifndef DISABLE_OPERATINGDIR
-    /* Set up the operating directory.  This entails chdir()ing there,
-     * so that file reads and writes will be based there. */
-    init_operating_dir();
+#ifdef FEAT_GUI_X11
+# ifdef FEAT_GUI_MOTIF
+    mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
+# else
+#  ifdef FEAT_GUI_ATHENA
+#   ifdef FEAT_GUI_NEXTAW
+    mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
+#   else
+    mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
+#   endif
+#  endif
+# endif
+    main_msg(_("-display <display>\tRun vim on <display>"));
+    main_msg(_("-iconic\t\tStart vim iconified"));
+    main_msg(_("-background <color>\tUse <color> for the background
(also: -bg)"));
+    main_msg(_("-foreground <color>\tUse <color> for normal text
(also: -fg)"));
+    main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
+    main_msg(_("-boldfont <font>\tUse <font> for bold text"));
+    main_msg(_("-italicfont <font>\tUse <font> for italic text"));
+    main_msg(_("-geometry <geom>\tUse <geom> for initial geometry
(also: -geom)"));
+    main_msg(_("-borderwidth <width>\tUse a border width of <width>
(also: -bw)"));
+    main_msg(_("-scrollbarwidth <width>  Use a scrollbar width of
<width> (also: -sw)"));
+# ifdef FEAT_GUI_ATHENA
+    main_msg(_("-menuheight <height>\tUse a menu bar height of
<height> (also: -mh)"));
+# endif
+    main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
+    main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
+    main_msg(_("-xrm <resource>\tSet the specified resource"));
+#endif /* FEAT_GUI_X11 */
+#ifdef FEAT_GUI_GTK
+    mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
+    main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
+    main_msg(_("-geometry <geom>\tUse <geom> for initial geometry
(also: -geom)"));
+    main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
+    main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
+    main_msg(_("--role <role>\tSet a unique role to identify the main
window"));
+    main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
+    main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
 #endif
+#ifdef FEAT_GUI_W32
+    main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
+    main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
+#endif
+
+#ifdef FEAT_GUI_GNOME
+    /* Gnome gives extra messages for --help if we continue, but not for -h. */
+    if (gui.starting)
+    {
+       mch_msg("\n");
+       gui.dofork = FALSE;
+    }
+    else
+#endif
+       mch_exit(0);
+}
+
+#if defined(HAS_SWAP_EXISTS_ACTION)
+/*
+ * Check the result of the ATTENTION dialog:
+ * When "Quit" selected, exit Vim.
+ * When "Recover" selected, recover the file.
+ */
+    static void
+check_swap_exists_action()
+{
+    if (swap_exists_action == SEA_QUIT)
+       getout(1);
+    handle_swap_exists(NULL);
+}
+#endif
+
+#endif
+
+#if defined(STARTUPTIME) || defined(PROTO)
+static void time_diff __ARGS((struct timeval *then, struct timeval *now));
+
+static struct timeval  prev_timeval;
+
+# ifdef WIN3264
+/*
+ * Windows doesn't have gettimeofday(), although it does have struct timeval.
+ */
+    static int
+gettimeofday(struct timeval *tv, char *dummy)
+{
+    long t = clock();
+    tv->tv_sec = t / CLOCKS_PER_SEC;
+    tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
+    return 0;
+}
+# endif
+
+/*
+ * Save the previous time before doing something that could nest.
+ * set "*tv_rel" to the time elapsed so far.
+ */
+    void
+time_push(tv_rel, tv_start)
+    void       *tv_rel, *tv_start;
+{
+    *((struct timeval *)tv_rel) = prev_timeval;
+    gettimeofday(&prev_timeval, NULL);
+    ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
+                                       - ((struct timeval *)tv_rel)->tv_usec;
+    ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
+                                        - ((struct timeval *)tv_rel)->tv_sec;
+    if (((struct timeval *)tv_rel)->tv_usec < 0)
+    {
+       ((struct timeval *)tv_rel)->tv_usec += 1000000;
+       --((struct timeval *)tv_rel)->tv_sec;
+    }
+    *(struct timeval *)tv_start = prev_timeval;
+}
+
+/*
+ * Compute the previous time after doing something that could nest.
+ * Subtract "*tp" from prev_timeval;
+ * Note: The arguments are (void *) to avoid trouble with systems that don't
+ * have struct timeval.
+ */
+    void
+time_pop(tp)
+    void       *tp;    /* actually (struct timeval *) */
+{
+    prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
+    prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
+    if (prev_timeval.tv_usec < 0)
+    {
+       prev_timeval.tv_usec += 1000000;
+       --prev_timeval.tv_sec;
+    }
+}
+
+    static void
+time_diff(then, now)
+    struct timeval     *then;
+    struct timeval     *now;
+{
+    long       usec;
+    long       msec;
+
+    usec = now->tv_usec - then->tv_usec;
+    msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
+    usec = usec % 1000L;
+    fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
+}

-#ifndef DISABLE_JUSTIFY
-    /* If punct wasn't specified, set its default value. */
-    if (punct == NULL)
-       punct = mallocstrcpy(NULL, "!.?");
+    void
+time_msg(mesg, tv_start)
+    char       *mesg;
+    void       *tv_start;  /* only for do_source: start time; actually
+                              (struct timeval *) */
+{
+    static struct timeval      start;
+    struct timeval             now;

-    /* If brackets wasn't specified, set its default value. */
-    if (brackets == NULL)
-       brackets = mallocstrcpy(NULL, "\"')>]}");
+    if (time_fd != NULL)
+    {
+       if (strstr(mesg, "STARTING") != NULL)
+       {
+           gettimeofday(&start, NULL);
+           prev_timeval = start;
+           fprintf(time_fd, "\n\ntimes in msec\n");
+           fprintf(time_fd, " clock   self+sourced   self:  sourced script\n");
+           fprintf(time_fd, " clock   elapsed:              other lines\n\n");
+       }
+       gettimeofday(&now, NULL);
+       time_diff(&start, &now);
+       if (((struct timeval *)tv_start) != NULL)
+       {
+           fprintf(time_fd, "  ");
+           time_diff(((struct timeval *)tv_start), &now);
+       }
+       fprintf(time_fd, "  ");
+       time_diff(&prev_timeval, &now);
+       prev_timeval = now;
+       fprintf(time_fd, ": %s\n", mesg);
+    }
+}
+
+#endif
+
+#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
+
+/*
+ * Common code for the X command server and the Win32 command server.
+ */
+
+static char_u *build_drop_cmd __ARGS((int filec, char **filev, int
tabs, int sendReply));
+
+/*
+ * Do the client-server stuff, unless "--servername ''" was used.
+ */
+    static void
+exec_on_server(parmp)
+    mparm_T    *parmp;
+{
+    if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
+    {
+# ifdef WIN32
+       /* Initialise the client/server messaging infrastructure. */
+       serverInitMessaging();
+# endif
+
+       /*
+        * When a command server argument was found, execute it.  This may
+        * exit Vim when it was successful.  Otherwise it's executed further
+        * on.  Remember the encoding used here in "serverStrEnc".
+        */
+       if (parmp->serverArg)
+       {
+           cmdsrv_main(&parmp->argc, parmp->argv,
+                                   parmp->serverName_arg, &parmp->serverStr);
+# ifdef FEAT_MBYTE
+           parmp->serverStrEnc = vim_strsave(p_enc);
+# endif
+       }
+
+       /* If we're still running, get the name to register ourselves.
+        * On Win32 can register right now, for X11 need to setup the
+        * clipboard first, it's further down. */
+       parmp->servername = serverMakeName(parmp->serverName_arg,
+                                                             parmp->argv[0]);
+# ifdef WIN32
+       if (parmp->servername != NULL)
+       {
+           serverSetName(parmp->servername);
+           vim_free(parmp->servername);
+       }
+# endif
+    }
+}
+
+/*
+ * Prepare for running as a Vim server.
+ */
+    static void
+prepare_server(parmp)
+    mparm_T    *parmp;
+{
+# if defined(FEAT_X11)
+    /*
+     * Register for remote command execution with :serversend and --remote
+     * unless there was a -X or a --servername '' on the command line.
+     * Only register nongui-vim's with an explicit --servername argument.
+     * When running as root --servername is also required.
+     */
+    if (X_DISPLAY != NULL && parmp->servername != NULL && (
+#  ifdef FEAT_GUI
+               (gui.in_use
+#   ifdef UNIX
+                && getuid() != ROOT_UID
+#   endif
+               ) ||
+#  endif
+               parmp->serverName_arg != NULL))
+    {
+       (void)serverRegisterName(X_DISPLAY, parmp->servername);
+       vim_free(parmp->servername);
+       TIME_MSG("register server name");
+    }
+    else
+       serverDelayedStartName = parmp->servername;
+# endif
+
+    /*
+     * Execute command ourselves if we're here because the send failed (or
+     * else we would have exited above).
+     */
+    if (parmp->serverStr != NULL)
+    {
+       char_u *p;
+
+       server_to_input_buf(serverConvert(parmp->serverStrEnc,
+                                                      parmp->serverStr, &p));
+       vim_free(p);
+    }
+}
+
+    static void
+cmdsrv_main(argc, argv, serverName_arg, serverStr)
+    int                *argc;
+    char       **argv;
+    char_u     *serverName_arg;
+    char_u     **serverStr;
+{
+    char_u     *res;
+    int                i;
+    char_u     *sname;
+    int                ret;
+    int                didone = FALSE;
+    int                exiterr = 0;
+    char       **newArgV = argv + 1;
+    int                newArgC = 1,
+               Argc = *argc;
+    int                argtype;
+#define ARGTYPE_OTHER          0
+#define ARGTYPE_EDIT           1
+#define ARGTYPE_EDIT_WAIT      2
+#define ARGTYPE_SEND           3
+    int                silent = FALSE;
+    int                tabs = FALSE;
+# ifndef FEAT_X11
+    HWND       srv;
+# else
+    Window     srv;
+
+    setup_term_clip();
+# endif
+
+    sname = serverMakeName(serverName_arg, argv[0]);
+    if (sname == NULL)
+       return;
+
+    /*
+     * Execute the command server related arguments and remove them
+     * from the argc/argv array; We may have to return into main()
+     */
+    for (i = 1; i < Argc; i++)
+    {
+       res = NULL;
+       if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
+       {
+           for (; i < *argc; i++)
+           {
+               *newArgV++ = argv[i];
+               newArgC++;
+           }
+           break;
+       }
+
+       if (STRICMP(argv[i], "--remote-send") == 0)
+           argtype = ARGTYPE_SEND;
+       else if (STRNICMP(argv[i], "--remote", 8) == 0)
+       {
+           char        *p = argv[i] + 8;
+
+           argtype = ARGTYPE_EDIT;
+           while (*p != NUL)
+           {
+               if (STRNICMP(p, "-wait", 5) == 0)
+               {
+                   argtype = ARGTYPE_EDIT_WAIT;
+                   p += 5;
+               }
+               else if (STRNICMP(p, "-silent", 7) == 0)
+               {
+                   silent = TRUE;
+                   p += 7;
+               }
+               else if (STRNICMP(p, "-tab", 4) == 0)
+               {
+                   tabs = TRUE;
+                   p += 4;
+               }
+               else
+               {
+                   argtype = ARGTYPE_OTHER;
+                   break;
+               }
+           }
+       }
+       else
+           argtype = ARGTYPE_OTHER;
+
+       if (argtype != ARGTYPE_OTHER)
+       {
+           if (i == *argc - 1)
+               mainerr_arg_missing((char_u *)argv[i]);
+           if (argtype == ARGTYPE_SEND)
+           {
+               *serverStr = (char_u *)argv[i + 1];
+               i++;
+           }
+           else
+           {
+               *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
+                                         tabs, argtype == ARGTYPE_EDIT_WAIT);
+               if (*serverStr == NULL)
+               {
+                   /* Probably out of memory, exit. */
+                   didone = TRUE;
+                   exiterr = 1;
+                   break;
+               }
+               Argc = i;
+           }
+# ifdef FEAT_X11
+           if (xterm_dpy == NULL)
+           {
+               mch_errmsg(_("No display"));
+               ret = -1;
+           }
+           else
+               ret = serverSendToVim(xterm_dpy, sname, *serverStr,
+                                                   NULL, &srv, 0, 0, silent);
+# else
+           /* Win32 always works? */
+           ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
+# endif
+           if (ret < 0)
+           {
+               if (argtype == ARGTYPE_SEND)
+               {
+                   /* Failed to send, abort. */
+                   mch_errmsg(_(": Send failed.\n"));
+                   didone = TRUE;
+                   exiterr = 1;
+               }
+               else if (!silent)
+                   /* Let vim start normally.  */
+                   mch_errmsg(_(": Send failed. Trying to execute locally\n"));
+               break;
+           }
+
+# ifdef FEAT_GUI_W32
+           /* Guess that when the server name starts with "g" it's a GUI
+            * server, which we can bring to the foreground here.
+            * Foreground() in the server doesn't work very well. */
+           if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
+               SetForegroundWindow(srv);
+# endif
+
+           /*
+            * For --remote-wait: Wait until the server did edit each
+            * file.  Also detect that the server no longer runs.
+            */
+           if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
+           {
+               int     numFiles = *argc - i - 1;
+               int     j;
+               char_u  *done = alloc(numFiles);
+               char_u  *p;
+# ifdef FEAT_GUI_W32
+               NOTIFYICONDATA ni;
+               int     count = 0;
+               extern HWND message_window;
+# endif
+
+               if (numFiles > 0 && argv[i + 1][0] == '+')
+                   /* Skip "+cmd" argument, don't wait for it to be edited. */
+                   --numFiles;
+
+# ifdef FEAT_GUI_W32
+               ni.cbSize = sizeof(ni);
+               ni.hWnd = message_window;
+               ni.uID = 0;
+               ni.uFlags = NIF_ICON|NIF_TIP;
+               ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
+               sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
+               Shell_NotifyIcon(NIM_ADD, &ni);
+# endif
+
+               /* Wait for all files to unload in remote */
+               vim_memset(done, 0, numFiles);
+               while (memchr(done, 0, numFiles) != NULL)
+               {
+# ifdef WIN32
+                   p = serverGetReply(srv, NULL, TRUE, TRUE);
+                   if (p == NULL)
+                       break;
+# else
+                   if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
+                       break;
+# endif
+                   j = atoi((char *)p);
+                   if (j >= 0 && j < numFiles)
+                   {
+# ifdef FEAT_GUI_W32
+                       ++count;
+                       sprintf(ni.szTip, _("%d of %d edited"),
+                                                            count, numFiles);
+                       Shell_NotifyIcon(NIM_MODIFY, &ni);
+# endif
+                       done[j] = 1;
+                   }
+               }
+# ifdef FEAT_GUI_W32
+               Shell_NotifyIcon(NIM_DELETE, &ni);
+# endif
+           }
+       }
+       else if (STRICMP(argv[i], "--remote-expr") == 0)
+       {
+           if (i == *argc - 1)
+               mainerr_arg_missing((char_u *)argv[i]);
+# ifdef WIN32
+           /* Win32 always works? */
+           if (serverSendToVim(sname, (char_u *)argv[i + 1],
+                                                   &res, NULL, 1, FALSE) < 0)
+# else
+           if (xterm_dpy == NULL)
+               mch_errmsg(_("No display: Send expression failed.\n"));
+           else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
+                                                &res, NULL, 1, 1, FALSE) < 0)
+# endif
+           {
+               if (res != NULL && *res != NUL)
+               {
+                   /* Output error from remote */
+                   mch_errmsg((char *)res);
+                   vim_free(res);
+                   res = NULL;
+               }
+               mch_errmsg(_(": Send expression failed.\n"));
+           }
+       }
+       else if (STRICMP(argv[i], "--serverlist") == 0)
+       {
+# ifdef WIN32
+           /* Win32 always works? */
+           res = serverGetVimNames();
+# else
+           if (xterm_dpy != NULL)
+               res = serverGetVimNames(xterm_dpy);
+# endif
+           if (called_emsg)
+               mch_errmsg("\n");
+       }
+       else if (STRICMP(argv[i], "--servername") == 0)
+       {
+           /* Already processed. Take it out of the command line */
+           i++;
+           continue;
+       }
+       else
+       {
+           *newArgV++ = argv[i];
+           newArgC++;
+           continue;
+       }
+       didone = TRUE;
+       if (res != NULL && *res != NUL)
+       {
+           mch_msg((char *)res);
+           if (res[STRLEN(res) - 1] != '\n')
+               mch_msg("\n");
+       }
+       vim_free(res);
+    }
+
+    if (didone)
+    {
+       display_errors();       /* display any collected messages */
+       exit(exiterr);  /* Mission accomplished - get out */
+    }
+
+    /* Return back into main() */
+    *argc = newArgC;
+    vim_free(sname);
+}
+
+/*
+ * Build a ":drop" command to send to a Vim server.
+ */
+    static char_u *
+build_drop_cmd(filec, filev, tabs, sendReply)
+    int                filec;
+    char       **filev;
+    int                tabs;           /* Use ":tab drop" instead of ":drop". 
*/
+    int                sendReply;
+{
+    garray_T   ga;
+    int                i;
+    char_u     *inicmd = NULL;
+    char_u     *p;
+    char_u     *cwd;
+
+    if (filec > 0 && filev[0][0] == '+')
+    {
+       inicmd = (char_u *)filev[0] + 1;
+       filev++;
+       filec--;
+    }
+    /* Check if we have at least one argument. */
+    if (filec <= 0)
+       mainerr_arg_missing((char_u *)filev[-1]);
+
+    /* Temporarily cd to the current directory to handle relative
file names. */
+    cwd = alloc(MAXPATHL);
+    if (cwd == NULL)
+       return NULL;
+    if (mch_dirname(cwd, MAXPATHL) != OK)
+    {
+       vim_free(cwd);
+       return NULL;
+    }
+    p = vim_strsave_escaped_ext(cwd,
+#ifdef BACKSLASH_IN_FILENAME
+                   "",  /* rem_backslash() will tell what chars to escape */
+#else
+                   PATH_ESC_CHARS,
+#endif
+                   '\\', TRUE);
+    vim_free(cwd);
+    if (p == NULL)
+       return NULL;
+    ga_init2(&ga, 1, 100);
+    ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
+    ga_concat(&ga, p);
+    vim_free(p);

-    /* If quotestr wasn't specified, set its default value. */
-    if (quotestr == NULL)
-       quotestr = mallocstrcpy(NULL,
-#ifdef HAVE_REGEX_H
-               "^([ \t]*[#:>|}])+"
+    /* Call inputsave() so that a prompt for an encryption key works. */
+    ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call
inputsave()|endif|");
+    if (tabs)
+       ga_concat(&ga, (char_u *)"tab ");
+    ga_concat(&ga, (char_u *)"drop");
+    for (i = 0; i < filec; i++)
+    {
+       /* On Unix the shell has already expanded the wildcards, don't want to
+        * do it again in the Vim server.  On MS-Windows only escape
+        * non-wildcard characters. */
+       p = vim_strsave_escaped((char_u *)filev[i],
+#ifdef UNIX
+               PATH_ESC_CHARS
 #else
-               "> "
+               (char_u *)" \t%#"
 #endif
                );
-#ifdef HAVE_REGEX_H
-    quoterc = regcomp(&quotereg, quotestr, REG_EXTENDED);
-
-    if (quoterc == 0) {
-       /* We no longer need quotestr, just quotereg. */
-       free(quotestr);
-       quotestr = NULL;
-    } else {
-       size_t size = regerror(quoterc, &quotereg, NULL, 0);
-
-       quoteerr = charalloc(size);
-       regerror(quoterc, &quotereg, quoteerr, size);
-    }
-#else
-    quotelen = strlen(quotestr);
-#endif /* !HAVE_REGEX_H */
-#endif /* !DISABLE_JUSTIFY */
-
-#ifndef DISABLE_SPELLER
-    /* If we don't have an alternative spell checker after reading the
-     * command line and/or rcfile(s), check $SPELL for one, as Pico
-     * does (unless we're using restricted mode, in which case spell
-     * checking is disabled, since it would allow reading from or
-     * writing to files not specified on the command line). */
-    if (!ISSET(RESTRICTED) && alt_speller == NULL) {
-       char *spellenv = getenv("SPELL");
-       if (spellenv != NULL)
-           alt_speller = mallocstrcpy(NULL, spellenv);
-    }
-#endif
-
-#ifndef NANO_TINY
-    /* If matchbrackets wasn't specified, set its default value. */
-    if (matchbrackets == NULL)
-       matchbrackets = mallocstrcpy(NULL, "(<[{)>]}");
-#endif
-
-#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
-    /* If whitespace wasn't specified, set its default value. */
-    if (whitespace == NULL) {
-#ifdef ENABLE_UTF8
-       if (using_utf8()) {
-           whitespace = mallocstrcpy(NULL, "»·");
-           whitespace_len[0] = 2;
-           whitespace_len[1] = 2;
-       } else
-#endif
+       if (p == NULL)
        {
-           whitespace = mallocstrcpy(NULL, ">.");
-           whitespace_len[0] = 1;
-           whitespace_len[1] = 1;
+           vim_free(ga.ga_data);
+           return NULL;
        }
+       ga_concat(&ga, (char_u *)" ");
+       ga_concat(&ga, p);
+       vim_free(p);
     }
-#endif
-
-    /* If tabsize wasn't specified, set its default value. */
-    if (tabsize == -1)
-       tabsize = WIDTH_OF_TAB;
-
-    /* Back up the old terminal settings so that they can be restored. */
-    tcgetattr(0, &oldterm);
-
-    /* Initialize curses mode.  If this fails, get out. */
-    if (initscr() == NULL)
-       exit(1);
-
-    /* Set up the terminal state. */
-    terminal_init();
-
-    /* Turn the cursor on for sure. */
-    curs_set(1);
+    ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call
inputrestore()|endif<CR>");

-#ifdef DEBUG
-    fprintf(stderr, "Main: set up windows\n");
-#endif
-
-    /* Initialize all the windows based on the current screen
-     * dimensions. */
-    window_init();
+    /* The :drop commands goes to Insert mode when 'insertmode' is set, use
+     * CTRL-\ CTRL-N again. */
+    ga_concat(&ga, (char_u *)"<C-\\><C-N>");

-    /* Set up the signal handlers. */
-    signal_init();
-
-#ifndef DISABLE_MOUSE
-    /* Initialize mouse support. */
-    mouse_init();
-#endif
+    /* Switch back to the correct current directory (prior to temporary path
+     * switch) unless 'autochdir' is set, in which case it will already be
+     * correct after the :drop command. */
+    ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");

-#ifdef DEBUG
-    fprintf(stderr, "Main: open file\n");
-#endif
-
-    /* If there's a +LINE or +LINE,COLUMN flag here, it is the first
-     * non-option argument, and it is followed by at least one other
-     * argument, the filename it applies to. */
-    if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') {
-       parse_line_column(&argv[optind][1], &startline, &startcol);
-       optind++;
-    }
-
-    if (optind < argc && !strcmp(argv[optind], "-")) {
-       stdin_pager();
-       set_modified();
-       optind++;
+    if (sendReply)
+       ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
+    ga_concat(&ga, (char_u *)":");
+    if (inicmd != NULL)
+    {
+       /* Can't use <CR> after "inicmd", because an "startinsert" would cause
+        * the following commands to be inserted as text.  Use a "|",
+        * hopefully "inicmd" does allow this... */
+       ga_concat(&ga, inicmd);
+       ga_concat(&ga, (char_u *)"|");
     }
+    /* Bring the window to the foreground, goto Insert mode when 'im' set and
+     * clear command line. */
+    ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
+    ga_append(&ga, NUL);
+    return ga.ga_data;
+}

-#ifdef ENABLE_MULTIBUFFER
-    old_multibuffer = ISSET(MULTIBUFFER);
-    SET(MULTIBUFFER);
-
-    /* Read all the files after the first one on the command line into
-     * new buffers. */
+/*
+ * Make our basic server name: use the specified "arg" if given, otherwise use
+ * the tail of the command "cmd" we were started with.
+ * Return the name in allocated memory.  This doesn't include a serial number.
+ */
+    static char_u *
+serverMakeName(arg, cmd)
+    char_u     *arg;
+    char       *cmd;
+{
+    char_u *p;
+
+    if (arg != NULL && *arg != NUL)
+       p = vim_strsave_up(arg);
+    else
     {
-       int i = optind + 1;
-       ssize_t iline = 1, icol = 1;
-
-       for (; i < argc; i++) {
-           /* If there's a +LINE or +LINE,COLUMN flag here, it is
-            * followed by at least one other argument, the filename it
-            * applies to. */
-           if (i < argc - 1 && argv[i][0] == '+' && iline == 1 &&
-               icol == 1)
-               parse_line_column(&argv[i][1], &iline, &icol);
-           else {
-               open_buffer(argv[i], FALSE);
-
-               if (iline > 1 || icol > 1) {
-                   do_gotolinecolumn(iline, icol, FALSE, FALSE, FALSE,
-                       FALSE);
-                   iline = 1;
-                   icol = 1;
-               }
-#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
-                  else {
-                   /* See if we have a POS history to use if we haven't 
overridden it. */
-                   ssize_t savedposline, savedposcol;
-                   if (check_poshistory(argv[i], &savedposline, &savedposcol))
-                       do_gotolinecolumn(savedposline, savedposcol, FALSE, 
FALSE, FALSE,
-                       FALSE);
-               }
-#endif
-           }
-       }
+       p = vim_strsave_up(gettail((char_u *)cmd));
+       /* Remove .exe or .bat from the name. */
+       if (p != NULL && vim_strchr(p, '.') != NULL)
+           *vim_strchr(p, '.') = NUL;
     }
-#endif
-
-    /* Read the first file on the command line into either the current
-     * buffer or a new buffer, depending on whether multibuffer mode is
-     * enabled. */
-    if (optind < argc)
-       open_buffer(argv[optind], FALSE);
-
-    /* We didn't open any files if all the command line arguments were
-     * invalid files like directories or if there were no command line
-     * arguments given.  In this case, we have to load a blank buffer.
-     * Also, we unset view mode to allow editing. */
-    if (openfile == NULL) {
-       open_buffer("", FALSE);
-       UNSET(VIEW_MODE);
+    return p;
+}
+#endif /* FEAT_CLIENTSERVER */
+
+#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
+/*
+ * Replace termcodes such as <CR> and insert as key presses if there is room.
+ */
+    void
+server_to_input_buf(str)
+    char_u     *str;
+{
+    char_u      *ptr = NULL;
+    char_u      *cpo_save = p_cpo;
+
+    /* Set 'cpoptions' the way we want it.
+     *    B set - backslashes are *not* treated specially
+     *    k set - keycodes are *not* reverse-engineered
+     *    < unset - <Key> sequences *are* interpreted
+     *  The last but one parameter of replace_termcodes() is TRUE so that the
+     *  <lt> sequence is recognised - needed for a real backslash.
+     */
+    p_cpo = (char_u *)"Bk";
+    str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
+    p_cpo = cpo_save;
+
+    if (*ptr != NUL)   /* trailing CTRL-V results in nothing */
+    {
+       /*
+        * Add the string to the input stream.
+        * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
+        *
+        * First clear typed characters from the typeahead buffer, there could
+        * be half a mapping there.  Then append to the existing string, so
+        * that multiple commands from a client are concatenated.
+        */
+       if (typebuf.tb_maplen < typebuf.tb_len)
+           del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
+       (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
+
+       /* Let input_available() know we inserted text in the typeahead
+        * buffer. */
+       typebuf_was_filled = TRUE;
     }
+    vim_free((char_u *)ptr);
+}

-#ifdef ENABLE_MULTIBUFFER
-    if (!old_multibuffer)
-       UNSET(MULTIBUFFER);
+/*
+ * Evaluate an expression that the client sent to a string.
+ */
+    char_u *
+eval_client_expr_to_string(expr)
+    char_u *expr;
+{
+    char_u     *res;
+    int                save_dbl = debug_break_level;
+    int                save_ro = redir_off;
+
+     /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
+      * typed. */
+    debug_break_level = -1;
+    redir_off = 0;
+    /* Do not display error message, otherwise Vim hangs, waiting for "cont"
+     * to be typed.  Do generate errors so that try/catch works. */
+    ++emsg_silent;
+
+    res = eval_to_string(expr, NULL, TRUE);
+
+    debug_break_level = save_dbl;
+    redir_off = save_ro;
+    --emsg_silent;
+    if (emsg_silent < 0)
+       emsg_silent = 0;
+
+    /* A client can tell us to redraw, but not to display the cursor, so do
+     * that here. */
+    setcursor();
+    out_flush();
+#ifdef FEAT_GUI
+    if (gui.in_use)
+       gui_update_cursor(FALSE, FALSE);
 #endif

-#ifdef DEBUG
-    fprintf(stderr, "Main: top and bottom win\n");
-#endif
+    return res;
+}

-#ifdef ENABLE_COLOR
-    if (openfile->syntax)
-       if (openfile->syntax->nmultis > 0)
-           precalc_multicolorinfo();
-#endif
+/*
+ * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
+ * return an allocated string.  Otherwise return "data".
+ * "*tofree" is set to the result when it needs to be freed later.
+ */
+    char_u *
+serverConvert(client_enc, data, tofree)
+    char_u *client_enc UNUSED;
+    char_u *data;
+    char_u **tofree;
+{
+    char_u     *res = data;
+
+    *tofree = NULL;
+# ifdef FEAT_MBYTE
+    if (client_enc != NULL && p_enc != NULL)
+    {
+       vimconv_T       vimconv;

-    if (startline > 1 || startcol > 1)
-       do_gotolinecolumn(startline, startcol, FALSE, FALSE, FALSE,
-               FALSE);
-#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
-    else {
-       /* See if we have a POS history to use if we haven't overridden it. */
-       ssize_t savedposline, savedposcol;
-       if (check_poshistory(argv[optind], &savedposline, &savedposcol))
-           do_gotolinecolumn(savedposline, savedposcol, FALSE, FALSE, FALSE, 
FALSE);
+       vimconv.vc_type = CONV_NONE;
+       if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
+                                             && vimconv.vc_type != CONV_NONE)
+       {
+           res = string_convert(&vimconv, data, NULL);
+           if (res == NULL)
+               res = data;
+           else
+               *tofree = res;
+       }
+       convert_setup(&vimconv, NULL, NULL);
     }
+# endif
+    return res;
+}
 #endif

-    display_main_list();
-
-    display_buffer();
-
-    while (TRUE) {
-       bool meta_key, func_key, s_or_t, ran_func, finished;
-
-       /* Make sure the cursor is in the edit window. */
-       reset_cursor();
-       wnoutrefresh(edit);
-
-#ifndef NANO_TINY
-       if (!jump_buf_main) {
-           /* If we haven't already, we're going to set jump_buf so
-            * that we return here after a SIGWINCH.  Indicate this. */
-           jump_buf_main = TRUE;
-
-           /* Return here after a SIGWINCH. */
-           sigsetjmp(jump_buf, 1);
-       }
+/*
+ * When FEAT_FKMAP is defined, also compile the Farsi source code.
+ */
+#if defined(FEAT_FKMAP) || defined(PROTO)
+# include "farsi.c"
 #endif

-       /* Just in case we were at the statusbar prompt, make sure the
-        * statusbar cursor position is reset. */
-       do_prompt_abort();
-
-       /* If constant cursor position display is on, and there are no
-        * keys waiting in the input buffer, display the current cursor
-        * position on the statusbar. */
-       if (ISSET(CONST_UPDATE) && get_key_buffer_len() == 0)
-           do_cursorpos(TRUE);
-
-        currmenu = MMAIN;
-
-       /* Read in and interpret characters. */
-       do_input(&meta_key, &func_key, &s_or_t, &ran_func, &finished,
-               TRUE);
-    }
-
-    /* We should never get here. */
-    assert(FALSE);
-}
-
+/*
+ * When FEAT_ARABIC is defined, also compile the Arabic source code.
+ */
+#if defined(FEAT_ARABIC) || defined(PROTO)
+# include "arabic.c"
+#endif


-- 
Eitan Adler



reply via email to

[Prev in Thread] Current Thread [Next in Thread]