Select a list as a group.
Select a list as a group.
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
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
- martin
- Official Nisus Person
- Posts: 5228
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Select a list as a group.
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.
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)
Re: Select a list as a group.
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.
Only if you have time to look at this. Not critical in the slightest.
Thanks
Lou
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.
![Laughing :lol:](./images/smilies/icon_lol.gif)
![Laughing :lol:](./images/smilies/icon_lol.gif)
Only if you have time to look at this. Not critical in the slightest.
Thanks
Lou
Re: Select a list as a group.
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'
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
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:
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'
![Laughing :lol:](./images/smilies/icon_lol.gif)
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'
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
Re: Select a list as a group.
And now to this.
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.
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
![Embarassed :oops:](./images/smilies/icon_redface.gif)
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
Re: Select a list as a group.
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
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
Re: Select a list as a group.
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:
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:
What is the \X stand for?Find and Replace '\X+', "\n<ol>\n\\0</ol>\n\n", 'Eas'
Re: Select a list as a group.
Hello Lou, Happy new year.
The wildcard "\X" selects anything. So "\X+" matches whatever you have. It corresponds to the wildcard Any.
philip
- martin
- Official Nisus Person
- Posts: 5228
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Select a list as a group.
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.
Re: Select a list as a group.
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.
Thank you for the explanations. What a useful character class, it has inspired me to re-think my approach to some of my macros.