Character styles, lost during 'Find and replace' macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
pereelmagne
Posts: 13
Joined: 2015-06-27 01:38:22

Character styles, lost during 'Find and replace' macro

Post by pereelmagne »

Hello,
I'm using a macro to change guillemets for «» and viceversa. It works well, but any character style applied inside the selected text is erased. I tried using the u and U options in the 'Find and Replace' commands, but the macro stopped working.

The first part of my macro is as follows:

Code: Select all

$findquotes = '«(.+?)»'
$replaceguillemets = '\“\1\”'

$guillemetsquotes = Find and Replace $findquotes, $replaceguillemets, 'Esa'
I wanted to use the properties 'EsauU', but it stops working. Any idea?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Character styles, lost during 'Find and replace' macro

Post by phspaelti »

'Stopped working'?

(You do remember that you set the 's' option. So it will only work on the selection. That was my problem :) )

Setting the 'u' and 'U' options is the solution, except for the usual caveats. Those options will preserve the formatting of the found bits, but for the newly inserted quotes, the 'U' option will apply the formatting that is set in your macro file!

So one approach would be to do this in steps:
  1. Find the bits with the guillemets
  2. Using the 'S' option replace the opening guillemets in the selection (without 'u' or 'U' option!)
  3. Replace the closing guillemets in the selection
The second and third steps should be enclosed in an 'if' statement to ensure they are only run if the first is successful.

I'd try and test this, but the margin here is to small :wink:
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Character styles, lost during 'Find and replace' macro

Post by phspaelti »

Let me amend my earlier comments a bit.

First of all the 'u' option is not what you want at all. The 'u' option would tell Nisus to limit matches where the found text matches the formatting of the search expression in your macro.

So the question is what does the 'U' option do.

If you don't use the option, Nisus will have the replacement always match the local formatting. But the "local formatting" is always the formatting at the beginning of the replaced bit. If the text you are replacing had mixed formatting (e.g. partly in italics, etc), this will be wiped out.

If you do use the 'U' option, then this mixed formatting is carried over for the matched bits (i.e., the '\1' in your case), but for the rest it will use the formatting from the macro. In your case this will be the formatting that you apply in this part of the macro:

Code: Select all

$replaceguillemets = '\“\1\”'
Note that formatting applied to that expression will override formatting applied in the document. So the end result will be a combination of the formatting from those two places. This can make the 'U' option very hard to use sometimes. I try to avoid it whenever possible.

In your case you presumably really want one type of quotes replacing the other, and to keep the local formatting. So not using 'U' is the correct choice, but only for the quotes. So the simplest is to just use:

Code: Select all

Find and Replace '«', '\“', 'EasS'
Find and Replace '»', '”', 'EasS'
So just replace opening and closing quotes separately. Using the 'S' option preserves the original selection, so you can do both replacements on the same selection, and you avoid matching anything else, so as not to disturb other formatting.
philip
pereelmagne
Posts: 13
Joined: 2015-06-27 01:38:22

Re: Character styles, lost during 'Find and replace' macro

Post by pereelmagne »

Many thanks for your help. Certainly, the properties 'u' and 'U' are not the best approach. Moreover, I should have guessed that the formatting of the first character in the selection was key.

Let's say I have a citation in French between guillemets as follows: “citacion-in-French”. I want to change those quotation marks for « and ». Instead of applying a character style with French language strictly to the text citacion-in-French, I can apply it to the citation with guillemets included, and yes, the character style is not overwritten.

I had a very long bibliography where I had to change one type of quotation marks for the other, and my macro was of great help after I knew that. Of course I have to try your approach, which is to make one substitution first (opening mark) and then the other (closing mark). And I will, as soon as I have some spare time.

Again, many thanks.
Post Reply