Class: View

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

Overview

Represents a division of a window (in emacs terms, it’s a window (which is within a frame))

Constant Summary collapse

@@hash =

Stores things user copies

{}

Class Method Summary collapse

Class Method Details

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



826
827
828
# File 'lib/xiki/view.rb', line 826

def self.<< txt, options={}
  self.insert txt, options
end

.>>(txt) ⇒ Object



1324
1325
1326
# File 'lib/xiki/view.rb', line 1324

def self.>> txt
  View.<< txt, :dont_move=>1
end

.[](n) ⇒ Object



307
308
309
# File 'lib/xiki/view.rb', line 307

def self.[] n
  self.nth n
end

.alert(txt) ⇒ Object



1010
1011
1012
1013
# File 'lib/xiki/view.rb', line 1010

def self.alert txt
  self.beep
  self.message txt
end

.at_bottomObject



696
697
698
# File 'lib/xiki/view.rb', line 696

def self.at_bottom
  self.cursor == self.bottom
end

.balanceObject

Accounts for bar



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/xiki/view.rb', line 407

def self.balance
  $el.balance_windows
  return if Keys.prefix_u
  if self.bar?
    buffer = $el.selected_window
    $el.select_window $el.frame_first_window
    #       enlarge_window (31 - window_width), true
    #       enlarge_window (48 - window_width), true

    $el.enlarge_window((42 - $el.window_width), true)
    #       $el.enlarge_window((22 - $el.window_width), true)

    $el.select_window buffer
  end
end

.barObject

Open the bar



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/xiki/view.rb', line 351

def self.bar
  # If bar already there, just go to it
  if self.bar?
    View.to_nth(0)
    return
  end

  # Remember buffers and heights
  orig = []
  self.list.each do |w|
    orig << [$el.window_buffer(w), $el.window_height(w)]
  end
  $el.delete_other_windows

  $el.split_window_horizontally 48   # Width of bar
  #     $el.split_window_horizontally 28   # Width of bar

  #     split_window_horizontally 32   # Width of bar
  #     split_window_horizontally 54   # Width of bar

  $el.other_window 1
  o = nil
  # For each window but last
  orig[0..-2].each do |w|
    $el.switch_to_buffer w[0]
    $el.split_window_vertically
    $el.set_window_text_height nil, w[1]
    o = $el.other_window 1
  end
  # Last window
  $el.switch_to_buffer orig[-1][0]
  $el.set_window_text_height o, orig[-1][1]
  # Go to first window
  $el.select_window self.first
end

.bar?Boolean

Returns whether bar is open

Returns:

  • (Boolean)


388
389
390
# File 'lib/xiki/view.rb', line 388

def self.bar?
  $el.window_width(self.first) < $el.frame_width
end

.beep(*args) ⇒ Object

def self.beep options={}



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/xiki/view.rb', line 1016

def self.beep *args

  # Pull out 1st arg if string
  txt = args[0].nil? || args[0].is_a?(Hash) ? nil : args.shift.to_s

  options = args[0] || {}
  raise "Too many args" if args.size > 1

  $el.beep
  if times = options[:times]
    (times-1).times do
      View.pause 0.11
      View.beep
    end
  end
  View.flash txt, :times=>6 if txt

  options[:return_txt] ? txt : nil
end

.beginningObject



704
705
706
# File 'lib/xiki/view.rb', line 704

def self.beginning
  $el.point_min
end

.block_positions(regex = "^> ") ⇒ Object

Returns bounds of block in the form [left, after_header, right].



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/xiki/view.rb', line 661

def self.block_positions regex="^> "

  orig = $el.point
  # Go to the end of the line, so if we're at the heading we'll find it
  Line.end
  found = $el.re_search_backward regex, nil, 1
  if found
    left = $el.point
    after_header = Line.left 2
  else
    left = after_header = $el.point
  end
  Line.end
  $el.re_search_forward regex, nil, 1
  right = ($el.point == $el.point_max) ? $el.point_max : Line.left
  $el.goto_char orig

  [left, after_header, right]

end

.bottomObject



712
713
714
# File 'lib/xiki/view.rb', line 712

def self.bottom
  $el.point_max
end

.bufferObject



505
506
507
# File 'lib/xiki/view.rb', line 505

def self.buffer
  $el.window_buffer
end

.buffer_of_nth(n) ⇒ Object



326
327
328
329
330
331
332
# File 'lib/xiki/view.rb', line 326

def self.buffer_of_nth n
  window = View.window
  View.to_nth n
  buffer = $el.window_buffer
  View.to_window window
  buffer
end

.buffer_open?(name) ⇒ Boolean

Returns whether a buffer is open / exists

Returns:

  • (Boolean)


776
777
778
# File 'lib/xiki/view.rb', line 776

def self.buffer_open? name
  Buffers.list.find{|b| $el.buffer_name(b) == name}
end

.buffer_visible?(name) ⇒ Boolean

Returns:

  • (Boolean)


780
781
782
783
784
785
# File 'lib/xiki/view.rb', line 780

def self.buffer_visible? name
  View.list.
    collect {|b| $el.window_buffer b}.
    collect {|u| $el.buffer_name u}.
    member?(name)
end

.charObject



982
983
984
# File 'lib/xiki/view.rb', line 982

def self.char
  $el.buffer_substring($el.point, $el.point+1)
end

.char_beforeObject



986
987
988
# File 'lib/xiki/view.rb', line 986

def self.char_before
  $el.buffer_substring($el.point-1, $el.point)
end

.clear(name = nil) ⇒ Object



716
717
718
719
720
721
722
723
724
725
726
# File 'lib/xiki/view.rb', line 716

def self.clear name=nil
  if name
    if View.buffer_visible?(name)
      View.to_buffer(name)
      View.clear
    end
    return
  end

  $el.erase_buffer
end

.columnObject



938
939
940
# File 'lib/xiki/view.rb', line 938

def self.column
  $el.point - $el.point_at_bol
end

.column=(to) ⇒ Object



942
943
944
945
# File 'lib/xiki/view.rb', line 942

def self.column= to
  Move.to_column to
  nil
end

.count_matchesObject



923
924
925
926
927
928
# File 'lib/xiki/view.rb', line 923

