NISUS Archives

April 2010

NISUS@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Thu, 29 Apr 2010 22:37:00 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (67 lines)
On Apr 29, 2010, at 6:14 PM, Žorvaršur Davķšsson wrote:

> The question is: Is it possible to influence which styles imported files (such as ReadMe files) will contain when they are opened for the first time in NWP? In Nisus Classic it was possible by editing the file called "Nisus Text Stationery". Now, since there is no such file in NWP, it seems that *all* imported text files are opened in NWP with a fixed, minimal default set of styles. This means in other words, I have to import my own styles from my style library every time I need my styles. It's a bit inconvenient, but I can live with that.

To make available all your default styles in a text file opened in NWP, you can paste the content of it on a new document window. Here is a macro which does it automatically.
<http://www2.odn.ne.jp/alt-quinon/files/NWPro/file/OpenTextFiles_nwm.zip>
(It does not handle rtf/doc files.)

A problem with this kind of macro is how to detect the text encoding. The macro uses Encode::Detect perl module if it is available. Otherwise, it chooses the most probable encoding from among UTF-8, CP-1252 (ISO-9959-1) and MacRoman by trial and error.

If you don't open a text file in other encodings, you don't need installing Encode::Detect. After all, it is not perfect. It is impossible to create a perfect encoding detector.


Kino
--

### Open Text Files (ver. 1.0) ###

# Open plain text files with encoding detection.

# If you have installed a perl module Encode::Detect, it will be used. Otherwise, the macro chooses the most probable encoding from among UTF-8, CP-1252 (ISO-9959-1) and MacRoman.

$paths = Choose Files '~/Documents', 'Select Text Files'
if $paths == undefined
	exit  # cancelled
end

Set Exported Perl Variables 'data', 'path'

foreach $path in $paths
	$data = ''
	begin Perl
		eval { require Encode::Detect };
		if ( ! $@ ) {
			use Encode;
			my $txt = `cat "$path"`;
			$data = decode("Detect", $txt);
			$data = decode_utf8($data);
		} else {
			my $conv_err = `mktemp /tmp/conv_err_XXXXXX`;
			chomp $conv_err;
			$data = `textutil -convert txt -inputencoding utf-8 -stdout "$path" 2> $conv_err`;
			if ( -s $conv_err ) { # then, the conversion from utf-8 has failed
				$data = `textutil -convert txt -inputencoding windows-1252 -stdout "$path" 2> $conv_err`;
			} else {
				$data = decode_utf8($data);
			};
			if ( -s $conv_err ) { # then, the conversion from windows-1252 has failed
				$data = `textutil -convert txt -inputencoding macintosh -stdout "$path" 2>/dev/null`;
			} else { # successfully converted from windows-1252 but it may be wrong
				$data = decode_utf8($data);
				if ( $data =~ /\b[\xD2\xD4]|[\xD3\xD5]\b|\xD5\p{Lower}/ ) {
					$data = `textutil -convert txt -inputencoding macintosh -stdout "$path" 2>/dev/null`;
					$data = decode_utf8($data);
				};
			};
			unlink $conv_err;
		};
	end
	$doc = Document.new false
	$doc.clearAndDisableUndoHistory
	$range = Range.new 0, $doc.text.length
	$doc.text.replaceInRange $range, $data
end

### end of macro ###

ATOM RSS1 RSS2