jed-users mailing list

[2006 Date Index] [2006 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

UTF-8 upgrade helper


Hi,

I've written these helper for upgrading from files from iso-8859-1 to
UTF-8. Maybe, they are usefull for someone else.

#if (_slang_utf8_ok)
define lat1_to_utf8()
{
    push_spot();
    bob();
    do
    {
        skip_chars("[[:print:][:cntrl:]]");
	variable ch = what_char();
	if (ch < 0)
        {
            del();
            insert_char(-ch);
        }
    } while ( not eobp() );
    pop_spot();
}

define utf8_to_lat1 ()
{
    push_spot();
    bob();
    do
    {
	variable ch = what_char ();
	if ((ch >= 128) and (ch < 256))
        {
            del();
            insert_byte(ch);
        }
    } while ( right(1) );
    pop_spot ();
}

define lat1_to_utf8_hook()
{
    if (_NARGS == 1)
      pop();

    if (andelse {blocal_var_exists("do_utf8_to_lat1")}
                {not get_blocal_var("do_utf8_to_lat1")})
      return;

    push_spot();
    try
    {
        bob();
        skip_chars("[[:print:][:cntrl:]]");

        if ( eobp() )
        {
            create_blocal_var("do_utf8_to_lat1");
            set_blocal_var(0, "do_utf8_to_lat1");
            return;
        }

        !if ( blocal_var_exists("do_utf8_to_lat1") )
        {
            create_blocal_var("do_utf8_to_lat1");
            set_blocal_var(0, "do_utf8_to_lat1");
            update(1);
            flush("This buffer contains non-UTF-8 chars. Convert them UTF-8? y/n ");
            if ( getkey() != 'y' )
              return;
            flush("Should this buffer be converted back upon save? y/n ");
            set_blocal_var(getkey() == 'y', "do_utf8_to_lat1");
        }

        do
        {
            variable ch = what_char();
            if (ch < 0)
            {
                del();
                insert_char(-ch);
            }
            skip_chars("[[:print:][:cntrl:]]");
        } while ( not eobp() );
        set_buffer_modified_flag(0);
    }
    catch UserBreakError:
      ;
    finally
    {
        pop_spot();
        flush("");
    }
}

define utf8_to_lat1_hook(file)
{
    !if (andelse {blocal_var_exists("do_utf8_to_lat1")}
                 {get_blocal_var("do_utf8_to_lat1")})
      return;

    push_spot();
    bob();
    do
    {
        variable ch = what_char();
        if (ch >= 128 and ch < 256)
        {
            del();
            insert_byte(ch);
        }
    } while ( right(1) );
    pop_spot();
}

append_to_hook("_jed_find_file_after_hooks", &lat1_to_utf8_hook());
append_to_hook("_jed_save_buffer_after_hooks", &lat1_to_utf8_hook());
append_to_hook("_jed_save_buffer_before_hooks", &utf8_to_lat1_hook());
#endif

And I've written some functions for UTF-8 features. Maybe they get a menu
entry "Edit->UTF-8 specials".

#if (_slang_utf8_ok)
define insert_after_char(char)
{
    !if (markp())
      throw UsageError, "You must define a region";

    narrow_to_region();
    try
    {
        bob();
        while (right(1))
          insert_char(char);
    }
    finally
    {
        widen_region();
    }
}

% http://www.utf8-zeichentabelle.de/unicode-utf8-table.pl
define stroke() { insert_after_char(0x336); }
define underline() { insert_after_char(0x332); }
define double_underline() { insert_after_char(0x333); }
define overline() { insert_after_char(0x305); }
define double_overline() { insert_after_char(0x33f); }
#endif

Bye, Jörg.

PS: All above is released under GPLv2.
-- 
Der Wunsch, klug zu erscheinen, verhindert oft, es zu werden.
    	    	    			      (Francois de la Rochefoucauld)

--------------------------
To unsubscribe send email to <jed-users-request@xxxxxxxxxxx> with
the word "unsubscribe" in the message body.
Need help? Email <jed-users-owner@xxxxxxxxxxx>.


[2006 date index] [2006 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]