]> git.lyx.org Git - features.git/commitdiff
command line import fixups
authorJohn Levon <levon@movementarian.org>
Thu, 30 May 2002 19:49:00 +0000 (19:49 +0000)
committerJohn Levon <levon@movementarian.org>
Thu, 30 May 2002 19:49:00 +0000 (19:49 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4303 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/buffer.C
src/buffer.h
src/lyx_main.C
src/lyxfunc.C

index f4d8ac514a12360886e851aacece17a0d00c9922..718f5e0ad82d17269e73517eec98450c08d10be4 100644 (file)
@@ -1,3 +1,12 @@
+2002-05-30  John Levon  <moz@compsoc.man.ac.uk>
+
+       * buffer.h: 
+       * buffer.C:
+       * lyx_main.C: fix a crash on bad command line,
+         and give a useful exit status on error
+
+       * lyxfunc.C (doImport): allow -i lyx to work
+  
 2002-03-30  André Pönitz <poenitz@gmx.net>
 
        * lyxfunc.C: mathed font changes 
index 0bf0910091fe12044ebfcc39006fd2ff97ae5ce4..481ca4990a549bc0ef8b096e934786a2b2896097 100644 (file)
@@ -3893,24 +3893,28 @@ void Buffer::markDepClean(string const & name)
 }
 
 
-bool Buffer::dispatch(string const & command)
+bool Buffer::dispatch(string const & command, bool * result)
 {
        // Split command string into command and argument
        string cmd;
        string line = frontStrip(command);
        string const arg = strip(frontStrip(split(line, cmd, ' ')));
 
-       return dispatch(lyxaction.LookupFunc(cmd), arg);
+       return dispatch(lyxaction.LookupFunc(cmd), arg, result);
 }
 
 
-bool Buffer::dispatch(int action, string const & argument)
+bool Buffer::dispatch(int action, string const & argument, bool * result)
 {
        bool dispatched = true;
        switch (action) {
-               case LFUN_EXPORT:
-                       Exporter::Export(this, argument, false);
+               case LFUN_EXPORT: {
+                       bool const tmp = Exporter::Export(this, argument, false);
+                       if (result)
+                               *result = tmp;
                        break;
+               }
 
                default:
                        dispatched = false;
index 1aacc136bab7c3f1fd8882b80bc46729d3757649..4da4b29a3aca63ad493ee75496e1d8bc1fa5687e 100644 (file)
@@ -76,10 +76,10 @@ public:
        /** High-level interface to buffer functionality.
            This function parses a command string and executes it
        */
-       bool dispatch(string const & command);
+       bool dispatch(string const & command, bool * result = 0);
 
        /// Maybe we know the function already by number...
-       bool dispatch(int ac, string const & argument);
+       bool dispatch(int ac, string const & argument, bool * result = 0);
 
        ///
        void resizeInsets(BufferView *);
index bc3da055e9bdf6a2b5880aa5cd7f65f058d87cc0..9502dd15f38d0d280af597cd3f3fca80e0927f84 100644 (file)
@@ -102,7 +102,7 @@ LyX::LyX(int * argc, char * argv[])
                        lyxerr << _("Wrong command line option `")
                               << argv[argi]
                               << _("'. Exiting.") << endl;
-                       exit(0);
+                       exit(1);
                }
        }
 
@@ -154,15 +154,16 @@ LyX::LyX(int * argc, char * argv[])
                if (!last_loaded)
                        last_loaded = bufferlist.newFile("tmpfile", string());
 
+               bool success = false;
                // try to dispatch to last loaded buffer first
-               bool dispatched = last_loaded->dispatch(batch_command);
+               bool dispatched = last_loaded->dispatch(batch_command, &success);
 
                // if this was successful, return.
                // Maybe we could do something more clever than aborting...
                if (dispatched) {
-                       lyxerr << "We are done!" << endl;
                        QuitLyX();
-                       return;
+                       exit(!success);
                }
 
                // otherwise, let the GUI handle the batch command
@@ -835,7 +836,7 @@ bool LyX::easyParse(int * argc, char * argv[])
                                lyxerr << _("List of supported debug flags:")
                                       << endl;
                                Debug::showTags(lyxerr);
-                               exit(0);
+                               exit(1);
                        }
                }
                // Check for "-sysdir"
@@ -846,7 +847,7 @@ bool LyX::easyParse(int * argc, char * argv[])
                        } else {
                                lyxerr << _("Missing directory for -sysdir switch!")
                                       << endl;
-                               exit(0);
+                               exit(1);
                        }
                }
                // Check for "-userdir"
@@ -857,7 +858,7 @@ bool LyX::easyParse(int * argc, char * argv[])
                        } else {
                                lyxerr << _("Missing directory for -userdir switch!")
                                       << endl;
-                               exit(0);
+                               exit(1);
                        }
                }
                // Check for --help or -help
