3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Angus Leeming
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
20 #include "BufferList.h"
21 #include "BufferView.h"
22 #include "buffer_funcs.h"
24 #include "CutAndPaste.h"
28 #include "LaTeXFeatures.h"
35 #include "frontends/alert.h"
36 #include "frontends/Application.h"
37 #include "frontends/FileDialog.h"
38 #include "frontends/LyXView.h"
40 #include "support/FileFilterList.h"
41 #include "support/filetools.h"
42 #include "support/Forkedcall.h"
43 #include "support/fs_extras.h"
44 #include "support/lyxlib.h"
45 #include "support/Package.h"
46 #include "support/Path.h"
47 #include "support/Systemcall.h"
49 #if !defined (HAVE_FORK)
53 #include <boost/shared_ptr.hpp>
54 #include <boost/filesystem/operations.hpp>
59 using std::back_inserter;
66 using std::istream_iterator;
68 using boost::shared_ptr;
69 namespace fs = boost::filesystem;
73 using support::bformat;
74 using support::FileFilterList;
75 using support::FileName;
76 using support::ForkedProcess;
77 using support::isLyXFilename;
78 using support::libFileSearch;
79 using support::makeAbsPath;
80 using support::makeDisplayPath;
81 using support::onlyFilename;
82 using support::onlyPath;
83 using support::package;
84 using support::removeAutosaveFile;
85 using support::rename;
87 using support::Systemcall;
88 using support::tempName;
89 using support::unlink;
90 using frontend::LyXView;
92 namespace Alert = frontend::Alert;
95 // Copyright CHT Software Service GmbH
98 // create new file with template
101 void newFile(LyXView & lv, string const & filename)
103 // Split argument by :
105 string tmpname = split(filename, name, ':');
106 LYXERR(Debug::INFO) << "Arg is " << filename
107 << "\nName is " << name
108 << "\nTemplate is " << tmpname << endl;
110 Buffer * const b = newFile(name, tmpname);
116 // Insert plain text file (if filename is empty, prompt for one)
117 void insertPlaintextFile(BufferView * bv, string const & f, bool asParagraph)
119 docstring const tmpstr =
120 getContentsOfPlaintextFile(bv, f, asParagraph);
125 Cursor & cur = bv->cursor();
126 cap::replaceSelection(cur);
129 cur.innerText()->insertStringAsParagraphs(cur, tmpstr);
131 cur.innerText()->insertStringAsLines(cur, tmpstr);
135 docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f,
141 FileDialog fileDlg(_("Select file to insert"),
143 ? LFUN_FILE_INSERT_PLAINTEXT_PARA
144 : LFUN_FILE_INSERT_PLAINTEXT) );
146 FileDialog::Result result =
147 fileDlg.open(from_utf8(bv->buffer().filePath()),
148 FileFilterList(), docstring());
150 if (result.first == FileDialog::Later)
153 fname = makeAbsPath(to_utf8(result.second));
159 if (!fs::is_readable(fname.toFilesystemEncoding())) {
160 docstring const error = from_ascii(strerror(errno));
161 docstring const file = makeDisplayPath(fname.absFilename(), 50);
162 docstring const text =
163 bformat(_("Could not read the specified document\n"
164 "%1$s\ndue to the error: %2$s"), file, error);
165 Alert::error(_("Could not read file"), text);
169 ifstream ifs(fname.toFilesystemEncoding().c_str());
171 docstring const error = from_ascii(strerror(errno));
172 docstring const file = makeDisplayPath(fname.absFilename(), 50);
173 docstring const text =
174 bformat(_("Could not open the specified document\n"
175 "%1$s\ndue to the error: %2$s"), file, error);
176 Alert::error(_("Could not open file"), text);
180 ifs.unsetf(ios::skipws);
181 istream_iterator<char> ii(ifs);
182 istream_iterator<char> end;
183 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
184 // We use this until the compilers get better...
185 std::vector<char> tmp;
186 copy(ii, end, back_inserter(tmp));
187 string const tmpstr(tmp.begin(), tmp.end());
189 // This is what we want to use and what we will use once the
190 // compilers get good enough.
191 //string tmpstr(ii, end); // yet a reason for using std::string
192 // alternate approach to get the file into a string:
194 copy(ii, end, back_inserter(tmpstr));
197 // FIXME UNICODE: We don't know the encoding of the file
198 docstring file_content = from_utf8(tmpstr);
199 if (file_content.empty()) {
200 Alert::error(_("Reading not UTF-8 encoded file"),
201 _("The file is not UTF-8 encoded.\n"
202 "It will be read as local 8Bit-encoded.\n"
203 "If this does not give the correct result\n"
204 "then please change the encoding of the file\n"
205 "to UTF-8 with a program other than LyX.\n"));
206 file_content = from_local8bit(tmpstr);
209 return normalize_c(file_content);
213 // This function runs "configure" and then rereads lyx.defaults to
214 // reconfigure the automatic settings.
215 void reconfigure(LyXView & lv, string const & option)
217 // emit message signal.
218 lv.message(_("Running configure..."));
220 // Run configure in user lyx directory
221 support::Path p(package().user_support());
222 string configure_command = package().configure_command();
223 configure_command += option;
225 one.startscript(Systemcall::Wait, configure_command);
227 // emit message signal.
228 lv.message(_("Reloading configuration..."));
229 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
230 // Re-read packages.lst
231 LaTeXFeatures::getAvailable();
233 Alert::information(_("System reconfigured"),
234 _("The system has been reconfigured.\n"
235 "You need to restart LyX to make use of any\n"
236 "updated document class specifications."));