Stripping ps1/ps2 when pasting into IDLE.
Tim Peters
tim_one at email.msn.com
Sat May 29 03:19:34 EDT 1999
[Mark Hammond[
> ... why not write a "SmartPaste" IDLE extension, and change
> the default bindings for paste to your extension.
[Lloyd Zusman]
> Well, this could work, but it wouldn't be general enough for my needs.
> When I cut or copy from outside of IDLE and try to paste into IDLE, no
> IDLE code is executed to do this pasting. The text just looks like
> it's being typed in from the keyboard. The only time an IDLE
> extension could be used for my customized pasting would be in the case
> where the original cutting or copying is done from within IDLE using
> its facilities.
Well, stuff doesn't get pasted by magic <wink/frown>. Tk is handling the
pasting, and if Tk can get at it so can IDLE. Here's a start, file
Paster.py:
from Tkinter import TclError
class Paster:
menudefs = [
('edit', [
None, # Separator
('Fancy paste', '<<fancy-paste>>'),
])
]
windows_keydefs = {
'<<fancy-paste>>': ['<Alt-F9>'],
}
def __init__(self, editwin):
self.text = editwin.text
def fancy_paste_event(self, event):
text = self.text
try:
stuff = text.selection_get(selection="CLIPBOARD")
except TclError:
# most likely the clipboard is empty
text.bell()
else:
# do stuff to stuff, e.g. ...
stuff = "(" + stuff + ")"
# replace clipboard with new stuff
text.clipboard_clear()
text.clipboard_append(stuff)
# pass it off to the regular Tk paste
text.event_generate("<<Paste>>")
return "break"
Then stick
"Paster",
at the end of the list in extend.py. That's it! "Fancy paste" shows up at
the bottom of the edit menu, and on Windows the same thing is invoked by
Alt+F9. Finding a free key binding is a nightmare, btw, and I just picked
on one I happen to know isn't bound to anything else. If more than one
event gets bound to a key sequence, Tk doesn't gripe, but neither does it
define what it will do when the key sequence triggers.
BTW, at least under Windows this uses the system clipboard in both
directions, so pasting from foreign windows into IDLE (or vice versa) is no
problem.
try-rebinding-the-default-<<paste>>-at-your-own-risk-ly y'rs - tim
More information about the Python-list
mailing list