From 2ea3ea0c7c4ed8af0a1e3b1683477fb4e9de2923 Mon Sep 17 00:00:00 2001 From: HD Moore Date: Sun, 20 Jan 2008 23:36:20 +0000 Subject: [PATCH] Syntax highlighting to code view git-svn-id: file:///home/svn/framework3/trunk@5296 4d416f70-5f16-0410-b530-b9f4589650da --- lib/msf/ui/gtk2/window/codeview.rb | 79 +++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/lib/msf/ui/gtk2/window/codeview.rb b/lib/msf/ui/gtk2/window/codeview.rb index 95af07eb5b..f19697d407 100644 --- a/lib/msf/ui/gtk2/window/codeview.rb +++ b/lib/msf/ui/gtk2/window/codeview.rb @@ -9,6 +9,8 @@ class MsfWindow # class CodeView < Msf::Ui::Gtk2::SkeletonBasic + + include Msf::Ui::Gtk2::MyControls def initialize(m) @@ -40,12 +42,85 @@ class MsfWindow "font" => "Courier" } ) - - buff.insert(buff.end_iter, File.read(m.file_path), "fixr") + + + font_desc = Pango::FontDescription.new('Courier') + textview.modify_font(font_desc) +# textview.set_pixels_above_lines(2) +# textview.set_pixels_below_lines(2) + + buff.create_tag('comment', {'foreground' => 'ForestGreen'}) + buff.create_tag('const', {'foreground' => 'DarkGreen'}) + buff.create_tag('method', {'foreground' => 'DarkRed'}) + buff.create_tag('string', { + 'foreground' => 'DarkBlue', + 'weight' => Pango::FontDescription::WEIGHT_BOLD + }) + + buff.create_tag('reserved', {'foreground' => 'purple'}) + + + buff.insert(buff.start_iter, File.read(m.file_path)) + + start_iter = buff.start_iter + end_iter = buff.end_iter + str = buff.get_text(start_iter, end_iter, true) + + tokenizer = RubyTokenizer.new + tokenizer.tokenize(str, start_iter.offset) do |tag, start, last| + buff.apply_tag( + tag.to_s, + buff.get_iter_at_offset(start), + buff.get_iter_at_offset(last) + ) + end + show_all end + + # + # Pulled from ruby-gtk2 / gtk-demo (under Ruby license) + # + class RubyTokenizer + RESERVED_WORDS = %w(begin end module class def if then else while unless do case when require yield) + RESERVED_WORDS_PATTERN = Regexp.compile(/(^|\s+)(#{RESERVED_WORDS.collect do |pat| Regexp.quote(pat) end.join('|')})(\s+|$)/) + + def tokenize(str, index = 0) + until str.empty? + tag = nil + + case str + when /".+?"/, /'.+?'/ + tag = :string + when /#.*$/ + tag = :comment + when RESERVED_WORDS_PATTERN + tag = :reserved + when /[A-Za-z0-9_]+\(|\)/ + tag = :method + when /[A-Z0-9][A-Za-z0-9_]+|false|true/ + tag = :const + end + + if tag + tokenize($~.pre_match, index) do |*args| + yield(*args) + end + + yield(tag, index + $~.begin(0), index + $~.end(0)) + + index += (str.length - $~.post_match.length) + str = $~.post_match + else + index += str.length + str = '' + end + end + end + end + end end