unidata-gen.el 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. ;; unidata-gen.el -- Create files containing character property data.
  2. ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
  3. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  4. ;; Registration Number H13PRO009
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; SPECIAL NOTICE
  18. ;;
  19. ;; This file must be byte-compilable/loadable by `temacs' and also
  20. ;; the entry function `unidata-gen-files' must be runnable by
  21. ;; `temacs'.
  22. ;; FILES TO BE GENERATED
  23. ;;
  24. ;; The entry function `unidata-gen-files' generates these files in
  25. ;; the current directory.
  26. ;;
  27. ;; charprop.el
  28. ;; It contains a series of forms of this format:
  29. ;; (define-char-code-property PROP FILE)
  30. ;; where PROP is a symbol representing a character property
  31. ;; (name, general-category, etc), and FILE is a name of one of
  32. ;; the following files.
  33. ;;
  34. ;; uni-name.el, uni-category.el, uni-combining.el, uni-bidi.el,
  35. ;; uni-decomposition.el, uni-decimal.el, uni-digit.el, uni-numeric.el,
  36. ;; uni-mirrored.el, uni-old-name.el, uni-comment.el, uni-uppercase.el,
  37. ;; uni-lowercase.el, uni-titlecase.el
  38. ;; They contain one or more forms of this format:
  39. ;; (define-char-code-property PROP CHAR-TABLE)
  40. ;; where PROP is the same as above, and CHAR-TABLE is a
  41. ;; char-table containing property values in a compressed format.
  42. ;;
  43. ;; When they are installed in .../lisp/international/, the file
  44. ;; "charprop.el" is preloaded in loadup.el. The other files are
  45. ;; automatically loaded when the Lisp functions
  46. ;; `get-char-code-property' and `put-char-code-property', and C
  47. ;; function uniprop_table are called.
  48. ;;
  49. ;; FORMAT OF A CHAR TABLE
  50. ;;
  51. ;; We want to make a file size containing a char-table small. We
  52. ;; also want to load the file and get a property value fast. We
  53. ;; also want to reduce the used memory after loading it. So,
  54. ;; instead of naively storing a property value for each character in
  55. ;; a char-table (and write it out into a file), we store compressed
  56. ;; data in a char-table as below.
  57. ;;
  58. ;; If succeeding 128*N characters have the same property value, we
  59. ;; store that value (or the encoded one) for them. Otherwise,
  60. ;; compress values (or the encoded ones) for succeeding 128
  61. ;; characters into a single string and store it for those
  62. ;; characters. The way of compression depends on a property. See
  63. ;; the section "SIMPLE TABLE", "RUN-LENGTH TABLE", and "WORD-LIST
  64. ;; TABLE".
  65. ;; The char table has five extra slots:
  66. ;; 1st: property symbol
  67. ;; 2nd: function to call to get a property value,
  68. ;; or an index number of C function to decode the value,
  69. ;; or nil if the value can be directly got from the table.
  70. ;; 3nd: function to call to put a property value,
  71. ;; or an index number of C function to encode the value,
  72. ;; or nil if the value can be directly stored in the table.
  73. ;; 4th: function to call to get a description of a property value, or nil
  74. ;; 5th: data referred by the above functions
  75. ;; List of elements of this form:
  76. ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
  77. ;; CHAR-or-RANGE: a character code or a cons of character codes
  78. ;; PROPn: string representing the nth property value
  79. (defvar unidata-list nil)
  80. ;; Name of the directory containing files of Unicode Character
  81. ;; Database.
  82. (defvar unidata-dir nil)
  83. (defun unidata-setup-list (unidata-text-file)
  84. (let* ((table (list nil))
  85. (tail table)
  86. (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH)
  87. ("^<Hangul Syllable" . HANGUL\ SYLLABLE)
  88. ("^<.*Surrogate" . nil)
  89. ("^<.*Private Use" . PRIVATE\ USE)))
  90. val char name)
  91. (setq unidata-text-file (expand-file-name unidata-text-file unidata-dir))
  92. (or (file-readable-p unidata-text-file)
  93. (error "File not readable: %s" unidata-text-file))
  94. (with-temp-buffer
  95. ;; Insert a file of this format:
  96. ;; (CHAR NAME CATEGORY ...)
  97. ;; where CHAR is a character code, the following elements are strings
  98. ;; representing character properties.
  99. (insert-file-contents unidata-text-file)
  100. (goto-char (point-min))
  101. (condition-case nil
  102. (while t
  103. (setq val (read (current-buffer))
  104. char (car val)
  105. name (cadr val))
  106. ;; Check this kind of block.
  107. ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
  108. ;; 9FCB;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
  109. (if (and (= (aref name 0) ?<)
  110. (string-match ", First>$" name))
  111. (let ((first char)
  112. (l block-names)
  113. block-name)
  114. (setq val (read (current-buffer))
  115. char (car val)
  116. block-name (cadr val)
  117. name nil)
  118. (while l
  119. (if (string-match (caar l) block-name)
  120. (setq name (cdar l) l nil)
  121. (setq l (cdr l))))
  122. (if (not name)
  123. ;; As this is a surrogate pair range, ignore it.
  124. (setq val nil)
  125. (setcar val (cons first char))
  126. (setcar (cdr val) name))))
  127. (when val
  128. (setcdr tail (list val))
  129. (setq tail (cdr tail))))
  130. (error nil)))
  131. (setq unidata-list (cdr table))))
  132. ;; Alist of this form:
  133. ;; (PROP INDEX GENERATOR FILENAME DOCSTRING DESCRIBER DEFAULT VAL-LIST)
  134. ;; PROP: character property
  135. ;; INDEX: index to each element of unidata-list for PROP.
  136. ;; It may be a function that generates an alist of character codes
  137. ;; vs. the corresponding property values.
  138. ;; GENERATOR: function to generate a char-table
  139. ;; FILENAME: filename to store the char-table
  140. ;; DOCSTRING: docstring for the property
  141. ;; DESCRIBER: function to call to get a description string of property value
  142. ;; DEFAULT: the default value of the property. It may have the form
  143. ;; (VAL0 (FROM1 TO1 VAL1) ...) which indicates that the default
  144. ;; value is VAL0 except for characters in the ranges specified by
  145. ;; FROMn and TOn (inclusive). The default value of characters
  146. ;; between FROMn and TOn is VALn.
  147. ;; VAL-LIST: list of specially ordered property values
  148. (defconst unidata-prop-alist
  149. '((name
  150. 1 unidata-gen-table-name "uni-name.el"
  151. "Unicode character name.
  152. Property value is a string or nil.
  153. The value nil stands for the default value \"null string\")."
  154. nil
  155. nil)
  156. (general-category
  157. 2 unidata-gen-table-symbol "uni-category.el"
  158. "Unicode general category.
  159. Property value is one of the following symbols:
  160. Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
  161. Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
  162. unidata-describe-general-category
  163. Cn
  164. ;; The order of elements must be in sync with unicode_category_t
  165. ;; in src/character.h.
  166. (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
  167. Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn))
  168. (canonical-combining-class
  169. 3 unidata-gen-table-integer "uni-combining.el"
  170. "Unicode canonical combining class.
  171. Property value is an integer."
  172. unidata-describe-canonical-combining-class
  173. 0)
  174. (bidi-class
  175. 4 unidata-gen-table-symbol "uni-bidi.el"
  176. "Unicode bidi class.
  177. Property value is one of the following symbols:
  178. L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
  179. AN, CS, NSM, BN, B, S, WS, ON"
  180. unidata-describe-bidi-class
  181. ;; The assignment of default values to blocks of code points
  182. ;; follows the file DerivedBidiClass.txt from the Unicode
  183. ;; Character Database (UCD).
  184. (L (#x0600 #x06FF AL) (#xFB50 #xFDFF AL) (#xFE70 #xFEFF AL)
  185. (#x0590 #x05FF R) (#x07C0 #x08FF R)
  186. (#xFB1D #xFB4F R) (#x10800 #x10FFF R) (#x1E800 #x1EFFF R))
  187. ;; The order of elements must be in sync with bidi_type_t in
  188. ;; src/dispextern.h.
  189. (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON))
  190. (decomposition
  191. 5 unidata-gen-table-decomposition "uni-decomposition.el"
  192. "Unicode decomposition mapping.
  193. Property value is a list of characters. The first element may be
  194. one of these symbols representing compatibility formatting tag:
  195. font, noBreak, initial, medial, final, isolated, circle, super,
  196. sub, vertical, wide, narrow, small, square, fraction, compat"
  197. unidata-describe-decomposition)
  198. (decimal-digit-value
  199. 6 unidata-gen-table-integer "uni-decimal.el"
  200. "Unicode numeric value (decimal digit).
  201. Property value is an integer 0..9, or nil.
  202. The value nil stands for NaN \"Numeric_Value\".")
  203. (digit-value
  204. 7 unidata-gen-table-integer "uni-digit.el"
  205. "Unicode numeric value (digit).
  206. Property value is an integer 0..9, or nil.
  207. The value nil stands for NaN \"Numeric_Value\".")
  208. (numeric-value
  209. 8 unidata-gen-table-numeric "uni-numeric.el"
  210. "Unicode numeric value (numeric).
  211. Property value is an integer, a floating point, or nil.
  212. The value nil stands for NaN \"Numeric_Value\".")
  213. (mirrored
  214. 9 unidata-gen-table-symbol "uni-mirrored.el"
  215. "Unicode bidi mirrored flag.
  216. Property value is a symbol `Y' or `N'. See also the property `mirroring'."
  217. nil
  218. N)
  219. (old-name
  220. 10 unidata-gen-table-name "uni-old-name.el"
  221. "Unicode old names as published in Unicode 1.0.
  222. Property value is a string or nil.
  223. The value nil stands for the default value \"null string\").")
  224. (iso-10646-comment
  225. 11 unidata-gen-table-name "uni-comment.el"
  226. "Unicode ISO 10646 comment.
  227. Property value is a string.")
  228. (uppercase
  229. 12 unidata-gen-table-character "uni-uppercase.el"
  230. "Unicode simple uppercase mapping.
  231. Property value is a character or nil.
  232. The value nil means that the actual property value of a character
  233. is the character itself."
  234. string)
  235. (lowercase
  236. 13 unidata-gen-table-character "uni-lowercase.el"
  237. "Unicode simple lowercase mapping.
  238. Property value is a character or nil.
  239. The value nil means that the actual property value of a character
  240. is the character itself."
  241. string)
  242. (titlecase
  243. 14 unidata-gen-table-character "uni-titlecase.el"
  244. "Unicode simple titlecase mapping.
  245. Property value is a character or nil.
  246. The value nil means that the actual property value of a character
  247. is the character itself."
  248. string)
  249. (mirroring
  250. unidata-gen-mirroring-list unidata-gen-table-character "uni-mirrored.el"
  251. "Unicode bidi-mirroring characters.
  252. Property value is a character that has the corresponding mirroring image or nil.
  253. The value nil means that the actual property value of a character
  254. is the character itself.")))
  255. ;; Functions to access the above data.
  256. (defsubst unidata-prop-index (prop) (nth 1 (assq prop unidata-prop-alist)))
  257. (defsubst unidata-prop-generator (prop) (nth 2 (assq prop unidata-prop-alist)))
  258. (defsubst unidata-prop-file (prop) (nth 3 (assq prop unidata-prop-alist)))
  259. (defsubst unidata-prop-docstring (prop) (nth 4 (assq prop unidata-prop-alist)))
  260. (defsubst unidata-prop-describer (prop) (nth 5 (assq prop unidata-prop-alist)))
  261. (defsubst unidata-prop-default (prop) (nth 6 (assq prop unidata-prop-alist)))
  262. (defsubst unidata-prop-val-list (prop) (nth 7 (assq prop unidata-prop-alist)))
  263. ;; SIMPLE TABLE
  264. ;;
  265. ;; If the type of character property value is character, and the
  266. ;; values of succeeding character codes are usually different, we use
  267. ;; a char-table described here to store such values.
  268. ;;
  269. ;; A char-table divides character code space (#x0..#x3FFFFF) into
  270. ;; #x8000 blocks (each block contains 128 characters).
  271. ;; If all characters of a block have no property, a char-table has the
  272. ;; symbol nil for that block. Otherwise a char-table has a string of
  273. ;; the following format for it.
  274. ;;
  275. ;; The first character of the string is ?\001.
  276. ;; The second character of the string is FIRST-INDEX.
  277. ;; The Nth (N > 1) character of the string is a property value of the
  278. ;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
  279. ;; the first character of the block.
  280. ;;
  281. ;; This kind of char-table has these extra slots:
  282. ;; 1st: the property symbol
  283. ;; 2nd: nil
  284. ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
  285. ;; 4th to 5th: nil
  286. (defun unidata-gen-table-character (prop &rest ignore)
  287. (let ((table (make-char-table 'char-code-property-table))
  288. (prop-idx (unidata-prop-index prop))
  289. (vec (make-vector 128 0))
  290. (tail unidata-list)
  291. elt range val idx slot)
  292. (if (functionp prop-idx)
  293. (setq tail (funcall prop-idx)
  294. prop-idx 1))
  295. (while tail
  296. (setq elt (car tail) tail (cdr tail))
  297. (setq range (car elt)
  298. val (nth prop-idx elt))
  299. (if (= (length val) 0)
  300. (setq val nil)
  301. (setq val (string-to-number val 16)))
  302. (if (consp range)
  303. (if val
  304. (set-char-table-range table range val))
  305. (let* ((start (lsh (lsh range -7) 7))
  306. (limit (+ start 127))
  307. first-index last-index)
  308. (fillarray vec 0)
  309. (if val
  310. (aset vec (setq last-index (setq first-index (- range start)))
  311. val))
  312. (while (and (setq elt (car tail) range (car elt))
  313. (integerp range)
  314. (<= range limit))
  315. (setq val (nth prop-idx elt))
  316. (when (> (length val) 0)
  317. (aset vec (setq last-index (- range start))
  318. (string-to-number val 16))
  319. (or first-index
  320. (setq first-index last-index)))
  321. (setq tail (cdr tail)))
  322. (when first-index
  323. (let ((str (string 1 first-index))
  324. c)
  325. (while (<= first-index last-index)
  326. (setq str (format "%s%c" str (or (aref vec first-index) 0))
  327. first-index (1+ first-index)))
  328. (set-char-table-range table (cons start limit) str))))))
  329. (set-char-table-extra-slot table 0 prop)
  330. (set-char-table-extra-slot table 2 0)
  331. table))
  332. ;; RUN-LENGTH TABLE
  333. ;;
  334. ;; If many characters of successive character codes have the same
  335. ;; property value, we use a char-table described here to store the
  336. ;; values.
  337. ;;
  338. ;; At first, instead of a value itself, we store an index number to
  339. ;; the VAL-TABLE (5th extra slot) in the table. We call that index
  340. ;; number as VAL-CODE here after.
  341. ;;
  342. ;; A char-table divides character code space (#x0..#x3FFFFF) into
  343. ;; #x8000 blocks (each block contains 128 characters).
  344. ;;
  345. ;; If all characters of a block have the same value, a char-table has
  346. ;; VAL-CODE for that block. Otherwise a char-table has a string of
  347. ;; the following format for that block.
  348. ;;
  349. ;; The first character of the string is ?\002.
  350. ;; The following characters has this form:
  351. ;; ( VAL-CODE RUN-LENGTH ? ) +
  352. ;; where:
  353. ;; VAL-CODE (0..127): index into VAL-TABLE.
  354. ;; RUN-LENGTH (130..255):
  355. ;; (RUN-LENGTH - 128) specifies how many characters have the same
  356. ;; value. If omitted, it means 1.
  357. ;;
  358. ;; This kind of char-table has these extra slots:
  359. ;; 1st: the property symbol
  360. ;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
  361. ;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
  362. ;; 4th: function or nil
  363. ;; 5th: VAL-TABLE
  364. ;; Encode the character property value VAL into an integer value by
  365. ;; VAL-LIST. By side effect, VAL-LIST is modified.
  366. ;; VAL-LIST has this form:
  367. ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
  368. ;; If VAL is one of VALn, just return n.
  369. ;; Otherwise, VAL-LIST is modified to this:
  370. ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
  371. (defun unidata-encode-val (val-list val)
  372. (let ((slot (assoc val val-list))
  373. val-code)
  374. (if slot
  375. (cdr slot)
  376. (setq val-code (length val-list))
  377. (nconc val-list (list (cons val val-code)))
  378. val-code)))
  379. ;; Generate a char-table for the character property PROP.
  380. (defun unidata-gen-table (prop val-func default-value val-list)
  381. (let ((table (make-char-table 'char-code-property-table))
  382. (prop-idx (unidata-prop-index prop))
  383. (vec (make-vector 128 0))
  384. tail elt range val val-code idx slot
  385. prev-range-data)
  386. (setq val-list (cons nil (copy-sequence val-list)))
  387. (setq tail val-list val-code 0)
  388. ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
  389. (while tail
  390. (setcar tail (cons (car tail) val-code))
  391. (setq tail (cdr tail) val-code (1+ val-code)))
  392. (if (consp default-value)
  393. (setq default-value (copy-sequence default-value))
  394. (setq default-value (list default-value)))
  395. (setcar default-value
  396. (unidata-encode-val val-list (car default-value)))
  397. (set-char-table-range table t (car default-value))
  398. (set-char-table-range table nil (car default-value))
  399. (dolist (elm (cdr default-value))
  400. (setcar (nthcdr 2 elm)
  401. (unidata-encode-val val-list (nth 2 elm)))
  402. (set-char-table-range table (cons (car elm) (nth 1 elm)) (nth 2 elm)))
  403. (setq tail unidata-list)
  404. (while tail
  405. (setq elt (car tail) tail (cdr tail))
  406. (setq range (car elt)
  407. val (funcall val-func (nth prop-idx elt)))
  408. (setq val-code (if val (unidata-encode-val val-list val)))
  409. (if (consp range)
  410. (when val-code
  411. (set-char-table-range table range val-code)
  412. (let ((from (car range)) (to (cdr range)))
  413. ;; If RANGE doesn't end at the char-table boundary (each
  414. ;; 128 characters), we may have to carry over the data
  415. ;; for the last several characters (at most 127 chars)
  416. ;; to the next loop. In that case, set PREV-RANGE-DATA
  417. ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
  418. ;; specifies the range of characters handled in the next
  419. ;; loop.
  420. (when (< (logand to #x7F) #x7F)
  421. (if (< from (logand to #x1FFF80))
  422. (setq from (logand to #x1FFF80)))
  423. (setq prev-range-data (cons (cons from to) val-code)))))
  424. (let* ((start (lsh (lsh range -7) 7))
  425. (limit (+ start 127))
  426. str count new-val from to vcode)
  427. (fillarray vec (car default-value))
  428. (dolist (elm (cdr default-value))
  429. (setq from (car elm) to (nth 1 elm))
  430. (when (and (<= from limit)
  431. (or (>= from start) (>= to start)))
  432. (setq from (max from start)
  433. to (min to limit)
  434. vcode (nth 2 elm))
  435. (while (<= from to)
  436. (aset vec (- from start) vcode)
  437. (setq from (1+ from)))))
  438. ;; See the comment above.
  439. (when (and prev-range-data
  440. (>= (cdr (car prev-range-data)) start))
  441. (setq from (car (car prev-range-data))
  442. to (cdr (car prev-range-data))
  443. vcode (cdr prev-range-data))
  444. (while (<= from to)
  445. (aset vec (- from start) vcode)
  446. (setq from (1+ from))))
  447. (setq prev-range-data nil)
  448. (if val-code
  449. (aset vec (- range start) val-code))
  450. (while (and (setq elt (car tail) range (car elt))
  451. (integerp range)
  452. (<= range limit))
  453. (setq new-val (funcall val-func (nth prop-idx elt)))
  454. (if (not (eq val new-val))
  455. (setq val new-val
  456. val-code (if val (unidata-encode-val val-list val))))
  457. (if val-code
  458. (aset vec (- range start) val-code))
  459. (setq tail (cdr tail)))
  460. (setq str "\002" val-code -1 count 0)
  461. (mapc #'(lambda (x)
  462. (if (= val-code x)
  463. (setq count (1+ count))
  464. (if (> count 2)
  465. (setq str (concat str (string val-code
  466. (+ count 128))))
  467. (if (= count 2)
  468. (setq str (concat str (string val-code val-code)))
  469. (if (= count 1)
  470. (setq str (concat str (string val-code))))))
  471. (setq val-code x count 1)))
  472. vec)
  473. (if (= count 128)
  474. (if val
  475. (set-char-table-range table (cons start limit) val-code))
  476. (if (= val-code 0)
  477. (set-char-table-range table (cons start limit) str)
  478. (if (> count 2)
  479. (setq str (concat str (string val-code (+ count 128))))
  480. (if (= count 2)
  481. (setq str (concat str (string val-code val-code)))
  482. (setq str (concat str (string val-code)))))
  483. (set-char-table-range table (cons start limit) str))))))
  484. (set-char-table-extra-slot table 0 prop)
  485. (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
  486. table))
  487. (defun unidata-gen-table-symbol (prop default-value val-list)
  488. (let ((table (unidata-gen-table prop
  489. #'(lambda (x) (and (> (length x) 0)
  490. (intern x)))
  491. default-value val-list)))
  492. (set-char-table-extra-slot table 1 0)
  493. (set-char-table-extra-slot table 2 1)
  494. table))
  495. (defun unidata-gen-table-integer (prop default-value val-list)
  496. (let ((table (unidata-gen-table prop
  497. #'(lambda (x) (and (> (length x) 0)
  498. (string-to-number x)))
  499. default-value val-list)))
  500. (set-char-table-extra-slot table 1 0)
  501. (set-char-table-extra-slot table 2 1)
  502. table))
  503. (defun unidata-gen-table-numeric (prop default-value val-list)
  504. (let ((table (unidata-gen-table prop
  505. #'(lambda (x)
  506. (if (string-match "/" x)
  507. (/ (float (string-to-number x))
  508. (string-to-number
  509. (substring x (match-end 0))))
  510. (if (> (length x) 0)
  511. (string-to-number x))))
  512. default-value val-list)))
  513. (set-char-table-extra-slot table 1 0)
  514. (set-char-table-extra-slot table 2 2)
  515. table))
  516. ;; WORD-LIST TABLE
  517. ;; If the table is for `name' property, each character in the string
  518. ;; is one of these:
  519. ;; DIFF-HEAD-CODE (0, 1, or 2):
  520. ;; specifies how to decode the following characters.
  521. ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
  522. ;; specifies an index number into WORD-TABLE (see below)
  523. ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
  524. ;; specifies a literal word.
  525. ;;
  526. ;; The 4th slots is a vector:
  527. ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
  528. ;; WORD-TABLE is a vector of word symbols.
  529. ;; BLOCK-NAME is a vector of name symbols for a block of characters.
  530. ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
  531. ;; Return the difference of symbol list L1 and L2 in this form:
  532. ;; (DIFF-HEAD SYM1 SYM2 ...)
  533. ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
  534. ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
  535. ;; returns ((+ (* 1 16) 2) g h).
  536. ;; It means that we can get L2 from L1 by prepending the first element
  537. ;; of L1 and appending the last 2 elements of L1 to the list (g h).
  538. ;; If L1 and L2 don't have common elements at the head and tail,
  539. ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
  540. (defun unidata-word-list-diff (l1 l2)
  541. (let ((beg 0)
  542. (end 0)
  543. (len1 (length l1))
  544. (len2 (length l2))
  545. result)
  546. (when (< len1 16)
  547. (while (and l1 (eq (car l1) (car l2)))
  548. (setq beg (1+ beg)
  549. l1 (cdr l1) len1 (1- len1) l2 (cdr l2) len2 (1- len2)))
  550. (while (and (< end len1) (< end len2)
  551. (eq (nth (- len1 end 1) l1) (nth (- len2 end 1) l2)))
  552. (setq end (1+ end))))
  553. (if (= (+ beg end) 0)
  554. (setq result (list -1))
  555. (setq result (list (+ (* beg 16) (+ beg (- len1 end))))))
  556. (while (< end len2)
  557. (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
  558. (setq end (1+ end)))
  559. result))
  560. ;; Return a compressed form of the vector VEC. Each element of VEC is
  561. ;; a list of symbols of which names can be concatenated to form a
  562. ;; character name. This function changes those elements into
  563. ;; compressed forms by utilizing the fact that diff of consecutive
  564. ;; elements is usually small.
  565. (defun unidata-word-list-compress (vec)
  566. (let (last-elt last-idx diff-head tail elt val)
  567. (dotimes (i 128)
  568. (setq elt (aref vec i))
  569. (when elt
  570. (if (null last-elt)
  571. (setq diff-head -1
  572. val (cons 0 elt))
  573. (setq val (unidata-word-list-diff last-elt elt))
  574. (if (= (car val) -1)
  575. (setq diff-head -1
  576. val (cons 0 (cdr val)))
  577. (if (eq diff-head (car val))
  578. (setq val (cons 2 (cdr val)))
  579. (setq diff-head (car val))
  580. (if (>= diff-head 0)
  581. (setq val (cons 1 val))))))
  582. (aset vec i val)
  583. (setq last-idx i last-elt elt)))
  584. (if (not last-idx)
  585. (setq vec nil)
  586. (if (< last-idx 127)
  587. (let ((shorter (make-vector (1+ last-idx) nil)))
  588. (dotimes (i (1+ last-idx))
  589. (aset shorter i (aref vec i)))
  590. (setq vec shorter))))
  591. vec))
  592. ;; Encode the word index IDX into a characters code that can be
  593. ;; embedded in a string.
  594. (defsubst unidata-encode-word (idx)
  595. ;; Exclude 0, 1, 2.
  596. (+ idx 3))
  597. ;; Decode the character code CODE (that is embedded in a string) into
  598. ;; the corresponding word name by looking up WORD-TABLE.
  599. (defsubst unidata-decode-word (code word-table)
  600. (setq code (- code 3))
  601. (if (< code (length word-table))
  602. (aref word-table code)))
  603. ;; Table of short transliterated name symbols of Hangul Jamo divided
  604. ;; into Choseong, Jungseong, and Jongseong.
  605. (defconst unidata-name-jamo-name-table
  606. [[G GG N D DD R M B BB S SS nil J JJ C K T P H]
  607. [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I]
  608. [G GG GS N NJ NH D L LG LM LB LS LT LP LH M B BS S SS NG J C K T P H]])
  609. ;; Return a name of CHAR. VAL is the current value of (aref TABLE
  610. ;; CHAR).
  611. (defun unidata-get-name (char val table)
  612. (cond
  613. ((stringp val)
  614. (if (> (aref val 0) 0)
  615. val
  616. (let* ((first-char (lsh (lsh char -7) 7))
  617. (word-table (aref (char-table-extra-slot table 4) 0))
  618. (i 1)
  619. (len (length val))
  620. (vec (make-vector 128 nil))
  621. (idx 0)
  622. (case-fold-search nil)
  623. c word-list tail-list last-list word diff-head)
  624. (while (< i len)
  625. (setq c (aref val i))
  626. (if (< c 3)
  627. (progn
  628. (if (or word-list tail-list)
  629. (aset vec idx
  630. (setq last-list (nconc word-list tail-list))))
  631. (setq i (1+ i) idx (1+ idx)
  632. word-list nil tail-list nil)
  633. (if (> c 0)
  634. (let ((l last-list))
  635. (if (= c 1)
  636. (setq diff-head
  637. (prog1 (aref val i) (setq i (1+ i)))))
  638. (setq tail-list (nthcdr (% diff-head 16) last-list))
  639. (dotimes (i (/ diff-head 16))
  640. (setq word-list (nconc word-list (list (car l)))
  641. l (cdr l))))))
  642. (setq word-list
  643. (nconc word-list
  644. (list (symbol-name
  645. (unidata-decode-word c word-table))))
  646. i (1+ i))))
  647. (if (or word-list tail-list)
  648. (aset vec idx (nconc word-list tail-list)))
  649. (setq val nil)
  650. (dotimes (i 128)
  651. (setq c (+ first-char i))
  652. (let ((name (aref vec i)))
  653. (if name
  654. (let ((tail (cdr (setq name (copy-sequence name))))
  655. elt)
  656. (while tail
  657. (setq elt (car tail))
  658. (or (string= elt "-")
  659. (progn
  660. (setcdr tail (cons elt (cdr tail)))
  661. (setcar tail " ")))
  662. (setq tail (cddr tail)))
  663. (setq name (apply 'concat name))))
  664. (aset table c name)
  665. (if (= c char)
  666. (setq val name))))
  667. val)))
  668. ((and (integerp val) (> val 0))
  669. (let* ((symbol-table (aref (char-table-extra-slot table 4) 1))
  670. (sym (aref symbol-table (1- val))))
  671. (cond ((eq sym 'HANGUL\ SYLLABLE)
  672. (let ((jamo-name-table (aref (char-table-extra-slot table 4) 2)))
  673. ;; SIndex = S - SBase
  674. (setq char (- char #xAC00))
  675. (let ( ;; LIndex = SIndex / NCount
  676. (L (/ char 588))
  677. ;; VIndex = (SIndex % NCount) * TCount
  678. (V (/ (% char 588) 28))
  679. ;; TIndex = SIndex % TCount
  680. (T (% char 28)))
  681. (format "HANGUL SYLLABLE %s%s%s"
  682. ;; U+110B is nil in this table.
  683. (or (aref (aref jamo-name-table 0) L) "")
  684. (aref (aref jamo-name-table 1) V)
  685. (if (= T 0) ""
  686. (aref (aref jamo-name-table 2) (1- T)))))))
  687. ((eq sym 'CJK\ IDEOGRAPH)
  688. (format "%s-%04X" sym char))
  689. ((eq sym 'CJK\ COMPATIBILITY\ IDEOGRAPH)
  690. (format "%s-%04X" sym char))
  691. ((eq sym 'VARIATION\ SELECTOR)
  692. (format "%s-%d" sym (+ (- char #xe0100) 17))))))))
  693. ;; Store VAL as the name of CHAR in TABLE.
  694. (defun unidata-put-name (char val table)
  695. (let ((current-val (aref table char)))
  696. (if (and (stringp current-val) (= (aref current-val 0) 0))
  697. (funcall (char-table-extra-slot table 1) char current-val table))
  698. (aset table char val)))
  699. (defun unidata-get-decomposition (char val table)
  700. (cond
  701. ((not val)
  702. (list char))
  703. ((consp val)
  704. val)
  705. ((stringp val)
  706. (if (> (aref val 0) 0)
  707. val
  708. (let* ((first-char (lsh (lsh char -7) 7))
  709. (word-table (char-table-extra-slot table 4))
  710. (i 1)
  711. (len (length val))
  712. (vec (make-vector 128 nil))
  713. (idx 0)
  714. (case-fold-search nil)
  715. c word-list tail-list last-list word diff-head)
  716. (while (< i len)
  717. (setq c (aref val i))
  718. (if (< c 3)
  719. (progn
  720. (if (or word-list tail-list)
  721. (aset vec idx
  722. (setq last-list (nconc word-list tail-list))))
  723. (setq i (1+ i) idx (1+ idx)
  724. word-list nil tail-list nil)
  725. (if (> c 0)
  726. (let ((l last-list))
  727. (if (= c 1)
  728. (setq diff-head
  729. (prog1 (aref val i) (setq i (1+ i)))))
  730. (setq tail-list (nthcdr (% diff-head 16) last-list))
  731. (dotimes (i (/ diff-head 16))
  732. (setq word-list (nconc word-list (list (car l)))
  733. l (cdr l))))))
  734. (setq word-list
  735. (nconc word-list
  736. (list (or (unidata-decode-word c word-table) c)))
  737. i (1+ i))))
  738. (if (or word-list tail-list)
  739. (aset vec idx (nconc word-list tail-list)))
  740. (dotimes (i 128)
  741. (aset table (+ first-char i) (aref vec i)))
  742. (setq val (aref vec (- char first-char)))
  743. (or val (list char)))))
  744. ;; Hangul syllable
  745. ((and (eq val 0) (>= char #xAC00) (<= char #xD7A3))
  746. ;; SIndex = S (char) - SBase (#xAC00)
  747. (setq char (- char #xAC00))
  748. (let (;; L = LBase + SIndex / NCount
  749. (L (+ #x1100 (/ char 588)))
  750. ;; V = VBase + (SIndex % NCount) * TCount
  751. (V (+ #x1161 (/ (% char 588) 28)))
  752. ;; LV = SBase + (SIndex / TCount) * TCount
  753. (LV (+ #xAC00 (* (/ char 28) 28)))
  754. ;; T = TBase + SIndex % TCount
  755. (T (+ #x11A7 (% char 28))))
  756. (if (= T #x11A7)
  757. (list L V)
  758. (list LV T))))
  759. ))
  760. ;; Store VAL as the decomposition information of CHAR in TABLE.
  761. (defun unidata-put-decomposition (char val table)
  762. (let ((current-val (aref table char)))
  763. (if (and (stringp current-val) (= (aref current-val 0) 0))
  764. (funcall (char-table-extra-slot table 1) char current-val table))
  765. (aset table char val)))
  766. ;; UnicodeData.txt contains these lines:
  767. ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
  768. ;; ...
  769. ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  770. ;; ...
  771. ;; The following command yields a file of about 96K bytes.
  772. ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
  773. ;; With the following function, we can get a file of almost the same
  774. ;; the size.
  775. ;; Generate a char-table for character names.
  776. (defun unidata-gen-table-word-list (prop val-func)
  777. (let ((table (make-char-table 'char-code-property-table))
  778. (prop-idx (unidata-prop-index prop))
  779. (word-list (list nil))
  780. word-table
  781. block-list block-word-table block-end
  782. tail elt range val idx slot)
  783. (setq tail unidata-list)
  784. (setq block-end -1)
  785. (while tail
  786. (setq elt (car tail) tail (cdr tail))
  787. (setq range (car elt)
  788. val (funcall val-func (nth prop-idx elt)))
  789. ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
  790. ;; "VARIATION SELECTOR-XXX" as a block.
  791. (if (and (consp val) (eq prop 'name)
  792. (or (and (eq (car val) 'CJK)
  793. (eq (nth 1 val) 'COMPATIBILITY))
  794. (and (>= range #xe0100)
  795. (eq (car val) 'VARIATION)
  796. (eq (nth 1 val) 'SELECTOR))))
  797. (let ((first (car val))
  798. (second (nth 1 val))
  799. (start range))
  800. (while (and (setq elt (car tail) range (car elt)
  801. val (funcall val-func (nth prop-idx elt)))
  802. (consp val)
  803. (eq first (car val))
  804. (eq second (nth 1 val)))
  805. (setq block-end range
  806. tail (cdr tail)))
  807. (setq range (cons start block-end)
  808. val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
  809. 'VARIATION\ SELECTOR))))
  810. (if (consp range)
  811. (if val
  812. (let ((slot (assq val block-list)))
  813. (setq range (cons (car range) (cdr range)))
  814. (setq block-end (cdr range))
  815. (if slot
  816. (nconc slot (list range))
  817. (push (list val range) block-list))))
  818. (let* ((start (lsh (lsh range -7) 7))
  819. (limit (+ start 127))
  820. (first tail)
  821. (vec (make-vector 128 nil))
  822. c name len)
  823. (if (<= start block-end)
  824. ;; START overlap with the previous block.
  825. (aset table range (nth prop-idx elt))
  826. (if val
  827. (aset vec (- range start) val))
  828. (while (and (setq elt (car tail) range (car elt))
  829. (integerp range)
  830. (<= range limit))
  831. (setq val (funcall val-func (nth prop-idx elt)))
  832. (if val
  833. (aset vec (- range start) val))
  834. (setq tail (cdr tail)))
  835. (setq vec (unidata-word-list-compress vec))
  836. (when vec
  837. (dotimes (i (length vec))
  838. (dolist (elt (aref vec i))
  839. (if (symbolp elt)
  840. (let ((slot (assq elt word-list)))
  841. (if slot
  842. (setcdr slot (1+ (cdr slot)))
  843. (setcdr word-list
  844. (cons (cons elt 1) (cdr word-list))))))))
  845. (set-char-table-range table (cons start limit) vec))))))
  846. (setq word-list (sort (cdr word-list)
  847. #'(lambda (x y) (> (cdr x) (cdr y)))))
  848. (setq tail word-list idx 0)
  849. (while tail
  850. (setcdr (car tail) (unidata-encode-word idx))
  851. (setq idx (1+ idx) tail (cdr tail)))
  852. (setq word-table (make-vector (length word-list) nil))
  853. (setq idx 0)
  854. (dolist (elt word-list)
  855. (aset word-table idx (car elt))
  856. (setq idx (1+ idx)))
  857. (if (and (eq prop 'decomposition)
  858. (> idx 32))
  859. (error "Too many symbols in decomposition data"))
  860. (dotimes (i (/ #x110000 128))
  861. (let* ((idx (* i 128))
  862. (vec (aref table idx)))
  863. (when (vectorp vec)
  864. (dotimes (i (length vec))
  865. (let ((tail (aref vec i))
  866. elt code)
  867. (if (not tail)
  868. (aset vec i "\0")
  869. (while tail
  870. (setq elt (car tail)
  871. code (if (integerp elt) elt
  872. (cdr (assq elt word-list))))
  873. (setcar tail (string code))
  874. (setq tail (cdr tail)))
  875. (aset vec i (mapconcat 'identity (aref vec i) "")))))
  876. (set-char-table-range
  877. table (cons idx (+ idx 127))
  878. (mapconcat 'identity vec "")))))
  879. (setq block-word-table (make-vector (length block-list) nil))
  880. (setq idx 0)
  881. (dolist (elt block-list)
  882. (dolist (e (cdr elt))
  883. (set-char-table-range table e (1+ idx)))
  884. (aset block-word-table idx (car elt))
  885. (setq idx (1+ idx)))
  886. (set-char-table-extra-slot table 0 prop)
  887. (set-char-table-extra-slot table 4 (cons word-table block-word-table))
  888. table))
  889. (defun unidata-split-name (str)
  890. (if (symbolp str)
  891. str
  892. (let ((len (length str))
  893. (l nil)
  894. (idx 0)
  895. c)
  896. (if (= len 0)
  897. nil
  898. (dotimes (i len)
  899. (setq c (aref str i))
  900. (if (= c 32)
  901. (setq l (cons (intern (substring str idx i)) l)
  902. idx (1+ i))
  903. (if (and (= c ?-) (< idx i)
  904. (< (1+ i) len) (/= (aref str (1+ i)) 32))
  905. (setq l (cons '- (cons (intern (substring str idx i)) l))
  906. idx (1+ i)))))
  907. (nreverse (cons (intern (substring str idx)) l))))))
  908. (defun unidata-gen-table-name (prop &rest ignore)
  909. (let* ((table (unidata-gen-table-word-list prop 'unidata-split-name))
  910. (word-tables (char-table-extra-slot table 4)))
  911. (byte-compile 'unidata-get-name)
  912. (byte-compile 'unidata-put-name)
  913. (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-name))
  914. (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-name))
  915. (if (eq prop 'name)
  916. (set-char-table-extra-slot table 4
  917. (vector (car word-tables)
  918. (cdr word-tables)
  919. unidata-name-jamo-name-table))
  920. (set-char-table-extra-slot table 4
  921. (vector (car word-tables))))
  922. table))
  923. (defun unidata-split-decomposition (str)
  924. (if (symbolp str)
  925. str
  926. (let ((len (length str))
  927. (l nil)
  928. (idx 0)
  929. c)
  930. (if (= len 0)
  931. nil
  932. (dotimes (i len)
  933. (setq c (aref str i))
  934. (if (= c 32)
  935. (setq l (if (= (aref str idx) ?<)
  936. (cons (intern (substring str (1+ idx) (1- i))) l)
  937. (cons (string-to-number (substring str idx i) 16) l))
  938. idx (1+ i))))
  939. (if (= (aref str idx) ?<)
  940. (setq l (cons (intern (substring str (1+ idx) (1- len))) l))
  941. (setq l (cons (string-to-number (substring str idx len) 16) l)))
  942. (nreverse l)))))
  943. (defun unidata-gen-table-decomposition (prop &rest ignore)
  944. (let* ((table (unidata-gen-table-word-list prop 'unidata-split-decomposition))
  945. (word-tables (char-table-extra-slot table 4)))
  946. (byte-compile 'unidata-get-decomposition)
  947. (byte-compile 'unidata-put-decomposition)
  948. (set-char-table-extra-slot table 1
  949. (symbol-function 'unidata-get-decomposition))
  950. (set-char-table-extra-slot table 2
  951. (symbol-function 'unidata-put-decomposition))
  952. (set-char-table-extra-slot table 4 (car word-tables))
  953. table))
  954. (defun unidata-describe-general-category (val)
  955. (cdr (assq val
  956. '((nil . "Uknown")
  957. (Lu . "Letter, Uppercase")
  958. (Ll . "Letter, Lowercase")
  959. (Lt . "Letter, Titlecase")
  960. (Lm . "Letter, Modifier")
  961. (Lo . "Letter, Other")
  962. (Mn . "Mark, Nonspacing")
  963. (Mc . "Mark, Spacing Combining")
  964. (Me . "Mark, Enclosing")
  965. (Nd . "Number, Decimal Digit")
  966. (Nl . "Number, Letter")
  967. (No . "Number, Other")
  968. (Pc . "Punctuation, Connector")
  969. (Pd . "Punctuation, Dash")
  970. (Ps . "Punctuation, Open")
  971. (Pe . "Punctuation, Close")
  972. (Pi . "Punctuation, Initial quote")
  973. (Pf . "Punctuation, Final quote")
  974. (Po . "Punctuation, Other")
  975. (Sm . "Symbol, Math")
  976. (Sc . "Symbol, Currency")
  977. (Sk . "Symbol, Modifier")
  978. (So . "Symbol, Other")
  979. (Zs . "Separator, Space")
  980. (Zl . "Separator, Line")
  981. (Zp . "Separator, Paragraph")
  982. (Cc . "Other, Control")
  983. (Cf . "Other, Format")
  984. (Cs . "Other, Surrogate")
  985. (Co . "Other, Private Use")
  986. (Cn . "Other, Not Assigned")))))
  987. (defun unidata-describe-canonical-combining-class (val)
  988. (cdr (assq val
  989. '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
  990. (1 . "Overlays and interior")
  991. (7 . "Nuktas")
  992. (8 . "Hiragana/Katakana voicing marks")
  993. (9 . "Viramas")
  994. (10 . "Start of fixed position classes")
  995. (199 . "End of fixed position classes")
  996. (200 . "Below left attached")
  997. (202 . "Below attached")
  998. (204 . "Below right attached")
  999. (208 . "Left attached (reordrant around single base character)")
  1000. (210 . "Right attached")
  1001. (212 . "Above left attached")
  1002. (214 . "Above attached")
  1003. (216 . "Above right attached")
  1004. (218 . "Below left")
  1005. (220 . "Below")
  1006. (222 . "Below right")
  1007. (224 . "Left (reordrant around single base character)")
  1008. (226 . "Right")
  1009. (228 . "Above left")
  1010. (230 . "Above")
  1011. (232 . "Above right")
  1012. (233 . "Double below")
  1013. (234 . "Double above")
  1014. (240 . "Below (iota subscript)")))))
  1015. (defun unidata-describe-bidi-class (val)
  1016. (cdr (assq val
  1017. '((L . "Left-to-Right")
  1018. (LRE . "Left-to-Right Embedding")
  1019. (LRO . "Left-to-Right Override")
  1020. (R . "Right-to-Left")
  1021. (AL . "Right-to-Left Arabic")
  1022. (RLE . "Right-to-Left Embedding")
  1023. (RLO . "Right-to-Left Override")
  1024. (PDF . "Pop Directional Format")
  1025. (EN . "European Number")
  1026. (ES . "European Number Separator")
  1027. (ET . "European Number Terminator")
  1028. (AN . "Arabic Number")
  1029. (CS . "Common Number Separator")
  1030. (NSM . "Non-Spacing Mark")
  1031. (BN . "Boundary Neutral")
  1032. (B . "Paragraph Separator")
  1033. (S . "Segment Separator")
  1034. (WS . "Whitespace")
  1035. (ON . "Other Neutrals")))))
  1036. (defun unidata-describe-decomposition (val)
  1037. (mapconcat
  1038. #'(lambda (x)
  1039. (if (symbolp x) (symbol-name x)
  1040. (concat (string ?')
  1041. (compose-string (string x) 0 1 (string ?\t x ?\t))
  1042. (string ?'))))
  1043. val " "))
  1044. (defun unidata-gen-mirroring-list ()
  1045. (let ((head (list nil))
  1046. tail)
  1047. (with-temp-buffer
  1048. (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir))
  1049. (goto-char (point-min))
  1050. (setq tail head)
  1051. (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t)
  1052. (let ((char (string-to-number (match-string 1) 16))
  1053. (mirror (match-string 2)))
  1054. (setq tail (setcdr tail (list (list char mirror)))))))
  1055. (cdr head)))
  1056. ;; Verify if we can retrieve correct values from the generated
  1057. ;; char-tables.
  1058. (defun unidata-check ()
  1059. (dolist (elt unidata-prop-alist)
  1060. (let* ((prop (car elt))
  1061. (index (unidata-prop-index prop))
  1062. (generator (unidata-prop-generator prop))
  1063. (table (progn
  1064. (message "Generating %S table..." prop)
  1065. (funcall generator prop)))
  1066. (decoder (char-table-extra-slot table 1))
  1067. (check #x400))
  1068. (dolist (e unidata-list)
  1069. (let ((char (car e))
  1070. (val1 (nth index e))
  1071. val2)
  1072. (if (and (stringp val1) (= (length val1) 0))
  1073. (setq val1 nil))
  1074. (unless (consp char)
  1075. (setq val2 (funcall decoder char (aref table char) table))
  1076. (if val1
  1077. (cond ((eq generator 'unidata-gen-table-symbol)
  1078. (setq val1 (intern val1)))
  1079. ((eq generator 'unidata-gen-table-integer)
  1080. (setq val1 (string-to-number val1)))
  1081. ((eq generator 'unidata-gen-table-character)
  1082. (setq val1 (string-to-number val1 16)))
  1083. ((eq generator 'unidata-gen-table-decomposition)
  1084. (setq val1 (unidata-split-decomposition val1)))))
  1085. (when (>= char check)
  1086. (message "%S %04X" prop check)
  1087. (setq check (+ check #x400)))
  1088. (or (equal val1 val2)
  1089. (insert (format "> %04X %S\n< %04X %S\n"
  1090. char val1 char val2)))
  1091. (sit-for 0)))))))
  1092. ;; The entry function. It generates files described in the header
  1093. ;; comment of this file.
  1094. (defun unidata-gen-files (&optional data-dir unidata-text-file)
  1095. (or data-dir
  1096. (setq data-dir (car command-line-args-left)
  1097. command-line-args-left (cdr command-line-args-left)
  1098. unidata-text-file (car command-line-args-left)
  1099. command-line-args-left (cdr command-line-args-left)))
  1100. (let ((coding-system-for-write 'utf-8-unix)
  1101. (charprop-file "charprop.el")
  1102. (unidata-dir data-dir))
  1103. (dolist (elt unidata-prop-alist)
  1104. (let* ((prop (car elt))
  1105. (file (unidata-prop-file prop)))
  1106. (if (file-exists-p file)
  1107. (delete-file file))))
  1108. (unidata-setup-list unidata-text-file)
  1109. (with-temp-file charprop-file
  1110. (insert ";; Automatically generated by unidata-gen.el.\n")
  1111. (dolist (elt unidata-prop-alist)
  1112. (let* ((prop (car elt))
  1113. (generator (unidata-prop-generator prop))
  1114. (file (unidata-prop-file prop))
  1115. (docstring (unidata-prop-docstring prop))
  1116. (describer (unidata-prop-describer prop))
  1117. (default-value (unidata-prop-default prop))
  1118. (val-list (unidata-prop-val-list prop))
  1119. table)
  1120. ;; Filename in this comment line is extracted by sed in
  1121. ;; Makefile.
  1122. (insert (format ";; FILE: %s\n" file))
  1123. (insert (format "(define-char-code-property '%S %S\n %S)\n"
  1124. prop file docstring))
  1125. (with-temp-buffer
  1126. (message "Generating %s..." file)
  1127. (when (file-exists-p file)
  1128. (insert-file-contents file)
  1129. (goto-char (point-max))
  1130. (search-backward ";; Local Variables:"))
  1131. (setq table (funcall generator prop default-value val-list))
  1132. (when describer
  1133. (unless (subrp (symbol-function describer))
  1134. (byte-compile describer)
  1135. (setq describer (symbol-function describer)))
  1136. (set-char-table-extra-slot table 3 describer))
  1137. (if (bobp)
  1138. (insert ";; Copyright (C) 1991-2009 Unicode, Inc.
  1139. ;; This file was generated from the Unicode data files at
  1140. ;; http://www.unicode.org/Public/UNIDATA/.
  1141. ;; See lisp/international/README for the copyright and permission notice.\n"))
  1142. (insert (format "(define-char-code-property '%S %S %S)\n"
  1143. prop table docstring))
  1144. (if (eobp)
  1145. (insert ";; Local Variables:\n"
  1146. ";; coding: utf-8\n"
  1147. ";; no-byte-compile: t\n"
  1148. ";; End:\n\n"
  1149. (format ";; %s ends here\n" file)))
  1150. (write-file file)
  1151. (message "Generating %s...done" file))))
  1152. (message "Writing %s..." charprop-file)
  1153. (insert ";; Local Variables:\n"
  1154. ";; coding: utf-8\n"
  1155. ";; no-byte-compile: t\n"
  1156. ";; End:\n\n"
  1157. (format ";; %s ends here\n" charprop-file)))))
  1158. ;;; unidata-gen.el ends here