PageRenderTime 163ms CodeModel.GetById 10ms app.highlight 127ms RepoModel.GetById 1ms app.codeStats 3ms

/IronPython_Main/External.LCA_RESTRICTED/Languages/Ruby/redist-libs/ruby/site_ruby/1.9.1/rbreadline.rb

#
Ruby | 8664 lines | 6081 code | 1132 blank | 1451 comment | 1492 complexity | 1b86031473b7980d86552309ca944a1e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1# rbreadline.rb -- a general facility for reading lines of input
   2#   with emacs style editing and completion.
   3
   4#
   5#  Inspired by GNU Readline, translation to Ruby
   6#  Copyright (C) 2009 by Park Heesob phasis@gmail.com
   7#
   8
   9class Fixnum
  10   def ord; self; end
  11end
  12
  13module RbReadline
  14   require 'etc'
  15
  16   RL_LIBRARY_VERSION = "5.2"
  17   RL_READLINE_VERSION  = 0x0502
  18   RB_READLINE_VERSION = "0.2.0"
  19
  20   EOF = "\xFF"
  21   ESC = "\C-["
  22   PAGE = "\C-L"
  23   SPACE = "\x20"
  24   RETURN = "\C-M"
  25   ABORT_CHAR = "\C-G"
  26   TAB = "\t"
  27   RUBOUT = "x7f"
  28   NEWLINE = "\n"
  29
  30   DEFAULT_BUFFER_SIZE = 256
  31   DEFAULT_MAX_KILLS = 10
  32
  33   MB_FIND_NONZERO = 1
  34   MB_FIND_ANY = 0
  35   MB_LEN_MAX = 4
  36
  37   DEFAULT_INPUTRC = "~/.inputrc"
  38   SYS_INPUTRC = "/etc/inputrc"
  39
  40   UpCase = 1
  41   DownCase = 2
  42   CapCase = 3
  43
  44   # Possible history errors passed to hist_error.
  45   EVENT_NOT_FOUND = 0
  46   BAD_WORD_SPEC  = 1
  47   SUBST_FAILED   = 2
  48   BAD_MODIFIER   = 3
  49   NO_PREV_SUBST  = 4
  50
  51   # Possible definitions for history starting point specification.
  52   ANCHORED_SEARCH = 1
  53   NON_ANCHORED_SEARCH = 0
  54
  55   # Possible definitions for what style of writing the history file we want.
  56   HISTORY_APPEND = 0
  57   HISTORY_OVERWRITE = 1
  58
  59   # Input error; can be returned by (*rl_getc_function) if readline is reading
  60   #   a top-level command (RL_ISSTATE (RL_STATE_READCMD)).
  61   READERR = 0xFE.chr
  62
  63   # Definitions available for use by readline clients.
  64   RL_PROMPT_START_IGNORE    = 1.chr
  65   RL_PROMPT_END_IGNORE    = 2.chr
  66
  67   # Possible values for do_replace argument to rl_filename_quoting_function,
  68   #   called by rl_complete_internal.
  69   NO_MATCH      =  0
  70   SINGLE_MATCH  =  1
  71   MULT_MATCH    =  2
  72
  73   # Callback data for reading numeric arguments
  74   NUM_SAWMINUS   = 0x01
  75   NUM_SAWDIGITS = 0x02
  76   NUM_READONE = 0x04
  77
  78   # A context for reading key sequences longer than a single character when
  79   #   using the callback interface.
  80   KSEQ_DISPATCHED = 0x01
  81   KSEQ_SUBSEQ = 0x02
  82   KSEQ_RECURSIVE = 0x04
  83
  84   # Possible state values for rl_readline_state
  85   RL_STATE_NONE        = 0x000000        # no state before first call
  86
  87   RL_STATE_INITIALIZING =    0x000001    # initializing
  88   RL_STATE_INITIALIZED  =    0x000002    # initialization done
  89   RL_STATE_TERMPREPPED  =    0x000004    # terminal is prepped
  90   RL_STATE_READCMD =    0x000008    # reading a command key
  91   RL_STATE_METANEXT =    0x000010    # reading input after ESC
  92   RL_STATE_DISPATCHING =    0x000020    # dispatching to a command
  93   RL_STATE_MOREINPUT    = 0x000040    # reading more input in a command function
  94   RL_STATE_ISEARCH    = 0x000080    # doing incremental search
  95   RL_STATE_NSEARCH    = 0x000100    # doing non-inc search
  96   RL_STATE_SEARCH        = 0x000200    # doing a history search
  97   RL_STATE_NUMERICARG    = 0x000400    # reading numeric argument
  98   RL_STATE_MACROINPUT    = 0x000800    # getting input from a macro
  99   RL_STATE_MACRODEF    = 0x001000    # defining keyboard macro
 100   RL_STATE_OVERWRITE    = 0x002000    # overwrite mode
 101   RL_STATE_COMPLETING    = 0x004000    # doing completion
 102   RL_STATE_SIGHANDLER    = 0x008000    # in readline sighandler
 103   RL_STATE_UNDOING    = 0x010000    # doing an undo
 104   RL_STATE_INPUTPENDING =    0x020000    # rl_execute_next called
 105   RL_STATE_TTYCSAVED     = 0x040000    # tty special chars saved
 106   RL_STATE_CALLBACK     = 0x080000    # using the callback interface
 107   RL_STATE_VIMOTION     = 0x100000    # reading vi motion arg
 108   RL_STATE_MULTIKEY     = 0x200000    # reading multiple-key command
 109   RL_STATE_VICMDONCE     = 0x400000    # entered vi command mode at least once
 110
 111   RL_STATE_DONE        = 0x800000    # done accepted line
 112
 113   NO_BELL = 0
 114   AUDIBLE_BELL = 1
 115   VISIBLE_BELL = 2
 116   # The actions that undo knows how to undo.  Notice that UNDO_DELETE means
 117   #   to insert some text, and UNDO_INSERT means to delete some text.   I.e.,
 118   #   the code tells undo what to undo, not how to undo it.
 119   UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END  = 0,1,2,3
 120
 121   # Definitions used when searching the line for characters.
 122   # NOTE: it is necessary that opposite directions are inverses
 123   FTO   =  1     # forward to
 124   BTO   = -1     # backward to
 125   FFIND =  2     # forward find
 126   BFIND = -2     # backward find
 127
 128   # Possible values for the found_quote flags word used by the completion
 129   #   functions.  It says what kind of (shell-like) quoting we found anywhere
 130   #   in the line.
 131   RL_QF_SINGLE_QUOTE   = 0x01
 132   RL_QF_DOUBLE_QUOTE   = 0x02
 133   RL_QF_BACKSLASH      = 0x04
 134   RL_QF_OTHER_QUOTE = 0x08
 135
 136   KEYMAP_SIZE = 257
 137   ANYOTHERKEY = KEYMAP_SIZE-1
 138
 139   @hConsoleHandle = nil
 140   @MessageBeep = nil
 141
 142   RL_IM_INSERT    = 1
 143   RL_IM_OVERWRITE    = 0
 144   RL_IM_DEFAULT    = RL_IM_INSERT
 145   @no_mode = -1
 146   @vi_mode = 0
 147   @emacs_mode = 1
 148   ISFUNC = 0
 149   ISKMAP = 1
 150   ISMACR = 2
 151
 152   HISTORY_WORD_DELIMITERS    = " \t\n;&()|<>"
 153   HISTORY_QUOTE_CHARACTERS   = "\"'`"
 154
 155   RL_SEARCH_ISEARCH = 0x01      # incremental search
 156   RL_SEARCH_NSEARCH = 0x02      # non-incremental search
 157   RL_SEARCH_CSEARCH = 0x04      # intra-line char search
 158
 159   # search flags
 160   SF_REVERSE     = 0x01
 161   SF_FOUND    = 0x02
 162   SF_FAILED      = 0x04
 163
 164   @slashify_in_quotes = "\\`\"$"
 165
 166   @sigint_proc = nil
 167   @sigint_blocked = false
 168
 169   @rl_prep_term_function = :rl_prep_terminal
 170   @rl_deprep_term_function = :rl_deprep_terminal
 171
 172   @_rl_history_saved_point = -1
 173
 174   @rl_max_kills =  DEFAULT_MAX_KILLS
 175   @rl_kill_ring = nil
 176   @rl_kill_index = 0
 177   @rl_kill_ring_length = 0
 178
 179   @pending_bytes = ''
 180   @stored_count = 0
 181
 182   @_rl_isearch_terminators = nil
 183   @_rl_iscxt = nil
 184   @last_isearch_string = nil
 185   @last_isearch_string_len = 0
 186   @default_isearch_terminators = "\033\012"
 187   @_rl_history_preserve_point = false
 188   @terminal_prepped = false
 189   @otio = nil
 190
 191   @msg_saved_prompt = false
 192
 193   @_rl_nscxt = nil
 194   @noninc_search_string = nil
 195   @noninc_history_pos = 0
 196   @prev_line_found = nil
 197
 198   rl_history_search_len = 0
 199   rl_history_search_pos = 0
 200   history_search_string = nil
 201   history_string_size = 0
 202
 203   @_rl_tty_chars = Struct.new(:t_eol,:t_eol2,:t_erase,:t_werase,:t_kill,:t_reprint,:t_intr,:t_eof,
 204   :t_quit,:t_susp,:t_dsusp,:t_start,:t_stop,:t_lnext,:t_flush,:t_status).new
 205   @_rl_last_tty_chars = nil
 206
 207   # Variables exported by this file.
 208   # The character that represents the start of a history expansion
 209   #   request.  This is usually `!'.
 210   @history_expansion_char = ?!
 211
 212   # The character that invokes word substitution if found at the start of
 213   #   a line.  This is usually `^'.
 214   @history_subst_char = ?^
 215
 216   # During tokenization, if this character is seen as the first character
 217   #   of a word, then it, and all subsequent characters upto a newline are
 218   #   ignored.  For a Bourne shell, this should be '#'.  Bash special cases
 219   #   the interactive comment character to not be a comment delimiter.
 220   @history_comment_char = ?\0
 221
 222   # The list of characters which inhibit the expansion of text if found
 223   #   immediately following history_expansion_char.
 224   @history_no_expand_chars = " \t\n\r="
 225
 226   # If set to a non-zero value, single quotes inhibit history expansion.
 227   #   The default is 0.
 228   @history_quotes_inhibit_expansion = 0
 229
 230   # Used to split words by history_tokenize_internal.
 231   @history_word_delimiters = HISTORY_WORD_DELIMITERS
 232
 233   # If set, this points to a function that is called to verify that a
 234   #   particular history expansion should be performed.
 235   @history_inhibit_expansion_function = nil
 236
 237   @rl_event_hook = nil
 238   # The visible cursor position.  If you print some text, adjust this.
 239   # NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
 240   #   supporting multibyte characters, and an absolute cursor position when
 241   #   in such a locale.  This is an artifact of the donated multibyte support.
 242   #   Care must be taken when modifying its value.
 243   @_rl_last_c_pos = 0
 244   @_rl_last_v_pos = 0
 245
 246   @cpos_adjusted = false
 247   @cpos_buffer_position = 0
 248
 249   # Number of lines currently on screen minus 1.
 250   @_rl_vis_botlin = 0
 251
 252   # Variables used only in this file.
 253   # The last left edge of text that was displayed.  This is used when
 254   #   doing horizontal scrolling.  It shifts in thirds of a screenwidth.
 255   @last_lmargin = 0
 256
 257   # The line display buffers.  One is the line currently displayed on
 258   #   the screen.  The other is the line about to be displayed.
 259   @visible_line = nil
 260   @invisible_line = nil
 261
 262   # A buffer for `modeline' messages.
 263   @msg_buf = 0.chr * 128
 264
 265   # Non-zero forces the redisplay even if we thought it was unnecessary.
 266   @forced_display = false
 267
 268   # Default and initial buffer size.  Can grow.
 269   @line_size = 1024
 270
 271   # Variables to keep track of the expanded prompt string, which may
 272   #   include invisible characters.
 273
 274   @local_prompt = nil
 275   @local_prompt_prefix = nil
 276   @local_prompt_len = 0
 277   @prompt_visible_length = 0
 278   @prompt_prefix_length = 0
 279
 280   # The number of invisible characters in the line currently being
 281   #   displayed on the screen.
 282   @visible_wrap_offset = 0
 283
 284   # The number of invisible characters in the prompt string.  Static so it
 285   #   can be shared between rl_redisplay and update_line
 286   @wrap_offset = 0
 287
 288   @prompt_last_invisible = 0
 289
 290   # The length (buffer offset) of the first line of the last (possibly
 291   #   multi-line) buffer displayed on the screen.
 292   @visible_first_line_len = 0
 293
 294   # Number of invisible characters on the first physical line of the prompt.
 295   #   Only valid when the number of physical characters in the prompt exceeds
 296   #   (or is equal to) _rl_screenwidth.
 297   @prompt_invis_chars_first_line = 0
 298
 299   @prompt_last_screen_line = 0
 300
 301   @prompt_physical_chars = 0
 302
 303   # Variables to save and restore prompt and display information.
 304
 305   # These are getting numerous enough that it's time to create a struct.
 306
 307   @saved_local_prompt = nil
 308   @saved_local_prefix = nil
 309   @saved_last_invisible =  0
 310   @saved_visible_length = 0
 311   @saved_prefix_length = 0
 312   @saved_local_length = 0
 313   @saved_invis_chars_first_line = 0
 314   @saved_physical_chars = 0
 315
 316   @inv_lbreaks = nil
 317   @vis_lbreaks = nil
 318   @_rl_wrapped_line = nil
 319
 320   @term_buffer = nil
 321   @term_string_buffer = nil
 322
 323   @tcap_initialized = false
 324
 325   # While we are editing the history, this is the saved
 326   #   version of the original line.
 327   @_rl_saved_line_for_history = nil
 328
 329   # An array of HIST_ENTRY.  This is where we store the history.
 330   @the_history = nil
 331   @history_base = 1
 332
 333   # Non-zero means that we have enforced a limit on the amount of
 334   #   history that we save.
 335   @history_stifled = false
 336
 337   # If HISTORY_STIFLED is non-zero, then this is the maximum number of
 338   #   entries to remember.
 339   @history_max_entries = 0
 340   @max_input_history = 0  # backwards compatibility
 341
 342   # The current location of the interactive history pointer.  Just makes
 343   #   life easier for outside callers.
 344   @history_offset = 0
 345
 346   # The number of strings currently stored in the history list.
 347   @history_length = 0
 348
 349   @_rl_vi_last_command = 'i' # default `.' puts you in insert mode
 350
 351   # Non-zero means enter insertion mode.
 352   @_rl_vi_doing_insert = 0
 353
 354   # Command keys which do movement for xxx_to commands.
 355   @vi_motion = " hl^$0ftFT;,%wbeWBE|"
 356
 357   # Keymap used for vi replace characters.  Created dynamically since
 358   #   rarely used.
 359   @vi_replace_map = nil
 360
 361   # The number of characters inserted in the last replace operation.
 362   @vi_replace_count = 0
 363
 364   # If non-zero, we have text inserted after a c[motion] command that put
 365   #   us implicitly into insert mode.  Some people want this text to be
 366   #   attached to the command so that it is `redoable' with `.'.
 367   @vi_continued_command = false
 368   @vi_insert_buffer = nil
 369   @vi_insert_buffer_size = 0
 370
 371   @_rl_vi_last_repeat = 1
 372   @_rl_vi_last_arg_sign = 1
 373   @_rl_vi_last_motion = 0
 374   @_rl_vi_last_search_char = 0
 375   @_rl_vi_last_replacement = 0
 376
 377   @_rl_vi_last_key_before_insert = 0
 378
 379   @vi_redoing = 0
 380
 381   # Text modification commands.  These are the `redoable' commands.
 382   @vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~"
 383
 384   # Arrays for the saved marks.
 385   @vi_mark_chars = Array.new(26,-1)
 386
 387   @emacs_standard_keymap = {
 388      "\C-@" => :rl_set_mark  ,
 389      "\C-a" => :rl_beg_of_line  ,
 390      "\C-b" => :rl_backward_char  ,
 391      "\C-d" => :rl_delete  ,
 392      "\C-e" => :rl_end_of_line  ,
 393      "\C-f" => :rl_forward_char  ,
 394      "\C-g" => :rl_abort  ,
 395      "\C-h" => :rl_rubout  ,
 396      "\C-i" => :rl_complete  ,
 397      "\C-j" => :rl_newline  ,
 398      "\C-k" => :rl_kill_line  ,
 399      "\C-l" => :rl_clear_screen  ,
 400      "\C-m" => :rl_newline  ,
 401      "\C-n" => :rl_get_next_history  ,
 402      "\C-p" => :rl_get_previous_history  ,
 403      "\C-q" => :rl_quoted_insert  ,
 404      "\C-r" => :rl_reverse_search_history  ,
 405      "\C-s" => :rl_forward_search_history  ,
 406      "\C-t" => :rl_transpose_chars  ,
 407      "\C-u" => :rl_unix_line_discard  ,
 408      "\C-v" => :rl_quoted_insert  ,
 409      "\C-w" => :rl_unix_word_rubout  ,
 410      "\C-y" => :rl_yank  ,
 411      "\C-]" => :rl_char_search  ,
 412      "\C-_" => :rl_undo_command  ,
 413      "\x7F" => :rl_rubout ,
 414      "\e\C-g" => :rl_abort  ,
 415      "\e\C-h" => :rl_backward_kill_word  ,
 416      "\e\C-i" => :rl_tab_insert  ,
 417      "\e\C-j" => :rl_vi_editing_mode  ,
 418      "\e\C-m" => :rl_vi_editing_mode  ,
 419      "\e\C-r" => :rl_revert_line  ,
 420      "\e\C-y" => :rl_yank_nth_arg  ,
 421      "\e\C-[" => :rl_complete  ,
 422      "\e\C-]" => :rl_backward_char_search  ,
 423      "\e " => :rl_set_mark  ,
 424      "\e#" => :rl_insert_comment  ,
 425      "\e&" => :rl_tilde_expand  ,
 426      "\e*" => :rl_insert_completions  ,
 427      "\e-" => :rl_digit_argument  ,
 428      "\e." => :rl_yank_last_arg ,
 429      "\e0" => :rl_digit_argument  ,
 430      "\e1" => :rl_digit_argument  ,
 431      "\e2" => :rl_digit_argument  ,
 432      "\e3" => :rl_digit_argument  ,
 433      "\e4" => :rl_digit_argument  ,
 434      "\e5" => :rl_digit_argument  ,
 435      "\e6" => :rl_digit_argument  ,
 436      "\e7" => :rl_digit_argument  ,
 437      "\e8" => :rl_digit_argument  ,
 438      "\e9" => :rl_digit_argument  ,
 439      "\e<" => :rl_beginning_of_history  ,
 440      "\e=" => :rl_possible_completions  ,
 441      "\e>" => :rl_end_of_history  ,
 442      "\e?" => :rl_possible_completions  ,
 443      "\eB" => :rl_backward_word  ,
 444      "\eC" => :rl_capitalize_word  ,
 445      "\eD" => :rl_kill_word  ,
 446      "\eF" => :rl_forward_word  ,
 447      "\eL" => :rl_downcase_word  ,
 448      "\eN" => :rl_noninc_forward_search  ,
 449      "\eP" => :rl_noninc_reverse_search  ,
 450      "\eR" => :rl_revert_line  ,
 451      "\eT" => :rl_transpose_words  ,
 452      "\eU" => :rl_upcase_word  ,
 453      "\eY" => :rl_yank_pop  ,
 454      "\e\\" => :rl_delete_horizontal_space  ,
 455      "\e_" => :rl_yank_last_arg  ,
 456      "\eb" => :rl_backward_word  ,
 457      "\ec" => :rl_capitalize_word  ,
 458      "\ed" => :rl_kill_word  ,
 459      "\ef" => :rl_forward_word  ,
 460      "\el" => :rl_downcase_word  ,
 461      "\en" => :rl_noninc_forward_search  ,
 462      "\ep" => :rl_noninc_reverse_search  ,
 463      "\er" => :rl_revert_line  ,
 464      "\et" => :rl_transpose_words  ,
 465      "\eu" => :rl_upcase_word  ,
 466      "\ey" => :rl_yank_pop  ,
 467      "\e~" => :rl_tilde_expand  ,
 468      "\377" => :rl_backward_kill_word  ,
 469
 470      "\C-x\C-g" => :rl_abort  ,
 471      "\C-x\C-r" => :rl_re_read_init_file  ,
 472      "\C-x\C-u" => :rl_undo_command  ,
 473      "\C-x\C-x" => :rl_exchange_point_and_mark  ,
 474      "\C-x(" => :rl_start_kbd_macro  ,
 475      "\C-x)" => :rl_end_kbd_macro   ,
 476      "\C-xE" => :rl_call_last_kbd_macro  ,
 477      "\C-xe" => :rl_call_last_kbd_macro  ,
 478      "\C-x\x7F" => :rl_backward_kill_line
 479   }
 480
 481   @vi_movement_keymap = {
 482      "\C-d" => :rl_vi_eof_maybe ,
 483      "\C-e" => :rl_emacs_editing_mode ,
 484      "\C-g" => :rl_abort ,
 485      "\C-h" => :rl_backward_char ,
 486      "\C-j" => :rl_newline ,
 487      "\C-k" => :rl_kill_line ,
 488      "\C-l" => :rl_clear_screen ,
 489      "\C-m" => :rl_newline ,
 490      "\C-n" => :rl_get_next_history ,
 491      "\C-p" => :rl_get_previous_history ,
 492      "\C-q" => :rl_quoted_insert ,
 493      "\C-r" => :rl_reverse_search_history ,
 494      "\C-s" => :rl_forward_search_history ,
 495      "\C-t" => :rl_transpose_chars ,
 496      "\C-u" => :rl_unix_line_discard ,
 497      "\C-v" => :rl_quoted_insert ,
 498      "\C-w" => :rl_unix_word_rubout ,
 499      "\C-y" => :rl_yank ,
 500      "\C-_" => :rl_vi_undo ,
 501      " " => :rl_forward_char ,
 502      "#" => :rl_insert_comment ,
 503      "$" => :rl_end_of_line ,
 504      "%" => :rl_vi_match ,
 505      "&" => :rl_vi_tilde_expand ,
 506      "*" => :rl_vi_complete ,
 507      "+" => :rl_get_next_history ,
 508      "," => :rl_vi_char_search ,
 509      "-" => :rl_get_previous_history ,
 510      "." => :rl_vi_redo ,
 511      "/" => :rl_vi_search ,
 512      "0" => :rl_beg_of_line ,
 513      "1" => :rl_vi_arg_digit ,
 514      "2" => :rl_vi_arg_digit ,
 515      "3" => :rl_vi_arg_digit ,
 516      "4" => :rl_vi_arg_digit ,
 517      "5" => :rl_vi_arg_digit ,
 518      "6" => :rl_vi_arg_digit ,
 519      "7" => :rl_vi_arg_digit ,
 520      "8" => :rl_vi_arg_digit ,
 521      "9" => :rl_vi_arg_digit ,
 522      "" => :rl_vi_char_search ,
 523      "=" => :rl_vi_complete ,
 524      "?" => :rl_vi_search ,
 525      "A" => :rl_vi_append_eol ,
 526      "B" => :rl_vi_prev_word ,
 527      "C" => :rl_vi_change_to ,
 528      "D" => :rl_vi_delete_to ,
 529      "E" => :rl_vi_end_word ,
 530      "F" => :rl_vi_char_search ,
 531      "G" => :rl_vi_fetch_history ,
 532      "I" => :rl_vi_insert_beg ,
 533      "N" => :rl_vi_search_again ,
 534      "P" => :rl_vi_put ,
 535      "R" => :rl_vi_replace ,
 536      "S" => :rl_vi_subst ,
 537      "T" => :rl_vi_char_search ,
 538      "U" => :rl_revert_line ,
 539      "W" => :rl_vi_next_word ,
 540      "X" => :rl_vi_rubout ,
 541      "Y" => :rl_vi_yank_to ,
 542      "\\" => :rl_vi_complete ,
 543      "^" => :rl_vi_first_print ,
 544      "_" => :rl_vi_yank_arg ,
 545      "`" => :rl_vi_goto_mark ,
 546      "a" => :rl_vi_append_mode ,
 547      "b" => :rl_vi_prev_word ,
 548      "c" => :rl_vi_change_to ,
 549      "d" => :rl_vi_delete_to ,
 550      "e" => :rl_vi_end_word ,
 551      "f" => :rl_vi_char_search ,
 552      "h" => :rl_backward_char ,
 553      "i" => :rl_vi_insertion_mode ,
 554      "j" => :rl_get_next_history ,
 555      "k" => :rl_get_previous_history ,
 556      "l" => :rl_forward_char ,
 557      "m" => :rl_vi_set_mark ,
 558      "n" => :rl_vi_search_again ,
 559      "p" => :rl_vi_put ,
 560      "r" => :rl_vi_change_char ,
 561      "s" => :rl_vi_subst ,
 562      "t" => :rl_vi_char_search ,
 563      "u" => :rl_vi_undo ,
 564      "w" => :rl_vi_next_word ,
 565      "x" => :rl_vi_delete ,
 566      "y" => :rl_vi_yank_to ,
 567      "|" => :rl_vi_column ,
 568      "~" => :rl_vi_change_case
 569   }
 570
 571   @vi_insertion_keymap = {
 572      "\C-a" => :rl_insert  ,
 573      "\C-b" => :rl_insert  ,
 574      "\C-c" => :rl_insert  ,
 575      "\C-d" => :rl_vi_eof_maybe  ,
 576      "\C-e" => :rl_insert  ,
 577      "\C-f" => :rl_insert  ,
 578      "\C-g" => :rl_insert  ,
 579      "\C-h" => :rl_rubout  ,
 580      "\C-i" => :rl_complete  ,
 581      "\C-j" => :rl_newline  ,
 582      "\C-k" => :rl_insert  ,
 583      "\C-l" => :rl_insert  ,
 584      "\C-m" => :rl_newline  ,
 585      "\C-n" => :rl_insert  ,
 586      "\C-o" => :rl_insert  ,
 587      "\C-p" => :rl_insert  ,
 588      "\C-q" => :rl_insert  ,
 589      "\C-r" => :rl_reverse_search_history  ,
 590      "\C-s" => :rl_forward_search_history  ,
 591      "\C-t" => :rl_transpose_chars  ,
 592      "\C-u" => :rl_unix_line_discard  ,
 593      "\C-v" => :rl_quoted_insert  ,
 594      "\C-w" => :rl_unix_word_rubout  ,
 595      "\C-x" => :rl_insert  ,
 596      "\C-y" => :rl_yank  ,
 597      "\C-z" => :rl_insert  ,
 598      "\C-[" => :rl_vi_movement_mode  ,
 599      "\C-\\" => :rl_insert  ,
 600      "\C-]" => :rl_insert  ,
 601      "\C-^" => :rl_insert  ,
 602      "\C-_" => :rl_vi_undo  ,
 603      "\x7F" => :rl_rubout
 604   }
 605
 606   @rl_library_version = RL_LIBRARY_VERSION
 607
 608   @rl_readline_version = RL_READLINE_VERSION
 609
 610   @rl_readline_name = "other"
 611
 612   @rl_getc_function = :rl_getc
 613
 614   # Non-zero tells rl_delete_text and rl_insert_text to not add to
 615   #   the undo list.
 616   @_rl_doing_an_undo = false
 617
 618   # How many unclosed undo groups we currently have.
 619   @_rl_undo_group_level = 0
 620
 621   # The current undo list for THE_LINE.
 622   @rl_undo_list = nil
 623
 624   # Application-specific redisplay function.
 625   @rl_redisplay_function = :rl_redisplay
 626
 627   # Global variables declared here.
 628   # What YOU turn on when you have handled all redisplay yourself.
 629   @rl_display_fixed = false
 630
 631   @_rl_suppress_redisplay = 0
 632   @_rl_want_redisplay = false
 633
 634   # The stuff that gets printed out before the actual text of the line.
 635   #   This is usually pointing to rl_prompt.
 636   @rl_display_prompt = nil
 637
 638   # True if this is `real' readline as opposed to some stub substitute.
 639   @rl_gnu_readline_p = true
 640
 641   for i in 32 .. 255
 642      @emacs_standard_keymap[i.chr] = :rl_insert unless @emacs_standard_keymap[i.chr]
 643      @vi_insertion_keymap[i.chr] = :rl_insert unless @vi_insertion_keymap[i.chr]
 644   end
 645   # A pointer to the keymap that is currently in use.
 646   #   By default, it is the standard emacs keymap.
 647   @_rl_keymap = @emacs_standard_keymap
 648
 649
 650   # The current style of editing.
 651   @rl_editing_mode = @emacs_mode
 652
 653   # The current insert mode:  input (the default) or overwrite
 654   @rl_insert_mode = RL_IM_DEFAULT
 655
 656   # Non-zero if we called this function from _rl_dispatch().  It's present
 657   #   so functions can find out whether they were called from a key binding
 658   #   or directly from an application.
 659   @rl_dispatching = false
 660
 661   # Non-zero if the previous command was a kill command.
 662   @_rl_last_command_was_kill = false
 663
 664   # The current value of the numeric argument specified by the user.
 665   @rl_numeric_arg = 1
 666
 667   # Non-zero if an argument was typed.
 668   @rl_explicit_arg = false
 669
 670   # Temporary value used while generating the argument.
 671   @rl_arg_sign = 1
 672
 673   # Non-zero means we have been called at least once before.
 674   @rl_initialized = false
 675
 676   # Flags word encapsulating the current readline state.
 677   @rl_readline_state = RL_STATE_NONE
 678
 679   # The current offset in the current input line.
 680   @rl_point = 0
 681
 682   # Mark in the current input line.
 683   @rl_mark = 0
 684
 685   # Length of the current input line.
 686   @rl_end = 0
 687
 688   # Make this non-zero to return the current input_line.
 689   @rl_done = false
 690
 691   # The last function executed by readline.
 692   @rl_last_func = nil
 693
 694   # Top level environment for readline_internal ().
 695   @readline_top_level = nil
 696
 697   # The streams we interact with.
 698   @_rl_in_stream = nil
 699   @_rl_out_stream = nil
 700
 701   # The names of the streams that we do input and output to.
 702   @rl_instream = nil
 703   @rl_outstream = nil
 704
 705   @pop_index = 0
 706   @push_index = 0
 707   @ibuffer = 0.chr * 512
 708   @ibuffer_len = @ibuffer.length - 1
 709
 710
 711   # Non-zero means echo characters as they are read.  Defaults to no echo
 712   #   set to 1 if there is a controlling terminal, we can get its attributes,
 713   #   and the attributes include `echo'.  Look at rltty.c:prepare_terminal_settings
 714   #   for the code that sets it.
 715   @readline_echoing_p = false
 716
 717   # Current prompt.
 718   @rl_prompt = nil
 719   @rl_visible_prompt_length = 0
 720
 721   # Set to non-zero by calling application if it has already printed rl_prompt
 722   #   and does not want readline to do it the first time.
 723   @rl_already_prompted = false
 724
 725   # The number of characters read in order to type this complete command.
 726   @rl_key_sequence_length = 0
 727
 728   # If non-zero, then this is the address of a function to call just
 729   #   before readline_internal_setup () prints the first prompt.
 730   @rl_startup_hook = nil
 731
 732   # If non-zero, this is the address of a function to call just before
 733   #   readline_internal_setup () returns and readline_internal starts
 734   #   reading input characters.
 735   @rl_pre_input_hook = nil
 736
 737   # The character that can generate an EOF.  Really read from
 738   #   the terminal driver... just defaulted here.
 739   @_rl_eof_char = "\cD"
 740
 741   # Non-zero makes this the next keystroke to read.
 742   @rl_pending_input = 0
 743
 744   # Pointer to a useful terminal name.
 745   @rl_terminal_name = nil
 746
 747   # Non-zero means to always use horizontal scrolling in line display.
 748   @_rl_horizontal_scroll_mode = false
 749
 750   # Non-zero means to display an asterisk at the starts of history lines
 751   #   which have been modified.
 752   @_rl_mark_modified_lines = false
 753
 754   # The style of `bell' notification preferred.  This can be set to NO_BELL,
 755   #   AUDIBLE_BELL, or VISIBLE_BELL.
 756   @_rl_bell_preference = AUDIBLE_BELL
 757
 758   # String inserted into the line by rl_insert_comment ().
 759   @_rl_comment_begin = nil
 760
 761   # Keymap holding the function currently being executed.
 762   @rl_executing_keymap = nil
 763
 764   # Keymap we're currently using to dispatch.
 765   @_rl_dispatching_keymap = nil
 766
 767   # Non-zero means to erase entire line, including prompt, on empty input lines.
 768   @rl_erase_empty_line = false
 769
 770   # Non-zero means to read only this many characters rather than up to a
 771   #   character bound to accept-line.
 772   @rl_num_chars_to_read = 0
 773
 774   # Line buffer and maintenence.
 775   @rl_line_buffer = nil
 776
 777   # Key sequence `contexts'
 778   @_rl_kscxt = nil
 779
 780   # Non-zero means do not parse any lines other than comments and
 781   #   parser directives.
 782   @_rl_parsing_conditionalized_out = false
 783
 784   # Non-zero means to convert characters with the meta bit set to
 785   #   escape-prefixed characters so we can indirect through
 786   #   emacs_meta_keymap or vi_escape_keymap.
 787   @_rl_convert_meta_chars_to_ascii = true
 788
 789   # Non-zero means to output characters with the meta bit set directly
 790   #   rather than as a meta-prefixed escape sequence.
 791   @_rl_output_meta_chars = false
 792
 793   # Non-zero means to look at the termios special characters and bind
 794   #   them to equivalent readline functions at startup.
 795   @_rl_bind_stty_chars = true
 796
 797   @rl_completion_display_matches_hook = nil
 798
 799   XOK = 1
 800
 801   @_rl_term_clreol = nil
 802   @_rl_term_clrpag = nil
 803   @_rl_term_cr = nil
 804   @_rl_term_backspace = nil
 805   @_rl_term_goto = nil
 806   @_rl_term_pc = nil
 807
 808   # Non-zero if we determine that the terminal can do character insertion.
 809   @_rl_terminal_can_insert = false
 810
 811   # How to insert characters.
 812   @_rl_term_im = nil
 813   @_rl_term_ei = nil
 814   @_rl_term_ic = nil
 815   @_rl_term_ip = nil
 816   @_rl_term_IC = nil
 817
 818   # How to delete characters.
 819   @_rl_term_dc = nil
 820   @_rl_term_DC = nil
 821
 822   @_rl_term_forward_char = nil
 823
 824   # How to go up a line.
 825   @_rl_term_up = nil
 826
 827   # A visible bell; char if the terminal can be made to flash the screen.
 828   @_rl_visible_bell = nil
 829
 830   # Non-zero means the terminal can auto-wrap lines.
 831   @_rl_term_autowrap = true
 832
 833   # Non-zero means that this terminal has a meta key.
 834   @term_has_meta = 0
 835
 836   # The sequences to write to turn on and off the meta key, if this
 837   #   terminal has one.
 838   @_rl_term_mm = nil
 839   @_rl_term_mo = nil
 840
 841   # The key sequences output by the arrow keys, if this terminal has any.
 842   @_rl_term_ku = nil
 843   @_rl_term_kd = nil
 844   @_rl_term_kr = nil
 845   @_rl_term_kl = nil
 846
 847   # How to initialize and reset the arrow keys, if this terminal has any.
 848   @_rl_term_ks = nil
 849   @_rl_term_ke = nil
 850
 851   # The key sequences sent by the Home and End keys, if any.
 852   @_rl_term_kh = nil
 853   @_rl_term_kH = nil
 854   @_rl_term_at7 = nil
 855
 856   # Delete key
 857   @_rl_term_kD = nil
 858
 859   # Insert key
 860   @_rl_term_kI = nil
 861
 862   # Cursor control
 863   @_rl_term_vs = nil    # very visible
 864   @_rl_term_ve = nil    # normal
 865
 866   # Variables that hold the screen dimensions, used by the display code.
 867   @_rl_screenwidth = @_rl_screenheight = @_rl_screenchars = 0
 868
 869   # Non-zero means the user wants to enable the keypad.
 870   @_rl_enable_keypad = false
 871
 872   # Non-zero means the user wants to enable a meta key.
 873   @_rl_enable_meta = true
 874
 875   # ****************************************************************
 876   #
 877   #    Completion matching, from readline's point of view.
 878   #
 879   # ****************************************************************
 880
 881   # Variables known only to the readline library.
 882
 883   # If non-zero, non-unique completions always show the list of matches.
 884   @_rl_complete_show_all = false
 885
 886   # If non-zero, non-unique completions show the list of matches, unless it
 887   #   is not possible to do partial completion and modify the line.
 888   @_rl_complete_show_unmodified = false
 889
 890   # If non-zero, completed directory names have a slash appended.
 891   @_rl_complete_mark_directories = true
 892
 893   # If non-zero, the symlinked directory completion behavior introduced in
 894   #   readline-4.2a is disabled, and symlinks that point to directories have
 895   #   a slash appended (subject to the value of _rl_complete_mark_directories).
 896   #   This is user-settable via the mark-symlinked-directories variable.
 897   @_rl_complete_mark_symlink_dirs = false
 898
 899   # If non-zero, completions are printed horizontally in alphabetical order,
 900   #   like `ls -x'.
 901   @_rl_print_completions_horizontally = false
 902
 903   @_rl_completion_case_fold = false
 904
 905   # If non-zero, don't match hidden files (filenames beginning with a `.' on
 906   #   Unix) when doing filename completion.
 907   @_rl_match_hidden_files = true
 908
 909   # Global variables available to applications using readline.
 910
 911   # Non-zero means add an additional character to each filename displayed
 912   #   during listing completion iff rl_filename_completion_desired which helps
 913   #   to indicate the type of file being listed.
 914   @rl_visible_stats = false
 915
 916   # If non-zero, then this is the address of a function to call when
 917   #   completing on a directory name.  The function is called with
 918   #   the address of a string (the current directory name) as an arg.
 919   @rl_directory_completion_hook = nil
 920
 921   @rl_directory_rewrite_hook = nil
 922
 923   # Non-zero means readline completion functions perform tilde expansion.
 924   @rl_complete_with_tilde_expansion = false
 925
 926   # Pointer to the generator function for completion_matches ().
 927   #   NULL means to use rl_filename_completion_function (), the default filename
 928   #   completer.
 929   @rl_completion_entry_function = nil
 930
 931   # Pointer to alternative function to create matches.
 932   #   Function is called with TEXT, START, and END.
 933   #   START and END are indices in RL_LINE_BUFFER saying what the boundaries
 934   #   of TEXT are.
 935   #   If this function exists and returns NULL then call the value of
 936   #   rl_completion_entry_function to try to match, otherwise use the
 937   #   array of strings returned.
 938   @rl_attempted_completion_function = nil
 939
 940   # Non-zero means to suppress normal filename completion after the
 941   #   user-specified completion function has been called.
 942   @rl_attempted_completion_over = false
 943
 944   # Set to a character indicating the type of completion being performed
 945   #   by rl_complete_internal, available for use by application completion
 946   #   functions.
 947   @rl_completion_type = 0
 948
 949   # Up to this many items will be displayed in response to a
 950   #   possible-completions call.  After that, we ask the user if
 951   #   she is sure she wants to see them all.  A negative value means
 952   #   don't ask.
 953   @rl_completion_query_items = 100
 954
 955   @_rl_page_completions = 1
 956
 957   # The basic list of characters that signal a break between words for the
 958   #   completer routine.  The contents of this variable is what breaks words
 959   #   in the shell, i.e. " \t\n\"\\'`@$><="
 960   @rl_basic_word_break_characters = " \t\n\"\\'`@$><=|&{(" # })
 961
 962   # List of basic quoting characters.
 963   @rl_basic_quote_characters = "\"'"
 964
 965   # The list of characters that signal a break between words for
 966   #   rl_complete_internal.  The default list is the contents of
 967   #   rl_basic_word_break_characters.
 968   @rl_completer_word_break_characters = nil
 969
 970   # Hook function to allow an application to set the completion word
 971   #   break characters before readline breaks up the line.  Allows
 972   #   position-dependent word break characters.
 973   @rl_completion_word_break_hook = nil
 974
 975   # List of characters which can be used to quote a substring of the line.
 976   #   Completion occurs on the entire substring, and within the substring
 977   #   rl_completer_word_break_characters are treated as any other character,
 978   #   unless they also appear within this list.
 979   @rl_completer_quote_characters = nil
 980
 981   # List of characters that should be quoted in filenames by the completer.
 982   @rl_filename_quote_characters = nil
 983
 984   # List of characters that are word break characters, but should be left
 985   #   in TEXT when it is passed to the completion function.  The shell uses
 986   #   this to help determine what kind of completing to do.
 987   @rl_special_prefixes = nil
 988
 989   # If non-zero, then disallow duplicates in the matches.
 990   @rl_ignore_completion_duplicates = true
 991
 992   # Non-zero means that the results of the matches are to be treated
 993   #   as filenames.  This is ALWAYS zero on entry, and can only be changed
 994   #   within a completion entry finder function.
 995   @rl_filename_completion_desired = false
 996
 997   # Non-zero means that the results of the matches are to be quoted using
 998   #   double quotes (or an application-specific quoting mechanism) if the
 999   #   filename contains any characters in rl_filename_quote_chars.  This is