def self.count_matches
  right = $el.buffer_size
  left = 1
  left = $el.point if Keys.prefix_u?
  $el.message how_many(Keys.input('pattern to count: a'), left, right).to_s
end

.create(prefix = nil) ⇒ Object

Creates a new window by splitting the current one



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/xiki/view.rb', line 165

def self.create prefix=nil
  prefix ||= Keys.prefix
  if prefix == 3   # If prefix is 3, do Vertical split
    $el.split_window_horizontally
    View.next
  elsif prefix == 4
    $el.split_window_vertically
    Keys.clear_prefix
    View.next
    View.enlarge
  elsif prefix == :u
    $el.split_window_vertically
  else
    $el.split_window_vertically
    View.next
  end
end

.currentObject

Return currently-selected window



510
511
512
# File 'lib/xiki/view.rb', line 510

def self.current
  $el.selected_window
end

.cursorObject



947
948
949
# File 'lib/xiki/view.rb', line 947

def self.cursor
  $el.point
end

.cursor=(n) ⇒ Object



950
951
952
# File 'lib/xiki/view.rb', line 950

def self.cursor= n
  $el.goto_char n
end

.delete(left = nil, right = nil) ⇒ Object

Delete string between two points, and returns deleted text.

p View.delete 1, 10 p View.delete :line p View.delete # Delete’s selection (and returns it)



971
972
973
974
975
976
977
978
979
980
# File 'lib/xiki/view.rb', line 971

def self.delete left=nil, right=nil
  return Line.delete if left == :line

  # Default to deleting region
  left, right = View.range if left.nil?

  txt = $el.buffer_substring left, right
  $el.delete_region left, right
  txt
end

.dimensions_fullObject

Toggle full-screen mode



1110
1111
1112
1113
1114
1115
1116
# File 'lib/xiki/view.rb', line 1110

def self.dimensions_full
  if $el.frame_parameter(nil, :fullscreen)   # If fullscreen on turn it off
    $el.set_frame_parameter(nil, :fullscreen, nil)
  else   # Else, turn it on
    self.fullscreen_on
  end
end

.dimensions_set(size_x, size_y, position_x = nil, position_y = nil) ⇒ Object



1102
1103
1104
1105
1106
1107
# File 'lib/xiki/view.rb', line 1102

def self.dimensions_set size_x, size_y, position_x=nil, position_y=nil
  self.fullscreen_off
  $el.set_frame_size(View.frame, size_x, size_y)
  $el.set_frame_position(View.frame, position_x, position_y) unless position_x.nil?
  nil
end

.dir(force_slash = nil) ⇒ Object



728
729
730
731
732
733
734
735
736
737
# File 'lib/xiki/view.rb', line 728

def self.dir force_slash=nil
  # TODO: merge with .path?
  result = File.expand_path($el.elvar.default_directory)

  if force_slash
    return result =~ /\/$/ ? result : "#{result}/"
  end

  result
end

.dir=(to) ⇒ Object



739
740
741
# File 'lib/xiki/view.rb', line 739

def self.dir= to
  $el.elvar.default_directory = File.expand_path(to)+"/"
end

.dir_of_after_barObject



342
343
344
345
346
347
348
# File 'lib/xiki/view.rb', line 342

def self.dir_of_after_bar
  window = View.window
  View.to_after_bar
  dir = View.dir
  View.to_window window
  dir
end

.dir_of_nth(n) ⇒ Object



334
335
336
337
338
339
340
# File 'lib/xiki/view.rb', line 334

def self.dir_of_nth n
  window = View.window
  View.to_nth n
  dir = View.dir
  View.to_window window
  dir
end

.edges(view = nil) ⇒ Object



1178
1179
1180
1181
# File 'lib/xiki/view.rb', line 1178

def self.edges view=nil
  view ||= self.current
  $el.window_edges(view).to_a
end

.empty?Boolean

Returns:

  • (Boolean)


844
845
846
# File 'lib/xiki/view.rb', line 844

def self.empty?
  $el.point_min == $el.point_max
end

.enlarge(height = nil) ⇒ Object



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
# File 'lib/xiki/view.rb', line 106

def self.enlarge height=nil
  default_height = 4
  small = Keys.prefix || height || default_height
  small = default_height if small == :u

  small += 1

  # If universal prefix and in bar, widen bar
  self.balance if Keys.prefix_u and View.bar?

  ws = self.windows_in_my_column

  wnum = ws.length   # Get number of windows

  usable_height = $el.frame_height - 1 - wnum

  biggest = usable_height - ((wnum-1) * (small-1))
  selected = $el.selected_window


  # Remember borders, and get rid of external border temporarily (it messes up heights)
  $el.elvar.xiki_modeline_box_tmp = Styles.attribute "mode-line", "box"
  $el.elvar.xiki_modeline_inactive_box_tmp = Styles.attribute "mode-line-inactive", "box"
  Styles.mode_line_inactive :border=>:nil
  Styles.mode_line :border=>:nil

  # Maybe have to redraw here?!
  View.refresh

  # Set border to 0

  self.enlarge_internal :up, ws, selected, biggest, small

  # Restore borders
  $el.set_face_attribute :mode_line, nil, ':box'.to_sym, $el.elvar.xiki_modeline_box_tmp
  $el.set_face_attribute :mode_line_inactive, nil, ':box'.to_sym, $el.elvar.xiki_modeline_inactive_box_tmp

  nil
end

.enlarge_internal(direction, ws, selected, biggest, small) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/xiki/view.rb', line 146

def self.enlarge_internal direction, ws, selected, biggest, small
  ws = ws.reverse if direction != :up

  3.times do
    ws.each_with_index do |w, i|
      # If current window, set to remaining
      if w == selected
        height = biggest
        $el.set_window_text_height w, height
      else  # Other small windows
        height = small - 1
        $el.set_window_text_height w, height
      end
    end
  end

end

.enter_dateObject



1328
1329
1330
1331
1332
# File 'lib/xiki/view.rb', line 1328

def self.enter_date
  $el.insert $el.elvar.current_prefix_arg ?
    Time.now.strftime("%Y-%m-%d %I:%M:%S%p").sub(' 0', ' ').downcase :
    Time.now.strftime("%Y-%m-%d")
end

.enter_upperObject



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/xiki/view.rb', line 1287

def self.enter_upper
  prefix = Keys.prefix :clear=>true
  orig = Location.new

  View.layout_todo :no_blink=>true
  todo_orig = Location.new
  View.to_highest

  View.line = prefix if prefix.is_a? Fixnum

  line = Line.value
  if prefix == :u
    Line.delete

    orig.line -= 1 if Bookmarks['$t'] == orig.file   # If in $t, adjust position by how much is deleted
  end

  todo_orig.go
  orig.go

  View.insert line
end

.expand_path(path) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/xiki/view.rb', line 1087

def self.expand_path path
  path = "#{View.dir}/#{path}" if path !~ /^[\/~]/

  # Expand ~

  had_slash = path =~ /\/$/   # Check whether / at end

  # This cleans up /./ nad /../ in paths as side-effect of above
  path = File.expand_path path

  path = "#{path}/" if had_slash && path !~ /\/$/   # Put / back at end, if it was there (and not there now)

  path
end

.extensionObject



748
749
750
# File 'lib/xiki/view.rb', line 748

def self.extension
  View.file[/\.(\w+)$/, 1]
end

.fileObject



743
744
745
746
# File 'lib/xiki/view.rb', line 743

def self.file
  file = $el.buffer_file_name
  file ? File.expand_path(file) : nil
end

.file_nameObject



756
757
758
759
760
# File 'lib/xiki/view.rb', line 756

def self.file_name
  $el.buffer_file_name ?
    $el.file_name_nondirectory($el.buffer_file_name) :
    nil
end

.file_of_nth(n) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/xiki/view.rb', line 318

def self.file_of_nth n
  orig = View.window
  View.to_nth n
  file = View.file
  View.to_window orig
  file
end

.file_or_bufferObject



752
753
754
# File 'lib/xiki/view.rb', line 752

def self.file_or_buffer
  self.file || self.name
end

.files(options = {}) ⇒ Object



274
275
276
277
278
279
280
281
282
283
# File 'lib/xiki/view.rb', line 274

def self.files options={}
  if options[:visible]
    return $el.window_list.
      map {|b| $el.window_buffer b}.
      collect {|u| $el.buffer_file_name u}.
      select {|f| f}
  end

  Buffers.list.map { |b| $el.buffer_file_name(b) }.select{|path| path}
end

.firstObject



398
399
400
# File 'lib/xiki/view.rb', line 398

def self.first
  $el.frame_first_window
end

.flash(message = nil, options = {}) ⇒ Object

Makes message temporarily appear and glow, then go away, like:

foo/

- Success!


1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
# File 'lib/xiki/view.rb', line 1391

def self.flash message=nil, options={}

  was_modified = $el.buffer_modified_p

  File.open("/tmp/flashes.log", "a") { |f| f << "#{message}\n" } if message

  message ||= "- Success!"

  orig = self.cursor
  indent = Line.indent

  blank_line = Line[/^$/]

  Line.next unless blank_line
  message = "#{message.strip}"
  message << "\n" unless blank_line
  message.gsub! /^/, "#{indent}  "
  self.insert message, :dont_move=>1
  left, right = Line.left, Line.left+message.length
  self.cursor = orig

  Effects.glow({:what=>[left, right], :reverse=>1, :times=>2}.merge(options))

  self.delete left, right

  self.cursor = orig

  $el.not_modified if ! was_modified

  nil
end

.flashesObject



1423
1424
1425
1426
# File 'lib/xiki/view.rb', line 1423

def self.flashes
  txt = IO.readlines File.expand_path("/tmp/flashes.log") rescue return "- No messages flashed yet!"
  txt = txt.reverse.uniq.join
end

.focusObject



915
916
917
# File 'lib/xiki/view.rb', line 915

def self.focus
  $el.x_focus_frame($el.selected_frame)
end

.frameObject



767
768
769
# File 'lib/xiki/view.rb', line 767

def self.frame
  $el.window_frame($el.frame_first_window)
end

