]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Move Cursor.h from Clipboard.h
[lyx.git] / src / insets / InsetInfo.cpp
1 /**
2  * \file InsetInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetInfo.h"
14 #include "LyX.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "CutAndPaste.h"
20 #include "Font.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetGraphics.h"
24 #include "InsetSpecialChar.h"
25 #include "KeyMap.h"
26 #include "LaTeXFeatures.h"
27 #include "Language.h"
28 #include "LayoutFile.h"
29 #include "LyXAction.h"
30 #include "LyXRC.h"
31 #include "LyXVC.h"
32 #include "Lexer.h"
33 #include "Paragraph.h"
34 #include "ParIterator.h"
35 #include "ParagraphParameters.h"
36 #include "version.h"
37
38 #include "frontends/Application.h"
39
40 #include "support/convert.h"
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/docstring_list.h"
44 #include "support/ExceptionMessage.h"
45 #include "support/FileName.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/Length.h"
49 #include "support/Messages.h"
50 #include "support/lstrings.h"
51 #include "support/qstring_helpers.h"
52 #include "support/Translator.h"
53
54 #include <sstream>
55
56 #include <QtGui/QImage>
57 #include <QDate>
58 #include <QLocale>
59
60 using namespace std;
61 using namespace lyx::support;
62
63 namespace lyx {
64
65 namespace {
66
67 typedef Translator<InsetInfoParams::info_type, string> NameTranslator;
68
69 NameTranslator const initTranslator()
70 {
71         NameTranslator translator(InsetInfoParams::UNKNOWN_INFO, "unknown");
72
73         translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "shortcuts");
74         translator.addPair(InsetInfoParams::SHORTCUT_INFO, "shortcut");
75         translator.addPair(InsetInfoParams::LYXRC_INFO, "lyxrc");
76         translator.addPair(InsetInfoParams::PACKAGE_INFO, "package");
77         translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "textclass");
78         translator.addPair(InsetInfoParams::MENU_INFO, "menu");
79         translator.addPair(InsetInfoParams::L7N_INFO, "l7n");
80         translator.addPair(InsetInfoParams::ICON_INFO, "icon");
81         translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
82         translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
83         translator.addPair(InsetInfoParams::VCS_INFO, "vcs");
84         translator.addPair(InsetInfoParams::DATE_INFO, "date");
85         translator.addPair(InsetInfoParams::MODDATE_INFO, "moddate");
86         translator.addPair(InsetInfoParams::FIXDATE_INFO, "fixdate");
87         translator.addPair(InsetInfoParams::TIME_INFO, "time");
88         translator.addPair(InsetInfoParams::MODTIME_INFO, "modtime");
89         translator.addPair(InsetInfoParams::FIXTIME_INFO, "fixtime");
90
91         return translator;
92 }
93
94 /// The translator between the information type enum and corresponding string.
95 NameTranslator const & nameTranslator()
96 {
97         static NameTranslator const translator = initTranslator();
98         return translator;
99 }
100
101
102 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
103
104 DefaultValueTranslator const initDVTranslator()
105 {
106         DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
107
108         translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "info-insert");
109         translator.addPair(InsetInfoParams::SHORTCUT_INFO, "info-insert");
110         translator.addPair(InsetInfoParams::LYXRC_INFO, "user_name");
111         translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
112         translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
113         translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
114         translator.addPair(InsetInfoParams::L7N_INFO, "");
115         translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
116         translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
117         translator.addPair(InsetInfoParams::LYX_INFO, "version");
118         translator.addPair(InsetInfoParams::VCS_INFO, "revision");
119         translator.addPair(InsetInfoParams::DATE_INFO, "loclong");
120         translator.addPair(InsetInfoParams::MODDATE_INFO, "loclong");
121         translator.addPair(InsetInfoParams::FIXDATE_INFO, "loclong");
122         translator.addPair(InsetInfoParams::TIME_INFO, "long");
123         translator.addPair(InsetInfoParams::MODTIME_INFO, "long");
124         translator.addPair(InsetInfoParams::FIXTIME_INFO, "long");
125
126         return translator;
127 }
128
129 /// The translator between the information type enum and some sensible default value.
130 DefaultValueTranslator const & defaultValueTranslator()
131 {
132         static DefaultValueTranslator const translator = initDVTranslator();
133         return translator;
134 }
135
136 } // namespace
137
138
139 /////////////////////////////////////////////////////////////////////
140 //
141 // InsetInfoParams
142 //
143 ///////////////////////////////////////////////////////////////////////
144
145 InsetInfoParams infoparams;
146
147 namespace{
148 set<string> getTexFileList(string const & filename)
149 {
150         set<string> list;
151         FileName const file = libFileSearch(string(), filename);
152         if (file.empty())
153                 return list;
154
155         // FIXME Unicode.
156         vector<docstring> doclist =
157                 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
158
159         // Normalise paths like /foo//bar ==> /foo/bar
160         for (auto doc : doclist) {
161                 doc = subst(doc, from_ascii("\r"), docstring());
162                 while (contains(doc, from_ascii("//")))
163                         doc = subst(doc, from_ascii("//"), from_ascii("/"));
164                 if (!doc.empty())
165                         list.insert(removeExtension(onlyFileName(to_utf8(doc))));
166         }
167
168         // remove duplicates
169         return list;
170 }
171
172 bool translateString(docstring const & in, docstring & out, string const & lcode)
173 {
174         out = translateIfPossible(in, lcode);
175         return in != out;
176 }
177 } // namespace anon
178
179
180 docstring InsetInfoParams::getDate(string const & iname, QDate const date) const
181 {
182         QLocale loc;
183         if (lang)
184                 loc = QLocale(toqstr(lang->code()));
185         if (iname == "long")
186                 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
187         else if (iname == "short")
188                 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
189         else if (iname == "ISO")
190                 return qstring_to_ucs4(date.toString(Qt::ISODate));
191         else if (iname == "loclong")
192                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
193                             : _("No long date format (language unknown)!");
194         else if (iname == "locmedium")
195                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
196                             : _("No medium date format (language unknown)!");
197         else if (iname == "locshort")
198                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
199                                 : _("No short date format (language unknown)!");
200         else
201                 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
202 }
203
204
205 docstring InsetInfoParams::getTime(string const & iname, QTime const time) const
206 {
207         QLocale loc;
208         if (lang)
209                 loc = QLocale(toqstr(lang->code()));
210         if (iname == "long")
211                 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
212         else if (iname == "short")
213                 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
214         else if (iname == "ISO")
215                 return qstring_to_ucs4(time.toString(Qt::ISODate));
216         else
217                 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
218 }
219
220
221 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
222                                                              string const & itype) const
223 {
224         vector<pair<string,docstring>> result;
225
226         switch (nameTranslator().find(itype)) {
227         case UNKNOWN_INFO:
228                 result.push_back(make_pair("invalid", _("Please select a valid type!")));
229                 break;
230
231         case SHORTCUT_INFO:
232         case SHORTCUTS_INFO:
233         case MENU_INFO:
234         case ICON_INFO: {
235                 result.push_back(make_pair("custom", _("Custom")));
236                 LyXAction::const_iterator fit = lyxaction.func_begin();
237                 LyXAction::const_iterator const fen = lyxaction.func_end();
238                 for (; fit != fen; ++fit) {
239                         string const lfun = fit->first;
240                         if (!lfun.empty())
241                                 result.push_back(make_pair(lfun, from_ascii(lfun)));
242                 }
243                 break;
244         }
245
246         case L7N_INFO:
247                 result.push_back(make_pair("custom", _("Custom")));
248                 break;
249
250         case LYXRC_INFO: {
251                 result.push_back(make_pair("custom", _("Custom")));
252                 set<string> rcs = lyxrc.getRCs();
253                 for (auto const & rc : rcs)
254                         result.push_back(make_pair(rc, from_ascii(rc)));
255                 break;
256         }
257
258         case PACKAGE_INFO:
259         case TEXTCLASS_INFO: {
260                 result.push_back(make_pair("custom", _("Custom")));
261                 string const filename = (itype == "package") ? "styFiles.lst"
262                                                             : "clsFiles.lst";
263                 set<string> flist = getTexFileList(filename);
264                 for (auto const & f : flist)
265                         result.push_back(make_pair(f, from_utf8(f)));
266                 break;
267         }
268
269         case BUFFER_INFO:
270                 result.push_back(make_pair("name", _("File name (with extension)")));
271                 result.push_back(make_pair("name-noext", _("File name (without extension)")));
272                 result.push_back(make_pair("path", _("File path")));
273                 result.push_back(make_pair("class", _("Used text class")));
274                 break;
275
276         case VCS_INFO: {
277                 if (!buf->lyxvc().inUse()) {
278                         result.push_back(make_pair("invalid", _("No version control!")));
279                         break;
280                 }
281                 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
282                 result.push_back(make_pair("revision-abbrev", _("Abbreviated revision[[Version Control]]")));
283                 result.push_back(make_pair("tree-revision", _("Tree revision")));
284                 result.push_back(make_pair("author", _("Author")));
285                 result.push_back(make_pair("date", _("Date")));
286                 result.push_back(make_pair("time", _("Time[[of day]]")));
287                 break;
288         }
289
290         case LYX_INFO:
291                 result.push_back(make_pair("version", _("LyX version")));
292                 result.push_back(make_pair("layoutformat", _("LyX layout format")));
293                 break;
294
295         case FIXDATE_INFO:
296         case DATE_INFO:
297         case MODDATE_INFO: {
298                 string const dt = split(name, '@');
299                 QDate date;
300                 if (itype == "moddate")
301                         date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
302                 else if (itype == "fixdate" && !dt.empty()) {
303                         QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
304                         date = (gdate.isValid()) ? gdate : QDate::currentDate();
305                 } else
306                         date = QDate::currentDate();
307                 result.push_back(make_pair("long",getDate("long", date)));
308                 result.push_back(make_pair("short", getDate("short", date)));
309                 result.push_back(make_pair("loclong", getDate("loclong", date)));
310                 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
311                 result.push_back(make_pair("locshort", getDate("locshort", date)));
312                 result.push_back(make_pair("ISO", getDate("ISO", date)));
313                 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
314                 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
315                 result.push_back(make_pair("MMM", getDate("MMM", date)));
316                 result.push_back(make_pair("dddd", getDate("dddd", date)));
317                 result.push_back(make_pair("ddd", getDate("ddd", date)));
318                 result.push_back(make_pair("custom", _("Custom")));
319                 break;
320         }
321         case FIXTIME_INFO:
322         case TIME_INFO:
323         case MODTIME_INFO: {
324                 string const tt = split(name, '@');
325                 QTime time;
326                 if (itype == "modtime")
327                         time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
328                 else if (itype == "fixtime" && !tt.empty()) {
329                         QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
330                         time = (gtime.isValid()) ? gtime : QTime::currentTime();
331                 } else
332                         time = QTime::currentTime();
333                 result.push_back(make_pair("long",getTime("long", time)));
334                 result.push_back(make_pair("short", getTime("short", time)));
335                 result.push_back(make_pair("ISO", getTime("ISO", time)));
336                 result.push_back(make_pair("custom", _("Custom")));
337                 break;
338         }
339         }
340
341         return result;
342 }
343
344
345 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
346                                        bool const usedefaults) const
347 {
348         string type;
349         string name = trim(split(to_utf8(arg), type, ' '));
350         if (name.empty() && usedefaults)
351                 name = defaultValueTranslator().find(type);
352
353         switch (nameTranslator().find(type)) {
354         case UNKNOWN_INFO:
355                 return false;
356
357         case SHORTCUT_INFO:
358         case SHORTCUTS_INFO:
359         case MENU_INFO: {
360                 FuncRequest func = lyxaction.lookupFunc(name);
361                 return func.action() != LFUN_UNKNOWN_ACTION;
362         }
363
364         case L7N_INFO:
365                 return !name.empty();
366
367         case ICON_INFO: {
368                 FuncCode const action = lyxaction.lookupFunc(name).action();
369                 if (action == LFUN_UNKNOWN_ACTION) {
370                         string dir = "images";
371                         return !imageLibFileSearch(dir, name, "svgz,png").empty();
372                 }
373                 return true;
374         }
375
376         case LYXRC_INFO: {
377                 set<string> rcs = lyxrc.getRCs();
378                 return rcs.find(name) != rcs.end();
379         }
380
381         case PACKAGE_INFO:
382         case TEXTCLASS_INFO:
383                 return true;
384
385         case BUFFER_INFO:
386                 return (name == "name" || name == "name-noext"
387                         || name == "path" || name == "class");
388
389         case VCS_INFO:
390                 if (name == "revision" || name == "revision-abbrev" || name == "tree-revision"
391                     || name == "author" || name == "date" || name == "time")
392                         return buf->lyxvc().inUse();
393                 return false;
394
395         case LYX_INFO:
396                 return name == "version" || name == "layoutformat";
397
398         case FIXDATE_INFO: {
399                 string date;
400                 string piece;
401                 date = split(name, piece, '@');
402                 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
403                         return false;
404                 if (!piece.empty())
405                         name = piece;
406         }
407         // fall through
408         case DATE_INFO:
409         case MODDATE_INFO: {
410                 if (name == "long" || name == "short" || name == "ISO")
411                         return true;
412                 else {
413                         QDate date = QDate::currentDate();
414                         return !date.toString(toqstr(name)).isEmpty();
415                 }
416         }
417         case FIXTIME_INFO: {
418                 string time;
419                 string piece;
420                 time = split(name, piece, '@');
421                 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
422                         return false;
423                 if (!piece.empty())
424                         name = piece;
425         }
426         // fall through
427         case TIME_INFO:
428         case MODTIME_INFO: {
429                 if (name == "long" || name == "short" || name == "ISO")
430                         return true;
431                 else {
432                         QTime time = QTime::currentTime();
433                         return !time.toString(toqstr(name)).isEmpty();
434                 }
435         }
436         }
437
438         return false;
439 }
440
441
442
443
444 string InsetInfoParams::infoType() const
445 {
446         return nameTranslator().find(type);
447 }
448
449
450
451 /////////////////////////////////////////////////////////////////////////
452 //
453 // InsetInfo
454 //
455 /////////////////////////////////////////////////////////////////////////
456
457
458
459 InsetInfo::InsetInfo(Buffer * buf, string const & name)
460         : InsetCollapsible(buf), initialized_(false)
461 {
462         params_.type = InsetInfoParams::UNKNOWN_INFO;
463         params_.force_ltr = false;
464         setInfo(name);
465         status_ = Collapsed;
466 }
467
468
469 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
470 {
471         // do not allow the cursor to be set in this Inset
472         return Inset::editXY(cur, x, y);
473 }
474
475
476 docstring InsetInfo::layoutName() const
477 {
478         return from_ascii("Info:" + params_.infoType());
479 }
480
481
482 docstring InsetInfo::toolTip(BufferView const &, int, int) const
483 {
484         docstring result;
485         switch (nameTranslator().find(params_.infoType())) {
486         case InsetInfoParams::UNKNOWN_INFO:
487                 result = _("Invalid information inset");
488                 break;
489         case InsetInfoParams::SHORTCUT_INFO:
490                 result = bformat(_("The keybard shortcut for the function '%1$s'"),
491                                 from_utf8(params_.name));
492                 break;
493         case InsetInfoParams::SHORTCUTS_INFO:
494                 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
495                                 from_utf8(params_.name));
496                 break;
497         case InsetInfoParams::MENU_INFO: 
498                 result = bformat(_("The menu location for the function '%1$s'"),
499                                 from_utf8(params_.name));
500                 break;
501         case InsetInfoParams::L7N_INFO: 
502                 result = bformat(_("The localization for the string '%1$s'"),
503                                 from_utf8(params_.name));
504                 break;
505         case InsetInfoParams::ICON_INFO:
506                 result = bformat(_("The toolbar icon for the function '%1$s'"),
507                                 from_utf8(params_.name));
508                 break;
509         case InsetInfoParams::LYXRC_INFO:
510                 result = bformat(_("The preference setting for the preference key '%1$s'"),
511                                 from_utf8(params_.name));
512                 break;
513         case InsetInfoParams::PACKAGE_INFO:
514                 result = bformat(_("Availability of the LaTeX package '%1$s'"),
515                                 from_utf8(params_.name));
516                 break;
517         case InsetInfoParams::TEXTCLASS_INFO:
518                 result = bformat(_("Availability of the LaTeX class '%1$s'"),
519                                 from_utf8(params_.name));
520                 break;
521         case InsetInfoParams::BUFFER_INFO:
522                 if (params_.name == "name")
523                         result = _("The name of this file (incl. extension)");
524                 else if (params_.name == "name-noext")
525                         result = _("The name of this file (without extension)");
526                 else if (params_.name == "path")
527                         result = _("The path where this file is saved");
528                 else if (params_.name == "class")
529                         result = _("The class this document uses");
530                 break;
531         case InsetInfoParams::VCS_INFO:
532                 if (params_.name == "revision")
533                         result = _("Version control revision");
534                 else if (params_.name == "revision-abbrev")
535                         result = _("Version control abbreviated revision");
536                 else if (params_.name == "tree-revision")
537                         result = _("Version control tree revision");
538                 else if (params_.name == "author")
539                          result = _("Version control author");
540                 else if (params_.name == "date")
541                         result = _("Version control date");
542                 else if (params_.name == "time")
543                         result = _("Version control time");
544                 break;
545         case InsetInfoParams::LYX_INFO:
546                 if (params_.name == "version")
547                         result = _("The current LyX version");
548                 else if (params_.name == "layoutformat")
549                         result = _("The current LyX layout format");
550                 break;
551         case InsetInfoParams::DATE_INFO:
552                 result = _("The current date");
553                 break;
554         case InsetInfoParams::MODDATE_INFO:
555                 result = _("The date of last save");
556                 break;
557         case InsetInfoParams::FIXDATE_INFO:
558                 result = _("A static date");
559                 break;
560         case InsetInfoParams::TIME_INFO:
561                 result = _("The current time");
562                 break;
563         case InsetInfoParams::MODTIME_INFO:
564                 result = _("The time of last save");
565                 break;
566         case InsetInfoParams::FIXTIME_INFO:
567                 result = _("A static time");
568                 break;
569         }
570
571         return result;
572 }
573
574
575 void InsetInfo::read(Lexer & lex)
576 {
577         string token;
578         while (lex.isOK()) {
579                 lex.next();
580                 token = lex.getString();
581                 if (token == "type") {
582                         lex.next();
583                         token = lex.getString();
584                         params_.type = nameTranslator().find(token);
585                 } else if (token == "arg") {
586                         lex.next(true);
587                         params_.name = lex.getString();
588                 } else if (token == "\\end_inset")
589                         break;
590         }
591         if (token != "\\end_inset") {
592                 lex.printError("Missing \\end_inset at this point");
593                 throw ExceptionMessage(WarningException,
594                         _("Missing \\end_inset at this point."),
595                         from_utf8(token));
596         }
597 }
598
599
600 void InsetInfo::write(ostream & os) const
601 {
602         os << "Info\ntype  \"" << params_.infoType()
603            << "\"\narg   " << Lexer::quoteString(params_.name);
604 }
605
606
607 bool InsetInfo::showInsetDialog(BufferView * bv) const
608 {
609         bv->showDialog("info");
610         return true;
611 }
612
613
614 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
615                 FuncStatus & flag) const
616 {
617         switch (cmd.action()) {
618         case LFUN_INSET_SETTINGS:
619                 return InsetCollapsible::getStatus(cur, cmd, flag);
620
621         case LFUN_INSET_DIALOG_UPDATE:
622         case LFUN_INSET_COPY_AS:
623         case LFUN_INSET_DISSOLVE:
624                 flag.setEnabled(true);
625                 return true;
626
627         case LFUN_INSET_MODIFY:
628                 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO)
629                         return Inset::getStatus(cur, cmd, flag);
630                 if (params_.validateArgument(&buffer(), cmd.argument())) {
631                         flag.setEnabled(true);
632                         string typestr;
633                         string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
634                         InsetInfoParams::info_type type = nameTranslator().find(typestr);
635                         string origname = params_.name;
636                         if (type == InsetInfoParams::FIXDATE_INFO
637                             || type == InsetInfoParams::FIXTIME_INFO)
638                                 split(params_.name, origname, '@');
639                         flag.setOnOff(type == params_.type && name == origname);
640                         return true;
641                 }
642                 //fall through
643
644         default:
645                 return false;
646         }
647 }
648
649
650 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
651 {
652         switch (cmd.action()) {
653         case LFUN_INSET_MODIFY:
654                 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO) {
655                         cur.undispatched();
656                         break;
657                 }
658                 cur.recordUndo();
659                 setInfo(to_utf8(cmd.argument()));
660                 cur.forceBufferUpdate();
661                 initialized_ = false;
662                 break;
663
664         case LFUN_INSET_COPY_AS: {
665                 cap::clearSelection();
666                 Cursor copy(cur);
667                 copy.pushBackward(*this);
668                 copy.pit() = 0;
669                 copy.pos() = 0;
670                 copy.resetAnchor();
671                 copy.pit() = copy.lastpit();
672                 copy.pos() = copy.lastpos();
673                 copy.setSelection();
674                 cap::copySelection(copy);
675                 break;
676         }
677
678         default:
679                 InsetCollapsible::doDispatch(cur, cmd);
680                 break;
681         }
682 }
683
684
685 void InsetInfo::setInfo(string const & name)
686 {
687         if (name.empty())
688                 return;
689
690         string saved_date_specifier;
691         // Store old date specifier for potential re-use
692         if (!params_.name.empty())
693                 saved_date_specifier = split(params_.name, '@');
694         // info_type name
695         string type;
696         params_.name = trim(split(name, type, ' '));
697         params_.type = nameTranslator().find(type);
698         if (params_.name.empty())
699                 params_.name = defaultValueTranslator().find(params_.type);
700         if (params_.type == InsetInfoParams::FIXDATE_INFO) {
701                 string const date_specifier = split(params_.name, '@');
702                 // If an explicit new fix date is specified, use that
703                 // Otherwise, use the old one or, if there is none,
704                 // the current date
705                 if (date_specifier.empty()) {
706                         if (saved_date_specifier.empty())
707                                 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
708                         else
709                                 params_.name += "@" + saved_date_specifier;
710                 }
711         }
712         else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
713                 string const time_specifier = split(params_.name, '@');
714                 // If an explicit new fix time is specified, use that
715                 // Otherwise, use the old one or, if there is none,
716                 // the current time
717                 if (time_specifier.empty()) {
718                         if (saved_date_specifier.empty())
719                                 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
720                         else
721                                 params_.name += "@" + saved_date_specifier;
722                 }
723         }
724 }
725
726
727 void InsetInfo::error(docstring const & err, Language const * lang)
728 {
729         docstring const res = translateIfPossible(err, lang->code());
730         bool const translated = res != err;
731         // If the string is not translated, we use default lang (English)
732         Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
733         setText(bformat(res, from_utf8(params_.name)), f, false);
734 }
735
736
737 void InsetInfo::info(docstring const & err, Language const * lang)
738 {
739         docstring const res = translateIfPossible(err, lang->code());
740         bool const translated = res != err;
741         // If the string is not translated, we use default lang (English)
742         Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
743         setText(translateIfPossible(err, lang->code()), f, false);
744 }
745
746
747 void InsetInfo::setText(docstring const & str, Language const * lang)
748 {
749         setText(str, Font(inherit_font, lang), false);
750 }
751
752
753 bool InsetInfo::forceLTR(OutputParams const &) const
754 {
755         return params_.force_ltr;
756 }
757
758
759 bool InsetInfo::forceLocalFontSwitch() const
760 {
761         return params_.type == InsetInfoParams::MENU_INFO
762                 || params_.type == InsetInfoParams::SHORTCUTS_INFO
763                 || params_.type == InsetInfoParams::SHORTCUT_INFO
764                 || params_.type == InsetInfoParams::L7N_INFO;
765 }
766
767
768 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted) {
769         // If the Buffer is a clone, then we neither need nor want to do any
770         // of what follows. We want, rather, just to inherit how things were
771         // in the original Buffer. This is especially important for VCS.
772         // Otherwise, we could in principle have different settings here
773         // than in the Buffer we were exporting.
774         if (buffer().isClone())
775                 return;
776
777         BufferParams const & bp = buffer().params();
778         params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
779         Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
780         // Some info insets use the language of the GUI (if available)
781         Language const * guilang = tryguilang ? tryguilang : params_.lang;
782
783         params_.force_ltr = !params_.lang->rightToLeft();
784         // This is just to get the string into the po files
785         docstring gui;
786         switch (params_.type) {
787         case InsetInfoParams::UNKNOWN_INFO:
788                 gui = _("Unknown Info!");
789                 info(from_ascii("Unknown Info!"), params_.lang);
790                 initialized_ = false;
791                 break;
792         case InsetInfoParams::SHORTCUT_INFO:
793         case InsetInfoParams::SHORTCUTS_INFO: {
794                 // shortcuts can change, so we need to re-do this each time
795                 FuncRequest const func = lyxaction.lookupFunc(params_.name);
796                 if (func.action() == LFUN_UNKNOWN_ACTION) {
797                         gui = _("Unknown action %1$s");
798                         error(from_ascii("Unknown action %1$s"), params_.lang);
799                         break;
800                 }
801                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
802                 if (bindings.empty()) {
803                         gui = _("undefined");
804                         info(from_ascii("undefined"), params_.lang);
805                         break;
806                 }
807                 docstring sequence;
808                 docstring seq_untranslated;
809                 if (params_.type == InsetInfoParams::SHORTCUT_INFO) {
810                         sequence = bindings.begin()->print(KeySequence::ForGui);
811                         seq_untranslated = bindings.begin()->print(KeySequence::ForGui, true);
812                 } else {
813                         sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
814                         seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
815                 }
816                 // QKeySequence returns special characters for keys on the mac
817                 // Since these are not included in many fonts, we
818                 // re-translate them to textual names (see #10641)
819                 odocstringstream ods;
820                 string const lcode = params_.lang->code();
821                 docstring trans;
822                 bool is_translated = sequence != seq_untranslated;
823                 for (char_type const c : sequence) {
824                         switch(c) {
825                         case 0x21b5://Return
826                                 gui = _("Return[[Key]]");
827                                 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
828                                 ods << trans;
829                                 break;
830                         case 0x21b9://Tab both directions (Win)
831                                 gui = _("Tab[[Key]]");
832                                 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
833                                 ods << trans;
834                                 break;
835                         case 0x21de://Qt::Key_PageUp
836                                 gui = _("PgUp");
837                                 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
838                                 ods << trans;
839                                 break;
840                         case 0x21df://Qt::Key_PageDown
841                                 gui = _("PgDown");
842                                 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
843                                 ods << trans;
844                                 break;
845                         case 0x21e4://Qt::Key_Backtab
846                                 gui = _("Backtab");
847                                 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
848                                 ods << trans;
849                                 break;
850                         case 0x21e5://Qt::Key_Tab
851                                 gui = _("Tab");
852                                 is_translated = translateString(from_ascii("Tab"), trans, lcode);
853                                 ods << trans;
854                                 break;
855                         case 0x21e7://Shift
856                                 gui = _("Shift");
857                                 is_translated = translateString(from_ascii("Shift"), trans, lcode);
858                                 ods << trans;
859                                 break;
860                         case 0x21ea://Qt::Key_CapsLock
861                                 gui = _("CapsLock");
862                                 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
863                                 ods << trans;
864                                 break;
865                         case 0x2303://Control
866                                 gui = _("Control[[Key]]");
867                                 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
868                                 ods << trans;
869                                 break;
870                         case 0x2318://CMD
871                                 gui = _("Command[[Key]]");
872                                 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
873                                 ods << trans;
874                                 break;
875                         case 0x2324://Qt::Key_Enter
876                                 gui = _("Return[[Key]]");
877                                 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
878                                 ods << trans;
879                                 break;
880                         case 0x2325://Option key
881                                 gui = _("Option[[Key]]");
882                                 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
883                                 ods << trans;
884                                 break;
885                         case 0x2326://Qt::Key_Delete
886                                 gui = _("Delete[[Key]]");
887                                 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
888                                 ods << trans;
889                                 break;
890                         case 0x232b://Qt::Key_Backspace
891                                 gui = _("Fn+Del");
892                                 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
893                                 ods << trans;
894                                 break;
895                         case 0x238b://Qt::Key_Escape
896                                 gui = _("Esc");
897                                 is_translated = translateString(from_ascii("Esc"), trans, lcode);
898                                 ods << trans;
899                                 break;
900                         default:
901                                 ods.put(c);
902                         }
903                 }
904                 setText(ods.str(), is_translated ? guilang : nullptr);
905                 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
906                 break;
907         }
908         case InsetInfoParams::LYXRC_INFO: {
909                 // this information could change, if the preferences are changed,
910                 // so we will recalculate each time through.
911                 ostringstream oss;
912                 if (params_.name.empty()) {
913                         gui = _("undefined");
914                         info(from_ascii("undefined"), params_.lang);
915                         break;
916                 }
917                 // FIXME this uses the serialization mechanism to get the info
918                 // we want, which i guess works but is a bit strange.
919                 lyxrc.write(oss, true, params_.name);
920                 string result = oss.str();
921                 if (result.size() < 2) {
922                         gui = _("undefined");
923                         info(from_ascii("undefined"), params_.lang);
924                         break;
925                 }
926                 string::size_type loc = result.rfind("\n", result.size() - 2);
927                 loc = loc == string::npos ? 0 : loc + 1;
928                 if (result.size() < loc + params_.name.size() + 1
929                           || result.substr(loc + 1, params_.name.size()) != params_.name) {
930                         gui = _("undefined");
931                         info(from_ascii("undefined"), params_.lang);
932                         break;
933                 }
934                 // remove leading comments and \\name and space
935                 result = result.substr(loc + params_.name.size() + 2);
936
937                 // remove \n and ""
938                 result = rtrim(result, "\n");
939                 result = trim(result, "\"");
940                 gui = _("not set");
941                 if (result.empty())
942                         result = "not set";
943                 setText(from_utf8(result), params_.lang);
944                 break;
945         }
946         case InsetInfoParams::PACKAGE_INFO:
947                 // only need to do this once.
948                 if (initialized_)
949                         break;
950                 // check in packages.lst
951                 if (LaTeXFeatures::isAvailable(params_.name)) {
952                         gui = _("yes");
953                         info(from_ascii("yes"), params_.lang);
954                 } else {
955                         gui = _("no");
956                         info(from_ascii("no"), params_.lang);
957                 }
958                 initialized_ = true;
959                 break;
960
961         case InsetInfoParams::TEXTCLASS_INFO: {
962                 // the TextClass can change
963                 LayoutFileList const & list = LayoutFileList::get();
964                 bool available = false;
965                 // params_.name is the class name
966                 if (list.haveClass(params_.name))
967                         available = list[params_.name].isTeXClassAvailable();
968                 if (available) {
969                         gui = _("yes");
970                         info(from_ascii("yes"), params_.lang);
971                 } else {
972                         gui = _("no");
973                         info(from_ascii("no"), params_.lang);
974                 }
975                 break;
976         }
977         case InsetInfoParams::MENU_INFO: {
978                 // only need to do this once.
979                 if (initialized_)
980                         break;
981                 docstring_list names;
982                 FuncRequest func = lyxaction.lookupFunc(params_.name);
983                 if (func.action() == LFUN_UNKNOWN_ACTION) {
984                         gui = _("Unknown action %1$s");
985                         error(from_ascii("Unknown action %1$s"), params_.lang);
986                         break;
987                 }
988                 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
989                         // The default output format is in the menu without argument,
990                         // so strip it here.
991                         if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
992                                 func = FuncRequest(func.action());
993                 // iterate through the menubackend to find it
994                 if (!theApp()) {
995                         gui = _("Can't determine menu entry for action %1$s in batch mode");
996                         error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
997                         initialized_ = true;
998                         break;
999                 }
1000                 // and we will not keep trying if we fail
1001                 initialized_ = theApp()->hasBufferView();
1002                 if (!theApp()->searchMenu(func, names)) {
1003                         gui = _("No menu entry for action %1$s");
1004                         error(from_ascii("No menu entry for action %1$s"), params_.lang);
1005                         break;
1006                 }
1007                 // if found, return its path.
1008                 clear();
1009                 Paragraph & par = paragraphs().front();
1010                 Font const f(inherit_font, guilang);
1011                 params_.force_ltr = !guilang->rightToLeft();
1012                 //Font fu = f;
1013                 //fu.fontInfo().setUnderbar(FONT_ON);
1014                 for (docstring const & name : names) {
1015                         // do not insert > for the top level menu item
1016                         if (&name != &names.front())
1017                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
1018                                                 f, Change(Change::UNCHANGED));
1019                         //FIXME: add proper underlines here. This
1020                         // involves rewriting searchMenu used above to
1021                         // return a vector of menus. If we do not do
1022                         // that, we might as well use below
1023                         // Paragraph::insert on each string (JMarc)
1024                         for (char_type c : name)
1025                                 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
1026                 }
1027                 break;
1028         }
1029         case InsetInfoParams::L7N_INFO: {
1030                 docstring locstring = _(params_.name);
1031                 // Remove trailing colons
1032                 locstring = rtrim(locstring, ":");
1033                 // Remove menu accelerators
1034                 if (contains(locstring, from_ascii("|"))) {
1035                         docstring nlocstring;
1036                         rsplit(locstring, nlocstring, '|');
1037                         locstring = nlocstring;
1038                 }
1039                 // Remove Qt accelerators, but keep literal ampersands
1040                 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
1041                 locstring = subst(locstring, from_ascii("&"), docstring());
1042                 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
1043                 setText(locstring, guilang);
1044                 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
1045                 break;
1046         }
1047         case InsetInfoParams::ICON_INFO: {
1048                 // only need to do this once.
1049                 if (initialized_)
1050                         break;
1051                 // and we will not keep trying if we fail
1052                 initialized_ = true;
1053                 FuncRequest func = lyxaction.lookupFunc(params_.name);
1054                 docstring icon_name = frontend::Application::iconName(func, true);
1055                 FileName file(to_utf8(icon_name));
1056                 if (file.onlyFileNameWithoutExt() == "unknown") {
1057                         string dir = "images";
1058                         FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1059                         if (!file2.empty())
1060                                 file = file2;
1061                 }
1062                 if (!file.exists())
1063                         break;
1064                 int percent_scale = 100;
1065                 if (use_gui) {
1066                         // Compute the scale factor for the icon such that its
1067                         // width on screen is equal to 1em in pixels.
1068                         // The scale factor is rounded to the integer nearest
1069                         // to the float value of the ratio 100*iconsize/imgsize.
1070                         int imgsize = QImage(toqstr(file.absFileName())).width();
1071                         if (imgsize > 0) {
1072                                 int iconsize = Length(1, Length::EM).inPixels(1);
1073                                 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1074                         }
1075                 }
1076                 InsetGraphics * inset = new InsetGraphics(buffer_);
1077                 InsetGraphicsParams igp;
1078                 igp.filename = file;
1079                 igp.lyxscale = percent_scale;
1080                 igp.scale = string();
1081                 igp.width = Length(1, Length::EM);
1082                 inset->setParams(igp);
1083                 clear();
1084                 Font const f(inherit_font, params_.lang);
1085                 paragraphs().front().insertInset(0, inset, f,
1086                                                  Change(Change::UNCHANGED));
1087                 break;
1088         }
1089         case InsetInfoParams::BUFFER_INFO: {
1090                 // this could all change, so we will recalculate each time
1091                 if (params_.name == "name")
1092                         setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1093                 else if (params_.name == "name-noext")
1094                         setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1095                 else if (params_.name == "path")
1096                         setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1097                 else if (params_.name == "class")
1098                         setText(from_utf8(bp.documentClass().name()), params_.lang);
1099                 break;
1100         }
1101         case InsetInfoParams::VCS_INFO: {
1102                 // this information could change, in principle, so we will 
1103                 // recalculate each time through
1104                 if (!buffer().lyxvc().inUse()) {
1105                         gui = _("No version control!");
1106                         info(from_ascii("No version control!"), params_.lang);
1107                         break;
1108                 }
1109                 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1110                 if (params_.name == "revision")
1111                         itype = LyXVC::File;
1112                 else if (params_.name == "revision-abbrev")
1113                         itype = LyXVC::FileAbbrev;
1114                 else if (params_.name == "tree-revision")
1115                         itype = LyXVC::Tree;
1116                 else if (params_.name == "author")
1117                         itype = LyXVC::Author;
1118                 else if (params_.name == "time")
1119                         itype = LyXVC::Time;
1120                 else if (params_.name == "date")
1121                         itype = LyXVC::Date;
1122                 string binfo = buffer().lyxvc().revisionInfo(itype);
1123                 if (binfo.empty()) {
1124                         gui = _("%1$s[[vcs data]] unknown");
1125                         error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1126                 } else
1127                         setText(from_utf8(binfo), params_.lang);
1128                 break;
1129         }
1130         case InsetInfoParams::LYX_INFO:
1131                 // only need to do this once.
1132                 if (initialized_)
1133                         break;
1134                 if (params_.name == "version")
1135                         setText(from_ascii(lyx_version), params_.lang);
1136                 else if (params_.name == "layoutformat")
1137                         setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1138                 initialized_ = true;
1139                 break;
1140         case InsetInfoParams::DATE_INFO:
1141         case InsetInfoParams::MODDATE_INFO:
1142         case InsetInfoParams::FIXDATE_INFO: {
1143                 string date_format = params_.name;
1144                 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1145                                                && contains(params_.name, '@'))
1146                                 ? split(params_.name, date_format, '@') : string();
1147                 QDate date;
1148                 if (params_.type == InsetInfoParams::MODDATE_INFO)
1149                         date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1150                 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1151                         date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1152                 else
1153                         date = QDate::currentDate();
1154                 setText(params_.getDate(date_format, date), params_.lang);
1155                 break;
1156         }
1157         case InsetInfoParams::TIME_INFO:
1158         case InsetInfoParams::MODTIME_INFO:
1159         case InsetInfoParams::FIXTIME_INFO: {
1160                 string time_format = params_.name;
1161                 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1162                                                && contains(params_.name, '@'))
1163                                 ? split(params_.name, time_format, '@') : string();
1164                 QTime time;
1165                 if (params_.type == InsetInfoParams::MODTIME_INFO)
1166                         time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1167                 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1168                         time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1169                 else
1170                         time = QTime::currentTime();
1171                 setText(params_.getTime(time_format, time), params_.lang);
1172                 break;
1173         }
1174         }
1175
1176         // Just to do something with that string
1177         LYXERR(Debug::INFO, "info inset text: " << gui);
1178         InsetCollapsible::updateBuffer(it, utype, deleted);
1179 }
1180
1181
1182 string InsetInfo::contextMenu(BufferView const &, int, int) const
1183 {
1184         //FIXME: We override the implementation of InsetCollapsible,
1185         //because this inset is not a collapsible inset.
1186         return contextMenuName();
1187 }
1188
1189
1190 string InsetInfo::contextMenuName() const
1191 {
1192         return "context-info";
1193 }
1194
1195
1196 } // namespace lyx