m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

731 lines
30 KiB

6 months ago
  1. # -*- coding: utf-8 -*-
  2. #*****************************************************************************
  3. # Copyright (C) 2003-2006 Gary Bishop.
  4. # Copyright (C) 2006 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #*****************************************************************************
  9. from __future__ import print_function, unicode_literals, absolute_import
  10. import os, sys, time
  11. import pyreadline.logger as logger
  12. from pyreadline.logger import log
  13. from pyreadline.lineeditor.lineobj import Point
  14. import pyreadline.lineeditor.lineobj as lineobj
  15. import pyreadline.lineeditor.history as history
  16. from . import basemode
  17. from pyreadline.unicode_helper import ensure_unicode
  18. def format(keyinfo):
  19. if len(keyinfo[-1]) != 1:
  20. k = keyinfo + (-1,)
  21. else:
  22. k = keyinfo + (ord(keyinfo[-1]),)
  23. return "(%s,%s,%s,%s,%x)"%k
  24. in_ironpython = "IronPython" in sys.version
  25. class IncrementalSearchPromptMode(object):
  26. def __init__(self, rlobj):
  27. pass
  28. def _process_incremental_search_keyevent(self, keyinfo):
  29. log("_process_incremental_search_keyevent")
  30. keytuple = keyinfo.tuple()
  31. #dispatch_func = self.key_dispatch.get(keytuple, default)
  32. revtuples = []
  33. fwdtuples = []
  34. for ktuple, func in self.key_dispatch.items():
  35. if func == self.reverse_search_history:
  36. revtuples.append(ktuple)
  37. elif func == self.forward_search_history:
  38. fwdtuples.append(ktuple)
  39. log("IncrementalSearchPromptMode %s %s"%(keyinfo, keytuple))
  40. if keyinfo.keyname == 'backspace':
  41. self.subsearch_query = self.subsearch_query[:-1]
  42. if len(self.subsearch_query) > 0:
  43. self.line = self.subsearch_fun(self.subsearch_query)
  44. else:
  45. self._bell()
  46. self.line = "" # empty query means no search result
  47. elif keyinfo.keyname in ['return', 'escape']:
  48. self._bell()
  49. self.prompt = self.subsearch_oldprompt
  50. self.process_keyevent_queue = self.process_keyevent_queue[:-1]
  51. self._history.history_cursor = len(self._history.history)
  52. if keyinfo.keyname == 'escape':
  53. self.l_buffer.set_line(self.subsearch_old_line)
  54. return True
  55. elif keyinfo.keyname:
  56. pass
  57. elif keytuple in revtuples:
  58. self.subsearch_fun = self._history.reverse_search_history
  59. self.subsearch_prompt = "reverse-i-search%d`%s': "
  60. self.line = self.subsearch_fun(self.subsearch_query)
  61. elif keytuple in fwdtuples:
  62. self.subsearch_fun = self._history.forward_search_history
  63. self.subsearch_prompt = "forward-i-search%d`%s': "
  64. self.line = self.subsearch_fun(self.subsearch_query)
  65. elif keyinfo.control == False and keyinfo.meta == False:
  66. self.subsearch_query += keyinfo.char
  67. self.line = self.subsearch_fun(self.subsearch_query)
  68. else:
  69. pass
  70. self.prompt = self.subsearch_prompt%(self._history.history_cursor, self.subsearch_query)
  71. self.l_buffer.set_line(self.line)
  72. def _init_incremental_search(self, searchfun, init_event):
  73. """Initialize search prompt
  74. """
  75. log("init_incremental_search")
  76. self.subsearch_query = ''
  77. self.subsearch_fun = searchfun
  78. self.subsearch_old_line = self.l_buffer.get_line_text()
  79. queue = self.process_keyevent_queue
  80. queue.append(self._process_incremental_search_keyevent)
  81. self.subsearch_oldprompt = self.prompt
  82. if (self.previous_func != self.reverse_search_history and
  83. self.previous_func != self.forward_search_history):
  84. self.subsearch_query = self.l_buffer[0:Point].get_line_text()
  85. if self.subsearch_fun == self.reverse_search_history:
  86. self.subsearch_prompt = "reverse-i-search%d`%s': "
  87. else:
  88. self.subsearch_prompt = "forward-i-search%d`%s': "
  89. self.prompt = self.subsearch_prompt%(self._history.history_cursor, "")
  90. if self.subsearch_query:
  91. self.line = self._process_incremental_search_keyevent(init_event)
  92. else:
  93. self.line = ""
  94. class SearchPromptMode(object):
  95. def __init__(self, rlobj):
  96. pass
  97. def _process_non_incremental_search_keyevent(self, keyinfo):
  98. keytuple = keyinfo.tuple()
  99. log("SearchPromptMode %s %s"%(keyinfo, keytuple))
  100. history = self._history
  101. if keyinfo.keyname == 'backspace':
  102. self.non_inc_query = self.non_inc_query[:-1]
  103. elif keyinfo.keyname in ['return', 'escape']:
  104. if self.non_inc_query:
  105. if self.non_inc_direction == -1:
  106. res = history.reverse_search_history(self.non_inc_query)
  107. else:
  108. res = history.forward_search_history(self.non_inc_query)
  109. self._bell()
  110. self.prompt = self.non_inc_oldprompt
  111. self.process_keyevent_queue = self.process_keyevent_queue[:-1]
  112. self._history.history_cursor = len(self._history.history)
  113. if keyinfo.keyname == 'escape':
  114. self.l_buffer = self.non_inc_oldline
  115. else:
  116. self.l_buffer.set_line(res)
  117. return False
  118. elif keyinfo.keyname:
  119. pass
  120. elif keyinfo.control == False and keyinfo.meta == False:
  121. self.non_inc_query += keyinfo.char
  122. else:
  123. pass
  124. self.prompt = self.non_inc_oldprompt + ":" + self.non_inc_query
  125. def _init_non_i_search(self, direction):
  126. self.non_inc_direction = direction
  127. self.non_inc_query = ""
  128. self.non_inc_oldprompt = self.prompt
  129. self.non_inc_oldline = self.l_buffer.copy()
  130. self.l_buffer.reset_line()
  131. self.prompt = self.non_inc_oldprompt + ":"
  132. queue = self.process_keyevent_queue
  133. queue.append(self._process_non_incremental_search_keyevent)
  134. def non_incremental_reverse_search_history(self, e): # (M-p)
  135. '''Search backward starting at the current line and moving up
  136. through the history as necessary using a non-incremental search for
  137. a string supplied by the user.'''
  138. return self._init_non_i_search(-1)
  139. def non_incremental_forward_search_history(self, e): # (M-n)
  140. '''Search forward starting at the current line and moving down
  141. through the the history as necessary using a non-incremental search
  142. for a string supplied by the user.'''
  143. return self._init_non_i_search(1)
  144. class LeaveModeTryNext(Exception):
  145. pass
  146. class DigitArgumentMode(object):
  147. def __init__(self, rlobj):
  148. pass
  149. def _process_digit_argument_keyevent(self, keyinfo):
  150. log("DigitArgumentMode.keyinfo %s"%keyinfo)
  151. keytuple = keyinfo.tuple()
  152. log("DigitArgumentMode.keytuple %s %s"%(keyinfo, keytuple))
  153. if keyinfo.keyname in ['return']:
  154. self.prompt = self._digit_argument_oldprompt
  155. self.process_keyevent_queue = self.process_keyevent_queue[:-1]
  156. return True
  157. elif keyinfo.keyname:
  158. pass
  159. elif (keyinfo.char in "0123456789" and
  160. keyinfo.control == False and
  161. keyinfo.meta == False):
  162. log("arg %s %s"%(self.argument, keyinfo.char))
  163. self.argument = self.argument * 10 + int(keyinfo.char)
  164. else:
  165. self.prompt = self._digit_argument_oldprompt
  166. raise LeaveModeTryNext
  167. self.prompt = "(arg: %s) "%self.argument
  168. def _init_digit_argument(self, keyinfo):
  169. """Initialize search prompt
  170. """
  171. c = self.console
  172. line = self.l_buffer.get_line_text()
  173. self._digit_argument_oldprompt = self.prompt
  174. queue = self.process_keyevent_queue
  175. queue = self.process_keyevent_queue
  176. queue.append(self._process_digit_argument_keyevent)
  177. if keyinfo.char == "-":
  178. self.argument = -1
  179. elif keyinfo.char in "0123456789":
  180. self.argument = int(keyinfo.char)
  181. log("<%s> %s"%(self.argument, type(self.argument)))
  182. self.prompt = "(arg: %s) "%self.argument
  183. log("arg-init %s %s"%(self.argument, keyinfo.char))
  184. class EmacsMode(DigitArgumentMode, IncrementalSearchPromptMode,
  185. SearchPromptMode, basemode.BaseMode):
  186. mode = "emacs"
  187. def __init__(self, rlobj):
  188. basemode.BaseMode.__init__(self, rlobj)
  189. IncrementalSearchPromptMode.__init__(self, rlobj)
  190. SearchPromptMode.__init__(self, rlobj)
  191. DigitArgumentMode.__init__(self, rlobj)
  192. self._keylog = (lambda x, y: None)
  193. self.previous_func = None
  194. self.prompt = ">>> "
  195. self._insert_verbatim = False
  196. self.next_meta = False # True to force meta on next character
  197. self.process_keyevent_queue = [self._process_keyevent]
  198. def __repr__(self):
  199. return "<EmacsMode>"
  200. def add_key_logger(self, logfun):
  201. """logfun should be function that takes disp_fun and line_"""\
  202. """buffer object """
  203. self._keylog = logfun
  204. def process_keyevent(self, keyinfo):
  205. try:
  206. r = self.process_keyevent_queue[-1](keyinfo)
  207. except LeaveModeTryNext:
  208. self.process_keyevent_queue = self.process_keyevent_queue[:-1]
  209. r = self.process_keyevent(keyinfo)
  210. if r:
  211. self.add_history(self.l_buffer.copy())
  212. return True
  213. return False
  214. def _process_keyevent(self, keyinfo):
  215. """return True when line is final
  216. """
  217. #Process exit keys. Only exit on empty line
  218. log("_process_keyevent <%s>"%keyinfo)
  219. def nop(e):
  220. pass
  221. if self.next_meta:
  222. self.next_meta = False
  223. keyinfo.meta = True
  224. keytuple = keyinfo.tuple()
  225. if self._insert_verbatim:
  226. self.insert_text(keyinfo)
  227. self._insert_verbatim = False
  228. self.argument = 0
  229. return False
  230. if keytuple in self.exit_dispatch:
  231. pars = (self.l_buffer, lineobj.EndOfLine(self.l_buffer))
  232. log("exit_dispatch:<%s, %s>"%pars)
  233. if lineobj.EndOfLine(self.l_buffer) == 0:
  234. raise EOFError
  235. if keyinfo.keyname or keyinfo.control or keyinfo.meta:
  236. default = nop
  237. else:
  238. default = self.self_insert
  239. dispatch_func = self.key_dispatch.get(keytuple, default)
  240. log("readline from keyboard:<%s,%s>"%(keytuple, dispatch_func))
  241. r = None
  242. if dispatch_func:
  243. r = dispatch_func(keyinfo)
  244. self._keylog(dispatch_func, self.l_buffer)
  245. self.l_buffer.push_undo()
  246. self.previous_func = dispatch_func
  247. return r
  248. ######### History commands
  249. def previous_history(self, e): # (C-p)
  250. '''Move back through the history list, fetching the previous
  251. command. '''
  252. self._history.previous_history(self.l_buffer)
  253. self.l_buffer.point = lineobj.EndOfLine
  254. self.finalize()
  255. def next_history(self, e): # (C-n)
  256. '''Move forward through the history list, fetching the next
  257. command. '''
  258. self._history.next_history(self.l_buffer)
  259. self.finalize()
  260. def beginning_of_history(self, e): # (M-<)
  261. '''Move to the first line in the history.'''
  262. self._history.beginning_of_history()
  263. self.finalize()
  264. def end_of_history(self, e): # (M->)
  265. '''Move to the end of the input history, i.e., the line currently
  266. being entered.'''
  267. self._history.end_of_history(self.l_buffer)
  268. self.finalize()
  269. def reverse_search_history(self, e): # (C-r)
  270. '''Search backward starting at the current line and moving up
  271. through the history as necessary. This is an incremental search.'''
  272. log("rev_search_history")
  273. self._init_incremental_search(self._history.reverse_search_history, e)
  274. self.finalize()
  275. def forward_search_history(self, e): # (C-s)
  276. '''Search forward starting at the current line and moving down
  277. through the the history as necessary. This is an incremental
  278. search.'''
  279. log("fwd_search_history")
  280. self._init_incremental_search(self._history.forward_search_history, e)
  281. self.finalize()
  282. def history_search_forward(self, e): # ()
  283. '''Search forward through the history for the string of characters
  284. between the start of the current line and the point. This is a
  285. non-incremental search. By default, this command is unbound.'''
  286. if (self.previous_func and
  287. hasattr(self._history, self.previous_func.__name__)):
  288. self._history.lastcommand = getattr(self._history,
  289. self.previous_func.__name__)
  290. else:
  291. self._history.lastcommand = None
  292. q = self._history.history_search_forward(self.l_buffer)
  293. self.l_buffer = q
  294. self.l_buffer.point = q.point
  295. self.finalize()
  296. def history_search_backward(self, e): # ()
  297. '''Search backward through the history for the string of characters
  298. between the start of the current line and the point. This is a
  299. non-incremental search. By default, this command is unbound.'''
  300. if (self.previous_func and
  301. hasattr(self._history, self.previous_func.__name__)):
  302. self._history.lastcommand = getattr(self._history,
  303. self.previous_func.__name__)
  304. else:
  305. self._history.lastcommand = None
  306. q = self._history.history_search_backward(self.l_buffer)
  307. self.l_buffer = q
  308. self.l_buffer.point = q.point
  309. self.finalize()
  310. def yank_nth_arg(self, e): # (M-C-y)
  311. '''Insert the first argument to the previous command (usually the
  312. second word on the previous line) at point. With an argument n,
  313. insert the nth word from the previous command (the words in the
  314. previous command begin with word 0). A negative argument inserts the
  315. nth word from the end of the previous command.'''
  316. self.finalize()
  317. def yank_last_arg(self, e): # (M-. or M-_)
  318. '''Insert last argument to the previous command (the last word of
  319. the previous history entry). With an argument, behave exactly like
  320. yank-nth-arg. Successive calls to yank-last-arg move back through
  321. the history list, inserting the last argument of each line in turn.'''
  322. self.finalize()
  323. def forward_backward_delete_char(self, e): # ()
  324. '''Delete the character under the cursor, unless the cursor is at
  325. the end of the line, in which case the character behind the cursor
  326. is deleted. By default, this is not bound to a key.'''
  327. self.finalize()
  328. def quoted_insert(self, e): # (C-q or C-v)
  329. '''Add the next character typed to the line verbatim. This is how to
  330. insert key sequences like C-q, for example.'''
  331. self._insert_verbatim = True
  332. self.finalize()
  333. def tab_insert(self, e): # (M-TAB)
  334. '''Insert a tab character. '''
  335. cursor = min(self.l_buffer.point, len(self.l_buffer.line_buffer))
  336. ws = ' ' * (self.tabstop - (cursor % self.tabstop))
  337. self.insert_text(ws)
  338. self.finalize()
  339. def transpose_chars(self, e): # (C-t)
  340. '''Drag the character before the cursor forward over the character
  341. at the cursor, moving the cursor forward as well. If the insertion
  342. point is at the end of the line, then this transposes the last two
  343. characters of the line. Negative arguments have no effect.'''
  344. self.l_buffer.transpose_chars()
  345. self.finalize()
  346. def transpose_words(self, e): # (M-t)
  347. '''Drag the word before point past the word after point, moving
  348. point past that word as well. If the insertion point is at the end
  349. of the line, this transposes the last two words on the line.'''
  350. self.l_buffer.transpose_words()
  351. self.finalize()
  352. def overwrite_mode(self, e): # ()
  353. '''Toggle overwrite mode. With an explicit positive numeric
  354. argument, switches to overwrite mode. With an explicit non-positive
  355. numeric argument, switches to insert mode. This command affects only
  356. emacs mode; vi mode does overwrite differently. Each call to
  357. readline() starts in insert mode. In overwrite mode, characters
  358. bound to self-insert replace the text at point rather than pushing
  359. the text to the right. Characters bound to backward-delete-char
  360. replace the character before point with a space.'''
  361. self.finalize()
  362. def kill_line(self, e): # (C-k)
  363. '''Kill the text from point to the end of the line. '''
  364. self.l_buffer.kill_line()
  365. self.finalize()
  366. def backward_kill_line(self, e): # (C-x Rubout)
  367. '''Kill backward to the beginning of the line. '''
  368. self.l_buffer.backward_kill_line()
  369. self.finalize()
  370. def unix_line_discard(self, e): # (C-u)
  371. '''Kill backward from the cursor to the beginning of the current
  372. line. '''
  373. # how is this different from backward_kill_line?
  374. self.l_buffer.unix_line_discard()
  375. self.finalize()
  376. def kill_whole_line(self, e): # ()
  377. '''Kill all characters on the current line, no matter where point
  378. is. By default, this is unbound.'''
  379. self.l_buffer.kill_whole_line()
  380. self.finalize()
  381. def kill_word(self, e): # (M-d)
  382. '''Kill from point to the end of the current word, or if between
  383. words, to the end of the next word. Word boundaries are the same as
  384. forward-word.'''
  385. self.l_buffer.kill_word()
  386. self.finalize()
  387. forward_kill_word = kill_word
  388. def backward_kill_word(self, e): # (M-DEL)
  389. '''Kill the word behind point. Word boundaries are the same as
  390. backward-word. '''
  391. self.l_buffer.backward_kill_word()
  392. self.finalize()
  393. def unix_word_rubout(self, e): # (C-w)
  394. '''Kill the word behind point, using white space as a word
  395. boundary. The killed text is saved on the kill-ring.'''
  396. self.l_buffer.unix_word_rubout()
  397. self.finalize()
  398. def kill_region(self, e): # ()
  399. '''Kill the text in the current region. By default, this command is
  400. unbound. '''
  401. self.finalize()
  402. def copy_region_as_kill(self, e): # ()
  403. '''Copy the text in the region to the kill buffer, so it can be
  404. yanked right away. By default, this command is unbound.'''
  405. self.finalize()
  406. def copy_backward_word(self, e): # ()
  407. '''Copy the word before point to the kill buffer. The word
  408. boundaries are the same as backward-word. By default, this command
  409. is unbound.'''
  410. self.finalize()
  411. def copy_forward_word(self, e): # ()
  412. '''Copy the word following point to the kill buffer. The word
  413. boundaries are the same as forward-word. By default, this command is
  414. unbound.'''
  415. self.finalize()
  416. def yank(self, e): # (C-y)
  417. '''Yank the top of the kill ring into the buffer at point. '''
  418. self.l_buffer.yank()
  419. self.finalize()
  420. def yank_pop(self, e): # (M-y)
  421. '''Rotate the kill-ring, and yank the new top. You can only do this
  422. if the prior command is yank or yank-pop.'''
  423. self.l_buffer.yank_pop()
  424. self.finalize()
  425. def delete_char_or_list(self, e): # ()
  426. '''Deletes the character under the cursor if not at the beginning or
  427. end of the line (like delete-char). If at the end of the line,
  428. behaves identically to possible-completions. This command is unbound
  429. by default.'''
  430. self.finalize()
  431. def start_kbd_macro(self, e): # (C-x ()
  432. '''Begin saving the characters typed into the current keyboard
  433. macro. '''
  434. self.finalize()
  435. def end_kbd_macro(self, e): # (C-x ))
  436. '''Stop saving the characters typed into the current keyboard macro
  437. and save the definition.'''
  438. self.finalize()
  439. def call_last_kbd_macro(self, e): # (C-x e)
  440. '''Re-execute the last keyboard macro defined, by making the
  441. characters in the macro appear as if typed at the keyboard.'''
  442. self.finalize()
  443. def re_read_init_file(self, e): # (C-x C-r)
  444. '''Read in the contents of the inputrc file, and incorporate any
  445. bindings or variable assignments found there.'''
  446. self.finalize()
  447. def abort(self, e): # (C-g)
  448. '''Abort the current editing command and ring the terminals bell
  449. (subject to the setting of bell-style).'''
  450. self._bell()
  451. self.finalize()
  452. def do_uppercase_version(self, e): # (M-a, M-b, M-x, ...)
  453. '''If the metafied character x is lowercase, run the command that is
  454. bound to the corresponding uppercase character.'''
  455. self.finalize()
  456. def prefix_meta(self, e): # (ESC)
  457. '''Metafy the next character typed. This is for keyboards without a
  458. meta key. Typing ESC f is equivalent to typing M-f. '''
  459. self.next_meta = True
  460. self.finalize()
  461. def undo(self, e): # (C-_ or C-x C-u)
  462. '''Incremental undo, separately remembered for each line.'''
  463. self.l_buffer.pop_undo()
  464. self.finalize()
  465. def revert_line(self, e): # (M-r)
  466. '''Undo all changes made to this line. This is like executing the
  467. undo command enough times to get back to the beginning.'''
  468. self.finalize()
  469. def tilde_expand(self, e): # (M-~)
  470. '''Perform tilde expansion on the current word.'''
  471. self.finalize()
  472. def set_mark(self, e): # (C-@)
  473. '''Set the mark to the point. If a numeric argument is supplied, the
  474. mark is set to that position.'''
  475. self.l_buffer.set_mark()
  476. self.finalize()
  477. def exchange_point_and_mark(self, e): # (C-x C-x)
  478. '''Swap the point with the mark. The current cursor position is set
  479. to the saved position, and the old cursor position is saved as the
  480. mark.'''
  481. self.finalize()
  482. def character_search(self, e): # (C-])
  483. '''A character is read and point is moved to the next occurrence of
  484. that character. A negative count searches for previous occurrences.'''
  485. self.finalize()
  486. def character_search_backward(self, e): # (M-C-])
  487. '''A character is read and point is moved to the previous occurrence
  488. of that character. A negative count searches for subsequent
  489. occurrences.'''
  490. self.finalize()
  491. def insert_comment(self, e): # (M-#)
  492. '''Without a numeric argument, the value of the comment-begin
  493. variable is inserted at the beginning of the current line. If a
  494. numeric argument is supplied, this command acts as a toggle: if the
  495. characters at the beginning of the line do not match the value of
  496. comment-begin, the value is inserted, otherwise the characters in
  497. comment-begin are deleted from the beginning of the line. In either
  498. case, the line is accepted as if a newline had been typed.'''
  499. self.finalize()
  500. def dump_variables(self, e): # ()
  501. '''Print all of the settable variables and their values to the
  502. Readline output stream. If a numeric argument is supplied, the
  503. output is formatted in such a way that it can be made part of an
  504. inputrc file. This command is unbound by default.'''
  505. self.finalize()
  506. def dump_macros(self, e): # ()
  507. '''Print all of the Readline key sequences bound to macros and the
  508. strings they output. If a numeric argument is supplied, the output
  509. is formatted in such a way that it can be made part of an inputrc
  510. file. This command is unbound by default.'''
  511. self.finalize()
  512. def digit_argument(self, e): # (M-0, M-1, ... M--)
  513. '''Add this digit to the argument already accumulating, or start a
  514. new argument. M-- starts a negative argument.'''
  515. self._init_digit_argument(e)
  516. #Should not finalize
  517. def universal_argument(self, e): # ()
  518. '''This is another way to specify an argument. If this command is
  519. followed by one or more digits, optionally with a leading minus
  520. sign, those digits define the argument. If the command is followed
  521. by digits, executing universal-argument again ends the numeric
  522. argument, but is otherwise ignored. As a special case, if this
  523. command is immediately followed by a character that is neither a
  524. digit or minus sign, the argument count for the next command is
  525. multiplied by four. The argument count is initially one, so
  526. executing this function the first time makes the argument count
  527. four, a second time makes the argument count sixteen, and so on. By
  528. default, this is not bound to a key.'''
  529. #Should not finalize
  530. #Create key bindings:
  531. def init_editing_mode(self, e): # (C-e)
  532. '''When in vi command mode, this causes a switch to emacs editing
  533. mode.'''
  534. self._bind_exit_key('Control-d')
  535. self._bind_exit_key('Control-z')
  536. # I often accidentally hold the shift or control while typing space
  537. self._bind_key('space', self.self_insert)
  538. self._bind_key('Shift-space', self.self_insert)
  539. self._bind_key('Control-space', self.self_insert)
  540. self._bind_key('Return', self.accept_line)
  541. self._bind_key('Left', self.backward_char)
  542. self._bind_key('Control-b', self.backward_char)
  543. self._bind_key('Right', self.forward_char)
  544. self._bind_key('Control-f', self.forward_char)
  545. self._bind_key('Control-h', self.backward_delete_char)
  546. self._bind_key('BackSpace', self.backward_delete_char)
  547. self._bind_key('Control-BackSpace', self.backward_delete_word)
  548. self._bind_key('Home', self.beginning_of_line)
  549. self._bind_key('End', self.end_of_line)
  550. self._bind_key('Delete', self.delete_char)
  551. self._bind_key('Control-d', self.delete_char)
  552. self._bind_key('Clear', self.clear_screen)
  553. self._bind_key('Alt-f', self.forward_word)
  554. self._bind_key('Alt-b', self.backward_word)
  555. self._bind_key('Control-l', self.clear_screen)
  556. self._bind_key('Control-p', self.previous_history)
  557. self._bind_key('Up', self.history_search_backward)
  558. self._bind_key('Control-n', self.next_history)
  559. self._bind_key('Down', self.history_search_forward)
  560. self._bind_key('Control-a', self.beginning_of_line)
  561. self._bind_key('Control-e', self.end_of_line)
  562. self._bind_key('Alt-<', self.beginning_of_history)
  563. self._bind_key('Alt->', self.end_of_history)
  564. self._bind_key('Control-r', self.reverse_search_history)
  565. self._bind_key('Control-s', self.forward_search_history)
  566. self._bind_key('Control-Shift-r', self.forward_search_history)
  567. self._bind_key('Alt-p',
  568. self.non_incremental_reverse_search_history)
  569. self._bind_key('Alt-n',
  570. self.non_incremental_forward_search_history)
  571. self._bind_key('Control-z', self.undo)
  572. self._bind_key('Control-_', self.undo)
  573. self._bind_key('Escape', self.kill_whole_line)
  574. self._bind_key('Meta-d', self.kill_word)
  575. self._bind_key('Control-Delete', self.forward_delete_word)
  576. self._bind_key('Control-w', self.unix_word_rubout)
  577. #self._bind_key('Control-Shift-v', self.quoted_insert)
  578. self._bind_key('Control-v', self.paste)
  579. self._bind_key('Alt-v', self.ipython_paste)
  580. self._bind_key('Control-y', self.yank)
  581. self._bind_key('Control-k', self.kill_line)
  582. self._bind_key('Control-m', self.set_mark)
  583. self._bind_key('Control-q', self.copy_region_to_clipboard)
  584. # self._bind_key('Control-shift-k', self.kill_whole_line)
  585. self._bind_key('Control-Shift-v', self.paste_mulitline_code)
  586. self._bind_key("Control-Right", self.forward_word_end)
  587. self._bind_key("Control-Left", self.backward_word)
  588. self._bind_key("Shift-Right",
  589. self.forward_char_extend_selection)
  590. self._bind_key("Shift-Left",
  591. self.backward_char_extend_selection)
  592. self._bind_key("Shift-Control-Right",
  593. self.forward_word_end_extend_selection)
  594. self._bind_key("Shift-Control-Left",
  595. self.backward_word_extend_selection)
  596. self._bind_key("Shift-Home",
  597. self.beginning_of_line_extend_selection)
  598. self._bind_key("Shift-End",
  599. self.end_of_line_extend_selection)
  600. self._bind_key("numpad0", self.self_insert)
  601. self._bind_key("numpad1", self.self_insert)
  602. self._bind_key("numpad2", self.self_insert)
  603. self._bind_key("numpad3", self.self_insert)
  604. self._bind_key("numpad4", self.self_insert)
  605. self._bind_key("numpad5", self.self_insert)
  606. self._bind_key("numpad6", self.self_insert)
  607. self._bind_key("numpad7", self.self_insert)
  608. self._bind_key("numpad8", self.self_insert)
  609. self._bind_key("numpad9", self.self_insert)
  610. self._bind_key("add", self.self_insert)
  611. self._bind_key("subtract", self.self_insert)
  612. self._bind_key("multiply", self.self_insert)
  613. self._bind_key("divide", self.self_insert)
  614. self._bind_key("vk_decimal", self.self_insert)
  615. log("RUNNING INIT EMACS")
  616. for i in range(0, 10):
  617. self._bind_key("alt-%d"%i, self.digit_argument)
  618. self._bind_key("alt--", self.digit_argument)
  619. # make it case insensitive
  620. def commonprefix(m):
  621. "Given a list of pathnames, returns the longest common leading component"
  622. if not m:
  623. return ''
  624. prefix = m[0]
  625. for item in m:
  626. for i in range(len(prefix)):
  627. if prefix[:i + 1].lower() != item[:i + 1].lower():
  628. prefix = prefix[:i]
  629. if i == 0:
  630. return ''
  631. break
  632. return prefix