diakonos 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ module Diakonos
2
+ def self.grep_array( regexp, array, lines_of_context, prefix, filepath )
3
+ num_lines = array.size
4
+ line_numbers = []
5
+ array.each_with_index do |line,index|
6
+ next if line !~ regexp
7
+ start_index = [ 0, index - lines_of_context ].max
8
+ end_index = [ index + lines_of_context, num_lines-1 ].min
9
+ (start_index..end_index).each do |i|
10
+ line_numbers << i
11
+ end
12
+ end
13
+
14
+ line_numbers.uniq!
15
+ results = []
16
+ last_i = line_numbers[ 0 ]
17
+ one_result = []
18
+ line_numbers.each do |i|
19
+ if i - last_i > 1
20
+ results << one_result.join( "\n" )
21
+ one_result = []
22
+ end
23
+ one_result << ( "#{prefix}#{i+1}: " << ( "%-300s | #{filepath}:#{i+1}" % array[ i ] ) )
24
+ last_i = i
25
+ end
26
+ if not one_result.empty?
27
+ results << one_result.join( "\n" )
28
+ end
29
+
30
+ results
31
+ end
32
+
33
+ class Diakonos
34
+
35
+ def grep_( regexp_source, *buffers )
36
+ original_buffer = @current_buffer
37
+ if @current_buffer.changing_selection
38
+ selected_text = @current_buffer.copySelection[ 0 ]
39
+ end
40
+ starting_row, starting_col = @current_buffer.last_row, @current_buffer.last_col
41
+
42
+ selected = getUserInput(
43
+ "Grep regexp: ",
44
+ @rlh_search,
45
+ regexp_source || selected_text || ""
46
+ ) { |input|
47
+ next if input.length < 2
48
+ begin
49
+ regexp = Regexp.new( input, Regexp::IGNORECASE )
50
+ grep_results = buffers.map { |buffer| buffer.grep( regexp ) }.flatten
51
+ if settings[ 'grep.context' ] == 0
52
+ join_str = "\n"
53
+ else
54
+ join_str = "\n---\n"
55
+ end
56
+ with_list_file do |list|
57
+ list.puts grep_results.join( join_str )
58
+ end
59
+ list_buffer = openListBuffer
60
+ list_buffer.highlightMatches regexp
61
+ list_buffer.display
62
+ rescue RegexpError
63
+ # Do nothing
64
+ end
65
+ }
66
+
67
+ if selected
68
+ spl = selected.split( "| " )
69
+ if spl.size > 1
70
+ openFile spl[ -1 ]
71
+ end
72
+ else
73
+ original_buffer.cursorTo starting_row, starting_col
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -15,10 +15,10 @@ class Hash
15
15
  elsif path.length == 1
16
16
  delete( path[ 0 ] )
17
17
  end
18
-
19
- return self
18
+
19
+ self
20
20
  end
21
-
21
+
22
22
  def setKeyPath( path, leaf )
23
23
  if path.length > 1
24
24
  node = self[ path[ 0 ] ]
@@ -29,33 +29,33 @@ class Hash
29
29
  elsif path.length == 1
30
30
  self[ path[ 0 ] ] = leaf
31
31
  end
32
-
33
- return self
32
+
33
+ self
34
34
  end
35
-
35
+
36
36
  def getNode( path )
37
37
  node = self[ path[ 0 ] ]
38
38
  if path.length > 1
39
- if node != nil and node.respond_to?( :getNode )
39
+ if node and node.respond_to?( :getNode )
40
40
  return node.getNode( path[ 1..-1 ] )
41
41
  end
42
42
  elsif path.length == 1
43
43
  return node
44
44
  end
45
-
46
- return nil
45
+
46
+ nil
47
47
  end
48
-
48
+
49
49
  def getLeaf( path )
50
50
  node = getNode( path )
51
51
  if node.respond_to?( :getNode )
52
52
  # Only want a leaf node
53
- return nil
53
+ nil
54
54
  else
55
- return node
55
+ node
56
56
  end
57
57
  end
58
-
58
+
59
59
  def leaves( _leaves = Set.new )
60
60
  each_value do |value|
61
61
  if value.respond_to?( :leaves )
@@ -64,10 +64,10 @@ class Hash
64
64
  _leaves << value
65
65
  end
66
66
  end
67
-
68
- return _leaves
67
+
68
+ _leaves
69
69
  end
70
-
70
+
71
71
  def paths_and_leaves( path_so_far = [], _paths_and_leaves = Set.new )
72
72
  each do |key, value|
73
73
  if value.respond_to?( :paths_and_leaves )
@@ -84,10 +84,10 @@ class Hash
84
84
  }
85
85
  end
86
86
  end
