Module: Readline

Includes:
RbReadline
Defined in:
lib/readline.rb

Overview

readline.rb – GNU Readline module Copyright © 1997-2001 Shugo Maeda

Ruby translation by Park Heesob [email protected]

Defined Under Namespace

Classes: Fcomp, History, Ucomp

Constant Summary

Constants included from RbReadline

RbReadline::ABORT_CHAR, RbReadline::ANCHORED_SEARCH, RbReadline::ANYOTHERKEY, RbReadline::AUDIBLE_BELL, RbReadline::BAD_MODIFIER, RbReadline::BAD_WORD_SPEC, RbReadline::BFIND, RbReadline::BTO, RbReadline::CapCase, RbReadline::DEFAULT_BUFFER_SIZE, RbReadline::DEFAULT_INPUTRC, RbReadline::DEFAULT_MAX_KILLS, RbReadline::DownCase, RbReadline::EOF, RbReadline::ESC, RbReadline::EVENT_NOT_FOUND, RbReadline::FFIND, RbReadline::FTO, RbReadline::HISTORY_APPEND, RbReadline::HISTORY_OVERWRITE, RbReadline::HISTORY_QUOTE_CHARACTERS, RbReadline::HISTORY_WORD_DELIMITERS, RbReadline::ISFUNC, RbReadline::ISKMAP, RbReadline::ISMACR, RbReadline::KEYMAP_SIZE, RbReadline::KSEQ_DISPATCHED, RbReadline::KSEQ_RECURSIVE, RbReadline::KSEQ_SUBSEQ, RbReadline::MB_FIND_ANY, RbReadline::MB_FIND_NONZERO, RbReadline::MB_LEN_MAX, RbReadline::MULT_MATCH, RbReadline::NEWLINE, RbReadline::NON_ANCHORED_SEARCH, RbReadline::NO_BELL, RbReadline::NO_MATCH, RbReadline::NO_PREV_SUBST, RbReadline::NUM_READONE, RbReadline::NUM_SAWDIGITS, RbReadline::NUM_SAWMINUS, RbReadline::PAGE, RbReadline::RB_READLINE_VERSION, RbReadline::READERR, RbReadline::RETURN, RbReadline::RL_IM_DEFAULT, RbReadline::RL_IM_INSERT, RbReadline::RL_IM_OVERWRITE, RbReadline::RL_LIBRARY_VERSION, RbReadline::RL_PROMPT_END_IGNORE, RbReadline::RL_PROMPT_START_IGNORE, RbReadline::RL_QF_BACKSLASH, RbReadline::RL_QF_DOUBLE_QUOTE, RbReadline::RL_QF_OTHER_QUOTE, RbReadline::RL_QF_SINGLE_QUOTE, RbReadline::RL_READLINE_VERSION, RbReadline::RL_SEARCH_CSEARCH, RbReadline::RL_SEARCH_ISEARCH, RbReadline::RL_SEARCH_NSEARCH, RbReadline::RL_STATE_CALLBACK, RbReadline::RL_STATE_COMPLETING, RbReadline::RL_STATE_DISPATCHING, RbReadline::RL_STATE_DONE, RbReadline::RL_STATE_INITIALIZED, RbReadline::RL_STATE_INITIALIZING, RbReadline::RL_STATE_INPUTPENDING, RbReadline::RL_STATE_ISEARCH, RbReadline::RL_STATE_MACRODEF, RbReadline::RL_STATE_MACROINPUT, RbReadline::RL_STATE_METANEXT, RbReadline::RL_STATE_MOREINPUT, RbReadline::RL_STATE_MULTIKEY, RbReadline::RL_STATE_NONE, RbReadline::RL_STATE_NSEARCH, RbReadline::RL_STATE_NUMERICARG, RbReadline::RL_STATE_OVERWRITE, RbReadline::RL_STATE_READCMD, RbReadline::RL_STATE_SEARCH, RbReadline::RL_STATE_SIGHANDLER, RbReadline::RL_STATE_TERMPREPPED, RbReadline::RL_STATE_TTYCSAVED, RbReadline::RL_STATE_UNDOING, RbReadline::RL_STATE_VICMDONCE, RbReadline::RL_STATE_VIMOTION, RbReadline::RUBOUT, RbReadline::SF_FAILED, RbReadline::SF_FOUND, RbReadline::SF_REVERSE, RbReadline::SINGLE_MATCH, RbReadline::SPACE, RbReadline::SUBST_FAILED, RbReadline::SYS_INPUTRC, RbReadline::TAB, RbReadline::UpCase, RbReadline::VISIBLE_BELL, RbReadline::XOK

