Merge Py Idle changes:

Rev 1.39 GvR
Properly fix SF bug #507298 (Gregor Lingl): shellpython2.2 -Qnew smart
indent error

Use // where int division is intended.  (This breaks IDLE for use with
previous Python versions -- I don't care.)

Rev 1.40 tim_one
Convert a pile of obvious "yes/no" functions to return bool.

Rev 1.41 foffani/loewis
(already merged)  - MS html help

Rev 1.42
(skip, done differently in Idlefork)

Rev 1.43 tzot/rhettinger
Extended IDLE's open module menu item to handle hierarchical module names.
Will look at doing something similar in import.c so that the effort won't
have to be repeated elsewhere.
Closes SF patch 600152.

Rev 1.44 doerwalter
(string methods)
This commit is contained in:
Kurt B. Kaiser 2002-09-16 02:13:15 +00:00
parent c0a0e0810b
commit 220ecbc731
1 changed files with 26 additions and 13 deletions

View File

@ -19,6 +19,20 @@ import aboutDialog, textView, configDialog
# The default tab setting for a Text widget, in average-width characters. # The default tab setting for a Text widget, in average-width characters.
TK_TABWIDTH_DEFAULT = 8 TK_TABWIDTH_DEFAULT = 8
def _find_module(fullname, path=None):
"""Version of imp.find_module() that handles hierarchical module names"""
file = None
for tgt in fullname.split('.'):
if file is not None:
file.close() # close intermediate files
(file, filename, descr) = imp.find_module(tgt, path)
if descr[2] == imp.PY_SOURCE:
break # find but not load the source file
module = imp.load_module(tgt, file, filename, descr)
path = module.__path__
return file, filename, descr
class EditorWindow: class EditorWindow:
from Percolator import Percolator from Percolator import Percolator
from ColorDelegator import ColorDelegator from ColorDelegator import ColorDelegator
@ -183,7 +197,7 @@ class EditorWindow:
self.text.after_idle(self.set_line_and_column) self.text.after_idle(self.set_line_and_column)
def set_line_and_column(self, event=None): def set_line_and_column(self, event=None):
line, column = string.split(self.text.index(INSERT), '.') line, column = self.text.index(INSERT).split('.')
self.status_bar.set_label('column', 'Col: %s' % column) self.status_bar.set_label('column', 'Col: %s' % column)
self.status_bar.set_label('line', 'Ln: %s' % line) self.status_bar.set_label('line', 'Ln: %s' % line)
@ -346,20 +360,19 @@ class EditorWindow:
except TclError: except TclError:
name = "" name = ""
else: else:
name = string.strip(name) name = name.strip()
if not name: if not name:
name = tkSimpleDialog.askstring("Module", name = tkSimpleDialog.askstring("Module",
"Enter the name of a Python module\n" "Enter the name of a Python module\n"
"to search on sys.path and open:", "to search on sys.path and open:",
parent=self.text) parent=self.text)
if name: if name:
name = string.strip(name) name = name.strip()
if not name: if not name:
return return
# XXX Ought to support package syntax
# XXX Ought to insert current file's directory in front of path # XXX Ought to insert current file's directory in front of path
try: try:
(f, file, (suffix, mode, type)) = imp.find_module(name) (f, file, (suffix, mode, type)) = _find_module(name)
except (NameError, ImportError), msg: except (NameError, ImportError), msg:
tkMessageBox.showerror("Import error", str(msg), parent=self.text) tkMessageBox.showerror("Import error", str(msg), parent=self.text)
return return
@ -401,17 +414,17 @@ class EditorWindow:
def ispythonsource(self, filename): def ispythonsource(self, filename):
if not filename: if not filename:
return 1 return True
base, ext = os.path.splitext(os.path.basename(filename)) base, ext = os.path.splitext(os.path.basename(filename))
if os.path.normcase(ext) in (".py", ".pyw"): if os.path.normcase(ext) in (".py", ".pyw"):
return 1 return True
try: try:
f = open(filename) f = open(filename)
line = f.readline() line = f.readline()
f.close() f.close()
except IOError: except IOError:
return 0 return False
return line[:2] == '#!' and string.find(line, 'python') >= 0 return line.startswith('#!') and 'python' in line
def close_hook(self): def close_hook(self):
if self.flist: if self.flist:
@ -621,7 +634,7 @@ class EditorWindow:
top, bot = self.getwindowlines() top, bot = self.getwindowlines()
lineno = self.getlineno(mark) lineno = self.getlineno(mark)
height = bot - top height = bot - top
newtop = max(1, lineno - height/2) newtop = max(1, lineno - height//2)
text.yview(float(newtop)) text.yview(float(newtop))
def getwindowlines(self): def getwindowlines(self):
@ -712,7 +725,7 @@ class EditorWindow:
if keydefs: if keydefs:
self.apply_bindings(keydefs) self.apply_bindings(keydefs)
for vevent in keydefs.keys(): for vevent in keydefs.keys():
methodname = string.replace(vevent, "-", "_") methodname = vevent.replace("-", "_")
while methodname[:1] == '<': while methodname[:1] == '<':
methodname = methodname[1:] methodname = methodname[1:]
while methodname[-1:] == '>': while methodname[-1:] == '>':
@ -1300,7 +1313,7 @@ class IndentSearcher:
def prepstr(s): def prepstr(s):
# Helper to extract the underscore from a string, e.g. # Helper to extract the underscore from a string, e.g.
# prepstr("Co_py") returns (2, "Copy"). # prepstr("Co_py") returns (2, "Copy").
i = string.find(s, '_') i = s.find('_')
if i >= 0: if i >= 0:
s = s[:i] + s[i+1:] s = s[:i] + s[i+1:]
return i, s return i, s
@ -1317,7 +1330,7 @@ def get_accelerator(keydefs, event):
if not keylist: if not keylist:
return "" return ""
s = keylist[0] s = keylist[0]
s = re.sub(r"-[a-z]\b", lambda m: string.upper(m.group()), s) s = re.sub(r"-[a-z]\b", lambda m: m.group().upper(), s)
s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s) s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
s = re.sub("Key-", "", s) s = re.sub("Key-", "", s)
s = re.sub("Cancel","Ctrl-Break",s) # dscherer@cmu.edu s = re.sub("Cancel","Ctrl-Break",s) # dscherer@cmu.edu