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,55 @@
1
+ module Diakonos
2
+ class Diakonos
3
+ def openListBuffer
4
+ @list_buffer = openFile( @list_filename )
5
+ end
6
+
7
+ def closeListBuffer
8
+ closeFile( @list_buffer )
9
+ @list_buffer = nil
10
+ end
11
+
12
+ def showing_list?
13
+ @list_buffer
14
+ end
15
+
16
+ def list_item_selected?
17
+ @list_buffer and @list_buffer.selecting?
18
+ end
19
+
20
+ def current_list_item
21
+ if @list_buffer
22
+ @list_buffer.select_current_line
23
+ end
24
+ end
25
+
26
+ def select_list_item
27
+ if @list_buffer
28
+ line = @list_buffer.select_current_line
29
+ @list_buffer.display
30
+ line
31
+ end
32
+ end
33
+
34
+ def previous_list_item
35
+ if @list_buffer
36
+ cursorUp
37
+ @list_buffer[ @list_buffer.currentRow ]
38
+ end
39
+ end
40
+
41
+ def next_list_item
42
+ if @list_buffer
43
+ cursorDown
44
+ @list_buffer[ @list_buffer.currentRow ]
45
+ end
46
+ end
47
+
48
+ def with_list_file
49
+ File.open( @list_filename, "w" ) do |f|
50
+ yield f
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ module Diakonos
2
+ class Diakonos
3
+ def log( string )
4
+ @log.puts string
5
+ @log.flush
6
+ end
7
+
8
+ def debugLog( string )
9
+ @debug.puts( Time.now.strftime( "[%a %H:%M:%S] #{string}" ) )
10
+ @debug.flush
11
+ end
12
+
13
+ def logBacktrace
14
+ begin
15
+ raise Exception
16
+ rescue Exception => e
17
+ e.backtrace[ 1..-1 ].each do |x|
18
+ debugLog x
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -1,10 +1,10 @@
1
1
  module Diakonos
2
-
2
+
3
3
  class Readline
4
-
4
+
5
5
  # completion_array is the array of strings that tab completion can use
6
6
  # The block returns true if a refresh is needed?
7
- def initialize( diakonos, window, prompt, initial_text = "", completion_array = nil, history = [], &block )
7
+ def initialize( diakonos, window, prompt, initial_text = "", completion_array = nil, history = [], do_complete = Diakonos::DONT_COMPLETE, on_dirs = :go_into_dirs, &block )
8
8
  @diakonos = diakonos
9
9
  @window = window
10
10
  @prompt = prompt
@@ -13,41 +13,48 @@ module Diakonos
13
13
  @initial_text = initial_text
14
14
  @completion_array = completion_array
15
15
  @list_filename = @diakonos.list_filename
16
-
16
+
17
17
  @history = history
18
18
  @history << initial_text
19
19
  @history_index = @history.length - 1
20
-
20
+
21
+ @do_complete = do_complete
22
+ @on_dirs = on_dirs
23
+
21
24
  @block = block
22
25
  end
23
-
26
+
24
27
  def redraw_prompt
25
28
  @diakonos.setILine @prompt
26
29
  end
27
-
30
+
28
31
  def call_block
29
32
  if @block
30
33
  @block.call( @input )
31
34
  @window.refresh
32
35
  end
33
36
  end
34
-
37
+
35
38
  # Returns nil on cancel.
36
39
  def readline
37
40
  @input = @initial_text
38
41
  if not @input.empty?
39
42
  call_block
40
43
  end
41
-
44
+
42
45
  @icurx = @window.curx
43
46
  @icury = @window.cury
44
47
  @window.addstr @initial_text
45
48
  @input_cursor = @initial_text.length
46
49
  @opened_list_file = false
47
-
50
+
51
+ if @do_complete
52
+ completeInput
53
+ end
54
+
48
55
  loop do
49
- c = @window.getch
50
-
56
+ c = @window.getch.ord
57
+
51
58
  case c
52
59
  when Curses::KEY_DC
53
60
  if @input_cursor < @input.length
@@ -60,7 +67,7 @@ module Diakonos
60
67
  if @input_cursor > 0
61
68
  @input_cursor += -1
62
69
  @window.setpos( @window.cury, @window.curx - 1 )
63
-
70
+
64
71
  # Curses::KEY_DC
65
72
  if @input_cursor < @input.length
66
73
  @window.delch
@@ -70,7 +77,7 @@ module Diakonos
70
77
  end
71
78
  when ENTER, Curses::KEY_F3
72
79
  item = @diakonos.current_list_item
73
- if item and File.directory? item
80
+ if @on_dirs == :go_into_dirs and item and File.directory? item
74
81
  completeInput
75
82
  else
76
83
  break
