2002-06-12 03:16:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file kbmap.C
|
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
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "kbmap.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
|
|
|
|
|
|
#include "debug.h"
|
2000-02-04 09:38:32 +00:00
|
|
|
|
#include "kbsequence.h"
|
2003-07-26 23:04:39 +00:00
|
|
|
|
#include "LyXAction.h"
|
|
|
|
|
#include "lyxlex.h"
|
2003-09-06 20:08:10 +00:00
|
|
|
|
|
|
|
|
|
#include "frontends/LyXKeySym.h"
|
|
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
2004-07-24 10:55:30 +00:00
|
|
|
|
|
|
|
|
|
#include <sstream>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-07-26 23:04:39 +00:00
|
|
|
|
using lyx::support::i18nLibFileSearch;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::endl;
|
|
|
|
|
using std::string;
|
|
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
|
|
2004-03-26 15:12:35 +00:00
|
|
|
|
string const kb_keymap::printKeySym(LyXKeySym const & key,
|
2002-06-18 15:44:30 +00:00
|
|
|
|
key_modifier::state 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
|
|
|
|
|
2003-06-18 13:47:11 +00:00
|
|
|
|
if (mod & key_modifier::shift)
|
|
|
|
|
buf += "S-";
|
|
|
|
|
if (mod & key_modifier::ctrl)
|
|
|
|
|
buf += "C-";
|
|
|
|
|
if (mod & key_modifier::alt)
|
|
|
|
|
buf += "M-";
|
|
|
|
|
|
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
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
|
string const kb_keymap::printKey(kb_key const & key) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2004-03-26 15:12:35 +00:00
|
|
|
|
return key.code->print(key.mod.first);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
|
string::size_type kb_keymap::bind(string const & seq, FuncRequest const & func)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-12-17 06:09:35 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::KBMAP)) {
|
|
|
|
|
lyxerr << "BIND: Sequence `"
|
|
|
|
|
<< seq << "' Action `"
|
2003-09-21 23:00:47 +00:00
|
|
|
|
<< func.action << '\'' << endl;
|
2000-12-17 06:09:35 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
|
kb_sequence k(0, 0);
|
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) {
|
2003-09-21 23:00:47 +00:00
|
|
|
|
defkey(&k, func);
|
2001-11-30 13:25:38 +00:00
|
|
|
|
} else {
|
1999-10-07 18:44:17 +00:00
|
|
|
|
lyxerr[Debug::KBMAP] << "Parse error at position " << res
|
|
|
|
|
<< " in key sequence '" << seq << "'."
|
|
|
|
|
<< endl;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-26 23:04:39 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
enum BindTags {
|
|
|
|
|
BN_BIND,
|
|
|
|
|
BN_BINDFILE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keyword_item bindTags[] = {
|
|
|
|
|
{ "\\bind", BN_BIND },
|
|
|
|
|
{ "\\bind_file", BN_BINDFILE }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-09 18:27:24 +00:00
|
|
|
|
bool kb_keymap::read(string const & bind_file)
|
2003-07-26 23:04:39 +00:00
|
|
|
|
{
|
|
|
|
|
const int bindCount = sizeof(bindTags) / sizeof(keyword_item);
|
|
|
|
|
|
|
|
|
|
LyXLex lexrc(bindTags, bindCount);
|
|
|
|
|
if (lyxerr.debugging(Debug::PARSER))
|
|
|
|
|
lexrc.printTable(lyxerr);
|
|
|
|
|
|
|
|
|
|
string const tmp = i18nLibFileSearch("bind", bind_file, "bind");
|
|
|
|
|
lexrc.setFile(tmp);
|
2003-07-29 09:43:15 +00:00
|
|
|
|
if (!lexrc.isOK()) {
|
2003-09-09 18:27:24 +00:00
|
|
|
|
lyxerr << "kb_keymap::read: cannot open bind file:"
|
2003-07-29 09:43:15 +00:00
|
|
|
|
<< tmp << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2003-07-26 23:04:39 +00:00
|
|
|
|
|
2003-07-29 09:43:15 +00:00
|
|
|
|
lyxerr[Debug::KBMAP] << "Reading bind file:" << tmp << endl;
|
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()) {
|
|
|
|
|
case LyXLex::LEX_UNDEF:
|
|
|
|
|
lexrc.printError("Unknown tag `$$Token'");
|
2003-07-29 09:43:15 +00:00
|
|
|
|
error = true;
|
2003-07-26 23:04:39 +00:00
|
|
|
|
continue;
|
|
|
|
|
case LyXLex::LEX_FEOF:
|
|
|
|
|
continue;
|
|
|
|
|
case BN_BIND:
|
|
|
|
|
{
|
|
|
|
|
string seq, cmd;
|
2003-09-09 18:27:24 +00:00
|
|
|
|
|
2003-07-26 23:04:39 +00:00
|
|
|
|
if (lexrc.next()) {
|
|
|
|
|
seq = lexrc.getString();
|
|
|
|
|
} else {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2003-09-09 18:27:24 +00:00
|
|
|
|
|
2003-07-26 23:04:39 +00:00
|
|
|
|
if (lexrc.next(true)) {
|
|
|
|
|
cmd = lexrc.getString();
|
|
|
|
|
} else {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2003-09-09 18:27:24 +00:00
|
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
|
FuncRequest func = lyxaction.lookupFunc(cmd);
|
|
|
|
|
if (func. action == LFUN_UNKNOWN_ACTION) {
|
2003-07-26 23:04:39 +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;
|
|
|
|
|
}
|
|
|
|
|
case BN_BINDFILE:
|
|
|
|
|
if (lexrc.next()) {
|
|
|
|
|
string const tmp(lexrc.getString());
|
2004-04-29 15:25:56 +00:00
|
|
|
|
error |= !read(tmp);
|
2003-07-26 23:04:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
lexrc.printError("BN_BINDFILE: Missing file name");
|
|
|
|
|
error = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-29 09:43:15 +00:00
|
|
|
|
if (error)
|
2003-09-09 18:27:24 +00:00
|
|
|
|
lyxerr << "kb_keymap::read: error while reading bind file:"
|
2003-07-29 09:43:15 +00:00
|
|
|
|
<< tmp << endl;
|
|
|
|
|
return !error;
|
2003-07-26 23:04:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
|
FuncRequest const &
|
|
|
|
|
kb_keymap::lookup(LyXKeySymPtr key,
|
|
|
|
|
key_modifier::state mod, kb_sequence * seq) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-09-21 23:00:47 +00:00
|
|
|
|
static FuncRequest const unknown(LFUN_UNKNOWN_ACTION);
|
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
|
if (table.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
seq->curmap = seq->stdmap;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
seq->mark_deleted();
|
2003-09-21 23:00:47 +00:00
|
|
|
|
return 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) {
|
2002-05-26 17:33:14 +00:00
|
|
|
|
key_modifier::state mask(cit->mod.second);
|
|
|
|
|
key_modifier::state check =
|
|
|
|
|
static_cast<key_modifier::state>(mod & ~mask);
|
2002-11-27 10:30:28 +00:00
|
|
|
|
|
2002-08-06 00:54:20 +00:00
|
|
|
|
if (*(cit->code) == *key && cit->mod.first == check) {
|
2001-11-30 13:25:38 +00:00
|
|
|
|
// match found
|
2001-07-12 11:11:10 +00:00
|
|
|
|
if (cit->table.get()) {
|
1999-12-16 06:43:25 +00:00
|
|
|
|
// this is a prefix key - set new map
|
2001-07-12 11:11:10 +00:00
|
|
|
|
seq->curmap = cit->table.get();
|
2003-09-21 23:00:47 +00:00
|
|
|
|
static FuncRequest prefix(LFUN_PREFIX);
|
|
|
|
|
return prefix;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
|
// final key - reset map
|
1999-09-27 18:44:28 +00:00
|
|
|
|
seq->curmap = seq->stdmap;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
seq->mark_deleted();
|
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:
|
|
|
|
|
seq->curmap = seq->stdmap;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
seq->mark_deleted();
|
2003-09-21 23:00:47 +00:00
|
|
|
|
|
|
|
|
|
return unknown;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
|
string const kb_keymap::print() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-11-30 13:25:38 +00:00
|
|
|
|
string 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) {
|
2001-11-30 13:25:38 +00:00
|
|
|
|
buf += printKey((*cit));
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
|
void kb_keymap::defkey(kb_sequence * seq,
|
|
|
|
|
FuncRequest const & func, unsigned int r)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
|
LyXKeySymPtr code = seq->sequence[r];
|
2002-08-06 00:54:20 +00:00
|
|
|
|
if (!code->isOK())
|
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-05-26 17:33:14 +00:00
|
|
|
|
key_modifier::state const mod1 = seq->modifiers[r].first;
|
|
|
|
|
key_modifier::state const mod2 = seq->modifiers[r].second;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
|
// check if key is already there
|
2003-06-18 13:47:11 +00:00
|
|
|
|
Table::iterator end = table.end();
|
|
|
|
|
for (Table::iterator it = table.begin(); it != end; ++it) {
|
2002-06-18 15:44:30 +00:00
|
|
|
|
if (*(code) == *(it->code)
|
|
|
|
|
&& mod1 == it->mod.first
|
|
|
|
|
&& mod2 == it->mod.second) {
|
2000-02-04 09:38:32 +00:00
|
|
|
|
// overwrite binding
|
2001-11-30 13:25:38 +00:00
|
|
|
|
if (r + 1 == seq->length()) {
|
2000-08-14 15:31:16 +00:00
|
|
|
|
lyxerr[Debug::KBMAP]
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< "Warning: New binding for '"
|
2001-11-30 13:25:38 +00:00
|
|
|
|
<< seq->print()
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< "' is overriding old binding..."
|
|
|
|
|
<< endl;
|
2002-05-21 23:50:36 +00:00
|
|
|
|
if (it->table.get()) {
|
|
|
|
|
it->table.reset();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2003-09-21 23:00:47 +00:00
|
|
|
|
it->func = func;
|
2004-11-08 10:54:29 +00:00
|
|
|
|
it->func.origin = FuncRequest::KEYBOARD;
|
2001-11-30 13:25:38 +00:00
|
|
|
|
return;
|
2001-07-12 11:11:10 +00:00
|
|
|
|
} else if (!it->table.get()) {
|
2001-11-30 13:25:38 +00:00
|
|
|
|
lyxerr << "Error: New binding for '" << seq->print()
|
1999-10-07 18:44:17 +00:00
|
|
|
|
<< "' is overriding old binding..."
|
2001-11-30 13:25:38 +00:00
|
|
|
|
<< endl;
|
|
|
|
|
return;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
} else {
|
2003-09-21 23:00:47 +00:00
|
|
|
|
it->table->defkey(seq, func, r + 1);
|
2001-11-30 13:25:38 +00:00
|
|
|
|
return;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
|
Table::iterator newone = table.insert(table.end(), kb_key());
|
2001-07-12 11:11:10 +00:00
|
|
|
|
newone->code = code;
|
2002-05-26 17:33:14 +00:00
|
|
|
|
newone->mod = seq->modifiers[r];
|
2001-11-30 13:25:38 +00:00
|
|
|
|
if (r + 1 == seq->length()) {
|
2003-09-21 23:00:47 +00:00
|
|
|
|
newone->func = func;
|
2002-05-21 23:50:36 +00:00
|
|
|
|
newone->table.reset();
|
2001-11-30 13:25:38 +00:00
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
} else {
|
2001-07-12 11:11:10 +00:00
|
|
|
|
newone->table.reset(new kb_keymap);
|
2003-09-21 23:00:47 +00:00
|
|
|
|
newone->table->defkey(seq, func, r + 1);
|
2001-11-30 13:25:38 +00:00
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-07-02 10:03:22 +00:00
|
|
|
|
string const kb_keymap::printbindings(FuncRequest const & func) const
|
1999-12-16 06:43:25 +00:00
|
|
|
|
{
|
2004-07-02 10:03:22 +00:00
|
|
|
|
std::ostringstream res;
|
|
|
|
|
Bindings bindings = findbindings(func);
|
|
|
|
|
for (Bindings::const_iterator cit = bindings.begin();
|
|
|
|
|
cit != bindings.end() ; ++cit)
|
|
|
|
|
res << '[' << cit->print() << ']';
|
|
|
|
|
return res.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
kb_keymap::Bindings
|
2004-07-02 10:03:22 +00:00
|
|
|
|
kb_keymap::findbindings(FuncRequest const & func) const
|
|
|
|
|
{
|
|
|
|
|
return findbindings(func, kb_sequence(0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
kb_keymap::Bindings
|
2004-07-02 10:03:22 +00:00
|
|
|
|
kb_keymap::findbindings(FuncRequest const & func,
|
|
|
|
|
kb_sequence const & prefix) const
|
|
|
|
|
{
|
|
|
|
|
Bindings res;
|
2000-02-04 09:38:32 +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();
|
2000-10-11 21:06:43 +00:00
|
|
|
|
for (Table::const_iterator cit = table.begin();
|
2000-08-03 21:17:52 +00:00
|
|
|
|
cit != end; ++cit) {
|
2001-07-12 11:11:10 +00:00
|
|
|
|
if (cit->table.get()) {
|
2004-07-02 10:03:22 +00:00
|
|
|
|
kb_sequence seq = prefix;
|
|
|
|
|
seq.addkey(cit->code, cit->mod.first);
|
2004-07-24 10:55:30 +00:00
|
|
|
|
Bindings res2 =
|
2004-07-02 10:03:22 +00:00
|
|
|
|
cit->table->findbindings(func, seq);
|
|
|
|
|
res.insert(res.end(), res2.begin(), res2.end());
|
2003-09-21 23:00:47 +00:00
|
|
|
|
} else if (cit->func == func) {
|
2004-07-02 10:03:22 +00:00
|
|
|
|
kb_sequence seq = prefix;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<LyXKeySym const *, key_modifier::state>
|
|
|
|
|
kb_keymap::find1keybinding(FuncRequest const & func) const
|
|
|
|
|
{
|
|
|
|
|
Table::const_iterator end = table.end();
|
|
|
|
|
for (Table::const_iterator cit = table.begin();
|
|
|
|
|
cit != end; ++cit) {
|
2004-07-24 10:55:30 +00:00
|
|
|
|
if (!cit->table.get() && cit->func == func)
|
2004-07-07 09:32:19 +00:00
|
|
|
|
return std::make_pair(cit->code.get(), cit->mod.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);
|
2004-07-24 10:55:30 +00:00
|
|
|
|
}
|