For many years now, 4D Write Pro has made it possible to create simple or complex documents, either via programming using templates or by simply creating new documents from scratch. Starting with 4D v19, both developers and end users will be happy to use the new find (and replace) capabilities of 4D Write Pro. Using not just the interface… but also the code!
Let’s delve into the details!
HDI: 4D Write Pro – Find & Replace
Say hello to WP Find All
Finding (and replacing) text inside a 4D Write Pro document is already possible with basic commands like WP Get text and WP Set text. With this feature release, a new command is available that makes it possible to go faster than ever, with optional criteria!
FIND any word occurrence
Searching and replacing text in a document is a pretty typical operation and must be a simple action.
However, it’s sometimes necessary to take into account certain particularities of typography or language. For example, in Latin languages, certain letters may be capitalized or accented. You may also wish to select only whole words and not parts of words, etc. On the other hand, in Japanese, the way of writing (as well as the width of characters) has its own importance and must sometimes be taken into account or, on the contrary, ignored.
It’s also useful to specify that a search be performed either in the entire document or limited to a specific part of the document (e.g., a paragraph, section, header, footer, etc.).
All of these considerations, from the simplest to the most complex, are configurable with this new command.
Replacements and modifications
If the purpose of a search is to replace a single word (or multiple words), this can now be performed in a single operation. The replacement string just needs to be passed as a parameter to the command! The cherry on top? You can choose if the style of the characters in the replaced string should be kept or not when replacing. This can be very convenient for example if the first letter is bold, colored, or simply has a different size. For example, you can replace “Romeo” with “Juliet” 😉
If it’s not a question of modifying the content, but rather the shape of the occurrences found, just use the collection returned by the command where each element is a range. They can be used as a parameter for general 4D Write Pro functions, such as WP Set attributes.
Code sample
In the text below, we want to replace “Alpha” with “Bravo”. We don’t care about the accents and capitals, so we’ll ignore those. However, we want the typography of each letter of the original word (Alpha) to be preserved:
Before…
$find:="Alpha"
$replace:="Bravo"
$options:=wk case insensitive+wk diacritic insensitive+wk keep character style
$_ranges:=WP Find all([SAMPLE]WP; $find; $options; $replace)
…After
Find more details about the command in the doc center!
4D Write Pro Interface
This new feature has been added to the 4D Write Pro Interface widgets:
Toolbar widget
Side-bar widget
Don’t forget, the source code is available on GitHub. Feel free to adapt it to suit your needs!