(*

Eudora script:

Transform bullet list (with hyphen) to soft-wrapped text.

Markus Frischknecht, copyright 1999, mailto:mfrischknecht@access.ch

Version 1.0, 28/8/99.
This script is free. Use and change at will, but if you release a modified
version please include the copyright above.

Note that selected text from Eudora will always return a last paragraph that is 
empty if the selected text ends with a CR (i.e. if the selection contains a 
complete paragraph at the end, i.e. CR as very last character). Therefore count of 
paragraphs is reduced by one before entering repeat loop.

Example: the three lines

- first point should go on like this on the following, i.e.
  the second line; maybe add an empty line after each bulleted point?
- and the second line
- the third line, also the last here in this example

should be changed to:

first point should go on like this on the following, i.e. the second line; maybe add an empty line after each bulleted point?
and the second line
the third line, also the last here in this example

Needs Jon's Commands for checking state of control key.

*)

-- is control character down? if so, remove empty lines between bullet list entries

if "Control" is in (keys pressed) then
	set translateEmptyLine to false
else
	set translateEmptyLine to true
end if


-- set up double tell mechanism, a generic way for identifying programs
-- under any System from Mac OS 7.5 to 8.5 (and probably later)

tell application "Finder"
	try
		set EU to (process 1 whose creator type is "CSOm") as «class psn »
	on error
		-- Eudora is not running, so abort
		beep
		return
	end try
end tell


tell application "Eudora Pro 3.1.3" to tell EU
	
	-- try on error clause around everything
	try
		
		set bullet to "- " -- maybe " - "
		set indent to "  " -- maybe "   "
		set indentwidth to count characters in indent -- or count text?
		
		
		set sel to selected text
		set paraCount to count paragraphs in sel
		
		-- set to true if preceding line appended but no carriage return added at end;
		-- i.e. start or in middle of soft-wrapped para
		set wrappingContinued to false
		
		set finalText to ""
		
		repeat with i from 1 to (paraCount - 1) -- (minus 1 necessary?)
			
			set paraText to paragraph i of sel
			
			if paraText is not "" then
				
				-- process paragraph if not empty
				
				-- set paraDone to false
				
				
				set startOfLine to text 1 through indentwidth of paraText -- bullet or indent
				set remainingText to text (indentwidth + 1) through -1 of paraText -- rest
				
				if startOfLine = bullet then
					-- set firstLine to true -- insert CR if not first paragraph and not just empty one above
					
					if wrappingContinued then
						set finalText to finalText & return -- above, line added to soft-wrapped para; add CR
					else
						-- even if line above not soft-wrapped, set flag now
						set wrappingContinued to true -- start of soft-wrapped para, set flag to true
					end if
					
				else if startOfLine = indent then
					-- para continues, insert space if necessary (test last char in existing converted text)
					-- only possible if finalText is not empty, so check first
					if finalText is not "" then
						if (character -1 of finalText) is not " " then
							set finalText to finalText & " "
						end if
					end if
					
				else
					-- If line started with something other than bullet or indent, 
					-- copy it to result as is, without any changes, including a return at end.
					-- Same as with bullet (i.e. start new line, do not add this as continuation
					-- of previous soft-wrapped line), but use full paragraph to add plus CR.
					
					-- for previous line: check if return is necessary to finish soft-wrapped para
					
					if wrappingContinued then
						-- above, line was added to soft-wrapped para; now add CR to finish that para
						set finalText to finalText & return
					end if
					
					set remainingText to paraText & return
					
				end if
				
				-- now add remaining of paragraph text to final
				set finalPara to remainingText
				
			else
				-- empty paragraph
				if wrappingContinued then
					-- if soft-wrapped para was in progress, finish it by adding CR
					set finalText to finalText & return
					set wrappingContinued to false -- and reset flag to "next para is start of soft-wrapped para"
				end if
				
				if translateEmptyLine then
					-- paragraph is empty: just add CR, i.e. empty line; but if control key
					-- was pressed, ignore empty lines, i.e. don't add them in changed text
					set finalPara to return
				end if
				
			end if -- if paraText is not ""
			
			
			set finalText to finalText & finalPara
			
			
		end repeat -- for number of paragraphs
		
		-- at the end, maybe final CR omitted because last para is always empty if the 
		-- selected text ended with a CR; so not necessary if translateEmptyLine is false
		-- because in that case, last CR was not added above
		
		if wrappingContinued then
			set finalText to finalText & return
		end if
		
		-- and insert into Eudora window, replacing selected text
		set selected text to finalText
		
	on error
		-- in case no selection exists, or no editable window open
		beep
	end try
	
	
end tell