Sometimes when editing scripts (or forum messages) I feel the need for a more powerful find-and-replace system than is offered by the standard Windows Edit Control find-and-replace (the one used by the MUSHclient notepad).
Examples are:
Now, to save reinventing the wheel (again) I thought it would be handy to have the functionality of the Lua string.gsub available for easy use in the Notepad.
Accordingly, verion 3.71 of MUSHclient offers an alternative to the standard find/replace dialog, the new Global Replace dialog.
The selected text (or all text in the window if none is selected) will be sent to the string.gsub function, with the specified "find" and "replacement" text being used for the function, like this:
If there is an error raised by string.gsub (eg. bad regular expression) then no text is replaced.
See the documentation for string.gsub in Lua for the exact workings of this function. Also see the forum posting:
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6034
You can replace in "line by line" mode, or "entire block" mode. Line-by-line is useful if you want to do something like add spaces (or remove spaces) at the start of each line.
Examples of doing the changes mentioned above:
This changes [aaa] to ["aaa"]
And:
This swaps the things in an assignment statement, so:
becomes:
Replacement function
For extra power, you can specify a replacement function (as you can for string.gsub in Lua) and really make complex replacements.
For example, here is how you could fix up HTML codes using the Global Replace:
The "find" box specifies the set of "[<>&]" - in other words the HTML characters we need to fix.
The replacement function, called "f" is named in the replacement box. It is called by string.gsub. It uses a table lookup to map "<" to "<" and so on.
Another example of using a function:
This searches for words that are in all upper-case (by searching for %u which is upper case letters, followed by %A which is something that is not a letter). When found the function "f" is called that converts it to lower-case.
Examples are:
- Change:
aaa [bbb] = 50 ccc [ddd] = 100 eee [fff] = 250 ggg [ggg] = 300
to
aaa ["bbb"] = 50 ccc ["ddd"] = 100 eee ["fff"] = 250 ggg ["ggg"] = 300
- Change:
aaa = bbb ccc = ddd eee = fff
to:
bbb = aaa ddd = ccc fff = eee
(In other words, reverse what is being assigned to what).
Now, to save reinventing the wheel (again) I thought it would be handy to have the functionality of the Lua string.gsub available for easy use in the Notepad.
Accordingly, verion 3.71 of MUSHclient offers an alternative to the standard find/replace dialog, the new Global Replace dialog.
The selected text (or all text in the window if none is selected) will be sent to the string.gsub function, with the specified "find" and "replacement" text being used for the function, like this:
new_selection = string.gsub (old_selection, find_text, replacement_text)
If there is an error raised by string.gsub (eg. bad regular expression) then no text is replaced.
See the documentation for string.gsub in Lua for the exact workings of this function. Also see the forum posting:
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6034
You can replace in "line by line" mode, or "entire block" mode. Line-by-line is useful if you want to do something like add spaces (or remove spaces) at the start of each line.
Examples of doing the changes mentioned above:
Find: %[(.+)%]
Replace: ["%1"]
This changes [aaa] to ["aaa"]
And:
Find: ( *)(.*) = (.*)
Replace: %1%3 = %2
This swaps the things in an assignment statement, so:
a = b
becomes:
b = a
Replacement function
For extra power, you can specify a replacement function (as you can for string.gsub in Lua) and really make complex replacements.
For example, here is how you could fix up HTML codes using the Global Replace:
Find: [<>&]
Replace: f
Each line: no
Escape: no
Call Function: yes
Script:
replacements = {
["<"] = "<",
[">"] = ">",
["&"] = "&",
}
function f (str)
return replacements [str] or str
end
The "find" box specifies the set of "[<>&]" - in other words the HTML characters we need to fix.
The replacement function, called "f" is named in the replacement box. It is called by string.gsub. It uses a table lookup to map "<" to "<" and so on.
Another example of using a function:
Find: %u+%A
Replace: f
Line by Line: no
Call Function: yes
Script:
function f (s)
return string.lower (s)
end
This searches for words that are in all upper-case (by searching for %u which is upper case letters, followed by %A which is something that is not a letter). When found the function "f" is called that converts it to lower-case.