2002-06-12 03:16:41 +00:00
|
|
|
/**
|
2007-04-27 08:05:58 +00:00
|
|
|
* \file KeyMap.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-11-30 13:25:38 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author John Levon
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-06-12 03:16:41 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2000-02-04 09:38:32 +00:00
|
|
|
|
2007-04-27 08:05:58 +00:00
|
|
|
#include "KeyMap.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
|
2007-04-27 08:19:12 +00:00
|
|
|
#include "KeySequence.h"
|
2003-07-26 23:04:39 +00:00
|
|
|
#include "LyXAction.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
#include "Lexer.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
#include "support/filetools.h"
|
2009-07-16 23:12:45 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
2014-06-09 11:05:50 +00:00
|
|
|
#include "support/TempFile.h"
|
2009-07-16 23:12:45 +00:00
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
2004-07-24 10:55:30 +00:00
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
#include <fstream>
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2007-10-18 15:48:51 +00:00
|
|
|
#include <utility>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2007-10-02 21:51:54 +00:00
|
|
|
string const KeyMap::printKeySym(KeySymbol const & key, KeyModifier mod)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-30 13:25:38 +00:00
|
|
|
string buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2004-03-26 15:12:35 +00:00
|
|
|
string const s = key.getSymbolName();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2007-10-02 21:51:54 +00:00
|
|
|
if (mod & ControlModifier)
|
2003-06-18 13:47:11 +00:00
|
|
|
buf += "C-";
|
2011-02-23 14:30:41 +00:00
|
|
|
#if defined(USE_MACOSX_PACKAGING) || defined(USE_META_KEYBINDING)
|
|
|
|
if (mod & MetaModifier)
|
|
|
|
buf += "M-";
|
|
|
|
if (mod & AltModifier)
|
|
|
|
buf += "A-";
|
|
|
|
#else
|
2007-10-02 21:51:54 +00:00
|
|
|
if (mod & AltModifier)
|
2003-06-18 13:47:11 +00:00
|
|
|
buf += "M-";
|
2011-02-23 14:30:41 +00:00
|
|
|
#endif
|
2007-10-30 16:34:35 +00:00
|
|
|
if (mod & ShiftModifier)
|
|
|
|
buf += "S-";
|
2003-06-18 13:47:11 +00:00
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
buf += s;
|
2001-11-30 13:25:38 +00:00
|
|
|
return buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 13:47:11 +00:00
|
|
|
|
2007-04-27 08:05:58 +00:00
|
|
|
size_t KeyMap::bind(string const & seq, FuncRequest const & func)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::KBMAP, "BIND: Sequence `" << seq << "' Action `"
|
2010-04-09 19:00:42 +00:00
|
|
|
<< func.action() << '\'');
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2020-02-29 05:21:00 +00:00
|
|
|
KeySequence k(nullptr, nullptr);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-10-01 15:31:59 +00:00
|
|
|
string::size_type const res = k.parse(seq);
|
2001-11-14 11:31:14 +00:00
|
|
|
if (res == string::npos) {
|
2007-10-26 20:43:22 +00:00
|
|
|
bind(&k, func);
|
2001-11-30 13:25:38 +00:00
|
|
|
} else {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::KBMAP, "Parse error at position " << res
|
|
|
|
<< " in key sequence '" << seq << "'.");
|
2001-11-30 13:25:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
return res == string::npos ? 0 : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t KeyMap::unbind(string const & seq, FuncRequest const & func)
|
|
|
|
{
|
2020-02-29 05:21:00 +00:00
|
|
|
KeySequence k(nullptr, nullptr);
|
2007-10-20 20:35:33 +00:00
|
|
|
|
|
|
|
string::size_type const res = k.parse(seq);
|
|
|
|
if (res == string::npos)
|
2007-10-26 20:43:22 +00:00
|
|
|
unbind(&k, func);
|
2007-10-20 20:35:33 +00:00
|
|
|
else
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::KBMAP, "Parse error at position " << res
|
|
|
|
<< " in key sequence '" << seq << "'.");
|
2007-10-20 20:35:33 +00:00
|
|
|
return res == string::npos ? 0 : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-02 18:59:25 +00:00
|
|
|
void KeyMap::bind(KeySequence * seq, FuncRequest const & func, unsigned int r)
|
|
|
|
{
|
|
|
|
KeySymbol code = seq->sequence[r];
|
|
|
|
if (!code.isOK())
|
|
|
|
return;
|
|
|
|
|
|
|
|
KeyModifier const mod1 = seq->modifiers[r].first;
|
|
|
|
KeyModifier const mod2 = seq->modifiers[r].second;
|
|
|
|
|
|
|
|
// check if key is already there
|
2016-06-11 08:14:38 +00:00
|
|
|
// FIXME perf: Profiling shows that this is responsible of 99% of the
|
|
|
|
// cost of GuiPrefs::applyView()
|
2009-04-02 18:59:25 +00:00
|
|
|
Table::iterator end = table.end();
|
|
|
|
for (Table::iterator it = table.begin(); it != end; ++it) {
|
|
|
|
if (code == it->code
|
|
|
|
&& mod1 == it->mod.first
|
|
|
|
&& mod2 == it->mod.second) {
|
|
|
|
// overwrite binding
|
|
|
|
if (r + 1 == seq->length()) {
|
|
|
|
LYXERR(Debug::KBMAP, "Warning: New binding for '"
|
|
|
|
<< to_utf8(seq->print(KeySequence::Portable))
|
|
|
|
<< "' is overriding old binding...");
|
2012-10-21 19:04:05 +00:00
|
|
|
if (it->prefixes)
|
2009-04-02 18:59:25 +00:00
|
|
|
it->prefixes.reset();
|
|
|
|
it->func = func;
|
2010-04-09 19:00:42 +00:00
|
|
|
it->func.setOrigin(FuncRequest::KEYBOARD);
|
2009-04-02 18:59:25 +00:00
|
|
|
return;
|
2012-10-21 19:04:05 +00:00
|
|
|
} else if (!it->prefixes) {
|
2009-04-02 18:59:25 +00:00
|
|
|
lyxerr << "Error: New binding for '"
|
|
|
|
<< to_utf8(seq->print(KeySequence::Portable))
|
|
|
|
<< "' is overriding old binding..."
|
|
|
|
<< endl;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
it->prefixes->bind(seq, func, r + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Table::iterator newone = table.insert(table.end(), Key());
|
|
|
|
newone->code = code;
|
|
|
|
newone->mod = seq->modifiers[r];
|
|
|
|
if (r + 1 == seq->length()) {
|
|
|
|
newone->func = func;
|
2010-04-09 19:00:42 +00:00
|
|
|
newone->func.setOrigin(FuncRequest::KEYBOARD);
|
2009-04-02 18:59:25 +00:00
|
|
|
newone->prefixes.reset();
|
|
|
|
} else {
|
|
|
|
newone->prefixes.reset(new KeyMap);
|
|
|
|
newone->prefixes->bind(seq, func, r + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KeyMap::unbind(KeySequence * seq, FuncRequest const & func, unsigned int r)
|
|
|
|
{
|
|
|
|
KeySymbol code = seq->sequence[r];
|
|
|
|
if (!code.isOK())
|
|
|
|
return;
|
|
|
|
|
|
|
|
KeyModifier const mod1 = seq->modifiers[r].first;
|
|
|
|
KeyModifier const mod2 = seq->modifiers[r].second;
|
|
|
|
|
|
|
|
// check if key is already there
|
2022-07-27 09:52:50 +00:00
|
|
|
vector <Table::iterator> removes;
|
2009-04-02 18:59:25 +00:00
|
|
|
Table::iterator end = table.end();
|
|
|
|
for (Table::iterator it = table.begin(); it != end; ++it) {
|
|
|
|
if (code == it->code
|
|
|
|
&& mod1 == it->mod.first
|
|
|
|
&& mod2 == it->mod.second) {
|
|
|
|
// remove
|
|
|
|
if (r + 1 == seq->length()) {
|
2022-07-27 09:52:50 +00:00
|
|
|
if (it->func == func || func == FuncRequest::unknown) {
|
|
|
|
removes.push_back(it);
|
2012-10-21 19:04:05 +00:00
|
|
|
if (it->prefixes)
|
2009-04-02 18:59:25 +00:00
|
|
|
it->prefixes.reset();
|
|
|
|
}
|
2012-10-21 19:04:05 +00:00
|
|
|
} else if (it->prefixes) {
|
2009-04-02 18:59:25 +00:00
|
|
|
it->prefixes->unbind(seq, func, r + 1);
|
|
|
|
if (it->prefixes->empty())
|
2022-07-27 09:52:50 +00:00
|
|
|
removes.push_back(it);
|
2009-04-02 18:59:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 09:52:50 +00:00
|
|
|
|
|
|
|
for (unsigned i = removes.size(); i > 0; --i)
|
|
|
|
table.erase(removes[i-1]);
|
2009-04-02 18:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 14:56:43 +00:00
|
|
|
FuncRequest KeyMap::getBinding(KeySequence const & seq, unsigned int r)
|
2007-10-20 20:35:33 +00:00
|
|
|
{
|
|
|
|
KeySymbol code = seq.sequence[r];
|
|
|
|
if (!code.isOK())
|
2008-09-24 14:56:43 +00:00
|
|
|
return FuncRequest::unknown;
|
2007-10-20 20:35:33 +00:00
|
|
|
|
|
|
|
KeyModifier const mod1 = seq.modifiers[r].first;
|
|
|
|
KeyModifier const mod2 = seq.modifiers[r].second;
|
|
|
|
|
|
|
|
// check if key is already there
|
|
|
|
Table::iterator end = table.end();
|
|
|
|
for (Table::iterator it = table.begin(); it != end; ++it) {
|
|
|
|
if (code == it->code
|
|
|
|
&& mod1 == it->mod.first
|
|
|
|
&& mod2 == it->mod.second) {
|
|
|
|
if (r + 1 == seq.length())
|
2022-12-09 13:45:14 +00:00
|
|
|
return (it->prefixes) ? FuncRequest::prefix : it->func;
|
2012-10-21 19:04:05 +00:00
|
|
|
else if (it->prefixes)
|
2009-04-02 18:57:19 +00:00
|
|
|
return it->prefixes->getBinding(seq, r + 1);
|
2007-10-20 20:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-24 14:56:43 +00:00
|
|
|
return FuncRequest::unknown;
|
2007-10-20 20:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KeyMap::clear()
|
|
|
|
{
|
|
|
|
table.clear();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 10:30:20 +00:00
|
|
|
bool KeyMap::read(string const & bind_file, KeyMap * unbind_map, BindReadType rt, bool i18n)
|
2009-07-16 23:12:45 +00:00
|
|
|
{
|
2022-11-25 10:30:20 +00:00
|
|
|
FileName bf = i18n ? i18nLibFileSearch("bind", bind_file, "bind")
|
|
|
|
: libFileSearch("bind", bind_file, "bind");
|
2009-07-16 23:12:45 +00:00
|
|
|
if (bf.empty()) {
|
|
|
|
if (rt == MissingOK)
|
|
|
|
return true;
|
2009-07-17 21:51:53 +00:00
|
|
|
|
2009-07-16 23:12:45 +00:00
|
|
|
lyxerr << "Could not find bind file: " << bind_file;
|
|
|
|
if (rt == Default) {
|
|
|
|
frontend::Alert::warning(_("Could not find bind file"),
|
|
|
|
bformat(_("Unable to find the bind file\n%1$s.\n"
|
|
|
|
"Please check your installation."), from_utf8(bind_file)));
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-17 21:51:53 +00:00
|
|
|
|
|
|
|
static string const defaultBindfile = "cua";
|
|
|
|
if (bind_file == defaultBindfile) {
|
2009-08-29 19:44:56 +00:00
|
|
|
frontend::Alert::warning(_("Could not find `cua.bind' file"),
|
|
|
|
_("Unable to find the default bind file `cua.bind'.\n"
|
2009-07-17 21:51:53 +00:00
|
|
|
"Please check your installation."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try it with the default file.
|
2009-07-16 23:12:45 +00:00
|
|
|
frontend::Alert::warning(_("Could not find bind file"),
|
|
|
|
bformat(_("Unable to find the bind file\n%1$s.\n"
|
|
|
|
"Falling back to default."), from_utf8(bind_file)));
|
2009-07-17 21:51:53 +00:00
|
|
|
return read(defaultBindfile, unbind_map);
|
2009-07-16 23:12:45 +00:00
|
|
|
}
|
|
|
|
return read(bf, unbind_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool KeyMap::read(FileName const & bind_file, KeyMap * unbind_map)
|
2011-01-16 19:53:36 +00:00
|
|
|
{
|
|
|
|
ReturnValues retval = readWithoutConv(bind_file, unbind_map);
|
|
|
|
if (retval != FormatMismatch)
|
|
|
|
return retval == ReadOK;
|
|
|
|
|
2011-01-18 15:07:26 +00:00
|
|
|
LYXERR(Debug::FILES, "Converting bind file to " << LFUN_FORMAT);
|
2014-06-09 11:05:50 +00:00
|
|
|
TempFile tmp("convert_bind");
|
|
|
|
FileName const tempfile = tmp.name();
|
2011-01-16 19:53:36 +00:00
|
|
|
bool const success = prefs2prefs(bind_file, tempfile, true);
|
|
|
|
if (!success) {
|
|
|
|
LYXERR0 ("Unable to convert " << bind_file <<
|
2011-01-18 15:07:26 +00:00
|
|
|
" to format " << LFUN_FORMAT);
|
2011-01-16 19:53:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
retval = readWithoutConv(tempfile, unbind_map);
|
|
|
|
return retval == ReadOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KeyMap::ReturnValues KeyMap::readWithoutConv(FileName const & bind_file, KeyMap * unbind_map)
|
2003-07-26 23:04:39 +00:00
|
|
|
{
|
2008-04-03 20:55:09 +00:00
|
|
|
enum {
|
|
|
|
BN_BIND,
|
|
|
|
BN_BINDFILE,
|
2011-01-16 19:53:36 +00:00
|
|
|
BN_FORMAT,
|
2010-12-17 19:56:51 +00:00
|
|
|
BN_UNBIND
|
2008-04-03 20:55:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LexerKeyword bindTags[] = {
|
2008-04-03 21:36:57 +00:00
|
|
|
{ "\\bind", BN_BIND },
|
2008-04-03 20:55:09 +00:00
|
|
|
{ "\\bind_file", BN_BINDFILE },
|
2008-04-03 21:36:57 +00:00
|
|
|
{ "\\unbind", BN_UNBIND },
|
2011-01-16 19:53:36 +00:00
|
|
|
{ "format", BN_FORMAT }
|
2008-04-03 20:55:09 +00:00
|
|
|
};
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
Lexer lexrc(bindTags);
|
2003-07-26 23:04:39 +00:00
|
|
|
if (lyxerr.debugging(Debug::PARSER))
|
|
|
|
lexrc.printTable(lyxerr);
|
|
|
|
|
2009-07-16 23:12:45 +00:00
|
|
|
lexrc.setFile(bind_file);
|
2003-07-29 09:43:15 +00:00
|
|
|
if (!lexrc.isOK()) {
|
2010-04-21 01:19:09 +00:00
|
|
|
LYXERR0("KeyMap::read: cannot open bind file:" << bind_file.absFileName());
|
2011-01-16 19:53:36 +00:00
|
|
|
return FileError;
|
2003-07-29 09:43:15 +00:00
|
|
|
}
|
2003-07-26 23:04:39 +00:00
|
|
|
|
2010-04-21 01:19:09 +00:00
|
|
|
LYXERR(Debug::KBMAP, "Reading bind file:" << bind_file.absFileName());
|
2003-07-26 23:04:39 +00:00
|
|
|
|
2011-01-16 19:53:36 +00:00
|
|
|
// format of pre-2.0 bind files, before this tag was introduced.
|
2020-10-10 14:00:16 +00:00
|
|
|
int format = 0;
|
2003-07-26 23:04:39 +00:00
|
|
|
bool error = false;
|
2004-04-29 15:25:56 +00:00
|
|
|
while (lexrc.isOK()) {
|
2003-07-26 23:04:39 +00:00
|
|
|
switch (lexrc.lex()) {
|
2008-04-03 21:36:57 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
case Lexer::LEX_UNDEF:
|
2003-07-26 23:04:39 +00:00
|
|
|
lexrc.printError("Unknown tag `$$Token'");
|
2003-07-29 09:43:15 +00:00
|
|
|
error = true;
|
2003-07-26 23:04:39 +00:00
|
|
|
continue;
|
2008-04-03 21:36:57 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
case Lexer::LEX_FEOF:
|
2003-07-26 23:04:39 +00:00
|
|
|
continue;
|
2003-09-09 18:27:24 +00:00
|
|
|
|
2011-01-16 19:53:36 +00:00
|
|
|
case BN_FORMAT:
|
|
|
|
if (lexrc.next())
|
|
|
|
format = lexrc.getInteger();
|
|
|
|
break;
|
|
|
|
|
2008-04-03 21:36:57 +00:00
|
|
|
case BN_BIND: {
|
|
|
|
if (!lexrc.next()) {
|
2003-07-26 23:04:39 +00:00
|
|
|
lexrc.printError("BN_BIND: Missing key sequence");
|
2003-07-29 09:43:15 +00:00
|
|
|
error = true;
|
2003-07-26 23:04:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-03 21:36:57 +00:00
|
|
|
string seq = lexrc.getString();
|
2003-09-09 18:27:24 +00:00
|
|
|
|
2008-04-03 21:36:57 +00:00
|
|
|
if (!lexrc.next(true)) {
|
2003-07-26 23:04:39 +00:00
|
|
|
lexrc.printError("BN_BIND: missing command");
|
2003-07-29 09:43:15 +00:00
|
|
|
error = true;
|
2003-07-26 23:04:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-03 21:36:57 +00:00
|
|
|
string cmd = lexrc.getString();
|
2003-09-09 18:27:24 +00:00
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
FuncRequest func = lyxaction.lookupFunc(cmd);
|
2022-07-27 09:52:50 +00:00
|
|
|
if (func == FuncRequest::unknown) {
|
2008-04-02 22:44:33 +00:00
|
|
|
lexrc.printError("BN_BIND: Unknown LyX function `$$Token'");
|
2003-07-29 09:43:15 +00:00
|
|
|
error = true;
|
2003-07-26 23:04:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-09-09 18:27:24 +00:00
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
bind(seq, func);
|
2003-07-26 23:04:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-10-20 20:35:33 +00:00
|
|
|
|
2008-04-03 21:36:57 +00:00
|
|
|
case BN_UNBIND: {
|
|
|
|
if (!lexrc.next()) {
|
2007-10-20 20:35:33 +00:00
|
|
|
lexrc.printError("BN_UNBIND: Missing key sequence");
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-03 21:36:57 +00:00
|
|
|
string seq = lexrc.getString();
|
2007-10-20 20:35:33 +00:00
|
|
|
|
2008-04-03 21:36:57 +00:00
|
|
|
if (!lexrc.next(true)) {
|
2007-10-20 20:35:33 +00:00
|
|
|
lexrc.printError("BN_UNBIND: missing command");
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-03 21:36:57 +00:00
|
|
|
string cmd = lexrc.getString();
|
2022-07-27 09:52:50 +00:00
|
|
|
FuncRequest func;
|
|
|
|
if (cmd == "*")
|
|
|
|
func = FuncRequest::unknown;
|
|
|
|
else {
|
|
|
|
func = lyxaction.lookupFunc(cmd);
|
|
|
|
if (func == FuncRequest::unknown) {
|
|
|
|
lexrc.printError("BN_UNBIND: Unknown LyX function `$$Token'");
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-20 20:35:33 +00:00
|
|
|
}
|
2011-01-16 19:53:36 +00:00
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
if (unbind_map)
|
|
|
|
unbind_map->bind(seq, func);
|
|
|
|
else
|
|
|
|
unbind(seq, func);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-03 21:36:57 +00:00
|
|
|
|
2003-07-26 23:04:39 +00:00
|
|
|
case BN_BINDFILE:
|
2008-04-03 21:36:57 +00:00
|
|
|
if (!lexrc.next()) {
|
2003-07-26 23:04:39 +00:00
|
|
|
lexrc.printError("BN_BINDFILE: Missing file name");
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-11-25 10:30:20 +00:00
|
|
|
string tmp = lexrc.getString();
|
|
|
|
if (prefixIs(tmp, "../")) {
|
|
|
|
tmp = split(tmp, '/');
|
|
|
|
// look in top dir
|
|
|
|
error |= !read(tmp, unbind_map, Default, false);
|
|
|
|
} else
|
|
|
|
// i18n search
|
|
|
|
error |= !read(tmp, unbind_map);
|
2003-07-26 23:04:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-01-16 19:53:36 +00:00
|
|
|
|
|
|
|
// This is triggered the first time through the loop unless
|
|
|
|
// we hit a format tag.
|
2011-01-18 15:07:26 +00:00
|
|
|
if (format != LFUN_FORMAT)
|
2011-01-16 19:53:36 +00:00
|
|
|
return FormatMismatch;
|
2003-07-26 23:04:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-16 19:53:36 +00:00
|
|
|
if (error) {
|
2010-04-21 01:19:09 +00:00
|
|
|
LYXERR0("KeyMap::read: error while reading bind file:" << bind_file.absFileName());
|
2011-01-16 19:53:36 +00:00
|
|
|
return ReadError;
|
|
|
|
}
|
|
|
|
return ReadOK;
|
2003-07-26 23:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
void KeyMap::write(string const & bind_file, bool append, bool unbind) const
|
|
|
|
{
|
2011-01-16 19:53:36 +00:00
|
|
|
ofstream os(bind_file.c_str(),
|
2008-03-21 22:31:57 +00:00
|
|
|
append ? (ios::app | ios::out) : ios::out);
|
2007-10-20 20:35:33 +00:00
|
|
|
|
|
|
|
if (!append)
|
|
|
|
os << "## This file is automatically generated by lyx\n"
|
2011-01-16 19:53:36 +00:00
|
|
|
<< "## All modifications will be lost\n\n"
|
2011-01-18 15:07:26 +00:00
|
|
|
<< "Format " << LFUN_FORMAT << "\n\n";
|
2011-01-16 19:53:36 +00:00
|
|
|
|
2007-10-20 20:35:33 +00:00
|
|
|
string tag = unbind ? "\\unbind" : "\\bind";
|
2022-07-27 09:52:50 +00:00
|
|
|
for (auto const & bnd : listBindings(false)) {
|
|
|
|
FuncCode const action = bnd.request.action();
|
|
|
|
string const arg = to_utf8(bnd.request.argument());
|
|
|
|
|
|
|
|
string cmd;
|
|
|
|
if (unbind && bnd.request == FuncRequest::unknown)
|
|
|
|
cmd = "*";
|
|
|
|
else
|
|
|
|
cmd = lyxaction.getActionName(action) + (arg.empty() ? string() : " " + arg);
|
2007-10-20 20:35:33 +00:00
|
|
|
os << tag << " \""
|
2022-07-27 09:52:50 +00:00
|
|
|
<< to_utf8(bnd.sequence.print(KeySequence::BindFile))
|
2010-08-09 20:37:31 +00:00
|
|
|
<< "\" " << Lexer::quoteString(cmd)
|
|
|
|
<< "\n";
|
2007-10-20 20:35:33 +00:00
|
|
|
}
|
|
|
|
os << "\n";
|
|
|
|
os.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
FuncRequest const & KeyMap::lookup(KeySymbol const &key,
|
2007-10-02 21:51:54 +00:00
|
|
|
KeyModifier mod, KeySequence * seq) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-02-04 09:38:32 +00:00
|
|
|
if (table.empty()) {
|
2009-04-02 15:53:23 +00:00
|
|
|
seq->reset();
|
2008-09-24 13:32:09 +00:00
|
|
|
return FuncRequest::unknown;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 13:47:11 +00:00
|
|
|
Table::const_iterator end = table.end();
|
|
|
|
for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
|
2007-10-02 21:51:54 +00:00
|
|
|
KeyModifier mask = cit->mod.second;
|
|
|
|
KeyModifier check = static_cast<KeyModifier>(mod & ~mask);
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2007-09-17 18:41:03 +00:00
|
|
|
if (cit->code == key && cit->mod.first == check) {
|
2001-11-30 13:25:38 +00:00
|
|
|
// match found
|
2012-10-21 19:04:05 +00:00
|
|
|
if (cit->prefixes) {
|
1999-12-16 06:43:25 +00:00
|
|
|
// this is a prefix key - set new map
|
2009-04-02 18:57:19 +00:00
|
|
|
seq->curmap = cit->prefixes.get();
|
2022-12-09 13:43:19 +00:00
|
|
|
return FuncRequest::prefix;
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
// final key - reset map
|
2009-04-02 15:53:23 +00:00
|
|
|
seq->reset();
|
2003-09-21 23:00:47 +00:00
|
|
|
return cit->func;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-04 09:38:32 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// error - key not found:
|
2009-04-02 15:53:23 +00:00
|
|
|
seq->reset();
|
2003-09-21 23:00:47 +00:00
|
|
|
|
2008-09-24 13:32:09 +00:00
|
|
|
return FuncRequest::unknown;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-27 08:05:58 +00:00
|
|
|
docstring const KeyMap::print(bool forgui) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2006-12-13 14:13:01 +00:00
|
|
|
docstring buf;
|
2003-06-18 13:47:11 +00:00
|
|
|
Table::const_iterator end = table.end();
|
|
|
|
for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
|
2007-09-17 18:41:03 +00:00
|
|
|
buf += cit->code.print(cit->mod.first, forgui);
|
2000-02-04 09:38:32 +00:00
|
|
|
buf += ' ';
|
|
|
|
}
|
2001-11-30 13:25:38 +00:00
|
|
|
return buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-31 11:19:16 +00:00
|
|
|
docstring KeyMap::printBindings(FuncRequest const & func,
|
2019-03-09 11:00:40 +00:00
|
|
|
KeySequence::outputFormat format,
|
|
|
|
bool const untranslated) const
|
1999-12-16 06:43:25 +00:00
|
|
|
{
|
2007-11-02 21:27:37 +00:00
|
|
|
Bindings bindings = findBindings(func);
|
2007-10-11 16:03:21 +00:00
|
|
|
if (bindings.empty())
|
|
|
|
return docstring();
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2007-10-11 16:03:21 +00:00
|
|
|
odocstringstream res;
|
|
|
|
Bindings::const_iterator cit = bindings.begin();
|
|
|
|
Bindings::const_iterator cit_end = bindings.end();
|
2008-10-31 11:19:16 +00:00
|
|
|
// print the first item
|
2019-03-09 11:00:40 +00:00
|
|
|
res << cit->print(format, untranslated);
|
2007-10-11 16:03:21 +00:00
|
|
|
// more than one shortcuts?
|
|
|
|
for (++cit; cit != cit_end; ++cit)
|
2019-03-09 11:00:40 +00:00
|
|
|
res << ", " << cit->print(format, untranslated);
|
2004-07-02 10:03:22 +00:00
|
|
|
return res.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-02 21:27:37 +00:00
|
|
|
KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func) const
|
2004-07-02 10:03:22 +00:00
|
|
|
{
|
2020-02-29 05:21:00 +00:00
|
|
|
return findBindings(func, KeySequence(nullptr, nullptr));
|
2004-07-02 10:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-02 21:27:37 +00:00
|
|
|
KeyMap::Bindings KeyMap::findBindings(FuncRequest const & func,
|
2007-04-27 08:19:12 +00:00
|
|
|
KeySequence const & prefix) const
|
2004-07-02 10:03:22 +00:00
|
|
|
{
|
|
|
|
Bindings res;
|
2007-11-02 21:27:37 +00:00
|
|
|
if (table.empty())
|
|
|
|
return res;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
Table::const_iterator end = table.end();
|
2007-11-02 21:27:37 +00:00
|
|
|
for (Table::const_iterator cit = table.begin(); cit != end; ++cit) {
|
2012-10-21 19:04:05 +00:00
|
|
|
if (cit->prefixes) {
|
2007-04-27 08:19:12 +00:00
|
|
|
KeySequence seq = prefix;
|
2004-07-02 10:03:22 +00:00
|
|
|
seq.addkey(cit->code, cit->mod.first);
|
2009-04-02 18:57:19 +00:00
|
|
|
Bindings res2 = cit->prefixes->findBindings(func, seq);
|
2004-07-02 10:03:22 +00:00
|
|
|
res.insert(res.end(), res2.begin(), res2.end());
|
2003-09-21 23:00:47 +00:00
|
|
|
} else if (cit->func == func) {
|
2007-04-27 08:19:12 +00:00
|
|
|
KeySequence seq = prefix;
|
2004-07-02 10:03:22 +00:00
|
|
|
seq.addkey(cit->code, cit->mod.first);
|
|
|
|
res.push_back(seq);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-21 23:00:47 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
2004-07-07 09:32:19 +00:00
|
|
|
|
|
|
|
|
2008-09-23 13:53:38 +00:00
|
|
|
KeyMap::BindingList KeyMap::listBindings(bool unbound, KeyMap::ItemType tag) const
|
2007-10-18 15:48:51 +00:00
|
|
|
{
|
|
|
|
BindingList list;
|
2020-02-29 05:21:00 +00:00
|
|
|
listBindings(list, KeySequence(nullptr, nullptr), tag);
|
2007-10-18 15:48:51 +00:00
|
|
|
if (unbound) {
|
2020-11-19 12:03:26 +00:00
|
|
|
for (auto const & name_code : lyxaction) {
|
|
|
|
FuncCode action = name_code.second;
|
2007-10-18 15:48:51 +00:00
|
|
|
bool has_action = false;
|
2010-01-05 21:01:33 +00:00
|
|
|
BindingList::const_iterator bit = list.begin();
|
|
|
|
BindingList::const_iterator const ben = list.end();
|
|
|
|
for (; bit != ben; ++bit)
|
2010-04-09 19:00:42 +00:00
|
|
|
if (bit->request.action() == action) {
|
2007-10-18 15:48:51 +00:00
|
|
|
has_action = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!has_action)
|
2020-02-29 05:21:00 +00:00
|
|
|
list.push_back(Binding(FuncRequest(action), KeySequence(nullptr, nullptr), tag));
|
2017-07-03 17:53:14 +00:00
|
|
|
}
|
2007-10-18 15:48:51 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KeyMap::listBindings(BindingList & list,
|
2008-09-23 13:53:38 +00:00
|
|
|
KeySequence const & prefix, KeyMap::ItemType tag) const
|
2007-10-18 15:48:51 +00:00
|
|
|
{
|
|
|
|
Table::const_iterator it = table.begin();
|
|
|
|
Table::const_iterator it_end = table.end();
|
|
|
|
for (; it != it_end; ++it) {
|
|
|
|
// a LFUN_COMMAND_PREFIX
|
2012-10-21 19:04:05 +00:00
|
|
|
if (it->prefixes) {
|
2007-10-18 15:48:51 +00:00
|
|
|
KeySequence seq = prefix;
|
|
|
|
seq.addkey(it->code, it->mod.first);
|
2009-04-02 18:57:19 +00:00
|
|
|
it->prefixes->listBindings(list, seq, tag);
|
2007-10-18 15:48:51 +00:00
|
|
|
} else {
|
|
|
|
KeySequence seq = prefix;
|
|
|
|
seq.addkey(it->code, it->mod.first);
|
2007-11-02 20:26:28 +00:00
|
|
|
list.push_back(Binding(it->func, seq, tag));
|
2007-10-18 15:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|