This patch implements the '--import' feature requested this morning
on the list.
It changes the logic of --execute a bit: It first tries to get the
command executed by Buffer::Dispatch (which does not require a Gui)
on a possibly empty buffer and if that failed, it asks
LyXFunc::Dispatch to handle the request. Since the latter needs a
LyXView attached (for reasons unknown to me ;-)), the Gui is needed in
this case. I think this is as reasonable as it could get given the current
mess ;-)
So if you issue 'lyx --import ascii myfile.txt' you get the gui with
myfile.txt imported, since import is still handled in LyXFunc. I think
most of it could go to lyx_cb and be used from Buffer::Dispatch
directly... I did not do that in that patch in order to keep it small.
I don't know whether this --import is a good idea, an
other approach would be to examine file extensions and do the import
automagically. This way, scripting would be much easier, i.e. you could
do a
lyx --export dvi file.txt (without gui!)
to read in a text file, convert it to lyx, tex, dvi instead of
lyx --execute command-sequence \
buffer-import ascii file.txt ; buffer-export dvi ;
(with gui :-( )
In any case, it does not do any harm to the current behaviour.
Oh, and it needs the lyxfunc patch (or something similar) in order
to pass filenames through LyXFunc::Dispatch.
Andre'
-------------------------- snip --------------------
Index: buffer.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.C,v
retrieving revision 1.71
diff -u -r1.71 buffer.C
--- buffer.C 2000/04/24 20:58:21 1.71
+++ buffer.C 2000/04/26 09:36:35
@@ -3859,28 +3859,28 @@
}
-void Buffer::Dispatch(string const & command)
+bool Buffer::Dispatch(string const & command)
{
// Split command string into command and argument
string cmd, line = frontStrip(command);
string arg = strip(frontStrip(split(line, cmd, ' ')));
- Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
+ return Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
}
-void Buffer::Dispatch(int action, string const & argument)
+bool Buffer::Dispatch(int action, string const & argument)
{
+ bool dispatched = true;
switch (action) {
case LFUN_EXPORT:
MenuExport(this, argument);
break;
default:
- lyxerr << "A truly unknown func!" << endl;
- break;
-
- } // end of switch
+ dispatched = false;
+ }
+ return dispatched;
}
void Buffer::ChangeLanguage(Language const * from, Language const * to)
Index: buffer.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.h,v
retrieving revision 1.26
diff -u -r1.26 buffer.h
--- buffer.h 2000/04/11 22:55:29 1.26
+++ buffer.h 2000/04/26 09:36:35
@@ -80,10 +80,10 @@
/** high-level interface to buffer functionality
This function parses a command string and executes it
*/
- void Dispatch(string const & command);
+ bool Dispatch(string const & command);
/// Maybe we know the function already by number...
- void Dispatch(int ac, string const & argument);
+ bool Dispatch(int ac, string const & argument);
/// should be changed to work for a list.
void resize() {
Index: lyx_gui.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyx_gui.C,v
retrieving revision 1.32
diff -u -r1.32 lyx_gui.C
--- lyx_gui.C 2000/04/17 14:00:17 1.32
+++ lyx_gui.C 2000/04/26 09:36:37
@@ -630,3 +630,8 @@
{
lyxViews->view()->buffer(b);
}
+
+LyXView * LyXGUI::getLyXView() const
+{
+ return lyxViews;
+}
Index: lyx_gui.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyx_gui.h,v
retrieving revision 1.4
diff -u -r1.4 lyx_gui.h
--- lyx_gui.h 2000/04/17 14:00:17 1.4
+++ lyx_gui.h 2000/04/26 09:36:38
@@ -52,8 +52,12 @@
*/
void init();
- /// Register the buffer with the first fount LyXView in lyxViews
+ /// Register the buffer with the first found LyXView in lyxViews
void regBuf(Buffer*);
+
+ /// Access to (first?) LyXView
+ LyXView * getLyXView() const;
+
//@}
private:
/**@name Construcor */
Index: lyx_main.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyx_main.C,v
retrieving revision 1.29
diff -u -r1.29 lyx_main.C
--- lyx_main.C 2000/04/14 17:20:16 1.29
+++ lyx_main.C 2000/04/26 09:36:38
@@ -16,6 +16,8 @@
#include "version.h"
#include "lyx_main.h"
#include "lyx_gui.h"
+#include "LyXView.h"
+#include "lyxfunc.h"
#include "lyx_gui_misc.h"
#include "lyxrc.h"
#include "support/path.h"
@@ -121,13 +123,28 @@
}
// Execute batch commands if available
- if (!batch_command.empty() && last_loaded) {
+ if (!batch_command.empty()) {
lyxerr << "About to handle -x '" << batch_command << "'" << endl;
- //Buffer buffer("Script Buffer");
- //buffer.Dispatch(batch_command);
- last_loaded->Dispatch(batch_command);
- lyxerr << "We are done!" << endl;
- return; // Maybe we could do something more clever than aborting..
+
+ // no buffer loaded, create one
+ if (!last_loaded)
+ last_loaded = bufferlist.newFile("tmpfile", string());
+
+ // try to dispatch to last loaded buffer first
+ bool dispatched = last_loaded->Dispatch(batch_command);
+
+ // if this was successful, return.
+ // Maybe we could do something more clever than aborting...
+ if (dispatched) {
+ lyxerr << "We are done!" << endl;
+ return;
+ }
+
+ // otherwise, let the GUI handle the batch command
+ lyxGUI->regBuf(last_loaded);
+ lyxGUI->getLyXView()->getLyXFunc()->Dispatch(batch_command);
+
+ // fall through...
}
// Let the ball begin...
@@ -573,7 +590,27 @@
"ps...] after ")
<< arg << _(" switch!") << endl;
}
+
+ else if (arg == "--import") {
+ if (i + 1 < *argc) {
+ string type(argv[i+1]);
+ string file(argv[i+2]);
+
+ (*argc) -= 3;
+ for (int j = i; j < (*argc); ++j)
+ argv[j] = argv[j + 3];
+ --i; // After shift, check this number again.
+
+ batch_command = "buffer-import " + type + " " + file;
+ cerr << "batch_command: " << batch_command << endl;
+
+ } else
+ lyxerr << _("Missing type [eg latex, "
+ "ps...] after ")
+ << arg << _(" switch!") << endl;
+ }
}
+
return gui;
}
-------------------------- snip --------------------
--
It'll take a long time to eat 63.000 peanuts.
André Pönitz ......................... [EMAIL PROTECTED]