Reply to topic  [ 6 posts ] 
Last Character in a Line (Soft Hyphens) 
Author Message

Joined: 2014-02-08 12:57:03
Posts: 170
Location: Australia
G’day, all

My world seems to be revolving around hyphens at the moment.

My current interest is the manual insertion of hyphens in words that are too long to fit at the end of one line but whose wrapping to the next line leaves too much white space in the (fully justified) previous line. I think that the use of soft hyphens in such cases could avoid the problem associated with “conventional” hyphens which persist inappropriately when subsequent textual manipulations (such as insertions or deletions) result in the word’s appearing all on the one line: soft hyphens, by contrast, magically appear and disappear as required.

The problem with soft hyphens is that Nisus Writer Pro displays a soft hyphen differently from a conventional hyphen, so all instances of the former which occur at the end of a line — and only those that occur at the end of a line! — will eventually need to be replaced by instances of the latter. Once this is done, it’s easy enough to delete the remaining soft hyphens if desired.

So — and this is another recurring theme in my life that I haven’t yet mastered — I somehow need to be able to determine whether a given character — in this case, the soft hyphen character — occurs at the end of a line or not.

The aim is to have a macro that one runs as part of one’s suite of processes just before a document is gift-wrapped for the world at large.

Any help would be appreciated.

Cheers,
Adrian

_________________
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996


2018-01-31 02:24:59
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hello adryan,

don't know much about soft-hyphens, but the following code should return an array of text selections for all line-final hyphens:
Code:
$doc = Document.active
$text = $doc.text

$hyphen = '-'
$hyphenSels = $text.find $hyphen, 'a'

$lineEndHyphens = Array.new
foreach $sel in $hyphenSels
    if $sel.bound == $text.rangeOfLineAtIndex($sel.location).bound
        $lineEndHyphens.push $sel
    end
end

Obviously you will have to add code to actually do something with those selections. If you are working with soft-hyphens you should be able to get the same to work by replacing the $hyphen assignment. It might be best to work with character codes, so something like:
Code:
$hyphen = Text.newWithCharacter XXX

_________________
philip


2018-01-31 02:52:47
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Come to think of it, I think your full macro would look something like this:
Code:
$doc = Document.active
$text = $doc.text

$hyphen = '-'
$softHyphen = Text.newWithCharacter XXX # replace with actual code
$hyphenSels = $text.find $softHyphen, 'a'

foreach $sel in $hyphenSels
    if $sel.bound == $text.rangeOfLineAtIndex($sel.location).bound
        $text.replaceInRange $sel.range, $hyphen
    end
end

_________________
philip


2018-01-31 02:57:13
Profile

Joined: 2014-02-08 12:57:03
Posts: 170
Location: Australia
G’day, Philip et al

That’s really great, Philip! Thanks for that. I think I now understand the workings of the rangeOfLineAtIndex command better.

Your second macro does the trick with 173 as the code for a soft hyphen. (I got this code from the Entities Palette of BBEdit.)

There is, however, one teensy-weensy problem; viz, the end-of-line hyphens inserted by the macro do not assume the attributes of the surrounding characters. (This seems to be a consequence of the soft hyphen’s failing to do so in the first place, which might be considered a bug in Nisus Writer.) I can easily fix this with a simple PowerFind Pro operation, but I assume there is some way it could be handled by the macro language.

When the macro is finalized, I suggest naming it “EOL Soft Hyphens to Hyphens”, unless you prefer something else.

Cheers,
Adrian

_________________
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996


2018-01-31 14:23:04
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
adryan wrote:
There is, however, one teensy-weensy problem; viz, the end-of-line hyphens inserted by the macro do not assume the attributes of the surrounding characters. (This seems to be a consequence of the soft hyphen’s failing to do so in the first place, which might be considered a bug in Nisus Writer.)

Really?
I would try the following first. Change the assignment in the macro to
Code:
$hyphen = Cast to String '-'


adryan wrote:
I suggest naming it “EOL Soft Hyphens to Hyphens”, unless you prefer something else.

Feel free to consider it your macro.

_________________
philip


2018-01-31 17:33:43
Profile

Joined: 2014-02-08 12:57:03
Posts: 170
Location: Australia
G'day, Philip et al

And another big thank-you, Philip. It all now works as advertised. A nice addition to the macro collection.

Cheers,
Adrian

_________________
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996


2018-01-31 20:31:17
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software