Changeset 836

Show
Ignore:
Timestamp:
09/13/05 12:05:34 (3 years ago)
Author:
tzanko
Message:

Fixed tickets #29 and #12. Worked on #11, did some optimizations. I cannot tell the difference on my machine, though.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nbshell/trunk/ChangeLog

    r835 r836  
     12005-09-13  Tzanko Matev  <tsanko@gmail.com> 
     2 
     3    * IPythonLog.py (IPythonLog.filename_iter): Changed generation of 
     4    figure names to avoid 'filename.nbk_1.png' names. This resolves 
     5    ticket #29. 
     6 
     7    * PythonPlugin.py (PythonNotebookViewPlugin):  
     8    * PythonWidget.py (Shell.OnKeyDown): Added history. The user can 
     9    access it using Ctrl-Up/Alt-P, Ctrl-Down/Alt-N. This resolves  
     10    ticket  #12. 
     11 
     12    * PythonPlugin.py (PythonNotebookView.Update, PromptLen, CanEdit): 
     13    Added a variable self.promptlens, which contains the lenghts of 
     14    the prompts and is generated when Update is called. This slightly 
     15    optimizes PromptLen and CanEdit. Added other small optimisations 
     16    to CanEdit. As a result typing should get a bit faster. See ticket 
     17    #11 for more info. 
     18 
     19     
    1202005-09-12  Tzanko Matev  <tsanko@gmail.com> 
    221 
  • nbshell/trunk/nbshell/IPythonLog.py

    r823 r836  
    1313__version__ = Release.version 
    1414 
     15import os.path 
    1516import sys 
    1617import StringIO 
     
    114115        """A generator function used for generating unique figure filenames""" 
    115116        counter = 1 
    116         fn = self.doc.fileinfo['path'] + '/' + self.doc.fileinfo['name'
     117        fn = self.doc.fileinfo['path'] + '/' + os.path.splitext(self.doc.fileinfo['name'])[0
    117118        while True: 
    118119            yield "%s_%d.png"%(fn,counter) 
  • nbshell/trunk/nbshell/PythonPlugin.py

    r835 r836  
    9797        self.view = None    #This plugin is designed for a single view. For 
    9898                            #multiple views there should be some modifications 
    99         print "block:" 
     99        #print "block:" #dbg 
    100100        #etree.dump(self.element) #dbg 
    101101        self.cells = [self.log.log[int(x.attrib['number'])] for x in element] 
     
    185185        self.prompt_in_tpl = 'In [%s]: ' 
    186186        self.prompt_out_tpl = 'Out[%s]: ' 
     187         
     188        self.hist_max_len = 666 #TODO: This must go in some configuration file 
     189        self.history = [] #FIFO of at most self.hist_max_len strings 
     190        self.hist_current = None #Index of the curret history item 
     191         
     192    def add_to_history(self, line = None): 
     193        """Adds the line to the history. If line is None gets the current line 
     194        from the window""" 
     195        if line is None: 
     196            linenum = self.window.GetCurrentLine() 
     197            line = self.window.GetLine(linenum)[self.promptlens[self.line2log[linenum][0]]:] 
     198         
     199        if line: #We don't need empty lines 
     200            self.history.append(line) 
     201            l = len(self.history) 
     202            if l > self.hist_max_len: 
     203                del(self.history[0]) 
     204            self.hist_current = l-1 
     205     
     206    def get_hist_item(self, dir= -1): 
     207        """Gets the next previous line from the history if dir<0 or the next 
     208        line if dir>0. If the history is empty return None""" 
     209 
     210        dir = dir/abs(dir) 
     211        if self.hist_current is None: 
     212            return None 
     213        self.hist_current+=dir 
     214        l = len(self.history) 
     215        if self.hist_current == l: 
     216            self.hist_current = 0 
     217        elif self.hist_current < 0: 
     218            self.hist_current = l-1 
     219         
     220        return self.history[self.hist_current] 
     221     
     222    def hist_replace(self, dir): 
     223        """Replaces the current line with prevoius (dir<0) or next (dir>0) 
     224        line from the history""" 
     225         
     226        t = self.get_hist_item(dir) 
     227        if not t: 
     228            return 
     229         
     230        pos = self.window.GetCurrentPos() 
     231        linenum = self.window.LineFromPosition(pos) 
     232 
     233        if not self.CanEdit(pos = pos, line = linenum): 
     234            return None 
     235 
     236        startpos = self.window.PositionFromLine(linenum) +\ 
     237                 self.promptlens[self.line2log[linenum][0]] 
     238        endpos = self.window.GetLineEndPosition(linenum) 
     239 
     240        self.window.SetTargetStart(startpos) 
     241        self.window.SetTargetEnd(endpos) 
     242        self.window.ReplaceTarget(t) 
     243 
    187244 
    188245    def SetFocus(self): 
     
    307364        # Here we set up the text which will be displayed in the window 
    308365        outtext = StringIO.StringIO() 
     366        #self.promptlen[i] holds the length of the prompt of cell i 
     367        self.promptlens = [] 
    309368        for i, cell in enumerate(cells): 
    310369            number =  cell.number 
     
    348407            for j in range(len(lines)): 
    349408                l2l_append((i, j+1)) 
     409             
     410            #set up self.promptlens 
     411            self.promptlens.append(len(prompt)) 
    350412                #print "i -> %s, id->%s, type->%s, text->%s"%(str(i), str(id), str(type), str(text)) 
    351413            #oldlinecnt = linecnt 
     
    398460        if line is None: #a separator line 
    399461            return 0 
    400          
     462        return self.promptlens[line[0]] 
    401463        #if line[1] != 1: #currently only the first line of a block is indented and has a prompt 
    402464        #    return 0 
    403465         
    404466        #get the number of digits of the number of the input 
    405         item = self.doc.element[line[0]] 
    406         type = self.doc.GetStuff(line[0])[0] 
    407         strnumber = item.attrib['number'] 
    408         return len(self.GetPrompt(type, strnumber, line[1] == 1)) 
     467        #item = self.doc.element[line[0]] 
     468        #type = self.doc.GetStuff(line[0])[0] 
     469        #strnumber = item.attrib['number'] 
     470        #return len(self.GetPrompt(type, strnumber, line[1] == 1)) 
    409471     
    410472    def UpdateDoc(self): 
     
    541603        #    return False 
    542604        #Check if the current cell is an input cell 
    543         if self.doc.GetStuff(id)[0] in ['input', 'special']: 
    544             return True 
    545         else: 
    546             return False 
    547      
    548     def CanEdit(self, oper = 'insert', pos = None): 
     605        return self.doc.element[id].attrib['type'] in ['input', 'special'] 
     606     
     607    def CanEdit(self, oper = 'insert', pos = None, line = None): 
    549608        """Return true if the given editing operation should succeed at the 
    550609        given position if pos is None use the current position. oper can be 
     
    561620         
    562621        pos = pos or self.window.GetCurrentPos() 
    563         line = self.window.LineFromPosition(pos) 
    564  
    565         #Check if the current line is editable 
    566         if not self.CanEditLine(line): 
     622        #This is for optimization  
     623        line = line or self.window.LineFromPosition(pos) 
     624        l2l = self.line2log 
     625        #Check if the current line is editable. self.CanEditLine does the same 
     626        #as the code below. It is not called for optimization 
     627        id = l2l[line] 
     628        if id is None: 
     629            return False 
     630        id = id[0] 
     631        if not self.doc.element[id].attrib['type'] in ['input', 'special']: 
    567632            return False 
    568633 
     
    576641        if oper == 'delete' and self.window.GetCharAt(pos) == ord('\n'): 
    577642            #Check if the next line belongs to the same input 
    578             l = len(self.line2log
    579             return (line +1 < l and self.line2log[line+1] is not None and 
    580                 self.line2log[line+1][0] == self.line2log[line][0]) 
     643            l = len(l2l
     644            return (line +1 < l and l2l[line+1] is not None and 
     645                l2l[line+1][0] == l2l[line][0]) 
    581646        elif oper == 'backspace': 
    582647            #Check if this is the first line 
    583             return pos > startpos + promptlen or self.line2log[line][1] > 1 
     648            return pos > startpos + promptlen or l2l[line][1] > 1 
    584649        else: 
    585650            return True 
  • nbshell/trunk/nbshell/PythonWidget.py

    r823 r836  
    266266        if not shiftDown and not controlDown and key == wx.WXK_RETURN: 
    267267            if self.view.CanEdit(): 
     268                self.view.add_to_history() 
    268269                self.view.InsertLineBreak() 
    269270        #Shift-Return, (Shift-Enter) is used to execute the current input 
     
    271272            if self.CallTipActive(): 
    272273                self.CallTipCancel() 
     274            self.view.add_to_history() 
    273275            self.view.ProcessLine() 
    274276        #Ctrl-Return will reexecute the current input and all cells after it 
     
    276278            if self.CallTipActive(): 
    277279                self.CallTipCancel() 
     280            self.view.add_to_history() 
    278281            self.view.ProcessLine(flag = True) 
    279282 
     
    351354        #    self.PasteAndRun() 
    352355        # Replace with the previous command from the history buffer. 
    353         #elif (controlDown and key == wx.WXK_UP) \ 
    354         #         or (altDown and key in (ord('P'), ord('p'))): 
    355         #    self.OnHistoryReplace(step=+1) 
     356        elif (controlDown and key == wx.WXK_UP) \ 
     357                 or (altDown and key in (ord('P'), ord('p'))): 
     358            self.view.hist_replace(dir=-1) 
    356359        # Replace with the next command from the history buffer. 
    357         #elif (controlDown and key == wx.WXK_DOWN) \ 
    358         #         or (altDown and key in (ord('N'), ord('n'))): 
    359         #    self.OnHistoryReplace(step=-1) 
     360        elif (controlDown and key == wx.WXK_DOWN) \ 
     361                 or (altDown and key in (ord('N'), ord('n'))): 
     362            self.view.hist_replace(dir=+1) 
    360363        # Insert the previous command from the history buffer. 
    361364        #elif (shiftDown and key == wx.WXK_UP) and self.view.CanEdit(): 
  • nbshell/trunk/nbshell/frame.py

    r835 r836  
    128128        menu.Append(filemenu, "&File") 
    129129        menu.Append(insertmenu, "&Insert") 
    130         menu.Append(nbshellmenu, "&NBShell") 
     130        menu.Append(nbshellmenu, "N&BShell") 
    131131        self.SetMenuBar(menu) 
    132132