Add LFUN_BUFFER_WRITE_AS_TEMPLATE

Convenience function to easier save a file as template in the appropriate
templates folder.
This commit is contained in:
Juergen Spitzmueller 2019-03-23 10:17:33 +01:00
parent 75d42f72d9
commit ebc4092649
6 changed files with 84 additions and 5 deletions

View File

@ -52,6 +52,7 @@ Menuset
Item "Close All" "buffer-close-all" Item "Close All" "buffer-close-all"
Item "Save|S" "buffer-write" Item "Save|S" "buffer-write"
Item "Save As...|A" "buffer-write-as" Item "Save As...|A" "buffer-write-as"
Item "Save As Template..." "buffer-write-as-template"
Item "Save All|l" "buffer-write-all" Item "Save All|l" "buffer-write-all"
Item "Revert to Saved|R" "buffer-reload" Item "Revert to Saved|R" "buffer-reload"
Submenu "Version Control|V" "file_vc" Submenu "Version Control|V" "file_vc"

View File

@ -482,6 +482,7 @@ enum FuncCode
LFUN_INSET_END_EDIT, // gb/rkh, 20180605 LFUN_INSET_END_EDIT, // gb/rkh, 20180605
// 375 // 375
LFUN_SEARCH_IGNORE, // kornel, 20181115 LFUN_SEARCH_IGNORE, // kornel, 20181115
LFUN_BUFFER_WRITE_AS_TEMPLATE, // spitz, 20190323
LFUN_LASTACTION // end of the table LFUN_LASTACTION // end of the table
}; };

View File

