quail.el 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095
  1. ;;; quail.el --- provides simple input method for multilingual text
  2. ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  6. ;; Registration Number H14PRO021
  7. ;; Author: Kenichi HANDA <handa@etl.go.jp>
  8. ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
  9. ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
  10. ;; Keywords: mule, multilingual, input method, 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. ;; In Quail minor mode, you can input multilingual text easily. By
  24. ;; defining a translation table (named Quail map) which maps ASCII key
  25. ;; string to multilingual character or string, you can input any text
  26. ;; from ASCII keyboard.
  27. ;;
  28. ;; We use words "translation" and "conversion" differently. The
  29. ;; former is done by Quail package itself, the latter is the further
  30. ;; process of converting a translated text to some more desirable
  31. ;; text. For instance, Quail package for Japanese (`quail-jp')
  32. ;; translates Roman text (transliteration of Japanese in Latin
  33. ;; alphabets) to Hiragana text, which is then converted to
  34. ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
  35. ;; CONVERSION-KEYS argument of the Quail package.
  36. ;; [There was an input method for Mule 2.3 called `Tamago' from the
  37. ;; Japanese `TAkusan MAtasete GOmen-nasai', or `Sorry for having you
  38. ;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is
  39. ;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a
  40. ;; smaller and simpler system; the smaller quail egg is also eaten in
  41. ;; Japan. Maybe others will be egged on to write more sorts of input
  42. ;; methods.]
  43. ;;; Code:
  44. (require 'help-mode)
  45. (eval-when-compile (require 'cl))
  46. (defgroup quail nil
  47. "Quail: multilingual input method."
  48. :group 'leim)
  49. ;; Buffer local variables
  50. (defvar quail-current-package nil
  51. "The current Quail package, which depends on the current input method.
  52. See the documentation of `quail-package-alist' for the format.")
  53. (make-variable-buffer-local 'quail-current-package)
  54. (put 'quail-current-package 'permanent-local t)
  55. ;; Quail uses the following variables to assist users.
  56. ;; A string containing available key sequences or translation list.
  57. (defvar quail-guidance-str nil)
  58. ;; A buffer to show completion list of the current key sequence.
  59. (defvar quail-completion-buf nil)
  60. ;; We may display the guidance string in a buffer on a one-line frame.
  61. (defvar quail-guidance-buf nil)
  62. (defvar quail-guidance-frame nil)
  63. ;; Each buffer in which Quail is activated should use different
  64. ;; guidance string.
  65. (make-variable-buffer-local 'quail-guidance-str)
  66. (put 'quail-guidance-str 'permanent-local t)
  67. (defvar quail-overlay nil
  68. "Overlay which covers the current translation region of Quail.")
  69. (make-variable-buffer-local 'quail-overlay)
  70. (defvar quail-conv-overlay nil
  71. "Overlay which covers the text to be converted in Quail mode.")
  72. (make-variable-buffer-local 'quail-conv-overlay)
  73. (defvar quail-current-key nil
  74. "Current key for translation in Quail mode.")
  75. (make-variable-buffer-local 'quail-current-key)
  76. (defvar quail-current-str nil
  77. "Currently selected translation of the current key.")
  78. (make-variable-buffer-local 'quail-current-str)
  79. (defvar quail-current-translations nil
  80. "Cons of indices and vector of possible translations of the current key.
  81. Indices is a list of (CURRENT START END BLOCK BLOCKS), where
  82. CURRENT is an index of the current translation,
  83. START and END are indices of the start and end of the current block,
  84. BLOCK is the current block index,
  85. BLOCKS is a number of blocks of translation.")
  86. (make-variable-buffer-local 'quail-current-translations)
  87. (defvar quail-current-data nil
  88. "Any Lisp object holding information of current translation status.
  89. When a key sequence is mapped to TRANS and TRANS is a cons
  90. of actual translation and some Lisp object to be referred
  91. for translating the longer key sequence, this variable is set
  92. to that Lisp object.")
  93. (make-variable-buffer-local 'quail-current-data)
  94. ;; Quail package handlers.
  95. (defvar quail-package-alist nil
  96. "List of Quail packages.
  97. A Quail package is a list of these elements:
  98. NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
  99. FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
  100. DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
  101. CONVERSION-KEYS, SIMPLE.
  102. QUAIL-MAP is a data structure to map key strings to translations. For
  103. the format, see the documentation of `quail-map-p'.
  104. DECODE-MAP is an alist of translations and corresponding keys.
  105. See the documentation of `quail-define-package' for the other elements.")
  106. ;; Return various slots in the current quail-package.
  107. (defsubst quail-name ()
  108. "Return the name of the current Quail package."
  109. (nth 0 quail-current-package))
  110. (defun quail-indent-to (col)
  111. (indent-to col)
  112. (let ((end (point)))
  113. (save-excursion
  114. (unless (zerop (skip-chars-backward "\t "))
  115. (put-text-property (point) end 'display (list 'space :align-to col))))))
  116. ;;;###autoload
  117. (defun quail-title ()
  118. "Return the title of the current Quail package."
  119. (let ((title (nth 1 quail-current-package)))
  120. ;; TITLE may be a string or a list. If it is a list, each element
  121. ;; is a string or the form (VAR STR1 STR2), and the interpretation
  122. ;; of the list is the same as that of mode-line-format.
  123. (if (stringp title)
  124. title
  125. (condition-case nil
  126. (mapconcat
  127. (lambda (x)
  128. (cond ((stringp x) x)
  129. ((and (listp x) (symbolp (car x)) (= (length x) 3))
  130. (if (symbol-value (car x))
  131. (nth 1 x) (nth 2 x)))
  132. (t "")))
  133. title "")
  134. (error "")))))
  135. (defsubst quail-map ()
  136. "Return the translation map of the current Quail package."
  137. (nth 2 quail-current-package))
  138. (defsubst quail-guidance ()
  139. "Return an object used for `guidance' feature of the current Quail package.
  140. See also the documentation of `quail-define-package'."
  141. (nth 3 quail-current-package))
  142. (defsubst quail-docstring ()
  143. "Return the documentation string of the current Quail package."
  144. (nth 4 quail-current-package))
  145. (defsubst quail-translation-keymap ()
  146. "Return translation keymap in the current Quail package.
  147. Translation keymap is a keymap used while translation region is active."
  148. (nth 5 quail-current-package))
  149. (defsubst quail-forget-last-selection ()
  150. "Return `forget-last-selection' flag of the current Quail package.
  151. See also the documentation of `quail-define-package'."
  152. (nth 6 quail-current-package))
  153. (defsubst quail-deterministic ()
  154. "Return `deterministic' flag of the current Quail package.
  155. See also the documentation of `quail-define-package'."
  156. (nth 7 quail-current-package))
  157. (defsubst quail-kbd-translate ()
  158. "Return `kbd-translate' flag of the current Quail package.
  159. See also the documentation of `quail-define-package'."
  160. (nth 8 quail-current-package))
  161. (defsubst quail-show-layout ()
  162. "Return `show-layout' flag of the current Quail package.
  163. See also the documentation of `quail-define-package'."
  164. (nth 9 quail-current-package))
  165. (defsubst quail-decode-map ()
  166. "Return decode map of the current Quail package.
  167. It is an alist of translations and corresponding keys."
  168. (nth 10 quail-current-package))
  169. (defsubst quail-maximum-shortest ()
  170. "Return `maximum-shortest' flag of the current Quail package.
  171. See also the documentation of `quail-define-package'."
  172. (nth 11 quail-current-package))
  173. (defsubst quail-overlay-plist ()
  174. "Return property list of an overly used in the current Quail package."
  175. (nth 12 quail-current-package))
  176. (defsubst quail-update-translation-function ()
  177. "Return a function for updating translation in the current Quail package."
  178. (nth 13 quail-current-package))
  179. (defsubst quail-conversion-keymap ()
  180. "Return conversion keymap in the current Quail package.
  181. Conversion keymap is a keymap used while conversion region is active
  182. but translation region is not active."
  183. (nth 14 quail-current-package))
  184. (defsubst quail-simple ()
  185. "Return t if the current Quail package is simple."
  186. (nth 15 quail-current-package))
  187. (defsubst quail-package (name)
  188. "Return Quail package named NAME."
  189. (assoc name quail-package-alist))
  190. (defun quail-add-package (package)
  191. "Add Quail package PACKAGE to `quail-package-alist'."
  192. (let ((pac (quail-package (car package))))
  193. (if pac
  194. (setcdr pac (cdr package))
  195. (setq quail-package-alist (cons package quail-package-alist)))))
  196. (defun quail-select-package (name)
  197. "Select Quail package named NAME as the current Quail package."
  198. (let ((package (quail-package name)))
  199. (if (null package)
  200. (error "No Quail package `%s'" name))
  201. (setq quail-current-package package)
  202. (setq-default quail-current-package package)
  203. name))
  204. ;;;###autoload
  205. (defun quail-use-package (package-name &rest libraries)
  206. "Start using Quail package PACKAGE-NAME.
  207. The remaining arguments are LIBRARIES to be loaded before using the package.
  208. This activates input method defined by PACKAGE-NAME by running
  209. `quail-activate', which see."
  210. (let ((package (quail-package package-name)))
  211. (if (null package)
  212. ;; Perhaps we have not yet loaded necessary libraries.
  213. (while libraries
  214. (if (not (load (car libraries) t))
  215. (progn
  216. (with-output-to-temp-buffer "*Help*"
  217. (princ "Quail package \"")
  218. (princ package-name)
  219. (princ "\" can't be activated\n because library \"")
  220. (princ (car libraries))
  221. (princ "\" is not in `load-path'.
  222. The most common case is that you have not yet installed appropriate
  223. libraries in LEIM (Libraries of Emacs Input Method) which is
  224. distributed separately from Emacs.
  225. LEIM is available from the same ftp directory as Emacs."))
  226. (error "Can't use the Quail package `%s'" package-name))
  227. (setq libraries (cdr libraries))))))
  228. (quail-select-package package-name)
  229. (setq current-input-method-title (quail-title))
  230. (quail-activate)
  231. ;; Hide all '... loaded' message.
  232. (message nil))
  233. (defvar quail-translation-keymap
  234. (let ((map (make-keymap))
  235. (i 0))
  236. (while (< i ?\ )
  237. (define-key map (char-to-string i) 'quail-other-command)
  238. (setq i (1+ i)))
  239. (while (< i 127)
  240. (define-key map (char-to-string i) 'quail-self-insert-command)
  241. (setq i (1+ i)))
  242. (setq i 128)
  243. (while (< i 256)
  244. (define-key map (vector i) 'quail-self-insert-command)
  245. (setq i (1+ i)))
  246. (define-key map "\177" 'quail-delete-last-char)
  247. (define-key map "\C-f" 'quail-next-translation)
  248. (define-key map "\C-b" 'quail-prev-translation)
  249. (define-key map "\C-n" 'quail-next-translation-block)
  250. (define-key map "\C-p" 'quail-prev-translation-block)
  251. (define-key map [right] 'quail-next-translation)
  252. (define-key map [left] 'quail-prev-translation)
  253. (define-key map [down] 'quail-next-translation-block)
  254. (define-key map [up] 'quail-prev-translation-block)
  255. (define-key map "\C-i" 'quail-completion)
  256. (define-key map "\C-@" 'quail-select-current)
  257. ;; Following simple.el, Enter key on numeric keypad selects the
  258. ;; current translation just like `C-SPC', and `mouse-2' chooses
  259. ;; any completion visible in the *Quail Completions* buffer.
  260. (define-key map [kp-enter] 'quail-select-current)
  261. (define-key map [mouse-2] 'quail-mouse-choose-completion)
  262. (define-key map [down-mouse-2] nil)
  263. (define-key map "\C-h" 'quail-translation-help)
  264. (define-key map [?\C- ] 'quail-select-current)
  265. (define-key map [tab] 'quail-completion)
  266. (define-key map [delete] 'quail-delete-last-char)
  267. (define-key map [backspace] 'quail-delete-last-char)
  268. map)
  269. "Keymap used processing translation in complex Quail modes.
  270. Only a few especially complex input methods use this map;
  271. most use `quail-simple-translation-keymap' instead.
  272. This map is activated while translation region is active.")
  273. (defvar quail-translation-docstring
  274. "When you type keys, the echo area shows the possible characters
  275. which correspond to that key sequence, each preceded by a digit. You
  276. can select one of the characters shown by typing the corresponding
  277. digit. Alternatively, you can use C-f and C-b to move through the
  278. line to select the character you want, then type a letter to begin
  279. entering another Chinese character or type a space or punctuation
  280. character.
  281. If there are more than ten possible characters for the given spelling,
  282. the echo area shows ten characters at a time; you can use C-n to move
  283. to the next group of ten, and C-p to move back to the previous group
  284. of ten.")
  285. ;; Categorize each Quail commands to make the output of quail-help
  286. ;; concise. This is done by putting `quail-help' property. The value
  287. ;; is:
  288. ;; hide -- never show this command
  289. ;; non-deterministic -- show only for non-deterministic input method
  290. (let ((l '((quail-other-command . hide)
  291. (quail-self-insert-command . hide)
  292. (quail-delete-last-char . hide)
  293. (quail-next-translation . non-deterministic)
  294. (quail-prev-translation . non-deterministic)
  295. (quail-next-translation-block . non-deterministic)
  296. (quail-prev-translation-block . non-deterministic))))
  297. (while l
  298. (put (car (car l)) 'quail-help (cdr (car l)))
  299. (setq l (cdr l))))
  300. (defvar quail-simple-translation-keymap
  301. (let ((map (make-keymap))
  302. (i 0))
  303. (while (< i ?\ )
  304. (define-key map (char-to-string i) 'quail-other-command)
  305. (setq i (1+ i)))
  306. (while (< i 127)
  307. (define-key map (char-to-string i) 'quail-self-insert-command)
  308. (setq i (1+ i)))
  309. (setq i 128)
  310. (while (< i 256)
  311. (define-key map (vector i) 'quail-self-insert-command)
  312. (setq i (1+ i)))
  313. (define-key map "\177" 'quail-delete-last-char)
  314. (define-key map [delete] 'quail-delete-last-char)
  315. (define-key map [backspace] 'quail-delete-last-char)
  316. ;;(let ((meta-map (make-sparse-keymap)))
  317. ;;(define-key map (char-to-string meta-prefix-char) meta-map)
  318. ;;(define-key map [escape] meta-map))
  319. map)
  320. "Keymap used while processing translation in simple Quail modes.
  321. A few especially complex input methods use `quail-translation-keymap' instead.
  322. This map is activated while translation region is active.")
  323. (defvar quail-conversion-keymap
  324. (let ((map (make-keymap))
  325. (i ?\ ))
  326. (while (< i 127)
  327. (define-key map (char-to-string i) 'quail-self-insert-command)
  328. (setq i (1+ i)))
  329. (setq i 128)
  330. (while (< i 256)
  331. (define-key map (vector i) 'quail-self-insert-command)
  332. (setq i (1+ i)))
  333. (define-key map "\C-b" 'quail-conversion-backward-char)
  334. (define-key map "\C-f" 'quail-conversion-forward-char)
  335. (define-key map "\C-a" 'quail-conversion-beginning-of-region)
  336. (define-key map "\C-e" 'quail-conversion-end-of-region)
  337. (define-key map "\C-d" 'quail-conversion-delete-char)
  338. (define-key map "\C-k" 'quail-conversion-delete-tail)
  339. (define-key map "\C-h" 'quail-translation-help)
  340. (define-key map "\177" 'quail-conversion-backward-delete-char)
  341. (define-key map [delete] 'quail-conversion-backward-delete-char)
  342. (define-key map [backspace] 'quail-conversion-backward-delete-char)
  343. map)
  344. "Keymap used for processing conversion in Quail mode.
  345. This map is activated while conversion region is active but translation
  346. region is not active.")
  347. ;; Just a dummy definition.
  348. (defun quail-other-command ()
  349. (interactive)
  350. )
  351. ;;;###autoload
  352. (defun quail-define-package (name language title
  353. &optional guidance docstring translation-keys
  354. forget-last-selection deterministic
  355. kbd-translate show-layout create-decode-map
  356. maximum-shortest overlay-plist
  357. update-translation-function
  358. conversion-keys simple)
  359. "Define NAME as a new Quail package for input LANGUAGE.
  360. TITLE is a string to be displayed at mode-line to indicate this package.
  361. Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
  362. FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
  363. CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
  364. UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
  365. GUIDANCE specifies how a guidance string is shown in echo area.
  366. If it is t, list of all possible translations for the current key is shown
  367. with the currently selected translation being highlighted.
  368. If it is an alist, the element has the form (CHAR . STRING). Each character
  369. in the current key is searched in the list and the corresponding string is
  370. shown.
  371. If it is nil, the current key is shown.
  372. DOCSTRING is the documentation string of this package. The command
  373. `describe-input-method' shows this string while replacing the form
  374. \\=\\<VAR> in the string by the value of VAR. That value should be a
  375. string. For instance, the form \\=\\<quail-translation-docstring> is
  376. replaced by a description about how to select a translation from a
  377. list of candidates.
  378. TRANSLATION-KEYS specifies additional key bindings used while translation
  379. region is active. It is an alist of single key character vs. corresponding
  380. command to be called.
  381. FORGET-LAST-SELECTION non-nil means a selected translation is not kept
  382. for the future to translate the same key. If this flag is nil, a
  383. translation selected for a key is remembered so that it can be the
  384. first candidate when the same key is entered later.
  385. DETERMINISTIC non-nil means the first candidate of translation is
  386. selected automatically without allowing users to select another
  387. translation for a key. In this case, unselected translations are of
  388. no use for an interactive use of Quail but can be used by some other
  389. programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
  390. to t.
  391. KBD-TRANSLATE non-nil means input characters are translated from a
  392. user's keyboard layout to the standard keyboard layout. See the
  393. documentation of `quail-keyboard-layout' and
  394. `quail-keyboard-layout-standard' for more detail.
  395. SHOW-LAYOUT non-nil means the `quail-help' command should show
  396. the user's keyboard layout visually with translated characters.
  397. If KBD-TRANSLATE is set, it is desirable to set also this flag unless
  398. this package defines no translations for single character keys.
  399. CREATE-DECODE-MAP non-nil means decode map is also created. A decode
  400. map is an alist of translations and corresponding original keys.
  401. Although this map is not used by Quail itself, it can be used by some
  402. other programs. For instance, Vietnamese supporting needs this map to
  403. convert Vietnamese text to VIQR format which uses only ASCII
  404. characters to represent Vietnamese characters.
  405. MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
  406. length of the shortest sequence. When we don't have a translation of
  407. key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
  408. the key at \"..AB\" and start translation of \"CD..\". Hangul
  409. packages, for instance, use this facility. If this flag is nil, we
  410. break the key just at \"..ABC\" and start translation of \"D..\".
  411. OVERLAY-PLIST if non-nil is a property list put on an overlay which
  412. covers Quail translation region.
  413. UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
  414. the current translation region according to a new translation data. By
  415. default, a translated text or a user's key sequence (if no translation
  416. for it) is inserted.
  417. CONVERSION-KEYS specifies additional key bindings used while
  418. conversion region is active. It is an alist of single key character
  419. vs. corresponding command to be called.
  420. If SIMPLE is non-nil, then we do not alter the meanings of
  421. commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
  422. non-Quail commands."
  423. (let (translation-keymap conversion-keymap)
  424. (if deterministic (setq forget-last-selection t))
  425. (if translation-keys
  426. (progn
  427. (setq translation-keymap (copy-keymap
  428. (if simple quail-simple-translation-keymap
  429. quail-translation-keymap)))
  430. (while translation-keys
  431. (define-key translation-keymap
  432. (car (car translation-keys)) (cdr (car translation-keys)))
  433. (setq translation-keys (cdr translation-keys))))
  434. (setq translation-keymap
  435. (if simple quail-simple-translation-keymap
  436. quail-translation-keymap)))
  437. (when conversion-keys
  438. (setq conversion-keymap (copy-keymap quail-conversion-keymap))
  439. (while conversion-keys
  440. (define-key conversion-keymap
  441. (car (car conversion-keys)) (cdr (car conversion-keys)))
  442. (setq conversion-keys (cdr conversion-keys))))
  443. (quail-add-package
  444. (list name title (list nil) guidance (or docstring "")
  445. translation-keymap
  446. forget-last-selection deterministic kbd-translate show-layout
  447. (if create-decode-map (list 'decode-map) nil)
  448. maximum-shortest overlay-plist update-translation-function
  449. conversion-keymap simple))
  450. ;; Update input-method-alist.
  451. (let ((slot (assoc name input-method-alist))
  452. (val (list language 'quail-use-package title docstring)))
  453. (if slot (setcdr slot val)
  454. (setq input-method-alist (cons (cons name val) input-method-alist)))))
  455. (quail-select-package name))
  456. ;; Quail minor mode handlers.
  457. ;; Setup overlays used in Quail mode.
  458. (defun quail-setup-overlays (conversion-mode)
  459. (let ((pos (point)))
  460. (if (overlayp quail-overlay)
  461. (move-overlay quail-overlay pos pos)
  462. (setq quail-overlay (make-overlay pos pos))
  463. (if input-method-highlight-flag
  464. (overlay-put quail-overlay 'face 'underline))
  465. (let ((l (quail-overlay-plist)))
  466. (while l
  467. (overlay-put quail-overlay (car l) (car (cdr l)))
  468. (setq l (cdr (cdr l))))))
  469. (if conversion-mode
  470. (if (overlayp quail-conv-overlay)
  471. (if (not (overlay-start quail-conv-overlay))
  472. (move-overlay quail-conv-overlay pos pos))
  473. (setq quail-conv-overlay (make-overlay pos pos))
  474. (if input-method-highlight-flag
  475. (overlay-put quail-conv-overlay 'face 'underline))))))
  476. ;; Delete overlays used in Quail mode.
  477. (defun quail-delete-overlays ()
  478. (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
  479. (delete-overlay quail-overlay))
  480. (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
  481. (delete-overlay quail-conv-overlay)))
  482. (defun quail-inactivate ()
  483. "Inactivate Quail input method.
  484. This function runs the normal hook `quail-inactivate-hook'."
  485. (interactive)
  486. (quail-activate -1))
  487. (defun quail-activate (&optional arg)
  488. "Activate Quail input method.
  489. With ARG, activate Quail input method if and only if arg is positive.
  490. This function runs `quail-activate-hook' if it activates the input
  491. method, `quail-inactivate-hook' if it deactivates it.
  492. While this input method is active, the variable
  493. `input-method-function' is bound to the function `quail-input-method'."
  494. (if (and arg
  495. (< (prefix-numeric-value arg) 0))
  496. ;; Let's inactivate Quail input method.
  497. (unwind-protect
  498. (progn
  499. (quail-delete-overlays)
  500. (setq describe-current-input-method-function nil)
  501. (quail-hide-guidance)
  502. (remove-hook 'post-command-hook 'quail-show-guidance t)
  503. (run-hooks 'quail-inactivate-hook))
  504. (kill-local-variable 'input-method-function))
  505. ;; Let's activate Quail input method.
  506. (if (null quail-current-package)
  507. ;; Quail package is not yet selected. Select one now.
  508. (let (name)
  509. (if quail-package-alist
  510. (setq name (car (car quail-package-alist)))
  511. (error "No Quail package loaded"))
  512. (quail-select-package name)))
  513. (setq inactivate-current-input-method-function 'quail-inactivate)
  514. (setq describe-current-input-method-function 'quail-help)
  515. (quail-delete-overlays)
  516. (setq quail-guidance-str "")
  517. (quail-show-guidance)
  518. ;; If we are in minibuffer, turn off the current input method
  519. ;; before exiting.
  520. (when (eq (selected-window) (minibuffer-window))
  521. (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)
  522. (add-hook 'post-command-hook 'quail-show-guidance nil t))
  523. (run-hooks 'quail-activate-hook)
  524. (make-local-variable 'input-method-function)
  525. (setq input-method-function 'quail-input-method)))
  526. (defun quail-exit-from-minibuffer ()
  527. (inactivate-input-method)
  528. (if (<= (minibuffer-depth) 1)
  529. (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
  530. ;; Keyboard layout translation handlers.
  531. ;; Some Quail packages provide localized keyboard simulation which
  532. ;; requires a particular keyboard layout. In this case, what we need
  533. ;; is locations of keys the user entered, not character codes
  534. ;; generated by those keys. However, for the moment, there's no
  535. ;; common way to get such information. So, we ask a user to give
  536. ;; information of his own keyboard layout, then translate it to the
  537. ;; standard layout which we defined so that all Quail packages depend
  538. ;; just on it.
  539. (defconst quail-keyboard-layout-standard
  540. "\
  541. \
  542. 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
  543. qQwWeErRtTyYuUiIoOpP[{]} \
  544. aAsSdDfFgGhHjJkKlL;:'\"\\| \
  545. zZxXcCvVbBnNmM,<.>/? \
  546. "
  547. "Standard keyboard layout of printable characters Quail assumes.
  548. See the documentation of `quail-keyboard-layout' for this format.
  549. This layout is almost the same as that of VT100,
  550. but the location of key \\ (backslash) is just right of key ' (single-quote),
  551. not right of RETURN key.")
  552. (defconst quail-keyboard-layout-len 180)
  553. ;; Here we provide several examples of famous keyboard layouts.
  554. ;; This is a candidate for a language environment-dependent setting.
  555. (defvar quail-keyboard-layout-alist
  556. (list
  557. (cons "standard" quail-keyboard-layout-standard)
  558. '("sun-type3" . "\
  559. \
  560. 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
  561. qQwWeErRtTyYuUiIoOpP[{]} \
  562. aAsSdDfFgGhHjJkKlL;:'\" \
  563. zZxXcCvVbBnNmM,<.>/? \
  564. ")
  565. '("atari-german" . "\
  566. \
  567. 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
  568. qQwWeErRtTzZuUiIoOpP\374\334+* \
  569. aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
  570. <>yYxXcCvVbBnNmM,;.:-_ \
  571. ")
  572. '("pc102-de" . "\
  573. \
  574. ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
  575. qQwWeErRtTzZuUiIoOpP\374\334+* \
  576. aAsSdDfFgGhHjJkKlL\366\326\344\304 \
  577. <>yYxXcCvVbBnNmM,;.:-_ \
  578. ")
  579. '("jp106" . "\
  580. \
  581. 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
  582. qQwWeErRtTyYuUiIoOpP@`[{ \
  583. aAsSdDfFgGhHjJkKlL;+:*]} \
  584. zZxXcCvVbBnNmM,<.>/?\\_ \
  585. ")
  586. '("pc105-uk" . "\
  587. \
  588. `\2541!2\"3\2434$5%6^7&8*9(0)-_=+ \
  589. qQwWeErRtTyYuUiIoOpP[{]} \
  590. aAsSdDfFgGhHjJkKlL;:'@#~ \
  591. \\|zZxXcCvVbBnNmM,<.>/? \
  592. ")
  593. )
  594. "Alist of keyboard names and corresponding layout strings.
  595. See the documentation of `quail-keyboard-layout' for the format of
  596. the layout string.")
  597. (defcustom quail-keyboard-layout quail-keyboard-layout-standard
  598. "A string which represents physical key layout of a particular keyboard.
  599. We assume there are six rows and each row has 15 keys (columns),
  600. the first row is above the `1' - `0' row,
  601. the first column of the second row is left of key `1',
  602. the first column of the third row is left of key `q',
  603. the first column of the fourth row is left of key `a',
  604. the first column of the fifth row is left of key `z',
  605. the sixth row is below the `z' - `/' row.
  606. Nth (N is even) and (N+1)th characters in the string are non-shifted
  607. and shifted characters respectively at the same location.
  608. The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
  609. The command `quail-set-keyboard-layout' usually sets this variable."
  610. :group 'quail
  611. :type `(choice
  612. ,@(mapcar (lambda (pair)
  613. (list 'const :tag (car pair) (cdr pair)))
  614. quail-keyboard-layout-alist)
  615. (string :tag "Other")))
  616. ;; A non-standard keyboard layout may miss some key locations of the
  617. ;; standard layout while having additional key locations not in the
  618. ;; standard layout. This alist maps those additional key locations to
  619. ;; the missing locations. The value is updated automatically by
  620. ;; quail-set-keyboard-layout.
  621. (defvar quail-keyboard-layout-substitution nil)
  622. (defun quail-update-keyboard-layout (kbd-type)
  623. (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
  624. (if (null layout)
  625. ;; Here, we had better ask a user to define his own keyboard
  626. ;; layout interactively.
  627. (error "Unknown keyboard type `%s'" kbd-type))
  628. (setq quail-keyboard-layout (cdr layout))
  629. (let ((i quail-keyboard-layout-len)
  630. subst-list missing-list)
  631. ;; Sum up additional key locations not in the standard layout in
  632. ;; subst-list, and missing key locations in missing-list.
  633. (while (> i 0)
  634. (setq i (1- i))
  635. (if (= (aref quail-keyboard-layout i) ? )
  636. (if (/= (aref quail-keyboard-layout-standard i) ? )
  637. (setq missing-list (cons i missing-list)))
  638. (if (= (aref quail-keyboard-layout-standard i) ? )
  639. (setq subst-list (cons (cons i nil) subst-list)))))
  640. (setq quail-keyboard-layout-substitution subst-list)
  641. ;; If there are additional key locations, map them to missing
  642. ;; key locations.
  643. (while missing-list
  644. (while (and subst-list (cdr (car subst-list)))
  645. (setq subst-list (cdr subst-list)))
  646. (if subst-list
  647. (setcdr (car subst-list) (car missing-list)))
  648. (setq missing-list (cdr missing-list))))))
  649. (defcustom quail-keyboard-layout-type "standard"
  650. "Type of keyboard layout used in Quail base input method.
  651. Available types are listed in the variable `quail-keyboard-layout-alist'."
  652. :group 'quail
  653. :type (cons 'choice (mapcar (lambda (elt)
  654. (list 'const (car elt)))
  655. quail-keyboard-layout-alist))
  656. :set #'(lambda (symbol value)
  657. (quail-update-keyboard-layout value)
  658. (set symbol value)))
  659. ;;;###autoload
  660. (defun quail-set-keyboard-layout (kbd-type)
  661. "Set the current keyboard layout to the same as keyboard KBD-TYPE.
  662. Since some Quail packages depends on a physical layout of keys (not
  663. characters generated by them), those are created by assuming the
  664. standard layout defined in `quail-keyboard-layout-standard'. This
  665. function tells Quail system the layout of your keyboard so that what
  666. you type is correctly handled."
  667. (interactive
  668. (let* ((completion-ignore-case t)
  669. (type (completing-read "Keyboard type: "
  670. quail-keyboard-layout-alist)))
  671. (list type)))
  672. (quail-update-keyboard-layout kbd-type)
  673. (setq quail-keyboard-layout-type kbd-type))
  674. (defun quail-keyboard-translate (char)
  675. "Translate CHAR to the one in the standard keyboard layout."
  676. (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
  677. ;; All Quail packages are designed based on
  678. ;; `quail-keyboard-layout-standard'.
  679. char
  680. (let ((i 0))
  681. ;; Find the key location on the current keyboard layout.
  682. (while (and (< i quail-keyboard-layout-len)
  683. (/= char (aref quail-keyboard-layout i)))
  684. (setq i (1+ i)))
  685. (if (= i quail-keyboard-layout-len)
  686. ;; CHAR is not in quail-keyboard-layout, which means that a
  687. ;; user typed a key which generated a character code to be
  688. ;; handled out of Quail. Just return CHAR and make
  689. ;; quail-execute-non-quail-command handle it correctly.
  690. char
  691. (let ((ch (aref quail-keyboard-layout-standard i)))
  692. (if (= ch ?\ )
  693. ;; This location not available in the standard keyboard
  694. ;; layout. Check if the location is used to substitute
  695. ;; for the other location of the standard layout.
  696. (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
  697. (aref quail-keyboard-layout-standard i)
  698. ;; Just return CHAR as well as above.
  699. char)
  700. ch))))))
  701. (defun quail-keyseq-translate (keyseq)
  702. (apply 'string
  703. (mapcar (function (lambda (x) (quail-keyboard-translate x)))
  704. keyseq)))
  705. (defun quail-insert-kbd-layout (kbd-layout)
  706. "Insert the visual keyboard layout table according to KBD-LAYOUT.
  707. The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
  708. (let (done-list layout i ch)
  709. ;; At first, convert KBD-LAYOUT to the same size vector that
  710. ;; contains translated character or string.
  711. (setq layout (string-to-vector kbd-layout)
  712. i 0)
  713. (while (< i quail-keyboard-layout-len)
  714. (setq ch (aref kbd-layout i))
  715. (if (quail-kbd-translate)
  716. (setq ch (quail-keyboard-translate ch)))
  717. (let* ((map (cdr (assq ch (cdr (quail-map)))))
  718. (translation (and map (quail-get-translation
  719. (car map) (char-to-string ch) 1))))
  720. (if translation
  721. (progn
  722. (if (consp translation)
  723. (if (> (length (cdr translation)) 0)
  724. (setq translation (aref (cdr translation) 0))
  725. (setq translation " ")))
  726. (setq done-list (cons translation done-list)))
  727. (setq translation (aref kbd-layout i)))
  728. (aset layout i translation))
  729. (setq i (1+ i)))
  730. (let ((pos (point))
  731. (bar "|")
  732. lower upper row)
  733. ;; Make table without horizontal lines. Each column for a key
  734. ;; has the form "| LU |" where L is for lower key and U is
  735. ;; for a upper key. If width of L (U) is greater than 1,
  736. ;; preceding (following) space is not inserted.
  737. (put-text-property 0 1 'face 'bold bar)
  738. (setq i 0)
  739. (while (< i quail-keyboard-layout-len)
  740. (when (= (% i 30) 0)
  741. (setq row (/ i 30))
  742. (if (> row 1)
  743. (insert-char 32 (+ row (/ (- row 2) 2)))))
  744. (setq lower (aref layout i)
  745. upper (aref layout (1+ i)))
  746. (insert bar)
  747. (if (< (if (stringp lower) (string-width lower) (char-width lower)) 2)
  748. (insert " "))
  749. (if (characterp lower)
  750. (if (eq (get-char-code-property lower 'general-category) 'Mn)
  751. ;; Pad the left and right of non-spacing characters.
  752. (setq lower (compose-string (string lower) 0 1
  753. (format "\t%c\t" lower)))
  754. (setq lower (string lower))))
  755. (if (characterp upper)
  756. (if (eq (get-char-code-property upper 'general-category) 'Mn)
  757. ;; Pad the left and right of non-spacing characters.
  758. (setq upper (compose-string (string upper) 0 1
  759. (format "\t%c\t" upper)))
  760. (setq upper (string upper))))
  761. (insert (bidi-string-mark-left-to-right lower)
  762. (propertize " " 'invisible t)
  763. (bidi-string-mark-left-to-right upper))
  764. (if (< (string-width upper) 2)
  765. (insert " "))
  766. (setq i (+ i 2))
  767. (if (= (% i 30) 0)
  768. (insert bar "\n")))
  769. ;; Insert horizontal lines while deleting blank key columns at the
  770. ;; beginning and end of each line.
  771. (save-restriction
  772. (narrow-to-region pos (point))
  773. (goto-char pos)
  774. ;;(while (looking-at "[| ]*$")
  775. ;;(forward-line 1)
  776. ;;(delete-region pos (point)))
  777. (let ((from1 100) (to1 0) from2 to2)
  778. (while (not (eobp))
  779. (if (looking-at "[| \u202c\u202d]*$")
  780. ;; The entire row is blank.
  781. (delete-region (point) (match-end 0))
  782. ;; Delete blank key columns at the head.
  783. (if (looking-at "\u202d? *\\(| \\)+")
  784. (subst-char-in-region (point) (match-end 0) ?| ? ))
  785. ;; Delete blank key columns at the tail.
  786. (if (re-search-forward "\\( |\\)+\u202c?$"
  787. (line-end-position) t)
  788. (delete-region (match-beginning 0) (point)))
  789. (beginning-of-line))
  790. ;; Calculate the start and end columns of a horizontal line.
  791. (if (eolp)
  792. (setq from2 from1 to2 to1)
  793. (skip-chars-forward " \u202d")
  794. (setq from2 (current-column))
  795. (end-of-line)
  796. (setq to2 (current-column))
  797. (if (< from2 from1)
  798. (setq from1 from2))
  799. (if (> to2 to1)
  800. (setq to1 to2))
  801. (beginning-of-line))
  802. ;; If the previous or the current line has at least one key
  803. ;; column, insert a horizontal line.
  804. (when (> to1 0)
  805. (insert-char 32 from1)
  806. (setq pos (point))
  807. (insert "+")
  808. (insert-char ?- (- (- to1 from1) 2))
  809. (insert "+")
  810. (put-text-property pos (point) 'face 'bold)
  811. (insert "\n"))
  812. (setq from1 from2 to1 to2)
  813. (forward-line 1)))
  814. ;; Insert "space bar" box.
  815. (forward-line -1)
  816. (setq pos (point))
  817. (insert
  818. " +-----------------------------+
  819. | space bar |
  820. +-----------------------------+
  821. ")
  822. (put-text-property pos (point) 'face 'bold)
  823. (insert ?\n)))
  824. done-list))
  825. ;;;###autoload
  826. (defun quail-show-keyboard-layout (&optional keyboard-type)
  827. "Show the physical layout of the keyboard type KEYBOARD-TYPE.
  828. The variable `quail-keyboard-layout-type' holds the currently selected
  829. keyboard type."
  830. (interactive
  831. (list (completing-read "Keyboard type (default current choice): "
  832. quail-keyboard-layout-alist
  833. nil t)))
  834. (or (and keyboard-type (> (length keyboard-type) 0))
  835. (setq keyboard-type quail-keyboard-layout-type))
  836. (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
  837. (or layout
  838. (error "Unknown keyboard type: %s" keyboard-type))
  839. (with-output-to-temp-buffer "*Help*"
  840. (with-current-buffer standard-output
  841. (insert "Keyboard layout (keyboard type: "
  842. keyboard-type
  843. ")\n")
  844. (quail-insert-kbd-layout (cdr layout))))))
  845. ;; Quail map
  846. (defsubst quail-map-p (object)
  847. "Return t if OBJECT is a Quail map.
  848. A Quail map holds information how a particular key should be translated.
  849. Its format is (TRANSLATION . ALIST).
  850. TRANSLATION is either a character, or a cons (INDEX . VECTOR).
  851. In the latter case, each element of VECTOR is a candidate for the translation,
  852. and INDEX points the currently selected translation.
  853. ALIST is normally a list of elements that look like (CHAR . DEFN),
  854. where DEFN is another Quail map for a longer key (CHAR added to the
  855. current key). It may also be a symbol of a function which returns an
  856. alist of the above format.
  857. Just after a Quail package is read, TRANSLATION may be a string or a
  858. vector. Then each element of the string or vector is a candidate for
  859. the translation. These objects are transformed to cons cells in the
  860. format \(INDEX . VECTOR), as described above."
  861. (and (consp object)
  862. (let ((translation (car object)))
  863. (or (integerp translation) (null translation)
  864. (vectorp translation) (stringp translation)
  865. (symbolp translation)
  866. (and (consp translation) (not (vectorp (cdr translation))))))
  867. (let ((alist (cdr object)))
  868. (or (and (listp alist) (consp (car alist)))
  869. (symbolp alist)))))
  870. ;;;###autoload
  871. (defmacro quail-define-rules (&rest rules)
  872. "Define translation rules of the current Quail package.
  873. Each argument is a list of KEY and TRANSLATION.
  874. KEY is a string meaning a sequence of keystrokes to be translated.
  875. TRANSLATION is a character, a string, a vector, a Quail map, or a function.
  876. If it is a character, it is the sole translation of KEY.
  877. If it is a string, each character is a candidate for the translation.
  878. If it is a vector, each element (string or character) is a candidate
  879. for the translation.
  880. In these cases, a key specific Quail map is generated and assigned to KEY.
  881. If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
  882. it is used to handle KEY.
  883. The first argument may be an alist of annotations for the following
  884. rules. Each element has the form (ANNOTATION . VALUE), where
  885. ANNOTATION is a symbol indicating the annotation type. Currently
  886. the following annotation types are supported.
  887. append -- the value non-nil means that the following rules should
  888. be appended to the rules of the current Quail package.
  889. face -- the value is a face to use for displaying TRANSLATIONs in
  890. candidate list.
  891. advice -- the value is a function to call after one of RULES is
  892. selected. The function is called with one argument, the
  893. selected TRANSLATION string, after the TRANSLATION is
  894. inserted.
  895. no-decode-map --- the value non-nil means that decoding map is not
  896. generated for the following translations."
  897. (let ((l rules)
  898. append no-decode-map props)
  899. ;; If the first argument is an alist of annotations, handle them.
  900. (if (consp (car (car l)))
  901. (let ((annotations (car l)))
  902. (setq append (assq 'append annotations))
  903. (if append
  904. (setq annotations (delete append annotations)
  905. append (cdr append)))
  906. (setq no-decode-map (assq 'no-decode-map annotations))
  907. (if no-decode-map
  908. (setq annotations (delete no-decode-map annotations)
  909. no-decode-map (cdr no-decode-map)))
  910. ;; Convert the remaining annotations to property list PROPS.
  911. (dolist (annotation annotations)
  912. (setq props
  913. (cons (car annotation)
  914. (cons (cdr annotation)
  915. props))))
  916. (setq l (cdr l))))
  917. ;; Process the remaining arguments one by one.
  918. (if append
  919. ;; There's no way to add new rules at compiling time.
  920. `(let ((tail ',l)
  921. (map (quail-map))
  922. (decode-map (and (quail-decode-map) (not ,no-decode-map)))
  923. (properties ',props)
  924. key trans)
  925. (while tail
  926. (setq key (car (car tail)) trans (car (cdr (car tail)))
  927. tail (cdr tail))
  928. (quail-defrule-internal key trans map t decode-map properties)))
  929. ;; We can build up quail map and decode map at compiling time.
  930. (let ((map (list nil))
  931. (decode-map (if (not no-decode-map) (list 'decode-map)))
  932. key trans)
  933. (while l
  934. (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
  935. (quail-defrule-internal key trans map t decode-map props))
  936. `(if (prog1 (quail-decode-map)
  937. (quail-install-map ',map))
  938. (quail-install-decode-map ',decode-map))))))
  939. ;;;###autoload
  940. (defun quail-install-map (map &optional name)
  941. "Install the Quail map MAP in the current Quail package.
  942. Optional 2nd arg NAME, if non-nil, is a name of Quail package for
  943. which to install MAP.
  944. The installed map can be referred by the function `quail-map'."
  945. (if (null quail-current-package)
  946. (error "No current Quail package"))
  947. (if (null (quail-map-p map))
  948. (error "Invalid Quail map `%s'" map))
  949. (setcar (cdr (cdr quail-current-package)) map))
  950. ;;;###autoload
  951. (defun quail-install-decode-map (decode-map &optional name)
  952. "Install the Quail decode map DECODE-MAP in the current Quail package.
  953. Optional 2nd arg NAME, if non-nil, is a name of Quail package for
  954. which to install MAP.
  955. The installed decode map can be referred by the function `quail-decode-map'."
  956. (if (null quail-current-package)
  957. (error "No current Quail package"))
  958. (if (if (consp decode-map)
  959. (eq (car decode-map) 'decode-map)
  960. (if (char-table-p decode-map)
  961. (eq (char-table-subtype decode-map) 'quail-decode-map)))
  962. (setcar (nthcdr 10 quail-current-package) decode-map)
  963. (error "Invalid Quail decode map `%s'" decode-map)))
  964. ;;;###autoload
  965. (defun quail-defrule (key translation &optional name append)
  966. "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
  967. KEY is a string meaning a sequence of keystrokes to be translated.
  968. TRANSLATION is a character, a string, a vector, a Quail map,
  969. a function, or a cons.
  970. It it is a character, it is the sole translation of KEY.
  971. If it is a string, each character is a candidate for the translation.
  972. If it is a vector, each element (string or character) is a candidate
  973. for the translation.
  974. If it is a cons, the car is one of the above and the cdr is a function
  975. to call when translating KEY (the return value is assigned to the
  976. variable `quail-current-data'). If the cdr part is not a function,
  977. the value itself is assigned to `quail-current-data'.
  978. In these cases, a key specific Quail map is generated and assigned to KEY.
  979. If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
  980. it is used to handle KEY.
  981. Optional 3rd argument NAME, if specified, says which Quail package
  982. to define this translation rule in. The default is to define it in the
  983. current Quail package.
  984. Optional 4th argument APPEND, if non-nil, appends TRANSLATION
  985. to the current translations for KEY instead of replacing them."
  986. (if name
  987. (let ((package (quail-package name)))
  988. (if (null package)
  989. (error "No Quail package `%s'" name))
  990. (setq quail-current-package package)))
  991. (quail-defrule-internal key translation (quail-map) append))
  992. (defun quail-vunion (v1 v2)
  993. (apply 'vector
  994. ;; No idea why this was here, but it seems to cause the
  995. ;; incorrect ordering, according to Nils Anders Danielsson.
  996. ;; (nreverse
  997. (delete-dups (nconc (append v1 ()) (append v2 ()))))) ;; )
  998. ;;;###autoload
  999. (defun quail-defrule-internal (key trans map &optional append decode-map props)
  1000. "Define KEY as TRANS in a Quail map MAP.
  1001. If Optional 4th arg APPEND is non-nil, TRANS is appended to the
  1002. current translations for KEY instead of replacing them.
  1003. Optional 5th arg DECODE-MAP is a Quail decode map.
  1004. Optional 6th arg PROPS is a property list annotating TRANS. See the
  1005. function `quail-define-rules' for the detail."
  1006. (if (not (or (stringp key) (vectorp key)))
  1007. (error "Invalid Quail key `%s'" key))
  1008. (if (not (or (numberp trans) (stringp trans) (vectorp trans)
  1009. (consp trans)
  1010. (symbolp trans)
  1011. (quail-map-p trans)))
  1012. (error "Invalid Quail translation `%s'" trans))
  1013. (if (null (quail-map-p map))
  1014. (error "Invalid Quail map `%s'" map))
  1015. (let ((len (length key))
  1016. (idx 0)
  1017. ch entry)
  1018. ;; Make a map for registering TRANS if necessary.
  1019. (while (< idx len)
  1020. (if (null (consp map))
  1021. ;; We come here, for example, when we try to define a rule
  1022. ;; for "ABC" but a rule for "AB" is already defined as a
  1023. ;; symbol.
  1024. (error "Quail key %s is too long" key))
  1025. (setq ch (aref key idx)
  1026. entry (assq ch (cdr map)))
  1027. (if (null entry)
  1028. (progn
  1029. (setq entry (cons ch (list nil)))
  1030. (setcdr map (cons entry (cdr map)))))
  1031. (setq map (cdr entry))
  1032. (setq idx (1+ idx)))
  1033. (if (symbolp trans)
  1034. (if (cdr map)
  1035. ;; We come here, for example, when we try to define a rule
  1036. ;; for "AB" as a symbol but a rule for "ABC" is already
  1037. ;; defined.
  1038. (error "Quail key %s is too short" key)
  1039. (setcdr entry trans))
  1040. (if (quail-map-p trans)
  1041. (if (not (listp (cdr map)))
  1042. ;; We come here, for example, when we try to define a rule
  1043. ;; for "AB" as a symbol but a rule for "ABC" is already
  1044. ;; defined.
  1045. (error "Quail key %s is too short" key)
  1046. (if (not (listp (cdr trans)))
  1047. (if (cdr map)
  1048. ;; We come here, for example, when we try to
  1049. ;; define a rule for "AB" as a symbol but a rule
  1050. ;; for "ABC" is already defined.
  1051. (error "Quail key %s is too short" key)
  1052. (setcdr entry trans))
  1053. (setcdr entry (append trans (cdr map)))))
  1054. ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
  1055. ;; to a vector of strings, add PROPS to each string and record
  1056. ;; this rule in DECODE-MAP.
  1057. (when (and (or props decode-map)
  1058. (not (consp trans)) (not (symbolp trans)))
  1059. (if (integerp trans)
  1060. (setq trans (vector trans))
  1061. (if (stringp trans)
  1062. (setq trans (string-to-vector trans))))
  1063. (let ((len (length trans))
  1064. elt)
  1065. (while (> len 0)
  1066. (setq len (1- len))
  1067. (setq elt (aref trans len))
  1068. (if (integerp elt)
  1069. (setq elt (char-to-string elt)))
  1070. (aset trans len elt)
  1071. (if props
  1072. (add-text-properties 0 (length elt) props elt))
  1073. (if decode-map
  1074. (setcdr decode-map
  1075. (cons (cons elt key) (cdr decode-map)))))))
  1076. (if (and (car map) append)
  1077. (let* ((prev (quail-get-translation (car map) key len))
  1078. (prevchars (if (integerp prev)
  1079. (vector prev)
  1080. (cdr prev))))
  1081. (if (integerp trans)
  1082. (setq trans (vector trans))
  1083. (if (stringp trans)
  1084. (setq trans (string-to-vector trans))))
  1085. (let ((new (quail-vunion prevchars trans)))
  1086. (setq trans
  1087. (if (equal new prevchars)
  1088. ;; Nothing to change, get back to orig value.
  1089. prev
  1090. (cons (list 0 0 0 0 nil) new))))))
  1091. (setcar map trans)))))
  1092. (defun quail-get-translation (def key len)
  1093. "Return the translation specified as DEF for KEY of length LEN.
  1094. The translation is either a character or a cons of the form (INDEX . VECTOR),
  1095. where VECTOR is a vector of candidates (character or string) for
  1096. the translation, and INDEX points into VECTOR to specify the currently
  1097. selected translation."
  1098. (if (and def (symbolp def))
  1099. (if (functionp def)
  1100. ;; DEF is a symbol of a function which returns valid translation.
  1101. (setq def (funcall def key len))
  1102. (setq def nil)))
  1103. (if (and (consp def) (not (vectorp (cdr def))))
  1104. (setq def (car def)))
  1105. (cond
  1106. ((or (integerp def) (consp def))
  1107. def)
  1108. ((null def)
  1109. ;; No translation.
  1110. nil)
  1111. ((stringp def)
  1112. ;; If the length is 1, we don't need vector but a single candidate
  1113. ;; as the translation.
  1114. (if (= (length def) 1)
  1115. (aref def 0)
  1116. ;; Each character in DEF is a candidate of translation. Reform
  1117. ;; it as (INDICES . VECTOR).
  1118. (cons (list 0 0 0 0 nil) (string-to-vector def))))
  1119. ((vectorp def)
  1120. ;; If the length is 1, and the length of element string is 1, we
  1121. ;; don't need vector but a single candidate as the translation.
  1122. (if (and (= (length def) 1)
  1123. (= (length (aref def 0)) 1))
  1124. (aref (aref def 0) 0)
  1125. ;; Each element (string or character) in DEF is a candidate of
  1126. ;; translation. Reform it as (INDICES . VECTOR).
  1127. (cons (list 0 0 0 0 nil) def)))
  1128. (t
  1129. (error "Invalid object in Quail map: %s" def))))
  1130. (defun quail-lookup-key (key &optional len not-reset-indices)
  1131. "Lookup KEY of length LEN in the current Quail map and return the definition.
  1132. The returned value is a Quail map specific to KEY."
  1133. (or len
  1134. (setq len (length key)))
  1135. (let ((idx 0)
  1136. (map (quail-map))
  1137. (kbd-translate (quail-kbd-translate))
  1138. slot ch translation def)
  1139. (while (and map (< idx len))
  1140. (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
  1141. (aref key idx)))
  1142. (setq idx (1+ idx))
  1143. (if (and (cdr map) (symbolp (cdr map)))
  1144. (setcdr map (funcall (cdr map) key idx)))
  1145. (setq slot (assq ch (cdr map)))
  1146. (if (and (cdr slot) (symbolp (cdr slot)))
  1147. (setcdr slot (funcall (cdr slot) key idx)))
  1148. (setq map (cdr slot)))
  1149. (setq def (car map))
  1150. (setq quail-current-translations nil)
  1151. (if (and map (setq translation (quail-get-translation def key len)))
  1152. (progn
  1153. (if (and (consp def) (not (vectorp (cdr def))))
  1154. (progn
  1155. (if (not (equal (car def) translation))
  1156. ;; We must reflect TRANSLATION to car part of DEF.
  1157. (setcar def translation))
  1158. (setq quail-current-data
  1159. (if (functionp (cdr def))
  1160. (funcall (cdr def))
  1161. (cdr def))))
  1162. (if (not (equal def translation))
  1163. ;; We must reflect TRANSLATION to car part of MAP.
  1164. (setcar map translation)))
  1165. (if (and (consp translation) (vectorp (cdr translation)))
  1166. (progn
  1167. (setq quail-current-translations translation)
  1168. (if (and (not not-reset-indices) (quail-forget-last-selection))
  1169. (setcar (car quail-current-translations) 0))))))
  1170. ;; We may have to reform cdr part of MAP.
  1171. (if (and (cdr map) (functionp (cdr map)))
  1172. (setcdr map (funcall (cdr map) key len)))
  1173. map))
  1174. (put 'quail-error 'error-conditions '(quail-error error))
  1175. (defun quail-error (&rest args)
  1176. (signal 'quail-error (apply 'format args)))
  1177. (defun quail-input-string-to-events (str)
  1178. "Convert input string STR to a list of events.
  1179. If STR has `advice' text property, append the following special event:
  1180. \(quail-advice STR)"
  1181. (let ((events (mapcar
  1182. (lambda (c)
  1183. (or
  1184. ;; Avoid "obsolete" warnings for translation-table-for-input.
  1185. (with-no-warnings
  1186. (and translation-table-for-input
  1187. (aref translation-table-for-input c)))
  1188. c))
  1189. str)))
  1190. (if (or (get-text-property 0 'advice str)
  1191. (next-single-property-change 0 'advice str))
  1192. (setq events
  1193. (nconc events (list (list 'quail-advice str)))))
  1194. events))
  1195. (defvar quail-translating nil)
  1196. (defvar quail-converting nil)
  1197. (defvar quail-conversion-str nil)
  1198. (defun quail-input-method (key)
  1199. (if (or buffer-read-only
  1200. overriding-terminal-local-map
  1201. overriding-local-map)
  1202. (list key)
  1203. (quail-setup-overlays (quail-conversion-keymap))
  1204. (let ((modified-p (buffer-modified-p))
  1205. (buffer-undo-list t)
  1206. (inhibit-modification-hooks t))
  1207. (unwind-protect
  1208. (let ((input-string (if (quail-conversion-keymap)
  1209. (quail-start-conversion key)
  1210. (quail-start-translation key))))
  1211. (setq quail-guidance-str "")
  1212. (when (and (stringp input-string)
  1213. (> (length input-string) 0))
  1214. (if input-method-exit-on-first-char
  1215. (list (aref input-string 0))
  1216. (quail-input-string-to-events input-string))))
  1217. (quail-delete-overlays)
  1218. (set-buffer-modified-p modified-p)
  1219. ;; Run this hook only when the current input method doesn't require
  1220. ;; conversion. When conversion is required, the conversion function
  1221. ;; should run this hook at a proper timing.
  1222. (unless (quail-conversion-keymap)
  1223. (run-hooks 'input-method-after-insert-chunk-hook))))))
  1224. (defun quail-overlay-region-events (overlay)
  1225. (let ((start (overlay-start overlay))
  1226. (end (overlay-end overlay)))
  1227. (if (< start end)
  1228. (prog1
  1229. (string-to-list (buffer-substring start end))
  1230. (delete-region start end)))))
  1231. (defsubst quail-delete-region ()
  1232. "Delete the text in the current translation region of Quail."
  1233. (if (overlay-start quail-overlay)
  1234. (delete-region (overlay-start quail-overlay)
  1235. (overlay-end quail-overlay))))
  1236. (defun quail-start-translation (key)
  1237. "Start translation of the typed character KEY by the current Quail package.
  1238. Return the input string."
  1239. ;; Check the possibility of translating KEY.
  1240. ;; If KEY is nil, we can anyway start translation.
  1241. (if (or (and (integerp key)
  1242. (assq (if (quail-kbd-translate)
  1243. (quail-keyboard-translate key) key)
  1244. (cdr (quail-map))))
  1245. (null key))
  1246. ;; OK, we can start translation.
  1247. (let* ((echo-keystrokes 0)
  1248. (help-char nil)
  1249. (overriding-terminal-local-map (quail-translation-keymap))
  1250. (generated-events nil) ;FIXME: What is this?
  1251. (input-method-function nil)
  1252. (modified-p (buffer-modified-p))
  1253. last-command-event last-command this-command)
  1254. (setq quail-current-key ""
  1255. quail-current-str ""
  1256. quail-translating t)
  1257. (if key
  1258. (setq unread-command-events (cons key unread-command-events)))
  1259. (while quail-translating
  1260. (set-buffer-modified-p modified-p)
  1261. (quail-show-guidance)
  1262. (let* ((prompt (if input-method-use-echo-area
  1263. (format "%s%s %s"
  1264. (or input-method-previous-message "")
  1265. quail-current-str
  1266. quail-guidance-str)))
  1267. (keyseq (read-key-sequence prompt nil nil t))
  1268. (cmd (lookup-key (quail-translation-keymap) keyseq)))
  1269. (if (if key
  1270. (and (commandp cmd) (not (eq cmd 'quail-other-command)))
  1271. (eq cmd 'quail-self-insert-command))
  1272. (progn
  1273. (setq last-command-event (aref keyseq (1- (length keyseq)))
  1274. last-command this-command
  1275. this-command cmd)
  1276. (setq key t)
  1277. (condition-case err
  1278. (call-interactively cmd)
  1279. (quail-error (message "%s" (cdr err)) (beep))))
  1280. ;; KEYSEQ is not defined in the translation keymap.
  1281. ;; Let's return the event(s) to the caller.
  1282. (setq unread-command-events
  1283. (string-to-list (this-single-command-raw-keys)))
  1284. (setq quail-translating nil))))
  1285. (quail-delete-region)
  1286. quail-current-str)
  1287. ;; Since KEY doesn't start any translation, just return it.
  1288. ;; But translate KEY if necessary.
  1289. (if (quail-kbd-translate)
  1290. (setq key (quail-keyboard-translate key)))
  1291. (char-to-string key)))
  1292. (defun quail-start-conversion (key)
  1293. "Start conversion of the typed character KEY by the current Quail package.
  1294. Return the input string."
  1295. ;; Check the possibility of translating KEY.
  1296. ;; If KEY is nil, we can anyway start translation.
  1297. (if (or (and (integerp key)
  1298. (assq (if (quail-kbd-translate)
  1299. (quail-keyboard-translate key) key)
  1300. (cdr (quail-map))))
  1301. (null key))
  1302. ;; Ok, we can start translation and conversion.
  1303. (let* ((echo-keystrokes 0)
  1304. (help-char nil)
  1305. (overriding-terminal-local-map (quail-conversion-keymap))
  1306. (generated-events nil) ;FIXME: What is this?
  1307. (input-method-function nil)
  1308. (modified-p (buffer-modified-p))
  1309. last-command-event last-command this-command)
  1310. (setq quail-current-key ""
  1311. quail-current-str ""
  1312. quail-translating t
  1313. quail-converting t
  1314. quail-conversion-str "")
  1315. (if key
  1316. (setq unread-command-events (cons key unread-command-events)))
  1317. (while quail-converting
  1318. (set-buffer-modified-p modified-p)
  1319. (or quail-translating
  1320. (progn
  1321. (setq quail-current-key ""
  1322. quail-current-str ""
  1323. quail-translating t)
  1324. (quail-setup-overlays nil)))
  1325. (quail-show-guidance)
  1326. (let* ((prompt (if input-method-use-echo-area
  1327. (format "%s%s%s %s"
  1328. (or input-method-previous-message "")
  1329. quail-conversion-str
  1330. quail-current-str
  1331. quail-guidance-str)))
  1332. (keyseq (read-key-sequence prompt nil nil t))
  1333. (cmd (lookup-key (quail-conversion-keymap) keyseq)))
  1334. (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
  1335. (progn
  1336. (setq last-command-event (aref keyseq (1- (length keyseq)))
  1337. last-command this-command
  1338. this-command cmd)
  1339. (setq key t)
  1340. (condition-case err
  1341. (call-interactively cmd)
  1342. (quail-error (message "%s" (cdr err)) (beep)))
  1343. (or quail-translating
  1344. (progn
  1345. (if quail-current-str
  1346. (setq quail-conversion-str
  1347. (concat quail-conversion-str
  1348. (if (stringp quail-current-str)
  1349. quail-current-str
  1350. (char-to-string quail-current-str)))))
  1351. (if (or input-method-exit-on-first-char
  1352. (= (length quail-conversion-str) 0))
  1353. (setq quail-converting nil)))))
  1354. ;; KEYSEQ is not defined in the conversion keymap.
  1355. ;; Let's return the event(s) to the caller.
  1356. (setq unread-command-events
  1357. (string-to-list (this-single-command-raw-keys)))
  1358. (setq quail-converting nil))))
  1359. (setq quail-translating nil)
  1360. (if (overlay-start quail-conv-overlay)
  1361. (delete-region (overlay-start quail-conv-overlay)
  1362. (overlay-end quail-conv-overlay)))
  1363. (if (> (length quail-conversion-str) 0)
  1364. quail-conversion-str))
  1365. ;; Since KEY doesn't start any translation, just return it.
  1366. ;; But translate KEY if necessary.
  1367. (if (quail-kbd-translate)
  1368. (setq key (quail-keyboard-translate key)))
  1369. (char-to-string key)))
  1370. (defun quail-terminate-translation ()
  1371. "Terminate the translation of the current key."
  1372. (setq quail-translating nil)
  1373. (setq quail-guidance-str " "))
  1374. (defun quail-select-current ()
  1375. "Accept the currently selected translation."
  1376. (interactive)
  1377. (quail-terminate-translation))
  1378. (defun quail-update-translation (control-flag)
  1379. "Update the current translation status according to CONTROL-FLAG.
  1380. If CONTROL-FLAG is integer value, it is the number of keys in the
  1381. head `quail-current-key' which can be translated. The remaining keys
  1382. are put back to `unread-command-events' to be handled again. If
  1383. CONTROL-FLAG is t, terminate the translation for the whole keys in
  1384. `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
  1385. with more keys."
  1386. (let ((func (quail-update-translation-function)))
  1387. (if func
  1388. (setq control-flag (funcall func control-flag))
  1389. (cond ((numberp control-flag)
  1390. (let ((len (length quail-current-key)))
  1391. (if (= control-flag 0)
  1392. (setq quail-current-str
  1393. (if (quail-kbd-translate)
  1394. (quail-keyseq-translate quail-current-key)
  1395. quail-current-key)))
  1396. (or input-method-exit-on-first-char
  1397. (while (> len control-flag)
  1398. (setq len (1- len))
  1399. (setq unread-command-events
  1400. (cons (aref quail-current-key len)
  1401. unread-command-events))))))
  1402. ((null control-flag)
  1403. (unless quail-current-str
  1404. (setq quail-current-str
  1405. (if (quail-kbd-translate)
  1406. (quail-keyseq-translate quail-current-key)
  1407. quail-current-key))
  1408. (if (and input-method-exit-on-first-char
  1409. (quail-simple))
  1410. (setq control-flag t)))))))
  1411. (or input-method-use-echo-area
  1412. (let (pos)
  1413. (quail-delete-region)
  1414. (setq pos (point))
  1415. (or enable-multibyte-characters
  1416. (let (char)
  1417. (if (stringp quail-current-str)
  1418. (catch 'tag
  1419. (mapc #'(lambda (ch)
  1420. (when (/= (unibyte-char-to-multibyte
  1421. (multibyte-char-to-unibyte ch))
  1422. ch)
  1423. (setq char ch)
  1424. (throw 'tag nil)))
  1425. quail-current-str))
  1426. (if (/= (unibyte-char-to-multibyte
  1427. (multibyte-char-to-unibyte quail-current-str))
  1428. quail-current-str)
  1429. (setq char quail-current-str)))
  1430. (when char
  1431. (message "Can't input %c in the current unibyte buffer" char)
  1432. (ding)
  1433. (sit-for 2)
  1434. (message nil)
  1435. (setq quail-current-str nil)
  1436. (throw 'quail-tag nil))))
  1437. (insert quail-current-str)
  1438. (move-overlay quail-overlay pos (point))
  1439. (if (overlayp quail-conv-overlay)
  1440. (if (not (overlay-start quail-conv-overlay))
  1441. (move-overlay quail-conv-overlay pos (point))
  1442. (if (< (overlay-end quail-conv-overlay) (point))
  1443. (move-overlay quail-conv-overlay
  1444. (overlay-start quail-conv-overlay)
  1445. (point)))))))
  1446. (let (quail-current-str)
  1447. (quail-update-guidance))
  1448. (or (stringp quail-current-str)
  1449. (setq quail-current-str (char-to-string quail-current-str)))
  1450. (if control-flag
  1451. (quail-terminate-translation)))
  1452. (defun quail-self-insert-command ()
  1453. "Translate the typed key by the current Quail map, and insert."
  1454. (interactive "*")
  1455. (setq quail-current-key
  1456. (concat quail-current-key (char-to-string last-command-event)))
  1457. (or (catch 'quail-tag
  1458. (quail-update-translation (quail-translate-key))
  1459. t)
  1460. ;; If someone throws for `quail-tag' by value nil, we exit from
  1461. ;; translation mode.
  1462. (setq quail-translating nil)))
  1463. (defun quail-map-definition (map)
  1464. "Return the actual definition part of Quail map MAP."
  1465. (let ((def (car map)))
  1466. (if (and (consp def) (not (vectorp (cdr def))))
  1467. (setq def (car def)))
  1468. (if (eq def t)
  1469. (setq def nil))
  1470. def))
  1471. (defun quail-get-current-str (len def)
  1472. "Return string to be shown as current translation of key sequence.
  1473. LEN is the length of the sequence. DEF is a definition part of the
  1474. Quail map for the sequence."
  1475. (or (and (consp def)
  1476. (if (> (length (cdr def)) (car (car def)))
  1477. (aref (cdr def) (car (car def)))
  1478. ""))
  1479. def
  1480. (and (> len 1)
  1481. (let* ((str (quail-get-current-str
  1482. (1- len)
  1483. (quail-map-definition (quail-lookup-key
  1484. quail-current-key (1- len)))))
  1485. (substr1 (substring quail-current-key (1- len) len))
  1486. (str1 (and (quail-deterministic)
  1487. (quail-get-current-str
  1488. 1
  1489. (quail-map-definition (quail-lookup-key
  1490. substr1 1))))))
  1491. (if str
  1492. (concat (if (stringp str) str (char-to-string str))
  1493. (if str1
  1494. (if (stringp str1) str1 (char-to-string str1))
  1495. substr1)))))))
  1496. (defvar quail-guidance-translations-starting-column 20)
  1497. (defun quail-update-current-translations (&optional relative-index)
  1498. "Update `quail-current-translations'.
  1499. Make RELATIVE-INDEX the current translation."
  1500. (let* ((indices (car quail-current-translations))
  1501. (cur (car indices))
  1502. (start (nth 1 indices))
  1503. (end (nth 2 indices)))
  1504. ;; Validate the index number of current translation.
  1505. (if (< cur 0)
  1506. (setcar indices (setq cur 0))
  1507. (if (>= cur (length (cdr quail-current-translations)))
  1508. (setcar indices
  1509. (setq cur (1- (length (cdr quail-current-translations)))))))
  1510. (if (or (null end) ; We have not yet calculated END.
  1511. (< cur start) ; We moved to the previous block.
  1512. (>= cur end)) ; We moved to the next block.
  1513. (let ((len (length (cdr quail-current-translations)))
  1514. (maxcol (- (window-width)
  1515. quail-guidance-translations-starting-column))
  1516. (block (nth 3 indices))
  1517. col idx width trans num-items)
  1518. (if (< cur start)
  1519. ;; We must calculate from the head.
  1520. (setq start 0 block 0)
  1521. (if end ; i.e. (>= cur end)
  1522. (setq start end)))
  1523. (setq idx start col 0 end start num-items 0)
  1524. ;; Loop until we hit the tail, or reach the block of CUR.
  1525. (while (and (< idx len) (>= cur end))
  1526. (if (= num-items 0)
  1527. (setq start idx col 0 block (1+ block)))
  1528. (setq trans (aref (cdr quail-current-translations) idx))
  1529. (setq width (if (integerp trans) (char-width trans)
  1530. (string-width trans)))
  1531. (setq col (+ col width 3) num-items (1+ num-items))
  1532. (if (and (> num-items 0)
  1533. (or (>= col maxcol) (> num-items 10)))
  1534. (setq end idx num-items 0)
  1535. (setq idx (1+ idx))))
  1536. (setcar (nthcdr 3 indices) block)
  1537. (if (>= idx len)
  1538. (progn
  1539. ;; We hit the tail before reaching MAXCOL.
  1540. (setq end idx)
  1541. (setcar (nthcdr 4 indices) block)))
  1542. (setcar (cdr indices) start)
  1543. (setcar (nthcdr 2 indices) end)))
  1544. (if relative-index
  1545. (if (>= (+ start relative-index) end)
  1546. (setcar indices (1- end))
  1547. (setcar indices (+ start relative-index))))
  1548. (setq quail-current-str
  1549. (aref (cdr quail-current-translations) (car indices)))
  1550. (or (stringp quail-current-str)
  1551. (setq quail-current-str (char-to-string quail-current-str)))))
  1552. (defun quail-translate-key ()
  1553. "Translate the current key sequence according to the current Quail map.
  1554. Return t if we can terminate the translation.
  1555. Return nil if the current key sequence may be followed by more keys.
  1556. Return number if we can't find any translation for the current key
  1557. sequence. The number is the count of valid keys in the current
  1558. sequence counting from the head."
  1559. (let* ((len (length quail-current-key))
  1560. (map (quail-lookup-key quail-current-key len))
  1561. def ch)
  1562. (if map
  1563. (let ((def (quail-map-definition map)))
  1564. (setq quail-current-str (quail-get-current-str len def))
  1565. ;; Return t only if we can terminate the current translation.
  1566. (and
  1567. ;; No alternative translations.
  1568. (or (null (consp def)) (= (length (cdr def)) 1))
  1569. ;; No translation for the longer key.
  1570. (null (cdr map))
  1571. ;; No shorter breaking point.
  1572. (or (null (quail-maximum-shortest))
  1573. (< len 3)
  1574. (null (quail-lookup-key quail-current-key (1- len)))
  1575. (null (quail-lookup-key
  1576. (substring quail-current-key -2 -1) 1)))))
  1577. ;; There's no translation for the current key sequence. Before
  1578. ;; giving up, we must check two possibilities.
  1579. (cond ((and
  1580. (quail-maximum-shortest)
  1581. (>= len 3)
  1582. (setq def (quail-map-definition
  1583. (quail-lookup-key quail-current-key (- len 2))))
  1584. (quail-lookup-key (substring quail-current-key -2) 2))
  1585. ;; Now the sequence is "...ABCD", which can be split into
  1586. ;; "...AB" and "CD..." to get valid translation.
  1587. ;; At first, get translation of "...AB".
  1588. (setq quail-current-str (quail-get-current-str (- len 2) def))
  1589. ;; Then, return the length of "...AB".
  1590. (- len 2))
  1591. ((and (> len 0)
  1592. (quail-lookup-key (substring quail-current-key 0 -1))
  1593. quail-current-translations
  1594. (not (quail-deterministic))
  1595. (setq ch (aref quail-current-key (1- len)))
  1596. (>= ch ?0) (<= ch ?9))
  1597. ;; A numeric key is entered to select a desirable translation.
  1598. (setq quail-current-key (substring quail-current-key 0 -1))
  1599. ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
  1600. (setq ch (if (= ch ?0) 9 (- ch ?1)))
  1601. (quail-update-current-translations ch)
  1602. ;; And, we can terminate the current translation.
  1603. t)
  1604. ((quail-deterministic)
  1605. ;; No way to handle the last character in this context.
  1606. ;; Commit the longest successfully translated characters, and
  1607. ;; handle the remaining characters in a new loop.
  1608. (setq def nil)
  1609. (while (and (not def) (> len 1))
  1610. (setq len (1- len))
  1611. (setq def (quail-map-definition
  1612. (quail-lookup-key quail-current-key len))))
  1613. (if def (setq quail-current-str
  1614. (quail-get-current-str len def))
  1615. (setq quail-current-str (aref quail-current-key 0)))
  1616. len)
  1617. (t
  1618. ;; No way to handle the last character in this context.
  1619. (setq def (quail-map-definition
  1620. (quail-lookup-key quail-current-key (1- len))))
  1621. (if def (setq quail-current-str
  1622. (quail-get-current-str (1- len) def)))
  1623. (1- len))))))
  1624. (defun quail-next-translation ()
  1625. "Select next translation in the current batch of candidates."
  1626. (interactive)
  1627. (if quail-current-translations
  1628. (let ((indices (car quail-current-translations)))
  1629. (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
  1630. ;; We are already at the tail.
  1631. (beep)
  1632. (setcar indices (1+ (car indices)))
  1633. (quail-update-current-translations)
  1634. (quail-update-translation nil)))
  1635. (setq unread-command-events
  1636. (cons last-command-event unread-command-events))
  1637. (quail-terminate-translation)))
  1638. (defun quail-prev-translation ()
  1639. "Select previous translation in the current batch of candidates."
  1640. (interactive)
  1641. (if quail-current-translations
  1642. (let ((indices (car quail-current-translations)))
  1643. (if (= (car indices) 0)
  1644. ;; We are already at the head.
  1645. (beep)
  1646. (setcar indices (1- (car indices)))
  1647. (quail-update-current-translations)
  1648. (quail-update-translation nil)))
  1649. (setq unread-command-events
  1650. (cons last-command-event unread-command-events))
  1651. (quail-terminate-translation)))
  1652. (defun quail-next-translation-block ()
  1653. "Select from the next block of translations."
  1654. (interactive)
  1655. (if quail-current-translations
  1656. (let* ((indices (car quail-current-translations))
  1657. (offset (- (car indices) (nth 1 indices))))
  1658. (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
  1659. ;; We are already at the last block.
  1660. (beep)
  1661. (setcar indices (+ (nth 2 indices) offset))
  1662. (quail-update-current-translations)
  1663. (quail-update-translation nil)))
  1664. (setq unread-command-events
  1665. (cons last-command-event unread-command-events))
  1666. (quail-terminate-translation)))
  1667. (defun quail-prev-translation-block ()
  1668. "Select the previous batch of 10 translation candidates."
  1669. (interactive)
  1670. (if quail-current-translations
  1671. (let* ((indices (car quail-current-translations))
  1672. (offset (- (car indices) (nth 1 indices))))
  1673. (if (= (nth 1 indices) 0)
  1674. ;; We are already at the first block.
  1675. (beep)
  1676. (setcar indices (1- (nth 1 indices)))
  1677. (quail-update-current-translations)
  1678. (if (< (+ (nth 1 indices) offset) (nth 2 indices))
  1679. (progn
  1680. (setcar indices (+ (nth 1 indices) offset))
  1681. (quail-update-current-translations)))
  1682. (quail-update-translation nil)))
  1683. (setq unread-command-events
  1684. (cons last-command-event unread-command-events))
  1685. (quail-terminate-translation)))
  1686. (defun quail-abort-translation ()
  1687. "Abort translation and delete the current Quail key sequence."
  1688. (interactive)
  1689. (quail-delete-region)
  1690. (setq quail-current-str nil)
  1691. (quail-terminate-translation))
  1692. (defun quail-delete-last-char ()
  1693. "Delete the last input character from the current Quail key sequence."
  1694. (interactive)
  1695. (if (= (length quail-current-key) 1)
  1696. (quail-abort-translation)
  1697. (setq quail-current-key (substring quail-current-key 0 -1))
  1698. (quail-delete-region)
  1699. (quail-update-translation (quail-translate-key))))
  1700. ;; For conversion mode.
  1701. (defsubst quail-point-in-conversion-region ()
  1702. "Return non-nil value if the point is in conversion region of Quail mode."
  1703. (let (start pos)
  1704. (and (setq start (overlay-start quail-conv-overlay))
  1705. (>= (setq pos (point)) start)
  1706. (<= pos (overlay-end quail-conv-overlay)))))
  1707. (defun quail-conversion-backward-char ()
  1708. (interactive)
  1709. (if (<= (point) (overlay-start quail-conv-overlay))
  1710. (quail-error "Beginning of conversion region"))
  1711. (setq quail-translating nil)
  1712. (forward-char -1))
  1713. (defun quail-conversion-forward-char ()
  1714. (interactive)
  1715. (if (>= (point) (overlay-end quail-conv-overlay))
  1716. (quail-error "End of conversion region"))
  1717. (setq quail-translating nil)
  1718. (forward-char 1))
  1719. (defun quail-conversion-beginning-of-region ()
  1720. (interactive)
  1721. (setq quail-translating nil)
  1722. (goto-char (overlay-start quail-conv-overlay)))
  1723. (defun quail-conversion-end-of-region ()
  1724. (interactive)
  1725. (setq quail-translating nil)
  1726. (goto-char (overlay-end quail-conv-overlay)))
  1727. (defun quail-conversion-delete-char ()
  1728. (interactive)
  1729. (setq quail-translating nil)
  1730. (if (>= (point) (overlay-end quail-conv-overlay))
  1731. (quail-error "End of conversion region"))
  1732. (delete-char 1)
  1733. (let ((start (overlay-start quail-conv-overlay))
  1734. (end (overlay-end quail-conv-overlay)))
  1735. (setq quail-conversion-str (buffer-substring start end))
  1736. (if (= start end)
  1737. (setq quail-converting nil))))
  1738. (defun quail-conversion-delete-tail ()
  1739. (interactive)
  1740. (if (>= (point) (overlay-end quail-conv-overlay))
  1741. (quail-error "End of conversion region"))
  1742. (delete-region (point) (overlay-end quail-conv-overlay))
  1743. (let ((start (overlay-start quail-conv-overlay))
  1744. (end (overlay-end quail-conv-overlay)))
  1745. (setq quail-conversion-str (buffer-substring start end))
  1746. (if (= start end)
  1747. (setq quail-converting nil))))
  1748. (defun quail-conversion-backward-delete-char ()
  1749. (interactive)
  1750. (if (> (length quail-current-key) 0)
  1751. (quail-delete-last-char)
  1752. (if (<= (point) (overlay-start quail-conv-overlay))
  1753. (quail-error "Beginning of conversion region"))
  1754. (delete-char -1)
  1755. (let ((start (overlay-start quail-conv-overlay))
  1756. (end (overlay-end quail-conv-overlay)))
  1757. (setq quail-conversion-str (buffer-substring start end))
  1758. (if (= start end)
  1759. (setq quail-converting nil)))))
  1760. (defun quail-do-conversion (func &rest args)
  1761. "Call FUNC to convert text in the current conversion region of Quail.
  1762. Remaining args are for FUNC."
  1763. (delete-overlay quail-overlay)
  1764. (apply func args))
  1765. (defun quail-no-conversion ()
  1766. "Do no conversion of the current conversion region of Quail."
  1767. (interactive)
  1768. (setq quail-converting nil))
  1769. ;; Guidance, Completion, and Help buffer handlers.
  1770. (defun quail-make-guidance-frame ()
  1771. "Make a new one-line frame for Quail guidance."
  1772. (let* ((fparam (frame-parameters))
  1773. (top (cdr (assq 'top fparam)))
  1774. (border (cdr (assq 'border-width fparam)))
  1775. (internal-border (cdr (assq 'internal-border-width fparam)))
  1776. (newtop (- top
  1777. (frame-char-height) (* internal-border 2) (* border 2))))
  1778. (if (< newtop 0)
  1779. (setq newtop (+ top (frame-pixel-height) internal-border border)))
  1780. ;; If I leave the `parent-id' parameter, my frame ends up with 13 lines
  1781. ;; rather than just 1. Not sure what is really going on, but
  1782. ;; clearly this parameter is not needed. --Stef
  1783. (setq fparam (delq (assoc 'parent-id fparam) fparam))
  1784. (make-frame (append '((user-position . t) (height . 1)
  1785. (minibuffer)
  1786. (menu-bar-lines . 0) (tool-bar-lines . 0))
  1787. (cons (cons 'top newtop) fparam)))))
  1788. (defun quail-setup-completion-buf ()
  1789. "Setup Quail completion buffer."
  1790. (unless (buffer-live-p quail-completion-buf)
  1791. (let ((mb enable-multibyte-characters))
  1792. (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
  1793. (with-current-buffer quail-completion-buf
  1794. (set-buffer-multibyte mb)
  1795. (setq buffer-read-only t)
  1796. (setq quail-overlay (make-overlay (point-min) (point-min)))
  1797. (overlay-put quail-overlay 'face 'highlight)))))
  1798. (defun quail-require-guidance-buf ()
  1799. "Return t if the current Quail package requires showing guidance buffer."
  1800. (and input-method-verbose-flag
  1801. (if (eq input-method-verbose-flag 'default)
  1802. (not (and (eq (selected-window) (minibuffer-window))
  1803. (quail-simple)))
  1804. (if (eq input-method-verbose-flag 'complex-only)
  1805. (not (quail-simple))
  1806. t))))
  1807. ;; Quail specific version of minibuffer-message. It displays STRING
  1808. ;; with timeout 1000000 seconds instead of two seconds.
  1809. (defun quail-minibuffer-message (string)
  1810. (message nil)
  1811. (let ((point-max (point-max))
  1812. (inhibit-quit t))
  1813. (save-excursion
  1814. (goto-char point-max)
  1815. (insert string))
  1816. (sit-for 1000000)
  1817. (delete-region point-max (point-max))
  1818. (when quit-flag
  1819. (setq quit-flag nil
  1820. unread-command-events '(7)))))
  1821. (defun quail-show-guidance ()
  1822. "Display a guidance for Quail input method in some window.
  1823. The guidance is normally displayed at the echo area,
  1824. or in a newly created frame (if the current buffer is a
  1825. minibuffer and the selected frame has no other windows)."
  1826. ;; At first, setup a buffer for completion.
  1827. (quail-setup-completion-buf)
  1828. (bury-buffer quail-completion-buf)
  1829. ;; Then, show the guidance.
  1830. (when (and (quail-require-guidance-buf)
  1831. (not input-method-use-echo-area)
  1832. (null unread-command-events)
  1833. (null unread-post-input-method-events))
  1834. (if (minibufferp)
  1835. (if (eq (minibuffer-window) (frame-root-window))
  1836. ;; Use another frame. It is sure that we are using some
  1837. ;; window system.
  1838. (let ((guidance quail-guidance-str))
  1839. (or (frame-live-p quail-guidance-frame)
  1840. (setq quail-guidance-frame
  1841. (quail-make-guidance-frame)))
  1842. (or (buffer-live-p quail-guidance-buf)
  1843. (setq quail-guidance-buf
  1844. (get-buffer-create " *Quail-guidance*")))
  1845. (with-current-buffer quail-guidance-buf
  1846. (erase-buffer)
  1847. (setq cursor-type nil)
  1848. (insert guidance))
  1849. (let ((win (frame-root-window quail-guidance-frame)))
  1850. (set-window-buffer win quail-guidance-buf)
  1851. (set-window-dedicated-p win t))
  1852. (quail-minibuffer-message
  1853. (format " [%s]" current-input-method-title)))
  1854. ;; Show the guidance in the next line of the current
  1855. ;; minibuffer.
  1856. (quail-minibuffer-message
  1857. (format " [%s]\n%s"
  1858. current-input-method-title quail-guidance-str)))
  1859. ;; Show the guidance in echo area without logging.
  1860. (let ((message-log-max nil))
  1861. (message "%s" quail-guidance-str)))))
  1862. (defun quail-hide-guidance ()
  1863. "Hide the Quail guidance."
  1864. (when (and (quail-require-guidance-buf)
  1865. (or (eq (selected-window) (minibuffer-window))
  1866. input-method-use-echo-area)
  1867. (eq (minibuffer-window) (frame-root-window)))
  1868. ;; We are using another frame for the guidance.
  1869. (if (frame-live-p quail-guidance-frame)
  1870. (delete-frame quail-guidance-frame))
  1871. (if (buffer-live-p quail-guidance-buf)
  1872. (kill-buffer quail-guidance-buf))))
  1873. (defun quail-update-guidance ()
  1874. "Update the Quail guidance buffer and completion buffer (if displayed now)."
  1875. ;; Update the guidance string.
  1876. (when (quail-require-guidance-buf)
  1877. (let ((guidance (quail-guidance)))
  1878. (cond ((or (eq guidance t)
  1879. (consp guidance))
  1880. ;; Show the current possible translations.
  1881. (setq quail-guidance-str
  1882. (quail-get-translations)))
  1883. ((null guidance)
  1884. ;; Show the current input keys.
  1885. (let ((key quail-current-key))
  1886. (if (quail-kbd-translate)
  1887. (setq key (quail-keyseq-translate key)))
  1888. (setq quail-guidance-str (if (stringp key) key (string key)))))
  1889. (t
  1890. (setq quail-guidance-str " ")))))
  1891. ;; Update completion buffer if displayed now. We highlight the
  1892. ;; selected candidate string in *Completion* buffer if any.
  1893. (let ((win (get-buffer-window quail-completion-buf))
  1894. key str pos)
  1895. (if win
  1896. (save-excursion
  1897. (setq str (if (stringp quail-current-str)
  1898. quail-current-str
  1899. (if (numberp quail-current-str)
  1900. (char-to-string quail-current-str)))
  1901. key quail-current-key)
  1902. (set-buffer quail-completion-buf)
  1903. (goto-char (point-min))
  1904. (if (null (search-forward (concat " " key ":") nil t))
  1905. (delete-overlay quail-overlay)
  1906. (setq pos (point))
  1907. (if (and str (search-forward (concat "." str) nil t))
  1908. (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
  1909. (move-overlay quail-overlay (match-beginning 0) (point)))
  1910. ;; Now POS points end of KEY and (point) points end of STR.
  1911. (if (pos-visible-in-window-p (point) win)
  1912. ;; STR is already visible.
  1913. nil
  1914. ;; We want to make both KEY and STR visible, but if the
  1915. ;; window is too short, make at least STR visible.
  1916. (setq pos (progn (point) (goto-char pos)))
  1917. (beginning-of-line)
  1918. (set-window-start win (point))
  1919. (if (not (pos-visible-in-window-p pos win))
  1920. (set-window-start win pos))
  1921. ))))))
  1922. (defun quail-get-translations ()
  1923. "Return a string containing the current possible translations."
  1924. (or (multibyte-string-p quail-current-key)
  1925. (setq quail-current-key (string-to-multibyte quail-current-key)))
  1926. (let ((map (quail-lookup-key quail-current-key nil t))
  1927. (str (copy-sequence quail-current-key)))
  1928. (if quail-current-translations
  1929. (quail-update-current-translations))
  1930. ;; Show the current key.
  1931. (let ((guidance (quail-guidance)))
  1932. (if (listp guidance)
  1933. ;; We must replace the typed key with the specified PROMPT-KEY.
  1934. (dotimes (i (length str))
  1935. (let ((prompt-key (cdr (assoc (aref str i) guidance))))
  1936. (if prompt-key
  1937. (aset str i (aref prompt-key 0)))))))
  1938. ;; Show followable keys.
  1939. (if (and (> (length quail-current-key) 0) (cdr map))
  1940. (setq str
  1941. (format "%s[%s]"
  1942. str
  1943. (concat (sort (mapcar (function (lambda (x) (car x)))
  1944. (cdr map))
  1945. '<)))))
  1946. ;; Show list of translations.
  1947. (if (and quail-current-translations
  1948. (not (quail-deterministic)))
  1949. (let* ((indices (car quail-current-translations))
  1950. (cur (car indices))
  1951. (start (nth 1 indices))
  1952. (end (nth 2 indices))
  1953. (idx start))
  1954. (if (< (string-width str)
  1955. (- quail-guidance-translations-starting-column 7))
  1956. (setq str
  1957. (concat str
  1958. (make-string
  1959. (- quail-guidance-translations-starting-column
  1960. 7 (string-width str))
  1961. 32))))
  1962. (setq str (format "%s(%02d/%s)"
  1963. str (nth 3 indices)
  1964. (if (nth 4 indices)
  1965. (format "%02d" (nth 4 indices))
  1966. "??")))
  1967. (while (< idx end)
  1968. (let ((len (length str))
  1969. (trans (aref (cdr quail-current-translations) idx)))
  1970. (or (stringp trans)
  1971. (setq trans (string trans)))
  1972. (setq str (format "%s %d.%s"
  1973. str
  1974. (if (= (- idx start) 9) 0
  1975. (1+ (- idx start)))
  1976. trans))
  1977. (if (= idx cur)
  1978. (put-text-property (+ len 3) (length str)
  1979. 'face 'highlight str))
  1980. (setq idx (1+ idx))))))
  1981. str))
  1982. (defvar quail-completion-max-depth 5
  1983. "The maximum depth of Quail completion list.")
  1984. (defun quail-completion ()
  1985. "List all completions for the current key.
  1986. All possible translations of the current key and whole possible longer keys
  1987. are shown (at most to the depth specified `quail-completion-max-depth')."
  1988. (interactive)
  1989. (quail-setup-completion-buf)
  1990. (let ((win (get-buffer-window quail-completion-buf 'visible))
  1991. (key quail-current-key)
  1992. (map (quail-lookup-key quail-current-key nil t))
  1993. (require-update nil))
  1994. (with-current-buffer quail-completion-buf
  1995. (if (and win
  1996. (equal key quail-current-key)
  1997. (eq last-command 'quail-completion))
  1998. ;; The window for Quail completion buffer has already been
  1999. ;; shown. We just scroll it appropriately.
  2000. (if (pos-visible-in-window-p (point-max) win)
  2001. (set-window-start win (point-min))
  2002. (let ((other-window-scroll-buffer quail-completion-buf)
  2003. ;; This nil binding is necessary to surely scroll
  2004. ;; quail-completion-buf.
  2005. (minibuffer-scroll-window nil))
  2006. (scroll-other-window)))
  2007. (setq quail-current-key key)
  2008. (let ((inhibit-read-only t))
  2009. (erase-buffer)
  2010. (insert "Possible completion and corresponding characters are:\n")
  2011. (quail-completion-1 key map 1)
  2012. (set-buffer-modified-p nil))
  2013. (goto-char (point-min))
  2014. (display-buffer (current-buffer))
  2015. (setq require-update t)))
  2016. (if require-update
  2017. (quail-update-guidance)))
  2018. (setq this-command 'quail-completion))
  2019. (defun quail-completion-1 (key map indent)
  2020. "List all completions of KEY in MAP with indentation INDENT."
  2021. (let ((len (length key)))
  2022. (quail-indent-to indent)
  2023. (insert key ":")
  2024. (if (and (symbolp map) (fboundp map))
  2025. (setq map (funcall map key len)))
  2026. (if (car map)
  2027. (quail-completion-list-translations map key (+ indent len 1))
  2028. (insert " -\n"))
  2029. (setq indent (+ indent 2))
  2030. (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
  2031. (let ((l (cdr map)))
  2032. (if (functionp l)
  2033. (setq l (funcall l)))
  2034. (dolist (elt (reverse l)) ; L = ((CHAR . DEFN) ....) ;
  2035. (quail-completion-1 (concat key (string (car elt)))
  2036. (cdr elt) indent))))))
  2037. (defun quail-completion-list-translations (map key indent)
  2038. "List all possible translations of KEY in Quail MAP with indentation INDENT."
  2039. (let (beg (translations
  2040. (quail-get-translation (car map) key (length key))))
  2041. (if (integerp translations)
  2042. (progn
  2043. (insert "(1/1) 1.")
  2044. ;; Endow the character `translations' with `mouse-face' text
  2045. ;; property to enable `mouse-2' completion.
  2046. (setq beg (point))
  2047. (insert translations)
  2048. (put-text-property beg (point) 'mouse-face 'highlight)
  2049. (insert "\n"))
  2050. ;; We need only vector part.
  2051. (setq translations (cdr translations))
  2052. ;; Insert every 10 elements with indices in a line.
  2053. (let ((len (length translations))
  2054. (i 0))
  2055. (while (< i len)
  2056. (when (zerop (% i 10))
  2057. (when (>= i 10)
  2058. (insert "\n")
  2059. (quail-indent-to indent))
  2060. (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
  2061. ;; We show the last digit of FROM while converting
  2062. ;; 0,1,..,9 to 1,2,..,0.
  2063. (insert (format " %d." (% (1+ i) 10)))
  2064. (setq beg (point))
  2065. (insert (aref translations i))
  2066. ;; Passing the mouse over a character will highlight.
  2067. (put-text-property beg (point) 'mouse-face 'highlight)
  2068. (setq i (1+ i)))
  2069. (insert "\n")))))
  2070. (defun quail-mouse-choose-completion (event)
  2071. "Click on an alternative in the `*Quail Completions*' buffer to choose it."
  2072. ;; This function is an exact copy of the mouse.el function
  2073. ;; `mouse-choose-completion' except that we:
  2074. ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
  2075. ;; 3) delete/terminate the current quail selection here.
  2076. ;; FIXME: Consolidate with `choose-completion'. The point number
  2077. ;; 1 has been done, already. The point number 3 should be fairly
  2078. ;; easy to move to a choose-completion-string-function. So all
  2079. ;; that's left is point number 2.
  2080. (interactive "e")
  2081. ;; Give temporary modes such as isearch a chance to turn off.
  2082. (run-hooks 'mouse-leave-buffer-hook)
  2083. (let ((buffer (window-buffer))
  2084. choice)
  2085. (with-current-buffer (window-buffer (posn-window (event-start event)))
  2086. (if completion-reference-buffer
  2087. (setq buffer completion-reference-buffer))
  2088. (save-excursion
  2089. (goto-char (posn-point (event-start event)))
  2090. (let (beg end)
  2091. (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
  2092. (setq end (point) beg (1+ (point))))
  2093. (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
  2094. (setq end (1- (point)) beg (point)))
  2095. (if (null beg)
  2096. (quail-error "No completion here"))
  2097. (setq beg (previous-single-property-change beg 'mouse-face))
  2098. (setq end (or (next-single-property-change end 'mouse-face)
  2099. (point-max)))
  2100. (setq choice (buffer-substring beg end)))))
  2101. ;; (let ((owindow (selected-window)))
  2102. ;; (select-window (posn-window (event-start event)))
  2103. ;; (if (and (one-window-p t 'selected-frame)
  2104. ;; (window-dedicated-p (selected-window)))
  2105. ;; ;; This is a special buffer's frame
  2106. ;; (iconify-frame (selected-frame))
  2107. ;; (or (window-dedicated-p (selected-window))
  2108. ;; (bury-buffer)))
  2109. ;; (select-window owindow))
  2110. (quail-delete-region)
  2111. (setq quail-current-str choice)
  2112. ;; FIXME: We need to pass `base-position' here.
  2113. ;; FIXME: why do we need choose-completion-string with all its
  2114. ;; completion-specific logic?
  2115. (choose-completion-string choice buffer)
  2116. (quail-terminate-translation)))
  2117. (defun quail-build-decode-map (map-list key decode-map num
  2118. &optional maxnum ignores)
  2119. "Build a decoding map.
  2120. Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
  2121. vs the corresponding translations defined in the Quail map
  2122. specified by the first element MAP-LIST. Each pair has the form
  2123. \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
  2124. \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
  2125. is a key sequence to reach MAP.
  2126. Optional 5th arg MAXNUM limits the number of accumulated pairs.
  2127. Optional 6th arg IGNORES is a list of translations to ignore."
  2128. (let* ((map (car map-list))
  2129. (translation (quail-get-translation (car map) key (length key)))
  2130. elt)
  2131. (cond ((integerp translation)
  2132. ;; Accept only non-ASCII chars not listed in IGNORES.
  2133. (when (and (> translation 127) (not (memq translation ignores)))
  2134. (setcdr decode-map
  2135. (cons (cons key translation) (cdr decode-map)))
  2136. (setq num (1+ num))))
  2137. ((consp translation)
  2138. (setq translation (cdr translation))
  2139. (let ((multibyte nil))
  2140. (mapc (function (lambda (x)
  2141. ;; Accept only non-ASCII chars not
  2142. ;; listed in IGNORES.
  2143. (if (and (if (integerp x) (> x 127)
  2144. (string-match-p "[^[:ascii:]]" x))
  2145. (not (member x ignores)))
  2146. (setq multibyte t))))
  2147. translation)
  2148. (when multibyte
  2149. (setcdr decode-map
  2150. (cons (cons key translation) (cdr decode-map)))
  2151. (setq num (+ num (length translation)))))))
  2152. (if (and maxnum (> num maxnum))
  2153. (- num)
  2154. (setq map (cdr map))
  2155. ;; Recursively check the deeper map.
  2156. (while (and map (>= num 0))
  2157. (setq elt (car map) map (cdr map))
  2158. (when (and (integerp (car elt)) (consp (cdr elt))
  2159. (not (memq (cdr elt) map-list)))
  2160. (setq num (quail-build-decode-map (cons (cdr elt) map-list)
  2161. (format "%s%c" key (car elt))
  2162. decode-map num maxnum ignores))))
  2163. num)))
  2164. (defun quail-insert-decode-map (decode-map)
  2165. "Insert pairs of key sequences vs the corresponding translations.
  2166. These are stored in DECODE-MAP using the concise format. DECODE-MAP
  2167. should be made by `quail-build-decode-map' (which see)."
  2168. (setq decode-map
  2169. (sort (cdr decode-map)
  2170. (function (lambda (x y)
  2171. (setq x (car x) y (car y))
  2172. (or (> (length x) (length y))
  2173. (and (= (length x) (length y))
  2174. (not (string< x y))))))))
  2175. (let ((window-width (window-width (get-buffer-window
  2176. (current-buffer) 'visible)))
  2177. (single-trans-width 4)
  2178. (single-list nil)
  2179. (multiple-list nil)
  2180. trans)
  2181. ;; Divide the elements of decoding map into single ones (i.e. the
  2182. ;; one that has single translation) and multiple ones (i.e. the
  2183. ;; one that has multiple translations).
  2184. (dolist (elt decode-map)
  2185. (setq trans (cdr elt))
  2186. (if (and (vectorp trans) (= (length trans) 1))
  2187. (setq trans (aref trans 0)))
  2188. (if (vectorp trans)
  2189. (push elt multiple-list)
  2190. (push (cons (car elt) trans) single-list)
  2191. (let ((width (if (stringp trans) (string-width trans)
  2192. (char-width trans))))
  2193. (if (> width single-trans-width)
  2194. (setq single-trans-width width)))))
  2195. (when single-list
  2196. ;; Figure out how many columns can fit.
  2197. (let* ((len (length single-list))
  2198. ;; The longest key is at the end, by virtue of the above `sort'.
  2199. (max-key-width (max 3 (length (caar (last single-list)))))
  2200. ;; Starting point: worst case.
  2201. (col-width (+ max-key-width 1 single-trans-width 1))
  2202. (cols (/ window-width col-width))
  2203. rows)
  2204. ;; Now, let's see if we can pack in a few more columns since
  2205. ;; the first columns can often be made narrower thanks to the
  2206. ;; length-sorting.
  2207. (while (let ((newrows (/ (+ len cols) (1+ cols))) ;Round up.
  2208. (width 0))
  2209. (dotimes (col (1+ cols))
  2210. (let ((last-col-elt (or (nth (1- (* (1+ col) newrows))
  2211. single-list)
  2212. (car (last single-list)))))
  2213. (incf width (+ (max 3 (length (car last-col-elt)))
  2214. 1 single-trans-width 1))))
  2215. (< width window-width))
  2216. (incf cols))
  2217. (setq rows (/ (+ len cols -1) cols)) ;Round up.
  2218. (let ((key-width (max 3 (length (car (nth (1- rows) single-list))))))
  2219. (insert "key")
  2220. (quail-indent-to (1+ key-width))
  2221. (insert "char")
  2222. (quail-indent-to (+ 1 key-width 1 single-trans-width 1)))
  2223. (insert "[type a key sequence to insert the corresponding character]\n")
  2224. (let ((pos (point))
  2225. (col 0))
  2226. (insert-char ?\n (+ rows 2))
  2227. (while single-list
  2228. (goto-char pos)
  2229. (let* ((key-width (max 3 (length
  2230. (car (or (nth (1- rows) single-list)
  2231. (car (last single-list)))))))
  2232. (col-width (+ key-width 1 single-trans-width 1)))
  2233. ;; Insert the header-line.
  2234. (move-to-column col)
  2235. (quail-indent-to col)
  2236. (insert-char ?- key-width)
  2237. (insert ?\s)
  2238. (insert-char ?- single-trans-width)
  2239. (forward-line 1)
  2240. ;; Insert the key-tran pairs.
  2241. (dotimes (row rows)
  2242. (let ((elt (pop single-list)))
  2243. (when elt
  2244. (move-to-column col)
  2245. (quail-indent-to col)
  2246. (insert (propertize (car elt)
  2247. 'face 'font-lock-comment-face))
  2248. (quail-indent-to (+ col key-width 1))
  2249. (insert (cdr elt))
  2250. (forward-line 1))))
  2251. (setq col (+ col col-width)))))
  2252. (goto-char (point-max))))
  2253. (when multiple-list
  2254. ;; Since decode-map is sorted, we known the longest key is at the end.
  2255. (let ((max-key-width (max 3 (length (caar (last multiple-list))))))
  2256. (insert "key")
  2257. (quail-indent-to (1+ max-key-width))
  2258. (insert "character(s) [type a key (sequence) and select one from the list]\n")
  2259. (insert-char ?- max-key-width)
  2260. (insert " ------------\n")
  2261. (dolist (elt multiple-list)
  2262. (insert (propertize (car elt)
  2263. 'face 'font-lock-comment-face))
  2264. (quail-indent-to max-key-width)
  2265. (if (vectorp (cdr elt))
  2266. (mapc (function
  2267. (lambda (x)
  2268. (let ((width (if (integerp x) (char-width x)
  2269. (string-width x))))
  2270. (when (> (+ (current-column) 1 width) window-width)
  2271. (insert "\n")
  2272. (quail-indent-to max-key-width))
  2273. (insert " " x))))
  2274. (cdr elt))
  2275. (insert " " (cdr elt)))
  2276. (insert ?\n))
  2277. (insert ?\n)))))
  2278. (define-button-type 'quail-keyboard-layout-button
  2279. :supertype 'help-xref
  2280. 'help-function (lambda (layout)
  2281. (help-setup-xref `(quail-keyboard-layout-button ,layout)
  2282. nil)
  2283. (quail-show-keyboard-layout layout))
  2284. 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
  2285. (define-button-type 'quail-keyboard-customize-button
  2286. :supertype 'help-customize-variable
  2287. 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
  2288. (defun quail-help (&optional package)
  2289. "Show brief description of the current Quail package.
  2290. Optional arg PACKAGE specifies the name of alternative Quail
  2291. package to describe."
  2292. (interactive)
  2293. (require 'help-mode)
  2294. (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
  2295. (mb enable-multibyte-characters)
  2296. (package-def
  2297. (if package
  2298. (assoc package quail-package-alist)
  2299. quail-current-package)))
  2300. ;; At first, make sure that the help buffer has window.
  2301. (let ((temp-buffer-show-hook nil))
  2302. (with-output-to-temp-buffer (help-buffer)
  2303. (with-current-buffer standard-output
  2304. (set-buffer-multibyte mb)
  2305. (setq quail-current-package package-def))))
  2306. ;; Then, insert text in the help buffer while paying attention to
  2307. ;; the width of the window in which the buffer displayed.
  2308. (with-current-buffer (help-buffer)
  2309. (setq buffer-read-only nil)
  2310. ;; Without this, a keyboard layout with R2L characters might be
  2311. ;; displayed reversed, right to left. See the thread starting at
  2312. ;; http://lists.gnu.org/archive/html/emacs-devel/2012-03/msg00062.html
  2313. ;; for a description of one such situation.
  2314. (setq bidi-paragraph-direction 'left-to-right)
  2315. (insert "Input method: " (quail-name)
  2316. " (mode line indicator:"
  2317. (quail-title)
  2318. ")\n\n")
  2319. (save-restriction
  2320. (narrow-to-region (point) (point))
  2321. (insert (quail-docstring))
  2322. (goto-char (point-min))
  2323. (with-syntax-table emacs-lisp-mode-syntax-table
  2324. (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
  2325. (let ((sym (intern-soft
  2326. (buffer-substring (+ (match-beginning 0) 2)
  2327. (1- (point))))))
  2328. (if (and (boundp sym)
  2329. (stringp (symbol-value sym)))
  2330. (replace-match (symbol-value sym) t t)))))
  2331. (goto-char (point-max)))
  2332. (or (bolp)
  2333. (insert "\n"))
  2334. (insert "\n")
  2335. (let ((done-list nil))
  2336. ;; Show keyboard layout if the current package requests it..
  2337. (when (quail-show-layout)
  2338. (insert "
  2339. KEYBOARD LAYOUT
  2340. ---------------
  2341. This input method works by translating individual input characters.
  2342. Assuming that your actual keyboard has the `")
  2343. (help-insert-xref-button
  2344. quail-keyboard-layout-type
  2345. 'quail-keyboard-layout-button
  2346. quail-keyboard-layout-type)
  2347. (insert "' layout,
  2348. translation results in the following \"virtual\" keyboard layout:
  2349. ")
  2350. (setq done-list
  2351. (quail-insert-kbd-layout quail-keyboard-layout))
  2352. (insert "If your keyboard has a different layout, rearranged from
  2353. `")
  2354. (help-insert-xref-button
  2355. "standard"
  2356. 'quail-keyboard-layout-button "standard")
  2357. (insert "', the \"virtual\" keyboard you get with this input method
  2358. will be rearranged in the same way.
  2359. You can set the variable `quail-keyboard-layout-type' to specify
  2360. the physical layout of your keyboard; the tables shown in
  2361. documentation of input methods including this one are based on the
  2362. physical keyboard layout as specified with that variable.
  2363. ")
  2364. (help-insert-xref-button
  2365. "[customize keyboard layout]"
  2366. 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
  2367. (insert "\n"))
  2368. ;; Show key sequences.
  2369. (let* ((decode-map (list 'decode-map))
  2370. (num (quail-build-decode-map (list (quail-map)) "" decode-map
  2371. ;; We used to use 512 here, but
  2372. ;; TeX has more than 1000 and
  2373. ;; it's good to see the list.
  2374. 0 5120 done-list)))
  2375. (when (> num 0)
  2376. (insert "
  2377. KEY SEQUENCE
  2378. ------------
  2379. ")
  2380. (if (quail-show-layout)
  2381. (insert "You can also input more characters")
  2382. (insert "You can input characters"))
  2383. (insert " by the following key sequences:\n")
  2384. (quail-insert-decode-map decode-map))))
  2385. (quail-help-insert-keymap-description
  2386. (quail-translation-keymap)
  2387. "\
  2388. KEY BINDINGS FOR TRANSLATION
  2389. ----------------------------\n")
  2390. (insert ?\n)
  2391. (if (quail-conversion-keymap)
  2392. (quail-help-insert-keymap-description
  2393. (quail-conversion-keymap)
  2394. "\
  2395. KEY BINDINGS FOR CONVERSION
  2396. ---------------------------\n"))
  2397. (setq quail-current-package nil)
  2398. ;; Resize the help window again, now that it has all its contents.
  2399. (save-selected-window
  2400. (select-window (get-buffer-window (current-buffer) t))
  2401. (run-hooks 'temp-buffer-show-hook)))))
  2402. (defun quail-help-insert-keymap-description (keymap &optional header)
  2403. (let ((pos1 (point))
  2404. pos2)
  2405. (if header
  2406. (insert header))
  2407. (save-excursion
  2408. (insert (substitute-command-keys "\\{keymap}")))
  2409. ;; Skip headers "key bindings", etc.
  2410. (forward-line 3)
  2411. (setq pos2 (point))
  2412. (with-syntax-table emacs-lisp-mode-syntax-table
  2413. (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
  2414. (let ((sym (intern-soft (buffer-substring (match-beginning 0)
  2415. (point)))))
  2416. (if (and sym (fboundp sym)
  2417. (or (eq (get sym 'quail-help) 'hide)
  2418. (and (quail-deterministic)
  2419. (eq (get sym 'quail-help) 'non-deterministic))))
  2420. (delete-region (line-beginning-position)
  2421. (1+ (line-end-position)))))))
  2422. (goto-char pos2)
  2423. (while (not (eobp))
  2424. (if (looking-at "[ \t]*$")
  2425. (delete-region (point) (1+ (line-end-position)))
  2426. (forward-line 1)))
  2427. (goto-char pos2)
  2428. (if (eobp)
  2429. (delete-region pos1 (point)))
  2430. (goto-char (point-max))))
  2431. (defun quail-translation-help ()
  2432. "Show help message while translating in Quail input method."
  2433. (interactive)
  2434. (if (not (eq this-command last-command))
  2435. (let (state-msg keymap)
  2436. (if (and quail-converting (= (length quail-current-key) 0))
  2437. (setq state-msg
  2438. (format "Converting string %S by input method %S.\n"
  2439. quail-conversion-str (quail-name))
  2440. keymap (quail-conversion-keymap))
  2441. (setq state-msg
  2442. (format "Translating key sequence %S by input method %S.\n"
  2443. quail-current-key (quail-name))
  2444. keymap (quail-translation-keymap)))
  2445. (with-output-to-temp-buffer "*Help*"
  2446. (with-current-buffer standard-output
  2447. (insert state-msg)
  2448. (quail-help-insert-keymap-description
  2449. keymap
  2450. "-----------------------\n")
  2451. ;; Isn't this redundant ? -stef
  2452. (help-mode)))))
  2453. (let (scroll-help)
  2454. (save-selected-window
  2455. (select-window (get-buffer-window "*Help*"))
  2456. (if (eq this-command last-command)
  2457. (if (< (window-end) (point-max))
  2458. (scroll-up)
  2459. (if (> (window-start) (point-min))
  2460. (set-window-start (selected-window) (point-min)))))
  2461. (setq scroll-help
  2462. (if (< (window-end (selected-window) 'up-to-date) (point-max))
  2463. "Type \\[quail-translation-help] to scroll up the help"
  2464. (if (> (window-start) (point-min))
  2465. "Type \\[quail-translation-help] to see the head of help"))))
  2466. (if scroll-help
  2467. (progn
  2468. (message "%s" (substitute-command-keys scroll-help))
  2469. (sit-for 1)
  2470. (message nil)
  2471. (quail-update-guidance)
  2472. ))))
  2473. ;; Add KEY (string) to the element of TABLE (char-table) for CHAR if
  2474. ;; it is not yet stored. As a result, the element is a string or a
  2475. ;; list of strings.
  2476. (defun quail-store-decode-map-key (table char key)
  2477. (let ((elt (aref table char)))
  2478. (if elt
  2479. (if (consp elt)
  2480. (or (member key elt)
  2481. (aset table char (cons key elt)))
  2482. (or (string= key elt)
  2483. (aset table char (list key elt))))
  2484. (aset table char key))
  2485. ;; Avoid "obsolete" warnings for translation-table-for-input.
  2486. (with-no-warnings
  2487. (if (and translation-table-for-input
  2488. (setq char (aref translation-table-for-input char)))
  2489. (let ((translation-table-for-input nil))
  2490. (quail-store-decode-map-key table char key))))))
  2491. ;; Helper function for quail-gen-decode-map. Store key strings to
  2492. ;; type each character under MAP in TABLE (char-table). MAP is an
  2493. ;; element of the current Quail map reached by typing keys in KEY
  2494. ;; (string).
  2495. (defun quail-gen-decode-map1 (map key table)
  2496. (when (and (consp map) (listp (cdr map)))
  2497. (let ((trans (car map)))
  2498. (cond ((integerp trans)
  2499. (quail-store-decode-map-key table trans key))
  2500. ((stringp trans)
  2501. (dotimes (i (length trans))
  2502. (quail-store-decode-map-key table (aref trans i) key)))
  2503. ((or (vectorp trans)
  2504. (and (consp trans)
  2505. (setq trans (cdr trans))))
  2506. (dotimes (i (length trans))
  2507. (let ((elt (aref trans i)))
  2508. (if (stringp elt)
  2509. (if (= (length elt) 1)
  2510. (quail-store-decode-map-key table (aref elt 0) key))
  2511. (quail-store-decode-map-key table elt key)))))))
  2512. (if (> (length key) 1)
  2513. (dolist (elt (cdr map))
  2514. (quail-gen-decode-map1 (cdr elt) key table))
  2515. (dolist (elt (cdr map))
  2516. (quail-gen-decode-map1 (cdr elt) (format "%s%c" key (car elt))
  2517. table)))))
  2518. (put 'quail-decode-map 'char-table-extra-slots 0)
  2519. ;; Generate a half-cooked decode map (char-table) for the current
  2520. ;; Quail map. An element for a character C is a key string or a list
  2521. ;; of a key strings to type to input C. The length of key string is at
  2522. ;; most 2. If it is 2, more keys may be required to input C.
  2523. (defun quail-gen-decode-map ()
  2524. (let ((table (make-char-table 'quail-decode-map nil)))
  2525. (dolist (elt (cdr (quail-map)))
  2526. (quail-gen-decode-map1 (cdr elt) (string (car elt)) table))
  2527. table))
  2528. ;; Check if CHAR equals to TARGET while also trying to translate CHAR
  2529. ;; by translation-table-for-input.
  2530. (defsubst quail-char-equal-p (char target)
  2531. (or (= char target)
  2532. ;; Avoid "obsolete" warnings for translation-table-for-input.
  2533. (with-no-warnings
  2534. (and translation-table-for-input
  2535. (setq char (aref translation-table-for-input char))
  2536. (= char target)))))
  2537. ;; Helper function for quail-find-key. Prepend key strings to type
  2538. ;; for inputting CHAR by the current input method to KEY-LIST and
  2539. ;; return the result. MAP is an element of the current Quail map
  2540. ;; reached by typing keys in KEY.
  2541. (defun quail-find-key1 (map key char key-list)
  2542. (let ((trans (car map))
  2543. (found-here nil))
  2544. (cond ((stringp trans)
  2545. (setq found-here
  2546. (and (= (length trans) 1)
  2547. (quail-char-equal-p (aref trans 0) char))))
  2548. ((or (vectorp trans) (consp trans))
  2549. (if (consp trans)
  2550. (setq trans (cdr trans)))
  2551. (setq found-here
  2552. (catch 'tag
  2553. (dotimes (i (length trans))
  2554. (let ((target (aref trans i)))
  2555. (if (integerp target)
  2556. (if (quail-char-equal-p target char)
  2557. (throw 'tag t))
  2558. (if (and (= (length target) 1)
  2559. (quail-char-equal-p (aref target 0) char))
  2560. (throw 'tag t))))))))
  2561. ((integerp trans)
  2562. (setq found-here (quail-char-equal-p trans char))))
  2563. (if found-here
  2564. (setq key-list (cons key key-list)))
  2565. (if (> (length key) 1)
  2566. (dolist (elt (cdr map))
  2567. (setq key-list
  2568. (quail-find-key1 (cdr elt) (format "%s%c" key (car elt))
  2569. char key-list))))
  2570. key-list))
  2571. ;; If non-nil, the value has the form (QUAIL-MAP . CODING-SYSTEM)
  2572. ;; where QUAIL-MAP is a quail-map of which decode map was generated
  2573. ;; while buffer-file-coding-system was CODING-SYSTEM.
  2574. (defvar quail-decode-map-generated nil)
  2575. (defun quail-find-key (char)
  2576. "Return a list of keys to type to input CHAR in the current input method.
  2577. If CHAR is an ASCII character and can be input by typing itself, return t."
  2578. (let ((decode-map (or (and (or (not quail-decode-map-generated)
  2579. (and (eq (car quail-decode-map-generated) (quail-map))
  2580. (eq (cdr quail-decode-map-generated)
  2581. (or buffer-file-coding-system t))))
  2582. (quail-decode-map))
  2583. (let ((map (quail-gen-decode-map)))
  2584. (setq quail-decode-map-generated
  2585. (cons (quail-map) (or buffer-file-coding-system t)))
  2586. (setcar (nthcdr 10 quail-current-package) map)
  2587. map)))
  2588. (key-list nil))
  2589. (if (consp decode-map)
  2590. (let ((str (string char)))
  2591. (mapc #'(lambda (elt)
  2592. (if (string= str (car elt))
  2593. (setq key-list (cons (cdr elt) key-list))))
  2594. (cdr decode-map)))
  2595. (let ((key-head (aref decode-map char)))
  2596. (if (stringp key-head)
  2597. (setq key-list (quail-find-key1
  2598. (quail-lookup-key key-head nil t)
  2599. key-head char nil))
  2600. (mapc #'(lambda (elt)
  2601. (setq key-list
  2602. (quail-find-key1
  2603. (quail-lookup-key elt nil t) elt char key-list)))
  2604. key-head))))
  2605. (or key-list
  2606. (and (< char 128)
  2607. (not (quail-lookup-key (string char) 1))))))
  2608. (defun quail-show-key ()
  2609. "Show a list of key strings to type for inputting a character at point."
  2610. (interactive)
  2611. (or current-input-method
  2612. (error "No input method is activated"))
  2613. (or (assoc current-input-method quail-package-alist)
  2614. (error "The current input method does not use Quail"))
  2615. (let* ((char (following-char))
  2616. (key-list (quail-find-key char)))
  2617. (cond ((consp key-list)
  2618. (message "To input `%c', type \"%s\""
  2619. char
  2620. (mapconcat 'identity key-list "\", \"")))
  2621. ((eq key-list t)
  2622. (message "To input `%s', just type it"
  2623. (single-key-description char)))
  2624. (t
  2625. (message "%c can't be input by the current input method" char)))))
  2626. ;; Quail map generator from state transition table.
  2627. (defun quail-map-from-table (table)
  2628. "Make quail map from state transition table TABLE.
  2629. TABLE is an alist, the form is:
  2630. ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
  2631. STATE-n are symbols to denote state. STATE-0 is the initial state.
  2632. TRANSITION-n-m are transition rules from STATE-n, and have the form
  2633. \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
  2634. RULES is a symbol whose value is an alist of keys \(string) vs the
  2635. corresponding characters or strings. The format of the symbol value of
  2636. RULES is the same as arguments to `quail-define-rules'.
  2637. If TRANSITION-n-m has the form (RULES . STATE-x), it means that
  2638. STATE-n transits to STATE-x when keys in RULES are input. Recursive
  2639. transition is allowed, i.e. STATE-x may be STATE-n.
  2640. If TRANSITION-n-m has the form RULES, the transition terminates
  2641. when keys in RULES are input.
  2642. The generated map can be set for the current Quail package by the
  2643. function `quail-install-map' (which see)."
  2644. (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
  2645. tail elt)
  2646. ;; STATE-ALIST is an alist of states vs the corresponding sub Quail
  2647. ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
  2648. ;; Set key sequence mapping rules in cdr part of each element.
  2649. (while table
  2650. (quail-map-from-table-1 state-alist (car table))
  2651. (setq table (cdr table)))
  2652. ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
  2653. ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
  2654. ;; them with MAPPING-RULES of STATE-x to make elements of
  2655. ;; STATE-ALIST valid Quail maps.
  2656. (setq tail state-alist)
  2657. (while tail
  2658. (setq elt (car tail) tail (cdr tail))
  2659. (quail-map-from-table-2 state-alist elt))
  2660. ;; Return the Quail map for the initial state.
  2661. (car state-alist)))
  2662. ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
  2663. ;; mapping rules in the element of STATE-ALIST that corresponds to
  2664. ;; STATE according to TRANSITION ...
  2665. (defun quail-map-from-table-1 (state-alist state-info)
  2666. (let* ((state (car state-info))
  2667. (map (assq state state-alist))
  2668. (transitions (cdr state-info))
  2669. elt)
  2670. (while transitions
  2671. (setq elt (car transitions) transitions (cdr transitions))
  2672. (let (rules dst-state key trans)
  2673. ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
  2674. ;; STATE-x is one of car parts of STATE-ALIST's elements.
  2675. (if (consp elt)
  2676. (setq rules (symbol-value (car elt))
  2677. ;; Set (STATE-x) as branches for all keys in RULES.
  2678. ;; It is replaced with actual branches for STATE-x
  2679. ;; later in `quail-map-from-table-2'.
  2680. dst-state (list (cdr elt)))
  2681. (setq rules (symbol-value elt)))
  2682. (while rules
  2683. (setq key (car (car rules)) trans (cdr (car rules))
  2684. rules (cdr rules))
  2685. (if (stringp trans)
  2686. (if (= (length trans) 1)
  2687. (setq trans (aref trans 0))
  2688. (setq trans (string-to-vector trans))))
  2689. (set-nested-alist key trans map nil dst-state))))))
  2690. ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
  2691. ;; the form is:
  2692. ;; (STATE (CHAR NESTED-ALIST) ...)
  2693. ;; NESTED-ALIST is a nested alist; the form is:
  2694. ;; (TRANS (CHAR NESTED-ALIST) ...)
  2695. ;; or
  2696. ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
  2697. ;; Here, the task is to replace all occurrences of (STATE-x) with:
  2698. ;; (cdr (assq STATE-x STATE-ALIST))
  2699. (defun quail-map-from-table-2 (state-alist element)
  2700. (let ((prev element)
  2701. (tail (cdr element))
  2702. elt)
  2703. (while (cdr tail)
  2704. (setq elt (car tail) prev tail tail (cdr tail))
  2705. (quail-map-from-table-2 state-alist (cdr elt)))
  2706. (setq elt (car tail))
  2707. (if (consp elt)
  2708. (quail-map-from-table-2 state-alist (cdr elt))
  2709. (setcdr prev (cdr (assq elt state-alist))))))
  2710. ;; Concatenate translations for all heading substrings of KEY in the
  2711. ;; current Quail map. Here, `heading substring' means (substring KEY
  2712. ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
  2713. (defun quail-lookup-map-and-concat (key)
  2714. (let* ((len (length key))
  2715. (translation-list nil)
  2716. map)
  2717. (while (> len 0)
  2718. (setq map (quail-lookup-key key len t)
  2719. len (1- len))
  2720. (if map
  2721. (let* ((def (quail-map-definition map))
  2722. (trans (if (consp def) (aref (cdr def) (car (car def)))
  2723. def)))
  2724. (if (integerp trans)
  2725. (setq trans (char-to-string trans)))
  2726. (setq translation-list (cons trans translation-list)))))
  2727. (apply 'concat translation-list)))
  2728. (defvar quail-directory-name "quail"
  2729. "Name of Quail directory which contains Quail packages.
  2730. This is a sub-directory of LEIM directory.")
  2731. ;;;###autoload
  2732. (defun quail-update-leim-list-file (dirname &rest dirnames)
  2733. "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
  2734. DIRNAME is a directory containing Emacs input methods;
  2735. normally, it should specify the `leim' subdirectory
  2736. of the Emacs source tree.
  2737. It searches for Quail packages under `quail' subdirectory of DIRNAME,
  2738. and update the file \"leim-list.el\" in DIRNAME.
  2739. When called from a program, the remaining arguments are additional
  2740. directory names to search for Quail packages under `quail' subdirectory
  2741. of each directory."
  2742. (interactive "FDirectory of LEIM: ")
  2743. (setq dirname (expand-file-name dirname))
  2744. (let ((leim-list (expand-file-name leim-list-file-name dirname))
  2745. quail-dirs list-buf pkg-list pos)
  2746. (if (not (file-writable-p leim-list))
  2747. (error "Can't write to file \"%s\"" leim-list))
  2748. (message "Updating %s ..." leim-list)
  2749. (setq list-buf (find-file-noselect leim-list))
  2750. ;; At first, clean up the file.
  2751. (with-current-buffer list-buf
  2752. (goto-char 1)
  2753. ;; Insert the correct header.
  2754. (if (looking-at (regexp-quote leim-list-header))
  2755. (goto-char (match-end 0))
  2756. (insert leim-list-header))
  2757. (setq pos (point))
  2758. (if (not (re-search-forward leim-list-entry-regexp nil t))
  2759. nil
  2760. ;; Remove garbage after the header.
  2761. (goto-char (match-beginning 0))
  2762. (if (< pos (point))
  2763. (delete-region pos (point)))
  2764. ;; Remove all entries for Quail.
  2765. (while (re-search-forward leim-list-entry-regexp nil 'move)
  2766. (goto-char (match-beginning 0))
  2767. (setq pos (point))
  2768. (condition-case nil
  2769. (let ((form (read list-buf)))
  2770. (when (equal (nth 3 form) ''quail-use-package)
  2771. (if (eolp) (forward-line 1))
  2772. (delete-region pos (point))))
  2773. (error
  2774. ;; Delete the remaining contents because it seems that
  2775. ;; this file is broken.
  2776. (message "Garbage in %s deleted" leim-list)
  2777. (delete-region pos (point-max)))))))
  2778. ;; Search for `quail' subdirectory under each DIRNAMES.
  2779. (setq dirnames (cons dirname dirnames))
  2780. (let ((l dirnames))
  2781. (while l
  2782. (setcar l (expand-file-name (car l)))
  2783. (setq dirname (expand-file-name quail-directory-name (car l)))
  2784. (if (file-readable-p dirname)
  2785. (setq quail-dirs (cons dirname quail-dirs))
  2786. (message "%s doesn't have `%s' subdirectory, just ignored"
  2787. (car l) quail-directory-name)
  2788. (setq quail-dirs (cons nil quail-dirs)))
  2789. (setq l (cdr l)))
  2790. (setq quail-dirs (nreverse quail-dirs)))
  2791. ;; Insert input method registering forms.
  2792. (while quail-dirs
  2793. (setq dirname (car quail-dirs))
  2794. (when dirname
  2795. (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
  2796. (while pkg-list
  2797. (message "Checking %s ..." (car pkg-list))
  2798. (with-temp-buffer
  2799. (insert-file-contents (car pkg-list))
  2800. (goto-char (point-min))
  2801. ;; Don't get fooled by commented-out code.
  2802. (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
  2803. (goto-char (match-beginning 0))
  2804. (condition-case nil
  2805. (let ((form (read (current-buffer))))
  2806. (with-current-buffer list-buf
  2807. (insert
  2808. (format "(register-input-method
  2809. %S %S '%s
  2810. %S %S
  2811. %S)\n"
  2812. (nth 1 form) ; PACKAGE-NAME
  2813. (nth 2 form) ; LANGUAGE
  2814. 'quail-use-package ; ACTIVATE-FUNC
  2815. (nth 3 form) ; PACKAGE-TITLE
  2816. (progn ; PACKAGE-DESCRIPTION (one line)
  2817. (string-match ".*" (nth 5 form))
  2818. (match-string 0 (nth 5 form)))
  2819. (file-relative-name ; PACKAGE-FILENAME
  2820. (file-name-sans-extension (car pkg-list))
  2821. (car dirnames))))))
  2822. (error
  2823. ;; Ignore the remaining contents of this file.
  2824. (goto-char (point-max))
  2825. (message "Some part of \"%s\" is broken" (car pkg-list))))))
  2826. (setq pkg-list (cdr pkg-list)))
  2827. (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
  2828. ;; At last, write out LEIM list file.
  2829. (with-current-buffer list-buf
  2830. (let ((coding-system-for-write 'utf-8))
  2831. (save-buffer 0)))
  2832. (kill-buffer list-buf)
  2833. (message "Updating %s ... done" leim-list)))
  2834. (defun quail-advice (args)
  2835. "Advise users about the characters input by the current Quail package.
  2836. The argument is a parameterized event of the form:
  2837. (quail-advice STRING)
  2838. where STRING is a string containing the input characters.
  2839. If STRING has property `advice' and the value is a function,
  2840. call it with one argument STRING."
  2841. (interactive "e")
  2842. (let* ((string (nth 1 args))
  2843. (func (get-text-property 0 'advice string)))
  2844. (if (functionp func)
  2845. (funcall func string))))
  2846. (global-set-key [quail-advice] 'quail-advice)
  2847. ;;
  2848. (provide 'quail)
  2849. ;;; quail.el ends here