mule-cmds.el 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. ;;; mule-cmds.el --- commands for multilingual environment -*-coding: iso-2022-7bit -*-
  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, i18n
  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. (eval-when-compile (require 'cl)) ; letf
  25. (defvar dos-codepage)
  26. (autoload 'widget-value "wid-edit")
  27. (defvar mac-system-coding-system)
  28. ;;; MULE related key bindings and menus.
  29. (defvar mule-keymap
  30. (let ((map (make-sparse-keymap)))
  31. (define-key map "f" 'set-buffer-file-coding-system)
  32. (define-key map "r" 'revert-buffer-with-coding-system)
  33. (define-key map "F" 'set-file-name-coding-system)
  34. (define-key map "t" 'set-terminal-coding-system)
  35. (define-key map "k" 'set-keyboard-coding-system)
  36. (define-key map "p" 'set-buffer-process-coding-system)
  37. (define-key map "x" 'set-selection-coding-system)
  38. (define-key map "X" 'set-next-selection-coding-system)
  39. (define-key map "\C-\\" 'set-input-method)
  40. (define-key map "c" 'universal-coding-system-argument)
  41. (define-key map "l" 'set-language-environment)
  42. map)
  43. "Keymap for Mule (Multilingual environment) specific commands.")
  44. ;; Keep "C-x C-m ..." for mule specific commands.
  45. (define-key ctl-x-map "\C-m" mule-keymap)
  46. (defvar describe-language-environment-map
  47. (let ((map (make-sparse-keymap "Describe Language Environment")))
  48. (define-key map
  49. [Default] `(menu-item ,(purecopy "Default") describe-specified-language-support))
  50. map))
  51. (defvar setup-language-environment-map
  52. (let ((map (make-sparse-keymap "Set Language Environment")))
  53. (define-key map
  54. [Default] `(menu-item ,(purecopy "Default") setup-specified-language-environment))
  55. map))
  56. (defvar set-coding-system-map
  57. (let ((map (make-sparse-keymap "Set Coding System")))
  58. (define-key-after map [universal-coding-system-argument]
  59. `(menu-item ,(purecopy "For Next Command") universal-coding-system-argument
  60. :help ,(purecopy "Coding system to be used by next command")))
  61. (define-key-after map [separator-1] menu-bar-separator)
  62. (define-key-after map [set-buffer-file-coding-system]
  63. `(menu-item ,(purecopy "For Saving This Buffer") set-buffer-file-coding-system
  64. :help ,(purecopy "How to encode this buffer when saved")))
  65. (define-key-after map [revert-buffer-with-coding-system]
  66. `(menu-item ,(purecopy "For Reverting This File Now")
  67. revert-buffer-with-coding-system
  68. :enable buffer-file-name
  69. :help ,(purecopy "Revisit this file immediately using specified coding system")))
  70. (define-key-after map [set-file-name-coding-system]
  71. `(menu-item ,(purecopy "For File Name") set-file-name-coding-system
  72. :help ,(purecopy "How to decode/encode file names")))
  73. (define-key-after map [separator-2] menu-bar-separator)
  74. (define-key-after map [set-keyboard-coding-system]
  75. `(menu-item ,(purecopy "For Keyboard") set-keyboard-coding-system
  76. :help ,(purecopy "How to decode keyboard input")))
  77. (define-key-after map [set-terminal-coding-system]
  78. `(menu-item ,(purecopy "For Terminal") set-terminal-coding-system
  79. :enable (null (memq initial-window-system '(x w32 ns)))
  80. :help ,(purecopy "How to encode terminal output")))
  81. (define-key-after map [separator-3] menu-bar-separator)
  82. (define-key-after map [set-selection-coding-system]
  83. `(menu-item ,(purecopy "For X Selections/Clipboard") set-selection-coding-system
  84. :visible (display-selections-p)
  85. :help ,(purecopy "How to en/decode data to/from selection/clipboard")))
  86. (define-key-after map [set-next-selection-coding-system]
  87. `(menu-item ,(purecopy "For Next X Selection") set-next-selection-coding-system
  88. :visible (display-selections-p)
  89. :help ,(purecopy "How to en/decode next selection/clipboard operation")))
  90. (define-key-after map [set-buffer-process-coding-system]
  91. `(menu-item ,(purecopy "For I/O with Subprocess") set-buffer-process-coding-system
  92. :visible (fboundp 'start-process)
  93. :enable (get-buffer-process (current-buffer))
  94. :help ,(purecopy "How to en/decode I/O from/to subprocess connected to this buffer")))
  95. map))
  96. (defvar mule-menu-keymap
  97. (let ((map (make-sparse-keymap "Mule (Multilingual Environment)")))
  98. (define-key-after map [set-language-environment]
  99. `(menu-item ,(purecopy "Set Language Environment") ,setup-language-environment-map))
  100. (define-key-after map [separator-mule] menu-bar-separator)
  101. (define-key-after map [toggle-input-method]
  102. `(menu-item ,(purecopy "Toggle Input Method") toggle-input-method))
  103. (define-key-after map [set-input-method]
  104. `(menu-item ,(purecopy "Select Input Method...") set-input-method))
  105. (define-key-after map [describe-input-method]
  106. `(menu-item ,(purecopy "Describe Input Method") describe-input-method))
  107. (define-key-after map [separator-input-method] menu-bar-separator)
  108. (define-key-after map [set-various-coding-system]
  109. `(menu-item ,(purecopy "Set Coding Systems") ,set-coding-system-map
  110. :enable (default-value 'enable-multibyte-characters)))
  111. (define-key-after map [view-hello-file]
  112. `(menu-item ,(purecopy "Show Multilingual Sample Text") view-hello-file
  113. :enable (file-readable-p
  114. (expand-file-name "HELLO" data-directory))
  115. :help ,(purecopy "Demonstrate various character sets")))
  116. (define-key-after map [separator-coding-system] menu-bar-separator)
  117. (define-key-after map [describe-language-environment]
  118. `(menu-item ,(purecopy "Describe Language Environment")
  119. ,describe-language-environment-map
  120. :help ,(purecopy "Show multilingual settings for a specific language")))
  121. (define-key-after map [describe-input-method]
  122. `(menu-item ,(purecopy "Describe Input Method...") describe-input-method
  123. :help ,(purecopy "Keyboard layout for a specific input method")))
  124. (define-key-after map [describe-coding-system]
  125. `(menu-item ,(purecopy "Describe Coding System...") describe-coding-system))
  126. (define-key-after map [list-character-sets]
  127. `(menu-item ,(purecopy "List Character Sets") list-character-sets
  128. :help ,(purecopy "Show table of available character sets")))
  129. (define-key-after map [mule-diag]
  130. `(menu-item ,(purecopy "Show All Multilingual Settings") mule-diag
  131. :help ,(purecopy "Display multilingual environment settings")))
  132. map)
  133. "Keymap for Mule (Multilingual environment) menu specific commands.")
  134. ;; This should be a single character key binding because users use it
  135. ;; very frequently while editing multilingual text. Now we can use
  136. ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
  137. ;; convenient because it requires shifting on most keyboards. An
  138. ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
  139. ;; but it won't be used that frequently.
  140. (define-key global-map "\C-\\" 'toggle-input-method)
  141. ;; This is no good because people often type Shift-SPC
  142. ;; meaning to type SPC. -- rms.
  143. ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
  144. ;; (define-key global-map [?\S- ] 'toggle-input-method)
  145. ;;; Mule related hyperlinks.
  146. (defconst help-xref-mule-regexp-template
  147. (purecopy (concat "\\(\\<\\("
  148. "\\(coding system\\)\\|"
  149. "\\(input method\\)\\|"
  150. "\\(character set\\)\\|"
  151. "\\(charset\\)"
  152. "\\)\\s-+\\)?"
  153. ;; Note starting with word-syntax character:
  154. "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
  155. (defun coding-system-change-eol-conversion (coding-system eol-type)
  156. "Return a coding system which differs from CODING-SYSTEM in EOL conversion.
  157. The returned coding system converts end-of-line by EOL-TYPE
  158. but text as the same way as CODING-SYSTEM.
  159. EOL-TYPE should be `unix', `dos', `mac', or nil.
  160. If EOL-TYPE is nil, the returned coding system detects
  161. how end-of-line is formatted automatically while decoding.
  162. EOL-TYPE can be specified by an integer 0, 1, or 2.
  163. They means `unix', `dos', and `mac' respectively."
  164. (if (symbolp eol-type)
  165. (setq eol-type (cond ((eq eol-type 'unix) 0)
  166. ((eq eol-type 'dos) 1)
  167. ((eq eol-type 'mac) 2)
  168. (t eol-type))))
  169. ;; We call `coding-system-base' before `coding-system-eol-type',
  170. ;; because the coding-system may not be initialized until then.
  171. (let* ((base (coding-system-base coding-system))
  172. (orig-eol-type (coding-system-eol-type coding-system)))
  173. (cond ((vectorp orig-eol-type)
  174. (if (not eol-type)
  175. coding-system
  176. (aref orig-eol-type eol-type)))
  177. ((not eol-type)
  178. base)
  179. ((= eol-type orig-eol-type)
  180. coding-system)
  181. ((progn (setq orig-eol-type (coding-system-eol-type base))
  182. (vectorp orig-eol-type))
  183. (aref orig-eol-type eol-type)))))
  184. (defun coding-system-change-text-conversion (coding-system coding)
  185. "Return a coding system which differs from CODING-SYSTEM in text conversion.
  186. The returned coding system converts text by CODING
  187. but end-of-line as the same way as CODING-SYSTEM.
  188. If CODING is nil, the returned coding system detects
  189. how text is formatted automatically while decoding."
  190. (let ((eol-type (coding-system-eol-type coding-system)))
  191. (coding-system-change-eol-conversion
  192. (if coding coding 'undecided)
  193. (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
  194. ;; Canonicalize the coding system name NAME by removing some prefixes
  195. ;; and delimiter characters. Support function of
  196. ;; coding-system-from-name.
  197. (defun canonicalize-coding-system-name (name)
  198. (if (string-match "^\\(ms\\|ibm\\|windows-\\)\\([0-9]+\\)$" name)
  199. ;; "ms950", "ibm950", "windows-950" -> "cp950"
  200. (concat "cp" (match-string 2 name))
  201. (if (string-match "^iso[-_ ]?[0-9]" name)
  202. ;; "iso-8859-1" -> "8859-1", "iso-2022-jp" ->"2022-jp"
  203. (setq name (substring name (1- (match-end 0)))))
  204. (let ((idx (string-match "[-_ /]" name)))
  205. ;; Delete "-", "_", " ", "/" but do distinguish "16-be" and "16be".
  206. (while idx
  207. (if (and (>= idx 2)
  208. (eq (string-match "16-[lb]e$" name (- idx 2))
  209. (- idx 2)))
  210. (setq idx (string-match "[-_ /]" name (match-end 0)))
  211. (setq name (concat (substring name 0 idx) (substring name (1+ idx)))
  212. idx (string-match "[-_ /]" name idx))))
  213. name)))
  214. (defun coding-system-from-name (name)
  215. "Return a coding system whose name matches with NAME (string or symbol)."
  216. (let (sym)
  217. (if (stringp name) (setq sym (intern name))
  218. (setq sym name name (symbol-name name)))
  219. (if (coding-system-p sym)
  220. sym
  221. (let ((eol-type
  222. (if (string-match "-\\(unix\\|dos\\|mac\\)$" name)
  223. (prog1 (intern (match-string 1 name))
  224. (setq name (substring name 0 (match-beginning 0)))))))
  225. (setq name (canonicalize-coding-system-name (downcase name)))
  226. (catch 'tag
  227. (dolist (elt (coding-system-list))
  228. (if (string= (canonicalize-coding-system-name (symbol-name elt))
  229. name)
  230. (throw 'tag (if eol-type (coding-system-change-eol-conversion
  231. elt eol-type)
  232. elt)))))))))
  233. (defun toggle-enable-multibyte-characters (&optional arg)
  234. "Change whether this buffer uses multibyte characters.
  235. With ARG, use multibyte characters if the ARG is positive.
  236. Note that this command does not convert the byte contents of
  237. the buffer; it only changes the way those bytes are interpreted.
  238. In general, therefore, this command *changes* the sequence of
  239. characters that the current buffer contains.
  240. We suggest you avoid using this command unless you know what you are
  241. doing. If you use it by mistake, and the buffer is now displayed
  242. wrong, use this command again to toggle back to the right mode."
  243. (interactive "P")
  244. (let ((new-flag
  245. (if (null arg) (null enable-multibyte-characters)
  246. (> (prefix-numeric-value arg) 0))))
  247. (set-buffer-multibyte new-flag))
  248. (force-mode-line-update))
  249. (defun view-hello-file ()
  250. "Display the HELLO file, which lists many languages and characters."
  251. (interactive)
  252. ;; We have to decode the file in any environment.
  253. (letf ((coding-system-for-read 'iso-2022-7bit))
  254. (view-file (expand-file-name "HELLO" data-directory))))
  255. (defun universal-coding-system-argument (coding-system)
  256. "Execute an I/O command using the specified coding system."
  257. (interactive
  258. (let ((default (and buffer-file-coding-system
  259. (not (eq (coding-system-type buffer-file-coding-system)
  260. 'undecided))
  261. buffer-file-coding-system)))
  262. (list (read-coding-system
  263. (if default
  264. (format "Coding system for following command (default %s): " default)
  265. "Coding system for following command: ")
  266. default))))
  267. (let* ((keyseq (read-key-sequence
  268. (format "Command to execute with %s:" coding-system)))
  269. (cmd (key-binding keyseq))
  270. prefix)
  271. ;; read-key-sequence ignores quit, so make an explicit check.
  272. ;; Like many places, this assumes quit == C-g, but it need not be.
  273. (if (equal last-input-event ?\C-g)
  274. (keyboard-quit))
  275. (when (memq cmd '(universal-argument digit-argument))
  276. (call-interactively cmd)
  277. ;; Process keys bound in `universal-argument-map'.
  278. (while (progn
  279. (setq keyseq (read-key-sequence nil t)
  280. cmd (key-binding keyseq t))
  281. (not (eq cmd 'universal-argument-other-key)))
  282. (let ((current-prefix-arg prefix-arg)
  283. ;; Have to bind `last-command-event' here so that
  284. ;; `digit-argument', for instance, can compute the
  285. ;; prefix arg.
  286. (last-command-event (aref keyseq 0)))
  287. (call-interactively cmd)))
  288. ;; This is the final call to `universal-argument-other-key', which
  289. ;; set's the final `prefix-arg.
  290. (let ((current-prefix-arg prefix-arg))
  291. (call-interactively cmd))
  292. ;; Read the command to execute with the given prefix arg.
  293. (setq prefix prefix-arg
  294. keyseq (read-key-sequence nil t)
  295. cmd (key-binding keyseq)))
  296. (let ((coding-system-for-read coding-system)
  297. (coding-system-for-write coding-system)
  298. (coding-system-require-warning t)
  299. (current-prefix-arg prefix))
  300. (message "")
  301. (call-interactively cmd))))
  302. (defun set-default-coding-systems (coding-system)
  303. "Set default value of various coding systems to CODING-SYSTEM.
  304. This sets the following coding systems:
  305. o coding system of a newly created buffer
  306. o default coding system for subprocess I/O
  307. This also sets the following values:
  308. o default value used as `file-name-coding-system' for converting file names
  309. if CODING-SYSTEM is ASCII-compatible
  310. o default value for the command `set-terminal-coding-system'
  311. o default value for the command `set-keyboard-coding-system'
  312. if CODING-SYSTEM is ASCII-compatible"
  313. (check-coding-system coding-system)
  314. (setq-default buffer-file-coding-system coding-system)
  315. (if (fboundp 'ucs-set-table-for-input)
  316. (dolist (buffer (buffer-list))
  317. (or (local-variable-p 'buffer-file-coding-system buffer)
  318. (ucs-set-table-for-input buffer))))
  319. (if (eq system-type 'darwin)
  320. ;; The file-name coding system on Darwin systems is always utf-8.
  321. (setq default-file-name-coding-system 'utf-8)
  322. (if (and (default-value 'enable-multibyte-characters)
  323. (or (not coding-system)
  324. (coding-system-get coding-system 'ascii-compatible-p)))
  325. (setq default-file-name-coding-system coding-system)))
  326. (setq default-terminal-coding-system coding-system)
  327. ;; Prevent default-terminal-coding-system from converting ^M to ^J.
  328. (setq default-keyboard-coding-system
  329. (coding-system-change-eol-conversion coding-system 'unix))
  330. ;; Preserve eol-type from existing default-process-coding-systems.
  331. ;; On non-unix-like systems in particular, these may have been set
  332. ;; carefully by the user, or by the startup code, to deal with the
  333. ;; users shell appropriately, so should not be altered by changing
  334. ;; language environment.
  335. (let ((output-coding
  336. (coding-system-change-text-conversion
  337. (car default-process-coding-system) coding-system))
  338. (input-coding
  339. (coding-system-change-text-conversion
  340. (cdr default-process-coding-system) coding-system)))
  341. (setq default-process-coding-system
  342. (cons output-coding input-coding))))
  343. (defun prefer-coding-system (coding-system)
  344. "Add CODING-SYSTEM at the front of the priority list for automatic detection.
  345. This also sets the following coding systems:
  346. o coding system of a newly created buffer
  347. o default coding system for subprocess I/O
  348. This also sets the following values:
  349. o default value used as `file-name-coding-system' for converting file names
  350. o default value for the command `set-terminal-coding-system'
  351. o default value for the command `set-keyboard-coding-system'
  352. If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
  353. systems set by this function will use that type of EOL conversion.
  354. A coding system that requires automatic detection of text+encoding
  355. \(e.g. undecided, unix) can't be preferred.
  356. To prefer, for instance, utf-8, say the following:
  357. \(prefer-coding-system 'utf-8)"
  358. (interactive "zPrefer coding system: ")
  359. (if (not (and coding-system (coding-system-p coding-system)))
  360. (error "Invalid coding system `%s'" coding-system))
  361. (if (memq (coding-system-type coding-system) '(raw-text undecided))
  362. (error "Can't prefer the coding system `%s'" coding-system))
  363. (let ((base (coding-system-base coding-system))
  364. (eol-type (coding-system-eol-type coding-system)))
  365. (set-coding-system-priority base)
  366. (and (called-interactively-p 'interactive)
  367. (or (eq base coding-system)
  368. (message "Highest priority is set to %s (base of %s)"
  369. base coding-system)))
  370. ;; If they asked for specific EOL conversion, honor that.
  371. (if (memq eol-type '(0 1 2))
  372. (setq base
  373. (coding-system-change-eol-conversion base eol-type)))
  374. (set-default-coding-systems base)
  375. (if (called-interactively-p 'interactive)
  376. (or (eq base default-file-name-coding-system)
  377. (message "The default value of `file-name-coding-system' was not changed because the specified coding system is not suitable for file names.")))))
  378. (defvar sort-coding-systems-predicate nil
  379. "If non-nil, a predicate function to sort coding systems.
  380. It is called with two coding systems, and should return t if the first
  381. one is \"less\" than the second.
  382. The function `sort-coding-systems' use it.")
  383. (defun sort-coding-systems (codings)
  384. "Sort coding system list CODINGS by a priority of each coding system.
  385. Return the sorted list. CODINGS is modified by side effects.
  386. If a coding system is most preferred, it has the highest priority.
  387. Otherwise, coding systems that correspond to MIME charsets have
  388. higher priorities. Among them, a coding system included in the
  389. `coding-system' key of the current language environment has higher
  390. priority. See also the documentation of `language-info-alist'.
  391. If the variable `sort-coding-systems-predicate' (which see) is
  392. non-nil, it is used to sort CODINGS instead."
  393. (if sort-coding-systems-predicate
  394. (sort codings sort-coding-systems-predicate)
  395. (let* ((from-priority (coding-system-priority-list))
  396. (most-preferred (car from-priority))
  397. (lang-preferred (get-language-info current-language-environment
  398. 'coding-system))
  399. (func (function
  400. (lambda (x)
  401. (let ((base (coding-system-base x)))
  402. ;; We calculate the priority number 0..255 by
  403. ;; using the 8 bits PMMLCEII as this:
  404. ;; P: 1 if most preferred.
  405. ;; MM: greater than 0 if mime-charset.
  406. ;; L: 1 if one of the current lang. env.'s codings.
  407. ;; C: 1 if one of codings listed in the category list.
  408. ;; E: 1 if not XXX-with-esc
  409. ;; II: if iso-2022 based, 0..3, else 1.
  410. (logior
  411. (lsh (if (eq base most-preferred) 1 0) 7)
  412. (lsh
  413. (let ((mime (coding-system-get base :mime-charset)))
  414. ;; Prefer coding systems corresponding to a
  415. ;; MIME charset.
  416. (if mime
  417. ;; Lower utf-16 priority so that we
  418. ;; normally prefer utf-8 to it, and put
  419. ;; x-ctext below that.
  420. (cond ((string-match-p "utf-16"
  421. (symbol-name mime))
  422. 2)
  423. ((string-match-p "^x-" (symbol-name mime))
  424. 1)
  425. (t 3))
  426. 0))
  427. 5)
  428. (lsh (if (memq base lang-preferred) 1 0) 4)
  429. (lsh (if (memq base from-priority) 1 0) 3)
  430. (lsh (if (string-match-p "-with-esc\\'"
  431. (symbol-name base))
  432. 0 1) 2)
  433. (if (eq (coding-system-type base) 'iso-2022)
  434. (let ((category (coding-system-category base)))
  435. ;; For ISO based coding systems, prefer
  436. ;; one that doesn't use designation nor
  437. ;; locking/single shifting.
  438. (cond
  439. ((or (eq category 'coding-category-iso-8-1)
  440. (eq category 'coding-category-iso-8-2))
  441. 2)
  442. ((or (eq category 'coding-category-iso-7-tight)
  443. (eq category 'coding-category-iso-7))
  444. 1)
  445. (t
  446. 0)))
  447. 1)
  448. ))))))
  449. (sort codings (function (lambda (x y)
  450. (> (funcall func x) (funcall func y))))))))
  451. (defun find-coding-systems-region (from to)
  452. "Return a list of proper coding systems to encode a text between FROM and TO.
  453. If FROM is a string, find coding systems in that instead of the buffer.
  454. All coding systems in the list can safely encode any multibyte characters
  455. in the text.
  456. If the text contains no multibyte characters, return a list of a single
  457. element `undecided'."
  458. (let ((codings (find-coding-systems-region-internal from to)))
  459. (if (eq codings t)
  460. ;; The text contains only ASCII characters. Any coding
  461. ;; systems are safe.
  462. '(undecided)
  463. ;; We need copy-sequence because sorting will alter the argument.
  464. (sort-coding-systems (copy-sequence codings)))))
  465. (defun find-coding-systems-string (string)
  466. "Return a list of proper coding systems to encode STRING.
  467. All coding systems in the list can safely encode any multibyte characters
  468. in STRING.
  469. If STRING contains no multibyte characters, return a list of a single
  470. element `undecided'."
  471. (find-coding-systems-region string nil))
  472. (defun find-coding-systems-for-charsets (charsets)
  473. "Return a list of proper coding systems to encode characters of CHARSETS.
  474. CHARSETS is a list of character sets.
  475. This only finds coding systems of type `charset', whose
  476. `:charset-list' property includes all of CHARSETS (plus `ascii' for
  477. ASCII-compatible coding systems). It was used in older versions of
  478. Emacs, but is unlikely to be what you really want now."
  479. ;; Deal with aliases.
  480. (setq charsets (mapcar (lambda (c)
  481. (get-charset-property c :name))
  482. charsets))
  483. (cond ((or (null charsets)
  484. (and (= (length charsets) 1)
  485. (eq 'ascii (car charsets))))
  486. '(undecided))
  487. ((or (memq 'eight-bit-control charsets)
  488. (memq 'eight-bit-graphic charsets))
  489. '(raw-text utf-8-emacs))
  490. (t
  491. (let (codings)
  492. (dolist (cs (coding-system-list t))
  493. (let ((cs-charsets (and (eq (coding-system-type cs) 'charset)
  494. (coding-system-charset-list cs)))
  495. (charsets charsets))
  496. (if (coding-system-get cs :ascii-compatible-p)
  497. (add-to-list 'cs-charsets 'ascii))
  498. (if (catch 'ok
  499. (when cs-charsets
  500. (while charsets
  501. (unless (memq (pop charsets) cs-charsets)
  502. (throw 'ok nil)))
  503. t))
  504. (push cs codings))))
  505. (nreverse codings)))))
  506. (defun find-multibyte-characters (from to &optional maxcount excludes)
  507. "Find multibyte characters in the region specified by FROM and TO.
  508. If FROM is a string, find multibyte characters in the string.
  509. The return value is an alist of the following format:
  510. ((CHARSET COUNT CHAR ...) ...)
  511. where
  512. CHARSET is a character set,
  513. COUNT is a number of characters,
  514. CHARs are the characters found from the character set.
  515. Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
  516. Optional 4th arg EXCLUDES is a list of character sets to be ignored."
  517. (let ((chars nil)
  518. charset char)
  519. (if (stringp from)
  520. (if (multibyte-string-p from)
  521. (let ((idx 0))
  522. (while (setq idx (string-match-p "[^\000-\177]" from idx))
  523. (setq char (aref from idx)
  524. charset (char-charset char))
  525. (unless (memq charset excludes)
  526. (let ((slot (assq charset chars)))
  527. (if slot
  528. (if (not (memq char (nthcdr 2 slot)))
  529. (let ((count (nth 1 slot)))
  530. (setcar (cdr slot) (1+ count))
  531. (if (or (not maxcount) (< count maxcount))
  532. (nconc slot (list char)))))
  533. (setq chars (cons (list charset 1 char) chars)))))
  534. (setq idx (1+ idx)))))
  535. (if enable-multibyte-characters
  536. (save-excursion
  537. (goto-char from)
  538. (while (re-search-forward "[^\000-\177]" to t)
  539. (setq char (preceding-char)
  540. charset (char-charset char))
  541. (unless (memq charset excludes)
  542. (let ((slot (assq charset chars)))
  543. (if slot
  544. (if (not (member char (nthcdr 2 slot)))
  545. (let ((count (nth 1 slot)))
  546. (setcar (cdr slot) (1+ count))
  547. (if (or (not maxcount) (< count maxcount))
  548. (nconc slot (list char)))))
  549. (setq chars (cons (list charset 1 char) chars)))))))))
  550. (nreverse chars)))
  551. (defun search-unencodable-char (coding-system)
  552. "Search forward from point for a character that is not encodable.
  553. It asks which coding system to check.
  554. If such a character is found, set point after that character.
  555. Otherwise, don't move point.
  556. When called from a program, the value is the position of the unencodable
  557. character found, or nil if all characters are encodable."
  558. (interactive
  559. (list (let ((default (or buffer-file-coding-system 'us-ascii)))
  560. (read-coding-system
  561. (format "Coding-system (default %s): " default)
  562. default))))
  563. (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
  564. (if pos
  565. (goto-char (1+ pos))
  566. (message "All following characters are encodable by %s" coding-system))
  567. pos))
  568. (defvar last-coding-system-specified nil
  569. "Most recent coding system explicitly specified by the user when asked.
  570. This variable is set whenever Emacs asks the user which coding system
  571. to use in order to write a file. If you set it to nil explicitly,
  572. then call `write-region', then afterward this variable will be non-nil
  573. only if the user was explicitly asked and specified a coding system.")
  574. (defvar select-safe-coding-system-accept-default-p nil
  575. "If non-nil, a function to control the behavior of coding system selection.
  576. The meaning is the same as the argument ACCEPT-DEFAULT-P of the
  577. function `select-safe-coding-system' (which see). This variable
  578. overrides that argument.")
  579. (defun select-safe-coding-system-interactively (from to codings unsafe
  580. &optional rejected default)
  581. "Select interactively a coding system for the region FROM ... TO.
  582. FROM can be a string, as in `write-region'.
  583. CODINGS is the list of base coding systems known to be safe for this region,
  584. typically obtained with `find-coding-systems-region'.
  585. UNSAFE is a list of coding systems known to be unsafe for this region.
  586. REJECTED is a list of coding systems which were safe but for some reason
  587. were not recommended in the particular context.
  588. DEFAULT is the coding system to use by default in the query."
  589. ;; At first, if some defaults are unsafe, record at most 11
  590. ;; problematic characters and their positions for them by turning
  591. ;; (CODING ...)
  592. ;; into
  593. ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
  594. (if unsafe
  595. (setq unsafe
  596. (mapcar #'(lambda (coding)
  597. (cons coding
  598. (if (stringp from)
  599. (mapcar #'(lambda (pos)
  600. (cons pos (aref from pos)))
  601. (unencodable-char-position
  602. 0 (length from) coding
  603. 11 from))
  604. (mapcar #'(lambda (pos)
  605. (cons pos (char-after pos)))
  606. (unencodable-char-position
  607. from to coding 11)))))
  608. unsafe)))
  609. ;; Change each safe coding system to the corresponding
  610. ;; mime-charset name if it is also a coding system. Such a name
  611. ;; is more friendly to users.
  612. (let ((l codings)
  613. mime-charset)
  614. (while l
  615. (setq mime-charset (coding-system-get (car l) :mime-charset))
  616. (if (and mime-charset (coding-system-p mime-charset)
  617. (coding-system-equal (car l) mime-charset))
  618. (setcar l mime-charset))
  619. (setq l (cdr l))))
  620. ;; Don't offer variations with locking shift, which you
  621. ;; basically never want.
  622. (let (l)
  623. (dolist (elt codings (setq codings (nreverse l)))
  624. (unless (or (eq 'coding-category-iso-7-else
  625. (coding-system-category elt))
  626. (eq 'coding-category-iso-8-else
  627. (coding-system-category elt)))
  628. (push elt l))))
  629. ;; Remove raw-text, emacs-mule and no-conversion unless nothing
  630. ;; else is available.
  631. (setq codings
  632. (or (delq 'raw-text
  633. (delq 'emacs-mule
  634. (delq 'no-conversion codings)))
  635. '(raw-text emacs-mule no-conversion)))
  636. (let ((window-configuration (current-window-configuration))
  637. (bufname (buffer-name))
  638. coding-system)
  639. (save-excursion
  640. ;; If some defaults are unsafe, make sure the offending
  641. ;; buffer is displayed.
  642. (when (and unsafe (not (stringp from)))
  643. (pop-to-buffer bufname)
  644. (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
  645. unsafe))))
  646. ;; Then ask users to select one from CODINGS while showing
  647. ;; the reason why none of the defaults are not used.
  648. (with-output-to-temp-buffer "*Warning*"
  649. (with-current-buffer standard-output
  650. (if (and (null rejected) (null unsafe))
  651. (insert "No default coding systems to try for "
  652. (if (stringp from)
  653. (format "string \"%s\"." from)
  654. (format "buffer `%s'." bufname)))
  655. (insert
  656. "These default coding systems were tried to encode"
  657. (if (stringp from)
  658. (concat " \"" (if (> (length from) 10)
  659. (concat (substring from 0 10) "...\"")
  660. (concat from "\"")))
  661. (format " text\nin the buffer `%s'" bufname))
  662. ":\n")
  663. (let ((pos (point))
  664. (fill-prefix " "))
  665. (dolist (x (append rejected unsafe))
  666. (princ " ") (princ x))
  667. (insert "\n")
  668. (fill-region-as-paragraph pos (point)))
  669. (when rejected
  670. (insert "These safely encode the text in the buffer,
  671. but are not recommended for encoding text in this context,
  672. e.g., for sending an email message.\n ")
  673. (dolist (x rejected)
  674. (princ " ") (princ x))
  675. (insert "\n"))
  676. (when unsafe
  677. (insert (if rejected "The other coding systems"
  678. "However, each of them")
  679. " encountered characters it couldn't encode:\n")
  680. (dolist (coding unsafe)
  681. (insert (format " %s cannot encode these:" (car coding)))
  682. (let ((i 0)
  683. (func1
  684. #'(lambda (bufname pos)
  685. (when (buffer-live-p (get-buffer bufname))
  686. (pop-to-buffer bufname)
  687. (goto-char pos))))
  688. (func2
  689. #'(lambda (bufname pos coding)
  690. (when (buffer-live-p (get-buffer bufname))
  691. (pop-to-buffer bufname)
  692. (if (< (point) pos)
  693. (goto-char pos)
  694. (forward-char 1)
  695. (search-unencodable-char coding)
  696. (forward-char -1))))))
  697. (dolist (elt (cdr coding))
  698. (insert " ")
  699. (if (stringp from)
  700. (insert (if (< i 10) (cdr elt) "..."))
  701. (if (< i 10)
  702. (insert-text-button
  703. (cdr elt)
  704. :type 'help-xref
  705. 'face 'link
  706. 'help-echo
  707. "mouse-2, RET: jump to this character"
  708. 'help-function func1
  709. 'help-args (list bufname (car elt)))
  710. (insert-text-button
  711. "..."
  712. :type 'help-xref
  713. 'face 'link
  714. 'help-echo
  715. "mouse-2, RET: next unencodable character"
  716. 'help-function func2
  717. 'help-args (list bufname (car elt)
  718. (car coding)))))
  719. (setq i (1+ i))))
  720. (insert "\n"))
  721. (insert (substitute-command-keys "\
  722. Click on a character (or switch to this window by `\\[other-window]'\n\
  723. and select the characters by RET) to jump to the place it appears,\n\
  724. where `\\[universal-argument] \\[what-cursor-position]' will give information about it.\n"))))
  725. (insert (substitute-command-keys "\nSelect \
  726. one of the safe coding systems listed below,\n\
  727. or cancel the writing with \\[keyboard-quit] and edit the buffer\n\
  728. to remove or modify the problematic characters,\n\
  729. or specify any other coding system (and risk losing\n\
  730. the problematic characters).\n\n"))
  731. (let ((pos (point))
  732. (fill-prefix " "))
  733. (dolist (x codings)
  734. (princ " ") (princ x))
  735. (insert "\n")
  736. (fill-region-as-paragraph pos (point)))))
  737. ;; Read a coding system.
  738. (setq coding-system
  739. (read-coding-system
  740. (format "Select coding system (default %s): " default)
  741. default))
  742. (setq last-coding-system-specified coding-system))
  743. (kill-buffer "*Warning*")
  744. (set-window-configuration window-configuration)
  745. coding-system))
  746. (defun select-safe-coding-system (from to &optional default-coding-system
  747. accept-default-p file)
  748. "Ask a user to select a safe coding system from candidates.
  749. The candidates of coding systems which can safely encode a text
  750. between FROM and TO are shown in a popup window. Among them, the most
  751. proper one is suggested as the default.
  752. The list of `buffer-file-coding-system' of the current buffer, the
  753. default `buffer-file-coding-system', and the most preferred coding
  754. system (if it corresponds to a MIME charset) is treated as the
  755. default coding system list. Among them, the first one that safely
  756. encodes the text is normally selected silently and returned without
  757. any user interaction. See also the command `prefer-coding-system'.
  758. However, the user is queried if the chosen coding system is
  759. inconsistent with what would be selected by `find-auto-coding' from
  760. coding cookies &c. if the contents of the region were read from a
  761. file. (That could lead to data corruption in a file subsequently
  762. re-visited and edited.)
  763. Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
  764. list of coding systems to be prepended to the default coding system
  765. list. However, if DEFAULT-CODING-SYSTEM is a list and the first
  766. element is t, the cdr part is used as the default coding system list,
  767. i.e. current `buffer-file-coding-system', default `buffer-file-coding-system',
  768. and the most preferred coding system are not used.
  769. Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
  770. determine the acceptability of the silently selected coding system.
  771. It is called with that coding system, and should return nil if it
  772. should not be silently selected and thus user interaction is required.
  773. Optional 5th arg FILE is the file name to use for this purpose.
  774. That is different from `buffer-file-name' when handling `write-region'
  775. \(for example).
  776. The variable `select-safe-coding-system-accept-default-p', if non-nil,
  777. overrides ACCEPT-DEFAULT-P.
  778. Kludgy feature: if FROM is a string, the string is the target text,
  779. and TO is ignored."
  780. (if (not (listp default-coding-system))
  781. (setq default-coding-system (list default-coding-system)))
  782. (let ((no-other-defaults nil)
  783. auto-cs)
  784. (unless (or (stringp from) find-file-literally)
  785. ;; Find an auto-coding that is specified for the current
  786. ;; buffer and file from the region FROM and TO.
  787. (save-excursion
  788. (save-restriction
  789. (widen)
  790. (goto-char from)
  791. (setq auto-cs (find-auto-coding (or file buffer-file-name "")
  792. (- to from)))
  793. (if auto-cs
  794. (if (coding-system-p (car auto-cs))
  795. (setq auto-cs (car auto-cs))
  796. (display-warning
  797. 'mule
  798. (format "\
  799. Invalid coding system `%s' is specified
  800. for the current buffer/file by the %s.
  801. It is highly recommended to fix it before writing to a file."
  802. (car auto-cs)
  803. (if (eq (cdr auto-cs) :coding) ":coding tag"
  804. (format "variable `%s'" (cdr auto-cs))))
  805. :warning)
  806. (or (yes-or-no-p "Really proceed with writing? ")
  807. (error "Save aborted"))
  808. (setq auto-cs nil))))))
  809. (if (eq (car default-coding-system) t)
  810. (setq no-other-defaults t
  811. default-coding-system (cdr default-coding-system)))
  812. ;; Change elements of the list to (coding . base-coding).
  813. (setq default-coding-system
  814. (mapcar (function (lambda (x) (cons x (coding-system-base x))))
  815. default-coding-system))
  816. (if (and auto-cs (not no-other-defaults))
  817. ;; If the file has a coding cookie, use it regardless of any
  818. ;; other setting.
  819. (let ((base (coding-system-base auto-cs)))
  820. (unless (memq base '(nil undecided))
  821. (setq default-coding-system (list (cons auto-cs base)))
  822. (setq no-other-defaults t))))
  823. (unless no-other-defaults
  824. ;; If buffer-file-coding-system is not nil nor undecided, append it
  825. ;; to the defaults.
  826. (if buffer-file-coding-system
  827. (let ((base (coding-system-base buffer-file-coding-system)))
  828. (or (eq base 'undecided)
  829. (rassq base default-coding-system)
  830. (setq default-coding-system
  831. (append default-coding-system
  832. (list (cons buffer-file-coding-system base)))))))
  833. (unless (and buffer-file-coding-system-explicit
  834. (cdr buffer-file-coding-system-explicit))
  835. ;; If default buffer-file-coding-system is not nil nor undecided,
  836. ;; append it to the defaults.
  837. (when (default-value 'buffer-file-coding-system)
  838. (let ((base (coding-system-base
  839. (default-value 'buffer-file-coding-system))))
  840. (or (eq base 'undecided)
  841. (rassq base default-coding-system)
  842. (setq default-coding-system
  843. (append default-coding-system
  844. (list (cons (default-value
  845. 'buffer-file-coding-system)
  846. base)))))))
  847. ;; If the most preferred coding system has the property mime-charset,
  848. ;; append it to the defaults.
  849. (let ((preferred (coding-system-priority-list t))
  850. base)
  851. (and (coding-system-p preferred)
  852. (setq base (coding-system-base preferred))
  853. (coding-system-get preferred :mime-charset)
  854. (not (rassq base default-coding-system))
  855. (setq default-coding-system
  856. (append default-coding-system
  857. (list (cons preferred base))))))))
  858. (if select-safe-coding-system-accept-default-p
  859. (setq accept-default-p select-safe-coding-system-accept-default-p))
  860. ;; Decide the eol-type from the top of the default codings,
  861. ;; current buffer-file-coding-system, or default buffer-file-coding-system.
  862. (if default-coding-system
  863. (let ((default-eol-type (coding-system-eol-type
  864. (caar default-coding-system))))
  865. (if (and (vectorp default-eol-type) buffer-file-coding-system)
  866. (setq default-eol-type (coding-system-eol-type
  867. buffer-file-coding-system)))
  868. (if (and (vectorp default-eol-type)
  869. (default-value 'buffer-file-coding-system))
  870. (setq default-eol-type
  871. (coding-system-eol-type
  872. (default-value 'buffer-file-coding-system))))
  873. (if (and default-eol-type (not (vectorp default-eol-type)))
  874. (dolist (elt default-coding-system)
  875. (setcar elt (coding-system-change-eol-conversion
  876. (car elt) default-eol-type))))))
  877. (let ((codings (find-coding-systems-region from to))
  878. (coding-system nil)
  879. (tick (if (not (stringp from)) (buffer-chars-modified-tick)))
  880. safe rejected unsafe)
  881. (if (eq (car codings) 'undecided)
  882. ;; Any coding system is ok.
  883. (setq coding-system (caar default-coding-system))
  884. ;; Reverse the list so that elements are accumulated in safe,
  885. ;; rejected, and unsafe in the correct order.
  886. (setq default-coding-system (nreverse default-coding-system))
  887. ;; Classify the defaults into safe, rejected, and unsafe.
  888. (dolist (elt default-coding-system)
  889. (if (or (eq (car codings) 'undecided)
  890. (memq (cdr elt) codings))
  891. (if (and (functionp accept-default-p)
  892. (not (funcall accept-default-p (cdr elt))))
  893. (push (car elt) rejected)
  894. (push (car elt) safe))
  895. (push (car elt) unsafe)))
  896. (if safe
  897. (setq coding-system (car safe))))
  898. ;; If all the defaults failed, ask a user.
  899. (when (not coding-system)
  900. (setq coding-system (select-safe-coding-system-interactively
  901. from to codings unsafe rejected (car codings))))
  902. ;; Check we're not inconsistent with what `coding:' spec &c would
  903. ;; give when file is re-read.
  904. ;; But don't do this if we explicitly ignored the cookie
  905. ;; by using `find-file-literally'.
  906. (when (and auto-cs
  907. (not (and
  908. coding-system
  909. (memq (coding-system-type coding-system) '(0 5)))))
  910. ;; Merge coding-system and auto-cs as far as possible.
  911. (if (not coding-system)
  912. (setq coding-system auto-cs)
  913. (if (not auto-cs)
  914. (setq auto-cs coding-system)
  915. (let ((eol-type-1 (coding-system-eol-type coding-system))
  916. (eol-type-2 (coding-system-eol-type auto-cs)))
  917. (if (eq (coding-system-base coding-system) 'undecided)
  918. (setq coding-system (coding-system-change-text-conversion
  919. coding-system auto-cs))
  920. (if (eq (coding-system-base auto-cs) 'undecided)
  921. (setq auto-cs (coding-system-change-text-conversion
  922. auto-cs coding-system))))
  923. (if (vectorp eol-type-1)
  924. (or (vectorp eol-type-2)
  925. (setq coding-system (coding-system-change-eol-conversion
  926. coding-system eol-type-2)))
  927. (if (vectorp eol-type-2)
  928. (setq auto-cs (coding-system-change-eol-conversion
  929. auto-cs eol-type-1)))))))
  930. (if (and auto-cs
  931. ;; Don't barf if writing a compressed file, say.
  932. ;; This check perhaps isn't ideal, but is probably
  933. ;; the best thing to do.
  934. (not (auto-coding-alist-lookup (or file buffer-file-name "")))
  935. (not (coding-system-equal coding-system auto-cs)))
  936. (unless (yes-or-no-p
  937. (format "Selected encoding %s disagrees with \
  938. %s specified by file contents. Really save (else edit coding cookies \
  939. and try again)? " coding-system auto-cs))
  940. (error "Save aborted"))))
  941. (when (and tick (/= tick (buffer-chars-modified-tick)))
  942. (error "Canceled because the buffer was modified"))
  943. coding-system)))
  944. (setq select-safe-coding-system-function 'select-safe-coding-system)
  945. (defun select-message-coding-system ()
  946. "Return a coding system to encode the outgoing message of the current buffer.
  947. It at first tries the first coding system found in these variables
  948. in this order:
  949. (1) local value of `buffer-file-coding-system'
  950. (2) value of `sendmail-coding-system'
  951. (3) value of `default-sendmail-coding-system'
  952. (4) default value of `buffer-file-coding-system'
  953. If the found coding system can't encode the current buffer,
  954. or none of them are bound to a coding system,
  955. it asks the user to select a proper coding system."
  956. (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
  957. buffer-file-coding-system)
  958. sendmail-coding-system
  959. default-sendmail-coding-system
  960. (default-value 'buffer-file-coding-system))))
  961. (if (eq coding 'no-conversion)
  962. ;; We should never use no-conversion for outgoing mail.
  963. (setq coding nil))
  964. (if (fboundp select-safe-coding-system-function)
  965. (funcall select-safe-coding-system-function
  966. (point-min) (point-max) coding
  967. (function (lambda (x) (coding-system-get x :mime-charset))))
  968. coding)))
  969. ;;; Language support stuff.
  970. (defvar language-info-alist nil
  971. "Alist of language environment definitions.
  972. Each element looks like:
  973. (LANGUAGE-NAME . ((KEY . INFO) ...))
  974. where LANGUAGE-NAME is a string, the name of the language environment,
  975. KEY is a symbol denoting the kind of information, and
  976. INFO is the data associated with KEY.
  977. Meaningful values for KEY include
  978. documentation value is documentation of what this language environment
  979. is meant for, and how to use it.
  980. charset value is a list of the character sets mainly used
  981. by this language environment.
  982. sample-text value is an expression which is evalled to generate
  983. a line of text written using characters appropriate
  984. for this language environment.
  985. setup-function value is a function to call to switch to this
  986. language environment.
  987. exit-function value is a function to call to leave this
  988. language environment.
  989. coding-system value is a list of coding systems that are good for
  990. saving text written in this language environment.
  991. This list serves as suggestions to the user;
  992. in effect, as a kind of documentation.
  993. coding-priority value is a list of coding systems for this language
  994. environment, in order of decreasing priority.
  995. This is used to set up the coding system priority
  996. list when you switch to this language environment.
  997. nonascii-translation
  998. value is a charset of dimension one to use for
  999. converting a unibyte character to multibyte
  1000. and vice versa.
  1001. input-method value is a default input method for this language
  1002. environment.
  1003. features value is a list of features requested in this
  1004. language environment.
  1005. ctext-non-standard-encodings
  1006. value is a list of non-standard encoding names used
  1007. in extended segments of CTEXT. See the variable
  1008. `ctext-non-standard-encodings' for more detail.
  1009. The following key takes effect only when multibyte characters are
  1010. globally disabled, i.e. the default value of `enable-multibyte-characters'
  1011. is nil (which is an obsolete and deprecated use):
  1012. unibyte-display value is a coding system to encode characters for
  1013. the terminal. Characters in the range of 160 to
  1014. 255 display not as octal escapes, but as non-ASCII
  1015. characters in this language environment.")
  1016. (defun get-language-info (lang-env key)
  1017. "Return information listed under KEY for language environment LANG-ENV.
  1018. KEY is a symbol denoting the kind of information.
  1019. For a list of useful values for KEY and their meanings,
  1020. see `language-info-alist'."
  1021. (if (symbolp lang-env)
  1022. (setq lang-env (symbol-name lang-env)))
  1023. (let ((lang-slot (assoc-string lang-env language-info-alist t)))
  1024. (if lang-slot
  1025. (cdr (assq key (cdr lang-slot))))))
  1026. (defun set-language-info (lang-env key info)
  1027. "Modify part of the definition of language environment LANG-ENV.
  1028. Specifically, this stores the information INFO under KEY
  1029. in the definition of this language environment.
  1030. KEY is a symbol denoting the kind of information.
  1031. INFO is the value for that information.
  1032. For a list of useful values for KEY and their meanings,
  1033. see `language-info-alist'."
  1034. (if (symbolp lang-env)
  1035. (setq lang-env (symbol-name lang-env)))
  1036. (set-language-info-internal lang-env key info)
  1037. (if (equal lang-env current-language-environment)
  1038. (cond ((eq key 'coding-priority)
  1039. (set-language-environment-coding-systems lang-env)
  1040. (set-language-environment-charset lang-env))
  1041. ((eq key 'input-method)
  1042. (set-language-environment-input-method lang-env))
  1043. ((eq key 'nonascii-translation)
  1044. (set-language-environment-nonascii-translation lang-env))
  1045. ((eq key 'charset)
  1046. (set-language-environment-charset lang-env))
  1047. ((and (not (default-value 'enable-multibyte-characters))
  1048. (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
  1049. (set-language-environment-unibyte lang-env)))))
  1050. (defun set-language-info-internal (lang-env key info)
  1051. "Internal use only.
  1052. Arguments are the same as `set-language-info'."
  1053. (let (lang-slot key-slot)
  1054. (setq lang-slot (assoc lang-env language-info-alist))
  1055. (if (null lang-slot) ; If no slot for the language, add it.
  1056. (setq lang-slot (list lang-env)
  1057. language-info-alist (cons lang-slot language-info-alist)))
  1058. (setq key-slot (assq key lang-slot))
  1059. (if (null key-slot) ; If no slot for the key, add it.
  1060. (progn
  1061. (setq key-slot (list key))
  1062. (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
  1063. (setcdr key-slot (purecopy info))
  1064. ;; Update the custom-type of `current-language-environment'.
  1065. (put 'current-language-environment 'custom-type
  1066. (cons 'choice (mapcar
  1067. (lambda (lang)
  1068. (list 'const lang))
  1069. (sort (mapcar 'car language-info-alist) 'string<))))))
  1070. (defun set-language-info-alist (lang-env alist &optional parents)
  1071. "Store ALIST as the definition of language environment LANG-ENV.
  1072. ALIST is an alist of KEY and INFO values. See the documentation of
  1073. `language-info-alist' for the meanings of KEY and INFO.
  1074. Optional arg PARENTS is a list of parent menu names; it specifies
  1075. where to put this language environment in the
  1076. Describe Language Environment and Set Language Environment menus.
  1077. For example, (\"European\") means to put this language environment
  1078. in the European submenu in each of those two menus."
  1079. (cond ((symbolp lang-env)
  1080. (setq lang-env (symbol-name lang-env)))
  1081. ((stringp lang-env)
  1082. (setq lang-env (purecopy lang-env))))
  1083. (let ((describe-map describe-language-environment-map)
  1084. (setup-map setup-language-environment-map))
  1085. (if parents
  1086. (let ((l parents)
  1087. map parent-symbol parent prompt)
  1088. (while l
  1089. (if (symbolp (setq parent-symbol (car l)))
  1090. (setq parent (symbol-name parent))
  1091. (setq parent parent-symbol parent-symbol (intern parent)))
  1092. (setq map (lookup-key describe-map (vector parent-symbol)))
  1093. ;; This prompt string is for define-prefix-command, so
  1094. ;; that the map it creates will be suitable for a menu.
  1095. (or map (setq prompt (format "%s Environment" parent)))
  1096. (if (not map)
  1097. (progn
  1098. (setq map (intern (format "describe-%s-environment-map"
  1099. (downcase parent))))
  1100. (define-prefix-command map nil prompt)
  1101. (define-key-after describe-map (vector parent-symbol)
  1102. (cons parent map))))
  1103. (setq describe-map (symbol-value map))
  1104. (setq map (lookup-key setup-map (vector parent-symbol)))
  1105. (if (not map)
  1106. (progn
  1107. (setq map (intern (format "setup-%s-environment-map"
  1108. (downcase parent))))
  1109. (define-prefix-command map nil prompt)
  1110. (define-key-after setup-map (vector parent-symbol)
  1111. (cons parent map))))
  1112. (setq setup-map (symbol-value map))
  1113. (setq l (cdr l)))))
  1114. ;; Set up menu items for this language env.
  1115. (let ((doc (assq 'documentation alist)))
  1116. (when doc
  1117. (define-key-after describe-map (vector (intern lang-env))
  1118. (cons lang-env 'describe-specified-language-support))))
  1119. (define-key-after setup-map (vector (intern lang-env))
  1120. (cons lang-env 'setup-specified-language-environment))
  1121. (dolist (elt alist)
  1122. (set-language-info-internal lang-env (car elt) (cdr elt)))
  1123. (if (equal lang-env current-language-environment)
  1124. (set-language-environment lang-env))))
  1125. (defun read-language-name (key prompt &optional default)
  1126. "Read a language environment name which has information for KEY.
  1127. If KEY is nil, read any language environment.
  1128. Prompt with PROMPT. DEFAULT is the default choice of language environment.
  1129. This returns a language environment name as a string."
  1130. (let* ((completion-ignore-case t)
  1131. (name (completing-read prompt
  1132. language-info-alist
  1133. (and key
  1134. (function (lambda (elm) (and (listp elm) (assq key elm)))))
  1135. t nil nil default)))
  1136. (if (and (> (length name) 0)
  1137. (or (not key)
  1138. (get-language-info name key)))
  1139. name)))
  1140. ;;; Multilingual input methods.
  1141. (defgroup leim nil
  1142. "LEIM: Libraries of Emacs Input Methods."
  1143. :group 'mule)
  1144. (defconst leim-list-file-name "leim-list.el"
  1145. "Name of LEIM list file.
  1146. This file contains a list of libraries of Emacs input methods (LEIM)
  1147. in the format of Lisp expression for registering each input method.
  1148. Emacs loads this file at startup time.")
  1149. (defconst leim-list-header (format
  1150. ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: utf-8;-*-
  1151. ;;
  1152. ;; This file is automatically generated.
  1153. ;;
  1154. ;; This file contains a list of LEIM (Library of Emacs Input Method)
  1155. ;; methods in the same directory as this file. Loading this file
  1156. ;; registers all the input methods in Emacs.
  1157. ;;
  1158. ;; Each entry has the form:
  1159. ;; (register-input-method
  1160. ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
  1161. ;; TITLE DESCRIPTION
  1162. ;; ARG ...)
  1163. ;; See the function `register-input-method' for the meanings of the arguments.
  1164. ;;
  1165. ;; If this directory is included in `load-path', Emacs automatically
  1166. ;; loads this file at startup time.
  1167. "
  1168. leim-list-file-name)
  1169. "Header to be inserted in LEIM list file.")
  1170. (defconst leim-list-entry-regexp "^(register-input-method"
  1171. "Regexp matching head of each entry in LEIM list file.
  1172. See also the variable `leim-list-header'.")
  1173. (defvar update-leim-list-functions
  1174. '(quail-update-leim-list-file)
  1175. "List of functions to call to update LEIM list file.
  1176. Each function is called with one arg, LEIM directory name.")
  1177. (defun update-leim-list-file (&rest dirs)
  1178. "Update LEIM list file in directories DIRS."
  1179. (dolist (function update-leim-list-functions)
  1180. (apply function dirs)))
  1181. (defvar current-input-method nil
  1182. "The current input method for multilingual text.
  1183. If nil, that means no input method is activated now.")
  1184. (make-variable-buffer-local 'current-input-method)
  1185. (put 'current-input-method 'permanent-local t)
  1186. (defvar current-input-method-title nil
  1187. "Title string of the current input method shown in mode line.")
  1188. (make-variable-buffer-local 'current-input-method-title)
  1189. (put 'current-input-method-title 'permanent-local t)
  1190. (define-widget 'mule-input-method-string 'string
  1191. "String widget with completion for input method."
  1192. :completions
  1193. (lambda (string pred action)
  1194. (let ((completion-ignore-case t))
  1195. (complete-with-action action input-method-alist string pred)))
  1196. :prompt-history 'input-method-history)
  1197. (defcustom default-input-method nil
  1198. "Default input method for multilingual text (a string).
  1199. This is the input method activated automatically by the command
  1200. `toggle-input-method' (\\[toggle-input-method])."
  1201. :link '(custom-manual "(emacs)Input Methods")
  1202. :group 'mule
  1203. :type `(choice (const nil)
  1204. mule-input-method-string)
  1205. :set-after '(current-language-environment))
  1206. (put 'input-method-function 'permanent-local t)
  1207. (defvar input-method-history nil
  1208. "History list of input methods read from the minibuffer.
  1209. Maximum length of the history list is determined by the value
  1210. of `history-length', which see.")
  1211. (make-variable-buffer-local 'input-method-history)
  1212. (put 'input-method-history 'permanent-local t)
  1213. (defvar inactivate-current-input-method-function nil
  1214. "Function to call for inactivating the current input method.
  1215. Every input method should set this to an appropriate value when activated.
  1216. This function is called with no argument.
  1217. This function should never change the value of `current-input-method'.
  1218. It is set to nil by the function `inactivate-input-method'.")
  1219. (make-variable-buffer-local 'inactivate-current-input-method-function)
  1220. (put 'inactivate-current-input-method-function 'permanent-local t)
  1221. (defvar describe-current-input-method-function nil
  1222. "Function to call for describing the current input method.
  1223. This function is called with no argument.")
  1224. (make-variable-buffer-local 'describe-current-input-method-function)
  1225. (put 'describe-current-input-method-function 'permanent-local t)
  1226. (defvar input-method-alist nil
  1227. "Alist of input method names vs how to use them.
  1228. Each element has the form:
  1229. (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
  1230. See the function `register-input-method' for the meanings of the elements.")
  1231. ;;;###autoload
  1232. (put 'input-method-alist 'risky-local-variable t)
  1233. (defun register-input-method (input-method lang-env &rest args)
  1234. "Register INPUT-METHOD as an input method for language environment LANG-ENV.
  1235. INPUT-METHOD and LANG-ENV are symbols or strings.
  1236. ACTIVATE-FUNC is a function to call to activate this method.
  1237. TITLE is a string to show in the mode line when this method is active.
  1238. DESCRIPTION is a string describing this method and what it is good for.
  1239. The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
  1240. All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
  1241. This function is mainly used in the file \"leim-list.el\" which is
  1242. created at Emacs build time, registering all Quail input methods
  1243. contained in the Emacs distribution.
  1244. In case you want to register a new Quail input method by yourself, be
  1245. careful to use the same input method title as given in the third
  1246. parameter of `quail-define-package'. (If the values are different, the
  1247. string specified in this function takes precedence.)
  1248. The commands `describe-input-method' and `list-input-methods' need
  1249. these duplicated values to show some information about input methods
  1250. without loading the relevant Quail packages.
  1251. \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
  1252. (if (symbolp lang-env)
  1253. (setq lang-env (symbol-name lang-env))
  1254. (setq lang-env (purecopy lang-env)))
  1255. (if (symbolp input-method)
  1256. (setq input-method (symbol-name input-method))
  1257. (setq input-method (purecopy input-method)))
  1258. (setq args (mapcar 'purecopy args))
  1259. (let ((info (cons lang-env args))
  1260. (slot (assoc input-method input-method-alist)))
  1261. (if slot
  1262. (setcdr slot info)
  1263. (setq slot (cons input-method info))
  1264. (setq input-method-alist (cons slot input-method-alist)))))
  1265. (defun read-input-method-name (prompt &optional default inhibit-null)
  1266. "Read a name of input method from a minibuffer prompting with PROMPT.
  1267. If DEFAULT is non-nil, use that as the default,
  1268. and substitute it into PROMPT at the first `%s'.
  1269. If INHIBIT-NULL is non-nil, null input signals an error.
  1270. The return value is a string."
  1271. (if default
  1272. (setq prompt (format prompt default)))
  1273. (let* ((completion-ignore-case t)
  1274. ;; As it is quite normal to change input method in the
  1275. ;; minibuffer, we must enable it even if
  1276. ;; enable-recursive-minibuffers is currently nil.
  1277. (enable-recursive-minibuffers t)
  1278. ;; This binding is necessary because input-method-history is
  1279. ;; buffer local.
  1280. (input-method (completing-read prompt input-method-alist
  1281. nil t nil 'input-method-history
  1282. default)))
  1283. (if (and input-method (symbolp input-method))
  1284. (setq input-method (symbol-name input-method)))
  1285. (if (> (length input-method) 0)
  1286. input-method
  1287. (if inhibit-null
  1288. (error "No valid input method is specified")))))
  1289. (defun activate-input-method (input-method)
  1290. "Switch to input method INPUT-METHOD for the current buffer.
  1291. If some other input method is already active, turn it off first.
  1292. If INPUT-METHOD is nil, deactivate any current input method."
  1293. (if (and input-method (symbolp input-method))
  1294. (setq input-method (symbol-name input-method)))
  1295. (if (and current-input-method
  1296. (not (string= current-input-method input-method)))
  1297. (inactivate-input-method))
  1298. (unless (or current-input-method (null input-method))
  1299. (let ((slot (assoc input-method input-method-alist)))
  1300. (if (null slot)
  1301. (error "Can't activate input method `%s'" input-method))
  1302. (setq current-input-method-title nil)
  1303. (let ((func (nth 2 slot)))
  1304. (if (functionp func)
  1305. (apply (nth 2 slot) input-method (nthcdr 5 slot))
  1306. (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
  1307. (progn
  1308. (require (cdr func))
  1309. (apply (car func) input-method (nthcdr 5 slot)))
  1310. (error "Can't activate input method `%s'" input-method))))
  1311. (setq current-input-method input-method)
  1312. (or (stringp current-input-method-title)
  1313. (setq current-input-method-title (nth 3 slot)))
  1314. (unwind-protect
  1315. (run-hooks 'input-method-activate-hook)
  1316. (force-mode-line-update)))))
  1317. (defun inactivate-input-method ()
  1318. "Turn off the current input method."
  1319. (when current-input-method
  1320. (if input-method-history
  1321. (unless (string= current-input-method (car input-method-history))
  1322. (setq input-method-history
  1323. (cons current-input-method
  1324. (delete current-input-method input-method-history))))
  1325. (setq input-method-history (list current-input-method)))
  1326. (unwind-protect
  1327. (progn
  1328. (setq input-method-function nil
  1329. current-input-method-title nil)
  1330. (funcall inactivate-current-input-method-function))
  1331. (unwind-protect
  1332. (run-hooks 'input-method-inactivate-hook)
  1333. (setq current-input-method nil)
  1334. (force-mode-line-update)))))
  1335. (defun set-input-method (input-method &optional interactive)
  1336. "Select and activate input method INPUT-METHOD for the current buffer.
  1337. This also sets the default input method to the one you specify.
  1338. If INPUT-METHOD is nil, this function turns off the input method, and
  1339. also causes you to be prompted for a name of an input method the next
  1340. time you invoke \\[toggle-input-method].
  1341. When called interactively, the optional arg INTERACTIVE is non-nil,
  1342. which marks the variable `default-input-method' as set for Custom buffers.
  1343. To deactivate the input method interactively, use \\[toggle-input-method].
  1344. To deactivate it programmatically, use `inactivate-input-method'."
  1345. (interactive
  1346. (let* ((default (or (car input-method-history) default-input-method)))
  1347. (list (read-input-method-name
  1348. (if default "Select input method (default %s): " "Select input method: ")
  1349. default t)
  1350. t)))
  1351. (activate-input-method input-method)
  1352. (setq default-input-method input-method)
  1353. (when interactive
  1354. (customize-mark-as-set 'default-input-method))
  1355. default-input-method)
  1356. (defvar toggle-input-method-active nil
  1357. "Non-nil inside `toggle-input-method'.")
  1358. (defun toggle-input-method (&optional arg interactive)
  1359. "Enable or disable multilingual text input method for the current buffer.
  1360. Only one input method can be enabled at any time in a given buffer.
  1361. The normal action is to enable an input method if none was enabled,
  1362. and disable the current one otherwise. Which input method to enable
  1363. can be determined in various ways--either the one most recently used,
  1364. or the one specified by `default-input-method', or as a last resort
  1365. by reading the name of an input method in the minibuffer.
  1366. With a prefix argument ARG, read an input method name with the minibuffer
  1367. and enable that one. The default is the most recent input method specified
  1368. \(not including the currently active input method, if any).
  1369. When called interactively, the optional argument INTERACTIVE is non-nil,
  1370. which marks the variable `default-input-method' as set for Custom buffers."
  1371. (interactive "P\np")
  1372. (if toggle-input-method-active
  1373. (error "Recursive use of `toggle-input-method'"))
  1374. (if (and current-input-method (not arg))
  1375. (inactivate-input-method)
  1376. (let ((toggle-input-method-active t)
  1377. (default (or (car input-method-history) default-input-method)))
  1378. (if (and arg default (equal current-input-method default)
  1379. (> (length input-method-history) 1))
  1380. (setq default (nth 1 input-method-history)))
  1381. (activate-input-method
  1382. (if (or arg (not default))
  1383. (progn
  1384. (read-input-method-name
  1385. (if default "Input method (default %s): " "Input method: " )
  1386. default t))
  1387. default))
  1388. (unless default-input-method
  1389. (prog1
  1390. (setq default-input-method current-input-method)
  1391. (when interactive
  1392. (customize-mark-as-set 'default-input-method)))))))
  1393. (autoload 'help-buffer "help-mode")
  1394. (defun describe-input-method (input-method)
  1395. "Describe input method INPUT-METHOD."
  1396. (interactive
  1397. (list (read-input-method-name
  1398. "Describe input method (default current choice): ")))
  1399. (if (and input-method (symbolp input-method))
  1400. (setq input-method (symbol-name input-method)))
  1401. (help-setup-xref (list #'describe-input-method
  1402. (or input-method current-input-method))
  1403. (called-interactively-p 'interactive))
  1404. (if (null input-method)
  1405. (describe-current-input-method)
  1406. (let ((current current-input-method))
  1407. (condition-case nil
  1408. (progn
  1409. (save-excursion
  1410. (activate-input-method input-method)
  1411. (describe-current-input-method))
  1412. (activate-input-method current))
  1413. (error
  1414. (activate-input-method current)
  1415. (help-setup-xref (list #'describe-input-method input-method)
  1416. (called-interactively-p 'interactive))
  1417. (with-output-to-temp-buffer (help-buffer)
  1418. (let ((elt (assoc input-method input-method-alist)))
  1419. (princ (format
  1420. "Input method: %s (`%s' in mode line) for %s\n %s\n"
  1421. input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
  1422. (defun describe-current-input-method ()
  1423. "Describe the input method currently in use.
  1424. This is a subroutine for `describe-input-method'."
  1425. (if current-input-method
  1426. (if (and (symbolp describe-current-input-method-function)
  1427. (fboundp describe-current-input-method-function))
  1428. (funcall describe-current-input-method-function)
  1429. (message "No way to describe the current input method `%s'"
  1430. current-input-method)
  1431. (ding))
  1432. (error "No input method is activated now")))
  1433. (defun read-multilingual-string (prompt &optional initial-input input-method)
  1434. "Read a multilingual string from minibuffer, prompting with string PROMPT.
  1435. The input method selected last time is activated in minibuffer.
  1436. If optional second argument INITIAL-INPUT is non-nil, insert it in the
  1437. minibuffer initially.
  1438. Optional 3rd argument INPUT-METHOD specifies the input method to be activated
  1439. instead of the one selected last time. It is a symbol or a string."
  1440. (setq input-method
  1441. (or input-method
  1442. current-input-method
  1443. default-input-method
  1444. (read-input-method-name "Input method: " nil t)))
  1445. (if (and input-method (symbolp input-method))
  1446. (setq input-method (symbol-name input-method)))
  1447. (let ((prev-input-method current-input-method))
  1448. (unwind-protect
  1449. (progn
  1450. (activate-input-method input-method)
  1451. (read-string prompt initial-input nil nil t))
  1452. (activate-input-method prev-input-method))))
  1453. ;; Variables to control behavior of input methods. All input methods
  1454. ;; should react to these variables.
  1455. (defcustom input-method-verbose-flag 'default
  1456. "A flag to control extra guidance given by input methods.
  1457. The value should be nil, t, `complex-only', or `default'.
  1458. The extra guidance is done by showing list of available keys in echo
  1459. area. When you use the input method in the minibuffer, the guidance
  1460. is shown at the bottom short window (split from the existing window).
  1461. If the value is t, extra guidance is always given, if the value is
  1462. nil, extra guidance is always suppressed.
  1463. If the value is `complex-only', only complex input methods such as
  1464. `chinese-py' and `japanese' give extra guidance.
  1465. If the value is `default', complex input methods always give extra
  1466. guidance, but simple input methods give it only when you are not in
  1467. the minibuffer.
  1468. See also the variable `input-method-highlight-flag'."
  1469. :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
  1470. (const complex-only) (const default))
  1471. :group 'mule)
  1472. (defcustom input-method-highlight-flag t
  1473. "If this flag is non-nil, input methods highlight partially-entered text.
  1474. For instance, while you are in the middle of a Quail input method sequence,
  1475. the text inserted so far is temporarily underlined.
  1476. The underlining goes away when you finish or abort the input method sequence.
  1477. See also the variable `input-method-verbose-flag'."
  1478. :type 'boolean
  1479. :group 'mule)
  1480. (defcustom input-method-activate-hook nil
  1481. "Normal hook run just after an input method is activated.
  1482. The variable `current-input-method' keeps the input method name
  1483. just activated."
  1484. :type 'hook
  1485. :group 'mule)
  1486. (defcustom input-method-inactivate-hook nil
  1487. "Normal hook run just after an input method is inactivated.
  1488. The variable `current-input-method' still keeps the input method name
  1489. just inactivated."
  1490. :type 'hook
  1491. :group 'mule)
  1492. (defcustom input-method-after-insert-chunk-hook nil
  1493. "Normal hook run just after an input method insert some chunk of text."
  1494. :type 'hook
  1495. :group 'mule)
  1496. (defvar input-method-exit-on-first-char nil
  1497. "This flag controls when an input method returns.
  1498. Usually, the input method does not return while there's a possibility
  1499. that it may find a different translation if a user types another key.
  1500. But, if this flag is non-nil, the input method returns as soon as the
  1501. current key sequence gets long enough to have some valid translation.")
  1502. (defcustom input-method-use-echo-area nil
  1503. "This flag controls how an input method shows an intermediate key sequence.
  1504. Usually, the input method inserts the intermediate key sequence,
  1505. or candidate translations corresponding to the sequence,
  1506. at point in the current buffer.
  1507. But, if this flag is non-nil, it displays them in echo area instead."
  1508. :type 'hook
  1509. :group 'mule)
  1510. (defvar input-method-exit-on-invalid-key nil
  1511. "This flag controls the behavior of an input method on invalid key input.
  1512. Usually, when a user types a key which doesn't start any character
  1513. handled by the input method, the key is handled by turning off the
  1514. input method temporarily. After that key, the input method is re-enabled.
  1515. But, if this flag is non-nil, the input method is never back on.")
  1516. (defcustom set-language-environment-hook nil
  1517. "Normal hook run after some language environment is set.
  1518. When you set some hook function here, that effect usually should not
  1519. be inherited to another language environment. So, you had better set
  1520. another function in `exit-language-environment-hook' (which see) to
  1521. cancel the effect."
  1522. :type 'hook
  1523. :group 'mule)
  1524. (defcustom exit-language-environment-hook nil
  1525. "Normal hook run after exiting from some language environment.
  1526. When this hook is run, the variable `current-language-environment'
  1527. is still bound to the language environment being exited.
  1528. This hook is mainly used for canceling the effect of
  1529. `set-language-environment-hook' (which see)."
  1530. :type 'hook
  1531. :group 'mule)
  1532. (put 'setup-specified-language-environment 'apropos-inhibit t)
  1533. (defun setup-specified-language-environment ()
  1534. "Switch to a specified language environment."
  1535. (interactive)
  1536. (let (language-name)
  1537. (if (and (symbolp last-command-event)
  1538. (or (not (eq last-command-event 'Default))
  1539. (setq last-command-event 'English))
  1540. (setq language-name (symbol-name last-command-event)))
  1541. (prog1
  1542. (set-language-environment language-name)
  1543. (customize-mark-as-set 'current-language-environment))
  1544. (error "Bogus calling sequence"))))
  1545. (defcustom current-language-environment "English"
  1546. "The last language environment specified with `set-language-environment'.
  1547. This variable should be set only with \\[customize], which is equivalent
  1548. to using the function `set-language-environment'."
  1549. :link '(custom-manual "(emacs)Language Environments")
  1550. :set (lambda (symbol value) (set-language-environment value))
  1551. :get (lambda (x)
  1552. (or (car-safe (assoc-string
  1553. (if (symbolp current-language-environment)
  1554. (symbol-name current-language-environment)
  1555. current-language-environment)
  1556. language-info-alist t))
  1557. "English"))
  1558. ;; custom type will be updated with `set-language-info'.
  1559. :type (if language-info-alist
  1560. (cons 'choice (mapcar
  1561. (lambda (lang)
  1562. (list 'const lang))
  1563. (sort (mapcar 'car language-info-alist) 'string<)))
  1564. 'string)
  1565. :initialize 'custom-initialize-default
  1566. :group 'mule)
  1567. (defun reset-language-environment ()
  1568. "Reset multilingual environment of Emacs to the default status.
  1569. The default status is as follows:
  1570. The default value of `buffer-file-coding-system' is nil.
  1571. The default coding system for process I/O is nil.
  1572. The default value for the command `set-terminal-coding-system' is nil.
  1573. The default value for the command `set-keyboard-coding-system' is nil.
  1574. The order of priorities of coding systems are as follows:
  1575. utf-8
  1576. iso-2022-7bit
  1577. iso-latin-1
  1578. iso-2022-7bit-lock
  1579. iso-2022-8bit-ss2
  1580. emacs-mule
  1581. raw-text"
  1582. (interactive)
  1583. ;; This function formerly set default-enable-multibyte-characters to t,
  1584. ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
  1585. (set-coding-system-priority
  1586. 'utf-8
  1587. 'iso-2022-7bit
  1588. 'iso-latin-1
  1589. 'iso-2022-7bit-lock
  1590. 'iso-2022-8bit-ss2
  1591. 'emacs-mule
  1592. 'raw-text)
  1593. (set-default-coding-systems nil)
  1594. (setq default-sendmail-coding-system 'iso-latin-1)
  1595. ;; On Darwin systems, this should be utf-8, but when this file is loaded
  1596. ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
  1597. (setq default-file-name-coding-system 'iso-latin-1)
  1598. ;; Preserve eol-type from existing default-process-coding-systems.
  1599. ;; On non-unix-like systems in particular, these may have been set
  1600. ;; carefully by the user, or by the startup code, to deal with the
  1601. ;; users shell appropriately, so should not be altered by changing
  1602. ;; language environment.
  1603. (let ((output-coding
  1604. ;; When bootstrapping, coding-systems are not defined yet, so
  1605. ;; we need to catch the error from check-coding-system.
  1606. (condition-case nil
  1607. (coding-system-change-text-conversion
  1608. (car default-process-coding-system) 'undecided)
  1609. (coding-system-error 'undecided)))
  1610. (input-coding
  1611. (condition-case nil
  1612. (coding-system-change-text-conversion
  1613. (cdr default-process-coding-system) 'iso-latin-1)
  1614. (coding-system-error 'iso-latin-1))))
  1615. (setq default-process-coding-system
  1616. (cons output-coding input-coding)))
  1617. ;; Put the highest priority to the charset iso-8859-1 to prefer the
  1618. ;; registry iso8859-1 over iso8859-2 in font selection. It also
  1619. ;; makes unibyte-display-via-language-environment to use iso-8859-1
  1620. ;; as the unibyte charset.
  1621. (set-charset-priority 'iso-8859-1)
  1622. ;; Don't alter the terminal and keyboard coding systems here.
  1623. ;; The terminal still supports the same coding system
  1624. ;; that it supported a minute ago.
  1625. ;; (set-terminal-coding-system-internal nil)
  1626. ;; (set-keyboard-coding-system-internal nil)
  1627. ;; Back in Emacs-20, it was necessary to provide some fallback implicit
  1628. ;; conversion, because almost no packages handled coding-system issues.
  1629. ;; Nowadays it'd just paper over bugs.
  1630. ;; (set-unibyte-charset 'iso-8859-1)
  1631. )
  1632. (reset-language-environment)
  1633. (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system display)
  1634. "Set up the display table and terminal coding system for LANGUAGE-NAME."
  1635. (let ((coding (get-language-info language-name 'unibyte-display)))
  1636. (if (and coding
  1637. (or (not coding-system)
  1638. (coding-system-equal coding coding-system)))
  1639. (standard-display-european-internal)
  1640. ;; The following 2 lines undo the 8-bit display that we set up
  1641. ;; in standard-display-european-internal, which see. This is in
  1642. ;; case the user has used standard-display-european earlier in
  1643. ;; this session.
  1644. (when standard-display-table
  1645. (dotimes (i 128)
  1646. (aset standard-display-table (+ i 128) nil))))
  1647. (set-terminal-coding-system (or coding-system coding) display)))
  1648. (defun set-language-environment (language-name)
  1649. "Set up multilingual environment for using LANGUAGE-NAME.
  1650. This sets the coding system priority and the default input method
  1651. and sometimes other things. LANGUAGE-NAME should be a string
  1652. which is the name of a language environment. For example, \"Latin-1\"
  1653. specifies the character set for the major languages of Western Europe.
  1654. If there is a prior value for `current-language-environment', this
  1655. runs the hook `exit-language-environment-hook'. After setting up
  1656. the new language environment, it runs `set-language-environment-hook'."
  1657. (interactive (list (read-language-name
  1658. nil
  1659. "Set language environment (default English): ")))
  1660. (if language-name
  1661. (if (symbolp language-name)
  1662. (setq language-name (symbol-name language-name)))
  1663. (setq language-name "English"))
  1664. (let ((slot (assoc-string language-name language-info-alist t)))
  1665. (unless slot
  1666. (error "Language environment not defined: %S" language-name))
  1667. (setq language-name (car slot)))
  1668. (if current-language-environment
  1669. (let ((func (get-language-info current-language-environment
  1670. 'exit-function)))
  1671. (run-hooks 'exit-language-environment-hook)
  1672. (if (functionp func) (funcall func))))
  1673. (reset-language-environment)
  1674. ;; The features might set up coding systems.
  1675. (let ((required-features (get-language-info language-name 'features)))
  1676. (while required-features
  1677. (require (car required-features))
  1678. (setq required-features (cdr required-features))))
  1679. (setq current-language-environment language-name)
  1680. (set-language-environment-coding-systems language-name)
  1681. (set-language-environment-input-method language-name)
  1682. (set-language-environment-nonascii-translation language-name)
  1683. (set-language-environment-charset language-name)
  1684. ;; Unibyte setups if necessary.
  1685. (unless (default-value 'enable-multibyte-characters)
  1686. (set-language-environment-unibyte language-name))
  1687. (let ((func (get-language-info language-name 'setup-function)))
  1688. (if (functionp func)
  1689. (funcall func)))
  1690. (setq current-iso639-language
  1691. (or (get-language-info language-name 'iso639-language)
  1692. current-iso639-language))
  1693. (run-hooks 'set-language-environment-hook)
  1694. (force-mode-line-update t))
  1695. (define-widget 'charset 'symbol
  1696. "An Emacs charset."
  1697. :tag "Charset"
  1698. :completions
  1699. (lambda (string pred action)
  1700. (let ((completion-ignore-case t))
  1701. (completion-table-with-predicate
  1702. obarray #'charsetp 'strict string pred action)))
  1703. :value 'ascii
  1704. :validate (lambda (widget)
  1705. (unless (charsetp (widget-value widget))
  1706. (widget-put widget :error (format "Invalid charset: %S"
  1707. (widget-value widget)))
  1708. widget))
  1709. :prompt-history 'charset-history)
  1710. (defcustom language-info-custom-alist nil
  1711. "Customizations of language environment parameters.
  1712. Value is an alist with elements like those of `language-info-alist'.
  1713. These are used to set values in `language-info-alist' which replace
  1714. the defaults. A typical use is replacing the default input method for
  1715. the environment. Use \\[describe-language-environment] to find the environment's settings.
  1716. This option is intended for use at startup. Removing items doesn't
  1717. remove them from the language info until you next restart Emacs.
  1718. Setting this variable directly does not take effect.
  1719. See `set-language-info-alist' for use in programs."
  1720. :group 'mule
  1721. :version "23.1"
  1722. :set (lambda (s v)
  1723. (custom-set-default s v)
  1724. ;; Can't do this before language environments are set up.
  1725. (when v
  1726. ;; modify language-info-alist
  1727. (dolist (elt v)
  1728. (set-language-info-alist (car elt) (cdr elt)))
  1729. ;; re-set the environment in case its parameters changed
  1730. (set-language-environment current-language-environment)))
  1731. :type `(alist
  1732. :key-type (string :tag "Language environment"
  1733. :completions
  1734. (lambda (string pred action)
  1735. (let ((completion-ignore-case t))
  1736. (complete-with-action
  1737. action language-info-alist string pred))))
  1738. :value-type
  1739. (alist :key-type symbol
  1740. :options ((documentation string)
  1741. (charset (repeat charset))
  1742. (sample-text string)
  1743. (setup-function function)
  1744. (exit-function function)
  1745. (coding-system (repeat coding-system))
  1746. (coding-priority (repeat coding-system))
  1747. (nonascii-translation charset)
  1748. (input-method mule-input-method-string)
  1749. (features (repeat symbol))
  1750. (unibyte-display coding-system)))))
  1751. (declare-function x-server-vendor "xfns.c" (&optional terminal))
  1752. (declare-function x-server-version "xfns.c" (&optional terminal))
  1753. (defun standard-display-european-internal ()
  1754. ;; Actually set up direct output of non-ASCII characters.
  1755. (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
  1756. ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
  1757. ;; the native font, and codes 160 and 146 stand for something very
  1758. ;; different there.
  1759. (or (and (eq window-system 'pc) (not (default-value
  1760. 'enable-multibyte-characters)))
  1761. (progn
  1762. ;; Most X fonts used to do the wrong thing for latin-1 code 160.
  1763. (unless (and (eq window-system 'x)
  1764. ;; XFree86 4 has fixed the fonts.
  1765. (string= "The XFree86 Project, Inc" (x-server-vendor))
  1766. (> (aref (number-to-string (nth 2 (x-server-version))) 0)
  1767. ?3))
  1768. ;; Make non-line-break space display as a plain space.
  1769. (aset standard-display-table (unibyte-char-to-multibyte 160) [32]))
  1770. ;; Most Windows programs send out apostrophes as \222. Most X fonts
  1771. ;; don't contain a character at that position. Map it to the ASCII
  1772. ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
  1773. ;; U+2019, normally from the windows-1252 character set. XFree 4
  1774. ;; fonts probably have the appropriate glyph at this position,
  1775. ;; so they could use standard-display-8bit. It's better to use a
  1776. ;; proper windows-1252 coding system. --fx]
  1777. (aset standard-display-table (unibyte-char-to-multibyte 146) [39]))))
  1778. (defun set-language-environment-coding-systems (language-name)
  1779. "Do various coding system setups for language environment LANGUAGE-NAME."
  1780. (let* ((priority (get-language-info language-name 'coding-priority))
  1781. (default-coding (car priority))
  1782. ;; If the default buffer-file-coding-system is nil, don't use
  1783. ;; coding-system-eol-type, because it treats nil as
  1784. ;; `no-conversion'. The default buffer-file-coding-system is set
  1785. ;; to nil by reset-language-environment, and in that case we
  1786. ;; want to have here the native EOL type for each platform.
  1787. ;; FIXME: there should be a common code that runs both on
  1788. ;; startup and here to set the default EOL type correctly.
  1789. ;; Right now, DOS/Windows platforms set this on dos-w32.el,
  1790. ;; which works only as long as the order of loading files at
  1791. ;; dump time and calling functions at startup is not modified
  1792. ;; significantly, i.e. as long as this function is called
  1793. ;; _after_ the default buffer-file-coding-system was set by
  1794. ;; dos-w32.el.
  1795. (eol-type
  1796. (coding-system-eol-type
  1797. (or (default-value 'buffer-file-coding-system)
  1798. (if (memq system-type '(windows-nt ms-dos)) 'dos 'unix)))))
  1799. (when priority
  1800. (set-default-coding-systems
  1801. (if (memq eol-type '(0 1 2 unix dos mac))
  1802. (coding-system-change-eol-conversion default-coding eol-type)
  1803. default-coding))
  1804. (setq default-sendmail-coding-system default-coding)
  1805. (apply 'set-coding-system-priority priority))))
  1806. (defun set-language-environment-input-method (language-name)
  1807. "Do various input method setups for language environment LANGUAGE-NAME."
  1808. (let ((input-method (get-language-info language-name 'input-method)))
  1809. (when input-method
  1810. (setq default-input-method input-method)
  1811. (if input-method-history
  1812. (setq input-method-history
  1813. (cons input-method
  1814. (delete input-method input-method-history)))))))
  1815. (defun set-language-environment-nonascii-translation (language-name)
  1816. "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
  1817. ;; Note: For DOS, we assumed that the charset cpXXX is already
  1818. ;; defined.
  1819. (let ((nonascii (get-language-info language-name 'nonascii-translation)))
  1820. (if (eq window-system 'pc)
  1821. (setq nonascii (intern (format "cp%d" dos-codepage))))
  1822. (or (and (charsetp nonascii)
  1823. (get-charset-property nonascii :ascii-compatible-p))
  1824. (setq nonascii 'iso-8859-1))
  1825. ;; Back in Emacs-20, it was necessary to provide some fallback implicit
  1826. ;; conversion, because almost no packages handled coding-system issues.
  1827. ;; Nowadays it'd just paper over bugs.
  1828. ;; (set-unibyte-charset nonascii)
  1829. ))
  1830. (defun set-language-environment-charset (language-name)
  1831. "Do various charset setups for language environment LANGUAGE-NAME."
  1832. ;; Put higher priorities to such charsets that are supported by the
  1833. ;; coding systems of higher priorities in this environment.
  1834. (let ((charsets (get-language-info language-name 'charset)))
  1835. (dolist (coding (get-language-info language-name 'coding-priority))
  1836. (let ((list (coding-system-charset-list coding)))
  1837. (if (consp list)
  1838. (setq charsets (append charsets list)))))
  1839. (if charsets
  1840. (apply 'set-charset-priority charsets))))
  1841. (defun set-language-environment-unibyte (language-name)
  1842. "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
  1843. (set-display-table-and-terminal-coding-system language-name))
  1844. (defun princ-list (&rest args)
  1845. "Print all arguments with `princ', then print \"\\n\"."
  1846. (mapc #'princ args)
  1847. (princ "\n"))
  1848. (make-obsolete 'princ-list "use mapc and princ instead" "23.3")
  1849. (put 'describe-specified-language-support 'apropos-inhibit t)
  1850. ;; Print language-specific information such as input methods,
  1851. ;; charsets, and coding systems. This function is intended to be
  1852. ;; called from the menu:
  1853. ;; [menu-bar mule describe-language-environment LANGUAGE]
  1854. ;; and should not run it by `M-x describe-current-input-method-function'.
  1855. (defun describe-specified-language-support ()
  1856. "Describe how Emacs supports the specified language environment."
  1857. (interactive)
  1858. (let (language-name)
  1859. (if (not (and (symbolp last-command-event)
  1860. (or (not (eq last-command-event 'Default))
  1861. (setq last-command-event 'English))
  1862. (setq language-name (symbol-name last-command-event))))
  1863. (error "This command should only be called from the menu bar"))
  1864. (describe-language-environment language-name)))
  1865. (defun describe-language-environment (language-name)
  1866. "Describe how Emacs supports language environment LANGUAGE-NAME."
  1867. (interactive
  1868. (list (read-language-name
  1869. 'documentation
  1870. "Describe language environment (default current choice): ")))
  1871. (if (null language-name)
  1872. (setq language-name current-language-environment))
  1873. (if (or (null language-name)
  1874. (null (get-language-info language-name 'documentation)))
  1875. (error "No documentation for the specified language"))
  1876. (if (symbolp language-name)
  1877. (setq language-name (symbol-name language-name)))
  1878. (dolist (feature (get-language-info language-name 'features))
  1879. (require feature))
  1880. (let ((doc (get-language-info language-name 'documentation)))
  1881. (help-setup-xref (list #'describe-language-environment language-name)
  1882. (called-interactively-p 'interactive))
  1883. (with-output-to-temp-buffer (help-buffer)
  1884. (with-current-buffer standard-output
  1885. (insert language-name " language environment\n\n")
  1886. (if (stringp doc)
  1887. (insert doc "\n\n"))
  1888. (condition-case nil
  1889. (let ((str (eval (get-language-info language-name 'sample-text))))
  1890. (if (stringp str)
  1891. (insert "Sample text:\n "
  1892. (replace-regexp-in-string "\n" "\n " str)
  1893. "\n\n")))
  1894. (error nil))
  1895. (let ((input-method (get-language-info language-name 'input-method))
  1896. (l (copy-sequence input-method-alist))
  1897. (first t))
  1898. (when (and input-method
  1899. (setq input-method (assoc input-method l)))
  1900. (insert "Input methods (default " (car input-method) ")\n")
  1901. (setq l (cons input-method (delete input-method l))
  1902. first nil))
  1903. (dolist (elt l)
  1904. (when (or (eq input-method elt)
  1905. (eq t (compare-strings language-name nil nil
  1906. (nth 1 elt) nil nil t)))
  1907. (when first
  1908. (insert "Input methods:\n")
  1909. (setq first nil))
  1910. (insert " " (car elt))
  1911. (search-backward (car elt))
  1912. (help-xref-button 0 'help-input-method (car elt))
  1913. (goto-char (point-max))
  1914. (insert " (\""
  1915. (if (stringp (nth 3 elt)) (nth 3 elt) (car (nth 3 elt)))
  1916. "\" in mode line)\n")))
  1917. (or first
  1918. (insert "\n")))
  1919. (insert "Character sets:\n")
  1920. (let ((l (get-language-info language-name 'charset)))
  1921. (if (null l)
  1922. (insert " nothing specific to " language-name "\n")
  1923. (while l
  1924. (insert " " (symbol-name (car l)))
  1925. (search-backward (symbol-name (car l)))
  1926. (help-xref-button 0 'help-character-set (car l))
  1927. (goto-char (point-max))
  1928. (insert ": " (charset-description (car l)) "\n")
  1929. (setq l (cdr l)))))
  1930. (insert "\n")
  1931. (insert "Coding systems:\n")
  1932. (let ((l (get-language-info language-name 'coding-system)))
  1933. (if (null l)
  1934. (insert " nothing specific to " language-name "\n")
  1935. (while l
  1936. (insert " " (symbol-name (car l)))
  1937. (search-backward (symbol-name (car l)))
  1938. (help-xref-button 0 'help-coding-system (car l))
  1939. (goto-char (point-max))
  1940. (insert " (`"
  1941. (coding-system-mnemonic (car l))
  1942. "' in mode line):\n\t"
  1943. (coding-system-doc-string (car l))
  1944. "\n")
  1945. (let ((aliases (coding-system-aliases (car l))))
  1946. (when aliases
  1947. (insert "\t(alias:")
  1948. (while aliases
  1949. (insert " " (symbol-name (car aliases)))
  1950. (setq aliases (cdr aliases)))
  1951. (insert ")\n")))
  1952. (setq l (cdr l)))))))))
  1953. ;;; Locales.
  1954. (defvar locale-translation-file-name nil
  1955. "File name for the system's file of locale-name aliases, or nil if none.")
  1956. ;; The following definitions might as well be marked as constants and
  1957. ;; purecopied, since they're normally used on startup, and probably
  1958. ;; should reflect the facilities of the base Emacs.
  1959. (defconst locale-language-names
  1960. (purecopy
  1961. '(
  1962. ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
  1963. ;; as specified in the Single Unix Spec, Version 2.
  1964. ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
  1965. ;; with additions from ISO 639/RA Newsletter No.1/1989;
  1966. ;; see Internet RFC 2165 (1997-06) and
  1967. ;; http://www.evertype.com/standards/iso639/iso639-en.html
  1968. ;; TERRITORY is a country code taken from ISO 3166
  1969. ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
  1970. ;; CODESET and MODIFIER are implementation-dependent.
  1971. ;; jasonr comments: MS Windows uses three letter codes for
  1972. ;; languages instead of the two letter ISO codes that POSIX
  1973. ;; uses. In most cases the first two letters are the same, so
  1974. ;; most of the regexps in locale-language-names work. Japanese
  1975. ;; and Chinese are exceptions, which are listed in the
  1976. ;; non-standard section at the bottom of locale-language-names.
  1977. ("aa_DJ" . "Latin-1") ; Afar
  1978. ("aa" . "UTF-8")
  1979. ;; ab Abkhazian
  1980. ("af" . "Latin-1") ; Afrikaans
  1981. ("am" "Ethiopic" utf-8) ; Amharic
  1982. ("an" . "Latin-9") ; Aragonese
  1983. ("ar" . "Arabic")
  1984. ; as Assamese
  1985. ; ay Aymara
  1986. ("az" . "UTF-8") ; Azerbaijani
  1987. ; ba Bashkir
  1988. ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
  1989. ("bg" "Bulgarian" cp1251) ; Bulgarian
  1990. ; bh Bihari
  1991. ; bi Bislama
  1992. ("bn" . "UTF-8") ; Bengali, Bangla
  1993. ("bo" . "Tibetan")
  1994. ("br" . "Latin-1") ; Breton
  1995. ("bs" . "Latin-2") ; Bosnian
  1996. ("byn" . "UTF-8") ; Bilin; Blin
  1997. ("ca" . "Latin-1") ; Catalan
  1998. ; co Corsican
  1999. ("cs" "Czech" iso-8859-2)
  2000. ("cy" "Welsh" iso-8859-14)
  2001. ("da" . "Latin-1") ; Danish
  2002. ("de" "German" iso-8859-1)
  2003. ; dv Divehi
  2004. ; dz Bhutani
  2005. ("el" "Greek" iso-8859-7)
  2006. ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
  2007. ;; That's actually what the GNU locales define, modulo things like
  2008. ;; en_IN -- fx.
  2009. ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
  2010. ("en" "English" iso-8859-1) ; English
  2011. ("eo" . "Esperanto") ; Esperanto
  2012. ("es" "Spanish" iso-8859-1)
  2013. ("et" . "Latin-1") ; Estonian
  2014. ("eu" . "Latin-1") ; Basque
  2015. ("fa" . "UTF-8") ; Persian
  2016. ("fi" . "Latin-1") ; Finnish
  2017. ("fj" . "Latin-1") ; Fiji
  2018. ("fo" . "Latin-1") ; Faroese
  2019. ("fr" "French" iso-8859-1) ; French
  2020. ("fy" . "Latin-1") ; Frisian
  2021. ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
  2022. ("gd" . "Latin-9") ; Scots Gaelic
  2023. ("gez" "Ethiopic" utf-8) ; Geez
  2024. ("gl" . "Latin-1") ; Gallegan; Galician
  2025. ; gn Guarani
  2026. ("gu" . "UTF-8") ; Gujarati
  2027. ("gv" . "Latin-1") ; Manx Gaelic
  2028. ; ha Hausa
  2029. ("he" "Hebrew" iso-8859-8)
  2030. ("hi" "Devanagari" utf-8) ; Hindi
  2031. ("hr" "Croatian" iso-8859-2) ; Croatian
  2032. ("hu" . "Latin-2") ; Hungarian
  2033. ; hy Armenian
  2034. ; ia Interlingua
  2035. ("id" . "Latin-1") ; Indonesian
  2036. ; ie Interlingue
  2037. ; ik Inupiak
  2038. ("is" . "Latin-1") ; Icelandic
  2039. ("it" "Italian" iso-8859-1) ; Italian
  2040. ; iu Inuktitut
  2041. ("iw" "Hebrew" iso-8859-8)
  2042. ("ja" "Japanese" euc-jp)
  2043. ; jw Javanese
  2044. ("ka" "Georgian" georgian-ps) ; Georgian
  2045. ; kk Kazakh
  2046. ("kl" . "Latin-1") ; Greenlandic
  2047. ; km Cambodian
  2048. ("kn" "Kannada" utf-8)
  2049. ("ko" "Korean" euc-kr)
  2050. ; ks Kashmiri
  2051. ; ku Kurdish
  2052. ("kw" . "Latin-1") ; Cornish
  2053. ; ky Kirghiz
  2054. ("la" . "Latin-1") ; Latin
  2055. ("lb" . "Latin-1") ; Luxemburgish
  2056. ("lg" . "Laint-6") ; Ganda
  2057. ; ln Lingala
  2058. ("lo" "Lao" utf-8) ; Laothian
  2059. ("lt" "Lithuanian" iso-8859-13)
  2060. ("lv" . "Latvian") ; Latvian, Lettish
  2061. ; mg Malagasy
  2062. ("mi" . "Latin-7") ; Maori
  2063. ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
  2064. ("ml" "Malayalam" utf-8)
  2065. ("mn" . "UTF-8") ; Mongolian
  2066. ; mo Moldavian
  2067. ("mr" "Devanagari" utf-8) ; Marathi
  2068. ("ms" . "Latin-1") ; Malay
  2069. ("mt" . "Latin-3") ; Maltese
  2070. ; my Burmese
  2071. ; na Nauru
  2072. ("nb" . "Latin-1") ; Norwegian
  2073. ("ne" "Devanagari" utf-8) ; Nepali
  2074. ("nl" "Dutch" iso-8859-1)
  2075. ("no" . "Latin-1") ; Norwegian
  2076. ("oc" . "Latin-1") ; Occitan
  2077. ("om_ET" . "UTF-8") ; (Afan) Oromo
  2078. ("om" . "Latin-1") ; (Afan) Oromo
  2079. ; or Oriya
  2080. ("pa" . "UTF-8") ; Punjabi
  2081. ("pl" . "Latin-2") ; Polish
  2082. ; ps Pashto, Pushto
  2083. ("pt" . "Latin-1") ; Portuguese
  2084. ; qu Quechua
  2085. ("rm" . "Latin-1") ; Rhaeto-Romanic
  2086. ; rn Kirundi
  2087. ("ro" "Romanian" iso-8859-2)
  2088. ("ru_RU" "Russian" iso-8859-5)
  2089. ("ru_UA" "Russian" koi8-u)
  2090. ; rw Kinyarwanda
  2091. ("sa" . "Devanagari") ; Sanskrit
  2092. ; sd Sindhi
  2093. ("se" . "UTF-8") ; Northern Sami
  2094. ; sg Sangho
  2095. ("sh" . "Latin-2") ; Serbo-Croatian
  2096. ; si Sinhalese
  2097. ("sid" . "UTF-8") ; Sidamo
  2098. ("sk" "Slovak" iso-8859-2)
  2099. ("sl" "Slovenian" iso-8859-2)
  2100. ; sm Samoan
  2101. ; sn Shona
  2102. ("so_ET" "UTF-8") ; Somali
  2103. ("so" "Latin-1") ; Somali
  2104. ("sq" . "Latin-1") ; Albanian
  2105. ("sr" . "Latin-2") ; Serbian (Latin alphabet)
  2106. ; ss Siswati
  2107. ("st" . "Latin-1") ; Sesotho
  2108. ; su Sundanese
  2109. ("sv" "Swedish" iso-8859-1) ; Swedish
  2110. ("sw" . "Latin-1") ; Swahili
  2111. ("ta" "Tamil" utf-8)
  2112. ("te" . "UTF-8") ; Telugu
  2113. ("tg" "Tajik" koi8-t)
  2114. ("th" "Thai" tis-620)
  2115. ("ti" "Ethiopic" utf-8) ; Tigrinya
  2116. ("tig_ER" . "UTF-8") ; Tigre
  2117. ; tk Turkmen
  2118. ("tl" . "Latin-1") ; Tagalog
  2119. ; tn Setswana
  2120. ; to Tonga
  2121. ("tr" "Turkish" iso-8859-9)
  2122. ; ts Tsonga
  2123. ("tt" . "UTF-8") ; Tatar
  2124. ; tw Twi
  2125. ; ug Uighur
  2126. ("uk" "Ukrainian" koi8-u)
  2127. ("ur" . "UTF-8") ; Urdu
  2128. ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
  2129. ("uz" . "Latin-1") ; Uzbek
  2130. ("vi" "Vietnamese" utf-8)
  2131. ; vo Volapuk
  2132. ("wa" . "Latin-1") ; Walloon
  2133. ; wo Wolof
  2134. ("xh" . "Latin-1") ; Xhosa
  2135. ("yi" . "Windows-1255") ; Yiddish
  2136. ; yo Yoruba
  2137. ; za Zhuang
  2138. ("zh_HK" . "Chinese-Big5")
  2139. ; zh_HK/BIG5-HKSCS \
  2140. ("zh_TW" . "Chinese-Big5")
  2141. ("zh_CN.GB2312" "Chinese-GB")
  2142. ("zh_CN.GBK" "Chinese-GBK")
  2143. ("zh_CN.GB18030" "Chinese-GB18030")
  2144. ("zh_CN.UTF-8" . "Chinese-GBK")
  2145. ("zh_CN" . "Chinese-GB")
  2146. ("zh" . "Chinese-GB")
  2147. ("zu" . "Latin-1") ; Zulu
  2148. ;; ISO standard locales
  2149. ("c$" . "ASCII")
  2150. ("posix$" . "ASCII")
  2151. ;; The "IPA" Emacs language environment does not correspond
  2152. ;; to any ISO 639 code, so let it stand for itself.
  2153. ("ipa$" . "IPA")
  2154. ;; Nonstandard or obsolete language codes
  2155. ("cz" . "Czech") ; e.g. Solaris 2.6
  2156. ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
  2157. ("iw" . "Hebrew") ; e.g. X11R6.4
  2158. ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
  2159. ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
  2160. ("jp" . "Japanese") ; e.g. MS Windows
  2161. ("chs" . "Chinese-GBK") ; MS Windows Chinese Simplified
  2162. ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
  2163. ("gbz" . "UTF-8") ; MS Windows Dari Persian
  2164. ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
  2165. ("wee" . "Latin-2") ; MS Windows Lower Sorbian
  2166. ("wen" . "Latin-2") ; MS Windows Upper Sorbian
  2167. ))
  2168. "Alist of locale regexps vs the corresponding languages and coding systems.
  2169. Each element has this form:
  2170. \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
  2171. The first element whose LOCALE-REGEXP matches the start of a
  2172. downcased locale specifies the LANG-ENV \(language environment)
  2173. and CODING-SYSTEM corresponding to that locale. If there is no
  2174. appropriate language environment, the element may have this form:
  2175. \(LOCALE-REGEXP . LANG-ENV)
  2176. In this case, LANG-ENV is one of generic language environments for an
  2177. specific encoding such as \"Latin-1\" and \"UTF-8\".")
  2178. (defconst locale-charset-language-names
  2179. (purecopy
  2180. '((".*8859[-_]?1\\>" . "Latin-1")
  2181. (".*8859[-_]?2\\>" . "Latin-2")
  2182. (".*8859[-_]?3\\>" . "Latin-3")
  2183. (".*8859[-_]?4\\>" . "Latin-4")
  2184. (".*8859[-_]?9\\>" . "Latin-5")
  2185. (".*8859[-_]?14\\>" . "Latin-8")
  2186. (".*8859[-_]?15\\>" . "Latin-9")
  2187. (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
  2188. ;; utf-8@euro exists, so put this last. (@euro really specifies
  2189. ;; the currency, rather than the charset.)
  2190. (".*@euro\\>" . "Latin-9")))
  2191. "List of pairs of locale regexps and charset language names.
  2192. The first element whose locale regexp matches the start of a downcased locale
  2193. specifies the language name whose charset corresponds to that locale.
  2194. This language name is used if the locale is not listed in
  2195. `locale-language-names'.")
  2196. (defconst locale-preferred-coding-systems
  2197. (purecopy
  2198. '((".*8859[-_]?1\\>" . iso-8859-1)
  2199. (".*8859[-_]?2\\>" . iso-8859-2)
  2200. (".*8859[-_]?3\\>" . iso-8859-3)
  2201. (".*8859[-_]?4\\>" . iso-8859-4)
  2202. (".*8859[-_]?9\\>" . iso-8859-9)
  2203. (".*8859[-_]?14\\>" . iso-8859-14)
  2204. (".*8859[-_]?15\\>" . iso-8859-15)
  2205. (".*utf\\(?:-?8\\)?" . utf-8)
  2206. ;; utf-8@euro exists, so put this after utf-8. (@euro really
  2207. ;; specifies the currency, rather than the charset.)
  2208. (".*@euro" . iso-8859-15)
  2209. ("koi8-?r" . koi8-r)
  2210. ("koi8-?u" . koi8-u)
  2211. ("tcvn" . tcvn)
  2212. ("big5[-_]?hkscs" . big5-hkscs)
  2213. ("big5" . big5)
  2214. ("euc-?tw" . euc-tw)
  2215. ("euc-?cn" . euc-cn)
  2216. ("gb2312" . gb2312)
  2217. ("gbk" . gbk)
  2218. ("gb18030" . gb18030)
  2219. ("ja.*[._]euc" . japanese-iso-8bit)
  2220. ("ja.*[._]jis7" . iso-2022-jp)
  2221. ("ja.*[._]pck" . japanese-shift-jis)
  2222. ("ja.*[._]sjis" . japanese-shift-jis)
  2223. ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
  2224. ))
  2225. "List of pairs of locale regexps and preferred coding systems.
  2226. The first element whose locale regexp matches the start of a downcased locale
  2227. specifies the coding system to prefer when using that locale.
  2228. This coding system is used if the locale specifies a specific charset.")
  2229. (defun locale-name-match (key alist)
  2230. "Search for KEY in ALIST, which should be a list of regexp-value pairs.
  2231. Return the value corresponding to the first regexp that matches the
  2232. start of KEY, or nil if there is no match."
  2233. (let (element)
  2234. (while (and alist (not element))
  2235. (if (string-match-p (concat "\\`\\(?:" (car (car alist)) "\\)") key)
  2236. (setq element (car alist)))
  2237. (setq alist (cdr alist)))
  2238. (cdr element)))
  2239. (defun locale-charset-match-p (charset1 charset2)
  2240. "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
  2241. Matching is done ignoring case and any hyphens and underscores in the
  2242. names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
  2243. (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
  2244. (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
  2245. (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
  2246. (defvar locale-charset-alist nil
  2247. "Coding system alist keyed on locale-style charset name.
  2248. Used by `locale-charset-to-coding-system'.")
  2249. (defun locale-charset-to-coding-system (charset)
  2250. "Find coding system corresponding to CHARSET.
  2251. CHARSET is any sort of non-Emacs charset name, such as might be used
  2252. in a locale codeset, or elsewhere. It is matched to a coding system
  2253. first by case-insensitive lookup in `locale-charset-alist'. Then
  2254. matches are looked for in the coding system list, treating case and
  2255. the characters `-' and `_' as insignificant. The coding system base
  2256. is returned. Thus, for instance, if charset \"ISO8859-2\",
  2257. `iso-latin-2' is returned."
  2258. (or (car (assoc-string charset locale-charset-alist t))
  2259. (let ((cs coding-system-alist)
  2260. c)
  2261. (while (and (not c) cs)
  2262. (if (locale-charset-match-p charset (caar cs))
  2263. (setq c (intern (caar cs)))
  2264. (pop cs)))
  2265. (if c (coding-system-base c)))))
  2266. ;; Fixme: This ought to deal with the territory part of the locale
  2267. ;; too, for setting things such as calendar holidays, ps-print paper
  2268. ;; size, spelling dictionary.
  2269. (defun locale-translate (locale)
  2270. "Expand LOCALE according to `locale-translation-file-name', if possible.
  2271. For example, translate \"swedish\" into \"sv_SE.ISO8859-1\"."
  2272. (if locale-translation-file-name
  2273. (with-temp-buffer
  2274. (set-buffer-multibyte nil)
  2275. (insert-file-contents locale-translation-file-name)
  2276. (if (re-search-forward
  2277. (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
  2278. (buffer-substring (point) (line-end-position))
  2279. locale))
  2280. locale))
  2281. (defun set-locale-environment (&optional locale-name frame)
  2282. "Set up multilingual environment for using LOCALE-NAME.
  2283. This sets the language environment, the coding system priority,
  2284. the default input method and sometimes other things.
  2285. LOCALE-NAME should be a string which is the name of a locale supported
  2286. by the system. Often it is of the form xx_XX.CODE, where xx is a
  2287. language, XX is a country, and CODE specifies a character set and
  2288. coding system. For example, the locale name \"ja_JP.EUC\" might name
  2289. a locale for Japanese in Japan using the `japanese-iso-8bit'
  2290. coding-system. The name may also have a modifier suffix, e.g. `@euro'
  2291. or `@cyrillic'.
  2292. If LOCALE-NAME is nil, its value is taken from the environment
  2293. variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
  2294. The locale names supported by your system can typically be found in a
  2295. directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
  2296. will be translated according to the table specified by
  2297. `locale-translation-file-name'.
  2298. If FRAME is non-nil, only set the keyboard coding system and the
  2299. terminal coding system for the terminal of that frame, and don't
  2300. touch session-global parameters like the language environment.
  2301. See also `locale-charset-language-names', `locale-language-names',
  2302. `locale-preferred-coding-systems' and `locale-coding-system'."
  2303. (interactive "sSet environment for locale: ")
  2304. ;; Do this at runtime for the sake of binaries possibly transported
  2305. ;; to a system without X.
  2306. (setq locale-translation-file-name
  2307. (let ((files
  2308. '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
  2309. "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
  2310. "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
  2311. "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
  2312. ;;
  2313. ;; The following name appears after the X-related names above,
  2314. ;; since the X-related names are what X actually uses.
  2315. "/usr/share/locale/locale.alias" ; GNU/Linux sans X
  2316. )))
  2317. (while (and files (not (file-exists-p (car files))))
  2318. (setq files (cdr files)))
  2319. (car files)))
  2320. (let ((locale locale-name))
  2321. (unless locale
  2322. ;; Use the first of these three environment variables
  2323. ;; that has a nonempty value.
  2324. (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
  2325. (while (and vars
  2326. (= 0 (length locale))) ; nil or empty string
  2327. (setq locale (getenv (pop vars) frame)))))
  2328. (when locale
  2329. (setq locale (locale-translate locale))
  2330. ;; Leave the system locales alone if the caller did not specify
  2331. ;; an explicit locale name, as their defaults are set from
  2332. ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
  2333. ;; want to set them to the same value as LC_CTYPE.
  2334. (when locale-name
  2335. (setq system-messages-locale locale)
  2336. (setq system-time-locale locale))
  2337. (if (string-match "^[a-z][a-z]" locale)
  2338. (setq current-iso639-language (intern (match-string 0 locale)))))
  2339. (setq woman-locale
  2340. (or system-messages-locale
  2341. (let ((msglocale (getenv "LC_MESSAGES" frame)))
  2342. (if (zerop (length msglocale))
  2343. locale
  2344. (locale-translate msglocale)))))
  2345. (when locale
  2346. (setq locale (downcase locale))
  2347. (let ((language-name
  2348. (locale-name-match locale locale-language-names))
  2349. (charset-language-name
  2350. (locale-name-match locale locale-charset-language-names))
  2351. (default-eol-type (coding-system-eol-type
  2352. (default-value 'buffer-file-coding-system)))
  2353. (coding-system
  2354. (or (locale-name-match locale locale-preferred-coding-systems)
  2355. (when locale
  2356. (if (string-match "\\.\\([^@]+\\)" locale)
  2357. (locale-charset-to-coding-system
  2358. (match-string 1 locale)))))))
  2359. (if (consp language-name)
  2360. ;; locale-language-names specify both lang-env and coding.
  2361. ;; But, what specified in locale-preferred-coding-systems
  2362. ;; has higher priority.
  2363. (setq coding-system (or coding-system
  2364. (nth 1 language-name))
  2365. language-name (car language-name))
  2366. ;; Otherwise, if locale is not listed in locale-language-names,
  2367. ;; use what listed in locale-charset-language-names.
  2368. (if (not language-name)
  2369. (setq language-name charset-language-name)))
  2370. ;; If a specific EOL conversion was specified in the default
  2371. ;; buffer-file-coding-system, preserve it in the coding system
  2372. ;; we will be using from now on.
  2373. (if (and (memq default-eol-type '(0 1 2 unix dos mac))
  2374. coding-system
  2375. (coding-system-p coding-system))
  2376. (setq coding-system (coding-system-change-eol-conversion
  2377. coding-system default-eol-type)))
  2378. (when language-name
  2379. ;; Set up for this character set. This is now the right way
  2380. ;; to do it for both unibyte and multibyte modes.
  2381. (unless frame
  2382. (set-language-environment language-name))
  2383. ;; If the default enable-multibyte-characters is nil,
  2384. ;; we are using single-byte characters,
  2385. ;; so the display table and terminal coding system are irrelevant.
  2386. (when (default-value 'enable-multibyte-characters)
  2387. (set-display-table-and-terminal-coding-system
  2388. language-name coding-system frame))
  2389. ;; Set the `keyboard-coding-system' if appropriate (tty
  2390. ;; only). At least X and MS Windows can generate
  2391. ;; multilingual input.
  2392. ;; XXX This was disabled unless `window-system', but that
  2393. ;; leads to buggy behavior when a tty frame is opened
  2394. ;; later. Setting the keyboard coding system has no adverse
  2395. ;; effect on X, so let's do it anyway. -- Lorentey
  2396. (let ((kcs (or coding-system
  2397. (car (get-language-info language-name
  2398. 'coding-system)))))
  2399. (if kcs (set-keyboard-coding-system kcs frame)))
  2400. (unless frame
  2401. (setq locale-coding-system
  2402. (car (get-language-info language-name 'coding-priority)))))
  2403. (when (and (not frame)
  2404. coding-system
  2405. (not (coding-system-equal coding-system
  2406. locale-coding-system)))
  2407. (prefer-coding-system coding-system)
  2408. ;; Fixme: perhaps prefer-coding-system should set this too.
  2409. ;; But it's not the time to do such a fundamental change.
  2410. (setq default-sendmail-coding-system coding-system)
  2411. (setq locale-coding-system coding-system))))
  2412. ;; On Windows, override locale-coding-system,
  2413. ;; default-file-name-coding-system, keyboard-coding-system,
  2414. ;; terminal-coding-system with system codepage.
  2415. (when (boundp 'w32-ansi-code-page)
  2416. (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
  2417. (when (coding-system-p code-page-coding)
  2418. (unless frame (setq locale-coding-system code-page-coding))
  2419. (set-keyboard-coding-system code-page-coding frame)
  2420. (set-terminal-coding-system code-page-coding frame)
  2421. ;; Set default-file-name-coding-system last, so that Emacs
  2422. ;; doesn't try to use cpNNNN when it defines keyboard and
  2423. ;; terminal encoding. That's because the above two lines
  2424. ;; will want to load code-pages.el, where cpNNNN are
  2425. ;; defined; if default-file-name-coding-system were set to
  2426. ;; cpNNNN while these two lines run, Emacs will want to use
  2427. ;; it for encoding the file name it wants to load. And that
  2428. ;; will fail, since cpNNNN is not yet usable until
  2429. ;; code-pages.el finishes loading.
  2430. (setq default-file-name-coding-system code-page-coding))))
  2431. (when (eq system-type 'darwin)
  2432. ;; On Darwin, file names are always encoded in utf-8, no matter
  2433. ;; the locale.
  2434. (setq default-file-name-coding-system 'utf-8)
  2435. ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
  2436. ;; the locale.
  2437. (when (and (null window-system)
  2438. (equal (getenv "TERM_PROGRAM" frame) "Apple_Terminal"))
  2439. (set-terminal-coding-system 'utf-8)
  2440. (set-keyboard-coding-system 'utf-8)))
  2441. ;; Default to A4 paper if we're not in a C, POSIX or US locale.
  2442. ;; (See comments in Flocale_info.)
  2443. (unless frame
  2444. (let ((locale locale)
  2445. (paper (locale-info 'paper)))
  2446. (if paper
  2447. ;; This will always be null at the time of writing.
  2448. (cond
  2449. ((equal paper '(216 279))
  2450. (setq ps-paper-type 'letter))
  2451. ((equal paper '(210 297))
  2452. (setq ps-paper-type 'a4)))
  2453. (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
  2454. (while (and vars (= 0 (length locale)))
  2455. (setq locale (getenv (pop vars) frame))))
  2456. (when locale
  2457. ;; As of glibc 2.2.5, these are the only US Letter locales,
  2458. ;; and the rest are A4.
  2459. (setq ps-paper-type
  2460. (or (locale-name-match locale '(("c$" . letter)
  2461. ("posix$" . letter)
  2462. (".._us" . letter)
  2463. (".._pr" . letter)
  2464. (".._ca" . letter)
  2465. ("enu$" . letter) ; Windows
  2466. ("esu$" . letter)
  2467. ("enc$" . letter)
  2468. ("frc$" . letter)))
  2469. 'a4)))))))
  2470. nil)
  2471. ;;; Character property
  2472. (put 'char-code-property-table 'char-table-extra-slots 5)
  2473. (defun define-char-code-property (name table &optional docstring)
  2474. "Define NAME as a character code property given by TABLE.
  2475. TABLE is a char-table of purpose `char-code-property-table' with
  2476. these extra slots:
  2477. 1st: NAME.
  2478. 2nd: Function to call to get a property value of a character.
  2479. It is called with three arguments CHAR, VAL, and TABLE, where
  2480. CHAR is a character, VAL is the value of (aref TABLE CHAR).
  2481. 3rd: Function to call to put a property value of a character.
  2482. It is called with the same arguments as above.
  2483. 4th: Function to call to get a description string of a property value.
  2484. It is called with one argument VALUE, a property value.
  2485. 5th: Data used by the above functions.
  2486. TABLE may be a name of file to load to build a char-table. The
  2487. file should contain a call of `define-char-code-property' with a
  2488. char-table of the above format as the argument TABLE.
  2489. TABLE may also be nil, in which case no property value is pre-assigned.
  2490. Optional 3rd argument DOCSTRING is a documentation string of the property.
  2491. See also the documentation of `get-char-code-property' and
  2492. `put-char-code-property'."
  2493. (or (symbolp name)
  2494. (error "Not a symbol: %s" name))
  2495. (if (char-table-p table)
  2496. (or (and (eq (char-table-subtype table) 'char-code-property-table)
  2497. (eq (char-table-extra-slot table 0) name))
  2498. (error "Invalid char-table: %s" table))
  2499. (or (stringp table)
  2500. (error "Not a char-table nor a file name: %s" table)))
  2501. (if (stringp table) (setq table (purecopy table)))
  2502. (let ((slot (assq name char-code-property-alist)))
  2503. (if slot
  2504. (setcdr slot table)
  2505. (setq char-code-property-alist
  2506. (cons (cons name table) char-code-property-alist))))
  2507. (put name 'char-code-property-documentation (purecopy docstring)))
  2508. (defvar char-code-property-table
  2509. (make-char-table 'char-code-property-table)
  2510. "Char-table containing a property list of each character code.
  2511. This table is used for properties not listed in `char-code-property-alist'.
  2512. See also the documentation of `get-char-code-property' and
  2513. `put-char-code-property'.")
  2514. (defun get-char-code-property (char propname)
  2515. "Return the value of CHAR's PROPNAME property."
  2516. (let ((table (unicode-property-table-internal propname)))
  2517. (if table
  2518. (let ((func (char-table-extra-slot table 1)))
  2519. (if (functionp func)
  2520. (funcall func char (aref table char) table)
  2521. (get-unicode-property-internal table char)))
  2522. (plist-get (aref char-code-property-table char) propname))))
  2523. (defun put-char-code-property (char propname value)
  2524. "Store CHAR's PROPNAME property with VALUE.
  2525. It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
  2526. (let ((table (unicode-property-table-internal propname)))
  2527. (if table
  2528. (let ((func (char-table-extra-slot table 2)))
  2529. (if (functionp func)
  2530. (funcall func char value table)
  2531. (put-unicode-property-internal table char value)))
  2532. (let* ((plist (aref char-code-property-table char))
  2533. (x (plist-put plist propname value)))
  2534. (or (eq x plist)
  2535. (aset char-code-property-table char x))))
  2536. value))
  2537. (defun char-code-property-description (prop value)
  2538. "Return a description string of character property PROP's value VALUE.
  2539. If there's no description string for VALUE, return nil."
  2540. (let ((table (unicode-property-table-internal prop)))
  2541. (if table
  2542. (let ((func (char-table-extra-slot table 3)))
  2543. (if (functionp func)
  2544. (funcall func value))))))
  2545. ;; Pretty description of encoded string
  2546. ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
  2547. (defconst iso-2022-control-alist
  2548. '((?\x1b . "ESC")
  2549. (?\x0e . "SO")
  2550. (?\x0f . "SI")
  2551. (?\x8e . "SS2")
  2552. (?\x8f . "SS3")
  2553. (?\x9b . "CSI")))
  2554. (defun encoded-string-description (str coding-system)
  2555. "Return a pretty description of STR that is encoded by CODING-SYSTEM."
  2556. (setq str (string-as-unibyte str))
  2557. (mapconcat
  2558. (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
  2559. ;; Try to get a pretty description for ISO 2022 escape sequences.
  2560. (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
  2561. (format "#x%02X" x))))
  2562. (function (lambda (x) (format "#x%02X" x))))
  2563. str " "))
  2564. (defun encode-coding-char (char coding-system &optional charset)
  2565. "Encode CHAR by CODING-SYSTEM and return the resulting string.
  2566. If CODING-SYSTEM can't safely encode CHAR, return nil.
  2567. The 3rd optional argument CHARSET, if non-nil, is a charset preferred
  2568. on encoding."
  2569. (let* ((str1 (string-as-multibyte (string char)))
  2570. (str2 (string-as-multibyte (string char char)))
  2571. (found (find-coding-systems-string str1))
  2572. enc1 enc2 i1 i2)
  2573. (if (and (consp found)
  2574. (eq (car found) 'undecided))
  2575. str1
  2576. (when (memq (coding-system-base coding-system) found)
  2577. ;; We must find the encoded string of CHAR. But, just encoding
  2578. ;; CHAR will put extra control sequences (usually to designate
  2579. ;; ASCII charset) at the tail if type of CODING is ISO 2022.
  2580. ;; To exclude such tailing bytes, we at first encode one-char
  2581. ;; string and two-char string, then check how many bytes at the
  2582. ;; tail of both encoded strings are the same.
  2583. (when charset
  2584. (put-text-property 0 1 'charset charset str1)
  2585. (put-text-property 0 2 'charset charset str2))
  2586. (setq enc1 (encode-coding-string str1 coding-system)
  2587. i1 (length enc1)
  2588. enc2 (encode-coding-string str2 coding-system)
  2589. i2 (length enc2))
  2590. (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
  2591. (setq i1 (1- i1) i2 (1- i2)))
  2592. ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
  2593. ;; and they are the extra control sequences at the tail to
  2594. ;; exclude.
  2595. (substring enc2 0 i2)))))
  2596. ;; Backwards compatibility. These might be better with :init-value t,
  2597. ;; but that breaks loadup.
  2598. (define-minor-mode unify-8859-on-encoding-mode
  2599. "Exists only for backwards compatibility."
  2600. :group 'mule
  2601. :global t)
  2602. ;; Doc said "obsolete" in 23.1, this statement only added in 24.1.
  2603. (make-obsolete 'unify-8859-on-encoding-mode "don't use it." "23.1")
  2604. (define-minor-mode unify-8859-on-decoding-mode
  2605. "Exists only for backwards compatibility."
  2606. :group 'mule
  2607. :global t)
  2608. ;; Doc said "obsolete" in 23.1, this statement only added in 24.1.
  2609. (make-obsolete 'unify-8859-on-decoding-mode "don't use it." "23.1")
  2610. (defvar nonascii-insert-offset 0)
  2611. (make-obsolete-variable 'nonascii-insert-offset "do not use it." "23.1")
  2612. (defvar nonascii-translation-table nil)
  2613. (make-obsolete-variable 'nonascii-translation-table "do not use it." "23.1")
  2614. (defvar ucs-names nil
  2615. "Alist of cached (CHAR-NAME . CHAR-CODE) pairs.")
  2616. (defun ucs-names ()
  2617. "Return alist of (CHAR-NAME . CHAR-CODE) pairs cached in `ucs-names'."
  2618. (or ucs-names
  2619. (let ((bmp-ranges
  2620. '((#x0000 . #x33FF)
  2621. ;; (#x3400 . #x4DBF) CJK Ideographs Extension A
  2622. (#x4DC0 . #x4DFF)
  2623. ;; (#x4E00 . #x9FFF) CJK Unified Ideographs
  2624. (#xA000 . #xD7FF)
  2625. ;; (#xD800 . #xFAFF) Surrogate/Private
  2626. (#xFB00 . #xFFFD)))
  2627. (upper-ranges
  2628. '((#x10000 . #x134FF)
  2629. ;; (#x13500 . #x167FF) unused
  2630. (#x16800 . #x16A3F)
  2631. ;; (#x16A40 . #x1AFFF) unused
  2632. (#x1B000 . #x1B0FF)
  2633. ;; (#x1B100 . #x1CFFF) unused
  2634. (#x1D000 . #x1FFFF)
  2635. ;; (#x20000 . #xDFFFF) CJK Ideograph Extension A, B, etc, unused
  2636. (#xE0000 . #xE01FF)))
  2637. (gc-cons-threshold 10000000)
  2638. c end name names)
  2639. (dolist (range bmp-ranges)
  2640. (setq c (car range)
  2641. end (cdr range))
  2642. (while (<= c end)
  2643. (if (setq name (get-char-code-property c 'name))
  2644. (push (cons name c) names))
  2645. (if (setq name (get-char-code-property c 'old-name))
  2646. (push (cons name c) names))
  2647. (setq c (1+ c))))
  2648. (dolist (range upper-ranges)
  2649. (setq c (car range)
  2650. end (cdr range))
  2651. (while (<= c end)
  2652. (if (setq name (get-char-code-property c 'name))
  2653. (push (cons name c) names))
  2654. (setq c (1+ c))))
  2655. (setq ucs-names names))))
  2656. (defun read-char-by-name (prompt)
  2657. "Read a character by its Unicode name or hex number string.
  2658. Display PROMPT and read a string that represents a character by its
  2659. Unicode property `name' or `old-name'.
  2660. This function returns the character as a number.
  2661. You can type a few of the first letters of the Unicode name and
  2662. use completion. If you type a substring of the Unicode name
  2663. preceded by an asterisk `*' and use completion, it will show all
  2664. the characters whose names include that substring, not necessarily
  2665. at the beginning of the name.
  2666. This function also accepts a hexadecimal number of Unicode code
  2667. point or a number in hash notation, e.g. #o21430 for octal,
  2668. #x2318 for hex, or #10r8984 for decimal."
  2669. (let* ((completion-ignore-case t)
  2670. (input (completing-read
  2671. prompt
  2672. (lambda (string pred action)
  2673. (if (eq action 'metadata)
  2674. '(metadata (category . unicode-name))
  2675. (complete-with-action action (ucs-names) string pred))))))
  2676. (cond
  2677. ((string-match-p "\\`[0-9a-fA-F]+\\'" input)
  2678. (string-to-number input 16))
  2679. ((string-match-p "\\`#" input)
  2680. (read input))
  2681. (t
  2682. (cdr (assoc-string input (ucs-names) t))))))
  2683. (defun ucs-insert (character &optional count inherit)
  2684. "Insert COUNT copies of CHARACTER of the given Unicode code point.
  2685. Interactively, prompts for a Unicode character name or a hex number
  2686. using `read-char-by-name'.
  2687. You can type a few of the first letters of the Unicode name and
  2688. use completion. If you type a substring of the Unicode name
  2689. preceded by an asterisk `*' and use completion, it will show all
  2690. the characters whose names include that substring, not necessarily
  2691. at the beginning of the name.
  2692. This function also accepts a hexadecimal number of Unicode code
  2693. point or a number in hash notation, e.g. #o21430 for octal,
  2694. #x2318 for hex, or #10r8984 for decimal.
  2695. The optional third arg INHERIT (non-nil when called interactively),
  2696. says to inherit text properties from adjoining text, if those
  2697. properties are sticky."
  2698. (interactive
  2699. (list (read-char-by-name "Unicode (name or hex): ")
  2700. (prefix-numeric-value current-prefix-arg)
  2701. t))
  2702. (unless count (setq count 1))
  2703. (if (and (stringp character)
  2704. (string-match-p "\\`[0-9a-fA-F]+\\'" character))
  2705. (setq character (string-to-number character 16)))
  2706. (cond
  2707. ((null character)
  2708. (error "Not a Unicode character"))
  2709. ((not (integerp character))
  2710. (error "Not a Unicode character code: %S" character))
  2711. ((or (< character 0) (> character #x10FFFF))
  2712. (error "Not a Unicode character code: 0x%X" character)))
  2713. (if inherit
  2714. (dotimes (i count) (insert-and-inherit character))
  2715. (dotimes (i count) (insert character))))
  2716. (define-key ctl-x-map "8\r" 'ucs-insert)
  2717. ;;; mule-cmds.el ends here