]> git.lyx.org Git - features.git/commitdiff
Add LFUN_BUFFER_WRITE_AS_TEMPLATE
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 23 Mar 2019 09:17:33 +0000 (10:17 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:19 +0000 (15:48 +0200)
Convenience function to easier save a file as template in the appropriate
templates folder.

lib/ui/stdmenus.inc
src/FuncCode.h
src/LyXAction.cpp
src/frontends/qt4/GuiApplication.cpp
src/frontends/qt4/GuiView.cpp
src/frontends/qt4/GuiView.h

index 9f6515dc21520aa46dcbac35f137f5b3a1ab5f7d..afe883a751cbb4809a994a46f7ab6935fd019e8f 100644 (file)
@@ -52,6 +52,7 @@ Menuset
                Item "Close All" "buffer-close-all"
                Item "Save|S" "buffer-write"
                Item "Save As...|A" "buffer-write-as"
+               Item "Save As Template..." "buffer-write-as-template"
                Item "Save All|l" "buffer-write-all"
                Item "Revert to Saved|R" "buffer-reload"
                Submenu "Version Control|V" "file_vc"
index 14b9ba152ffbd756f7ef62ff285192cff3fbbb73..b9aed5d4582f59a8e9107ce5ddee7885a3d0a25d 100644 (file)
@@ -482,6 +482,7 @@ enum FuncCode
        LFUN_INSET_END_EDIT,            // gb/rkh, 20180605
        // 375
        LFUN_SEARCH_IGNORE,             // kornel, 20181115
+       LFUN_BUFFER_WRITE_AS_TEMPLATE,  // spitz, 20190323
        LFUN_LASTACTION                 // end of the table
 };
 
index 082d6080d4e6383010a9b97cd41e2466675ae6fe..92734a202a5d4e9d97f73cef0e130abde9671362 100644 (file)
@@ -893,6 +893,17 @@ void LyXAction::init()
  */
                { 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
  * \li Action: Clear the external modification flag on the current buffer.
index fdacceea515d9a6b0344a083d6c563ad479f0ee0..666b961095e0700e7c682f27c807b0ccd4eb85d2 100644 (file)
@@ -1222,7 +1222,8 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
        // GuiView::getStatus(). See above.
        /*
        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)));
                enable = b && (b->isUnnamed() || !b->isClean());
                break;
index 6624d813625cf75fbf56080f922d30496a99ceab..2f59145af90aa3b1a0d01d7b1fa5721779b4fed8 100644 (file)
@@ -52,6 +52,7 @@
 #include "FuncRequest.h"
 #include "Intl.h"
 #include "Layout.h"
+#include "LayoutFile.h"
 #include "Lexer.h"
 #include "LyXAction.h"
 #include "LyX.h"
@@ -1990,6 +1991,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                }
                // fall through
        case LFUN_BUFFER_WRITE_AS:
+       case LFUN_BUFFER_WRITE_AS_TEMPLATE:
                enable = doc_buffer != 0;
                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)
 {
        FileName fname = b.fileName();
        FileName const oldname = fname;
+       bool const as_template = (kind == LV_WRITE_AS_TEMPLATE);
 
        if (!newname.empty()) {
                // 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 {
                // Switch to this Buffer.
                setBuffer(&b);
 
                // No argument? Ask user through dialog.
                // 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.setButton2(qt_("&Templates"), toqstr(lyxrc.template_path));
 
                if (!isLyXFileName(fname.absFileName()))
                        fname.changeExtension(".lyx");
 
+               string const path = as_template ?
+                                       getTemplatesPath(b)
+                                     : fname.onlyPath().absFileName();
                FileDialog::Result result =
-                       dlg.save(toqstr(fname.onlyPath().absFileName()),
+                       dlg.save(toqstr(path),
                                   QStringList(qt_("LyX Documents (*.lyx)")),
                                         toqstr(fname.onlyFileName()));
 
@@ -2760,6 +2811,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname, RenameKind kin
                break;
        }
        case LV_WRITE_AS:
+       case LV_WRITE_AS_TEMPLATE:
                break;
        }
        // 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());
                        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: {
                        Buffer * first = theBufferList().first();
                        if (!first)
index 3c5041662fe52ca0fb915ec8aa2884ea649750f2..493b070b252eef190ea1a09abed82e87fcdc39de 100644 (file)
@@ -380,7 +380,14 @@ private:
        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.
        /**
        Write a buffer to a new file name and rename the buffer