⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader.py

📁 python s60 1.4.5版本的源代码
💻 PY
📖 第 1 页 / 共 2 页
字号:
#   Copyright 2000-2004 Michael Hudson mwh@python.net
#
#                        All Rights Reserved
#
# Portions Copyright (c) 2005 Nokia Corporation 
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation.
#
# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import types
try:
    import unicodedata
except ImportError:
    import dumbunicodedata as unicodedata
from pyrepl import commands
import ascii
from pyrepl import input

def _make_unctrl_map():
    uc_map = {}
    for c in map(unichr, range(256)):
        if unicodedata.category(c)[0] <> 'C':
            uc_map[c] = c
    for i in range(32):
        c = unichr(i)
        uc_map[c] = u'^' + unichr(ord('A') + i - 1)
    uc_map['\177'] = u'^?'
    for i in range(256):
        c = unichr(i)
        if not uc_map.has_key(c):
            uc_map[c] = u'\\%03o'%i 
    return uc_map

# disp_str proved to be a bottleneck for large inputs, so it's been
# rewritten in C; it's not required though.
try:
    raise ImportError # currently it's borked by the unicode support
    
    from _pyrepl_utils import disp_str, init_unctrl_map
    
    init_unctrl_map(_make_unctrl_map())

    del init_unctrl_map
except ImportError:
    def _my_unctrl(c, u=_make_unctrl_map()):
#        import unicodedata
        if c in u:
            return u[c]
        else:
            if unicodedata.category(c).startswith('C'):
                return '\u%04x'%(ord(c),)
            else:
                return c

    def disp_str(buffer, join=''.join, uc=_my_unctrl):
        """ disp_str(buffer:string) -> (string, [int])

        Return the string that should be the printed represenation of
        |buffer| and a list detailing where the characters of |buffer|
        get used up.  E.g.:

        >>> disp_str(chr(3))
        ('^C', [1, 0])

        the list always contains 0s or 1s at present; it could conceivably
        go higher as and when unicode support happens."""
        s = map(uc, buffer)
        return (join(s),
                map(ord, join(map(lambda x:'\001'+(len(x)-1)*'\000', s))))

    del _my_unctrl

del _make_unctrl_map

# syntax classes:

[SYNTAX_WHITESPACE,
 SYNTAX_WORD,
 SYNTAX_SYMBOL] = range(3)

def make_default_syntax_table():
    # XXX perhaps should use some unicodedata here?
    st = {}
    for c in map(unichr, range(256)):
        st[c] = SYNTAX_SYMBOL
    for c in [a for a in map(unichr, range(256)) if a.isalpha()]:
        st[c] = SYNTAX_WORD
    st[u'\n'] = st[u' '] = SYNTAX_WHITESPACE
    return st

default_keymap = tuple(
    [(r'\C-a', 'beginning-of-line'),
     (r'\C-b', 'left'),
     (r'\C-c', 'interrupt'),
     (r'\C-d', 'delete'),
     (r'\C-e', 'end-of-line'),
     (r'\C-f', 'right'),
     (r'\C-g', 'cancel'),
     (r'\C-h', 'backspace'),
     (r'\C-j', 'accept'),
     (r'\<return>', 'accept'),
     (r'\C-k', 'kill-line'),
     (r'\C-l', 'clear-screen'),
     (r'\C-m', 'accept'),
     (r'\C-q', 'quoted-insert'),
     (r'\C-t', 'transpose-characters'),
     (r'\C-u', 'unix-line-discard'),
     (r'\C-v', 'quoted-insert'),
     (r'\C-w', 'unix-word-rubout'),
     (r'\C-x\C-s', 'save-history'),
     (r'\C-x\C-u', 'upcase-region'),
     (r'\C-y', 'yank'),
     (r'\C-z', 'suspend'),
     
     (r'\M-b', 'backward-word'),
     (r'\M-c', 'capitalize-word'),
     (r'\M-d', 'kill-word'),
     (r'\M-f', 'forward-word'),
     (r'\M-l', 'downcase-word'),
     (r'\M-t', 'transpose-words'),
     (r'\M-u', 'upcase-word'),
     (r'\M-y', 'yank-pop'),
     (r'\M--', 'digit-arg'),
     (r'\M-0', 'digit-arg'),
     (r'\M-1', 'digit-arg'),
     (r'\M-2', 'digit-arg'),
     (r'\M-3', 'digit-arg'),
     (r'\M-4', 'digit-arg'),
     (r'\M-5', 'digit-arg'),
     (r'\M-6', 'digit-arg'),
     (r'\M-7', 'digit-arg'),
     (r'\M-8', 'digit-arg'),
     (r'\M-9', 'digit-arg'),
     (r'\M-\n', 'insert-nl'),
     ('\\\\', 'self-insert')] + \
    [(c, 'self-insert')
     for c in map(chr, range(32, 127)) if c <> '\\'] + \
    [(c, 'self-insert')
     for c in map(chr, range(128, 256)) if c.isalpha()] + \
    [(r'\<up>', 'up'),
     (r'\<down>', 'down'),
     (r'\<left>', 'left'),
     (r'\<right>', 'right'),
     (r'\<insert>', 'quoted-insert'),
     (r'\<delete>', 'delete'),
     (r'\<backspace>', 'backspace'),
     (r'\M-\<backspace>', 'backward-kill-word'),
     (r'\<end>', 'end'),
     (r'\<home>', 'home'),
     (r'\<f1>', 'help'),
     (r'\EOF', 'end'),  # the entries in the terminfo database for xterms
     (r'\EOH', 'home'), # seem to be wrong.  this is a less than ideal
                        # workaround
     ])

