Skip to content

Commit

Permalink
add new macro trigger scope
Browse files Browse the repository at this point in the history
(?inEnv:...) added
  • Loading branch information
sunderme committed Jan 11, 2025
1 parent 21e8cee commit cfb419d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/latexeditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ bool DefaultInputBinding::runMacros(QKeyEvent *event, QEditor *editor)
// workaround for #2866 (tab as trigger in macro on osx)
prev+="\t";
}
const LatexDocument *doc = qobject_cast<LatexDocument *>(editor->document());
StackEnvironment env;

foreach (const Macro &m, completerConfig->userMacros) {
if (!m.isActiveForTrigger(Macro::ST_REGEX)) continue;
if (!m.isActiveForLanguage(language)) continue;
Expand All @@ -133,6 +136,26 @@ bool DefaultInputBinding::runMacros(QKeyEvent *event, QEditor *editor)
}
}
}
QStringList envTriggers = m.getTriggerInEnvs();
if(!envTriggers.isEmpty()){
if(env.isEmpty()){
doc->getEnv(editor->cursor().lineNumber(),env);
}
// use topEnv as trigger env
const QStringList ignoreEnv = {"document","normal"};
if(!env.isEmpty() && !ignoreEnv.contains(env.top().name)){
QString envName=env.top().name;
QStringList envAliases = doc->lp->environmentAliases.values(envName);
bool aliasNotFound=std::none_of(envAliases.cbegin(),envAliases.cend(),[&envTriggers](const QString &alias){
return envTriggers.contains(alias);
});
if(!envTriggers.contains(envName)&& aliasNotFound){
continue;
}
} else {
continue;
}
}
const QRegularExpression &r = m.triggerRegex;
QRegularExpressionMatch match=r.match(prev);
if (match.hasMatch()) {
Expand Down
15 changes: 15 additions & 0 deletions src/usermacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ void Macro::init(const QString &nname, Macro::Type ntype, const QString &ntag, c
triggerFormatExcludesUnprocessed = realtrigger.mid(start, closing - start).replace(',', '|').replace(" ", ""); //handle later, when the formats are loaded
realtrigger.remove(0, closing + 1);
}
if (realtrigger.startsWith("(?inEnv:")) {
int start = realtrigger.indexOf(':') + 1;
int closing = realtrigger.indexOf(")");
QString envs= realtrigger.mid(start, closing - start).replace(',', '|').replace(" ", ""); //handle later, when the formats are loaded
triggerInEnvs = envs.split("|");
realtrigger.remove(0, closing + 1);
}

} while (lastLen != realtrigger.length());

Expand Down Expand Up @@ -274,6 +281,14 @@ QList<int> Macro::getFormatExcludeTriggers() const
{
return triggerFormatExcludes;
}
/*!
* \brief return the list of environment trigger
* \return
*/
QStringList Macro::getTriggerInEnvs() const
{
return triggerInEnvs;
}

bool Macro::save(const QString &fileName) const {
QFile file(fileName);
Expand Down
3 changes: 3 additions & 0 deletions src/usermacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Macro
bool hasFormatTriggers() const;
QList<int>getFormatTriggers() const;
QList<int>getFormatExcludeTriggers() const;
QStringList getTriggerInEnvs() const;

bool load(const QString &fileName);
bool loadFromText(const QString &text);
Expand All @@ -79,6 +80,8 @@ class Macro
QString triggerFormatExcludesUnprocessed;
QList<int> triggerFormatExcludes;

QStringList triggerInEnvs;

QString m_shortcut;
};

