Commit 1d1a5a70 authored by con's avatar con

Continue on fancy open-vi-in-Terminal AppleScript.

parent 3ecced27
......@@ -38,7 +38,7 @@
<category xml:lang="de">Text</category>
<executable output="reloaddocument">
<path>xterm</path>
<arguments>-geom %{CurrentDocument:Width}x%{CurrentDocument:Height}+%{CurrentDocument:XPos}+%{CurrentDocument:YPos} -e vi %{CurrentDocument:FilePath} +%{CurrentDocument:Line} +"normal %{CurrentDocument:Column}|"</arguments>
<arguments>-geom %{CurrentDocument:Width}x%{CurrentDocument:Height}+%{CurrentDocument:XPos}+%{CurrentDocument:YPos} -e vi \"%{CurrentDocument:FilePath}\" +%{CurrentDocument:Row} +"normal %{CurrentDocument:Column}|"</arguments>
<workingdirectory>%{CurrentDocument:Path}</workingdirectory>
</executable>
</externaltool>
......@@ -39,10 +39,51 @@
<executable output="reloaddocument">
<path>osascript</path>
<input>
--Terminal opens a window by default when it is not running, so check
on applicationIsRunning(applicationName)
tell application "System Events" to count (every process whose name is applicationName)
return result is greater than 0
end applicationIsRunning
set terminalWasRunning to applicationIsRunning("Terminal")
set editorScript to "vi \"%{CurrentDocument:FilePath}\" +%{CurrentDocument:Row} +\"normal %{CurrentDocument:Column}|\"; exit"
tell application "Terminal"
do script "vi \"%{CurrentDocument:FilePath}\"; exit"
--do script will open a new window if none given, but terminal already opens one if not running
if terminalWasRunning then
do script editorScript
else
do script editorScript in first window
end if
set currentTab to the result
set currentWindow to first window whose tabs contains currentTab
--set the geometry
set font size of current settings of currentTab to %{CurrentDocument:FontSize}
set number of columns of currentTab to %{CurrentDocument:ColumnCount}
set number of rows of currentTab to %{CurrentDocument:RowCount}
set position of currentWindow to {%{CurrentDocument:XPos}, %{CurrentDocument:YPos}}
activate
--make sure that the command has been called before waiting for it to end
try --window might have closed
repeat until currentTab's history contains editorScript
delay 0.2
end repeat
on error
return
end try
--wait for the command to end and close the window
repeat
delay 0.2
try --window might have closed
if busy of currentTab is false then
exit repeat
end if
on error
return
end try
end repeat
try --window might have closed
close currentWindow
end try
end tell
</input>
<workingdirectory>%{CurrentDocument:Path}</workingdirectory>
......
......@@ -2141,6 +2141,8 @@ void EditorManager::updateVariable(const QString &variable)
{
static const char * const kCurrentDocumentFilePath = "CurrentDocument:FilePath";
static const char * const kCurrentDocumentPath = "CurrentDocument:Path";
static const char * const kCurrentDocumentXPos = "CurrentDocument:XPos";
static const char * const kCurrentDocumentYPos = "CurrentDocument:YPos";
if (variable == QLatin1String(kCurrentDocumentFilePath)
|| variable == QLatin1String(kCurrentDocumentPath)) {
QString value;
......@@ -2155,5 +2157,19 @@ void EditorManager::updateVariable(const QString &variable)
}
}
VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentXPos)) {
QString value;
IEditor *curEditor = currentEditor();
if (curEditor) {
value = QString::number(curEditor->widget()->mapToGlobal(QPoint(0,0)).x());
}
VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentYPos)) {
QString value;
IEditor *curEditor = currentEditor();
if (curEditor) {
value = QString::number(curEditor->widget()->mapToGlobal(QPoint(0,0)).y());
}
VariableManager::instance()->insert(variable, value);
}
}
......@@ -332,6 +332,10 @@ void ExternalToolRunner::run()
deleteLater();
return;
}
if (m_tool->outputHandling() == ExternalTool::ReloadDocument
|| m_tool->errorHandling() == ExternalTool::ReloadDocument) {
// TODO ask modified file to save, block modification notifications
}
m_process = new QProcess;
// TODO error handling, finish reporting, reading output, etc
connect(m_process, SIGNAL(started()), this, SLOT(started()));
......@@ -359,6 +363,9 @@ void ExternalToolRunner::finished()
if (m_tool->outputHandling() == ExternalTool::ReplaceSelection
|| m_tool->errorHandling() == ExternalTool::ReplaceSelection) {
emit ExternalToolManager::instance()->replaceSelectionRequested(m_processOutput);
} else if (m_tool->outputHandling() == ExternalTool::ReloadDocument
|| m_tool->errorHandling() == ExternalTool::ReloadDocument) {
// TODO reload document without popup
}
ICore::instance()->messageManager()->printToOutputPane(
tr("'%1' finished").arg(m_resolvedExecutable), false);
......
......@@ -5799,6 +5799,16 @@ int BaseTextEditorEditable::currentColumn() const
return cursor.position() - cursor.block().position() + 1;
}
int BaseTextEditorEditable::columnCount() const
{
return e->columnCount();
}
int BaseTextEditorEditable::rowCount() const
{
return e->rowCount();
}
QRect BaseTextEditorEditable::cursorRect(int pos) const
{
QTextCursor tc = e->textCursor();
......@@ -6044,7 +6054,7 @@ void BaseTextEditor::handleBlockSelection(int diff_row, int diff_col)
viewport()->update();
// ### TODO ensure horizontal visibility
// ### TODO ensure horizontal visibility
// const bool rtl = q->isRightToLeft();
// if (cr.left() < visible.left() || cr.right() > visible.right()) {
// int x = cr.center().x() + horizontalOffset() - visible.width()/2;
......@@ -6052,3 +6062,15 @@ void BaseTextEditor::handleBlockSelection(int diff_row, int diff_col)
// }
}
int BaseTextEditor::columnCount() const
{
QFontMetricsF fm(font());
return viewport()->rect().width() / fm.width(QLatin1Char('x'));
}
int BaseTextEditor::rowCount() const
{
QFontMetricsF fm(font());
return viewport()->rect().height() / fm.lineSpacing();
}
......@@ -205,6 +205,9 @@ public:
void setVisibleWrapColumn(int column);
int visibleWrapColumn() const;
int columnCount() const;
int rowCount() const;
void setActionHack(QObject *);
QObject *actionHack() const;
......@@ -568,6 +571,8 @@ public:
int currentLine() const;
int currentColumn() const;
void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
int columnCount() const;
int rowCount() const;
inline int position(
ITextEditor::PositionOperation posOp = ITextEditor::Current
......
......@@ -106,6 +106,8 @@ public:
virtual int position(PositionOperation posOp = Current, int at = -1) const = 0;
virtual void convertPosition(int pos, int *line, int *column) const = 0;
virtual QRect cursorRect(int pos = -1) const = 0;
virtual int columnCount() const = 0;
virtual int rowCount() const = 0;
virtual QString contents() const = 0;
virtual QString selectedText() const = 0;
......
......@@ -216,16 +216,40 @@ void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
void TextEditorPlugin::updateVariable(const QString &variable)
{
static const char * const kCurrentDocumentSelection= "CurrentDocument:Selection";
if (variable == QLatin1String(kCurrentDocumentSelection)) {
QString selectedText;
static const char * const kCurrentDocumentSelection = "CurrentDocument:Selection";
static const char * const kCurrentDocumentRow = "CurrentDocument:Row";
static const char * const kCurrentDocumentColumn = "CurrentDocument:Column";
static const char * const kCurrentDocumentRowCount = "CurrentDocument:RowCount";
static const char * const kCurrentDocumentColumnCount = "CurrentDocument:ColumnCount";
static const char * const kCurrentDocumentFontSize = "CurrentDocument:FontSize";
static QSet<QString> variables = QSet<QString>()
<< QString::fromLatin1(kCurrentDocumentSelection)
<< QString::fromLatin1(kCurrentDocumentRow)
<< QString::fromLatin1(kCurrentDocumentColumn)
<< QString::fromLatin1(kCurrentDocumentRowCount)
<< QString::fromLatin1(kCurrentDocumentColumnCount)
<< QString::fromLatin1(kCurrentDocumentFontSize);
if (variables.contains(variable)) {
QString value;
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
if (editor) {
selectedText = editor->selectedText();
selectedText.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
if (variable == QLatin1String(kCurrentDocumentSelection)) {
value = editor->selectedText();
value.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
} else if (variable == QLatin1String(kCurrentDocumentRow)) {
value = QString::number(editor->currentLine());
} else if (variable == QLatin1String(kCurrentDocumentColumn)) {
value = QString::number(editor->currentColumn());
} else if (variable == QLatin1String(kCurrentDocumentRowCount)) {
value = QString::number(editor->rowCount());
} else if (variable == QLatin1String(kCurrentDocumentColumnCount)) {
value = QString::number(editor->columnCount());
} else if (variable == QLatin1String(kCurrentDocumentFontSize)) {
value = QString::number(editor->widget()->font().pointSize());
}
}
Core::VariableManager::instance()->insert(variable, selectedText);
Core::VariableManager::instance()->insert(variable, value);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment