~pyreadline/pyreadline/trunk

« back to all changes in this revision

Viewing changes to pyreadline/lineeditor/history.py

  • Committer: Jorgen Stenarson
  • Date: 2011-04-14 16:52:22 UTC
  • Revision ID: jorgen.stenarson@bostream.nu-20110414165222-i77zmr4fjt6jlf0d
Fix for reverse and forward incremental search

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from pyreadline.logger import log
24
24
 
25
 
_ignore_leading_spaces = False
26
25
 
27
26
class LineHistory(object):
28
27
    def __init__(self):
32
31
        self.history_filename = os.path.expanduser('~/.history') #Cannot expand unicode strings correctly on python2.4
33
32
        self.lastcommand = None
34
33
        self.query = u""
 
34
        self.last_search_for = u""
35
35
 
36
36
    def get_current_history_length(self):
37
37
        u'''Return the number of lines currently in the history.
140
140
    def reverse_search_history(self, searchfor, startpos=None):
141
141
        if startpos is None:
142
142
            startpos = self.history_cursor
143
 
        if _ignore_leading_spaces:
144
 
            res = [(idx, line.lstrip())  
145
 
                    for idx, line in enumerate(self.history[startpos:0:-1]) 
146
 
                        if line.lstrip().startswith(searchfor.lstrip())]
147
 
        else:
148
 
            res = [(idx, line)
149
 
                    for idx, line in enumerate(self.history[startpos:0:-1]) 
150
 
                        if line.startswith(searchfor)]
151
 
        if res:
152
 
            self.history_cursor -= res[0][0]
153
 
            return res[0][1].get_line_text()
154
 
        return u""
 
143
 
 
144
        result =  lineobj.ReadLineTextBuffer("")
 
145
 
 
146
        for idx, line in list(enumerate(self.history))[startpos:0:-1]:
 
147
            if searchfor in line:
 
148
                startpos = idx
 
149
                break
 
150
 
 
151
        #If we get a new search without change in search term it means
 
152
        #someone pushed ctrl-r and we should find the next match
 
153
        if self.last_search_for == searchfor:
 
154
            startpos -= 1
 
155
            for idx, line in list(enumerate(self.history))[startpos:0:-1]:
 
156
                if searchfor in line:
 
157
                    startpos = idx
 
158
                    break
 
159
 
 
160
        result = self.history[startpos]
 
161
        self.history_cursor = startpos
 
162
        self.last_search_for = searchfor
 
163
        return result.get_line_text()
155
164
        
156
165
    def forward_search_history(self, searchfor, startpos=None):
157
166
        if startpos is None:
158
167
            startpos = self.history_cursor
159
 
        if _ignore_leading_spaces:
160
 
            res = [(idx, line.lstrip()) 
161
 
                    for idx, line in enumerate(self.history[startpos:]) 
162
 
                        if line.lstrip().startswith(searchfor.lstrip())]
 
168
        origpos = startpos
 
169
        
 
170
        result =  lineobj.ReadLineTextBuffer("")
 
171
 
 
172
        for idx, line in list(enumerate(self.history))[startpos:]:
 
173
            if searchfor in line:
 
174
                startpos = idx
 
175
                break
 
176
 
 
177
        #If we get a new search without change in search term it means
 
178
        #someone pushed ctrl-r and we should find the next match
 
179
        if self.last_search_for == searchfor:
 
180
            startpos += 1
 
181
            for idx, line in list(enumerate(self.history))[startpos:]:
 
182
                if searchfor in line:
 
183
                    startpos = idx
 
184
                    break
 
185
        if len(self.history) == startpos:
 
186
            if origpos == len(self.history):
 
187
                return u""
 
188
            else:
 
189
                return self.history[origpos]
163
190
        else:
164
 
            res = [(idx, line) 
165
 
                        for idx, line in enumerate(self.history[startpos:]) 
166
 
                            if line.startswith(searchfor)]
167
 
        if res:
168
 
            self.history_cursor += res[0][0]
169
 
            return res[0][1].get_line_text()
170
 
        return u""
 
191
            result = self.history[startpos]
 
192
        self.history_cursor = startpos
 
193
        self.last_search_for = searchfor
 
194
        return result.get_line_text()
171
195
 
172
196
    def _search(self, direction, partial):
173
197
        try: