diakonos 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -346,7 +346,10 @@ class Buffer
346
346
  end
347
347
 
348
348
  def selection_mark
349
- return @text_marks[ SELECTION ]
349
+ @text_marks[ SELECTION ]
350
+ end
351
+ def selecting?
352
+ !!selection_mark
350
353
  end
351
354
 
352
355
  def select_current_line
@@ -360,6 +363,11 @@ class Buffer
360
363
  @lines[ @last_row ]
361
364
  end
362
365
 
366
+ def select_all
367
+ anchorSelection( 0, 0, DONT_DISPLAY )
368
+ cursorTo( @lines.length - 1, @lines[ -1 ].length, DO_DISPLAY )
369
+ end
370
+
363
371
  def select( from_regexp, to_regexp, include_ending = true )
364
372
  start_row = nil
365
373
 
@@ -721,7 +729,7 @@ class Buffer
721
729
  paste str, @indent_closers
722
730
  cursorTo r, c
723
731
  if /%_/ === str
724
- find( [ /%_/ ], :down, '', CHOICE_YES_AND_STOP )
732
+ find( [ /%_/ ], :direction => :down, :replacement => '', :auto_choice => CHOICE_YES_AND_STOP )
725
733
  end
726
734
  else
727
735
  @diakonos.log h[ :regexp ].inspect + " does not match '#{line}'"
@@ -730,19 +738,104 @@ class Buffer
730
738
  end
731
739
 
732
740
  def collapseWhitespace
733
- removeSelection( DONT_DISPLAY ) if selection_mark != nil
741
+ if selection_mark
742
+ removeSelection DONT_DISPLAY
743
+ end
734
744
 
