Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Tue, 16 Oct 2012 10:30:01 +0200
Local: Tues, Oct 16 2012 4:30 am
Subject: [on topic] Re: readline trick needed
Steven D'Aprano wrote:
If IPython does what you want why don't you have a look at the source?
> I'm working with the readline module, and I'm trying to set a key > combination to process the current command line by calling a known > function, *and* enter the command line. > Something along the lines of:
> * execute function spam() in some context where it can access
> Function spam() may or may not modify the command line.
Anyway, here's what I came up with (no warranties as it was all trial-and-
$ cat readline_callback.py
rlname = ctypes.util.find_library("readline")
# int rl_add_defun (const char *name, rl_command_func_t *function, int key)
rl = ctypes.CDLL(rlname)
RL_COMMAND_FUNC = ctypes.CFUNCTYPE(c_int, c_int, c_int)
intercepted_line = None
def control(c):
rl.rl_add_defun("lovely-spam", RL_COMMAND_FUNC(lovely_spam), control("P"))
#rl.rl_add_defun("lovely-spam", RL_COMMAND_FUNC(lovely_spam), -1)
$ python -i readline_callback.py >>> alpha = 42
1764
>>> alpha The relevant documentation:
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||