Japanese in find/replace perl macro

Have a problem? A question? This is the place for answers from other Express users.
Post Reply
credneb
Posts: 140
Joined: 2007-03-28 07:30:34

Japanese in find/replace perl macro

Post by credneb » 2007-03-28 07:42:28

I've been working on learning NWE macros, and wrote one yesterday that does a global find/replace to remove new line codes from selected lines globally.

The problem is that the find/replace strings contain Japanese characters. After saving and reopening the macro to work on it some more, the Japanese characters are garbage, the macro is unreadable, and it cannot be saved.

Is it not possible to have Japanese in a grep expression in Perl or a NWE macro? If so, how? I assume I am simply doing something wrong.

If not, wow.

Thank you.

Cliff Bender

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

Post by martin » 2007-03-28 13:44:40

Hello Cliff, there are a couple of things which could trigger problems with Japanese (or other non-ASCII) characters in macros. Make sure that:

1. You always use straight quotes rather than curly quotes (eg: in a print statement). They can creep in by accident quite easily if you have QuickFix set to curl your quotes automatically.

2. When you save your Macro, make sure the Encoding is set to UTF-8.

3. You should have these lines somewhere at the top of your Macro:

use utf8;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

This will tell Perl that the file is encoded using UTF-8 and that you wish to treat both the input and output for the macro as UTF-8.

4. Make sure you have your Plain Text Encoding set to UTF-8 as well, not just Automatic. This setting is located in your General Nisus Writer Express preferences.

If you follow these steps, and re-save your macro, hopefully all should be well.

credneb
Posts: 140
Joined: 2007-03-28 07:30:34

Post by credneb » 2007-04-11 17:20:17

Martin,

Thank you for the reply. Your suggestions worked in that I can now save Japanese in the macro.

The problem now is that the text written back to the document after the replace is garbage and is inserted at the cursor instead of overwriting the old text.

The same thing happens running the included Insert Line Numbers macro.

The document language is set to Japanese, and all text is in Hiragino Mincho Pro.

Any idea what the problem is?

For reference, the basic macro is pasted below.

Thank you.

Cliff Bender

*********
#Nisus Macro Block
#source front selection
#destination front selection
#End Nisus Macro Block
use utf8;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");


#This macro template lets you replace text with other text
#You can use regular expressions to do text pattern matching
while (my $line = <STDIN>) { #Reads each line of the selection from the clipboard

$line =~ s/\n【/zxq【/ig; #replaces CR+[Jpn bracket]
$line =~ s/\n /zxq /ig; #CR+[Jpn space]

$line =~ s/\n//ig;
$line =~ s/zxq/\n/ig;

print $line; #writes out each line to the clipboard
}

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

Post by martin » 2007-04-12 14:58:48

Cliff- I've not tested your macro, but I think this may be a bug in NWX. If you can, please download the Nisus Writer Pro public beta. I believe the problem may be fixed. Thanks, please let me know how it goes.

credneb
Posts: 140
Joined: 2007-03-28 07:30:34

Post by credneb » 2007-04-16 18:32:38

Martin,

I had already downloaded the Pro beta, and fairly quickly and easily did what i wanted to do using the new macro capability, specifically the Finda and Replace command. Much faster, too.

Thank you for your help. I will use Pro from now on, likely.

Cliff Bender

Post Reply