Dear Vidar,

some time ago I wrote a function to create a nice-looking headers for my 
Python scripts. I hope it'll give you some inspiration. Feel free to copy & 
modify it according to your needs.

What it does: After pressing <F8> (see the mapping below) it asks for the 
header text. When finished typing, it produces a header like this:

# This is my header ################################
<an empty line>
Here comes your function/class definition.

with the length of line adjusted to 78 (a safe value for 80-char-wide 
terminals).


    function CreateHeader()
        let headerLength = 78

        let header = input("Header: ")
        if strlen(header) == 0
            return
        endif

        if strlen(header) > headerLength - 6
            " Begin with '# ' and end with ' ###'
            return
        endif

        let header = "# " . header . " "
        let lenOfHeader = strlen(header)
        let i = 0
        while i < headerLength - lenOfHeader
            let header = header . "#"
            let i = i + 1
        endwhile
        let lineNo = line(".")
        call append(lineNo, header)

        " Add an empty line and position the cursor
        call append(lineNo + 1, "")
        call cursor(lineNo + 2, 0)
        execute "startinsert"
    endfunction

    map <F8> :call CreateHeader()<CR>
    imap <F8> <Esc>:call CreateHeader()<CR>


-- 
There's no trick to being a humorist when you have the whole government 
working for you. --Will Rogers

Reply via email to