3.8.5. Passing enquoted strings and special characters

NppExec, starting from version 0.7, allows to use three different quote characters to enquote a string:

This allows to pass some quote characters within a string enquoted in the other quote characters. For example:

messagebox `"a message in quotes"`  // preserves the "" quotes
messagebox "Can't stop"             // preserves a single ' quote

Here is an advanced example, now using Scintilla as well:

set local newline ~ strfromhex 0A 00  // '\n' character

set local s = "a string in double-quotes"
set local len ~ strlensci $(newline)$(s)
sci_sendmsg SCI_APPENDTEXT $(len) `$(newline)$(s)`

set local t ~ strreplace '$(s)' '"' `'`
set local t ~ strreplace `$(t)` "double" "single"
set local len ~ strlensci $(newline)$(t)
sci_sendmsg SCI_APPENDTEXT $(len) `$(newline)$(t)`

set local r ~ strreplace `$(t)` `'` "`"
set local len ~ strlensci $(newline)$(r)
sci_sendmsg SCI_APPENDTEXT $(len) "$(newline)$(r)"

set local findflags ~ NPE_SF_INWHOLETEXT | NPE_SF_SETSEL
sci_find $(findflags) `"`

The previous example additionally demonstrates a technique of adding a new-line character '\n' to the text. It uses NppExec's function strfromhex to get a Unicode two-byte character '\n' from its hexadecimal representation of 0A 00 (the lower bytes goes first, the upper bytes goes next). This is equal to:

set local newline ~ chr 0x0A  // '\n' character

Similarly, a tabulation character '\t' can be obtained as:

set local tab ~ chr 0x09  // '\t' character
echo ab$(tab)cd

Here is an example that takes the %PATH% environment variable and represents it as a multi-line string by replacing all the ';' with '\n':

npp_console local -
set local n ~ chr 0x0A // '\n'
set local s ~ strreplace "$(SYS.PATH)" ; $(n)
npp_console local +
echo $(s)

The strfromhex is more powerful than the chr since strfromhex allows to operate with a sequence of bytes while chr is limited to one character. For example, Windows uses '\r\n' line endings that consist of two characters '\r' and '\n'. These two characters are represented by 4 bytes in a Unicode two-byte character string:

set local newline ~ strfromhex 0D 00 0A 00  // '\r\n' characters

The '\r' is 0D 00 and '\n' is 0A 00 here.

Here is an example that uses strunescape to construct a multi-line message:

set local msg ~ strunescape Drive1 is C:\\\nDrive2 is D:\\
messagebox `$(msg)`

See also: Calculations, strlen and so on [3.8.4].