From 095cf2339405a9d8c1789168a545d7f75e2cac0e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 3 Oct 2007 18:56:37 +0000 Subject: [PATCH] shuffle some code around git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20706 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 96 +++++++++++++++++ src/BufferView.h | 2 + src/Makefile.am | 2 - src/callback.cpp | 191 ---------------------------------- src/callback.h | 33 ------ src/frontends/qt4/Action.cpp | 1 - src/frontends/qt4/GuiView.cpp | 3 +- 7 files changed, 100 insertions(+), 228 deletions(-) delete mode 100644 src/callback.cpp delete mode 100644 src/callback.h diff --git a/src/BufferView.cpp b/src/BufferView.cpp index c95a19a0c7..a7a38b723c 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -2019,4 +2019,100 @@ void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui) } +static docstring contentsOfPlaintextFile(BufferView * bv, string const & f, + bool asParagraph) +{ + FileName fname(f); + + if (fname.empty()) { + FileDialog fileDlg(_("Select file to insert"), + ( asParagraph + ? LFUN_FILE_INSERT_PLAINTEXT_PARA + : LFUN_FILE_INSERT_PLAINTEXT) ); + + FileDialog::Result result = + fileDlg.open(from_utf8(bv->buffer().filePath()), + FileFilterList(), docstring()); + + if (result.first == FileDialog::Later) + return docstring(); + + fname = makeAbsPath(to_utf8(result.second)); + + if (fname.empty()) + return docstring(); + } + + if (!fs::is_readable(fname.toFilesystemEncoding())) { + docstring const error = from_ascii(strerror(errno)); + docstring const file = makeDisplayPath(fname.absFilename(), 50); + docstring const text = + bformat(_("Could not read the specified document\n" + "%1$s\ndue to the error: %2$s"), file, error); + Alert::error(_("Could not read file"), text); + return docstring(); + } + + ifstream ifs(fname.toFilesystemEncoding().c_str()); + if (!ifs) { + docstring const error = from_ascii(strerror(errno)); + docstring const file = makeDisplayPath(fname.absFilename(), 50); + docstring const text = + bformat(_("Could not open the specified document\n" + "%1$s\ndue to the error: %2$s"), file, error); + Alert::error(_("Could not open file"), text); + return docstring(); + } + + ifs.unsetf(ios::skipws); + istream_iterator ii(ifs); + istream_iterator end; +#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD) + // We use this until the compilers get better... + std::vector tmp; + copy(ii, end, back_inserter(tmp)); + string const tmpstr(tmp.begin(), tmp.end()); +#else + // This is what we want to use and what we will use once the + // compilers get good enough. + //string tmpstr(ii, end); // yet a reason for using std::string + // alternate approach to get the file into a string: + string tmpstr; + copy(ii, end, back_inserter(tmpstr)); +#endif + + // FIXME UNICODE: We don't know the encoding of the file + docstring file_content = from_utf8(tmpstr); + if (file_content.empty()) { + Alert::error(_("Reading not UTF-8 encoded file"), + _("The file is not UTF-8 encoded.\n" + "It will be read as local 8Bit-encoded.\n" + "If this does not give the correct result\n" + "then please change the encoding of the file\n" + "to UTF-8 with a program other than LyX.\n")); + file_content = from_local8bit(tmpstr); + } + + return normalize_c(file_content); +} + +// Insert plain text file (if filename is empty, prompt for one) +void BufferView::insertPlaintextFile(string const & f, bool asParagraph) +{ + docstring const tmpstr = + contentsOfPlaintextFile(this, f, asParagraph); + + if (tmpstr.empty()) + return; + + Cursor & cur = cursor(); + cap::replaceSelection(cur); + recordUndo(cur); + if (asParagraph) + cur.innerText()->insertStringAsParagraphs(cur, tmpstr); + else + cur.innerText()->insertStringAsLines(cur, tmpstr); +} + + } // namespace lyx diff --git a/src/BufferView.h b/src/BufferView.h index 1c02f954d7..2d40bc1966 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -256,6 +256,8 @@ public: /// void setGuiDelegate(frontend::GuiBufferViewDelegate *); + // Insert plain text file (if filename is empty, prompt for one) + void insertPlaintextFile(std::string const & fileName, bool asParagraph); private: // the position relative to (0, baseline) of outermost paragraph Point coordOffset(DocIterator const & dit, bool boundary) const; diff --git a/src/Makefile.am b/src/Makefile.am index 273757014d..c5a2d6b38e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -183,8 +183,6 @@ liblyxcore_la_SOURCES = \ lfuns.h \ LyXAction.cpp \ LyXAction.h \ - callback.cpp \ - callback.h \ LyX.cpp \ lyxfind.cpp \ lyxfind.h \ diff --git a/src/callback.cpp b/src/callback.cpp deleted file mode 100644 index e2922d5ce2..0000000000 --- a/src/callback.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/** - * \file callback.cpp - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Lars Gullik Bjønnes - * \author Angus Leeming - * \author John Levon - * \author André Pönitz - * \author Jürgen Vigna - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "callback.h" - -#include "Buffer.h" -#include "BufferList.h" -#include "BufferView.h" -#include "buffer_funcs.h" -#include "Cursor.h" -#include "CutAndPaste.h" -#include "debug.h" -#include "gettext.h" -#include "Session.h" -#include "LaTeXFeatures.h" -#include "LyX.h" -#include "Layout.h" -#include "LyXRC.h" -#include "Text.h" -#include "Undo.h" - -#include "frontends/alert.h" -#include "frontends/Application.h" -#include "frontends/FileDialog.h" -#include "frontends/LyXView.h" - -#include "support/FileFilterList.h" -#include "support/filetools.h" -#include "support/Forkedcall.h" -#include "support/fs_extras.h" -#include "support/lyxlib.h" -#include "support/Package.h" -#include "support/Path.h" -#include "support/Systemcall.h" - -#if !defined (HAVE_FORK) -# define fork() -1 -#endif - -#include -#include - -#include -#include - -using std::back_inserter; -using std::copy; -using std::endl; -using std::make_pair; -using std::string; -using std::ifstream; -using std::ios; -using std::istream_iterator; - -using boost::shared_ptr; -namespace fs = boost::filesystem; - -namespace lyx { - -using support::bformat; -using support::FileFilterList; -using support::FileName; -using support::ForkedProcess; -using support::isLyXFilename; -using support::libFileSearch; -using support::makeAbsPath; -using support::makeDisplayPath; -using support::onlyFilename; -using support::onlyPath; -using support::package; -using support::removeAutosaveFile; -using support::rename; -using support::split; -using support::Systemcall; -using support::tempName; -using support::unlink; -using frontend::LyXView; - -namespace Alert = frontend::Alert; - -// Insert plain text file (if filename is empty, prompt for one) -void insertPlaintextFile(BufferView * bv, string const & f, bool asParagraph) -{ - docstring const tmpstr = - getContentsOfPlaintextFile(bv, f, asParagraph); - - if (tmpstr.empty()) - return; - - Cursor & cur = bv->cursor(); - cap::replaceSelection(cur); - recordUndo(cur); - if (asParagraph) - cur.innerText()->insertStringAsParagraphs(cur, tmpstr); - else - cur.innerText()->insertStringAsLines(cur, tmpstr); -} - - -docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f, - bool asParagraph) -{ - FileName fname(f); - - if (fname.empty()) { - FileDialog fileDlg(_("Select file to insert"), - ( (asParagraph) - ? LFUN_FILE_INSERT_PLAINTEXT_PARA - : LFUN_FILE_INSERT_PLAINTEXT) ); - - FileDialog::Result result = - fileDlg.open(from_utf8(bv->buffer().filePath()), - FileFilterList(), docstring()); - - if (result.first == FileDialog::Later) - return docstring(); - - fname = makeAbsPath(to_utf8(result.second)); - - if (fname.empty()) - return docstring(); - } - - if (!fs::is_readable(fname.toFilesystemEncoding())) { - docstring const error = from_ascii(strerror(errno)); - docstring const file = makeDisplayPath(fname.absFilename(), 50); - docstring const text = - bformat(_("Could not read the specified document\n" - "%1$s\ndue to the error: %2$s"), file, error); - Alert::error(_("Could not read file"), text); - return docstring(); - } - - ifstream ifs(fname.toFilesystemEncoding().c_str()); - if (!ifs) { - docstring const error = from_ascii(strerror(errno)); - docstring const file = makeDisplayPath(fname.absFilename(), 50); - docstring const text = - bformat(_("Could not open the specified document\n" - "%1$s\ndue to the error: %2$s"), file, error); - Alert::error(_("Could not open file"), text); - return docstring(); - } - - ifs.unsetf(ios::skipws); - istream_iterator ii(ifs); - istream_iterator end; -#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD) - // We use this until the compilers get better... - std::vector tmp; - copy(ii, end, back_inserter(tmp)); - string const tmpstr(tmp.begin(), tmp.end()); -#else - // This is what we want to use and what we will use once the - // compilers get good enough. - //string tmpstr(ii, end); // yet a reason for using std::string - // alternate approach to get the file into a string: - string tmpstr; - copy(ii, end, back_inserter(tmpstr)); -#endif - - // FIXME UNICODE: We don't know the encoding of the file - docstring file_content = from_utf8(tmpstr); - if (file_content.empty()) { - Alert::error(_("Reading not UTF-8 encoded file"), - _("The file is not UTF-8 encoded.\n" - "It will be read as local 8Bit-encoded.\n" - "If this does not give the correct result\n" - "then please change the encoding of the file\n" - "to UTF-8 with a program other than LyX.\n")); - file_content = from_local8bit(tmpstr); - } - - return normalize_c(file_content); -} - - -} // namespace lyx diff --git a/src/callback.h b/src/callback.h deleted file mode 100644 index 8936f50580..0000000000 --- a/src/callback.h +++ /dev/null @@ -1,33 +0,0 @@ -// -*- C++ -*- -/** - * \file callback.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Lars Gullik Bjønnes - * - * Full author contact details are available in file CREDITS. - */ - -#ifndef CALLBACK_H -#define CALLBACK_H - -#include "support/docstring.h" - -namespace lyx { - -class Buffer; -class BufferView; - -/// -extern bool quitting; - -/// read plain text file (if \p f is empty, prompt for a filename) -docstring const getContentsOfPlaintextFile(BufferView * bv, - std::string const & f, bool asParagraph); -/// -void insertPlaintextFile(BufferView * bv, std::string const & f, bool asParagraph); - -} // namespace lyx - -#endif diff --git a/src/frontends/qt4/Action.cpp b/src/frontends/qt4/Action.cpp index 683f073165..63a05c68eb 100644 --- a/src/frontends/qt4/Action.cpp +++ b/src/frontends/qt4/Action.cpp @@ -14,7 +14,6 @@ #include "frontends/LyXView.h" -#include "callback.h" #include "debug.h" #include "FuncRequest.h" #include "FuncStatus.h" diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 0b0e9f3c3d..6aa66d9bd2 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -37,7 +37,6 @@ #include "BufferParams.h" #include "BufferView.h" #include "BufferList.h" -#include "callback.h" #include "debug.h" #include "FuncRequest.h" #include "Layout.h" @@ -79,6 +78,8 @@ using support::FileName; using support::libFileSearch; using support::makeDisplayPath; +extern bool quitting; + namespace frontend { namespace { -- 2.39.2