rb-readline-r7 0.5.2.0

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