@@ -140,6 +147,12 @@ module Diakonos
140
147
  @block.call @input
141
148
  end
142
149
  cursorWriteInput
150
+ when Curses::KEY_F5
151
+ @diakonos.decrease_grep_context
152
+ call_block
153
+ when Curses::KEY_F6
154
+ @diakonos.increase_grep_context
155
+ call_block
143
156
  else
144
157
  if c > 31 and c < 255 and c != BACKSPACE
145
158
  if @input_cursor == @input.length
@@ -157,15 +170,15 @@ module Diakonos
157
170
  end
158
171
  end
159
172
  end
160
-
173
+
161
174
  @diakonos.closeListBuffer
162
-
175
+
163
176
  @history[ -1 ] = @input
164
177
  end
165
178
 
166
179
  def redrawInput
167
180
  input = @input[ 0...Curses::cols ]
168
-
181
+
169
182
  curx = @window.curx
170
183
  cury = @window.cury
171
184
  @window.setpos( @icury, @icurx )
@@ -173,11 +186,11 @@ module Diakonos
173
186
  @window.setpos( cury, curx )
174
187
  @window.refresh
175
188
  end
176
-
189
+
177
190
  # Redisplays the input text starting at the start of the user input area,
178
191
  # positioning the cursor at the end of the text.
179
192
  def cursorWriteInput
180
- if @input != nil
193
+ if @input
181
194
  @input_cursor = @input.length
182
195
  @window.setpos( @window.cury, @icurx + @input.length )
183
196
  redrawInput
@@ -190,8 +203,12 @@ module Diakonos
190
203
  matches = @completion_array.find_all { |el| el[ 0...len ] == @input and len <= el.length }
191
204
  else
192
205
  matches = Dir.glob( ( @input.subHome() + "*" ).gsub( /\*\*/, "*" ) )
206
+ if @on_dirs == :accept_dirs
207
+ matches = matches.select { |m| File.directory? m }
208
+ end
193
209
  end
194
-
210
+ matches.sort!
211
+
195
212
  if matches.length == 1
196
213
  @input = matches[ 0 ]
197
214
  cursorWriteInput
@@ -205,7 +222,9 @@ module Diakonos
205
222
  if @completion_array.nil? and FileTest.directory?( @input )
206
223
  @input << "/"
207
224
  cursorWriteInput
208
- completeInput
225
+ if @on_dirs != :accept_dirs
226
+ completeInput
227
+ end
209
228
  end
210
229
  elsif matches.length > 1
211
230
  common = matches[ 0 ]
@@ -219,12 +238,12 @@ module Diakonos
219
238
  @diakonos.log "'#{match}' is not a directory"
220
239
  end
221
240
  f.puts
222
-
241
+
223
242
  if match[ 0 ] != common[ 0 ]
224
243
  common = nil
225
244
  break
226
245
  end
227
-
246
+
228
247
  up_to = [ common.length - 1, match.length - 1 ].min
229
248
  i = 1
230
249
  while ( i <= up_to ) and ( match[ 0..i ] == common[ 0..i ] )
@@ -233,7 +252,7 @@ module Diakonos
233
252
  common = common[ 0...i ]
234
253
  end
235
254
  end
236
- if common == nil
255
+ if common.nil?
237
256
  File.open( @list_filename, "w" ) do |f|
238
257
  f.puts "(no matches)"
239
258
  end
@@ -249,7 +268,7 @@ module Diakonos
249
268
  @diakonos.openListBuffer
250
269
  @window.setpos( @window.cury, @window.curx )
251
270
  end
252
-
271
+
253
272
  end
254
273
 
255
274
  end
@@ -1,6 +1,6 @@
1
1
  class Regexp
2
2
  def uses_bos
3
- return ( source[ 0 ] == ?^ )
3
+ source[ 0 ] == ?^
4
4
  end
5
5
  end
6
6
 
