titdic-cnv.el 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. ;;; titdic-cnv.el --- convert cxterm dictionary (TIT format) to Quail package -*- coding:iso-2022-7bit; -*-
  2. ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  6. ;; Registration Number H14PRO021
  7. ;; Copyright (C) 2003
  8. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  9. ;; Registration Number H13PRO009
  10. ;; Keywords: Quail, TIT, cxterm
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;; Convert cxterm dictionary (of TIT format) to quail-package.
  24. ;;
  25. ;; Usage (within Emacs):
  26. ;; M-x titdic-convert<CR>CXTERM-DICTIONARY-NAME<CR>
  27. ;; Usage (from shell):
  28. ;; % emacs -batch -l titdic-cnv -f batch-titdic-convert\
  29. ;; [-dir DIR] [DIR | FILE] ...
  30. ;;
  31. ;; When you run titdic-convert within Emacs, you have a chance to
  32. ;; modify arguments of `quail-define-package' before saving the
  33. ;; converted file. For instance, you are likely to modify TITLE,
  34. ;; DOCSTRING, and KEY-BINDINGS.
  35. ;; Cxterm dictionary file (*.tit) is a line-oriented text (English,
  36. ;; Chinese, Japanese, and Korean) file. The whole file contains of
  37. ;; two parts, the definition part (`header' here after) followed by
  38. ;; the dictionary part (`body' here after). All lines begin with
  39. ;; leading '#' are ignored.
  40. ;;
  41. ;; Each line in the header part has two fields, KEY and VALUE. These
  42. ;; fields are separated by one or more white characters.
  43. ;;
  44. ;; Each line in the body part has two fields, KEYSEQ and TRANSLATIONS.
  45. ;; These fields are separated by one or more white characters.
  46. ;;
  47. ;; See the manual page of `tit2cit' of cxterm distribution for more
  48. ;; detail.
  49. ;;
  50. ;; Near the end of this file, we also have a few other tools to convert
  51. ;; miscellaneous dictionaries.
  52. ;;; Code:
  53. (require 'quail)
  54. ;; List of values of key "ENCODE:" and the corresponding Emacs
  55. ;; coding-system and language environment name.
  56. (defvar tit-encode-list
  57. '(("GB" euc-china "Chinese-GB")
  58. ("BIG5" cn-big5 "Chinese-BIG5")
  59. ("JIS" euc-japan "Japanese")
  60. ("KS" euc-kr "Korean")))
  61. ;; Alist of input method names and the corresponding title and extra
  62. ;; docstring. For each of input method generated from TIT dictionary,
  63. ;; a docstring is automatically generated from the comments in the
  64. ;; dictionary. The extra docstring in this alist is to add more
  65. ;; information.
  66. ;; The command describe-input-method shows the automatically generated
  67. ;; docstring, then an extra docstring while replacing the form \<VAR>
  68. ;; by the value of variable VAR. For instance, the form
  69. ;; \<quail-translation-docstring> is replaced by a description about
  70. ;; how to select a translation from a list of candidates.
  71. (defvar quail-cxterm-package-ext-info
  72. '(("chinese-4corner" "$(0(?-F(B")
  73. ("chinese-array30" "$(0#R#O(B")
  74. ("chinese-ccdospy" "$AKuF4(B"
  75. "Pinyin base input method for Chinese charset GB2312 \(`chinese-gb2312').
  76. Pinyin is the standard Roman transliteration method for Chinese.
  77. For the detail of Pinyin system, see the documentation of the input
  78. method `chinese-py'.
  79. This input method works almost the same way as `chinese-py'. The
  80. difference is that you type a single key for these Pinyin spelling.
  81. Pinyin: zh en eng ang ch an ao ai ong sh ing yu($A(9(B)
  82. keyseq: a f g h i j k l s u y v
  83. For example:
  84. Chinese: $A0!(B $A9{(B $AVP(B $AND(B $A9b(B $ASq(B $AH+(B
  85. Pinyin: a guo zhong wen guang yu quan
  86. Keyseq: a1 guo4 as1 wf4 guh1 yu..6 qvj6
  87. \\<quail-translation-docstring>
  88. For double-width GB2312 characters corresponding to ASCII, use the
  89. input method `chinese-qj'.")
  90. ("chinese-ecdict" "$(05CKH(B"
  91. "In this input method, you enter a Chinese (Big5) character or word
  92. by typing the corresponding English word. For example, if you type
  93. \"computer\", \"$(0IZH+(B\" is input.
  94. \\<quail-translation-docstring>")
  95. ("chinese-etzy" "$(06/0D(B"
  96. "Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
  97. `chinese-big5-2').
  98. Zhuyin is a kind of phonetic symbol. One to three Zhuyin symbols
  99. compose one Chinese character.
  100. In this input method, you enter a Chinese character by first typing
  101. keys corresponding to Zhuyin symbols (see the above table) followed by
  102. SPC, 1, 2, 3, or 4 specifying a tone (SPC:$(0?v(N(B, 1:$(0M=Vy(B, 2:$(0Dm(N(B, 3: $(0&9Vy(B,
  103. 4:$(0(+Vy(B).
  104. \\<quail-translation-docstring>")
  105. ("chinese-punct-b5" "$(0O:(BB"
  106. "Input method for Chinese punctuation and symbols of Big5
  107. \(`chinese-big5-1' and `chinese-big5-2').")
  108. ("chinese-punct" "$A1j(BG"
  109. "Input method for Chinese punctuation and symbols of GB2312
  110. \(`chinese-gb2312').")
  111. ("chinese-py-b5" "$(03<(BB"
  112. "Pinyin base input method for Chinese Big5 characters
  113. \(`chinese-big5-1', `chinese-big5-2').
  114. This input method works almost the same way as `chinese-py' (which
  115. see).
  116. This input method supports only Han characters. The more convenient
  117. method is `chinese-py-punct-b5', which is the combination of this
  118. method and `chinese-punct-b5' and which supports both Han characters
  119. and punctuation/symbols.
  120. For double-width Big5 characters corresponding to ASCII, use the input
  121. method `chinese-qj-b5'.
  122. The input method `chinese-py' and `chinese-tonepy' are also Pinyin
  123. based, but for the character set GB2312 (`chinese-gb2312').")
  124. ("chinese-qj-b5" "$(0)A(BB")
  125. ("chinese-qj" "$AH+(BG")
  126. ("chinese-sw" "$AJWN2(B"
  127. "Radical base input method for Chinese charset GB2312 (`chinese-gb2312').
  128. In this input method, you enter a Chinese character by typing two
  129. keys. The first key corresponds to the first ($AJW(B) radical, the second
  130. key corresponds to the last ($AN2(B) radical. The correspondence of keys
  131. and radicals is as below:
  132. first radical:
  133. a b c d e f g h i j k l m n o p q r s t u v w x y z
  134. $APD(B $AZ"(B $AJ,(B $AX<(B $A;p(B $A?Z(B $A^P(B $Ac_(B $AZ%(B $A\3(B $AXi(B $AD>(B $Alj(B $Ab;(B $ATB(B $Afy(B $AJ/(B $AMu(B $A0K(B $AX/(B $AHU(B $AeA(B $Aak(B $AVq(B $AR;(B $AHK(B
  135. last radical:
  136. a b c d e f g h i j k l m n o p q r s t u v w x y z
  137. $ASV(B $AI=(B $AMA(B $A56(B $AZb(B $A?Z(B $ARB(B $Aqb(B $A4s(B $A6!(B $A[L(B $Ala(B $AJ.(B $A4u(B $AXg(B $ACE(B $A=q(B $AX-(B $AE.(B $ARR(B $A`m(B $AP!(B $A3'(B $A3f(B $A_.(B $A27(B
  138. \\<quail-translation-docstring>")
  139. ("chinese-tonepy" "$A5wF4(B"
  140. "Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
  141. Pinyin is the standard roman transliteration method for Chinese.
  142. For the details of Pinyin system, see the documentation of the input
  143. method `chinese-py'.
  144. This input method works almost the same way as `chinese-py'. The
  145. difference is that you must type 1..5 after each Pinyin spelling to
  146. specify a tone (1:$ARuF=(B, 2:$AQtF=(B, 3:$AIOIy(B, 4$AOBIy(B, 5:$AGaIy(B).
  147. \\<quail-translation-docstring>
  148. For instance, to input $ADc(B, you type \"n i 3 3\", the first \"n i\" is
  149. a Pinyin, the next \"3\" specifies tone, and the last \"3\" selects
  150. the third character from the candidate list.
  151. For double-width GB2312 characters corresponding to ASCII, use the
  152. input method `chinese-qj'.")
  153. ("chinese-zozy" "$(0I\0D(B"
  154. "Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
  155. `chinese-big5-2').
  156. Zhuyin is a kind of a phonetic symbol. One to three Zhuyin symbols
  157. compose a Chinese character.
  158. In this input method, you enter a Chinese character by first typing
  159. keys corresponding to Zhuyin symbols (see the above table) followed by
  160. SPC, 6, 3, 4, or 7 specifying a tone (SPC:$(0?v(N(B, 6:$(0Dm(N(B, 3:$(0&9Vy(B, 4:$(0(+Vy(B,
  161. 7:$(0M=Vy(B).
  162. \\<quail-translation-docstring>")))
  163. ;; Return a value of the key in the current line.
  164. (defsubst tit-read-key-value ()
  165. (if (looking-at "[^ \t\r\n]+")
  166. (car (read-from-string (concat "\"" (match-string 0) "\"")))))
  167. ;; Return an appropriate quail-package filename from FILENAME (TIT
  168. ;; dictionary filename). For instance, ".../ZOZY.tit" -> "ZOZY.el".
  169. (defun tit-make-quail-package-file-name (filename &optional dirname)
  170. (expand-file-name
  171. (concat (file-name-nondirectory (substring filename 0 -4)) ".el")
  172. dirname))
  173. ;; This value is nil if we are processing phrase dictionary.
  174. (defvar tit-dictionary t)
  175. (defvar tit-encode nil)
  176. (defvar tit-default-encode "GB")
  177. ;; Generate elements of KEY-BINDINGS arg for `quail-define-package' so
  178. ;; that each characters in KEYS invokes FUNCTION-SYMBOL.
  179. (defun tit-generate-key-bindings (keys function-symbol)
  180. (let ((len (length keys))
  181. (i 0)
  182. (first t)
  183. key)
  184. (while (< i len)
  185. (or first (princ "\n "))
  186. (setq key (aref keys i))
  187. (if (if (< key ?\ )
  188. (eq (lookup-key quail-translation-keymap
  189. (char-to-string key))
  190. 'quail-execute-non-quail-command)
  191. (<= key 127))
  192. (progn
  193. (princ (cons (cond ((< key ?\ ) (format "\"\\C-%c\"" (+ key ?@)))
  194. ((< key 127) (format "\"%c\"" key))
  195. (t "\"\\C-?\""))
  196. function-symbol))
  197. (setq first nil)))
  198. (setq i (1+ i)))))
  199. ;; Analyze header part of TIT dictionary and generate an appropriate
  200. ;; `quail-define-package' function call.
  201. (defun tit-process-header (filename)
  202. (message "Processing header part...")
  203. (goto-char (point-min))
  204. ;; At first, generate header part of the Quail package while
  205. ;; collecting information from the original header.
  206. (let ((package (concat
  207. "chinese-"
  208. (substring (downcase (file-name-nondirectory filename))
  209. 0 -4)))
  210. ;; TIT keywords and the corresponding default values.
  211. (tit-multichoice t)
  212. (tit-prompt "")
  213. (tit-comments nil)
  214. (tit-backspace "\010\177")
  215. (tit-deleteall "\015\025")
  216. (tit-moveright ".>")
  217. (tit-moveleft ",<")
  218. (tit-keyprompt nil))
  219. (princ ";; Quail package `")
  220. (princ package)
  221. (princ (format "' -*- coding:%s -*-\n" coding-system-for-write))
  222. (princ ";; Generated by the command `titdic-convert'\n;;\tDate: ")
  223. (princ (current-time-string))
  224. (princ "\n;;\tOriginal TIT dictionary file: ")
  225. (princ (file-name-nondirectory filename))
  226. (princ "\n\n;;; Comment:\n\n")
  227. (princ ";; Byte-compile this file again after any modification.\n\n")
  228. (princ ";;; Start of the header of original TIT dictionary.\n\n")
  229. (while (not (eobp))
  230. (let ((ch (following-char))
  231. (pos (point)))
  232. (cond ((= ch ?C) ; COMMENT
  233. (cond ((looking-at "COMMENT")
  234. (let ((pos (match-end 0)))
  235. (end-of-line)
  236. (setq tit-comments
  237. (cons (buffer-substring-no-properties pos (point))
  238. tit-comments))))))
  239. ((= ch ?M) ; MULTICHOICE, MOVERIGHT, MOVELEFT
  240. (cond ((looking-at "MULTICHOICE:[ \t]*")
  241. (goto-char (match-end 0))
  242. (setq tit-multichoice (looking-at "YES")))
  243. ((looking-at "MOVERIGHT:[ \t]*")
  244. (goto-char (match-end 0))
  245. (setq tit-moveright (tit-read-key-value)))
  246. ((looking-at "MOVELEFT:[ \t]*")
  247. (goto-char (match-end 0))
  248. (setq tit-moveleft (tit-read-key-value)))))
  249. ((= ch ?P) ; PROMPT
  250. (cond ((looking-at "PROMPT:[ \t]*")
  251. (goto-char (match-end 0))
  252. (setq tit-prompt (tit-read-key-value))
  253. ;; Some TIT dictionaries that are encoded by
  254. ;; euc-china contains invalid character at the tail.
  255. (let* ((last (aref tit-prompt (1- (length tit-prompt))))
  256. (split (split-char last)))
  257. (if (or (eq (nth 1 split) 32)
  258. (eq (nth 2 split) 32))
  259. (setq tit-prompt (substring tit-prompt 0 -1)))))))
  260. ((= ch ?B) ; BACKSPACE, BEGINDICTIONARY,
  261. ; BEGINPHRASE
  262. (cond ((looking-at "BACKSPACE:[ \t]*")
  263. (goto-char (match-end 0))
  264. (setq tit-backspace (tit-read-key-value)))
  265. ((looking-at "BEGINDICTIONARY")
  266. (setq tit-dictionary t))
  267. ((looking-at "BEGINPHRASE")
  268. (setq tit-dictionary nil))))
  269. ((= ch ?K) ; KEYPROMPT
  270. (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
  271. (let ((key-char (match-string 1)))
  272. (goto-char (match-end 0))
  273. (if (string-match "\\\\[0-9]+" key-char)
  274. (setq key-char
  275. (car (read-from-string (format "\"%s\""
  276. key-char)))))
  277. (setq tit-keyprompt
  278. (cons (cons key-char (tit-read-key-value))
  279. tit-keyprompt)))))))
  280. (end-of-line)
  281. (princ ";; ")
  282. (princ (buffer-substring-no-properties pos (point)))
  283. (princ "\n")
  284. (forward-line 1)))
  285. (princ "\n;;; End of the header of original TIT dictionary.\n\n")
  286. (princ ";;; Code:\n\n(require 'quail)\n\n")
  287. (princ "(quail-define-package ")
  288. ;; Args NAME, LANGUAGE, TITLE
  289. (let ((title (nth 1 (assoc package quail-cxterm-package-ext-info))))
  290. (princ "\"")
  291. (princ package)
  292. (princ "\" \"")
  293. (princ (nth 2 (assoc tit-encode tit-encode-list)))
  294. (princ "\" \"")
  295. (princ (or title
  296. (if (string-match "[:$A!K$(0!(!J(B]+\\([^:$A!K$(0!(!K(B]+\\)" tit-prompt)
  297. (substring tit-prompt (match-beginning 1) (match-end 1))
  298. tit-prompt)))
  299. (princ "\"\n"))
  300. ;; Arg GUIDANCE
  301. (if tit-keyprompt
  302. (progn
  303. (princ " '(")
  304. (while tit-keyprompt
  305. (princ " ")
  306. (princ (format "(%d . \"%s\")\n"
  307. (string-to-char (car (car tit-keyprompt)))
  308. (cdr (car tit-keyprompt))))
  309. (setq tit-keyprompt (cdr tit-keyprompt)))
  310. (princ ")"))
  311. (princ " t\n"))
  312. ;; Arg DOCSTRING
  313. (let ((doc (concat tit-prompt "\n"))
  314. (comments (if tit-comments
  315. (mapconcat 'identity (nreverse tit-comments) "\n")))
  316. (doc-ext (nth 2 (assoc package quail-cxterm-package-ext-info))))
  317. (if comments
  318. (setq doc (concat doc "\n" comments "\n")))
  319. (if doc-ext
  320. (setq doc (concat doc "\n" doc-ext "\n")))
  321. (prin1 doc)
  322. (terpri))
  323. ;; Arg KEY-BINDINGS
  324. (princ " '(")
  325. (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
  326. (princ "\n ")
  327. (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
  328. (princ "\n ")
  329. (tit-generate-key-bindings tit-moveright 'quail-next-translation)
  330. (princ "\n ")
  331. (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
  332. (princ ")\n")
  333. ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
  334. ;; The remaining args are all nil.
  335. (princ " nil")
  336. (princ (if tit-multichoice " nil" " t"))
  337. (princ (if tit-keyprompt " t t)\n\n" " nil nil)\n\n"))))
  338. (defsubst tit-flush-translations (key translations)
  339. (if (string-match "\\\\[0-9][0-9][0-9]" key)
  340. (let ((newkey (concat (substring key 0 (match-beginning 0))
  341. (car (read-from-string
  342. (concat "\"" (match-string 0 key) "\"")))))
  343. (idx (match-end 0)))
  344. (while (string-match "\\\\[0-9][0-9][0-9]" key idx)
  345. (setq newkey (concat
  346. newkey
  347. (substring key idx (match-beginning 0))
  348. (car (read-from-string
  349. (concat "\"" (match-string 0 key) "\"")))))
  350. (setq idx (match-end 0)))
  351. (setq key (concat newkey (substring key idx)))))
  352. (prin1 (list key (if tit-dictionary translations
  353. (vconcat (nreverse translations)))))
  354. (princ "\n"))
  355. ;; Convert body part of TIT dictionary into `quail-define-rules'
  356. ;; function call.
  357. (defun tit-process-body ()
  358. (message "Formatting translation rules...")
  359. (let* ((template (list nil nil))
  360. (second (cdr template))
  361. (prev-key "")
  362. ch key translations pos)
  363. (princ "(quail-define-rules\n")
  364. (while (null (eobp))
  365. (setq ch (following-char))
  366. (if (or (= ch ?#) (= ch ?\n))
  367. (forward-line 1)
  368. (setq pos (point))
  369. (skip-chars-forward "^ \t\n")
  370. (setq key (buffer-substring-no-properties pos (point)))
  371. (skip-chars-forward " \t")
  372. (setq ch (following-char))
  373. (if (or (= ch ?#) (= ch ?\n))
  374. ;; This entry contains no translations. Let's ignore it.
  375. (forward-line 1)
  376. (or (string= key prev-key)
  377. (progn
  378. (if translations
  379. (tit-flush-translations prev-key translations))
  380. (setq translations nil
  381. prev-key key)))
  382. (if tit-dictionary
  383. (progn
  384. (setq pos (point))
  385. (skip-chars-forward "^ \t#\n")
  386. (setq translations
  387. (if translations
  388. (concat translations
  389. (buffer-substring-no-properties pos (point)))
  390. (buffer-substring-no-properties pos (point)))))
  391. (while (not (eolp))
  392. (setq pos (point))
  393. (skip-chars-forward "^ \t\n")
  394. (setq translations (cons (buffer-substring-no-properties
  395. pos (point))
  396. translations))
  397. (skip-chars-forward " \t")
  398. (setq ch (following-char))
  399. (if (= ch ?#) (end-of-line))))
  400. (forward-line 1))))
  401. (if translations
  402. (tit-flush-translations prev-key translations))
  403. (princ ")\n")))
  404. ;;;###autoload
  405. (defun titdic-convert (filename &optional dirname)
  406. "Convert a TIT dictionary of FILENAME into a Quail package.
  407. Optional argument DIRNAME if specified is the directory name under which
  408. the generated Quail package is saved."
  409. (interactive "FTIT dictionary file: ")
  410. (let ((coding-system-for-write nil))
  411. (with-temp-file (tit-make-quail-package-file-name filename dirname)
  412. (let ((standard-output (current-buffer)))
  413. (with-temp-buffer
  414. (set-buffer-multibyte nil)
  415. ;; Here we must use `raw-text' instead of `no-conversion' to
  416. ;; enable auto-decoding of eol format (CRLF->LF).
  417. (let ((coding-system-for-read 'raw-text))
  418. (insert-file-contents (expand-file-name filename)))
  419. ;; Decode the buffer contents from the encoding specified by a
  420. ;; value of the key "ENCODE:".
  421. (if (not (search-forward "\nBEGIN" nil t))
  422. (error "TIT dictionary doesn't have body part"))
  423. (let ((limit (point))
  424. coding-system slot)
  425. (goto-char (point-min))
  426. (if (re-search-forward "^ENCODE:[ \t]*" limit t)
  427. (progn
  428. (goto-char (match-end 0))
  429. (setq tit-encode (tit-read-key-value)))
  430. (setq tit-encode tit-default-encode))
  431. (setq slot (assoc tit-encode tit-encode-list))
  432. (if (not slot)
  433. (error "Invalid ENCODE: value in TIT dictionary"))
  434. (setq coding-system (nth 1 slot))
  435. (message "Decoding with coding system %s..." coding-system)
  436. (goto-char (point-min))
  437. (decode-coding-region (point-min) (point-max) coding-system)
  438. ;; Explicitly set eol format to `unix'.
  439. (setq coding-system-for-write
  440. (coding-system-change-eol-conversion coding-system 'unix))
  441. (remove-text-properties (point-min) (point-max) '(charset nil)))
  442. (set-buffer-multibyte t)
  443. ;; Set point the starting position of the body part.
  444. (goto-char (point-min))
  445. (if (not (search-forward "\nBEGIN" nil t))
  446. (error "TIT dictionary can't be decoded correctly"))
  447. ;; Process the header part.
  448. (forward-line 1)
  449. (narrow-to-region (point-min) (point))
  450. (tit-process-header filename)
  451. (widen)
  452. ;; Process the body part
  453. (tit-process-body))))))
  454. ;;;###autoload
  455. (defun batch-titdic-convert (&optional force)
  456. "Run `titdic-convert' on the files remaining on the command line.
  457. Use this from the command line, with `-batch';
  458. it won't work in an interactive Emacs.
  459. For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
  460. generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
  461. To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
  462. (defvar command-line-args-left) ; Avoid compiler warning.
  463. (if (not noninteractive)
  464. (error "`batch-titdic-convert' should be used only with -batch"))
  465. (if (string= (car command-line-args-left) "-h")
  466. (progn
  467. (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
  468. (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
  469. (message "To convert XXX.tit into DIR/xxx.el:")
  470. (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
  471. (let (targetdir filename files file)
  472. (if (string= (car command-line-args-left) "-dir")
  473. (progn
  474. (setq command-line-args-left (cdr command-line-args-left))
  475. (setq targetdir (car command-line-args-left))
  476. (setq command-line-args-left (cdr command-line-args-left))))
  477. (while command-line-args-left
  478. (setq filename (expand-file-name (car command-line-args-left)))
  479. (if (file-directory-p filename)
  480. (progn
  481. (message "Converting all tit files in the directory %s" filename)
  482. (setq files (directory-files filename t "\\.tit$")))
  483. (setq files (list filename)))
  484. (while files
  485. (setq file (expand-file-name (car files)))
  486. (when (or force
  487. (file-newer-than-file-p
  488. file (tit-make-quail-package-file-name file targetdir)))
  489. (message "Converting %s to quail-package..." file)
  490. (titdic-convert file targetdir))
  491. (setq files (cdr files)))
  492. (setq command-line-args-left (cdr command-line-args-left)))
  493. (message "Byte-compile the created files by:")
  494. (message " %% emacs -batch -f batch-byte-compile XXX.el")))
  495. (kill-emacs 0))
  496. ;;; Converter of miscellaneous dictionaries other than TIT format.
  497. ;; Alist of input method names and the corresponding information.
  498. ;; Each element has this form:
  499. ;; (INPUT-METHOD-NAME ;; Name of the input method.
  500. ;; INPUT-METHOD-TITLE ;; Title string of the input method
  501. ;; DICFILE ;; Name of the source dictionary file.
  502. ;; CODING ;; Coding system of the dictionary file.
  503. ;; QUAILFILE ;; Name of the Quail package file.
  504. ;; CONVERTER ;; Function to generate the Quail package.
  505. ;; COPYRIGHT-NOTICE ;; Copyright notice of the source dictionary.
  506. ;; )
  507. (defvar quail-misc-package-ext-info
  508. '(("chinese-b5-tsangchi" "$(06A(BB"
  509. "cangjie-table.b5" big5 "tsang-b5.el"
  510. tsang-b5-converter
  511. "\
  512. ;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
  513. ;; #
  514. ;; # Permission to copy and distribute both modified and
  515. ;; # unmodified versions is granted without royalty provided
  516. ;; # this notice is preserved.")
  517. ("chinese-b5-quick" "$(0X|(BB"
  518. "cangjie-table.b5" big5 "quick-b5.el"
  519. quick-b5-converter
  520. "\
  521. ;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
  522. ;; #
  523. ;; # Permission to copy and distribute both modified and
  524. ;; # unmodified versions is granted without royalty provided
  525. ;; # this notice is preserved.")
  526. ("chinese-cns-tsangchi" "$(GT?(BC"
  527. "cangjie-table.cns" iso-2022-cn-ext "tsang-cns.el"
  528. tsang-cns-converter
  529. "\
  530. ;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
  531. ;; #
  532. ;; # Permission to copy and distribute both modified and
  533. ;; # unmodified versions is granted without royalty provided
  534. ;; # this notice is preserved.")
  535. ("chinese-cns-quick" "$(Gv|(BC"
  536. "cangjie-table.cns" iso-2022-cn-ext "quick-cns.el"
  537. quick-cns-converter
  538. "\
  539. ;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
  540. ;; #
  541. ;; # Permission to copy and distribute both modified and
  542. ;; # unmodified versions is granted without royalty provided
  543. ;; # this notice is preserved.")
  544. ("chinese-py" "$AF4(BG"
  545. "pinyin.map" cn-gb-2312 "PY.el"
  546. py-converter
  547. "\
  548. ;; \"pinyin.map\" is included in a free package called CCE. It is
  549. ;; available at:
  550. ;; http://ftp.debian.org/debian/dists/potato/main
  551. ;; /source/utils/cce_0.36.orig.tar.gz
  552. ;; This package contains the following copyright notice.
  553. ;;
  554. ;;
  555. ;; Copyright (C) 1999, Rui He, herui@cs.duke.edu
  556. ;;
  557. ;;
  558. ;; CCE(Console Chinese Environment) 0.32
  559. ;;
  560. ;; CCE is free software; you can redistribute it and/or modify it under the
  561. ;; terms of the GNU General Public License as published by the Free Software
  562. ;; Foundation; either version 1, or (at your option) any later version.
  563. ;;
  564. ;; CCE is distributed in the hope that it will be useful, but WITHOUT ANY
  565. ;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  566. ;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  567. ;; details.
  568. ;;
  569. ;; You should have received a copy of the GNU General Public License along with
  570. ;; CCE; see the file COPYING. If not, write to the Free Software Foundation,
  571. ;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
  572. ("chinese-ziranma" "$AWTH;(B"
  573. "ziranma.cin" cn-gb-2312 "ZIRANMA.el"
  574. ziranma-converter
  575. "\
  576. ;; \"ziranma.cin\" is included in a free package called CCE. It is
  577. ;; available at:
  578. ;; http://ftp.debian.org/debian/dists/potato/main
  579. ;; /source/utils/cce_0.36.orig.tar.gz
  580. ;; This package contains the following copyright notice.
  581. ;;
  582. ;;
  583. ;; Copyright (C) 1999, Rui He, herui@cs.duke.edu
  584. ;;
  585. ;;
  586. ;; CCE(Console Chinese Environment) 0.32
  587. ;;
  588. ;; CCE is free software; you can redistribute it and/or modify it under the
  589. ;; terms of the GNU General Public License as published by the Free Software
  590. ;; Foundation; either version 1, or (at your option) any later version.
  591. ;;
  592. ;; CCE is distributed in the hope that it will be useful, but WITHOUT ANY
  593. ;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  594. ;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  595. ;; details.
  596. ;;
  597. ;; You should have received a copy of the GNU General Public License along with
  598. ;; CCE; see the file COPYING. If not, write to the Free Software Foundation,
  599. ;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
  600. ("chinese-ctlau" "$AAuTA(B"
  601. "CTLau.html" cn-gb-2312 "CTLau.el"
  602. ctlau-gb-converter
  603. "\
  604. ;; \"CTLau.html\" is available at:
  605. ;;
  606. ;; http://umunhum.stanford.edu/~lee/chicomp/CTLau.html
  607. ;;
  608. ;; It contains the following copyright notice:
  609. ;;
  610. ;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
  611. ;; #
  612. ;; # This program is free software; you can redistribute it and/or
  613. ;; # modify it under the terms of the GNU General Public License
  614. ;; # as published by the Free Software Foundation; either version 2
  615. ;; # of the License, or any later version.
  616. ;; #
  617. ;; # This program is distributed in the hope that it will be useful,
  618. ;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
  619. ;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  620. ;; # GNU General Public License for more details.
  621. ;; #
  622. ;; # You should have received a copy of the GNU General Public License
  623. ;; # along with this program; if not, write to the Free Software Foundation,
  624. ;; # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
  625. ("chinese-ctlaub" "$(0N,Gn(B"
  626. "CTLau-b5.html" big5 "CTLau-b5.el"
  627. ctlau-b5-converter
  628. "\
  629. ;; \"CTLau-b5.html\" is available at:
  630. ;;
  631. ;; http://umunhum.stanford.edu/~lee/chicomp/CTLau-b5.html
  632. ;;
  633. ;; It contains the following copyright notice:
  634. ;;
  635. ;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
  636. ;; #
  637. ;; # This program is free software; you can redistribute it and/or
  638. ;; # modify it under the terms of the GNU General Public License
  639. ;; # as published by the Free Software Foundation; either version 2
  640. ;; # of the License, or any later version.
  641. ;; #
  642. ;; # This program is distributed in the hope that it will be useful,
  643. ;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
  644. ;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  645. ;; # GNU General Public License for more details.
  646. ;; #
  647. ;; # You should have received a copy of the GNU General Public License
  648. ;; # along with this program; if not, write to the Free Software Foundation,
  649. ;; # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
  650. ))
  651. ;; Generate a code of a Quail package in the current buffer from Tsang
  652. ;; dictionary in the buffer DICBUF. The input method name of the
  653. ;; Quail package is NAME, and the title string is TITLE.
  654. ;; TSANG-P is non-nil, generate $(06AQo(B input method. Otherwise
  655. ;; generate $(0X|/y(B (simple version of $(06AQo(B). If BIG5-P is non-nil, the
  656. ;; input method is for inputting Big5 characters. Otherwise the input
  657. ;; method is for inputting CNS characters.
  658. (defun tsang-quick-converter (dicbuf name title tsang-p big5-p)
  659. (let ((fulltitle (if tsang-p (if big5-p "$(06AQo(B" "$(GT?on(B")
  660. (if big5-p "$(0X|/y(B" "$(Gv|Mx(B")))
  661. dic)
  662. (goto-char (point-max))
  663. (if big5-p
  664. (insert (format "\"$(0&d'GTT&,!J(B%s$(0!K(BBIG5
  665. $(0KHM$(B%s$(0TT&,WoOu(B
  666. [Q $(0'D(B] [W $(0(q(B] [E $(0'V(B] [R $(0&H(B] [T $(0'>(B] [Y $(0&4(B] [U $(0&U(B] [I $(0'B(B] [O $(0&*(B] [P $(0'A(B]
  667. [A $(0'K(B] [S $(0&T(B] [D $(0'N(B] [F $(0'W(B] [G $(0&I(B] [H $(0*M(B] [J $(0&3(B] [L $(0&d(B]
  668. [Z ] [X $(0[E(B] [C $(01[(B] [V $(0&M(B] [B $(0'M(B] [N $(0&_(B] [M $(0&"(B]
  669. \\\\<quail-translation-docstring>\"\n"
  670. fulltitle fulltitle))
  671. (insert (format "\"$(GDcEFrSD+!J(B%s$(G!K(BCNS
  672. $(GiGk#(B%s$(GrSD+uomu(B
  673. [Q $(GEC(B] [W $(GFp(B] [E $(GEU(B] [R $(GDG(B] [T $(GE=(B] [Y $(GD3(B] [U $(GDT(B] [I $(GEA(B] [O $(GD)(B] [P $(GE@(B]
  674. [A $(GEJ(B] [S $(GDS(B] [D $(GEM(B] [F $(GEV(B] [G $(GDH(B] [H $(GHL(B] [J $(GD2(B] [L $(GDc(B]
  675. [Z ] [X $(GyE(B] [C $(GOZ(B] [V $(GDL(B] [B $(GEL(B] [N $(GD^(B] [M $(GD!(B]
  676. \\\\<quail-translation-docstring>\"\n"
  677. fulltitle fulltitle)))
  678. (insert " '((\".\" . quail-next-translation-block)
  679. (\",\" . quail-prev-translation-block))
  680. nil nil)\n\n")
  681. (insert "(quail-define-rules\n")
  682. (with-current-buffer dicbuf
  683. ;; Handle double CR line ends, which result when checking out of
  684. ;; CVS on MS-Windows.
  685. (goto-char (point-min))
  686. (search-forward "A440")
  687. (beginning-of-line)
  688. (let ((table (make-hash-table :test 'equal))
  689. val)
  690. (while (not (eobp))
  691. (forward-char 5)
  692. (let ((trans (char-to-string (following-char)))
  693. key slot)
  694. (re-search-forward "\\([A-Z]+\\)\r*$" nil t)
  695. (setq key (downcase
  696. (if (or tsang-p
  697. (<= (- (match-end 1) (match-beginning 1)) 1))
  698. (match-string 1)
  699. (string (char-after (match-beginning 1))
  700. (char-after (1- (match-end 1)))))))
  701. (setq val (gethash key table))
  702. (if val (setq trans (concat val trans)))
  703. (puthash key trans table)
  704. (forward-line 1)))
  705. (maphash #'(lambda (key val) (setq dic (cons (cons key val) dic)))
  706. table)))
  707. (setq dic (sort dic (function (lambda (x y) (string< (car x ) (car y))))))
  708. (dolist (elt dic)
  709. (insert (format "(%S\t%S)\n" (car elt) (cdr elt))))
  710. (let ((punctuation '((";" "$(0!'!2!"!#!.!/(B" "$(G!'!2!"!#!.!/(B")
  711. (":" "$(0!(!+!3!%!$!&!0!1(B" "$(G!(!+!3!%!$!&!0!1(B")
  712. ("'" "$(0!e!d(B" "$(G!e!d(B")
  713. ("\"" "$(0!g!f!h!i!q(B" "$(G!g!f!h!i!q(B")
  714. ("\\" "$(0"`"b#M(B" "$(G"`"b#M(B")
  715. ("|" "$(0!6!8!:"^(B" "$(G!6!8!:"^(B")
  716. ("/" "$(0"_"a#L(B" "$(G"_"a#L(B")
  717. ("?" "$(0!)!4(B" "$(G!)!4(B")
  718. ("<" "$(0!R"6"A!T"H(B" "$(G!R"6"A!T"H(B")
  719. (">" "$(0!S"7"B!U(B" "$(G!S"7"B!U(B")
  720. ("[" "$(0!F!J!b!H!L!V!Z!X!\(B" "$(G!F!J!b!H!L!V!Z!X!\(B")
  721. ("]" "$(0!G!K!c!I!M!W![!Y!](B" "$(G!G!K!c!I!M!W![!Y!](B")
  722. ("{" "$(0!B!`!D(B " "$(G!B!`!D(B ")
  723. ("}" "$(0!C!a!E(B" "$(G!C!a!E(B")
  724. ("`" "$(0!j!k(B" "$(G!j!k(B")
  725. ("~" "$(0"D"+",!<!=(B" "$(G"D"+",!<!=(B")
  726. ("!" "$(0!*!5(B" "$(G!*!5(B")
  727. ("@" "$(0"i"n(B" "$(G"i"n(B")
  728. ("#" "$(0!l"-(B" "$(G!l"-(B")
  729. ("$" "$(0"c"l(B" "$(G"c"l(B")
  730. ("%" "$(0"h"m(B" "$(G"h"m(B")
  731. ("&" "$(0!m".(B" "$(G!m".(B")
  732. ("*" "$(0!n"/!o!w!x(B" "$(G!n"/!o!w!x(B")
  733. ("(" "$(0!>!^!@(B" "$(G!>!^!@(B")
  734. (")" "$(0!?!_!A(B" "$(G!?!_!A(B")
  735. ("-" "$(0!7!9"#"$"1"@(B" "$(G!7!9"#"$"1"@(B")
  736. ("_" "$(0"%"&(B" "$(G"%"&(B")
  737. ("=" "$(0"8"C(B" "$(G"8"C(B")
  738. ("+" "$(0"0"?(B" "$(G"0"?(B"))))
  739. (dolist (elt punctuation)
  740. (insert (format "(%S %S)\n" (concat "z" (car elt))
  741. (if big5-p (nth 1 elt) (nth 2 elt))))))
  742. (insert ")\n")))
  743. (defun tsang-b5-converter (dicbuf name title)
  744. (tsang-quick-converter dicbuf name title t t))
  745. (defun quick-b5-converter (dicbuf name title)
  746. (tsang-quick-converter dicbuf name title nil t))
  747. (defun tsang-cns-converter (dicbuf name title)
  748. (tsang-quick-converter dicbuf name title t nil))
  749. (defun quick-cns-converter (dicbuf name title)
  750. (tsang-quick-converter dicbuf name title nil nil))
  751. ;; Generate a code of a Quail package in the current buffer from
  752. ;; Pinyin dictionary in the buffer DICBUF. The input method name of
  753. ;; the Quail package is NAME, and the title string is TITLE.
  754. (defun py-converter (dicbuf name title)
  755. (goto-char (point-max))
  756. (insert (format "%S\n" "$A::WVJdHk!KF4Rt!K(B
  757. $AF4Rt7=08(B
  758. $AP!P4S"NDWVD84z1m!8F4Rt!97{:E#,(B \"u(yu) $ATrSC(B u: $A1mJ>!C(B
  759. Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
  760. Pinyin is the standard roman transliteration method for Chinese.
  761. Pinyin uses a sequence of Latin alphabetic characters for each Chinese
  762. character. The sequence is made by the combination of the initials
  763. \(the beginning sounds) and finals (the ending sounds).
  764. initials: b p m f d t n l z c s zh ch sh r j q x g k h
  765. finals: a o e i er ai ei oa ou an en ang eng ong i ia iao ie iu ian in
  766. iang ing iong u ua uo uai ui uan un uan ueng yu yue yuan yun
  767. (Note: In the correct Pinyin writing, the sequence \"yu\" in the last
  768. four finals should be written by the character u-umlaut `$A(9(B'.)
  769. With this input method, you enter a Chinese character by first
  770. entering its pinyin spelling.
  771. \\<quail-translation-docstring>
  772. For instance, to input $ADc(B, you type \"n i C-n 3\". The first \"n i\"
  773. is a Pinyin, \"C-n\" selects the next group of candidates (each group
  774. contains at most 10 characters), \"3\" select the third character in
  775. that group.
  776. This input method supports only Han characters. The related input
  777. method `chinese-py-punct' is the combination of this method and
  778. `chinese-punct'; it supports both Han characters and punctuation
  779. characters.
  780. For double-width GB2312 characters corresponding to ASCII, use the
  781. input method `chinese-qj'.
  782. The correct Pinyin system specifies tones by diacritical marks, but
  783. this input method doesn't use them, which results in easy (you don't
  784. have to know the exact tones), but verbose (many characters are assigned
  785. to the same key sequence) input. You may also want to try the input
  786. method `chinese-tonepy' with which you must specify tones by digits
  787. \(1..5)."))
  788. (insert " '((\"\C-?\" . quail-delete-last-char)
  789. (\".\" . quail-next-translation)
  790. (\">\" . quail-next-translation)
  791. (\",\" . quail-prev-translation)
  792. (\"<\" . quail-prev-translation))
  793. nil nil nil nil)\n\n")
  794. (insert "(quail-define-rules\n")
  795. (let ((pos (point)))
  796. (insert-buffer-substring-no-properties dicbuf)
  797. (goto-char pos)
  798. (re-search-forward "^[a-z]")
  799. (beginning-of-line)
  800. (delete-region pos (point))
  801. (while (not (eobp))
  802. (insert "(\"")
  803. (skip-chars-forward "a-z")
  804. (insert "\" \"")
  805. (delete-char 1)
  806. (end-of-line)
  807. (while (= (preceding-char) ?\r)
  808. (delete-char -1))
  809. (insert "\")")
  810. (forward-line 1)))
  811. (insert ")\n"))
  812. ;; Generate a code of a Quail package in the current buffer from
  813. ;; Ziranma dictionary in the buffer DICBUF. The input method name of
  814. ;; the Quail package is NAME, and the title string is TITLE.
  815. (defun ziranma-converter (dicbuf name title)
  816. (let (dic)
  817. (with-current-buffer dicbuf
  818. (goto-char (point-min))
  819. (search-forward "\n%keyname end")
  820. (forward-line 1)
  821. (let ((table (make-hash-table :test 'equal))
  822. elt pos key trans val)
  823. (while (not (eobp))
  824. (setq pos (point))
  825. (skip-chars-forward "^ \t")
  826. (setq key (buffer-substring-no-properties pos (point)))
  827. (skip-chars-forward " \t")
  828. (setq pos (point))
  829. (skip-chars-forward "^\r\n")
  830. (setq trans (vector (buffer-substring-no-properties pos (point))))
  831. (setq val (gethash key table))
  832. (if val (setq trans (vconcat val trans)))
  833. (puthash key trans table)
  834. (forward-line 1))
  835. (maphash #'(lambda (key trans)
  836. (let ((len (length trans))
  837. i)
  838. (if (and (= len 1) (= (length (aref trans 0)) 1))
  839. (setq trans (aref trans 0))
  840. (setq i 0)
  841. (while (and (< i len)
  842. (= (length (aref trans i)) 1))
  843. (setq i (1+ i)))
  844. (if (= i len)
  845. (setq trans (mapconcat 'identity trans "")))))
  846. (setq dic (cons (cons key trans) dic)))
  847. table)))
  848. (setq dic (sort dic (function (lambda (x y) (string< (car x) (car y))))))
  849. (goto-char (point-max))
  850. (insert (format "%S\n" "$A::WVJdHk!K!>WTH;!?!K(B
  851. $A<|EL6TUU1m(B:
  852. $A)3)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)7(B
  853. $A)'#Q(B $A)'#W(B $A)'#E(B $A)'#R(B $A)'#T(B $A)'#Y(B $A)'#U(Bsh$A)'#I(Bch$A)'#O(B $A)'#P(B $A)'(B
  854. $A)'(B iu$A)'(B ua$A)'(B e$A)'(B uan$A)'(B ue$A)'(B uai$A)'(B u$A)'(B i$A)'(B o$A)'(B un$A)'(B
  855. $A)'(B $A)'(B ia$A)'(B $A)'(B van$A)'(B ve$A)'(B ing$A)'(B $A)'(B $A)'(B uo$A)'(B vn$A)'(B
  856. $A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)?(B
  857. $A)'#A(B $A)'#S(B $A)'#D(B $A)'#F(B $A)'#G(B $A)'#H(B $A)'#J(B $A)'#K(B $A)'#L(B $A)'(B
  858. $A)'(B a$A)'(Biong$A)'(Buang$A)'(B en$A)'(B eng$A)'(B ang$A)'(B an$A)'(B ao$A)'(B ai$A)'(B
  859. $A)'(B $A)'(B ong$A)'(Biang$A)'(B $A)'(B ng$A)'(B $A)'(B $A)'(B $A)'(B $A)'(B
  860. $A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)%)7(B
  861. $A)'#Z(B $A)'#X(B $A)'#C(B $A)'#V(Bzh$A)'#B(B $A)'#N(B $A)'#M(B $A)'#,(B $A)'#.(B $A)'(B $A#/(B $A)'(B
  862. $A)'(B ei$A)'(B ie$A)'(B iao$A)'(B ui$A)'(B ou$A)'(B in$A)'(B ian$A)'G0R3)':sR3)'7{:E)'(B
  863. $A)'(B $A)'(B $A)'(B $A)'(B v$A)'(B $A)'(B $A)'(B $A)'(B $A)'(B $A)'(B $A)'(B
  864. $A);)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)?(B
  865. Pinyin base input method for Chinese GB2312 characters (`chinese-gb2312').
  866. Pinyin is the standard roman transliteration method for Chinese.
  867. For the details of Pinyin system, see the documentation of the input
  868. method `chinese-py'.
  869. Unlike the standard spelling of Pinyin, in this input method all
  870. initials and finals are assigned to single keys (see the above table).
  871. For instance, the initial \"ch\" is assigned to the key `i', the final
  872. \"iu\" is assigned to the key `q', and tones 1, 2, 3, 4, and $AGaIy(B are
  873. assigned to the keys `q', `w', `e', `r', `t' respectively.
  874. \\<quail-translation-docstring>
  875. To input one-letter words, you type 4 keys, the first two for the
  876. Pinyin of the letter, next one for tone, and the last one is always a
  877. quote ('). For instance, \"vsq'\" input $AVP(B. Exceptions are these
  878. letters. You can input them just by typing a single key.
  879. Character: $A04(B $A2;(B $A4N(B $A5D(B $A6~(B $A7"(B $A8v(B $A:M(B $A3v(B $A<0(B $A?I(B $AAK(B $AC;(B
  880. Key: a b c d e f g h i j k l m
  881. Character: $ADc(B $AE7(B $AF,(B $AF_(B $AHK(B $AH}(B $AK{(B $AJG(B $AWE(B $ANR(B $AP!(B $AR;(B $ATZ(B
  882. Key: n o p q r s t u v w x y z
  883. To input two-letter words, you have two ways. One way is to type 4
  884. keys, two for the first Pinyin, two for the second Pinyin. For
  885. instance, \"vsgo\" inputs $AVP9z(B. Another way is to type 3 keys: 2
  886. initials of two letters, and quote ('). For instance, \"vg'\" also
  887. inputs $AVP9z(B.
  888. To input three-letter words, you type 4 keys: initials of three
  889. letters, and the last is quote ('). For instance, \"bjy'2\" inputs $A11(B
  890. $A>)Q<(B (the last `2' is to select one of the candidates).
  891. To input words of more than three letters, you type 4 keys, initials
  892. of the first three letters and the last letter. For instance,
  893. \"bjdt\" inputs $A11>)5gJSL((B.
  894. To input symbols and punctuation, type `/' followed by one of `a' to
  895. `z', then select one of the candidates."))
  896. (insert " '((\"\C-?\" . quail-delete-last-char)
  897. (\".\" . quail-next-translation)
  898. (\"[\" . quail-next-translation)
  899. (\",\" . quail-prev-translation)
  900. (\"]\" . quail-prev-translation))
  901. nil nil nil nil)\n\n")
  902. (insert "(quail-define-rules\n")
  903. (dolist (elt dic)
  904. (insert (format "(%S %S)\n" (car elt) (cdr elt))))
  905. (insert ")\n")))
  906. ;; Generate the code for a Quail package in the current buffer from a
  907. ;; CTLau or CTLau-b5 dictionary in the buffer DICBUF. The input
  908. ;; method name of the Quail package is NAME, and the title string is
  909. ;; TITLE. DESCRIPTION is the string shown by describe-input-method.
  910. (defun ctlau-converter (dicbuf name title description)
  911. (goto-char (point-max))
  912. (insert (format "%S\n" description))
  913. (insert " '((\"\C-?\" . quail-delete-last-char)
  914. (\".\" . quail-next-translation)
  915. (\">\" . quail-next-translation)
  916. (\",\" . quail-prev-translation)
  917. (\"<\" . quail-prev-translation))
  918. nil nil nil nil)\n\n")
  919. (insert "(quail-define-rules\n")
  920. (let (dicbuf-start dicbuf-end key-start key (pos (point)))
  921. ;; Find the dictionary, which starts below a horizontal rule and
  922. ;; ends at the second to last line in the HTML file.
  923. (with-current-buffer dicbuf
  924. (goto-char (point-min))
  925. (re-search-forward "^#<hr>")
  926. (forward-line 1)
  927. (setq dicbuf-start (point))
  928. (goto-char (point-max))
  929. (re-search-backward "^<hr>")
  930. (setq dicbuf-end (point)))
  931. (insert-buffer-substring-no-properties dicbuf dicbuf-start dicbuf-end)
  932. ;; CTLau-b5.html contains characters (0xa1 0xbc) which show up as
  933. ;; hollow boxes when the original characters in CTLau.html from
  934. ;; which the file is converted have no Big5 equivalent. Go
  935. ;; through and delete them.
  936. (goto-char pos)
  937. (while (search-forward "$(0!{(B" nil t)
  938. (delete-char -1))
  939. ;; Uppercase keys in dictionary need to be downcased. Backslashes
  940. ;; at the beginning of keys need to be turned into double
  941. ;; backslashes.
  942. (goto-char pos)
  943. (while (not (eobp))
  944. (insert "(\"")
  945. (if (char-equal (following-char) ?\\)
  946. (insert "\\"))
  947. (setq key-start (point))
  948. (skip-chars-forward "\\\\A-Z")
  949. (downcase-region key-start (point))
  950. (insert "\" \"")
  951. (delete-char 1)
  952. (end-of-line)
  953. (while (= (preceding-char) ?\r)
  954. (delete-char -1))
  955. (insert "\")")
  956. (forward-line 1)))
  957. (insert ")\n"))
  958. (defun ctlau-gb-converter (dicbuf name title)
  959. (ctlau-converter dicbuf name title
  960. "$A::WVJdHk!KAuN}OiJ=TARt!K(B
  961. $AAuN}OiJ=TASoW"Rt7=08(B
  962. Sidney Lau's Cantonese transcription scheme as described in his book
  963. \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
  964. This file was prepared by Fung Fung Lee ($A@n7c7e(B).
  965. Originally converted from CTCPS3.tit
  966. Last modified: June 2, 1993.
  967. Some infrequent GB characters are accessed by typing \\, followed by
  968. the Cantonese romanization of the respective radical ($A2?JW(B)."))
  969. (defun ctlau-b5-converter (dicbuf name title)
  970. (ctlau-converter dicbuf name title
  971. "$(0KH)tTT&,!(N,Tg>A*#Gn5x!((B
  972. $(0N,Tg>A*#GnM$0D5x'J7{(B
  973. Sidney Lau's Cantonese transcription scheme as described in his book
  974. \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
  975. This file was prepared by Fung Fung Lee ($(0,XFS76(B).
  976. Originally converted from CTCPS3.tit
  977. Last modified: June 2, 1993.
  978. Some infrequent characters are accessed by typing \\, followed by
  979. the Cantonese romanization of the respective radical ($(0?f5}(B)."))
  980. (declare-function dos-8+3-filename "dos-fns.el" (filename))
  981. (defun miscdic-convert (filename &optional dirname)
  982. "Convert a dictionary file FILENAME into a Quail package.
  983. Optional argument DIRNAME if specified is the directory name under which
  984. the generated Quail package is saved."
  985. (interactive "FInput method dictionary file: ")
  986. (or (file-readable-p filename)
  987. (error "%s does not exist" filename))
  988. (let ((tail quail-misc-package-ext-info)
  989. coding-system-for-write
  990. slot
  991. name title dicfile coding quailfile converter copyright
  992. dicbuf)
  993. (while tail
  994. (setq slot (car tail)
  995. dicfile (nth 2 slot)
  996. quailfile (nth 4 slot))
  997. (when (and (or (string-match dicfile filename)
  998. ;; MS-DOS filesystem truncates file names to 8+3
  999. ;; limits, so "cangjie-table.cns" becomes
  1000. ;; "cangjie-.cns", and the above string-match
  1001. ;; fails. Give DOS users a chance...
  1002. (and (fboundp 'msdos-long-file-names)
  1003. (not (msdos-long-file-names))
  1004. (string-match (dos-8+3-filename dicfile) filename)))
  1005. (if (file-newer-than-file-p
  1006. filename (expand-file-name quailfile dirname))
  1007. t
  1008. (message "%s is up to date" quailfile)
  1009. nil))
  1010. (setq name (car slot)
  1011. title (nth 1 slot)
  1012. coding (nth 3 slot)
  1013. converter (nth 5 slot)
  1014. copyright (nth 6 slot))
  1015. (message "Converting %s to %s..." dicfile quailfile)
  1016. ;; Explicitly set eol format to `unix'.
  1017. (setq coding-system-for-write
  1018. (coding-system-change-eol-conversion coding 'unix))
  1019. (with-temp-file (expand-file-name quailfile dirname)
  1020. (insert (format ";; Quail package `%s' -*- coding:%s -*-\n"
  1021. name coding))
  1022. (insert ";; Generated by the command `miscdic-convert'\n")
  1023. (insert ";; Date: " (current-time-string) "\n")
  1024. (insert ";; Source dictionary file: " dicfile "\n")
  1025. (insert ";; Copyright notice of the source file\n")
  1026. (insert ";;------------------------------------------------------\n")
  1027. (insert copyright "\n")
  1028. (insert ";;------------------------------------------------------\n")
  1029. (insert "\n")
  1030. (insert ";;; Code:\n\n")
  1031. (insert "(require 'quail)\n")
  1032. (insert "(quail-define-package \"" name "\" \""
  1033. (if (eq coding 'big5) "Chinese-BIG5"
  1034. (if (eq coding 'iso-2022-cn-ext) "Chinese-CNS"
  1035. "Chinese-GB"))
  1036. "\" \"" title "\" t\n")
  1037. (let* ((coding-system-for-read
  1038. (coding-system-change-eol-conversion coding 'unix))
  1039. (dicbuf (find-file-noselect filename)))
  1040. (funcall converter dicbuf name title)
  1041. (kill-buffer dicbuf)))
  1042. (message "Converting %s to %s...done" dicfile quailfile))
  1043. (setq tail (cdr tail)))))
  1044. (defun batch-miscdic-convert ()
  1045. "Run `miscdic-convert' on the files remaining on the command line.
  1046. Use this from the command line, with `-batch';
  1047. it won't work in an interactive Emacs.
  1048. If there's an argument \"-dir\", the next argument specifies a directory
  1049. to store generated Quail packages."
  1050. (defvar command-line-args-left) ; Avoid compiler warning.
  1051. (if (not noninteractive)
  1052. (error "`batch-miscdic-convert' should be used only with -batch"))
  1053. (let ((dir default-directory)
  1054. filename)
  1055. (while command-line-args-left
  1056. (if (string= (car command-line-args-left) "-dir")
  1057. (progn
  1058. (setq command-line-args-left (cdr command-line-args-left))
  1059. (setq dir (car command-line-args-left))
  1060. (setq command-line-args-left (cdr command-line-args-left))))
  1061. (setq filename (car command-line-args-left)
  1062. command-line-args-left (cdr command-line-args-left))
  1063. (if (file-directory-p filename)
  1064. (dolist (file (directory-files filename t nil t))
  1065. (or (file-directory-p file)
  1066. (miscdic-convert file dir)))
  1067. (miscdic-convert filename dir))))
  1068. (kill-emacs 0))
  1069. ;;; titdic-cnv.el ends here