• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • script
katecommandlinescript.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2009 Dominik Haumann <dhaumann kde org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "katecommandlinescript.h"
22 
23 #include <QScriptValue>
24 #include <QScriptEngine>
25 
26 #include <klocale.h>
27 
28 #include "katedocument.h"
29 #include "kateview.h"
30 #include "katecmd.h"
31 #include "kshell.h"
32 
33 KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
34  : KateScript(url)
35  , m_commandHeader(header)
36 {
37  KateCmd::self()->registerCommand (this);
38 }
39 
40 KateCommandLineScript::~KateCommandLineScript()
41 {
42  KateCmd::self()->unregisterCommand (this);
43 }
44 
45 const KateCommandLineScriptHeader& KateCommandLineScript::commandHeader()
46 {
47  return m_commandHeader;
48 }
49 
50 
51 bool KateCommandLineScript::callFunction(const QString& cmd, const QStringList args, QString &errorMessage)
52 {
53  clearExceptions();
54  QScriptValue command = function(cmd);
55  if(!command.isValid()) {
56  errorMessage = i18n("Function '%1' not found in script: %2", cmd, url());
57  return false;
58  }
59 
60  // add the arguments that we are going to pass to the function
61  QScriptValueList arguments;
62  foreach (const QString& arg, args) {
63  arguments << QScriptValue(m_engine, arg);
64  }
65 
66  QScriptValue result = command.call(QScriptValue(), arguments);
67  // error during the calling?
68  if(m_engine->hasUncaughtException()) {
69  errorMessage = backtrace(result, i18n("Error calling %1", cmd));
70  return false;
71  }
72 
73  return true;
74 }
75 
76 ScriptActionInfo KateCommandLineScript::actionInfo(const QString& cmd)
77 {
78  clearExceptions();
79  QScriptValue actionFunc = function("action");
80  if(!actionFunc.isValid()) {
81  kDebug() << i18n("Function 'action' not found in script: %1", url());
82  return ScriptActionInfo();
83  }
84 
85  // add the arguments that we are going to pass to the function
86  QScriptValueList arguments;
87  arguments << cmd;
88 
89  QScriptValue result = actionFunc.call(QScriptValue(), arguments);
90  // error during the calling?
91  if(m_engine->hasUncaughtException()) {
92  displayBacktrace(result, i18n("Error calling action(%1)", cmd));
93  return ScriptActionInfo();
94  }
95 
96  ScriptActionInfo info;
97  info.setCommand(cmd);
98  info.setText(result.property("text").toString());
99  info.setIcon(result.property("icon").toString());
100  info.setCategory(result.property("category").toString());
101  info.setInteractive(result.property("interactive").toBool());
102  info.setShortcut(result.property("shortcut").toString());
103 
104  return info;
105 }
106 
107 const QStringList& KateCommandLineScript::cmds()
108 {
109  return m_commandHeader.functions();
110 }
111 
112 bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg)
113 {
114  KShell::Errors errorCode;
115  QStringList args(KShell::splitArgs(cmd, KShell::NoOptions, &errorCode));
116 
117  if (errorCode != KShell::NoError) {
118  msg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", cmd);
119  return false;
120  }
121 
122  QString _cmd(args.first());
123  args.removeFirst();
124 
125  if (!view) {
126  msg = i18n("Could not access view");
127  return false;
128  }
129 
130  if (setView(qobject_cast<KateView*>(view))) {
131  // setView fails if the script cannot be loaded
132  // balance edit stack in any case!
133  qobject_cast<KateView*>(view)->doc()->pushEditState();
134  bool success = callFunction(_cmd, args, msg);
135  qobject_cast<KateView*>(view)->doc()->popEditState();
136  return success;
137  }
138 
139  return false;
140 }
141 
142 
143 bool KateCommandLineScript::exec(KTextEditor::View *view, const QString &cmd, QString &msg,
144  const KTextEditor::Range &range)
145 {
146  view->setSelection(range);
147  return exec( view, cmd, msg );
148 }
149 
150 bool KateCommandLineScript::supportsRange(const QString &)
151 {
152  return true;
153 }
154 
155 bool KateCommandLineScript::help(KTextEditor::View* view, const QString& cmd, QString &msg)
156 {
157  if (!setView(qobject_cast<KateView*>(view))) {
158  // setView fails, if the script cannot be loaded
159  return false;
160  }
161 
162  clearExceptions();
163  QScriptValue helpFunction = function("help");
164  if(!helpFunction.isValid()) {
165  return false;
166  }
167 
168  // add the arguments that we are going to pass to the function
169  QScriptValueList arguments;
170  arguments << QScriptValue(m_engine, cmd);
171 
172  QScriptValue result = helpFunction.call(QScriptValue(), arguments);
173 
174  // error during the calling?
175  if(m_engine->hasUncaughtException()) {
176  msg = backtrace(result, i18n("Error calling 'help %1'", cmd));
177  return false;
178  }
179 
180  if (result.isUndefined() || !result.isString()) {
181  kDebug(13050) << i18n("No help specified for command '%1' in script %2", cmd, url());
182  return false;
183  }
184  msg = result.toString();
185 
186  return !msg.isEmpty();
187 }
188 
189 // kate: space-indent on; indent-width 2; replace-tabs on;
kateview.h
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
KateCommandLineScript::help
virtual bool help(KTextEditor::View *view, const QString &cmd, QString &msg)
Definition: katecommandlinescript.cpp:155
KateCommandLineScript::cmds
virtual const QStringList & cmds()
Definition: katecommandlinescript.cpp:107
KateCmd::unregisterCommand
bool unregisterCommand(KTextEditor::Command *cmd)
Definition: katecmd.cpp:59
ScriptActionInfo::setText
void setText(const QString &text)
Definition: katecommandlinescript.h:56
KateScript
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:106
KateCommandLineScriptHeader
Definition: katecommandlinescript.h:32
QList::removeFirst
void removeFirst()
KateCmd::self
static KateCmd * self()
Definition: katecmd.cpp:112
QScriptValue
QScriptValue::isUndefined
bool isUndefined() const
katedocument.h
KateScript::backtrace
QString backtrace(const QScriptValue &error, const QString &header=QString())
Returns the backtrace when a script has errored out.
Definition: katescript.cpp:106
KateCommandLineScript::~KateCommandLineScript
virtual ~KateCommandLineScript()
Definition: katecommandlinescript.cpp:40
QScriptEngine::hasUncaughtException
bool hasUncaughtException() const
KateCommandLineScript::actionInfo
ScriptActionInfo actionInfo(const QString &cmd)
Definition: katecommandlinescript.cpp:76
QScriptValue::call
QScriptValue call(const QScriptValue &thisObject, const QScriptValueList &args)
QScriptValue::toString
QString toString() const
ScriptActionInfo::setIcon
void setIcon(const QString &icon)
Definition: katecommandlinescript.h:58
KateCommandLineScript::callFunction
bool callFunction(const QString &cmd, const QStringList args, QString &errorMessage)
Definition: katecommandlinescript.cpp:51
ScriptActionInfo::setShortcut
void setShortcut(const QString &shortcut)
Definition: katecommandlinescript.h:64
ScriptActionInfo::setCommand
void setCommand(const QString &command)
Definition: katecommandlinescript.h:54
KateCommandLineScriptHeader::functions
const QStringList & functions() const
Definition: katecommandlinescript.h:40
QScriptValue::toBool
bool toBool() const
katecommandlinescript.h
QString::isEmpty
bool isEmpty() const
ScriptActionInfo::setInteractive
void setInteractive(bool interactive)
Definition: katecommandlinescript.h:62
katecmd.h
QList::first
T & first()
ScriptActionInfo::setCategory
void setCategory(const QString &category)
Definition: katecommandlinescript.h:60
QString
KateScript::url
const QString & url()
The script's URL.
Definition: katescript.h:123
KateScript::clearExceptions
void clearExceptions()
Clears any uncaught exceptions in the script engine.
Definition: katescript.cpp:128
KateCommandLineScript::commandHeader
const KateCommandLineScriptHeader & commandHeader()
Definition: katecommandlinescript.cpp:45
QScriptValue::property
QScriptValue property(const QString &name, const ResolveFlags &mode) const
QStringList
KateView
Definition: kateview.h:77
KateScript::m_engine
QScriptEngine * m_engine
The Qt interpreter for this script.
Definition: katescript.h:188
QScriptValue::isString
bool isString() const
KateCommandLineScript::KateCommandLineScript
KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header)
Definition: katecommandlinescript.cpp:33
KateCommandLineScript::supportsRange
virtual bool supportsRange(const QString &cmd)
Definition: katecommandlinescript.cpp:150
KateScript::setView
bool setView(KateView *view)
set view for this script for the execution will trigger load!
Definition: katescript.cpp:219
KateScript::displayBacktrace
void displayBacktrace(const QScriptValue &error, const QString &header=QString())
Displays the backtrace when a script has errored out.
Definition: katescript.cpp:119
QScriptValue::isValid
bool isValid() const
KateCmd::registerCommand
bool registerCommand(KTextEditor::Command *cmd)
Definition: katecmd.cpp:38
ScriptActionInfo
Definition: katecommandlinescript.h:47
KateCommandLineScript::exec
virtual bool exec(KTextEditor::View *view, const QString &cmd, QString &msg)
Definition: katecommandlinescript.cpp:112
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal