Skip navigation.

UNKNOWN SPACE

For glory and dignity

Posts tagged with "Emacs"

Hack emacs's pycomplete.py

, ,

I hacked pycomplete.py under /usr/lib/site-python/pycomplete.py

Now it is work well for completion.





"""
Python dot expression completion using Pymacs.

This almost certainly needs work, but if you add

    (require 'pycomplete)

to your .xemacs/init.el file (untried w/ GNU Emacs so far) and have Pymacs
installed, when you hit M-TAB it will try to complete the dot expression
before point.  For example, given this import at the top of the file:

    import time

typing "time.cl" then hitting M-TAB should complete "time.clock".

This is unlikely to be done the way Emacs completion ought to be done, but
it's a start.  Perhaps someone with more Emacs mojo can take this stuff and
do it right.

See pycomplete.el for the Emacs Lisp side of things.
"""

import sys
import os.path
import string

from Pymacs import lisp

try:
    x = set
except NameError:
    from sets import Set as set
else:
    del x

def get_all_completions(s, imports=None):
    """Return contextual completion of s (string of >= zero chars).

    If given, imports is a list of import statements to be executed first.
    """
    locald = {}
    if imports is not None:
        for stmt in imports:
            try:
                exec stmt in globals(), locald
            except TypeError:
                raise TypeError, "invalid type: %s" % stmt

    dots = s.split(".")
    if not s or len(dots) == 1:
        keys = set()
        keys.update(locald.keys())
        keys.update(globals().keys())
        import __builtin__
        keys.update(dir(__builtin__))
        keys = list(keys)
        keys.sort()
        if s:
            return [k for k in keys if k.startswith(s)]
        else:
            return keys

    sym = None
    for i in range(1, len(dots)):
        s = ".".join(dots[:i])
        try:
            sym = eval(s, globals(), locald)
        except NameError:
            try:
                sym = __import__(s, globals(), locald, [])
            except ImportError:
                return []
    if sym is not None:
        s = dots[-1]
        return [k for k in dir(sym) if k.startswith(s)]

def pycomplete(s, imports=None):
    completions = get_all_completions(s, imports)
    dots = s.split(".")
    result = os.path.commonprefix([k[len(dots[-1]):] for k in completions])

    if result == "":
        if completions:
            width = lisp.window_width() - 2
            colum = width / 20
            white = "                    "

            msg = ""

            counter = 0
            for completion in completions :
                if completion.__len__() < 20 :
                    msg += completion + white[completion.__len__():]
                    counter += 1
                else :
                    msg += completion + white[completion.__len__() - 20:]
                    counter += 2

                if counter >= colum :
                    counter = 0
                    msg += '\n'

        else:
            msg = "no completions!"
        lisp.message(msg)
    return  result    

if __name__ == "__main__":
    print "<empty> ->", pycomplete("")
    print "sys.get ->", pycomplete("sys.get")
    print "sy ->", pycomplete("sy")
    print "sy (sys in context) ->", pycomplete("sy", imports=["import sys"])
    print "foo. ->", pycomplete("foo.")
    print "Enc (email * imported) ->",
    print pycomplete("Enc", imports=["from email import *"])
    print "E (email * imported) ->",
    print pycomplete("E", imports=["from email import *"])

    print "Enc ->", pycomplete("Enc")
    print "E ->", pycomplete("E")

# Local Variables :
# pymacs-auto-reload : t
# End :


p: p: p:

Xref, a helpful tool for Emacs

, ,

The web page of xref is here: http://www.xref-tech.com


F8      - Completion of identifiers. Info on symbols.
F7      - Delete an xrefactory window.
F6      - look for cross-references of the selected identifier,
          push those references onto stack,
          and go to the definition.
F5      - pop last cross-references pushed by F6,
          go to the position from where those references were pushed.
F4      - go to the next reference of the top symbol pushed by F6.
F3      - go to the previous reference of the top symbol pushed by F6.

C-F8    - compile & run. Invoke last compilation and run of program.
C-F7    - show symbol and references on the top of reference stack
C-F6    - push and list references of the selected identifier
C-F5    - repush lastly poped references
C-F4    - move to the next reference of selected symbol, no pushing.
          After a compilation move to the next error message.
C-F3    - move to the previous reference of selected symbol, no pushing.
          After a compilation move to the previous error message.




Auto completion.

December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31