Skip to content

Commit c5896bd

Browse files
committed
More show source.
1 parent f2e931a commit c5896bd

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

bpython/gtk_.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import gobject
4141
import gtk
4242
import pango
43+
from pygments.lexers import PythonLexer
4344

4445
from bpython import importcompletion, repl
4546
from bpython.formatter import theme_map
@@ -474,25 +475,10 @@ def do_key_press_event(self, event):
474475
if event.keyval == gtk.keysyms.F2:
475476
source = self.get_source_of_current_name()
476477
if source is not None:
477-
win = gtk.Window()
478-
sw = gtk.ScrolledWindow()
479-
view = gtk.TextView()
480-
buffer = view.get_buffer()
481-
add_tags_to_buffer(self.config.color_gtk_scheme, buffer)
482-
from pygments.lexers import PythonLexer
483-
tokens = PythonLexer().get_tokens(source)
484-
for (token, value) in tokens:
485-
while token not in theme_map:
486-
token = token.parent
487-
iter_ = buffer.get_end_iter()
488-
buffer.insert_with_tags_by_name(iter_, value,
489-
theme_map[token])
490-
sw.add(view)
491-
win.add(sw)
492-
win.show_all()
478+
show_source_in_new_window(source, self.config.color_gtk_scheme,
479+
self.config.syntax)
493480
else:
494-
# XXX Error message
495-
pass
481+
self.interact.notify('Cannot show source.')
496482
elif event.keyval == gtk.keysyms.Return:
497483
if self.list_win_visible:
498484
self.list_win_visible = False
@@ -750,6 +736,23 @@ def writetb(self, lines):
750736
)
751737
self.move_cursor(len(string))
752738

739+
def show_source_in_new_window(source, color_scheme=None, highlight=True):
740+
win = gtk.Window()
741+
sw = gtk.ScrolledWindow()
742+
view = gtk.TextView()
743+
buffer = view.get_buffer()
744+
if highlight:
745+
add_tags_to_buffer(color_scheme, buffer)
746+
for (token, value) in PythonLexer().get_tokens(source):
747+
while token not in theme_map:
748+
token = token.parent
749+
iter_ = buffer.get_end_iter()
750+
buffer.insert_with_tags_by_name(iter_, value, theme_map[token])
751+
else:
752+
buffer.insert(buffer.get_end_iter(), source)
753+
sw.add(view)
754+
win.add(sw)
755+
win.show_all()
753756

754757
def init_import_completion():
755758
try:

0 commit comments

Comments
 (0)