1 /************************************************
3 readline.c - GNU Readline module
6 created at: Wed Jan 20 13:59:32 JST 1999
8 Copyright (C) 1997-2008 Shugo Maeda
9 Copyright (C) 2008 TAKAO Kouji
11 $Id: readline.c 20709 2008-12-13 01:59:19Z yugui $
14 - TAKAO Kouji <kouji at takao7 dot net> (current maintainer)
16 ************************************************/
19 #include RUBY_EXTCONF_H
22 #include "ruby/config.h"
26 #ifdef HAVE_READLINE_READLINE_H
27 #include <readline/readline.h>
29 #ifdef HAVE_READLINE_HISTORY_H
30 #include <readline/history.h>
32 #ifdef HAVE_EDITLINE_READLINE_H
33 #include <editline/readline.h>
36 #include "ruby/ruby.h"
43 static VALUE mReadline;
45 #define EDIT_LINE_LIBRARY_VERSION "EditLine wrapper"
47 #define COMPLETION_PROC "completion_proc"
48 #define COMPLETION_CASE_FOLD "completion_case_fold"
49 static ID completion_proc, completion_case_fold;
51 #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
52 # define rl_filename_completion_function filename_completion_function
54 #ifndef HAVE_RL_USERNAME_COMPLETION_FUNCTION
55 # define rl_username_completion_function username_completion_function
57 #ifndef HAVE_RL_COMPLETION_MATCHES
58 # define rl_completion_matches completion_matches
61 static int (*history_get_offset_func)(int);
63 static char **readline_attempted_completion_function(const char *text,
66 #define OutputStringValue(str) do {\
67 SafeStringValue(str);\
68 str = rb_str_conv_enc(str, rb_enc_get(str), rb_locale_encoding());\
71 #ifdef HAVE_RL_EVENT_HOOK
75 * Document-class: Readline
77 * The Readline module provides interface for GNU Readline.
78 * This module defines a number of methods to facilitate completion
79 * and accesses input history from the Ruby interpreter.
80 * This module supported Edit Line(libedit) too.
81 * libedit is compatible with GNU Readline.
83 * GNU Readline:: http://www.gnu.org/directory/readline.html
84 * libedit:: http://www.thrysoee.dk/editline/
86 * Reads one inputted line with line edit by Readline.readline method.
87 * At this time, the facilitatation completion and the key
88 * bind like Emacs can be operated like GNU Readline.
91 * while buf = Readline.readline("> ", true)
95 * The content that the user input can be recorded to the history.
96 * The history can be accessed by Readline::HISTORY constant.
99 * while buf = Readline.readline("> ", true)
100 * p Readline::HISTORY.to_a
101 * print("-> ", buf, "\n")
104 * Most of methods raise SecurityError exception if $SAFE is 4.
106 * Documented by TAKAO Kouji <kouji at takao7 dot net>.
109 static int readline_event(void);
114 rb_thread_schedule();
119 FD_SET(fileno(rl_instream), &rset);
120 rb_thread_select(fileno(rl_instream) + 1, &rset, NULL, NULL, NULL);
128 * Readline.readline(prompt = "", add_hist = false) -> string or nil
130 * Shows the +prompt+ and reads the inputted line with line editing.
131 * The inputted line is added to the history if +add_hist+ is true.
133 * Returns nil when the inputted line is empty and user inputs EOF
134 * (Presses ^D on UNIX).
136 * Raises IOError exception if below conditions are satisfied.
137 * 1. stdin is not tty.
138 * 2. stdin was closed. (errno is EBADF after called isatty(2).)
140 * This method supports thread. Switchs the thread context when waits
143 * Supports line edit when inputs line. Provides VI and Emacs editing mode.
144 * Default is Emacs editing mode.
146 * NOTE: Terminates ruby interpreter and does not return the terminal
147 * status after user pressed '^C' when wait inputting line.
148 * Give 3 examples that avoid it.
150 * * Catches the Interrupt exception by pressed ^C after returns
155 * stty_save = `stty -g`.chomp
157 * while buf = Readline.readline
161 * system("stty", stty_save)
167 * * Catches the INT signal by pressed ^C after returns terminal
172 * stty_save = `stty -g`.chomp
173 * trap("INT") { system "stty", stty_save; exit }
175 * while buf = Readline.readline
179 * * Ignores pressing ^C:
183 * trap("INT", "SIG_IGN")
185 * while buf = Readline.readline
189 * Can make as follows with Readline::HISTORY constant.
190 * It does not record to the history if the inputted line is empty or
191 * the same it as last one.
195 * while buf = Readline.readline("> ", true)
196 * # p Readline::HISTORY.to_a
197 * Readline::HISTORY.pop if /^\s*$/ =~ buf
200 * if Readline::HISTORY[Readline::HISTORY.length-2] == buf
201 * Readline::HISTORY.pop
206 * # p Readline::HISTORY.to_a
207 * print "-> ", buf, "\n"
210 * Raises SecurityError exception if $SAFE is 4.
213 readline_readline(int argc, VALUE *argv, VALUE self)
215 VALUE tmp, add_hist, result;
221 if (rb_scan_args(argc, argv, "02", &tmp, &add_hist) > 0) {
222 OutputStringValue(tmp);
223 prompt = RSTRING_PTR(tmp);
226 if (!isatty(0) && errno == EBADF) rb_raise(rb_eIOError, "closed stdin");
228 buff = (char*)rb_protect((VALUE(*)_((VALUE)))readline, (VALUE)prompt,
231 #if defined HAVE_RL_CLEANUP_AFTER_SIGNAL
232 /* restore terminal mode and signal handler*/
233 rl_cleanup_after_signal();
234 #elif defined HAVE_RL_DEPREP_TERM_FUNCTION
235 /* restore terminal mode */
236 if (rl_deprep_term_function != NULL) /* NULL in libedit. [ruby-dev:29116] */
237 (*rl_deprep_term_function)();
240 rl_deprep_terminal();
245 if (RTEST(add_hist) && buff) {
249 result = rb_locale_str_new_cstr(buff);
253 if (buff) free(buff);
259 * Readline.input = input
261 * Specifies a File object +input+ that is input stream for
262 * Readline.readline method.
264 * Raises SecurityError exception if $SAFE is 4.
267 readline_s_set_input(VALUE self, VALUE input)
272 Check_Type(input, T_FILE);
273 GetOpenFile(input, ifp);
274 rl_instream = rb_io_stdio_file(ifp);
280 * Readline.output = output
282 * Specifies a File object +output+ that is output stream for
283 * Readline.readline method.
285 * Raises SecurityError exception if $SAFE is 4.
288 readline_s_set_output(VALUE self, VALUE output)
293 Check_Type(output, T_FILE);
294 GetOpenFile(output, ofp);
295 rl_outstream = rb_io_stdio_file(ofp);
301 * Readline.completion_proc = proc
303 * Specifies a Proc object +proc+ to determine completion behavior. It
304 * should take input-string, and return an array of completion
307 * Raises ArgumentError exception if +proc+ does not respond to call method.
309 * Raises SecurityError exception if $SAFE is 4.
312 readline_s_set_completion_proc(VALUE self, VALUE proc)
315 if (!rb_respond_to(proc, rb_intern("call")))
316 rb_raise(rb_eArgError, "argument must respond to `call'");
317 return rb_ivar_set(mReadline, completion_proc, proc);
322 * Readline.completion_proc -> proc
324 * Returns the completion Proc object.
326 * Raises SecurityError exception if $SAFE is 4.
329 readline_s_get_completion_proc(VALUE self)
332 return rb_attr_get(mReadline, completion_proc);
337 * Readline.completion_case_fold = bool
339 * Sets whether or not to ignore case on completion.
341 * Raises SecurityError exception if $SAFE is 4.
344 readline_s_set_completion_case_fold(VALUE self, VALUE val)
347 return rb_ivar_set(mReadline, completion_case_fold, val);
352 * Readline.completion_case_fold -> bool
354 * Returns true if completion ignores case. If no, returns false.
356 * NOTE: Returns the same object that is specified by
357 * Readline.completion_case_fold= method.
361 * Readline.completion_case_fold = "This is a String."
362 * p Readline.completion_case_fold # => "This is a String."
364 * Raises SecurityError exception if $SAFE is 4.
367 readline_s_get_completion_case_fold(VALUE self)
370 return rb_attr_get(mReadline, completion_case_fold);
374 readline_attempted_completion_function(const char *text, int start, int end)
376 VALUE proc, ary, temp;
381 proc = rb_attr_get(mReadline, completion_proc);
384 #ifdef HAVE_RL_ATTEMPTED_COMPLETION_OVER
385 rl_attempted_completion_over = 1;
387 case_fold = RTEST(rb_attr_get(mReadline, completion_case_fold));
388 ary = rb_funcall(proc, rb_intern("call"), 1, rb_locale_str_new_cstr(text));
389 if (TYPE(ary) != T_ARRAY)
391 matches = RARRAY_LEN(ary);
394 result = ALLOC_N(char *, matches + 2);
395 for (i = 0; i < matches; i++) {
396 temp = rb_obj_as_string(RARRAY_PTR(ary)[i]);
397 result[i + 1] = ALLOC_N(char, RSTRING_LEN(temp) + 1);
398 strcpy(result[i + 1], RSTRING_PTR(temp));
400 result[matches + 1] = NULL;
403 result[0] = strdup(result[1]);
409 while (i < matches) {
410 register int c1, c2, si;
414 (c1 = TOLOWER(result[i][si])) &&
415 (c2 = TOLOWER(result[i + 1][si]));
420 (c1 = result[i][si]) &&
421 (c2 = result[i + 1][si]);
426 if (low > si) low = si;
429 result[0] = ALLOC_N(char, low + 1);
430 strncpy(result[0], result[1], low);
431 result[0][low] = '\0';
439 * Readline.vi_editing_mode -> nil
441 * Specifies VI editing mode. See the manual of GNU Readline for
442 * details of VI editing mode.
444 * Raises NotImplementedError if the using readline library does not support.
446 * Raises SecurityError exception if $SAFE is 4.
449 readline_s_vi_editing_mode(VALUE self)
451 #ifdef HAVE_RL_VI_EDITING_MODE
453 rl_vi_editing_mode(1,0);
457 return Qnil; /* not reached */
458 #endif /* HAVE_RL_VI_EDITING_MODE */
463 * Readline.vi_editing_mode? -> bool
465 * Returns true if vi mode is active. Returns false if not.
467 * Raises NotImplementedError if the using readline library does not support.
469 * Raises SecurityError exception if $SAFE is 4.
472 readline_s_vi_editing_mode_p(VALUE self)
474 #ifdef HAVE_RL_EDITING_MODE
476 return rl_editing_mode == 0 ? Qtrue : Qfalse;
479 return Qnil; /* not reached */
480 #endif /* HAVE_RL_EDITING_MODE */
485 * Readline.emacs_editing_mode -> nil
487 * Specifies Emacs editing mode. The default is this mode. See the
488 * manual of GNU Readline for details of Emacs editing mode.
490 * Raises NotImplementedError if the using readline library does not support.
492 * Raises SecurityError exception if $SAFE is 4.
495 readline_s_emacs_editing_mode(VALUE self)
497 #ifdef HAVE_RL_EMACS_EDITING_MODE
499 rl_emacs_editing_mode(1,0);
503 return Qnil; /* not reached */
504 #endif /* HAVE_RL_EMACS_EDITING_MODE */
509 * Readline.emacs_editing_mode? -> bool
511 * Returns true if emacs mode is active. Returns false if not.
513 * Raises NotImplementedError if the using readline library does not support.
515 * Raises SecurityError exception if $SAFE is 4.
518 readline_s_emacs_editing_mode_p(VALUE self)
520 #ifdef HAVE_RL_EDITING_MODE
522 return rl_editing_mode == 1 ? Qtrue : Qfalse;
525 return Qnil; /* not reached */
526 #endif /* HAVE_RL_EDITING_MODE */
531 * Readline.completion_append_character = char
533 * Specifies a character to be appended on completion.
534 * Nothing will be appended if an empty string ("") or nil is
540 * Readline.readline("> ", true)
541 * Readline.completion_append_character = " "
551 * Completes "b" and appends " ". So, you can continuously input "/usr".
555 * NOTE: Only one character can be specified. When "string" is
556 * specified, sets only "s" that is the first.
560 * Readline.completion_append_character = "string"
561 * p Readline.completion_append_character # => "s"
563 * Raises NotImplementedError if the using readline library does not support.
565 * Raises SecurityError exception if $SAFE is 4.
568 readline_s_set_completion_append_character(VALUE self, VALUE str)
570 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
573 rl_completion_append_character = '\0';
576 OutputStringValue(str);
577 if (RSTRING_LEN(str) == 0) {
578 rl_completion_append_character = '\0';
580 rl_completion_append_character = RSTRING_PTR(str)[0];
586 return Qnil; /* not reached */
587 #endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */
592 * Readline.completion_append_character -> char
594 * Returns a string containing a character to be appended on
595 * completion. The default is a space (" ").
597 * Raises NotImplementedError if the using readline library does not support.
599 * Raises SecurityError exception if $SAFE is 4.
602 readline_s_get_completion_append_character(VALUE self)
604 #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
608 if (rl_completion_append_character == '\0')
611 buf[0] = (char) rl_completion_append_character;
612 return rb_locale_str_new(buf, 1);
615 return Qnil; /* not reached */
616 #endif /* HAVE_RL_COMPLETION_APPEND_CHARACTER */
621 * Readline.basic_word_break_characters = string
623 * Sets the basic list of characters that signal a break between words
624 * for the completer routine. The default is the characters which
625 * break words for completion in Bash: "\t\n\"\\'`@$><=;|&{(".
627 * Raises NotImplementedError if the using readline library does not support.
629 * Raises SecurityError exception if $SAFE is 4.
632 readline_s_set_basic_word_break_characters(VALUE self, VALUE str)
634 #ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
635 static char *basic_word_break_characters = NULL;
638 OutputStringValue(str);
639 if (basic_word_break_characters == NULL) {
640 basic_word_break_characters =
641 ALLOC_N(char, RSTRING_LEN(str) + 1);
644 REALLOC_N(basic_word_break_characters, char, RSTRING_LEN(str) + 1);
646 strncpy(basic_word_break_characters,
647 RSTRING_PTR(str), RSTRING_LEN(str));
648 basic_word_break_characters[RSTRING_LEN(str)] = '\0';
649 rl_basic_word_break_characters = basic_word_break_characters;
653 return Qnil; /* not reached */
654 #endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */
659 * Readline.basic_word_break_characters -> string
661 * Gets the basic list of characters that signal a break between words
662 * for the completer routine.
664 * Raises NotImplementedError if the using readline library does not support.
666 * Raises SecurityError exception if $SAFE is 4.
669 readline_s_get_basic_word_break_characters(VALUE self, VALUE str)
671 #ifdef HAVE_RL_BASIC_WORD_BREAK_CHARACTERS
673 if (rl_basic_word_break_characters == NULL)
675 return rb_locale_str_new_cstr(rl_basic_word_break_characters);
678 return Qnil; /* not reached */
679 #endif /* HAVE_RL_BASIC_WORD_BREAK_CHARACTERS */
684 * Readline.completer_word_break_characters = string
686 * Sets the basic list of characters that signal a break between words
687 * for rl_complete_internal(). The default is the value of
688 * Readline.basic_word_break_characters.
690 * Raises NotImplementedError if the using readline library does not support.
692 * Raises SecurityError exception if $SAFE is 4.
695 readline_s_set_completer_word_break_characters(VALUE self, VALUE str)
697 #ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
698 static char *completer_word_break_characters = NULL;
701 OutputStringValue(str);
702 if (completer_word_break_characters == NULL) {
703 completer_word_break_characters =
704 ALLOC_N(char, RSTRING_LEN(str) + 1);
707 REALLOC_N(completer_word_break_characters, char, RSTRING_LEN(str) + 1);
709 strncpy(completer_word_break_characters,
710 RSTRING_PTR(str), RSTRING_LEN(str));
711 completer_word_break_characters[RSTRING_LEN(str)] = '\0';
712 rl_completer_word_break_characters = completer_word_break_characters;
716 return Qnil; /* not reached */
717 #endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */
722 * Readline.completer_word_break_characters -> string
724 * Gets the basic list of characters that signal a break between words
725 * for rl_complete_internal().
727 * Raises NotImplementedError if the using readline library does not support.
729 * Raises SecurityError exception if $SAFE is 4.
732 readline_s_get_completer_word_break_characters(VALUE self, VALUE str)
734 #ifdef HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS
736 if (rl_completer_word_break_characters == NULL)
738 return rb_locale_str_new_cstr(rl_completer_word_break_characters);
741 return Qnil; /* not reached */
742 #endif /* HAVE_RL_COMPLETER_WORD_BREAK_CHARACTERS */
747 * Readline.basic_quote_characters = string
749 * Sets a list of quote characters which can cause a word break.
751 * Raises NotImplementedError if the using readline library does not support.
753 * Raises SecurityError exception if $SAFE is 4.
756 readline_s_set_basic_quote_characters(VALUE self, VALUE str)
758 #ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
759 static char *basic_quote_characters = NULL;
762 OutputStringValue(str);
763 if (basic_quote_characters == NULL) {
764 basic_quote_characters =
765 ALLOC_N(char, RSTRING_LEN(str) + 1);
768 REALLOC_N(basic_quote_characters, char, RSTRING_LEN(str) + 1);
770 strncpy(basic_quote_characters,
771 RSTRING_PTR(str), RSTRING_LEN(str));
772 basic_quote_characters[RSTRING_LEN(str)] = '\0';
773 rl_basic_quote_characters = basic_quote_characters;
778 return Qnil; /* not reached */
779 #endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */
784 * Readline.basic_quote_characters -> string
786 * Gets a list of quote characters which can cause a word break.
788 * Raises NotImplementedError if the using readline library does not support.
790 * Raises SecurityError exception if $SAFE is 4.
793 readline_s_get_basic_quote_characters(VALUE self, VALUE str)
795 #ifdef HAVE_RL_BASIC_QUOTE_CHARACTERS
797 if (rl_basic_quote_characters == NULL)
799 return rb_locale_str_new_cstr(rl_basic_quote_characters);
802 return Qnil; /* not reached */
803 #endif /* HAVE_RL_BASIC_QUOTE_CHARACTERS */
808 * Readline.completer_quote_characters = string
810 * Sets a list of characters which can be used to quote a substring of
811 * the line. Completion occurs on the entire substring, and within
812 * the substring Readline.completer_word_break_characters are treated
813 * as any other character, unless they also appear within this list.
815 * Raises NotImplementedError if the using readline library does not support.
817 * Raises SecurityError exception if $SAFE is 4.
820 readline_s_set_completer_quote_characters(VALUE self, VALUE str)
822 #ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
823 static char *completer_quote_characters = NULL;
826 OutputStringValue(str);
827 if (completer_quote_characters == NULL) {
828 completer_quote_characters =
829 ALLOC_N(char, RSTRING_LEN(str) + 1);
832 REALLOC_N(completer_quote_characters, char, RSTRING_LEN(str) + 1);
834 strncpy(completer_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
835 completer_quote_characters[RSTRING_LEN(str)] = '\0';
836 rl_completer_quote_characters = completer_quote_characters;
841 return Qnil; /* not reached */
842 #endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */
847 * Readline.completer_quote_characters -> string
849 * Gets a list of characters which can be used to quote a substring of
852 * Raises NotImplementedError if the using readline library does not support.
854 * Raises SecurityError exception if $SAFE is 4.
857 readline_s_get_completer_quote_characters(VALUE self, VALUE str)
859 #ifdef HAVE_RL_COMPLETER_QUOTE_CHARACTERS
861 if (rl_completer_quote_characters == NULL)
863 return rb_locale_str_new_cstr(rl_completer_quote_characters);
866 return Qnil; /* not reached */
867 #endif /* HAVE_RL_COMPLETER_QUOTE_CHARACTERS */
872 * Readline.filename_quote_characters = string
874 * Sets a list of characters that cause a filename to be quoted by the completer
875 * when they appear in a completed filename. The default is nil.
877 * Raises NotImplementedError if the using readline library does not support.
879 * Raises SecurityError exception if $SAFE is 4.
882 readline_s_set_filename_quote_characters(VALUE self, VALUE str)
884 #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
885 static char *filename_quote_characters = NULL;
888 OutputStringValue(str);
889 if (filename_quote_characters == NULL) {
890 filename_quote_characters =
891 ALLOC_N(char, RSTRING_LEN(str) + 1);
894 REALLOC_N(filename_quote_characters, char, RSTRING_LEN(str) + 1);
896 strncpy(filename_quote_characters, RSTRING_PTR(str), RSTRING_LEN(str));
897 filename_quote_characters[RSTRING_LEN(str)] = '\0';
898 rl_filename_quote_characters = filename_quote_characters;
903 return Qnil; /* not reached */
904 #endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */
909 * Readline.filename_quote_characters -> string
911 * Gets a list of characters that cause a filename to be quoted by the completer
912 * when they appear in a completed filename.
914 * Raises NotImplementedError if the using readline library does not support.
916 * Raises SecurityError exception if $SAFE is 4.
919 readline_s_get_filename_quote_characters(VALUE self, VALUE str)
921 #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
923 if (rl_filename_quote_characters == NULL)
925 return rb_locale_str_new_cstr(rl_filename_quote_characters);
928 return Qnil; /* not reached */
929 #endif /* HAVE_RL_FILENAME_QUOTE_CHARACTERS */
933 hist_to_s(VALUE self)
935 return rb_str_new_cstr("HISTORY");
939 history_get_offset_history_base(int offset)
941 return history_base + offset;
945 history_get_offset_0(int offset)
951 hist_get(VALUE self, VALUE index)
953 HIST_ENTRY *entry = NULL;
962 entry = history_get(history_get_offset_func(i));
965 rb_raise(rb_eIndexError, "invalid index");
967 return rb_locale_str_new_cstr(entry->line);
971 hist_set(VALUE self, VALUE index, VALUE str)
973 #ifdef HAVE_REPLACE_HISTORY_ENTRY
974 HIST_ENTRY *entry = NULL;
979 OutputStringValue(str);
984 entry = replace_history_entry(i, RSTRING_PTR(str), NULL);
987 rb_raise(rb_eIndexError, "invalid index");
992 return Qnil; /* not reached */
997 hist_push(VALUE self, VALUE str)
1000 OutputStringValue(str);
1001 add_history(RSTRING_PTR(str));
1006 hist_push_method(int argc, VALUE *argv, VALUE self)
1013 OutputStringValue(str);
1014 add_history(RSTRING_PTR(str));
1020 rb_remove_history(int index)
1022 #ifdef HAVE_REMOVE_HISTORY
1027 entry = remove_history(index);
1029 val = rb_locale_str_new_cstr(entry->line);
1030 free((void *) entry->line);
1037 return Qnil; /* not reached */
1042 hist_pop(VALUE self)
1045 if (history_length > 0) {
1046 return rb_remove_history(history_length - 1);
1053 hist_shift(VALUE self)
1056 if (history_length > 0) {
1057 return rb_remove_history(0);
1064 hist_each(VALUE self)
1069 RETURN_ENUMERATOR(self, 0, 0);
1072 for (i = 0; i < history_length; i++) {
1073 entry = history_get(history_get_offset_func(i));
1076 rb_yield(rb_locale_str_new_cstr(entry->line));
1082 hist_length(VALUE self)
1085 return INT2NUM(history_length);
1089 hist_empty_p(VALUE self)
1092 return history_length == 0 ? Qtrue : Qfalse;
1096 hist_delete_at(VALUE self, VALUE index)
1103 i += history_length;
1104 if (i < 0 || i > history_length - 1) {
1105 rb_raise(rb_eIndexError, "invalid index");
1107 return rb_remove_history(i);
1111 hist_clear(VALUE self)
1113 #ifdef HAVE_CLEAR_HISTORY
1119 return Qnil; /* not reached */
1124 filename_completion_proc_call(VALUE self, VALUE str)
1130 matches = rl_completion_matches(StringValuePtr(str),
1131 rl_filename_completion_function);
1133 result = rb_ary_new();
1134 for (i = 0; matches[i]; i++) {
1135 rb_ary_push(result, rb_locale_str_new_cstr(matches[i]));
1139 if (RARRAY_LEN(result) >= 2)
1140 rb_ary_shift(result);
1149 username_completion_proc_call(VALUE self, VALUE str)
1155 matches = rl_completion_matches(StringValuePtr(str),
1156 rl_username_completion_function);
1158 result = rb_ary_new();
1159 for (i = 0; matches[i]; i++) {
1160 rb_ary_push(result, rb_locale_str_new_cstr(matches[i]));
1164 if (RARRAY_LEN(result) >= 2)
1165 rb_ary_shift(result);
1176 VALUE history, fcomp, ucomp, version;
1178 /* Allow conditional parsing of the ~/.inputrc file. */
1179 rl_readline_name = "Ruby";
1183 completion_proc = rb_intern(COMPLETION_PROC);
1184 completion_case_fold = rb_intern(COMPLETION_CASE_FOLD);
1186 mReadline = rb_define_module("Readline");
1187 rb_define_module_function(mReadline, "readline",
1188 readline_readline, -1);
1189 rb_define_singleton_method(mReadline, "input=",
1190 readline_s_set_input, 1);
1191 rb_define_singleton_method(mReadline, "output=",
1192 readline_s_set_output, 1);
1193 rb_define_singleton_method(mReadline, "completion_proc=",
1194 readline_s_set_completion_proc, 1);
1195 rb_define_singleton_method(mReadline, "completion_proc",
1196 readline_s_get_completion_proc, 0);
1197 rb_define_singleton_method(mReadline, "completion_case_fold=",
1198 readline_s_set_completion_case_fold, 1);
1199 rb_define_singleton_method(mReadline, "completion_case_fold",
1200 readline_s_get_completion_case_fold, 0);
1201 rb_define_singleton_method(mReadline, "vi_editing_mode",
1202 readline_s_vi_editing_mode, 0);
1203 rb_define_singleton_method(mReadline, "vi_editing_mode?",
1204 readline_s_vi_editing_mode_p, 0);
1205 rb_define_singleton_method(mReadline, "emacs_editing_mode",
1206 readline_s_emacs_editing_mode, 0);
1207 rb_define_singleton_method(mReadline, "emacs_editing_mode?",
1208 readline_s_emacs_editing_mode_p, 0);
1209 rb_define_singleton_method(mReadline, "completion_append_character=",
1210 readline_s_set_completion_append_character, 1);
1211 rb_define_singleton_method(mReadline, "completion_append_character",
1212 readline_s_get_completion_append_character, 0);
1213 rb_define_singleton_method(mReadline, "basic_word_break_characters=",
1214 readline_s_set_basic_word_break_characters, 1);
1215 rb_define_singleton_method(mReadline, "basic_word_break_characters",
1216 readline_s_get_basic_word_break_characters, 0);
1217 rb_define_singleton_method(mReadline, "completer_word_break_characters=",
1218 readline_s_set_completer_word_break_characters, 1);
1219 rb_define_singleton_method(mReadline, "completer_word_break_characters",
1220 readline_s_get_completer_word_break_characters, 0);
1221 rb_define_singleton_method(mReadline, "basic_quote_characters=",
1222 readline_s_set_basic_quote_characters, 1);
1223 rb_define_singleton_method(mReadline, "basic_quote_characters",
1224 readline_s_get_basic_quote_characters, 0);
1225 rb_define_singleton_method(mReadline, "completer_quote_characters=",
1226 readline_s_set_completer_quote_characters, 1);
1227 rb_define_singleton_method(mReadline, "completer_quote_characters",
1228 readline_s_get_completer_quote_characters, 0);
1229 rb_define_singleton_method(mReadline, "filename_quote_characters=",
1230 readline_s_set_filename_quote_characters, 1);
1231 rb_define_singleton_method(mReadline, "filename_quote_characters",
1232 readline_s_get_filename_quote_characters, 0);
1234 history = rb_obj_alloc(rb_cObject);
1235 rb_extend_object(history, rb_mEnumerable);
1236 rb_define_singleton_method(history,"to_s", hist_to_s, 0);
1237 rb_define_singleton_method(history,"[]", hist_get, 1);
1238 rb_define_singleton_method(history,"[]=", hist_set, 2);
1239 rb_define_singleton_method(history,"<<", hist_push, 1);
1240 rb_define_singleton_method(history,"push", hist_push_method, -1);
1241 rb_define_singleton_method(history,"pop", hist_pop, 0);
1242 rb_define_singleton_method(history,"shift", hist_shift, 0);
1243 rb_define_singleton_method(history,"each", hist_each, 0);
1244 rb_define_singleton_method(history,"length", hist_length, 0);
1245 rb_define_singleton_method(history,"size", hist_length, 0);
1246 rb_define_singleton_method(history,"empty?", hist_empty_p, 0);
1247 rb_define_singleton_method(history,"delete_at", hist_delete_at, 1);
1248 rb_define_singleton_method(history,"clear", hist_clear, 0);
1251 * The history buffer. It extends Enumerable module, so it behaves
1252 * just like an array.
1253 * For example, gets the fifth content that the user input by
1256 rb_define_const(mReadline, "HISTORY", history);
1258 fcomp = rb_obj_alloc(rb_cObject);
1259 rb_define_singleton_method(fcomp, "call",
1260 filename_completion_proc_call, 1);
1262 * The Object with the call method that is a completion for filename.
1263 * This is sets by Readline.completion_proc= method.
1265 rb_define_const(mReadline, "FILENAME_COMPLETION_PROC", fcomp);
1267 ucomp = rb_obj_alloc(rb_cObject);
1268 rb_define_singleton_method(ucomp, "call",
1269 username_completion_proc_call, 1);
1271 * The Object with the call method that is a completion for usernames.
1272 * This is sets by Readline.completion_proc= method.
1274 rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
1275 history_get_offset_func = history_get_offset_history_base;
1276 #if defined HAVE_RL_LIBRARY_VERSION
1277 version = rb_str_new_cstr(rl_library_version);
1278 #if defined HAVE_CLEAR_HISTORY || defined HAVE_REMOVE_HISTORY
1279 if (strncmp(rl_library_version, EDIT_LINE_LIBRARY_VERSION,
1280 strlen(EDIT_LINE_LIBRARY_VERSION)) == 0) {
1282 if (history_get(history_get_offset_func(0)) == NULL) {
1283 history_get_offset_func = history_get_offset_0;
1285 #if !defined HAVE_CLEAR_HISTORY
1289 HIST_ENTRY *entry = remove_history(0);
1297 version = rb_str_new_cstr("2.0 or prior version");
1299 /* Version string of GNU Readline or libedit. */
1300 rb_define_const(mReadline, "VERSION", version);
1302 rl_attempted_completion_function = readline_attempted_completion_function;
1303 #ifdef HAVE_RL_EVENT_HOOK
1304 rl_event_hook = readline_event;
1306 #ifdef HAVE_RL_CLEAR_SIGNALS