@@ -0,0 +1,70 @@
1
+ module Diakonos
2
+ class Diakonos
3
+
4
+ def new_session( filepath )
5
+ basename = File.basename( filepath )
6
+ if not pid_session?( filepath )
7
+ name = basename
8
+ end
9
+ @session = {
10
+ 'filename' => File.expand_path( filepath ),
11
+ 'settings' => Hash.new,
12
+ 'name' => name,
13
+ 'files' => [],
14
+ 'dir' => Dir.getwd,
15
+ }
16
+ end
17
+
18
+ def load_session_data( filename )
19
+ return if not File.exist? filename
20
+ File.open( filename ) do |f|
21
+ loaded = YAML::load( f )
22
+ if loaded
23
+ if(
24
+ loaded[ 'filename' ] and
25
+ loaded[ 'settings' ] and
26
+ loaded[ 'settings' ].respond_to?( :values ) and
27
+ loaded[ 'name' ] and
28
+ loaded[ 'files' ] and
29
+ loaded[ 'files' ].respond_to?( :each )
30
+ )
31
+ @session = loaded
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def save_session( session_file = @session[ 'filename' ] )
38
+ return if session_file.nil?
39
+ @session[ 'files' ] = @buffers.collect { |filepath,buffer|
40
+ buffer.name ? filepath : nil
41
+ }.compact
42
+ File.open( session_file, 'w' ) do |f|
43
+ f.puts @session.to_yaml
44
+ end
45
+ end
46
+
47
+ def session_filepath_for( session_id )
48
+ if session_id and session_id !~ %r{/}
49
+ "#{@session_dir}/#{session_id}"
50
+ else
51
+ session_id
52
+ end
53
+ end
54
+
55
+ def pid_session?( path = @session[ 'filename' ] )
56
+ %r{/\d+$} === path
57
+ end
58
+
59
+ def increase_grep_context
60
+ current = settings[ 'grep.context' ]
61
+ @session[ 'settings' ][ 'grep.context' ] = current + 1
62
+ end
63
+ def decrease_grep_context
64
+ current = settings[ 'grep.context' ]
65
+ if current > 0
66
+ @session[ 'settings' ][ 'grep.context' ] = current - 1
67
+ end
68
+ end
69
+ end
70
+ end
@@ -16,13 +16,13 @@ class SizedArray < Array
16
16
  def concat( other_array )
17
17
  super( other_array )
18
18
  resize
19
- return self
19
+ self
20
20
  end
21
21
 
22
22
  def fill( *args )
23
23
  retval = super( *args )
24
24
  resize
25
- return self
25
+ self
26
26
  end
27
27
 
28
28
  def <<( item )
@@ -30,7 +30,7 @@ class SizedArray < Array
30
30
  if size > @capacity
31
31
  retval = shift
32
32
  end
33
- return retval
33
+ retval
34
34
  end
35
35
 
36
36
  def push( item )
@@ -42,7 +42,7 @@ class SizedArray < Array
42
42
  if size > @capacity
43
43
  retval = pop
44
44
  end
45
- return retval
45
+ retval
46
46
  end
47
47
  end
48
48
 
@@ -1,6 +1,6 @@
1
1
  class String
2
2
  def subHome
3
- return gsub( /~/, ENV[ "HOME" ] )
3
+ gsub( /~/, ENV[ "HOME" ] )
4
4
  end
5
5
 
6
6
  def keyCode
@@ -57,22 +57,22 @@ class String
57
57
  when /^f(\d\d?)$/
58
58
  retval = Curses::KEY_F0 + $1.to_i
59
59
  when /^ctrl\+[a-gi-z]$/
60
- retval = downcase[ -1 ] - 96
60
+ retval = downcase[ -1 ].ord - 96
61
61
  when /^ctrl\+h$/
62
62
  retval = Diakonos::CTRL_H
63
63
  when /^alt\+(.)$/
64
- retval = [ Diakonos::ESCAPE, $1[ 0 ] ]
64
+ retval = [ Diakonos::ESCAPE, $1[ 0 ].ord ]
65
65
  when /^ctrl\+alt\+(.)$/, /^alt\+ctrl\+(.)$/
66
- retval = [ Diakonos::ESCAPE, downcase[ -1 ] - 96 ]
66
+ retval = [ Diakonos::ESCAPE, downcase[ -1 ].ord - 96 ]
67
67
  when /^keycode(\d+)$/
68
68
  retval = $1.to_i
69
69
  when /^.$/
70
- retval = self[ 0 ]
70
+ retval = self[ 0 ].ord
71
71
  end
72
72
  if retval.class != Array
73
73
  retval = [ retval ]
74
74
  end
75
- return retval
75
+ retval
76
76
  end
77
77
 
78
78
  def toFormatting
@@ -115,7 +115,7 @@ class String
115
115
  end
116
116
  end
117
117
  end
118
- return formatting
118
+ formatting
119
119
  end
120
120
 
121
121
  def toColourConstant
@@ -140,7 +140,7 @@ class String
140
140
  to_i
141
141
  end
142
142
  end
143
-
143
+
144
144
  def toDirection( default = :down )
145
145
  direction = nil
146
146
  case self
@@ -157,24 +157,24 @@ class String
157
157
  else
158
158
  direction = default
159
159
  end
160
- return direction
160
+ direction
161
161
  end
162
-
162
+
163
163
  def to_a
164
- return [ self ]
164
+ [ self ]
165
165
  end
166
166
 
167
167
  def to_b
168
- retval = false
169
168
  case downcase
