Class: Line

Inherits:
Object show all
Defined in:
lib/xiki/line.rb,
lib/xiki/vim/line.rb

Class Method Summary collapse

Class Method Details

.<(txt) ⇒ Object



317
318
319
320
# File 'lib/xiki/line.rb', line 317

def self.< txt
  self.delete :leave
  View.insert txt
end

.<<(txt, options = {}) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/xiki/line.rb', line 309

def self.<< txt, options={}
  orig = View.cursor
  Keys.clear_prefix
  Move.to_end
  View.insert txt
  View.cursor = orig if options[:dont_move]
end

.=~(regex) ⇒ Object



217
218
219
# File 'lib/xiki/line.rb', line 217

def self.=~ regex
  self.value =~ regex
end

.[](regex, index = 0) ⇒ Object

Text on current line (minus linebreak)



28
29
30
# File 'lib/xiki/line.rb', line 28

def self.[] regex, index=0
  self.value[regex, index]
end

.add_slash(options = {}) ⇒ Object

Add slash to end of line, and optionally a string as well

Line.add_slash Line.add_slash “hi”



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/xiki/line.rb', line 328

def self.add_slash options={}

  line = Line.value

  line =~ /\/$/ || line.blank? ?
    Move.to_end :
    Line << "/"

  txt = options.is_a?(String) ? options : options[:txt]

  orig = View.cursor
  Line << txt if txt
  View.cursor = orig if options[:left]
  nil
end

.at_left?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/xiki/line.rb', line 84

def self.at_left?
  $el.point_at_bol == $el.point
end

.at_right?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/xiki/line.rb', line 72

def self.at_right?
  $el.point_at_eol == $el.point
end

.before_cursorObject



344
345
346
# File 'lib/xiki/line.rb', line 344

def self.before_cursor
  View.txt self.left, View.cursor
end

.beginningObject Also known as: start



144
145
146
147
# File 'lib/xiki/line.rb', line 144

def self.beginning
  $el.forward_line 0
  nil
end

.blank?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/xiki/line.rb', line 76

def self.blank?
  self.matches /^$/
end

.boundsObject



97
98
99
# File 'lib/xiki/line.rb', line 97

def self.bounds
  [$el.point_at_bol, $el.point_at_eol]
end