@@ -870,6 +871,7 @@ bool LyX::easyParse(int * argc, char * argv[])
                        commandLineVersionInfo();
                        exit(0);
                }
+               // FIXME: why is this commented out ? 
                // Check for "-nw": No XWindows as for emacs this should
                // give a LyX that could be used in a terminal window.
                //else if (arg == "-nw") {
@@ -886,7 +888,8 @@ bool LyX::easyParse(int * argc, char * argv[])
                                lyxerr << _("Missing command string after  -x switch!") << endl;
 
                        // Argh. Setting gui to false segfaults..
-                       //gui = false;
+                       // FIXME: when ? how ? 
+                       // gui = false;
                }
 
                else if (arg == "-e" || arg == "--export") {
@@ -895,25 +898,34 @@ bool LyX::easyParse(int * argc, char * argv[])
                                removeargs = 2;
                                batch_command = "buffer-export " + type;
                                gui = false;
-                       } else
+                       } else {
                                lyxerr << _("Missing file type [eg latex, "
                                            "ps...] after ")
                                       << arg << _(" switch!") << endl;
+                               exit(1);
+                       }
                }
                else if (arg == "-i" || arg == "--import") {
                        if (i + 1 < *argc) {
-                               string const type(argv[i+1]);
+                               if (!argv[i+2]) {
+                                       lyxerr << _("Missing filename for --import") << endl;
+                                       exit(1);
+                               }
                                string const file(argv[i+2]);
+                               string const type(argv[i+1]);
                                removeargs = 3;
-
                                batch_command = "buffer-import " + type + " " + file;
                                lyxerr << "batch_command: "
                                       << batch_command << endl;
 
-                       } else
+                       } else {
                                lyxerr << _("Missing type [eg latex, "
                                            "ps...] after ")
                                       << arg << _(" switch!") << endl;
+                               exit(1);
+                       }
                }
 
                if (removeargs > 0) {
index c5dfc104133b2ac91f3d19a46c850227ea09ab88..088828537d18d16f4e0b2df36b113e0ede41f28e 100644 (file)
@@ -1912,15 +1912,16 @@ void LyXFunc::open(string const & fname)
 }
 
 
-// checks for running without gui are missing.
 void LyXFunc::doImport(string const & argument)
 {
        string format;
        string filename = split(argument, format, ' ');
+
        lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format
                            << " file: " << filename << endl;
-
-       if (filename.empty()) { // need user interaction
+       // need user interaction
+       if (filename.empty()) {
                string initpath = lyxrc.document_path;
 
                if (owner->view()->available()) {
@@ -1957,7 +1958,6 @@ void LyXFunc::doImport(string const & argument)
                        owner->message(_("Canceled."));
        }
 
-       // still no filename? abort
        if (filename.empty())
                return;
 
@@ -1967,35 +1967,35 @@ void LyXFunc::doImport(string const & argument)
        string const lyxfile = ChangeExtension(filename, ".lyx");
 
        // Check if the document already is open
-       if (bufferlist.exists(lyxfile)) {
+       if (lyxrc.use_gui && bufferlist.exists(lyxfile)) {
                switch (Alert::askConfirmation(_("Document is already open:"),
                                        MakeDisplayPath(lyxfile, 50),
                                        _("Do you want to close that document now?\n"
                                          "('No' will just switch to the open version)")))
                        {
-                       case 1: // Yes: close the document
-                               if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
+                       case 1:
                                // If close is canceled, we cancel here too.
+                               if (!bufferlist.close(bufferlist.getBuffer(lyxfile)))
                                        return;
                                break;
-                       case 2: // No: switch to the open document
+                       case 2:
                                owner->view()->buffer(bufferlist.getBuffer(lyxfile));
                                return;
-                       case 3: // Cancel: Do nothing
+                       case 3:
                                owner->message(_("Canceled."));
                                return;
                        }
        }
 
-       // Check if a LyX document by the same root exists in filesystem
-       FileInfo const f(lyxfile, true);
-       if (f.exist() && !Alert::askQuestion(_("A document by the name"),
-                                     MakeDisplayPath(lyxfile),
-                                     _("already exists. Overwrite?"))) {
-               owner->message(_("Canceled"));
-               return;
+       // if the file exists already, and we didn't do 
+       // -i lyx thefile.lyx, warn
+       if (FileInfo(lyxfile, true).exist() && filename != lyxfile) {
+               if (!Alert::askQuestion(_("A document by the name"),
+                       MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) {
+                       owner->message(_("Canceled"));
+                       return;
+               }
        }
-       // filename should be valid now
 
        Importer::Import(owner, filename, format);
 }