rb-readline 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,520 +1,524 @@
1
- # readline.rb -- GNU Readline module
2
- # Copyright (C) 1997-2001 Shugo Maeda
3
- #
4
- # Ruby translation by Park Heesob phasis@gmail.com
5
-
6
- module Readline
7
-
8
- require 'rbreadline'
9
- include RbReadline
10
-
11
- @completion_proc = nil
12
- @completion_case_fold = false
13
-
14
- # Begins an interactive terminal process using +prompt+ as the command
15
- # prompt that users see when they type commands. The method returns the
16
- # line entered whenever a carriage return is encountered.
17
- #
18
- # If an +add_history+ argument is provided, commands entered by users are
19
- # stored in a history buffer that can be recalled for later use.
20
- #
21
- # Note that this method depends on $stdin and $stdout both being open.
22
- # Because this is meant as an interactive console interface, they should
23
- # generally not be redirected.
24
- #
25
- # Example:
26
- #
27
- # loop{ Readline.readline('> ') }
28
- #
29
- def readline(prompt, add_history=nil)
30
- if $stdin.closed?
31
- raise IOError, "stdin closed"
32
- end
33
-
34
- RbReadline.rl_instream = $stdin
35
- RbReadline.rl_outstream = $stdout
36
-
37
- status = 0
38
-
39
- begin
40
- buff = RbReadline.readline(prompt)
41
- rescue Exception => e
42
- buff = nil
43
- RbReadline.rl_cleanup_after_signal()
44
- RbReadline.rl_deprep_terminal()
45
- raise e
46
- end
47
-
48
- if add_history && buff
49
- RbReadline.add_history(buff)
50
- end
51
-
52
- return buff ? buff.dup : nil
53
- end
54
-
55
- # Sets the input stream (an IO object) for readline interaction. The
56
- # default is <tt>$stdin</tt>.
57
- #
58
- def self.input=(input)
59
- RbReadline.rl_instream = input
60
- end
61
-
62
- # Sets the output stream (an IO object) for readline interaction. The
63
- # default is <tt>$stdout</tt>.
64
- #
65
- def self.output=(output)
66
- RbReadline.rl_outstream = output
67
- end
68
-
69
- # Returns current line buffer
70
- #
71
- def self.line_buffer
72
- RbReadline.rl_line_buffer
73
- end
74
-
75
- # Sets the auto-completion procedure (i.e. tab auto-complete).
76
- #
77
- # The +proc+ argument is typically a Proc object. It must respond to
78
- # <tt>.call</tt>, take a single String argument and return an Array of
79
- # candidates for completion.
80
- #
81
- # Example:
82
- #
83
- # list = ['search', 'next', 'clear']
84
- # Readline.completion_proc = proc{ |s| list.grep( /^#{Regexp.escape(s)}/) }
85
- #
86
- def self.completion_proc=(proc)
87
- unless defined? proc.call
88
- raise ArgumentError,"argument must respond to `call'"
89
- end
90
- @completion_proc = proc
91
- end
92
-
93
- # Returns the current auto-completion procedure.
94
- #
95
- def self.completion_proc()
96
- @completion_proc
97
- end
98
-
99
- # Sets whether or not the completion proc should ignore case sensitivity.
100
- # The default is false, i.e. completion procs are case sensitive.
101
- #
102
- def self.completion_case_fold=(bool)
103
- @completion_case_fold = bool
104
- end
105
-
106
- # Returns whether or not the completion proc is case sensitive. The
107
- # default is false, i.e. completion procs are case sensitive.
108
- #
109
- def self.completion_case_fold()
110
- @completion_case_fold
111
- end
112
-
113
- # Returns nil if no matches are found or an array of strings:
114
- #
115
- # [0] is the replacement for text
116
- # [1..n] the possible matches
117
- # [n+1] nil
118
- #
119
- # The possible matches should not include [0].
120
- #
121
- # If this method sets RbReadline.rl_attempted_completion_over to true,
122
- # then the default completion function will not be called when this
123
- # function returns nil.
124
- def self.readline_attempted_completion_function(text,start,_end)
125
- proc = @completion_proc
126
- return nil if proc.nil?
127
-
128
- RbReadline.rl_attempted_completion_over = true
129
-
130
- case_fold = @completion_case_fold
131
- ary = proc.call(text)
132
- if ary.class != Array
133
- ary = Array(ary)
134
- else
135
- ary.compact!
136
- end
137
-
138
- matches = ary.length
139
- return nil if (matches == 0)
140
- result = Array.new(matches+2)
141
- for i in 0 ... matches
142
- result[i+1] = ary[i].dup
143
- end
144
- result[matches+1] = nil
145
-
146
- if(matches==1)
147
- result[0] = result[1].dup
148
- result[1] = nil
149
- else
150
- i = 1
151
- low = 100000
152
-
153
- while (i < matches)
154
- if (case_fold)
155
- si = 0
156
- while ((c1 = result[i][si,1].downcase) &&
157
- (c2 = result[i + 1][si,1].downcase))
158
- break if (c1 != c2)
159
- si += 1
160
- end
161
- else
162
- si = 0
163
- while ((c1 = result[i][si,1]) &&
164
- (c2 = result[i + 1][si,1]))
165
- break if (c1 != c2)
166
- si += 1
167
- end
168
- end
169
- if (low > si)
170
- low = si
171
- end
172
- i+=1
173
- end
174
- result[0] = result[1][0,low]
175
- end
176
-
177
- result
178
- end
179
-
180
- # Sets vi editing mode.
181
- #
182
- def self.vi_editing_mode()
183
- RbReadline.rl_vi_editing_mode(1,0)
184
- nil
185
- end
186
-
187
- # Sets emacs editing mode
188
- #
189
- def self.emacs_editing_mode()
190
- RbReadline.rl_emacs_editing_mode(1,0)
191
- nil
192
- end
193
-
194
- # Sets the character that is automatically appended after the
195
- # Readline.completion_proc method is called.
196
- #
197
- # If +char+ is nil or empty, then a null character is used.
198
- #
199
- def self.completion_append_character=(char)
200
- if char.nil?
201
- RbReadline.rl_completion_append_character = ?\0
202
- elsif char.length==0
203
- RbReadline.rl_completion_append_character = ?\0
204
- else
205
- RbReadline.rl_completion_append_character = char[0]
206
- end
207
- end
208
-
209
- # Returns the character that is automatically appended after the
210
- # Readline.completion_proc method is called.
211
- #
212
- def self.completion_append_character()
213
- if RbReadline.rl_completion_append_character == ?\0
214
- nil
215
- end
216
- return RbReadline.rl_completion_append_character
217
- end
218
-
219
- # Sets the character string that signal a break between words for the
220
- # completion proc.
221
- #
222
- def self.basic_word_break_characters=(str)
223
- RbReadline.rl_basic_word_break_characters = str.dup
224
- end
225
-
226
- # Returns the character string that signal a break between words for the
227
- # completion proc. The default is " \t\n\"\\'`@$><=|&{(".
228
- #
229
- def self.basic_word_break_characters()
230
- if RbReadline.rl_basic_word_break_characters.nil?
231
- nil
232
- else
233
- RbReadline.rl_basic_word_break_characters.dup
234
- end
235
- end
236
-
237
- # Sets the character string that signal the start or end of a word for
238
- # the completion proc.
239
- #
240
- def self.completer_word_break_characters=(str)
241
- RbReadline.rl_completer_word_break_characters = str.dup
242
- end
243
-
244
- # Returns the character string that signal the start or end of a word for
245
- # the completion proc.
246
- #
247
- def self.completer_word_break_characters()
248
- if RbReadline.rl_completer_word_break_characters.nil?
249
- nil
250
- else
251
- RbReadline.rl_completer_word_break_characters.dup
252
- end
253
- end
254
-
255
- # Sets the list of quote characters that can cause a word break.
256
- #
257
- def self.basic_quote_characters=(str)
258
- RbReadline.rl_basic_quote_characters = str.dup
259
- end
260
-
261
- # Returns the list of quote characters that can cause a word break.
262
- # The default is "'\"" (single and double quote characters).
263
- #
264
- def self.basic_quote_characters()
265
- if RbReadline.rl_basic_quote_characters.nil?
266
- nil
267
- else
268
- RbReadline.rl_basic_quote_characters.dup
269
- end
270
- end
271
-
272
- # Sets the list of characters that can be used to quote a substring of
273
- # the line, i.e. a group of characters within quotes.
274
- #
275
- def self.completer_quote_characters=(str)
276
- RbReadline.rl_completer_quote_characters = str.dup
277
- end
278
-
279
- # Returns the list of characters that can be used to quote a substring
280
- # of the line, i.e. a group of characters inside quotes.
281
- #
282
- def self.completer_quote_characters()
283
- if RbReadline.rl_completer_quote_characters.nil?
284
- nil
285
- else
286
- RbReadline.rl_completer_quote_characters.dup
287
- end
288
- end
289
-
290
- # Sets the character string of one or more characters that indicate quotes
291
- # for the filename completion of user input.
292
- #
293
- def self.filename_quote_characters=(str)
294
- RbReadline.rl_filename_quote_characters = str.dup
295
- end
296
-
297
- # Returns the character string used to indicate quotes for the filename
298
- # completion of user input.
299
- #
300
- def self.filename_quote_characters()
301
- if RbReadline.rl_filename_quote_characters.nil?
302
- nil
303
- else
304
- RbReadline.rl_filename_quote_characters.dup
305
- end
306
- end
307
-
308
- # The History class encapsulates a history of all commands entered by
309
- # users at the prompt, providing an interface for inspection and retrieval
310
- # of all commands.
311
- class History
312
- extend Enumerable
313
-
314
- # The History class, stringified in all caps.
315
- #--
316
- # Why?
317
- #
318
- def self.to_s
319
- "HISTORY"
320
- end
321
-
322
- # Returns the command that was entered at the specified +index+
323
- # in the history buffer.
324
- #
325
- # Raises an IndexError if the entry is nil.
326
- #
327
- def self.[](index)
328
- if index < 0
329
- index += RbReadline.history_length
330
- end
331
- entry = RbReadline.history_get(RbReadline.history_base+index)
332
- if entry.nil?
333
- raise IndexError,"invalid index"
334
- end
335
- entry.line.dup
336
- end
337
-
338
- # Sets the command +str+ at the given index in the history buffer.
339
- #
340
- # You can only replace an existing entry. Attempting to create a new
341
- # entry will result in an IndexError.
342
- #
343
- def self.[]=(index,str)
344
- if index<0
345
- index += RbReadline.history_length
346
- end
347
- entry = RbReadline.replace_history_entry(index,str,nil)
348
- if entry.nil?
349
- raise IndexError,"invalid index"
350
- end
351
- str
352
- end
353
-
354
- # Synonym for Readline.add_history.
355
- #
356
- def self.<<(str)
357
- RbReadline.add_history(str)
358
- end
359
-
360
- # Pushes a list of +args+ onto the history buffer.
361
- #
362
- def self.push(*args)
363
- args.each do |str|
364
- RbReadline.add_history(str)
365
- end
366
- end
367
-
368
- # Internal function that removes the item at +index+ from the history
369
- # buffer, performing necessary duplication in the process.
370
- #--
371
- # TODO: mark private?
372
- #
373
- def self.rb_remove_history(index)
374
- entry = RbReadline.remove_history(index)
375
- if (entry)
376
- val = entry.line.dup
377
- entry = nil
378
- return val
379
- end
380
- nil
381
- end
382
-
383
- # Removes and returns the last element from the history buffer.
384
- #
385
- def self.pop()
386
- if RbReadline.history_length>0
387
- rb_remove_history(RbReadline.history_length-1)
388
- else
389
- nil
390
- end
391
- end
392
-
393
- # Removes and returns the first element from the history buffer.
394
- #
395
- def self.shift()
396
- if RbReadline.history_length>0
397
- rb_remove_history(0)
398
- else
399
- nil
400
- end
401
- end
402
-
403
- # Iterates over each entry in the history buffer.
404
- #
405
- def self.each()
406
- for i in 0 ... RbReadline.history_length
407
- entry = RbReadline.history_get(RbReadline.history_base + i)
408
- break if entry.nil?
409
- yield entry.line.dup
410
- end
411
- self
412
- end
413
-
414
- # Returns the length of the history buffer.
415
- #
416
- def self.length()
417
- RbReadline.history_length
418
- end
419
-
420
- # Synonym for Readline.length.
421
- #
422
- def self.size()
423
- RbReadline.history_length
424
- end
425
-
426
- # Returns a bolean value indicating whether or not the history buffer
427
- # is empty.
428
- #
429
- def self.empty?()
430
- RbReadline.history_length == 0
431
- end
432
-
433
- # Deletes an entry from the histoyr buffer at the specified +index+.
434
- #
435
- def self.delete_at(index)
436
- if index < 0
437
- i += RbReadline.history_length
438
- end
439
- if index < 0 || index > RbReadline.history_length - 1
440
- raise IndexError, "invalid index"
441
- end
442
- rb_remove_history(index)
443
- end
444
-
445
- end
446
-
447
- HISTORY = History
448
-
449
- # The Fcomp class provided to encapsulate typical filename completion
450
- # procedure. You will not typically use this directly, but will instead
451
- # use the Readline::FILENAME_COMPLETION_PROC.
452
- #
453
- class Fcomp
454
- def self.call(str)
455
- matches = RbReadline.rl_completion_matches(str,
456
- :rl_filename_completion_function)
457
- if (matches)
458
- result = []
459
- i = 0
460
- while(matches[i])
461
- result << matches[i].dup
462
- matches[i] = nil
463
- i += 1
464
- end
465
- matches = nil
466
- if (result.length >= 2)
467
- result.shift
468
- end
469
- else
470
- result = nil
471
- end
472
- return result
473
- end
474
- end
475
-
476
- FILENAME_COMPLETION_PROC = Fcomp
477
-
478
- # The Ucomp class provided to encapsulate typical filename completion
479
- # procedure. You will not typically use this directly, but will instead
480
- # use the Readline::USERNAME_COMPLETION_PROC.
481
- #
482
- # Note that this feature currently only works on Unix systems since it
483
- # ultimately uses the Etc module to iterate over a list of users.
484
- #
485
- class Ucomp
486
- def self.call(str)
487
- matches = RbReadline.rl_completion_matches(str,
488
- :rl_username_completion_function)
489
- if (matches)
490
- result = []
491
- i = 0
492
- while(matches[i])
493
- result << matches[i].dup
494
- matches[i] = nil
495
- i += 1
496
- end
497
- matches = nil
498
- if (result.length >= 2)
499
- result.shift
500
- end
501
- else
502
- result = nil
503
- end
504
- return result
505
- end
506
- end
507
-
508
- USERNAME_COMPLETION_PROC = Ucomp
509
-
510
- RbReadline.rl_readline_name = "Ruby"
511
-
512
- RbReadline.using_history()
513
-
514
- VERSION = RbReadline.rl_library_version
515
-
516
- module_function :readline
517
-
518
- RbReadline.rl_attempted_completion_function = :readline_attempted_completion_function
519
-
520
- end
1
+ # encoding: US-ASCII
2
+ #
3
+ # readline.rb -- GNU Readline module
4
+ # Copyright (C) 1997-2001 Shugo Maeda
5
+ #
6
+ # Ruby translation by Park Heesob phasis@gmail.com
7
+
8
+ module Readline
9
+
10
+ require 'rbreadline'
11
+ include RbReadline
12
+
13
+ @completion_proc = nil
14
+ @completion_case_fold = false
15
+
16
+ # Begins an interactive terminal process using +prompt+ as the command
17
+ # prompt that users see when they type commands. The method returns the
18
+ # line entered whenever a carriage return is encountered.
19
+ #
20
+ # If an +add_history+ argument is provided, commands entered by users are
21
+ # stored in a history buffer that can be recalled for later use.
22
+ #
23
+ # Note that this method depends on $stdin and $stdout both being open.
24
+ # Because this is meant as an interactive console interface, they should
25
+ # generally not be redirected.
26
+ #
27
+ # Example:
28
+ #
29
+ # loop{ Readline.readline('> ') }
30
+ #
31
+ def readline(prompt, add_history=nil)
32
+ if $stdin.closed?
33
+ raise IOError, "stdin closed"
34
+ end
35
+
36
+ RbReadline.rl_instream = $stdin
37
+ RbReadline.rl_outstream = $stdout
38
+
39
+ begin
40
+ buff = RbReadline.readline(prompt)
41
+ rescue Exception => e
42
+ buff = nil
43
+ RbReadline.rl_cleanup_after_signal()
44
+ RbReadline.rl_deprep_terminal()
45
+ raise e
46
+ end
47
+
48
+ if add_history && buff
49
+ RbReadline.add_history(buff)
50
+ end
51
+
52
+ return buff ? buff.dup : nil
53
+ end
54
+
55
+ # Sets the input stream (an IO object) for readline interaction. The
56
+ # default is <tt>$stdin</tt>.
57
+ #
58
+ def self.input=(input)
59
+ RbReadline.rl_instream = input
60
+ end
61
+
62
+ # Sets the output stream (an IO object) for readline interaction. The
63
+ # default is <tt>$stdout</tt>.
64
+ #
65
+ def self.output=(output)
66
+ RbReadline.rl_outstream = output
67
+ end
68
+
69
+ # Returns current line buffer
70
+ #
71
+ def self.line_buffer
72
+ RbReadline.rl_line_buffer
73
+ end
74
+
75
+ # Sets the auto-completion procedure (i.e. tab auto-complete).
76
+ #
77
+ # The +proc+ argument is typically a Proc object. It must respond to
78
+ # <tt>.call</tt>, take a single String argument and return an Array of
79
+ # candidates for completion.
80
+ #
81
+ # Example:
82
+ #
83
+ # list = ['search', 'next', 'clear']
84
+ # Readline.completion_proc = proc{ |s| list.grep( /^#{Regexp.escape(s)}/) }
85
+ #
86
+ def self.completion_proc=(proc)
87
+ unless defined? proc.call
88
+ raise ArgumentError,"argument must respond to `call'"
89
+ end
90
+ @completion_proc = proc
91
+ end
92
+
93
+ # Returns the current auto-completion procedure.
94
+ #
95
+ def self.completion_proc()
96
+ @completion_proc
97
+ end
98
+
99
+ # Sets whether or not the completion proc should ignore case sensitivity.
100
+ # The default is false, i.e. completion procs are case sensitive.
101
+ #
102
+ def self.completion_case_fold=(bool)
103
+ @completion_case_fold = bool
104
+ end
105
+
106
+ # Returns whether or not the completion proc is case sensitive. The
107
+ # default is false, i.e. completion procs are case sensitive.
108
+ #
109
+ def self.completion_case_fold()
110
+ @completion_case_fold
111
+ end
112
+
113
+ # Returns nil if no matches are found or an array of strings:
114
+ #
115
+ # [0] is the replacement for text
116
+ # [1..n] the possible matches
117
+ # [n+1] nil
118
+ #
119
+ # The possible matches should not include [0].
120
+ #
121
+ # If this method sets RbReadline.rl_attempted_completion_over to true,
122
+ # then the default completion function will not be called when this
123
+ # function returns nil.
124
+ def self.readline_attempted_completion_function(text,start,_end)
125
+ proc = @completion_proc
126
+ return nil if proc.nil?
127
+
128
+ RbReadline.rl_attempted_completion_over = true
129
+
130
+ case_fold = @completion_case_fold
131
+ ary = proc.call(text)
132
+ if ary.class != Array
133
+ ary = Array(ary)
134
+ else
135
+ ary.compact!
136
+ end
137
+
138
+ matches = ary.length
139
+ return nil if (matches == 0)
140
+ result = Array.new(matches+2)
141
+ for i in 0 ... matches
142
+ result[i+1] = ary[i].dup
143
+ end
144
+ result[matches+1] = nil
145
+
146
+ if(matches==1)
147
+ result[0] = result[1].dup
148
+ result[1] = nil
149
+ else
150
+ i = 1
151
+ low = 100000
152
+
153
+ while (i < matches)
154
+ if (case_fold)
155
+ si = 0
156
+ while ((c1 = result[i][si,1].downcase) &&
157
+ (c2 = result[i + 1][si,1].downcase))
158
+ break if (c1 != c2)
159
+ si += 1
160
+ end
161
+ else
162
+ si = 0
163
+ while ((c1 = result[i][si,1]) &&
164
+ (c2 = result[i + 1][si,1]))
165
+ break if (c1 != c2)
166
+ si += 1
167
+ end
168
+ end
169
+ if (low > si)
170
+ low = si
171
+ end
172
+ i+=1
173
+ end
174
+ result[0] = result[1][0,low]
175
+ end
176
+
177
+ result
178
+ end
179
+
180
+ # Sets vi editing mode.
181
+ #
182
+ def self.vi_editing_mode()
183
+ RbReadline.rl_vi_editing_mode(1,0)
184
+ nil
185
+ end
186
+
187
+ # Sets emacs editing mode
188
+ #
189
+ def self.emacs_editing_mode()
190
+ RbReadline.rl_emacs_editing_mode(1,0)
191
+ nil
192
+ end
193
+
194
+ # Sets the character that is automatically appended after the
195
+ # Readline.completion_proc method is called.
196
+ #
197
+ # If +char+ is nil or empty, then a null character is used.
198
+ #
199
+ def self.completion_append_character=(char)
200
+ if char.nil?
201
+ RbReadline.rl_completion_append_character = ?\0
202
+ elsif char.length==0
203
+ RbReadline.rl_completion_append_character = ?\0
204
+ else
205
+ RbReadline.rl_completion_append_character = char[0].chr
206
+ end
207
+ end
208
+
209
+ # Returns the character that is automatically appended after the
210
+ # Readline.completion_proc method is called.
211
+ #
212
+ def self.completion_append_character()
213
+ if RbReadline.rl_completion_append_character == ?\0
214
+ return nil
215
+ end
216
+ return RbReadline.rl_completion_append_character
217
+ end
218
+
219
+ # Sets the character string that signal a break between words for the
220
+ # completion proc.
221
+ #
222
+ def self.basic_word_break_characters=(str)
223
+ RbReadline.rl_basic_word_break_characters = str.dup
224
+ end
225
+
226
+ # Returns the character string that signal a break between words for the
227
+ # completion proc. The default is " \t\n\"\\'`@$><=|&{(".
228
+ #
229
+ def self.basic_word_break_characters()
230
+ if RbReadline.rl_basic_word_break_characters.nil?
231
+ nil
232
+ else
233
+ RbReadline.rl_basic_word_break_characters.dup
234
+ end
235
+ end
236
+
237
+ # Sets the character string that signal the start or end of a word for
238
+ # the completion proc.
239
+ #
240
+ def self.completer_word_break_characters=(str)
241
+ RbReadline.rl_completer_word_break_characters = str.dup
242
+ end
243
+
244
+ # Returns the character string that signal the start or end of a word for
245
+ # the completion proc.
246
+ #
247
+ def self.completer_word_break_characters()
248
+ if RbReadline.rl_completer_word_break_characters.nil?
249
+ nil
250
+ else
251
+ RbReadline.rl_completer_word_break_characters.dup
252
+ end
253
+ end
254
+
255
+ # Sets the list of quote characters that can cause a word break.
256
+ #
257
+ def self.basic_quote_characters=(str)
258
+ RbReadline.rl_basic_quote_characters = str.dup
259
+ end
260
+
261
+ # Returns the list of quote characters that can cause a word break.
262
+ # The default is "'\"" (single and double quote characters).
263
+ #
264
+ def self.basic_quote_characters()
265
+ if RbReadline.rl_basic_quote_characters.nil?
266
+ nil
267
+ else
268
+ RbReadline.rl_basic_quote_characters.dup
269
+ end
270
+ end
271
+
272
+ # Sets the list of characters that can be used to quote a substring of
273
+ # the line, i.e. a group of characters within quotes.
274
+ #
275
+ def self.completer_quote_characters=(str)
276
+ RbReadline.rl_completer_quote_characters = str.dup
277
+ end
278
+
279
+ # Returns the list of characters that can be used to quote a substring
280
+ # of the line, i.e. a group of characters inside quotes.
281
+ #
282
+ def self.completer_quote_characters()
283
+ if RbReadline.rl_completer_quote_characters.nil?
284
+ nil
285
+ else
286
+ RbReadline.rl_completer_quote_characters.dup
287
+ end
288
+ end
289
+
290
+ # Sets the character string of one or more characters that indicate quotes
291
+ # for the filename completion of user input.
292
+ #
293
+ def self.filename_quote_characters=(str)
294
+ RbReadline.rl_filename_quote_characters = str.dup
295
+ end
296
+
297
+ # Returns the character string used to indicate quotes for the filename
298
+ # completion of user input.
299
+ #
300
+ def self.filename_quote_characters()
301
+ if RbReadline.rl_filename_quote_characters.nil?
302
+ nil
303
+ else
304
+ RbReadline.rl_filename_quote_characters.dup
305
+ end
306
+ end
307
+
308
+ # Returns the current offset in the current input line.
309
+ #
310
+ def self.point()
311
+ RbReadline.rl_point
312
+ end
313
+
314
+ # The History class encapsulates a history of all commands entered by
315
+ # users at the prompt, providing an interface for inspection and retrieval
316
+ # of all commands.
317
+ class History
318
+ extend Enumerable
319
+
320
+ # The History class, stringified in all caps.
321
+ #--
322
+ # Why?
323
+ #
324
+ def self.to_s
325
+ "HISTORY"
326
+ end
327
+
328
+ # Returns the command that was entered at the specified +index+
329
+ # in the history buffer.
330
+ #
331
+ # Raises an IndexError if the entry is nil.
332
+ #
333
+ def self.[](index)
334
+ if index < 0
335
+ index += RbReadline.history_length
336
+ end
337
+ entry = RbReadline.history_get(RbReadline.history_base+index)
338
+ if entry.nil?
339
+ raise IndexError,"invalid index"
340
+ end
341
+ entry.line.dup
342
+ end
343
+
344
+ # Sets the command +str+ at the given index in the history buffer.
345
+ #
346
+ # You can only replace an existing entry. Attempting to create a new
347
+ # entry will result in an IndexError.
348
+ #
349
+ def self.[]=(index,str)
350
+ if index<0
351
+ index += RbReadline.history_length
352
+ end
353
+ entry = RbReadline.replace_history_entry(index,str,nil)
354
+ if entry.nil?
355
+ raise IndexError,"invalid index"
356
+ end
357
+ str
358
+ end
359
+
360
+ # Synonym for Readline.add_history.
361
+ #
362
+ def self.<<(str)
363
+ RbReadline.add_history(str)
364
+ end
365
+
366
+ # Pushes a list of +args+ onto the history buffer.
367
+ #
368
+ def self.push(*args)
369
+ args.each do |str|
370
+ RbReadline.add_history(str)
371
+ end
372
+ end
373
+
374
+ # Internal function that removes the item at +index+ from the history
375
+ # buffer, performing necessary duplication in the process.
376
+ #--
377
+ # TODO: mark private?
378
+ #
379
+ def self.rb_remove_history(index)
380
+ entry = RbReadline.remove_history(index)
381
+ if (entry)
382
+ val = entry.line.dup
383
+ entry = nil
384
+ return val
385
+ end
386
+ nil
387
+ end
388
+
389
+ # Removes and returns the last element from the history buffer.
390
+ #
391
+ def self.pop()
392
+ if RbReadline.history_length>0
393
+ rb_remove_history(RbReadline.history_length-1)
394
+ else
395
+ nil
396
+ end
397
+ end
398
+
399
+ # Removes and returns the first element from the history buffer.
400
+ #
401
+ def self.shift()
402
+ if RbReadline.history_length>0
403
+ rb_remove_history(0)
404
+ else
405
+ nil
406
+ end
407
+ end
408
+
409
+ # Iterates over each entry in the history buffer.
410
+ #
411
+ def self.each()
412
+ for i in 0 ... RbReadline.history_length
413
+ entry = RbReadline.history_get(RbReadline.history_base + i)
414
+ break if entry.nil?
415
+ yield entry.line.dup
416
+ end
417
+ self
418
+ end
419
+
420
+ # Returns the length of the history buffer.
421
+ #
422
+ def self.length()
423
+ RbReadline.history_length
424
+ end
425
+
426
+ # Synonym for Readline.length.
427
+ #
428
+ def self.size()
429
+ RbReadline.history_length
430
+ end
431
+
432
+ # Returns a bolean value indicating whether or not the history buffer
433
+ # is empty.
434
+ #
435
+ def self.empty?()
436
+ RbReadline.history_length == 0
437
+ end
438
+
439
+ # Deletes an entry from the histoyr buffer at the specified +index+.
440
+ #
441
+ def self.delete_at(index)
442
+ if index < 0
443
+ i += RbReadline.history_length
444
+ end
445
+ if index < 0 || index > RbReadline.history_length - 1
446
+ raise IndexError, "invalid index"
447
+ end
448
+ rb_remove_history(index)
449
+ end
450
+
451
+ end
452
+
453
+ HISTORY = History
454
+
455
+ # The Fcomp class provided to encapsulate typical filename completion
456
+ # procedure. You will not typically use this directly, but will instead
457
+ # use the Readline::FILENAME_COMPLETION_PROC.
458
+ #
459
+ class Fcomp
460
+ def self.call(str)
461
+ matches = RbReadline.rl_completion_matches(str, :rl_filename_completion_function)
462
+ if (matches)
463
+ result = []
464
+ i = 0
465
+ while(matches[i])
466
+ result << matches[i].dup
467
+ matches[i] = nil
468
+ i += 1
469
+ end
470
+ matches = nil
471
+ if (result.length >= 2)
472
+ result.shift
473
+ end
474
+ else
475
+ result = nil
476
+ end
477
+ return result
478
+ end
479
+ end
480
+
481
+ FILENAME_COMPLETION_PROC = Fcomp
482
+
483
+ # The Ucomp class provided to encapsulate typical filename completion
484
+ # procedure. You will not typically use this directly, but will instead
485
+ # use the Readline::USERNAME_COMPLETION_PROC.
486
+ #
487
+ # Note that this feature currently only works on Unix systems since it
488
+ # ultimately uses the Etc module to iterate over a list of users.
489
+ #
490
+ class Ucomp
491
+ def self.call(str)
492
+ matches = RbReadline.rl_completion_matches(str, :rl_username_completion_function)
493
+ if (matches)
494
+ result = []
495
+ i = 0
496
+ while(matches[i])
497
+ result << matches[i].dup
498
+ matches[i] = nil
499
+ i += 1
500
+ end
501
+ matches = nil
502
+ if (result.length >= 2)
503
+ result.shift
504
+ end
505
+ else
506
+ result = nil
507
+ end
508
+ return result
509
+ end
510
+ end
511
+
512
+ USERNAME_COMPLETION_PROC = Ucomp
513
+
514
+ RbReadline.rl_readline_name = "Ruby"
515
+
516
+ RbReadline.using_history()
517
+
518
+ VERSION = RbReadline.rl_library_version
519
+
520
+ module_function :readline
521
+
522
+ RbReadline.rl_attempted_completion_function = :readline_attempted_completion_function
523
+
524
+ end