Your question is very interesting, I promise to upvote it as soon as possible. As usual, I'm out of votes.
I'll try to write a humble answer. I'm really sorry if the solution might cause any trouble due to any bug or annoyance, I'm a Lua newbie. :)
First things first: we need to enable the scripting language plugin in TeXworks. We can do it by simply going to the Preferences
, in the Scripts
tab:
We just need to mark the checkbox. :)
Now, let's see where to put our script. Go to Scripts
, Scripting TeXworks
, Show Scripts Folder
:
The operating system file manager will appear with the folder we want. :)
In my Mac, it's under ~/Library/TeXworks/scripts
. Now, create a new file named replaceList.lua
and add the following content, as it is:
--[[TeXworksScript
Title: Replace list
Description: A replacement script
Author: Paulo Cereda
Version: 1.0
Date: 2012-11-28
Script-Type: standalone
Context: TeXDocument
]]
-- implements the 'strip' function in order to extract
-- elements from a string according to a specific
-- separator. The output is a table containing each
-- element from the input string.
-- Thanks to the http://lua-users.org/wiki/SplitJoin wiki.
string.split = function(str, pattern)
pattern = pattern or "[^%s]+"
if pattern:len() == 0 then
pattern = "[^%s]+"
end
local parts = {__index = table.insert}
setmetatable(parts, parts)
str:gsub(pattern, parts)
setmetatable(parts, nil)
parts.__index = nil
return parts
end
-- gets a string containing a list of patterns. This is
-- the first dialog window that appears in the script
-- execution.
local listOfPatterns = TW.getText(nil, "List of patterns to replace - 1/2", "Please, add a list of patterns to replace, separated by comma.")
-- gets a string containing a list of words to replace the
-- patterns obtainted in the previous step. This is the
-- second dialog window that appears in the script
-- execution
local listOfReplacements = TW.getText(nil, "List of patterns to replace - 2/2", "Please, add a list of replacements, separated by comma.")
-- checks if both inputs are valid
if (listOfPatterns ~= nil) and (listOfReplacements) then
-- split the patterns into elements of a table. In our
-- case, the separator is a comma.
local patternsToLook = string.split(listOfPatterns, "[^,%s]+")
-- split the values into elements of a table. In our case,
-- the separator is a comma.
local valuesToReplace = string.split(listOfReplacements, "[^,%s]+")
-- the number of elements of both tables must be equal
if (#patternsToLook == #valuesToReplace) then
-- iterate through the patterns table
for index, currentValue in ipairs(patternsToLook) do
-- select all the text from the current
-- document
TW.target.selectAll();
-- get all the selected text
local text = TW.target.selection
-- checks if there's actually a text
if (text ~= nil) then
-- replace all occurrences of the current
-- pattern by its corresponding value
local toReplace, _ = string.gsub(text, currentValue, valuesToReplace[index])
-- insert modified text into the
-- document
TW.target.insertText(toReplace)
end
end
end
end
My sincere apologies to Patrick, Taco and other Lua masters. :)
Now, we have a new file in our folder, replaceList.lua
:
Now, back to TeXworks, we need to reload our list of scripts. It's easy, we need to go to Scripts
, Scripting TeXworks
, Reload Scripts List
:
Done. :)
Let's create a test file:
Time to run our script. Simply go to Scripts
and select Replace list
:
Now, the magic of TeXworks scripting API will appear. First, we will define a list of patterns to look for:
Regex supported, I guess.
I'm telling our script to look for three words, separated by commas. After clicking OK
, a new window will appear, with a list of replacement words:
Of course, both lists have to have the very same size. :)
After we click OK
, our new text is presented:
There we go! :)
I hope my humble answer helps. :)
Best Answer
When one invokes the Find function (with
CTL-F
) in TeXworks, a window pops upOne of the boxes that can be checked is
Find all occurrences
. If that box is not checked, the search will just take you to the next occurrence in the file. If that box is checked, it will bring up the window that displays all hits for the entered string.Given the number of settings in that window, it is easy to "glaze over" and miss the particular one to which I draw attention.
The setting is "persistent", by which I mean that once you check or uncheck the box, all your future searches retain that setting, until you change it again.
My guess, in the OP's case is that the default (installation) setting for the search mode was NOT to find all occurrences, so the OP merely need to check that box the next time find is invoked, and should be good to go.