Select a list as a group.

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 95
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: 5134
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: 95
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: 1264
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: 1264
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: 95
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
Post Reply