Regular Expressions with text styles in a macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
dakofi
Posts: 17
Joined: 2003-02-07 07:48:35

Regular Expressions with text styles in a macro

Post by dakofi »

Hi

In Nisus Classic I used to write macros by using the Find and Replace comand. I prepared the Regular Expression in the Find and Replace mask and gave them, if needed, the right styles (like size, font, bold, italic, standard, etc.). After that I copied it into the macro Now, I tried to do it the same way but, if I search for standard 10 (unfortunately I cannot define it as standard or not italic). It found all letters with a size 10 which is not what I intended.
For that reason I tried to take another way. I protected all characters I still need by giving them the new size 12.

The commands are as follows:

Find and Replace ‘^([\.,\d]+)’, ‘\1’, ‘aEU’

#The number at the beginning of the line has to have a new stile: italic 12,
#the characters are now in italic but the size remained on 10.

Find and Replace ‘(.+)’, ‘\1’, ‘aEuU’
#find all characters in italic and gives them the new size 12.
#the next statement fetches all the italic characters, but their size won't be changed.

Find and Replace ‘.+’, ‘’, ‘aEu’
#This command should delete all characters with standard style and size 10.
#But it deletes the italic characters in size 10 as well.

The same regular expressions with styles are working correctly in the find and replace window, if the style sensitive button is ticked.

Is it in NWP impossible to work as I used to?
How do I have to do it now?
Is it possible, that the error is caused by the German application I work with?

Thanks for your help.

dakofi
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Regular Expressions with text styles in a macro

Post by Kino »

dakofi wrote:Now, I tried to do it the same way but, if I search for standard 10 (unfortunately I cannot define it as standard or not italic). It found all letters with a size 10 which is not what I intended.
If I understand you well, you want to delete all text portions in 10 pt which are NOT in Italic style. Right? Then...

Yeah, you are right. It seems that you cannot do it with a macro command such as Find and Replace '.+', '', 'Eau'. A workaround would be to [1] Find All text portions in Italic, [2] Invert Selection and [3] Replace All in 10 point with nothing in selection.

For the second step, you need Martin's Invert Selection macro, that you can download from the Macro Repository.
http://www.nisus.com/files/GetMacroRedi ... ectInverse (direct link)

After the installation of the macro, you can do something like this.

Code: Select all

Find All '.+', 'Eu' # Italic
Invert Selection
Replace All '.+', '', 'Eus' # 10 pt in Selection
As the Invert Selection macro is so useful in a wide variety of situations, I think it should be added to the built-in macros so that it is available to all users.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Regular Expressions with text styles in a macro

Post by martin »

Kino's method is very clever and probably the best solution.

If you don't need a macro, you can use the Find & Replace panel to do what you want:

1. Select your expression.
2. Use the menu Format > Any Attributes.
3. Use the menu Format > Font Size > 10.

At this juncture the search expression appears as 10 point and in a plain typeface, eg: not italic. However, there is a difference between this default plain typeface and an explicit setting of "no italics". To force matching only non-italic text:

4. Use the menu Format > Italic to turn italics on.
5. Again use the menu Format > Italic to turn italics explicitly off.

The search should then only select text that is both 10 points and not italic.
Kino wrote:If I understand you well, you want to delete all text portions in 10 pt which are NOT in Italic style. Right? Then...

Yeah, you are right. It seems that you cannot do it with a macro command such as Find and Replace '.+', '', 'Eau'.
The reason this is not possible in a macro is somewhat complicated and has to do with the way attributes (such as italic) are stored in RTF versus how they are stored once a file is loaded into NWP. In RTF the formatting "explicitly not italic" is not differentiated from the implicit "not italic by omission", unless the lack of italics is required to override a style (eg: Heading 2). This is mirrored in the way NWP behaves when you modify attributes via the GUI. If a newly applied attribute matches the formatting already defined by the styles, or the default formatting, then it is removed as redundant.

This behavior is generally a feature, but in the case of attribute sensitive macros it's an unfortunate hindrance. It's possible we could modify the way formatting changes made to macros are processed so that redundant/default attributes aren't removed, but we'd also need to extend RTF to store these kinds of attributes, which while possible, but would require a large amount of effort.
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Regular Expressions with text styles in a macro

Post by Kino »

Thank you, Martin, a lot for the detailed explanation. It is always very exciting to know how Nisus Writer Pro works internally.

Another way is to simulate the manual operation described in Martin's reply.

Code: Select all

$anyText = Cast to String '.+'
Find and Replace $anyText, '', 'Eua!'
Set Find Shown true
Menu ':Format:Size:10'
Menu ':Format:Italic'
Menu ':Format:Italic'

# The next command is cosmetic and unnecessary.
Set Find Shown false

Menu ':Edit:Find:Replace All'
Yet another possibility is to use Attributes commands. Attention: the macro below will delete non-Italic newlines in 10 pt too.

Code: Select all

$doc = Document.active
$text = $doc.text
$textObjects = $doc.allTexts

foreach $text in $textObjects
	$index = $text.length - 1
	if $index >= 0
		while $index >= 0
			$attr = $text.displayAttributesAtIndex $index
			$range = $text.rangeOfDisplayAttributesAtIndex $index
			if $attr.italic == false
				if $attr.fontSize == 10
					$text.deleteInRange $range
				end
			end
			$index = $range.location - 1
		end
	end
end
There is more than one way to do it ;-)

Edit: I corrected a bug in the macro above by replacing "rangeOfAttributesAtIndex" with "rangeOfDisplayAttributesAtIndex".
Last edited by Kino on 2008-10-04 23:07:58, edited 1 time in total.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Regular Expressions with text styles in a macro

Post by martin »

Excellent Kino, thanks for the additional smart solutions to this problem!
Post Reply