need help with find and replace, please

Everything related to our flagship word processor.
Post Reply
sethgodin
Posts: 28
Joined: 2006-12-14 09:39:15

need help with find and replace, please

Post by sethgodin »

I have a long doc, and there are 300 sections

each section has a first line in style HEADING 4
and the rest of the section is multiple paragraphs in LO-normal

I want to find the first paragraph of each section (the paragraph that follows the first line in style HEADING 4

and change that to style FIRST PARAGRAPH

I can use the wildcards to find the header, no problem
but when I try to search for two different styles in one powerfind, it doesn't work

thanks!
adryan
Posts: 615
Joined: 2014-02-08 12:57:03
Location: Australia

Re: need help with find and replace, please

Post by adryan »

G'day, Seth et al

Ideally, this would be handled by the "PrecededBy()" feature of the Find system. But, as you have found, you can only have one Paragraph Style in a Find expression.

If you assign one particular Paragraph Style to a substring of a Find expression and then assign a different Paragraph Style to a disjoint substring of the same Find expression, the entire expression is assigned the most recent Paragraph Style.

I think this behavior should be changed. A Find expression should not be treated as a single paragraph for the purpose of assigning Paragraph Styles. Returns in Find expressions should be recognized as paragraph delimiters and multiple Paragraph Styles permitted.

I can think of no easy way to accomplish what you want in a single step using the Find/Replace system. One possible approach would be:–

(1) Replace each paragraph of HEADING 4 Style with itself followed by a paragraph containing a dummy string not used elsewhere in your document (eg, "zyxb").
(2) Find all occurrences of this dummy string together with the following paragraph.
(3) Assign the desired Paragraph Style (viz, FIRST PARAGRAPH) to the result of the Find operation (ie, to each paragraph pair).
(4) Delete all occurrences of the paragraph containing the dummy string.

The only Formatting checkbox that should be ticked is the one for the Find operation in Step 1.

I hope this helps.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
phspaelti
Posts: 1353
Joined: 2007-02-07 00:58:12
Location: Japan

Re: need help with find and replace, please

Post by phspaelti »

Hello Seth, Adrian,

This is where it's really better to use a macro. The macro language has nice constructs that make this task reasonably easy.
Here is the code:

Code: Select all

#Select First Paragraph Following Heading 4
$doc = Document.active
$sels = $doc.text.findAll $doc.styleWithName("Heading 4")
$firstParaSels = Array.new
foreach $sel in $sels
    $firstParaSel = TextSelection.new $doc.text, $doc.text.rangeOfParagraphAtIndex $sel.bound
    $firstParaSels.push $firstParaSel
end
$doc.setSelections $firstParaSels
In clear text
  1. Find all paragraphs with "Heading 4" style
  2. Go through each case and create a new selection that starts right after the heading
  3. Select all the new selections
Attachments
Select First Paragraph Following H4.nwm
(5.79 KiB) Downloaded 262 times
philip
adryan
Posts: 615
Joined: 2014-02-08 12:57:03
Location: Australia

Re: need help with find and replace, please

Post by adryan »

G'day, Philip et al

Yes, that's much nicer.

I suspected a Macro approach would be the best option, but I don't have anywhere near the facility with the Macro Language that you do, Philip, and in any case I thought I'd see how much one could milk the Find/Replace system for a solution.

One of the advantages of Philip's Macro is that a Style is explicitly named, whereas once a Style is applied to a string in the Find field of the Find/Replace dialog box its identity is concealed and it requires a bit of work to elucidate it. Furthermore, Styles applied in the dialog box do not survive the Macroize operation and need to be reapplied in the content of a Macro thus created.

Bottom line: Learn the Macro Language, Adrian!

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
xiamenese
Posts: 568
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: need help with find and replace, please

Post by xiamenese »

Sadly, I am definitely past my use-by date for learning the Macro Language. But I do have an extension question for Philip:

Would it be difficult to rework the macro so that it asked you what paragraph style to input in a variable for:

Code: Select all

$doc.styleWithName("…")
so that it isn't hard-wired to "Heading 4", as the OP needs? The ability to choose other levels of heading, without having separate macros for each, would be potentially very useful.

Cheers
Mark
User avatar
phspaelti
Posts: 1353
Joined: 2007-02-07 00:58:12
Location: Japan

Re: need help with find and replace, please

Post by phspaelti »

xiamenese wrote: 2024-06-12 04:18:33 Would it be difficult to rework the macro so that it asked you what paragraph style to input ?
Not at all.

(I'm pretty sure I've written such a macro before, and perhaps shared it somewhere in this forum. But by now I have so many macros that it's usually quicker to write a new one, rather than look for the old one :P )
Attachments
Select First Paragraph Following Style.nwm
(6.08 KiB) Downloaded 262 times
philip
User avatar
xiamenese
Posts: 568
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: need help with find and replace, please

Post by xiamenese »

Thank you Philip, not just for this, but for all the help you so generously give to all of us.

Cheers
Mark
Post Reply