Expand Down
2 changes: 1 addition & 1 deletion src/usermenudialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ void UserMenuDialog::abbrevChanged()
void UserMenuDialog::triggerChanged()
{
// check if trigger is a valid regex
const QStringList fixedTriggers{"?txs-start","?new-file","?new-from-template","?load-file","?load-this-file","?save-file","?close-file","?master-changed","?after-typeset","?after-command-run","?highlighted-as","?not-highlighted-as","?language"};
const QStringList fixedTriggers{"?txs-start","?new-file","?new-from-template","?load-file","?load-this-file","?save-file","?close-file","?master-changed","?after-typeset","?after-command-run","?highlighted-as","?not-highlighted-as","?language","?inEnv"};
const QString text=ui.triggerEdit->text();
QRegularExpression re(text);
bool fixedTriggerFound=std::any_of(fixedTriggers.constBegin(),fixedTriggers.constEnd(),[text](const QString elem){return text.contains(elem);});
Expand Down
2 changes: 2 additions & 0 deletions utilities/manual/build/html/CHANGELOG.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ <h2>TeXstudio 4.8.6<a class="headerlink" href="#texstudio-4-8-6" title="Link to
<li><p>hide spelling errors in picture envs like tikzpicture</p></li>
<li><p>better handle \newtcbox <a class="reference external" href="https://github.com/texstudio-org/texstudio/issues/3878">#3878</a></p></li>
<li><p>fix rare crash <a class="reference external" href="https://github.com/texstudio-org/texstudio/issues/3936">#3936</a></p></li>
<li><p>repair user macro triger (?highlight-as:…), still not usable for math env detection</p></li>
<li><p>add new user macro trigger (?inEnv:…)</p></li>
</ul>
</section>
<section id="texstudio-4-8-5">
Expand Down
3 changes: 3 additions & 0 deletions utilities/manual/build/html/advanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,9 @@ <h4>Limitation of Scope<a class="headerlink" href="#limitation-of-scope" title="
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">(?not-highlighted-as:...)</span></code></p></td>
<td><p>Similar to <code class="docutils literal notranslate"><span class="pre">(?highlighted-as:...)</span></code>, but the macro is deactivated in the given environments.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">(?inEnv:...)</span></code></p></td>
<td><p>Restrict the macro to certain environments. Only the latest environment is checked in case of nested environments. Environments are only detected in the lines between “\begin”/”\end”, excluding the lines that conatin those commands! It also checks for aliases, i.e. all math type environments are aliased as “math” as well.<br>Example: <code class="docutils literal notranslate"><span class="pre">(?inEnv:math)</span></code></p></td>
</tr>
</tbody>
</table>
</div>
Expand Down
2 changes: 1 addition & 1 deletion utilities/manual/build/html/searchindex.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions utilities/manual/source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- hide spelling errors in picture envs like tikzpicture
- better handle \newtcbox [#3878](https://github.com/texstudio-org/texstudio/issues/3878)
- fix rare crash [#3936](https://github.com/texstudio-org/texstudio/issues/3936)
- repair user macro triger (?highlight-as:...), still not usable for math env detection
- add new user macro trigger (?inEnv:...)

## TeXstudio 4.8.5

Expand Down
1 change: 1 addition & 0 deletions utilities/manual/source/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ expression of the pattern `(?[scope-type]:...)`.
| `(?language:...)` | The macro is only active if the highlighting of the document matches the given language.<br> Example: `(?language:latex)` |
| `(?highlighted-as:...)` | Restrict the macro to certain highlighted environments. The possible values correspond to the list on the syntax highlighting config page.<br>Example: `(?highlighted-as:numbers,math-delimiter,math-keyword)` |
| `(?not-highlighted-as:...)` | Similar to `(?highlighted-as:...)`, but the macro is deactivated in the given environments. |
| `(?inEnv:...)` | Restrict the macro to certain environments. Only the latest environment is checked in case of nested environments. Environments are only detected in the lines between "\begin"/"\end", excluding the lines that conatin those commands! It also checks for aliases, i.e. all math type environments are aliased as "math" as well.<br>Example: `(?inEnv:math)` |
You may combine `(?language:...)` and `(?highlighted-as:...)`
expressions. However, combing `(?highlighted-as:...)` and
Expand Down

0 comments on commit cfb419d

Please sign in to comment.