With this patch, LyX fills the command buffer history with the commands
that were used before.
Remarks or objections ?
Vincent
Index: src/frontends/qt4/GuiCommandBuffer.cpp
===================================================================
--- src/frontends/qt4/GuiCommandBuffer.cpp (revision 28166)
+++ src/frontends/qt4/GuiCommandBuffer.cpp (working copy)
@@ -23,6 +23,7 @@
#include "LyXFunc.h"
#include "LyXAction.h"
#include "FuncRequest.h"
+#include "Session.h"
#include "support/lyxalgo.h"
#include "support/lstrings.h"
@@ -80,7 +81,7 @@
GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
- : view_(view), history_pos_(history_.end())
+ : view_(view)
{
transform(lyxaction.func_begin(), lyxaction.func_end(),
back_inserter(commands_), firster());
@@ -117,6 +118,17 @@
top->addLayout(layout);
top->setMargin(0);
setFocusProxy(edit_);
+
+ LastCommandsSection::LastCommands last_commands
+ = theSession().lastCommands().getcommands();
+ LastCommandsSection::LastCommands::const_iterator it
+ = last_commands.begin();
+ LastCommandsSection::LastCommands::const_iterator end
+ = last_commands.end();
+ for(; it != end; ++it)
+ history_.push_back(*it);
+ history_pos_ = history_.end();
+
}
@@ -133,7 +145,9 @@
view_->setFocus();
edit_->setText(QString());
edit_->clearFocus();
- dispatch(fromqstr(cmd));
+ string const cmd_ = fromqstr(cmd);
+ theSession().lastCommands().add(cmd_);
+ dispatch(cmd_);
}
Index: src/Session.cpp
===================================================================
--- src/Session.cpp (revision 28166)
+++ src/Session.cpp (working copy)
@@ -33,6 +33,7 @@
string const sec_bookmarks = "[bookmarks]";
string const sec_session = "[session info]";
string const sec_toolbars = "[toolbars]";
+string const sec_lastcommands = "[last commands]";
} // anon namespace
@@ -301,9 +302,65 @@
}
-Session::Session(unsigned int num) :
- last_files(num)
+LastCommandsSection::LastCommandsSection(unsigned int num) :
+ default_num_last_commands(30),
+ absolute_max_last_commands(100)
{
+ setNumberOfLastCommands(num);
+}
+
+
+void LastCommandsSection::read(istream & is)
+{
+ string tmp;
+ do {
+ char c = is.peek();
+ if (c == '[')
+ break;
+ getline(is, tmp);
+ if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
+ continue;
+
+ lastcommands.push_back(tmp);
+ } while (is.good());
+}
+
+
+void LastCommandsSection::write(ostream & os) const
+{
+ os << '\n' << sec_lastcommands << '\n';
+ copy(lastcommands.begin(), lastcommands.end(),
+ ostream_iterator<string>(os, "\n"));
+}
+
+
+void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
+{
+ if (0 < no && no <= absolute_max_last_commands)
+ num_lastcommands = no;
+ else {
+ LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
+ << "\tdefault (=" << default_num_last_commands << ")
used.");
+ num_lastcommands = default_num_last_commands;
+ }
+}
+
+
+void LastCommandsSection::add(string const & command)
+{
+ lastcommands.push_back(command);
+}
+
+
+void LastCommandsSection::clear()
+{
+ lastcommands.clear();
+}
+
+
+Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
+ last_files(num_last_files), last_commands(num_last_commands)
+{
// locate the session file
// note that the session file name 'session' is hard-coded
session_file = FileName(addName(package().user_support().absFilename(),
"session"));
@@ -333,6 +390,9 @@
lastFilePos().read(is);
else if (tmp == sec_bookmarks)
bookmarks().read(is);
+ else if (tmp == sec_lastcommands)
+ lastCommands().read(is);
+
else
LYXERR(Debug::INIT, "LyX: Warning: unknown Session
section: " << tmp);
}
@@ -349,6 +409,7 @@
lastFiles().write(os);
lastOpened().write(os);
lastFilePos().write(os);
+ lastCommands().write(os);
bookmarks().write(os);
} else
LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
Index: src/Session.h
===================================================================
--- src/Session.h (revision 28166)
+++ src/Session.h (working copy)
@@ -27,6 +27,7 @@
3. opened files when a lyx session is closed (lastopened)
4. bookmarks
5. general purpose session info in the form of key/value pairs
+ 6. the latest commands entered in the command buffer (lastcommands)
*/
namespace lyx {
@@ -263,11 +264,59 @@
};
+class LastCommandsSection : SessionSection
+{
+public:
+ ///
+ typedef std::vector<std::string> LastCommands;
+
+public:
+ ///
+ LastCommandsSection(unsigned int num);
+ ///
+ void read(std::istream & is);
+
+ ///
+ void write(std::ostream & os) const;
+
+ /// Return lastcommands container (vector)
+ LastCommands const getcommands() const { return lastcommands; }
+
+ /** add command to lastcommands list
+ @param command command to add
+ */
+ void add(std::string const & command);
+
+ /** clear lastcommands list
+ */
+ void clear();
+
+private:
+ /// number of commands in the lastcommands list.
+ unsigned int num_lastcommands;
+
+ /** Used by the constructor to set the number of stored last commands.
+ @param num the number of lastcommands to set.
+ */
+ void setNumberOfLastCommands(unsigned int num);
+
+ /// a list of lastopened commands
+ LastCommands lastcommands;
+
+ /// Default number of lastcommands.
+ unsigned int const default_num_last_commands;
+
+ /// Max number of lastcommands.
+ unsigned int const absolute_max_last_commands;
+};
+
+
class Session
{
public:
/// Read the session file. @param num length of lastfiles
- explicit Session(unsigned int num = 4);
+ explicit Session(unsigned int num_last_files = 4,
+ unsigned int num_last_commands = 30);
/// Write the session file.
void writeFile() const;
///
@@ -286,6 +335,10 @@
BookmarksSection & bookmarks() { return bookmarks_; }
///
BookmarksSection const & bookmarks() const { return bookmarks_; }
+ ///
+ LastCommandsSection & lastCommands() { return last_commands; }
+ ///
+ LastCommandsSection const & lastCommands() const { return
last_commands; }
private:
friend class LyX;
@@ -311,6 +364,8 @@
LastFilePosSection last_file_pos;
///
BookmarksSection bookmarks_;
+ ///
+ LastCommandsSection last_commands;
};
/// This is a singleton class. Get the instance.