Class Method Summary collapse

Methods included from RbReadline

__rl_fix_point, _extract_last_quote, _rl_abort_internal, _rl_adjust_point, _rl_any_typein, _rl_arg_dispatch, _rl_arg_getchar, _rl_arg_init, _rl_arg_overflow, _rl_backspace, _rl_bind_tty_special_chars, _rl_char_search, _rl_char_search_internal, _rl_char_value, _rl_clean_up_for_exit, _rl_clear_screen, _rl_clear_to_eol, _rl_col_width, _rl_compare_chars, _rl_control_keypad, _rl_copy_to_kill_ring, _rl_current_display_line, _rl_dispatch, _rl_dispatch_subseq, _rl_enable_meta_key, _rl_erase_at_end_of_line, _rl_erase_entire_line, _rl_find_completion_word, _rl_find_next_mbchar, _rl_find_prev_mbchar, _rl_fix_point, _rl_get_char_len, _rl_get_screen_size, _rl_history_set_point, _rl_init_eightbit, _rl_init_line_state, _rl_init_terminal_io, _rl_input_available, _rl_insert_char, _rl_insert_next, _rl_insert_typein, _rl_internal_char_cleanup, _rl_internal_pager, _rl_is_mbchar_matched, _rl_isearch_cleanup, _rl_isearch_dispatch, _rl_isearch_fini, _rl_isearch_init, _rl_make_prompt_for_search, _rl_move_cursor_relative, _rl_move_vert, _rl_nsearch_abort, _rl_nsearch_cleanup, _rl_nsearch_dispatch, _rl_nsearch_dosearch, _rl_nsearch_init, _rl_output_some_chars, _rl_overwrite_char, _rl_overwrite_rubout, _rl_read_init_file, _rl_read_mbchar, _rl_read_mbstring, _rl_redisplay_after_sigwinch, _rl_replace_text, _rl_reset_argument, _rl_rubout_char, _rl_scxt_alloc, _rl_search_getchar, _rl_set_insert_mode, _rl_set_mark_at_pos, _rl_start_using_history, _rl_strip_prompt, _rl_subseq_getchar, _rl_to_lower, _rl_unget_char, _rl_update_final, _rl_vi_done_inserting, _rl_vi_initialize_line, _rl_vi_reset_last, _rl_vi_save_insert, _rl_vi_textmod_command, _rl_walphabetic, add_history, alloc_history_entry, alloc_undo_entry, append_to_match, bind_arrow_keys, bind_arrow_keys_internal, bind_termcap_arrow_keys, block_sigint, clear_history, compute_lcd_of_matches, cr, cr_faster, ctrl_char, current_history, delete_chars, display_matches, endsrch_char, expand_prompt, fnprint, fnwidth, gen_completion_matches, get_term_capabilities, get_y_or_n, handle_parser_directive, hist_inittime, history_arg_extract, history_base, history_get, history_is_stifled, history_length, history_list, history_search_pos, history_search_prefix, history_set_pos, ibuffer_space, init_line_structures, insert_all_matches, insert_match, insert_some_chars, inv_line, inv_llen, isascii, isprint, m_offset, make_history_line_current, make_quoted_replacement, meta_char, next_history, no_terminal?, noninc_dosearch, noninc_search, noninc_search_from_pos, parser_else, parser_endif, parser_if, parser_include, path_isdir, postprocess_matches, prepare_terminal_settings, previous_history, print_filename, printable_part, prompt_ending_index, readline_default_bindings, readline_initialize_everything, readline_internal, readline_internal_charloop, readline_internal_setup, readline_internal_teardown, redraw_prompt, release_sigint, remove_duplicate_matches, remove_history, replace_history_data, replace_history_entry, retry_if_interrupted, rl_abort, rl_add_undo, rl_alphabetic, rl_arrow_keys, rl_attempted_completion_function, rl_attempted_completion_function=, rl_attempted_completion_over, rl_attempted_completion_over=, rl_backward, rl_backward_byte, rl_backward_char, rl_backward_char_search, rl_backward_kill_line, rl_backward_kill_word, rl_backward_word, rl_basic_quote_characters, rl_basic_quote_characters=, rl_basic_word_break_characters, rl_basic_word_break_characters=, rl_beg_of_line, rl_begin_undo_group, rl_beginning_of_history, rl_bind_key, rl_bind_keyseq_if_unbound, rl_bind_keyseq_if_unbound_in_map, rl_bind_keyseq_in_map, rl_capitalize_word, rl_change_case, rl_char_search, rl_character_len, rl_cleanup_after_signal, rl_clear_message, rl_clear_pending_input, rl_clear_screen, rl_clear_signals, rl_complete, rl_complete_internal, rl_completer_quote_characters, rl_completer_quote_characters=, rl_completer_word_break_characters, rl_completer_word_break_characters=, rl_completion_append_character, rl_completion_append_character=, rl_completion_matches, rl_copy_text, rl_crlf, rl_delete, rl_delete_horizontal_space, rl_delete_or_show_completions, rl_delete_text, rl_deprep_term_function, rl_deprep_term_function=, rl_deprep_terminal, rl_digit_argument, rl_digit_loop, rl_ding, rl_display_match_list, rl_display_search, rl_do_lowercase_version, rl_do_undo, rl_downcase_word, rl_emacs_editing_mode, rl_end_of_history, rl_end_of_line, rl_end_undo_group, rl_event_hook, rl_event_hook=, rl_exchange_point_and_mark, rl_execute_next, rl_expand_prompt, rl_extend_line_buffer, rl_filename_completion_function, rl_filename_quote_characters, rl_filename_quote_characters=, rl_forced_update_display, rl_forward, rl_forward_byte, rl_forward_char, rl_forward_search_history, rl_forward_word, rl_free_undo_list, rl_function_of_keyseq, rl_generic_bind, rl_get_char, rl_get_keymap_name_from_edit_mode, rl_get_next_history, rl_get_previous_history, rl_initialize, rl_insert, rl_insert_comment, rl_insert_completions, rl_insert_text, rl_instream, rl_instream=, rl_isstate, rl_kill_full_line, rl_kill_line, rl_kill_text, rl_kill_word, rl_library_version, rl_library_version=, rl_line_buffer, rl_maybe_replace_line, rl_maybe_save_line, rl_maybe_unsave_line, rl_message, rl_modifying, rl_named_function, rl_newline, rl_noninc_forward_search, rl_noninc_reverse_search, rl_on_new_line, rl_on_new_line_with_prompt, rl_outstream, rl_outstream=, rl_overwrite_mode, rl_parse_and_bind, rl_point, rl_possible_completions, rl_prep_terminal, rl_quoted_insert, rl_re_read_init_file, rl_read_init_file, rl_read_key, rl_readline_name, rl_readline_name=, rl_redisplay, rl_refresh_line, rl_replace_from_history, rl_replace_line, rl_reset_line_state, rl_resize_terminal, rl_restart_output, rl_restore_prompt, rl_reverse_search_history, rl_revert_line, rl_rubout, rl_rubout_or_delete, rl_save_prompt, rl_search_history, rl_set_keymap_from_edit_mode, rl_set_mark, rl_set_prompt, rl_set_signals, rl_setstate, rl_sigwinch_handler, rl_stuff_char, rl_tab_insert, rl_tilde_expand, rl_translate_keyseq, rl_transpose_chars, rl_transpose_words, rl_tty_set_default_bindings, rl_tty_unset_default_bindings, rl_undo_command, rl_unix_filename_rubout, rl_unix_line_discard, rl_unix_word_rubout, rl_unsetstate, rl_upcase_word, rl_username_completion_function, rl_variable_bind, rl_vi_check, rl_vi_editing_mode, rl_vi_insertion_mode, rl_yank, rl_yank_last_arg, rl_yank_nth_arg, rl_yank_nth_arg_internal, rl_yank_pop, save_tty_chars, set_completion_defaults, sh_set_lines_and_columns, space_to_eol, stat_char, stifle_history, tgetflag, trans, unstifle_history, update_line, using_history, vis_chars, vis_line, vis_llen, vis_pos, w_offset, where_history, whitespace

