xterm.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. ;;; xterm.el --- define function key sequences and standard colors for xterm
  2. ;; Copyright (C) 1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: FSF
  4. ;; Keywords: terminals
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (defgroup xterm nil
  19. "XTerm support."
  20. :version "24.1"
  21. :group 'emacs)
  22. (defcustom xterm-extra-capabilities 'check
  23. "Whether Xterm supports some additional, more modern, features.
  24. If nil, just assume that it does not.
  25. If `check', try to check if it does.
  26. If a list, assume that the listed features are supported, without checking.
  27. The relevant features are:
  28. modifyOtherKeys -- if supported, more key bindings work (e.g, \"\\C-,\")
  29. reportBackground -- if supported, Xterm reports its background color
  30. "
  31. :version "24.1"
  32. :group 'xterm
  33. :type '(choice (const :tag "No" nil)
  34. (const :tag "Check" check)
  35. ;; NOTE: If you add entries here, make sure to update
  36. ;; `tocheck-capabilities' in `terminal-init-xterm' as well.
  37. (set (const :tag "modifyOtherKeys support" modifyOtherKeys)
  38. (const :tag "report background" reportBackground))))
  39. (defvar xterm-function-map
  40. (let ((map (make-sparse-keymap)))
  41. ;; xterm from X.org 6.8.2 uses these key definitions.
  42. (define-key map "\eOP" [f1])
  43. (define-key map "\eOQ" [f2])
  44. (define-key map "\eOR" [f3])
  45. (define-key map "\eOS" [f4])
  46. (define-key map "\e[15~" [f5])
  47. (define-key map "\e[17~" [f6])
  48. (define-key map "\e[18~" [f7])
  49. (define-key map "\e[19~" [f8])
  50. (define-key map "\e[20~" [f9])
  51. (define-key map "\e[21~" [f10])
  52. (define-key map "\e[23~" [f11])
  53. (define-key map "\e[24~" [f12])
  54. (define-key map "\eO2P" [S-f1])
  55. (define-key map "\eO2Q" [S-f2])
  56. (define-key map "\eO2R" [S-f3])
  57. (define-key map "\eO2S" [S-f4])
  58. (define-key map "\e[1;2P" [S-f1])
  59. (define-key map "\e[1;2Q" [S-f2])
  60. (define-key map "\e[1;2R" [S-f3])
  61. (define-key map "\e[1;2S" [S-f4])
  62. (define-key map "\e[15;2~" [S-f5])
  63. (define-key map "\e[17;2~" [S-f6])
  64. (define-key map "\e[18;2~" [S-f7])
  65. (define-key map "\e[19;2~" [S-f8])
  66. (define-key map "\e[20;2~" [S-f9])
  67. (define-key map "\e[21;2~" [S-f10])
  68. (define-key map "\e[23;2~" [S-f11])
  69. (define-key map "\e[24;2~" [S-f12])
  70. (define-key map "\eO5P" [C-f1])
  71. (define-key map "\eO5Q" [C-f2])
  72. (define-key map "\eO5R" [C-f3])
  73. (define-key map "\eO5S" [C-f4])
  74. (define-key map "\e[15;5~" [C-f5])
  75. (define-key map "\e[17;5~" [C-f6])
  76. (define-key map "\e[18;5~" [C-f7])
  77. (define-key map "\e[19;5~" [C-f8])
  78. (define-key map "\e[20;5~" [C-f9])
  79. (define-key map "\e[21;5~" [C-f10])
  80. (define-key map "\e[23;5~" [C-f11])
  81. (define-key map "\e[24;5~" [C-f12])
  82. (define-key map "\eO6P" [C-S-f1])
  83. (define-key map "\eO6Q" [C-S-f2])
  84. (define-key map "\eO6R" [C-S-f3])
  85. (define-key map "\eO6S" [C-S-f4])
  86. (define-key map "\e[15;6~" [C-S-f5])
  87. (define-key map "\e[17;6~" [C-S-f6])
  88. (define-key map "\e[18;6~" [C-S-f7])
  89. (define-key map "\e[19;6~" [C-S-f8])
  90. (define-key map "\e[20;6~" [C-S-f9])
  91. (define-key map "\e[21;6~" [C-S-f10])
  92. (define-key map "\e[23;6~" [C-S-f11])
  93. (define-key map "\e[24;6~" [C-S-f12])
  94. (define-key map "\eO3P" [M-f1])
  95. (define-key map "\eO3Q" [M-f2])
  96. (define-key map "\eO3R" [M-f3])
  97. (define-key map "\eO3S" [M-f4])
  98. (define-key map "\e[15;3~" [M-f5])
  99. (define-key map "\e[17;3~" [M-f6])
  100. (define-key map "\e[18;3~" [M-f7])
  101. (define-key map "\e[19;3~" [M-f8])
  102. (define-key map "\e[20;3~" [M-f9])
  103. (define-key map "\e[21;3~" [M-f10])
  104. (define-key map "\e[23;3~" [M-f11])
  105. (define-key map "\e[24;3~" [M-f12])
  106. (define-key map "\eO4P" [M-S-f1])
  107. (define-key map "\eO4Q" [M-S-f2])
  108. (define-key map "\eO4R" [M-S-f3])
  109. (define-key map "\eO4S" [M-S-f4])
  110. (define-key map "\e[15;4~" [M-S-f5])
  111. (define-key map "\e[17;4~" [M-S-f6])
  112. (define-key map "\e[18;4~" [M-S-f7])
  113. (define-key map "\e[19;4~" [M-S-f8])
  114. (define-key map "\e[20;4~" [M-S-f9])
  115. (define-key map "\e[21;4~" [M-S-f10])
  116. (define-key map "\e[23;4~" [M-S-f11])
  117. (define-key map "\e[24;4~" [M-S-f12])
  118. (define-key map "\eOA" [up])
  119. (define-key map "\eOB" [down])
  120. (define-key map "\eOC" [right])
  121. (define-key map "\eOD" [left])
  122. (define-key map "\eOF" [end])
  123. (define-key map "\eOH" [home])
  124. (define-key map "\e[1;2A" [S-up])
  125. (define-key map "\e[1;2B" [S-down])
  126. (define-key map "\e[1;2C" [S-right])
  127. (define-key map "\e[1;2D" [S-left])
  128. (define-key map "\e[1;2F" [S-end])
  129. (define-key map "\e[1;2H" [S-home])
  130. (define-key map "\e[1;4A" [M-S-up])
  131. (define-key map "\e[1;4B" [M-S-down])
  132. (define-key map "\e[1;4C" [M-S-right])
  133. (define-key map "\e[1;4D" [M-S-left])
  134. (define-key map "\e[1;4F" [M-S-end])
  135. (define-key map "\e[1;4H" [M-S-home])
  136. (define-key map "\e[1;5A" [C-up])
  137. (define-key map "\e[1;5B" [C-down])
  138. (define-key map "\e[1;5C" [C-right])
  139. (define-key map "\e[1;5D" [C-left])
  140. (define-key map "\e[1;5F" [C-end])
  141. (define-key map "\e[1;5H" [C-home])
  142. (define-key map "\e[1;6A" [C-S-up])
  143. (define-key map "\e[1;6B" [C-S-down])
  144. (define-key map "\e[1;6C" [C-S-right])
  145. (define-key map "\e[1;6D" [C-S-left])
  146. (define-key map "\e[1;6F" [C-S-end])
  147. (define-key map "\e[1;6H" [C-S-home])
  148. (define-key map "\e[1;7A" [C-M-up])
  149. (define-key map "\e[1;7B" [C-M-down])
  150. (define-key map "\e[1;7C" [C-M-right])
  151. (define-key map "\e[1;7D" [C-M-left])
  152. (define-key map "\e[1;7F" [C-M-end])
  153. (define-key map "\e[1;7H" [C-M-home])
  154. (define-key map "\e[1;8A" [C-M-S-up])
  155. (define-key map "\e[1;8B" [C-M-S-down])
  156. (define-key map "\e[1;8C" [C-M-S-right])
  157. (define-key map "\e[1;8D" [C-M-S-left])
  158. (define-key map "\e[1;8F" [C-M-S-end])
  159. (define-key map "\e[1;8H" [C-M-S-home])
  160. (define-key map "\e[1;3A" [M-up])
  161. (define-key map "\e[1;3B" [M-down])
  162. (define-key map "\e[1;3C" [M-right])
  163. (define-key map "\e[1;3D" [M-left])
  164. (define-key map "\e[1;3F" [M-end])
  165. (define-key map "\e[1;3H" [M-home])
  166. (define-key map "\e[2~" [insert])
  167. (define-key map "\e[3~" [delete])
  168. (define-key map "\e[5~" [prior])
  169. (define-key map "\e[6~" [next])
  170. (define-key map "\e[2;2~" [S-insert])
  171. (define-key map "\e[3;2~" [S-delete])
  172. (define-key map "\e[5;2~" [S-prior])
  173. (define-key map "\e[6;2~" [S-next])
  174. (define-key map "\e[2;4~" [M-S-insert])
  175. (define-key map "\e[3;4~" [M-S-delete])
  176. (define-key map "\e[5;4~" [M-S-prior])
  177. (define-key map "\e[6;4~" [M-S-next])
  178. (define-key map "\e[2;5~" [C-insert])
  179. (define-key map "\e[3;5~" [C-delete])
  180. (define-key map "\e[5;5~" [C-prior])
  181. (define-key map "\e[6;5~" [C-next])
  182. (define-key map "\e[2;6~" [C-S-insert])
  183. (define-key map "\e[3;6~" [C-S-delete])
  184. (define-key map "\e[5;6~" [C-S-prior])
  185. (define-key map "\e[6;6~" [C-S-next])
  186. (define-key map "\e[2;7~" [C-M-insert])
  187. (define-key map "\e[3;7~" [C-M-delete])
  188. (define-key map "\e[5;7~" [C-M-prior])
  189. (define-key map "\e[6;7~" [C-M-next])
  190. (define-key map "\e[2;8~" [C-M-S-insert])
  191. (define-key map "\e[3;8~" [C-M-S-delete])
  192. (define-key map "\e[5;8~" [C-M-S-prior])
  193. (define-key map "\e[6;8~" [C-M-S-next])
  194. (define-key map "\e[2;3~" [M-insert])
  195. (define-key map "\e[3;3~" [M-delete])
  196. (define-key map "\e[5;3~" [M-prior])
  197. (define-key map "\e[6;3~" [M-next])
  198. (define-key map "\e[4~" [select])
  199. (define-key map "\e[29~" [print])
  200. (define-key map "\eOj" [kp-multiply])
  201. (define-key map "\eOk" [kp-add])
  202. (define-key map "\eOl" [kp-separator])
  203. (define-key map "\eOm" [kp-subtract])
  204. (define-key map "\eOo" [kp-divide])
  205. (define-key map "\eOp" [kp-0])
  206. (define-key map "\eOq" [kp-1])
  207. (define-key map "\eOr" [kp-2])
  208. (define-key map "\eOs" [kp-3])
  209. (define-key map "\eOt" [kp-4])
  210. (define-key map "\eOu" [kp-5])
  211. (define-key map "\eOv" [kp-6])
  212. (define-key map "\eOw" [kp-7])
  213. (define-key map "\eOx" [kp-8])
  214. (define-key map "\eOy" [kp-9])
  215. ;; These keys are available in xterm starting from version 216
  216. ;; if the modifyOtherKeys resource is set to 1.
  217. (define-key map "\e[27;5;9~" [C-tab])
  218. (define-key map "\e[27;5;13~" [C-return])
  219. (define-key map "\e[27;5;39~" [?\C-\'])
  220. (define-key map "\e[27;5;44~" [?\C-,])
  221. (define-key map "\e[27;5;45~" [?\C--])
  222. (define-key map "\e[27;5;46~" [?\C-.])
  223. (define-key map "\e[27;5;47~" [?\C-/])
  224. (define-key map "\e[27;5;48~" [?\C-0])
  225. (define-key map "\e[27;5;49~" [?\C-1])
  226. ;; Not all C-DIGIT keys have a distinct binding.
  227. (define-key map "\e[27;5;57~" [?\C-9])
  228. (define-key map "\e[27;5;59~" [?\C-\;])
  229. (define-key map "\e[27;5;61~" [?\C-=])
  230. (define-key map "\e[27;5;92~" [?\C-\\])
  231. (define-key map "\e[27;6;33~" [?\C-!])
  232. (define-key map "\e[27;6;34~" [?\C-\"])
  233. (define-key map "\e[27;6;35~" [?\C-#])
  234. (define-key map "\e[27;6;36~" [?\C-$])
  235. (define-key map "\e[27;6;37~" [?\C-%])
  236. (define-key map "\e[27;6;38~" [?\C-&])
  237. (define-key map "\e[27;6;40~" [?\C-(])
  238. (define-key map "\e[27;6;41~" [?\C-)])
  239. (define-key map "\e[27;6;42~" [?\C-*])
  240. (define-key map "\e[27;6;43~" [?\C-+])
  241. (define-key map "\e[27;6;58~" [?\C-:])
  242. (define-key map "\e[27;6;60~" [?\C-<])
  243. (define-key map "\e[27;6;62~" [?\C->])
  244. (define-key map "\e[27;6;63~" [(control ??)])
  245. ;; These are the strings emitted for various C-M- combinations
  246. ;; for keyboards that the Meta and Alt modifiers are on the same
  247. ;; key (usually labeled "Alt").
  248. (define-key map "\e[27;13;9~" [C-M-tab])
  249. (define-key map "\e[27;13;13~" [C-M-return])
  250. (define-key map "\e[27;13;39~" [?\C-\M-\'])
  251. (define-key map "\e[27;13;44~" [?\C-\M-,])
  252. (define-key map "\e[27;13;45~" [?\C-\M--])
  253. (define-key map "\e[27;13;46~" [?\C-\M-.])
  254. (define-key map "\e[27;13;47~" [?\C-\M-/])
  255. (define-key map "\e[27;13;48~" [?\C-\M-0])
  256. (define-key map "\e[27;13;49~" [?\C-\M-1])
  257. (define-key map "\e[27;13;50~" [?\C-\M-2])
  258. (define-key map "\e[27;13;51~" [?\C-\M-3])
  259. (define-key map "\e[27;13;52~" [?\C-\M-4])
  260. (define-key map "\e[27;13;53~" [?\C-\M-5])
  261. (define-key map "\e[27;13;54~" [?\C-\M-6])
  262. (define-key map "\e[27;13;55~" [?\C-\M-7])
  263. (define-key map "\e[27;13;56~" [?\C-\M-8])
  264. (define-key map "\e[27;13;57~" [?\C-\M-9])
  265. (define-key map "\e[27;13;59~" [?\C-\M-\;])
  266. (define-key map "\e[27;13;61~" [?\C-\M-=])
  267. (define-key map "\e[27;13;92~" [?\C-\M-\\])
  268. (define-key map "\e[27;14;33~" [?\C-\M-!])
  269. (define-key map "\e[27;14;34~" [?\C-\M-\"])
  270. (define-key map "\e[27;14;35~" [?\C-\M-#])
  271. (define-key map "\e[27;14;36~" [?\C-\M-$])
  272. (define-key map "\e[27;14;37~" [?\C-\M-%])
  273. (define-key map "\e[27;14;38~" [?\C-\M-&])
  274. (define-key map "\e[27;14;40~" [?\C-\M-\(])
  275. (define-key map "\e[27;14;41~" [?\C-\M-\)])
  276. (define-key map "\e[27;14;42~" [?\C-\M-*])
  277. (define-key map "\e[27;14;43~" [?\C-\M-+])
  278. (define-key map "\e[27;14;58~" [?\C-\M-:])
  279. (define-key map "\e[27;14;60~" [?\C-\M-<])
  280. (define-key map "\e[27;14;62~" [?\C-\M->])
  281. (define-key map "\e[27;14;63~" [(control meta ??)])
  282. (define-key map "\e[27;7;9~" [C-M-tab])
  283. (define-key map "\e[27;7;13~" [C-M-return])
  284. (define-key map "\e[27;7;32~" [?\C-\M-\s])
  285. (define-key map "\e[27;7;39~" [?\C-\M-\'])
  286. (define-key map "\e[27;7;44~" [?\C-\M-,])
  287. (define-key map "\e[27;7;45~" [?\C-\M--])
  288. (define-key map "\e[27;7;46~" [?\C-\M-.])
  289. (define-key map "\e[27;7;47~" [?\C-\M-/])
  290. (define-key map "\e[27;7;48~" [?\C-\M-0])
  291. (define-key map "\e[27;7;49~" [?\C-\M-1])
  292. (define-key map "\e[27;7;50~" [?\C-\M-2])
  293. (define-key map "\e[27;7;51~" [?\C-\M-3])
  294. (define-key map "\e[27;7;52~" [?\C-\M-4])
  295. (define-key map "\e[27;7;53~" [?\C-\M-5])
  296. (define-key map "\e[27;7;54~" [?\C-\M-6])
  297. (define-key map "\e[27;7;55~" [?\C-\M-7])
  298. (define-key map "\e[27;7;56~" [?\C-\M-8])
  299. (define-key map "\e[27;7;57~" [?\C-\M-9])
  300. (define-key map "\e[27;7;59~" [?\C-\M-\;])
  301. (define-key map "\e[27;7;61~" [?\C-\M-=])
  302. (define-key map "\e[27;7;92~" [?\C-\M-\\])
  303. (define-key map "\e[27;8;33~" [?\C-\M-!])
  304. (define-key map "\e[27;8;34~" [?\C-\M-\"])
  305. (define-key map "\e[27;8;35~" [?\C-\M-#])
  306. (define-key map "\e[27;8;36~" [?\C-\M-$])
  307. (define-key map "\e[27;8;37~" [?\C-\M-%])
  308. (define-key map "\e[27;8;38~" [?\C-\M-&])
  309. (define-key map "\e[27;8;40~" [?\C-\M-\(])
  310. (define-key map "\e[27;8;41~" [?\C-\M-\)])
  311. (define-key map "\e[27;8;42~" [?\C-\M-*])
  312. (define-key map "\e[27;8;43~" [?\C-\M-+])
  313. (define-key map "\e[27;8;58~" [?\C-\M-:])
  314. (define-key map "\e[27;8;60~" [?\C-\M-<])
  315. (define-key map "\e[27;8;62~" [?\C-\M->])
  316. (define-key map "\e[27;8;63~" [(control meta ??)])
  317. (define-key map "\e[27;2;9~" [S-tab])
  318. (define-key map "\e[27;2;13~" [S-return])
  319. (define-key map "\e[27;6;9~" [C-S-tab])
  320. (define-key map "\e[27;6;13~" [C-S-return])
  321. ;; Other versions of xterm might emit these.
  322. (define-key map "\e[A" [up])
  323. (define-key map "\e[B" [down])
  324. (define-key map "\e[C" [right])
  325. (define-key map "\e[D" [left])
  326. (define-key map "\e[1~" [home])
  327. (define-key map "\eO2A" [S-up])
  328. (define-key map "\eO2B" [S-down])
  329. (define-key map "\eO2C" [S-right])
  330. (define-key map "\eO2D" [S-left])
  331. (define-key map "\eO2F" [S-end])
  332. (define-key map "\eO2H" [S-home])
  333. (define-key map "\eO5A" [C-up])
  334. (define-key map "\eO5B" [C-down])
  335. (define-key map "\eO5C" [C-right])
  336. (define-key map "\eO5D" [C-left])
  337. (define-key map "\eO5F" [C-end])
  338. (define-key map "\eO5H" [C-home])
  339. (define-key map "\e[11~" [f1])
  340. (define-key map "\e[12~" [f2])
  341. (define-key map "\e[13~" [f3])
  342. (define-key map "\e[14~" [f4])
  343. map)
  344. "Function key map overrides for xterm.")
  345. (defvar xterm-alternatives-map
  346. (let ((map (make-sparse-keymap)))
  347. ;; The terminal initialization C code file might have initialized
  348. ;; function keys F13->F60 from the termcap/terminfo information.
  349. ;; On a PC-style keyboard these keys correspond to
  350. ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
  351. ;; here substitutes the corresponding definitions in function-key-map.
  352. ;; The mapping from escape sequences to Fn is done in input-decode-map
  353. ;; whereas this here mapping is done in local-function-key-map so that
  354. ;; bindings to f45 still work, in case your keyboard really has an f45
  355. ;; key rather than C-S-f9.
  356. (define-key map [f13] [S-f1])
  357. (define-key map [f14] [S-f2])
  358. (define-key map [f15] [S-f3])
  359. (define-key map [f16] [S-f4])
  360. (define-key map [f17] [S-f5])
  361. (define-key map [f18] [S-f6])
  362. (define-key map [f19] [S-f7])
  363. (define-key map [f20] [S-f8])
  364. (define-key map [f21] [S-f9])
  365. (define-key map [f22] [S-f10])
  366. (define-key map [f23] [S-f11])
  367. (define-key map [f24] [S-f12])
  368. (define-key map [f25] [C-f1])
  369. (define-key map [f26] [C-f2])
  370. (define-key map [f27] [C-f3])
  371. (define-key map [f28] [C-f4])
  372. (define-key map [f29] [C-f5])
  373. (define-key map [f30] [C-f6])
  374. (define-key map [f31] [C-f7])
  375. (define-key map [f32] [C-f8])
  376. (define-key map [f33] [C-f9])
  377. (define-key map [f34] [C-f10])
  378. (define-key map [f35] [C-f11])
  379. (define-key map [f36] [C-f12])
  380. (define-key map [f37] [C-S-f1])
  381. (define-key map [f38] [C-S-f2])
  382. (define-key map [f39] [C-S-f3])
  383. (define-key map [f40] [C-S-f4])
  384. (define-key map [f41] [C-S-f5])
  385. (define-key map [f42] [C-S-f6])
  386. (define-key map [f43] [C-S-f7])
  387. (define-key map [f44] [C-S-f8])
  388. (define-key map [f45] [C-S-f9])
  389. (define-key map [f46] [C-S-f10])
  390. (define-key map [f47] [C-S-f11])
  391. (define-key map [f48] [C-S-f12])
  392. (define-key map [f49] [M-f1])
  393. (define-key map [f50] [M-f2])
  394. (define-key map [f51] [M-f3])
  395. (define-key map [f52] [M-f4])
  396. (define-key map [f53] [M-f5])
  397. (define-key map [f54] [M-f6])
  398. (define-key map [f55] [M-f7])
  399. (define-key map [f56] [M-f8])
  400. (define-key map [f57] [M-f9])
  401. (define-key map [f58] [M-f10])
  402. (define-key map [f59] [M-f11])
  403. (define-key map [f60] [M-f12])
  404. map)
  405. "Keymap of possible alternative meanings for some keys.")
  406. ;; List of terminals for which modify-other-keys has been turned on.
  407. (defvar xterm-modify-other-keys-terminal-list nil)
  408. (defun terminal-init-xterm ()
  409. "Terminal initialization function for xterm."
  410. ;; rxvt terminals sometimes set the TERM variable to "xterm", but
  411. ;; rxvt's keybindings are incompatible with xterm's. It is
  412. ;; better in that case to use rxvt's initialization function.
  413. (if (and (getenv "COLORTERM" (selected-frame))
  414. (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
  415. (tty-run-terminal-initialization (selected-frame) "rxvt")
  416. (let ((map (copy-keymap xterm-alternatives-map)))
  417. (set-keymap-parent map (keymap-parent local-function-key-map))
  418. (set-keymap-parent local-function-key-map map))
  419. (let ((map (copy-keymap xterm-function-map)))
  420. ;; Use inheritance to let the main keymap override those defaults.
  421. ;; This way we don't override terminfo-derived settings or settings
  422. ;; made in the .emacs file.
  423. (set-keymap-parent map (keymap-parent input-decode-map))
  424. (set-keymap-parent input-decode-map map)))
  425. (xterm-register-default-colors)
  426. (tty-set-up-initial-frame-faces)
  427. ;; Try to turn on the modifyOtherKeys feature on modern xterms.
  428. ;; When it is turned on many more key bindings work: things like
  429. ;; C-. C-, etc.
  430. ;; To do that we need to find out if the current terminal supports
  431. ;; modifyOtherKeys. At this time only xterm does.
  432. (when xterm-extra-capabilities
  433. (let ((coding-system-for-read 'binary)
  434. (chr nil)
  435. (str "")
  436. (recompute-faces nil)
  437. ;; If `xterm-extra-capabilities' is 'check, we don't know
  438. ;; the capabilities. We need to check for those defined
  439. ;; as `xterm-extra-capabilities' set options. Otherwise,
  440. ;; we don't need to check for any capabilities because
  441. ;; they are given by setting `xterm-extra-capabilities' to
  442. ;; a list (which could be empty).
  443. (tocheck-capabilities (if (eq 'check xterm-extra-capabilities)
  444. '(modifyOtherKeys reportBackground)))
  445. ;; The given capabilities are either the contents of
  446. ;; `xterm-extra-capabilities', if it's a list, or an empty list.
  447. (given-capabilities (if (consp xterm-extra-capabilities)
  448. xterm-extra-capabilities))
  449. version)
  450. ;; 1. Set `version'
  451. ;; Pending input can be mistakenly returned by the calls to
  452. ;; read-event below. Discard it.
  453. (discard-input)
  454. ;; Try to find out the type of terminal by sending a "Secondary
  455. ;; Device Attributes (DA)" query.
  456. (send-string-to-terminal "\e[>0c")
  457. ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
  458. ;; If the timeout is completely removed for read-event, this
  459. ;; might hang for terminals that pretend to be xterm, but don't
  460. ;; respond to this escape sequence. RMS' opinion was to remove
  461. ;; it completely. That might be right, but let's first try to
  462. ;; see if by using a longer timeout we get rid of most issues.
  463. (when (and (equal (read-event nil nil 2) ?\e)
  464. (equal (read-event nil nil 2) ?\[))
  465. (while (not (equal (setq chr (read-event nil nil 2)) ?c))
  466. (setq str (concat str (string chr))))
  467. (if (string-match ">0;\\([0-9]+\\);0" str)
  468. (setq version (string-to-number (match-string 1 str)))))
  469. ;; 2. If reportBackground is known to be supported, or the
  470. ;; version is 242 or higher, assume the xterm supports
  471. ;; reporting the background color (TODO: maybe earlier
  472. ;; versions do too...)
  473. (when (or (memq 'reportBackground given-capabilities)
  474. (and (memq 'reportBackground tocheck-capabilities)
  475. version
  476. (>= version 242)))
  477. (send-string-to-terminal "\e]11;?\e\\")
  478. (when (and (equal (read-event nil nil 2) ?\e)
  479. (equal (read-event nil nil 2) ?\]))
  480. (setq str "")
  481. (while (not (equal (setq chr (read-event nil nil 2)) ?\\))
  482. (setq str (concat str (string chr))))
  483. (if (string-match
  484. "11;rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
  485. (setq recompute-faces
  486. (xterm-maybe-set-dark-background-mode
  487. (string-to-number (match-string 1 str) 16)
  488. (string-to-number (match-string 2 str) 16)
  489. (string-to-number (match-string 3 str) 16))))))
  490. ;; 3. If modifyOtherKeys is known to be supported or the
  491. ;; version is 216 (the version when modifyOtherKeys was
  492. ;; introduced) or higher, initialize the modifyOtherKeys support.
  493. (if (or (memq 'modifyOtherKeys given-capabilities)
  494. (and (memq 'modifyOtherKeys tocheck-capabilities)
  495. version
  496. (>= version 216)))
  497. (terminal-init-xterm-modify-other-keys))
  498. ;; Recompute faces here in case the background mode was
  499. ;; set to dark. We used to call
  500. ;; `tty-set-up-initial-frame-faces' only once, but that
  501. ;; caused the light background faces to be computed
  502. ;; incorrectly. See:
  503. ;; http://permalink.gmane.org/gmane.emacs.devel/119627
  504. (when recompute-faces
  505. (tty-set-up-initial-frame-faces))))
  506. (run-hooks 'terminal-init-xterm-hook))
  507. (defun terminal-init-xterm-modify-other-keys ()
  508. "Terminal initialization for xterm's modifyOtherKeys support."
  509. ;; Make sure that the modifyOtherKeys state is restored when
  510. ;; suspending, resuming and exiting.
  511. (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
  512. (add-hook 'suspend-resume-hook 'xterm-turn-on-modify-other-keys)
  513. (add-hook 'kill-emacs-hook 'xterm-remove-modify-other-keys)
  514. (add-hook 'delete-terminal-functions 'xterm-remove-modify-other-keys)
  515. ;; Add the selected frame to the list of frames that
  516. ;; need to deal with modify-other-keys.
  517. (push (frame-terminal (selected-frame))
  518. xterm-modify-other-keys-terminal-list)
  519. (xterm-turn-on-modify-other-keys))
  520. ;; Set up colors, for those versions of xterm that support it.
  521. (defvar xterm-standard-colors
  522. ;; The names in the comments taken from XTerm-col.ad in the xterm
  523. ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
  524. ;; from rgb.txt.
  525. '(("black" 0 ( 0 0 0)) ; black
  526. ("red" 1 (205 0 0)) ; red3
  527. ("green" 2 ( 0 205 0)) ; green3
  528. ("yellow" 3 (205 205 0)) ; yellow3
  529. ("blue" 4 ( 0 0 238)) ; blue2
  530. ("magenta" 5 (205 0 205)) ; magenta3
  531. ("cyan" 6 ( 0 205 205)) ; cyan3
  532. ("white" 7 (229 229 229)) ; gray90
  533. ("brightblack" 8 (127 127 127)) ; gray50
  534. ("brightred" 9 (255 0 0)) ; red
  535. ("brightgreen" 10 ( 0 255 0)) ; green
  536. ("brightyellow" 11 (255 255 0)) ; yellow
  537. ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
  538. ("brightmagenta" 13 (255 0 255)) ; magenta
  539. ("brightcyan" 14 ( 0 255 255)) ; cyan
  540. ("brightwhite" 15 (255 255 255))) ; white
  541. "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
  542. (defun xterm-rgb-convert-to-16bit (prim)
  543. "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
  544. (logior prim (lsh prim 8)))
  545. (defun xterm-register-default-colors ()
  546. "Register the default set of colors for xterm or compatible emulator.
  547. This function registers the number of colors returned by `display-color-cells'
  548. for the currently selected frame. The first 16 colors are taken from
  549. `xterm-standard-colors', which see, while the rest are computed assuming
  550. either the 88- or 256-color standard color scheme supported by latest
  551. versions of xterm."
  552. (let* ((ncolors (display-color-cells (selected-frame)))
  553. (colors xterm-standard-colors)
  554. (color (car colors)))
  555. (if (> ncolors 0)
  556. ;; Clear the 8 default tty colors registered by startup.el
  557. (tty-color-clear))
  558. ;; Only register as many colors as are supported by the display.
  559. (while (and (> ncolors 0) colors)
  560. (tty-color-define (car color) (cadr color)
  561. (mapcar 'xterm-rgb-convert-to-16bit
  562. (car (cddr color))))
  563. (setq colors (cdr colors)
  564. color (car colors)
  565. ncolors (1- ncolors)))
  566. ;; We've exhausted the colors from `xterm-standard-colors'. If there
  567. ;; are more colors to support, compute them now.
  568. (when (> ncolors 0)
  569. (cond
  570. ((= ncolors 240) ; 256-color xterm
  571. ;; 216 non-gray colors first
  572. (let ((r 0) (g 0) (b 0))
  573. (while (> ncolors 24)
  574. ;; This and other formulas taken from 256colres.pl and
  575. ;; 88colres.pl in the xterm distribution.
  576. (tty-color-define (format "color-%d" (- 256 ncolors))
  577. (- 256 ncolors)
  578. (mapcar 'xterm-rgb-convert-to-16bit
  579. (list (if (zerop r) 0 (+ (* r 40) 55))
  580. (if (zerop g) 0 (+ (* g 40) 55))
  581. (if (zerop b) 0 (+ (* b 40) 55)))))
  582. (setq b (1+ b))
  583. (if (> b 5)
  584. (setq g (1+ g)
  585. b 0))
  586. (if (> g 5)
  587. (setq r (1+ r)
  588. g 0))
  589. (setq ncolors (1- ncolors))))
  590. ;; Now the 24 gray colors
  591. (while (> ncolors 0)
  592. (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
  593. (tty-color-define (format "color-%d" (- 256 ncolors))
  594. (- 256 ncolors)
  595. (list color color color))
  596. (setq ncolors (1- ncolors))))
  597. ((= ncolors 72) ; 88-color xterm
  598. ;; 64 non-gray colors
  599. (let ((levels '(0 139 205 255))
  600. (r 0) (g 0) (b 0))
  601. (while (> ncolors 8)
  602. (tty-color-define (format "color-%d" (- 88 ncolors))
  603. (- 88 ncolors)
  604. (mapcar 'xterm-rgb-convert-to-16bit
  605. (list (nth r levels)
  606. (nth g levels)
  607. (nth b levels))))
  608. (setq b (1+ b))
  609. (if (> b 3)
  610. (setq g (1+ g)
  611. b 0))
  612. (if (> g 3)
  613. (setq r (1+ r)
  614. g 0))
  615. (setq ncolors (1- ncolors))))
  616. ;; Now the 8 gray colors
  617. (while (> ncolors 0)
  618. (setq color (xterm-rgb-convert-to-16bit
  619. (floor
  620. (if (= ncolors 8)
  621. 46.36363636
  622. (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
  623. (tty-color-define (format "color-%d" (- 88 ncolors))
  624. (- 88 ncolors)
  625. (list color color color))
  626. (setq ncolors (1- ncolors))))
  627. (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
  628. ;; Modifying color mappings means realized faces don't use the
  629. ;; right colors, so clear them.
  630. (clear-face-cache)))
  631. (defun xterm-turn-on-modify-other-keys ()
  632. "Turn the modifyOtherKeys feature of xterm back on."
  633. (let ((terminal (frame-terminal (selected-frame))))
  634. (when (and (terminal-live-p terminal)
  635. (memq terminal xterm-modify-other-keys-terminal-list))
  636. (send-string-to-terminal "\e[>4;1m" terminal))))
  637. (defun xterm-turn-off-modify-other-keys (&optional frame)
  638. "Temporarily turn off the modifyOtherKeys feature of xterm."
  639. (let ((terminal (when frame (frame-terminal frame))))
  640. (when (and (terminal-live-p terminal)
  641. (memq terminal xterm-modify-other-keys-terminal-list))
  642. (send-string-to-terminal "\e[>4m" terminal))))
  643. (defun xterm-remove-modify-other-keys (&optional terminal)
  644. "Turn off the modifyOtherKeys feature of xterm for good."
  645. (setq terminal (or terminal (frame-terminal (selected-frame))))
  646. (when (and (terminal-live-p terminal)
  647. (memq terminal xterm-modify-other-keys-terminal-list))
  648. (setq xterm-modify-other-keys-terminal-list
  649. (delq terminal xterm-modify-other-keys-terminal-list))
  650. (send-string-to-terminal "\e[>4m" terminal)))
  651. (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
  652. ;; Use the heuristic in `frame-set-background-mode' to decide if a
  653. ;; frame is dark.
  654. (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
  655. (set-terminal-parameter nil 'background-mode 'dark)
  656. t))
  657. ;;; xterm.el ends here