2007-10-20 23:27:03 +00:00
|
|
|
/**
|
|
|
|
* \file CmdDef.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Bernhard Roider
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "CmdDef.h"
|
|
|
|
|
|
|
|
#include "LyXAction.h"
|
|
|
|
#include "Lexer.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2007-10-20 23:27:03 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2007-12-02 22:10:26 +00:00
|
|
|
#include <string>
|
2007-11-28 22:12:03 +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;
|
2007-10-20 23:27:03 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
bool CmdDef::read(string const & def_file)
|
|
|
|
{
|
2008-04-03 20:55:09 +00:00
|
|
|
enum {
|
|
|
|
BN_DEFFILE,
|
|
|
|
BN_DEFINE
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerKeyword cmdDefTags[] = {
|
|
|
|
{ "\\def_file", BN_DEFFILE },
|
|
|
|
{ "\\define", BN_DEFINE }
|
|
|
|
};
|
|
|
|
|
2008-04-05 21:24:57 +00:00
|
|
|
Lexer lex(cmdDefTags);
|
2007-10-20 23:27:03 +00:00
|
|
|
FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.setContext("CmdDef::read");
|
|
|
|
lex.setFile(tmp);
|
|
|
|
if (!lex.isOK()) {
|
|
|
|
LYXERR0( "CmdDef::read: cannot open def file:" << tmp);
|
2007-10-20 23:27:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error = false;
|
2008-04-05 21:24:57 +00:00
|
|
|
while (lex.isOK()) {
|
|
|
|
switch (lex.lex()) {
|
2007-10-20 23:27:03 +00:00
|
|
|
case Lexer::LEX_UNDEF:
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("Unknown tag");
|
2007-10-20 23:27:03 +00:00
|
|
|
error = true;
|
|
|
|
continue;
|
|
|
|
case Lexer::LEX_FEOF:
|
|
|
|
continue;
|
|
|
|
case BN_DEFINE:
|
|
|
|
{
|
|
|
|
string name, def;
|
|
|
|
|
2008-04-05 21:24:57 +00:00
|
|
|
if (lex.next()) {
|
|
|
|
name = lex.getString();
|
2007-10-20 23:27:03 +00:00
|
|
|
} else {
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFINE: Missing command name");
|
2007-10-20 23:27:03 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-05 21:24:57 +00:00
|
|
|
if (lex.next(true)) {
|
|
|
|
def = lex.getString();
|
2007-10-20 23:27:03 +00:00
|
|
|
} else {
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFINE: missing command definition");
|
2007-10-20 23:27:03 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
newCmdDefResult e = newCmdDef(name, def);
|
|
|
|
switch (e) {
|
2008-03-04 14:04:59 +00:00
|
|
|
case CmdDefNameEmpty:
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFINE: Command name is empty");
|
2008-03-04 14:04:59 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
case CmdDefExists:
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFINE: Command `" + name + "' already defined");
|
2008-03-04 14:04:59 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
case CmdDefInvalid:
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
|
2008-03-04 14:04:59 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
case CmdDefOk:
|
|
|
|
break;
|
2007-10-20 23:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BN_DEFFILE:
|
2008-04-05 21:24:57 +00:00
|
|
|
if (lex.next()) {
|
|
|
|
string const tmp = lex.getString();
|
2007-10-20 23:27:03 +00:00
|
|
|
error |= !read(tmp);
|
|
|
|
} else {
|
2008-04-05 21:24:57 +00:00
|
|
|
lex.printError("BN_DEFFILE: Missing file name");
|
2007-10-20 23:27:03 +00:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
2008-04-05 21:24:57 +00:00
|
|
|
LYXERR0("CmdDef::read: error while reading def file:" << tmp);
|
2007-10-20 23:27:03 +00:00
|
|
|
return !error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CmdDef::lock(string const & name, FuncRequest & func)
|
|
|
|
{
|
2007-12-02 22:10:26 +00:00
|
|
|
if (cmdDefMap.empty()) {
|
2007-10-20 23:27:03 +00:00
|
|
|
func = FuncRequest::unknown;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string const name2 = trim(name);
|
|
|
|
|
2007-12-02 22:10:26 +00:00
|
|
|
if (lockSet.find(name2) != lockSet.end()) {
|
|
|
|
func = FuncRequest::noaction;
|
2007-10-20 23:27:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-02 22:10:26 +00:00
|
|
|
CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
|
|
|
|
|
|
|
|
if (pos == cmdDefMap.end()) {
|
|
|
|
func = FuncRequest::unknown;
|
2007-10-20 23:27:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-02 22:10:26 +00:00
|
|
|
lockSet.insert(name2);
|
|
|
|
func = pos->second;
|
2007-10-20 23:27:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CmdDef::release(string const & name)
|
|
|
|
{
|
|
|
|
string const name2 = trim(name);
|
2007-12-02 22:10:26 +00:00
|
|
|
lockSet.erase(name2);
|
2007-10-20 23:27:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 20:55:09 +00:00
|
|
|
|
2007-10-20 23:27:03 +00:00
|
|
|
CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name,
|
2012-10-27 13:45:27 +00:00
|
|
|
string const & def)
|
2007-10-20 23:27:03 +00:00
|
|
|
{
|
|
|
|
string const name2 = trim(name);
|
|
|
|
|
|
|
|
if (name2.empty())
|
|
|
|
return CmdDefNameEmpty;
|
|
|
|
|
|
|
|
if (cmdDefMap.find(name) != cmdDefMap.end())
|
|
|
|
return CmdDefExists;
|
|
|
|
|
|
|
|
FuncRequest func = lyxaction.lookupFunc(def);
|
2010-04-09 19:00:42 +00:00
|
|
|
if (func.action() == LFUN_NOACTION
|
|
|
|
|| func.action() == LFUN_UNKNOWN_ACTION) {
|
2007-10-20 23:27:03 +00:00
|
|
|
return CmdDefInvalid;
|
|
|
|
}
|
|
|
|
|
2007-12-02 22:10:26 +00:00
|
|
|
cmdDefMap[name2] = func;
|
2007-10-20 23:27:03 +00:00
|
|
|
|
|
|
|
return CmdDefOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|