emacs.py 29 KB

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