Module: Amun::Behaviours::Movement

Included in:
Emacs
Defined in:
lib/amun/behaviours/movement.rb

Overview

Movement behaviour between lines, characters and paragraphs along with emacs keymap for it

Instance Method Summary collapse

Instance Method Details

#backward_charObject



32
33
34
35
# File 'lib/amun/behaviours/movement.rb', line 32

def backward_char(*)
  buffer.point -= 1
  true
end

#beginning_of_lineObject



64
65
66
67
68
69
70
71
# File 'lib/amun/behaviours/movement.rb', line 64

def beginning_of_line(*)
  point = buffer.point
  return true if point == buffer.size

  line_start = buffer.rindex(/^/, point)
  buffer.point = line_start <= point ? line_start : 0
  true
end

#end_of_lineObject



73
74
75
76
77
78
79
80
# File 'lib/amun/behaviours/movement.rb', line 73

def end_of_line(*)
  point = buffer.point
  return true if buffer[point] == "\n"

  line_end = buffer.index("\n", point)
  buffer.point = line_end || buffer.length
  true
end

#forward_charObject



27
28
29
30
# File 'lib/amun/behaviours/movement.rb', line 27

def forward_char(*)
  buffer.point += 1
  true
end

#movement_keymap_initializeObject

attach the movement events only



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/amun/behaviours/movement.rb', line 7

def movement_keymap_initialize
  bind "\C-f", self, :forward_char
  bind Curses::KEY_RIGHT, self, :forward_char

  bind "\C-b", self, :backward_char
  bind Curses::KEY_LEFT, self, :backward_char

  bind "\C-n", self, :next_line
  bind Curses::KEY_DOWN, self, :next_line

  bind "\C-p", self, :previous_line
  bind Curses::KEY_UP, self, :previous_line

  bind "\C-a", self, :beginning_of_line
  bind Curses::KEY_HOME, self, :beginning_of_line

  bind "\C-e", self, :end_of_line
  bind Curses::KEY_END, self, :end_of_line
end

#next_lineObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/amun/behaviours/movement.rb', line 37

def next_line(*)
  point = buffer.point

  line_begin = buffer.rindex(/^/, point)

  line_end = buffer.index(/$/, point)
  return true if line_end == buffer.size

  next_line_end = buffer.index(/$/, line_end + 1)
  point_offset = point - line_begin
  buffer.point = [line_end + 1 + point_offset, next_line_end].min
  true
end

#previous_lineObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/amun/behaviours/movement.rb', line 51

def previous_line(*)
  point = buffer.point

  line_begin = point == buffer.size && buffer[point - 1] == "\n" ? point : buffer.rindex(/^/, point)
  return true if line_begin.zero?

  previous_line_begin = buffer.rindex(/^/, line_begin - 1)

  point_offset = point - line_begin
  buffer.point = [previous_line_begin + point_offset, line_begin - 1].min
  true
end