735
- line = @lines[ @last_row ]
736
- head = line[ 0...@last_col ]
737
- tail = line[ @last_col..-1 ]
738
- new_head = head.sub( /\s+$/, '' )
739
- new_line = new_head + tail.sub( /^\s+/, ' ' )
740
- if new_line != line
741
- takeSnapshot( TYPING )
742
- @lines[ @last_row ] = new_line
743
- cursorTo( @last_row, @last_col - ( head.length - new_head.length ) )
744
- setModified
745
+ line = @lines[ @last_row ]
746
+ head = line[ 0...@last_col ]
747
+ tail = line[ @last_col..-1 ]
748
+ new_head = head.sub( /\s+$/, '' )
749
+ new_line = new_head + tail.sub( /^\s+/, ' ' )
750
+ if new_line != line
751
+ takeSnapshot( TYPING )
752
+ @lines[ @last_row ] = new_line
753
+ cursorTo( @last_row, @last_col - ( head.length - new_head.length ) )
754
+ setModified
755
+ end
756
+ end
757
+
758
+ def selected_lines
759
+ selection = selection_mark
760
+ if selection
761
+ if selection.end_col == 0
762
+ end_row = selection.end_row - 1
763
+ else
764
+ end_row = selection.end_row
765
+ end
766
+ @lines[ selection.start_row..end_row ]
767
+ else
768
+ [ @lines[ @last_row ] ]
769
+ end
770
+ end
771
+
772
+ def columnize( delimiter = /=>?|:|,/, num_spaces_padding = 1 )
773
+ takeSnapshot
774
+
775
+ lines = selected_lines
776
+ column_width = 0
777
+ lines.each do |line|
778
+ pos = ( line =~ delimiter )
779
+ if pos
780
+ column_width = [ pos, column_width ].max
781
+ end
782
+ end
783
+
784
+ padding = ' ' * num_spaces_padding
785
+ one_modified = false
786
+
787
+ lines.each do |line|
788
+ old_line = line.dup
789
+ if line =~ /^(.+?)(#{delimiter.source})(.*)$/
790
+ pre = $1
791
+ del = $2
792
+ post = $3
793
+ if pre !~ /\s$/
794
+ del = " #{del}"
795
+ end
796
+ if post !~ /^\s/
797
+ del = "#{del} "
798
+ end
799
+ del.sub!( /^\s+/, ' ' * num_spaces_padding )
800
+ del.sub!( /\s+$/, ' ' * num_spaces_padding )
801
+ line.replace( ( "%-#{column_width}s" % pre ) + del + post )
745
802
  end
803
+ one_modified ||= ( line != old_line )
804
+ end
805
+
806
+ if one_modified
807
+ setModified
808
+ end
809
+ end
810
+
811
+ def comment_out
812
+ takeSnapshot
813
+ one_modified = false
814
+ selected_lines.each do |line|
815
+ old_line = line.dup
816
+ line.gsub!( /^(\s*)/, "\\1" + @settings[ "lang.#{@language}.comment_string" ].to_s )
817
+ line << @settings[ "lang.#{@language}.comment_close_string" ].to_s
818
+ one_modified ||= ( line != old_line )
819
+ end
820
+ if one_modified
821
+ setModified
822
+ end
823
+ end
824
+
825
+ def uncomment
826
+ takeSnapshot
827
+ comment_string = Regexp.escape( @settings[ "lang.#{@language}.comment_string" ].to_s )
828
+ comment_close_string = Regexp.escape( @settings[ "lang.#{@language}.comment_close_string" ].to_s )
829
+ one_modified = false
830
+ selected_lines.each do |line|
831
+ old_line = line.dup
832
+ line.gsub!( /^(\s*)#{comment_string}/, "\\1" )
833
+ line.gsub!( /#{comment_close_string}$/, '' )
834
+ one_modified ||= ( line != old_line )
835
+ end
836
+ if one_modified
837
+ setModified
838
+ end
746
839
  end
747
840
 
748
841
  def deleteLine
@@ -761,7 +854,7 @@ class Buffer
761
854
  cursorTo( row, 0 )
762
855
  setModified
763
856
 
764
- return retval
857
+ retval
765
858
  end
766
859
 
767
860
  def deleteToEOL
@@ -800,11 +893,15 @@ class Buffer
800
893
  def lineAt( y )
801
894
  row = @top_line + y
802
895
  if row < 0
803
- return nil
896
+ nil
804
897
  else
805
- return @lines[ row ]
898
+ @lines[ row ]
806
899
  end
807
900
  end
901
+
902
+ def current_line
903
+ @lines[ @last_row ]
904
+ end
808
905
 
809
906
  # Returns true iff the given column, x, is less than the length of the given line, y.
810
907
  def inLine( x, y )
@@ -838,11 +935,11 @@ class Buffer
838
935
  end
839
936
 
840
937
  def currentRow
841
- return @last_row
938
+ @last_row
842
939
  end
843
940
 
844
941
  def currentColumn
845
- return @last_col
942
+ @last_col
846
943
  end
847
944
 
848
945
  # Returns the amount the view was actually panned.
@@ -1318,11 +1415,17 @@ class Buffer
1318
1415
  # split across newline characters. Once the first element is found,
1319
1416
  # each successive element must match against lines following the first
1320
1417
  # element.
1321
- def find( regexps, direction = :down, replacement = nil, auto_choice = nil )
1418
+ def find( regexps, options = {} )
1322
1419
  return if regexps.nil?
1323
1420
  regexp = regexps[ 0 ]
1324
1421
  return if regexp == nil or regexp == //
1325
1422
 
1423
+ direction = options[ :direction ]
1424
+ replacement = options[ :replacement ]
1425
+ auto_choice = options[ :auto_choice ]
1426
+ from_row = options[ :starting_row ] || @last_row
1427
+ from_col = options[ :starting_col ] || @last_col
1428
+
1326
1429
  if direction == :opposite
1327
1430
  case @last_search_direction
1328
1431
  when :up
@@ -1343,10 +1446,10 @@ class Buffer
1343
1446
  if direction == :down
1344
1447
  # Check the current row first.
1345
1448
 
1346
- if index = @lines[ @last_row ].index( regexp, ( @last_finding ? @last_finding.start_col : @last_col ) + 1 )
1449
+ if index = @lines[ from_row ].index( regexp, ( @last_finding ? @last_finding.start_col : from_col ) + 1 )
1347
1450
  match = Regexp.last_match
1348
1451
  found_text = match[ 0 ]
1349
- finding = Finding.new( @last_row, index, @last_row, index + found_text.length )
1452
+ finding = Finding.new( from_row, index, from_row, index + found_text.length )
1350
1453
  if finding.match( regexps, @lines )
1351
1454
  throw :found
1352
1455
  else
@@ -1356,7 +1459,7 @@ class Buffer
1356
1459
 
1357
1460
  # Check below the cursor.
1358
1461
 
1359
- ( (@last_row + 1)...@lines.length ).each do |i|
1462
+ ( (from_row + 1)...@lines.length ).each do |i|
1360
1463
  if index = @lines[ i ].index( regexp )
1361
1464
  match = Regexp.last_match
1362
1465
  found_text = match[ 0 ]
@@ -1373,7 +1476,7 @@ class Buffer
1373
1476
 
1374
1477
  wrapped = true
1375
1478
 
1376
- ( 0...@last_row ).each do |i|
1479
+ ( 0...from_row ).each do |i|
1377
1480
  if index = @lines[ i ].index( regexp )
1378
1481
  match = Regexp.last_match
1379
1482
  found_text = match[ 0 ]
@@ -1388,12 +1491,12 @@ class Buffer
1388
1491
 
1389
1492
  # And finally, the other side of the current row.
1390
1493
 
1391
- #if index = @lines[ @last_row ].index( regexp, ( @last_finding ? @last_finding.start_col : @last_col ) - 1 )
1392
- if index = @lines[ @last_row ].index( regexp )
1393
- if index <= ( @last_finding ? @last_finding.start_col : @last_col )
1494
+ #if index = @lines[ from_row ].index( regexp, ( @last_finding ? @last_finding.start_col : from_col ) - 1 )
1495
+ if index = @lines[ from_row ].index( regexp )
1496
+ if index <= ( @last_finding ? @last_finding.start_col : from_col )
1394
1497
  match = Regexp.last_match
1395
1498
  found_text = match[ 0 ]
1396
- finding = Finding.new( @last_row, index, @last_row, index + found_text.length )
1499
+ finding = Finding.new( from_row, index, from_row, index + found_text.length )
1397
1500
  if finding.match( regexps, @lines )
1398
1501
  throw :found
1399
1502
  else
@@ -1405,11 +1508,11 @@ class Buffer
1405
1508
  elsif direction == :up
1406
1509
  # Check the current row first.
1407
1510
 
1408
- col_to_check = ( @last_finding ? @last_finding.end_col : @last_col ) - 1
1409
- if ( col_to_check >= 0 ) and ( index = @lines[ @last_row ][ 0...col_to_check ].rindex( regexp ) )
1511
+ col_to_check = ( @last_finding ? @last_finding.end_col : from_col ) - 1
1512
+ if ( col_to_check >= 0 ) and ( index = @lines[ from_row ][ 0...col_to_check ].rindex( regexp ) )
1410
1513
  match = Regexp.last_match
1411
1514
  found_text = match[ 0 ]
1412
- finding = Finding.new( @last_row, index, @last_row, index + found_text.length )
1515
+ finding = Finding.new( from_row, index, from_row, index + found_text.length )
1413
1516
  if finding.match( regexps, @lines )
1414
1517
  throw :found
1415
1518
  else
@@ -1419,7 +1522,7 @@ class Buffer
1419
1522
 
1420
1523
  # Check above the cursor.
1421
1524
 
1422
- (@last_row - 1).downto( 0 ) do |i|
1525
+ (from_row - 1).downto( 0 ) do |i|
1423
1526
  if index = @lines[ i ].rindex( regexp )
1424
1527
  match = Regexp.last_match
1425
1528
  found_text = match[ 0 ]
@@ -1436,7 +1539,7 @@ class Buffer
1436
1539
 
1437
1540
  wrapped = true
1438
1541
 
1439
- (@lines.length - 1).downto(@last_row + 1) do |i|
1542
+ (@lines.length - 1).downto(from_row + 1) do |i|
1440
1543
  if index = @lines[ i ].rindex( regexp )
1441
1544
  match = Regexp.last_match
1442
1545
  found_text = match[ 0 ]
@@ -1451,12 +1554,12 @@ class Buffer
1451
1554
 
1452
1555
  # And finally, the other side of the current row.
1453
1556
 
1454
- search_col = ( @last_finding ? @last_finding.start_col : @last_col ) + 1
1455
- if index = @lines[ @last_row ].rindex( regexp )
1557
+ search_col = ( @last_finding ? @last_finding.start_col : from_col ) + 1
1558
+ if index = @lines[ from_row ].rindex( regexp )
1456
1559
  if index > search_col
1457
1560
  match = Regexp.last_match
1458
1561
  found_text = match[ 0 ]
1459
- finding = Finding.new( @last_row, index, @last_row, index + found_text.length )
1562
+ finding = Finding.new( from_row, index, from_row, index + found_text.length )
1460
1563
  if finding.match( regexps, @lines )
1461
1564
  throw :found
1462
1565
  else
@@ -1468,7 +1571,9 @@ class Buffer
1468
1571
  end
1469
1572
 
1470
1573
  if finding
1471
- @diakonos.setILine( "(search wrapped around BOF/EOF)" ) if wrapped
1574
+ if wrapped and not options[ :quiet ]
1575
+ @diakonos.setILine( "(search wrapped around BOF/EOF)" )
1576
+ end
1472
1577
 
1473
1578
  removeSelection( DONT_DISPLAY )
1474
1579
  @last_finding = finding
@@ -1510,11 +1615,11 @@ class Buffer
1510
1615
  case choice
1511
1616
  when CHOICE_YES
1512
1617
  paste [ actual_replacement ]
1513
- find( regexps, direction, replacement )
1618
+ find( regexps, :direction => direction, :replacement => replacement )
1514
1619
  when CHOICE_ALL
1515
1620
  replaceAll( regexp, replacement )
1516
1621
  when CHOICE_NO
1517
- find( regexps, direction, replacement )
1622
+ find( regexps, :direction => direction, :replacement => replacement )
1518
1623
  when CHOICE_CANCEL
1519
1624
  # Do nothing further.
1520
1625
  when CHOICE_YES_AND_STOP
@@ -1523,7 +1628,11 @@ class Buffer
1523
1628
  end
1524
1629
  end
1525
1630
  else
1631
+ removeSelection DONT_DISPLAY
1632
+ clearMatches DO_DISPLAY
1633
+ if not options[ :quiet ]
1526
1634
  @diakonos.setILine "/#{regexp.source}/ not found."
1635
+ end
1527
1636
  end
1528
1637
  end
1529
1638
 
@@ -1559,10 +1668,12 @@ class Buffer
1559
1668
  end
1560
1669
 
1561
1670
  def findAgain( last_search_regexps, direction = @last_search_direction )
1562
- if @last_search_regexps == nil
1563
- @last_search_regexps = last_search_regexps
1671
+ if @last_search_regexps.nil?
1672
+ @last_search_regexps = last_search_regexps
1673
+ end
1674
+ if @last_search_regexps
1675
+ find( @last_search_regexps, :direction => direction )
1564
1676
  end
1565
- find( @last_search_regexps, direction ) if( @last_search_regexps != nil )
1566
1677
  end
1567
1678
 
1568
1679
  def seek( regexp, direction = :down )
@@ -1717,38 +1828,70 @@ class Buffer
1717
1828
  end
1718
1829
 
1719
1830
  def undo
1720
- if @current_buffer_state < @buffer_states.length - 1
1721
- @current_buffer_state += 1
1722
- @lines = @buffer_states[ @current_buffer_state ]
1723
- cursorTo( @cursor_states[ @current_buffer_state - 1 ][ 0 ], @cursor_states[ @current_buffer_state - 1 ][ 1 ] )
1724
- @diakonos.setILine "Undo level: #{@current_buffer_state} of #{@buffer_states.length - 1}"
1725
- setModified
1726
- end
1831
+ if @current_buffer_state < @buffer_states.length - 1
1832
+ @current_buffer_state += 1
1833
+ @lines = @buffer_states[ @current_buffer_state ]
1834
+ cursorTo( @cursor_states[ @current_buffer_state - 1 ][ 0 ], @cursor_states[ @current_buffer_state - 1 ][ 1 ] )
1835
+ @diakonos.setILine "Undo level: #{@current_buffer_state} of #{@buffer_states.length - 1}"
1836
+ setModified
1837
+ end
1727
1838
  end
1728
1839
 
1729
1840
  # Since redo is a Ruby keyword...
1730
1841
  def unundo
1731
- if @current_buffer_state > 0
1732
- @current_buffer_state += -1
1733
- @lines = @buffer_states[ @current_buffer_state ]
1734
- cursorTo( @cursor_states[ @current_buffer_state ][ 0 ], @cursor_states[ @current_buffer_state ][ 1 ] )
1735
- @diakonos.setILine "Undo level: #{@current_buffer_state} of #{@buffer_states.length - 1}"
1736
- setModified
1842
+ if @current_buffer_state > 0
1843
+ @current_buffer_state += -1
1844
+ @lines = @buffer_states[ @current_buffer_state ]
1845
+ cursorTo( @cursor_states[ @current_buffer_state ][ 0 ], @cursor_states[ @current_buffer_state ][ 1 ] )
1846
+ @diakonos.setILine "Undo level: #{@current_buffer_state} of #{@buffer_states.length - 1}"
1847
+ setModified
1848
+ end
1849
+ end
1850
+
1851
+ def wrap_paragraph
1852
+ start_row = end_row = @last_row
1853
+ until start_row == 0 || @lines[ start_row - 1 ].strip == ''
1854
+ start_row -= 1
1855
+ end
1856
+ until end_row == @lines.size || @lines[ end_row ].strip == ''
1857
+ end_row += 1
1858
+ end
1859
+
1860
+ lines = []
1861
+ line = ''
1862
+ words = @lines[ start_row...end_row ].join( ' ' ).scan( /\S+/ )
1863
+ words.each do |word|
1864
+ if word =~ /^[a-z']+[.!?]$/
1865
+ word = "#{word} "
1866
+ end
1867
+ if line.length + word.length + 1 > ( @settings[ "lang.#{@language}.wrap_margin" ] || 80 )
1868
+ lines << line.strip
1869
+ line = ''
1737
1870
  end
1871
+ line << " #{word}"
1872
+ end
1873
+ line.strip!
1874
+ if not line.empty?
1875
+ lines << line
1876
+ end
1877
+ if @lines[ start_row...end_row ] != lines
1878
+ @lines[ start_row...end_row ] = lines
1879
+ setModified
1880
+ end
1738
1881
  end
1739
1882
 
1740
1883
  def goToLine( line = nil, column = nil )
1741
- cursorTo( line || @last_row, column || 0, DO_DISPLAY )
1884
+ cursorTo( line || @last_row, column || 0, DO_DISPLAY )
1742
1885
  end
1743
1886
 
1744
1887
  def goToNextBookmark
1745
- cur_pos = Bookmark.new( self, @last_row, @last_col )
1746
- next_bm = @bookmarks.find do |bm|
1747
- bm > cur_pos
1748
- end
1749
- if next_bm != nil
1750
- cursorTo( next_bm.row, next_bm.col, DO_DISPLAY )
1751
- end
1888
+ cur_pos = Bookmark.new( self, @last_row, @last_col )
1889
+ next_bm = @bookmarks.find do |bm|
1890
+ bm > cur_pos
1891
+ end
1892
+ if next_bm
1893
+ cursorTo( next_bm.row, next_bm.col, DO_DISPLAY )
1894
+ end
1752
1895
  end
1753
1896
 
1754
1897
  def goToPreviousBookmark
@@ -1,219 +1,255 @@
1
1
  module Diakonos
2
-
3
- class Readline
4
-
2
+
3
+ class Readline
4
+
5
5
  # completion_array is the array of strings that tab completion can use
6
- def initialize( diakonos, window, initial_text = "", completion_array = nil, history = [] )
7
- @window = window
8
- @diakonos = diakonos
9
- @initial_text = initial_text
10
- @completion_array = completion_array
11
- @list_filename = @diakonos.list_filename
12
-
13
- @history = history
14
- @history << initial_text
15
- @history_index = @history.length - 1
6
+ # The block returns true if a refresh is needed?
7
+ def initialize( diakonos, window, prompt, initial_text = "", completion_array = nil, history = [], &block )
8
+ @diakonos = diakonos
9
+ @window = window
10
+ @prompt = prompt
11
+ pos = redraw_prompt
12
+ @window.setpos( 0, pos )
13
+ @initial_text = initial_text
14
+ @completion_array = completion_array
15
+ @list_filename = @diakonos.list_filename
16
+
17
+ @history = history
18
+ @history << initial_text
19
+ @history_index = @history.length - 1
20
+
21
+ @block = block
16
22
  end
17
-
23
+
24
+ def redraw_prompt
25
+ @diakonos.setILine @prompt
26
+ end
27
+
28
+ def call_block
29
+ if @block
30
+ @block.call( @input )
31
+ @window.refresh
32
+ end
33
+ end
34
+
18
35
  # Returns nil on cancel.
19
36
  def readline
20
- @input = @initial_text
21
- @icurx = @window.curx
22
- @icury = @window.cury
23
- @window.addstr @initial_text
24
- @input_cursor = @initial_text.length
25
- @opened_list_file = false
26
-
27
- loop do
28
- c = @window.getch
29
-
30
- case c
31
- when Curses::KEY_DC
32
- if @input_cursor < @input.length
33
- @window.delch
34
- @input = @input[ 0...@input_cursor ] + @input[ (@input_cursor + 1)..-1 ]
35
- end
36
- when BACKSPACE, CTRL_H
37
- # Curses::KEY_LEFT
38
- if @input_cursor > 0
39
- @input_cursor += -1
40
- @window.setpos( @window.cury, @window.curx - 1 )
41
-
42
- # Curses::KEY_DC
43
- if @input_cursor < @input.length
44
- @window.delch
45
- @input = @input[ 0...@input_cursor ] + @input[ (@input_cursor + 1)..-1 ]
46
- end
47
- end
48
- when ENTER
49
- item = @diakonos.current_list_item
50
- if item and File.directory? item
51
- completeInput
52
- else
53
- break
54
- end
55
- when ESCAPE, CTRL_C, CTRL_D, CTRL_Q
56
- @input = nil
57
- break
58
- when Curses::KEY_LEFT
59
- if @input_cursor > 0
60
- @input_cursor += -1
61
- @window.setpos( @window.cury, @window.curx - 1 )
62
- end
63
- when Curses::KEY_RIGHT
64
- if @input_cursor < @input.length
65
- @input_cursor += 1
66
- @window.setpos( @window.cury, @window.curx + 1 )
67
- end
68
- when Curses::KEY_HOME
69
- @input_cursor = 0
70
- @window.setpos( @icury, @icurx )
71
- when Curses::KEY_END
72
- @input_cursor = @input.length
73
- @window.setpos( @window.cury, @icurx + @input.length )
74
- when TAB
75
- completeInput
76
- when Curses::KEY_NPAGE
77
- @diakonos.pageDown
78
- line = @diakonos.select_list_item
79
- if line
80
- @input = line
81
- cursorWriteInput
82
- end
83
- when Curses::KEY_PPAGE
84
- @diakonos.pageUp
85
- line = @diakonos.select_list_item
86
- if line
87
- @input = line
88
- cursorWriteInput
89
- end
90
- when Curses::KEY_UP
91
- if @diakonos.showing_list?
92
- @input = @diakonos.previous_list_item
93
- @diakonos.select_list_item
94
- elsif @history_index > 0
95
- @history[ @history_index ] = @input
96
- @history_index -= 1
97
- @input = @history[ @history_index ]
98
- end
99
- cursorWriteInput
100
- when Curses::KEY_DOWN
101
- if @diakonos.showing_list?
102
- @input = @diakonos.next_list_item
103
- @diakonos.select_list_item
104
- elsif @history_index < @history.length - 1
105
- @history[ @history_index ] = @input
106
- @history_index += 1
107
- @input = @history[ @history_index ]
108
- end
109
- cursorWriteInput
110
- when CTRL_K
111
- @input = ""
112
- cursorWriteInput
113
- else
114
- if c > 31 and c < 255 and c != BACKSPACE
115
- if @input_cursor == @input.length
116
- @input << c
117
- @window.addch c
118
- else
119
- @input = @input[ 0...@input_cursor ] + c.chr + @input[ @input_cursor..-1 ]
120
- @window.setpos( @window.cury, @window.curx + 1 )
121
- redrawInput
122
- end
123
- @input_cursor += 1
124
- else
125
- @diakonos.log "Other input: #{c}"
126
- end
37
+ @input = @initial_text
38
+ if not @input.empty?
39
+ call_block
40
+ end
41
+
42
+ @icurx = @window.curx
43
+ @icury = @window.cury
44
+ @window.addstr @initial_text
45
+ @input_cursor = @initial_text.length
46
+ @opened_list_file = false
47
+
48
+ loop do
49
+ c = @window.getch
50
+
51
+ case c
52
+ when Curses::KEY_DC
53
+ if @input_cursor < @input.length
54
+ @window.delch
55
+ @input = @input[ 0...@input_cursor ] + @input[ (@input_cursor + 1)..-1 ]
56
+ call_block
57
+ end
58
+ when BACKSPACE, CTRL_H
59
+ # Curses::KEY_LEFT
60
+ if @input_cursor > 0
61
+ @input_cursor += -1
62
+ @window.setpos( @window.cury, @window.curx - 1 )
63
+
64
+ # Curses::KEY_DC
65
+ if @input_cursor < @input.length
66
+ @window.delch
67
+ @input = @input[ 0...@input_cursor ] + @input[ (@input_cursor + 1)..-1 ]
68
+ call_block
69
+ end
70
+ end
71
+ when ENTER, Curses::KEY_F3
72
+ item = @diakonos.current_list_item
73
+ if item and File.directory? item
74
+ completeInput
75
+ else
76
+ break
77
+ end
78
+ when ESCAPE, CTRL_C, CTRL_D, CTRL_Q
79
+ @input = nil
80
+ break
81
+ when Curses::KEY_LEFT
82
+ if @input_cursor > 0
83
+ @input_cursor += -1
84
+ @window.setpos( @window.cury, @window.curx - 1 )
85
+ end
86
+ when Curses::KEY_RIGHT
87
+ if @input_cursor < @input.length
88
+ @input_cursor += 1
89
+ @window.setpos( @window.cury, @window.curx + 1 )
90
+ end
91
+ when Curses::KEY_HOME
92
+ @input_cursor = 0
93
+ @window.setpos( @icury, @icurx )
94
+ when Curses::KEY_END
95
+ @input_cursor = @input.length
96
+ @window.setpos( @window.cury, @icurx + @input.length )
97
+ when TAB
98
+ completeInput
99
+ when Curses::KEY_NPAGE
100
+ @diakonos.pageDown
101
+ line = @diakonos.select_list_item
102
+ if line
103
+ @input = line
104
+ cursorWriteInput
105
+ end
106
+ when Curses::KEY_PPAGE
107
+ @diakonos.pageUp
108
+ line = @diakonos.select_list_item
109
+ if line
110
+ @input = line
111
+ cursorWriteInput
112
+ end
113
+ when Curses::KEY_UP
114
+ if @diakonos.showing_list?
115
+ if @diakonos.list_item_selected?
116
+ @diakonos.previous_list_item
117
+ end
118
+ @input = @diakonos.select_list_item
119
+ elsif @history_index > 0
120
+ @history[ @history_index ] = @input
121
+ @history_index -= 1
122
+ @input = @history[ @history_index ]
123
+ end
124
+ cursorWriteInput
125
+ when Curses::KEY_DOWN
126
+ if @diakonos.showing_list?
127
+ if @diakonos.list_item_selected?
128
+ @diakonos.next_list_item
129
+ end
130
+ @input = @diakonos.select_list_item
131
+ elsif @history_index < @history.length - 1
132
+ @history[ @history_index ] = @input
133
+ @history_index += 1
134
+ @input = @history[ @history_index ]
135
+ end
136
+ cursorWriteInput
137
+ when CTRL_K
138
+ @input = ""
139
+ if @block
140
+ @block.call @input
141
+ end
142
+ cursorWriteInput
143
+ else
144
+ if c > 31 and c < 255 and c != BACKSPACE
145
+ if @input_cursor == @input.length
146
+ @input << c
147
+ @window.addch c
148
+ else
149
+ @input = @input[ 0...@input_cursor ] + c.chr + @input[ @input_cursor..-1 ]
150
+ @window.setpos( @window.cury, @window.curx + 1 )
151
+ redrawInput
127
152
  end
153
+ @input_cursor += 1
154
+ call_block
155
+ else
156
+ @diakonos.log "Other input: #{c}"
157
+ end
128
158
  end
129
-
130
- @diakonos.closeListBuffer
131
-
132
- @history[ -1 ] = @input
133
-
134
- return @input
159
+ end
160
+
161
+ @diakonos.closeListBuffer
162
+
163
+ @history[ -1 ] = @input
135
164
  end
136
165
 
137
166
  def redrawInput
138
- curx = @window.curx
139
- cury = @window.cury
140
- @window.setpos( @icury, @icurx )
141
- @window.addstr "%-#{ Curses::cols - curx }s%s" % [ @input, " " * ( Curses::cols - @input.length ) ]
142
- @window.setpos( cury, curx )
143
- @window.refresh
167
+ input = @input[ 0...Curses::cols ]
168
+
169
+ curx = @window.curx
170
+ cury = @window.cury
171
+ @window.setpos( @icury, @icurx )
172
+ @window.addstr "%-#{ Curses::cols - curx }s%s" % [ input, " " * ( Curses::cols - input.length ) ]
173
+ @window.setpos( cury, curx )
174
+ @window.refresh
144
175
  end
145
-
176
+
146
177
  # Redisplays the input text starting at the start of the user input area,
147
178
  # positioning the cursor at the end of the text.
148
179
  def cursorWriteInput
149
- if @input != nil
150
- @input_cursor = @input.length
151
- @window.setpos( @window.cury, @icurx + @input.length )
152
- redrawInput
153
- end
180
+ if @input != nil
181
+ @input_cursor = @input.length
182
+ @window.setpos( @window.cury, @icurx + @input.length )
183
+ redrawInput
184
+ end
154
185
  end
155
186
 
156
187
  def completeInput
157
- if @completion_array != nil and @input.length > 0
158
- len = @input.length
159
- matches = @completion_array.find_all { |el| el[ 0...len ] == @input and len < el.length }
160
- else
161
- matches = Dir.glob( ( @input.subHome() + "*" ).gsub( /\*\*/, "*" ) )
188
+ if @completion_array and @input.length > 0
189
+ len = @input.length
190
+ matches = @completion_array.find_all { |el| el[ 0...len ] == @input and len <= el.length }
191
+ else
192
+ matches = Dir.glob( ( @input.subHome() + "*" ).gsub( /\*\*/, "*" ) )
193
+ end
194
+
195
+ if matches.length == 1
196
+ @input = matches[ 0 ]
197
+ cursorWriteInput
198
+ File.open( @list_filename, "w" ) do |f|
199
+ if @completion_array.nil?
200
+ f.puts "(unique)"
201
+ else
202
+ f.puts @input
203
+ end
162
204
  end
163
-
164
- if matches.length == 1
165
- @input = matches[ 0 ]
166
- cursorWriteInput
167
- File.open( @list_filename, "w" ) do |f|
168
- f.puts "(unique)"
169
- end
170
- if @completion_array == nil and FileTest.directory?( @input )
171
- @input << "/"
172
- cursorWriteInput
173
- completeInput
205
+ if @completion_array.nil? and FileTest.directory?( @input )
206
+ @input << "/"
207
+ cursorWriteInput
208
+ completeInput
209
+ end
210
+ elsif matches.length > 1
211
+ common = matches[ 0 ]
212
+ File.open( @list_filename, "w" ) do |f|
213
+ i = nil
214
+ matches.each do |match|
215
+ f.print match
216
+ if FileTest.directory?( match )
217
+ f.print '/'
218
+ else
219
+ @diakonos.log "'#{match}' is not a directory"
174
220
  end
175
- elsif matches.length > 1
176
- common = matches[ 0 ]
177
- File.open( @list_filename, "w" ) do |f|
178
- i = nil
179
- matches.each do |match|
180
- f.print match
181
- if FileTest.directory?( match )
182
- f.print '/'
183
- else
184
- @diakonos.log "'#{match}' is not a directory"
185
- end
186
- f.puts
187
-
188
- if match[ 0 ] != common[ 0 ]
189
- common = nil
190
- break
191
- end
192
-
193
- up_to = [ common.length - 1, match.length - 1 ].min
194
- i = 1
195
- while ( i <= up_to ) and ( match[ 0..i ] == common[ 0..i ] )
196
- i += 1
197
- end
198
- common = common[ 0...i ]
199
- end
221
+ f.puts
222
+
223
+ if match[ 0 ] != common[ 0 ]
224
+ common = nil
225
+ break
200
226
  end
201
- if common == nil
202
- File.open( @list_filename, "w" ) do |f|
203
- f.puts "(no matches)"
204
- end
205
- else
206
- @input = common
207
- cursorWriteInput
227
+
228
+ up_to = [ common.length - 1, match.length - 1 ].min
229
+ i = 1
230
+ while ( i <= up_to ) and ( match[ 0..i ] == common[ 0..i ] )
231
+ i += 1
208
232
  end
233
+ common = common[ 0...i ]
234
+ end
235
+ end
236
+ if common == nil
237
+ File.open( @list_filename, "w" ) do |f|
238
+ f.puts "(no matches)"
239
+ end
209
240
  else
210
- File.open( @list_filename, "w" ) do |f|
211
- f.puts "(no matches)"
212
- end
241
+ @input = common
242
+ cursorWriteInput
243
+ end
244
+ else
245
+ File.open( @list_filename, "w" ) do |f|
246
+ f.puts "(no matches)"
213
247
  end
214
- @diakonos.openListBuffer
215
- @window.setpos( @window.cury, @window.curx )
248
+ end
249
+ @diakonos.openListBuffer
250
+ @window.setpos( @window.cury, @window.curx )
216
251
  end
217
- end
252
+
253
+ end
218
254
 
219
255
  end