Class Method Details

.basic_quote_charactersObject

Returns the list of quote characters that can cause a word break. The default is “‘"” (single and double quote characters).



269
270
271
272
273
274
275
# File 'lib/readline.rb', line 269

def self.basic_quote_characters()
  if RbReadline.rl_basic_quote_characters.nil?
    nil
  else
    RbReadline.rl_basic_quote_characters.dup
  end
end

.basic_quote_characters=(str) ⇒ Object

Sets the list of quote characters that can cause a word break.



262
263
264
# File 'lib/readline.rb', line 262

def self.basic_quote_characters=(str)
  RbReadline.rl_basic_quote_characters = str.dup
end

.basic_word_break_charactersObject

Returns the character string that signal a break between words for the completion proc. The default is “ tn"\‘`@$><=|&{(”.



234
235
236
237
238
239
240
# File 'lib/readline.rb', line 234

def self.basic_word_break_characters()
  if RbReadline.rl_basic_word_break_characters.nil?
    nil
  else
    RbReadline.rl_basic_word_break_characters.dup
  end
end

.basic_word_break_characters=(str) ⇒ Object

Sets the character string that signal a break between words for the completion proc.



227
228
229
# File 'lib/readline.rb', line 227

def self.basic_word_break_characters=(str)
  RbReadline.rl_basic_word_break_characters = str.dup
