Commit 1d1a5a70 authored by con's avatar con
Browse files

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

parent 3ecced27
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
<category xml:lang="de">Text</category> <category xml:lang="de">Text</category>
<executable output="reloaddocument"> <executable output="reloaddocument">
<path>xterm</path> <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> <workingdirectory>%{CurrentDocument:Path}</workingdirectory>
</executable> </executable>
</externaltool> </externaltool>
...@@ -39,10 +39,51 @@ ...@@ -39,10 +39,51 @@
<executable output="reloaddocument"> <executable output="reloaddocument">
<path>osascript</path> <path>osascript</path>
<input> <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" 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 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 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 end tell
</input> </input>
<workingdirectory>%{CurrentDocument:Path}</workingdirectory> <workingdirectory>%{CurrentDocument:Path}</workingdirectory>
......
...@@ -2141,6 +2141,8 @@ void EditorManager::updateVariable(const QString &variable) ...@@ -2141,6 +2141,8 @@ void EditorManager::updateVariable(const QString &variable)
{ {
static const char * const kCurrentDocumentFilePath = "CurrentDocument:FilePath"; static const char * const kCurrentDocumentFilePath = "CurrentDocument:FilePath";
static const char * const kCurrentDocumentPath = "CurrentDocument:Path"; 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) if (variable == QLatin1String(kCurrentDocumentFilePath)
|| variable == QLatin1String(kCurrentDocumentPath)) { || variable == QLatin1String(kCurrentDocumentPath)) {
QString value; QString value;
...@@ -2155,5 +2157,19 @@ void EditorManager::updateVariable(const QString &variable) ...@@ -2155,5 +2157,19 @@ void EditorManager::updateVariable(const QString &variable)
} }
} }
VariableManager::instance()->insert(variable, value); 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() ...@@ -332,6 +332,10 @@ void ExternalToolRunner::run()
deleteLater(); deleteLater();
return; 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; m_process = new QProcess;
// TODO error handling, finish reporting, reading output, etc // TODO error handling, finish reporting, reading output, etc
connect(m_process, SIGNAL(started()), this, SLOT(started())); connect(m_process, SIGNAL(started()), this, SLOT(started()));
...@@ -359,6 +363,9 @@ void ExternalToolRunner::finished() ...@@ -359,6 +363,9 @@ void ExternalToolRunner::finished()
if (m_tool->outputHandling() == ExternalTool::ReplaceSelection if (m_tool->outputHandling() == ExternalTool::ReplaceSelection
|| m_tool->errorHandling() == ExternalTool::ReplaceSelection) { || m_tool->errorHandling() == ExternalTool::ReplaceSelection) {
emit ExternalToolManager::instance()->replaceSelectionRequested(m_processOutput); 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( ICore::instance()->messageManager()->printToOutputPane(
tr("'%1' finished").arg(m_resolvedExecutable), false); tr("'%1' finished").arg(m_resolvedExecutable), false);
......
...@@ -5799,6 +5799,16 @@ int BaseTextEditorEditable::currentColumn() const ...@@ -5799,6 +5799,16 @@ int BaseTextEditorEditable::currentColumn() const
return cursor.position() - cursor.block().position() + 1; 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 QRect BaseTextEditorEditable::cursorRect(int pos) const
{ {
QTextCursor tc = e->textCursor(); QTextCursor tc = e->textCursor();
...@@ -6044,7 +6054,7 @@ void BaseTextEditor::handleBlockSelection(int diff_row, int diff_col) ...@@ -6044,7 +6054,7 @@ void BaseTextEditor::handleBlockSelection(int diff_row, int diff_col)
viewport()->update(); viewport()->update();
// ### TODO ensure horizontal visibility // ### TODO ensure horizontal visibility
// const bool rtl = q->isRightToLeft(); // const bool rtl = q->isRightToLeft();
// if (cr.left() < visible.left() || cr.right() > visible.right()) { // if (cr.left() < visible.left() || cr.right() > visible.right()) {
// int x = cr.center().x() + horizontalOffset() - visible.width()/2; // int x = cr.center().x() + horizontalOffset() - visible.width()/2;
...@@ -6052,3 +6062,15 @@ void BaseTextEditor::handleBlockSelection(int diff_row, int diff_col) ...@@ -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: ...@@ -205,6 +205,9 @@ public:
void setVisibleWrapColumn(int column); void setVisibleWrapColumn(int column);
int visibleWrapColumn() const; int visibleWrapColumn() const;
int columnCount() const;
int rowCount() const;
void setActionHack(QObject *); void setActionHack(QObject *);
QObject *actionHack() const; QObject *actionHack() const;
...@@ -568,6 +571,8 @@ public: ...@@ -568,6 +571,8 @@ public:
int currentLine() const; int currentLine() const;
int currentColumn() const; int currentColumn() const;
void gotoLine(int line, int column = 0) { e->gotoLine(line, column); } void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
int columnCount() const;
int rowCount() const;
inline int position( inline int position(
ITextEditor::PositionOperation posOp = ITextEditor::Current ITextEditor::PositionOperation posOp = ITextEditor::Current
......
...@@ -106,6 +106,8 @@ public: ...@@ -106,6 +106,8 @@ public:
virtual int position(PositionOperation posOp = Current, int at = -1) const = 0; virtual int position(PositionOperation posOp = Current, int at = -1) const = 0;
virtual void convertPosition(int pos, int *line, int *column) const = 0; virtual void convertPosition(int pos, int *line, int *column) const = 0;
virtual QRect cursorRect(int pos = -1) 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 contents() const = 0;
virtual QString selectedText() const = 0; virtual QString selectedText() const = 0;
......
...@@ -216,16 +216,40 @@ void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings) ...@@ -216,16 +216,40 @@ void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
void TextEditorPlugin::updateVariable(const QString &variable) void TextEditorPlugin::updateVariable(const QString &variable)
{ {
static const char * const kCurrentDocumentSelection= "CurrentDocument:Selection"; static const char * const kCurrentDocumentSelection = "CurrentDocument:Selection";
if (variable == QLatin1String(kCurrentDocumentSelection)) { static const char * const kCurrentDocumentRow = "CurrentDocument:Row";
QString selectedText; 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(); Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
ITextEditor *editor = qobject_cast<ITextEditor *>(iface); ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
if (editor) { if (editor) {
selectedText = editor->selectedText(); if (variable == QLatin1String(kCurrentDocumentSelection)) {
selectedText.replace(QChar::ParagraphSeparator, QLatin1String("\n")); 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