87
-
88
- return _paths_and_leaves
87
+
88
+ _paths_and_leaves
89
89
  end
90
-
90
+
91
91
  def each_path_and_leaf( path_so_far = [] )
92
92
  each do |key, value|
93
93
  if value.respond_to?( :each_path_and_leaf )
@@ -97,5 +97,12 @@ class Hash
97
97
  end
98
98
  end
99
99
  end
100
+
101
+ # Implement Ruby 1.9's Hash#key for Ruby 1.8
102
+ if ! method_defined?( :key )
103
+ def key( *args )
104
+ index( *args )
105
+ end
106
+ end
100
107
  end
101
108
 
@@ -0,0 +1,92 @@
1
+ module Diakonos
2
+ class Diakonos
3
+ def init_help
4
+ @base_help_dir = "#{@diakonos_home}/help"
5
+ mkdir @base_help_dir
6
+
7
+ @help_dir = "#{@diakonos_home}/help/#{VERSION}"
8
+ if not File.exist?( @help_dir ) or Dir[ "#{@help_dir}/*" ].size == 0
9
+ if @testing
10
+ `cp -r ./help #{@help_dir}`
11
+ else
12
+ puts "Help files for this Diakonos version were not found (#{@help_dir})."
13
+ puts "Would you like to download the help files right now from the Diakonos website? (y/n)"
14
+ answer = $stdin.gets
15
+ case answer
16
+ when /^y/i
17
+ if not fetch_help
18
+ $stderr.puts "Failed to get help for version #{VERSION}."
19
+ sleep 2
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ @help_tags = `grep -h Tags #{@help_dir}/* | cut -d ' ' -f 2-`.split.uniq
26
+ end
27
+
28
+ def fetch_help
29
+ require 'open-uri'
30
+ success = false
31
+ puts "Fetching help documents for version #{VERSION}..."
32
+
33
+ filename = "diakonos-help-#{VERSION}.tar.gz"
34
+ uri = "http://purepistos.net/diakonos/#{filename}"
35
+ tarball = "#{@base_help_dir}/#{filename}"
36
+ begin
37
+ open( uri ) do |http|
38
+ bytes = http.read
39
+ File.open( tarball, 'w' ) do |f|
40
+ f.print bytes
41
+ end
42
+ end
43
+ mkdir @help_dir
44
+ `tar zxf #{tarball} -C #{@base_help_dir}`
45
+ success = true
46
+ rescue OpenURI::HTTPError => e
47
+ $stderr.puts "Failed to fetch from #{uri} ."
48
+ end
49
+
50
+ success
51
+ end
52
+
53
+ def open_help_buffer
54
+ @help_buffer = openFile( @help_filename )
55
+ end
56
+ def close_help_buffer
57
+ closeFile @help_buffer
58
+ @help_buffer = nil
59
+ end
60
+
61
+ def matching_help_documents( str )
62
+ docs = []
63
+
64
+ if str =~ %r{^/(.+)$}
65
+ regexp = $1
66
+ files = Dir[ "#{@help_dir}/*" ].select{ |f|
67
+ File.open( f ) { |io| io.grep( /#{regexp}/i ) }.any?
68
+ }
69
+ else
70
+ terms = str.gsub( /[^a-zA-Z0-9-]/, ' ' ).split.join( '|' )
71
+ file_grep = `egrep -i -l '^Tags.*\\b(#{terms})\\b' #{@help_dir}/*`
72
+ files = file_grep.split( /\s+/ )
73
+ end
74
+
75
+ files.each do |file|
76
+ File.open( file ) do |f|
77
+ docs << ( "%-300s | %s" % [ f.gets.strip, file ] )
78
+ end
79
+ end
80
+
81
+ docs.sort { |a,b| a.gsub( /^# (?:an?|the) */i, '# ' ) <=> b.gsub( /^# (?:an?|the) */i, '# ' ) }
82
+ end
83
+
84
+ def open_help_document( selected_string )
85
+ help_file = selected_string.split( "| " )[ -1 ]
86
+ if File.exist? help_file
87
+ openFile help_file
88
+ end
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,13 @@
1
+ module Diakonos
2
+ class Diakonos
3
+ def register_proc( the_proc, hook_name, priority = 0 )
4
+ @hooks[ hook_name ] << { :proc => the_proc, :priority => priority }
5
+ end
6
+
7
+ def runHookProcs( hook_id, *args )
8
+ @hooks[ hook_id ].each do |hook_proc|
9
+ hook_proc[ :proc ].call( *args )
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,139 @@
1
+ module Diakonos
2
+
3
+ TAB = 9
4
+ ENTER = 13
5
+ ESCAPE = 27
6
+ BACKSPACE = 127
7
+ CTRL_C = 3
8
+ CTRL_D = 4
9
+ CTRL_K = 11
10
+ CTRL_Q = 17
11
+ CTRL_H = 263
12
+ RESIZE2 = 4294967295
13
+
14
+ DO_COMPLETE = true
15
+ DONT_COMPLETE = false
16
+
17
+ CHOICE_NO = 0
18
+ CHOICE_YES = 1
19
+ CHOICE_ALL = 2
20
+ CHOICE_CANCEL = 3
21
+ CHOICE_YES_TO_ALL = 4
22
+ CHOICE_NO_TO_ALL = 5
23
+ CHOICE_YES_AND_STOP = 6
24
+ CHOICE_DELETE = 7
25
+ CHOICE_KEYS = [
26
+ [ "n".ord, "N".ord ],
27
+ [ "y".ord, "Y".ord ],
28
+ [ "a".ord, "A".ord ],
29
+ [ "c".ord, "C".ord, ESCAPE, CTRL_C, CTRL_D, CTRL_Q ],
30
+ [ "e".ord ],
31
+ [ "o".ord ],
32
+ [ "s".ord ],
33
+ [ "d".ord ],
34
+ ]
35
+ CHOICE_STRINGS = [ '(n)o', '(y)es', '(a)ll', '(c)ancel', 'y(e)s to all', 'n(o) to all', 'yes and (s)top', '(d)elete' ]
36
+
37
+ class Diakonos
38
+ # completion_array is the array of strings that tab completion can use
39
+ def getUserInput( prompt, history = @rlh_general, initial_text = "", completion_array = nil, do_complete = DONT_COMPLETE, on_dirs = :go_into_dirs, &block )
40
+ if @playing_macro
41
+ retval = @macro_input_history.shift
42
+ else
43
+ retval = Readline.new( self, @win_interaction, prompt, initial_text, completion_array, history, do_complete, on_dirs, &block ).readline
44
+ if @macro_history
45
+ @macro_input_history.push retval
46
+ end
47
+ setILine
48
+ end
49
+ retval
50
+ end
51
+
52
+ def interactionBlink( message = nil )
53
+ terminateMessage
54
+ setILine @settings[ 'interaction.blink_string' ]
55
+ sleep @settings[ 'interaction.blink_duration' ]
56
+ setILine message if message
57
+ end
58
+
59
+ # choices should be an array of CHOICE_* constants.
60
+ # default is what is returned when Enter is pressed.
61
+ def getChoice( prompt, choices, default = nil )
62
+ retval = @iterated_choice
63
+ if retval
64
+ @choice_iterations -= 1
65
+ if @choice_iterations < 1
66
+ @iterated_choice = nil
67
+ @do_display = true
68
+ end
69
+ return retval
70
+ end
71
+
72
+ @saved_main_x = @win_main.curx
73
+ @saved_main_y = @win_main.cury
74
+
75
+ msg = prompt + " "
76
+ choice_strings = choices.collect do |choice|
77
+ CHOICE_STRINGS[ choice ]
78
+ end
79
+ msg << choice_strings.join( ", " )
80
+
81
+ if default.nil?
82
+ showMessage msg
83
+ else
84
+ setILine msg
85
+ end
86
+
87
+ c = nil
88
+ while retval.nil?
89
+ c = @win_interaction.getch.ord
90
+
91
+ case c
92
+ when Curses::KEY_NPAGE
93
+ pageDown
94
+ when Curses::KEY_PPAGE
95
+ pageUp
96
+ else
97
+ if @message_expiry and Time.now < @message_expiry
98
+ interactionBlink
99
+ showMessage msg
100
+ else
101
+ case c
102
+ when ENTER
103
+ retval = default
104
+ when ?0..?9
105
+ if @choice_iterations < 1
106
+ @choice_iterations = ( c - ?0 )
107
+ else
108
+ @choice_iterations = @choice_iterations * 10 + ( c - ?0 )
109
+ end
110
+ else
111
+ choices.each do |choice|
112
+ if CHOICE_KEYS[ choice ].include? c
113
+ retval = choice
114
+ break
115
+ end
116
+ end
117
+ end
118
+
119
+ if retval.nil?
120
+ interactionBlink( msg )
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ terminateMessage
127
+ setILine
128
+
129
+ if @choice_iterations > 0
130
+ @choice_iterations -= 1
131
+ @iterated_choice = retval
132
+ @do_display = false
133
+ end
134
+
135
+ retval
136
+ end
137
+
138
+ end
139
+ end
@@ -52,7 +52,7 @@ module KeyCode
52
52
  if self.class == Fixnum
53
53
  retval = KEYSTRINGS[ self ]
54
54
  end
55
- if retval == nil
55
+ if retval.nil?
56
56
  case self
57
57
  when Curses::KEY_DOWN
58
58
  retval = "down"
@@ -100,10 +100,10 @@ module KeyCode
100
100
  retval = "resize2"
101
101
  end
102
102
  end
103
- if retval == nil and self.class == Fixnum
103
+ if retval.nil? and self.class == Fixnum
104
104
  retval = "keycode#{self}"
105
105
  end
106
- return retval
106
+ retval
107
107
  end
108
108
  end
109
109
 
@@ -0,0 +1,124 @@
1
+ module Diakonos
2
+ class Diakonos
3
+ def capture_keychain( c, context )
4
+ if c == ENTER
5
+ @capturing_keychain = false
6
+ @current_buffer.deleteSelection
7
+ str = context.to_keychain_s.strip
8
+ @current_buffer.insertString str
9
+ cursorRight( Buffer::STILL_TYPING, str.length )
10
+ else
11
+ keychain_pressed = context.concat [ c ]
12
+
13
+ function_and_args = @keychains.getLeaf( keychain_pressed )
14
+
15
+ if function_and_args
16
+ function, args = function_and_args
17
+ end
18
+
19
+ partial_keychain = @keychains.getNode( keychain_pressed )
20
+ if partial_keychain
21
+ setILine( "Part of existing keychain: " + keychain_pressed.to_keychain_s + "..." )
22
+ else
23
+ setILine keychain_pressed.to_keychain_s + "..."
24
+ end
25
+ processKeystroke( keychain_pressed )
26
+ end
27
+ end
28
+
29
+ def capture_mapping( c, context )
30
+ if c == ENTER
31
+ @capturing_mapping = false
32
+ @current_buffer.deleteSelection
33
+ setILine
34
+ else
35
+ keychain_pressed = context.concat [ c ]
36
+
37
+ function_and_args = @keychains.getLeaf( keychain_pressed )
38
+
39
+ if function_and_args
40
+ function, args = function_and_args
41
+ setILine "#{keychain_pressed.to_keychain_s.strip} -> #{function}( #{args} )"
42
+ else
43
+ partial_keychain = @keychains.getNode( keychain_pressed )
44
+ if partial_keychain
45
+ setILine( "Several mappings start with: " + keychain_pressed.to_keychain_s + "..." )
46
+ processKeystroke( keychain_pressed )
47
+ else
48
+ setILine "There is no mapping for " + keychain_pressed.to_keychain_s
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ # context is an array of characters (bytes) which are keystrokes previously
55
+ # typed (in a chain of keystrokes)
56
+ def processKeystroke( context = [] )
57
+ c = @win_main.getch.ord
58
+
59
+ if @capturing_keychain
60
+ capture_keychain c, context
61
+ elsif @capturing_mapping
62
+ capture_mapping c, context
63
+ else
64
+
65
+ if context.empty?
66
+ if c > 31 and c < 255 and c != BACKSPACE
67
+ if @macro_history
68
+ @macro_history.push "typeCharacter #{c}"
69
+ end
70
+ @there_was_non_movement = true
71
+ typeCharacter c
72
+ return
73
+ end
74
+ end
75
+ keychain_pressed = context.concat [ c ]
76
+
77
+ function_and_args = @keychains.getLeaf( keychain_pressed )
78
+
79
+ if function_and_args
80
+ function, args = function_and_args
81
+ setILine if not @settings[ "context.combined" ]
82
+
83
+ if args
84
+ to_eval = "#{function}( #{args} )"
85
+ else
86
+ to_eval = function
87
+ end
88
+
89
+ if @macro_history
90
+ @macro_history.push to_eval
91
+ end
92
+
93
+ begin
94
+ eval to_eval, nil, "eval"
95
+ @last_commands << to_eval unless to_eval == "repeatLast"
96
+ if not @there_was_non_movement
97
+ @there_was_non_movement = ( not to_eval.movement? )
98
+ end
99
+ rescue Exception => e
100
+ debugLog e.message
101
+ debugLog e.backtrace.join( "\n\t" )
102
+ showException e
103
+ end
104
+ else
105
+ partial_keychain = @keychains.getNode( keychain_pressed )
106
+ if partial_keychain
107
+ setILine( keychain_pressed.to_keychain_s + "..." )
108
+ processKeystroke( keychain_pressed )
109
+ else
110
+ setILine "Nothing assigned to #{keychain_pressed.to_keychain_s}"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ protected :processKeystroke
116
+
117
+ def typeCharacter( c )
118
+ @current_buffer.deleteSelection( Buffer::DONT_DISPLAY )
119
+ @current_buffer.insertChar c
120
+ cursorRight( Buffer::STILL_TYPING )
121
+ end
122
+
123
+ end
124
+ end