1000   #   ALWAYS non-zero on entry, and can only be changed within a completion
1001   #   entry finder function.
1002   @rl_filename_quoting_desired = true
1003
1004   # This function, if defined, is called by the completer when real
1005   #   filename completion is done, after all the matching names have been
1006   #   generated. It is passed a (char**) known as matches in the code below.
1007   #   It consists of a NULL-terminated array of pointers to potential
1008   #   matching strings.  The 1st element (matches[0]) is the maximal
1009   #   substring that is common to all matches. This function can re-arrange
1010   #   the list of matches as required, but all elements of the array must be
1011   #   free()'d if they are deleted. The main intent of this function is
1012   #   to implement FIGNORE a la SunOS csh.
1013   @rl_ignore_some_completions_function = nil
1014
1015   # Set to a function to quote a filename in an application-specific fashion.
1016   #   Called with the text to quote, the type of match found (single or multiple)
1017   #   and a pointer to the quoting character to be used, which the function can
1018   #   reset if desired.
1019   #rl_filename_quoting_function = rl_quote_filename
1020
1021   # Function to call to remove quoting characters from a filename.  Called
1022   #   before completion is attempted, so the embedded quotes do not interfere
1023   #   with matching names in the file system.  Readline doesn't do anything
1024   #   with this it's set only by applications.
1025   @rl_filename_dequoting_function = nil
1026
1027   # Function to call to decide whether or not a word break character is
1028   #   quoted.  If a character is quoted, it does not break words for the
1029   #   completer.
1030   @rl_char_is_quoted_p = nil
1031
1032   # If non-zero, the completion functions don't append anything except a
1033   #   possible closing quote.  This is set to 0 by rl_complete_internal and
1034   #   may be changed by an application-specific completion function.
1035   @rl_completion_suppress_append = false
1036
1037   # Character appended to completed words when at the end of the line.  The
1038   #   default is a space.
1039   @rl_completion_append_character = ' '
1040
1041   # If non-zero, the completion functions don't append any closing quote.
1042   #   This is set to 0 by rl_complete_internal and may be changed by an
1043   #   application-specific completion function.
1044   @rl_completion_suppress_quote = false
1045
1046   # Set to any quote character readline thinks it finds before any application
1047   #   completion function is called.
1048   @rl_completion_quote_character = 0
1049
1050   # Set to a non-zero value if readline found quoting anywhere in the word to
1051   #   be completed set before any application completion function is called.
1052   @rl_completion_found_quote = false
1053
1054   # If non-zero, a slash will be appended to completed filenames that are
1055   #  symbolic links to directory names, subject to the value of the
1056   #  mark-directories variable (which is user-settable).  This exists so
1057   #  that application completion functions can override the user's preference
1058   #  (set via the mark-symlinked-directories variable) if appropriate.
1059   #  It's set to the value of _rl_complete_mark_symlink_dirs in
1060   #  rl_complete_internal before any application-specific completion
1061   #  function is called, so without that function doing anything, the user's
1062   #  preferences are honored.
1063   @rl_completion_mark_symlink_dirs = false
1064
1065   # If non-zero, inhibit completion (temporarily).
1066   @rl_inhibit_completion = false
1067
1068   # Variables local to this file.
1069
1070   # Local variable states what happened during the last completion attempt.
1071   @completion_changed_buffer = nil
1072
1073   # Non-zero means treat 0200 bit in terminal input as Meta bit.
1074   @_rl_meta_flag = false
1075
1076   # Stack of previous values of parsing_conditionalized_out.
1077   @if_stack = []
1078   @if_stack_depth = 0
1079
1080
1081   # The last key bindings file read.
1082   @last_readline_init_file = nil
1083
1084   # The file we're currently reading key bindings from.
1085   @current_readline_init_file = nil
1086   @current_readline_init_include_level = 0
1087   @current_readline_init_lineno = 0
1088
1089   ENV["HOME"] ||= ENV["HOMEDRIVE"]+ENV["HOMEPATH"]
1090
1091   @directory = nil
1092   @filename = nil
1093   @dirname = nil
1094   @users_dirname = nil
1095   @filename_len = 0
1096
1097   attr_accessor :rl_attempted_completion_function,:rl_deprep_term_function,
1098   :rl_event_hook,:rl_attempted_completion_over,:rl_basic_quote_characters,
1099   :rl_basic_word_break_characters,:rl_completer_quote_characters,
1100   :rl_completer_word_break_characters,:rl_completion_append_character,
1101   :rl_filename_quote_characters,:rl_instream,:rl_library_version,:rl_outstream,
1102   :rl_readline_name,:history_length,:history_base
1103
1104   module_function
1105   # Okay, now we write the entry_function for filename completion.  In the
1106   #   general case.  Note that completion in the shell is a little different
1107   #   because of all the pathnames that must be followed when looking up the
1108   #   completion for a command.
1109   def rl_filename_completion_function(text, state)
1110      # If we don't have any state, then do some initialization.
1111      if (state == 0)
1112         # If we were interrupted before closing the directory or reading
1113         #all of its contents, close it.
1114         if(@directory)
1115            @directory.close
1116            @directory = nil
1117         end
1118         text.delete!(0.chr)
1119         @filename = text.dup
1120         if text.length == 0
1121            text = "."
1122         end
1123         @dirname = File.dirname(text)
1124         # We aren't done yet.  We also support the "~user" syntax.
1125
1126         # Save the version of the directory that the user typed.
1127         @users_dirname = @dirname.dup
1128
1129         if (@dirname[0] == ?~)
1130            @dirname = File.expand_path(@dirname)
1131         end
1132
1133         # The directory completion hook should perform any necessary
1134         #   dequoting.
1135         if (@rl_directory_completion_hook && send(rl_directory_completion_hook,@dirname))
1136            @users_dirname = @dirname.dup
1137         elsif (@rl_completion_found_quote && @rl_filename_dequoting_function)
1138            # delete single and double quotes
1139            @temp = send(@rl_filename_dequoting_function,@users_dirname, @rl_completion_quote_character)
1140            @users_dirname = temp
1141         end
1142
1143         @directory = Dir.new(@dirname)
1144
1145         # Now dequote a non-null filename.
1146         if (@filename && @filename.length>0 && @rl_completion_found_quote && @rl_filename_dequoting_function)
1147            # delete single and double quotes
1148            temp = send(@rl_filename_dequoting_function,@filename, @rl_completion_quote_character)
1149            @filename = temp
1150         end
1151
1152         @filename_len = @filename.length
1153         @rl_filename_completion_desired = true
1154      end
1155
1156      # At this point we should entertain the possibility of hacking wildcarded
1157      #   filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
1158      #   contains globbing characters, then build an array of directories, and
1159      #   then map over that list while completing.
1160      # *** UNIMPLEMENTED ***
1161
1162      # Now that we have some state, we can read the directory.
1163      entry = nil
1164      while(@directory && (entry = @directory.read))
1165         d_name = entry
1166         # Special case for no filename.  If the user has disabled the
1167         #   `match-hidden-files' variable, skip filenames beginning with `.'.
1168         #All other entries except "." and ".." match.
1169         if (@filename_len == 0)
1170            if (!@_rl_match_hidden_files && d_name[0]==?.)
1171               next
1172            end
1173            if (d_name != '.' && d_name != '..')
1174               break
1175            end
1176         else
1177            # Otherwise, if these match up to the length of filename, then
1178            #   it is a match.
1179
1180            if (@_rl_completion_case_fold)
1181               break if d_name =~ /^#{Regexp.escape(@filename)}/i
1182            else
1183               break if d_name =~ /^#{Regexp.escape(@filename)}/
1184            end
1185         end
1186      end
1187
1188      if entry.nil?
1189         if @directory
1190            @directory.close
1191            @directory = nil
1192         end
1193         @dirname = nil
1194         @filename = nil
1195         @users_dirname = nil
1196
1197         return nil
1198      else
1199         if (@dirname != '.')
1200            if (@rl_complete_with_tilde_expansion && @users_dirname[0] == ?~)
1201               temp = @dirname
1202               if(temp[-1] != ?/)
1203                  temp += '/'
1204               end
1205            else
1206               temp = @users_dirname
1207               if(temp[-1] != ?/)
1208                  temp += '/'
1209               end
1210            end
1211            temp += entry
1212         else
1213            temp = entry.dup
1214         end
1215         return (temp)
1216      end
1217   end
1218
1219   # A completion function for usernames.
1220   #   TEXT contains a partial username preceded by a random
1221   #   character (usually `~').
1222   def rl_username_completion_function(text, state)
1223      return nil if RUBY_PLATFORM =~ /mswin|mingw/
1224
1225      if (state == 0)
1226         first_char = text[0]
1227         first_char_loc = (text[0] == ?~ ? 1 : 0)
1228
1229         username = text[first_char_loc..-1]
1230         namelen = username.length
1231         Etc.setpwent()
1232      end
1233
1234      while (entry = Etc.getpwent())
1235         # Null usernames should result in all users as possible completions.
1236         break if (namelen == 0 || entry.name =~ /^#{username}/ )
1237      end
1238
1239      if entry.nil?
1240         Etc.endpwent()
1241         return nil
1242      else
1243         value = text.dup
1244         value[first_char_loc..-1] = entry.name
1245
1246         if (first_char == ?~)
1247            @rl_filename_completion_desired = true
1248         end
1249
1250         return (value)
1251      end
1252   end
1253
1254   #*************************************************************
1255   #
1256   #  Application-callable completion match generator functions
1257   #
1258   #*************************************************************
1259
1260   # Return an array of (char *) which is a list of completions for TEXT.
1261   #   If there are no completions, return a NULL pointer.
1262   #   The first entry in the returned array is the substitution for TEXT.
1263   #   The remaining entries are the possible completions.
1264   #   The array is terminated with a NULL pointer.
1265   #
1266   #   ENTRY_FUNCTION is a function of two args, and returns a (char *).
1267   #     The first argument is TEXT.
1268   #     The second is a state argument it should be zero on the first call, and
1269   #     non-zero on subsequent calls.  It returns a NULL pointer to the caller
1270   #     when there are no more matches.
1271   #
1272   def rl_completion_matches(text, entry_function)
1273      matches = 0
1274      match_list_size = 10
1275      match_list = []
1276      match_list[1] = nil
1277      while (string = send(entry_function, text, matches))
1278         match_list[matches+=1] = string
1279         match_list[matches+1] = nil
1280      end
1281
1282      # If there were any matches, then look through them finding out the
1283      # lowest common denominator.  That then becomes match_list[0].
1284      if (matches!=0)
1285         compute_lcd_of_matches(match_list, matches, text)
1286      else                # There were no matches.
1287         match_list = nil
1288      end
1289      return (match_list)
1290   end
1291
1292   def _rl_to_lower(char)
1293      char.nil? ? nil : char.chr.downcase
1294   end
1295
1296   # Find the common prefix of the list of matches, and put it into
1297   #   matches[0].
1298   def compute_lcd_of_matches(match_list, matches, text)
1299      # If only one match, just use that.  Otherwise, compare each
1300      #   member of the list with the next, finding out where they
1301      #   stop matching.
1302      if (matches == 1)
1303         match_list[0] = match_list[1]
1304         match_list[1] = nil
1305         return 1
1306      end
1307
1308      i = 1
1309      low = 100000
1310      while(i<matches)
1311         if (@_rl_completion_case_fold)
1312            si = 0
1313            while((c1 = _rl_to_lower(match_list[i][si])) &&
1314               (c2 = _rl_to_lower(match_list[i + 1][si])))
1315               if !@rl_byte_oriented
1316                  if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
1317                     break
1318                  elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
1319                     si += v - 1
1320                  end
1321               else
1322                  break if (c1 != c2)
1323               end
1324               si += 1
1325            end
1326         else
1327            si = 0
1328            while((c1 = match_list[i][si]) &&
1329               (c2 = match_list[i + 1][si]))
1330               if !@rl_byte_oriented
1331                  if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
1332                     break
1333                  elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
1334                     si += v - 1
1335                  end
1336               else
1337                  break if (c1 != c2)
1338               end
1339               si += 1
1340            end
1341         end
1342
1343         if (low > si)
1344            low = si
1345         end
1346         i += 1
1347      end
1348
1349      # If there were multiple matches, but none matched up to even the
1350      #   first character, and the user typed something, use that as the
1351      #   value of matches[0].
1352      if (low == 0 && text && text.length>0 )
1353         match_list[0] = text.dup
1354      else
1355         # XXX - this might need changes in the presence of multibyte chars
1356
1357         # If we are ignoring case, try to preserve the case of the string
1358         # the user typed in the face of multiple matches differing in case.
1359         if (@_rl_completion_case_fold)
1360
1361            # We're making an assumption here:
1362            #  IF we're completing filenames AND
1363            #     the application has defined a filename dequoting function AND
1364            #     we found a quote character AND
1365            #     the application has requested filename quoting
1366            #  THEN
1367            #     we assume that TEXT was dequoted before checking against
1368            #     the file system and needs to be dequoted here before we
1369            #     check against the list of matches
1370            #  FI
1371            if (@rl_filename_completion_desired &&
1372               @rl_filename_dequoting_function &&
1373               @rl_completion_found_quote &&
1374               @rl_filename_quoting_desired)
1375
1376               dtext = send(@rl_filename_dequoting_function,text, @rl_completion_quote_character)
1377               text = dtext
1378            end
1379
1380            # sort the list to get consistent answers.
1381            match_list = [match_list[0]] + match_list[1..-1].sort
1382
1383            si = text.length
1384            if (si <= low)
1385               for i in 1 .. matches
1386                  if match_list[i][0,si] == text
1387                     match_list[0] = match_list[i][0,low]
1388                     break
1389                  end
1390                  # no casematch, use first entry
1391                  if (i > matches)
1392                     match_list[0] = match_list[1][0,low]
1393                  end
1394               end
1395            else
1396               # otherwise, just use the text the user typed.
1397               match_list[0] = text[0,low]
1398            end
1399         else
1400            match_list[0] = match_list[1][0,low]
1401         end
1402      end
1403
1404      return matches
1405   end
1406
1407
1408   def rl_vi_editing_mode(count, key)
1409      _rl_set_insert_mode(RL_IM_INSERT, 1)    # vi mode ignores insert mode
1410      @rl_editing_mode = @vi_mode
1411      rl_vi_insertion_mode(1, key)
1412      0
1413   end
1414
1415   # Switching from one mode to the other really just involves
1416   #   switching keymaps.
1417   def rl_vi_insertion_mode(count, key)
1418      @_rl_keymap = @vi_insertion_keymap
1419      @_rl_vi_last_key_before_insert = key
1420      0
1421   end
1422
1423   def rl_emacs_editing_mode(count, key)
1424      @rl_editing_mode = @emacs_mode
1425      _rl_set_insert_mode(RL_IM_INSERT, 1) # emacs mode default is insert mode
1426      @_rl_keymap = @emacs_standard_keymap
1427      0
1428   end
1429
1430   # Function for the rest of the library to use to set insert/overwrite mode.
1431   def _rl_set_insert_mode(im, force)
1432      @r

Large files files are truncated, but you can click here to view the full file