# How to restrict TexStudio macro script to work only in text/normal mode?

I understand that you can write (?highlighted-as:numbers,math-delimiter,math-keyword) before your trigger and TexStudio will execute the macro only in Math mode. I already tested that in "scripts type" and "normal type" and it works fine as expected. Somewhere I forgot where I read that (?highlighted-as:normal) works in a comparable way in normal/text mode type, i.e. if written before a trigger, it will restrict/limit the scope of the macro to only normal/text mode. I was trying to implement this in a script, but it does not work. It basically runs the Javascript in both math and normal/text mode, not what I want.

Example of what I have been trying to do:

Name: foobar replacement
Trigger (?highlighted-as:normal) foo
type: script
%SCRIPT
replace = {' foo ':' bar '}
trigger=triggerMatches
if (trigger in replace) {trigger=replace[trigger]}
editor.write(trigger)


This script will run in both math and normal mode. I need it to only run in text/normal mode. What am I doing wrong? Of course I could do each replacement without the script type, just normal type, but it will be very tedious to write a macro for each replacement one by one. Is there anyway to tell TexStudio to execute this script in normal mode only? Or is there anyway while writing a script to get the current mode (whether it's math/normal text, true/false?) and then write an IF statement to execute a certain set of commands if in one mode and a different set of commands if in a different mode?

Sorry for these questions if they are simple or "basics" for some of you. I am newbie to LaTeX and TexStudio and I am just trying to write some scripts in TexStudio to make my life a bit easier, no intention at the moment to delve deeper "under the hood" into LaTeX like a pro and use \ifmmode and such. Thanks very much in advance!

• Thanks for the formatting! my question looks much better. I appreciate it. – GPL May 6 '15 at 3:17
• Maybe get to option windown and go to configure TexStudio and click on autocomplete ... or you can write LaTeX code in any text editor like kate :v – juanuni May 6 '15 at 3:20
• You're welcome. I wasn't really sure whether it was meant to be done this way or not. But at least it looks like code. By the way, I doubt the problem here will be that your questions are too simple/basic. But you will need somebody who knows that particular editor. – cfr May 6 '15 at 3:20
• @juanuni I guess the OP has already got that far. Did you read the post? – cfr May 6 '15 at 3:21
• Oh, my bad ... I read bad the words ... – juanuni May 6 '15 at 3:38

I have not found a "direct" way to do this, but I found something which accomplishes such task using RegEx. The good news is that TexStudio uses javascript regex in its "trigger" and that allow me to construct a trigger that can be only executed upon meeting the criteria of my RegEx. One practical example is I want the double space "" to be interpreted as end of line in a regular "text" mode and therefore put ". \\" and moves the cursor to the next line, while I want the same double double space "" in a LaTeX enumeration environment such as description to be interpreted as "\\" then moves the cursor to the next line inside the brackets of "\item[]".
This is how I did it in TexStudio:

macro for new item in description environment if the line begins with \item
Name: next itm
trigger: (?<=^\\item.* )
type: normal
LaTeX content
\\
\item[%|]

Macro for a newline if the line "does not" begin with "\item" (i.e. it is a regular text)
Name: new line
trigger: (?<=^[^\\item].* )
type: normal
LaTeX content
. \\
%|

Note that the double space after ".*" is significant if you want to make your trigger just a double tabs on space-bar. You may need to delete the brackets in \item[%|] to be just \item %| if you don't want to use description as your enumeration environment. Limitations:you will need to explicitly exclude all instances in which you are using the trigger of double space to mean something other than newline.
Please feel free to correct me or this answer. I am by no mean "expert" like some of you guys.
Glad to explain the regex trigger I used here if someone wants me to.