Module: RbReadline

Included in:
Readline
Defined in:
lib/rbreadline.rb,
lib/rbreadline/version.rb

Constant Summary collapse

RL_LIBRARY_VERSION =
"5.2"
RL_READLINE_VERSION =
0x0502
EOF =
"\xFF"
ESC =
"\C-["
PAGE =
"\C-L"
SPACE =
"\x20"
RETURN =
"\C-M"
ABORT_CHAR =
"\C-G"
TAB =
"\t"
RUBOUT =
"\x7f"
NEWLINE =
"\n"
DEFAULT_BUFFER_SIZE =
256
DEFAULT_MAX_KILLS =
10
MB_FIND_NONZERO =
1
MB_FIND_ANY =
0
MB_LEN_MAX =
4
DEFAULT_INPUTRC =
"~/.inputrc"
SYS_INPUTRC =
"/etc/inputrc"
UpCase =
1
DownCase =
2
CapCase =
3
EVENT_NOT_FOUND =

Possible history errors passed to hist_error.

0
BAD_WORD_SPEC =
1
SUBST_FAILED =
2
BAD_MODIFIER =
3
NO_PREV_SUBST =
4
ANCHORED_SEARCH =

Possible definitions for history starting point specification.

1
NON_ANCHORED_SEARCH =
0
HISTORY_APPEND =

Possible definitions for what style of writing the history file we want.

0
HISTORY_OVERWRITE =
1
READERR =

Input error; can be returned by (*rl_getc_function) if readline is reading

a top-level command (RL_ISSTATE (RL_STATE_READCMD)).
0xFE.chr
RL_PROMPT_START_IGNORE =

Definitions available for use by readline clients.

1.chr
RL_PROMPT_END_IGNORE =
2.chr
NO_MATCH =

Possible values for do_replace argument to rl_filename_quoting_function,

called by rl_complete_internal.
0
SINGLE_MATCH =
1
MULT_MATCH =
2
NUM_SAWMINUS =

Callback data for reading numeric arguments

0x01
NUM_SAWDIGITS =
0x02
NUM_READONE =
0x04
KSEQ_DISPATCHED =

A context for reading key sequences longer than a single character when

using the callback interface.
0x01
KSEQ_SUBSEQ =
0x02
KSEQ_RECURSIVE =
0x04
RL_STATE_NONE =

Possible state values for rl_readline_state

0x000000
RL_STATE_INITIALIZING =

no state before first call

0x000001
RL_STATE_INITIALIZED =

initializing

0x000002
RL_STATE_TERMPREPPED =

initialization done

0x000004
RL_STATE_READCMD =

terminal is prepped

0x000008
RL_STATE_METANEXT =

reading a command key

0x000010
RL_STATE_DISPATCHING =

reading input after ESC

0x000020
RL_STATE_MOREINPUT =

dispatching to a command

0x000040
RL_STATE_ISEARCH =

reading more input in a command function

0x000080
RL_STATE_NSEARCH =

doing incremental search

0x000100
RL_STATE_SEARCH =

doing non-inc search

0x000200
RL_STATE_NUMERICARG =

doing a history search

0x000400
RL_STATE_MACROINPUT =

reading numeric argument

0x000800
RL_STATE_MACRODEF =

getting input from a macro

0x001000
RL_STATE_OVERWRITE =

defining keyboard macro

0x002000
RL_STATE_COMPLETING =

overwrite mode

0x004000
RL_STATE_SIGHANDLER =

doing completion

0x008000
RL_STATE_UNDOING =

in readline sighandler

0x010000
RL_STATE_INPUTPENDING =

doing an undo

0x020000
RL_STATE_TTYCSAVED =

rl_execute_next called

0x040000
RL_STATE_CALLBACK =

tty special chars saved

0x080000
RL_STATE_VIMOTION =

using the callback interface

0x100000
RL_STATE_MULTIKEY =

reading vi motion arg

0x200000
RL_STATE_VICMDONCE =

reading multiple-key command

0x400000
RL_STATE_DONE =

entered vi command mode at least once

0x800000
NO_BELL =

done accepted line

0
AUDIBLE_BELL =
1
VISIBLE_BELL =
2
FTO =

Definitions used when searching the line for characters. NOTE: it is necessary that opposite directions are inverses

1
BTO =

forward to

-1     # backward to
FFIND =

backward to

2
BFIND =

forward find

-2     # backward find
RL_QF_SINGLE_QUOTE =

Possible values for the found_quote flags word used by the completion

functions.  It says what kind of (shell-like) quoting we found anywhere
in the line.
0x01
RL_QF_DOUBLE_QUOTE =
0x02
RL_QF_BACKSLASH =
0x04
RL_QF_OTHER_QUOTE =
0x08
KEYMAP_SIZE =
257
ANYOTHERKEY =
KEYMAP_SIZE-1
RL_IM_INSERT =
1
RL_IM_OVERWRITE =
0
RL_IM_DEFAULT =
RL_IM_INSERT
ISFUNC =
0
ISKMAP =
1
ISMACR =
2
HISTORY_WORD_DELIMITERS =
" \t\n;&()|<>"
HISTORY_QUOTE_CHARACTERS =
"\"'`"
RL_SEARCH_ISEARCH =

incremental search

0x01
RL_SEARCH_NSEARCH =

non-incremental search

0x02
RL_SEARCH_CSEARCH =

intra-line char search

0x04
SF_REVERSE =

search flags

0x01
SF_FOUND =
0x02
SF_FAILED =
0x04
XOK =
1
RB_READLINE_VERSION =
"0.5.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#history_base=(value) ⇒ Object

Sets the attribute history_base

Parameters:

  • value

    the value to set the attribute history_base to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def history_base=(value)
  @history_base = value
end

#history_length=(value) ⇒ Object

Sets the attribute history_length

Parameters:

  • value

    the value to set the attribute history_length to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def history_length=(value)
  @history_length = value
end

#rl_point=(value) ⇒ Object

Sets the attribute rl_point

Parameters:

  • value

    the value to set the attribute rl_point to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_point=(value)
  @rl_point = value
end

Class Method Details

.__rl_fix_point(x) ⇒ Object

Fix up point so that it is within the line boundaries after killing

text.  If FIX_MARK_TOO is non-zero, the mark is forced within line
boundaries also.

4991
4992
4993
4994
4995
4996
4997
4998
4999
# File 'lib/rbreadline.rb', line 4991

def __rl_fix_point(x)
  if (x > @rl_end)
    @rl_end
  elsif (x < 0)
    0
  else
    x
  end
end

._extract_last_quote(string, quote_char) ⇒ Object


7603
7604
7605
7606
7607
# File 'lib/rbreadline.rb', line 7603

