Class: Location

Inherits:
Object show all
Defined in:
lib/xiki/location.rb

Overview

Saves and restores locations. A location can include multiple of these: a file, a line number, a column number

Constant Summary collapse

@@spots =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Location

Save file and location



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xiki/location.rb', line 26

def initialize *args
  # Just use file if it's passed
  if args[0] && args[0].class == String
    @file = File.expand_path(args.shift)
    return
  end

  if args[0] && args[0].class == Hash
    options = args.shift
    if options[:save_scroll_position]
      @scroll_position = ($el.line_number_at_pos $el.point) - ($el.line_number_at_pos $el.window_start)
    end
  end

  @file = $el.buffer_file_name
  @buffer = View.name
  @line = Line.number
  @column = $el.point - $el.point_at_bol

  # Get buffer if no file
  @buffer = $el.buffer_name unless @file

end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



17
18
19
# File 'lib/xiki/location.rb', line 17

def buffer
  @buffer
end

#columnObject

Returns the value of attribute column.



16
17
18
# File 'lib/xiki/location.rb', line 16

def column
  @column
end

#fileObject

Returns the value of attribute file.



15
16
17
# File 'lib/xiki/location.rb', line 15

def file
  @file
end

#lineObject

Returns the value of attribute line.



14
15
16
# File 'lib/xiki/location.rb', line 14

def line
  @line
end

Class Method Details

.as_spot(key = '0') ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/xiki/location.rb', line 146

def self.as_spot key='0'

  # Remember window (in case buffer in 2 windows)
  @@spot_index = View.index if key == '0'

  if View.file   # If buffer has a file, just save in location
    Location.save(key)
    return @@spots[key] = nil
  end

  # Must be a buffer
  @@spots[key] = [$el.buffer_name(View.buffer), $el.point]
end

.enter_at_spotObject

Enter selected text at spot



138
139
140
141
142
143
144
# File 'lib/xiki/location.rb', line 138

def self.enter_at_spot
  txt = View.selection
  txt = FileTree.snippet if Keys.prefix_u?
  Location.jump("0")
  View.set_mark
  $el.insert txt
end

.go(path = nil, options = {}) ⇒ Object

Use View.open instead of this, where possible Opens path, in other window if already open. If nothing passed, prompt user. If string assume it’s a path. If symbol, assume it’s a bookmark.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xiki/location.rb', line 54

def self.go path=nil, options={}
  # If no param passed, get key from user and go to corresponding location
  if path.nil?
    loc = Keys.input(:optional => true)
    loc ||= "0"
    loc = "_#{loc}"
    View.open("$#{loc}")
    # Optionally go to point
    $el.bookmark_jump(loc) unless $el.elvar.current_prefix_arg
    return

  # If symbol, look up location in map and go to it
  elsif path.class == Symbol
    # @@hash[path.to_s].go
    View.open("$#{path.to_s}")
    $el.bookmark_jump(path.to_s)
    return

  # If string, look up location in map and go to it
  elsif path.class == String and path[/^\$./]
    View.open(path, :goto_point => true)

    # Jump to specific boomkark point - redundant??
    # Is this even a bookmark?
    $el.bookmark_jump(path.sub(/^\$/, ""))
    return
  end

  # Otherwise, go to path passed in
  self.new(path).go(options)
end

.jump(path = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/xiki/location.rb', line 128

def self.jump path=nil
  path ||= "0"
  path = "_#{path}"
  #path = path.to_s unless path.class == String
  View.open("$#{path}")
  # Optionally go to point
  $el.bookmark_jump(path) #unless $el.elvar.current_prefix_arg
end


5
6
7
8
9
10
11
12
# File 'lib/xiki/location.rb', line 5

def self.menu
  %`
  - examples/
    | orig = Location.new   # Save where we are
    | Line.next 4;  View.open '/tmp'   # Go Somewhere else
    | orig.go   # Go back to where we were
  `
end

.save(name = nil) ⇒ Object

Saves a generic location based on user input



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/xiki/location.rb', line 114

def self.save name=nil
  # Use string if user types it quickly (or arg, if passed)
  name ||= Keys.input(:prompt => "Save this spot as (pause when done): ", :optional => true)
  name ||= "0"
  #path = path.to_s unless path.class == String
  name = "_#{name}"
  # Remove beginning $ (it might have been passed in with the arg)
  name.sub!(/^\$/, "")

  # Save location in corresponding register
  # @@hash[name] = Location.new
  $el.bookmark_set(name)
end

.to_spot(key = '0') ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/xiki/location.rb', line 160

def self.to_spot key='0'
  if @@spots[key]
    View.to_buffer @@spots[key][0]
    View.to @@spots[key][1]
  else    # If file, just jump
    # If original window/buffer still there, go back
    if key == '0'
      if $el.buffer_file_name($el.window_buffer(View.nth(@@spot_index))) == Bookmarks["$_0"]
        View.to_nth @@spot_index
      end
    end
    Location.jump(key)
  end
end

Instance Method Details

#file_or_bufferObject



21
22
23
# File 'lib/xiki/location.rb', line 21

def file_or_buffer
  @file || @buffer
end

#go(options = {}) ⇒ Object

Goes to location, with whatever information we have. Note that if file is already shown, we just move to its window.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xiki/location.rb', line 88

def go options={}
  if ! options[:assume_file]
    if @file
      View.open(@file, options)
    else
      View.to_buffer(@buffer)
    end
  end

  $el.goto_line @line if @line

  # Exit if no column is set
  return unless @column

  # If enough space, just move to column
  if $el.point + @column <= $el.point_at_eol
    $el.forward_char @column
  # Otherwise, move to end
  else
    $el.end_of_line
  end

  $el.recenter @scroll_position if @scroll_position
end