Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'mong8se/highlight'

I've been using this locally for about a week now without any problems,
so merging to master.

* mong8se/highlight:
  Make matching character highlighting case-insensitive
  doc: Update HISTORY section for match string highlighting
  Added highlighting of entered characters in filenames
  • Loading branch information...
commit bbdcfb2adf54d1fcbb6fbd007ab6e25dab623153 2 parents f11066c + 1c47bcc
@wincent wincent authored
View
3  doc/command-t.txt
@@ -726,6 +726,9 @@ HISTORY *command-t-history*
from Jeff Kreeftmeijer)
- documentation update (patch from Nicholas Alpi)
- added |:CommandTMinHeight| option (patch from Nate Kane)
+- highlight (by underlining) matched characters in the match listing (requires
+ Vim to have been compiled with the +conceal feature, which is available in
+ Vim 7.3 or later; patch from Steven Moazami)
1.3.1 (18 December 2011)
View
56 ruby/command-t/match_window.rb
@@ -26,10 +26,12 @@
module CommandT
class MatchWindow
- @@selection_marker = '> '
- @@marker_length = @@selection_marker.length
- @@unselected_marker = ' ' * @@marker_length
- @@buffer = nil
+ SELECTION_MARKER = '> '
+ MARKER_LENGTH = SELECTION_MARKER.length
+ UNSELECTED_MARKER = ' ' * MARKER_LENGTH
+ MH_START = '<commandt>'
+ MH_END = '</commandt>'
+ @@buffer = nil
def initialize options = {}
@prompt = options[:prompt]
@@ -94,9 +96,21 @@ def initialize options = {}
# syntax coloring
if VIM::has_syntax?
- ::VIM::command "syntax match CommandTSelection \"^#{@@selection_marker}.\\+$\""
+ ::VIM::command "syntax match CommandTSelection \"^#{SELECTION_MARKER}.\\+$\""
::VIM::command 'syntax match CommandTNoEntries "^-- NO MATCHES --$"'
::VIM::command 'syntax match CommandTNoEntries "^-- NO SUCH FILE OR DIRECTORY --$"'
+
+ if VIM::has_conceal?
+ ::VIM::command 'setlocal conceallevel=2'
+ ::VIM::command 'setlocal concealcursor=nvic'
+ ::VIM::command 'syntax region CommandTCharMatched ' \
+ "matchgroup=CommandTCharMatched start=+#{MH_START}+ " \
+ "matchgroup=CommandTCharMatchedEnd end=+#{MH_END}+ concealends"
+ ::VIM::command 'highlight def CommandTCharMatched ' \
+ 'term=bold,underline cterm=bold,underline ' \
+ 'gui=bold,underline'
+ end
+
::VIM::command 'highlight link CommandTSelection Visual'
::VIM::command 'highlight link CommandTNoEntries Error'
::VIM::evaluate 'clearmatches()'
@@ -282,15 +296,37 @@ def restore_window_dimensions
def match_text_for_idx idx
match = truncated_match @matches[idx].to_s
if idx == @selection
- prefix = @@selection_marker
+ prefix = SELECTION_MARKER
suffix = padding_for_selected_match match
else
- prefix = @@unselected_marker
+ if VIM::has_syntax? && VIM::has_conceal?
+ match = match_with_syntax_highlight match
+ end
+ prefix = UNSELECTED_MARKER
suffix = ''
end
prefix + match + suffix
end
+ # Highlight matching characters within the matched string.
+ #
+ # Note that this is only approximate; it will highlight the first matching
+ # instances within the string, which may not actually be the instances that
+ # were used by the matching/scoring algorithm to determine the best score
+ # for the match.
+ #
+ def match_with_syntax_highlight match
+ highlight_chars = @prompt.abbrev.downcase.chars.to_a
+ match.chars.inject([]) do |output, char|
+ if char.downcase == highlight_chars.first
+ highlight_chars.shift
+ output.concat [MH_START, char, MH_END]
+ else
+ output << char
+ end
+ end.join
+ end
+
# Print just the specified match.
def print_match idx
return unless VIM::Window.select(@window)
@@ -331,10 +367,10 @@ def print_matches
# highlighting extends all the way to the right edge of the window.
def padding_for_selected_match str
len = str.length
- if len >= @window_width - @@marker_length
+ if len >= @window_width - MARKER_LENGTH
''
else
- ' ' * (@window_width - @@marker_length - len)
+ ' ' * (@window_width - MARKER_LENGTH - len)
end
end
@@ -342,7 +378,7 @@ def padding_for_selected_match str
# window width.
def truncated_match str
len = str.length
- available_width = @window_width - @@marker_length
+ available_width = @window_width - MARKER_LENGTH
return str if len <= available_width
left = (available_width / 2) - 1
right = (available_width / 2) - 2 + (available_width % 2)
View
4 ruby/command-t/vim.rb
@@ -34,6 +34,10 @@ def self.exists? str
::VIM::evaluate(%{exists("#{str}")}).to_i != 0
end
+ def self.has_conceal?
+ ::VIM::evaluate('has("conceal")').to_i != 0
+ end
+
def self.pwd
::VIM::evaluate 'getcwd()'
end
Please sign in to comment.
Something went wrong with that request. Please try again.