NppExec ver. 0.4 introduces built-in calculator abilities provided by Function Parser (fparser). To calculate your math expression, use the special form of the SET command:
set result ~ <math expression>
For example:
set result ~ sin(pi/4) + cos(pi/4)
If this example gives you an error "- fparser calc error: Syntax error: Unknown identifier at pos 4", it means you do not have necessary NppExec files in your "Notepad++\plugins\NppExec" folder. In such case, you need to download the latest NppExec package manually and unpack it to your "Notepad++\plugins" folder [2.3].
You can find more details regarding fparser's syntax and abilities inside the file "Notepad++\plugins\doc\NppExec\fparser.html".
By the way, NppExec also uses fparser for its own purposes such as internal implementation of NPP_SENDMSG and SCI_SENDMSG. In particular, fparser is used to calculate and store the values of constants defined inside "Notepad_plus_msgs.h", "Scintilla.h" and other files in the "Notepad++\plugins\NppExec" folder. But this is just for information; never mind :)
There are two special cases of this form of the SET command which are processed by NppExec itself. I am talking about
set n ~ strlen <string>
and
set n ~ strlenutf8 <string>
These two functions calculate an ANSI and an UTF-8 string length. For Latin characters, both 'strlen' and 'strlenutf8' will return the same value. But for non-Latin characters (such as Cyrillic), 'strlenutf8' will return a greater value than 'strlen' because each non-Latin character in UTF-8 may be represented by 2 or 3 bytes (unlike Latin characters which are always represented by 1 byte in UTF-8). Actually, 'strlenutf8' returns the number of bytes in an UTF-8 string. This number has to be specified for some Scintilla's messages invoked via SCI_SENDMSG. Type "help sci_sendmsg" in NppExec's Console for more details regarding SCI_SENDMSG.
Here is an example which uses 'strlen' and SCI_SENDMSG:
set s = My string set n ~ strlen $(s) SCI_SENDMSG SCI_APPENDTEXT $(n) "$(s)"
If you want to add spaces before and after "My string" (e.g. to have " My string "), you should modify the script since NppExec removes leading and trailing spaces around the value:
set s = My string // leading/trailing spaces will be removed set n ~ strlen $(s) // length without leading/trailing spaces set n ~ $(n) + 2 // length with additional two spaces SCI_SENDMSG SCI_APPENDTEXT $(n) " $(s) " // adding the spaces
You will encounter similar problem when calculating 'strlen' of a string which starts or ends with spaces. These spaces will be removed before calculating the string length. To avoid this, you should enclose such string in double quotes:
set n ~ strlen " ABC " // n=8: string length with two " symbols set n ~ $(n) - 2 // n=6: length without the " symbols
NppExec ver. 0.5 had introduced several additional functions: strupper, strlower and substr. Even more functions have been added later. Type "help set" for more details.
Here is an example that uses substr to extract a directory name from $(CURRENT_DIRECTORY) :
npp_console local - set local dirname = $(CURRENT_DIRECTORY) set local c ~ substr -1 1 $(dirname) // trailing character if "$(c)" == "\" then set local dirname ~ substr 0 -1 $(dirname) // removing trailing \ else if "$(c)" == "/" then set local dirname ~ substr 0 -1 $(dirname) // removing trailing / endif set local n1 ~ strrfind "$(dirname)" \ // position of last \ set local n2 ~ strrfind "$(dirname)" / // position of last / if $(n1) > $(n2) then set local n1 ~ $(n1) + 1 set local dirname ~ substr $(n1) - $(dirname) else set local n2 ~ $(n2) + 1 set local dirname ~ substr $(n2) - $(dirname) endif npp_console local + echo $(dirname)
Now let's discuss something different: pseudo-conditions in NppExec. Pseudo-conditions are available via the NPP_EXEC command plus some variable. The next idea lies behind this: you can form a script name dynamically in order to execute different script basing on some variable's value. Let me explain: a command "NPP_EXEC script$(var)" will execute different script according to the value of $(var). For example, let's assume you want to execute some program and then to have different behaviour depending on successful or unsuccessful execution of that program. Of course, you can use "cmd /c program && do_something_different" to achieve this, but such approach does not allow to use NppExec-specific functions (since everything is executing under cmd.exe). So, let's assume we have the following script:
program.exe // execute the program NPP_EXEC OnProgramEnd_$(EXITCODE) // pseudo-condition
Any program "program.exe" is expected to return 0 in case of its successful execution. Otherwise it returns non-zero value. (This is a common behaviour of executables.) Thus we can create NppExec's script named "OnProgramEnd_0" which will be called only in case of successful execution of "program.exe" -- i.e. when $(EXITCODE) will be 0. Otherwise, in case of -1 or 1 (and so on) we will try to execute non-existing script "OnProgramEnd_-1" or "OnProgramEnd_1" (and so on).
For example, create the following script and name it "OnProgramEnd_0" to test the previous statements (and the pseudo-conditions, actually):
// OnProgramEnd_0 echo The program returned 0: executed successfully!
So, we can say that the two above scripts allow us to emulate an IF condition. Here is its meaning: IF (program.exe returns 0) THEN execute "OnProgramEnd_0". Otherwise we try to execute non-existing script that can be treated as "do nothing".
Going further, we can implement an IF-THEN-ELSE statement in a similar way. Our script will be changed a little:
program.exe // execute the program set N ~ $(EXITCODE) != 0 NPP_EXEC OnProgramEnd_$(N) // pseudo-condition
The value of $(N) will be either 1 (true) when $(EXITCODE) is not 0, or 0 (false) when $(EXITCODE) is 0. So we will execute either "OnProgramEnd_0" or "OnProgramEnd_1". Now the only remaining thing is to create a script named "OnProgramEnd_1":
// OnProgramEnd_1 echo The program returned non-zero: execution failed.
NppExec ver. 0.5 introduced native support of IF-GOTO commands. Type "help if" for more details.
Finally, let's talk about "special" characters in NppExec. I mean "//" (a comment) and a double quote symbol.
While using a command with a comment, all the text after "//" is being cut from that command.
But what if you want to use "//" as a part of your text? Is it possible to do it?
Yes, it's possible. You can use environment variables for that:
set a = / // now $(a) is "/" echo $(a)$(a) // prints "//" set s = $(a)$(a) // now $(s) is "//"
The same technique can be used to pass a double quote symbol:
NPP_SENDMSG WM_COMMAND IDM_FILE_NEW set q = " sci_sendmsg SCI_SETTEXT 0 "$(q)some text in double quotes$(q)"
Not a big trick, actually, but may be useful :)
See also: Calculating [4.6.2]; Passing enquoted strings and special characters [3.8.5].