Module: Readline
- Defined in:
- readline.c
Constant Summary collapse
- HISTORY =
The history buffer. It extends Enumerable module, so it behaves just like an array. For example, gets the fifth content that the user input by HISTORY.
history
- FILENAME_COMPLETION_PROC =
The Object with the call method that is a completion for filename. This is sets by Readline.completion_proc= method.
fcomp
- USERNAME_COMPLETION_PROC =
The Object with the call method that is a completion for usernames. This is sets by Readline.completion_proc= method.
ucomp
- VERSION =
Version string of GNU Readline or libedit.
version
Class Method Summary collapse
-
.basic_quote_characters ⇒ String
Gets a list of quote characters which can cause a word break.
-
.basic_quote_characters=(string) ⇒ Object
Sets a list of quote characters which can cause a word break.
-
.basic_word_break_characters ⇒ String
Gets the basic list of characters that signal a break between words for the completer routine.
-
.basic_word_break_characters=(string) ⇒ Object
Sets the basic list of characters that signal a break between words for the completer routine.
-
.completer_quote_characters ⇒ String
Gets a list of characters which can be used to quote a substring of the line.
-
.completer_quote_characters=(string) ⇒ Object
Sets a list of characters which can be used to quote a substring of the line.
-
.completer_word_break_characters ⇒ String
Gets the basic list of characters that signal a break between words for rl_complete_internal().
-
.completer_word_break_characters=(string) ⇒ Object
Sets the basic list of characters that signal a break between words for rl_complete_internal().
-
.completion_append_character ⇒ String
Returns a string containing a character to be appended on completion.
-
.completion_append_character=(char) ⇒ Object
Specifies a character to be appended on completion.
-
.completion_case_fold ⇒ Boolean
Returns true if completion ignores case.
-
.completion_case_fold=(bool) ⇒ Object
Sets whether or not to ignore case on completion.
-
.completion_proc ⇒ Proc
Returns the completion Proc object.
-
.completion_proc=(proc) ⇒ Object
Specifies a Proc object
proc
to determine completion behavior. -
.emacs_editing_mode ⇒ nil
Specifies Emacs editing mode.
-
.emacs_editing_mode? ⇒ Boolean
Returns true if emacs mode is active.
-
.filename_quote_characters ⇒ String
Gets a list of characters that cause a filename to be quoted by the completer when they appear in a completed filename.
-
.filename_quote_characters=(string) ⇒ Object
Sets a list of characters that cause a filename to be quoted by the completer when they appear in a completed filename.
-
.get_screen_size ⇒ Array
Returns the terminal’s rows and columns.
-
.input=(input) ⇒ Object
Specifies a File object
input
that is input stream for Readline.readline method. -
.insert_text(string) ⇒ self
Insert text into the line at the current cursor position.
-
.line_buffer ⇒ String
Returns the full line that is being edited.
-
.output=(output) ⇒ Object
Specifies a File object
output
that is output stream for Readline.readline method. -
.point ⇒ Integer
Returns the index of the current cursor position in
Readline.line_buffer
. -
.pre_input_hook ⇒ Proc
Returns a Proc object
proc
to call after the first prompt has been printed and just before readline starts reading input characters. -
.pre_input_hook=(proc) ⇒ Object
Specifies a Proc object
proc
to call after the first prompt has been printed and just before readline starts reading input characters. -
.readline(prompt = "", add_hist = false) ⇒ String?
Shows the
prompt
and reads the inputted line with line editing. -
.redisplay ⇒ self
Change what’s displayed on the screen to reflect the current contents.
-
.refresh_line ⇒ nil
Clear the current input line.
-
.set_screen_size(rows, columns) ⇒ self
Set terminal size to
rows
andcolumns
. -
.special_prefixes ⇒ String
Gets the list of characters that are word break characters, but should be left in text when it is passed to the completion function.
-
.special_prefixes=(string) ⇒ Object
Sets the list of characters that are word break characters, but should be left in text when it is passed to the completion function.
-
.vi_editing_mode ⇒ nil
Specifies VI editing mode.
-
.vi_editing_mode? ⇒ Boolean
Returns true if vi mode is active.
Class Method Details
.basic_quote_characters ⇒ String
Gets a list of quote characters which can cause a word break.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1311 1312 1313 1314 1315 1316 1317 1318 |
# File 'readline.c', line 1311
static VALUE
readline_s_get_basic_quote_characters(VALUE self, VALUE str)
{
rb_secure(4);
if (rl_basic_quote_characters == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_basic_quote_characters);
}
|
.basic_quote_characters=(string) ⇒ Object
Sets a list of quote characters which can cause a word break.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 |
# File 'readline.c', line 1275
static VALUE
readline_s_set_basic_quote_characters(VALUE self, VALUE str)
{
static char *basic_quote_characters = NULL;
rb_secure(4);
OutputStringValue(str);
if (basic_quote_characters == NULL) {
basic_quote_characters =
ALLOC_N(char, RSTRING_LEN(str) + 1);
}
else {
REALLOC_N(basic_quote_characters, char, RSTRING_LEN(str) + 1);
}
strncpy(basic_quote_characters,
RSTRING_PTR(str), RSTRING_LEN(str));
basic_quote_characters[RSTRING_LEN(str)] = '\0';
rl_basic_quote_characters = basic_quote_characters;
return self;
}
|
.basic_word_break_characters ⇒ String
Gets the basic list of characters that signal a break between words for the completer routine.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1123 1124 1125 1126 1127 1128 1129 1130 |
# File 'readline.c', line 1123
static VALUE
readline_s_get_basic_word_break_characters(VALUE self, VALUE str)
{
rb_secure(4);
if (rl_basic_word_break_characters == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_basic_word_break_characters);
}
|
.basic_word_break_characters=(string) ⇒ Object
Sets the basic list of characters that signal a break between words for the completer routine. The default is the characters which break words for completion in Bash: “ tn"\‘`@$><=;|&{(”.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 |
# File 'readline.c', line 1087
static VALUE
readline_s_set_basic_word_break_characters(VALUE self, VALUE str)
{
static char *basic_word_break_characters = NULL;
rb_secure(4);
OutputStringValue(str);
if (basic_word_break_characters == NULL) {
basic_word_break_characters =
ALLOC_N(char, RSTRING_LEN(str) + 1);
}
else {
REALLOC_N(basic_word_break_characters, char, RSTRING_LEN(str) + 1);
}
strncpy(basic_word_break_characters,
RSTRING_PTR(str), RSTRING_LEN(str));
basic_word_break_characters[RSTRING_LEN(str)] = '\0';
rl_basic_word_break_characters = basic_word_break_characters;
return self;
}
|
.completer_quote_characters ⇒ String
Gets a list of characters which can be used to quote a substring of the line.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1373 1374 1375 1376 1377 1378 1379 1380 |
# File 'readline.c', line 1373
static VALUE
readline_s_get_completer_quote_characters(VALUE self, VALUE str)
{
rb_secure(4);
if (rl_completer_quote_characters == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_completer_quote_characters);
}
|
.completer_quote_characters=(string) ⇒ Object
Sets a list of characters which can be used to quote a substring of the line. Completion occurs on the entire substring, and within the substring Readline.completer_word_break_characters are treated as any other character, unless they also appear within this list.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
# File 'readline.c', line 1337
static VALUE
readline_s_set_completer_quote_characters(VALUE self, VALUE str)
{
static char *completer_quote_characters = NULL;
rb_secure(4);
OutputStringValue(str);
if (completer_quote_characters == NULL) {
completer_quote_characters =
ALLOC_N(char, RSTRING_LEN(str) + 1);
}
else {
REALLOC_N(completer_quote_characters, char, RSTRING_LEN(str) + 1);
}
strncpy(completer_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
completer_quote_characters[RSTRING_LEN(str)] = '\0';
rl_completer_quote_characters = completer_quote_characters;
return self;
}
|
.completer_word_break_characters ⇒ String
Gets the basic list of characters that signal a break between words for rl_complete_internal().
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1184 1185 1186 1187 1188 1189 1190 1191 |
# File 'readline.c', line 1184
static VALUE
readline_s_get_completer_word_break_characters(VALUE self, VALUE str)
{
rb_secure(4);
if (rl_completer_word_break_characters == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_completer_word_break_characters);
}
|
.completer_word_break_characters=(string) ⇒ Object
Sets the basic list of characters that signal a break between words for rl_complete_internal(). The default is the value of Readline.basic_word_break_characters.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 |
# File 'readline.c', line 1148
static VALUE
readline_s_set_completer_word_break_characters(VALUE self, VALUE str)
{
static char *completer_word_break_characters = NULL;
rb_secure(4);
OutputStringValue(str);
if (completer_word_break_characters == NULL) {
completer_word_break_characters =
ALLOC_N(char, RSTRING_LEN(str) + 1);
}
else {
REALLOC_N(completer_word_break_characters, char, RSTRING_LEN(str) + 1);
}
strncpy(completer_word_break_characters,
RSTRING_PTR(str), RSTRING_LEN(str));
completer_word_break_characters[RSTRING_LEN(str)] = '\0';
rl_completer_word_break_characters = completer_word_break_characters;
return self;
}
|
.completion_append_character ⇒ String
Returns a string containing a character to be appended on completion. The default is a space (“ ”).
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'readline.c', line 1058
static VALUE
readline_s_get_completion_append_character(VALUE self)
{
char buf[1];
rb_secure(4);
if (rl_completion_append_character == '\0')
return Qnil;
buf[0] = (char) rl_completion_append_character;
return rb_locale_str_new(buf, 1);
}
|
.completion_append_character=(char) ⇒ Object
Specifies a character to be appended on completion. Nothing will be appended if an empty string (“”) or nil is specified.
For example:
require "readline"
Readline.readline("> ", true)
Readline.completion_append_character = " "
Result:
>
Input "/var/li".
> /var/li
Press TAB key.
> /var/lib
Completes "b" and appends " ". So, you can continuously input "/usr".
> /var/lib /usr
NOTE: Only one character can be specified. When “string” is specified, sets only “s” that is the first.
require "readline"
Readline.completion_append_character = "string"
p Readline.completion_append_character # => "s"
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'readline.c', line 1025
static VALUE
readline_s_set_completion_append_character(VALUE self, VALUE str)
{
rb_secure(4);
if (NIL_P(str)) {
rl_completion_append_character = '\0';
}
else {
OutputStringValue(str);
if (RSTRING_LEN(str) == 0) {
rl_completion_append_character = '\0';
} else {
rl_completion_append_character = RSTRING_PTR(str)[0];
}
}
return self;
}
|
.completion_case_fold ⇒ Boolean
Returns true if completion ignores case. If no, returns false.
NOTE: Returns the same object that is specified by Readline.completion_case_fold= method.
require "readline"
Readline.completion_case_fold = "This is a String."
p Readline.completion_case_fold # => "This is a String."
Raises SecurityError exception if $SAFE is 4.
715 716 717 718 719 720 |
# File 'readline.c', line 715
static VALUE
readline_s_get_completion_case_fold(VALUE self)
{
rb_secure(4);
return rb_attr_get(mReadline, completion_case_fold);
}
|
.completion_case_fold=(bool) ⇒ Object
Sets whether or not to ignore case on completion.
Raises SecurityError exception if $SAFE is 4.
692 693 694 695 696 697 |
# File 'readline.c', line 692
static VALUE
readline_s_set_completion_case_fold(VALUE self, VALUE val)
{
rb_secure(4);
return rb_ivar_set(mReadline, completion_case_fold, val);
}
|
.completion_proc ⇒ Proc
Returns the completion Proc object.
Raises SecurityError exception if $SAFE is 4.
677 678 679 680 681 682 |
# File 'readline.c', line 677
static VALUE
readline_s_get_completion_proc(VALUE self)
{
rb_secure(4);
return rb_attr_get(mReadline, completion_proc);
}
|
.completion_proc=(proc) ⇒ Object
Specifies a Proc object proc
to determine completion behavior. It should take input string and return an array of completion candidates.
The default completion is used if proc
is nil.
The String that is passed to the Proc depends on the Readline.completer_word_break_characters property. By default the word under the cursor is passed to the Proc. For example, if the input is “foo bar” then only “bar” would be passed to the completion Proc.
Upon successful completion the Readline.completion_append_character will be appended to the input so the user can start working on their next argument.
Examples
Completion for a Static List
require 'readline'
LIST = [
'search', 'download', 'open',
'help', 'history', 'quit',
'url', 'next', 'clear',
'prev', 'past'
].sort
comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) }
Readline.completion_append_character = " "
Readline.completion_proc = comp
while line = Readline.readline('> ', true)
p line
end
Completion For Directory Contents
require 'readline'
Readline.completion_append_character = " "
Readline.completion_proc = Proc.new do |str|
Dir[str+'*'].grep(/^#{Regexp.escape(str)}/)
end
while line = Readline.readline('> ', true)
p line
end
Autocomplete strategies
When working with auto-complete there are some strategies that work well. To get some ideas you can take a look at the completion.rb file for irb.
The common strategy is to take a list of possible completions and filter it down to those completions that start with the user input. In the above examples Enumerator.grep is used. The input is escaped to prevent Regexp special characters from interfering with the matching.
It may also be helpful to use the Abbrev library to generate completions.
Raises ArgumentError if proc
does not respond to the call method.
Raises SecurityError if $SAFE is 4.
660 661 662 663 664 665 666 667 |
# File 'readline.c', line 660
static VALUE
readline_s_set_completion_proc(VALUE self, VALUE proc)
{
rb_secure(4);
if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call")))
rb_raise(rb_eArgError, "argument must respond to `call'");
return rb_ivar_set(mReadline, completion_proc, proc);
}
|
.emacs_editing_mode ⇒ nil
Specifies Emacs editing mode. The default is this mode. See the manual of GNU Readline for details of Emacs editing mode.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
954 955 956 957 958 959 960 |
# File 'readline.c', line 954
static VALUE
readline_s_emacs_editing_mode(VALUE self)
{
rb_secure(4);
rl_emacs_editing_mode(1,0);
return Qnil;
}
|
.emacs_editing_mode? ⇒ Boolean
Returns true if emacs mode is active. Returns false if not.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
976 977 978 979 980 981 |
# File 'readline.c', line 976
static VALUE
readline_s_emacs_editing_mode_p(VALUE self)
{
rb_secure(4);
return rl_editing_mode == 1 ? Qtrue : Qfalse;
}
|
.filename_quote_characters ⇒ String
Gets a list of characters that cause a filename to be quoted by the completer when they appear in a completed filename.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1433 1434 1435 1436 1437 1438 1439 1440 |
# File 'readline.c', line 1433
static VALUE
readline_s_get_filename_quote_characters(VALUE self, VALUE str)
{
rb_secure(4);
if (rl_filename_quote_characters == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_filename_quote_characters);
}
|
.filename_quote_characters=(string) ⇒ Object
Sets a list of characters that cause a filename to be quoted by the completer when they appear in a completed filename. The default is nil.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 |
# File 'readline.c', line 1397
static VALUE
readline_s_set_filename_quote_characters(VALUE self, VALUE str)
{
static char *filename_quote_characters = NULL;
rb_secure(4);
OutputStringValue(str);
if (filename_quote_characters == NULL) {
filename_quote_characters =
ALLOC_N(char, RSTRING_LEN(str) + 1);
}
else {
REALLOC_N(filename_quote_characters, char, RSTRING_LEN(str) + 1);
}
strncpy(filename_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
filename_quote_characters[RSTRING_LEN(str)] = '\0';
rl_filename_quote_characters = filename_quote_characters;
return self;
}
|
.get_screen_size ⇒ Array
Returns the terminal’s rows and columns.
See GNU Readline’s rl_get_screen_size function.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'readline.c', line 881
static VALUE
readline_s_get_screen_size(VALUE self)
{
int rows, columns;
VALUE res;
rb_secure(4);
rl_get_screen_size(&rows, &columns);
res = rb_ary_new();
rb_ary_push(res, INT2NUM(rows));
rb_ary_push(res, INT2NUM(columns));
return res;
}
|
.input=(input) ⇒ Object
Specifies a File object input
that is input stream for Readline.readline method.
Raises SecurityError exception if $SAFE is 4.
452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'readline.c', line 452
static VALUE
readline_s_set_input(VALUE self, VALUE input)
{
rb_io_t *ifp;
rb_secure(4);
Check_Type(input, T_FILE);
GetOpenFile(input, ifp);
rl_instream = rb_io_stdio_file(ifp);
#ifdef HAVE_RL_GETC_FUNCTION
readline_instream = input;
#endif
return input;
}
|
.insert_text(string) ⇒ self
Insert text into the line at the current cursor position.
See GNU Readline’s rl_insert_text function.
Raises SecurityError if $SAFE is 4.
555 556 557 558 559 560 561 562 |
# File 'readline.c', line 555
static VALUE
readline_s_insert_text(VALUE self, VALUE str)
{
rb_secure(4);
OutputStringValue(str);
rl_insert_text(RSTRING_PTR(str));
return self;
}
|
.line_buffer ⇒ String
Returns the full line that is being edited. This is useful from within the complete_proc for determining the context of the completion request.
The length of Readline.line_buffer
and GNU Readline’s rl_end are same.
734 735 736 737 738 739 740 741 |
# File 'readline.c', line 734
static VALUE
readline_s_get_line_buffer(VALUE self)
{
rb_secure(4);
if (rl_line_buffer == NULL)
return Qnil;
return rb_locale_str_new_cstr(rl_line_buffer);
}
|
.output=(output) ⇒ Object
Specifies a File object output
that is output stream for Readline.readline method.
Raises SecurityError exception if $SAFE is 4.
476 477 478 479 480 481 482 483 484 485 486 |
# File 'readline.c', line 476
static VALUE
readline_s_set_output(VALUE self, VALUE output)
{
rb_io_t *ofp;
rb_secure(4);
Check_Type(output, T_FILE);
GetOpenFile(output, ofp);
rl_outstream = rb_io_stdio_file(ofp);
return output;
}
|
.point ⇒ Integer
Returns the index of the current cursor position in Readline.line_buffer
.
The index in Readline.line_buffer
which matches the start of input-string passed to completion_proc is computed by subtracting the length of input-string from Readline.point
.
start = (the length of input-string) - Readline.point
760 761 762 763 764 765 |
# File 'readline.c', line 760
static VALUE
readline_s_get_point(VALUE self)
{
rb_secure(4);
return INT2NUM(rl_point);
}
|
.pre_input_hook ⇒ Proc
Returns a Proc object proc
to call after the first prompt has been printed and just before readline starts reading input characters. The default is nil.
Raises SecurityError if $SAFE is 4.
522 523 524 525 526 527 |
# File 'readline.c', line 522
static VALUE
readline_s_get_pre_input_hook(VALUE self)
{
rb_secure(4);
return rb_attr_get(mReadline, id_pre_input_hook);
}
|
.pre_input_hook=(proc) ⇒ Object
Specifies a Proc object proc
to call after the first prompt has been printed and just before readline starts reading input characters.
See GNU Readline’s rl_pre_input_hook variable.
Raises ArgumentError if proc
does not respond to the call method.
Raises SecurityError if $SAFE is 4.
503 504 505 506 507 508 509 510 |
# File 'readline.c', line 503
static VALUE
readline_s_set_pre_input_hook(VALUE self, VALUE proc)
{
rb_secure(4);
if (!NIL_P(proc) && !rb_respond_to(proc, rb_intern("call")))
rb_raise(rb_eArgError, "argument must respond to `call'");
return rb_ivar_set(mReadline, id_pre_input_hook, proc);
}
|
.readline(prompt = "", add_hist = false) ⇒ String?
Shows the prompt
and reads the inputted line with line editing. The inputted line is added to the history if add_hist
is true.
Returns nil when the inputted line is empty and user inputs EOF (Presses ^D on UNIX).
Raises IOError exception if below conditions are satisfied.
-
stdin is not tty.
-
stdin was closed. (errno is EBADF after called isatty(2).)
This method supports thread. Switchs the thread context when waits inputting line.
Supports line edit when inputs line. Provides VI and Emacs editing mode. Default is Emacs editing mode.
NOTE: Terminates ruby interpreter and does not return the terminal status after user pressed ‘^C’ when wait inputting line. Give 3 examples that avoid it.
-
Catches the Interrupt exception by pressed ^C after returns terminal status:
require "readline" stty_save = `stty -g`.chomp begin while buf = Readline.readline p buf end rescue Interrupt system("stty", stty_save) exit end end end
-
Catches the INT signal by pressed ^C after returns terminal status:
require "readline" stty_save = `stty -g`.chomp trap("INT") { system "stty", stty_save; exit } while buf = Readline.readline p buf end
-
Ignores pressing ^C:
require "readline" trap("INT", "SIG_IGN") while buf = Readline.readline p buf end
Can make as follows with Readline::HISTORY constant. It does not record to the history if the inputted line is empty or the same it as last one.
require "readline"
while buf = Readline.readline("> ", true)
# p Readline::HISTORY.to_a
Readline::HISTORY.pop if /^\s*$/ =~ buf
begin
if Readline::HISTORY[Readline::HISTORY.length-2] == buf
Readline::HISTORY.pop
end
rescue IndexError
end
# p Readline::HISTORY.to_a
print "-> ", buf, "\n"
end
Raises SecurityError exception if $SAFE is 4.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'readline.c', line 377
static VALUE
readline_readline(int argc, VALUE *argv, VALUE self)
{
VALUE tmp, add_hist, result;
char *prompt = NULL;
char *buff;
int status;
rb_secure(4);
if (rb_scan_args(argc, argv, "02", &tmp, &add_hist) > 0) {
OutputStringValue(tmp);
#if USE_INSERT_IGNORE_ESCAPE
tmp = insert_ignore_escape(self, tmp);
rb_str_locktmp(tmp);
#endif
prompt = RSTRING_PTR(tmp);
}
if (!isatty(fileno(rl_instream)) && errno == EBADF) rb_raise(rb_eIOError, "closed stdin");
if (rl_outstream) {
struct stat stbuf;
int fd = fileno(rl_outstream);
if (fd < 0 || fstat(fd, &stbuf) != 0) {
rb_raise(rb_eIOError, "closed stdout");
}
}
#ifdef _WIN32
rl_prep_terminal(1);
#endif
buff = (char*)rb_protect(readline_get, (VALUE)prompt, &status);
#if USE_INSERT_IGNORE_ESCAPE
if (prompt) {
rb_str_unlocktmp(tmp);
}
#endif
if (status) {
#if defined HAVE_RL_CLEANUP_AFTER_SIGNAL
/* restore terminal mode and signal handler*/
#if defined HAVE_RL_FREE_LINE_STATE
rl_free_line_state();
#endif
rl_cleanup_after_signal();
#elif defined HAVE_RL_DEPREP_TERM_FUNCTION
/* restore terminal mode */
if (rl_deprep_term_function != NULL) /* NULL in libedit. [ruby-dev:29116] */
(*rl_deprep_term_function)();
else
#else
rl_deprep_terminal();
#endif
rb_jump_tag(status);
}
if (RTEST(add_hist) && buff) {
add_history(buff);
}
if (buff) {
result = rb_locale_str_new_cstr(buff);
}
else
result = Qnil;
if (buff) free(buff);
return result;
}
|
.redisplay ⇒ self
Change what’s displayed on the screen to reflect the current contents.
See GNU Readline’s rl_redisplay function.
Raises SecurityError if $SAFE is 4.
579 580 581 582 583 584 585 |
# File 'readline.c', line 579
static VALUE
readline_s_redisplay(VALUE self)
{
rb_secure(4);
rl_redisplay();
return self;
}
|
.refresh_line ⇒ nil
Clear the current input line.
Raises SecurityError exception if $SAFE is 4.
1454 1455 1456 1457 1458 1459 1460 |
# File 'readline.c', line 1454
static VALUE
readline_s_refresh_line(VALUE self)
{
rb_secure(4);
rl_refresh_line(0, 0);
return Qnil;
}
|
.set_screen_size(rows, columns) ⇒ self
Set terminal size to rows
and columns
.
See GNU Readline’s rl_set_screen_size function.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
857 858 859 860 861 862 863 |
# File 'readline.c', line 857
static VALUE
readline_s_set_screen_size(VALUE self, VALUE rows, VALUE columns)
{
rb_secure(4);
rl_set_screen_size(NUM2INT(rows), NUM2INT(columns));
return self;
}
|
.special_prefixes ⇒ String
Gets the list of characters that are word break characters, but should be left in text when it is passed to the completion function.
See GNU Readline’s rl_special_prefixes variable.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
# File 'readline.c', line 1246
static VALUE
readline_s_get_special_prefixes(VALUE self)
{
VALUE str;
rb_secure(4);
if (rl_special_prefixes == NULL) return Qnil;
str = rb_ivar_get(mReadline, id_special_prefixes);
if (!NIL_P(str)) {
str = rb_str_dup_frozen(str);
RBASIC(str)->klass = rb_cString;
}
return str;
}
|
.special_prefixes=(string) ⇒ Object
Sets the list of characters that are word break characters, but should be left in text when it is passed to the completion function. Programs can use this to help determine what kind of completing to do. For instance, Bash sets this variable to “$@” so that it can complete shell variables and hostnames.
See GNU Readline’s rl_special_prefixes variable.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
# File 'readline.c', line 1213
static VALUE
readline_s_set_special_prefixes(VALUE self, VALUE str)
{
rb_secure(4);
if (!NIL_P(str)) {
OutputStringValue(str);
str = rb_str_dup_frozen(str);
RBASIC(str)->klass = 0;
}
rb_ivar_set(mReadline, id_special_prefixes, str);
if (NIL_P(str)) {
rl_special_prefixes = NULL;
}
else {
rl_special_prefixes = RSTRING_PTR(str);
}
return self;
}
|
.vi_editing_mode ⇒ nil
Specifies VI editing mode. See the manual of GNU Readline for details of VI editing mode.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
910 911 912 913 914 915 916 |
# File 'readline.c', line 910
static VALUE
readline_s_vi_editing_mode(VALUE self)
{
rb_secure(4);
rl_vi_editing_mode(1,0);
return Qnil;
}
|
.vi_editing_mode? ⇒ Boolean
Returns true if vi mode is active. Returns false if not.
Raises NotImplementedError if the using readline library does not support.
Raises SecurityError exception if $SAFE is 4.
932 933 934 935 936 937 |
# File 'readline.c', line 932
static VALUE
readline_s_vi_editing_mode_p(VALUE self)
{
rb_secure(4);
return rl_editing_mode == 0 ? Qtrue : Qfalse;
}
|