Auto code blocks/snippets with script

Auto code blocks/snippets with script

30
Basic UserBasic User
30

    Dec 20, 2007#1

    Hello, this is a revised script based on the one discussed at Snippets with macro. It auto-completes a block using a template for each flow control construct. Maybe it will be useful for others.

    Also, can anyone can tell me why that parseInt() appears to be necessary? (if I just pass i itself, an empty template is used)

    autoblock.js

    Code: Select all

    // index must match template number
    templates = []
    templates[0] = 'if'
    templates[1] = 'else'
    templates[2] = 'elif'
    templates[3] = 'for'
    templates[4] = 'while'
    templates[5] = 'do'
    templates[6] = 'switch'
    
    function autoblock()
    {
        doc = UltraEdit.activeDocument;
        
        col = doc.currentColumnNum;
        if (typeof(UltraEdit.activeDocumentIdx) == "undefined") col++;
        line = doc.currentLineNum;
        
        doc.key('LEFT ARROW');
        doc.selectWord();
        
        keyword = doc.selection;
        
        for(i in templates)
        {
            if(keyword == templates[i])
            {
                doc.key('DEL');
                doc.gotoLineSelect(0, 1);
                doc.insertTemplate(parseInt(i));
                return;
            }
        }
        
        doc.gotoLine(line, col);
        UltraEdit.messageBox('No Match!');
    }
    
    autoblock()
    The corresponding templates:

    Code: Select all

    [$replace$]if(^)
    [$replace$]{
    [$replace$]    
    [$replace$]}
    

    Code: Select all

    [$replace$]else
    [$replace$]{
    [$replace$]    ^
    [$replace$]}
    

    Code: Select all

    [$replace$]else if(^)
    [$replace$]{
    [$replace$]    
    [$replace$]}
    

    Code: Select all

    [$replace$]for(^)
    [$replace$]{
    [$replace$]    
    [$replace$]}
    

    Code: Select all

    [$replace$]while(^)
    [$replace$]{
    [$replace$]    
    [$replace$]}
    

    Code: Select all

    [$replace$]do
    [$replace$]{
    [$replace$]    
    [$replace$]} while(^);
    

    Code: Select all

    [$replace$]switch(^)
    [$replace$]{
    [$replace$]  case XXX:
    [$replace$]    
    [$replace$]    break;
    [$replace$]  
    [$replace$]  default:
    [$replace$]    
    [$replace$]    break;
    [$replace$]}

    23
    Basic UserBasic User
    23

      Jun 06, 2008#2

      How do you hotkey a script to make the autoblocks work? Can you set it to control-space?