~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/irb/ext/history.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#   history.rb - 
 
3
#       $Release Version: 0.9.5$
 
4
#       $Revision: 11708 $
 
5
#       $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
 
6
#       by Keiju ISHITSUKA(keiju@ruby-lang.org)
 
7
#
 
8
# --
 
9
#
 
10
#   
 
11
#
 
12
 
 
13
module IRB
 
14
 
 
15
  class Context
 
16
 
 
17
    NOPRINTING_IVARS.push "@eval_history_values"
 
18
 
 
19
    alias _set_last_value set_last_value
 
20
 
 
21
    def set_last_value(value)
 
22
      _set_last_value(value)
 
23
 
 
24
#      @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
 
25
      if @eval_history #and !@eval_history_values.equal?(llv)
 
26
        @eval_history_values.push @line_no, @last_value
 
27
        @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
 
28
      end
 
29
 
 
30
      @last_value
 
31
    end
 
32
 
 
33
    attr_reader :eval_history
 
34
    def eval_history=(no)
 
35
      if no
 
36
        if defined?(@eval_history) && @eval_history
 
37
          @eval_history_values.size(no)
 
38
        else
 
39
          @eval_history_values = History.new(no)
 
40
          IRB.conf[:__TMP__EHV__] = @eval_history_values
 
41
          @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
 
42
          IRB.conf.delete(:__TMP_EHV__)
 
43
        end
 
44
      else
 
45
        @eval_history_values = nil
 
46
      end
 
47
      @eval_history = no
 
48
    end
 
49
  end
 
50
 
 
51
  class History
 
52
    @RCS_ID='-$Id: history.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
 
53
 
 
54
    def initialize(size = 16)
 
55
      @size = size
 
56
      @contents = []
 
57
    end
 
58
 
 
59
    def size(size)
 
60
      if size != 0 && size < @size 
 
61
        @contents = @contents[@size - size .. @size]
 
62
      end
 
63
      @size = size
 
64
    end
 
65
 
 
66
    def [](idx)
 
67
      begin
 
68
        if idx >= 0
 
69
          @contents.find{|no, val| no == idx}[1]
 
70
        else
 
71
          @contents[idx][1]
 
72
        end
 
73
      rescue NameError
 
74
        nil
 
75
      end
 
76
    end
 
77
 
 
78
    def push(no, val)
 
79
      @contents.push [no, val]
 
80
      @contents.shift if @size != 0 && @contents.size > @size
 
81
    end
 
82
    
 
83
    alias real_inspect inspect
 
84
 
 
85
    def inspect
 
86
      if @contents.empty?
 
87
        return real_inspect
 
88
      end
 
89
 
 
90
      unless (last = @contents.pop)[1].equal?(self)
 
91
        @contents.push last
 
92
        last = nil
 
93
      end
 
94
      str = @contents.collect{|no, val|
 
95
        if val.equal?(self)
 
96
          "#{no} ...self-history..."
 
97
        else
 
98
          "#{no} #{val.inspect}"
 
99
        end
 
100
      }.join("\n")
 
101
      if str == ""
 
102
        str = "Empty."
 
103
      end
 
104
      @contents.push last if last
 
105
      str
 
106
    end
 
107
  end
 
108
end
 
109
 
 
110