Search code examples
perlautocompletecshtcsh

tcsh auto-complete and getting the pattern for another autocompete


Is it possible to get the match pattern so far with tc shell's complete?

What I mean is, assuming a command A, and execute:

A[TAB]
c1
c2
c3

A c3[TAB] <- here I want to complete to execute a command with the matched pattern so far: c3....

Update: Basically I'm trying to autocomplete from an XML.

    <xml>
      <key name="c1"></key>
      <key name="c2"></key>
      <key name="c3">
         <key name="c3-b2">
           <key name="c3-xdr"></key>
         </key>
      </key>
</xml>

so Basically I would like to be able to auto-complete like: c3[TAB]/c3-b2[TAB]/c3-xdr

I'm currently trying to use Term::ReadLine::Gnu with Perl.

and no, I cannot use Bash


Solution

  • Here is how I'd go about it. First, I'd use a simple xml-parsing script to print out the paths. Here's one I came up with (you can easily write one of your own using perl, or just use this one):

    # xml_paths.py
    import sys
    from xml import sax
    
    class PathHandler(sax.handler.ContentHandler):
        def __init__(self):
            self.path = []
        def startElement(self, name, attrs):
            if name == 'key':
                self.path.append(attrs['name'])
                print '/'.join(self.path)
        def endElement(self, name):
            if name == 'key':
                self.path.pop()
    
    sax.parse(sys.argv[1], PathHandler())
    

    Finally, complete it (you should use absolute paths to the script and the xml file):

    % complete A 'p#*#`python xml_paths.py a.xml`#'
    % A <TAB>
    c1              c2              c3              c3/c3-b2        c3/c3-b2/c3-xdr
    % A c3/<TAB>
    c3/c3-b2        c3/c3-b2/c3-xdr 
    % A c3/c3-b2
    

    For clarity, this is the output of the xml-parsing script:

    % python xml_paths.py a.xml
    c1
    c2
    c3
    c3/c3-b2
    c3/c3-b2/c3-xdr