end

.completer_quote_charactersObject

Returns the list of characters that can be used to quote a substring of the line, i.e. a group of characters inside quotes.



287
288
289
290
291
292
293
# File 'lib/readline.rb', line 287

def self.completer_quote_characters()
  if RbReadline.rl_completer_quote_characters.nil?
    nil
  else
    RbReadline.rl_completer_quote_characters.dup
  end
end

.completer_quote_characters=(str) ⇒ Object

Sets the list of characters that can be used to quote a substring of the line, i.e. a group of characters within quotes.



280
281
282
# File 'lib/readline.rb', line 280

def self.completer_quote_characters=(str)
  RbReadline.rl_completer_quote_characters = str.dup
end

.completer_word_break_charactersObject

Returns the character string that signal the start or end of a word for the completion proc.



252
253
254
255
256
257
258
# File 'lib/readline.rb', line 252

def self.completer_word_break_characters()
  if RbReadline.rl_completer_word_break_characters.nil?
    nil
  else
    RbReadline.rl_completer_word_break_characters.dup
  end
end

.completer_word_break_characters=(str) ⇒ Object

Sets the character string that signal the start or end of a word for the completion proc.



245
246
247
# File 'lib/readline.rb', line 245

def self.completer_word_break_characters=(str)
  RbReadline.rl_completer_word_break_characters = str.dup
end

.completion_append_characterObject

Returns the character that is automatically appended after the Readline.completion_proc method is called.



217
218
219
220
221
222
# File 'lib/readline.rb', line 217

def self.completion_append_character()
  if RbReadline.rl_completion_append_character == ?\0
    return nil
  end
  return RbReadline.rl_completion_append_character
end

.completion_append_character=(char) ⇒ Object

Sets the character that is automatically appended after the Readline.completion_proc method is called.

If char is nil or empty, then a null character is used.



204
205
206
207
208
209
210
211
212
# File 'lib/readline.rb', line 204

def self.completion_append_character=(char)
  if char.nil?
    RbReadline.rl_completion_append_character = ?\0
  elsif char.length==0
    RbReadline.rl_completion_append_character = ?\0
  else
    RbReadline.rl_completion_append_character = char[0].chr
  end
end

.completion_case_foldObject

Returns whether or not the completion proc is case sensitive. The default is false, i.e. completion procs are case sensitive.



114
115
116
# File 'lib/readline.rb', line 114

def self.completion_case_fold()
  @completion_case_fold
end

.completion_case_fold=(bool) ⇒ Object

Sets whether or not the completion proc should ignore case sensitivity. The default is false, i.e. completion procs are case sensitive.



107
108
109
# File 'lib/readline.rb', line 107

def self.completion_case_fold=(bool)
  @completion_case_fold = bool
end

.completion_procObject

Returns the current auto-completion procedure.



100
101
102
# File 'lib/readline.rb', line 100

def self.completion_proc()
  @completion_proc
end

.completion_proc=(proc) ⇒ Object

Sets the auto-completion procedure (i.e. tab auto-complete).

The proc argument is typically a Proc object. It must respond to .call, take a single String argument and return an Array of candidates for completion.

Example:

