ethio-util.el 69.3 KB
Newer Older
1
;;; ethio-util.el --- utilities for Ethiopic
Karl Heuer's avatar
Karl Heuer committed
2

3
;; Copyright (C) 1997 Electrotechnical Laboratory, JAPAN.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;; Licensed to the Free Software Foundation.
Karl Heuer's avatar
Karl Heuer committed
5

Kenichi Handa's avatar
Kenichi Handa committed
6
;; Keywords: mule, multilingual, Ethiopic
Karl Heuer's avatar
Karl Heuer committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Kenichi Handa's avatar
Kenichi Handa committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Kenichi Handa's avatar
Kenichi Handa committed
24 25

;; Author: TAKAHASHI Naoto <ntakahas@etl.go.jp>
Karl Heuer's avatar
Karl Heuer committed
26 27 28

;;; Code:

29 30 31 32 33
;;;###autoload
(defun setup-ethiopic-environment ()
  "Setup multilingual environment for Ethiopic."
  (interactive)
  (setup-english-environment)
34
  (setq default-input-method "ethiopic"))
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
;;
;; Ethio minor mode
;;

(defvar ethio-mode nil "Non-nil if in Ethio minor mode.")
(make-variable-buffer-local 'ethio-mode)

(or (assq 'ethio-mode minor-mode-alist)
    (setq minor-mode-alist
	  (cons '(ethio-mode " Ethio") minor-mode-alist)))

(defvar ethio-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map " "     'ethio-insert-space)
    (define-key map [?\S- ] 'ethio-insert-ethio-space)
    (define-key map [?\C-'] 'ethio-gemination)
    (define-key map [f2]    'ethio-toggle-space)
    (define-key map [S-f2]  'ethio-replace-space) ; as requested
    (define-key map [f3]    'ethio-toggle-punctuation)
    (define-key map [f4]    'ethio-sera-to-fidel-buffer)
    (define-key map [S-f4]  'ethio-sera-to-fidel-region)
    (define-key map [C-f4]  'ethio-sera-to-fidel-mail-or-marker)
    (define-key map [f5]    'ethio-fidel-to-sera-buffer)
    (define-key map [S-f5]  'ethio-fidel-to-sera-region)
    (define-key map [C-f5]  'ethio-fidel-to-sera-mail-or-marker)
    (define-key map [f6]    'ethio-modify-vowel)
    (define-key map [f7]    'ethio-replace-space)
    (define-key map [f8]    'ethio-input-special-character)
    map)
  "Keymap for Ethio minor mode.")

(or (assq 'ethio-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'ethio-mode ethio-mode-map) minor-mode-map-alist)))

;;;###autoload
(defun ethio-mode (&optional arg)
  "Toggle Ethio minor mode.
With arg, turn Ethio mode on if and only if arg is positive.

Also, Ethio minor mode is automatically turned on
when you activate the Ethiopic quail package.

The keys that are defined in ethio-mode are:
\\{ethio-mode-map}"

  (interactive)
  (setq ethio-mode
	(if (null arg) (not ethio-mode)
	  (> (prefix-numeric-value arg) 0)))
  (if ethio-mode
      (progn
	(add-hook 'find-file-hooks 'ethio-find-file)
	(add-hook 'write-file-hooks 'ethio-write-file)
	(add-hook 'after-save-hook 'ethio-find-file))
    (remove-hook 'find-file-hooks 'ethio-find-file)
    (remove-hook 'write-file-hooks 'ethio-write-file)
    (remove-hook 'after-save-hook 'ethio-find-file)))
94

Karl Heuer's avatar
Karl Heuer committed
95 96 97 98
;;
;; ETHIOPIC UTILITY FUNCTIONS
;;

Kenichi Handa's avatar
Kenichi Handa committed
99 100 101 102 103 104 105 106 107 108
;; If the filename ends in ".sera", editing is done in fidel
;; but file I/O is done in SERA.
;;
;; If the filename ends in ".java", editing is done in fidel
;; but file I/O is done in the \uXXXX style, where XXXX is 
;; the Unicode codepoint for the Ethiopic character.
;;
;; If the filename ends in ".tex", editing is done in fidel
;; but file I/O is done in EthioTeX format.
;;
Karl Heuer's avatar
Karl Heuer committed
109
;; To automatically convert Ethiopic text to SERA format when sending mail,
Kenichi Handa's avatar
Kenichi Handa committed
110
;;   (add-hook 'mail-send-hook 'ethio-fidel-to-sera-mail)
Karl Heuer's avatar
Karl Heuer committed
111 112
;;
;; To automatically convert SERA format to Ethiopic when receiving mail,
Kenichi Handa's avatar
Kenichi Handa committed
113
;;   (add-hook 'rmail-show-message-hook 'ethio-sera-to-fidel-mail)
Karl Heuer's avatar
Karl Heuer committed
114 115
;;
;; To automatically convert Ethiopic text to SERA format when posting news,
Kenichi Handa's avatar
Kenichi Handa committed
116 117
;;   (add-hook 'news-inews-hook 'ethio-fidel-to-sera-mail)

Karl Heuer's avatar
Karl Heuer committed
118
;;
Kenichi Handa's avatar
Kenichi Handa committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
;; users' preference
;;

(defvar ethio-primary-language 'tigrigna
  "*Symbol that defines the primary language in SERA --> FIDEL conversion.
The value should be one of: `tigrigna', `amharic' or `english'.")

(defvar ethio-secondary-language 'english
  "*Symbol that defines the secondary language in SERA --> FIDEL conversion.
The value should be one of: `tigrigna', `amharic' or `english'.")

(defvar ethio-use-colon-for-colon nil
  "*Non-nil means associate ASCII colon with Ethiopic colon.
If nil, associate ASCII colon with Ethiopic word separator, i.e., two
vertically stacked dots.  All SERA <--> FIDEL converters refer this
variable.")

(defvar ethio-use-three-dot-question nil
  "*Non-nil means associate ASCII question mark with Ethiopic old style question mark (three vertically stacked dots).
If nil, associate ASCII question mark with Ethiopic stylised question
mark.  All SERA <--> FIDEL converters refer this variable.")

(defvar ethio-quote-vowel-always nil
  "*Non-nil means always put an apostrophe before an isolated vowel (except at word initial) in FIDEL --> SERA conversion.
If nil, put an apostrophe only between a sixth-form consonant and an
isolated vowel.")

(defvar ethio-W-sixth-always nil
  "*Non-nil means convert the Wu-form of a 12-form consonant to \"W'\" instead of \"Wu\" in FIDEL --> SERA conversion.")

(defvar ethio-numeric-reduction 0
  "*Degree of reduction in converting Ethiopic digits into Arabic digits.
Should be 0, 1 or 2.
For example, ({10}{9}{100}{80}{7}) is converted into:
    `10`9`100`80`7  if `ethio-numeric-reduction' is 0,
    `109100807	    if `ethio-numeric-reduction' is 1,
    `10900807	    if `ethio-numeric-reduction' is 2.")

(defvar ethio-implicit-period-conversion t
  "*Non-nil means replacing the Ethiopic dot at the end of an Ethiopic sentence
with an Ethiopic full stop.")

(defvar ethio-java-save-lowercase nil
  "*Non-nil means save Ethiopic characters in lowercase hex numbers to Java files.
If nil, use uppercases.")
Karl Heuer's avatar
Karl Heuer committed
164 165 166 167

;;
;; SERA to FIDEL
;;
Kenichi Handa's avatar
Kenichi Handa committed
168 169
 
(defconst ethio-sera-to-fidel-table
Karl Heuer's avatar
Karl Heuer committed
170 171 172
  [
   nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
Kenichi Handa's avatar
Kenichi Handa committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
;;; SP
   (" "
    (?: (if ethio-use-colon-for-colon " $(3$l(B" "$(3$h(B")
	(32 (if ethio-use-colon-for-colon " $(3$l(B " "$(3$h(B"))
	(?- " $(3$m(B")
	(?: " $(3$i(B")
	(?| (if ethio-use-colon-for-colon " $(3$l(B|" " $(3$h(B|")
	    (?: " $(3$o(B"))))

;;; !   "   #   $   %   &    '
   nil nil nil nil nil nil ("" (?' "$(3%s(B"))
;;; (   )   *   +    ,      -               .
   nil nil nil nil ("$(3$j(B") ("-" (?: "$(3$l(B")) ("$(3%u(B")
;;;  /   0   1   2   3   4   5   6   7   8   9
    nil nil nil nil nil nil nil nil nil nil nil
;;; :
   ((if ethio-use-colon-for-colon "$(3$l(B" "$(3$h(B")
    (32 (if ethio-use-colon-for-colon "$(3$l(B " "$(3$h(B"))
    (?- "$(3$m(B")
    (?: "$(3$i(B")
    (?| (if ethio-use-colon-for-colon "$(3$l(B|" "$(3$h(B|")
	(?: "$(3$o(B")))
;;;  ;      <              =    >
   ("$(3$k(B") ("<" (?< "$(3%v(B")) nil (">" (?> "$(3%w(B"))
;;; ?
   ((if ethio-use-three-dot-question "$(3$n(B" "$(3%x(B"))
;;; @
    nil
Karl Heuer's avatar
Karl Heuer committed
201
;;; A
Kenichi Handa's avatar
Kenichi Handa committed
202
   ("$(3"f(B" (?2 "$(3#8(B"))
Karl Heuer's avatar
Karl Heuer committed
203
;;; B
Kenichi Handa's avatar
Kenichi Handa committed
204 205
   ("$(3"((B" (?e "$(3"#(B") (?u "$(3"$(B") (?i "$(3"%(B") (?a "$(3"&(B") (?E "$(3"'(B") (?o "$(3")(B")
         (?W "$(3%b(B" (?e "$(3%2(B") (?u "$(3%b(B") (?i "$(3%B(B") (?a "$(3"*(B") (?E "$(3%R(B")))
Karl Heuer's avatar
Karl Heuer committed
206
;;; C
Kenichi Handa's avatar
Kenichi Handa committed
207 208 209
   ("$(3$4(B" (?e "$(3$/(B") (?u "$(3$0(B") (?i "$(3$1(B") (?a "$(3$2(B") (?E "$(3$3(B") (?o "$(3$5(B")
         (?W "$(3$6(B" (?a "$(3$6(B")
                  (?e "$(3$4%n(B") (?u "$(3$4%r(B") (?i "$(3$4%o(B") (?E "$(3$4%q(B")))
Karl Heuer's avatar
Karl Heuer committed
210
;;; D
Kenichi Handa's avatar
Kenichi Handa committed
211 212 213
   ("$(3#b(B" (?e "$(3#](B") (?u "$(3#^(B") (?i "$(3#_(B") (?a "$(3#`(B") (?E "$(3#a(B") (?o "$(3#c(B")
         (?W "$(3#d(B" (?a "$(3#d(B")
                  (?e "$(3#b%n(B") (?u "$(3#b%r(B") (?i "$(3#b%o(B") (?E "$(3#b%q(B")))
Karl Heuer's avatar
Karl Heuer committed
214
;;; E
Kenichi Handa's avatar
Kenichi Handa committed
215
   ("$(3"g(B" (?2 "$(3#9(B"))
Karl Heuer's avatar
Karl Heuer committed
216
;;; F
Kenichi Handa's avatar
Kenichi Handa committed
217 218 219
   ("$(3$T(B" (?e "$(3$O(B") (?u "$(3$P(B") (?i "$(3$Q(B") (?a "$(3$R(B") (?E "$(3$S(B") (?o "$(3$U(B")
         (?W "$(3%d(B" (?e "$(3%4(B") (?u "$(3%d(B") (?i "$(3%D(B") (?a "$(3$V(B") (?E "$(3%T(B"))
	 (?Y "$(3$a(B" (?a "$(3$a(B")))
Karl Heuer's avatar
Karl Heuer committed
220
;;; G
Kenichi Handa's avatar
Kenichi Handa committed
221 222
   ("$(3$$(B" (?e "$(3#}(B") (?u "$(3#~(B") (?i "$(3$!(B") (?a "$(3$"(B") (?E "$(3$#(B") (?o "$(3$%(B")
         (?W "$(3%c(B" (?e "$(3%3(B") (?u "$(3%c(B") (?i "$(3%C(B") (?a "$(3$&(B") (?E "$(3%S(B")))
Karl Heuer's avatar
Karl Heuer committed
223
;;; H
Kenichi Handa's avatar
Kenichi Handa committed
224 225 226
   ("$(3!6(B" (?e "$(3!1(B") (?u "$(3!2(B") (?i "$(3!3(B") (?a "$(3!4(B") (?E "$(3!5(B") (?o "$(3!7(B")
         (?W "$(3!8(B" (?a "$(3!8(B")
                  (?e "$(3!6%n(B") (?u "$(3!6%r(B") (?i "$(3!6%o(B") (?E "$(3!6%q(B")))
Karl Heuer's avatar
Karl Heuer committed
227
;;; I
Kenichi Handa's avatar
Kenichi Handa committed
228
   ("$(3"h(B" (?2 "$(3#:(B"))
Karl Heuer's avatar
Karl Heuer committed
229
;;; J
Kenichi Handa's avatar
Kenichi Handa committed
230 231 232
   ("$(3#j(B" (?e "$(3#e(B") (?u "$(3#f(B") (?i "$(3#g(B") (?a "$(3#h(B") (?E "$(3#i(B") (?o "$(3#k(B") 
         (?W "$(3#l(B" (?a "$(3#l(B")
	          (?e "$(3#j%n(B") (?u "$(3#j%r(B") (?i "$(3#j%o(B") (?E "$(3#j%q(B")))
Karl Heuer's avatar
Karl Heuer committed
233
;;; K
Kenichi Handa's avatar
Kenichi Handa committed
234 235
   ("$(3#"(B" (?e "$(3"{(B") (?u "$(3"|(B") (?i "$(3"}(B") (?a "$(3"~(B") (?E "$(3#!(B") (?o "$(3##(B")
         (?W "$(3#*(B" (?e "$(3#%(B") (?u "$(3#*(B") (?i "$(3#'(B") (?a "$(3#((B") (?E "$(3#)(B")))
Karl Heuer's avatar
Karl Heuer committed
236
;;; L
Kenichi Handa's avatar
Kenichi Handa committed
237 238 239
   ("$(3!.(B" (?e "$(3!)(B") (?u "$(3!*(B") (?i "$(3!+(B") (?a "$(3!,(B") (?E "$(3!-(B") (?o "$(3!/(B")
         (?W "$(3!0(B" (?a "$(3!0(B")
                  (?e "$(3!.%n(B") (?u "$(3!.%r(B") (?i "$(3!.%o(B") (?E "$(3!.%q(B")))
Karl Heuer's avatar
Karl Heuer committed
240
;;; M
Kenichi Handa's avatar
Kenichi Handa committed
241 242 243
   ("$(3!>(B" (?e "$(3!9(B") (?u "$(3!:(B") (?i "$(3!;(B") (?a "$(3!<(B") (?E "$(3!=(B") (?o "$(3!?(B")
         (?W "$(3%a(B" (?e "$(3%1(B") (?u "$(3%a(B") (?i "$(3%A(B") (?a "$(3!@(B") (?E "$(3%Q(B"))
	 (?Y "$(3$_(B" (?a "$(3$_(B")))
Karl Heuer's avatar
Karl Heuer committed
244
;;; N
Kenichi Handa's avatar
Kenichi Handa committed
245 246 247
   ("$(3"`(B" (?e "$(3"[(B") (?u "$(3"\(B") (?i "$(3"](B") (?a "$(3"^(B") (?E "$(3"_(B") (?o "$(3"a(B")
         (?W "$(3"b(B" (?a "$(3"b(B")
                  (?e "$(3"`%n(B") (?u "$(3"`%r(B") (?i "$(3"`%o(B") (?E "$(3"`%q(B")))
Karl Heuer's avatar
Karl Heuer committed
248
;;; O
Kenichi Handa's avatar
Kenichi Handa committed
249
   ("$(3"i(B" (?2 "$(3#;(B"))
Karl Heuer's avatar
Karl Heuer committed
250
;;; P
Kenichi Handa's avatar
Kenichi Handa committed
251 252 253
   ("$(3$<(B" (?e "$(3$7(B") (?u "$(3$8(B") (?i "$(3$9(B") (?a "$(3$:(B") (?E "$(3$;(B") (?o "$(3$=(B")
         (?W "$(3$>(B" (?a "$(3$>(B")
                  (?e "$(3$<%n(B") (?u "$(3$<%r(B") (?i "$(3$<%o(B") (?E "$(3$<%q(B")))
Karl Heuer's avatar
Karl Heuer committed
254
;;; Q
Kenichi Handa's avatar
Kenichi Handa committed
255 256
   ("$(3!v(B" (?e "$(3!q(B") (?u "$(3!r(B") (?i "$(3!s(B") (?a "$(3!t(B") (?E "$(3!u(B") (?o "$(3!w(B")
         (?W "$(3!~(B" (?e "$(3!y(B") (?u "$(3!~(B") (?i "$(3!{(B") (?a "$(3!|(B") (?E "$(3!}(B")))
Karl Heuer's avatar
Karl Heuer committed
257
;;; R
Kenichi Handa's avatar
Kenichi Handa committed
258 259 260 261
   ("$(3!N(B" (?e "$(3!I(B") (?u "$(3!J(B") (?i "$(3!K(B") (?a "$(3!L(B") (?E "$(3!M(B") (?o "$(3!O(B")
         (?W "$(3!P(B" (?a "$(3!P(B")
	          (?e "$(3!N%n(B") (?u "$(3!N%r(B") (?i "$(3!N%o(B") (?E "$(3!N%q(B"))
         (?Y "$(3$`(B" (?a "$(3$`(B")))
Karl Heuer's avatar
Karl Heuer committed
262
;;; S
Kenichi Handa's avatar
Kenichi Handa committed
263 264 265 266 267 268 269
   ("$(3$D(B" (?e "$(3$?(B") (?u "$(3$@(B") (?i "$(3$A(B") (?a "$(3$B(B") (?E "$(3$C(B") (?o "$(3$E(B")
         (?W "$(3$F(B" (?a "$(3$F(B")
                  (?e "$(3$D%n(B") (?u "$(3$D%r(B") (?i "$(3$D%o(B") (?E "$(3$D%q(B"))
	 (?2 "$(3$L(B"
	     (?e "$(3$G(B") (?u "$(3$H(B") (?i "$(3$I(B") (?a "$(3$J(B") (?E "$(3$K(B") (?o "$(3$M(B")
	     (?W "$(3$F(B" (?a "$(3$F(B")
		 (?e "$(3$L%n(B") (?u "$(3$L%r(B") (?i "$(3$L%o(B") (?E "$(3$L%q(B"))))
Karl Heuer's avatar
Karl Heuer committed
270
;;; T
Kenichi Handa's avatar
Kenichi Handa committed
271 272 273
   ("$(3$,(B" (?e "$(3$'(B") (?u "$(3$((B") (?i "$(3$)(B") (?a "$(3$*(B") (?E "$(3$+(B") (?o "$(3$-(B")
         (?W "$(3$.(B" (?a "$(3$.(B")
	          (?e "$(3$,%n(B") (?u "$(3$,%r(B") (?i "$(3$,%o(B") (?E "$(3$,%q(B")))
Karl Heuer's avatar
Karl Heuer committed
274
;;; U
Kenichi Handa's avatar
Kenichi Handa committed
275
   ("$(3"d(B" (?2 "$(3#6(B"))
Karl Heuer's avatar
Karl Heuer committed
276
;;; V
Kenichi Handa's avatar
Kenichi Handa committed
277 278 279
   ("$(3"0(B" (?e "$(3"+(B") (?u "$(3",(B") (?i "$(3"-(B") (?a "$(3".(B") (?E "$(3"/(B") (?o "$(3"1(B")
         (?W "$(3"2(B" (?a "$(3"2(B")
	          (?e "$(3"0%n(B") (?u "$(3"0%r(B") (?i "$(3"0%o(B") (?E "$(3"0%q(B")))
Karl Heuer's avatar
Karl Heuer committed
280
;;; W
Kenichi Handa's avatar
Kenichi Handa committed
281
   ("$(3%r(B" (?e "$(3%n(B") (?u "$(3%r(B") (?i "$(3%o(B") (?a "$(3%p(B") (?E "$(3%q(B"))
Karl Heuer's avatar
Karl Heuer committed
282
;;; X
Kenichi Handa's avatar
Kenichi Handa committed
283
   ("$(3%N(B" (?e "$(3%I(B") (?u "$(3%J(B") (?i "$(3%K(B") (?a "$(3%L(B") (?E "$(3%M(B") (?o "$(3%O(B"))
Karl Heuer's avatar
Karl Heuer committed
284
;;; Y
Kenichi Handa's avatar
Kenichi Handa committed
285 286 287
   ("$(3#R(B" (?e "$(3#M(B") (?u "$(3#N(B") (?i "$(3#O(B") (?a "$(3#P(B") (?E "$(3#Q(B") (?o "$(3#S(B")
         (?W "$(3#T(B" (?a "$(3#T(B")
	          (?e "$(3#R%n(B") (?u "$(3#R%r(B") (?i "$(3#R%o(B") (?E "$(3#R%q(B")))
Karl Heuer's avatar
Karl Heuer committed
288
;;; Z
Kenichi Handa's avatar
Kenichi Handa committed
289 290 291
   ("$(3#J(B" (?e "$(3#E(B") (?u "$(3#F(B") (?i "$(3#G(B") (?a "$(3#H(B") (?E "$(3#I(B") (?o "$(3#K(B")
         (?W "$(3#L(B" (?a "$(3#L(B")
	          (?e "$(3#J%n(B") (?u "$(3#J%r(B") (?i "$(3#J%o(B") (?E "$(3#J%q(B")))
Karl Heuer's avatar
Karl Heuer committed
292 293 294
;;; [   \   ]   ^   _
   nil nil nil nil nil
;;; `
Kenichi Handa's avatar
Kenichi Handa committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
   (""
    (?: "$(3$h(B")
    (?? (if ethio-use-three-dot-question "$(3%x(B" "$(3$n(B"))
    (?! "$(3%t(B")
    (?e "$(3#5(B") (?u "$(3#6(B") (?U "$(3#6(B") (?i "$(3#7(B") (?a "$(3#8(B") (?A "$(3#8(B")
        (?E "$(3#9(B") (?I "$(3#:(B") (?o "$(3#;(B") (?O "$(3#;(B")
    (?g "$(3%^(B" 
        (?e "$(3%Y(B") (?u "$(3%Z(B") (?i "$(3%[(B") (?a "$(3%\(B") (?E "$(3%](B") (?o "$(3%_(B"))
    (?h "$(3"H(B"
        (?e "$(3"C(B") (?u "$(3"D(B") (?i "$(3"E(B") (?a "$(3"F(B") (?E "$(3"G(B") (?o "$(3"I(B")
	          (?W "$(3"P(B" (?e "$(3"K(B") (?u "$(3"P(B") (?i "$(3"M(B") (?a "$(3"N(B") (?E "$(3"O(B")))
    (?k "$(3%>(B" 
        (?e "$(3%9(B") (?u "$(3%:(B") (?i "$(3%;(B") (?a "$(3%<(B") (?E "$(3%=(B") (?o "$(3%?(B"))
    (?s "$(3!F(B" 
        (?e "$(3!A(B") (?u "$(3!B(B") (?i "$(3!C(B") (?a "$(3!D(B") (?E "$(3!E(B") (?o "$(3!G(B")
	(?W "$(3!H(B" (?a "$(3!H(B")
		   (?e "$(3!F%n(B") (?u "$(3!F%r(B") (?i "$(3!F%o(B") (?E "$(3!F%q(B")))
    (?S "$(3$L(B" 
	(?e "$(3$G(B") (?u "$(3$H(B") (?i "$(3$I(B") (?a "$(3$J(B") (?E "$(3$K(B") (?o "$(3$M(B")
	(?W "$(3$F(B" (?a "$(3$F(B")
	         (?e "$(3$L%n(B") (?u "$(3$L%r(B") (?i "$(3$L%o(B") (?E "$(3$L%q(B")))
    (?q "$(3%.(B" (?e "$(3%)(B") (?u "$(3%*(B") (?i "$(3%+(B") (?a "$(3%,(B") (?E "$(3%-(B") (?o "$(3%/(B")))
Karl Heuer's avatar
Karl Heuer committed
317
;;; a
Kenichi Handa's avatar
Kenichi Handa committed
318
   ("$(3"f(B" (?2 "$(3#8(B"))
Karl Heuer's avatar
Karl Heuer committed
319
;;; b
Kenichi Handa's avatar
Kenichi Handa committed
320 321
   ("$(3"((B" (?e "$(3"#(B") (?u "$(3"$(B") (?i "$(3"%(B") (?a "$(3"&(B") (?E "$(3"'(B") (?o "$(3")(B")
         (?W "$(3%b(B" (?e "$(3%2(B") (?u "$(3%b(B") (?i "$(3%B(B") (?a "$(3"*(B") (?E "$(3%R(B")))
Karl Heuer's avatar
Karl Heuer committed
322
;;; c
Kenichi Handa's avatar
Kenichi Handa committed
323 324 325
   ("$(3"@(B" (?e "$(3";(B") (?u "$(3"<(B") (?i "$(3"=(B") (?a "$(3">(B") (?E "$(3"?(B") (?o "$(3"A(B")
         (?W "$(3"B(B" (?a "$(3"B(B")
	          (?e "$(3"@%n(B") (?u "$(3"@%r(B") (?i "$(3"@%o(B") (?E "$(3"@%q(B")))
Karl Heuer's avatar
Karl Heuer committed
326
;;; d
Kenichi Handa's avatar
Kenichi Handa committed
327 328 329
   ("$(3#Z(B" (?e "$(3#U(B") (?u "$(3#V(B") (?i "$(3#W(B") (?a "$(3#X(B") (?E "$(3#Y(B") (?o "$(3#[(B")
         (?W "$(3#\(B" (?a "$(3#\(B")
	          (?e "$(3#Z%o(B") (?u "$(3#Z%r(B") (?i "$(3#Z%p(B") (?E "$(3#Z%q(B")))
Karl Heuer's avatar
Karl Heuer committed
330
;;; e
Kenichi Handa's avatar
Kenichi Handa committed
331
   ("$(3"c(B" (?2 "$(3#5(B") (?a "$(3"j(B"))
Karl Heuer's avatar
Karl Heuer committed
332
;;; f
Kenichi Handa's avatar
Kenichi Handa committed
333 334 335
   ("$(3$T(B" (?e "$(3$O(B") (?u "$(3$P(B") (?i "$(3$Q(B") (?a "$(3$R(B") (?E "$(3$S(B") (?o "$(3$U(B")
         (?W "$(3%d(B" (?e "$(3%4(B") (?u "$(3%d(B") (?i "$(3%D(B") (?a "$(3$V(B") (?E "$(3%T(B"))
	 (?Y "$(3$a(B" (?a "$(3$a(B")))
Karl Heuer's avatar
Karl Heuer committed
336
;;; g
Kenichi Handa's avatar
Kenichi Handa committed
337 338 339
   ("$(3#r(B" (?e "$(3#m(B") (?u "$(3#n(B") (?i "$(3#o(B") (?a "$(3#p(B") (?E "$(3#q(B") (?o "$(3#s(B") 
         (?W "$(3#z(B" (?e "$(3#u(B") (?u "$(3#z(B") (?i "$(3#w(B") (?a "$(3#x(B") (?E "$(3#y(B"))
         (?2 "$(3%^(B" (?e "$(3%Y(B") (?u "$(3%Z(B") (?i "$(3%[(B") (?a "$(3%\(B") (?E "$(3%](B") (?o "$(3%_(B")))
Karl Heuer's avatar
Karl Heuer committed
340
;;; h
Kenichi Handa's avatar
Kenichi Handa committed
341 342 343 344
   ("$(3!&(B" (?e "$(3!!(B") (?u "$(3!"(B") (?i "$(3!#(B") (?a "$(3!$(B") (?E "$(3!%(B") (?o "$(3!'(B")
         (?W "$(3"P(B" (?e "$(3"K(B") (?u "$(3"P(B") (?i "$(3"M(B") (?a "$(3"N(B") (?E "$(3"O(B"))
	 (?2 "$(3"H(B" (?e "$(3"C(B") (?u "$(3"D(B") (?i "$(3"E(B") (?a "$(3"F(B") (?E "$(3"G(B") (?o "$(3"I(B")
	          (?W "$(3"P(B" (?e "$(3"K(B") (?u "$(3"P(B") (?i "$(3"M(B") (?a "$(3"N(B") (?E "$(3"O(B"))))
Karl Heuer's avatar
Karl Heuer committed
345
;;; i
Kenichi Handa's avatar
Kenichi Handa committed
346
   ("$(3"e(B" (?2 "$(3#7(B"))
Karl Heuer's avatar
Karl Heuer committed
347
;;; j
Kenichi Handa's avatar
Kenichi Handa committed
348 349 350
   ("$(3#j(B" (?e "$(3#e(B") (?u "$(3#f(B") (?i "$(3#g(B") (?a "$(3#h(B") (?E "$(3#i(B") (?o "$(3#k(B") 
         (?W "$(3#l(B" (?a "$(3#l(B")
	          (?e "$(3#j%n(B") (?u "$(3#j%r(B") (?i "$(3#j%o(B") (?E "$(3#j%q(B")))
Karl Heuer's avatar
Karl Heuer committed
351
;;; k
Kenichi Handa's avatar
Kenichi Handa committed
352 353 354
   ("$(3"p(B" (?e "$(3"k(B") (?u "$(3"l(B") (?i "$(3"m(B") (?a "$(3"n(B") (?E "$(3"o(B") (?o "$(3"q(B")
         (?W "$(3"x(B" (?e "$(3"s(B") (?u "$(3"x(B") (?i "$(3"u(B") (?a "$(3"v(B") (?E "$(3"w(B"))
	 (?2 "$(3%>(B" (?e "$(3%9(B") (?u "$(3%:(B") (?i "$(3%;(B") (?a "$(3%<(B") (?E "$(3%=(B") (?o "$(3%?(B")))
Karl Heuer's avatar
Karl Heuer committed
355
;;; l
Kenichi Handa's avatar
Kenichi Handa committed
356 357 358
   ("$(3!.(B" (?e "$(3!)(B") (?u "$(3!*(B") (?i "$(3!+(B") (?a "$(3!,(B") (?E "$(3!-(B") (?o "$(3!/(B")
         (?W "$(3!0(B" (?a "$(3!0(B")
                  (?e "$(3!.%n(B") (?u "$(3!.%r(B") (?i "$(3!.%o(B") (?E "$(3!.%q(B")))
Karl Heuer's avatar
Karl Heuer committed
359
;;; m
Kenichi Handa's avatar
Kenichi Handa committed
360 361 362
   ("$(3!>(B" (?e "$(3!9(B") (?u "$(3!:(B") (?i "$(3!;(B") (?a "$(3!<(B") (?E "$(3!=(B") (?o "$(3!?(B")
         (?W "$(3%a(B" (?e "$(3%1(B") (?u "$(3%a(B") (?i "$(3%A(B") (?a "$(3!@(B") (?E "$(3%Q(B"))
	 (?Y "$(3$_(B" (?a "$(3$_(B")))
Karl Heuer's avatar
Karl Heuer committed
363
;;; n
Kenichi Handa's avatar
Kenichi Handa committed
364 365 366
   ("$(3"X(B" (?e "$(3"S(B") (?u "$(3"T(B") (?i "$(3"U(B") (?a "$(3"V(B") (?E "$(3"W(B") (?o "$(3"Y(B")
         (?W "$(3"Z(B" (?a "$(3"Z(B")
	          (?e "$(3"X%n(B") (?u "$(3"X%r(B") (?i "$(3"X%o(B") (?E "$(3"X%q(B")))
Karl Heuer's avatar
Karl Heuer committed
367
;;; o
Kenichi Handa's avatar
Kenichi Handa committed
368
   ("$(3"i(B" (?2 "$(3#;(B"))
Karl Heuer's avatar
Karl Heuer committed
369
;;; p
Kenichi Handa's avatar
Kenichi Handa committed
370 371
   ("$(3$\(B" (?e "$(3$W(B") (?u "$(3$X(B") (?i "$(3$Y(B") (?a "$(3$Z(B") (?E "$(3$[(B") (?o "$(3$](B")
         (?W "$(3%e(B" (?e "$(3%5(B") (?u "$(3%e(B") (?i "$(3%E(B") (?a "$(3$^(B") (?E "$(3%U(B")))
Karl Heuer's avatar
Karl Heuer committed
372
;;; q
Kenichi Handa's avatar
Kenichi Handa committed
373 374 375
   ("$(3!f(B" (?e "$(3!a(B") (?u "$(3!b(B") (?i "$(3!c(B") (?a "$(3!d(B") (?E "$(3!e(B") (?o "$(3!g(B")
         (?W "$(3!n(B" (?e "$(3!i(B") (?u "$(3!n(B") (?i "$(3!k(B") (?a "$(3!l(B") (?E "$(3!m(B"))
         (?2 "$(3%.(B" (?e "$(3%)(B") (?u "$(3%*(B") (?i "$(3%+(B") (?a "$(3%,(B") (?E "$(3%-(B") (?o "$(3%/(B")))
Karl Heuer's avatar
Karl Heuer committed
376
;;; r
Kenichi Handa's avatar
Kenichi Handa committed
377 378 379 380
   ("$(3!N(B" (?e "$(3!I(B") (?u "$(3!J(B") (?i "$(3!K(B") (?a "$(3!L(B") (?E "$(3!M(B") (?o "$(3!O(B")
         (?W "$(3!P(B" (?a "$(3!P(B")
	          (?e "$(3!N%n(B") (?u "$(3!N%r(B") (?i "$(3!N%o(B") (?E "$(3!N%q(B"))
         (?Y "$(3$`(B" (?a "$(3$`(B")))
Karl Heuer's avatar
Karl Heuer committed
381
;;; s
Kenichi Handa's avatar
Kenichi Handa committed
382 383 384 385 386 387
   ("$(3!V(B" (?e "$(3!Q(B") (?u "$(3!R(B") (?i "$(3!S(B") (?a "$(3!T(B") (?E "$(3!U(B") (?o "$(3!W(B")
         (?W "$(3!X(B" (?a "$(3!X(B")
	          (?e "$(3!V%n(B") (?u "$(3!V%r(B") (?i "$(3!V%o(B") (?E "$(3!V%q(B"))
	 (?2 "$(3!F(B" (?e "$(3!A(B") (?u "$(3!B(B") (?i "$(3!C(B") (?a "$(3!D(B") (?E "$(3!E(B") (?o "$(3!G(B")
		  (?W "$(3!H(B" (?a "$(3!H(B")
		           (?e "$(3!F%n(B") (?u "$(3!F%r(B") (?i "$(3!F%o(B") (?E "$(3!F%q(B"))))
Karl Heuer's avatar
Karl Heuer committed
388
;;; t
Kenichi Handa's avatar
Kenichi Handa committed
389 390 391
   ("$(3"8(B" (?e "$(3"3(B") (?u "$(3"4(B") (?i "$(3"5(B") (?a "$(3"6(B") (?E "$(3"7(B") (?o "$(3"9(B")
         (?W "$(3":(B" (?a "$(3":(B")
	          (?e "$(3"8%n(B") (?u "$(3"8%r(B") (?i "$(3"8%o(B") (?E "$(3"8%q(B")))
Karl Heuer's avatar
Karl Heuer committed
392
;;; u
Kenichi Handa's avatar
Kenichi Handa committed
393
   ("$(3"d(B" (?2 "$(3#6(B"))
Karl Heuer's avatar
Karl Heuer committed
394
;;; v
Kenichi Handa's avatar
Kenichi Handa committed
395 396 397
   ("$(3"0(B" (?e "$(3"+(B") (?u "$(3",(B") (?i "$(3"-(B") (?a "$(3".(B") (?E "$(3"/(B") (?o "$(3"1(B")
         (?W "$(3"2(B" (?a "$(3"2(B")
	          (?e "$(3"0%n(B") (?u "$(3"0%r(B") (?i "$(3"0%o(B") (?E "$(3"0%q(B")))
Karl Heuer's avatar
Karl Heuer committed
398
;;; w
Kenichi Handa's avatar
Kenichi Handa committed
399 400
   ("$(3#2(B" (?e "$(3#-(B") (?u "$(3#.(B") (?i "$(3#/(B") (?a "$(3#0(B") (?E "$(3#1(B") (?o "$(3#3(B")
         (?W "$(3%p(B" (?e "$(3%n(B") (?u "$(3%r(B") (?i "$(3%o(B") (?a "$(3%p(B") (?E "$(3%q(B")))
Karl Heuer's avatar
Karl Heuer committed
401
;;; x
Kenichi Handa's avatar
Kenichi Handa committed
402 403 404
   ("$(3!^(B" (?e "$(3!Y(B") (?u "$(3!Z(B") (?i "$(3![(B") (?a "$(3!\(B") (?E "$(3!](B") (?o "$(3!_(B")
         (?W "$(3!`(B" (?a "$(3!`(B")
	          (?e "$(3!^%n(B") (?u "$(3!^%r(B") (?i "$(3!^%o(B") (?E "$(3!^%q(B")))
Karl Heuer's avatar
Karl Heuer committed
405
;;; y
Kenichi Handa's avatar
Kenichi Handa committed
406 407 408
   ("$(3#R(B" (?e "$(3#M(B") (?u "$(3#N(B") (?i "$(3#O(B") (?a "$(3#P(B") (?E "$(3#Q(B") (?o "$(3#S(B")
         (?W "$(3#T(B" (?a "$(3#T(B")
	          (?e "$(3#R%n(B") (?u "$(3#R%r(B") (?i "$(3#R%o(B") (?E "$(3#R%q(B")))
Karl Heuer's avatar
Karl Heuer committed
409
;;; z
Kenichi Handa's avatar
Kenichi Handa committed
410 411 412 413 414
   ("$(3#B(B" (?e "$(3#=(B") (?u "$(3#>(B") (?i "$(3#?(B") (?a "$(3#@(B") (?E "$(3#A(B") (?o "$(3#C(B")
         (?W "$(3#D(B" (?a "$(3#D(B")
	          (?e "$(3#B%n(B") (?u "$(3#B%r(B") (?i "$(3#B%o(B") (?E "$(3#B%q(B")))
;;; {   |   }   ~  DEL
   nil nil nil nil nil
Karl Heuer's avatar
Karl Heuer committed
415 416 417
   ])

;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
418 419 420 421 422 423 424 425
(defun ethio-sera-to-fidel-region (beg end &optional secondary force)
  "Convert the characters in region from SERA to FIDEL.
The variable `ethio-primary-language' specifies the primary language
and `ethio-secondary-language' specifies the secondary.

If the 3rd parameter SECONDARY is given and non-nil, assume the region
begins begins with the secondary language; otherwise with the primary
language.
Karl Heuer's avatar
Karl Heuer committed
426

Kenichi Handa's avatar
Kenichi Handa committed
427 428
If the 4th parameter FORCE is given and non-nil, perform conversion
even if the buffer is read-only.
Karl Heuer's avatar
Karl Heuer committed
429

Kenichi Handa's avatar
Kenichi Handa committed
430
See also the descriptions of the variables
Kenichi Handa's avatar
Kenichi Handa committed
431
`ethio-use-colon-for-colon' and
Kenichi Handa's avatar
Kenichi Handa committed
432
`ethio-use-three-dot-question'."
Karl Heuer's avatar
Karl Heuer committed
433 434

  (interactive "r\nP")
Kenichi Handa's avatar
Kenichi Handa committed
435 436 437
  (save-restriction
    (narrow-to-region beg end)
    (ethio-sera-to-fidel-buffer secondary force)))
Karl Heuer's avatar
Karl Heuer committed
438 439

;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
440 441 442 443 444
(defun ethio-sera-to-fidel-buffer (&optional secondary force)
  "Convert the current buffer from SERA to FIDEL.

The variable `ethio-primary-language' specifies the primary
language and `ethio-secondary-language' specifies the secondary.
Karl Heuer's avatar
Karl Heuer committed
445

Kenichi Handa's avatar
Kenichi Handa committed
446 447 448
If the 1st optional parameter SECONDARY is non-nil, assume the buffer
begins with the secondary language; otherwise with the primary
language.
Karl Heuer's avatar
Karl Heuer committed
449

Kenichi Handa's avatar
Kenichi Handa committed
450 451 452 453
If the 2nd optional parametr FORCE is non-nil, perform conversion even if the
buffer is read-only.

See also the descriptions of the variables
Kenichi Handa's avatar
Kenichi Handa committed
454
`ethio-use-colon-for-colon' and
Kenichi Handa's avatar
Kenichi Handa committed
455
`ethio-use-three-dot-question'."
Karl Heuer's avatar
Karl Heuer committed
456 457

  (interactive "P")
Kenichi Handa's avatar
Kenichi Handa committed
458

Karl Heuer's avatar
Karl Heuer committed
459 460 461 462
  (if (and buffer-read-only
	   (not force)
	   (not (y-or-n-p "Buffer is read-only.  Force to convert? ")))
      (error ""))
Kenichi Handa's avatar
Kenichi Handa committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

  (let ((ethio-primary-language ethio-primary-language)
	(ethio-secondary-language ethio-secondary-language)
	(ethio-use-colon-for-colon ethio-use-colon-for-colon)
	(ethio-use-three-dot-question ethio-use-three-dot-question)
	;; The above four variables may be changed temporary
	;; by tilde escapes during conversion.  So we bind them to other
	;; variables but of the same names.
	(buffer-read-only nil)
	(case-fold-search nil)
	current-language
	next-language)

    (setq current-language
	  (if secondary
	      ethio-secondary-language
	    ethio-primary-language))

Karl Heuer's avatar
Karl Heuer committed
481
    (goto-char (point-min))
Kenichi Handa's avatar
Kenichi Handa committed
482

Karl Heuer's avatar
Karl Heuer committed
483
    (while (not (eobp))
Kenichi Handa's avatar
Kenichi Handa committed
484 485 486 487 488 489 490 491 492 493 494 495 496
      (setq next-language
	    (cond
	     ((eq current-language 'english)
	      (ethio-sera-to-fidel-english))
	     ((eq current-language 'amharic)
	      (ethio-sera-to-fidel-ethio 'amharic))
	     ((eq current-language 'tigrigna)
	      (ethio-sera-to-fidel-ethio 'tigrigna))
	     (t				; we don't know what to do
	      (ethio-sera-to-fidel-english))))

      (setq current-language
	    (cond
Karl Heuer's avatar
Karl Heuer committed
497

Kenichi Handa's avatar
Kenichi Handa committed
498 499 500
	     ;; when language tag is explicitly specified
	     ((not (eq next-language 'toggle))
	      next-language)
Karl Heuer's avatar
Karl Heuer committed
501

Kenichi Handa's avatar
Kenichi Handa committed
502 503 504
	     ;; found a toggle in a primary language section
	     ((eq current-language ethio-primary-language)
	      ethio-secondary-language)
Karl Heuer's avatar
Karl Heuer committed
505

Kenichi Handa's avatar
Kenichi Handa committed
506 507 508 509
	     ;; found a toggle in a secondary, third, fourth, ...
	     ;; language section
	     (t
	      ethio-primary-language))))
Karl Heuer's avatar
Karl Heuer committed
510

Kenichi Handa's avatar
Kenichi Handa committed
511 512 513 514 515 516 517 518 519 520 521 522
    ;; If ethio-implicit-period-conversion is non-nil, the
    ;; Ethiopic dot "$(3%u(B" at the end of an Ethiopic sentence is
    ;; replaced with the Ethiopic full stop "$(3$i(B".
    (if ethio-implicit-period-conversion
	(progn
	  (goto-char (point-min))
	  (while (re-search-forward "\\([$(3!!(B-$(3$a%)(B-$(3%e%n(B-$(3%r%s(B]\\)$(3%u(B\\([ \t]\\)"
				    nil t)
	    (replace-match "\\1$(3$i(B\\2"))
	  (goto-char (point-min))
	  (while (re-search-forward "\\([$(3!!(B-$(3$a%)(B-$(3%e%n(B-$(3%r%s(B]\\)$(3%u(B$" nil t)
	    (replace-match "\\1$(3$i(B"))))
Karl Heuer's avatar
Karl Heuer committed
523

Kenichi Handa's avatar
Kenichi Handa committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    ;; gemination
    (goto-char (point-min))
    (while (re-search-forward "\\ce$(3%s(B" nil 0)
      (compose-region
       (save-excursion (backward-char 2) (point))
       (point)))
    ))

(defun ethio-sera-to-fidel-english nil
  "Handle English section in SERA to FIDEL conversion.
Conversion stops when a language switch is found.  Then delete that
switch and return the name of the new language as a symbol."
  (let ((new-language nil))

    (while (and (not (eobp)) (null new-language))
      (cond
Karl Heuer's avatar
Karl Heuer committed
540

Kenichi Handa's avatar
Kenichi Handa committed
541 542
       ;; if no more "\", nothing to do.
       ((not (search-forward "\\" nil 0)))
Karl Heuer's avatar
Karl Heuer committed
543

Kenichi Handa's avatar
Kenichi Handa committed
544 545
       ;; hereafter point is put after a "\".
       ;; first delete that "\", then check the following chars
Karl Heuer's avatar
Karl Heuer committed
546

Kenichi Handa's avatar
Kenichi Handa committed
547 548 549 550 551
       ;; "\\" :  leave the second "\"
       ((progn
	  (delete-backward-char 1)
	  (= (following-char) ?\\ ))
	(forward-char 1))
Karl Heuer's avatar
Karl Heuer committed
552

Kenichi Handa's avatar
Kenichi Handa committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
       ;; "\ " :  delete the following " "
       ((= (following-char) 32)
	(delete-char 1)
	(setq new-language 'toggle))

       ;; a language flag
       ((setq new-language (ethio-process-language-flag)))

       ;; just a "\" :  not special sequence.
       (t
	(setq new-language 'toggle))))

    new-language))

(defun ethio-sera-to-fidel-ethio (lang)
  "Handle Ethiopic section in SERA to FIDEL conversion.
Conversion stops when a language switch is found.  Then delete that
switch and return the name of the new language as a symbol.

The parameter LANG (symbol, either `amharic' or `tigrigna') affects
the conversion of \"a\"."

  (let ((new-language nil)
	(verbatim nil)
	start table table2 ch)

    (setcar (aref ethio-sera-to-fidel-table ?a)
	    (if (eq lang 'tigrigna) "$(3"f(B" "$(3"c(B"))	  

    (while (and (not (eobp)) (null new-language))
      (setq ch (following-char))
Karl Heuer's avatar
Karl Heuer committed
584
      (cond
Kenichi Handa's avatar
Kenichi Handa committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

       ;; skip from "<" to ">" (or from "&" to ";") if in w3-mode
       ((and (boundp 'sera-being-called-by-w3)
	     sera-being-called-by-w3
	     (or (= ch ?<) (= ch ?&)))
	(search-forward (if (= ch ?<) ">" ";")
			nil 0))

       ;; leave non-ASCII characters as they are
       ((>= ch 128)
	(forward-char 1))

       ;; ethiopic digits
       ((looking-at "`[1-9][0-9]*")
	(delete-char 1)
	(ethio-convert-digit))

       ;; if not seeing a "\", do sera to fidel conversion
       ((/= ch ?\\ )
	(setq start (point))
	(forward-char 1)
	(setq table (aref ethio-sera-to-fidel-table ch))
	(while (setq table2 (cdr (assoc (following-char) table)))
	  (setq table table2)
	  (forward-char 1))
	(if (setq ch (car table))
	    (progn
	      (delete-region start (point))
	      (if (stringp ch)
		  (insert ch)
		(insert (eval ch))))))

       ;; if control reaches here, we must be looking at a "\"

       ;; verbatim mode
       (verbatim
	(if (looking-at "\\\\~! ?")

	    ;; "\~!" or "\~! ".  switch to non-verbatim mode
	    (progn
	      (replace-match "")
	      (setq verbatim nil))

	  ;; "\" but not "\~!" nor "\~! ".  skip the current "\".
	  (forward-char 1)))

       ;; hereafter, non-verbatim mode and looking at a "\"
       ;; first delete that "\", then check the following chars.

       ;; "\ " : delete the following " "
       ((progn
	  (delete-char 1)
	  (setq ch (following-char))
	  (= ch 32))
	(delete-char 1)
	(setq new-language 'toggle))

       ;; "\~!" or "\~! " : switch to verbatim mode
       ((looking-at "~! ?")
	(replace-match "")
	(setq verbatim t))

       ;; a language flag
       ((setq new-language (ethio-process-language-flag)))

       ;; "\~" but not "\~!" nor a language flag
Karl Heuer's avatar
Karl Heuer committed
651 652
       ((= ch ?~)
	(delete-char 1)
Kenichi Handa's avatar
Kenichi Handa committed
653 654 655 656 657 658 659
	(ethio-tilde-escape))

       ;; ASCII punctuation escape.  skip
       ((looking-at "\\(,\\|\\.\\|;\\|:\\|'\\|`\\|\?\\|\\\\\\)+")
	(goto-char (match-end 0)))

       ;; "\", but not special sequence
Karl Heuer's avatar
Karl Heuer committed
660
       (t
Kenichi Handa's avatar
Kenichi Handa committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	(setq new-language 'toggle))))

    new-language))

(defun ethio-process-language-flag nil
  "Process a language flag of the form \"~lang\" or \"~lang1~lang2\".

If looking at \"~lang1~lang2\", set `ethio-primary-language' and
`ethio-une-secondary-language' based on \"lang1\" and \"lang2\".
Then delete the language flag \"~lang1~lang2\" from the buffer.
Return value is the new primary language.

If looking at \"~lang\", delete that language flag \"~lang\" from the
buffer and return that language.  In this case
`ethio-primary-language' and `ethio-uni-secondary-language'
are left unchanged.

If an unsupported language flag is found, just return nil without
changing anything."

  (let (lang1 lang2)
    (cond

     ;; ~lang1~lang2
     ((and (looking-at
	    "~\\([a-z][a-z][a-z]?\\)~\\([a-z][a-z][a-z]?\\)[ \t\n\\]")
	   (setq lang1
		 (ethio-flag-to-language
		  (buffer-substring (match-beginning 1) (match-end 1))))
	   (setq lang2
		 (ethio-flag-to-language
		  (buffer-substring (match-beginning 2) (match-end 2)))))
      (setq ethio-primary-language lang1
	    ethio-secondary-language lang2)
      (delete-region (point) (match-end 2))
      (if (= (following-char) 32)
	  (delete-char 1))
      ethio-primary-language)

     ;; ~lang
     ((and (looking-at "~\\([a-z][a-z][a-z]?\\)[ \t\n\\]")
	   (setq lang1
		 (ethio-flag-to-language
		  (buffer-substring (match-beginning 1) (match-end 1)))))
      (delete-region (point) (match-end 1))
      (if (= (following-char) 32)
	  (delete-char 1))
      lang1)

     ;; otherwise
     (t
      nil))))

(defun ethio-tilde-escape nil
  "Handle a SERA tilde escape in Ethiopic section and delete it.
Delete the escape even it is not recognised."

  (let ((p (point)) command)
    (skip-chars-forward "^ \t\n\\\\")
    (setq command (buffer-substring p (point)))
    (delete-region p (point))
    (if (= (following-char) 32)
	(delete-char 1))

    (cond

     ;; \~-:
     ((string= command "-:")
      (setq ethio-use-colon-for-colon t))

     ;; \~`:
     ((string= command "`:")
      (setq ethio-use-colon-for-colon nil))
Karl Heuer's avatar
Karl Heuer committed
734

Kenichi Handa's avatar
Kenichi Handa committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
     ;; \~?
     ((string= command "?")
      (setq ethio-use-three-dot-question nil))

     ;; \~`|
     ((string= command "`|")
      (setq ethio-use-three-dot-question t))

     ;; \~e
     ((string= command "e")
      (insert "$(3%j(B"))

     ;; \~E
     ((string= command "E")
      (insert "$(3%k(B"))

     ;; \~a
     ((string= command "a")
      (insert "$(3%l(B"))

     ;; \~A
     ((string= command "A")
      (insert "$(3%m(B"))

     ;; \~X
     ((string= command "X")
      (insert "$(3%i(B"))

     ;; unsupported tilde escape
     (t
      nil))))

(defun ethio-flag-to-language (flag)
  (cond
   ((or (string= flag "en") (string= flag "eng")) 'english)
   ((or (string= flag "ti") (string= flag "tir")) 'tigrigna)
   ((or (string= flag "am") (string= flag "amh")) 'amharic)
   (t nil)))
  
(defun ethio-convert-digit nil
Karl Heuer's avatar
Karl Heuer committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
  "Convert Arabic digits to Ethiopic digits."
  (let (ch z)
    (while (and (>= (setq ch (following-char)) ?1)
		(<= ch ?9))
      (delete-char 1)

      ;; count up following zeros
      (setq z 0)
      (while (= (following-char) ?0)
	(delete-char 1)
	(setq z (1+ z)))

      (cond

       ;; first digit is 10, 20, ..., or 90
       ((= (mod z 2) 1)
Kenichi Handa's avatar
Kenichi Handa committed
791
	(insert (aref [?$(3$y(B ?$(3$z(B ?$(3${(B ?$(3$|(B ?$(3$}(B ?$(3$~(B ?$(3%!(B ?$(3%"(B ?$(3%#(B] (- ch ?1)))
Karl Heuer's avatar
Karl Heuer committed
792 793 794 795
	(setq z (1- z)))

       ;; first digit is 2, 3, ..., or 9
       ((/= ch ?1)
Kenichi Handa's avatar
Kenichi Handa committed
796
	(insert (aref [?$(3$q(B ?$(3$r(B ?$(3$s(B ?$(3$t(B ?$(3$u(B ?$(3$v(B ?$(3$w(B ?$(3$x(B] (- ch ?2))))
Karl Heuer's avatar
Karl Heuer committed
797 798 799

       ;; single 1
       ((= z 0)
Kenichi Handa's avatar
Kenichi Handa committed
800
	(insert "$(3$p(B")))
Karl Heuer's avatar
Karl Heuer committed
801 802 803

      ;; 100
      (if (= (mod z 4) 2)
Kenichi Handa's avatar
Kenichi Handa committed
804
	  (insert "$(3%$(B"))
Karl Heuer's avatar
Karl Heuer committed
805 806

      ;; 10000
Kenichi Handa's avatar
Kenichi Handa committed
807
      (insert-char ?$(3%%(B (/ z 4)))))
Karl Heuer's avatar
Karl Heuer committed
808

809 810 811 812 813 814 815 816 817 818 819
;;;###autoload
(defun ethio-sera-to-fidel-mail-or-marker (&optional arg)
  "Execute ethio-sera-to-fidel-mail or ethio-sera-to-fidel-marker depending on the current major mode.
If in rmail-mode or in mail-mode, execute the former; otherwise latter."

  (interactive "P")
  (if (or (eq major-mode 'rmail-mode)
	  (eq major-mode 'mail-mode))
      (ethio-sera-to-fidel-mail (prefix-numeric-value arg))
    (ethio-sera-to-fidel-marker arg)))

Karl Heuer's avatar
Karl Heuer committed
820
;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
821 822
(defun ethio-sera-to-fidel-mail (&optional arg)
  "Convert SERA to FIDEL to read/write mail and news.
Karl Heuer's avatar
Karl Heuer committed
823 824

If the buffer contains the markers \"<sera>\" and \"</sera>\",
Kenichi Handa's avatar
Kenichi Handa committed
825
convert the segments between them into FIDEL.
Karl Heuer's avatar
Karl Heuer committed
826

Kenichi Handa's avatar
Kenichi Handa committed
827 828
If invoked interactively and there is no marker, convert the subject field
and the body into FIDEL using `ethio-sera-to-fidel-region'."
Karl Heuer's avatar
Karl Heuer committed
829 830

  (interactive "p")
Kenichi Handa's avatar
Kenichi Handa committed
831 832
  (let ((buffer-read-only nil)
	border)
Karl Heuer's avatar
Karl Heuer committed
833 834
    (save-excursion

Kenichi Handa's avatar
Kenichi Handa committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848
      ;; look for the header-body separator
      (goto-char (point-min))
      (if (search-forward
	   (if (eq major-mode 'rmail-mode)
	       "\n\n" (concat "\n" mail-header-separator "\n"))
	   nil t)
	  (setq border (point))
	(error "header separator not found"))

      ;; note that the point is placed at the border
      (if (or (re-search-forward "^<sera>$" nil t)
	      (progn
		(goto-char (point-min))
		(re-search-forward "^Subject: <sera>" border t)))
Karl Heuer's avatar
Karl Heuer committed
849

Kenichi Handa's avatar
Kenichi Handa committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	  ;; there are markers
	  (progn
	    ;; we start with the body so that the border will not change
	    ;; use "^<sera>\n" instead of "^<sera>$" not to leave a blank line
	    (goto-char border)
	    (while (re-search-forward "^<sera>\n" nil t)
	      (replace-match "")
	      (ethio-sera-to-fidel-region
	       (point)
	       (progn
		 (if (re-search-forward "^</sera>\n" nil 0)
		     (replace-match ""))
		 (point))))
	    ;; now process the subject
	    (goto-char (point-min))
	    (if (re-search-forward "^Subject: <sera>" border t)
		(ethio-sera-to-fidel-region
		 (progn (delete-backward-char 6) (point))
		 (progn
		   (if (re-search-forward "</sera>$" (line-end-position) 0)
		       (replace-match ""))
		   (point)))))

	;; in case there are no marks but invoked interactively
	(if arg
	    (progn
	      (ethio-sera-to-fidel-region border (point-max))
	      (goto-char (point-min))
	      (if (re-search-forward "^Subject: " border t)
		  (ethio-sera-to-fidel-region (point) (line-end-position))))))
Karl Heuer's avatar
Karl Heuer committed
880 881 882 883 884 885 886 887

      ;; adjust the rmail marker
      (if (eq major-mode 'rmail-mode)
	  (set-marker
	   (aref rmail-message-vector (1+ rmail-current-message))
	   (point-max))))))

;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
888 889 890 891 892
(defun ethio-sera-to-fidel-marker (&optional force)
  "Convert the regions surrounded by \"<sera>\" and \"</sera>\" from SERA to FIDEL.
Assume that each region begins with `ethio-primary-language'.
The markers \"<sera>\" and \"</sera>\" themselves are not deleted."
  (interactive "P")
Karl Heuer's avatar
Karl Heuer committed
893
  (if (and buffer-read-only
Kenichi Handa's avatar
Kenichi Handa committed
894
	   (not force)
Karl Heuer's avatar
Karl Heuer committed
895 896 897 898 899
	   (not (y-or-n-p "Buffer is read-only.  Force to convert? ")))
      (error ""))
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "<sera>" nil t)
Kenichi Handa's avatar
Kenichi Handa committed
900
      (ethio-sera-to-fidel-region
Karl Heuer's avatar
Karl Heuer committed
901 902 903 904 905 906 907 908 909 910 911
       (point)
       (if (re-search-forward "</sera>" nil t)
	   (match-beginning 0)
	 (point-max))
       nil
       'force))))

;;
;; FIDEL to SERA
;;

Kenichi Handa's avatar
Kenichi Handa committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
(defconst ethio-fidel-to-sera-map
 [ "he"  "hu"  "hi"  "ha"  "hE"  "h"  "ho"    ""       ;;   0 - 7
   "le"  "lu"  "li"  "la"  "lE"  "l"  "lo"  "lWa"      ;;   8
   "He"  "Hu"  "Hi"  "Ha"  "HE"  "H"  "Ho"  "HWa"      ;;  16
   "me"  "mu"  "mi"  "ma"  "mE"  "m"  "mo"  "mWa"      ;;  24
  "`se" "`su" "`si" "`sa" "`sE" "`s" "`so" "`sWa"      ;;  32
   "re"  "ru"  "ri"  "ra"  "rE"  "r"  "ro"  "rWa"      ;;  40
   "se"  "su"  "si"  "sa"  "sE"  "s"  "so"  "sWa"      ;;  48
   "xe"  "xu"  "xi"  "xa"  "xE"  "x"  "xo"  "xWa"      ;;  56
   "qe"  "qu"  "qi"  "qa"  "qE"  "q"  "qo"    ""       ;;  64
  "qWe"   ""  "qWi" "qWa" "qWE"  "qW'" ""     ""       ;;  72
   "Qe"  "Qu"  "Qi"  "Qa"  "QE"  "Q"  "Qo"    ""       ;;  80
  "QWe"   ""  "QWi" "QWa" "QWE"  "QW'" ""     ""       ;;  88
   "be"  "bu"  "bi"  "ba"  "bE"  "b"  "bo"  "bWa"      ;;  96
   "ve"  "vu"  "vi"  "va"  "vE"  "v"  "vo"  "vWa"      ;; 104
   "te"  "tu"  "ti"  "ta"  "tE"  "t"  "to"  "tWa"      ;; 112
   "ce"  "cu"  "ci"  "ca"  "cE"  "c"  "co"  "cWa"      ;; 120
  "`he" "`hu" "`hi" "`ha" "`hE" "`h" "`ho"    ""       ;; 128
  "hWe"   ""  "hWi" "hWa"  "hWE" "hW'" ""     ""       ;; 136
   "ne"  "nu"  "ni"  "na"  "nE"  "n"  "no"  "nWa"      ;; 144
   "Ne"  "Nu"  "Ni"  "Na"  "NE"  "N"  "No"  "NWa"      ;; 152
    "e"   "u"   "i"   "A"   "E"  "I"   "o"   "ea"      ;; 160
   "ke"  "ku"  "ki"  "ka"  "kE"  "k"  "ko"    ""       ;; 168
  "kWe"   ""  "kWi" "kWa" "kWE"  "kW'" ""     ""       ;; 176
   "Ke"  "Ku"  "Ki"  "Ka"  "KE"  "K"  "Ko"    ""       ;; 184
  "KWe"   ""  "KWi" "KWa" "KWE"  "KW'" ""     ""       ;; 192
   "we"  "wu"  "wi"  "wa"  "wE"  "w"  "wo"    ""       ;; 200
   "`e"  "`u"  "`i"  "`a"  "`E" "`I"  "`o"    ""       ;; 208
   "ze"  "zu"  "zi"  "za"  "zE"  "z"  "zo"  "zWa"      ;; 216
   "Ze"  "Zu"  "Zi"  "Za"  "ZE"  "Z"  "Zo"  "ZWa"      ;; 224
   "ye"  "yu"  "yi"  "ya"  "yE"  "y"  "yo"  "yWa"      ;; 232
   "de"  "du"  "di"  "da"  "dE"  "d"  "do"  "dWa"      ;; 240
   "De"  "Du"  "Di"  "Da"  "DE"  "D"  "Do"  "DWa"      ;; 248
   "je"  "ju"  "ji"  "ja"  "jE"  "j"  "jo"  "jWa"      ;; 256
   "ge"  "gu"  "gi"  "ga"  "gE"  "g"  "go"    ""       ;; 264
  "gWe"   ""  "gWi" "gWa" "gWE" "gW'"  ""     ""       ;; 272
   "Ge"  "Gu"  "Gi"  "Ga"  "GE"  "G"  "Go"  "GWa"      ;; 280
   "Te"  "Tu"  "Ti"  "Ta"  "TE"  "T"  "To"  "TWa"      ;; 288
   "Ce"  "Cu"  "Ci"  "Ca"  "CE"  "C"  "Co"  "CWa"      ;; 296
   "Pe"  "Pu"  "Pi"  "Pa"  "PE"  "P"  "Po"  "PWa"      ;; 304
   "Se"  "Su"  "Si"  "Sa"  "SE"  "S"  "So"  "SWa"      ;; 312
  "`Se" "`Su" "`Si" "`Sa" "`SE" "`S" "`So"    ""       ;; 320
   "fe"  "fu"  "fi"  "fa"  "fE"  "f"  "fo"  "fWa"      ;; 328
   "pe"  "pu"  "pi"  "pa"  "pE"  "p"  "po"  "pWa"      ;; 336
  "mYa" "rYa" "fYa"   ""    ""   ""    ""     ""       ;; 344
   " "  " : "  "::"  ","   ";"  "-:"  ":-"   "`?"      ;; 352
  ":|:"  "1"   "2"   "3"   "4"   "5"   "6"   "7"       ;; 360
   "8"   "9"   "10"  "20"  "30"  "40" "50"   "60"      ;; 368
   "70"  "80"  "90" "100" "10000" ""   ""     ""       ;; 376
  "`qe" "`qu" "`qi" "`qa" "`qE" "`q" "`qo"    ""       ;; 384
  "mWe" "bWe" "GWe" "fWe" "pWe"  ""    ""     ""       ;; 392
  "`ke" "`ku" "`ki" "`ka" "`kE" "`k" "`ko"    ""       ;; 400
  "mWi" "bWi" "GWi" "fWi" "pWi"  ""    ""     ""       ;; 408
   "Xe"  "Xu"  "Xi"  "Xa"  "XE"  "X"  "Xo"    ""       ;; 416
  "mWE" "bWE" "GWE" "fWE" "pWE"  ""    ""     ""       ;; 424
  "`ge" "`gu" "`gi" "`ga" "`gE" "`g" "`go"    ""       ;; 432
  "mW'" "bW'" "GW'" "fW'" "pW'"  ""    ""     ""       ;; 440
  "\\~X " "\\~e " "\\~E " "\\~a " "\\~A " "wWe" "wWi" "wWa" ;; 448
  "wWE" "wW'"  "''"  "`!"  "."  "<<"  ">>"   "?" ])    ;; 456

(defun ethio-prefer-amharic-p nil
  (or (eq ethio-primary-language 'amharic)
      (and (not (eq ethio-primary-language 'tigrigna))
	   (eq ethio-secondary-language 'amharic))))

(defun ethio-language-to-flag (lang)
  (cond
   ((eq lang 'english) "eng")
   ((eq lang 'tigrigna) "tir")
   ((eq lang 'amharic) "amh")
   (t "")))
Karl Heuer's avatar
Karl Heuer committed
983 984

;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
985 986 987 988
(defun ethio-fidel-to-sera-region (begin end &optional secondary force)
  "Replace all the FIDEL characters in the region to the SERA format.
The variable `ethio-primary-language' specifies the primary
language and `ethio-secondary-language' specifies the secondary.
Karl Heuer's avatar
Karl Heuer committed
989

Kenichi Handa's avatar
Kenichi Handa committed
990 991 992
If the 3dr parameter SECONDARY is given and non-nil, try to convert
the region so that it begins in the secondary language; otherwise with
the primary language.
Karl Heuer's avatar
Karl Heuer committed
993

Kenichi Handa's avatar
Kenichi Handa committed
994 995 996 997
If the 4th parameter FORCE is given and non-nil, convert even if the
buffer is read-only.

See also the descriptions of the variables
Kenichi Handa's avatar
Kenichi Handa committed
998
`ethio-use-colon-for-colon', `ethio-use-three-dot-question',
Kenichi Handa's avatar
Kenichi Handa committed
999
`ethio-quote-vowel-always' and `ethio-numeric-reduction'."
Karl Heuer's avatar
Karl Heuer committed
1000 1001

  (interactive "r\nP")
Kenichi Handa's avatar
Kenichi Handa committed
1002 1003 1004
  (save-restriction
    (narrow-to-region begin end)
    (ethio-fidel-to-sera-buffer secondary force)))
Karl Heuer's avatar
Karl Heuer committed
1005 1006

;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
1007 1008 1009 1010
(defun ethio-fidel-to-sera-buffer (&optional secondary force)
  "Replace all the FIDEL characters in the current buffer to the SERA format.
The variable `ethio-primary-language' specifies the primary
language and `ethio-secondary-language' specifies the secondary.
Karl Heuer's avatar
Karl Heuer committed
1011

Kenichi Handa's avatar
Kenichi Handa committed
1012 1013 1014
If the 1st optional parameter SECONDARY is non-nil, try to convert the
region so that it begins in the secondary language; otherwise with the
primary language.
Karl Heuer's avatar
Karl Heuer committed
1015

Kenichi Handa's avatar
Kenichi Handa committed
1016
If the 2nd optional parameter FORCE is non-nil, convert even if the
Karl Heuer's avatar
Karl Heuer committed
1017 1018
buffer is read-only.

Kenichi Handa's avatar
Kenichi Handa committed
1019
See also the descriptions of the variables
Kenichi Handa's avatar
Kenichi Handa committed
1020
`ethio-use-colon-for-colon', `ethio-use-three-dot-question',
Kenichi Handa's avatar
Kenichi Handa committed
1021
`ethio-quote-vowel-always' and `ethio-numeric-reduction'."
Karl Heuer's avatar
Karl Heuer committed
1022 1023 1024 1025 1026 1027 1028

  (interactive "P")
  (if (and buffer-read-only
	   (not force)
	   (not (y-or-n-p "Buffer is read-only.  Force to convert? ")))
      (error ""))

Kenichi Handa's avatar
Kenichi Handa committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  (let ((buffer-read-only nil)
	(case-fold-search nil)
	(lonec nil) ;; t means previous char was a lone consonant
	(fidel nil) ;; t means previous char was a FIDEL
	(digit nil) ;; t means previous char was an Ethiopic digit
	(flag (if (ethio-prefer-amharic-p) "\\~amh " "\\~tir "))
	mode ch)

    ;; user's preference in transcription
    (if ethio-use-colon-for-colon
	(progn
	  (aset ethio-fidel-to-sera-map 353 "`:")
	  (aset ethio-fidel-to-sera-map 357 ":"))
      (aset ethio-fidel-to-sera-map 353 " : ")
      (aset ethio-fidel-to-sera-map 357 "-:"))

    (if ethio-use-three-dot-question
	(progn
	  (aset ethio-fidel-to-sera-map 359 "?")
	  (aset ethio-fidel-to-sera-map 463 "`?"))
      (aset ethio-fidel-to-sera-map 359 "`?")
      (aset ethio-fidel-to-sera-map 463 "?"))

    (mapcar
     '(lambda (x)
	(aset (aref ethio-fidel-to-sera-map x)
	      2
	      (if ethio-W-sixth-always ?' ?u)))
     '(77 93 141 181 197 277 440 441 442 443 444 457))

    (if (ethio-prefer-amharic-p)
	(aset ethio-fidel-to-sera-map 160 "a")
      (aset ethio-fidel-to-sera-map 160 "e"))
    ;; end of user's preference

    ;; first, decompose geminated characters
    (decompose-region (point-min) (point-max))

    ;; main conversion routine
Karl Heuer's avatar
Karl Heuer committed
1068 1069 1070 1071
    (goto-char (point-min))
    (while (not (eobp))
      (setq ch (following-char))

Kenichi Handa's avatar
Kenichi Handa committed
1072
      (cond				; ethiopic, english, neutral
Karl Heuer's avatar
Karl Heuer committed
1073

Kenichi Handa's avatar
Kenichi Handa committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
       ;; ethiopic character.  must go to ethiopic mode, if not in it.
       ((eq (char-charset ch) 'ethiopic)
	(setq ch (ethio-char-to-ethiocode ch))
	(delete-char 1)
	(if (not (eq mode 'ethiopic))
	    (progn
	      (insert flag)
	      (setq mode 'ethiopic)))

	(cond				; fidel, punc, digit

	 ;; fidels
	 ((or (<= ch 346)		;  he - fYa
	      (and (>= ch 384) (<= ch 444)) ; `qe - pw
	      (and (>= ch 453) (<= ch 457))) ; wWe - wW
	  (if (and (memq ch '(160 161 162 163 164 166 167)) ; (e - ea)
		   (or lonec
		       (and ethio-quote-vowel-always
			    fidel)))
	      (insert "'"))
	  (insert (aref ethio-fidel-to-sera-map ch))
	  (setq lonec (ethio-lone-consonant-p ch)
		fidel t
		digit nil))

	 ;; punctuations or icons
	 ((or (and (>= ch 353) (<= ch 360)) ;  : - :|:
	      (>= ch 458)		;  '' -  ?
	      (and (>= ch 448) (<= ch 452))) ;  \~X \~e \~E \~a \~A
	  (insert (aref ethio-fidel-to-sera-map ch))
	  (setq lonec nil
		fidel nil
		digit nil))

	 ;; now CH must be an ethiopic digit

	 ;; reduction = 0 or not preceded by Ethiopic number(s)
	 ((or (= ethio-numeric-reduction 0)
	      (not digit))
	  (insert "`" (aref ethio-fidel-to-sera-map ch))
	  (setq lonec nil
		fidel nil
		digit t))

	 ;; reduction = 2 and following 10s, 100s, 10000s 
	 ((and (= ethio-numeric-reduction 2)
	       (memq ch '(370 379 380)))
	  (insert (substring (aref ethio-fidel-to-sera-map ch) 1))
	  (setq lonec nil
		fidel nil
		digit t))

	 ;; ordinary following digits
	 (t
	  (insert (aref ethio-fidel-to-sera-map ch))
	  (setq lonec nil
		fidel nil
		digit t))))

       ;; english character.  must go to english mode, if not in it.
       ((or (and (>= ch ?a) (<= ch ?z))
	    (and (>= ch ?A) (<= ch ?Z)))
	(if (not (eq mode 'english))
	    (insert "\\~eng "))
	(forward-char 1)
	(setq mode 'english
	      lonec nil
	      fidel nil
	      digit nil))
Karl Heuer's avatar
Karl Heuer committed
1143

Kenichi Handa's avatar
Kenichi Handa committed
1144 1145
       ;; ch can appear both in ethiopic section and in english section.
       (t
Karl Heuer's avatar
Karl Heuer committed
1146

Kenichi Handa's avatar
Kenichi Handa committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	;; we must decide the mode, if not decided yet
	(if (null mode)
	    (progn
	      (setq mode
		    (if secondary
			ethio-secondary-language
		      ethio-primary-language))
	      (if (eq mode 'english)
		  (insert "\\~eng ")
		(insert flag)
		(setq mode 'ethiopic)))) ; tigrigna & amharic --> ethiopic

	(cond				; \ , eng-mode , punc , w3 , other
Karl Heuer's avatar
Karl Heuer committed
1160 1161 1162

	 ;; backslash is always quoted
	 ((= ch ?\\ )
Kenichi Handa's avatar
Kenichi Handa committed
1163 1164
	  (insert "\\")
	  (forward-char 1))
Karl Heuer's avatar
Karl Heuer committed
1165

Kenichi Handa's avatar
Kenichi Handa committed
1166 1167 1168
	 ;; nothing to do if in english mode
	 ((eq mode 'english)
	  (forward-char 1))
Karl Heuer's avatar
Karl Heuer committed
1169

Kenichi Handa's avatar
Kenichi Handa committed
1170
	 ;; now we must be in ethiopic mode and seeing a non-"\"
Karl Heuer's avatar
Karl Heuer committed
1171

Kenichi Handa's avatar
Kenichi Handa committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	 ;; ascii punctuations in ethiopic mode
	 ((looking-at "[,.;:'`?]+")
	  (insert "\\")
	  (goto-char (1+ (match-end 0)))) ; because we inserted one byte (\)

	 ;; skip from "<" to ">" (or from "&" to ";") if called from w3
	 ((and (boundp 'sera-being-called-by-w3)
	       sera-being-called-by-w3
	       (or (= ch ?<) (= ch ?&)))
	  (search-forward (if (= ch ?<) ">" ";")
			  nil 0))

	 ;; neutral character.  no need to quote.  just skip it.
	 (t
	  (forward-char 1)))
Karl Heuer's avatar
Karl Heuer committed
1187 1188 1189 1190

	(setq lonec nil
	      fidel nil
	      digit nil)))
Kenichi Handa's avatar
Kenichi Handa committed
1191 1192
    ;; end of main conversion routine
    )))
Karl Heuer's avatar
Karl Heuer committed
1193

Kenichi Handa's avatar
Kenichi Handa committed
1194 1195 1196
(defun ethio-lone-consonant-p (ethiocode)
  "If ETHIOCODE is an Ethiopic lone consonant, return t."
  (or (and (< ethiocode 344) (= (% ethiocode 8) 5))
Karl Heuer's avatar
Karl Heuer committed
1197

Kenichi Handa's avatar
Kenichi Handa committed
1198 1199
      ;;                     `q  `k   X  `g  mW  bW  GW  fW  pW  wW
      (memq ethiocode '(389 405 421 437 440