4 #pragma implementation "trans_mgr.h"
14 #include "insets/insetlatexaccent.h"
15 #include "BufferView.h"
18 #include "support/lstrings.h"
21 extern string DoAccent(const string&,tex_accent);
22 extern void InsertCorrectQuote();
23 extern string DoAccent(char,tex_accent);
24 extern BufferView* current_view;
28 TransFSMData::TransFSMData()
31 deadkey_info_.accent=deadkey2_info_.accent=TEX_NOACCENT;
37 const char TransState::TOKEN_SEP=4;
40 TransState::~TransState()
45 TransInitState::TransInitState()
51 string TransInitState::normalkey(char c,char *t)
63 //bool TransInitState::backspace()
69 string TransInitState::deadkey(char c,KmodInfo d)
73 currentState=deadkey_state_;
79 TransDeadkeyState::TransDeadkeyState()
85 string TransDeadkeyState::normalkey(char c,char *trans)
89 // Check if it is an exception
90 KmodException l=deadkey_info_.exception_list;
99 // Not an exception. Check if it allowed
100 if (current_view->currentBuffer()->params.allowAccents==true ||
101 countChar(deadkey_info_.allowed, c) > 0) {
102 res=DoAccent(c,deadkey_info_.accent);
111 currentState=init_state_;
116 //bool TransDeadkeyState::backspace()
118 // currentState=init_state_;
123 string TransDeadkeyState::deadkey(char c,KmodInfo d)
127 // Check if the same deadkey was typed twice
131 deadkey_info_.accent=TEX_NOACCENT;
132 currentState=init_state_;
136 // Check if it is a combination or an exception
138 l=deadkey_info_.exception_list;
141 if (l->combined==true && l->accent==d.accent) {
145 currentState=combined_state_;
151 deadkey_info_.accent=TEX_NOACCENT;
152 currentState=init_state_;
159 // Not a combination or an exception.
160 // Output deadkey1 and keep deadkey2
166 currentState=deadkey_state_;
171 TransCombinedState::TransCombinedState()
173 combined_state_=this;
177 string TransCombinedState::normalkey(char c,char *trans)
181 // Check if the key is allowed on the combination
182 if (countChar(comb_info_->data, c) > 0) {
184 temp=DoAccent(c,deadkey2_info_.accent);
185 res=DoAccent(temp,deadkey_info_.accent);
186 currentState=init_state_;
188 // Not allowed. Output deadkey1 and check deadkey2 + c
193 deadkey_info_=deadkey2_info_;
194 // Call deadkey state and leave it to setup the FSM
195 res+=deadkey_state_->normalkey(c,trans);
201 // bool TransCombinedState::backspace()
203 // // cancel the second deadkey
205 // deadkey2_info_.accent=TEX_NOACCENT;
206 // currentState=deadkey_state_;
212 string TransCombinedState::deadkey(char c,KmodInfo d)
214 // Third key in a row. Output the first one and
215 // reenter with shifted deadkeys
221 deadkey_info_=deadkey2_info_;
222 res+=deadkey_state_->deadkey(c,d);
228 TransFSM::TransFSM():
234 currentState=init_state_;
240 TransManager::TransManager()
241 : active_(0),t1_(new Trans),t2_(new Trans),chset_(new CharacterSet)
245 Trans* TransManager::default_=new Trans;
248 TransManager::~TransManager()
256 int TransManager::SetPrimary(string const & language)
258 if (t1_->GetName()==language)
261 return t1_->Load(language);
265 int TransManager::SetSecondary(string const & language)
267 if (t2_->GetName()==language)
270 return t2_->Load(language);
274 bool TransManager::setCharset(const char *set)
276 return chset_->loadFile(set);
280 void TransManager::EnablePrimary()
282 if (t1_->IsDefined())
285 lyxerr[Debug::KBMAP] << "Enabling primary keymap" << endl;
289 void TransManager::EnableSecondary()
291 if (t2_->IsDefined( ))
293 lyxerr[Debug::KBMAP] << "Enabling secondary keymap" << endl;
297 void TransManager::DisableKeymap()
300 lyxerr[Debug::KBMAP] << "Disabling keymap" << endl;
304 void TransManager::TranslateAndInsert(char c,LyXText *text)
308 res=active_->process(c,*this);
310 // Process with tokens
313 while(res.length()>0) {
314 res=split(res, temp,TransState::TOKEN_SEP);
320 void TransManager::insertVerbatim(const string& str,LyXText *text)
324 for (int i=0;i<l;i++){
326 && text->GetFont(text->cursor.par,
327 text->cursor.pos).latex() == LyXFont::OFF)
328 InsertCorrectQuote();
330 text->InsertChar(str[i]);
335 void TransManager::insert(string str,LyXText *text)
339 // Go through the character encoding only if the current
340 // encoding (chset_->name()) matches the current font_norm
343 if (chset_->getName()!=lyxrc->font_norm ||
344 chset_->encodeString(str)==false) {
345 // Could not find an encoding
346 InsetLatexAccent ins(str);
347 if (ins.CanDisplay()) {
348 text->InsertInset(new InsetLatexAccent(ins));
353 insertVerbatim(str,text);
357 //bool TransManager::backspace()
359 // return trans_fsm_.currentState->backspace();
363 void TransManager::deadkey(char c,tex_accent accent,LyXText *t)
368 if (c==0 && active_!=default_) {
369 // A deadkey was pressed that cannot be printed
370 // or a accent command was typed in the minibuffer
372 if (active_->isAccentDefined(accent,i)==true) {
373 res=trans_fsm_.currentState->deadkey(c,i);
378 // Fall through to the next case
381 if (active_==default_ || c==0) {
383 i.allowed=lyx_accent_table[accent].native;
387 string res=trans_fsm_.currentState->deadkey(c,i);
390 // Go through the translation
391 TranslateAndInsert(c,t);