mule.el 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. ;;; mule.el --- basic commands for multilingual environment
  2. ;; Copyright (C) 1997-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: mule, multilingual, character set, coding system
  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. ;;; Code:
  24. (defconst mule-version "6.0 (HANACHIRUSATO)" "\
  25. Version number and name of this version of MULE (multilingual environment).")
  26. (defconst mule-version-date "2003.9.1" "\
  27. Distribution date of this version of MULE (multilingual environment).")
  28. ;;; CHARSET
  29. ;; Backward compatibility code for handling emacs-mule charsets.
  30. (defvar private-char-area-1-min #xF0000)
  31. (defvar private-char-area-1-max #xFFFFE)
  32. (defvar private-char-area-2-min #x100000)
  33. (defvar private-char-area-2-max #x10FFFE)
  34. ;; Table of emacs-mule charsets indexed by their emacs-mule ID.
  35. (defvar emacs-mule-charset-table (make-vector 256 nil))
  36. (aset emacs-mule-charset-table 0 'ascii)
  37. ;; Convert the argument of old-style call of define-charset to a
  38. ;; property list used by the new-style.
  39. ;; INFO-VECTOR is a vector of the format:
  40. ;; [DIMENSION CHARS WIDTH DIRECTION ISO-FINAL-CHAR ISO-GRAPHIC-PLANE
  41. ;; SHORT-NAME LONG-NAME DESCRIPTION]
  42. (defun convert-define-charset-argument (emacs-mule-id info-vector)
  43. (let* ((dim (aref info-vector 0))
  44. (chars (aref info-vector 1))
  45. (total (if (= dim 1) chars (* chars chars)))
  46. (code-space (if (= dim 1) (if (= chars 96) [32 127] [33 126])
  47. (if (= chars 96) [32 127 32 127] [33 126 33 126])))
  48. code-offset)
  49. (if (integerp emacs-mule-id)
  50. (or (= emacs-mule-id 0)
  51. (and (>= emacs-mule-id 129) (< emacs-mule-id 256))
  52. (error "Invalid CHARSET-ID: %d" emacs-mule-id))
  53. (let (from-id to-id)
  54. (if (= dim 1) (setq from-id 160 to-id 224)
  55. (setq from-id 224 to-id 255))
  56. (while (and (< from-id to-id)
  57. (not (aref emacs-mule-charset-table from-id)))
  58. (setq from-id (1+ from-id)))
  59. (if (= from-id to-id)
  60. (error "No more room for the new Emacs-mule charset"))
  61. (setq emacs-mule-id from-id)))
  62. (if (> (- private-char-area-1-max private-char-area-1-min) total)
  63. (setq code-offset private-char-area-1-min
  64. private-char-area-1-min (+ private-char-area-1-min total))
  65. (if (> (- private-char-area-2-max private-char-area-2-min) total)
  66. (setq code-offset private-char-area-2-min
  67. private-char-area-2-min (+ private-char-area-2-min total))
  68. (error "No more space for a new charset")))
  69. (list :dimension dim
  70. :code-space code-space
  71. :iso-final-char (aref info-vector 4)
  72. :code-offset code-offset
  73. :emacs-mule-id emacs-mule-id)))
  74. (defun define-charset (name docstring &rest props)
  75. "Define NAME (symbol) as a charset with DOCSTRING.
  76. The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE
  77. may be any symbol. The following have special meanings, and one of
  78. `:code-offset', `:map', `:subset', `:superset' must be specified.
  79. `:short-name'
  80. VALUE must be a short string to identify the charset. If omitted,
  81. NAME is used.
  82. `:long-name'
  83. VALUE must be a string longer than `:short-name' to identify the
  84. charset. If omitted, the value of the `:short-name' attribute is used.
  85. `:dimension'
  86. VALUE must be an integer 0, 1, 2, or 3, specifying the dimension of
  87. code-points of the charsets. If omitted, it is calculated from the
  88. value of the `:code-space' attribute.
  89. `:code-space'
  90. VALUE must be a vector of length at most 8 specifying the byte code
  91. range of each dimension in this format:
  92. [ MIN-1 MAX-1 MIN-2 MAX-2 ... ]
  93. where MIN-N is the minimum byte value of Nth dimension of code-point,
  94. MAX-N is the maximum byte value of that.
  95. `:min-code'
  96. VALUE must be an integer specifying the minimum code point of the
  97. charset. If omitted, it is calculated from `:code-space'. VALUE may
  98. be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
  99. the code point and LOW is the least significant 16 bits.
  100. `:max-code'
  101. VALUE must be an integer specifying the maximum code point of the
  102. charset. If omitted, it is calculated from `:code-space'. VALUE may
  103. be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
  104. the code point and LOW is the least significant 16 bits.
  105. `:iso-final-char'
  106. VALUE must be a character in the range 32 to 127 (inclusive)
  107. specifying the final char of the charset for ISO-2022 encoding. If
  108. omitted, the charset can't be encoded by ISO-2022 based
  109. coding-systems.
  110. `:iso-revision-number'
  111. VALUE must be an integer in the range 0..63, specifying the revision
  112. number of the charset for ISO-2022 encoding.
  113. `:emacs-mule-id'
  114. VALUE must be an integer of 0, 129..255. If omitted, the charset
  115. can't be encoded by coding-systems of type `emacs-mule'.
  116. `:ascii-compatible-p'
  117. VALUE must be nil or t (default nil). If VALUE is t, the charset is
  118. compatible with ASCII, i.e. the first 128 code points map to ASCII.
  119. `:supplementary-p'
  120. VALUE must be nil or t. If the VALUE is t, the charset is
  121. supplementary, which means it is used only as a parent or a
  122. subset of some other charset, or it is provided just for backward
  123. compatibility.
  124. `:invalid-code'
  125. VALUE must be a nonnegative integer that can be used as an invalid
  126. code point of the charset. If the minimum code is 0 and the maximum
  127. code is greater than Emacs's maximum integer value, `:invalid-code'
  128. should not be omitted.
  129. `:code-offset'
  130. VALUE must be an integer added to the index number of a character to
  131. get the corresponding character code.
  132. `:map'
  133. VALUE must be vector or string.
  134. If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
  135. where CODE-n is a code-point of the charset, and CHAR-n is the
  136. corresponding character code.
  137. If it is a string, it is a name of file that contains the above
  138. information. Each line of the file must be this format:
  139. 0xXXX 0xYYY
  140. where XXX is a hexadecimal representation of CODE-n and YYY is a
  141. hexadecimal representation of CHAR-n. A line starting with `#' is a
  142. comment line.
  143. `:subset'
  144. VALUE must be a list:
  145. ( PARENT MIN-CODE MAX-CODE OFFSET )
  146. PARENT is a parent charset. MIN-CODE and MAX-CODE specify the range
  147. of characters inherited from the parent. OFFSET is an integer value
  148. to add to a code point of the parent charset to get the corresponding
  149. code point of this charset.
  150. `:superset'
  151. VALUE must be a list of parent charsets. The charset inherits
  152. characters from them. Each element of the list may be a cons (PARENT
  153. . OFFSET), where PARENT is a parent charset, and OFFSET is an offset
  154. value to add to a code point of PARENT to get the corresponding code
  155. point of this charset.
  156. `:unify-map'
  157. VALUE must be vector or string.
  158. If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
  159. where CODE-n is a code-point of the charset, and CHAR-n is the
  160. corresponding Unicode character code.
  161. If it is a string, it is a name of file that contains the above
  162. information. The file format is the same as what described for `:map'
  163. attribute."
  164. (when (vectorp (car props))
  165. ;; Old style code:
  166. ;; (define-charset CHARSET-ID CHARSET-SYMBOL INFO-VECTOR)
  167. ;; Convert the argument to make it fit with the current style.
  168. (let ((vec (car props)))
  169. (setq props (convert-define-charset-argument name vec)
  170. name docstring
  171. docstring (aref vec 8))))
  172. (let ((attrs (mapcar 'list '(:dimension
  173. :code-space
  174. :min-code
  175. :max-code
  176. :iso-final-char
  177. :iso-revision-number
  178. :emacs-mule-id
  179. :ascii-compatible-p
  180. :supplementary-p
  181. :invalid-code
  182. :code-offset
  183. :map
  184. :subset
  185. :superset
  186. :unify-map
  187. :plist))))
  188. ;; If :dimension is omitted, get the dimension from :code-space.
  189. (let ((dimension (plist-get props :dimension)))
  190. (or dimension
  191. (let ((code-space (plist-get props :code-space)))
  192. (setq dimension (if code-space (/ (length code-space) 2) 4))
  193. (setq props (plist-put props :dimension dimension)))))
  194. (let ((code-space (plist-get props :code-space)))
  195. (or code-space
  196. (let ((dimension (plist-get props :dimension)))
  197. (setq code-space (make-vector 8 0))
  198. (dotimes (i dimension)
  199. (aset code-space (1+ (* i 2)) #xFF))
  200. (setq props (plist-put props :code-space code-space)))))
  201. ;; If :emacs-mule-id is specified, update emacs-mule-charset-table.
  202. (let ((emacs-mule-id (plist-get props :emacs-mule-id)))
  203. (if (integerp emacs-mule-id)
  204. (aset emacs-mule-charset-table emacs-mule-id name)))
  205. (dolist (slot attrs)
  206. (setcdr slot (purecopy (plist-get props (car slot)))))
  207. ;; Make sure that the value of :code-space is a vector of 8
  208. ;; elements.
  209. (let* ((slot (assq :code-space attrs))
  210. (val (cdr slot))
  211. (len (length val)))
  212. (if (< len 8)
  213. (setcdr slot
  214. (vconcat val (make-vector (- 8 len) 0)))))
  215. ;; Add :name and :docstring properties to PROPS.
  216. (setq props
  217. (cons :name (cons name (cons :docstring (cons (purecopy docstring) props)))))
  218. (or (plist-get props :short-name)
  219. (plist-put props :short-name (symbol-name name)))
  220. (or (plist-get props :long-name)
  221. (plist-put props :long-name (plist-get props :short-name)))
  222. (plist-put props :base name)
  223. ;; We can probably get a worthwhile amount in purespace.
  224. (setq props
  225. (mapcar (lambda (elt)
  226. (if (stringp elt)
  227. (purecopy elt)
  228. elt))
  229. props))
  230. (setcdr (assq :plist attrs) props)
  231. (apply 'define-charset-internal name (mapcar 'cdr attrs))))
  232. (defun load-with-code-conversion (fullname file &optional noerror nomessage)
  233. "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
  234. The file contents are decoded before evaluation if necessary.
  235. If optional third arg NOERROR is non-nil,
  236. report no error if FILE doesn't exist.
  237. Print messages at start and end of loading unless
  238. optional fourth arg NOMESSAGE is non-nil.
  239. Return t if file exists."
  240. (if (null (file-readable-p fullname))
  241. (and (null noerror)
  242. (signal 'file-error (list "Cannot open load file" file)))
  243. ;; Read file with code conversion, and then eval.
  244. (let* ((buffer
  245. ;; We can't use `generate-new-buffer' because files.el
  246. ;; is not yet loaded.
  247. (get-buffer-create (generate-new-buffer-name " *load*")))
  248. (load-in-progress t)
  249. (source (save-match-data (string-match "\\.el\\'" fullname))))
  250. (unless nomessage
  251. (if source
  252. (message "Loading %s (source)..." file)
  253. (message "Loading %s..." file)))
  254. (when purify-flag
  255. (push (purecopy file) preloaded-file-list))
  256. (unwind-protect
  257. (let ((load-file-name fullname)
  258. (set-auto-coding-for-load t)
  259. (inhibit-file-name-operation nil))
  260. (with-current-buffer buffer
  261. ;; So that we don't get completely screwed if the
  262. ;; file is encoded in some complicated character set,
  263. ;; read it with real decoding, as a multibyte buffer.
  264. (set-buffer-multibyte t)
  265. ;; Don't let deactivate-mark remain set.
  266. (let (deactivate-mark)
  267. (insert-file-contents fullname))
  268. ;; If the loaded file was inserted with no-conversion or
  269. ;; raw-text coding system, make the buffer unibyte.
  270. ;; Otherwise, eval-buffer might try to interpret random
  271. ;; binary junk as multibyte characters.
  272. (if (and enable-multibyte-characters
  273. (or (eq (coding-system-type last-coding-system-used)
  274. 'raw-text)))
  275. (set-buffer-multibyte nil))
  276. ;; Make `kill-buffer' quiet.
  277. (set-buffer-modified-p nil))
  278. ;; Have the original buffer current while we eval.
  279. (eval-buffer buffer nil
  280. ;; This is compatible with what `load' does.
  281. (if purify-flag file fullname)
  282. nil t))
  283. (let (kill-buffer-hook kill-buffer-query-functions)
  284. (kill-buffer buffer)))
  285. (do-after-load-evaluation fullname)
  286. (unless (or nomessage noninteractive)
  287. (if source
  288. (message "Loading %s (source)...done" file)
  289. (message "Loading %s...done" file)))
  290. t)))
  291. (defun charset-info (charset)
  292. "Return a vector of information of CHARSET.
  293. This function is provided for backward compatibility.
  294. The elements of the vector are:
  295. CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
  296. LEADING-CODE-BASE, LEADING-CODE-EXT,
  297. ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
  298. REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
  299. PLIST.
  300. where
  301. CHARSET-ID is always 0.
  302. BYTES is always 0.
  303. DIMENSION is the number of bytes of a code-point of the charset:
  304. 1, 2, 3, or 4.
  305. CHARS is the number of characters in a dimension:
  306. 94, 96, 128, or 256.
  307. WIDTH is always 0.
  308. DIRECTION is always 0.
  309. LEADING-CODE-BASE is always 0.
  310. LEADING-CODE-EXT is always 0.
  311. ISO-FINAL-CHAR (character) is the final character of the
  312. corresponding ISO 2022 charset. If the charset is not assigned
  313. any final character, the value is -1.
  314. ISO-GRAPHIC-PLANE is always 0.
  315. REVERSE-CHARSET is always -1.
  316. SHORT-NAME (string) is the short name to refer to the charset.
  317. LONG-NAME (string) is the long name to refer to the charset
  318. DESCRIPTION (string) is the description string of the charset.
  319. PLIST (property list) may contain any type of information a user
  320. want to put and get by functions `put-charset-property' and
  321. `get-charset-property' respectively."
  322. (vector 0
  323. 0
  324. (charset-dimension charset)
  325. (charset-chars charset)
  326. 0
  327. 0
  328. 0
  329. 0
  330. (charset-iso-final-char charset)
  331. 0
  332. -1
  333. (get-charset-property charset :short-name)
  334. (get-charset-property charset :short-name)
  335. (charset-description charset)
  336. (charset-plist charset)))
  337. ;; It is better not to use backquote in this file,
  338. ;; because that makes a bootstrapping problem
  339. ;; if you need to recompile all the Lisp files using interpreted code.
  340. (defun charset-id (charset)
  341. "Always return 0. This is provided for backward compatibility."
  342. 0)
  343. (make-obsolete 'charset-id "do not use it." "23.1")
  344. (defmacro charset-bytes (charset)
  345. "Always return 0. This is provided for backward compatibility."
  346. 0)
  347. (make-obsolete 'charset-bytes "do not use it." "23.1")
  348. (defun get-charset-property (charset propname)
  349. "Return the value of CHARSET's PROPNAME property.
  350. This is the last value stored with
  351. (put-charset-property CHARSET PROPNAME VALUE)."
  352. (plist-get (charset-plist charset) propname))
  353. (defun put-charset-property (charset propname value)
  354. "Set CHARSETS's PROPNAME property to value VALUE.
  355. It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
  356. (set-charset-plist charset
  357. (plist-put (charset-plist charset) propname
  358. (if (stringp value)
  359. (purecopy value)
  360. value))))
  361. (defun charset-description (charset)
  362. "Return description string of CHARSET."
  363. (plist-get (charset-plist charset) :docstring))
  364. (defun charset-dimension (charset)
  365. "Return dimension of CHARSET."
  366. (plist-get (charset-plist charset) :dimension))
  367. (defun charset-chars (charset &optional dimension)
  368. "Return number of characters contained in DIMENSION of CHARSET.
  369. DIMENSION defaults to the first dimension."
  370. (unless dimension (setq dimension 1))
  371. (let ((code-space (plist-get (charset-plist charset) :code-space)))
  372. (1+ (- (aref code-space (1- (* 2 dimension)))
  373. (aref code-space (- (* 2 dimension) 2))))))
  374. (defun charset-iso-final-char (charset)
  375. "Return ISO-2022 final character of CHARSET.
  376. Return -1 if charset isn't an ISO 2022 one."
  377. (or (plist-get (charset-plist charset) :iso-final-char)
  378. -1))
  379. (defmacro charset-short-name (charset)
  380. "Return short name of CHARSET."
  381. (plist-get (charset-plist charset) :short-name))
  382. (defmacro charset-long-name (charset)
  383. "Return long name of CHARSET."
  384. (plist-get (charset-plist charset) :long-name))
  385. (defun charset-list ()
  386. "Return list of all charsets ever defined."
  387. charset-list)
  388. (make-obsolete 'charset-list "use variable `charset-list'." "23.1")
  389. ;;; CHARACTER
  390. (define-obsolete-function-alias 'char-valid-p 'characterp "23.1")
  391. (defun generic-char-p (char)
  392. "Always return nil. This is provided for backward compatibility."
  393. nil)
  394. (make-obsolete 'generic-char-p "generic characters no longer exist." "23.1")
  395. (defun make-char-internal (charset-id &optional code1 code2)
  396. (let ((charset (aref emacs-mule-charset-table charset-id)))
  397. (or charset
  398. (error "Invalid Emacs-mule charset ID: %d" charset-id))
  399. (make-char charset code1 code2)))
  400. ;; Save the ASCII case table in case we need it later. Some locales
  401. ;; (such as Turkish) modify the case behavior of ASCII characters,
  402. ;; which can interfere with networking code that uses ASCII strings.
  403. (defvar ascii-case-table
  404. ;; Code copied from copy-case-table to avoid requiring case-table.el
  405. (let ((tbl (copy-sequence (standard-case-table)))
  406. (up (char-table-extra-slot (standard-case-table) 0)))
  407. (if up (set-char-table-extra-slot tbl 0 (copy-sequence up)))
  408. (set-char-table-extra-slot tbl 1 nil)
  409. (set-char-table-extra-slot tbl 2 nil)
  410. tbl)
  411. "Case table for the ASCII character set.")
  412. ;; Coding system stuff
  413. ;; Coding system is a symbol that has been defined by the function
  414. ;; `define-coding-system'.
  415. (defconst coding-system-iso-2022-flags
  416. '(long-form
  417. ascii-at-eol
  418. ascii-at-cntl
  419. 7-bit
  420. locking-shift
  421. single-shift
  422. designation
  423. revision
  424. direction
  425. init-at-bol
  426. designate-at-bol
  427. safe
  428. latin-extra
  429. composition
  430. euc-tw-shift
  431. use-roman
  432. use-oldjis)
  433. "List of symbols that control ISO-2022 encoder/decoder.
  434. The value of the `:flags' attribute in the argument of the function
  435. `define-coding-system' must be one of them.
  436. If `long-form' is specified, use a long designation sequence on
  437. encoding for the charsets `japanese-jisx0208-1978', `chinese-gb2312',
  438. and `japanese-jisx0208'. The long designation sequence doesn't
  439. conform to ISO 2022, but is used by such coding systems as
  440. `compound-text'.
  441. If `ascii-at-eol' is specified, designate ASCII to g0 at end of line
  442. on encoding.
  443. If `ascii-at-cntl' is specified, designate ASCII to g0 before control
  444. codes and SPC on encoding.
  445. If `7-bit' is specified, use 7-bit code only on encoding.
  446. If `locking-shift' is specified, decode locking-shift code correctly
  447. on decoding, and use locking-shift to invoke a graphic element on
  448. encoding.
  449. If `single-shift' is specified, decode single-shift code correctly on
  450. decoding, and use single-shift to invoke a graphic element on encoding.
  451. If `designation' is specified, decode designation code correctly on
  452. decoding, and use designation to designate a charset to a graphic
  453. element on encoding.
  454. If `revision' is specified, produce an escape sequence to specify
  455. revision number of a charset on encoding. Such an escape sequence is
  456. always correctly decoded on decoding.
  457. If `direction' is specified, decode ISO6429's code for specifying
  458. direction correctly, and produce the code on encoding.
  459. If `init-at-bol' is specified, on encoding, it is assumed that
  460. invocation and designation statuses are reset at each beginning of
  461. line even if `ascii-at-eol' is not specified; thus no codes for
  462. resetting them are produced.
  463. If `safe' is specified, on encoding, characters not supported by a
  464. coding are replaced with `?'.
  465. If `latin-extra' is specified, the code-detection routine assumes that a
  466. code specified in `latin-extra-code-table' (which see) is valid.
  467. If `composition' is specified, an escape sequence to specify
  468. composition sequence is correctly decoded on decoding, and is produced
  469. on encoding.
  470. If `euc-tw-shift' is specified, the EUC-TW specific shifting code is
  471. correctly decoded on decoding, and is produced on encoding.
  472. If `use-roman' is specified, JIS0201-1976-Roman is designated instead
  473. of ASCII.
  474. If `use-oldjis' is specified, JIS0208-1976 is designated instead of
  475. JIS0208-1983.")
  476. (defun define-coding-system (name docstring &rest props)
  477. "Define NAME (a symbol) as a coding system with DOCSTRING and attributes.
  478. The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE
  479. may be any symbol.
  480. The following attributes have special meanings. Those labeled as
  481. \"(required)\" should not be omitted.
  482. `:mnemonic' (required)
  483. VALUE is a character to display on mode line for the coding system.
  484. `:coding-type' (required)
  485. VALUE must be one of `charset', `utf-8', `utf-16', `iso-2022',
  486. `emacs-mule', `shift-jis', `ccl', `raw-text', `undecided'.
  487. `:eol-type'
  488. VALUE is the EOL (end-of-line) format of the coding system. It must be
  489. one of `unix', `dos', `mac'. The symbol `unix' means Unix-like EOL
  490. \(i.e. single LF), `dos' means DOS-like EOL \(i.e. sequence of CR LF),
  491. and `mac' means Mac-like EOL \(i.e. single CR). If omitted, Emacs
  492. detects the EOL format automatically when decoding.
  493. `:charset-list'
  494. VALUE must be a list of charsets supported by the coding system. On
  495. encoding by the coding system, if a character belongs to multiple
  496. charsets in the list, a charset that comes earlier in the list is
  497. selected. If `:coding-type' is `iso-2022', VALUE may be `iso-2022',
  498. which indicates that the coding system supports all ISO-2022 based
  499. charsets. If `:coding-type' is `emacs-mule', VALUE may be
  500. `emacs-mule', which indicates that the coding system supports all
  501. charsets that have the `:emacs-mule-id' property.
  502. `:ascii-compatible-p'
  503. If VALUE is non-nil, the coding system decodes all 7-bit bytes into
  504. the corresponding ASCII characters, and encodes all ASCII characters
  505. back to the corresponding 7-bit bytes. VALUE defaults to nil.
  506. `:decode-translation-table'
  507. VALUE must be a translation table to use on decoding.
  508. `:encode-translation-table'
  509. VALUE must be a translation table to use on encoding.
  510. `:post-read-conversion'
  511. VALUE must be a function to call after some text is inserted and
  512. decoded by the coding system itself and before any functions in
  513. `after-insert-functions' are called. This function is passed one
  514. argument; the number of characters in the text to convert, with
  515. point at the start of the text. The function should leave point
  516. the same, and return the new character count.
  517. `:pre-write-conversion'
  518. VALUE must be a function to call after all functions in
  519. `write-region-annotate-functions' and `buffer-file-format' are
  520. called, and before the text is encoded by the coding system
  521. itself. This function should convert the whole text in the
  522. current buffer. For backward compatibility, this function is
  523. passed two arguments which can be ignored.
  524. `:default-char'
  525. VALUE must be a character. On encoding, a character not supported by
  526. the coding system is replaced with VALUE.
  527. `:for-unibyte'
  528. VALUE non-nil means that visiting a file with the coding system
  529. results in a unibyte buffer.
  530. `:mime-charset'
  531. VALUE must be a symbol whose name is that of a MIME charset converted
  532. to lower case.
  533. `:mime-text-unsuitable'
  534. VALUE non-nil means the `:mime-charset' property names a charset which
  535. is unsuitable for the top-level media type \"text\".
  536. `:flags'
  537. VALUE must be a list of symbols that control the ISO-2022 converter.
  538. Each must be a member of the list `coding-system-iso-2022-flags'
  539. \(which see). This attribute has a meaning only when `:coding-type'
  540. is `iso-2022'.
  541. `:designation'
  542. VALUE must be a vector [G0-USAGE G1-USAGE G2-USAGE G3-USAGE].
  543. GN-USAGE specifies the usage of graphic register GN as follows.
  544. If it is nil, no charset can be designated to GN.
  545. If it is a charset, the charset is initially designated to GN, and
  546. never used by the other charsets.
  547. If it is a list, the elements must be charsets, nil, 94, or 96. GN
  548. can be used by all the listed charsets. If the list contains 94, any
  549. iso-2022 charset whose code-space ranges are 94 long can be designated
  550. to GN. If the list contains 96, any charsets whose whose ranges are
  551. 96 long can be designated to GN. If the first element is a charset,
  552. that charset is initially designated to GN.
  553. This attribute has a meaning only when `:coding-type' is `iso-2022'.
  554. `:bom'
  555. This attributes specifies whether the coding system uses a `byte order
  556. mark'. VALUE must be nil, t, or cons of coding systems whose
  557. `:coding-type' is `utf-16' or `utf-8'.
  558. If the value is nil, on decoding, don't treat the first two-byte as
  559. BOM, and on encoding, don't produce BOM bytes.
  560. If the value is t, on decoding, skip the first two-byte as BOM, and on
  561. encoding, produce BOM bytes according to the value of `:endian'.
  562. If the value is cons, on decoding, check the first two-byte. If they
  563. are 0xFE 0xFF, use the car part coding system of the value. If they
  564. are 0xFF 0xFE, use the cdr part coding system of the value.
  565. Otherwise, treat them as bytes for a normal character. On encoding,
  566. produce BOM bytes according to the value of `:endian'.
  567. This attribute has a meaning only when `:coding-type' is `utf-16' or
  568. `utf-8'.
  569. `:endian'
  570. VALUE must be `big' or `little' specifying big-endian and
  571. little-endian respectively. The default value is `big'.
  572. This attribute has a meaning only when `:coding-type' is `utf-16'.
  573. `:ccl-decoder'
  574. VALUE is a symbol representing the registered CCL program used for
  575. decoding. This attribute has a meaning only when `:coding-type' is
  576. `ccl'.
  577. `:ccl-encoder'
  578. VALUE is a symbol representing the registered CCL program used for
  579. encoding. This attribute has a meaning only when `:coding-type' is
  580. `ccl'."
  581. (let* ((common-attrs (mapcar 'list
  582. '(:mnemonic
  583. :coding-type
  584. :charset-list
  585. :ascii-compatible-p
  586. :decode-translation-table
  587. :encode-translation-table
  588. :post-read-conversion
  589. :pre-write-conversion
  590. :default-char
  591. :for-unibyte
  592. :plist
  593. :eol-type)))
  594. (coding-type (plist-get props :coding-type))
  595. (spec-attrs (mapcar 'list
  596. (cond ((eq coding-type 'iso-2022)
  597. '(:initial
  598. :reg-usage
  599. :request
  600. :flags))
  601. ((eq coding-type 'utf-8)
  602. '(:bom))
  603. ((eq coding-type 'utf-16)
  604. '(:bom
  605. :endian))
  606. ((eq coding-type 'ccl)
  607. '(:ccl-decoder
  608. :ccl-encoder
  609. :valids))))))
  610. (dolist (slot common-attrs)
  611. (setcdr slot (plist-get props (car slot))))
  612. (dolist (slot spec-attrs)
  613. (setcdr slot (plist-get props (car slot))))
  614. (if (eq coding-type 'iso-2022)
  615. (let ((designation (plist-get props :designation))
  616. (flags (plist-get props :flags))
  617. (initial (make-vector 4 nil))
  618. (reg-usage (cons 4 4))
  619. request elt)
  620. (dotimes (i 4)
  621. (setq elt (aref designation i))
  622. (cond ((charsetp elt)
  623. (aset initial i elt)
  624. (setq request (cons (cons elt i) request)))
  625. ((consp elt)
  626. (aset initial i (car elt))
  627. (if (charsetp (car elt))
  628. (setq request (cons (cons (car elt) i) request)))
  629. (dolist (e (cdr elt))
  630. (cond ((charsetp e)
  631. (setq request (cons (cons e i) request)))
  632. ((eq e 94)
  633. (setcar reg-usage i))
  634. ((eq e 96)
  635. (setcdr reg-usage i))
  636. ((eq e t)
  637. (setcar reg-usage i)
  638. (setcdr reg-usage i)))))))
  639. (setcdr (assq :initial spec-attrs) initial)
  640. (setcdr (assq :reg-usage spec-attrs) reg-usage)
  641. (setcdr (assq :request spec-attrs) request)
  642. ;; Change :flags value from a list to a bit-mask.
  643. (let ((bits 0)
  644. (i 0))
  645. (dolist (elt coding-system-iso-2022-flags)
  646. (if (memq elt flags)
  647. (setq bits (logior bits (lsh 1 i))))
  648. (setq i (1+ i)))
  649. (setcdr (assq :flags spec-attrs) bits))))
  650. ;; Add :name and :docstring properties to PROPS.
  651. (setq props
  652. (cons :name (cons name (cons :docstring (cons (purecopy docstring)
  653. props)))))
  654. (setcdr (assq :plist common-attrs) props)
  655. (apply 'define-coding-system-internal
  656. name (mapcar 'cdr (append common-attrs spec-attrs)))))
  657. (defun coding-system-doc-string (coding-system)
  658. "Return the documentation string for CODING-SYSTEM."
  659. (plist-get (coding-system-plist coding-system) :docstring))
  660. (defun coding-system-mnemonic (coding-system)
  661. "Return the mnemonic character of CODING-SYSTEM.
  662. The mnemonic character of a coding system is used in mode line to
  663. indicate the coding system. If CODING-SYSTEM is nil, return ?=."
  664. (plist-get (coding-system-plist coding-system) :mnemonic))
  665. (defun coding-system-type (coding-system)
  666. "Return the coding type of CODING-SYSTEM.
  667. A coding type is a symbol indicating the encoding method of CODING-SYSTEM.
  668. See the function `define-coding-system' for more detail."
  669. (plist-get (coding-system-plist coding-system) :coding-type))
  670. (defun coding-system-charset-list (coding-system)
  671. "Return list of charsets supported by CODING-SYSTEM.
  672. If CODING-SYSTEM supports all ISO-2022 charsets, return `iso-2022'.
  673. If CODING-SYSTEM supports all emacs-mule charsets, return `emacs-mule'."
  674. (plist-get (coding-system-plist coding-system) :charset-list))
  675. (defun coding-system-category (coding-system)
  676. "Return a category symbol of CODING-SYSTEM."
  677. (plist-get (coding-system-plist coding-system) :category))
  678. (defun coding-system-get (coding-system prop)
  679. "Extract a value from CODING-SYSTEM's property list for property PROP.
  680. For compatibility with Emacs 20/21, this accepts old-style symbols
  681. like `mime-charset' as well as the current style like `:mime-charset'."
  682. (or (plist-get (coding-system-plist coding-system) prop)
  683. (if (not (keywordp prop))
  684. ;; For backward compatibility.
  685. (if (eq prop 'ascii-incompatible)
  686. (not (plist-get (coding-system-plist coding-system)
  687. :ascii-compatible-p))
  688. (plist-get (coding-system-plist coding-system)
  689. (intern (concat ":" (symbol-name prop))))))))
  690. (defun coding-system-eol-type-mnemonic (coding-system)
  691. "Return the string indicating end-of-line format of CODING-SYSTEM."
  692. (let* ((eol-type (coding-system-eol-type coding-system))
  693. (val (cond ((eq eol-type 0) eol-mnemonic-unix)
  694. ((eq eol-type 1) eol-mnemonic-dos)
  695. ((eq eol-type 2) eol-mnemonic-mac)
  696. (t eol-mnemonic-undecided))))
  697. (if (stringp val)
  698. val
  699. (char-to-string val))))
  700. (defun coding-system-lessp (x y)
  701. (cond ((eq x 'no-conversion) t)
  702. ((eq y 'no-conversion) nil)
  703. ((eq x 'emacs-mule) t)
  704. ((eq y 'emacs-mule) nil)
  705. ((eq x 'undecided) t)
  706. ((eq y 'undecided) nil)
  707. (t (let ((c1 (coding-system-mnemonic x))
  708. (c2 (coding-system-mnemonic y)))
  709. (or (< (downcase c1) (downcase c2))
  710. (and (not (> (downcase c1) (downcase c2)))
  711. (< c1 c2)))))))
  712. (defun coding-system-equal (coding-system-1 coding-system-2)
  713. "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
  714. Two coding systems are identical if both symbols are equal
  715. or one is an alias of the other."
  716. (or (eq coding-system-1 coding-system-2)
  717. (and (equal (coding-system-plist coding-system-1)
  718. (coding-system-plist coding-system-2))
  719. (let ((eol-type-1 (coding-system-eol-type coding-system-1))
  720. (eol-type-2 (coding-system-eol-type coding-system-2)))
  721. (or (eq eol-type-1 eol-type-2)
  722. (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
  723. (defun add-to-coding-system-list (coding-system)
  724. "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
  725. (if (or (null coding-system-list)
  726. (coding-system-lessp coding-system (car coding-system-list)))
  727. (setq coding-system-list (cons coding-system coding-system-list))
  728. (let ((len (length coding-system-list))
  729. mid (tem coding-system-list))
  730. (while (> len 1)
  731. (setq mid (nthcdr (/ len 2) tem))
  732. (if (coding-system-lessp (car mid) coding-system)
  733. (setq tem mid
  734. len (- len (/ len 2)))
  735. (setq len (/ len 2))))
  736. (setcdr tem (cons coding-system (cdr tem))))))
  737. (defun coding-system-list (&optional base-only)
  738. "Return a list of all existing non-subsidiary coding systems.
  739. If optional arg BASE-ONLY is non-nil, only base coding systems are
  740. listed. The value doesn't include subsidiary coding systems which are
  741. made from bases and aliases automatically for various end-of-line
  742. formats (e.g. iso-latin-1-unix, koi8-r-dos)."
  743. (let ((codings nil))
  744. (dolist (coding coding-system-list)
  745. (if (eq (coding-system-base coding) coding)
  746. (if base-only
  747. (setq codings (cons coding codings))
  748. (dolist (alias (coding-system-aliases coding))
  749. (setq codings (cons alias codings))))))
  750. codings))
  751. (defconst char-coding-system-table nil
  752. "It exists just for backward compatibility, and the value is always nil.")
  753. (make-obsolete-variable 'char-coding-system-table nil "23.1")
  754. (defun transform-make-coding-system-args (name type &optional doc-string props)
  755. "For internal use only.
  756. Transform XEmacs style args for `make-coding-system' to Emacs style.
  757. Value is a list of transformed arguments."
  758. (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
  759. (eol-type (plist-get props 'eol-type))
  760. properties tmp)
  761. (cond
  762. ((eq eol-type 'lf) (setq eol-type 'unix))
  763. ((eq eol-type 'crlf) (setq eol-type 'dos))
  764. ((eq eol-type 'cr) (setq eol-type 'mac)))
  765. (if (setq tmp (plist-get props 'post-read-conversion))
  766. (setq properties (plist-put properties 'post-read-conversion tmp)))
  767. (if (setq tmp (plist-get props 'pre-write-conversion))
  768. (setq properties (plist-put properties 'pre-write-conversion tmp)))
  769. (cond
  770. ((eq type 'shift-jis)
  771. `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
  772. ((eq type 'iso2022) ; This is not perfect.
  773. (if (plist-get props 'escape-quoted)
  774. (error "escape-quoted is not supported: %S"
  775. `(,name ,type ,doc-string ,props)))
  776. (let ((g0 (plist-get props 'charset-g0))
  777. (g1 (plist-get props 'charset-g1))
  778. (g2 (plist-get props 'charset-g2))
  779. (g3 (plist-get props 'charset-g3))
  780. (use-roman
  781. (and
  782. (eq (cadr (assoc 'latin-jisx0201
  783. (plist-get props 'input-charset-conversion)))
  784. 'ascii)
  785. (eq (cadr (assoc 'ascii
  786. (plist-get props 'output-charset-conversion)))
  787. 'latin-jisx0201)))
  788. (use-oldjis
  789. (and
  790. (eq (cadr (assoc 'japanese-jisx0208-1978
  791. (plist-get props 'input-charset-conversion)))
  792. 'japanese-jisx0208)
  793. (eq (cadr (assoc 'japanese-jisx0208
  794. (plist-get props 'output-charset-conversion)))
  795. 'japanese-jisx0208-1978))))
  796. (if (charsetp g0)
  797. (if (plist-get props 'force-g0-on-output)
  798. (setq g0 `(nil ,g0))
  799. (setq g0 `(,g0 t))))
  800. (if (charsetp g1)
  801. (if (plist-get props 'force-g1-on-output)
  802. (setq g1 `(nil ,g1))
  803. (setq g1 `(,g1 t))))
  804. (if (charsetp g2)
  805. (if (plist-get props 'force-g2-on-output)
  806. (setq g2 `(nil ,g2))
  807. (setq g2 `(,g2 t))))
  808. (if (charsetp g3)
  809. (if (plist-get props 'force-g3-on-output)
  810. (setq g3 `(nil ,g3))
  811. (setq g3 `(,g3 t))))
  812. `(,name 2 ,mnemonic ,doc-string
  813. (,g0 ,g1 ,g2 ,g3
  814. ,(plist-get props 'short)
  815. ,(not (plist-get props 'no-ascii-eol))
  816. ,(not (plist-get props 'no-ascii-cntl))
  817. ,(plist-get props 'seven)
  818. t
  819. ,(not (plist-get props 'lock-shift))
  820. ,use-roman
  821. ,use-oldjis
  822. ,(plist-get props 'no-iso6429)
  823. nil nil nil nil)
  824. ,properties ,eol-type)))
  825. ((eq type 'big5)
  826. `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
  827. ((eq type 'ccl)
  828. `(,name 4 ,mnemonic ,doc-string
  829. (,(plist-get props 'decode) . ,(plist-get props 'encode))
  830. ,properties ,eol-type))
  831. (t
  832. (error "unsupported XEmacs style make-coding-style arguments: %S"
  833. `(,name ,type ,doc-string ,props))))))
  834. (defun make-coding-system (coding-system type mnemonic doc-string
  835. &optional
  836. flags
  837. properties
  838. eol-type)
  839. "Define a new coding system CODING-SYSTEM (symbol).
  840. This function is provided for backward compatibility."
  841. ;; For compatibility with XEmacs, we check the type of TYPE. If it
  842. ;; is a symbol, perhaps, this function is called with XEmacs-style
  843. ;; arguments. Here, try to transform that kind of arguments to
  844. ;; Emacs style.
  845. (if (symbolp type)
  846. (let ((args (transform-make-coding-system-args coding-system type
  847. mnemonic doc-string)))
  848. (setq coding-system (car args)
  849. type (nth 1 args)
  850. mnemonic (nth 2 args)
  851. doc-string (nth 3 args)
  852. flags (nth 4 args)
  853. properties (nth 5 args)
  854. eol-type (nth 6 args))))
  855. (setq type
  856. (cond ((eq type 0) 'emacs-mule)
  857. ((eq type 1) 'shift-jis)
  858. ((eq type 2) 'iso2022)
  859. ((eq type 3) 'big5)
  860. ((eq type 4) 'ccl)
  861. ((eq type 5) 'raw-text)
  862. (t
  863. (error "Invalid coding system type: %s" type))))
  864. (setq properties
  865. (let ((plist nil) key)
  866. (dolist (elt properties)
  867. (setq key (car elt))
  868. (cond ((eq key 'post-read-conversion)
  869. (setq key :post-read-conversion))
  870. ((eq key 'pre-write-conversion)
  871. (setq key :pre-write-conversion))
  872. ((eq key 'translation-table-for-decode)
  873. (setq key :decode-translation-table))
  874. ((eq key 'translation-table-for-encode)
  875. (setq key :encode-translation-table))
  876. ((eq key 'safe-charsets)
  877. (setq key :charset-list))
  878. ((eq key 'mime-charset)
  879. (setq key :mime-charset))
  880. ((eq key 'valid-codes)
  881. (setq key :valids)))
  882. (setq plist (plist-put plist key (cdr elt))))
  883. plist))
  884. (setq properties (plist-put properties :mnemonic mnemonic))
  885. (plist-put properties :coding-type type)
  886. (cond ((eq eol-type 0) (setq eol-type 'unix))
  887. ((eq eol-type 1) (setq eol-type 'dos))
  888. ((eq eol-type 2) (setq eol-type 'mac))
  889. ((vectorp eol-type) (setq eol-type nil)))
  890. (plist-put properties :eol-type eol-type)
  891. (cond
  892. ((eq type 'iso2022)
  893. (plist-put properties :flags
  894. (list (and (or (consp (nth 0 flags))
  895. (consp (nth 1 flags))
  896. (consp (nth 2 flags))
  897. (consp (nth 3 flags))) 'designation)
  898. (or (nth 4 flags) 'long-form)
  899. (and (nth 5 flags) 'ascii-at-eol)
  900. (and (nth 6 flags) 'ascii-at-cntl)
  901. (and (nth 7 flags) '7-bit)
  902. (and (nth 8 flags) 'locking-shift)
  903. (and (nth 9 flags) 'single-shift)
  904. (and (nth 10 flags) 'use-roman)
  905. (and (nth 11 flags) 'use-oldjis)
  906. (or (nth 12 flags) 'direction)
  907. (and (nth 13 flags) 'init-at-bol)
  908. (and (nth 14 flags) 'designate-at-bol)
  909. (and (nth 15 flags) 'safe)
  910. (and (nth 16 flags) 'latin-extra)))
  911. (plist-put properties :designation
  912. (let ((vec (make-vector 4 nil)))
  913. (dotimes (i 4)
  914. (let ((spec (nth i flags)))
  915. (if (eq spec t)
  916. (aset vec i '(94 96))
  917. (if (consp spec)
  918. (progn
  919. (if (memq t spec)
  920. (setq spec (append (delq t spec) '(94 96))))
  921. (aset vec i spec))))))
  922. vec)))
  923. ((eq type 'ccl)
  924. (plist-put properties :ccl-decoder (car flags))
  925. (plist-put properties :ccl-encoder (cdr flags))))
  926. (apply 'define-coding-system coding-system doc-string properties))
  927. (make-obsolete 'make-coding-system 'define-coding-system "23.1")
  928. (defun merge-coding-systems (first second)
  929. "Fill in any unspecified aspects of coding system FIRST from SECOND.
  930. Return the resulting coding system."
  931. (let ((base (coding-system-base second))
  932. (eol (coding-system-eol-type second)))
  933. ;; If FIRST doesn't specify text conversion, merge with that of SECOND.
  934. (if (eq (coding-system-base first) 'undecided)
  935. (setq first (coding-system-change-text-conversion first base)))
  936. ;; If FIRST doesn't specify eol conversion, merge with that of SECOND.
  937. (if (and (vectorp (coding-system-eol-type first))
  938. (numberp eol) (>= eol 0) (<= eol 2))
  939. (setq first (coding-system-change-eol-conversion
  940. first eol)))
  941. first))
  942. (defun autoload-coding-system (symbol form)
  943. "Define SYMBOL as a coding-system that is defined on demand.
  944. FORM is a form to evaluate to define the coding-system."
  945. (put symbol 'coding-system-define-form form)
  946. (setq coding-system-alist (cons (list (symbol-name symbol))
  947. coding-system-alist))
  948. (dolist (elt '("-unix" "-dos" "-mac"))
  949. (let ((name (concat (symbol-name symbol) elt)))
  950. (put (intern name) 'coding-system-define-form form)
  951. (setq coding-system-alist (cons (list name) coding-system-alist)))))
  952. ;; This variable is set in these three cases:
  953. ;; (1) A file is read by a coding system specified explicitly.
  954. ;; after-insert-file-set-coding sets the car of this value to
  955. ;; coding-system-for-read, and sets the cdr to nil.
  956. ;; (2) A buffer is saved.
  957. ;; After writing, basic-save-buffer-1 sets the car of this value
  958. ;; to last-coding-system-used.
  959. ;; (3) set-buffer-file-coding-system is called.
  960. ;; The cdr of this value is set to the specified coding system.
  961. ;; This variable is used for decoding in revert-buffer and encoding in
  962. ;; select-safe-coding-system.
  963. (defvar buffer-file-coding-system-explicit nil
  964. "The file coding system explicitly specified for the current buffer.
  965. The value is a cons of coding systems for reading (decoding) and
  966. writing (encoding).
  967. Internal use only.")
  968. (make-variable-buffer-local 'buffer-file-coding-system-explicit)
  969. (put 'buffer-file-coding-system-explicit 'permanent-local t)
  970. (defun read-buffer-file-coding-system ()
  971. (let* ((bcss (find-coding-systems-region (point-min) (point-max)))
  972. (css-table
  973. (unless (equal bcss '(undecided))
  974. (append '("dos" "unix" "mac")
  975. (delq nil (mapcar (lambda (cs)
  976. (if (memq (coding-system-base cs) bcss)
  977. (symbol-name cs)))
  978. coding-system-list)))))
  979. (combined-table
  980. (if css-table
  981. (completion-table-in-turn css-table coding-system-alist)
  982. coding-system-alist))
  983. (auto-cs
  984. (unless find-file-literally
  985. (save-excursion
  986. (save-restriction
  987. (widen)
  988. (goto-char (point-min))
  989. (funcall set-auto-coding-function
  990. (or buffer-file-name "") (buffer-size))))))
  991. (preferred
  992. (let ((bfcs (default-value 'buffer-file-coding-system)))
  993. (cons (and (or (equal bcss '(undecided))
  994. (memq (coding-system-base bfcs) bcss))
  995. bfcs)
  996. (mapcar (lambda (cs)
  997. (and (coding-system-p cs)
  998. (coding-system-get cs :mime-charset)
  999. (or (equal bcss '(undecided))
  1000. (memq (coding-system-base cs) bcss))
  1001. cs))
  1002. (coding-system-priority-list)))))
  1003. (default
  1004. (let ((current (coding-system-base buffer-file-coding-system)))
  1005. ;; Generally use as a default the first preferred coding-system
  1006. ;; different from the current coding-system, except for
  1007. ;; the case of auto-cs since choosing anything else is asking
  1008. ;; for trouble (would lead to using a different coding
  1009. ;; system than specified in the coding tag).
  1010. (or auto-cs
  1011. (car (delq nil
  1012. (mapcar (lambda (cs)
  1013. (if (eq current (coding-system-base cs))
  1014. nil
  1015. cs))
  1016. preferred))))))
  1017. (completion-ignore-case t)
  1018. (completion-pcm--delim-wild-regex ; Let "u8" complete to "utf-8".
  1019. (concat completion-pcm--delim-wild-regex
  1020. "\\|\\([[:alpha:]]\\)[[:digit:]]"))
  1021. (cs (completing-read
  1022. (format "Coding system for saving file (default %s): " default)
  1023. combined-table
  1024. nil t nil 'coding-system-history
  1025. (if default (symbol-name default)))))
  1026. (unless (zerop (length cs)) (intern cs))))
  1027. (defun set-buffer-file-coding-system (coding-system &optional force nomodify)
  1028. "Set the file coding-system of the current buffer to CODING-SYSTEM.
  1029. This means that when you save the buffer, it will be converted
  1030. according to CODING-SYSTEM. For a list of possible values of
  1031. CODING-SYSTEM, use \\[list-coding-systems].
  1032. If CODING-SYSTEM leaves the text conversion unspecified, or if it leaves
  1033. the end-of-line conversion unspecified, FORCE controls what to do.
  1034. If FORCE is nil, get the unspecified aspect (or aspects) from the buffer's
  1035. previous `buffer-file-coding-system' value (if it is specified there).
  1036. Otherwise, leave it unspecified.
  1037. This marks the buffer modified so that the succeeding \\[save-buffer]
  1038. surely saves the buffer with CODING-SYSTEM. From a program, if you
  1039. don't want to mark the buffer modified, specify t for NOMODIFY.
  1040. If you know exactly what coding system you want to use,
  1041. just set the variable `buffer-file-coding-system' directly."
  1042. (interactive
  1043. (list (read-buffer-file-coding-system)
  1044. current-prefix-arg))
  1045. (check-coding-system coding-system)
  1046. (if (and coding-system buffer-file-coding-system (null force))
  1047. (setq coding-system
  1048. (merge-coding-systems coding-system buffer-file-coding-system)))
  1049. (when (called-interactively-p 'interactive)
  1050. ;; Check whether save would succeed, and jump to the offending char(s)
  1051. ;; if not.
  1052. (let ((css (find-coding-systems-region (point-min) (point-max))))
  1053. (unless (or (eq (car css) 'undecided)
  1054. (memq (coding-system-base coding-system) css))
  1055. (setq coding-system (select-safe-coding-system-interactively
  1056. (point-min) (point-max) css
  1057. (list coding-system))))))
  1058. (setq buffer-file-coding-system coding-system)
  1059. (if buffer-file-coding-system-explicit
  1060. (setcdr buffer-file-coding-system-explicit coding-system)
  1061. (setq buffer-file-coding-system-explicit (cons nil coding-system)))
  1062. (unless nomodify
  1063. (set-buffer-modified-p t))
  1064. (force-mode-line-update))
  1065. (defun revert-buffer-with-coding-system (coding-system &optional force)
  1066. "Visit the current buffer's file again using coding system CODING-SYSTEM.
  1067. For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
  1068. If CODING-SYSTEM leaves the text conversion unspecified, or if it leaves
  1069. the end-of-line conversion unspecified, FORCE controls what to do.
  1070. If FORCE is nil, get the unspecified aspect (or aspects) from the buffer's
  1071. previous `buffer-file-coding-system' value (if it is specified there).
  1072. Otherwise, determine it from the file contents as usual for visiting a file."
  1073. (interactive "zCoding system for visited file (default nil): \nP")
  1074. (check-coding-system coding-system)
  1075. (if (and coding-system buffer-file-coding-system (null force))
  1076. (setq coding-system
  1077. (merge-coding-systems coding-system buffer-file-coding-system)))
  1078. (let ((coding-system-for-read coding-system))
  1079. (revert-buffer)))
  1080. (defun set-file-name-coding-system (coding-system)
  1081. "Set coding system for decoding and encoding file names to CODING-SYSTEM.
  1082. It actually just set the variable `file-name-coding-system' (which see)
  1083. to CODING-SYSTEM."
  1084. (interactive "zCoding system for file names (default nil): ")
  1085. (check-coding-system coding-system)
  1086. (if (and coding-system
  1087. (not (coding-system-get coding-system :ascii-compatible-p))
  1088. (not (coding-system-get coding-system :suitable-for-file-name)))
  1089. (error "%s is not suitable for file names" coding-system))
  1090. (setq file-name-coding-system coding-system))
  1091. (defvar default-terminal-coding-system nil
  1092. "Default value for the terminal coding system.
  1093. This is normally set according to the selected language environment.
  1094. See also the command `set-terminal-coding-system'.")
  1095. (defun set-terminal-coding-system (coding-system &optional terminal)
  1096. "Set coding system of terminal output to CODING-SYSTEM.
  1097. All text output to TERMINAL will be encoded
  1098. with the specified coding system.
  1099. For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
  1100. The default is determined by the selected language environment
  1101. or by the previous use of this command.
  1102. TERMINAL may be a terminal object, a frame, or nil for the
  1103. selected frame's terminal. The setting has no effect on
  1104. graphical terminals."
  1105. (interactive
  1106. (list (let ((default (if (and (not (terminal-coding-system))
  1107. default-terminal-coding-system)
  1108. default-terminal-coding-system)))
  1109. (read-coding-system
  1110. (format "Coding system for terminal display (default %s): "
  1111. default)
  1112. default))))
  1113. (if (and (not coding-system)
  1114. (not (terminal-coding-system)))
  1115. (setq coding-system default-terminal-coding-system))
  1116. (if coding-system
  1117. (setq default-terminal-coding-system coding-system))
  1118. (set-terminal-coding-system-internal coding-system terminal)
  1119. (redraw-frame (selected-frame)))
  1120. (defvar default-keyboard-coding-system nil
  1121. "Default value of the keyboard coding system.
  1122. This is normally set according to the selected language environment.
  1123. See also the command `set-keyboard-coding-system'.")
  1124. (defun set-keyboard-coding-system (coding-system &optional terminal)
  1125. "Set coding system for keyboard input on TERMINAL to CODING-SYSTEM.
  1126. For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
  1127. The default is determined by the selected language environment
  1128. or by the previous use of this command.
  1129. If CODING-SYSTEM is nil or the coding-type of CODING-SYSTEM is
  1130. `raw-text', the decoding of keyboard input is disabled.
  1131. TERMINAL may be a terminal object, a frame, or nil for the
  1132. selected frame's terminal. The setting has no effect on
  1133. graphical terminals."
  1134. (interactive
  1135. (list (let* ((coding (keyboard-coding-system nil))
  1136. (default (if (eq (coding-system-type coding) 'raw-text)
  1137. default-keyboard-coding-system)))
  1138. (read-coding-system
  1139. (format "Coding system for keyboard input (default %s): "
  1140. default)
  1141. default))))
  1142. (let ((coding-type (coding-system-type coding-system))
  1143. (saved-meta-mode
  1144. (terminal-parameter terminal 'keyboard-coding-saved-meta-mode)))
  1145. (if (not (eq coding-type 'raw-text))
  1146. (let (accept-8-bit)
  1147. (if (not (or (coding-system-get coding-system :suitable-for-keyboard)
  1148. (coding-system-get coding-system :ascii-compatible-p)))
  1149. (error "Unsuitable coding system for keyboard: %s" coding-system))
  1150. (cond ((memq coding-type '(charset utf-8 shift-jis big5 ccl))
  1151. (setq accept-8-bit t))
  1152. ((eq coding-type 'iso-2022)
  1153. (let ((flags (coding-system-get coding-system :flags)))
  1154. (or (memq '7-bit flags)
  1155. (setq accept-8-bit t))))
  1156. (t
  1157. (error "Unsupported coding system for keyboard: %s"
  1158. coding-system)))
  1159. (when accept-8-bit
  1160. (or saved-meta-mode
  1161. (set-terminal-parameter terminal
  1162. 'keyboard-coding-saved-meta-mode
  1163. (cons (nth 2 (current-input-mode))
  1164. nil)))
  1165. (set-input-meta-mode 8))
  1166. ;; Avoid end-of-line conversion.
  1167. (setq coding-system
  1168. (coding-system-change-eol-conversion coding-system 'unix)))
  1169. (when saved-meta-mode
  1170. (set-input-meta-mode (car saved-meta-mode))
  1171. (set-terminal-parameter terminal
  1172. 'keyboard-coding-saved-meta-mode
  1173. nil))))
  1174. (set-keyboard-coding-system-internal coding-system terminal)
  1175. (setq keyboard-coding-system coding-system))
  1176. (defcustom keyboard-coding-system nil
  1177. "Specify coding system for keyboard input.
  1178. If you set this on a terminal which can't distinguish Meta keys from
  1179. 8-bit characters, you will have to use ESC to type Meta characters.
  1180. See Info node `Terminal Coding' and Info node `Unibyte Mode'.
  1181. On non-windowing terminals, this is set from the locale by default.
  1182. Setting this variable directly does not take effect;
  1183. use either \\[customize] or \\[set-keyboard-coding-system]."
  1184. :type '(coding-system :tag "Coding system")
  1185. :link '(info-link "(emacs)Terminal Coding")
  1186. :link '(info-link "(emacs)Unibyte Mode")
  1187. :set (lambda (symbol value)
  1188. ;; Don't load encoded-kb unnecessarily.
  1189. (if (or value (boundp 'encoded-kbd-setup-display))
  1190. (set-keyboard-coding-system value)
  1191. (set-default 'keyboard-coding-system nil))) ; must initialize
  1192. :version "22.1"
  1193. :group 'keyboard
  1194. :group 'mule)
  1195. (defun set-buffer-process-coding-system (decoding encoding)
  1196. "Set coding systems for the process associated with the current buffer.
  1197. DECODING is the coding system to be used to decode input from the process,
  1198. ENCODING is the coding system to be used to encode output to the process.
  1199. For a list of possible coding systems, use \\[list-coding-systems]."
  1200. (interactive
  1201. "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
  1202. (let ((proc (get-buffer-process (current-buffer))))
  1203. (if (null proc)
  1204. (error "No process")
  1205. (check-coding-system decoding)
  1206. (check-coding-system encoding)
  1207. (set-process-coding-system proc decoding encoding)))
  1208. (force-mode-line-update))
  1209. (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
  1210. (defun set-selection-coding-system (coding-system)
  1211. "Make CODING-SYSTEM used for communicating with other X clients.
  1212. When sending or receiving text via cut_buffer, selection, and clipboard,
  1213. the text is encoded or decoded by CODING-SYSTEM."
  1214. (interactive "zCoding system for X selection: ")
  1215. (check-coding-system coding-system)
  1216. (setq selection-coding-system coding-system))
  1217. ;; Coding system lastly specified by the command
  1218. ;; set-next-selection-coding-system.
  1219. (defvar last-next-selection-coding-system nil)
  1220. (defun set-next-selection-coding-system (coding-system)
  1221. "Use CODING-SYSTEM for next communication with other window system clients.
  1222. This setting is effective for the next communication only."
  1223. (interactive
  1224. (list (read-coding-system
  1225. (if last-next-selection-coding-system
  1226. (format "Coding system for the next selection (default %S): "
  1227. last-next-selection-coding-system)
  1228. "Coding system for the next selection: ")
  1229. last-next-selection-coding-system)))
  1230. (if coding-system
  1231. (setq last-next-selection-coding-system coding-system)
  1232. (setq coding-system last-next-selection-coding-system))
  1233. (check-coding-system coding-system)
  1234. (setq next-selection-coding-system coding-system))
  1235. (defun set-coding-priority (arg)
  1236. "Set priority of coding categories according to ARG.
  1237. ARG is a list of coding categories ordered by priority.
  1238. This function is provided for backward compatibility."
  1239. (apply 'set-coding-system-priority
  1240. (mapcar #'(lambda (x) (symbol-value x)) arg)))
  1241. (make-obsolete 'set-coding-priority 'set-coding-system-priority "23.1")
  1242. ;;; X selections
  1243. (defvar ctext-non-standard-encodings-alist
  1244. (mapcar 'purecopy
  1245. '(("big5-0" big5 2 big5)
  1246. ("ISO8859-14" iso-8859-14 1 latin-iso8859-14)
  1247. ("ISO8859-15" iso-8859-15 1 latin-iso8859-15)
  1248. ("gbk-0" gbk 2 chinese-gbk)
  1249. ("koi8-r" koi8-r 1 koi8-r)
  1250. ("microsoft-cp1251" windows-1251 1 windows-1251)))
  1251. "Alist of non-standard encoding names vs the corresponding usages in CTEXT.
  1252. It controls how extended segments of a compound text are handled
  1253. by the coding system `compound-text-with-extensions'.
  1254. Each element has the form (ENCODING-NAME CODING-SYSTEM N-OCTET CHARSET).
  1255. ENCODING-NAME is an encoding name of an \"extended segment\".
  1256. CODING-SYSTEM is the coding-system to encode (or decode) the
  1257. characters into (or from) the extended segment.
  1258. N-OCTET is the number of octets (bytes) that encodes a character
  1259. in the segment. It can be 0 (meaning the number of octets per
  1260. character is variable), 1, 2, 3, or 4.
  1261. CHARSET is a character set containing characters that are encoded
  1262. in the segment. It can be a list of character sets.
  1263. On decoding CTEXT, all encoding names listed here are recognized.
  1264. On encoding CTEXT, encoding names in the variable
  1265. `ctext-non-standard-encodings' (which see) and in the information
  1266. listed for the current language environment under the key
  1267. `ctext-non-standard-encodings' are used.")
  1268. (defvar ctext-non-standard-encodings nil
  1269. "List of non-standard encoding names used in extended segments of CTEXT.
  1270. Each element must be one of the names listed in the variable
  1271. `ctext-non-standard-encodings-alist' (which see).")
  1272. (defvar ctext-non-standard-encodings-regexp
  1273. (purecopy
  1274. (string-to-multibyte
  1275. (concat
  1276. ;; For non-standard encodings.
  1277. "\\(\e%/[0-4][\200-\377][\200-\377]\\([^\002]+\\)\002\\)"
  1278. "\\|"
  1279. ;; For UTF-8 encoding.
  1280. "\\(\e%G[^\e]*\e%@\\)"))))
  1281. ;; Functions to support "Non-Standard Character Set Encodings" defined
  1282. ;; by the COMPOUND-TEXT spec. They also support "The UTF-8 encoding"
  1283. ;; described in the section 7 of the documentation of COMPOUND-TEXT
  1284. ;; distributed with XFree86.
  1285. (defun ctext-post-read-conversion (len)
  1286. "Decode LEN characters encoded as Compound Text with Extended Segments."
  1287. ;; We don't need the following because it is expected that this
  1288. ;; function is mainly used for decoding X selection which is not
  1289. ;; that big data.
  1290. ;;(buffer-disable-undo) ; minimize consing due to insertions and deletions
  1291. (save-match-data
  1292. (save-restriction
  1293. (narrow-to-region (point) (+ (point) len))
  1294. (let ((case-fold-search nil)
  1295. last-coding-system-used
  1296. pos bytes)
  1297. (decode-coding-region (point-min) (point-max) 'ctext)
  1298. (while (re-search-forward ctext-non-standard-encodings-regexp
  1299. nil 'move)
  1300. (setq pos (match-beginning 0))
  1301. (if (match-beginning 1)
  1302. ;; ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
  1303. (let* ((M (multibyte-char-to-unibyte (char-after (+ pos 4))))
  1304. (L (multibyte-char-to-unibyte (char-after (+ pos 5))))
  1305. (encoding (match-string 2))
  1306. (encoding-info (assoc-string
  1307. encoding
  1308. ctext-non-standard-encodings-alist t))
  1309. (coding (if encoding-info
  1310. (nth 1 encoding-info)
  1311. (setq encoding (intern (downcase encoding)))
  1312. (and (coding-system-p encoding)
  1313. encoding))))
  1314. (setq bytes (- (+ (* (- M 128) 128) (- L 128))
  1315. (- (point) (+ pos 6))))
  1316. (when coding
  1317. (delete-region pos (point))
  1318. (forward-char bytes)
  1319. (decode-coding-region (- (point) bytes) (point) coding)))
  1320. ;; ESC % G --UTF-8-BYTES-- ESC % @
  1321. (delete-char -3)
  1322. (delete-region pos (+ pos 3))
  1323. (decode-coding-region pos (point) 'utf-8))))
  1324. (goto-char (point-min))
  1325. (- (point-max) (point)))))
  1326. (defvar ctext-standard-encodings
  1327. '(ascii latin-jisx0201 katakana-jisx0201
  1328. latin-iso8859-1 latin-iso8859-2 latin-iso8859-3 latin-iso8859-4
  1329. greek-iso8859-7 arabic-iso8859-6 hebrew-iso8859-8 cyrillic-iso8859-5
  1330. latin-iso8859-9
  1331. chinese-gb2312 japanese-jisx0208 korean-ksc5601)
  1332. "List of approved standard encodings (i.e. charsets) of X's Compound Text.
  1333. Coding-system `compound-text-with-extensions' encodes a character
  1334. belonging to any of those charsets using the normal ISO2022
  1335. designation sequence unless the current language environment or
  1336. the variable `ctext-non-standard-encodings' decide to use an extended
  1337. segment of CTEXT for that character. See also the documentation
  1338. of `ctext-non-standard-encodings-alist'.")
  1339. ;; Return an alist of CHARSET vs CTEXT-USAGE-INFO generated from
  1340. ;; `ctext-non-standard-encodings' and a list specified by the key
  1341. ;; `ctext-non-standard-encodings' for the current language
  1342. ;; environment. CTEXT-USAGE-INFO is one of the element of
  1343. ;; `ctext-non-standard-encodings-alist' or nil. In the former case, a
  1344. ;; character in CHARSET is encoded using extended segment. In the
  1345. ;; latter case, a character in CHARSET is encoded using normal ISO2022
  1346. ;; designation sequence. If a character is not in any of CHARSETs, it
  1347. ;; is encoded using UTF-8 encoding extension.
  1348. (defun ctext-non-standard-encodings-table ()
  1349. (let* ((table (append ctext-non-standard-encodings
  1350. (copy-sequence
  1351. (get-language-info current-language-environment
  1352. 'ctext-non-standard-encodings))))
  1353. (tail table)
  1354. elt)
  1355. (while tail
  1356. (setq elt (car tail))
  1357. (let* ((slot (assoc elt ctext-non-standard-encodings-alist))
  1358. (charset (nth 3 slot)))
  1359. (if (charsetp charset)
  1360. (setcar tail
  1361. (cons (plist-get (charset-plist charset) :base) slot))
  1362. (setcar tail (cons (car charset) slot))
  1363. (dolist (cs (cdr charset))
  1364. (setcdr tail
  1365. (cons (cons (plist-get (charset-plist (car cs)) :base) slot)
  1366. (cdr tail)))
  1367. (setq tail (cdr tail))))
  1368. (setq tail (cdr tail))))
  1369. table))
  1370. (defun ctext-pre-write-conversion (from to)
  1371. "Encode characters between FROM and TO as Compound Text w/Extended Segments.
  1372. If FROM is a string, generate a new temp buffer, insert the text,
  1373. and convert it in the temporary buffer. Otherwise, convert
  1374. in-place."
  1375. (save-match-data
  1376. ;; Setup a working buffer if necessary.
  1377. (when (stringp from)
  1378. (set-buffer (generate-new-buffer " *temp"))
  1379. (set-buffer-multibyte (multibyte-string-p from))
  1380. (insert from)
  1381. (setq from (point-min) to (point-max)))
  1382. (save-restriction
  1383. (narrow-to-region from to)
  1384. (goto-char from)
  1385. (let ((encoding-table (ctext-non-standard-encodings-table))
  1386. (charset-list (sort-charsets
  1387. (copy-sequence ctext-standard-encodings)))
  1388. (end-pos (make-marker))
  1389. last-coding-system-used
  1390. last-pos charset encoding-info)
  1391. (dolist (elt encoding-table)
  1392. (push (car elt) charset-list))
  1393. (setq end-pos (point-marker))
  1394. (while (re-search-forward "[^\0-\177]+" nil t)
  1395. ;; Found a sequence of non-ASCII characters.
  1396. (set-marker end-pos (match-end 0))
  1397. (goto-char (match-beginning 0))
  1398. (setq last-pos (point)
  1399. charset (char-charset (following-char) charset-list))
  1400. (forward-char 1)
  1401. (while (and (< (point) end-pos)
  1402. (eq charset (char-charset (following-char) charset-list)))
  1403. (forward-char 1))
  1404. (if charset
  1405. (if (setq encoding-info (cdr (assq charset encoding-table)))
  1406. ;; Encode this range using an extended segment.
  1407. (let ((encoding-name (car encoding-info))
  1408. (coding-system (nth 1 encoding-info))
  1409. (noctets (nth 2 encoding-info))
  1410. len)
  1411. (encode-coding-region last-pos (point) coding-system)
  1412. (setq len (+ (length encoding-name) 1
  1413. (- (point) last-pos)))
  1414. ;; According to the spec of CTEXT, it is not
  1415. ;; necessary to produce this extra designation
  1416. ;; sequence, but some buggy application
  1417. ;; (e.g. crxvt-gb) requires it.
  1418. (insert "\e(B")
  1419. (save-excursion
  1420. (goto-char last-pos)
  1421. (insert (format "\e%%/%d" noctets))
  1422. (insert-byte (+ (/ len 128) 128) 1)
  1423. (insert-byte (+ (% len 128) 128) 1)
  1424. (insert encoding-name)
  1425. (insert 2)))
  1426. ;; Encode this range as characters in CHARSET.
  1427. (put-text-property last-pos (point) 'charset charset))
  1428. ;; Encode this range using UTF-8 encoding extension.
  1429. (encode-coding-region last-pos (point) 'mule-utf-8)
  1430. (save-excursion
  1431. (goto-char last-pos)
  1432. (insert "\e%G"))
  1433. (insert "\e%@")))
  1434. (goto-char (point-min)))))
  1435. ;; Must return nil, as build_annotations_2 expects that.
  1436. nil)
  1437. ;;; FILE I/O
  1438. ;; TODO many elements of this list are also in inhibit-local-variables-regexps.
  1439. (defcustom auto-coding-alist
  1440. ;; .exe and .EXE are added to support archive-mode looking at DOS
  1441. ;; self-extracting exe archives.
  1442. (mapcar (lambda (arg) (cons (purecopy (car arg)) (cdr arg)))
  1443. '(("\\.\\(\
  1444. arc\\|zip\\|lzh\\|lha\\|zoo\\|[jew]ar\\|xpi\\|rar\\|7z\\|\
  1445. ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\|RAR\\|7Z\\)\\'"
  1446. . no-conversion-multibyte)
  1447. ("\\.\\(exe\\|EXE\\)\\'" . no-conversion)
  1448. ("\\.\\(sx[dmicw]\\|odt\\|tar\\|t[bg]z\\)\\'" . no-conversion)
  1449. ("\\.\\(gz\\|Z\\|bz\\|bz2\\|xz\\|gpg\\)\\'" . no-conversion)
  1450. ("\\.\\(jpe?g\\|png\\|gif\\|tiff?\\|p[bpgn]m\\)\\'" . no-conversion)
  1451. ("\\.pdf\\'" . no-conversion)
  1452. ("/#[^/]+#\\'" . emacs-mule)))
  1453. "Alist of filename patterns vs corresponding coding systems.
  1454. Each element looks like (REGEXP . CODING-SYSTEM).
  1455. A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
  1456. The settings in this alist take priority over `coding:' tags
  1457. in the file (see the function `set-auto-coding')
  1458. and the contents of `file-coding-system-alist'."
  1459. :version "24.1" ; added xz
  1460. :group 'files
  1461. :group 'mule
  1462. :type '(repeat (cons (regexp :tag "File name regexp")
  1463. (symbol :tag "Coding system"))))
  1464. (defcustom auto-coding-regexp-alist
  1465. (mapcar (lambda (arg) (cons (purecopy (car arg)) (cdr arg)))
  1466. '(("\\`BABYL OPTIONS:[ \t]*-\\*-[ \t]*rmail[ \t]*-\\*-" . no-conversion)
  1467. ("\\`\xFE\xFF" . utf-16be-with-signature)
  1468. ("\\`\xFF\xFE" . utf-16le-with-signature)
  1469. ("\\`\xEF\xBB\xBF" . utf-8-with-signature)
  1470. ("\\`;ELC\024\0\0\0" . emacs-mule))) ; Emacs 20-compiled
  1471. "Alist of patterns vs corresponding coding systems.
  1472. Each element looks like (REGEXP . CODING-SYSTEM).
  1473. A file whose first bytes match REGEXP is decoded by CODING-SYSTEM on reading.
  1474. The settings in this alist take priority over `coding:' tags
  1475. in the file (see the function `set-auto-coding')
  1476. and the contents of `file-coding-system-alist'."
  1477. :group 'files
  1478. :group 'mule
  1479. :type '(repeat (cons (regexp :tag "Regexp")
  1480. (symbol :tag "Coding system"))))
  1481. (defun auto-coding-regexp-alist-lookup (from to)
  1482. "Lookup `auto-coding-regexp-alist' for the contents of the current buffer.
  1483. The value is a coding system is specified for the region FROM and TO,
  1484. or nil."
  1485. (save-excursion
  1486. (goto-char from)
  1487. (let ((alist auto-coding-regexp-alist)
  1488. coding-system)
  1489. (while (and alist (not coding-system))
  1490. (let ((regexp (car (car alist))))
  1491. (if enable-multibyte-characters
  1492. (setq regexp (string-to-multibyte regexp)))
  1493. (if (re-search-forward regexp to t)
  1494. (setq coding-system (cdr (car alist)))
  1495. (setq alist (cdr alist)))))
  1496. coding-system)))
  1497. ;; See the bottom of this file for built-in auto coding functions.
  1498. (defcustom auto-coding-functions '(sgml-xml-auto-coding-function
  1499. sgml-html-meta-auto-coding-function)
  1500. "A list of functions which attempt to determine a coding system.
  1501. Each function in this list should be written to operate on the
  1502. current buffer, but should not modify it in any way. The buffer
  1503. will contain undecoded text of parts of the file. Each function
  1504. should take one argument, SIZE, which says how many characters
  1505. \(starting from point) it should look at.
  1506. If one of these functions succeeds in determining a coding
  1507. system, it should return that coding system. Otherwise, it
  1508. should return nil.
  1509. If a file has a `coding:' tag, that takes precedence over these
  1510. functions, so they won't be called at all."
  1511. :group 'files
  1512. :group 'mule
  1513. :type '(repeat function))
  1514. (defvar set-auto-coding-for-load nil
  1515. "Non-nil means respect a \"unibyte: t\" entry in file local variables.
  1516. Emacs binds this variable to t when loading or byte-compiling Emacs Lisp
  1517. files.")
  1518. (defun auto-coding-alist-lookup (filename)
  1519. "Return the coding system specified by `auto-coding-alist' for FILENAME."
  1520. (let ((alist auto-coding-alist)
  1521. (case-fold-search (memq system-type '(windows-nt ms-dos cygwin)))
  1522. coding-system)
  1523. (while (and alist (not coding-system))
  1524. (if (string-match (car (car alist)) filename)
  1525. (setq coding-system (cdr (car alist)))
  1526. (setq alist (cdr alist))))
  1527. coding-system))
  1528. (put 'enable-character-translation 'permanent-local t)
  1529. (put 'enable-character-translation 'safe-local-variable 'booleanp)
  1530. (defun find-auto-coding (filename size)
  1531. "Find a coding system for a file FILENAME of which SIZE bytes follow point.
  1532. These bytes should include at least the first 1k of the file
  1533. and the last 3k of the file, but the middle may be omitted.
  1534. The function checks FILENAME against the variable `auto-coding-alist'.
  1535. If FILENAME doesn't match any entries in the variable, it checks the
  1536. contents of the current buffer following point against
  1537. `auto-coding-regexp-alist'. If no match is found, it checks for a
  1538. `coding:' tag in the first one or two lines following point. If no
  1539. `coding:' tag is found, it checks any local variables list in the last
  1540. 3K bytes out of the SIZE bytes. Finally, if none of these methods
  1541. succeed, it checks to see if any function in `auto-coding-functions'
  1542. gives a match.
  1543. If a coding system is specified, the return value is a cons
  1544. \(CODING . SOURCE), where CODING is the specified coding system and
  1545. SOURCE is a symbol `auto-coding-alist', `auto-coding-regexp-alist',
  1546. `:coding', or `auto-coding-functions' indicating by what CODING is
  1547. specified. Note that the validity of CODING is not checked;
  1548. it's the caller's responsibility to check it.
  1549. If nothing is specified, the return value is nil."
  1550. (or (let ((coding-system (auto-coding-alist-lookup filename)))
  1551. (if coding-system
  1552. (cons coding-system 'auto-coding-alist)))
  1553. ;; Try using `auto-coding-regexp-alist'.
  1554. (let ((coding-system (auto-coding-regexp-alist-lookup (point)
  1555. (+ (point) size))))
  1556. (if coding-system
  1557. (cons coding-system 'auto-coding-regexp-alist)))
  1558. (let* ((case-fold-search t)
  1559. (head-start (point))
  1560. (head-end (+ head-start (min size 1024)))
  1561. (tail-start (+ head-start (max (- size 3072) 0)))
  1562. (tail-end (+ head-start size))
  1563. coding-system head-found tail-found pos char-trans)
  1564. ;; Try a short cut by searching for the string "coding:"
  1565. ;; and for "unibyte:" at the head and tail of SIZE bytes.
  1566. (setq head-found (or (search-forward "coding:" head-end t)
  1567. (search-forward "unibyte:" head-end t)
  1568. (search-forward "enable-character-translation:"
  1569. head-end t)))
  1570. (if (and head-found (> head-found tail-start))
  1571. ;; Head and tail are overlapped.
  1572. (setq tail-found head-found)
  1573. (goto-char tail-start)
  1574. (setq tail-found (or (search-forward "coding:" tail-end t)
  1575. (search-forward "unibyte:" tail-end t)
  1576. (search-forward "enable-character-translation:"
  1577. tail-end t))))
  1578. ;; At first check the head.
  1579. (when head-found
  1580. (goto-char head-start)
  1581. (setq head-end (set-auto-mode-1))
  1582. (setq head-start (point))
  1583. (when (and head-end (< head-found head-end))
  1584. (goto-char head-start)
  1585. (when (and set-auto-coding-for-load
  1586. (re-search-forward
  1587. "\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
  1588. head-end t))
  1589. (setq coding-system 'raw-text))
  1590. (when (and (not coding-system)
  1591. (re-search-forward
  1592. "\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
  1593. head-end t))
  1594. (setq coding-system (intern (match-string 2))))
  1595. (when (re-search-forward
  1596. "\\(.*;\\)?[ \t]*enable-character-translation:[ \t]*\\([^ ;]+\\)"
  1597. head-end t)
  1598. (setq char-trans (match-string 2)))))
  1599. ;; If no coding: tag in the head, check the tail.
  1600. ;; Here we must pay attention to the case that the end-of-line
  1601. ;; is just "\r" and we can't use "^" nor "$" in regexp.
  1602. (when (and tail-found (or (not coding-system) (not char-trans)))
  1603. (goto-char tail-start)
  1604. (re-search-forward "[\r\n]\^L" tail-end t)
  1605. (if (re-search-forward
  1606. "[\r\n]\\([^[\r\n]*\\)[ \t]*Local Variables:[ \t]*\\([^\r\n]*\\)[\r\n]"
  1607. tail-end t)
  1608. ;; The prefix is what comes before "local variables:" in its
  1609. ;; line. The suffix is what comes after "local variables:"
  1610. ;; in its line.
  1611. (let* ((prefix (regexp-quote (match-string 1)))
  1612. (suffix (regexp-quote (match-string 2)))
  1613. (re-coding
  1614. (concat
  1615. "[\r\n]" prefix
  1616. ;; N.B. without the \n below, the regexp can
  1617. ;; eat newlines.
  1618. "[ \t]*coding[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
  1619. suffix "[\r\n]"))
  1620. (re-unibyte
  1621. (concat
  1622. "[\r\n]" prefix
  1623. "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
  1624. suffix "[\r\n]"))
  1625. (re-char-trans
  1626. (concat
  1627. "[\r\n]" prefix
  1628. "[ \t]*enable-character-translation[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
  1629. suffix "[\r\n]"))
  1630. (re-end
  1631. (concat "[\r\n]" prefix "[ \t]*End *:[ \t]*" suffix
  1632. "[\r\n]?"))
  1633. (pos (1- (point))))
  1634. (forward-char -1) ; skip back \r or \n.
  1635. (re-search-forward re-end tail-end 'move)
  1636. (setq tail-end (point))
  1637. (goto-char pos)
  1638. (when (and set-auto-coding-for-load
  1639. (re-search-forward re-unibyte tail-end t))
  1640. (setq coding-system 'raw-text))
  1641. (when (and (not coding-system)
  1642. (re-search-forward re-coding tail-end t))
  1643. (setq coding-system (intern (match-string 1))))
  1644. (when (and (not char-trans)
  1645. (re-search-forward re-char-trans tail-end t))
  1646. (setq char-trans (match-string 1))))))
  1647. (if coding-system
  1648. ;; If the coding-system name ends with "!", remove it and
  1649. ;; set char-trans to "nil".
  1650. (let ((name (symbol-name coding-system)))
  1651. (if (= (aref name (1- (length name))) ?!)
  1652. (setq coding-system (intern (substring name 0 -1))
  1653. char-trans "nil"))))
  1654. (when (and char-trans
  1655. (not (setq char-trans (intern char-trans))))
  1656. (make-local-variable 'enable-character-translation)
  1657. (setq enable-character-translation nil))
  1658. (if coding-system
  1659. (cons coding-system :coding)))
  1660. ;; Finally, try all the `auto-coding-functions'.
  1661. (let ((funcs auto-coding-functions)
  1662. (coding-system nil))
  1663. (while (and funcs (not coding-system))
  1664. (setq coding-system (condition-case e
  1665. (save-excursion
  1666. (goto-char (point-min))
  1667. (funcall (pop funcs) size))
  1668. (error nil))))
  1669. (if coding-system
  1670. (cons coding-system 'auto-coding-functions)))))
  1671. (defun set-auto-coding (filename size)
  1672. "Return coding system for a file FILENAME of which SIZE bytes follow point.
  1673. See `find-auto-coding' for how the coding system is found.
  1674. Return nil if an invalid coding system is found.
  1675. The variable `set-auto-coding-function' (which see) is set to this
  1676. function by default."
  1677. (let ((found (find-auto-coding filename size)))
  1678. (if (and found (coding-system-p (car found)))
  1679. (car found))))
  1680. (setq set-auto-coding-function 'set-auto-coding)
  1681. (defun after-insert-file-set-coding (inserted &optional visit)
  1682. "Set `buffer-file-coding-system' of current buffer after text is inserted.
  1683. INSERTED is the number of characters that were inserted, as figured
  1684. in the situation before this function. Return the number of characters
  1685. inserted, as figured in the situation after. The two numbers can be
  1686. different if the buffer has become unibyte.
  1687. The optional second arg VISIT non-nil means that we are visiting a file."
  1688. (if (and visit
  1689. coding-system-for-read
  1690. (not (eq coding-system-for-read 'auto-save-coding)))
  1691. (setq buffer-file-coding-system-explicit
  1692. (cons coding-system-for-read nil)))
  1693. (if last-coding-system-used
  1694. (let ((coding-system
  1695. (find-new-buffer-file-coding-system last-coding-system-used)))
  1696. (if coding-system
  1697. (setq buffer-file-coding-system coding-system))))
  1698. inserted)
  1699. ;; The coding-spec and eol-type of coding-system returned is decided
  1700. ;; independently in the following order.
  1701. ;; 1. That of buffer-file-coding-system locally bound.
  1702. ;; 2. That of CODING.
  1703. (defun find-new-buffer-file-coding-system (coding)
  1704. "Return a coding system for a buffer when a file of CODING is inserted.
  1705. The local variable `buffer-file-coding-system' of the current buffer
  1706. is set to the returned value.
  1707. Return nil if there's no need to set `buffer-file-coding-system'."
  1708. (let (local-coding local-eol
  1709. found-coding found-eol
  1710. new-coding new-eol)
  1711. (if (null coding)
  1712. ;; Nothing found about coding.
  1713. nil
  1714. ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
  1715. ;; and LOCAL-CODING.
  1716. (setq local-eol (coding-system-eol-type buffer-file-coding-system))
  1717. (if (null (numberp local-eol))
  1718. ;; But eol-type is not yet set.
  1719. (setq local-eol nil))
  1720. (if (and buffer-file-coding-system
  1721. (not (eq (coding-system-type buffer-file-coding-system)
  1722. 'undecided)))
  1723. (setq local-coding (coding-system-base buffer-file-coding-system)))
  1724. (if (and (local-variable-p 'buffer-file-coding-system)
  1725. local-eol local-coding)
  1726. ;; The current buffer has already set full coding-system, we
  1727. ;; had better not change it.
  1728. nil
  1729. (setq found-eol (coding-system-eol-type coding))
  1730. (if (null (numberp found-eol))
  1731. ;; But eol-type is not found.
  1732. ;; If EOL conversions are inhibited, force unix eol-type.
  1733. (setq found-eol (if inhibit-eol-conversion 0)))
  1734. (setq found-coding (coding-system-base coding))
  1735. (if (and (not found-eol) (eq found-coding 'undecided))
  1736. ;; No valid coding information found.
  1737. nil
  1738. ;; Some coding information (eol or text) found.
  1739. ;; The local setting takes precedence over the found one.
  1740. (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
  1741. (or local-coding found-coding)
  1742. (or found-coding local-coding)))
  1743. (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
  1744. (or local-eol found-eol)
  1745. (or found-eol local-eol)))
  1746. (let ((eol-type (coding-system-eol-type new-coding)))
  1747. (if (and (numberp new-eol) (vectorp eol-type))
  1748. (aref eol-type new-eol)
  1749. new-coding)))))))
  1750. (defun modify-coding-system-alist (target-type regexp coding-system)
  1751. "Modify one of look up tables for finding a coding system on I/O operation.
  1752. There are three of such tables, `file-coding-system-alist',
  1753. `process-coding-system-alist', and `network-coding-system-alist'.
  1754. TARGET-TYPE specifies which of them to modify.
  1755. If it is `file', it affects `file-coding-system-alist' (which see).
  1756. If it is `process', it affects `process-coding-system-alist' (which see).
  1757. If it is `network', it affects `network-coding-system-alist' (which see).
  1758. REGEXP is a regular expression matching a target of I/O operation.
  1759. The target is a file name if TARGET-TYPE is `file', a program name if
  1760. TARGET-TYPE is `process', or a network service name or a port number
  1761. to connect to if TARGET-TYPE is `network'.
  1762. CODING-SYSTEM is a coding system to perform code conversion on the I/O
  1763. operation, or a cons cell (DECODING . ENCODING) specifying the coding
  1764. systems for decoding and encoding respectively, or a function symbol
  1765. which, when called, returns such a cons cell."
  1766. (or (memq target-type '(file process network))
  1767. (error "Invalid target type: %s" target-type))
  1768. (or (stringp regexp)
  1769. (and (eq target-type 'network) (integerp regexp))
  1770. (error "Invalid regular expression: %s" regexp))
  1771. (if (symbolp coding-system)
  1772. (if (not (fboundp coding-system))
  1773. (progn
  1774. (check-coding-system coding-system)
  1775. (setq coding-system (cons coding-system coding-system))))
  1776. (check-coding-system (car coding-system))
  1777. (check-coding-system (cdr coding-system)))
  1778. (cond ((eq target-type 'file)
  1779. (let ((slot (assoc regexp file-coding-system-alist)))
  1780. (if slot
  1781. (setcdr slot coding-system)
  1782. (setq file-coding-system-alist
  1783. (cons (cons regexp coding-system)
  1784. file-coding-system-alist)))))
  1785. ((eq target-type 'process)
  1786. (let ((slot (assoc regexp process-coding-system-alist)))
  1787. (if slot
  1788. (setcdr slot coding-system)
  1789. (setq process-coding-system-alist
  1790. (cons (cons regexp coding-system)
  1791. process-coding-system-alist)))))
  1792. (t
  1793. (let ((slot (assoc regexp network-coding-system-alist)))
  1794. (if slot
  1795. (setcdr slot coding-system)
  1796. (setq network-coding-system-alist
  1797. (cons (cons regexp coding-system)
  1798. network-coding-system-alist)))))))
  1799. (defun decode-coding-inserted-region (from to filename
  1800. &optional visit beg end replace)
  1801. "Decode the region between FROM and TO as if it is read from file FILENAME.
  1802. The idea is that the text between FROM and TO was just inserted somehow.
  1803. Optional arguments VISIT, BEG, END, and REPLACE are the same as those
  1804. of the function `insert-file-contents'.
  1805. Part of the job of this function is setting `buffer-undo-list' appropriately."
  1806. (save-excursion
  1807. (save-restriction
  1808. (let ((coding coding-system-for-read)
  1809. undo-list-saved)
  1810. (if visit
  1811. ;; Temporarily turn off undo recording, if we're decoding the
  1812. ;; text of a visited file.
  1813. (setq buffer-undo-list t)
  1814. ;; Otherwise, if we can recognize the undo elt for the insertion,
  1815. ;; remove it and get ready to replace it later.
  1816. ;; In the mean time, turn off undo recording.
  1817. (let ((last (car-safe buffer-undo-list)))
  1818. (if (and (consp last) (eql (car last) from) (eql (cdr last) to))
  1819. (setq undo-list-saved (cdr buffer-undo-list)
  1820. buffer-undo-list t))))
  1821. (narrow-to-region from to)
  1822. (goto-char (point-min))
  1823. (or coding
  1824. (setq coding (funcall set-auto-coding-function
  1825. filename (- (point-max) (point-min)))))
  1826. (or coding
  1827. (setq coding (car (find-operation-coding-system
  1828. 'insert-file-contents
  1829. (cons filename (current-buffer))
  1830. visit beg end replace))))
  1831. (if (coding-system-p coding)
  1832. (or enable-multibyte-characters
  1833. (setq coding
  1834. (coding-system-change-text-conversion coding 'raw-text)))
  1835. (setq coding nil))
  1836. (if coding
  1837. (decode-coding-region (point-min) (point-max) coding)
  1838. (setq last-coding-system-used coding))
  1839. ;; If we're decoding the text of a visited file,
  1840. ;; the undo list should start out empty.
  1841. (if visit
  1842. (setq buffer-undo-list nil)
  1843. ;; If we decided to replace the undo entry for the insertion,
  1844. ;; do so now.
  1845. (if undo-list-saved
  1846. (setq buffer-undo-list
  1847. (cons (cons from (point-max)) undo-list-saved))))))))
  1848. (defun recode-region (start end new-coding coding)
  1849. "Re-decode the region (previously decoded by CODING) by NEW-CODING."
  1850. (interactive
  1851. (list (region-beginning) (region-end)
  1852. (read-coding-system "Text was really in: ")
  1853. (let ((coding (or buffer-file-coding-system last-coding-system-used)))
  1854. (read-coding-system
  1855. (concat "But was interpreted as"
  1856. (if coding (format " (default %S): " coding) ": "))
  1857. coding))))
  1858. (or (and new-coding coding)
  1859. (error "Coding system not specified"))
  1860. ;; Check it before we encode the region.
  1861. (check-coding-system new-coding)
  1862. (save-restriction
  1863. (narrow-to-region start end)
  1864. (encode-coding-region (point-min) (point-max) coding)
  1865. (decode-coding-region (point-min) (point-max) new-coding))
  1866. (if (region-active-p)
  1867. (deactivate-mark)))
  1868. (defun make-translation-table (&rest args)
  1869. "Make a translation table from arguments.
  1870. A translation table is a char table intended for character
  1871. translation in CCL programs.
  1872. Each argument is a list of elements of the form (FROM . TO), where FROM
  1873. is a character to be translated to TO.
  1874. The arguments and forms in each argument are processed in the given
  1875. order, and if a previous form already translates TO to some other
  1876. character, say TO-ALT, FROM is also translated to TO-ALT."
  1877. (let ((table (make-char-table 'translation-table))
  1878. revlist)
  1879. (dolist (elts args)
  1880. (dolist (elt elts)
  1881. (let ((from (car elt))
  1882. (to (cdr elt))
  1883. to-alt rev-from rev-to)
  1884. ;; If we have already translated TO to TO-ALT, FROM should
  1885. ;; also be translated to TO-ALT.
  1886. (if (setq to-alt (aref table to))
  1887. (setq to to-alt))
  1888. (aset table from to)
  1889. ;; If we have already translated some chars to FROM, they
  1890. ;; should also be translated to TO.
  1891. (when (setq rev-from (assq from revlist))
  1892. (dolist (elt (cdr rev-from))
  1893. (aset table elt to))
  1894. (setq revlist (delq rev-from revlist)
  1895. rev-from (cdr rev-from)))
  1896. ;; Now update REVLIST.
  1897. (setq rev-to (assq to revlist))
  1898. (if rev-to
  1899. (setcdr rev-to (cons from (cdr rev-to)))
  1900. (setq rev-to (list to from)
  1901. revlist (cons rev-to revlist)))
  1902. (if rev-from
  1903. (setcdr rev-to (append rev-from (cdr rev-to)))))))
  1904. ;; Return TABLE just created.
  1905. (set-char-table-extra-slot table 1 1)
  1906. table))
  1907. (defun make-translation-table-from-vector (vec)
  1908. "Make translation table from decoding vector VEC.
  1909. VEC is an array of 256 elements to map unibyte codes to multibyte
  1910. characters. Elements may be nil for undefined code points."
  1911. (let ((table (make-char-table 'translation-table))
  1912. (rev-table (make-char-table 'translation-table))
  1913. ch)
  1914. (dotimes (i 256)
  1915. (setq ch (aref vec i))
  1916. (when ch
  1917. (aset table i ch)
  1918. (if (>= ch 256)
  1919. (aset rev-table ch i))))
  1920. (set-char-table-extra-slot table 0 rev-table)
  1921. (set-char-table-extra-slot table 1 1)
  1922. (set-char-table-extra-slot rev-table 1 1)
  1923. table))
  1924. (defun make-translation-table-from-alist (alist)
  1925. "Make translation table from N<->M mapping in ALIST.
  1926. ALIST is an alist, each element has the form (FROM . TO).
  1927. FROM and TO are a character or a vector of characters.
  1928. If FROM is a character, that character is translated to TO.
  1929. If FROM is a vector of characters, that sequence is translated to TO.
  1930. The first extra-slot of the value is a translation table for reverse mapping."
  1931. (let ((tables (vector (make-char-table 'translation-table)
  1932. (make-char-table 'translation-table)))
  1933. table max-lookup from to idx val)
  1934. (dotimes (i 2)
  1935. (setq table (aref tables i))
  1936. (setq max-lookup 1)
  1937. (dolist (elt alist)
  1938. (if (= i 0)
  1939. (setq from (car elt) to (cdr elt))
  1940. (setq from (cdr elt) to (car elt)))
  1941. (if (characterp from)
  1942. (setq idx from)
  1943. (setq idx (aref from 0)
  1944. max-lookup (max max-lookup (length from))))
  1945. (setq val (aref table idx))
  1946. (if val
  1947. (progn
  1948. (or (consp val)
  1949. (setq val (list (cons (vector idx) val))))
  1950. (if (characterp from)
  1951. (setq from (vector from)))
  1952. (setq val (nconc val (list (cons from to)))))
  1953. (if (characterp from)
  1954. (setq val to)
  1955. (setq val (list (cons from to)))))
  1956. (aset table idx val))
  1957. (set-char-table-extra-slot table 1 max-lookup))
  1958. (set-char-table-extra-slot (aref tables 0) 0 (aref tables 1))
  1959. (aref tables 0)))
  1960. (defun define-translation-table (symbol &rest args)
  1961. "Define SYMBOL as the name of translation table made by ARGS.
  1962. This sets up information so that the table can be used for
  1963. translations in a CCL program.
  1964. If the first element of ARGS is a char-table whose purpose is
  1965. `translation-table', just define SYMBOL to name it. (Note that this
  1966. function does not bind SYMBOL.)
  1967. Any other ARGS should be suitable as arguments of the function
  1968. `make-translation-table' (which see).
  1969. This function sets properties `translation-table' and
  1970. `translation-table-id' of SYMBOL to the created table itself and the
  1971. identification number of the table respectively. It also registers
  1972. the table in `translation-table-vector'."
  1973. (let ((table (if (and (char-table-p (car args))
  1974. (eq (char-table-subtype (car args))
  1975. 'translation-table))
  1976. (car args)
  1977. (apply 'make-translation-table args)))
  1978. (len (length translation-table-vector))
  1979. (id 0)
  1980. (done nil))
  1981. (put symbol 'translation-table table)
  1982. (while (not done)
  1983. (if (>= id len)
  1984. (setq translation-table-vector
  1985. (vconcat translation-table-vector (make-vector len nil))))
  1986. (let ((slot (aref translation-table-vector id)))
  1987. (if (or (not slot)
  1988. (eq (car slot) symbol))
  1989. (progn
  1990. (aset translation-table-vector id (cons symbol table))
  1991. (setq done t))
  1992. (setq id (1+ id)))))
  1993. (put symbol 'translation-table-id id)
  1994. id))
  1995. (defun translate-region (start end table)
  1996. "From START to END, translate characters according to TABLE.
  1997. TABLE is a string or a char-table.
  1998. If TABLE is a string, the Nth character in it is the mapping
  1999. for the character with code N.
  2000. If TABLE is a char-table, the element for character N is the mapping
  2001. for the character with code N.
  2002. It returns the number of characters changed."
  2003. (interactive
  2004. (list (region-beginning)
  2005. (region-end)
  2006. (let (table l)
  2007. (dotimes (i (length translation-table-vector))
  2008. (if (consp (aref translation-table-vector i))
  2009. (push (list (symbol-name
  2010. (car (aref translation-table-vector i)))) l)))
  2011. (if (not l)
  2012. (error "No translation table defined"))
  2013. (while (not table)
  2014. (setq table (completing-read "Translation table: " l nil t)))
  2015. (intern table))))
  2016. (if (symbolp table)
  2017. (let ((val (get table 'translation-table)))
  2018. (or (char-table-p val)
  2019. (error "Invalid translation table name: %s" table))
  2020. (setq table val)))
  2021. (translate-region-internal start end table))
  2022. (defmacro with-category-table (table &rest body)
  2023. "Execute BODY like `progn' with TABLE the current category table.
  2024. The category table of the current buffer is saved, BODY is evaluated,
  2025. then the saved table is restored, even in case of an abnormal exit.
  2026. Value is what BODY returns."
  2027. (declare (indent 1) (debug t))
  2028. (let ((old-table (make-symbol "old-table"))
  2029. (old-buffer (make-symbol "old-buffer")))
  2030. `(let ((,old-table (category-table))
  2031. (,old-buffer (current-buffer)))
  2032. (unwind-protect
  2033. (progn
  2034. (set-category-table ,table)
  2035. ,@body)
  2036. (with-current-buffer ,old-buffer
  2037. (set-category-table ,old-table))))))
  2038. (defun define-translation-hash-table (symbol table)
  2039. "Define SYMBOL as the name of the hash translation TABLE for use in CCL.
  2040. Analogous to `define-translation-table', but updates
  2041. `translation-hash-table-vector' and the table is for use in the CCL
  2042. `lookup-integer' and `lookup-character' functions."
  2043. (unless (and (symbolp symbol)
  2044. (hash-table-p table))
  2045. (error "Bad args to define-translation-hash-table"))
  2046. (let ((len (length translation-hash-table-vector))
  2047. (id 0)
  2048. done)
  2049. (put symbol 'translation-hash-table table)
  2050. (while (not done)
  2051. (if (>= id len)
  2052. (setq translation-hash-table-vector
  2053. (vconcat translation-hash-table-vector [nil])))
  2054. (let ((slot (aref translation-hash-table-vector id)))
  2055. (if (or (not slot)
  2056. (eq (car slot) symbol))
  2057. (progn
  2058. (aset translation-hash-table-vector id (cons symbol table))
  2059. (setq done t))
  2060. (setq id (1+ id)))))
  2061. (put symbol 'translation-hash-table-id id)
  2062. id))
  2063. ;;; Initialize some variables.
  2064. (put 'use-default-ascent 'char-table-extra-slots 0)
  2065. (setq use-default-ascent (make-char-table 'use-default-ascent))
  2066. (put 'ignore-relative-composition 'char-table-extra-slots 0)
  2067. (setq ignore-relative-composition
  2068. (make-char-table 'ignore-relative-composition))
  2069. (make-obsolete 'set-char-table-default
  2070. "generic characters no longer exist." "23.1")
  2071. ;;; Built-in auto-coding-functions:
  2072. (defun sgml-xml-auto-coding-function (size)
  2073. "Determine whether the buffer is XML, and if so, its encoding.
  2074. This function is intended to be added to `auto-coding-functions'."
  2075. (setq size (+ (point) size))
  2076. (when (re-search-forward "\\`[[:space:]\n]*<\\?xml" size t)
  2077. (let ((end (save-excursion
  2078. ;; This is a hack.
  2079. (re-search-forward "[\"']\\s-*\\?>" size t))))
  2080. (when end
  2081. (if (re-search-forward "encoding=[\"']\\(.+?\\)[\"']" end t)
  2082. (let* ((match (match-string 1))
  2083. (sym (intern (downcase match))))
  2084. (if (coding-system-p sym)
  2085. sym
  2086. (message "Warning: unknown coding system \"%s\"" match)
  2087. nil))
  2088. ;; Files without an encoding tag should be UTF-8. But users
  2089. ;; may be naive about encodings, and have saved the file from
  2090. ;; another editor that does not help them get the encoding right.
  2091. ;; Detect the encoding and warn the user if it is detected as
  2092. ;; something other than UTF-8.
  2093. (let ((detected
  2094. (with-coding-priority '(utf-8)
  2095. (coding-system-base
  2096. (detect-coding-region (point-min) size t)))))
  2097. ;; Pure ASCII always comes back as undecided.
  2098. (if (memq detected '(utf-8 undecided))
  2099. 'utf-8
  2100. (warn "File contents detected as %s.
  2101. Consider adding an encoding attribute to the xml declaration,
  2102. or saving as utf-8, as mandated by the xml specification." detected)
  2103. detected)))))))
  2104. (defun sgml-html-meta-auto-coding-function (size)
  2105. "If the buffer has an HTML meta tag, use it to determine encoding.
  2106. This function is intended to be added to `auto-coding-functions'."
  2107. (let ((case-fold-search t))
  2108. (setq size (min (+ (point) size)
  2109. (save-excursion
  2110. ;; Limit the search by the end of the HTML header.
  2111. (or (search-forward "</head>" (+ (point) size) t)
  2112. ;; In case of no header, search only 10 lines.
  2113. (forward-line 10))
  2114. (point))))
  2115. ;; Make sure that the buffer really contains an HTML document, by
  2116. ;; checking that it starts with a doctype or a <HTML> start tag
  2117. ;; (allowing for whitespace at bob). Note: 'DOCTYPE NETSCAPE' is
  2118. ;; useful for Mozilla bookmark files.
  2119. (when (and (re-search-forward "\\`[[:space:]\n]*\\(<!doctype[[:space:]\n]+\\(html\\|netscape\\)\\|<html\\)" size t)
  2120. (re-search-forward "<meta\\s-+\\(http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']text/\\sw+;\\s-*\\)?charset=[\"']?\\(.+?\\)[\"'\\s-/>]" size t))
  2121. (let* ((match (match-string 2))
  2122. (sym (intern (downcase match))))
  2123. (if (coding-system-p sym)
  2124. sym
  2125. (message "Warning: unknown coding system \"%s\"" match)
  2126. nil)))))
  2127. (defun xml-find-file-coding-system (args)
  2128. "Determine the coding system of an XML file without a declaration.
  2129. Strictly speaking, the file should be utf-8, but mistakes are
  2130. made, and there are genuine cases where XML fragments are saved,
  2131. with the encoding properly specified in a master document, or
  2132. added by processing software."
  2133. (if (eq (car args) 'insert-file-contents)
  2134. (let ((detected
  2135. (with-coding-priority '(utf-8)
  2136. (coding-system-base
  2137. (detect-coding-region (point-min) (point-max) t)))))
  2138. ;; Pure ASCII always comes back as undecided.
  2139. (cond
  2140. ((memq detected '(utf-8 undecided))
  2141. 'utf-8)
  2142. ((eq detected 'utf-16le-with-signature) 'utf-16le-with-signature)
  2143. ((eq detected 'utf-16be-with-signature) 'utf-16be-with-signature)
  2144. (t
  2145. (warn "File contents detected as %s.
  2146. Consider adding an xml declaration with the encoding specified,
  2147. or saving as utf-8, as mandated by the xml specification." detected)
  2148. detected)))
  2149. ;; Don't interfere with the user's wishes for saving the buffer.
  2150. ;; We did what we could when the buffer was created to ensure the
  2151. ;; correct encoding was used, or the user was warned, so any
  2152. ;; non-conformity here is deliberate on the part of the user.
  2153. 'undecided))
  2154. ;;;
  2155. (provide 'mule)
  2156. ;;; mule.el ends here