Skip to content

Commit

Permalink
Add support for --insert-text (#1022)
Browse files Browse the repository at this point in the history
Use `texworks --insert-text "..."` to insert some text into the top-most
open editor window. This should help support pushing content from
external programs such as, e.g., bibliography managers.

Note that this only works if an editor window is already open. It
effectively does nothing when there is no editor window open yet (e.g.,
if TeXworks was not running before).
  • Loading branch information
stloeffler committed Jan 12, 2024
1 parent d693571 commit c868937
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/InterProcessCommunicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class InterProcessCommunicator : public QObject

void sendBringToFront();
void sendOpenFile(const QString & path, const int position = -1);
void sendInsertText(const QString & text);

signals:
void receivedBringToFront();
void receivedOpenFile(const QString & path, const int position);
void receivedInsertText(const QString & text);
};

} // namespace Tw
Expand Down
13 changes: 13 additions & 0 deletions src/InterProcessCommunicatorDBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public slots:
Q_Q(InterProcessCommunicator);
emit q->receivedBringToFront();
}
Q_NOREPLY void insertText(QString text) {
Q_Q(InterProcessCommunicator);
emit q->receivedInsertText(text);
}
};

InterProcessCommunicator::InterProcessCommunicator()
Expand Down Expand Up @@ -120,6 +124,15 @@ void InterProcessCommunicator::sendOpenFile(const QString & path, const int posi
d->interface->call(QStringLiteral("openFile"), path, position);
}

void InterProcessCommunicator::sendInsertText(const QString &text)
{
Q_D(InterProcessCommunicator);
if (d->interface == nullptr || !d->interface->isValid()) {
return;
}
d->interface->call(QStringLiteral("insertText"), text);
}


} // namespace Tw

Expand Down
5 changes: 5 additions & 0 deletions src/InterProcessCommunicatorDummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ void InterProcessCommunicator::sendOpenFile(const QString & path, const int posi
emit receivedOpenFile(path, position);
}

void InterProcessCommunicator::sendInsertText(const QString & text)
{
emit receivedInsertText(text);
}

} // namespace Tw
19 changes: 19 additions & 0 deletions src/InterProcessCommunicatorWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Tw {
#define TW_HIDDEN_WINDOW_CLASS "TeXworks:MessageTarget"
#define TW_OPEN_FILE_MSG (('T' << 8) + 'W') // just a small sanity check for the receiver
#define TW_BRING_TO_FRONT_MSG (('B' << 8) + 'F') // just a small sanity check for the receiver
#define TW_INSERT_TEXT_MSG (('I' << 8) + 'T') // just a small sanity check for the receiver

LRESULT CALLBACK TW_HiddenWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

Expand Down Expand Up @@ -68,6 +69,9 @@ class InterProcessCommunicatorPrivate
emit q->receivedOpenFile(sl[0], sl[1].toInt());
break;
}
case TW_INSERT_TEXT_MSG:
emit q->receivedInsertText(QString::fromUtf8(data));
break;
default:
break;
}
Expand Down Expand Up @@ -189,4 +193,19 @@ void InterProcessCommunicator::sendOpenFile(const QString & path, const int posi
SendMessageA(hWnd, WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&cds));
}

void InterProcessCommunicator::sendInsertText(const QString & text)
{
HWND hWnd = InterProcessCommunicatorPrivate::findMessageWindow();
if (hWnd == NULL) {
return;
}

QByteArray ba = text.toUtf8();
COPYDATASTRUCT cds;
cds.dwData = TW_INSERT_TEXT_MSG;
cds.cbData = ba.length();
cds.lpData = ba.data();
SendMessageA(hWnd, WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&cds));
}

} // namespace Tw
18 changes: 18 additions & 0 deletions src/TWApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ TWApp::CommandLineData TWApp::processCommandLine()
Tw::Utils::CommandlineParser clp;
clp.registerSwitch(QString::fromLatin1("help"), tr("Display this message"), QString::fromLatin1("?"));
clp.registerOption(QString::fromLatin1("position"), tr("Open the following file at the given position (line or page)"), QString::fromLatin1("p"));
clp.registerOption(QStringLiteral("insert-text"), tr("Insert the given text in the top-most TeX editor window (only works if TeXworks is already running)"));
clp.registerSwitch(QString::fromLatin1("version"), tr("Display version information"), QString::fromLatin1("v"));

if (clp.parse()) {
Expand Down Expand Up @@ -345,6 +346,11 @@ There is NO WARRANTY, to the extent permitted by law.\n\n").arg(QString::fromLat
QTextStream strm(stdout);
clp.printUsage(strm);
}
if ((i = clp.getNextOption(QStringLiteral("insert-text"))) >= 0) {
Tw::Utils::CommandlineParser::CommandlineItem & item = clp.at(i);
item.processed = true;
retVal.insertText.append(item.value.toString());
}
}
return retVal;
}
Expand All @@ -359,11 +365,15 @@ bool TWApp::ensureSingleInstance(const CommandLineData &cld)
continue;
m_IPC.sendOpenFile(fi.absoluteFilePath(), fileToOpen.position);
}
if (!cld.insertText.isEmpty()) {
m_IPC.sendInsertText(cld.insertText);
}
exitLater(0);
return false;
}
QObject::connect(&m_IPC, &Tw::InterProcessCommunicator::receivedBringToFront, this, &TWApp::bringToFront);
QObject::connect(&m_IPC, &Tw::InterProcessCommunicator::receivedOpenFile, this, &TWApp::openFile);
QObject::connect(&m_IPC, &Tw::InterProcessCommunicator::receivedInsertText, this, &TWApp::insertText);
return true;
}

Expand Down Expand Up @@ -873,6 +883,14 @@ QObject* TWApp::openFile(const QString &fileName, int pos /* = 0 */)
return TeXDocumentWindow::openDocument(fileName, true, true, pos, 0, 0);
}

void TWApp::insertText(const QString &text)
{
TeXDocumentWindow * topTeXWin = qobject_cast<TeXDocumentWindow*>(topTeXWindow());
if (topTeXWin != nullptr) {
topTeXWin->textCursor().insertText(text);
}
}

void TWApp::preferences()
{
PrefsDialog::doPrefsDialog(activeWindow());
Expand Down
2 changes: 2 additions & 0 deletions src/TWApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class TWApp : public QApplication
public slots:
void bringToFront();
QObject* openFile(const QString& fileName, const int pos = -1);
void insertText(const QString & text);

void preferences();

Expand Down Expand Up @@ -252,6 +253,7 @@ private slots:
};
bool shouldContinue{true};
std::vector<fileToOpenStruct> filesToOpen;
QString insertText;
};

void init();
Expand Down

0 comments on commit c868937

Please sign in to comment.