" " Filename: cream-templates.vim " " Cream -- An easy-to-use configuration of the famous Vim text editor " [ http://cream.sourceforge.net ] Copyright (C) 2001-2007 Steve Hall " " License: " This program is free software; you can redistribute it and/or modify " it under the terms of the GNU General Public License as published by " the Free Software Foundation; either version 2 of the License, or " (at your option) any later version. " [ http://www.gnu.org/licenses/gpl.html ] " " This program is distributed in the hope that it will be useful, but " WITHOUT ANY WARRANTY; without even the implied warranty of " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU " General Public License for more details. " " You should have received a copy of the GNU General Public License " along with this program; if not, write to the Free Software " Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA " 02111-1307, USA. " " " Description: This is a template expander. While typing, a brief word " ("keyword") can be expanded into a larger body of standard text " ("template"). (Inspired by imaps.vim, by Srinath Avadhanula.) " " cursor placement marker let s:findchar = "{+}" "" mapping (expands valid keyword to template) "inoremap =Cream_template_expand() " Cream_template_expand() {{{1 function! Cream_template_expand() " test the word before the cursor and expand it if it matches " test the word before the cursor normal h let word = expand('') normal l let rhs = '' if exists('g:cream_template_' . &filetype . '_' . word) " first find out whether this word is a mapping in the current file-type. execute 'let rhs = g:cream_template_' . &filetype . '_' . word elseif exists('g:cream_template__' . word) " also check for general purpose mappings. execute 'let rhs = g:cream_template__' . word end " if this is a mapping if rhs != '' " erase the map's letters with equal number of backspaces let bkspc = "\" . substitute(word, ".", "\", 'ge') " is the cursor placement marker found? if stridx(rhs, s:findchar) != -1 let i = strlen(s:findchar) else let i = 0 endif if rhs =~ s:findchar if i > 0 let movement = "\?" . s:findchar . "\" . i . "s" else let movement = "\?" . s:findchar . "\" endif else let movement = "" end return bkspc . rhs . movement endif return '' endfunction " Cream_template_assign() {{{1 function! Cream_template_assign(ftype, mapping, template) " set (initialize) template mappings to be available " arguments: " {ftype} Filetype mapping is applicable in. Can be empty to " match all files. " {mapping} Word to complete template on. " {template} String to insert on completion. " " create variable " Note: Final template is stored here as a double-quoted string. " ALL ESCAPING MUST BE APPROPRIATE AS FOR A DOUBLE-QUOTED STRING, " NOT A LITERAL ONE! execute "let g:cream_template_" . a:ftype . "_" . a:mapping . " = \"" . a:template . "\"" " register with both filetype*s* array AND particular filetype array " * "g:cream_template" arrays all filetypes with templates available " * "g:cream_template . a:ftype" arrays each filetype's templates " initialize filetype's global ("g:cream_template_html") if !exists("g:cream_template_" . a:ftype) " verify filetype is registered (so we don't have to search " through all variables later) " initialize if !exists("g:cream_template") let g:cream_template = a:ftype . "\n" else " only add if not already in array if match(g:cream_template, a:ftype . "\\n") == -1 execute "let g:cream_template = g:cream_template . \"" . a:ftype . "\" . \"\\n\"" endif endif " initialize container execute "let g:cream_template_" . a:ftype . " = ''" endif " append current to global registration for that filetype execute "let g:cream_template_" . a:ftype . " = g:cream_template_" . a:ftype . " . \"" . a:mapping . "\" . \"\\n\"" endfunction " Cream_template_listing() {{{1 function! Cream_template_listing() " diaplay all registered templates " compose display string let mystr = "" " get variable for each filetype registered let x = MvNumberOfElements(g:cream_template, "\n") let i = 0 while i < x " form: "g:cream_template_html" let myvar = MvElementAt(g:cream_template, "\n", i) "let myarray = g:cream_template_{myvar} let y = MvNumberOfElements(g:cream_template_{myvar}, "\n") let j = 0 while j < y " form: "g:cream_template_html_table" let myword = MvElementAt(g:cream_template_{myvar}, "\n", j) let mytemplate = g:cream_template_{myvar}_{myword} " compose string " header if j == 0 let mystr = mystr . "\n" let mywidth = winwidth(0) - 1 let k = 0 while k < mywidth let mystr = mystr . "_" let k = k + 1 endwhile let mystr = mystr . "\n" " empty is applicable to all filetypes if myvar == "" let mystr = mystr . "General Templates (available in all files)" . "\n\n" else let mystr = mystr . toupper(myvar) . " Templates (available in " . myvar . " files only)\n\n" endif let mystr = mystr . "WORD TEMPLATE \n" let mystr = mystr . "--------- -------------------------------------------- \n" endif " remove *initial* newline or return let mytemplate = substitute(mytemplate, '^[\n\r]', '', 'g') " remove dec 001-007 chars let mytemplate = substitute(mytemplate, '[-]', '', 'g') " remove dec 008 () let mytemplate = substitute(mytemplate, '[]', '', 'g') " remove "" let mytemplate = substitute(mytemplate, '\', '', 'g') "" remove dec 009 () "let mytemplate = substitute(mytemplate, '[ ]', '', 'g') " remove dec 011-012 chars let mytemplate = substitute(mytemplate, '[ - ]', '', 'g') " remove dec 014-031 chars let mytemplate = substitute(mytemplate, '[-]', '', 'g') "" trim off remaining returns and replace with ellipses ("...") "let mytemplate = substitute(mytemplate, "\n.*$", "...", "") " remove s:findchar let mytemplate = substitute(mytemplate, s:findchar, '', 'g') " space off templates from left margin let myspacer = " " " preceed entries containing return second lines with a spacer if match(mytemplate, "\n") != -1 let mytemplate = substitute(mytemplate, "\n", "\n" . myspacer, "g") endif " space single/first string let myspacerlen = strlen(myspacer) let myspacer = "" " insert character &textwidth number of times let k = 0 while k < myspacerlen - strlen(myword) let myspacer = myspacer . " " let k = k + 1 endwhile " cat let mystr = mystr . myword . myspacer . mytemplate . "\n" let j = j + 1 endwhile let i = i + 1 endwhile " set margin, right let k = 0 let myright = "" while k < 1 let myright = myright . " " let k = k + 1 endwhile let mystr = substitute(mystr, "\n", '\n' . myright, 'g') " set margin, left let mywidth = winwidth(0) - 1 let mystr = substitute(mystr, "\\v([^\n]{," . mywidth . "})[^\n]*", '\1', 'g') " now add pre-header explanation, so it can wrap/return without concerns let mystr = \ "\n" . \ " The listing below indicates words that can be expanded\n" . \ " into the adjacent template. The templates are grouped \n" . \ " by the file type in which each is available.\n" . \ "\n" . \ " Simply press Shift+Space(x2) with the cursor at the end\n" . \ " of the word. (Note that the word must be separated\n" . \ " by a space or return from the following word.)\n" . \ "\n" . \ " Press the Space Bar to continue... (Esc to quit)\n" . \ mystr echo mystr endfunction " 1}}} " Templates " General {{{1 " date: "20 November 2002" (See F11 for stamps) call Cream_template_assign("", "date", "\=strftime('%d %B %Y')\") " datestamp: "2002-12-04T11:16:27EST" (Same as F11x4) call Cream_template_assign("", "datestamp", "\\=strftime('%Y-%m-%dT%H:%M:%S%Z')\") " URLs call Cream_template_assign("", "url", "http://www.") call Cream_template_assign("", "urlcream", "http://cream.sourceforge.net") call Cream_template_assign("", "urlvim", "http://www.vim.org") call Cream_template_assign("", "urlgpl", "http://www.gnu.org/licenses/gpl.html") call Cream_template_assign("", "urlpi", "http://3.141592653589793238462643383279502884197169399375105820974944592.com/") " ASCII chars 32-126 (Note "!" and " " are swapped for readibility) call Cream_template_assign("", "ascii", "! \\\"#$%&'()*+,-./0123456789:;\<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{\|}~") " a "ruler" -- nice for counting the length of words call Cream_template_assign("", "ruler", "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" . \ " 10^ 20^ 30^ 40^ 50^ 60^ 70^ 80^ 90^ 100^\n") " some important numbers call Cream_template_assign("", "e", "2.7182818284590452353602874713526624977573") call Cream_template_assign("", "pi", "3.1415926535897932384626433832795028841972") " pi to 1,000 places, " each line is 50 chars. (who could resist, eh?) call Cream_template_assign("", "PI", \ "141 592 6535 897 932 3846 264 338 3279 502 884 1971 693 993 7510\n" . \ "582 097 4944 592 307 8164 062 862 0899 862 803 4825 342 117 0679\n" . \ "821 480 8651 328 230 6647 093 844 6095 505 822 3172 535 940 8128\n" . \ "481 117 4502 841 027 0193 852 110 5559 644 622 9489 549 303 8196\n" . \ "442 881 0975 665 933 4461 284 756 4823 378 678 3165 271 201 9091\n" . \ "456 485 6692 346 034 8610 454 326 6482 133 936 0726 024 914 1273\n" . \ "724 587 0066 063 155 8817 488 152 0920 962 829 2540 917 153 6436\n" . \ "789 259 0360 011 330 5305 488 204 6652 138 414 6951 941 511 6094\n" . \ "330 572 7036 575 959 1953 092 186 1173 819 326 1179 310 511 8548\n" . \ "074 462 3799 627 495 6735 188 575 2724 891 227 9381 830 119 4912\n" . \ "983 367 3362 440 656 6430 860 213 9494 639 522 4737 190 702 1798\n" . \ "609 437 0277 053 921 7176 293 176 7523 846 748 1846 766 940 5132\n" . \ "000 568 1271 452 635 6082 778 577 1342 757 789 6091 736 371 7872\n" . \ "146 844 0901 224 953 4301 465 495 8537 105 079 2279 689 258 9235\n" . \ "420 199 5611 212 902 1960 864 034 4181 598 136 2977 477 130 9960\n" . \ "518 707 2113 499 999 9837 297 804 9951 059 731 7328 160 963 1859\n" . \ "502 445 9455 346 908 3026 425 223 0825 334 468 5035 261 931 1881\n" . \ "710 100 0313 783 875 2886 587 533 2083 814 206 1717 766 914 7303\n" . \ "598 253 4904 287 554 6873 115 956 2863 882 353 7875 937 519 5778\n" . \ "185 778 0532 171 226 8066 130 019 2787 661 119 5909 216 420 1989\n" . \ " Source: http://arvindn.cjb.net/pi/\n") " standard abbreviations call Cream_template_assign("", "i18n", "internationalization") call Cream_template_assign("", "l10n", "localization") " HTML {{{1 call Cream_template_assign("html", "html", \ '\' . \ '\' . \ '\' . \ '\\' . \ '\' . \ '\' . \ '\' . \ '\' . \ '\\' . \ '\' . \ '' . s:findchar . '\' . \ '\' . \ '\') call Cream_template_assign("html", "table", \ '\' . \ '\' . \ '\\' . \ '\\' . \ '
' . s:findchar . '
\') call Cream_template_assign("html", "tr", \ '\' . \ '\' . s:findchar . '\' . \ '\\') call Cream_template_assign("html", "td", '' . s:findchar . '\') call Cream_template_assign("html", "p", '

' . s:findchar . '

\') call Cream_template_assign("html", "a", '') call Cream_template_assign("html", "mail", '') call Cream_template_assign("html", "b", '' . s:findchar . '') call Cream_template_assign("html", "i", '' . s:findchar . '') call Cream_template_assign("html", "u", '' . s:findchar . '') call Cream_template_assign("html", "ol", '
    \
  1. ' . s:findchar . '
  2. \
\') call Cream_template_assign("html", "ul", '
    \
  • ' . s:findchar . '
  • \
\') call Cream_template_assign("html", "li", '
  • ' . s:findchar . '
  • \') call Cream_template_assign("html", "h1", '

    ' . s:findchar . '

    \') call Cream_template_assign("html", "h2", '

    ' . s:findchar . '

    \') call Cream_template_assign("html", "h3", '

    ' . s:findchar . '

    \') call Cream_template_assign("html", "h4", '

    ' . s:findchar . '

    \') call Cream_template_assign("html", "h5", '
    ' . s:findchar . '
    \') call Cream_template_assign("html", "h6", '
    ' . s:findchar . '
    \') " HTML character equivilences {{{1 " " Are these really helpful? A list would be better. call Cream_template_assign("html", "_", "\ \n\" . s:findchar) " HTML greek characters {{{1 " " Cream: single letters are more valuable otherwise "let s:imaps_html_a = "\α" "let s:imaps_html_b = "\β" "let s:imaps_html_c = "\χ" "let s:imaps_html_d = "\δ" "let s:imaps_html_e = "\ε" "let s:imaps_html_f = "\φ" "let s:imaps_html_g = "\γ" "let s:imaps_html_h = "\η" "let s:imaps_html_k = "\κ" "let s:imaps_html_l = "\λ" "let s:imaps_html_m = "\μ" "let s:imaps_html_n = "\ν" "let s:imaps_html_p = "\π" "let s:imaps_html_q = "\θ" "let s:imaps_html_r = "\ρ" "let s:imaps_html_s = "\σ" "let s:imaps_html_t = "\τ" "let s:imaps_html_u = "\υ" "let s:imaps_html_v = "\ς" "let s:imaps_html_w = "\ω" "let s:imaps_html_x = "\ξ" "let s:imaps_html_y = "\ψ" "let s:imaps_html_z = "\ζ" "let s:imaps_html_A = "\Α" "let s:imaps_html_B = "\Β" "let s:imaps_html_C = "\Χ" "let s:imaps_html_D = "\Δ" "let s:imaps_html_E = "\Ε" "let s:imaps_html_F = "\Φ" "let s:imaps_html_G = "\Γ" "let s:imaps_html_H = "\Η" "let s:imaps_html_K = "\Κ" "let s:imaps_html_L = "\Λ" "let s:imaps_html_M = "\Μ" "let s:imaps_html_N = "\Ν" "let s:imaps_html_P = "\Π" "let s:imaps_html_Q = "\Θ" "let s:imaps_html_R = "\Ρ" "let s:imaps_html_S = "\Σ" "let s:imaps_html_T = "\Τ" "let s:imaps_html_U = "\Υ" "let s:imaps_html_V = "\&Varsigma;" "let s:imaps_html_W = "\Ω" "let s:imaps_html_X = "\Ξ" "let s:imaps_html_Y = "\Ψ" "let s:imaps_html_Z = "\Ζ" " Vim {{{1 call Cream_template_assign("vim", "function", \ "function! \" . s:findchar . \"()\n" . \ "\n" . \ "endfunction\n") call Cream_template_assign("vim", "while", \ "let i = 0\n" . \ "while i \< \" . s:findchar . \"\n" . \ "\n" . \ "\let i = i + 1\n" . \ "\endwhile\n") call Cream_template_assign("vim", "debug", \ '\"*** DEBUG:\\' . \ '\let n = confirm(\' . \ '\\\ \"DEBUG:\\n\" .\' . \ '\\ \" ' . s:findchar . 'myvar = \\\"\" . myvar . \"\\\"\\n\" .\' . \ '\\ \"\\n\", \"&Ok\\n&Cancel\", 1, \"Info\")\' . \ 'if n != 1\' . \ 'return\' . \ '\endif\' . \ '\"***\\') call Cream_template_assign("vim", "confirm", \ "call confirm(\\n" . \ "\\\\\ \\\"\" . s:findchar . \"\\\\n\\\" .\\n" . \ "\\\\ \\\"\\\\n\\\", \\\"&Ok\\\", 1, \\\"Info\\\")\n\") " VB (Basic) {{{1 call Cream_template_assign("basic", "debug", \ "\'*** DEBUG:\\n" . \ "\Msg (\\\"Progress to here!\\\" & Chr(13) & _\\n" . \ "\\\\\" \" . s:findchar . \"myvar = \\\" & myvar & Chr(13) & _\\n" . \ "Chr(13))\\n" . \ "\'***\\n" . \ "") " Lisp {{{1 call Cream_template_assign("lisp", "debug", \ '\(princ \"DEBUG: \"' . s:findchar . 'myvar : \") (princ myvar) (princ \"\\n\")\\') "1}}} " vim:foldmethod=marker