list = ['search', 'next', 'clear']
Readline.completion_proc = proc{ |s| list.grep( /^#{Regexp.escape(s)}/) }


91
92
93
94
95
96
# File 'lib/readline.rb', line 91

def self.completion_proc=(proc)
  unless proc.respond_to? :call
    raise ArgumentError,"argument must respond to `call'"
  end
  @completion_proc = proc
end

.emacs_editing_modeObject

Sets emacs editing mode



194
195
196
197
# File 'lib/readline.rb', line 194

def self.emacs_editing_mode()
  RbReadline.rl_emacs_editing_mode(1,0)
  nil
end

.filename_quote_charactersObject

Returns the character string used to indicate quotes for the filename completion of user input.



305
306
307
308
309
310
311
# File 'lib/readline.rb', line 305

def self.filename_quote_characters()
  if RbReadline.rl_filename_quote_characters.nil?
    nil
  else
    RbReadline.rl_filename_quote_characters.dup
  end
end

.filename_quote_characters=(str) ⇒ Object

Sets the character string of one or more characters that indicate quotes for the filename completion of user input.



298
299
300
# File 'lib/readline.rb', line 298

def self.filename_quote_characters=(str)
  RbReadline.rl_filename_quote_characters = str.dup
end

.input=(input) ⇒ Object

Sets the input stream (an IO object) for readline interaction. The default is $stdin.



63
64
65
# File 'lib/readline.rb', line 63

def self.input=(input)
  RbReadline.rl_instream = input
end

.line_bufferObject

Returns current line buffer



76
77
78
# File 'lib/readline.rb', line 76

def self.line_buffer
  RbReadline.rl_line_buffer
end

.output=(output) ⇒ Object

Sets the output stream (an IO object) for readline interaction. The default is $stdout.



70
71
72
# File 'lib/readline.rb', line 70

def self.output=(output)
  RbReadline.rl_outstream = output
end

.pointObject

Returns the current offset in the current input line.



315
316
317
# File 'lib/readline.rb', line 315

def self.point()
  RbReadline.rl_point
end

.readline(prompt = "", add_history = nil) ⇒ Object

Begins an interactive terminal process using prompt as the command prompt that users see when they type commands. The method returns the line entered whenever a carriage return is encountered.

If an add_history argument is provided, commands entered by users are stored in a history buffer that can be recalled for later use.

Note that this method depends on $stdin and $stdout both being open. Because this is meant as an interactive console interface, they should generally not be redirected.

If you would like to add non-visible characters to the the prompt (for example to add colors) you must prepend the character 001 (^A) before each sequence of non-visible characters and add the character 002 (^B) after, otherwise line wrapping may not work properly.

Example:

loop{ Readline.readline('> ') }


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/readline.rb', line 36

def readline(prompt = "", add_history = nil)
  if $stdin.closed?
    raise IOError, "stdin closed"
  end

  RbReadline.rl_instream = $stdin
  RbReadline.rl_outstream = $stdout

  begin
    buff = RbReadline.readline(prompt)
  rescue Exception => e
    buff = nil
    RbReadline.rl_cleanup_after_signal()
    RbReadline.rl_deprep_terminal()
    raise e
  end

  if add_history && buff
    RbReadline.add_history(buff)
  end

  return buff ? buff.dup : nil
end

.readline_attempted_completion_function(text, start, _end) ⇒ Object

Returns nil if no matches are found or an array of strings:

[0] is the replacement for text
[1..n] the possible matches
[n+1] nil

The possible matches should not include [0].

If this method sets RbReadline.rl_attempted_completion_over to true, then the default completion function will not be called when this function returns nil.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/readline.rb', line 129

def self.readline_attempted_completion_function(text,start,_end)
  proc = @completion_proc
  return nil if proc.nil?

  RbReadline.rl_attempted_completion_over = true

  case_fold = @completion_case_fold
  ary = proc.call(text)
  if ary.class != Array
    ary = Array(ary)
  else
    ary.compact!
  end

  matches = ary.length
  return nil if (matches == 0)
  result = Array.new(matches+2)
  for i in 0 ... matches
    result[i+1] = ary[i].dup
  end
  result[matches+1] = nil

  if(matches==1)
    result[0] = result[1].dup
    result[1] = nil
  else
    i = 1
    low = 100000

    while (i < matches)
      if (case_fold)
        si = 0
        while ((c1 = result[i][si,1].downcase) &&
               (c2 = result[i + 1][si,1].downcase))
          break if (c1 != c2)
          si += 1
        end
      else
        si = 0
        while ((c1 = result[i][si,1]) &&
               (c2 = result[i + 1][si,1]))
          break if (c1 != c2)
          si += 1
        end
      end
      if (low > si)
        low = si
      end
      i+=1
    end
    result[0] = result[1][0,low]
  end

  result
end

.silence_warnings(&block) ⇒ Object

Temporarily disable warnings and call a block



321
322
323
324
325
326
327
# File 'lib/readline.rb', line 321

def self.silence_warnings(&block)
  warn_level = $VERBOSE
  $VERBOSE = nil
  result = block.call
  $VERBOSE = warn_level
  result
end

.vi_editing_modeObject

Sets vi editing mode.



187
188
189
190
# File 'lib/readline.rb', line 187

def self.vi_editing_mode()
  RbReadline.rl_vi_editing_mode(1,0)
  nil
end