def _extract_last_quote(string, quote_char)
  quote_char = Regexp.escape(quote_char)
  string =~ /(#{quote_char}(?:\\.|[^#{quote_char}])+?#{quote_char}) *$/
  $1
end

._rl_abort_internalObject

How to abort things.


4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
# File 'lib/rbreadline.rb', line 4796

def _rl_abort_internal()
  rl_ding()
  rl_clear_message()
  _rl_reset_argument()
  rl_clear_pending_input()

  rl_unsetstate(RL_STATE_MACRODEF)

  @rl_last_func = nil
  #throw :readline_top_level
  send(@rl_redisplay_function)
  @_rl_want_redisplay = false
  0
end

._rl_adjust_point(string, point) ⇒ Object

adjust pointed byte and find mbstate of the point of string.

adjusted point will be point <= adjusted_point, and returns
differences of the byte(adjusted_point - point).
if point is invalied (point < 0 || more than string length),
it returns -1

8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
# File 'lib/rbreadline.rb', line 8616

def _rl_adjust_point(string, point)

  length = string.length
  return -1 if (point < 0)
  return -1 if (length < point)

  pos = 0

  case @encoding
  when 'E'
    x = string.scan(/./me)
    i, len = 0, x.length
    while (pos < point && i < len)
      pos += x[i].length
      i += 1
    end
  when 'S'
    x = string.scan(/./ms)
    i, len = 0, x.length
    while (pos < point && i < len)
      pos += x[i].length
      i += 1
    end
  when 'U'
    x = string.scan(/./mu)
    i, len = 0, x.length
    while (pos < point && i < len)
      pos += x[i].length
      i += 1
    end
  when 'X'
    enc = string.encoding
    str = string.force_encoding(@encoding_name)
    len = str.length
    if point <= length / 2
      # count byte size from head
      i = 0
      while (pos < point && i < len)
        pos += str[i].bytesize
        i += 1
      end
    else
      # count byte size from tail
      pos = str.bytesize
      i = len - 1
      while (pos > point && i >= 0)
        pos -= str[i].bytesize
        i -= 1
      end
      pos += str[i + 1].bytesize if pos < point
    end
    string.force_encoding(enc)
  else
    pos = point
  end
  pos - point
end

._rl_any_typeinObject


5561
5562
5563
# File 'lib/rbreadline.rb', line 5561

def _rl_any_typein()
  return (@push_index != @pop_index)
end

._rl_arg_dispatch(cxt, c) ⇒ Object

Process C as part of the current numeric argument. Return -1 if the

argument should be aborted, 0 if we should not read any more chars, and
1 if we should continue to read chars.

7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
# File 'lib/rbreadline.rb', line 7806

def _rl_arg_dispatch(cxt, c)
  key = c

  # If we see a key bound to `universal-argument' after seeing digits,
  #    it ends the argument but is otherwise ignored.
  if (@_rl_keymap[c] == :rl_universal_argument)
    if ((cxt & NUM_SAWDIGITS) == 0)
      @rl_numeric_arg *= 4
      return 1
    elsif (rl_isstate(RL_STATE_CALLBACK))
      @_rl_argcxt |= NUM_READONE
      return 0 # XXX
    else
      rl_setstate(RL_STATE_MOREINPUT)
      key = rl_read_key()
      rl_unsetstate(RL_STATE_MOREINPUT)
      rl_restore_prompt()
      rl_clear_message()
      rl_unsetstate(RL_STATE_NUMERICARG)
      if key.is_a?(Integer) && key < 0
        return -1
      end
      return (_rl_dispatch(key, @_rl_keymap))
    end
  end

  #c = (c[0].ord & ~0x80).chr
  r = c[1,1]
  if (r>='0' && r<='9')
    r = r.to_i
    @rl_numeric_arg = @rl_explicit_arg ? (@rl_numeric_arg * 10) +  r : r
    @rl_explicit_arg = 1
    @_rl_argcxt |= NUM_SAWDIGITS
  elsif (c == '-' && !@rl_explicit_arg)
    @rl_numeric_arg = 1
    @_rl_argcxt |= NUM_SAWMINUS
    @rl_arg_sign = -1
  else
    # Make M-- command equivalent to M--1 command.
    if ((@_rl_argcxt & NUM_SAWMINUS)!=0 && @rl_numeric_arg == 1 && !@rl_explicit_arg)
      @rl_explicit_arg = 1
    end
    rl_restore_prompt()
    rl_clear_message()
    rl_unsetstate(RL_STATE_NUMERICARG)

    r = _rl_dispatch(key, @_rl_keymap)
    if (rl_isstate(RL_STATE_CALLBACK))
      # At worst, this will cause an extra redisplay.  Otherwise,
      #   we have to wait until the next character comes in.
      if (!@rl_done)
        send(@rl_redisplay_function)
      end
      r = 0
    end
    return r
  end
  1
end

._rl_arg_getcharObject


7795
7796
7797
7798
7799
7800
7801
# File 'lib/rbreadline.rb', line 7795

def _rl_arg_getchar()
  rl_message("(arg: #{@rl_arg_sign * @rl_numeric_arg}) ")
  rl_setstate(RL_STATE_MOREINPUT)
  c = rl_read_key()
  rl_unsetstate(RL_STATE_MOREINPUT)
  c
end

._rl_arg_initObject


7789
7790
7791
7792
7793
# File 'lib/rbreadline.rb', line 7789

def _rl_arg_init()
  rl_save_prompt()
  @_rl_argcxt = 0
  rl_setstate(RL_STATE_NUMERICARG)
end

._rl_arg_overflowObject


7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
# File 'lib/rbreadline.rb', line 7866

def _rl_arg_overflow()
  if (@rl_numeric_arg > 1000000)
    @_rl_argcxt = 0
    @rl_explicit_arg = @rl_numeric_arg = 0
    rl_ding()
    rl_restore_prompt()
    rl_clear_message()
    rl_unsetstate(RL_STATE_NUMERICARG)
    return 1
  end
  0
end

._rl_backspace(count) ⇒ Object

Move the cursor back.


4681
4682
4683
4684
4685
4686
4687
4688
# File 'lib/rbreadline.rb', line 4681

def _rl_backspace(count)
  if (@_rl_term_backspace)
    @_rl_out_stream.write(@_rl_term_backspace * count)
  else
    @_rl_out_stream.write("\b"*count)
  end
  0
end

._rl_bind_tty_special_chars(kmap) ⇒ Object


6992
6993
6994
6995
6996
6997
# File 'lib/rbreadline.rb', line 6992

def _rl_bind_tty_special_chars(kmap)
  kmap[@_rl_tty_chars.t_erase] = :rl_rubout
  kmap[@_rl_tty_chars.t_kill] = :rl_unix_line_discard
  kmap[@_rl_tty_chars.t_werase] = :rl_unix_word_rubout
  kmap[@_rl_tty_chars.t_lnext] = :rl_quoted_insert
end

._rl_char_search(count, fdir, bdir) ⇒ Object


7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
# File 'lib/rbreadline.rb', line 7636

def _rl_char_search(count, fdir, bdir)
  mbchar = ''
  mb_len = _rl_read_mbchar(mbchar, MB_LEN_MAX)

  if (mbchar.is_a?(Integer) && c < 0) || mbchar == 0.chr
    return -1
  end

  if (count < 0)
    return (_rl_char_search_internal(-count, bdir, mbchar, mb_len))
  else
    return (_rl_char_search_internal(count, fdir, mbchar, mb_len))
  end
end

._rl_char_search_internal(count, dir, smbchar, len) ⇒ Object


7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
# File 'lib/rbreadline.rb', line 7609

def _rl_char_search_internal(count, dir, smbchar, len)
  pos = @rl_point
  inc = (dir < 0) ? -1 : 1
  while (count!=0)
    if ((dir < 0 && pos <= 0) || (dir > 0 && pos >= @rl_end))
      rl_ding()
      return -1
    end
    pos = (inc > 0) ? _rl_find_next_mbchar(@rl_line_buffer, pos, 1, MB_FIND_ANY) :
      _rl_find_prev_mbchar(@rl_line_buffer, pos, MB_FIND_ANY)
    begin
      if (_rl_is_mbchar_matched(@rl_line_buffer, pos, @rl_end, smbchar, len)!=0)
        count-=1
        if (dir < 0)
          @rl_point = (dir == BTO) ? pos+1 : pos
        else
          @rl_point = (dir == FTO) ? pos-1 : pos
        end
        break
      end
      prepos = pos
    end while ((dir < 0) ? (pos = _rl_find_prev_mbchar(@rl_line_buffer, pos, MB_FIND_ANY)) != prepos :
               (pos = _rl_find_next_mbchar(@rl_line_buffer, pos, 1, MB_FIND_ANY)) != prepos)
  end
  0
end

._rl_char_value(buf, ind) ⇒ Object


5203
5204
5205
# File 'lib/rbreadline.rb', line 5203

def _rl_char_value(buf,ind)
  buf[ind,1]
end

._rl_clean_up_for_exitObject


1518
1519
1520
1521
1522
1523
1524
1525
# File 'lib/rbreadline.rb', line 1518

def _rl_clean_up_for_exit()
  if @readline_echoing_p
    _rl_move_vert(@_rl_vis_botlin)
    @_rl_vis_botlin = 0
    @rl_outstream.flush
    rl_restart_output(1, 0)
  end
end

._rl_clear_screenObject


4672
4673
4674
4675
4676
4677
4678
# File 'lib/rbreadline.rb', line 4672

def _rl_clear_screen()
  if (@_rl_term_clrpag)
    @rl_outstream.write(@_rl_term_clrpag)
  else
    rl_crlf()
  end
end

._rl_clear_to_eol(count) ⇒ Object

Clear to the end of the line. COUNT is the minimum

number of character spaces to clear,

4649
4650
4651
4652
4653
4654
4655
# File 'lib/rbreadline.rb', line 4649

def _rl_clear_to_eol(count)
  if (@_rl_term_clreol)
    @rl_outstream.write(@_rl_term_clreol)
  elsif (count!=0)
    space_to_eol(count)
  end
end

._rl_col_width(string, start, _end) ⇒ Object


3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
# File 'lib/rbreadline.rb', line 3887

def _rl_col_width(string,start,_end)
  return 0 if _end <= start

  # Find the first occurance of 0.chr, which marks the end of the string.
  # Because newlines are also in the string as 0.chrs (they are tracked
  # seperately), we need to ignore any 0.chrs that lie before _end.
  index = string[_end...string.length].index(0.chr)

  str = index ? string[0,index+_end] : string
  width = 0

  case @encoding
  when 'N'
    return (_end - start)
  when 'U'
    str[start ... _end].scan(/./mu).each {|s| width += s.unpack('U').first >= 0x1000 ? 2 : 1 }
  when 'S'
    str[start ... _end].scan(/./ms).each {|s| width += s.length }
  when 'E'
    str[start ... _end].scan(/./me).each {|s| width += s.length }
  when 'X'
    str[start ... _end].force_encoding(@encoding_name).codepoints.each {|s| width += s > 0x1000 ? 2 : 1 }
  end
  width
end

._rl_compare_chars(buf1, pos1, buf2, pos2) ⇒ Object

compare the specified two characters. If the characters matched,

return true. Otherwise return false.

8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
# File 'lib/rbreadline.rb', line 8736

def _rl_compare_chars(buf1, pos1, buf2, pos2)
  return false if buf1[pos1].nil? || buf2[pos2].nil?
  case @encoding
  when 'E'
    buf1[pos1..-1].scan(/./me)[0] == buf2[pos2..-1].scan(/./me)[0]
  when 'S'
    buf1[pos1..-1].scan(/./ms)[0] == buf2[pos2..-1].scan(/./ms)[0]
  when 'U'
    buf1[pos1..-1].scan(/./mu)[0] == buf2[pos2..-1].scan(/./mu)[0]
  when 'X'
    buf1[pos1..-1].force_encoding(@encoding_name)[0] == buf2[pos2..-1].force_encoding(@encoding_name)[0]
  else
    buf1[pos1] == buf2[pos2]
  end
end

._rl_control_keypad(on) ⇒ Object


7029
7030
7031
7032
7033
7034
7035
# File 'lib/rbreadline.rb', line 7029

def _rl_control_keypad(on)
  if on && @_rl_term_ks
    @_rl_out_stream.write(@_rl_term_ks)
  elsif !on && @_rl_term_ke
    @_rl_out_stream.write(@_rl_term_ke)
  end
end

._rl_copy_to_kill_ring(text, append) ⇒ Object

Add TEXT to the kill ring, allocating a new kill ring slot as necessary.

This uses TEXT directly, so the caller must not free it.  If APPEND is
non-zero, and the last command was a kill, the text is appended to the
current kill ring slot, otherwise prepended.

5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
# File 'lib/rbreadline.rb', line 5934

def _rl_copy_to_kill_ring(text, append)
  # First, find the slot to work with.
  if (!@_rl_last_command_was_kill)
    # Get a new slot.
    if @rl_kill_ring.nil?
      # If we don't have any defined, then make one.
      @rl_kill_ring_length = 1
      @rl_kill_ring = Array.new(@rl_kill_ring_length+1)
      @rl_kill_ring[slot = 0] = nil
    else
      # We have to add a new slot on the end, unless we have
      #   exceeded the max limit for remembering kills.
      slot = @rl_kill_ring_length
      if (slot == @rl_max_kills)
        @rl_kill_ring[0,slot] = @rl_kill_ring[1,slot]
      else
        slot = @rl_kill_ring_length += 1
      end
      @rl_kill_ring[slot-=1] = nil
    end
  else
    slot = @rl_kill_ring_length - 1
  end

  # If the last command was a kill, prepend or append.
  if (@_rl_last_command_was_kill && @rl_editing_mode != @vi_mode)
    old = @rl_kill_ring[slot]

    if (append)
      new = old + text
    else
      new = text + old
    end
    old = nil
    text = nil
    @rl_kill_ring[slot] = new
  else
    @rl_kill_ring[slot] = text
  end

  @rl_kill_index = slot
  0
end

._rl_current_display_lineObject

return the `current display line' of the cursor – the number of lines to

move up to get to the first screen line of the current readline line.

5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
# File 'lib/rbreadline.rb', line 5317

def _rl_current_display_line()
  # Find out whether or not there might be invisible characters in the
  #   editing buffer.
  if (@rl_display_prompt == @rl_prompt)
    nleft = @_rl_last_c_pos - @_rl_screenwidth - @rl_visible_prompt_length
  else
    nleft = @_rl_last_c_pos - @_rl_screenwidth
  end

  if (nleft > 0)
    ret = 1 + nleft / @_rl_screenwidth
  else
    ret = 0
  end

  ret
end

._rl_dispatch(key, map) ⇒ Object

Do the command associated with KEY in MAP.

If the associated command is really a keymap, then read
another key, and dispatch into that map.

4361
4362
4363
4364
# File 'lib/rbreadline.rb', line 4361

def _rl_dispatch(key, map)
  @_rl_dispatching_keymap = map
  return _rl_dispatch_subseq(key, map, false)
end

._rl_dispatch_subseq(key, map, got_subseq) ⇒ Object


4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
# File 'lib/rbreadline.rb', line 4367

def _rl_dispatch_subseq(key, map, got_subseq)
  func = map[key]
  if (func)
    @rl_executing_keymap = map

    @rl_dispatching = true
    rl_setstate(RL_STATE_DISPATCHING)
    send(map[key],@rl_numeric_arg * @rl_arg_sign, key)
    rl_unsetstate(RL_STATE_DISPATCHING)
    @rl_dispatching = false
    if (@rl_pending_input == 0 && map[key] != :rl_digit_argument)
      @rl_last_func = map[key]
    end
  else
    if(map.keys.detect{|x| x =~ /^#{Regexp.escape(key)}/})
      key += _rl_subseq_getchar(key)
      return _rl_dispatch_subseq(key,map,got_subseq)
    elsif(key.length>1 && key[1].ord < 0x7f)
      _rl_abort_internal()
      return -1
    else
      @rl_dispatching = true
      rl_setstate(RL_STATE_DISPATCHING)
      send(:rl_insert,@rl_numeric_arg * @rl_arg_sign, key)
      rl_unsetstate(RL_STATE_DISPATCHING)
      @rl_dispatching = false
    end
  end
  0
end

._rl_enable_meta_keyObject


2478
2479
2480
2481
2482
# File 'lib/rbreadline.rb', line 2478

def _rl_enable_meta_key()
  if(@term_has_meta && @_rl_term_mm)
    @_rl_out_stream.write(@_rl_term_mm)
  end
end

._rl_erase_at_end_of_line(l) ⇒ Object

Quick redisplay hack when erasing characters at the end of the line.


5862
5863
5864
5865
5866
5867
5868
5869
# File 'lib/rbreadline.rb', line 5862

def _rl_erase_at_end_of_line(l)
  _rl_backspace(l)
  @rl_outstream.write(' '*l)
  _rl_backspace(l)
  @_rl_last_c_pos -= l
  @visible_line[@_rl_last_c_pos,l] = 0.chr * l
  @rl_display_fixed = true if !@rl_display_fixed
end

._rl_erase_entire_lineObject


4707
4708
4709
4710
4711
4712
# File 'lib/rbreadline.rb', line 4707

def _rl_erase_entire_line()
  cr()
  _rl_clear_to_eol(0)
  cr()
  @rl_outstream.flush
end

._rl_find_completion_wordObject


6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
# File 'lib/rbreadline.rb', line 6190

def _rl_find_completion_word()
  _end = @rl_point
  found_quote = 0
  delimiter = 0.chr
  quote_char = 0.chr

  brkchars = nil
  if @rl_completion_word_break_hook
    brkchars = send(@rl_completion_word_break_hook)
  end
  if brkchars.nil?
    brkchars = @rl_completer_word_break_characters
  end
  if (@rl_completer_quote_characters)
    # We have a list of characters which can be used in pairs to
    # quote substrings for the completer.  Try to find the start
    # of an unclosed quoted substring.
    # FOUND_QUOTE is set so we know what kind of quotes we found.
    scan = 0
    pass_next = false
    while scan < _end
      if !@rl_byte_oriented
        scan = _rl_find_next_mbchar(@rl_line_buffer, scan, 1, MB_FIND_ANY)
      else
        scan += 1
      end

      if (pass_next)
        pass_next = false
        next
      end

      # Shell-like semantics for single quotes -- don't allow backslash
      #   to quote anything in single quotes, especially not the closing
      #   quote.  If you don't like this, take out the check on the value
      #   of quote_char.
      if (quote_char != "'" && @rl_line_buffer[scan,1] == "\\")
        pass_next = true
        found_quote |= RL_QF_BACKSLASH
        next
      end

      if (quote_char != 0.chr)
        # Ignore everything until the matching close quote char.
        if (@rl_line_buffer[scan,1] == quote_char)
          # Found matching close.  Abandon this substring.
          quote_char = 0.chr
          @rl_point = _end
        end

      elsif (@rl_completer_quote_characters.include?(@rl_line_buffer[scan,1]))

        # Found start of a quoted substring.
        quote_char = @rl_line_buffer[scan,1]
        @rl_point = scan + 1
        # Shell-like quoting conventions.
        if (quote_char == "'")
          found_quote |= RL_QF_SINGLE_QUOTE
        elsif (quote_char == '"')
          found_quote |= RL_QF_DOUBLE_QUOTE
        else
          found_quote |= RL_QF_OTHER_QUOTE
        end
      end
    end
  end

  if (@rl_point == _end && quote_char == 0.chr)

    # We didn't find an unclosed quoted substring upon which to do
    #   completion, so use the word break characters to find the
    #   substring on which to complete.


    while (@rl_point =  !@rl_byte_oriented ?
           _rl_find_prev_mbchar(@rl_line_buffer, @rl_point, MB_FIND_ANY):(@rl_point-1))>0

      scan = @rl_line_buffer[@rl_point,1]
      if !brkchars.include?(scan)
        next
      end
      # Call the application-specific function to tell us whether
      #   this word break character is quoted and should be skipped.
      if (@rl_char_is_quoted_p && found_quote!=0 &&
          send(@rl_char_is_quoted_p,@rl_line_buffer, @rl_point))
        next
      end

      # Convoluted code, but it avoids an n^2 algorithm with calls
      #   to char_is_quoted.
      break
    end
  end

  # If we are at an unquoted word break, then advance past it.
  scan = @rl_line_buffer[@rl_point,1]

  # If there is an application-specific function to say whether or not
  #   a character is quoted and we found a quote character, let that
  #   function decide whether or not a character is a word break, even
  #   if it is found in rl_completer_word_break_characters.  Don't bother
  #   if we're at the end of the line, though.
  if (scan != 0.chr)
    if (@rl_char_is_quoted_p)
      isbrk = (found_quote == 0 ||
               !send(@rl_char_is_quoted_p,@rl_line_buffer, @rl_point)) &&
               brkchars.include?(scan)
    else
      isbrk = brkchars.include?(scan)
    end

    if (isbrk)
      # If the character that caused the word break was a quoting
      #   character, then remember it as the delimiter.
      if (@rl_basic_quote_characters &&
          @rl_basic_quote_characters.include?(scan) &&
          (_end - @rl_point) > 1)
        delimiter = scan
      end

      # If the character isn't needed to determine something special
      #   about what kind of completion to perform, then advance past it.
      if (@rl_special_prefixes.nil? || !@rl_special_prefixes.include?(scan) )
        @rl_point+=1
      end
    end
  end

  return [quote_char,found_quote!=0,delimiter]
end

._rl_find_next_mbchar(string, seed, count, flags) ⇒ Object

Find next `count' characters started byte point of the specified seed.

If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
characters.

8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
# File 'lib/rbreadline.rb', line 8677

def _rl_find_next_mbchar(string, seed, count, flags)
  if @encoding == 'N'
    return (seed + count)
  end
  seed = 0 if seed < 0
  return seed if count <= 0

  point = seed + _rl_adjust_point(string,seed)
  if (seed < point)
    count -= 1
  end

  str = (flags == MB_FIND_NONZERO) ? string.sub(/\x00+$/,'') : string

  case @encoding
  when 'E'
    point += str[point..-1].scan(/./me)[0,count].to_s.length
  when 'S'
    point += str[point..-1].scan(/./ms)[0,count].to_s.length
  when 'U'
    point += str[point..-1].scan(/./mu)[0,count].to_s.length
  when 'X'
    point += str[point..-1].force_encoding(@encoding_name)[0,count].bytesize
  else
    point += count
    point = str.length if point >= str.length
  end
  point
end

._rl_find_prev_mbchar(string, seed, flags) ⇒ Object

Find previous character started byte point of the specified seed.

Returned point will be point <= seed.  If flags is MB_FIND_NONZERO,
we look for non-zero-width multibyte characters.

8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
# File 'lib/rbreadline.rb', line 8710

def _rl_find_prev_mbchar(string, seed, flags)
  if @encoding == 'N'
    return ((seed == 0) ? seed : seed - 1)
  end

  length = string.length
  if seed < 0
    return 0
  elsif length < seed
    return length
  end

  case @encoding
  when 'E'
    string[0,seed].scan(/./me)[0..-2].to_s.length
  when 'S'
    string[0,seed].scan(/./ms)[0..-2].to_s.length
  when 'U'
    string[0,seed].scan(/./mu)[0..-2].to_s.length
  when 'X'
    string[0,seed].force_encoding(@encoding_name)[0..-2].bytesize
  end
end

._rl_fix_point(fix_mark_too) ⇒ Object


5001
5002
5003
5004
5005
5006
# File 'lib/rbreadline.rb', line 5001

def _rl_fix_point(fix_mark_too)
  @rl_point = __rl_fix_point(@rl_point)
  if (fix_mark_too)
    @rl_mark = __rl_fix_point(@rl_mark)
  end
end

._rl_get_char_len(src) ⇒ Object

return the number of bytes parsed from the multibyte sequence starting at src, if a non-L'0' wide character was recognized. It returns 0, if a L'0' wide character was recognized. It returns (size_t)(-1), if an invalid multibyte sequence was encountered. It returns (size_t)(-2) if it couldn't parse a complete multibyte character.


8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
# File 'lib/rbreadline.rb', line 8757

def _rl_get_char_len(src)
  return 0 if src[0,1] == 0.chr || src.length==0
  case @encoding
  when 'E'
    len = src.scan(/./me)[0].to_s.length
  when 'S'
    len = src.scan(/./ms)[0].to_s.length
  when 'U'
    len = src.scan(/./mu)[0].to_s.length
  when 'X'
    src = src.dup.force_encoding(@encoding_name)
    len = src.valid_encoding? ? src[0].bytesize : 0
  else
    len = 1
  end
  len==0 ? -2 : len
end

._rl_get_screen_size(tty, ignore_env) ⇒ Object

Get readline's idea of the screen size. TTY is a file descriptor open

to the terminal.  If IGNORE_ENV is true, we do not pay attention to the
values of $LINES and $COLUMNS.  The tests for TERM_STRING_BUFFER being
non-null serve to check whether or not we have initialized termcap.

1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
# File 'lib/rbreadline.rb', line 1859

def _rl_get_screen_size(tty, ignore_env)

  if @hConsoleHandle
    csbi = Fiddle::Pointer.malloc(24)
    @GetConsoleScreenBufferInfo.Call(@hConsoleHandle,csbi)
    wc,wr = csbi[0,4].unpack('SS')
    # wr,wc, = `mode con`.scan(/\d+\n/).map{|x| x.to_i}
    @_rl_screenwidth = wc
    @_rl_screenheight = wr
  else
    wr, wc = 0
    retry_if_interrupted do
      wr, wc = `stty size`.split(' ').map { |x| x.to_i }
    end
    @_rl_screenwidth = wc
    @_rl_screenheight = wr
    if ignore_env==0 && ENV['LINES']
      @_rl_screenheight = ENV['LINES'].to_i
    end
    if ignore_env==0 && ENV['COLUMNS']
      @_rl_screenwidth = ENV['COLUMNS'].to_i
    end
  end

  # If all else fails, default to 80x24 terminal.
  if @_rl_screenwidth.nil? || @_rl_screenwidth <= 1
    @_rl_screenwidth = 80
  end
  if @_rl_screenheight.nil? || @_rl_screenheight <= 0
    @_rl_screenheight = 24
  end
  # If we're being compiled as part of bash, set the environment
  #   variables $LINES and $COLUMNS to new values.  Otherwise, just
  #   do a pair of putenv () or setenv () calls.
  sh_set_lines_and_columns(@_rl_screenheight, @_rl_screenwidth)

  if !@_rl_term_autowrap
    @_rl_screenwidth-=1
  end
  @_rl_screenchars = @_rl_screenwidth * @_rl_screenheight
end

._rl_history_set_pointObject


5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
# File 'lib/rbreadline.rb', line 5495

def _rl_history_set_point ()
  @rl_point = (@_rl_history_preserve_point && @_rl_history_saved_point != -1) ?
    @_rl_history_saved_point : @rl_end
  if (@rl_point > @rl_end)
    @rl_point = @rl_end
  end
  if (@rl_editing_mode == @vi_mode && @_rl_keymap != @vi_insertion_keymap)
    @rl_point = 0
  end
  if (@rl_editing_mode == @emacs_mode)
    @rl_mark = (@rl_point == @rl_end ? 0 : @rl_end)
  end
end

._rl_init_eightbitObject


2079
2080
2081
# File 'lib/rbreadline.rb', line 2079

def _rl_init_eightbit()

end

._rl_init_line_stateObject


2598
2599
2600
2601
# File 'lib/rbreadline.rb', line 2598

def _rl_init_line_state()
  @rl_point = @rl_end = @rl_mark = 0
  @rl_line_buffer = ""
end

._rl_init_terminal_io(terminal_name) ⇒ Object


1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
# File 'lib/rbreadline.rb', line 1971

def _rl_init_terminal_io(terminal_name)
  term = terminal_name ? terminal_name : ENV["TERM"]
  @_rl_term_clrpag = @_rl_term_cr = @_rl_term_clreol = nil
  tty = @rl_instream ? @rl_instream.fileno : 0

  if no_terminal?
    term = "dumb"
    @_rl_bind_stty_chars = false
  end

  @term_string_buffer ||= 0.chr * 2032

  @term_buffer ||= 0.chr * 4080

  buffer = @term_string_buffer

  tgetent_ret = (term != "dumb") ? 1 : -1

  if (tgetent_ret <= 0)
    buffer = @term_buffer = @term_string_buffer = nil

    @_rl_term_autowrap = false    # used by _rl_get_screen_size

    # Allow calling application to set default height and width, using
    #rl_set_screen_size
    if (@_rl_screenwidth <= 0 || @_rl_screenheight <= 0)
      _rl_get_screen_size(tty, 0)
    end

    # Defaults.
    if (@_rl_screenwidth <= 0 || @_rl_screenheight <= 0)
      @_rl_screenwidth = 79
      @_rl_screenheight = 24
    end

    # Everything below here is used by the redisplay code (tputs).
    @_rl_screenchars = @_rl_screenwidth * @_rl_screenheight
    @_rl_term_cr = "\r"
    @_rl_term_im = @_rl_term_ei = @_rl_term_ic = @_rl_term_IC = nil
    @_rl_term_up = @_rl_term_dc = @_rl_term_DC = @_rl_visible_bell = nil
    @_rl_term_ku = @_rl_term_kd = @_rl_term_kl = @_rl_term_kr = nil
    @_rl_term_kh = @_rl_term_kH = @_rl_term_kI = @_rl_term_kD = nil
    @_rl_term_ks = @_rl_term_ke = @_rl_term_at7 = nil
    @_rl_term_mm = @_rl_term_mo = nil
    @_rl_term_ve = @_rl_term_vs = nil
    @_rl_term_forward_char = nil
    @_rl_terminal_can_insert = @term_has_meta = false

    # Reasonable defaults for tgoto().  Readline currently only uses
    #   tgoto if _rl_term_IC or _rl_term_DC is defined, but just in case we
    #   change that later...
    @_rl_term_backspace = "\b"

    return 0
  end

  get_term_capabilities(buffer)

  @_rl_term_cr ||= "\r"
  @_rl_term_autowrap = !!(tgetflag("am") && tgetflag("xn"))

  # Allow calling application to set default height and width, using
  #   rl_set_screen_size
  if (@_rl_screenwidth <= 0 || @_rl_screenheight <= 0)
    _rl_get_screen_size(tty, 0)
  end

  # Check to see if this terminal has a meta key and clear the capability
  #   variables if there is none.
  @term_has_meta = !!(tgetflag("km") || tgetflag("MT"))
  if !@term_has_meta
    @_rl_term_mm = @_rl_term_mo = nil
  end

  # Attempt to find and bind the arrow keys.  Do not override already
  #   bound keys in an overzealous attempt, however.

  bind_termcap_arrow_keys(@emacs_standard_keymap)

  bind_termcap_arrow_keys(@vi_movement_keymap)
  bind_termcap_arrow_keys(@vi_insertion_keymap)

  return 0
end

._rl_input_availableObject


4060
4061
4062
# File 'lib/rbreadline.rb', line 4060

def _rl_input_available
  IO.select([ $stdin ], nil, [ $stdin ], @_keyboard_input_timeout)
end

._rl_insert_char(count, c) ⇒ Object

Insert the character C at the current location, moving point forward.

If C introduces a multibyte sequence, we read the whole sequence and
then insert the multibyte char into the line buffer.

5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
# File 'lib/rbreadline.rb', line 5580

def _rl_insert_char(count, c)
  return 0 if (count <= 0)

  incoming = ''

  if @rl_byte_oriented
    incoming << c
    incoming_length = 1
  else
    @pending_bytes << c
    if _rl_get_char_len(@pending_bytes) == -2
      return 1
    else
      incoming = @pending_bytes
      @pending_bytes = ''
      incoming_length = incoming.length
    end
  end

  if(count>1)
    string = incoming * count
    rl_insert_text(string)
    string = nil
    return 0
  end

  if @rl_byte_oriented
    # We are inserting a single character.
    #If there is pending input, then make a string of all of the
    #pending characters that are bound to rl_insert, and insert
    #them all.
    if (_rl_any_typein())
      _rl_insert_typein(c)
    else
      rl_insert_text(c)
    end
  else
    rl_insert_text(incoming)
  end

  return 0
end

._rl_insert_next(count) ⇒ Object

Insert the next typed character verbatim.


5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
# File 'lib/rbreadline.rb', line 5658

def _rl_insert_next(count)
  rl_setstate(RL_STATE_MOREINPUT)
  c = rl_read_key()
  rl_unsetstate(RL_STATE_MOREINPUT)

  if c.is_a?(Integer) && c < 0
    return -1
  end

  _rl_insert_char(count, c)
end

._rl_insert_typein(c) ⇒ Object


5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
# File 'lib/rbreadline.rb', line 5565

def _rl_insert_typein(c)
  string = c

  while ((key = rl_get_char()) && @_rl_keymap[key] == :rl_insert)
    string << key
  end
  if (key)
    _rl_unget_char(key)
  end
  rl_insert_text(string)
end

._rl_internal_char_cleanupObject


4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
# File 'lib/rbreadline.rb', line 4714

def _rl_internal_char_cleanup()
  # In vi mode, when you exit insert mode, the cursor moves back
  #   over the previous character.  We explicitly check for that here.
  if (@rl_editing_mode == @vi_mode && @_rl_keymap == @vi_movement_keymap)
    rl_vi_check()
  end

  if (@rl_num_chars_to_read!=0 && @rl_end >= @rl_num_chars_to_read)
    send(@rl_redisplay_function)
    @_rl_want_redisplay = false
    rl_newline(1, "\n")
  end

  if (!@rl_done)
    send(@rl_redisplay_function)
    @_rl_want_redisplay = false
  end

  # If the application writer has told us to erase the entire line if
  #   the only character typed was something bound to rl_newline, do so.
  if (@rl_erase_empty_line && @rl_done && @rl_last_func == :rl_newline &&
      @rl_point == 0 && @rl_end == 0)
    _rl_erase_entire_line()
  end
end

._rl_internal_pager(lines) ⇒ Object


6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
# File 'lib/rbreadline.rb', line 6541

def _rl_internal_pager(lines)
  @rl_outstream.write("--More--")
  @rl_outstream.flush
  i = get_y_or_n(1)
  _rl_erase_entire_line()
  if (i == 0)
    return -1
  elsif (i == 2)
    return (lines - 1)
  else
    return 0
  end
end

._rl_is_mbchar_matched(string, seed, _end, mbchar, length) ⇒ Object


8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
# File 'lib/rbreadline.rb', line 8822

def _rl_is_mbchar_matched(string, seed, _end, mbchar, length)
  return 0 if ((_end - seed) < length)

  for i in 0 ... length
    if (string[seed + i] != mbchar[i])
      return 0
    end
  end
  1
end

._rl_isearch_cleanup(cxt, r) ⇒ Object


4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
# File 'lib/rbreadline.rb', line 4347

def _rl_isearch_cleanup(cxt, r)
  if (r >= 0)
    _rl_isearch_fini(cxt)
  end
  @_rl_iscxt = nil

  rl_unsetstate(RL_STATE_ISEARCH)

  r != 0
end

._rl_isearch_dispatch(cxt, c) ⇒ Object

Process just-read character C according to isearch context CXT. Return

-1 if the caller should just free the context and return, 0 if we should
break out of the loop, and 1 if we should continue to read characters.

4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
# File 'lib/rbreadline.rb', line 4067

def _rl_isearch_dispatch(cxt, c)
  f = nil

  if c.is_a?(Integer) && c < 0
    cxt.sflags |= SF_FAILED
    cxt.history_pos = cxt.last_found_line
    return -1
  end

  # Translate the keys we do something with to opcodes.
  if (c && @_rl_keymap[c])
    f = @_rl_keymap[c]
    if (f == :rl_reverse_search_history)
      cxt.lastc = (cxt.sflags & SF_REVERSE)!=0 ? -1 : -2
    elsif (f == :rl_forward_search_history)
      cxt.lastc = (cxt.sflags & SF_REVERSE)!=0 ? -2 : -1
    elsif (f == :rl_rubout)
      cxt.lastc = -3
    elsif (c == "\C-G")
      cxt.lastc = -4
    elsif (c == "\C-W")  # XXX
      cxt.lastc = -5
    elsif (c == "\C-Y")  # XXX
      cxt.lastc = -6
    end
  end

  # The characters in isearch_terminators (set from the user-settable
  #   variable isearch-terminators) are used to terminate the search but
  #   not subsequently execute the character as a command.  The default
  #   value is "\033\012" (ESC and C-J).
  if (cxt.lastc.class == String && cxt.search_terminators.include?(cxt.lastc))
    # ESC still terminates the search, but if there is pending
    #input or if input arrives within 0.1 seconds (on systems
    #with select(2)) it is used as a prefix character
    #with rl_execute_next.  WATCH OUT FOR THIS!  This is intended
    #to allow the arrow keys to be used like ^F and ^B are used
    #to terminate the search and execute the movement command.
    #XXX - since _rl_input_available depends on the application-
    #settable keyboard timeout value, this could alternatively
    #use _rl_input_queued(100000)
    if (cxt.lastc == ESC && _rl_input_available())
      rl_execute_next(ESC)
    end
    return (0)
  end

  if !@rl_byte_oriented
    if (cxt.lastc.class == String && (cxt.mb.length == 1) && endsrch_char(cxt.lastc))
      # This sets rl_pending_input to c; it will be picked up the next
      #   time rl_read_key is called.
      rl_execute_next(cxt.lastc)
      return (0)
    end
  elsif (cxt.lastc.class == String && endsrch_char(cxt.lastc))
    # This sets rl_pending_input to LASTC; it will be picked up the next
    #   time rl_read_key is called.
    rl_execute_next(cxt.lastc)
    return (0)
  end

  # Now dispatch on the character.  `Opcodes' affect the search string or
  #   state.  Other characters are added to the string.
  case (cxt.lastc)

    # search again
  when -1
    if (cxt.search_string_index == 0)
      if (@last_isearch_string)
        cxt.search_string_size = 64 + @last_isearch_string_len
        cxt.search_string = @last_isearch_string.dup
        cxt.search_string_index = @last_isearch_string_len
        rl_display_search(cxt.search_string, (cxt.sflags & SF_REVERSE)!=0, -1)
      else
        return (1)
      end
    elsif (cxt.sflags & SF_REVERSE)!=0
      cxt.sline_index-=1
    elsif (cxt.sline_index != cxt.sline_len)
      cxt.sline_index+=1
    else
      rl_ding()
    end

    # switch directions
  when -2
    cxt.direction = -cxt.direction
    if (cxt.direction < 0)
      cxt.sflags |= SF_REVERSE
    else
      cxt.sflags &= ~SF_REVERSE
    end
    # delete character from search string.
  when -3  # C-H, DEL
    # This is tricky.  To do this right, we need to keep a
    # stack of search positions for the current search, with
    # sentinels marking the beginning and end.  But this will
    # do until we have a real isearch-undo.
    if (cxt.search_string_index == 0)
      rl_ding()
    else
      cxt.search_string_index -= 1
      cxt.search_string.chop!
    end
  when -4  # C-G, abort
    rl_replace_line(cxt.lines[cxt.save_line], false)
    @rl_point = cxt.save_point
    @rl_mark = cxt.save_mark
    rl_restore_prompt()
    rl_clear_message()
    return -1
  when -5  # C-W
    # skip over portion of line we already matched and yank word
    wstart = @rl_point + cxt.search_string_index
    if (wstart >= @rl_end)
      rl_ding()
    else
      # if not in a word, move to one.
      cval = _rl_char_value(@rl_line_buffer, wstart)
      if (!_rl_walphabetic(cval))
        rl_ding()
      else
        if !@rl_byte_oriented
          n = _rl_find_next_mbchar(@rl_line_buffer, wstart, 1, MB_FIND_NONZERO)
        else
          n = wstart+1
        end
        while (n < @rl_end)
          cval = _rl_char_value(@rl_line_buffer, n)
          break if !_rl_walphabetic(cval)
          if !@rl_byte_oriented
            n = _rl_find_next_mbchar(@rl_line_buffer, n, 1, MB_FIND_NONZERO)
          else
            n = n+1
          end
        end
        wlen = n - wstart + 1
        if (cxt.search_string_index + wlen + 1 >= cxt.search_string_size)
          cxt.search_string_size += wlen + 1
        end
        cxt.search_string[cxt.search_string_index..-1] = @rl_line_buffer[wstart,wlen]
        cxt.search_string_index += wlen
      end
    end

  when -6  # C-Y
    # skip over portion of line we already matched and yank rest
    wstart = @rl_point + cxt.search_string_index
    if (wstart >= @rl_end)
      rl_ding()
    else
      n = @rl_end - wstart + 1
      if (cxt.search_string_index + n + 1 >= cxt.search_string_size)
        cxt.search_string_size += n + 1
      end
      cxt.search_string[cxt.search_string_index..-1] = @rl_line_buffer[wstart,n]
    end

    # Add character to search string and continue search.
  else
    if (cxt.search_string_index + 2 >= cxt.search_string_size)
      cxt.search_string_size += 128
    end
    if !@rl_byte_oriented
      for j in 0 ... cxt.mb.length
        cxt.search_string << cxt.mb[j,1]
        cxt.search_string_index += 1
      end
    else
      cxt.search_string << c
      cxt.search_string_index += 1
    end
  end

  while (cxt.sflags &= ~(SF_FOUND|SF_FAILED))!=0
    limit = cxt.sline_len - cxt.search_string_index + 1
    # Search the current line.
    while ((cxt.sflags & SF_REVERSE)!=0 ? (cxt.sline_index >= 0) : (cxt.sline_index < limit))

      if (cxt.search_string[0,cxt.search_string_index] == cxt.sline[cxt.sline_index,cxt.search_string_index])
        cxt.sflags |= SF_FOUND
        break
      else
        cxt.sline_index += cxt.direction
      end
    end
    break if (cxt.sflags & SF_FOUND)!=0

    # Move to the next line, but skip new copies of the line
    # we just found and lines shorter than the string we're
    # searching for.
    begin
      # Move to the next line.
      cxt.history_pos += cxt.direction

      # At limit for direction?
      if ((cxt.sflags & SF_REVERSE)!=0 ? (cxt.history_pos < 0) : (cxt.history_pos == cxt.hlen))
        cxt.sflags |= SF_FAILED
        break
      end

      # We will need these later.
      cxt.sline = cxt.lines[cxt.history_pos]
      cxt.sline_len = cxt.sline.length
    end while ((cxt.prev_line_found && cxt.prev_line_found == cxt.lines[cxt.history_pos]) ||
               (cxt.search_string_index > cxt.sline_len))

    break if (cxt.sflags & SF_FAILED)!=0

    # Now set up the line for searching...
    cxt.sline_index = (cxt.sflags & SF_REVERSE)!=0 ? cxt.sline_len - cxt.search_string_index : 0
  end

  if (cxt.sflags & SF_FAILED)!=0
    # We cannot find the search string.  Ding the bell.
    rl_ding()
    cxt.history_pos = cxt.last_found_line
    return 1
  end

  # We have found the search string.  Just display it.  But don't
  #   actually move there in the history list until the user accepts
  #   the location.
  if (cxt.sflags & SF_FOUND)!=0
    cxt.prev_line_found = cxt.lines[cxt.history_pos]
    rl_replace_line(cxt.lines[cxt.history_pos], false)
    @rl_point = cxt.sline_index
    cxt.last_found_line = cxt.history_pos
    rl_display_search(cxt.search_string, (cxt.sflags & SF_REVERSE)!=0, (cxt.history_pos == cxt.save_line) ? -1 : cxt.history_pos)
  end
  1
end

._rl_isearch_fini(cxt) ⇒ Object


4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
# File 'lib/rbreadline.rb', line 4311

def _rl_isearch_fini(cxt)
  # First put back the original state.
  @rl_line_buffer = cxt.lines[cxt.save_line].dup
  rl_restore_prompt()

  # Save the search string for possible later use.
  @last_isearch_string = cxt.search_string
  @last_isearch_string_len = cxt.search_string_index
  cxt.search_string = nil

  if (cxt.last_found_line < cxt.save_line)
    rl_get_previous_history(cxt.save_line - cxt.last_found_line, 0)
  else
    rl_get_next_history(cxt.last_found_line - cxt.save_line, 0)
  end

  # If the string was not found, put point at the end of the last matching
  #   line.  If last_found_line == orig_line, we didn't find any matching
  #   history lines at all, so put point back in its original position.
  if (cxt.sline_index < 0)

    if (cxt.last_found_line == cxt.save_line)
      cxt.sline_index = cxt.save_point
    else
      cxt.sline_index = @rl_line_buffer.delete(0.chr).length
    end
    @rl_mark = cxt.save_mark
  end

  @rl_point = cxt.sline_index
  # Don't worry about where to put the mark here; rl_get_previous_history
  #   and rl_get_next_history take care of it.
  rl_clear_message()
end

._rl_isearch_init(direction) ⇒ Object


7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
# File 'lib/rbreadline.rb', line 7269

def _rl_isearch_init(direction)
  cxt = _rl_scxt_alloc(RL_SEARCH_ISEARCH, 0)
  if (direction < 0)
    cxt.sflags |= SF_REVERSE
  end

  cxt.search_terminators = @_rl_isearch_terminators ? @_rl_isearch_terminators :
    @default_isearch_terminators

  # Create an arrary of pointers to the lines that we want to search.
  hlist = history_list()
  rl_maybe_replace_line()
  i = 0
  if (hlist)
    i += 1 while(hlist[i])
  end

  # Allocate space for this many lines, +1 for the current input line,
  #   and remember those lines.
  cxt.hlen = i
  cxt.lines = []
  for i in 0 ... cxt.hlen
    cxt.lines[i] = hlist[i].line
  end

  if (@_rl_saved_line_for_history)
    cxt.lines[i] = @_rl_saved_line_for_history.line.dup
  else
    # Keep track of this so we can free it.
    cxt.allocated_line = @rl_line_buffer.dup
    cxt.lines << cxt.allocated_line
  end

  cxt.hlen+=1

  # The line where we start the search.
  cxt.history_pos = cxt.save_line

  rl_save_prompt()

  # Initialize search parameters.
  cxt.search_string_size = 128
  cxt.search_string_index = 0
  cxt.search_string = ""

  # Normalize DIRECTION into 1 or -1.
  cxt.direction = (direction >= 0) ? 1 : -1

  cxt.sline = @rl_line_buffer
  cxt.sline_len = cxt.sline.delete(0.chr).length
  cxt.sline_index = @rl_point

  @_rl_iscxt = cxt     # save globally
  cxt
end

._rl_make_prompt_for_search(pchar) ⇒ Object


8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
# File 'lib/rbreadline.rb', line 8169

def _rl_make_prompt_for_search(pchar)
  rl_save_prompt()

  # We've saved the prompt, and can do anything with the various prompt
  #   strings we need before they're restored.  We want the unexpanded
  #   portion of the prompt string after any final newline.
  _p = @rl_prompt ? @rl_prompt.rindex("\n") : nil
  if _p.nil?
    len = (@rl_prompt && @rl_prompt.length>0 ) ? @rl_prompt.length : 0
    if (len>0)
      pmt = @rl_prompt.dup
    else
      pmt = ''
    end
    pmt << pchar
  else
    _p+=1
    pmt = @rl_prompt[_p..-1]
    pmt << pchar
  end

  # will be overwritten by expand_prompt, called from rl_message
  @prompt_physical_chars = @saved_physical_chars + 1
  pmt
end

._rl_move_cursor_relative(new, data, start = 0) ⇒ Object

Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.

(Well, when we don't have multibyte characters, _rl_last_c_pos is a
buffer index.)
DATA is the contents of the screen line of interest; i.e., where
the movement is being done.

1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
# File 'lib/rbreadline.rb', line 1532

def _rl_move_cursor_relative(new, data, start=0)
  woff = w_offset(@_rl_last_v_pos, @wrap_offset)
  cpos = @_rl_last_c_pos

  if !@rl_byte_oriented
    dpos = _rl_col_width(data, start, start+new)

    # Use NEW when comparing against the last invisible character in the
    # prompt string, since they're both buffer indices and DPOS is a desired
    # display position.
    if (new > @prompt_last_invisible)     # XXX - don't use woff here
      dpos -= woff
      # Since this will be assigned to _rl_last_c_pos at the end (more
      #   precisely, _rl_last_c_pos == dpos when this function returns),
      #   let the caller know.
      @cpos_adjusted = true
    end
  else
    dpos = new
  end
  # If we don't have to do anything, then return.
  if (cpos == dpos)
    return
  end

  if @hConsoleHandle
    csbi = Fiddle::Pointer.malloc(24)
    @GetConsoleScreenBufferInfo.Call(@hConsoleHandle,csbi)
    x,y = csbi[4,4].unpack('SS')
    x = dpos
    @SetConsoleCursorPosition.Call(@hConsoleHandle,y*65536+x)
    @_rl_last_c_pos = dpos
    return
  end

  # It may be faster to output a CR, and then move forwards instead
  #   of moving backwards.
  # i == current physical cursor position.
  if !@rl_byte_oriented
    i = @_rl_last_c_pos
  else
    i = @_rl_last_c_pos - woff
  end

  if (dpos == 0 || cr_faster(dpos, @_rl_last_c_pos) ||
      (@_rl_term_autowrap && i == @_rl_screenwidth))
    @rl_outstream.write(@_rl_term_cr)
    cpos = @_rl_last_c_pos = 0
  end

  if (cpos < dpos)
    # Move the cursor forward.  We do it by printing the command
    # to move the cursor forward if there is one, else print that
    # portion of the output buffer again.  Which is cheaper?

    # The above comment is left here for posterity.  It is faster
    # to print one character (non-control) than to print a control
    # sequence telling the terminal to move forward one character.
    # That kind of control is for people who don't know what the
    # data is underneath the cursor.

    # However, we need a handle on where the current display position is
    # in the buffer for the immediately preceding comment to be true.
    # In multibyte locales, we don't currently have that info available.
    # Without it, we don't know where the data we have to display begins
    # in the buffer and we have to go back to the beginning of the screen
    # line.  In this case, we can use the terminal sequence to move forward
    # if it's available.
    if !@rl_byte_oriented
      if (@_rl_term_forward_char)
        @rl_outstream.write(@_rl_term_forward_char * (dpos-cpos))
      else
        @rl_outstream.write(@_rl_term_cr)
        @rl_outstream.write(data[start,new])
      end
    else
      @rl_outstream.write(data[start+cpos,new-cpos])
    end
  elsif (cpos > dpos)
    _rl_backspace(cpos - dpos)
  end
  @_rl_last_c_pos = dpos
end

._rl_move_vert(to) ⇒ Object

PWP: move the cursor up or down.


1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
# File 'lib/rbreadline.rb', line 1618

def _rl_move_vert(to)
  if (@_rl_last_v_pos == to || to > @_rl_screenheight)
    return
  end

  if ((delta = to - @_rl_last_v_pos) > 0)
    @rl_outstream.write("\n"*delta)
    @rl_outstream.write("\r")
    @_rl_last_c_pos = 0
  else
    if(@_rl_term_up)
      @rl_outstream.write(@_rl_term_up*(-delta))
    end
  end
  @_rl_last_v_pos = to        # Now TO is here
end

._rl_nsearch_abort(cxt) ⇒ Object


8228
8229
8230
8231
8232
8233
8234
8235
# File 'lib/rbreadline.rb', line 8228

def _rl_nsearch_abort(cxt)
  rl_maybe_unsave_line()
  rl_clear_message()
  @rl_point = cxt.save_point
  @rl_mark = cxt.save_mark
  rl_restore_prompt()
  rl_unsetstate(RL_STATE_NSEARCH)
end

._rl_nsearch_cleanup(cxt, r) ⇒ Object


8221
8222
8223
8224
8225
8226
# File 'lib/rbreadline.rb', line 8221

def _rl_nsearch_cleanup(cxt, r)
  cxt = nil
  @_rl_nscxt = nil
  rl_unsetstate(RL_STATE_NSEARCH)
  r != 1
end

._rl_nsearch_dispatch(cxt, c) ⇒ Object

Process just-read character C according to search context CXT. Return -1

if the caller should abort the search, 0 if we should break out of the
loop, and 1 if we should continue to read characters.

8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
# File 'lib/rbreadline.rb', line 8240

def _rl_nsearch_dispatch(cxt, c)
  case (c)
  when "\C-W"
    rl_unix_word_rubout(1, c)
  when "\C-U"
    rl_unix_line_discard(1, c)
  when RETURN,NEWLINE
    return 0
  when "\C-H",RUBOUT
    if (@rl_point == 0)
      _rl_nsearch_abort(cxt)
      return -1
    end
    _rl_rubout_char(1, c)
  when "\C-C","\C-G"
    rl_ding()
    _rl_nsearch_abort(cxt)
    return -1
  else
    if !@rl_byte_oriented
      rl_insert_text(cxt.mb)
    else
      _rl_insert_char(1, c)
    end
  end

  send(@rl_redisplay_function)
  1
end

._rl_nsearch_dosearch(cxt) ⇒ Object

Perform one search according to CXT, using NONINC_SEARCH_STRING. Return

-1 if the search should be aborted, any other value means to clean up
using _rl_nsearch_cleanup ().  Returns 1 if the search was successful,
0 otherwise.

8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
# File 'lib/rbreadline.rb', line 8274

def _rl_nsearch_dosearch(cxt)
  @rl_mark = cxt.save_mark

  # If rl_point == 0, we want to re-use the previous search string and
  #   start from the saved history position.  If there's no previous search
  #   string, punt.
  if (@rl_point == 0)
    if @noninc_search_string.nil?
      rl_ding()
      rl_restore_prompt()
      rl_unsetstate(RL_STATE_NSEARCH)
      return -1
    end
  else
    # We want to start the search from the current history position.
    @noninc_history_pos = cxt.save_line
    @noninc_search_string = @rl_line_buffer.dup

    # If we don't want the subsequent undo list generated by the search
    #matching a history line to include the contents of the search string,
    #we need to clear rl_line_buffer here.  For now, we just clear the
    #undo list generated by reading the search string.  (If the search
    #fails, the old undo list will be restored by rl_maybe_unsave_line.)
    rl_free_undo_list()
  end

  rl_restore_prompt()
  noninc_dosearch(@noninc_search_string, cxt.direction)
end

._rl_nsearch_init(dir, pchar) ⇒ Object


8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
# File 'lib/rbreadline.rb', line 8195

def _rl_nsearch_init(dir, pchar)
  cxt = _rl_scxt_alloc(RL_SEARCH_NSEARCH, 0)
  if (dir < 0)
    cxt.sflags |= SF_REVERSE      # not strictly needed
  end
  cxt.direction = dir
  cxt.history_pos = cxt.save_line
  rl_maybe_save_line()
  # Clear the undo list, since reading the search string should create its
  #   own undo list, and the whole list will end up being freed when we
  #   finish reading the search string.
  @rl_undo_list = nil

  # Use the line buffer to read the search string.
  @rl_line_buffer[0,1] = 0.chr
  @rl_end = @rl_point = 0

  _p = _rl_make_prompt_for_search(pchar ? pchar : ':')
  rl_message(_p)
  _p = nil

  rl_setstate(RL_STATE_NSEARCH)
  @_rl_nscxt = cxt
  cxt
end

._rl_output_some_chars(string, start, count) ⇒ Object

Write COUNT characters from STRING to the output stream.


3914
3915
3916
3917
3918
3919
3920
3921
# File 'lib/rbreadline.rb', line 3914

def _rl_output_some_chars(string,start,count)
  case @encoding
  when 'X'
    @_rl_out_stream.write(string[start, count].force_encoding(@encoding_name))
  else
    @_rl_out_stream.write(string[start, count])
  end
end

._rl_overwrite_char(count, c) ⇒ Object

Overwrite the character at point (or next COUNT characters) with C.

If C introduces a multibyte character sequence, read the entire sequence
before starting the overwrite loop.

5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
# File 'lib/rbreadline.rb', line 5626

def _rl_overwrite_char(count, c)

  # Read an entire multibyte character sequence to insert COUNT times.
  if (count > 0 && !@rl_byte_oriented)
    mbkey = ''
    _rl_read_mbstring(c, mbkey, MB_LEN_MAX)
  end
  rl_begin_undo_group()

  count.times do
    if !@rl_byte_oriented
      rl_insert_text(mbkey)
    else
      _rl_insert_char(1, c)
    end
    if (@rl_point < @rl_end)
      rl_delete(1, c)
    end
  end

  rl_end_undo_group()

  return 0
end

._rl_overwrite_rubout(count, key) ⇒ Object

This is different from what vi does, so the code's not shared. Emacs

rubout in overwrite mode has one oddity:  it replaces a control
character that's displayed as two characters (^X) with two spaces.

5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
# File 'lib/rbreadline.rb', line 5810

def _rl_overwrite_rubout(count, key)
  if (@rl_point == 0)
    rl_ding()
    return 1
  end

  opoint = @rl_point

  # L == number of spaces to insert
  l = 0
  count.times do
    rl_backward_char(1, key)
    l += rl_character_len(@rl_line_buffer[@rl_point,1], @rl_point) # not exactly right
  end

  rl_begin_undo_group()

  if (count > 1 || @rl_explicit_arg)
    rl_kill_text(opoint, @rl_point)
  else
    rl_delete_text(opoint, @rl_point)
  end
  # Emacs puts point at the beginning of the sequence of spaces.
  if (@rl_point < @rl_end)
    opoint = @rl_point
    _rl_insert_char(l, ' ')
    @rl_point = opoint
  end

  rl_end_undo_group()

  0
end

._rl_read_init_file(filename, include_level) ⇒ Object


2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
# File 'lib/rbreadline.rb', line 2112

def _rl_read_init_file(filename, include_level)
  @current_readline_init_file = filename
  @current_readline_init_include_level = include_level

  openname = File.expand_path(filename)
  begin
    buffer = nil
    File.open(openname) do |file|
      buffer = file.read
    end
  rescue
    return -1
  end

  if (include_level == 0 && filename != @last_readline_init_file)
    @last_readline_init_file = filename.dup
  end

  @currently_reading_init_file = true

  # Loop over the lines in the file.  Lines that start with `#' are
  #   comments; all other lines are commands for readline initialization.
  @current_readline_init_lineno = 1

  buffer.each_line do |line|
    line.strip!
    next if line =~ /^#/
    next if line == ''
    rl_parse_and_bind(line)
  end

  return 0
end

._rl_read_mbchar(mbchar, size) ⇒ Object

read multibyte char


8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
# File 'lib/rbreadline.rb', line 8776

def _rl_read_mbchar(mbchar, size)
  mb_len = 0
  while (mb_len < size)
    rl_setstate(RL_STATE_MOREINPUT)
    c = rl_read_key()
    rl_unsetstate(RL_STATE_MOREINPUT)

    break if c.is_a?(Integer) && c < 0

    mbchar << c
    mb_len += 1
    case @encoding
    when 'E'
      break unless mbchar.scan(/./me).empty?
    when 'S'
      break unless mbchar.scan(/./ms).empty?
    when 'U'
      break unless mbchar.scan(/./mu).empty?
    when 'X'
      break if mbchar.dup.force_encoding(@encoding_name).valid_encoding?
    end
  end
  mb_len
end

._rl_read_mbstring(first, mb, mlen) ⇒ Object

Read a multibyte-character string whose first character is FIRST into

the buffer MB of length MLEN.  Returns the last character read, which
may be FIRST.  Used by the search functions, among others.  Very similar
to _rl_read_mbchar.

8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
# File 'lib/rbreadline.rb', line 8805

def _rl_read_mbstring(first, mb, mlen)
  c = first
  (0...mlen).each do
    mb << c
    if _rl_get_char_len(mb) == -2
      # Read more for multibyte character
      rl_setstate(RL_STATE_MOREINPUT)
      c = rl_read_key()
      break if c.is_a?(Integer) && c < 0
      rl_unsetstate(RL_STATE_MOREINPUT)
    else
      break
    end
  end
  c
end

._rl_redisplay_after_sigwinchObject

Redisplay the current line after a SIGWINCH is received.


8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
# File 'lib/rbreadline.rb', line 8853

def _rl_redisplay_after_sigwinch()
  # Clear the current line and put the cursor at column 0.  Make sure
  #   the right thing happens if we have wrapped to a new screen line.
  if @_rl_term_cr
    @rl_outstream.write(@_rl_term_cr)
    @_rl_last_c_pos = 0
    if @_rl_term_clreol
      @rl_outstream.write(@_rl_term_clreol)
    else
      space_to_eol(@_rl_screenwidth)
      @rl_outstream.write(@_rl_term_cr)
    end

    if @_rl_last_v_pos > 0
      _rl_move_vert(0)
    end
  else
    rl_crlf()
  end

  # Redraw only the last line of a multi-line prompt.
  t = @rl_display_prompt.index("\n")
  if t
    redraw_prompt(@rl_display_prompt[(t+1)..-1])
  else
    rl_forced_update_display()
  end
end

._rl_replace_text(text, start, _end) ⇒ Object

Replace the contents of the line buffer between START and END with

TEXT.  The operation is undoable.  To replace the entire line in an
undoable mode, use _rl_replace_text(text, 0, rl_end)

5030
5031
5032
5033
5034
5035
5036
5037
# File 'lib/rbreadline.rb', line 5030

def _rl_replace_text(text, start, _end)
  rl_begin_undo_group()
  rl_delete_text(start, _end + 1)
  @rl_point = start
  n = rl_insert_text(text)
  rl_end_undo_group()
  n
end

._rl_reset_argumentObject

Create a default argument.


4017
4018
4019
4020
4021
# File 'lib/rbreadline.rb', line 4017

def _rl_reset_argument()
  @rl_numeric_arg = @rl_arg_sign = 1
  @rl_explicit_arg = false
  @_rl_argcxt = 0
end

._rl_rubout_char(count, key) ⇒ Object


5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
# File 'lib/rbreadline.rb', line 5871

def _rl_rubout_char(count, key)
  # Duplicated code because this is called from other parts of the library.
  if (count < 0)
    return (rl_delete(-count, key))
  end
  if (@rl_point == 0)
    rl_ding()
    return -1
  end

  orig_point = @rl_point
  if (count > 1 || @rl_explicit_arg)
    rl_backward_char(count, key)
    rl_kill_text(orig_point, @rl_point)
  elsif (@rl_byte_oriented)
    c = @rl_line_buffer[@rl_point-=1,1]
    rl_delete_text(@rl_point, orig_point)
    # The erase-at-end-of-line hack is of questionable merit now.
    if (@rl_point == @rl_end && isprint(c) && @_rl_last_c_pos!=0)
      l = rl_character_len(c, @rl_point)
      _rl_erase_at_end_of_line(l)
    end
  else
    @rl_point = _rl_find_prev_mbchar(@rl_line_buffer, @rl_point, MB_FIND_NONZERO)
    rl_delete_text(@rl_point, orig_point)
  end

  0
end

._rl_scxt_alloc(type, flags) ⇒ Object


7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
# File 'lib/rbreadline.rb', line 7229

def _rl_scxt_alloc(type, flags)
  cxt = Struct.new(:type,:sflags,:search_string,:search_string_index,:search_string_size,:lines,:allocated_line,
                   :hlen,:hindex,:save_point,:save_mark,:save_line,:last_found_line,:prev_line_found,:save_undo_list,:history_pos,
                   :direction,:lastc,:sline,:sline_len,:sline_index,:search_terminators,:mb).new

  cxt.type = type
  cxt.sflags = flags

  cxt.search_string = nil
  cxt.search_string_size = cxt.search_string_index = 0

  cxt.lines = nil
  cxt.allocated_line = nil
  cxt.hlen = cxt.hindex = 0

  cxt.save_point = @rl_point
  cxt.save_mark = @rl_mark
  cxt.save_line = where_history()
  cxt.last_found_line = cxt.save_line
  cxt.prev_line_found = nil

  cxt.save_undo_list = nil

  cxt.history_pos = 0
  cxt.direction = 0

  cxt.lastc = 0

  cxt.sline = nil
  cxt.sline_len = cxt.sline_index = 0

  cxt.search_terminators = nil

  cxt
end

._rl_search_getchar(cxt) ⇒ Object


4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
# File 'lib/rbreadline.rb', line 4044

def _rl_search_getchar(cxt)
  # Read a key and decide how to proceed.
  rl_setstate(RL_STATE_MOREINPUT)
  c = cxt.lastc = rl_read_key()
  rl_unsetstate(RL_STATE_MOREINPUT)
  if !@rl_byte_oriented
    cxt.mb = ""
    c = cxt.lastc = _rl_read_mbstring(cxt.lastc, cxt.mb, MB_LEN_MAX)
  end
  c
end

._rl_set_insert_mode(im, force) ⇒ Object

Function for the rest of the library to use to set insert/overwrite mode.


1449
1450
1451
# File 'lib/rbreadline.rb', line 1449

def _rl_set_insert_mode(im, force)
  @rl_insert_mode = im
end

._rl_set_mark_at_pos(position) ⇒ Object

Set the mark at POSITION.


7124
7125
7126
7127
7128
# File 'lib/rbreadline.rb', line 7124

def _rl_set_mark_at_pos(position)
  return -1 if (position > @rl_end)
  @rl_mark = position
  0
end

._rl_start_using_historyObject

Set the history pointer back to the last entry in the history.


2604
2605
2606
2607
# File 'lib/rbreadline.rb', line 2604

def _rl_start_using_history()
  using_history()
  @_rl_saved_line_for_history = nil
end

._rl_strip_prompt(pmt) ⇒ Object


3883
3884
3885
# File 'lib/rbreadline.rb', line 3883

def _rl_strip_prompt(pmt)
  return expand_prompt(pmt).first
end

._rl_subseq_getchar(key) ⇒ Object


4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
# File 'lib/rbreadline.rb', line 4633

def _rl_subseq_getchar(key)
  if (key == ESC)
    rl_setstate(RL_STATE_METANEXT)
  end
  rl_setstate(RL_STATE_MOREINPUT)
  k = rl_read_key()
  rl_unsetstate(RL_STATE_MOREINPUT)
  if (key == ESC)
    rl_unsetstate(RL_STATE_METANEXT)
  end

  return k
end

._rl_to_lower(char) ⇒ Object


1312
1313
1314
# File 'lib/rbreadline.rb', line 1312

def _rl_to_lower(char)
  char.nil? ? nil : char.chr.downcase
end

._rl_unget_char(key) ⇒ Object

Stuff KEY into the front of the input buffer.

Returns non-zero if successful, zero if there is
no space left in the buffer.

4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
# File 'lib/rbreadline.rb', line 4621

def _rl_unget_char(key)
  if (ibuffer_space()!=0)
    @pop_index-=1
    if (@pop_index < 0)
      @pop_index = @ibuffer_len - 1
    end
    @ibuffer[@pop_index] = key
    return (1)
  end
  return (0)
end

._rl_update_finalObject


5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
# File 'lib/rbreadline.rb', line 5732

def _rl_update_final()
  full_lines = false
  # If the cursor is the only thing on an otherwise-blank last line,
  #   compensate so we don't print an extra CRLF.
  if (@_rl_vis_botlin && @_rl_last_c_pos == 0 &&
      @visible_line[@vis_lbreaks[@_rl_vis_botlin],1] == 0.chr )
    @_rl_vis_botlin-=1
    full_lines = true
  end
  _rl_move_vert(@_rl_vis_botlin)
  # If we've wrapped lines, remove the final xterm line-wrap flag.
  if (full_lines && @_rl_term_autowrap && (vis_llen(@_rl_vis_botlin) == @_rl_screenwidth))
    last_line = @visible_line[@vis_lbreaks[@_rl_vis_botlin]..-1]
    @cpos_buffer_position = -1 # don't know where we are in buffer
    _rl_move_cursor_relative(@_rl_screenwidth - 1, last_line)   # XXX
    _rl_clear_to_eol(0)
    @rl_outstream.write(last_line[@_rl_screenwidth - 1,1])
  end
  @_rl_vis_botlin = 0
  rl_crlf()
  @rl_outstream.flush
  @rl_display_fixed = true if !@rl_display_fixed
end

._rl_vi_done_insertingObject


5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
# File 'lib/rbreadline.rb', line 5692

def _rl_vi_done_inserting()
  if (@_rl_vi_doing_insert)

    # The `C', `s', and `S' commands set this.
    rl_end_undo_group()
    # Now, the text between rl_undo_list->next->start and
    # rl_undo_list->next->end is what was inserted while in insert
    # mode.  It gets copied to VI_INSERT_BUFFER because it depends
    # on absolute indices into the line which may change (though they
    # probably will not).
    @_rl_vi_doing_insert = 0
    _rl_vi_save_insert(@rl_undo_list.next)
    @vi_continued_command = 1
  else
    if ((@_rl_vi_last_key_before_insert == 'i' || @_rl_vi_last_key_before_insert == 'a') && @rl_undo_list)
      _rl_vi_save_insert(@rl_undo_list)

      # XXX - Other keys probably need to be checked.
    elsif (@_rl_vi_last_key_before_insert == 'C')
      rl_end_undo_group()
    end
    while (@_rl_undo_group_level > 0)
      rl_end_undo_group()
    end
    @vi_continued_command = 0
  end
end

._rl_vi_initialize_lineObject


3839
3840
3841
# File 'lib/rbreadline.rb', line 3839

def _rl_vi_initialize_line
  rl_unsetstate(RL_STATE_VICMDONCE)
end

._rl_vi_reset_lastObject


5725
5726
5727
5728
5729
5730
# File 'lib/rbreadline.rb', line 5725

def _rl_vi_reset_last()
  @_rl_vi_last_command = 'i'
  @_rl_vi_last_repeat = 1
  @_rl_vi_last_arg_sign = 1
  @_rl_vi_last_motion = 0
end

._rl_vi_save_insert(up) ⇒ Object


5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
# File 'lib/rbreadline.rb', line 5679

def _rl_vi_save_insert(up)
  if (up.nil? || up.what != UNDO_INSERT)
    if (@vi_insert_buffer_size >= 1)
      @vi_insert_buffer[0] = 0.chr
    end
    return
  end
  start = up.start
  _end = up.end
  len = _end - start + 1
  @vi_insert_buffer = @rl_line_buffer[start,len-1]
end

._rl_vi_textmod_command(c) ⇒ Object

Is the command C a VI mode text modification command?


5721
5722
5723
# File 'lib/rbreadline.rb', line 5721

def _rl_vi_textmod_command(c)
  return @vi_textmod[c]
end

._rl_walphabetic(c) ⇒ Object


5219
5220
5221
# File 'lib/rbreadline.rb', line 5219

def _rl_walphabetic(c)
  rl_alphabetic(c)
end

.add_history(string) ⇒ Object

Place STRING at the end of the history list. The data field

is  set to NULL.

6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
# File 'lib/rbreadline.rb', line 6153

def add_history(string)
  if (@history_stifled && (@history_length == @history_max_entries))
    # If the history is stifled, and history_length is zero,
    # and it equals history_max_entries, we don't save items.
    return if (@history_length == 0)
    @the_history.shift
  else
    if @the_history.nil?
      @the_history = []
      @history_length = 1
    else
      @history_length+=1
    end
  end

  temp = alloc_history_entry(string, hist_inittime())
  @the_history[@history_length] = nil
  @the_history[@history_length - 1] = temp
end

.alloc_history_entry(string, ts) ⇒ Object


6133
6134
6135
6136
6137
6138
6139
6140
# File 'lib/rbreadline.rb', line 6133

def alloc_history_entry(string, ts)
  temp = Struct.new(:line,:data,:timestamp).new
  temp.line = string ? string.encode('UTF-8', invalid: :replace, undef: :replace, replace: '').delete(0.chr) : string
  temp.data = nil
  temp.timestamp = ts

  return temp
end

.alloc_undo_entry(what, start, _end, text) ⇒ Object


4930
4931
4932
4933
4934
4935
4936
4937
4938
# File 'lib/rbreadline.rb', line 4930

def alloc_undo_entry(what, start, _end, text)
  temp = Struct.new(:what,:start,:end,:text,:next).new
  temp.what = what
  temp.start = start
  temp.end = _end
  temp.text = text
  temp.next = nil
  temp
end

.append_to_match(text, delimiter, quote_char, nontrivial_match) ⇒ Object

Append any necessary closing quote and a separator character to the just-inserted match. If the user has specified that directories should be marked by a trailing `/', append one of those instead. The default trailing character is a space. Returns the number of characters appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS has them) and don't add a suffix for a symlink to a directory. A nontrivial match is one that actually adds to the word being completed. The variable rl_completion_mark_symlink_dirs controls this behavior (it's initially set to the what the user has chosen, indicated by the value of _rl_complete_mark_symlink_dirs, but may be modified by an application's completion function).


8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
# File 'lib/rbreadline.rb', line 8469

def append_to_match(text, delimiter, quote_char, nontrivial_match)
  temp_string = 0.chr * 4
  temp_string_index = 0
  if (quote_char && @rl_point>0 && !@rl_completion_suppress_quote &&
      @rl_line_buffer[@rl_point - 1,1] != quote_char)
    temp_string[temp_string_index] = quote_char
    temp_string_index += 1
  end
  if (delimiter != 0.chr)
    temp_string[temp_string_index] = delimiter
    temp_string_index += 1
  elsif (!@rl_completion_suppress_append && @rl_completion_append_character)
    temp_string[temp_string_index] = @rl_completion_append_character
    temp_string_index += 1
  end
  temp_string[temp_string_index] = 0.chr
  temp_string_index += 1

  if (@rl_filename_completion_desired)
    filename = File.expand_path(text)
    return temp_string_index unless File.exists? filename

    s = (nontrivial_match && !@rl_completion_mark_symlink_dirs) ?
      File.lstat(filename) : File.stat(filename)
    if s.directory?
      if @_rl_complete_mark_directories
        # This is clumsy.  Avoid putting in a double slash if point
        # is at the end of the line and the previous character is a
        # slash.
        if (@rl_point>0 && @rl_line_buffer[@rl_point,1] == 0.chr && @rl_line_buffer[@rl_point - 1,1] == '/' )

        elsif (@rl_line_buffer[@rl_point,1] != '/')
          rl_insert_text('/')
        end
      end
      # Don't add anything if the filename is a symlink and resolves to a
      # directory.
    elsif s.symlink? && File.stat(filename).directory?

    else
      if (@rl_point == @rl_end && temp_string_index>0)
        rl_insert_text(temp_string)
      end
    end
    filename = nil
  else
    if (@rl_point == @rl_end && temp_string_index>0)
      rl_insert_text(temp_string)
    end
  end
  temp_string_index
end

.bind_arrow_keysObject

Try and bind the common arrow key prefixes after giving termcap and

the inputrc file a chance to bind them and create `real' keymaps
for the arrow key prefix.

2540
2541
2542
2543
2544
# File 'lib/rbreadline.rb', line 2540

def bind_arrow_keys()
  bind_arrow_keys_internal(@emacs_standard_keymap)
  bind_arrow_keys_internal(@vi_movement_keymap)
  bind_arrow_keys_internal(@vi_insertion_keymap)
end

.bind_arrow_keys_internal(map) ⇒ Object

Bind some common arrow key sequences in MAP.


2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
# File 'lib/rbreadline.rb', line 2503

def bind_arrow_keys_internal(map)
  xkeymap = @_rl_keymap
  @_rl_keymap = map

  if RUBY_PLATFORM =~ /mswin|mingw/
    rl_bind_keyseq_if_unbound("\340H", :rl_get_previous_history) # Up
    rl_bind_keyseq_if_unbound("\340P", :rl_get_next_history) # Down
    rl_bind_keyseq_if_unbound("\340M", :rl_forward_char)  # Right
    rl_bind_keyseq_if_unbound("\340K", :rl_backward_char) # Left
    rl_bind_keyseq_if_unbound("\340G", :rl_beg_of_line)   # Home
    rl_bind_keyseq_if_unbound("\340O", :rl_end_of_line)   # End
    rl_bind_keyseq_if_unbound("\340s", :rl_backward_word) # Ctrl-Left
    rl_bind_keyseq_if_unbound("\340t", :rl_forward_word) # Ctrl-Right
    rl_bind_keyseq_if_unbound("\340S", :rl_delete) # Delete
    rl_bind_keyseq_if_unbound("\340R", :rl_overwrite_mode) # Insert
  else
    rl_bind_keyseq_if_unbound("\033[A", :rl_get_previous_history)
    rl_bind_keyseq_if_unbound("\033[B", :rl_get_next_history)
    rl_bind_keyseq_if_unbound("\033[C", :rl_forward_char)
    rl_bind_keyseq_if_unbound("\033[D", :rl_backward_char)
    rl_bind_keyseq_if_unbound("\033[H", :rl_beg_of_line)
    rl_bind_keyseq_if_unbound("\033[F", :rl_end_of_line)

    rl_bind_keyseq_if_unbound("\033OA", :rl_get_previous_history)
    rl_bind_keyseq_if_unbound("\033OB", :rl_get_next_history)
    rl_bind_keyseq_if_unbound("\033OC", :rl_forward_char)
    rl_bind_keyseq_if_unbound("\033OD", :rl_backward_char)
    rl_bind_keyseq_if_unbound("\033OH", :rl_beg_of_line)
    rl_bind_keyseq_if_unbound("\033OF", :rl_end_of_line)
  end

  @_rl_keymap = xkeymap
end

.bind_termcap_arrow_keys(map) ⇒ Object

Bind the arrow key sequences from the termcap description in MAP.


1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
# File 'lib/rbreadline.rb', line 1953

def bind_termcap_arrow_keys(map)
  xkeymap = @_rl_keymap
  @_rl_keymap = map

  rl_bind_keyseq_if_unbound(@_rl_term_ku, :rl_get_previous_history)
  rl_bind_keyseq_if_unbound(@_rl_term_kd, :rl_get_next_history)
  rl_bind_keyseq_if_unbound(@_rl_term_kr, :rl_forward_char)
  rl_bind_keyseq_if_unbound(@_rl_term_kl, :rl_backward_char)

  rl_bind_keyseq_if_unbound(@_rl_term_kh, :rl_beg_of_line) # Home
  rl_bind_keyseq_if_unbound(@_rl_term_at7, :rl_end_of_line)   # End

  rl_bind_keyseq_if_unbound(@_rl_term_kD, :rl_delete)
  rl_bind_keyseq_if_unbound(@_rl_term_kI, :rl_overwrite_mode)

  @_rl_keymap = xkeymap
end

.block_sigintObject


6942
6943
6944
6945
6946
# File 'lib/rbreadline.rb', line 6942

def block_sigint()
  return if @sigint_blocked
  @sigint_proc = Signal.trap("INT", "IGNORE")
  @sigint_blocked = true
end

.clear_historyObject


8551
8552
8553
8554
# File 'lib/rbreadline.rb', line 8551

def clear_history()
  @the_history = nil
  @history_offset = @history_length = 0
end

.compute_lcd_of_matches(match_list, matches, text) ⇒ Object

Find the common prefix of the list of matches, and put it into

matches[0].

1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
# File 'lib/rbreadline.rb', line 1318

def compute_lcd_of_matches(match_list, matches, text)
  # If only one match, just use that.  Otherwise, compare each
  #   member of the list with the next, finding out where they
  #   stop matching.
  if (matches == 1)
    match_list[0] = match_list[1]
    match_list[1] = nil
    return 1
  end

  i = 1
  low = 100000
  while(i<matches)
    if (@_rl_completion_case_fold)
      si = 0
      while((c1 = _rl_to_lower(match_list[i][si])) &&
            (c2 = _rl_to_lower(match_list[i + 1][si])))
        if !@rl_byte_oriented
          if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
            break
          elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
            si += v - 1
          end
        else
          break if (c1 != c2)
        end
        si += 1
      end
    else
      si = 0
      while((c1 = match_list[i][si]) &&
            (c2 = match_list[i + 1][si]))
        if !@rl_byte_oriented
          if(!_rl_compare_chars(match_list[i],si,match_list[i+1],si))
            break
          elsif ((v = _rl_get_char_len(match_list[i][si..-1])) > 1)
            si += v - 1
          end
        else
          break if (c1 != c2)
        end
        si += 1
      end
    end

    if (low > si)
      low = si
    end
    i += 1
  end

  # If there were multiple matches, but none matched up to even the
  #   first character, and the user typed something, use that as the
  #   value of matches[0].
  if (low == 0 && text && text.length>0 )
    match_list[0] = text.dup
  else
    # XXX - this might need changes in the presence of multibyte chars

    # If we are ignoring case, try to preserve the case of the string
    # the user typed in the face of multiple matches differing in case.
    if (@_rl_completion_case_fold)

      # We're making an assumption here:
      #  IF we're completing filenames AND
      #     the application has defined a filename dequoting function AND
      #     we found a quote character AND
      #     the application has requested filename quoting
      #  THEN
      #     we assume that TEXT was dequoted before checking against
      #     the file system and needs to be dequoted here before we
      #     check against the list of matches
      #  FI
      if (@rl_filename_completion_desired &&
          @rl_filename_dequoting_function &&
          @rl_completion_found_quote &&
          @rl_filename_quoting_desired)

        dtext = send(@rl_filename_dequoting_function,text, @rl_completion_quote_character)
        text = dtext
      end

      # sort the list to get consistent answers.
      match_list = [match_list[0]] + match_list[1..-1].sort

      si = text.length
      if (si <= low)
        for i in 1 .. matches
          if match_list[i][0,si] == text
            match_list[0] = match_list[i][0,low]
            break
          end
          # no casematch, use first entry
          if (i > matches)
            match_list[0] = match_list[1][0,low]
          end
        end
      else
        # otherwise, just use the text the user typed.
        match_list[0] = text[0,low]
      end
    else
      match_list[0] = match_list[1][0,low]
    end
  end

  return matches
end

.crObject

Move to the start of the current line.


4700
4701
4702
4703
4704
4705
# File 'lib/rbreadline.rb', line 4700

def cr()
  if (@_rl_term_cr)
    @_rl_out_stream.write(@_rl_term_cr)
    @_rl_last_c_pos = 0
  end
end

.cr_faster(new, cur) ⇒ Object


2610
2611
2612
# File 'lib/rbreadline.rb', line 2610

def cr_faster(new, cur)
  (new + 1) < (cur - new)
end

.ctrl_char(c) ⇒ Object


2667
2668
2669
# File 'lib/rbreadline.rb', line 2667

def ctrl_char(c)
  c < "\x20"
end

.current_historyObject

Return the history entry at the current position, as determined by

history_offset.  If there is no entry there, return a NULL pointer.

2659
2660
2661
# File 'lib/rbreadline.rb', line 2659

def current_history()
  return ((@history_offset == @history_length) || @the_history.nil?) ? nil : @the_history[@history_offset]
end

.delete_chars(count) ⇒ Object

Delete COUNT characters from the display line.


8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
# File 'lib/rbreadline.rb', line 8596

def delete_chars(count)
  return if (count > @_rl_screenwidth)   # XXX

  if @hConsoleHandle.nil?
    #if (@_rl_term_DC)
    #   buffer = tgoto(_rl_term_DC, count, count);
    #   @_rl_out_stream.write(buffer * count)
    #else
    if (@_rl_term_dc)
      @_rl_out_stream.write(@_rl_term_dc * count)
    end
    #end
  end
end

.display_matches(matches) ⇒ Object

Display MATCHES, a list of matching filenames in argv format. This handles the simple case – a single match – first. If there is more than one match, we compute the number of strings in the list and the length of the longest string, which will be needed by the display function. If the application wants to handle displaying the list of matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the address of a function, and we just call it. If we're handling the display ourselves, we just call rl_display_match_list. We also check that the list of matches doesn't exceed the user-settable threshold, and ask the user if he wants to see the list if there are more matches than RL_COMPLETION_QUERY_ITEMS.


6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
# File 'lib/rbreadline.rb', line 6722

def display_matches(matches)
  # Move to the last visible line of a possibly-multiple-line command.
  _rl_move_vert(@_rl_vis_botlin)

  # Handle simple case first.  What if there is only one answer?
  if matches[1].nil?
    temp = printable_part(matches[0])
    rl_crlf()
    print_filename(temp, matches[0])
    rl_crlf()
    rl_forced_update_display()
    @rl_display_fixed = true
    return
  end

  # There is more than one answer.  Find out how many there are,
  #   and find the maximum printed length of a single entry.
  max = 0
  i = 1
  while(matches[i])
    temp = printable_part(matches[i])
    len = fnwidth(temp)

    if (len > max)
      max = len
    end
    i += 1
  end
  len = i - 1

  # If the caller has defined a display hook, then call that now.
  if (@rl_completion_display_matches_hook)
    send(@rl_completion_display_matches_hook,matches, len, max)
    return
  end

  # If there are many items, then ask the user if she really wants to
  #   see them all.
  if (@rl_completion_query_items > 0 && len >= @rl_completion_query_items)

    rl_crlf()
    @rl_outstream.write("Display all #{len} possibilities? (y or n)")
    @rl_outstream.flush
    if (get_y_or_n(false)==0)
      rl_crlf()

      rl_forced_update_display()
      @rl_display_fixed = true

      return
    end
  end

  rl_display_match_list(matches, len, max)

  rl_forced_update_display()
  @rl_display_fixed = true
end

.endsrch_char(c) ⇒ Object


4056
4057
4058
# File 'lib/rbreadline.rb', line 4056

def endsrch_char(c)
  ((ctrl_char(c) || meta_char(c) || (c) == RUBOUT) && ((c) != "\C-G"))
end

.expand_prompt(pmt) ⇒ Object

Current implementation:

 \001 (^A) start non-visible characters
 \002 (^B) end non-visible characters
all characters except \001 and \002 (following a \001) are copied to
the returned string all characters except those between \001 and
\002 are assumed to be `visible'.

1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# File 'lib/rbreadline.rb', line 1676

def expand_prompt(pmt)
  # Short-circuit if we can.
  if (@rl_byte_oriented && pmt[RL_PROMPT_START_IGNORE].nil?)
    r = pmt.dup
    lp = r.length
    lip = 0
    niflp = 0
    vlp = lp
    return [r,lp,lip,niflp,vlp]
  end

  l = pmt.length
  ret = ''
  invfl = 0    # invisible chars in first line of prompt
  invflset = 0    # we only want to set invfl once

  igstart = 0
  rl = 0
  ignoring = false
  last = ninvis = physchars = 0
  for pi in 0 ... pmt.length
    # This code strips the invisible character string markers
    #RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE
    if (!ignoring && pmt[pi,1] == RL_PROMPT_START_IGNORE)        # XXX - check ignoring?
      ignoring = true
      igstart = pi
      next
    elsif (ignoring && pmt[pi,1] == RL_PROMPT_END_IGNORE)
      ignoring = false
      if (pi != (igstart + 1))
        last = ret.length - 1
      end
      next
    else
      if !@rl_byte_oriented
        pind = pi
        ind = _rl_find_next_mbchar(pmt, pind, 1, MB_FIND_NONZERO)
        l = ind - pind
        while (l>0)
          l-=1
          ret << pmt[pi]
          pi += 1
        end
        if (!ignoring)
          rl += ind - pind
          physchars += _rl_col_width(pmt, pind, ind)
        else
          ninvis += ind - pind
        end
        pi-=1       # compensate for later increment
      else
        ret << pmt[pi]
        if (!ignoring)
          rl+=1            # visible length byte counter
          physchars+=1
        else
          ninvis+=1        # invisible chars byte counter
        end

        if (invflset == 0 && rl >= @_rl_screenwidth)
          invfl = ninvis
          invflset = 1
        end
      end
    end
  end

  if (rl < @_rl_screenwidth)
    invfl = ninvis
  end
  lp = rl
  lip = last
  niflp = invfl
  vlp = physchars
  return [ret,lp,lip,niflp,vlp]
end

.fnprint(to_print) ⇒ Object


6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
# File 'lib/rbreadline.rb', line 6502

def fnprint(to_print)
  printed_len = 0

  case @encoding
  when 'E'
    arr = to_print.scan(/./me)
  when 'S'
    arr = to_print.scan(/./ms)
  when 'U'
    arr = to_print.scan(/./mu)
  when 'X'
    arr = to_print.dup.force_encoding(@encoding_name).chars
  else
    arr = to_print.scan(/./m)
  end

  arr.each do |s|
    if(ctrl_char(s))
      @rl_outstream.write('^'+(s[0].ord|0x40).chr.upcase)
      printed_len += 2
    elsif s == RUBOUT
      @rl_outstream.write('^?')
      printed_len += 2
    else
      @rl_outstream.write(s)
      if @encoding=='U'
        printed_len += s.unpack('U').first >= 0x1000 ? 2 : 1
      elsif @encoding=='X'
        printed_len += s.ord >= 0x1000 ? 2 : 1
      else
        printed_len += s.length
      end
    end

  end

  printed_len
end

.fnwidth(string) ⇒ Object

Compute width of STRING when displayed on screen by print_filename


6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
# File 'lib/rbreadline.rb', line 6672

def fnwidth(string)
  left = string.length + 1
  width = pos = 0
  while (string[pos] && string[pos,1] != 0.chr)
    if (ctrl_char(string[0,1]) || string[0,1] == RUBOUT)
      width += 2
      pos+=1
    else
      case @encoding
      when 'E'
        wc = string[pos,left-pos].scan(/./me)[0]
        bytes = wc.length
        tempwidth = wc.length
      when 'S'
        wc = string[pos,left-pos].scan(/./ms)[0]
        bytes = wc.length
        tempwidth = wc.length
      when 'U'
        wc = string[pos,left-pos].scan(/./mu)[0]
        bytes = wc.length
        tempwidth = wc.unpack('U').first >= 0x1000 ? 2 : 1
      when 'X'
        wc = string[pos,left-pos].force_encoding(@encoding_name)[0]
        bytes = wc.bytesize
        tempwidth = wc.ord >= 0x1000 ? 2 : 1
      else
        wc = string[pos,left-pos].scan(/./m)[0]
        bytes = wc.length
        tempwidth = wc.length
      end
      clen = bytes
      pos += clen
      w = tempwidth
      width += (w >= 0) ? w : 1
    end
  end
  width
end

.gen_completion_matches(text, start, _end, our_func, found_quote, quote_char) ⇒ Object


6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
# File 'lib/rbreadline.rb', line 6321

def gen_completion_matches(text, start, _end, our_func, found_quote, quote_char)
  @rl_completion_found_quote = found_quote
  @rl_completion_quote_character = quote_char

  # If the user wants to TRY to complete, but then wants to give
  #   up and use the default completion function, they set the
  #   variable rl_attempted_completion_function.
  if (@rl_attempted_completion_function)
    matches = Readline.send(@rl_attempted_completion_function,text, start, _end)
    if (matches || @rl_attempted_completion_over)
      @rl_attempted_completion_over = false
      return (matches)
    end
  end
  # XXX -- filename dequoting moved into rl_filename_completion_function

  matches = rl_completion_matches(text, our_func)
  matches
end

.get_term_capabilities(buffer) ⇒ Object


1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
# File 'lib/rbreadline.rb', line 1813

def get_term_capabilities(buffer)
  hash = {}
    `infocmp -C`.split(':').select{|x| x =~ /(.*)=(.*)/ and hash[$1]=$2.gsub("\\r", "\r").gsub('\\E',"\e").gsub(/\^(.)/){($1[0].ord ^ ((?a..?z).include?($1[0]) ? 0x60 : 0x40)).chr}}
    @_rl_term_at7          =     hash["@7"]
    @_rl_term_DC           =     hash["DC"]
    @_rl_term_IC           =     hash["IC"]
    @_rl_term_clreol       =     hash["ce"]
    @_rl_term_clrpag       =     hash["cl"]
    @_rl_term_cr           =     hash["cr"]
    @_rl_term_dc           =     hash["dc"]
    @_rl_term_ei           =     hash["ei"]
    @_rl_term_ic           =     hash["ic"]
    @_rl_term_im           =     hash["im"]
    @_rl_term_kD           =     hash["kD"]
    @_rl_term_kH           =     hash["kH"]
    @_rl_term_kI           =     hash["kI"]
    @_rl_term_kd           =     hash["kd"]
    @_rl_term_ke           =     hash["ke"]
    @_rl_term_kh           =     hash["kh"]
    @_rl_term_kl           =     hash["kl"]
    @_rl_term_kr           =     hash["kr"]
    @_rl_term_ks           =     hash["ks"]
    @_rl_term_ku           =     hash["ku"]
    @_rl_term_backspace    =     hash["le"]
    @_rl_term_mm           =     hash["mm"]
    @_rl_term_mo           =     hash["mo"]
    @_rl_term_forward_char =     hash["nd"]
    @_rl_term_pc           =     hash["pc"]
    @_rl_term_up           =     hash["up"]
    @_rl_visible_bell      =     hash["vb"]
    @_rl_term_vs           =     hash["vs"]
    @_rl_term_ve           =     hash["ve"]
    @tcap_initialized = true
end

.get_y_or_n(for_pager) ⇒ Object

The user must press “y” or “n”. Non-zero return means “y” pressed.


6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
# File 'lib/rbreadline.rb', line 6645

def get_y_or_n(for_pager)
  while(true)

    rl_setstate(RL_STATE_MOREINPUT)
    c = rl_read_key()
    rl_unsetstate(RL_STATE_MOREINPUT)

    if (c == 'y' || c == 'Y' || c == ' ')
      return (1)
    end
    if (c == 'n' || c == 'N' || c == RUBOUT)
      return (0)
    end
    if (c == ABORT_CHAR || (c.is_a?(Integer) && c < 0))
      _rl_abort_internal()
    end
    if (for_pager && (c == NEWLINE || c == RETURN))
      return (2)
    end
    if (for_pager && (c == 'q' || c == 'Q'))
      return (0)
    end
    rl_ding()
  end
end

.handle_parser_directive(statement) ⇒ Object

Handle a parser directive. STATEMENT is the line of the directive

without any leading `$'.

2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
# File 'lib/rbreadline.rb', line 2235

def handle_parser_directive(statement)

  directive,args = statement.split(' ')

  case directive.downcase
  when "if"
    parser_if(args)
    return 0
  when "endif"
    parser_endif(args)
    return 0
  when "else"
    parser_else(args)
    return 0
  when "include"
    parser_include(args)
    return 0
  end

  #_rl_init_file_error("unknown parser directive")
  return 1
end

.hist_inittimeObject


6142
6143
6144
6145
6146
6147
6148
6149
# File 'lib/rbreadline.rb', line 6142

def hist_inittime()
  t = Time.now.to_i
  ts = "X%u" % t
  ret = ts.dup
  ret[0,1] = @history_comment_char

  ret
end

.history_arg_extract(first, last, string) ⇒ Object


7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
# File 'lib/rbreadline.rb', line 7576

def history_arg_extract(first, last, string)
  if first != "$" || last != "$"
    fail "RbReadline.history_arg_extract called with currently unsupported args."
  end

  # Find the last index of an unescaped quote character.
  last_unescaped_quote_char = -1
  RbReadline::HISTORY_QUOTE_CHARACTERS.each_char do |quote_char|
    quote_char = Regexp.escape(quote_char)
    if index = string =~ /(?:\\.|[^#{quote_char}\\])#{quote_char} *$/
      last_unescaped_quote_char = index if index > last_unescaped_quote_char
    end
  end
  last_unescaped_quote_char += 1 # Because of the regex used above.

  # Find the last index of an unescaped word delimiter.
  delimiters = RbReadline::HISTORY_WORD_DELIMITERS.chars.to_a.map { |d| Regexp.escape(d) }
  unless last_unescaped_delimiter = string =~ /(?:\\.|[^#{delimiters.join}])+? *$/
    last_unescaped_delimiter = 0
  end

  if last_unescaped_quote_char >= last_unescaped_delimiter
    quoted_arg = _extract_last_quote(string, string[last_unescaped_quote_char,1])
  end
  quoted_arg or string[last_unescaped_delimiter...string.length]
end

.history_baseObject

Returns the value of attribute history_base


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def history_base
  @history_base
end

.history_get(offset) ⇒ Object

Return the history entry which is logically at OFFSET in the history array.

OFFSET is relative to history_base.

6909
6910
6911
6912
6913
# File 'lib/rbreadline.rb', line 6909

def history_get(offset)
  local_index = offset - @history_base
  return (local_index >= @history_length || local_index < 0 || @the_history.nil?) ?
    nil : @the_history[local_index]
end

.history_is_stifledObject


8547
8548
8549
# File 'lib/rbreadline.rb', line 8547

def history_is_stifled()
  return (@history_stifled)
end

.history_lengthObject

Returns the value of attribute history_length


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def history_length
  @history_length
end

.history_listObject


7265
7266
7267
# File 'lib/rbreadline.rb', line 7265

def history_list()
  @the_history
end

.history_search_pos(string, dir, pos) ⇒ Object

Search for STRING in the history list. DIR is < 0 for searching

backwards.  POS is an absolute index into the history list at
which point to begin searching.

8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
# File 'lib/rbreadline.rb', line 8097

def history_search_pos(string, dir, pos)
  old = where_history()
  history_set_pos(pos)
  if (history_search(string, dir) == -1)
    history_set_pos(old)
    return (-1)
  end
  ret = where_history()
  history_set_pos(old)
  ret
end

.history_search_prefix(string, direction) ⇒ Object

Do an anchored search for string through the history in DIRECTION.


8090
8091
8092
# File 'lib/rbreadline.rb', line 8090

def history_search_prefix (string, direction)
  history_search_internal(string, direction, ANCHORED_SEARCH)
end

.history_set_pos(pos) ⇒ Object

Make the current history item be the one at POS, an absolute index.

Returns zero if POS is out of range, else non-zero.

8081
8082
8083
8084
8085
8086
8087
# File 'lib/rbreadline.rb', line 8081

def history_set_pos(pos)
  if (pos > @history_length || pos < 0 || @the_history.nil?)
    return (0)
  end
  @history_offset = pos
  1
end

.ibuffer_spaceObject

Return the amount of space available in the buffer for stuffing

characters.

4593
4594
4595
4596
4597
4598
4599
# File 'lib/rbreadline.rb', line 4593

def ibuffer_space()
  if (@pop_index > @push_index)
    return (@pop_index - @push_index - 1)
  else
    return (@ibuffer_len - (@push_index - @pop_index))
  end
end

.init_line_structures(minsize) ⇒ Object

Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their associated

arrays of line break markers.  MINSIZE is the minimum size of VISIBLE_LINE
and INVISIBLE_LINE; if it is greater than LINE_SIZE, LINE_SIZE is
increased.  If the lines have already been allocated, this ensures that
they can hold at least MINSIZE characters.

2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
# File 'lib/rbreadline.rb', line 2631

def init_line_structures(minsize)
  if @invisible_line.nil? # initialize it
    if (@line_size < minsize)
      @line_size = minsize
    end
    @visible_line = 0.chr * @line_size
    @invisible_line = 0.chr * @line_size # 1.chr
  elsif (@line_size < minsize)  # ensure it can hold MINSIZE chars
    @line_size *= 2
    if (@line_size < minsize)
      @line_size = minsize
    end
    @visible_line << 0.chr * (@line_size - @visible_line.length)
    @invisible_line << 1.chr * (@line_size - @invisible_line.length)
  end
  @visible_line[minsize,@line_size-minsize] = 0.chr * (@line_size-minsize)
  @invisible_line[minsize,@line_size-minsize] = 1.chr * (@line_size-minsize)

  if @vis_lbreaks.nil?
    @inv_lbreaks = []
    @vis_lbreaks = []
    @_rl_wrapped_line = []
    @inv_lbreaks[0] = @vis_lbreaks[0] = 0
  end
end

.insert_all_matches(matches, point, qc) ⇒ Object


6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
# File 'lib/rbreadline.rb', line 6389

def insert_all_matches(matches, point, qc)
  rl_begin_undo_group()
  # remove any opening quote character; make_quoted_replacement will add
  #   it back.
  if (qc && qc.length>0 && point>0 && @rl_line_buffer[point - 1,1] == qc)
    point-=1
  end
  rl_delete_text(point, @rl_point)
  @rl_point = point
  if (matches[1])
    i = 1
    while(matches[i])
      rp = make_quoted_replacement(matches[i], SINGLE_MATCH, qc)
      rl_insert_text(rp)
      rl_insert_text(" ")
      if (rp != matches[i])
        rp = nil
      end
      i += 1
    end
  else
    rp = make_quoted_replacement(matches[0], SINGLE_MATCH, qc)
    rl_insert_text(rp)
    rl_insert_text(" ")
    if (rp != matches[0])
      rp = nil
    end
  end
  rl_end_undo_group()
end

.insert_match(match, start, mtype, qc) ⇒ Object


6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
# File 'lib/rbreadline.rb', line 6461

def insert_match(match, start, mtype, qc)
  oqc = qc
  replacement = make_quoted_replacement(match, mtype, qc)

  # Now insert the match.
  if (replacement)
    # Don't double an opening quote character.
    if (qc && qc.length>0 && start!=0 && @rl_line_buffer[start - 1,1] == qc &&
        replacement[0,1] == qc)
      start-=1
      # If make_quoted_replacement changed the quoting character, remove
      # the opening quote and insert the (fully-quoted) replacement.
    elsif (qc && (qc != oqc) && start!=0 && @rl_line_buffer[start - 1,1] == oqc &&
           replacement[0,1] != oqc)
      start-=1
    end
    _rl_replace_text(replacement, start, @rl_point - 1)
    if (replacement != match)
      replacement = nil
    end
  end
end

.insert_some_chars(string, count, col) ⇒ Object

Insert COUNT characters from STRING to the output stream at column COL.


8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
# File 'lib/rbreadline.rb', line 8557

def insert_some_chars(string, count, col)
  if @hConsoleHandle
    _rl_output_some_chars(string,0,count)
  else
    # DEBUGGING
    if (@rl_byte_oriented)
      if (count != col)
        $stderr.write("readline: debug: insert_some_chars: count (#{count}) != col (#{col})\n");
      end
    end
    # If IC is defined, then we do not have to "enter" insert mode.
    #if (@_rl_term_IC)
    #   buffer = tgoto(@_rl_term_IC, 0, col)
    #   @_rl_out_stream.write(buffer)
    #   _rl_output_some_chars(string,0,count)
    #else
    # If we have to turn on insert-mode, then do so.
    if (@_rl_term_im)
      @_rl_out_stream.write(@_rl_term_im)
    end
    # If there is a special command for inserting characters, then
    # use that first to open up the space.
    if (@_rl_term_ic)
      @_rl_out_stream.write(@_rl_term_ic * count)
    end

    # Print the text.
    _rl_output_some_chars(string,0, count)

    # If there is a string to turn off insert mode, we had best use
    # it now.
    if (@_rl_term_ei)
      @_rl_out_stream.write(@_rl_term_ei)
    end
    #end
  end
end

.inv_line(line) ⇒ Object


2703
2704
2705
# File 'lib/rbreadline.rb', line 2703

def inv_line(line)
  @invisible_line[@inv_lbreaks[line] .. -1]
end

.inv_llen(l) ⇒ Object


2687
2688
2689
# File 'lib/rbreadline.rb', line 2687

def inv_llen(l)
  (@inv_lbreaks[l+1] - @inv_lbreaks[l])
end

.isascii(c) ⇒ Object


8015
8016
8017
8018
# File 'lib/rbreadline.rb', line 8015

def isascii(c)
  int_val = c[0].to_i # 1.8 + 1.9 compat.
  return (int_val < 128 && int_val > 0)
end

.isprint(c) ⇒ Object


2671
2672
2673
# File 'lib/rbreadline.rb', line 2671

def isprint(c)
  c >= "\x20" && c < "\x7f"
end

.m_offset(margin, offset) ⇒ Object


2707
2708
2709
# File 'lib/rbreadline.rb', line 2707

def m_offset(margin, offset)
  ((margin) == 0 ? offset : 0)
end

.make_history_line_current(entry) ⇒ Object

Make the data from the history entry ENTRY be the contents of the

current line.  This doesn't do anything with rl_point; the caller
must set it.

8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
# File 'lib/rbreadline.rb', line 8064

def make_history_line_current(entry)
  _rl_replace_text(entry.line, 0, @rl_end)
  _rl_fix_point(1)
  if (@rl_editing_mode == @vi_mode)
    # POSIX.2 says that the `U' command doesn't affect the copy of any
    #   command lines to the edit line.  We're going to implement that by
    #   making the undo list start after the matching line is copied to the
    #   current editing buffer.
    rl_free_undo_list()
  end
  if (@_rl_saved_line_for_history)
    @_rl_saved_line_for_history = nil
  end
end

.make_quoted_replacement(match, mtype, qc) ⇒ Object


6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
# File 'lib/rbreadline.rb', line 6420

def make_quoted_replacement(match, mtype, qc)
  # If we are doing completion on quoted substrings, and any matches
  #   contain any of the completer_word_break_characters, then auto-
  #   matically prepend the substring with a quote character (just pick
  #   the first one from the list of such) if it does not already begin
  #   with a quote string.  FIXME: Need to remove any such automatically
  #   inserted quote character when it no longer is necessary, such as
  #   if we change the string we are completing on and the new set of
  #   matches don't require a quoted substring.
  replacement = match

  should_quote = match && @rl_completer_quote_characters &&
    @rl_filename_completion_desired &&
    @rl_filename_quoting_desired

  if (should_quote)
    should_quote = should_quote && (qc.nil? || qc == 0.chr ||
                                    (@rl_completer_quote_characters &&
                                     @rl_completer_quote_characters.include?(qc)))
  end

  if (should_quote)

    # If there is a single match, see if we need to quote it.
    #   This also checks whether the common prefix of several
    # matches needs to be quoted.
    should_quote = @rl_filename_quote_characters ?
      !!match[@rl_filename_quote_characters] :
      false

    do_replace = should_quote ? mtype : NO_MATCH
    # Quote the replacement, since we found an embedded
    # word break character in a potential match.
    if (do_replace != NO_MATCH && @rl_filename_quoting_function)
      replacement = send(@rl_filename_quoting_function,match, do_replace, qc)
    end
  end
  replacement
end

.meta_char(c) ⇒ Object


2663
2664
2665
# File 'lib/rbreadline.rb', line 2663

def meta_char(c)
  c > "\x7f" && c <= "\xff"
end

.next_historyObject

Move history_offset forward to the next history entry, and return

a pointer to that entry.  If there is no next entry then return a
NULL pointer.

5445
5446
5447
# File 'lib/rbreadline.rb', line 5445

def next_history()
  (@history_offset == @history_length) ? nil : @the_history[@history_offset+=1]
end

.no_terminal?Boolean

Returns:

  • (Boolean)

8914
8915
8916
8917
# File 'lib/rbreadline.rb', line 8914

def no_terminal?
  term = ENV["TERM"]
  term.nil? || (term == 'dumb') || (RUBY_PLATFORM =~ /mswin|mingw/)
end

.noninc_dosearch(string, dir) ⇒ Object

Search for a line in the history containing STRING. If DIR is < 0, the

search is backwards through previous entries, else through subsequent
entries.  Returns 1 if the search was successful, 0 otherwise.

8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
# File 'lib/rbreadline.rb', line 8138

def noninc_dosearch(string, dir)
  if (string.nil? || string == '' || @noninc_history_pos < 0)
    rl_ding()
    return 0
  end

  pos = noninc_search_from_pos(string, @noninc_history_pos + dir, dir)
  if (pos == -1)
    # Search failed, current history position unchanged.
    rl_maybe_unsave_line()
    rl_clear_message()
    @rl_point = 0
    rl_ding()
    return 0
  end

  @noninc_history_pos = pos

  oldpos = where_history()
  history_set_pos(@noninc_history_pos)
  entry = current_history()
  if (@rl_editing_mode != @vi_mode)
    history_set_pos(oldpos)
  end
  make_history_line_current(entry)
  @rl_point = 0
  @rl_mark = @rl_end
  rl_clear_message()
  1
end

.noninc_search(dir, pchar) ⇒ Object

Search non-interactively through the history list. DIR < 0 means to

search backwards through the history of previous commands; otherwise
the search is for commands subsequent to the current position in the
history list.  PCHAR is the character to use for prompting when reading
the search string; if not specified (0), it defaults to `:'.

8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
# File 'lib/rbreadline.rb', line 8025

def noninc_search(dir, pchar)
  cxt = _rl_nsearch_init(dir, pchar)
  if (rl_isstate(RL_STATE_CALLBACK))
    return (0)
  end
  # Read the search string.
  r = 0
  while (true)
    c = _rl_search_getchar(cxt)
    if (c == 0.chr)
      break
    end
    r = _rl_nsearch_dispatch(cxt, c)
    if (r < 0)
      return 1
    elsif (r == 0)
      break
    end
  end

  r = _rl_nsearch_dosearch(cxt)
  (r >= 0) ? _rl_nsearch_cleanup(cxt, r) : (r != 1)
end

.noninc_search_from_pos(string, pos, dir) ⇒ Object

Search the history list for STRING starting at absolute history position

POS.  If STRING begins with `^', the search must match STRING at the
beginning of a history line, otherwise a full substring match is performed
for STRING.  DIR < 0 means to search backwards through the history list,
DIR >= 0 means to search forward.

8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
# File 'lib/rbreadline.rb', line 8114

def noninc_search_from_pos(string, pos, dir)
  return 1 if (pos < 0)

  old = where_history()
  return -1 if (history_set_pos(pos) == 0)

  rl_setstate(RL_STATE_SEARCH)
  if (string[0,1] == '^')
    ret = history_search_prefix(string + 1, dir)
  else
    ret = history_search(string, dir)
  end
  rl_unsetstate(RL_STATE_SEARCH)

  if (ret != -1)
    ret = where_history()
  end
  history_set_pos(old)
  ret
end

.parser_else(args) ⇒ Object

Invert the current parser state if there is anything on the stack.


2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
# File 'lib/rbreadline.rb', line 2191

def parser_else(args)
  if @if_stack.empty?
    #_rl_init_file_error ("$else found without matching $if")
    return 0
  end

  # Check the previous (n) levels of the stack to make sure that
  #   we haven't previously turned off parsing.
  return 0 if @if_stack.detect {|x| x }

  # Invert the state of parsing if at top level.
  @_rl_parsing_conditionalized_out = !@_rl_parsing_conditionalized_out
  return 0
end

.parser_endif(args) ⇒ Object

Terminate a conditional, popping the value of

_rl_parsing_conditionalized_out from the stack.

2208
2209
2210
2211
2212
2213
2214
2215
# File 'lib/rbreadline.rb', line 2208

def parser_endif(args)
  if (@if_stack.length>0)
    @_rl_parsing_conditionalized_out = @if_stack.pop
  else
    #_rl_init_file_error ("$endif without matching $if")
  end
  0
end

.parser_if(args) ⇒ Object

Push _rl_parsing_conditionalized_out, and set parser state based

on ARGS.

2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
# File 'lib/rbreadline.rb', line 2148

def parser_if(args)
  # Push parser state.
  @if_stack << @_rl_parsing_conditionalized_out

  # If parsing is turned off, then nothing can turn it back on except
  #   for finding the matching endif.  In that case, return right now.
  if @_rl_parsing_conditionalized_out
    return 0
  end

  args.downcase!
  # Handle "$if term=foo" and "$if mode=emacs" constructs.  If this
  #   isn't term=foo, or mode=emacs, then check to see if the first
  #   word in ARGS is the same as the value stored in rl_readline_name.
  if (@rl_terminal_name && args =~ /^term=/)
    # Terminals like "aaa-60" are equivalent to "aaa".
    tname = @rl_terminal_name.downcase.gsub(/-.*$/,'')

    # Test the `long' and `short' forms of the terminal name so that
    #if someone has a `sun-cmd' and does not want to have bindings
    #that will be executed if the terminal is a `sun', they can put
    #`$if term=sun-cmd' into their .inputrc.
    @_rl_parsing_conditionalized_out = (args[5..-1] != tname && args[5..-1] != @rl_terminal_name.downcase)
  elsif args =~ /^mode=/
    if args[5..-1] == "emacs"
      mode = @emacs_mode
    elsif args[5..-1] == "vi"
      mode = @vi_mode
    else
      mode = @no_mode
    end
  @_rl_parsing_conditionalized_out = (mode != @rl_editing_mode)
  # Check to see if the first word in ARGS is the same as the
  #   value stored in rl_readline_name.
  elsif (args == @rl_readline_name)
    @_rl_parsing_conditionalized_out = false
  else
    @_rl_parsing_conditionalized_out = true
  end
  return 0
end

.parser_include(args) ⇒ Object


2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
# File 'lib/rbreadline.rb', line 2217

def parser_include(args)
  return 0 if (@_rl_parsing_conditionalized_out)

  old_init_file = @current_readline_init_file
  old_line_number = @current_readline_init_lineno
  old_include_level = @current_readline_init_include_level

  r = _rl_read_init_file(args, old_include_level + 1)

  @current_readline_init_file = old_init_file
  @current_readline_init_lineno = old_line_number
  @current_readline_init_include_level = old_include_level

  return r
end

.path_isdir(filename) ⇒ Object


6555
6556
6557
# File 'lib/rbreadline.rb', line 6555

def path_isdir(filename)
  return File.directory?(filename)
end

.postprocess_matches(matchesp, matching_filenames) ⇒ Object


6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
# File 'lib/rbreadline.rb', line 6353

def postprocess_matches(matchesp, matching_filenames)
  matches = matchesp

  return 0 if matches.nil?

  # It seems to me that in all the cases we handle we would like
  #   to ignore duplicate possiblilities.  Scan for the text to
  #   insert being identical to the other completions.
  if (@rl_ignore_completion_duplicates)
    remove_duplicate_matches(matches)
  end

  # If we are matching filenames, then here is our chance to
  #   do clever processing by re-examining the list.  Call the
  #   ignore function with the array as a parameter.  It can
  #   munge the array, deleting matches as it desires.
  if (@rl_ignore_some_completions_function && matching_filenames)
    nmatch = matches.length
    send(@rl_ignore_some_completions_function,matches)
    if (matches.nil? || matches[0].nil?)
      matches = nil
      return 0
    else
      # If we removed some matches, recompute the common prefix.
      i = matches.length
      if (i > 1 && i < nmatch)
        t = matches[0]
        compute_lcd_of_matches(matches, i - 1, t)
      end
    end
  end

  matchesp = matches
  1
end

.prepare_terminal_settings(meta_flag) ⇒ Object


6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
# File 'lib/rbreadline.rb', line 6999

def prepare_terminal_settings(meta_flag)
  retry_if_interrupted do
    @readline_echoing_p = (`stty -a`.scan(/-*echo\b/).first == 'echo')
  end

  # First, the basic settings to put us into character-at-a-time, no-echo
  #   input mode.
  setting = " -echo -icrnl cbreak"

  # If this terminal doesn't care how the 8th bit is used, then we can
  #   use it for the meta-key.  If only one of even or odd parity is
  #  specified, then the terminal is using parity, and we cannot.
  retry_if_interrupted do
    if (`stty -a`.scan(/-parenb\b/).first == '-parenb')
      setting << " pass8"
    end
  end

  setting << " -ixoff"

  rl_bind_key(@_rl_tty_chars.t_start, :rl_restart_output) unless @_rl_tty_chars.t_start.nil?
  @_rl_eof_char = @_rl_tty_chars.t_eof

  #setting << " -isig"

  retry_if_interrupted do
    `stty #{setting}`
  end
end

.previous_historyObject

Back up history_offset to the previous history entry, and return

a pointer to that entry.  If there is no previous entry then return
a NULL pointer.

5438
5439
5440
# File 'lib/rbreadline.rb', line 5438

def previous_history()
  @history_offset!=0 ? @the_history[@history_offset-=1] : nil
end

Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we

are using it, check for and output a single character for `special'
filenames.  Return the number of characters we output.

6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
# File 'lib/rbreadline.rb', line 6584

def print_filename(to_print, full_pathname)
  printed_len = fnprint(to_print)

  if (@rl_filename_completion_desired && (@rl_visible_stats || @_rl_complete_mark_directories))

    # If to_print != full_pathname, to_print is the basename of the
    # path passed.  In this case, we try to expand the directory
    # name before checking for the stat character.
    if (to_print != full_pathname)

      if full_pathname.nil? || full_pathname.length==0
        dn = '/'
      else
        dn = File.dirname(full_pathname)
      end
      s = File.expand_path(dn)
      if (@rl_directory_completion_hook)
        send(@rl_directory_completion_hook,s)
      end

      slen = s.length
      new_full_pathname = s.dup
      if (s[-1,1] == '/' )
        slen-=1
      else
        new_full_pathname[slen,1] = '/'
      end
      new_full_pathname[slen .. -1] = '/' + to_print

      if (@rl_visible_stats)
        extension_char = stat_char(new_full_pathname)
      else
        if (path_isdir(new_full_pathname))
          extension_char = '/'
        end
      end

      new_full_pathname = nil

    else

      s = File.expand_path(full_pathname)
      if (@rl_visible_stats)
        extension_char = stat_char(s)
      else
        if (path_isdir(s))
          extension_char = '/'
        end
      end
    end
    s = nil
    if (extension_char)
      @rl_outstream.write(extension_char)
      printed_len+=1
    end
  end

  printed_len
end

.printable_part(pathname) ⇒ Object

Return the portion of PATHNAME that should be output when listing

possible completions.  If we are hacking filename completion, we
are only interested in the basename, the portion following the
final slash.  Otherwise, we return what we were passed.  Since
printing empty strings is not very informative, if we're doing
filename completion, and the basename is the empty string, we look
for the previous slash and return the portion following that.  If
there's no previous slash, we just return what we were passed.

6492
6493
6494
6495
6496
6497
6498
6499
6500
# File 'lib/rbreadline.rb', line 6492

def printable_part(pathname)
  if (!@rl_filename_completion_desired)  # don't need to do anything
    return (pathname)
  end

  temp = pathname.rindex('/')
  return pathname if temp.nil?
  File.basename(pathname)
end

.prompt_ending_indexObject

  • _rl_last_c_pos is an absolute cursor position in multibyte locales and a buffer index in others. This macro is used when deciding whether the current cursor position is in the middle of a prompt string containing invisible characters.


2618
2619
2620
2621
2622
2623
2624
# File 'lib/rbreadline.rb', line 2618

def prompt_ending_index()
  if !@rl_byte_oriented
    @prompt_physical_chars
  else
    (@prompt_last_invisible+1)
  end
end

.readline(prompt) ⇒ Object

Read a line of input. Prompt with PROMPT. An empty PROMPT means

none.  A return value of NULL means that EOF was encountered.

4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
# File 'lib/rbreadline.rb', line 4859

def readline(prompt)
  # If we are at EOF return a NULL string.
  if (@rl_pending_input == EOF)
    rl_clear_pending_input()
    return nil
  end

  rl_set_prompt(prompt)

  rl_initialize()
  @readline_echoing_p = true
  if (@rl_prep_term_function)
    send(@rl_prep_term_function,@_rl_meta_flag)
  end
  rl_set_signals()

  value = readline_internal()
  if(@rl_deprep_term_function)
    send(@rl_deprep_term_function)
  end

  rl_clear_signals()

  value
end

.readline_default_bindingsObject

If this system allows us to look at the values of the regular

input editing characters, then bind them to their readline
equivalents, iff the characters are not bound to keymaps.

2073
2074
2075
2076
2077
# File 'lib/rbreadline.rb', line 2073

def readline_default_bindings()
  if @_rl_bind_stty_chars
    rl_tty_set_default_bindings(@_rl_keymap)
  end
end

.readline_initialize_everythingObject

Initialize the entire state of the world.


2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
# File 'lib/rbreadline.rb', line 2547

def readline_initialize_everything()
  # Set up input and output if they are not already set up.
  @rl_instream ||= $stdin

  @rl_outstream ||= $stdout

  # Bind _rl_in_stream and _rl_out_stream immediately.  These values
  #   may change, but they may also be used before readline_internal ()
  #   is called.
  @_rl_in_stream = @rl_instream
  @_rl_out_stream = @rl_outstream

  # Allocate data structures.
  @rl_line_buffer = ""

  # Initialize the terminal interface.
  @rl_terminal_name ||= ENV["TERM"]
  _rl_init_terminal_io(@rl_terminal_name)

  # Bind tty characters to readline functions.
  readline_default_bindings()

  # Decide whether we should automatically go into eight-bit mode.
  _rl_init_eightbit()

  # Read in the init file.
  rl_read_init_file(nil)

  # XXX
  if (@_rl_horizontal_scroll_mode && @_rl_term_autowrap)
    @_rl_screenwidth -= 1
    @_rl_screenchars -= @_rl_screenheight
  end

  # Override the effect of any `set keymap' assignments in the
  #   inputrc file.
  rl_set_keymap_from_edit_mode()

  # Try to bind a common arrow key prefix, if not already bound.
  bind_arrow_keys()

  # Enable the meta key, if this terminal has one.
  if @_rl_enable_meta
    _rl_enable_meta_key()
  end

  # If the completion parser's default word break characters haven't
  #   been set yet, then do so now.
  @rl_completer_word_break_characters ||= @rl_basic_word_break_characters
end

.readline_internalObject

Read a line of input from the global rl_instream, doing output on

the global rl_outstream.
If rl_prompt is non-null, then that is our prompt.

4851
4852
4853
4854
4855
# File 'lib/rbreadline.rb', line 4851

def readline_internal()
  readline_internal_setup()
  eof = readline_internal_charloop()
  readline_internal_teardown(eof)
end

.readline_internal_charloopObject


4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
# File 'lib/rbreadline.rb', line 4740

def readline_internal_charloop()
  lastc = -1
  eof_found = false

  while (!@rl_done)
    lk = @_rl_last_command_was_kill

    #  send(rl_redisplay_function)
    #  @_rl_want_redisplay = false

    if (@rl_pending_input == 0)
      # Then initialize the argument and number of keys read.
      _rl_reset_argument()
      @rl_key_sequence_length = 0
    end

    rl_setstate(RL_STATE_READCMD)
    c = rl_read_key()
    rl_unsetstate(RL_STATE_READCMD)
    # look at input.c:rl_getc() for the circumstances under which this will
    #be returned; punt immediately on read error without converting it to
    #a newline.
    if (c == READERR)
      eof_found = true
      break
    end

    # EOF typed to a non-blank line is a <NL>.
    if (c == EOF && @rl_end!=0)
      c = NEWLINE
    end

    # The character _rl_eof_char typed to blank line, and not as the
    #previous character is interpreted as EOF.
    if (((c == @_rl_eof_char && lastc != c) || c == EOF) && @rl_end==0)
      eof_found = true
      break
    end
    lastc = c
    if _rl_dispatch(c, @_rl_keymap)== -1
      next
    end

    # If there was no change in _rl_last_command_was_kill, then no kill
    #has taken place.  Note that if input is pending we are reading
    #a prefix command, so nothing has changed yet.
    if (@rl_pending_input == 0 && lk == @_rl_last_command_was_kill)
      @_rl_last_command_was_kill = false
    end
    _rl_internal_char_cleanup()
  end

  eof_found
end

.readline_internal_setupObject


3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
# File 'lib/rbreadline.rb', line 3982

def readline_internal_setup()
  @_rl_in_stream = @rl_instream
  @_rl_out_stream = @rl_outstream

  if (@rl_startup_hook)
    send(@rl_startup_hook)
  end

  # If we're not echoing, we still want to at least print a prompt, because
  #   rl_redisplay will not do it for us.  If the calling application has a
  #   custom redisplay function, though, let that function handle it.
  if (!@readline_echoing_p && @rl_redisplay_function == :rl_redisplay)
    if (@rl_prompt && !@rl_already_prompted)
      nprompt = _rl_strip_prompt(@rl_prompt)
      @_rl_out_stream.write(nprompt)
      @_rl_out_stream.flush
    end
  else
    if (@rl_prompt && @rl_already_prompted)
      rl_on_new_line_with_prompt()
    else
      rl_on_new_line()
    end
    send(@rl_redisplay_function)
  end

  if (@rl_editing_mode == @vi_mode)
    rl_vi_insertion_mode(1, 'i')
  end
  if (@rl_pre_input_hook)
    send(@rl_pre_input_hook)
  end
end

.readline_internal_teardown(eof) ⇒ Object


4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
# File 'lib/rbreadline.rb', line 4822

def readline_internal_teardown(eof)
  # Restore the original of this history line, iff the line that we
  #   are editing was originally in the history, AND the line has changed.
  entry = current_history()

  if (entry && @rl_undo_list)
    temp = @rl_line_buffer.delete(0.chr).dup
    rl_revert_line(1, 0)
    entry = replace_history_entry(where_history(), @rl_line_buffer, nil)
    entry = nil

    @rl_line_buffer = temp+0.chr
    temp = nil
  end

  # At any rate, it is highly likely that this line has an undo list.  Get
  #   rid of it now.
  if (@rl_undo_list)
    rl_free_undo_list()
  end
  # Restore normal cursor, if available.
  _rl_set_insert_mode(RL_IM_INSERT, 0)

  (eof ? nil : @rl_line_buffer.delete(0.chr))
end

.redraw_prompt(t) ⇒ Object

Redraw the last line of a multi-line prompt that may possibly contain terminal escape sequences. Called with the cursor at column 0 of the line to draw the prompt on.


8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
# File 'lib/rbreadline.rb', line 8836

def redraw_prompt(t)
  oldp = @rl_display_prompt
  rl_save_prompt()

  @rl_display_prompt = t
  @local_prompt,@prompt_visible_length,@prompt_last_invisible,@prompt_invis_chars_first_line,@prompt_physical_chars =
    expand_prompt(t)
  @local_prompt_prefix = nil
  @local_prompt_len = @local_prompt ? @local_prompt.length : 0

  rl_forced_update_display()

  @rl_display_prompt = oldp
  rl_restore_prompt()
end

.release_sigintObject


6948
6949
6950
6951
6952
# File 'lib/rbreadline.rb', line 6948

def release_sigint()
  return if !@sigint_blocked
  Signal.trap("INT", @sigint_proc)
  @sigint_blocked = false
end

.remove_duplicate_matches(matches) ⇒ Object

Filter out duplicates in MATCHES. This frees up the strings in

MATCHES.

6343
6344
6345
6346
6347
6348
6349
6350
6351
# File 'lib/rbreadline.rb', line 6343

def remove_duplicate_matches(matches)
  # Sort the items.
  # Sort the array without matches[0], since we need it to
  #   stay in place no matter what.
  if matches.length>0
    matches[1..-2] = matches[1..-2].sort.uniq
  end
  matches
end

.remove_history(which) ⇒ Object

Remove history element WHICH from the history. The removed

element is returned to you so you can free the line, data,
and containing structure.

6932
6933
6934
6935
6936
6937
6938
6939
6940
# File 'lib/rbreadline.rb', line 6932

def remove_history(which)
  if (which < 0 || which >= @history_length || @history_length ==  0 || @the_history.nil?)
    return nil
  end
  return_value = @the_history[which]
  @the_history.delete_at(which)
  @history_length-=1
  return_value
end

.replace_history_data(which, old, new) ⇒ Object

Replace the DATA in the specified history entries, replacing OLD with

NEW.  WHICH says which one(s) to replace:  WHICH == -1 means to replace
all of the history entries where entry->data == OLD; WHICH == -2 means
to replace the `newest' history entry where entry->data == OLD; and
WHICH >= 0 means to replace that particular history entry's data, as
long as it matches OLD.

5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
# File 'lib/rbreadline.rb', line 5058

def replace_history_data(which,old, new)
  new = new.dup if new
  if (which < -2 || which >= @history_length || @history_length == 0 || @the_history.nil?)
    return
  end
  if (which >= 0)
    entry = @the_history[which]
    if (entry && entry.data == old)
      entry.data = new
    end
    return
  end

  last = -1
  for i in 0 ... @history_length
    entry = @the_history[i]
    if entry.nil?
      next
    end
    if (entry.data == old)
      last = i
      if (which == -1)
        entry.data = new
      end
    end
  end
  if (which == -2 && last >= 0)
    entry = @the_history[last]
    entry.data = new  # XXX - we don't check entry->old
  end
end

.replace_history_entry(which, line, data) ⇒ Object

Make the history entry at WHICH have LINE and DATA. This returns

the old entry so you can dispose of the data.  In the case of an
invalid WHICH, a NULL pointer is returned.

5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
# File 'lib/rbreadline.rb', line 5412

def replace_history_entry (which, line, data)
  if (which < 0 || which >= @history_length)
    return nil
  end
  temp = Struct.new(:line,:timestamp,:data).new
  old_value = @the_history[which]
  temp.line = line.delete(0.chr)
  temp.data = data
  temp.timestamp = old_value.timestamp.dup
  @the_history[which] = temp
  old_value
end

.retry_if_interrupted(&block) ⇒ Object


6954
6955
6956
6957
6958
6959
6960
6961
6962
# File 'lib/rbreadline.rb', line 6954

def retry_if_interrupted(&block)
  tries = 0
  begin
    yield block
  rescue Errno::EINTR
    tries += 1
    retry if tries <= 10
  end
end

.rl_abort(count, key) ⇒ Object


4811
4812
4813
# File 'lib/rbreadline.rb', line 4811

def rl_abort(count, key)
  _rl_abort_internal()
end

.rl_add_undo(what, start, _end, text) ⇒ Object

  • Remember how to undo something. Concatenate some undos if that seems right.


4942
4943
4944
4945
4946
# File 'lib/rbreadline.rb', line 4942

def rl_add_undo(what, start, _end, text)
  temp = alloc_undo_entry(what, start, _end, text)
  temp.next = @rl_undo_list
  @rl_undo_list = temp
end

.rl_alphabetic(c) ⇒ Object


5210
5211
5212
5213
5214
5215
5216
5217
# File 'lib/rbreadline.rb', line 5210

def rl_alphabetic(c)
  if c =~ /\w/
    return true
  end

  return !!(@_rl_allow_pathname_alphabetic_chars &&
            @pathname_alphabetic_chars[c])
end

.rl_arrow_keys(count, c) ⇒ Object


5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
# File 'lib/rbreadline.rb', line 5541

def rl_arrow_keys(count, c)
  rl_setstate(RL_STATE_MOREINPUT)
  ch = rl_read_key()
  rl_unsetstate(RL_STATE_MOREINPUT)

  case (ch.upcase)
  when 'A'
    rl_get_previous_history(count, ch)
  when 'B'
    rl_get_next_history(count, ch)
  when 'C'
    rl_forward_byte(count, ch)
  when 'D'
    rl_backward_byte(count, ch)
  else
    rl_ding()
  end
  0
end

.rl_attempted_completion_functionObject

Returns the value of attribute rl_attempted_completion_function


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_attempted_completion_function
  @rl_attempted_completion_function
end

.rl_attempted_completion_function=(value) ⇒ Object

Sets the attribute rl_attempted_completion_function

Parameters:

  • value

    the value to set the attribute rl_attempted_completion_function to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_attempted_completion_function=(value)
  @rl_attempted_completion_function = value
end

.rl_attempted_completion_overObject

Returns the value of attribute rl_attempted_completion_over


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_attempted_completion_over
  @rl_attempted_completion_over
end

.rl_attempted_completion_over=(value) ⇒ Object

Sets the attribute rl_attempted_completion_over

Parameters:

  • value

    the value to set the attribute rl_attempted_completion_over to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_attempted_completion_over=(value)
  @rl_attempted_completion_over = value
end

.rl_backward(count, key) ⇒ Object

Backwards compatibility.


5187
5188
5189
# File 'lib/rbreadline.rb', line 5187

def rl_backward(count, key)
  rl_backward_char(count, key)
end

.rl_backward_byte(count, key) ⇒ Object

Move backward COUNT bytes.


5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
# File 'lib/rbreadline.rb', line 5142

def rl_backward_byte(count, key)
  if (count < 0)
    return (rl_forward_byte(-count, key))
  end
  if (count > 0)
    if (@rl_point < count)
      @rl_point = 0
      rl_ding()
    else
      @rl_point -= count
    end
  end

  if (@rl_point < 0)
    @rl_point = 0
  end
  0
end

.rl_backward_char(count, key) ⇒ Object

Move backward COUNT characters.


5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
# File 'lib/rbreadline.rb', line 5162

def rl_backward_char(count, key)
  if @rl_byte_oriented
    return (rl_backward_byte(count, key))
  end
  if (count < 0)
    return (rl_forward_char(-count, key))
  end

  if (count > 0)
    point = @rl_point
    while (count > 0 && point > 0)
      point = _rl_find_prev_mbchar(@rl_line_buffer, point, MB_FIND_NONZERO)
      count-=1
    end
    if (count > 0)
      @rl_point = 0
      rl_ding()
    else
      @rl_point = point
    end
  end
  0
end

.rl_backward_char_search(count, key) ⇒ Object


7781
7782
7783
# File 'lib/rbreadline.rb', line 7781

def rl_backward_char_search (count, key)
  _rl_char_search(count, BFIND, FFIND)
end

.rl_backward_kill_line(direction, ignore) ⇒ Object

Kill backwards to the start of the line. If DIRECTION is negative, kill

forwards to the line end instead.

7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
# File 'lib/rbreadline.rb', line 7156

def rl_backward_kill_line(direction, ignore)
  if (direction < 0)
    return (rl_kill_line(1, ignore))
  else
    if (@rl_point==0)
      rl_ding()
    else
      orig_point = @rl_point
      rl_beg_of_line(1, ignore)
      if (@rl_point != orig_point)
        rl_kill_text(orig_point, @rl_point)
      end
      if (@rl_editing_mode == @emacs_mode)
        @rl_mark = @rl_point
      end
    end
  end
  0
end

.rl_backward_kill_word(count, ignore) ⇒ Object

Rubout the word before point, placing it on the kill ring.


7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
# File 'lib/rbreadline.rb', line 7750

def rl_backward_kill_word(count, ignore)
  if (count < 0)
    return (rl_kill_word(-count, ignore))
  else
    orig_point = @rl_point
    rl_backward_word(count, ignore)
    if (@rl_point != orig_point)
      rl_kill_text(orig_point, @rl_point)
    end
    if (@rl_editing_mode == @emacs_mode)
      @rl_mark = @rl_point
    end
  end
  0
end

.rl_backward_word(count, key) ⇒ Object

Move backward a word. We do what Emacs does. Handles multibyte chars.


5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
# File 'lib/rbreadline.rb', line 5279

def rl_backward_word(count, key)
  if (count < 0)
    return (rl_forward_word(-count, key))
  end
  while (count>0)
    return 0 if (@rl_point == 0)

    # Like rl_forward_word (), except that we look at the characters
    # just before point.
    _p =  !@rl_byte_oriented ? _rl_find_prev_mbchar(@rl_line_buffer, @rl_point, MB_FIND_NONZERO):(@rl_point-1)
    c = _rl_char_value(@rl_line_buffer, _p)
    if (!_rl_walphabetic(c))
      @rl_point = _p
      while (@rl_point > 0)
        _p = !@rl_byte_oriented ? _rl_find_prev_mbchar(@rl_line_buffer, @rl_point, MB_FIND_NONZERO):(@rl_point-1)
        c = _rl_char_value(@rl_line_buffer, _p)
        if (_rl_walphabetic(c))
          break
        end
        @rl_point = _p
      end
    end
    while (@rl_point>0)
      _p = !@rl_byte_oriented ? _rl_find_prev_mbchar(@rl_line_buffer, @rl_point, MB_FIND_NONZERO):(@rl_point-1)
      c = _rl_char_value(@rl_line_buffer, _p)
      if (!_rl_walphabetic(c))
        break
      else
        @rl_point = _p
      end
    end
    count -= 1
  end
  0
end

.rl_basic_quote_charactersObject

Returns the value of attribute rl_basic_quote_characters


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_basic_quote_characters
  @rl_basic_quote_characters
end

.rl_basic_quote_characters=(value) ⇒ Object

Sets the attribute rl_basic_quote_characters

Parameters:

  • value

    the value to set the attribute rl_basic_quote_characters to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_basic_quote_characters=(value)
  @rl_basic_quote_characters = value
end

.rl_basic_word_break_charactersObject

Returns the value of attribute rl_basic_word_break_characters


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_basic_word_break_characters
  @rl_basic_word_break_characters
end

.rl_basic_word_break_characters=(value) ⇒ Object

Sets the attribute rl_basic_word_break_characters

Parameters:

  • value

    the value to set the attribute rl_basic_word_break_characters to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_basic_word_break_characters=(value)
  @rl_basic_word_break_characters = value
end

.rl_beg_of_line(count, key) ⇒ Object

Move to the beginning of the line.


5192
5193
5194
5195
# File 'lib/rbreadline.rb', line 5192

def rl_beg_of_line(count, key)
  @rl_point = 0
  0
end

.rl_begin_undo_groupObject

Begin a group. Subsequent undos are undone as an atomic operation.


5009
5010
5011
5012
5013
# File 'lib/rbreadline.rb', line 5009

def rl_begin_undo_group()
  rl_add_undo(UNDO_BEGIN, 0, 0, nil)
  @_rl_undo_group_level+=1
  0
end

.rl_beginning_of_history(count, key) ⇒ Object

Meta-< goes to the start of the history.


7916
7917
7918
# File 'lib/rbreadline.rb', line 7916

def rl_beginning_of_history(count, key)
  rl_get_previous_history(1 + where_history(), key)
end

.rl_bind_key(key, function) ⇒ Object

Bind KEY to FUNCTION. Returns non-zero if KEY is out of range.


2441
2442
2443
2444
2445
# File 'lib/rbreadline.rb', line 2441

def rl_bind_key(key, function)
  @_rl_keymap[rl_translate_keyseq(key)] = function
  @rl_binding_keymap = @_rl_keymap
  0
end

.rl_bind_keyseq_if_unbound(keyseq, default_func) ⇒ Object


1948
1949
1950
# File 'lib/rbreadline.rb', line 1948

def rl_bind_keyseq_if_unbound(keyseq, default_func)
  rl_bind_keyseq_if_unbound_in_map(keyseq, default_func, @_rl_keymap)
end

.rl_bind_keyseq_if_unbound_in_map(keyseq, default_func, kmap) ⇒ Object

Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right

now, this is always used to attempt to bind the arrow keys, hence the
check for rl_vi_movement_mode.

1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
# File 'lib/rbreadline.rb', line 1936

def rl_bind_keyseq_if_unbound_in_map(keyseq, default_func, kmap)
  if (keyseq)
    func = rl_function_of_keyseq(keyseq, kmap, nil)
    if (func.nil? || func == :rl_vi_movement_mode)
      return (rl_bind_keyseq_in_map(keyseq, default_func, kmap))
    else
      return 1
    end
  end
  0
end

.rl_bind_keyseq_in_map(keyseq, function, map) ⇒ Object

Bind the key sequence represented by the string KEYSEQ to

FUNCTION.  This makes new keymaps as necessary.  The initial
place to do bindings is in MAP.

1928
1929
1930
# File 'lib/rbreadline.rb', line 1928

def rl_bind_keyseq_in_map(keyseq, function, map)
  rl_generic_bind(ISFUNC, keyseq, function, map)
end

.rl_capitalize_word(count, key) ⇒ Object

Upcase the first letter, downcase the rest.


7939
7940
7941
# File 'lib/rbreadline.rb', line 7939

def rl_capitalize_word(count, key)
  rl_change_case(count, CapCase)
end

.rl_change_case(count, op) ⇒ Object

The meaty function.

Change the case of COUNT words, performing OP on them.
OP is one of UpCase, DownCase, or CapCase.
If a negative argument is given, leave point where it started,
otherwise, leave it where it moves to.

7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
# File 'lib/rbreadline.rb', line 7964

def rl_change_case(count, op)
  start = @rl_point
  rl_forward_word(count, 0)
  _end = @rl_point

  if (op != UpCase && op != DownCase && op != CapCase)
    rl_ding()
    return -1
  end

  if (count < 0)
    start,_end = _end,start
  end

  # We are going to modify some text, so let's prepare to undo it.
  rl_modifying(start, _end)

  inword = false
  while (start < _end)
    c = _rl_char_value(@rl_line_buffer, start)
    #  This assumes that the upper and lower case versions are the same width.
    if !@rl_byte_oriented
      _next = _rl_find_next_mbchar(@rl_line_buffer, start, 1, MB_FIND_NONZERO)
    else
      _next = start + 1
    end

    if (!_rl_walphabetic(c))
      inword = false
      start = _next
      next
    end

    if (op == CapCase)
      nop = inword ? DownCase : UpCase
      inword = true
    else
      nop = op
    end
    if (isascii(c))
      nc = (nop == UpCase) ? c.upcase : c.downcase
      @rl_line_buffer[start] = nc
    end

    start = _next
  end

  @rl_point = _end
  0
end

.rl_char_search(count, key) ⇒ Object


7651
7652
7653
# File 'lib/rbreadline.rb', line 7651

def rl_char_search(count, key)
  _rl_char_search(count, FFIND, BFIND)
end

.rl_character_len(c, pos) ⇒ Object


5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
# File 'lib/rbreadline.rb', line 5793

def rl_character_len(c, pos)
  if (meta_char(c))
    return ((!@_rl_output_meta_chars) ? 4 : 1)
  end
  if (c == "\t")
    return (((pos | 7) + 1) - pos)
  end
  if (ctrl_char(c) || c == RUBOUT)
    return (2)
  end

  return ((isprint(c)) ? 1 : 2)
end

.rl_cleanup_after_signalObject

Clean up the terminal and readline state after catching a signal, before

resending it to the calling application.

1509
1510
1511
1512
1513
1514
1515
1516
# File 'lib/rbreadline.rb', line 1509

def rl_cleanup_after_signal()
  _rl_clean_up_for_exit()
  if (@rl_deprep_term_function)
    send(@rl_deprep_term_function)
  end
  rl_clear_pending_input()
  rl_clear_signals()
end

.rl_clear_messageObject

How to clear things from the “echo-area”.


4301
4302
4303
4304
4305
4306
4307
4308
4309
# File 'lib/rbreadline.rb', line 4301

def rl_clear_message()
  @rl_display_prompt = @rl_prompt
  if (@msg_saved_prompt)
    rl_restore_prompt()
    @msg_saved_prompt = nil
  end
  send(@rl_redisplay_function)
  0
end

.rl_clear_pending_inputObject

Clear any pending input pushed with rl_execute_next()


1648
1649
1650
1651
1652
# File 'lib/rbreadline.rb', line 1648

def rl_clear_pending_input()
  @rl_pending_input = 0
  rl_unsetstate(RL_STATE_INPUTPENDING)
  0
end

.rl_clear_screen(count, key) ⇒ Object

C-l typed to a line without quoting clears the screen, and then reprints

the prompt and the current input line.  Given a numeric arg, redraw only
the current line.

5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
# File 'lib/rbreadline.rb', line 5365

def rl_clear_screen(count, key)
  if (@rl_explicit_arg)
    rl_refresh_line(count, key)
    return 0
  end

  _rl_clear_screen()      # calls termcap function to clear screen
  rl_forced_update_display()
  @rl_display_fixed = true
  0
end

.rl_clear_signalsObject


1658
1659
1660
1661
1662
# File 'lib/rbreadline.rb', line 1658

def rl_clear_signals()
  if Signal.list['WINCH']
    trap "WINCH",@def_proc
  end
end

.rl_complete(ignore, invoking_key) ⇒ Object

Complete the word at or before point. You have supplied the function

that does the initial simple matching selection algorithm (see
rl_completion_matches ()).  The default is to do filename completion.

6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
# File 'lib/rbreadline.rb', line 6893

def rl_complete(ignore, invoking_key)
  if (@rl_inhibit_completion)
    return (_rl_insert_char(ignore, invoking_key))
  elsif (@rl_last_func == :rl_complete && !@completion_changed_buffer)
    return (rl_complete_internal('?'))
  elsif (@_rl_complete_show_all)
    return (rl_complete_internal('!'))
  elsif (@_rl_complete_show_unmodified)
    return (rl_complete_internal('@'))
  else
    return (rl_complete_internal(TAB))
  end
end

.rl_complete_internal(what_to_do) ⇒ Object

Complete the word at or before point.

WHAT_TO_DO says what to do with the completion.
`?' means list the possible completions.
TAB means do standard completion.
`*' means insert all of the possible completions.
`!' means to do standard completion, and list all possible completions if
there is more than one.
`@' means to do standard completion, and list all possible completions if
there is more than one and partial completion is not possible.

6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
# File 'lib/rbreadline.rb', line 6790

def rl_complete_internal(what_to_do)
  rl_setstate(RL_STATE_COMPLETING)
  set_completion_defaults(what_to_do)

  saved_line_buffer = @rl_line_buffer ? @rl_line_buffer.delete(0.chr) : nil
  our_func = @rl_completion_entry_function ?
    @rl_completion_entry_function : :rl_filename_completion_function
  # We now look backwards for the start of a filename/variable word.
  _end = @rl_point
  found_quote = false
  delimiter = 0.chr
  quote_char = 0.chr

  if (@rl_point!=0)
    # This (possibly) changes rl_point.  If it returns a non-zero char,
    #   we know we have an open quote.
    quote_char,found_quote,delimiter = _rl_find_completion_word()
  end

  start = @rl_point
  @rl_point = _end

  text = rl_copy_text(start, _end)
  matches = gen_completion_matches(text, start, _end, our_func, found_quote, quote_char)
  # nontrivial_lcd is set if the common prefix adds something to the word
  #   being completed.
  nontrivial_lcd = !!(matches && text != matches[0])
  text = nil
  if matches.nil?
    rl_ding()
    saved_line_buffer = nil
    @completion_changed_buffer = false
    rl_unsetstate(RL_STATE_COMPLETING)
    return 0
  end

  # If we are matching filenames, the attempted completion function will
  #   have set rl_filename_completion_desired to a non-zero value.  The basic
  #   rl_filename_completion_function does this.
  i = @rl_filename_completion_desired
  if (postprocess_matches(matches, i) == 0)
    rl_ding()
    saved_line_buffer = nil
    @completion_changed_buffer = false
    rl_unsetstate(RL_STATE_COMPLETING)
    return 0
  end

  case (what_to_do)

  when TAB,'!','@'
    # Insert the first match with proper quoting.
    if (matches[0])
      insert_match(matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, quote_char)
    end
  # If there are more matches, ring the bell to indicate.
  # If we are in vi mode, Posix.2 says to not ring the bell.
  # If the `show-all-if-ambiguous' variable is set, display
  # all the matches immediately.  Otherwise, if this was the
  # only match, and we are hacking files, check the file to
  # see if it was a directory.  If so, and the `mark-directories'
  # variable is set, add a '/' to the name.  If not, and we
  # are at the end of the line, then add a space.
  if (matches[1])
    if (what_to_do == '!')
      display_matches(matches)
    elsif (what_to_do == '@')
      if (!nontrivial_lcd)
        display_matches(matches)
      end
    elsif (@rl_editing_mode != @vi_mode)
      rl_ding()   # There are other matches remaining.
    end
  else
    append_to_match(matches[0], delimiter, quote_char, nontrivial_lcd)
  end
  when '*'
    insert_all_matches(matches, start, quote_char)
  when '?'
    display_matches(matches)
  else
    $stderr.write("\r\nreadline: bad value #{what_to_do} for what_to_do in rl_complete\n")
    rl_ding()
    saved_line_buffer = nil
    rl_unsetstate(RL_STATE_COMPLETING)
    return 1
  end

  matches = nil

  # Check to see if the line has changed through all of this manipulation.
  if (saved_line_buffer)
    @completion_changed_buffer = @rl_line_buffer.delete(0.chr) != saved_line_buffer
    saved_line_buffer = nil
  end

  rl_unsetstate(RL_STATE_COMPLETING)
  0
end

.rl_completer_quote_charactersObject

Returns the value of attribute rl_completer_quote_characters


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completer_quote_characters
  @rl_completer_quote_characters
end

.rl_completer_quote_characters=(value) ⇒ Object

Sets the attribute rl_completer_quote_characters

Parameters:

  • value

    the value to set the attribute rl_completer_quote_characters to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completer_quote_characters=(value)
  @rl_completer_quote_characters = value
end

.rl_completer_word_break_charactersObject

Returns the value of attribute rl_completer_word_break_characters


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completer_word_break_characters
  @rl_completer_word_break_characters
end

.rl_completer_word_break_characters=(value) ⇒ Object

Sets the attribute rl_completer_word_break_characters

Parameters:

  • value

    the value to set the attribute rl_completer_word_break_characters to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completer_word_break_characters=(value)
  @rl_completer_word_break_characters = value
end

.rl_completion_append_characterObject

Returns the value of attribute rl_completion_append_character


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completion_append_character
  @rl_completion_append_character
end

.rl_completion_append_character=(value) ⇒ Object

Sets the attribute rl_completion_append_character

Parameters:

  • value

    the value to set the attribute rl_completion_append_character to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_completion_append_character=(value)
  @rl_completion_append_character = value
end

.rl_completion_matches(text, entry_function) ⇒ Object

Return an array of (char *) which is a list of completions for TEXT.

If there are no completions, return a NULL pointer.
The first entry in the returned array is the substitution for TEXT.
The remaining entries are the possible completions.
The array is terminated with a NULL pointer.

ENTRY_FUNCTION is a function of two args, and returns a (char *).
  The first argument is TEXT.
  The second is a state argument it should be zero on the first call, and
  non-zero on subsequent calls.  It returns a NULL pointer to the caller
  when there are no more matches.

1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/rbreadline.rb', line 1293

def rl_completion_matches(text, entry_function)
  matches = 0
  match_list = []
  match_list[1] = nil
  while (string = send(entry_function, text, matches))
    match_list[matches+=1] = string
    match_list[matches+1] = nil
  end

  # If there were any matches, then look through them finding out the
  # lowest common denominator.  That then becomes match_list[0].
  if (matches!=0)
    compute_lcd_of_matches(match_list, matches, text)
  else                # There were no matches.
    match_list = nil
  end
  return (match_list)
end

.rl_copy_text(from, to) ⇒ Object


4983
4984
4985
# File 'lib/rbreadline.rb', line 4983

def rl_copy_text(from, to)
  return @rl_line_buffer[from...to]
end

.rl_crlfObject

Move to the start of the next line.


4691
4692
4693
4694
4695
4696
4697
# File 'lib/rbreadline.rb', line 4691

def rl_crlf()
  if (@_rl_term_cr)
    @_rl_out_stream.write(@_rl_term_cr)
  end
  @_rl_out_stream.write("\n")
  return 0
end

.rl_delete(count, key) ⇒ Object

Delete the character under the cursor. Given a numeric argument,

kill that many characters instead.

5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
# File 'lib/rbreadline.rb', line 5903

def rl_delete(count, key)
  if (count < 0)
    return (_rl_rubout_char(-count, key))
  end
  if (@rl_point == @rl_end)
    rl_ding()
    return -1
  end

  if (count > 1 || @rl_explicit_arg)
    xpoint = @rl_point
    rl_forward_byte(count, key)

    rl_kill_text(xpoint, @rl_point)
    @rl_point = xpoint
  else
    if !@rl_byte_oriented
      xpoint =_rl_find_next_mbchar(@rl_line_buffer, @rl_point, 1, MB_FIND_NONZERO)
    else
      xpoint = @rl_point + 1
    end

    rl_delete_text(@rl_point, xpoint)
  end
  0
end

.rl_delete_horizontal_space(count, ignore) ⇒ Object

Delete all spaces and tabs around point.


6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
# File 'lib/rbreadline.rb', line 6075

def rl_delete_horizontal_space(count, ignore)
  start = @rl_point

  while (@rl_point!=0 && whitespace(@rl_line_buffer[@rl_point - 1]))
    @rl_point-=1
  end
  start = @rl_point
  while (@rl_point < @rl_end && whitespace(@rl_line_buffer[@rl_point]))
    @rl_point+=1
  end
  if (start != @rl_point)
    rl_delete_text(start, @rl_point)
    @rl_point = start
  end
  if (@rl_point < 0)
    @rl_point = 0
  end
  0
end

.rl_delete_or_show_completions(count, key) ⇒ Object

Like the tcsh editing function delete-char-or-list. The eof character

is caught before this is invoked, so this really does the same thing as
delete-char-or-list-or-eof, as long as it's bound to the eof character.

6103
6104
6105
6106
6107
6108
6109
# File 'lib/rbreadline.rb', line 6103

def rl_delete_or_show_completions(count, key)
  if (@rl_end != 0 && @rl_point == @rl_end)
    return (rl_possible_completions(count, key))
  else
    return (rl_delete(count, key))
  end
end

.rl_delete_text(from, to) ⇒ Object

Delete the string between FROM and TO. FROM is inclusive, TO is not.

Returns the number of characters deleted.

4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
# File 'lib/rbreadline.rb', line 4951

def rl_delete_text(from, to)

  # Fix it if the caller is confused.
  if (from > to)
    from,to = to,from
  end

  # fix boundaries
  if (to > @rl_end)
    to = @rl_end
    if (from > to)
      from = to
    end
  end
  if (from < 0)
    from = 0
  end
  text = rl_copy_text(from, to)
  diff = to - from
  @rl_line_buffer[from...to] = ''
  @rl_line_buffer << 0.chr * diff
  # Remember how to undo this delete.
  if (!@_rl_doing_an_undo)
    rl_add_undo(UNDO_DELETE, from, to, text)
  else
    text = nil
  end
  @rl_end -= diff
  @rl_line_buffer[@rl_end,1] = 0.chr
  return (diff)
end

.rl_deprep_term_functionObject

Returns the value of attribute rl_deprep_term_function


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_deprep_term_function
  @rl_deprep_term_function
end

.rl_deprep_term_function=(value) ⇒ Object

Sets the attribute rl_deprep_term_function

Parameters:

  • value

    the value to set the attribute rl_deprep_term_function to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_deprep_term_function=(value)
  @rl_deprep_term_function = value
end

.rl_deprep_terminalObject

Restore the terminal's normal settings and modes.


7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
# File 'lib/rbreadline.rb', line 7099

def rl_deprep_terminal()
  return if ENV["TERM"].nil?
  return if (!@terminal_prepped)

  # Try to keep this function from being interrupted.
  block_sigint()

  if (@_rl_enable_keypad)
    _rl_control_keypad(false)
  end

  @rl_outstream.flush

  # restore terminal setting
  retry_if_interrupted do
    `stty #{@otio}`
  end

  @terminal_prepped = false
  rl_unsetstate(RL_STATE_TERMPREPPED)

  release_sigint()
end

.rl_digit_argument(ignore, key) ⇒ Object

Start a numeric argument with initial value KEY


7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
# File 'lib/rbreadline.rb', line 7896

def rl_digit_argument(ignore, key)
  _rl_arg_init()
  if (rl_isstate(RL_STATE_CALLBACK))
    _rl_arg_dispatch(@_rl_argcxt, key)
    rl_message("(arg: #{@rl_arg_sign * @rl_numeric_arg}) ")
    return 0
  else
    rl_execute_next(key)
    return (rl_digit_loop())
  end
end

.rl_digit_loopObject

Handle C-u style numeric args, as well as M–, and M-digits.


7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
# File 'lib/rbreadline.rb', line 7880

def rl_digit_loop()
  while (true)
    return 1 if _rl_arg_overflow()!=0
    c = _rl_arg_getchar()
    if (c >= "\xFE")
      _rl_abort_internal()
      return -1
    end
    r = _rl_arg_dispatch(@_rl_argcxt, c)
    break if (r <= 0 || !rl_isstate(RL_STATE_NUMERICARG))
  end

  return r
end

.rl_dingObject

Ring the terminal bell.


4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
# File 'lib/rbreadline.rb', line 4024

def rl_ding()
  if @MessageBeep
    @MessageBeep.Call(0)
  elsif @readline_echoing_p
    if @_rl_bell_preference == VISIBLE_BELL
      if (@_rl_visible_bell)
        @_rl_out_stream.write(@_rl_visible_bell.chr)
      else
        $stderr.write("\007")
        $stderr.flush
      end
    elsif @_rl_bell_preference == AUDIBLE_BELL
      $stderr.write("\007")
      $stderr.flush
    end
    return 0
  end
  return -1
end

.rl_display_match_list(matches, len, max) ⇒ Object

A convenience function for displaying a list of strings in

columnar format on readline's output stream.  MATCHES is the list
of strings, in argv format, LEN is the number of strings in MATCHES,
and MAX is the length of the longest string in MATCHES.

8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
# File 'lib/rbreadline.rb', line 8383

def rl_display_match_list(matches, len, max)
  # How many items of MAX length can we fit in the screen window?
  max += 2
  limit = @_rl_screenwidth / max
  if (limit != 1 && (limit * max == @_rl_screenwidth))
    limit-=1
  end
  # Avoid a possible floating exception.  If max > _rl_screenwidth,
  #   limit will be 0 and a divide-by-zero fault will result.
  if (limit == 0)
    limit = 1
  end
  # How many iterations of the printing loop?
  count = (len + (limit - 1)) / limit

  # Watch out for special case.  If LEN is less than LIMIT, then
  #   just do the inner printing loop.
  #     0 < len <= limit  implies  count = 1.

  # Sort the items if they are not already sorted.
  if (!@rl_ignore_completion_duplicates)
    matches[1,len] = matches[1,len].sort
  end
  rl_crlf()

  lines = 0
  if (!@_rl_print_completions_horizontally)
    # Print the sorted items, up-and-down alphabetically, like ls.
    for i in 1 .. count
      l = i
      for j in 0 ... limit
        if (l > len || matches[l].nil?)
          break
        else
          temp = printable_part(matches[l])
          printed_len = print_filename(temp, matches[l])

          if (j + 1 < limit)
            @rl_outstream.write(' '*(max - printed_len))
          end
        end
        l += count
      end
      rl_crlf()
      lines+=1
      if (@_rl_page_completions && lines >= (@_rl_screenheight - 1) && i < count)
        lines = _rl_internal_pager(lines)
        return if (lines < 0)
      end
    end
  else
    # Print the sorted items, across alphabetically, like ls -x.
    i = 1
    while(matches[i])
      temp = printable_part(matches[i])
      printed_len = print_filename(temp, matches[i])
      # Have we reached the end of this line?
      if (matches[i+1])
        if ((limit > 1) && (i % limit) == 0)
          rl_crlf()
          lines+=1
          if (@_rl_page_completions && lines >= @_rl_screenheight - 1)
            lines = _rl_internal_pager(lines)
            return if (lines < 0)
          end
        else
          @rl_outstream.write(' '*(max - printed_len))
        end
      end
      i += 1
    end
    rl_crlf()
  end
end

.rl_display_search(search_string, reverse_p, where) ⇒ Object

Display the current state of the search in the echo-area.

SEARCH_STRING contains the string that is being searched for,
DIRECTION is zero for forward, or non-zero for reverse,
WHERE is the history list number of the current line.  If it is
-1, then this line is the starting one.

7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
# File 'lib/rbreadline.rb', line 7380

def rl_display_search(search_string, reverse_p, where)
  message = '('
  if (reverse_p)
    message << "reverse-"
  end
  message << "i-search)`"

  if (search_string)
    message << search_string
  end
  message << "': "

  rl_message(message)
  message = nil
  send(@rl_redisplay_function)
end

.rl_do_lowercase_version(ignore1, ignore2) ⇒ Object

What to do for some uppercase characters, like meta characters,

and some characters appearing in emacs_ctlx_keymap.  This function
is just a stub, you bind keys to it and the code in _rl_dispatch ()
is special cased.

5789
5790
5791
# File 'lib/rbreadline.rb', line 5789

def rl_do_lowercase_version(ignore1, ignore2)
  0
end

.rl_do_undoObject


7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
# File 'lib/rbreadline.rb', line 7661

def rl_do_undo()
  start = _end = waiting_for_begin = 0
  begin
    return 0 if @rl_undo_list.nil?

    @_rl_doing_an_undo = true
    rl_setstate(RL_STATE_UNDOING)

    # To better support vi-mode, a start or end value of -1 means
    # rl_point, and a value of -2 means rl_end.
    if (@rl_undo_list.what == UNDO_DELETE || @rl_undo_list.what == UNDO_INSERT)
      start = trans(@rl_undo_list.start)
      _end = trans(@rl_undo_list.end)
    end

    case (@rl_undo_list.what)
      # Undoing deletes means inserting some text.
    when UNDO_DELETE
      @rl_point = start
      rl_insert_text(@rl_undo_list.text)
      @rl_undo_list.text = nil

      # Undoing inserts means deleting some text.
    when UNDO_INSERT
      rl_delete_text(start, _end)
      @rl_point = start
      # Undoing an END means undoing everything 'til we get to a BEGIN.
    when UNDO_END
      waiting_for_begin+=1
      # Undoing a BEGIN means that we are done with this group.
    when UNDO_BEGIN
      if (waiting_for_begin!=0)
        waiting_for_begin-=1
      else
        rl_ding()
      end
    end

    @_rl_doing_an_undo = false
    rl_unsetstate(RL_STATE_UNDOING)

    release = @rl_undo_list
    @rl_undo_list = @rl_undo_list.next
    replace_history_data(-1, release, @rl_undo_list)
    release = nil
  end while (waiting_for_begin!=0)

  1
end

.rl_downcase_word(count, key) ⇒ Object

Lowercase the word at point.


7934
7935
7936
# File 'lib/rbreadline.rb', line 7934

def rl_downcase_word(count, key)
  rl_change_case(count, DownCase)
end

.rl_emacs_editing_mode(count, key) ⇒ Object


1441
1442
1443
1444
1445
1446
# File 'lib/rbreadline.rb', line 1441

def rl_emacs_editing_mode(count, key)
  @rl_editing_mode = @emacs_mode
  _rl_set_insert_mode(RL_IM_INSERT, 1) # emacs mode default is insert mode
  @_rl_keymap = @emacs_standard_keymap
  0
end

.rl_end_of_history(count, key) ⇒ Object

Meta-> goes to the end of the history. (The current line).


7921
7922
7923
7924
7925
7926
# File 'lib/rbreadline.rb', line 7921

def rl_end_of_history(count, key)
  rl_maybe_replace_line()
  using_history()
  rl_maybe_unsave_line()
  0
end

.rl_end_of_line(count, key) ⇒ Object

Move to the end of the line.


5198
5199
5200
5201
# File 'lib/rbreadline.rb', line 5198

def rl_end_of_line(count, key)
  @rl_point = @rl_end
  0
end

.rl_end_undo_groupObject

End an undo group started with rl_begin_undo_group ().


5016
5017
5018
5019
5020
# File 'lib/rbreadline.rb', line 5016

def rl_end_undo_group()
  rl_add_undo(UNDO_END, 0, 0, nil)
  @_rl_undo_group_level-=1
  0
end

.rl_event_hookObject

Returns the value of attribute rl_event_hook


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_event_hook
  @rl_event_hook
end

.rl_event_hook=(value) ⇒ Object

Sets the attribute rl_event_hook

Parameters:

  • value

    the value to set the attribute rl_event_hook to.


1106
1107
1108
# File 'lib/rbreadline.rb', line 1106

def rl_event_hook=(value)
  @rl_event_hook = value
end

.rl_exchange_point_and_mark(count, key) ⇒ Object

Exchange the position of mark and point.


8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
# File 'lib/rbreadline.rb', line 8366

def rl_exchange_point_and_mark(count, key)
  if (@rl_mark > @rl_end)
    @rl_mark = -1
  end
  if (@rl_mark == -1)
    rl_ding()
    return -1
  else
    @rl_point, @rl_mark = @rl_mark, @rl_point
  end
  0
end

.rl_execute_next(c) ⇒ Object

Make C be the next command to be executed.


7909
7910
7911
7912
7913
# File 'lib/rbreadline.rb', line 7909

def rl_execute_next(c)
  @rl_pending_input = c
  rl_setstate(RL_STATE_INPUTPENDING)
  0
end

.rl_expand_prompt(prompt) ⇒ Object

*

  • Expand the prompt string into the various display components, if

  • necessary.

*

  • local_prompt = expanded last line of string in rl_display_prompt

  • (portion after the final newline)

  • local_prompt_prefix = portion before last newline of rl_display_prompt,

  • expanded via expand_prompt

  • prompt_visible_length = number of visible characters in local_prompt

  • prompt_prefix_length = number of visible characters in local_prompt_prefix

*

  • This function is called once per call to readline(). It may also be

  • called arbitrarily to expand the primary prompt.

*

  • The return value is the number of visible characters on the last line

  • of the (possibly multi-line) prompt.

*


1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
# File 'lib/rbreadline.rb', line 1771

def rl_expand_prompt(prompt)
  @local_prompt = @local_prompt_prefix = nil
  @local_prompt_len = 0
  @prompt_last_invisible = @prompt_invis_chars_first_line = 0
  @prompt_visible_length = @prompt_physical_chars = 0

  if (prompt.nil? || prompt == '')
    return (0)
  end

  pi = prompt.rindex("\n")
  if pi.nil?
    # The prompt is only one logical line, though it might wrap.
    @local_prompt,@prompt_visible_length,@prompt_last_invisible,@prompt_invis_chars_first_line,@prompt_physical_chars = expand_prompt(prompt)
    @local_prompt_prefix = nil
    @local_prompt_len = @local_prompt ? @local_prompt.length : 0
    return (@prompt_visible_length)
  else
    # The prompt spans multiple lines.
    pi += 1 if prompt.length!=pi+1
    t = pi
    @local_prompt,@prompt_visible_length,@prompt_last_invisible,@prompt_invis_chars_first_line,@prompt_physical_chars = expand_prompt(prompt[pi..-1])
    c = prompt[t]
    prompt[t] = 0.chr
    # The portion of the prompt string up to and including the
    #final newline is now null-terminated.
    @local_prompt_prefix,@prompt_prefix_length,_,_, = expand_prompt(prompt)
    prompt[t] = c
    @local_prompt_len = @local_prompt ? @local_prompt.length : 0
    return (@prompt_prefix_length)
  end
end

.rl_extend_line_buffer(len) ⇒ Object

Increase the size of RL_LINE_BUFFER until it has enough space to hold

LEN characters.

4887
4888
4889
4890
4891
4892
# File 'lib/rbreadline.rb', line 4887

def rl_extend_line_buffer(len)
  while (len >= @rl_line_buffer.length)
    @rl_line_buffer << 0.chr * DEFAULT_BUFFER_SIZE
  end
  @the_line = @rl_line_buffer
end

.rl_filename_completion_function(text, state) ⇒ Object

Okay, now we write the entry_function for filename completion. In the general case. Note that completion in the shell is a little different because of all the pathnames that must be followed when looking up the completion for a command.


1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'lib/rbreadline.rb', line 1119

def rl_filename_completion_function(text, state)
  # If we don't have any state, then do some initialization.
  if (state == 0)
    # If we were interrupted before closing the directory or reading
    #all of its contents, close it.
    if(@directory)
      @directory.close
      @directory = nil
    end

    text.delete!(0.chr)
    if text.length == 0
      @dirname = "."
      @filename = ""
    elsif text.rindex(File::SEPARATOR) == text.length-1
      @dirname = text
      @filename = ""
    else
      @dirname, @filename = File.split(text)

      # This preserves the "./" when the user types "./dirname<tab>".
      if @dirname == "." && text[0,2] == ".#{File::SEPARATOR}"
        @dirname += File::SEPARATOR
      end
    end

    # We aren't done yet.  We also support the "~user" syntax.

    # Save the version of the directory that the user typed.
    @users_dirname = @dirname.dup

    if (@dirname[0,1] == '~')
      @dirname = File.expand_path(@dirname)
    end

    # The directory completion hook should perform any necessary
    #   dequoting.