.frame_height(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/xiki/view.rb', line 75

def self.frame_height options={}
  return $el.frame_parameter(nil, :height) if options.empty?
  return unless options.is_a?(Hash)

  if options[:add]
    self.frame_height = self.frame_height + 1
  end

  nil
end

.frame_height=(chars) ⇒ Object



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

def self.frame_height= chars
  $el.set_frame_parameter nil, :height, chars
end

.frame_width(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/xiki/view.rb', line 86

def self.frame_width options={}
  return $el.frame_parameter(nil, :width) if options.empty?
  return unless options.is_a?(Hash)

  if options[:add]
    self.frame_width = self.frame_width + 1
  end

  nil
end

.frame_width=(chars) ⇒ Object



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

def self.frame_width= chars
  $el.set_frame_parameter nil, :width, chars
end

.fullscreen_offObject



1118
1119
1120
# File 'lib/xiki/view.rb', line 1118

def self.fullscreen_off
  $el.set_frame_parameter(nil, :fullscreen, nil) if $el.frame_parameter(nil, :fullscreen)
end

.fullscreen_onObject



1122
1123
1124
# File 'lib/xiki/view.rb', line 1122

def self.fullscreen_on
  $el.set_frame_parameter nil, :fullscreen, :fullboth
end

.gsub!(from, to) ⇒ Object



1280
1281
1282
1283
1284
1285
# File 'lib/xiki/view.rb', line 1280

def self.gsub! from, to
  $el.with(:save_excursion) do
    View.to_highest
    $el.replace_regexp(from, to)
  end
end

.handle_barObject



541
542
543
# File 'lib/xiki/view.rb', line 541

def self.handle_bar
  self.to_after_bar if self.in_bar?
end

.heightObject

Make current window larger. Take into account that there might be other vertical windows



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

def self.height
  $el.window_height
end

.hidden?Boolean

Returns true if anything is hidden

Returns:

  • (Boolean)


424
425
426
# File 'lib/xiki/view.rb', line 424

def self.hidden?
  $el.point_min != 1 || $el.point_max != $el.buffer_size+1
end

.hideObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/xiki/view.rb', line 428

def self.hide
  Keys.prefix_times.times do
    left = View.left_edge

    # If there's one above me and before me
    index = View.index
    middle = false
    size = View.list.size
    if index > 0 && index < (size - 1)   # Check existance
      if( left == View.left_edge(View.list[index - 1]) &&
        left == View.left_edge(View.list[index + 1]) )  # Check alignment
        middle = true
      end
    end
    # If I'm the last
    last = index == (size - 1)

    $el.delete_window
    $el.previous_multiframe_window if View.left_edge != left || middle || last
  end
  nil
end

.hide_others(options = {}) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/xiki/view.rb', line 451

def self.hide_others options={}
  if $el.elvar.current_prefix_arg || self.in_bar? || options[:all]
    return $el.delete_other_windows
  end
  ws = self.windows_in_my_column
  selected = $el.selected_window
  # New height should be window minus 2 for each window
  ws.each do |w|
    # If current window, set to remaining
    unless w == selected
      $el.delete_window w
    end
  end
end

.in_bar?Boolean

Returns whether we’re in the bar

Returns:

  • (Boolean)


393
394
395
396
# File 'lib/xiki/view.rb', line 393

def self.in_bar?
  self.bar? &&  # Bar is open
    View.edges[0] == 0  # Window is at left of frame
end

.indexObject



919
920
921
# File 'lib/xiki/view.rb', line 919

def self.index
  View.list.index(View.window)
end

.initObject



1040
1041
1042
1043
# File 'lib/xiki/view.rb', line 1040

def self.init
  @@dimension_options ||= []   # Set to empty if not set yet
  $el.winner_mode 1 rescue nil
end

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



807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/xiki/view.rb', line 807

def self.insert txt, options={}
  if options[:utf8]
    File.open("/tmp/tmp.txt", "w") {|f| f << txt}
    orig = $el.elvar.coding_system_for_read   # Read file as utf-8
    $el.elvar.coding_system_for_read = 'utf-8'.to_sym
    $el.insert_file_contents "/tmp/tmp.txt"

    # breaks when utf8 - doesn't go far enaugh??!
    # Maybe get length of file instead of string?
    # Or, set encoding of buffer to utf8?

    $el.elvar.coding_system_for_read = orig
    Move.forward txt.size unless options[:dont_move]   # .insert_file_contents leaves cursor at beginning
  else
    $el.insert txt
    Move.backward txt.size if options[:dont_move]
  end
end

.insert_lineObject



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/xiki/view.rb', line 1131

def self.insert_line

  orig_indent = Line.indent
  n = Keys.prefix   # Check for numeric prefix

  if n == 0
    n = nil
    Line.previous
  end

  Move.to_end if Line.before_cursor =~ /^ +$/   # If at awkward position, move

  indent_txt = n ?
    (" " * n) :
    Line[/^[ |$~&%@\/\\#+!-]+/] || ""   # No numeric prefix, so just grab this line's opening indent text

  Deletes.delete_whitespace if ! Line.at_left? && ! Line.at_right?

  Line.to_right if Line.at_left?

  View.insert "\n#{indent_txt}"
end

.killObject



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'lib/xiki/view.rb', line 1066

def self.kill

  # To avoid "Buffer has a running process; keep the buffer? (y or n)" message
  if process = $el.get_buffer_process(View.buffer)
    $el.set_process_query_on_exit_flag process, nil
  end

  $el.kill_this_buffer
  nil
end

.kill_allObject



1077
1078
1079
# File 'lib/xiki/view.rb', line 1077

def self.kill_all
  $el.erase_buffer
end

.kill_paragraphObject



1081
1082
1083
1084
1085
# File 'lib/xiki/view.rb', line 1081

def self.kill_paragraph
  left, right = View.paragraph(:bounds => true)
  Effects.blink(:left=>left, :right=>right)
  View.delete(left, right)
end

.lastObject



402
403
404
# File 'lib/xiki/view.rb', line 402

def self.last
  View.list[-1]
end

.layout_files(options = {}) ⇒ Object



1214
1215
1216
1217
1218
# File 'lib/xiki/view.rb', line 1214

def self.layout_files options={}
  FileTree.open_in_bar
  View.to_nth 1
  Effects.blink(:what=>:line) unless options[:no_blink]
end

.layout_output(options = {}) ⇒ Object



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/xiki/view.rb', line 1220

def self.layout_output options={}
  # If current line has Ol., grab line and path

  prefix = Keys.prefix

  if options[:called_by_launch]
    prefix = nil
  end

  View.layout_files if ! View.bar?   # If not already showing bar, do it first, so it shows up on left

  found = prefix != :u && ! options[:dont_highlight] && Line =~ /^ *Ol\b/ && OlHelper.source_to_output(View.file, Line.number)

  orig = nil
  if prefix == :-   # If want to keep cursor where it is
    return View.flash("- Not found!") if ! found
    orig = Location.new
  end

  Code.open_log_view options

  if found
    if found >= (Line.number(View.bottom) - 1)   # If too far back to be shown
      found = nil
      View.flash("- Not found!", :times=>1)
    else
      View.to_bottom
      Line.previous(found+1)
    end
  end

  if found
    Color.colorize :l
    Effects.blink(:what=>:line)
  end

  if orig
    return orig.go
  end

end

.layout_right(nth = nil) ⇒ Object



1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/xiki/view.rb', line 1183

def self.layout_right nth=nil
  nth ||= Keys.prefix
  if nth   # If numeric prefix, go to nth
    down = nth - 1
    self.to_after_bar
    # If there's only one column (last view is at left), go to top
    Move.to_window(1) if self.edges[0] == 0
    down.times { self.next }
    Effects.blink(:what=>:line)# if options[:blink]
    return
  end

  current = View.name

  second_visible = Buffers.list.each{|b|
    name = $el.buffer_name(b)
    next unless name != current
    next unless self.buffer_visible?(name)
    next unless self.edges(get_buffer_window(b))[0] != 0  # Window is at left of frame
    break name   # Found
  }
  self.to_buffer second_visible
  Effects.blink(:what=>:line)# if options[:blink]

end

.layout_todo(options = {}) ⇒ Object



1209
1210
1211
1212
# File 'lib/xiki/view.rb', line 1209

def self.layout_todo options={}
  FileTree.open_in_bar
  Effects.blink(:what=>:line) unless options[:no_blink]
end

.left_edge(view = nil) ⇒ Object



571
572
573
574
# File 'lib/xiki/view.rb', line 571

def self.left_edge view=nil
  view ||= $el.selected_window  # Default to current view
  self.edges(view)[0]
end

.lengthObject



1491
1492
1493
# File 'lib/xiki/view.rb', line 1491

def self.length
  $el.buffer_size
end

.lineObject



930
931
932
# File 'lib/xiki/view.rb', line 930

def self.line
  Line.number
end

.line=(n = nil) ⇒ Object



902
903
904
# File 'lib/xiki/view.rb', line 902

def self.line= n=nil
  self.to_line n
end

.line_numberObject



934
935
936
# File 'lib/xiki/view.rb', line 934

def self.line_number
  Line.number
end

.listObject

Return list of windows



266
267
268
# File 'lib/xiki/view.rb', line 266

def self.list
  $el.window_list(nil, nil, $el.frame_first_window).to_a
end

.list_namesObject



270
271
272
# File 'lib/xiki/view.rb', line 270

def self.list_names
  self.list.map{|v| $el.buffer_name $el.window_buffer(v)}
end

.mark=(pos = nil) ⇒ Object



803
804
805
# File 'lib/xiki/view.rb', line 803

def self.mark= pos=nil
  self.set_mark pos
end


7
8
9
10
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
51
52
# File 'lib/xiki/view.rb', line 7

def self.menu
  %`
  << @window/
  - .flashes/
  - api/
    > Summary
    | View class is the catch-all class for dealing with editing text.
    | It has methods for inserting text and grabbing text, and quite a few
    | other things.

    > Text
    @View << 'Hello'    # Inserts into the view.
    @p View.txt         # Return all the text.
    @p View.txt 1, 5    # Return text in a range.
    @p View.cursor      # Return where the cursor is.
    @p View.line        # Returns the line number the cursor is on.

    > Files and dirs
    @p View.name                  # Returns file name.
    @p View.file                  # Returns file with whole path.
    @p View.dir                   # Returns dir of file.
    @View.open "/tmp/"            # Opens file in the first view, (switching to it if it's already open)
    @View.open nil, :txt=>"Hi"    # Show message in new buffer.

    > Messages
    @View.flash                 # Shows temporary message inline.
    @View.flash 'Saved'
    @View.message 'Hi there'    # Shows message at bottom.
    @View.prompt                # Prompt user to type at end af line.
    @View.prompt 'Type here'
    @View.beep                  # Makes a noise.

    > Advanced
    @View.<< "hey", :dont_move=>1    # Without moving cursor

    > Also see
    << line/

  - docs/
    > Summary
    | Menus to deal with the layout, etc.

    - Keys/
      | layout+create - TODO should we just refer to a @layout menu for these?
  `
end

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



1003
1004
1005
1006
1007
1008
# File 'lib/xiki/view.rb', line 1003

def self.message txt, options={}
  $el.message txt
  View.beep if options[:beep]

  nil
end

.modeObject



1036
1037
1038
# File 'lib/xiki/view.rb', line 1036

def self.mode
  $el.elvar.major_mode.to_s.gsub('-','_').to_sym
end

.modified?Boolean

Returns:

  • (Boolean)


1380
1381
1382
# File 'lib/xiki/view.rb', line 1380

def self.modified?
  $el.buffer_modified_p
end

.nameObject



999
1000
1001
# File 'lib/xiki/view.rb', line 999

def self.name
  $el.buffer_name
end

.next(options = {}) ⇒ Object



466
467
468
469
470
471
# File 'lib/xiki/view.rb', line 466

def self.next options={}
  (Keys.prefix_times || options[:times] || 1).times do
    $el.other_window 1
  end
  Effects.blink(:what=>:line) if options[:blink]
end

.nth(n) ⇒ Object



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

def self.nth n
  orig = View.window
  View.to_nth n
  win = View.window
  View.to_window orig
  win
end

.open(path, options = {}) ⇒ Object

Opens file (or whatever) from the path (can contain $bookmarks), just moving to or exposing its view if it’s already open. By default it will open in 2nd view if we’re in the bar view.

View.open “/etc/paths” View.open “hello”, :txt=>“> HinMessage to show in new buffer.”



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/xiki/view.rb', line 199

def self.open path, options={}

  return self.show_txt(options[:txt], :name=>path) if options[:txt]

  # Pull off line number if there
  path.sub!(/(.+?:\d+).*/, "\\1")
  line_number = path.slice!(/:\d+$/)

  # Open after bar if in bar
  if View.in_bar? && (! options[:stay_in_bar]) && path != "$0" && path != Bookmarks['$t'] && path != Bookmarks['$f']
    View.to_after_bar
  end

  # Expand $bookmark strings at beginning
  expanded = Bookmarks.expand(path)
  if expanded == ""   # If nothing there, return false
    path.sub!(/^- /, '')
    if path =~ /^\$\w+$/
      buffer = Bookmarks.buffer_bookmark path.sub(/^\$/, '')
      View.to_buffer buffer if buffer
    end
    return nil
  end

  if expanded   # Handle opening in other window
    if options[:same_view]
      $el.find_file expanded
    elsif expanded == $el.buffer_file_name   # If already there
      # do nothing
    elsif ( ( $el.window_list.collect {|b| $el.window_buffer b} ).collect {|u| $el.buffer_file_name u} ).member?(expanded)   # If already displayed, move to its window
      $el.find_file_other_window expanded
    else   # If not visible, just open it
      $el.find_file expanded
    end
  end

  # Jump to point if :goto_point (we assume path is just a bookmark)
  if options[:go_to_point] == true
    $el.bookmark_jump path.sub(/^\$/, "")
  end

  if line_number
    View.to_line line_number[/\d+/]
  end
end

.open_in_barObject



519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/xiki/view.rb', line 519

def self.open_in_bar
  # Remember buffer
  buffer = self.buffer

  # If already open, just go there
  if View.bar?
    $el.select_window(View.first)
  else
    View.bar
  end
  View.to_buffer buffer
end

.open_in_rightObject



532
533
534
535
536
537
538
539
# File 'lib/xiki/view.rb', line 532

def self.open_in_right
  # Remember buffer
  buffer = self.buffer

  View.bar unless View.bar?  # If not open yet, open it
  # Go to after bar
  View.to_after_bar
end

.paragraph(options = {}) ⇒ Object



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/xiki/view.rb', line 1045

def self.paragraph options={}
  orig = Location.new
  found = Search.backward "^$", :go_anyway=>true
  Line.next if found
  left = Line.left
  Search.forward "^$", :go_anyway=>true
  right = Line.right
  orig.go
  left = Line.left if options[:start_here]

  return [left, right] if options[:bounds]
  txt = View.txt(left, right)
  View.delete(left, right) if options[:delete]
  return [left, right] if options[:bounds]
  txt
end

.path(options = {}) ⇒ Object



762
763
764
765
# File 'lib/xiki/view.rb', line 762

def self.path options={}
  # TODO: merge with .dir?
  $el.elvar.default_directory
end

.pause(n = 0.5) ⇒ Object

wait / sleep / sit



1432
1433
1434
# File 'lib/xiki/view.rb', line 1432

def self.pause n=0.5   # wait / sleep / sit
  $el.sit_for n
end

.previous(options = {}) ⇒ Object



473
474
475
476
477
478
# File 'lib/xiki/view.rb', line 473

def self.previous options={}
  (Keys.prefix_times || options[:times] || 1).times do
    $el.other_window -1
  end
  Effects.blink(:what=>:line) if options[:blink]
end

.prompt(message = "Type something here", options = {}) ⇒ Object

Makes message glow at end of line, and adds “/”, like

foo/Type something here View.prompt @ View.prompt/



1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/xiki/view.rb', line 1341

def self.prompt message="Type something here", options={}
  ControlLock.disable

  if ! Line.blank?
    Move.to_end
    View << "/" unless Line =~ /\/$/
  end

  self.insert(message, :dont_move=>1)

  left, right = self.cursor, Line.right
  Effects.glow({:what=>[left, right], :reverse=>1}.merge(options))
  self.delete left, right


  return unless options[:timed]

  View.<< message, :dont_move=>1

  $el.elvar.inhibit_quit = true

  # Wait for first, then loop until pause
  c = $el.read_char("")
  key = Keys.to_letter c
  View.delete View.cursor, Line.right   # Delete temporary message
  View << key

  while(c = $el.read_char("", nil, 0.35))
    key = Keys.to_letter c
    if c == 7   # C-g?
      Cursor.restore :before_input
      $el.elvar.inhibit_quit = nil
      $el.keyboard_quit
    end
    View << key
  end
  $el.elvar.inhibit_quit = nil
end

.rangeObject



493
494
495
# File 'lib/xiki/view.rb', line 493

def self.range
  [$el.region_beginning, $el.region_end]
end

.range_leftObject



497
498
499
# File 'lib/xiki/view.rb', line 497

def self.range_left
  $el.region_beginning
end

.range_rightObject



501
502
503
# File 'lib/xiki/view.rb', line 501

def self.range_right
  $el.region_end
end

.recenter(n = nil) ⇒ Object



848
849
850
851
852
853
854
855
856
857
858
# File 'lib/xiki/view.rb', line 848

def self.recenter n=nil
  n ||= Keys.prefix

  return View.recenter_under "^\\( *def \\| *it \\|^>\\)", :relative=>1 if n == :u

  if n == :uu
    return View.recenter_under "^\\( *def \\| *it \\|^>\\)", :relative=>1, :incude_comments=>1
  end

  $el.recenter n
end

.recenter_topObject



890
891
892
# File 'lib/xiki/view.rb', line 890

def self.recenter_top
  $el.recenter 0
end

.recenter_under(pattern, options = {}) ⇒ Object

Scrolls window so top line matches pattern (first match above).

View.recenter_under “^ *def ”



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/xiki/view.rb', line 865

def self.recenter_under pattern, options={}
  orig = Location.new
  Line.next
  Search.backward pattern

  # If :relative, search backward from here...

  if options[:relative]
    Move.to_previous_paragraph if options[:incude_comments] && Line.value(0) =~ /^ *#/
    line = View.line
    View.recenter_top
    orig.go
    difference = View.line - line
    View.recenter -2 if difference > (View.height-3)
    return
  end

  # Search from top...

  View.to_highest
  Search.forward pattern
  Line.to_beginning
  View.recenter_top
end

.refreshObject

redraw



1428
1429
1430
# File 'lib/xiki/view.rb', line 1428

def self.refresh   # redraw
  $el.sit_for 0
end

.restObject



894
895
896
# File 'lib/xiki/view.rb', line 894

def self.rest
  $el.buffer_substring($el.point, $el.point_max)
end

.restore(name = nil) ⇒ Object

Saves the configuration



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/xiki/view.rb', line 253

def self.restore name=nil
  name ||= Keys.input(:optional => true)   # Get single key from user if no param
  name ||= "0"   # Set to "0" if user entered nothing
  # Todo: if "l", winner_undo
  if(name == "l")
    $el.winner_undo
    return
  end
  # Use it to get configuration out of hash
  $el.set_window_configuration(@@hash[name])
end

.save(name = nil) ⇒ Object

Saves the configuration



246
247
248
249
250
# File 'lib/xiki/view.rb', line 246

def self.save name=nil
  name ||= Keys.input(:optional => true)
  name ||= "0"
  @@hash[name] = $el.current_window_configuration
end

.screen_height(options = {}) ⇒ Object



101
102
103
# File 'lib/xiki/view.rb', line 101

def self.screen_height options={}
  $el.x_display_pixel_height
end

.screen_width(options = {}) ⇒ Object



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

def self.screen_width options={}
  $el.x_display_pixel_width
end

.scroll_barsObject



995
996
997
# File 'lib/xiki/view.rb', line 995

def self.scroll_bars
  $el.frame_parameter(nil, :vertical_scroll_bars) ? true : nil
end

.scroll_bars=(on) ⇒ Object



991
992
993
# File 'lib/xiki/view.rb', line 991

def self.scroll_bars= on
  $el.toggle_scroll_bar on ? 1 : 0
end

.scroll_positionObject



1310
1311
1312
# File 'lib/xiki/view.rb', line 1310

def self.scroll_position
  $el.line_number_at_pos($el.point) - $el.line_number_at_pos($el.window_start)
end

.scroll_position=(pos) ⇒ Object



1314
1315
1316
# File 'lib/xiki/view.rb', line 1314

def self.scroll_position= pos
  $el.recenter pos
end

.selection(options = {}) ⇒ Object

Return selected text (aka the “region”)



487
488
489
490
491
# File 'lib/xiki/view.rb', line 487

def self.selection options={}
  txt = $el.buffer_substring($el.region_beginning, $el.region_end)
  $el.delete_region($el.point, $el.mark) if options[:delete]
  txt
end

.sensible_defaultsObject

Call this at startup to set some sensible view-related default behavior



792
793
794
795
796
# File 'lib/xiki/view.rb', line 792

def self.sensible_defaults
  $el.el4r_lisp_eval("(progn (setq truncate-partial-width-windows nil)
    (set 'default-truncate-lines t)
    )")
end

.set_mark(pos = nil) ⇒ Object



798
799
800
801
# File 'lib/xiki/view.rb', line 798

def self.set_mark pos=nil
  pos ||= self.cursor
  $el.set_mark pos
end

.shiftObject



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/xiki/view.rb', line 1154

def self.shift
  times = Keys.prefix_times;  Keys.clear_prefix

  choice = Keys.input :chars=>1, :prompt=>"Move this view to: (n)ext, (p)revious"
  Effects.blink
  times.times do

    buffer_a =  View.buffer

    # Go to other and switch to A
    choice == "n" ? View.next : View.previous
    buffer_b = View.buffer
    $el.switch_to_buffer buffer_a

    # Go to first and switch to B
    choice == "n" ? View.previous : View.next
    $el.switch_to_buffer buffer_b

    choice == "n" ? View.next : View.previous

  end
  Effects.blink
end

.show_dirObject



480
481
482
483
484
# File 'lib/xiki/view.rb', line 480

def self.show_dir
  ($el.elvar.current_prefix_arg || 1).times do
    $el.dired_jump
  end
end

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



183
184
185
186
187
188
189
# File 'lib/xiki/view.rb', line 183

def self.show_txt txt, options={}
  View.to_buffer(options[:name] || "message")
  View.kill_all
  Notes.mode
  View << "#{txt.strip}\n"
  View.to_highest
end

.split(options = {}) ⇒ Object



1262
1263
1264
1265
1266
# File 'lib/xiki/view.rb', line 1262

def self.split options={}
  options[:horizontally] ?
    $el.split_window_horizontally :
    $el.split_window_vertically
end

.startObject

Line at top of visible part of view



1127
1128
1129
# File 'lib/xiki/view.rb', line 1127

def self.start
  $el.window_start
end

.status(val = nil, options = {}) ⇒ Object

Set small string in mode line.

View.status “aa” View.status nil View.status “bb”, :nth=>2 View.status “dotsies”, :nth=>3 View.status :scale=>5 View.status :scale=>1 View.status :scale=>0 View.status :bars=>[2, 5] # Show 2 bars



1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
# File 'lib/xiki/view.rb', line 1448

def self.status val=nil, options={}

  if !options && ! val || val == :docs   # If nothing passed, show help message
    return "
      > Examples
      @View.status 'aa'
      @View.status :scale=>5
      @View.status 'abc', :nth=>3
      "
  end

  options = val if val.is_a? Hash

  nth = options[:nth] || 1

  # If :bars=>[n, n]...

  if bars = options[:bars]
    nth = 3

    a, b = bars
    if a < b
      common, remainder, remainder_char = a, b - a, 'i'
    else
      common, remainder, remainder_char = b, a - b, 'f'
    end

    val = " #{"z" * common}#{remainder_char * remainder}"
  end

  if scale = options[:scale]
    nth = 3
    val = {0=>' ', 1=>'e', 2=>'i', 3=>'v', 4=>'ø', 5=>'ß'}[scale]
  end

  key = "xiki_status#{nth}".to_sym

  $el.make_local_variable key
  $el.elvar[key] = val

  nil
end

.to(n) ⇒ Object



911
912
913
# File 'lib/xiki/view.rb', line 911

def self.to n
  $el.goto_char n
end

.to_after_barObject



556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/xiki/view.rb', line 556

def self.to_after_bar
  return unless self.bar?

  # Get width of last window
  width_of_last = $el.window_width(self.last)

  # Go to first window not on left margin
  self.list.each do |w|
    if self.edges(w)[0] != 0  # Window is at left of frame
      $el.select_window(w)
      break
    end
  end
end

.to_bottomObject



692
693
694
# File 'lib/xiki/view.rb', line 692

def self.to_bottom
  $el.end_of_buffer
end

.to_buffer(name, options = {}) ⇒ Object

Switches to a buffer



582
583
584
585
586
587
588
589
590
591
592
# File 'lib/xiki/view.rb', line 582

def self.to_buffer name, options={}
  return if $el.buffer_name == name   # If we're here already, do nothing

  found = View.list.find do |w|
    $el.buffer_name($el.window_buffer(w)) == name
  end
  found ? View.to_window(found) : $el.switch_to_buffer(name)

  View.clear if options[:clear]
  View.dir = options[:dir] if options[:dir]
end

.to_endObject



700
701
702
# File 'lib/xiki/view.rb', line 700

def self.to_end
  $el.end_of_buffer
end

.to_highestObject



682
683
684
685
686
# File 'lib/xiki/view.rb', line 682

def self.to_highest
  prefix = Keys.prefix
  return self.to_line(prefix) if(prefix)   # If prefix, go to that line
  self.to_top
end

.to_line(n = nil) ⇒ Object



898
899
900
# File 'lib/xiki/view.rb', line 898

def self.to_line n=nil
  Move.to_line n
end

.to_line_with_prefix(first = "") ⇒ Object



906
907
908
909
# File 'lib/xiki/view.rb', line 906

def self.to_line_with_prefix first=""
  line = "#{first}#{Keys.input(:prompt=>"goto line: #{first}")}"
  View.to_line line
end

.to_nth(n) ⇒ Object



297
298
299
300
301
302
303
304
305
# File 'lib/xiki/view.rb', line 297

def self.to_nth n
  # If greater than size of windows, open last
  #insert self.list.size.to_s
  if n+1 > self.list.size
    return $el.select_window(self.list.last)
  end
  # Otherwise, open nth
  $el.select_window(self.list[n])
end

.to_nth_paragraph(n) ⇒ Object

Move to nth window



286
287
288
289
290
291
292
293
294
295
# File 'lib/xiki/view.rb', line 286

def self.to_nth_paragraph n
  View.to_relative

  if Line.blank? && ! Line.value(2).blank?   # Weird case when just one blank line at top
    n -= 1
    Line.next if n == 0
  end

  n.times { Move.to_next_paragraph }
end

.to_relativeObject



1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
# File 'lib/xiki/view.rb', line 1268

def self.to_relative
  if Keys.prefix == 0
    $el.goto_char window_end - 1
    Line.to_left
    return
  end
  $el.goto_char $el.window_start
  ((Keys.prefix || 1) -1).times do
    Line.next
  end
end

.to_topObject



688
689
690
# File 'lib/xiki/view.rb', line 688

def self.to_top
  $el.beginning_of_buffer
end

.to_upper(options = {}) ⇒ Object



545
546
547
548
549
550
551
552
553
554
# File 'lib/xiki/view.rb', line 545

def self.to_upper options={}
  down = Keys.prefix_times - 1
  Keys.clear_prefix
  View.to_after_bar

  # If there's only one column (last view is at left), go to top
  Move.to_window(1) if self.edges[0] == 0
  down.times { View.next }
  Effects.blink(:what=>:line) if options[:blink]
end

.to_window(window) ⇒ Object

Move to window



515
516
517
# File 'lib/xiki/view.rb', line 515

def self.to_window window
  $el.select_window(window)
end

.topObject



708
709
710
# File 'lib/xiki/view.rb', line 708

def self.top
  $el.point_min
end

.top_edge(view = nil) ⇒ Object



576
577
578
579
# File 'lib/xiki/view.rb', line 576

def self.top_edge view=nil
  view ||= $el.selected_window  # Default to current view
  self.edges(view)[1]
end

.txt(options = {}, right = nil) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/xiki/view.rb', line 594

def self.txt options={}, right=nil

  options.is_a?(Hash) && buffer = options[:buffer] and return Buffers.txt(buffer)

  # If 2nd arg is there, we were passed right,left
  if right
    left = options
  else
    left = options[:left] || $el.point_min
    right = options[:right] || $el.point_max
  end

  # If :utf8 option, write to file via elisp and read via ruby (for correct encoding)
  if options.is_a?(Hash) && options[:utf8]
    # Write to file via elisp
    $el.write_region left, right, "/tmp/utf.txt", nil

    # Read via ruby (so correct encoding is obtained)
    return File.read("/tmp/utf.txt")
  end

  $el.buffer_substring left, right
end

.txt_per_prefix(prefix = nil, options = {}) ⇒ Object

Returns text from view according to prefix…

  • 3 means 3 lines, etc.

  • no prefix means the notes block

  • etc



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/xiki/view.rb', line 622

def self.txt_per_prefix prefix=nil, options={}
  prefix ||= Keys.prefix(:clear=>1)

  prefix = prefix.abs if prefix.is_a?(Fixnum)
  left, right = [nil, nil]

  case prefix
  when 0   # Do paragraph
    left, right = View.paragraph(:bounds=>true)
  when 1..6   # Should probably catch all numeric prefix?
    left = Line.left
    right = $el.point_at_bol(prefix+1)
  when :-
    left, right = View.range if options[:selection]
  end

  # If no prefixes
  if left == nil
    if options[:default_is_line]
      left, right = [Line.left, Line.right]
    else
      ignore, left, right = View.block_positions("^>")
    end
  end

  Effects.blink(:left=>left, :right=>right) if options[:blink]
  txt = options[:just_positions] ? nil : View.txt(left, right)

  if options[:remove_heading] && txt =~ /^>/
    txt.sub! /.+\n/, ''
    # left won't be fixed, but who cares, for now
  end

  return txt if options[:just_txt]

  return [txt, left, right]
end

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



1318
1319
1320
1321
1322
# File 'lib/xiki/view.rb', line 1318

def self.under txt, options={}
  options[:escape] = '' if options[:escape].nil?
  txt = CodeTree.returned_to_s txt
  Tree.under txt, options  # .merge(:escape=>'')
end

.unindent(txt) ⇒ Object



830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'lib/xiki/view.rb', line 830

def self.unindent txt

  # Trim off optional first line
  txt = txt.sub /^\n/, ''
  # Get indent of first line
  indent = txt[/\A +/]
  # Delete this much indent on other lines
  txt.gsub! /^#{indent}/, ''
  # Delete empty space at end
  txt.sub!(/ +\z/, '')

  $el.insert txt
end

.url(txt) ⇒ Object



1062
1063
1064
# File 'lib/xiki/view.rb', line 1062

def self.url txt
  $el.browse_url txt
end

.visible_line_numberObject



955
956
957
# File 'lib/xiki/view.rb', line 955

def self.visible_line_number
  Line.number - Line.number($el.window_start) + 1
end

.visible_line_number=(num) ⇒ Object



959
960
961
962
# File 'lib/xiki/view.rb', line 959

def self.visible_line_number= num
  line = Line.number($el.window_start) + num - 1
  View.line = line
end

.windowObject



771
772
773
# File 'lib/xiki/view.rb', line 771

def self.window
  $el.selected_window
end

.windows_in_my_columnObject



57
58
59
60
# File 'lib/xiki/view.rb', line 57

def self.windows_in_my_column
  my_left = left_edge
  $el.window_list(nil, nil, $el.frame_first_window).to_a.select { |w| left_edge(w) == my_left }
end

.wrap(on_or_off = :on) ⇒ Object



787
788
789
# File 'lib/xiki/view.rb', line 787

def self.wrap on_or_off=:on
  $el.elvar.truncate_lines = on_or_off.to_sym == :off
end