170
- when "true", "t", "1", "yes", "y", "on", "+"
171
- retval = true
169
+ when "true", "t", "1", "yes", "y", "on", "+"
170
+ true
171
+ else
172
+ false
172
173
  end
173
- return retval
174
174
  end
175
175
 
176
176
  def indentation_level( indent_size, indent_roundup, tab_size = Diakonos::DEFAULT_TAB_SIZE, indent_ignore_charset = nil )
177
- if indent_ignore_charset == nil
177
+ if indent_ignore_charset.nil?
178
178
  level = 0
179
179
  if self =~ /^([\s]+)/
180
180
  #whitespace = $1.gsub( /\t/, ' ' * tab_size )
@@ -198,10 +198,10 @@ class String
198
198
  level = 0
199
199
  end
200
200
  end
201
-
202
- return level
201
+
202
+ level
203
203
  end
204
-
204
+
205
205
  def expandTabs( tab_size = Diakonos::DEFAULT_TAB_SIZE )
206
206
  s = dup
207
207
  while s.sub!( /\t/ ) { |match_text|
@@ -211,9 +211,9 @@ class String
211
211
  " " * ( tab_size - ( index % tab_size ) )
212
212
  }
213
213
  end
214
- return s
214
+ s
215
215
  end
216
-
216
+
217
217
  def newlineSplit
218
218
  retval = split( /\\n/ )
219
219
  if self =~ /\\n$/
@@ -226,9 +226,9 @@ class String
226
226
  end
227
227
  retval[ -1 ] = "^" << retval[ -1 ]
228
228
  end
229
- return retval
229
+ retval
230
230
  end
231
-
231
+
232
232
  # Works like normal String#index except returns the index
233
233
  # of the first matching regexp group if one or more groups are specified
234
234
  # in the regexp. Both the index and the matched text are returned.
@@ -236,7 +236,7 @@ class String
236
236
  if regexp.class != Regexp
237
237
  return index( regexp, offset )
238
238
  end
239
-
239
+
240
240
  i = nil
241
241
  match_text = nil
242
242
  working_offset = 0
@@ -249,14 +249,14 @@ class String
249
249
  if match.length > 1
250
250
  # Find first matching group
251
251
  1.upto( match.length - 1 ) do |match_item_index|
252
- if match[ match_item_index ] != nil
252
+ if match[ match_item_index ]
253
253
  i = match.begin( match_item_index )
254
254
  match_text = match[ match_item_index ]
255
255
  break
256
256
  end
257
257
  end
258
258
  end
259
-
259
+
260
260
  break if i >= offset
261
261
  else
262
262
  i = nil
@@ -264,10 +264,10 @@ class String
264
264
  end
265
265
  working_offset += 1
266
266
  end
267
-
268
- return i, match_text
267
+
268
+ [ i, match_text ]
269
269
  end
270
-
270
+
271
271
  # Works like normal String#rindex except returns the index
272
272
  # of the first matching regexp group if one or more groups are specified
273
273
  # in the regexp. Both the index and the matched text are returned.
@@ -275,7 +275,7 @@ class String
275
275
  if regexp.class != Regexp
276
276
  return rindex( regexp, offset )
277
277
  end
278
-
278
+
279
279
  i = nil
280
280
  match_text = nil
281
281
  working_offset = length
@@ -288,19 +288,19 @@ class String
288
288
  if match.length > 1
289
289
  # Find first matching group
290
290
  1.upto( match.length - 1 ) do |match_item_index|
291
- if match[ match_item_index ] != nil
291
+ if match[ match_item_index ]
292
292
  i = match.end( match_item_index ) - 1
293
293
  match_text = match[ match_item_index ]
294
294
  break
295
295
  end
296
296
  end
297
297
  end
298
-
298
+
299
299
  if match_text == ""
300
300
  # Assume that an empty string means that it matched $
301
301
  i += 1
302
302
  end
303
-
303
+
304
304
  break if i <= offset
305
305
  else
306
306
  i = nil
@@ -308,12 +308,19 @@ class String
308
308
  end
309
309
  working_offset -= 1
310
310
  end
311
-
312
- return i, match_text
311
+
312
+ [ i, match_text ]
313
313
  end
314
-
314
+
315
315
  def movement?
316
- return ( ( self =~ /^((cursor|page|scroll)(Up|Down|Left|Right)|find)/ ) != nil )
316
+ self =~ /^((cursor|page|scroll)(Up|Down|Left|Right)|find)/
317
+ end
318
+
319
+ # Backport of Ruby 1.9's String#ord into Ruby 1.8
320
+ unless method_defined?( :ord )
321
+ def ord
322
+ self[ 0 ]
323
+ end
317
324
  end
318
325
  end
319
326