1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
2001-11-30 13:25:38 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2000-02-04 09:38:32 +00:00
|
|
|
|
2000-01-21 17:41:57 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "kbmap.h"
|
2002-01-12 20:00:47 +00:00
|
|
|
#include "commandtags.h"
|
2000-02-04 09:38:32 +00:00
|
|
|
#include "kbsequence.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// The only modifiers that we handle. We want to throw away things
|
2001-11-30 13:25:38 +00:00
|
|
|
// like NumLock.
|
2000-02-04 09:38:32 +00:00
|
|
|
enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
string const kb_keymap::printKeysym(unsigned int key, unsigned int 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
|
|
|
mod &= ModsMask;
|
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
char const * const s = XKeysymToString(key);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
if (mod & ShiftMask) buf += "S-";
|
|
|
|
if (mod & ControlMask) buf += "C-";
|
|
|
|
if (mod & Mod1Mask) buf += "M-";
|
|
|
|
if (s) buf += s;
|
2001-11-30 13:25:38 +00:00
|
|
|
return buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
char kb_keymap::getiso(unsigned int c)
|
|
|
|
{
|
|
|
|
switch (c & 0x0000FF00) {
|
|
|
|
// latin 1 byte 3 = 0
|
|
|
|
case 0x00000000: break;
|
|
|
|
// latin 2 byte 3 = 1
|
|
|
|
case 0x00000100:
|
|
|
|
// latin 3 byte 3 = 2
|
|
|
|
case 0x00000200:
|
|
|
|
// latin 4 byte 3 = 3
|
|
|
|
case 0x00000300:
|
|
|
|
// latin 8 byte 3 = 18 (0x12)
|
|
|
|
case 0x00001200:
|
|
|
|
// latin 9 byte 3 = 19 (0x13)
|
|
|
|
case 0x00001300:
|
|
|
|
c &= 0x000000FF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
1999-09-27 18:44:28 +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
|
|
|
{
|
2001-11-30 13:25:38 +00:00
|
|
|
return printKeysym(key.code, key.mod & 0xffff);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-12 20:00:47 +00:00
|
|
|
string::size_type kb_keymap::bind(string const & seq, int action)
|
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 `"
|
|
|
|
<< action << "'" << endl;
|
|
|
|
}
|
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) {
|
1999-09-27 18:44:28 +00:00
|
|
|
defkey(&k, action);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-12 20:00:47 +00:00
|
|
|
int kb_keymap::lookup(unsigned int key,
|
2000-02-04 09:38:32 +00:00
|
|
|
unsigned int mod, kb_sequence * seq) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
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();
|
|
|
|
return LFUN_UNKNOWN_ACTION;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
//suppress modifier bits we do not handle
|
|
|
|
mod &= ModsMask;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
for (Table::const_iterator cit = table.begin();
|
|
|
|
cit != table.end(); ++cit) {
|
2001-07-12 11:11:10 +00:00
|
|
|
unsigned int const msk1 = cit->mod & 0xffff;
|
|
|
|
unsigned int const msk0 = (cit->mod >> 16) & 0xffff;
|
|
|
|
if (cit->code == key && (mod & ~msk0) == msk1) {
|
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();
|
2001-11-30 13:25:38 +00:00
|
|
|
return LFUN_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();
|
2001-07-12 11:11:10 +00:00
|
|
|
return cit->action;
|
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();
|
|
|
|
return LFUN_UNKNOWN_ACTION;
|
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;
|
2000-02-04 09:38:32 +00:00
|
|
|
for (Table::const_iterator cit = table.begin();
|
|
|
|
cit != table.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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-12 20:00:47 +00:00
|
|
|
void kb_keymap::defkey(kb_sequence * seq, int action, unsigned int r)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-30 13:25:38 +00:00
|
|
|
unsigned int const code = seq->sequence[r];
|
|
|
|
if (code == NoSymbol) return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
unsigned int const modmsk = seq->modifiers[r];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-11-30 13:25:38 +00:00
|
|
|
// check if key is already there
|
|
|
|
for (Table::iterator it = table.begin(); it != table.end(); ++it) {
|
2001-07-12 11:11:10 +00:00
|
|
|
if (code == it->code && modmsk == it->mod) {
|
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
|
|
|
}
|
2001-07-12 11:11:10 +00:00
|
|
|
it->action = action;
|
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 {
|
2001-11-30 13:25:38 +00:00
|
|
|
it->table->defkey(seq, action, r + 1);
|
|
|
|
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;
|
|
|
|
newone->mod = modmsk;
|
2001-11-30 13:25:38 +00:00
|
|
|
if (r + 1 == seq->length()) {
|
2001-07-12 11:11:10 +00:00
|
|
|
newone->action = action;
|
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);
|
2001-11-30 13:25:38 +00:00
|
|
|
newone->table->defkey(seq, action, r + 1);
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-12 20:00:47 +00:00
|
|
|
string const kb_keymap::findbinding(int act, string const & prefix) const
|
1999-12-16 06:43:25 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string 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()) {
|
|
|
|
res += cit->table->findbinding(act,
|
|
|
|
prefix
|
2001-11-30 13:25:38 +00:00
|
|
|
+ printKey((*cit))
|
2001-07-12 11:11:10 +00:00
|
|
|
+ " ");
|
|
|
|
} else if (cit->action == act) {
|
2000-10-11 21:06:43 +00:00
|
|
|
res += "[";
|
2001-11-30 13:25:38 +00:00
|
|
|
res += prefix + printKey((*cit));
|
2000-10-11 21:06:43 +00:00
|
|
|
res += "] ";
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|