mule.el 92 KB

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