]> git.lyx.org Git - lyx.git/blob - src/LyXSendto.C
Fix crash when running lyx -dbg insets -e ...
[lyx.git] / src / LyXSendto.C
1 #include <config.h>
2
3 #include FORMS_H_LOCATION
4 #include "print_form.h"
5 #include "lyx_main.h"
6 #include "lyxrc.h"
7 #include "LString.h"
8 #include "support/filetools.h"
9 #include "support/path.h"
10 #include "buffer.h"
11 #include "lyx_gui_misc.h"
12 #include "support/syscall.h"
13 #include "gettext.h"
14 #include "bufferview_funcs.h"
15 #include "exporter.h"
16 #include "BufferView.h"
17
18 extern FD_form_sendto * fd_form_sendto;
19 extern BufferView * current_view;
20 extern int MakeLaTeXOutput(Buffer * buffer);
21 extern bool CreatePostscript(Buffer * buffer, bool wait);
22
23 // Whereas this feature is under the menu item File->Export->Custom,
24 // I kept the old name sendto in the code because I am lazy (JMarc)
25
26 void MenuSendto()
27 {
28     static int ow = -1;
29     static int oh;
30
31     // do this only if the command is empty
32     if (!fl_get_input(fd_form_sendto->input_cmd) &&
33         !lyxrc.custom_export_command.empty())
34         fl_set_input(fd_form_sendto->input_cmd,
35                      lyxrc.custom_export_command.c_str());
36     if (fd_form_sendto->form_sendto->visible) {
37         fl_raise_form(fd_form_sendto->form_sendto);
38     } else {  
39         fl_show_form(fd_form_sendto->form_sendto,
40                      FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
41                      _("Send Document to Command"));
42         if (ow < 0) {
43                 ow = fd_form_sendto->form_sendto->w;
44                 oh = fd_form_sendto->form_sendto->h;
45         }
46         fl_set_form_minsize(fd_form_sendto->form_sendto, ow, oh);
47     }
48 }
49
50
51 void SendtoApplyCB(FL_OBJECT *, long)
52 {
53     if (!current_view->available())
54         return;
55
56     string command = fl_get_input(fd_form_sendto->input_cmd);
57     if (command.empty())
58         return;
59     Buffer * buffer = current_view->buffer();
60     string ftypeext;
61     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
62         ftypeext = ".lyx";
63     else if (fl_get_button(fd_form_sendto->radio_ftype_latex))
64         ftypeext = ".tex";
65     else if (fl_get_button(fd_form_sendto->radio_ftype_dvi))
66         ftypeext = ".dvi";
67     else if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
68         ftypeext = ".txt";
69     else {
70         ftypeext = ".ps";
71         if (!Exporter::Export(buffer, "ps", true))
72                 return;
73     }
74
75     string const fname = OnlyFilename(ChangeExtension(buffer->getLatexName(),
76                                                       ftypeext));
77     if (!contains(command, "$$FName"))
78         command = "( " + command + " ) <$$FName";
79     command = subst(command, "$$FName", fname);
80     command += " &"; // execute in background
81     // push directorypath, if necessary 
82     string path = OnlyPath(buffer->fileName());
83     if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)){
84         path = buffer->tmppath;
85     }
86     Path p(path);
87     // save the .lyx file in tmp_dir if this filetype is requested
88     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
89         buffer->writeFile(fname, true);
90     // if the .tex file is requested save it to the tempdir
91     // as now we don't do the MakeLaTeXOutput anymore
92     if (fl_get_button(fd_form_sendto->radio_ftype_latex))
93         buffer->makeLaTeXFile(fname, path, false);
94     // create the .txt file in tmp_dir if this filetype is requested
95     if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
96         buffer->writeFileAscii(fname, lyxrc.ascii_linelen);
97     Systemcalls one(Systemcalls::System, command);    
98 }
99
100
101 void SendtoCancelCB(FL_OBJECT *, long)
102 {
103     fl_hide_form(fd_form_sendto->form_sendto);
104 }
105
106
107 void SendtoOKCB(FL_OBJECT * ob, long data)
108 {
109     SendtoCancelCB(ob, data);
110     SendtoApplyCB(ob, data);
111 }