Keyword Completion 
Author Message
 Keyword Completion

One of the things that irritates me about Pop11 is the need to type closing
keywords for such things as if, repeat, define etc. I have hence written a
little bit of code that does it for you. Simply type in the first two, three
or four letters of a keyword (covered in the header below), press the space
bar, and the closing part will be added automatically. I am sure there are
neater ways to do it that the way I have chosen, but so what, it works for
me... I hope you find it as useful as I do.

/*
 * lazy.p
 *
 * Key word completion, it works for the following keywords:
 *
 * if, unl[ess], rep[eat], for, whi[le], swi[tchon], def[ine],
 * defm[-m,ine :method], defc[-c,ine :class]
 *
 * Type the unbracketed part followed by the SPACE key for completion
 *
 * Loading this library turns the above facility on, to turn if off type ESC 2,
 * to turn it back on type ESC 1
 *
 * Tim Read, Attention & Affect Project, University of Birmingham 1993
 *
 */

vars tim_vedprocesstrap, vedpos, vedleft, vedright, ved_temp_char_count = 0,
     vtcl = [];

define tim_vedprocesstrap();
  lvars len,vv;
  vtcl <> [^ved_last_char] -> vtcl;
  if ved_last_char = `\r` then
    0 -> ved_temp_char_count;
    [] -> vtcl;
  elseif ved_last_char = `\s` then
    length(vtcl) -> len;
    if len > 2 and vtcl(len-2) = `i` and vtcl(len-1) = `f` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('  then\n');
        vedpos(vv-4,'endif;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `u` and vtcl(len-2) = `n` and vtcl(len-1) = `l` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('ess  then\n');
        vedpos(vv-5,'endunless;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `r` and vtcl(len-2) = `e` and vtcl(len-1) = `p` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('eat  times\n');
        vedpos(vv-5,'endrepeat;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `f` and vtcl(len-2) = `o` and vtcl(len-1) = `r` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('  do\n');
        vedpos(vv-5,'endfor;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `w` and vtcl(len-2) = `h` and vtcl(len-1) = `i` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('le  do\n');
        vedpos(vv-5,'endwhile;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `s` and vtcl(len-2) = `w` and vtcl(len-1) = `i` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('tchon\n');
        vedpos(vv-5,'endswitchon;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `d` and vtcl(len-2) = `e` and vtcl(len-1) = `f` then
        vedcolumn -> vv;
        vedchardelete();vedinsertstring('ine ();\n');
        vedpos(vv-5,'enddefine;');
        vedleft(3); vedcharup();
    elseif len > 3 and vtcl(len-3) = `e` and vtcl(len-2) = `f` and vtcl(len-1) = `m` then
        vedchardelete();vedcolumn -> vv;
        vedchardelete();vedinsertstring('ine :method ();\n');
        vedpos(vv-5,'enddefine;');
        vedright(5);vedcharup();
    elseif len > 3 and vtcl(len-3) = `e` and vtcl(len-2) = `f` and vtcl(len-1) = `c` then
        vedchardelete();vedcolumn -> vv;
        vedchardelete();vedinsertstring('ine :class ;\n');
        vedpos(vv-5,'enddefine;');
        vedright(4);vedcharup();
    endif;
    vedsetcursor();
  endif;
enddefine;

define vedpos(no,str);
  if no > 0 then
    repeat no times
      vedinsertstring(' ');
    endrepeat;
  endif;
  vedinsertstring(str);
enddefine;

define vedleft(no);
  repeat no times
    vedcharleft();
  endrepeat;
enddefine;

define vedright(no);
  repeat no times
    vedcharright();
  endrepeat;
enddefine;

vedsetkey('\^[1', procedure; tim_vedprocesstrap -> vedprocesstrap endprocedure);
vedsetkey('\^[2', procedure; identfn -> vedprocesstrap endprocedure);

;;; turn it on by default
tim_vedprocesstrap -> vedprocesstrap;
--
   ---------------------------------------------------------------------

   Cognitive Science Research Centre, School of Computer Science,
   The University of Birmingham, Edgbaston, Birmingham, B15 2TT, England
   Phone: +44-(0)21-414-4766, Fax: +44-(0)21-414-4281
   ---------------------------------------------------------------------



Fri, 31 May 1996 19:27:50 GMT  
 Keyword Completion

Quote:

> One of the things that irritates me about Pop11 is the need to type closing
> keywords for such things as if, repeat, define etc. I have hence written a
> little bit of code that does it for you. Simply type in the first two, three
> or four letters of a keyword (covered in the header below), press the space

I've been slowly building up the following library over a number of
years. I've been reluctant to post it in the past because it has a
number of minor bugs, poor documentation and some of the code is
really old and crufty (I wrote some of it as a 2nd year undergrad -
eeek!)

I'm not trying to offend Tim but it has more features than Tim's
version and it should be easier to extend.

Season to your own tastes, but if you make any major modifications
I'd appreciate a copy, especially if the change included some docs
as I could then put the whole thing in the Pop archive.

Ian.

----- cut --- snip ----

/*       File:                     VED_AUTO.P
         Author:                   Ian Rogers
         Machine:                  tsuna
         Date:                     10th November 1986
         Related Files:    N/A
         Purpose:                  Sets up all the automatic expansion bits that I know of.
                                           I promise to tidy it up... one day...
 */

uses vedmatchbracket;

compile_mode :pop11 +normal;
compile_mode :pop11 +varsch +defpdr;

vars vedwiggling = true;

define macrotable = newmapping([], 8, false, true);
enddefine;

lvars p = newproperty([
                [d {'define' '' 'enddefine;'}]          [ed 'enddefine']
                [ds 'define syntax']
                [dom 'define:method']   [dc 'define:class']
                [dm 'defmethod']        [em 'enddefmethod']
                [fl 'flavour']          [efl 'endflavour']      [iv 'ivars']
                [ex 'external']         [ee 'endexternal']
                [exd 'external declare']        [dec 'declare']
                [exr 'external require']        [req 'require']

                [for {'for' '' 'endfor;'}]
                [For {'For' '' 'endfor;'}]
                [t 'then']                      [ei 'endif']            [ef 'endfor']
                [lv 'lvars']            [va 'vars']                     [ma 'macro']
                [lc 'lconstant']        [gl 'global']           [co 'constant']

                [r      {'repeat' '' 'endrepeat;'}]             [er 'endrepeat']
                [ul {'unless' '' 'endunless;'}]
                [ut {'until' '' 'enduntil;'}]           [until {'until' '' 'enduntil;'}]
                [wh {'while' '' 'endwhile;'}]           [while {'while' '' 'endwhile;'}]
                [el 'endunless']        [et 'enduntil']         [ew 'endwhile']

                [if {'if' '' 'endif;'}]
                [l 'else']                      [li 'elseif']           [lu 'elseunless']
                [ql 'quitloop']         [qi 'quitif']           [qu 'quitunless']

        [treu 'true']
        [flase 'false']         [fasle 'false']         [fales 'false']

        ], 29,false,true);
p -> macrotable('p');
p -> macrotable('ph');
p -> macrotable('icd');

newproperty([
        [f      'fun']
        [t 'then']
        [l 'else']
        [e 'end']
        [or 'orelse']],5,false,true) -> macrotable('ml');

newproperty([
        [c 'cond']
        [d 'defun']
        [l 'lambda']], 5, false, true) -> macrotable('lsp');

newproperty([
        [d 'define']
        [l 'else']], 7, false, true) ->> macrotable('c') -> macrotable('ml');

newproperty([
        [teh the] [hte the] [Teh The]
        [nad and] [adn and] [liek like]
        [htat that] [fro for] [fo of]
        [t then] [l else]
        ], 7, false, true) ->> macrotable('') -> macrotable('txt');

define mywiggleproccy(char);
lvars   char bra ket procedure (endtest mvpdr)
        ;
dlocal vedwiggletimes = 3;
#_IF (pop_internal_version >= 136000)
        if vedwiggling and not(vedonstatus) and
                        not(sysfiletype(vedcurrent) = '') then
                vedpositionpush();
                vedchoosebracket(char) -> endtest -> mvpdr -> bra -> ket;
                if mvpdr == vedcharleft then mvpdr() endif;
                vedfindbracket(bra, ket, endtest, mvpdr);
                vedwiggle(vedline, vedcolumn);
                vedpositionpop();
   endif
#_ENDIF
enddefine;

define indent_to_previous;
        lvars str tstop spstop c
                ;
        vedtrimline();
        vedthisline() -> str;
        veddocr();
        returnif(not(vedcompileable) or vedonstatus or str = '');
        -1 -> c;
        for c with_index spstop in_string str do
                quitif(c /== `\s` and c /== `\t`);
        endfor;
        returnif(c == -1 or c == `\t` or c == `\s`);
        spstop - spstop mod vedindentstep -> tstop;
        until vedcolumn >= tstop do vedcharinsert(`\t`) enduntil;
        until vedcolumn >= spstop do vedcharinsert(`\s`) enduntil;
enddefine;

define vedlastword();
        lvars string vedtemp
                ;
        lconstant
                        alpha = item_chartype(`a`),
                ;
        dlocal
                        vedline vedcolumn
                        %item_chartype(`&`)% = alpha,
                        %item_chartype(`#`)% = alpha,
                ;
        vedsetlinesize();
        if vedcolumn == 1 or vedcolumn > vvedlinesize + 1
                        or (vedthisline() -> string; string(vedcolumn - 1) = ` `) then
                false
        else
                vedcolumn -> vedtemp;
                vedcharleft();
                until vedatitemstart(vedcolumn,string,vvedlinesize + 1) do
                        vedcolumn - 1 -> vedcolumn;
                enduntil;
                consword(substring(vedcolumn,vedtemp-vedcolumn,string))
        endif
enddefine;

define vedwordbreak(vedoldthing);
        lvars   vedoldthing vedtemp last_word vedmacros x y
                ;                    
        if not(vedonstatus) and
                (macrotable(sysfiletype(vedcurrent)) or macrotable('')) ->> vedmacros
        then
                if (vedmacros(vedlastword() ->> last_word) ->> vedtemp) then
                        if vedtemp.isprocedure then
                                vedtemp() -> vedtemp;
                        endif;
                        if isstring(vedtemp) then
                                repeat length(last_word) times
                                        vedchardelete();
                                endrepeat;
                                vedinsertstring(vedtemp)
                        elseif isvector(vedtemp) then
                                repeat length(last_word) times
                                        vedchardelete();
                                endrepeat;
                                vedinsertstring(vedtemp(1));
                                vedcolumn -> x;
                                vedline -> y;
                                if x >= vvedlinesize then
                                        vedinsertstring('\s');
                                        vedinsertstring(vedtemp(2));
                                        indent_to_previous();  
                                        vedinsertstring(vedtemp(3));
                                        vedjumpto(y, x);
                                endif;
                        endif
                endif;
        endif;
        vedoldthing();
enddefine;

define check_define(pdr);
        lvars   pdr, str, item,
                        parms, rep;
                ;
        vedwordbreak(pdr);
        vedthisline() -> str;
        returnunless(isstartstring('define\s', str));
        returnif(issubstring('/*', 1, str));

        [%      incharitem(stringin(str)) -> rep;
                for item from_repeater rep do
                        nextunless(item == "(");
                        until (rep() ->> item) == ")" do
                                item,
                                nextif((rep() ->> item) == ")")(2);
                                if member(item, [: =]) then
                                        rep() -> ; rep() -> item;
                                endif;
                                unless item == "," then
                                        vederror('badly formed pdr args (no comma)');
                                endunless;
                        enduntil;
                endfor;
        %] -> parms;

        unless parms == [] then
                vedlinebelow();
                vedinsertstring('\tlvars\t');
                for item in parms do
                        vedinsertstring(item sys_>< '');
                        vedinsertstring(',\s');
                endfor;
                vedlinebelow();
                vedinsertstring('\t\t;');
        endunless;

        vedlinebelow();
        vedinsertstring('\t');
enddefine;

define ved_wig();
        not(vedwiggling) -> vedwiggling;
        vedputmessage('Wiggling ' sys_>< if vedwiggling then 'ON'
                                                                         else 'OFF' endif)
enddefine;

define ved_macros();
        ;;; sets auto macro stuff (default)
        vedsetkey(' ', vedwordbreak(% vedcharinsert(%` `%)%));
        vedsetkey('\t', vedwordbreak(% vedcharinsert(%`\t`%)%));
        vedsetkey(':', vedwordbreak(% vedcharinsert(%`:`%)%));
        vedsetkey(';', check_define(% vedcharinsert(%`;`%)%));
        vedsetkey(',', vedwordbreak(% vedcharinsert(%`,`%)%));
        vedsetkey('(', vedwordbreak(% vedcharinsert(%`(`%)%));
        vedsetkey(')', vedwordbreak(% vedcharinsert(%`)`%)%)
                                        pdcomp mywiggleproccy(%`)`%));
        vedsetkey('\^[;', ';;; ');
        vedsetkey('\^[?', ';;; ??? ');
        vedsetkey('\^[/', ';;; ??? ');
        vedsetkey('\^M', indent_to_previous);
enddefine;

define ved_nomacros();
;;; resets auto macro stuff
        vedsetkey(' ', vedinsertvedchar);
        vedsetkey('\t', vedinsertvedchar);
        vedsetkey(':', vedinsertvedchar);
        vedsetkey(';', vedinsertvedchar);
        vedsetkey(',', vedinsertvedchar);
        vedsetkey('(', vedinsertvedchar);
        vedsetkey(')', vedinsertvedchar);
        vedsetkey('\^M', veddocr);
enddefine;

define ved_auto();
        ved_macros();
enddefine;

----- cut ------- snip -----

HELP VED_AUTO                                                 Ian Rogers

This library provides various keyword substitution facilities.

         CONTENTS - (Use <ENTER> g to access required sections)

 -- Features
 -- Commands
 -- Known bugs

-- Features -----------------------------------------------------------

    o The substitution is based on property tables (which are easily
    extended)

    o The tables are associated with file extensions, (so that different
    associations occur for different file types),

    o The substitutions occur at key points of text entry (at: ' ',
    '\t', ':', ';', ',', '(', ')')

    o The return key automatically indents the next line.

    o parameter declarations are inserted automatically.

-- Commands -----------------------------------------------------------

The facilities are switched by the following two Ved commands:

ved_macros                                                   [procedure]
ved_nomacros                                                 [procedure]
        Respectively  switch  on  and   off  the  keyword   substitution
        facilities.

ved_auto                                                     [procedure]
        Identical to ved_macros.

-- Known bugs ---------------------------------------------------------

Indentation should really check the previous line for an opener or
closer and adjust accordingly.

Pop11 procedure output parameters are only discovered if they're inside
parentheses. Ie:

    define foo(a) -> (b, c);

and not:

    define foo(a) -> b -> c;

If you've already had your parameter declarations inserted, but want to
change the formal arguments, be careful not to delete the ; as you'll
get all the parameters reinserted when you type the ; again.

?? ?????/ved_auto
?? Copyright University of Sussex 1993. All rights reserved.



Fri, 31 May 1996 23:17:28 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. extracting keywords from %(keyword)s-based templates

2. passing keyword arguments that are themselfs python keywords

3. ANN: CJDLookup revision 3, context sensitive code completion tool

4. Escape Completion in VisualWorks

5. Do Completion Ports Leak Memory?

6. Editor with code completion

7. Printing on completion

8. Printing on completion

9. Eiffel editor with code completion ?

10. ANN: rlcompleter2 0.95 Interactive Completion

11. rlcompleter2 <TAB> interactive completion

12. Signaling the completion of a windowed session

 

 
Powered by phpBB® Forum Software