.delete(leave_linebreak = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/xiki/line.rb', line 101

def self.delete leave_linebreak=nil
  value = self.value
  bol, eol = $el.point_at_bol, $el.point_at_eol
  eol += 1 unless leave_linebreak
  $el.delete_region(bol, eol)
  value
end

.do_lines_sortObject



348
349
350
351
352
353
# File 'lib/xiki/line.rb', line 348

def self.do_lines_sort
  old = $el.elvar.sort_fold_case# rescue true
  $el.elvar.sort_fold_case = true
  $el.sort_lines(nil, $el.region_beginning, $el.region_end)
  $el.elvar.sort_fold_case = old
end

.do_lines_toggleObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/xiki/line.rb', line 355

def self.do_lines_toggle
  prefix = Keys.prefix :clear=>1

  prefix ||= 1   # Default to one line

  if prefix.is_a? Fixnum   # If number, grab that many lines
    line_a = [Line.left, Line.left(1+prefix)]
    line_b = [Line.left(1+prefix), Line.left(1+prefix*2)]
    Effects.glow :fade_out=>1, :what=>line_b
    txt = View.delete *line_b

    View.to line_a[0]
    View.<< txt, :dont_move=>1
    line_a_size = line_a[1] - line_a[0]
    line_b.map! {|o| o - line_a_size}
    Effects.glow :fade_in=>1, :what=>line_b

    return
  end

end

.duplicate_lineObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/xiki/line.rb', line 239

def self.duplicate_line

  prefix = Keys.prefix

  # If in file tree, actually duplicate file
  if prefix == :u && FileTree.handles?
    Location.as_spot
    FileTree.copy_to :prefix=>1
    return
  end

  column = View.column

  line = "#{Line.value}\n"
  Line.to_left
  Code.comment(:line) if prefix == :u
  times = if prefix.nil?
      1
    elsif prefix == :u
      1   # Put commented line after
      # 0   # Put commented line before
    elsif prefix == 0
      0
    elsif prefix > 0
      prefix + 1
    elsif prefix < 0
      prefix
    end

  Line.next times
  View.insert line
  Line.previous

  View.column = column
end

.endObject



153
154
155
# File 'lib/xiki/line.rb', line 153

def self.end
  $el.end_of_line
end

.enter_docsObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/xiki/line.rb', line 393

def self.enter_docs

  line = Line.value

  if line.blank?   # If blank line, prompt for menu
    return Launcher.insert "docs"

  elsif line =~ /^(\w+\.\w+| +([+-] )?\.)/i   # If a method, grab docs from source and insert
    orig = Location.new

    txt = nil
    $el.with(:save_window_excursion) do
      Launcher.as_open

      # Grab comments
      right = View.cursor
      Move.to_previous_paragraph
      txt = View.txt View.cursor, right
      orig.go
    end

    if txt =~ /^ *[^ #]/   # Do nothing if it wasn't one big comment
      return View.flash "- No docs found for this method!"
    end

    txt = txt.gsub /^ *#( )?/, "@\\1"
    Tree.<< txt, :no_slash=>1, :no_search=>1
    return
  end

  Line.add_slash :txt=>"docs/", :unless_blank=>1
  Launcher.launch
end

.gsub!(from, to) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'lib/xiki/line.rb', line 299

def self.gsub! from, to
  orig = Location.new
  value = Line.value
  return unless value.gsub! from, to
  self.delete :leave
  View.insert value
  orig.go :assume_file=>1
  value
end

.indent(line = nil) ⇒ Object



63
64
65
66
# File 'lib/xiki/line.rb', line 63

def self.indent line=nil
  line ||= self.value
  line[/^\s*/].gsub("\t", '        ')
end

.initObject



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/xiki/line.rb', line 377

def self.init

  return if ! $el   # Do nothing if not running under el4r

  # Define lisp function to get list of displayed lines
  # In case something has been done to change them
  $el.el4r_lisp_eval %q`
    (defun xiki-line-number (pos)
      (save-excursion
        (goto-char pos)
        (forward-line 0)
        (1+ (count-lines 1 (point))))
    )
  `
end

.is_bullet?(line = nil) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
229
# File 'lib/xiki/line.rb', line 226

def self.is_bullet? line=nil
  line ||= self.value
  self.matches /^\s*[+-] /, line
end

.label(line = nil) ⇒ Object



192
193
194
195
196
197
# File 'lib/xiki/line.rb', line 192

def self.label line=nil
  line ||= self.value
  # Space or blank can follow colon
  label = line[/^\s*[+-] (.+?): /, 1]
  label ||= line[/^\s*[+-] (.+?):$/, 1]
end

.left(down = 1) ⇒ Object



80
81
82
# File 'lib/xiki/line.rb', line 80

def self.left down=1
  $el.point_at_bol down
end

.matches(o, line = nil) ⇒ Object

Whether line matches pattern



48
49
50
51
# File 'lib/xiki/line.rb', line 48

def self.matches o, line=nil
  line ||= self.value
  line[o]
end


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/xiki/line.rb', line 3

def self.menu
  "
  - api/
    > Moving
    @ Line.previous   # up
    @ Line.next   # down
    |
    > Getting text
    @ p Line.value   # line text
    @ p Line.indent   # indent
    @ p Line.without_indent   # without indent
    @ p Line.label   # label
    @ p Line.without_label   # without label
    |
    > Getting Positions
    @ p Line.left   # line start
    @ p Line.right   # line ending
    |
    > Tests
    @ p Line.blank?   # blank
    @ Line[/s...thing/]   # matches something
  "
end

.move(direction) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/xiki/line.rb', line 275

def self.move direction
  column = View.column
  many = Keys.prefix_times

  many = (0 - many) if direction == :previous

  line = Line.value 1, :include_linebreak=>true, :delete=>true   # Get line
  Line.to_next many
  View.insert line

  Line.previous
  View.column = column
end

.next(times = nil) ⇒ Object

Moves down, going to first column



130
131
132
133
# File 'lib/xiki/line.rb', line 130

def self.next times=nil
  $el.forward_line times
  nil
end

.next_matches(pattern) ⇒ Object

Whether next line matches pattern



59
60
61
# File 'lib/xiki/line.rb', line 59

def self.next_matches pattern
  self.next_value =~ pattern
end

.next_value(n = 2) ⇒ Object

Return value of next line



54
55
56
# File 'lib/xiki/line.rb', line 54

def self.next_value n=2
  #buffer_substring(point_at_bol(n), point_at_eol(n))
end

.numberObject



231
232
233
# File 'lib/xiki/line.rb', line 231

def self.number pos=nil
  $el.xiki_line_number pos || $el.point
end

.previous(times = nil) ⇒ Object

Moves up, going to first column



136
137
138
139
140
141
142
# File 'lib/xiki/line.rb', line 136

def self.previous times=nil
  times = times ?
    -times :
    -1
  $el.forward_line times
  nil
end

.rightObject



68
69
70
# File 'lib/xiki/line.rb', line 68

def self.right
  $el.point_at_eol
end

.sub!(from, to) ⇒ Object



289
290
291
292
293
294
295
296
297
# File 'lib/xiki/line.rb', line 289

def self.sub! from, to
  orig = Location.new
  value = Line.value
  return unless value.sub! from, to
  self.delete :leave
  View.insert value
  orig.go :assume_file=>1
  value
end

.symbol(options = {}) ⇒ Object

Gets symbol at point



110
111
112
113
114
115
116
117
# File 'lib/xiki/line.rb', line 110

def self.symbol options={}
  symbol = $el.thing_at_point(:symbol)
  # Delete if option passed
  if options[:delete]
    $el.delete_region(* $el.bounds_of_thing_at_point(:symbol))
  end
  symbol
end

.to_beginning(options = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/xiki/line.rb', line 174

def self.to_beginning options={}
  down = options[:down]
  prefix = options[:prefix] || Keys.prefix
  down ||= prefix

  # If prefix go down n lines first
  Line.next down if down.is_a? Fixnum

  Line.to_left

  prefix == :u || options[:quote]?
    $el.skip_chars_forward("[^ \t]") :
    $el.skip_chars_forward("[^ \t|]")   # If quoted, skip quote unless :u

  nil
end

.to_blankObject



235
236
237
# File 'lib/xiki/line.rb', line 235

def self.to_blank
  $el.re_search_forward "^[ \t]*$"
end

.to_endObject



161
162
163
# File 'lib/xiki/line.rb', line 161

def self.to_end
  self.to_right
end

.to_leftObject



149
150
151
# File 'lib/xiki/line.rb', line 149

def self.to_left
  self.beginning
end

.to_next(times = nil) ⇒ Object

Moves down, going to first column



120
121
122
# File 'lib/xiki/line.rb', line 120

def self.to_next times=nil
  self.next times
end

.to_previous(times = nil) ⇒ Object

Moves up, going to first column



125
126
127
# File 'lib/xiki/line.rb', line 125

def self.to_previous times=nil
  self.previous times
end

.to_rightObject



157
158
159
# File 'lib/xiki/line.rb', line 157

def self.to_right
  $el.end_of_line
end

.to_startObject



165
166
167
# File 'lib/xiki/line.rb', line 165

def self.to_start
  self.to_left
end

.to_wordsObject



169
170
171
172
# File 'lib/xiki/line.rb', line 169

def self.to_words
  $el.beginning_of_line
  $el.skip_chars_forward "[^ \t]"
end

.txtObject



93
94
95
# File 'lib/xiki/line.rb', line 93

def self.txt
  self.without_indent
end

.valueObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xiki/line.rb', line 32

def self.value n=1, options={}
  eol = "(point-at-eol #{n})"

  # Optionally include linebreak
  eol = "(+ 1 #{eol})" if options[:include_linebreak]

  result = $el.el4r_lisp_eval("(buffer-substring (point-at-bol #{n}) #{eol})")

  if options[:delete]
    $el.el4r_lisp_eval("(delete-region (point-at-bol #{n}) #{eol})")
  end

  result
end

.without_indent(txt = nil) ⇒ Object



88
89
90
91
# File 'lib/xiki/line.rb', line 88

def self.without_indent txt=nil
  txt ||= self.value
  txt.sub(/^\s+/, "")
end

.without_label(options = {}) ⇒ Object Also known as: content



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/xiki/line.rb', line 199

def self.without_label options={}
  line = options.has_key?(:line) ? options[:line] : self.value

  return nil if line.nil?

  # Delete comment (parenthesis)
  line = line.sub /^(\s*)(?:[+-]|<+) [^\n\(]+\) (.*)/, "\\1\\2"

  # If paren at end of line, delete label
  line.sub! /^(\s*)(?:[+-]|<+) [^\n\(]+?\)$/, "\\1"
  # If just bullet
  line.sub! /^(\s*)(?:[+-]|<+) (.+)/, "\\1\\2"
  # Remove whitespace by default
  line.sub!(/^ */, '') unless options[:leave_indent]

  line
end