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