Logo Search packages:      
Sourcecode: lfm version File versions  Download package

def lfm::pyview::FileView::run (   self  ) 

run application, manage keys, etc

Definition at line 741 of file pyview.py.

00741                  :
        """run application, manage keys, etc"""

        self.show()
        while 1:
            ch = self.win_file.getch()

            # cursor up
            if ch in [ord('p'), ord('P'), curses.KEY_UP]:
                if self.mode == MODE_TEXT:
                    if self.wrap:
                        if self.col == 0:
                            if self.line > 0:
                                self.__move_lines(-1)
                                self.col = int(self.__get_line_length() / self.maxw) * self.maxw
                        else:
                            self.col -= self.maxw
                    else:
                        self.__move_lines(-1)
                else:
                    self.__move_hex(-1)
                self.show()
            # cursor down
            elif ch in [ord('n'), ord('N'), curses.KEY_DOWN]:
                if self.mode == MODE_TEXT:
                    if self.wrap:
                        if self.line >= self.nlines - 1 and \
                               self.__get_line_length() < (self.maxw * (self.maxh - 2)):
                            pass
                        else:
                            self.col += self.maxw
                            if self.col >= self.__get_line_length():
                                self.col = 0
                                self.__move_lines(1)
                            else:
                                self.__move_lines(0)
                    else:
                        self.__move_lines(1)
                else:
                    self.__move_hex(1)
                self.show()
            # page previous
            elif ch in [curses.KEY_PPAGE, curses.KEY_BACKSPACE,
                        0x08, 0x10]:                         # BackSpace, Ctrl-P
                if self.mode == MODE_TEXT:
                    if self.wrap:
                        lines = self.__get_prev_lines_text()
                        if self.col:     # if we aren't at 1st char of line
                            line0 = self.__get_1line()[:self.col]
                            lines.insert(0, line0)
                        else:
                            line0 = ''
                        y = self.maxh - 2
                        for i in range(len(lines)):
                            y -= 1
                            dy = 0
                            if y < 0:
                                break
                            exit1 = 0
                            len2 = len(lines[i])
                            lenz = len2
                            while len2 > self.maxw:
                                dy += 1
                                y -= 1
                                if y < 0:
                                    i += 1
                                    dy = int(lenz / self.maxw) + 1 - dy
                                    exit1 = 1
                                    break
                                len2 -= self.maxw
                            if exit1:
                                break
                        else:
                            i += 1
                        if line0:
                            i -= 1
                        if y < 0:
                            self.__move_lines(-i)
                            if i == 0:
                                self.col = (dy - 1) * self.maxw
                            else:
                                self.col = dy * self.maxw
                        else:
                            self.__move_lines(-(self.maxh-2))
                            self.col = 0
                    else:
                        self.__move_lines(-(self.maxh-2))
                else:
                    self.__move_hex(-(self.maxh-2))
                self.show()
            # page next
            elif ch in [curses.KEY_NPAGE, ord(' '), 0x0E]:   # Ctrl-N
                if self.mode == MODE_TEXT:
                    if self.wrap:
                        lines = self.__get_lines_text()
                        lines[0] = lines[0][self.col:]
                        y = 0
                        for i in range(len(lines)):
                            y += 1
                            dy = 0
                            if y > self.maxh - 2:
                                break
                            exit1 = 0
                            len2 = len(lines[i])
                            while len2 > self.maxw:
                                dy += 1
                                y += 1
                                if y > self.maxh - 2:
                                    exit1 = 1
                                    break
                                len2 -= self.maxw
                            if exit1:
                                break
                        else:
                            i += 1
                        self.__move_lines(i)
                        if i == 0:
                            self.col += dy * self.maxw
                        else:
                            self.col = dy * self.maxw
                    else:
                        self.__move_lines(self.maxh-2)
                else:
                    self.__move_hex(self.maxh-2)
                self.show()
            # home
            elif (ch in [curses.KEY_HOME, 0x16A]):  # home
                if self.mode == MODE_TEXT:
                    self.__move_lines(-self.nlines)
                else:
                    self.__move_hex(-self.nbytes)                    
                self.col = 0
                self.show()
            # end
            elif (ch in [curses.KEY_END, 0x181]):   # end
                if self.mode == MODE_TEXT:
                    self.__move_lines(self.nlines)
                else:
                    self.__move_hex(self.nbytes)
                self.col = 0
                self.show()
            
            # cursor left
            elif ch in [curses.KEY_LEFT]:
                if self.mode == MODE_HEX or self.wrap:
                    continue
                if self.col > 9:
                    self.col -= 10
                    self.show()
            # cursor right
            elif ch in [curses.KEY_RIGHT]:
                if self.mode == MODE_HEX or self.wrap:
                    continue
                if self.col + self.maxw < self.col_max + 2:
                    self.col += 10
                    self.show()

            # un/wrap
            elif ch in [ord('w'), ord('W'), curses.KEY_F2]:
                if self.mode == MODE_HEX:
                    continue
                self. wrap = not self.wrap
                self.__move_lines(0)
                self.col = 0
                self.show()

            # text / hexadecimal mode
            elif ch in [ord('m'), ord('M'), curses.KEY_F4]:
                if self.mode == MODE_TEXT:
                    self.mode = MODE_HEX
                    self.col = 0
                else:
                    self.mode = MODE_TEXT
                    self.__move_lines(0)
                self.show()

            #  goto line/byte
            elif ch in [ord('g'), ord('G'), curses.KEY_F5]:
                rel = 0
                if self.mode == MODE_TEXT:
                    title = 'Goto line'
                    help = 'Type line number'
                else:
                    title = 'Goto byte'
                    help = 'Type byte offset'
                n = messages.Entry(title, help, '', 1, 0).run()
                if n == None or n == '':
                    self.show()
                    continue
                if n[0] == '+' or n[0] == '-':
                    rel = 1
                try:
                    if n[rel:rel+2] == '0x':
                        if rel:
                            n = long(n[0] + str(int(n[1:], 16)))
                        else:
                            n = long(n, 16)
                    else:
                        n = long(n)
                except ValueError:
                    self.show()
                    if self.mode == MODE_TEXT:
                        messages.error('Goto line',
                                       'Invalid line number <%s>' % n)
                    else:
                        messages.error('Goto byte',
                                       'Invalid byte number <%s>' % n)
                    self.show()
                    continue

                if self.mode == MODE_TEXT:
                    if rel:
                        self.line += n
                    else:
                        self.line = n - 1
                    if self.line > self.nlines:
                        self.line = self.nlines - 1
                    self.__move_lines(0)
                else:
                    if rel:
                        self.pos += n
                    else:
                        self.pos = n
                    if self.pos > self.nbytes:
                        self.pos = self.nbytes
                    self.__move_hex(0)
                self.show()

            #  find
            elif ch in [ord('/')]:
                if self.__find('Find') == -1:
                    continue
                self.__find_next()               
            # find previous
            elif ch in [curses.KEY_F6]:
                if not self.matches:
                    if self.__find('Find Previous') == -1:
                        continue
                self.__find_previous()
            # find next
            elif ch in [curses.KEY_F7]:
                if not self.matches:
                    if self.__find('Find Next') == -1:
                        continue
                self.__find_next()

            # go to bookmark
            elif 0x30 <= ch <= 0x39:
                bk = self.bookmarks[ch - 0x30]
                if bk == -1:
                    continue
                self.line = bk
                self.__move_lines(0)
                self.show()
            # set bookmark
            elif ch in [ord('b'), ord('B')]:
                while 1:
                    ch = messages.get_a_key('Set bookmark',
                                            'Press 0-9 to save bookmark, Ctrl-C to quit')
                    if 0x30 <= ch <= 0x39:
                        self.bookmarks[ch-0x30] = self.line
                        break
                    elif ch == -1:
                        break
                self.show()

            # shell
            elif ch in [0x0F]:          # Ctrl-O
                curses.endwin()
                if self.stdin_flag:
                    os.system('sh')
                else:
                    os.system('cd \"%s\"; sh' % os.path.dirname(self.file))
                curses.curs_set(0)
                self.show()

            #  help
            elif ch in [ord('h'), ord('H'), curses.KEY_F1]:
                buf = [('', 2)]
                buf.append(('%s v%s (C) %s, by %s' % \
                            (PYVIEW_NAME, VERSION, DATE, AUTHOR), 5))
                text = PYVIEW_README.split('\n')
                for l in text:
                    buf.append((l, 6))
                InternalView('Help for %s' % PYVIEW_NAME, buf).run()
                self.show()

            # resize window
            elif ch in [curses.KEY_RESIZE]:
                self.resize_window()
                
            # quit
            elif ch in [ord('q'), ord('Q'), ord('x'), ord('X'),
                        curses.KEY_F3, curses.KEY_F10]:
                self.fd.close()
                return


##################################################


Generated by  Doxygen 1.6.0   Back to index