del c # from the listcomps

class Reader(object):
    """The Reader class implements the bare bones of a command reader,
    handling such details as editing and cursor motion.  What it does
    not support are such things as completion or history support -
    these are implemented elsewhere.

    Instance variables of note include:

      * buffer:
        A *list* (*not* a string at the moment :-) containing all the
        characters that have been entered.
      * console:
        Hopefully encapsulates the OS dependent stuff.
      * pos:
        A 0-based index into `buffer' for where the insertion point
        is.
      * screeninfo:
        Ahem.  This list contains some info needed to move the
        insertion point around reasonably efficiently.  I'd like to
        get rid of it, because its contents are obtuse (to put it
        mildly) but I haven't worked out if that is possible yet.
      * cxy, lxy:
        the position of the insertion point in screen ... XXX
      * syntax_table:
        Dictionary mapping characters to `syntax class'; read the
        emacs docs to see what this means :-)
      * commands:
        Dictionary mapping command names to command classes.
      * arg:
        The emacs-style prefix argument.  It will be None if no such
        argument has been provided.
      * dirty:
        True if we need to refresh the display.
      * kill_ring:
        The emacs-style kill-ring; manipulated with yank & yank-pop
      * ps1, ps2, ps3, ps4:
        prompts.  ps1 is the prompt for a one-line input; for a
        multiline input it looks like:
            ps2> first line of input goes here
            ps3> second and further
            ps3> lines get ps3
            ...
            ps4> and the last one gets ps4
        As with the usual top-level, you can set these to instances if
        you like; str() will be called on them (once) at the beginning
        of each command.  Don't put really long or newline containing
        strings here, please!
        This is just the default policy; you can change it freely by
        overriding get_prompt() (and indeed some standard subclasses
        do).
      * finished:
        handle1 will set this to a true value if a command signals
        that we're done.
    """

    help_text = """\
This is pyrepl.  Hear my roar.

Helpful text may appear here at some point in the future when I'm
feeling more loquacious than I am now."""

    def __init__(self, console):
        self.buffer = []
        self.ps1 = "->> "
        self.ps2 = "/>> "
        self.ps3 = "|.. "
        self.ps4 = "\__ "
        self.kill_ring = []
        self.arg = None
        self.finished = 0
        self.console = console
        self.commands = {}
        self.msg = ''
        for v in vars(commands).values():
            if  ( isinstance(v, type)
                  and issubclass(v, commands.Command)
                  and v.__name__[0].islower() ):
                self.commands[v.__name__] = v
                self.commands[v.__name__.replace('_', '-')] = v
        self.syntax_table = make_default_syntax_table()
        self.input_trans_stack = []
        self.keymap = self.collect_keymap()
        self.input_trans = input.KeymapTranslator(
            self.keymap,
            invalid_cls='invalid-key',
            character_cls='self-insert')

    def collect_keymap(self):
        return default_keymap

    def calc_screen(self):
        """The purpose of this method is to translate changes in
        self.buffer into changes in self.screen.  Currently it rips
        everything down and starts from scratch, which whilst not
        especially efficient is certainly simple(r).
        """
        lines = self.get_unicode().split("\n")
        screen = []
        screeninfo = []
        w = self.console.width - 1
        p = self.pos
        for ln, line in zip(range(len(lines)), lines):
            ll = len(line)
            if 0 <= p <= ll:
                if self.msg:
                    for mline in self.msg.split("\n"):
                        screen.append(mline)
                        screeninfo.append((0, []))
                self.lxy = p, ln
            prompt = self.get_prompt(ln, ll >= p >= 0)
            p -= ll + 1
            lp = len(prompt)
            l, l2 = disp_str(line)
            wrapcount = (len(l) + lp) / w
            if wrapcount == 0:
                screen.append(prompt + l)
                screeninfo.append((lp, l2+[1]))
            else:
                screen.append(prompt + l[:w-lp] + "\\")
                screeninfo.append((lp, l2[:w-lp]))
                for i in range(-lp + w, -lp + wrapcount*w, w):
                    screen.append(l[i:i+w] +  "\\")
                    screeninfo.append((0, l2[i:i + w]))
                screen.append(l[wrapcount*w - lp:])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -