/* Style.fse by Troels Walsted Hansen
** $VER: Style.fse 2.0 (30.11.94)
**
** Purpose: Apply style to word.
**
** History:
** Style.fse v2.00ß (30.11.94)
**  · rewritten completely
**  · determines whether word contains illegal characters
**  · works as a toggle - run once to place one set of stylechars,
**    run again to remove them
**  · doesn't handle characters at the left or right of a word correctly :(
**    e.g. '>>hello,' becomes '*>>hello,*' instead of '>>*hello*,'

*/

options results
parse arg portname stylechar
address(portname)

XPOS
cur = result

GETLINE
line = result

/* If the current character is non-alphabetic, then start one character
** over to the left.  This allows the cursor to be immediately after
** the key word (say on a space or bracket.)
*/

/* find word */

totalwnumber = words(line)
if(totalwnumber < 1) then call QuitWithError('There''s no text on this line.')

do i=1 to totalwnumber
	if(cur < wordindex(line, i)) then break
end

/* some useful variables for later on */

styleword = word(line, i-1)
wordnumber = i-1
wordlength = length(styleword)
wordstart = wordindex(line, wordnumber)

/* determine whether some other character than `: ; . , ! ) ( " ? >'
** has been placed at the beginning or end of the line
*/

leftchar = left(styleword, 1)
rightchar = right(styleword, 1)

if(translate(leftchar, '..................................................................................', '*/_#:;.,!)("?>qwertyuiopasdfghjklzxcvbnmøæåQWERTYUIOPASDFGHJKLZXCVBNMØÆÅ1234567890') ~= '.') then
	call QuitWithError('Leftmost character is illegal.')

if(translate(rightchar, '..................................................................................', '*/_#:;.,!)("?>qwertyuiopasdfghjklzxcvbnmøæåQWERTYUIOPASDFGHJKLZXCVBNMØÆÅ1234567890') ~= '.') then
	call QuitWithError('Rightmost character is illegal.')

/* if the current stylechars are already in place, remove them and exit */

if(leftchar = stylechar & rightchar = stylechar & wordlength > 2) then
do
	SETPOS X wordstart
	INSERTINPUT '7F'x

	SETPOS X wordstart+wordlength-2
	INSERTINPUT '7F'x

	exit
end

/* if other stylechars then the current ones are present, ignore them */

if(leftchar = '*' | leftchar = '/' | leftchar = '_' | leftchar = '#' & leftchar = rightchar) then
	fixedstyleword = strip(styleword, B, '*/_#')
else
	fixedstyleword = styleword

translated = translate(fixedstyleword, '......................................................................................................', '->!:;@.,"()[]?''\®©þ¡ßð×¹²³¢¼½¾ÞÐ£Çç¿qwertyuiopasdfghjklzxcvbnmøæåQWERTYUIOPASDFGHJKLZXCVBNMØÆÅ1234567890')

dotword = ''
do length(fixedstyleword)
	dotword = dotword || '.'
end

if(translated ~= dotword) then call QuitWithError('Word contain(s) illegal character(s).')

/* place stylechars */

SETPOS X wordstart
INSERTSTRING strip(stylechar)

SETPOS X wordstart+wordlength+1
INSERTSTRING strip(stylechar)

SETPOS wordstart+wordlength+2

exit

QuitWithError:
	parse arg errorstring
	REQUESTNOTIFY TEXT '"'errorstring'"' BT '"_Ok"'
	exit
end
