Class: EasyJump

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/easy_jump.rb

Overview

Similar feature as Vim EasyMotion github.com/easymotion/vim-easymotion

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEasyJump



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vimamsa/easy_jump.rb', line 11

def initialize()
  visible_range = get_visible_area()
  visible_text = buf[visible_range[0]..visible_range[1]]
  wsmarks = scan_word_start_marks(visible_text)
  line_starts = scan_indexes(visible_text, /^/)
  lsh = Hash[line_starts.collect { |x| [x, true] }]
  wsmh = Hash[wsmarks.collect { |x| [x, true] }]

  # Exclude work starts that are too close to start of line
  wsmarks.select! { |x|
    r = true
    r = false if lsh[x] or lsh[x - 1] or lsh[x - 2]
    r
  }

  # Exclude those word start positions that are too close to each other
  wsmarks.sort!
  wsm2 = [wsmarks[0]]
  for i in 1..(wsmarks.size - 1)
    if (wsmarks[i] - wsm2[-1]) >= 4 or visible_text[wsm2[-1]..wsmarks[i]].include?("\n")

      wsm2 << wsmarks[i]
    end
  end
  wsmarks = wsm2

  linestart_buf = (line_starts).collect { |x| x + visible_range[0] }
  wsmarks_buf = (wsmarks).collect { |x| x + visible_range[0] }

  # All line starts should be accessible with just two key presses, so put them first in order
  # Other word start positions ordered by distance from current pos
  wsmarks_buf.sort_by! { |x| (x - buf.pos).abs }
  @easy_jump_wsmarks = linestart_buf + wsmarks_buf

  @jump_sequence = make_jump_sequence(@easy_jump_wsmarks.size)

  vma.kbd.set_keyhandling_override(self.method(:easy_jump_input_char))
  @easy_jump_input = ""
  easy_jump_draw
end

Class Method Details

.startObject

def self.initialize() make_jump_sequence end



7
8
9
# File 'lib/vimamsa/easy_jump.rb', line 7

def self.start()
  @@cur = EasyJump.new
end

Instance Method Details

#easy_jump_drawObject



76
77
78
79
80
81
82
# File 'lib/vimamsa/easy_jump.rb', line 76

def easy_jump_draw()
  vma.gui.start_overlay_draw
  for i in 0..(@easy_jump_wsmarks.size - 1)
    vma.gui.overlay_draw_text(@jump_sequence[i], @easy_jump_wsmarks[i]) 
  end
  vma.gui.end_overlay_draw
end

#easy_jump_input_char(c, event_type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vimamsa/easy_jump.rb', line 52

def easy_jump_input_char(c, event_type)
  return true if event_type != :key_press
  # vma.paint_stack = []
  debug "EASY JUMP: easy_jump_input_char [#{c}]"
  @easy_jump_input << c.upcase
  if @jump_sequence.include?(@easy_jump_input)
    jshash = Hash[@jump_sequence.map.with_index.to_a]
    nthword = jshash[@easy_jump_input]
    debug "nthword:#{nthword} #{[@easy_jump_wsmarks[nthword], @jump_sequence[nthword]]}"
    buf.set_pos(@easy_jump_wsmarks[nthword])
    # @kbd.set_mode(:command)
    vma.kbd.remove_keyhandling_override
    @jump_sequence = []
    vma.gui.clear_overlay()
  end
  if @easy_jump_input.size > 2
    # @kbd.set_mode(:command)
    vma.kbd.remove_keyhandling_override
    @jump_sequence = []
    vma.gui.clear_overlay()
  end
  return true
end

#make_jump_sequence(num_items) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vimamsa/easy_jump.rb', line 84

def make_jump_sequence(num_items)
  left_hand = "asdfvgbqwertzxc123".upcase.split("")
  right_hand = "jklhnnmyuiop890".upcase.split("")

  sequence = []
  left_hand_fast = "asdf".upcase.split("")
  right_hand_fast = "jkl;".upcase.split("")

  left_hand_slow = "wergc".upcase.split("") # v
  right_hand_slow = "uiophnm,".upcase.split("")

  left_hand_slow2 = "tzx23".upcase.split("")
  right_hand_slow2 = "yb9'".upcase.split("")

  # Rmoved characters that can be mixed: O0Q, 8B, I1, VY

  left_fast_slow = Array.new(left_hand_fast).concat(left_hand_slow)
  right_fast_slow = Array.new(right_hand_fast).concat(right_hand_slow)

  left_hand_all = Array.new(left_hand_fast).concat(left_hand_slow).concat(left_hand_slow2)
  right_hand_all = Array.new(right_hand_fast).concat(right_hand_slow).concat(right_hand_slow2)

  left_hand_fast.each { |x|
    left_hand_fast.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  right_hand_fast.each { |x|
    right_hand_fast.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  right_hand_fast.each { |x|
    left_hand_fast.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  left_hand_fast.each { |x|
    right_hand_fast.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  left_hand_slow.each { |x|
    right_fast_slow.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  right_hand_slow.each { |x|
    left_fast_slow.each { |y|
      sequence << "#{x}#{y}"
    }
  }

  left_hand_slow2.each { |x|
    right_hand_all.each { |y|
      left_hand_all.each { |z|
        sequence << "#{x}#{y}#{z}"
      }
    }
  }

  right_hand_slow2.each { |x|
    left_hand_all.each { |y|
      right_hand_all.each { |z|
        sequence << "#{x}#{y}#{z}"
      }
    }
  }

  return sequence
end