lib/textbringer/modes/programming_mode.rb in textbringer-0.1.8 vs lib/textbringer/modes/programming_mode.rb in textbringer-0.1.9

- old
+ new

@@ -4,19 +4,22 @@ class ProgrammingMode < FundamentalMode # abstract mode undefine_command(:programming_mode) define_generic_command :indent_line - define_generic_command :newline_and_reindent + define_generic_command :reindent_then_newline_and_indent + define_generic_command :indent_region define_generic_command :forward_definition define_generic_command :backward_definition define_generic_command :compile define_generic_command :toggle_test PROGRAMMING_MODE_MAP = Keymap.new PROGRAMMING_MODE_MAP.define_key("\t", :indent_line_command) - PROGRAMMING_MODE_MAP.define_key("\C-m", :newline_and_reindent_command) + PROGRAMMING_MODE_MAP.define_key("\C-m", + :reindent_then_newline_and_indent_command) + PROGRAMMING_MODE_MAP.define_key("\e\C-\\", :indent_region_command) PROGRAMMING_MODE_MAP.define_key("\C-c\C-n", :forward_definition_command) PROGRAMMING_MODE_MAP.define_key("\C-c\C-p", :backward_definition_command) PROGRAMMING_MODE_MAP.define_key("\C-c\C-c", :compile_command) PROGRAMMING_MODE_MAP.define_key("\C-c\C-t", :toggle_test_command) @@ -30,23 +33,21 @@ result = false level = calculate_indentation return result if level.nil? @buffer.save_excursion do @buffer.beginning_of_line - has_space = @buffer.looking_at?(/[ \t]+/) - if has_space - s = @buffer.match_string(0) - break if /\t/ !~ s && s.size == level - @buffer.delete_region(@buffer.match_beginning(0), - @buffer.match_end(0)) - else - break if level == 0 + @buffer.composite_edit do + if @buffer.looking_at?(/[ \t]+/) + s = @buffer.match_string(0) + break if /\t/ !~ s && s.size == level + @buffer.delete_region(@buffer.match_beginning(0), + @buffer.match_end(0)) + else + break if level == 0 + end + @buffer.indent_to(level) end - @buffer.indent_to(level) - if has_space - @buffer.merge_undo(2) - end result = true end pos = @buffer.point @buffer.beginning_of_line @buffer.forward_char while /[ \t]/ =~ @buffer.char_after @@ -54,27 +55,33 @@ @buffer.goto_char(pos) end result end - def newline_and_reindent - n = 1 - if indent_line - n += 1 + def reindent_then_newline_and_indent + @buffer.composite_edit do + indent_line + @buffer.save_excursion do + pos = @buffer.point + @buffer.beginning_of_line + if /\A[ \t]+\z/ =~ @buffer.substring(@buffer.point, pos) + @buffer.delete_region(@buffer.point, pos) + end + end + @buffer.insert("\n") + indent_line end + end + + def indent_region(s = @buffer.mark, e = @buffer.point) + s, e = Buffer.region_boundaries(s, e) @buffer.save_excursion do - pos = @buffer.point - @buffer.beginning_of_line - if /\A[ \t]+\z/ =~ @buffer.substring(@buffer.point, pos) - @buffer.delete_region(@buffer.point, pos) - n += 1 + @buffer.goto_char(s) + while @buffer.point < e + indent_line + @buffer.forward_line end end - @buffer.insert("\n") - if indent_line - n += 1 - end - @buffer.merge_undo(n) if n > 1 end private def calculate_indentation