summaryrefslogtreecommitdiff
path: root/lib/irb/ext/history.rb
blob: 55cd56de2fddd51263fa59176fcec8c478af3081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
#   history.rb - 
#   	$Release Version: 0.9$
#   	$Revision$
#   	$Date$
#   	by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
#
# --
#
#   
#

module IRB

  class Context

    NOPRINTING_IVARS.push "@eval_history_values"

    alias _set_last_value set_last_value

    def set_last_value(value)
      _set_last_value(value)

      @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
      if (@eval_history ||= nil) #and !@eval_history_values.equal?(llv)
 	@eval_history_values.push @line_no, @last_value
 	@workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
      end

      @last_value
    end

    attr_reader :eval_history
    def eval_history=(no)
      if no
	if (@eval_history ||= nil)
	  @eval_history_values.size(no)
	else
	  @eval_history_values = History.new(no)
	  IRB.conf[:__TMP__EHV__] = @eval_history_values
	  @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
	  IRB.conf.delete(:__TMP_EHV__)
	end
      else
	@eval_history_values = nil
      end
      @eval_history = no
    end
  end

  class History
    @RCS_ID='-$Id$-'

    def initialize(size = 16)
      @size = size
      @contents = []
    end

    def size(size)
      if size != 0 && size < @size 
	@contents = @contents[@size - size .. @size]
      end
      @size = size
    end

    def [](idx)
      begin
	if idx >= 0
	  @contents.find{|no, val| no == idx}[1]
	else
	  @contents[idx][1]
	end
      rescue NameError
	nil
      end
    end

    def push(no, val)
      @contents.push [no, val]
      @contents.shift if @size != 0 && @contents.size > @size
    end
    
    alias real_inspect inspect

    def inspect
      if @contents.empty?
	return real_inspect
      end

      unless (last = @contents.pop)[1].equal?(self)
	@contents.push last
	last = nil
      end
      str = @contents.collect{|no, val|
	if val.equal?(self)
	  "#{no} ...self-history..."
	else
	  "#{no} #{val.inspect}"
	end
      }.join("\n")
      if str == ""
	str = "Empty."
      end
      @contents.push last if last
      str
    end
  end
end