Select a list as a group.

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Select a list as a group.

Post by loulesko »

Hello, there. Making a bit of a comeback to the forum. I kinda miss it here.

Wondering if there is a way to select a bullet list or number list as one unit and put carriage returns (or any other characters) around it.

Thanks
Lou
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Select a list as a group.

Post by martin »

Hi Lou, and welcome back!

I'm not sure I entirely understand what you want, but here's a macro that takes a stab at it. You should place your insertion point (aka: caret) anywhere in a list and run the macro. It will insert newlines before and after the contiguous paragraphs in the same list style.

Code: Select all

Menu "Format:Lists:Select Next in Style"
$doc = Document.active
$selection = $doc.textSelection
Select End
Insert Text "\n"

$before = TextSelection.newWithLocationAndLength($selection.text, $selection.location - 1, 0)
$doc.setSelection($before)
Insert Text "\n"

$selection.location += 1 # adjust for insert
$doc.setSelection($selection)
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Select a list as a group.

Post by loulesko »

Martin, thanks for this.

I was actually looking to select each list number or bullet and put a leading and trailing CR.

I used the following in an old "copy as html" macro of mine to wrap the lists in <ol> tags and CRs:

$style = $doc.styleWithName('Number List')
Replace All $style, '\n<ol>\n\0</ol>\n\n'

But that doesn't seem to work anymore. I read in the macro manual that \0 doesn't work in the Replace All or Find All commands. Is that new in the last five years or, probably more accurate, I'm totally missing something. :lol: :lol:

Only if you have time to look at this. Not critical in the slightest.

Thanks
Lou
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Select a list as a group.

Post by phspaelti »

Hello Lou,
Did that bit of macro code ever work in the past?

First about '\0'. '\0' refers to the found text. So it couldn't have any meaning in a "Find (All)" expression. That would amount to saying 'find what you find' :lol: It also doesn't work in Replace All because Replace All doesn't involve a Find. Replace All will generally apply to the current selection, and in that context '\0' will appeal to the last Find which may be something unrelated or non-existant.
Also Replace All takes only one argument (besides the Options argument), so having both $style and '\n<ol>\n\0</ol>\n\n' isn't going to work. You could use

Code: Select all

Find and Replace $style, '\n<ol>\n\0</ol>\n\n'
But this is also not likely to bring you any joy, since the $style find argument doesn't seem to create a $0 variable AFAIK. Find using styles is a nice feature that I use a lot, but I am always conservative in how I use it.
Also I believe '\n' doesn't work in single quoted strings. You need to use double quoted strings to get the back-slash to interpolate.
So taking all that into consideration, something like this should work:

Code: Select all

$style = $doc.styleWithName "Number List"
Find All $style
Find and Replace '\X+', "\n<ol>\n\\0</ol>\n\n", 'Eas'
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Select a list as a group.

Post by phspaelti »

And now to this.
loulesko wrote: 2022-10-01 10:43:51 I was actually looking to select each list number or bullet and put a leading and trailing CR.
Here you are saying select each list number or bullet. But what your example is doing is selecting the whole list as a unit. And I suspect that that is why Martin was asking what you are trying to do. Part of the problem is that if you try to wrap list items in <return>s you will keep multiplying them. That's because the list styles turn your paragraphs into bullet points or numbered list items. So this will become a Sisyphean task. It will also ruin any automatic numbering. The point is you will have to remove the list style first and then add returns. But once you remove the list style, the list will be gone :oops:

From the bit of code you provided it looks like you are trying to turn a formatted list into HTML. For this I would suggest not trying to change the document, but rather to create a new document, copying the text bits and assembling them with HTML codes, with the styles removed. My approach for this is to create an output Array object. Then go through the document line by line, wrap the lines in tags as appropriate, and push them to the Array. Finally join the lines up using <return> as the joiner to create a single output text object.
philip
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Select a list as a group.

Post by loulesko »

Philip,

Hello. Been a while since we exchanged on the forum. As always, thank for your input.

The \0 was part of a macro I wrote ages ago which converts text to html.

viewtopic.php?t=5494

I remember it working in that instance. I was indeed looking to capture the list as a group and inserting a leading and trailing CR to create space above and below. But I see your point about it repeating. I'm gonna use Martin's code above, which works incredibly well.

Have a fabulous day.

Lou
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Select a list as a group.

Post by loulesko »

Philip,
I had to take a few weeks to re-familiarize myself with the macro language after being gone from it for awhile. Your suggestion has really helped on multiple levels.

I'd love to know something. You recommended:
Find and Replace '\X+', "\n<ol>\n\\0</ol>\n\n", 'Eas'
What is the \X stand for?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Select a list as a group.

Post by phspaelti »

loulesko wrote: 2022-12-29 13:24:53 What is the \X stand for?
Hello Lou, Happy new year.
The wildcard "\X" selects anything. So "\X+" matches whatever you have. It corresponds to the wildcard Any.
philip
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Select a list as a group.

Post by martin »

phspaelti wrote: 2023-01-01 01:58:22
loulesko wrote: 2022-12-29 13:24:53 What is the \X stand for?
Hello Lou, Happy new year.
The wildcard "\X" selects anything. So "\X+" matches whatever you have. It corresponds to the wildcard Any.
To expand on this a little bit: \X is a character class that will match any Unicode grapheme cluster. That's any single character, or any sequence of characters, that is treated and displayed as a single unit. For example: a base "e" character followed by an accent combining mark which together shows as "é" on screen.

It's basically an updated version of the dot/period regex syntax, e.g. the patterns .+ and \X+ match roughly the same thing. There is however one major difference: typically the dot will not match newlines, but \X will.
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Select a list as a group.

Post by loulesko »

Happy New Year Martin and Philip

Thank you for the explanations. What a useful character class, it has inspired me to re-think my approach to some of my macros.
Post Reply