KTextEditor

katecmds.cpp
1 /*
2  SPDX-FileCopyrightText: 2003-2005 Anders Lund <[email protected]>
3  SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <[email protected]>
4  SPDX-FileCopyrightText: 2001 Charles Samuels <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "katecmds.h"
10 
11 #include "kateautoindent.h"
12 #include "katecmd.h"
13 #include "katedocument.h"
14 #include "katepartdebug.h"
15 #include "katerenderer.h"
16 #include "katesyntaxmanager.h"
17 #include "katetextline.h"
18 #include "kateview.h"
19 
20 #include <KLocalizedString>
21 
22 #include <QDateTime>
23 #include <QRegularExpression>
24 
25 // BEGIN CoreCommands
26 KateCommands::CoreCommands *KateCommands::CoreCommands::m_instance = nullptr;
27 
28 // this returns whether the string s could be converted to
29 // a bool value, one of on|off|1|0|true|false. the argument val is
30 // set to the extracted value in case of success
31 static bool getBoolArg(const QString &t, bool *val)
32 {
33  bool res(false);
34  QString s = t.toLower();
35  res = (s == QLatin1String("on") || s == QLatin1String("1") || s == QLatin1String("true"));
36  if (res) {
37  *val = true;
38  return true;
39  }
40  res = (s == QLatin1String("off") || s == QLatin1String("0") || s == QLatin1String("false"));
41  if (res) {
42  *val = false;
43  return true;
44  }
45  return false;
46 }
47 
49 {
50  QString realcmd = cmd.trimmed();
51  if (realcmd == QLatin1String("indent")) {
52  msg = i18n(
53  "<p>indent</p>"
54  "<p>Indents the selected lines or the current line</p>");
55  return true;
56  } else if (realcmd == QLatin1String("unindent")) {
57  msg = i18n(
58  "<p>unindent</p>"
59  "<p>Unindents the selected lines or current line.</p>");
60  return true;
61  } else if (realcmd == QLatin1String("cleanindent")) {
62  msg = i18n(
63  "<p>cleanindent</p>"
64  "<p>Cleans up the indentation of the selected lines or current line according to the indentation settings in the document. </p>");
65  return true;
66  } else if (realcmd == QLatin1String("comment")) {
67  msg = i18n(
68  "<p>comment</p>"
69  "<p>Inserts comment markers to make the selection or selected lines or current line a comment according to the text format as defined by the syntax highlight definition for the document.</p>");
70  return true;
71  } else if (realcmd == QLatin1String("uncomment")) {
72  msg = i18n(
73  "<p>uncomment</p>"
74  "<p>Removes comment markers from the selection or selected lines or current line according to the text format as defined by the syntax highlight definition for the document.</p>");
75  return true;
76  } else if (realcmd == QLatin1String("goto")) {
77  msg = i18n(
78  "<p>goto <b>line number</b></p>"
79  "<p>This command navigates to the specified line number.</p>");
80  return true;
81  } else if (realcmd == QLatin1String("set-indent-pasted-text")) {
82  msg = i18n(
83  "<p>set-indent-pasted-text <b>enable</b></p>"
84  "<p>If enabled, indentation of text pasted from the clipboard is adjusted using the current indenter.</p>"
85  "<p>Possible true values: 1 on true<br/>"
86  "possible false values: 0 off false</p>");
87  return true;
88  } else if (realcmd == QLatin1String("kill-line")) {
89  msg = i18n("Deletes the current line.");
90  return true;
91  } else if (realcmd == QLatin1String("set-tab-width")) {
92  msg = i18n(
93  "<p>set-tab-width <b>width</b></p>"
94  "<p>Sets the tab width to the number <b>width</b></p>");
95  return true;
96  } else if (realcmd == QLatin1String("set-replace-tab")) {
97  msg = i18n(
98  "<p>set-replace-tab <b>enable</b></p>"
99  "<p>If enabled, tabs are replaced with spaces as you type.</p>"
100  "<p>Possible true values: 1 on true<br/>"
101  "possible false values: 0 off false</p>");
102  return true;
103  } else if (realcmd == QLatin1String("set-show-tabs")) {
104  msg = i18n(
105  "<p>set-show-tabs <b>enable</b></p>"
106  "<p>If enabled, TAB characters and trailing whitespace will be visualized by a small dot.</p>"
107  "<p>Possible true values: 1 on true<br/>"
108  "possible false values: 0 off false</p>");
109  return true;
110  } else if (realcmd == QLatin1String("set-remove-trailing-spaces")) {
111  msg = i18n(
112  "<p>set-remove-trailing-spaces <b>mode</b></p>"
113  "<p>Removes the trailing spaces in the document depending on the <b>mode</b>.</p>"
114  "<p>Possible values:"
115  "<ul>"
116  "<li><b>none</b>: never remove trailing spaces.</li>"
117  "<li><b>modified</b>: remove trailing spaces only of modified lines.</li>"
118  "<li><b>all</b>: remove trailing spaces in the entire document.</li>"
119  "</ul></p>");
120  return true;
121  } else if (realcmd == QLatin1String("set-indent-width")) {
122  msg = i18n(
123  "<p>set-indent-width <b>width</b></p>"
124  "<p>Sets the indentation width to the number <b>width</b>. Used only if you are indenting with spaces.</p>");
125  return true;
126  } else if (realcmd == QLatin1String("set-indent-mode")) {
127  msg = i18n(
128  "<p>set-indent-mode <b>mode</b></p>"
129  "<p>The mode parameter is a value as seen in the Tools - Indentation menu</p>");
130  return true;
131  } else if (realcmd == QLatin1String("set-auto-indent")) {
132  msg = i18n(
133  "<p>set-auto-indent <b>enable</b></p>"
134  "<p>Enable or disable autoindentation.</p>"
135  "<p>possible true values: 1 on true<br/>"
136  "possible false values: 0 off false</p>");
137  return true;
138  } else if (realcmd == QLatin1String("set-line-numbers")) {
139  msg = i18n(
140  "<p>set-line-numbers <b>enable</b></p>"
141  "<p>Sets the visibility of the line numbers pane.</p>"
142  "<p> possible true values: 1 on true<br/>"
143  "possible false values: 0 off false</p>");
144  return true;
145  } else if (realcmd == QLatin1String("set-folding-markers")) {
146  msg = i18n(
147  "<p>set-folding-markers <b>enable</b></p>"
148  "<p>Sets the visibility of the folding markers pane.</p>"
149  "<p> possible true values: 1 on true<br/>"
150  "possible false values: 0 off false</p>");
151  return true;
152  } else if (realcmd == QLatin1String("set-icon-border")) {
153  msg = i18n(
154  "<p>set-icon-border <b>enable</b></p>"
155  "<p>Sets the visibility of the icon border.</p>"
156  "<p> possible true values: 1 on true<br/>"
157  "possible false values: 0 off false</p>");
158  return true;
159  } else if (realcmd == QLatin1String("set-word-wrap")) {
160  msg = i18n(
161  "<p>set-word-wrap <b>enable</b></p>"
162  "<p>Enables dynamic word wrap according to <b>enable</b></p>"
163  "<p> possible true values: 1 on true<br/>"
164  "possible false values: 0 off false</p>");
165  return true;
166  } else if (realcmd == QLatin1String("set-word-wrap-column")) {
167  msg = i18n(
168  "<p>set-word-wrap-column <b>width</b></p>"
169  "<p>Sets the line width for hard wrapping to <b>width</b>. This is used if you are having your text wrapped automatically.</p>");
170  return true;
171  } else if (realcmd == QLatin1String("set-replace-tabs-save")) {
172  msg = i18n(
173  "<p>set-replace-tabs-save <b>enable</b></p>"
174  "<p>When enabled, tabs will be replaced with whitespace whenever the document is saved.</p>"
175  "<p> possible true values: 1 on true<br/>"
176  "possible false values: 0 off false</p>");
177  return true;
178  } else if (realcmd == QLatin1String("set-highlight")) {
179  msg = i18n(
180  "<p>set-highlight <b>highlight</b></p>"
181  "<p>Sets the syntax highlighting system for the document. The argument must be a valid highlight name, as seen in the Tools → Highlighting menu. This command provides an autocompletion list for its argument.</p>");
182  return true;
183  } else if (realcmd == QLatin1String("set-mode")) {
184  msg = i18n(
185  "<p>set-mode <b>mode</b></p>"
186  "<p>Sets the mode as seen in Tools - Mode</p>");
187  return true;
188  } else if (realcmd == QLatin1String("set-show-indent")) {
189  msg = i18n(
190  "<p>set-show-indent <b>enable</b></p>"
191  "<p>If enabled, indentation will be visualized by a vertical dotted line.</p>"
192  "<p> possible true values: 1 on true<br/>"
193  "possible false values: 0 off false</p>");
194  return true;
195  } else if (realcmd == QLatin1String("print")) {
196  msg = i18n("<p>Open the Print dialog to print the current document.</p>");
197  return true;
198  } else {
199  return false;
200  }
201 }
202 
204 {
205 #define KCC_ERR(s) \
206  { \
207  errorMsg = s; \
208  return false; \
209  }
210  // cast it hardcore, we know that it is really a kateview :)
211  KTextEditor::ViewPrivate *v = static_cast<KTextEditor::ViewPrivate *>(view);
212 
213  if (!v) {
214  KCC_ERR(i18n("Could not access view"));
215  }
216 
217  // create a list of args
218 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
219  QStringList args(_cmd.split(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts));
220 #else
221  QStringList args(_cmd.split(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts));
222 #endif
223  QString cmd(args.takeFirst());
224 
225  // ALL commands that takes no arguments.
226  if (cmd == QLatin1String("indent")) {
227  if (range.isValid()) {
228  v->doc()->editStart();
229  for (int line = range.start().line(); line <= range.end().line(); line++) {
230  v->doc()->indent(KTextEditor::Range(line, 0, line, 0), 1);
231  }
232  v->doc()->editEnd();
233  } else {
234  v->indent();
235  }
236  return true;
237  } else if (cmd == QLatin1String("unindent")) {
238  if (range.isValid()) {
239  v->doc()->editStart();
240  for (int line = range.start().line(); line <= range.end().line(); line++) {
241  v->doc()->indent(KTextEditor::Range(line, 0, line, 0), -1);
242  }
243  v->doc()->editEnd();
244  } else {
245  v->unIndent();
246  }
247  return true;
248  } else if (cmd == QLatin1String("cleanindent")) {
249  if (range.isValid()) {
250  v->doc()->editStart();
251  for (int line = range.start().line(); line <= range.end().line(); line++) {
252  v->doc()->indent(KTextEditor::Range(line, 0, line, 0), 0);
253  }
254  v->doc()->editEnd();
255  } else {
256  v->cleanIndent();
257  }
258  return true;
259  } else if (cmd == QLatin1String("fold")) {
260  return (v->textFolding().newFoldingRange(range.isValid() ? range : v->selectionRange(), Kate::TextFolding::Persistent | Kate::TextFolding::Folded) != -1);
261  } else if (cmd == QLatin1String("tfold")) {
262  return (v->textFolding().newFoldingRange(range.isValid() ? range : v->selectionRange(), Kate::TextFolding::Folded) != -1);
263  } else if (cmd == QLatin1String("unfold")) {
264  QVector<QPair<qint64, Kate::TextFolding::FoldingRangeFlags>> startingRanges = v->textFolding().foldingRangesStartingOnLine(v->cursorPosition().line());
265  bool unfolded = false;
266  for (int i = 0; i < startingRanges.size(); ++i) {
267  if (startingRanges[i].second & Kate::TextFolding::Folded) {
268  unfolded = v->textFolding().unfoldRange(startingRanges[i].first) || unfolded;
269  }
270  }
271  return unfolded;
272  } else if (cmd == QLatin1String("comment")) {
273  if (range.isValid()) {
274  v->doc()->editStart();
275  for (int line = range.start().line(); line <= range.end().line(); line++) {
276  v->doc()->comment(v, line, 0, 1);
277  }
278  v->doc()->editEnd();
279  } else {
280  v->comment();
281  }
282  return true;
283  } else if (cmd == QLatin1String("uncomment")) {
284  if (range.isValid()) {
285  v->doc()->editStart();
286  for (int line = range.start().line(); line <= range.end().line(); line++) {
287  v->doc()->comment(v, line, 0, -1);
288  }
289  v->doc()->editEnd();
290  } else {
291  v->uncomment();
292  }
293  return true;
294  } else if (cmd == QLatin1String("kill-line")) {
295  if (range.isValid()) {
296  v->doc()->editStart();
297  for (int line = range.start().line(); line <= range.end().line(); line++) {
298  v->doc()->removeLine(range.start().line());
299  }
300  v->doc()->editEnd();
301  } else {
302  v->killLine();
303  }
304  return true;
305  } else if (cmd == QLatin1String("print")) {
306  v->print();
307  return true;
308  }
309 
310  // ALL commands that take a string argument
311  else if (cmd == QLatin1String("set-indent-mode") || cmd == QLatin1String("set-highlight") || cmd == QLatin1String("set-mode")) {
312  // need at least one item, otherwise args.first() crashes
313  if (args.isEmpty()) {
314  KCC_ERR(i18n("Missing argument. Usage: %1 <value>", cmd));
315  }
316 
317  if (cmd == QLatin1String("set-indent-mode")) {
318  v->doc()->config()->setIndentationMode(args.join(QLatin1Char(' ')));
319  v->doc()->rememberUserDidSetIndentationMode();
320  return true;
321  } else if (cmd == QLatin1String("set-highlight")) {
322  if (v->doc()->setHighlightingMode(args.join(QLatin1Char(' ')))) {
323  static_cast<KTextEditor::DocumentPrivate *>(v->doc())->setDontChangeHlOnSave();
324  return true;
325  }
326 
327  KCC_ERR(i18n("No such highlighting '%1'", args.first()));
328  } else if (cmd == QLatin1String("set-mode")) {
329  if (v->doc()->setMode(args.first())) {
330  return true;
331  }
332 
333  KCC_ERR(i18n("No such mode '%1'", args.first()));
334  }
335  }
336  // ALL commands that takes exactly one integer argument.
337  else if (cmd == QLatin1String("set-tab-width") || cmd == QLatin1String("set-indent-width") || cmd == QLatin1String("set-word-wrap-column") || cmd == QLatin1String("goto")) {
338  // find a integer value > 0
339  if (args.isEmpty()) {
340  KCC_ERR(i18n("Missing argument. Usage: %1 <value>", cmd));
341  }
342  bool ok;
343  int val(args.first().toInt(&ok, 10)); // use base 10 even if the string starts with '0'
344  if (!ok)
345  KCC_ERR(i18n("Failed to convert argument '%1' to integer.", args.first()));
346 
347  if (cmd == QLatin1String("set-tab-width")) {
348  if (val < 1) {
349  KCC_ERR(i18n("Width must be at least 1."));
350  }
351  v->doc()->config()->setTabWidth(val);
352  } else if (cmd == QLatin1String("set-indent-width")) {
353  if (val < 1) {
354  KCC_ERR(i18n("Width must be at least 1."));
355  }
356  v->doc()->config()->setIndentationWidth(val);
357  } else if (cmd == QLatin1String("set-word-wrap-column")) {
358  if (val < 2) {
359  KCC_ERR(i18n("Column must be at least 1."));
360  }
361  v->doc()->setWordWrapAt(val);
362  } else if (cmd == QLatin1String("goto")) {
363  if (args.first().at(0) == QLatin1Char('-') || args.first().at(0) == QLatin1Char('+')) {
364  // if the number starts with a minus or plus sign, add/subtract the number
365  val = v->cursorPosition().line() + val;
366  } else {
367  val--; // convert given line number to the internal representation of line numbers
368  }
369 
370  // constrain cursor to the range [0, number of lines]
371  if (val < 0) {
372  val = 0;
373  } else if (val > v->doc()->lines() - 1) {
374  val = v->doc()->lines() - 1;
375  }
376 
377  v->setCursorPosition(KTextEditor::Cursor(val, 0));
378  return true;
379  }
380  return true;
381  }
382 
383  // ALL commands that takes 1 boolean argument.
384  else if (cmd == QLatin1String("set-icon-border") || cmd == QLatin1String("set-folding-markers") || cmd == QLatin1String("set-indent-pasted-text") || cmd == QLatin1String("set-line-numbers") || cmd == QLatin1String("set-replace-tabs") ||
385  cmd == QLatin1String("set-show-tabs") || cmd == QLatin1String("set-word-wrap") || cmd == QLatin1String("set-wrap-cursor") || cmd == QLatin1String("set-replace-tabs-save") || cmd == QLatin1String("set-show-indent")) {
386  if (args.isEmpty()) {
387  KCC_ERR(i18n("Usage: %1 on|off|1|0|true|false", cmd));
388  }
389  bool enable = false;
390  KateDocumentConfig *const config = v->doc()->config();
391  if (getBoolArg(args.first(), &enable)) {
392  if (cmd == QLatin1String("set-icon-border")) {
393  v->setIconBorder(enable);
394  } else if (cmd == QLatin1String("set-folding-markers")) {
395  v->setFoldingMarkersOn(enable);
396  } else if (cmd == QLatin1String("set-line-numbers")) {
397  v->setLineNumbersOn(enable);
398  } else if (cmd == QLatin1String("set-show-indent")) {
399  v->renderer()->setShowIndentLines(enable);
400  } else if (cmd == QLatin1String("set-indent-pasted-text")) {
401  config->setIndentPastedText(enable);
402  } else if (cmd == QLatin1String("set-replace-tabs")) {
403  config->setReplaceTabsDyn(enable);
404  } else if (cmd == QLatin1String("set-show-tabs")) {
405  config->setShowTabs(enable);
406  } else if (cmd == QLatin1String("set-show-trailing-spaces")) {
407  config->setShowSpaces(enable ? KateDocumentConfig::Trailing : KateDocumentConfig::None);
408  } else if (cmd == QLatin1String("set-word-wrap")) {
409  v->doc()->setWordWrap(enable);
410  }
411 
412  return true;
413  } else
414  KCC_ERR(i18n("Bad argument '%1'. Usage: %2 on|off|1|0|true|false", args.first(), cmd));
415  } else if (cmd == QLatin1String("set-remove-trailing-spaces")) {
416  // need at least one item, otherwise args.first() crashes
417  if (args.count() != 1) {
418  KCC_ERR(i18n("Usage: set-remove-trailing-spaces 0|-|none or 1|+|mod|modified or 2|*|all"));
419  }
420 
421  QString tmp = args.first().toLower().trimmed();
422  if (tmp == QLatin1String("1") || tmp == QLatin1String("modified") || tmp == QLatin1String("mod") || tmp == QLatin1String("+")) {
423  v->doc()->config()->setRemoveSpaces(1);
424  } else if (tmp == QLatin1String("2") || tmp == QLatin1String("all") || tmp == QLatin1String("*")) {
425  v->doc()->config()->setRemoveSpaces(2);
426  } else {
427  v->doc()->config()->setRemoveSpaces(0);
428  }
429  }
430 
431  // unlikely..
432  KCC_ERR(i18n("Unknown command '%1'", cmd));
433 }
434 
436 {
437  static QStringList l;
438 
439  if (l.isEmpty())
440  l << QStringLiteral("indent") << QStringLiteral("unindent") << QStringLiteral("cleanindent") << QStringLiteral("comment") << QStringLiteral("uncomment") << QStringLiteral("kill-line") << QStringLiteral("fold")
441  << QStringLiteral("tfold");
442 
443  return l.contains(range);
444 }
445 
447 {
448  Q_UNUSED(view)
449 
450  if (cmd == QLatin1String("set-highlight")) {
451  QStringList l;
452  for (const auto &hl : KateHlManager::self()->modeList()) {
453  l << hl.name();
454  }
455 
457  co->setItems(l);
458  co->setIgnoreCase(true);
459  return co;
460  } else if (cmd == QLatin1String("set-remove-trailing-spaces")) {
461  QStringList l;
462  l << QStringLiteral("none") << QStringLiteral("modified") << QStringLiteral("all");
463 
465  co->setItems(l);
466  co->setIgnoreCase(true);
467  return co;
468  } else if (cmd == QLatin1String("set-indent-mode")) {
471  co->setItems(l);
472  co->setIgnoreCase(true);
473  return co;
474  }
475 
476  return nullptr;
477 }
478 // END CoreCommands
479 
480 // BEGIN Character
481 KateCommands::Character *KateCommands::Character::m_instance = nullptr;
482 
484 {
485  if (cmd.trimmed() == QLatin1String("char")) {
486  msg = i18n(
487  "<p> char <b>identifier</b> </p>"
488  "<p>This command allows you to insert literal characters by their numerical identifier, in decimal, octal or hexadecimal form.</p>"
489  "<p>Examples:<ul>"
490  "<li>char <b>234</b></li>"
491  "<li>char <b>0x1234</b></li>"
492  "</ul></p>");
493  return true;
494  }
495  return false;
496 }
497 
499 {
500  QString cmd = _cmd;
501 
502  // hex, octal, base 9+1
503  QRegularExpression num(QStringLiteral("^char *(0?x[0-9A-Fa-f]{1,4}|0[0-7]{1,6}|[0-9]{1,5})$"));
504  QRegularExpressionMatch match = num.match(cmd);
505  if (!match.hasMatch()) {
506  return false;
507  }
508 
509  cmd = match.captured(1);
510 
511  // identify the base
512 
513  unsigned short int number = 0;
514  int base = 10;
515  if (cmd.startsWith(QLatin1Char('x'))) {
516  cmd.remove(0, 1);
517  base = 16;
518  } else if (cmd.startsWith(QLatin1String("0x"))) {
519  cmd.remove(0, 2);
520  base = 16;
521  } else if (cmd[0] == QLatin1Char('0')) {
522  base = 8;
523  }
524  bool ok;
525  number = cmd.toUShort(&ok, base);
526  if (!ok || number == 0) {
527  return false;
528  }
529  if (number <= 255) {
530  char buf[2];
531  buf[0] = (char)number;
532  buf[1] = 0;
533 
534  view->document()->insertText(view->cursorPosition(), QString::fromLatin1(buf));
535  } else {
536  // do the unicode thing
537  QChar c(number);
538 
539  view->document()->insertText(view->cursorPosition(), QString(&c, 1));
540  }
541 
542  return true;
543 }
544 
545 // END Character
546 
547 // BEGIN Date
548 KateCommands::Date *KateCommands::Date::m_instance = nullptr;
549 
551 {
552  if (cmd.trimmed() == QLatin1String("date")) {
553  msg = i18n(
554  "<p>date or date <b>format</b></p>"
555  "<p>Inserts a date/time string as defined by the specified format, or the format yyyy-MM-dd hh:mm:ss if none is specified.</p>"
556  "<p>Possible format specifiers are:"
557  "<table>"
558  "<tr><td>d</td><td>The day as number without a leading zero (1-31).</td></tr>"
559  "<tr><td>dd</td><td>The day as number with a leading zero (01-31).</td></tr>"
560  "<tr><td>ddd</td><td>The abbreviated localized day name (e.g. 'Mon'..'Sun').</td></tr>"
561  "<tr><td>dddd</td><td>The long localized day name (e.g. 'Monday'..'Sunday').</td></tr>"
562  "<tr><td>M</td><td>The month as number without a leading zero (1-12).</td></tr>"
563  "<tr><td>MM</td><td>The month as number with a leading zero (01-12).</td></tr>"
564  "<tr><td>MMM</td><td>The abbreviated localized month name (e.g. 'Jan'..'Dec').</td></tr>"
565  "<tr><td>yy</td><td>The year as two digit number (00-99).</td></tr>"
566  "<tr><td>yyyy</td><td>The year as four digit number (1752-8000).</td></tr>"
567  "<tr><td>h</td><td>The hour without a leading zero (0..23 or 1..12 if AM/PM display).</td></tr>"
568  "<tr><td>hh</td><td>The hour with a leading zero (00..23 or 01..12 if AM/PM display).</td></tr>"
569  "<tr><td>m</td><td>The minute without a leading zero (0..59).</td></tr>"
570  "<tr><td>mm</td><td>The minute with a leading zero (00..59).</td></tr>"
571  "<tr><td>s</td><td>The second without a leading zero (0..59).</td></tr>"
572  "<tr><td>ss</td><td>The second with a leading zero (00..59).</td></tr>"
573  "<tr><td>z</td><td>The milliseconds without leading zeroes (0..999).</td></tr>"
574  "<tr><td>zzz</td><td>The milliseconds with leading zeroes (000..999).</td></tr>"
575  "<tr><td>AP</td><td>Use AM/PM display. AP will be replaced by either \"AM\" or \"PM\".</td></tr>"
576  "<tr><td>ap</td><td>Use am/pm display. ap will be replaced by either \"am\" or \"pm\".</td></tr>"
577  "</table></p>");
578  return true;
579  }
580  return false;
581 }
582 
584 {
585  if (!cmd.startsWith(QLatin1String("date"))) {
586  return false;
587  }
588 
589  if (QDateTime::currentDateTime().toString(cmd.mid(5, cmd.length() - 5)).length() > 0) {
590  view->document()->insertText(view->cursorPosition(), QDateTime::currentDateTime().toString(cmd.mid(5, cmd.length() - 5)));
591  } else {
592  view->document()->insertText(view->cursorPosition(), QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")));
593  }
594 
595  return true;
596 }
597 
598 // END Date
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
QString captured(int nth) const const
QString toString(Qt::DateFormat format) const const
This KTextEditor::Command provides access to a lot of the core functionality of kate part...
Definition: katecmds.h:30
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
insert the current date/time in the given format
Definition: katecmds.h:155
bool exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &range=KTextEditor::Range(-1,-0,-1, 0)) override
execute command
Definition: katecmds.cpp:583
virtual Document * document() const =0
Get the view&#39;s document, that means the view is a view of the returned document.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool help(class KTextEditor::View *, const QString &, QString &) override
This command does not have help.
Definition: katecmds.cpp:48
QString & remove(int position, int n)
static QStringList listIdentifiers()
List all possible names, i.e.
KCompletion * completionObject(KTextEditor::View *, const QString &) override
override from KTextEditor::Command
Definition: katecmds.cpp:446
The Cursor represents a position in a Document.
Definition: cursor.h:71
constexpr bool isValid() const Q_DECL_NOEXCEPT
Validity check.
insert a unicode or ascii character base 9+1: 1234 hex: 0x1234 or x1234 octal: 01231 ...
Definition: katecmds.h:116
bool exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg)
execute command
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool hasMatch() const const
virtual Cursor cursorPosition() const =0
Get the view&#39;s current cursor position.
Range is folded away.
virtual void setIgnoreCase(bool ignoreCase)
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
An object representing a section of text, from one Cursor to another.
Range is persistent, e.g.
QString toLower() const const
SkipEmptyParts
bool exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &range=KTextEditor::Range(-1,-0,-1, 0)) override
execute command
Definition: katecmds.cpp:498
bool help(class KTextEditor::View *, const QString &, QString &) override
This command does not have help.
Definition: katecmds.cpp:550
QString i18n(const char *text, const TYPE &arg...)
QDateTime currentDateTime()
QString mid(int position, int n) const const
constexpr Cursor end() const Q_DECL_NOEXCEPT
Get the end position of this range.
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:185
int length() const const
QString fromLatin1(const char *str, int size)
A KCompletion object that completes last ?unquoted? word in the string passed.
Definition: katecmd.h:56
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:143
int size() const const
virtual void setItems(const QStringList &itemList)
bool help(class KTextEditor::View *, const QString &, QString &) override
This command does not have help.
Definition: katecmds.cpp:483
bool supportsRange(const QString &range) override
Find out if a given command can act on a range.
Definition: katecmds.cpp:435
ushort toUShort(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Sep 19 2020 22:57:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.