@ -893,6 +893,17 @@ void LyXAction::init()
*/ */
{ LFUN_BUFFER_WRITE_AS, "buffer-write-as", ReadOnly, Buffer }, { LFUN_BUFFER_WRITE_AS, "buffer-write-as", ReadOnly, Buffer },
/*!
* \var lyx::FuncCode lyx::LFUN_BUFFER_WRITE_AS_TEMPLATE
* \li Action: Rename and save current buffer in the local templates directory.
* \li Syntax: buffer-write-as-template <FILENAME>
* \li Params: <FILENAME>: New name of the buffer/file. A relative path
is with respect to the original location of the buffer/file.
* \endvar
*/
{ LFUN_BUFFER_WRITE_AS_TEMPLATE, "buffer-write-as-template", ReadOnly, Buffer },
/*! /*!
* \var lyx::FuncCode lyx::LFUN_BUFFER_EXTERNAL_MODIFICATION_CLEAR * \var lyx::FuncCode lyx::LFUN_BUFFER_EXTERNAL_MODIFICATION_CLEAR
* \li Action: Clear the external modification flag on the current buffer. * \li Action: Clear the external modification flag on the current buffer.

View File

@ -1222,7 +1222,8 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
// GuiView::getStatus(). See above. // GuiView::getStatus(). See above.
/* /*
case LFUN_BUFFER_WRITE: case LFUN_BUFFER_WRITE:
case LFUN_BUFFER_WRITE_AS: { case LFUN_BUFFER_WRITE_AS:
case LFUN_BUFFER_WRITE_AS_TEMPLATE: {
Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0))); Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
enable = b && (b->isUnnamed() || !b->isClean()); enable = b && (b->isUnnamed() || !b->isClean());
break; break;

View File

@ -52,6 +52,7 @@
#include "FuncRequest.h" #include "FuncRequest.h"
#include "Intl.h" #include "Intl.h"
#include "Layout.h" #include "Layout.h"
#include "LayoutFile.h"
#include "Lexer.h" #include "Lexer.h"
#include "LyXAction.h" #include "LyXAction.h"
#include "LyX.h" #include "LyX.h"
@ -1990,6 +1991,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
} }
// fall through // fall through
case LFUN_BUFFER_WRITE_AS: case LFUN_BUFFER_WRITE_AS:
case LFUN_BUFFER_WRITE_AS_TEMPLATE:
enable = doc_buffer != 0; enable = doc_buffer != 0;
break; break;
@ -2646,29 +2648,78 @@ void GuiView::insertLyXFile(docstring const & fname)
} }
string const GuiView::getTemplatesPath(Buffer & b)
{
// We start off with the user's templates path
string result = addPath(package().user_support().absFileName(), "templates");
// Do we have a layout category?
string const cat = b.params().baseClass() ?
b.params().baseClass()->category()
: string();
if (!cat.empty()) {
string subpath = addPath(result, cat);
// If we have a subdirectory for the category already,
// navigate there
FileName sp = FileName(subpath);
if (sp.isDirectory())
result = subpath;
else {
// Ask whether we should create such a subdirectory
docstring const text =
bformat(_("It is suggested to save the template in a subdirectory\n"
"appropriate to the layout category (%1$s).\n"
"This subdirectory does not exists yet.\n"
"Do you want to create it?"),
from_utf8(cat));
if (Alert::prompt(_("Create Category Directory?"),
text, 0, 1, _("&Yes, Create"), _("&No, Save Template in Parent Directory")) == 0) {
// If the user agreed, we try to create it and report if this failed.
if (!sp.createDirectory(0777))
Alert::error(_("Subdirectory creation failed!"),
_("Could not create subdirectory.\n"
"The template will be saved in the parent directory."));
else
result = subpath;
}
}
}
return result;
}
bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kind) bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kind)
{ {
FileName fname = b.fileName(); FileName fname = b.fileName();
FileName const oldname = fname; FileName const oldname = fname;
bool const as_template = (kind == LV_WRITE_AS_TEMPLATE);
if (!newname.empty()) { if (!newname.empty()) {
// FIXME UNICODE // FIXME UNICODE
fname = support::makeAbsPath(to_utf8(newname), oldname.onlyPath().absFileName()); if (as_template)
fname = support::makeAbsPath(to_utf8(newname), getTemplatesPath(b));
else
fname = support::makeAbsPath(to_utf8(newname),
oldname.onlyPath().absFileName());
} else { } else {
// Switch to this Buffer. // Switch to this Buffer.
setBuffer(&b); setBuffer(&b);
// No argument? Ask user through dialog. // No argument? Ask user through dialog.
// FIXME UNICODE // FIXME UNICODE
FileDialog dlg(qt_("Choose a filename to save document as")); QString const title = as_template ? qt_("Choose a filename to save template as")
: qt_("Choose a filename to save document as");
FileDialog dlg(title);
dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path)); dlg.setButton1(qt_("D&ocuments"), toqstr(lyxrc.document_path));
dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path)); dlg.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
if (!isLyXFileName(fname.absFileName())) if (!isLyXFileName(fname.absFileName()))
fname.changeExtension(".lyx"); fname.changeExtension(".lyx");
string const path = as_template ?
getTemplatesPath(b)
: fname.onlyPath().absFileName();
FileDialog::Result result = FileDialog::Result result =
dlg.save(toqstr(fname.onlyPath().absFileName()), dlg.save(toqstr(path),
QStringList(qt_("LyX Documents (*.lyx)")), QStringList(qt_("LyX Documents (*.lyx)")),
toqstr(fname.onlyFileName())); toqstr(fname.onlyFileName()));
@ -2760,6 +2811,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kin
break; break;
} }
case LV_WRITE_AS: case LV_WRITE_AS:
case LV_WRITE_AS_TEMPLATE:
break; break;
} }
// LyXVC created the file already in case of LV_VC_RENAME or // LyXVC created the file already in case of LV_VC_RENAME or
@ -4042,6 +4094,12 @@ void GuiView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
renameBuffer(*doc_buffer, cmd.argument()); renameBuffer(*doc_buffer, cmd.argument());
break; break;
case LFUN_BUFFER_WRITE_AS_TEMPLATE:
LASSERT(doc_buffer, break);
renameBuffer(*doc_buffer, cmd.argument(),
LV_WRITE_AS_TEMPLATE);
break;
case LFUN_BUFFER_WRITE_ALL: { case LFUN_BUFFER_WRITE_ALL: {
Buffer * first = theBufferList().first(); Buffer * first = theBufferList().first();
if (!first) if (!first)

View File

@ -380,7 +380,14 @@ private:
bool exportBufferAs(Buffer & b, docstring const & iformat); bool exportBufferAs(Buffer & b, docstring const & iformat);
/// ///
enum RenameKind { LV_WRITE_AS, LV_VC_RENAME, LV_VC_COPY }; enum RenameKind {
LV_WRITE_AS,
LV_WRITE_AS_TEMPLATE,
LV_VC_RENAME,
LV_VC_COPY,
};
/// Get a path for LFUN_BUFFER_WRITE_AS_TEMPLATE
std::string const getTemplatesPath(Buffer & buf);
/// Save a buffer as a new file. /// Save a buffer as a new file.
/** /**
Write a buffer to a new file name and rename the buffer Write a buffer to a new file name and rename the buffer