home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 December (Special) / PCWorld_2005-12_Special_cd.bin / Bezpecnost / lsti / lsti.exe / framework-2.5.exe / euc_jp.pm < prev    next >
Text File  |  2001-04-22  |  5KB  |  188 lines

  1. #!/usr/local/bin/perl
  2. #
  3. #    euc_jp.pm : EUC Japanese Character Support Functions
  4. #        This modules is experimental.  API may be changed.
  5. #
  6. #    $Id: euc_jp.pm,v 1.2 2001-04-22 22:35:41+09 hayashi Exp $
  7. #
  8. #    Copyright (c) 2001 Hiroo Hayashi.  All rights reserved.
  9. #
  10. #    This program is free software; you can redistribute it and/or
  11. #    modify it under the same terms as Perl itself.
  12. #
  13.  
  14. package Term::ReadLine::Gnu::XS;
  15.  
  16. use Carp;
  17. use strict;
  18.  
  19. # make aliases
  20. use vars qw(%Attribs);
  21. *Attribs = \%Term::ReadLine::Gnu::Attribs;
  22.  
  23. # enable Meta
  24. rl_prep_terminal(1);
  25.  
  26. rl_add_defun('euc-jp-forward', \&ej_forward);
  27. rl_add_defun('euc-jp-backward', \&ej_backward);
  28. rl_add_defun('euc-jp-backward-delete-char', \&ej_rubout);
  29. rl_add_defun('euc-jp-delete-char', \&ej_delete);
  30. rl_add_defun('euc-jp-forward-backward-delete-char', \&ej_rubout_or_delete);
  31. rl_add_defun('euc-jp-transpose-chars', \&ej_transpose_chars);
  32.  
  33. rl_bind_key(ord "\cf", 'euc-jp-forward');
  34. rl_bind_key(ord "\cb", 'euc-jp-backward');
  35. rl_bind_key(ord "\ch", 'euc-jp-backward-delete-char');
  36. #rl_bind_key(ord "\cd", 'euc-jp-delete-char');
  37. rl_bind_key(ord "\cd", 'euc-jp-forward-backward-delete-char');
  38. rl_bind_key(ord "\ct", 'euc-jp-transpose-chars');
  39.  
  40. 1;
  41.  
  42. #    An EUC Japanese character consists of two 8 bit characters.
  43. #    And the MSBs (most significant bit) of both bytes are set.
  44.  
  45. #    To support Shift-JIS charactor set the following two functions
  46. #    must be extended.
  47. sub ej_first_byte_p {
  48.     my ($p) = @_;
  49.     my $l = $Attribs{line_buffer};
  50.     return substr($l, $p, 1) =~ /[\x80-\xff]/
  51.     && substr($l, 0, $p) =~ /^([\x00-x7f]|([\x80-\xff][\x80-\xff]))*$/;
  52. }
  53.  
  54. sub ej_second_byte_p {
  55.     my ($p) = @_;
  56.     my $l = $Attribs{line_buffer};
  57.     return $p > 0 && substr($l, $p, 1) =~ /[\x80-\xff]/
  58.     && substr($l, 0, $p) !~ /^([\x00-x7f]|([\x80-\xff][\x80-\xff]))*$/;
  59. }
  60.  
  61. #forward-char
  62. sub ej_forward {
  63.     my($count, $key) = @_;
  64.     if ($count < 0) {
  65.     ej_backward(-$count, $key);
  66.     } else  {
  67.     while ($count--) {
  68.         if (ej_first_byte_p($Attribs{point})) {
  69.         rl_call_function('forward-char', 2, $key);
  70.         } else {
  71.         rl_call_function('forward-char', 1, $key);
  72.         }
  73.     }
  74.     }
  75.     return 0;
  76. }
  77.  
  78. #backward-char
  79. sub ej_backward {
  80.     my($count, $key) = @_;
  81.     if ($count < 0) {
  82.     ej_forward(-$count, $key);
  83.     } else  {
  84.     while ($count--) {
  85.         if (ej_second_byte_p($Attribs{point})) {
  86.         rl_call_function('backward-char', 1, $key);
  87.         }
  88.         if (ej_second_byte_p($Attribs{point} - 1)) {
  89.         rl_call_function('backward-char', 2, $key);
  90.         } else {
  91.         rl_call_function('backward-char', 1, $key);
  92.         }
  93.     }
  94.     }
  95.     return 0;
  96. }
  97.  
  98. #backward-delete-char
  99. sub ej_rubout {
  100.     my($count, $key) = @_;
  101.     if ($count < 0) {
  102.     ej_delete(-$count, $key);
  103.     } else  {
  104.     if ($Attribs{point} <= 0) {
  105.         rl_ding();
  106.         return 1;
  107.     }
  108.     while ($count--) {
  109.         if (ej_second_byte_p($Attribs{point})) {
  110.         $Attribs{point}--;
  111.         }
  112.         if (ej_second_byte_p($Attribs{point} - 1)) {
  113.         rl_call_function('backward-delete-char', 2, $key);
  114.         } else {
  115.         rl_call_function('backward-delete-char', 1, $key);
  116.         }
  117.     }
  118.     }
  119.     return 0;
  120. }
  121.  
  122. #delete-char
  123. sub ej_delete {
  124.     my($count, $key) = @_;
  125.     if ($count < 0) {
  126.     ej_rubout(-$count, $key);
  127.     } else  {
  128.     while ($count--) {
  129.         if (ej_first_byte_p($Attribs{point})) {
  130.         rl_call_function('delete-char', 2, $key);
  131.         } elsif (ej_second_byte_p($Attribs{point})) {
  132.         rl_call_function('backward-delete-char', 1, $key);
  133.         rl_call_function('delete-char', 1, $key);
  134.         } else {
  135.         rl_call_function('delete-char', 1, $key);
  136.         }
  137.     }
  138.     }
  139.     return 0;
  140. }
  141.  
  142. #forward-backward-delete-char
  143. sub ej_rubout_or_delete {
  144.     my($count, $key) = @_;
  145.     if ($Attribs{end} != 0 && $Attribs{point} == $Attribs{end}) {
  146.     return ej_rubout($count, $key);
  147.     } else  {
  148.     return ej_delete($count, $key);
  149.     }
  150. }
  151.  
  152. #transpose-chars
  153. sub ej_transpose_chars {
  154.     my($count, $key) = @_;
  155.  
  156.     return 0 unless $count;
  157.  
  158.     if (ej_second_byte_p($Attribs{point})) {
  159.     $Attribs{point}--;
  160.     }
  161.     if ($Attribs{point}    == 0    # the beginning of the line
  162.     || ($Attribs{end} < 2)    # only one ascii char
  163.     # only one EUC char
  164.     || ($Attribs{end} == 2 && ej_first_byte_p(0))) {
  165.     rl_ding();
  166.     return -1;
  167.     }
  168.     rl_begin_undo_group();
  169.     if ($Attribs{point} == $Attribs{end}) {
  170.     # If point is at the end of the line
  171.     ej_backward(1, $key);
  172.     $count = 1;
  173.     }
  174.     ej_backward(1, $key);
  175.     my $dummy;
  176.     if (ej_first_byte_p($Attribs{point})) {
  177.     $dummy = substr($Attribs{line_buffer}, $Attribs{point}, 2);
  178.     rl_delete_text($Attribs{point}, $Attribs{point} + 2);
  179.     } else {
  180.     $dummy = substr($Attribs{line_buffer}, $Attribs{point}, 1);
  181.     rl_delete_text($Attribs{point}, $Attribs{point} + 1);
  182.     }
  183.     ej_forward($count, $key);
  184.     rl_insert_text($dummy);
  185.     rl_end_undo_group();
  186.     return 0;
  187. }
  188.