// Insert ascii file (if filename is empty, prompt for one)
void InsertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
{
- string fname = f;
-
if (!bv->available())
return;
-
+
+ string const tmpstr = getContentsOfAsciiFile(bv, f, asParagraph);
+ if (tmpstr.empty())
+ return;
+
+ // insert the string
+ bv->hideCursor();
+
+ // clear the selection
+ bool flag = (bv->text == bv->getLyXText());
+ if (flag)
+ bv->beforeChange(bv->text);
+ if (!asParagraph)
+ bv->getLyXText()->insertStringAsLines(bv, tmpstr);
+ else
+ bv->getLyXText()->insertStringAsParagraphs(bv, tmpstr);
+ if (flag)
+ bv->update(bv->text,
+ BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+}
+
+
+// Insert ascii file (if filename is empty, prompt for one)
+string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagraph)
+{
+ string fname = f;
+
if (fname.empty()) {
FileDialog fileDlg(bv->owner(), _("Select file to insert"),
(asParagraph) ? LFUN_FILE_INSERT_ASCII_PARA : LFUN_FILE_INSERT_ASCII);
FileDialog::Result result = fileDlg.Select(bv->owner()->buffer()->filePath());
if (result.first == FileDialog::Later)
- return;
+ return string();
fname = result.second;
if (fname.empty())
- return;
+ return string();
}
FileInfo fi(fname);
if (!fi.readable()) {
Alert::err_alert(_("Error! Specified file is unreadable: "),
MakeDisplayPath(fname, 50));
- return;
+ return string();
}
ifstream ifs(fname.c_str());
if (!ifs) {
Alert::err_alert(_("Error! Cannot open specified file: "),
MakeDisplayPath(fname, 50));
- return;
+ return string();
}
ifs.unsetf(ios::skipws);
string tmpstr;
copy(ii, end, back_inserter(tmpstr));
#endif
- // insert the string
- bv->hideCursor();
- // clear the selection
- bv->beforeChange(bv->text);
- if (!asParagraph)
- bv->text->insertStringAsLines(bv, tmpstr);
- else
- bv->text->insertStringAsParagraphs(bv, tmpstr);
- bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+ return tmpstr;
}
bv->owner()->prohibitInput();
if (label.empty()) {
Paragraph * par = bv->getLyXText()->cursor.par();
- LyXLayout const * layout =
- &textclasslist.Style(bv->buffer()->params.textclass,
- par->getLayout());
+ LyXTextClass const & tclass =
+ textclasslist[bv->buffer()->params.textclass];
+ LyXLayout const * layout = &tclass[par->layout()];
if (layout->latextype == LATEX_PARAGRAPH && par->previous()) {
Paragraph * par2 = par->previous();
- LyXLayout const * layout2 =
- &textclasslist.Style(bv->buffer()->params.textclass,
- par2->getLayout());
+ LyXLayout const * layout2 = &tclass[par2->layout()];
if (layout2->latextype != LATEX_PARAGRAPH) {
par = par2;
layout = layout2;