]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.C
more cursor dispatch
[lyx.git] / src / MenuBackend.C
1 /**
2  * \file MenuBackend.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author André Pönitz
10  * \author Dekel Tsur
11  * \author Martin Vermeer
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "MenuBackend.h"
19
20 #include "BranchList.h"
21 #include "buffer.h"
22 #include "bufferlist.h"
23 #include "bufferparams.h"
24 #include "CutAndPaste.h"
25 #include "debug.h"
26 #include "exporter.h"
27 #include "Floating.h"
28 #include "FloatList.h"
29 #include "format.h"
30 #include "gettext.h"
31 #include "importer.h"
32 #include "kbmap.h"
33 #include "lastfiles.h"
34 #include "LyXAction.h"
35 #include "lyx_main.h" // for lastfiles
36 #include "lyxfunc.h"
37 #include "lyxlex.h"
38 #include "toc.h"
39
40 #include "frontends/LyXView.h"
41
42 #include "support/filetools.h"
43 #include "support/lstrings.h"
44 #include "support/tostr.h"
45
46 #include <boost/bind.hpp>
47
48 #include <algorithm>
49
50 using lyx::support::compare_ascii_no_case;
51 using lyx::support::contains;
52 using lyx::support::MakeDisplayPath;
53 using lyx::support::token;
54
55 using boost::bind;
56
57 using std::auto_ptr;
58 using std::endl;
59 using std::equal_to;
60 using std::find_if;
61 using std::max;
62 using std::sort;
63 using std::string;
64 using std::vector;
65
66
67 extern BufferList bufferlist;
68 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
69
70 namespace {
71
72 class MenuNamesEqual : public std::unary_function<Menu, bool> {
73 public:
74         MenuNamesEqual(string const & name)
75                 : name_(name) {}
76         bool operator()(Menu const & menu) const
77         {
78                 return menu.name() == name_;
79         }
80 private:
81         string name_;
82 };
83
84 } // namespace anon
85
86
87 // This is the global menu definition
88 MenuBackend menubackend;
89
90
91 MenuItem::MenuItem(Kind kind)
92         : kind_(kind), optional_(false)
93 {}
94
95
96 MenuItem::MenuItem(Kind kind, string const & label,
97                    string const & submenu, bool optional)
98         : kind_(kind), label_(label),
99           submenuname_(submenu), optional_(optional)
100 {
101         BOOST_ASSERT(kind == Submenu);
102 }
103
104
105 MenuItem::MenuItem(Kind kind, string const & label,
106                    FuncRequest const & func, bool optional)
107         : kind_(kind), label_(label), func_(func), optional_(optional)
108 {}
109
110
111 MenuItem::~MenuItem()
112 {}
113
114
115 void MenuItem::submenu(Menu * menu)
116 {
117         submenu_.reset(menu);
118 }
119
120
121 string const MenuItem::label() const
122 {
123         return token(label_, '|', 0);
124 }
125
126
127 string const MenuItem::shortcut() const
128 {
129         return token(label_, '|', 1);
130 }
131
132
133 string const MenuItem::binding() const
134 {
135         if (kind_ != Command)
136                 return string();
137
138         // Get the keys bound to this action, but keep only the
139         // first one later
140         string bindings = toplevel_keymap->findbinding(func_);
141
142         if (!bindings.empty()) {
143                 return bindings.substr(1, bindings.find(']') - 1);
144         } else {
145                 lyxerr[Debug::KBMAP]
146                         << "No bindings for "
147                         << lyxaction.getActionName(func_.action)
148                         << '(' << func_.argument << ')' << endl;
149                 return string();
150         }
151
152 }
153
154
155 Menu & Menu::add(MenuItem const & i, LyXView const * view)
156 {
157         if (!view) {
158                 items_.push_back(i);
159                 return *this;
160         }
161
162         switch (i.kind()) {
163         case MenuItem::Command:
164         {
165                 FuncStatus status =
166                         view->getLyXFunc().getStatus(i.func());
167                 if (status.unknown()
168                     || (status.disabled() && i.optional()))
169                         break;
170                 items_.push_back(i);
171                 items_.back().status(status);
172                 break;
173         }
174         case MenuItem::Submenu:
175         {
176                 if (i.submenu()) {
177                         bool disabled = true;
178                         for (const_iterator cit = i.submenu()->begin();
179                              cit != i.submenu()->end(); ++cit) {
180                                 if ((cit->kind() == MenuItem::Command
181                                      || cit->kind() == MenuItem::Submenu)
182                                     && !cit->status().disabled()) {
183                                         disabled = false;
184                                         break;
185                                 }
186                         }
187                         if (!disabled || !i.optional()) {
188                                 items_.push_back(i);
189                                 items_.back().status().disabled(disabled);
190                         }
191                 }
192                 else
193                         items_.push_back(i);
194                 break;
195         }
196         case MenuItem::Separator:
197                 if (!items_.empty()
198                     && items_.back().kind() != MenuItem::Separator)
199                         items_.push_back(i);
200                 break;
201         default:
202                 items_.push_back(i);
203         }
204
205         return *this;
206 }
207
208
209 Menu & Menu::read(LyXLex & lex)
210 {
211         enum Menutags {
212                 md_item = 1,
213                 md_branches,
214                 md_documents,
215                 md_charstyles,
216                 md_endmenu,
217                 md_exportformats,
218                 md_importformats,
219                 md_lastfiles,
220                 md_optitem,
221                 md_optsubmenu,
222                 md_separator,
223                 md_submenu,
224                 md_toc,
225                 md_updateformats,
226                 md_viewformats,
227                 md_floatlistinsert,
228                 md_floatinsert,
229                 md_pasterecent,
230                 md_last
231         };
232
233         struct keyword_item menutags[md_last - 1] = {
234                 { "branches", md_branches },
235                 { "charstyles", md_charstyles },
236                 { "documents", md_documents },
237                 { "end", md_endmenu },
238                 { "exportformats", md_exportformats },
239                 { "floatinsert", md_floatinsert },
240                 { "floatlistinsert", md_floatlistinsert },
241                 { "importformats", md_importformats },
242                 { "item", md_item },
243                 { "lastfiles", md_lastfiles },
244                 { "optitem", md_optitem },
245                 { "optsubmenu", md_optsubmenu },
246                 { "pasterecent", md_pasterecent },
247                 { "separator", md_separator },
248                 { "submenu", md_submenu },
249                 { "toc", md_toc },
250                 { "updateformats", md_updateformats },
251                 { "viewformats", md_viewformats }
252         };
253
254         lex.pushTable(menutags, md_last - 1);
255         if (lyxerr.debugging(Debug::PARSER))
256                 lex.printTable(lyxerr);
257
258         bool quit = false;
259         bool optional = false;
260
261         while (lex.isOK() && !quit) {
262                 switch (lex.lex()) {
263                 case md_optitem:
264                         optional = true;
265                         // fallback to md_item
266                 case md_item: {
267                         lex.next(true);
268                         string const name = _(lex.getString());
269                         lex.next(true);
270                         string const command = lex.getString();
271                         FuncRequest func = lyxaction.lookupFunc(command);
272                         add(MenuItem(MenuItem::Command, name, func, optional));
273                         optional = false;
274                         break;
275                 }
276
277                 case md_separator:
278                         add(MenuItem(MenuItem::Separator));
279                         break;
280
281                 case md_lastfiles:
282                         add(MenuItem(MenuItem::Lastfiles));
283                         break;
284
285                 case md_charstyles:
286                         add(MenuItem(MenuItem::CharStyles));
287                         break;
288
289                 case md_documents:
290                         add(MenuItem(MenuItem::Documents));
291                         break;
292
293                 case md_toc:
294                         add(MenuItem(MenuItem::Toc));
295                         break;
296
297                 case md_viewformats:
298                         add(MenuItem(MenuItem::ViewFormats));
299                         break;
300
301                 case md_updateformats:
302                         add(MenuItem(MenuItem::UpdateFormats));
303                         break;
304
305                 case md_exportformats:
306                         add(MenuItem(MenuItem::ExportFormats));
307                         break;
308
309                 case md_importformats:
310                         add(MenuItem(MenuItem::ImportFormats));
311                         break;
312
313                 case md_floatlistinsert:
314                         add(MenuItem(MenuItem::FloatListInsert));
315                         break;
316
317                 case md_floatinsert:
318                         add(MenuItem(MenuItem::FloatInsert));
319                         break;
320
321                 case md_pasterecent:
322                         add(MenuItem(MenuItem::PasteRecent));
323                         break;
324
325                 case md_branches:
326                         add(MenuItem(MenuItem::Branches));
327                         break;
328
329                 case md_optsubmenu:
330                         optional = true;
331                         // fallback to md_submenu
332                 case md_submenu: {
333                         lex.next(true);
334                         string const mlabel = _(lex.getString());
335                         lex.next(true);
336                         string const mname = lex.getString();
337                         add(MenuItem(MenuItem::Submenu, mlabel, mname,
338                                      optional));
339                         optional = false;
340                         break;
341                 }
342
343                 case md_endmenu:
344                         quit = true;
345                         break;
346
347                 default:
348                         lex.printError("Menu::read: "
349                                        "Unknown menu tag: `$$Token'");
350                         break;
351                 }
352         }
353         lex.popTable();
354         return *this;
355 }
356
357
358 void Menu::checkShortcuts() const
359 {
360         // This is a quadratic algorithm, but we do not care because
361         // menus are short enough
362         for (const_iterator it1 = begin(); it1 != end(); ++it1) {
363                 string shortcut = it1->shortcut();
364                 if (shortcut.empty())
365                         continue;
366                 if (!contains(it1->label(), shortcut))
367                         lyxerr << "Menu warning: menu entry \""
368                                << it1->label()
369                                << "\" does not contain shortcut `"
370                                << shortcut << "'." << endl;
371                 for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
372                         if (!compare_ascii_no_case(it2->shortcut(), shortcut)) {
373                                 lyxerr << "Menu warning: menu entries "
374                                        << '"' << it1->fulllabel()
375                                        << "\" and \"" << it2->fulllabel()
376                                        << "\" share the same shortcut."
377                                        << endl;
378                         }
379                 }
380         }
381 }
382
383
384 namespace {
385
386 class compare_format {
387 public:
388         bool operator()(Format const * p1, Format const * p2) {
389                 return *p1 < *p2;
390         }
391 };
392
393 string const limit_string_length(string const & str)
394 {
395         string::size_type const max_item_length = 45;
396
397         if (str.size() > max_item_length)
398                 return str.substr(0, max_item_length - 3) + "...";
399         else
400                 return str;
401 }
402
403
404 void expandLastfiles(Menu & tomenu, LyXView const * view)
405 {
406         LastFiles const & lastfiles = LyX::cref().lastfiles();
407
408         int ii = 1;
409         LastFiles::const_iterator lfit = lastfiles.begin();
410         LastFiles::const_iterator end = lastfiles.end();
411
412         for (; lfit != end && ii < 10; ++lfit, ++ii) {
413                 string const label = tostr(ii) + ". "
414                         + MakeDisplayPath((*lfit), 30)
415                         + '|' + tostr(ii);
416                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_FILE_OPEN, (*lfit))), view);
417         }
418 }
419
420 void expandDocuments(Menu & tomenu, LyXView const * view)
421 {
422         typedef vector<string> Strings;
423         Strings const names = bufferlist.getFileNames();
424
425         if (names.empty()) {
426                 tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
427                                     FuncRequest(LFUN_NOACTION)), view);
428                 return;
429         }
430
431         int ii = 1;
432         Strings::const_iterator docit = names.begin();
433         Strings::const_iterator end = names.end();
434         for (; docit != end; ++docit, ++ii) {
435                 string label = MakeDisplayPath(*docit, 20);
436                 if (ii < 10)
437                         label = tostr(ii) + ". " + label + '|' + tostr(ii);
438                 tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_SWITCHBUFFER, *docit)), view);
439         }
440 }
441
442
443 void expandFormats(MenuItem::Kind kind, Menu & tomenu, LyXView const * view)
444 {
445         if (!view->buffer() && kind != MenuItem::ImportFormats) {
446                 tomenu.add(MenuItem(MenuItem::Command,
447                                     _("No Documents Open!"),
448                                     FuncRequest(LFUN_NOACTION)),
449                                     view);
450                 return;
451         }
452
453         typedef vector<Format const *> Formats;
454         Formats formats;
455         kb_action action;
456
457         switch (kind) {
458         case MenuItem::ImportFormats:
459                 formats = Importer::GetImportableFormats();
460                 action = LFUN_IMPORT;
461                 break;
462         case MenuItem::ViewFormats:
463                 formats = Exporter::GetExportableFormats(*view->buffer(), true);
464                 action = LFUN_PREVIEW;
465                 break;
466         case MenuItem::UpdateFormats:
467                 formats = Exporter::GetExportableFormats(*view->buffer(), true);
468                 action = LFUN_UPDATE;
469                 break;
470         default:
471                 formats = Exporter::GetExportableFormats(*view->buffer(), false);
472                 action = LFUN_EXPORT;
473         }
474         sort(formats.begin(), formats.end(), compare_format());
475
476         Formats::const_iterator fit = formats.begin();
477         Formats::const_iterator end = formats.end();
478         for (; fit != end ; ++fit) {
479                 if ((*fit)->dummy())
480                         continue;
481                 string label = (*fit)->prettyname();
482                 // we need to hide the default graphic export formats
483                 // from the external menu, because we need them only
484                 // for the internal lyx-view and external latex run
485                 if (label == "EPS" || label == "XPM" || label == "PNG")
486                         continue;
487
488                 if (kind == MenuItem::ImportFormats) {
489                         if ((*fit)->name() == "text")
490                                 label = _("ASCII text as lines");
491                         else if ((*fit)->name() == "textparagraph")
492                                 label = _("ASCII text as paragraphs");
493                         label += "...";
494                 }
495                 if (!(*fit)->shortcut().empty())
496                         label += '|' + (*fit)->shortcut();
497
498                 tomenu.add(MenuItem(MenuItem::Command, label,
499                                     FuncRequest(action, (*fit)->name())),
500                            view);
501         }
502 }
503
504
505 void expandFloatListInsert(Menu & tomenu, LyXView const * view)
506 {
507         if (!view->buffer()) {
508                 tomenu.add(MenuItem(MenuItem::Command,
509                                     _("No Documents Open!"),
510                                     FuncRequest(LFUN_NOACTION)),
511                            view);
512                 return;
513         }
514
515         FloatList const & floats =
516                 view->buffer()->params().getLyXTextClass().floats();
517         FloatList::const_iterator cit = floats.begin();
518         FloatList::const_iterator end = floats.end();
519         for (; cit != end; ++cit) {
520                 tomenu.add(MenuItem(MenuItem::Command,
521                                     _(cit->second.listName()),
522                                     FuncRequest(LFUN_FLOAT_LIST,
523                                                 cit->second.type())),
524                            view);
525         }
526 }
527
528
529 void expandFloatInsert(Menu & tomenu, LyXView const * view)
530 {
531         if (!view->buffer()) {
532                 tomenu.add(MenuItem(MenuItem::Command,
533                                     _("No Documents Open!"),
534                                     FuncRequest(LFUN_NOACTION)),
535                            view);
536                 return;
537         }
538
539         FloatList const & floats =
540                 view->buffer()->params().getLyXTextClass().floats();
541         FloatList::const_iterator cit = floats.begin();
542         FloatList::const_iterator end = floats.end();
543         for (; cit != end; ++cit) {
544                 // normal float
545                 string const label = _(cit->second.name());
546                 tomenu.add(MenuItem(MenuItem::Command, label,
547                                     FuncRequest(LFUN_INSET_FLOAT,
548                                                 cit->second.type())),
549                            view);
550         }
551 }
552
553
554 void expandCharStyleInsert(Menu & tomenu, LyXView const * view)
555 {
556         if (!view->buffer()) {
557                 tomenu.add(MenuItem(MenuItem::Command,
558                                     _("No Documents Open!"),
559                                     FuncRequest(LFUN_NOACTION)),
560                            view);
561                 return;
562         }
563         CharStyles & charstyles =
564                 view->buffer()->params().getLyXTextClass().charstyles();
565         CharStyles::iterator cit = charstyles.begin();
566         CharStyles::iterator end = charstyles.end();
567         for (; cit != end; ++cit) {
568                 string const label = cit->name;
569                 tomenu.add(MenuItem(MenuItem::Command, label,
570                                     FuncRequest(LFUN_INSERT_CHARSTYLE,
571                                                 cit->name)), view);
572         }
573 }
574
575
576 Menu::size_type const max_number_of_items = 25;
577
578 void expandToc2(Menu & tomenu,
579                 lyx::toc::Toc const & toc_list,
580                 lyx::toc::Toc::size_type from,
581                 lyx::toc::Toc::size_type to, int depth)
582 {
583         int shortcut_count = 0;
584         if (to - from <= max_number_of_items) {
585                 for (lyx::toc::Toc::size_type i = from; i < to; ++i) {
586                         string label(4 * max(0, toc_list[i].depth - depth),' ');
587                         label += limit_string_length(toc_list[i].str);
588                         if (toc_list[i].depth == depth
589                             && shortcut_count < 9) {
590                                 if (label.find(tostr(shortcut_count + 1)) != string::npos)
591                                         label += '|' + tostr(++shortcut_count);
592                         }
593                         tomenu.add(MenuItem(MenuItem::Command, label,
594                                             FuncRequest(toc_list[i].action())));
595                 }
596         } else {
597                 lyx::toc::Toc::size_type pos = from;
598                 while (pos < to) {
599                         lyx::toc::Toc::size_type new_pos = pos + 1;
600                         while (new_pos < to &&
601                                toc_list[new_pos].depth > depth)
602                                 ++new_pos;
603
604                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
605                         label += limit_string_length(toc_list[pos].str);
606                         if (toc_list[pos].depth == depth &&
607                             shortcut_count < 9) {
608                                 if (label.find(tostr(shortcut_count + 1)) != string::npos)
609                                         label += '|' + tostr(++shortcut_count);
610                                 }
611                         if (new_pos == pos + 1) {
612                                 tomenu.add(MenuItem(MenuItem::Command,
613                                                     label, FuncRequest(toc_list[pos].action())));
614                         } else {
615                                 MenuItem item(MenuItem::Submenu, label);
616                                 item.submenu(new Menu);
617                                 expandToc2(*item.submenu(),
618                                            toc_list, pos, new_pos, depth + 1);
619                                 tomenu.add(item);
620                         }
621                         pos = new_pos;
622                 }
623         }
624 }
625
626
627 void expandToc(Menu & tomenu, LyXView const * view)
628 {
629         // To make things very cleanly, we would have to pass view to
630         // all MenuItem constructors and to expandToc2. However, we
631         // know that all the entries in a TOC will be have status_ ==
632         // OK, so we avoid this unnecessary overhead (JMarc)
633
634         if (!view->buffer()) {
635                 tomenu.add(MenuItem(MenuItem::Command,
636                                     _("No Documents Open!"),
637                                     FuncRequest(LFUN_NOACTION)),
638                            view);
639                 return;
640         }
641
642         lyx::toc::TocList toc_list = lyx::toc::getTocList(*view->buffer());
643         lyx::toc::TocList::const_iterator cit = toc_list.begin();
644         lyx::toc::TocList::const_iterator end = toc_list.end();
645         for (; cit != end; ++cit) {
646                 // Handle this later
647                 if (cit->first == "TOC")
648                         continue;
649
650                 // All the rest is for floats
651                 auto_ptr<Menu> menu(new Menu);
652                 lyx::toc::Toc::const_iterator ccit = cit->second.begin();
653                 lyx::toc::Toc::const_iterator eend = cit->second.end();
654                 for (; ccit != eend; ++ccit) {
655                         string const label = limit_string_length(ccit->str);
656                         menu->add(MenuItem(MenuItem::Command,
657                                            label,
658                                            FuncRequest(ccit->action())));
659                 }
660                 string const & floatName = cit->first;
661                 // Is the _(...) really needed here? (Lgb)
662                 MenuItem item(MenuItem::Submenu, _(floatName));
663                 item.submenu(menu.release());
664                 tomenu.add(item);
665         }
666
667         // Handle normal TOC
668         cit = toc_list.find("TOC");
669         if (cit == end) {
670                 tomenu.add(MenuItem(MenuItem::Command,
671                                     _("No Table of contents"),
672                                     FuncRequest()),
673                            view);
674         } else {
675                 expandToc2(tomenu, cit->second, 0, cit->second.size(), 0);
676         }
677 }
678
679
680 void expandPasteRecent(Menu & tomenu, LyXView const * view)
681 {
682         if (!view || !view->buffer())
683                 return;
684
685         vector<string> const selL =
686                 CutAndPaste::availableSelections(*view->buffer());
687
688         vector<string>::const_iterator cit = selL.begin();
689         vector<string>::const_iterator end = selL.end();
690
691         for (unsigned int index = 0; cit != end; ++cit, ++index) {
692                 tomenu.add(MenuItem(MenuItem::Command, *cit,
693                                     FuncRequest(LFUN_PASTE, tostr(index))));
694         }
695 }
696
697
698 void expandBranches(Menu & tomenu, LyXView const * view)
699 {
700         if (!view || !view->buffer())
701                 return;
702
703         BufferParams const & params = view->buffer()->params();
704
705         std::list<Branch>::const_iterator cit = params.branchlist().begin();
706         std::list<Branch>::const_iterator end = params.branchlist().end();
707
708         for (int ii = 1; cit != end; ++cit, ++ii) {
709                 string label = cit->getBranch();
710                 if (ii < 10)
711                         label = tostr(ii) + ". " + label + "|" + tostr(ii);
712                 tomenu.add(MenuItem(MenuItem::Command, label,
713                                     FuncRequest(LFUN_INSERT_BRANCH,
714                                                 cit->getBranch())), view);
715         }
716 }
717
718
719 } // namespace anon
720
721
722 void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
723                          LyXView const * view) const
724 {
725         for (Menu::const_iterator cit = frommenu.begin();
726              cit != frommenu.end() ; ++cit) {
727                 switch (cit->kind()) {
728                 case MenuItem::Lastfiles:
729                         expandLastfiles(tomenu, view);
730                         break;
731
732                 case MenuItem::Documents:
733                         expandDocuments(tomenu, view);
734                         break;
735
736                 case MenuItem::ImportFormats:
737                 case MenuItem::ViewFormats:
738                 case MenuItem::UpdateFormats:
739                 case MenuItem::ExportFormats:
740                         expandFormats(cit->kind(), tomenu, view);
741                         break;
742
743                 case MenuItem::CharStyles:
744                         expandCharStyleInsert(tomenu, view);
745                         break;
746
747                 case MenuItem::FloatListInsert:
748                         expandFloatListInsert(tomenu, view);
749                         break;
750
751                 case MenuItem::FloatInsert:
752                         expandFloatInsert(tomenu, view);
753                         break;
754
755                 case MenuItem::PasteRecent:
756                         expandPasteRecent(tomenu, view);
757                         break;
758
759                 case MenuItem::Branches:
760                         expandBranches(tomenu, view);
761                         break;
762
763                 case MenuItem::Toc:
764                         expandToc(tomenu, view);
765                         break;
766
767                 case MenuItem::Submenu: {
768                         MenuItem item(*cit);
769                         item.submenu(new Menu(cit->submenuname()));
770                         expand(getMenu(cit->submenuname()),
771                                *item.submenu(), view);
772                         tomenu.add(item, view);
773                 }
774                 break;
775
776                 default:
777                         tomenu.add(*cit, view);
778                 }
779         }
780
781         // we do not want the menu to end with a separator
782         if (!tomenu.empty()
783             && tomenu.items_.back().kind() == MenuItem::Separator)
784                 tomenu.items_.pop_back();
785
786         // Check whether the shortcuts are unique
787         tomenu.checkShortcuts();
788 }
789
790
791 bool Menu::hasSubmenu(string const & name) const
792 {
793 #if 1
794         return find_if(begin(), end(),
795                        bind(std::equal_to<string>(),
796                             bind(&MenuItem::submenuname, _1),
797                             name)) != end();
798 #else
799         // I would have prefered this, but I am not sure if it
800         // makes a difference. (Lgb)
801         return find_if(
802                 make_transform_iterator(begin(),
803                                         bind(&MenuItem::submenuname, _1)),
804                 make_transform_iterator(end(),
805                                         bind(&MenuItem::submenuname, _1)),
806                 name
807                 ).base() != end();
808 #endif
809 }
810
811
812 void MenuBackend::read(LyXLex & lex)
813 {
814         enum Menutags {
815                 md_menu = 1,
816                 md_menubar,
817                 md_endmenuset,
818                 md_last
819         };
820
821         struct keyword_item menutags[md_last - 1] = {
822                 { "end", md_endmenuset },
823                 { "menu", md_menu },
824                 { "menubar", md_menubar }
825         };
826
827         //consistency check
828         if (compare_ascii_no_case(lex.getString(), "menuset")) {
829                 lyxerr << "Menubackend::read: ERROR wrong token:`"
830                        << lex.getString() << '\'' << endl;
831         }
832
833         lex.pushTable(menutags, md_last - 1);
834         if (lyxerr.debugging(Debug::PARSER))
835                 lex.printTable(lyxerr);
836
837         bool quit = false;
838
839         while (lex.isOK() && !quit) {
840                 switch (lex.lex()) {
841                 case md_menubar:
842                         menubar_.read(lex);
843                         break;
844                 case md_menu: {
845                         lex.next(true);
846                         string const name = lex.getString();
847                         if (hasMenu(name)) {
848                                 getMenu(name).read(lex);
849                         } else {
850                                 Menu menu(name);
851                                 menu.read(lex);
852                                 add(menu);
853                         }
854                         break;
855                 }
856                 case md_endmenuset:
857                         quit = true;
858                         break;
859                 default:
860                         lex.printError("menubackend::read: "
861                                        "Unknown menu tag: `$$Token'");
862                         break;
863                 }
864         }
865         lex.popTable();
866 }
867
868
869 void MenuBackend::add(Menu const & menu)
870 {
871         menulist_.push_back(menu);
872 }
873
874
875 bool MenuBackend::hasMenu(string const & name) const
876 {
877         return find_if(begin(), end(), MenuNamesEqual(name)) != end();
878 }
879
880
881 Menu const & MenuBackend::getMenu(string const & name) const
882 {
883         const_iterator cit = find_if(begin(), end(), MenuNamesEqual(name));
884         if (cit == end())
885                 lyxerr << "No submenu named " << name << endl;
886         BOOST_ASSERT(cit != end());
887         return (*cit);
888 }
889
890
891 Menu & MenuBackend::getMenu(string const & name)
892 {
893         iterator it = find_if(begin(), end(), MenuNamesEqual(name));
894         if (it == end())
895                 lyxerr << "No submenu named " << name << endl;
896         BOOST_ASSERT(it != end());
897         return (*it);
898 }
899
900
901 Menu const & MenuBackend::getMenubar() const
902 {
903         return menubar_;
904 }