|
| 1 | +"=============================================================================== |
| 2 | +" |
| 3 | +" File: helloworld.vim |
| 4 | +" |
| 5 | +" Description: Part of the C-Support toolbox. |
| 6 | +" |
| 7 | +" Small example for a tool, which may serve as a template for |
| 8 | +" your own tool. |
| 9 | +" |
| 10 | +" See help file TODO.txt . |
| 11 | +" |
| 12 | +" VIM Version: 7.0+ |
| 13 | +" Author: TODO |
| 14 | +" Organization: |
| 15 | +" Version: see variable g:HelloWorld_Version below |
| 16 | +" Created: TO.DO.TODO |
| 17 | +" Revision: --- |
| 18 | +" License: Copyright (c) TODO, TODO |
| 19 | +" This program is free software; you can redistribute it and/or |
| 20 | +" modify it under the terms of the GNU General Public License as |
| 21 | +" published by the Free Software Foundation, version 2 of the |
| 22 | +" License. |
| 23 | +" This program is distributed in the hope that it will be |
| 24 | +" useful, but WITHOUT ANY WARRANTY; without even the implied |
| 25 | +" warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 26 | +" PURPOSE. |
| 27 | +" See the GNU General Public License version 2 for more details. |
| 28 | +"=============================================================================== |
| 29 | +" |
| 30 | +"------------------------------------------------------------------------------- |
| 31 | +" Basic checks. {{{1 |
| 32 | +"------------------------------------------------------------------------------- |
| 33 | +" |
| 34 | +" need at least 7.0 |
| 35 | +if v:version < 700 |
| 36 | + echohl WarningMsg |
| 37 | + echo 'The plugin mmtoolbox/helloworld.vim needs Vim version >= 7.' |
| 38 | + echohl None |
| 39 | + finish |
| 40 | +endif |
| 41 | +" |
| 42 | +" prevent duplicate loading |
| 43 | +" need compatible |
| 44 | +if &cp || exists('g:HelloWorld_Version') |
| 45 | + finish |
| 46 | +endif |
| 47 | +let g:HelloWorld_Version= '1.0' " version number of this script; do not change |
| 48 | +" |
| 49 | +"------------------------------------------------------------------------------- |
| 50 | +" Auxiliary functions {{{1 |
| 51 | +"------------------------------------------------------------------------------- |
| 52 | +" |
| 53 | +"------------------------------------------------------------------------------- |
| 54 | +" s:ErrorMsg : Print an error message. {{{2 |
| 55 | +" |
| 56 | +" Parameters: |
| 57 | +" line1 - a line (string) |
| 58 | +" line2 - a line (string) |
| 59 | +" ... - ... |
| 60 | +" Returns: |
| 61 | +" - |
| 62 | +"------------------------------------------------------------------------------- |
| 63 | +function! s:ErrorMsg ( ... ) |
| 64 | + echohl WarningMsg |
| 65 | + for line in a:000 |
| 66 | + echomsg line |
| 67 | + endfor |
| 68 | + echohl None |
| 69 | +endfunction " ---------- end of function s:ErrorMsg ---------- |
| 70 | +" |
| 71 | +"------------------------------------------------------------------------------- |
| 72 | +" s:GetGlobalSetting : Get a setting from a global variable. {{{2 |
| 73 | +" |
| 74 | +" Parameters: |
| 75 | +" varname - name of the variable (string) |
| 76 | +" Returns: |
| 77 | +" - |
| 78 | +" |
| 79 | +" If g:<varname> exists, assign: |
| 80 | +" s:<varname> = g:<varname> |
| 81 | +"------------------------------------------------------------------------------- |
| 82 | +function! s:GetGlobalSetting ( varname ) |
| 83 | + if exists ( 'g:'.a:varname ) |
| 84 | + exe 'let s:'.a:varname.' = g:'.a:varname |
| 85 | + endif |
| 86 | +endfunction " ---------- end of function s:GetGlobalSetting ---------- |
| 87 | +" |
| 88 | +"------------------------------------------------------------------------------- |
| 89 | +" s:ImportantMsg : Print an important message. {{{2 |
| 90 | +" |
| 91 | +" Parameters: |
| 92 | +" line1 - a line (string) |
| 93 | +" line2 - a line (string) |
| 94 | +" ... - ... |
| 95 | +" Returns: |
| 96 | +" - |
| 97 | +"------------------------------------------------------------------------------- |
| 98 | +function! s:ImportantMsg ( ... ) |
| 99 | + echohl Search |
| 100 | + echo join ( a:000, "\n" ) |
| 101 | + echohl None |
| 102 | +endfunction " ---------- end of function s:ImportantMsg ---------- |
| 103 | +" |
| 104 | +"------------------------------------------------------------------------------- |
| 105 | +" s:Question : Ask the user a question. {{{2 |
| 106 | +" |
| 107 | +" Parameters: |
| 108 | +" prompt - prompt, shown to the user (string) |
| 109 | +" highlight - "normal" or "warning" (string, default "normal") |
| 110 | +" Returns: |
| 111 | +" retval - the user input (integer) |
| 112 | +" |
| 113 | +" The possible values of 'retval' are: |
| 114 | +" 1 - answer was yes ("y") |
| 115 | +" 0 - answer was no ("n") |
| 116 | +" -1 - user aborted ("ESC" or "CTRL-C") |
| 117 | +"------------------------------------------------------------------------------- |
| 118 | +function! s:Question ( prompt, ... ) |
| 119 | + " |
| 120 | + let ret = -2 |
| 121 | + " |
| 122 | + " highlight prompt |
| 123 | + if a:0 == 0 || a:1 == 'normal' |
| 124 | + echohl Search |
| 125 | + elseif a:1 == 'warning' |
| 126 | + echohl Error |
| 127 | + else |
| 128 | + echoerr 'Unknown option : "'.a:1.'"' |
| 129 | + return |
| 130 | + end |
| 131 | + " |
| 132 | + " question |
| 133 | + echo a:prompt.' [y/n]: ' |
| 134 | + " |
| 135 | + " answer: "y", "n", "ESC" or "CTRL-C" |
| 136 | + while ret == -2 |
| 137 | + let c = nr2char( getchar() ) |
| 138 | + " |
| 139 | + if c == "y" |
| 140 | + let ret = 1 |
| 141 | + elseif c == "n" |
| 142 | + let ret = 0 |
| 143 | + elseif c == "\<ESC>" || c == "\<C-C>" |
| 144 | + let ret = -1 |
| 145 | + endif |
| 146 | + endwhile |
| 147 | + " |
| 148 | + " reset highlighting |
| 149 | + echohl None |
| 150 | + " |
| 151 | + return ret |
| 152 | +endfunction " ---------- end of function s:Question ---------- |
| 153 | +" }}}2 |
| 154 | +"------------------------------------------------------------------------------- |
| 155 | +" |
| 156 | +"------------------------------------------------------------------------------- |
| 157 | +" Modul setup. {{{1 |
| 158 | +"------------------------------------------------------------------------------- |
| 159 | +" |
| 160 | +" platform specifics {{{2 |
| 161 | +" |
| 162 | +let s:MSWIN = has("win16") || has("win32") || has("win64") || has("win95") |
| 163 | +let s:UNIX = has("unix") || has("macunix") || has("win32unix") |
| 164 | +" |
| 165 | +if s:MSWIN |
| 166 | + " |
| 167 | + "------------------------------------------------------------------------------- |
| 168 | + " MS Windows |
| 169 | + "------------------------------------------------------------------------------- |
| 170 | + " |
| 171 | + let s:plugin_dir = substitute( expand('<sfile>:p:h:h:h'), '\\', '/', 'g' ) |
| 172 | + " |
| 173 | +else |
| 174 | + " |
| 175 | + "------------------------------------------------------------------------------- |
| 176 | + " Linux/Unix |
| 177 | + "------------------------------------------------------------------------------- |
| 178 | + " |
| 179 | + let s:plugin_dir = expand('<sfile>:p:h:h:h') |
| 180 | + " |
| 181 | +endif |
| 182 | +" |
| 183 | +" settings {{{2 |
| 184 | +" |
| 185 | +let s:WorldAvailable = 1 |
| 186 | +" |
| 187 | +let s:Enabled = 1 |
| 188 | +" |
| 189 | +if ! s:WorldAvailable |
| 190 | + let s:Enabled = 0 |
| 191 | +endif |
| 192 | +" |
| 193 | +" custom commands {{{2 |
| 194 | +" |
| 195 | +if s:Enabled == 1 |
| 196 | + command! -bang -nargs=* -complete=file Hello :echo "Hello world (<bang>): ".<q-args> |
| 197 | + command! -nargs=0 HelloHelp :call mmtoolbox#helloworld#HelpPlugin() |
| 198 | +else |
| 199 | + " |
| 200 | + " Disabled : Print why the script is disabled. {{{3 |
| 201 | + function! mmtoolbox#helloworld#Disabled () |
| 202 | + let txt = "HelloWorld tool not working:\n" |
| 203 | + if ! s:WorldAvailable |
| 204 | + let txt .= "world not available (say what!?)" |
| 205 | + else |
| 206 | + let txt .= "unknown reason" |
| 207 | + endif |
| 208 | + call s:ImportantMsg ( txt ) |
| 209 | + return |
| 210 | + endfunction " ---------- end of function mmtoolbox#helloworld#Disabled ---------- |
| 211 | + " }}}3 |
| 212 | + " |
| 213 | + command! -nargs=* HelloHelp :call mmtoolbox#helloworld#Disabled() |
| 214 | + " |
| 215 | +endif |
| 216 | +" |
| 217 | +" }}}2 |
| 218 | +" |
| 219 | +"------------------------------------------------------------------------------- |
| 220 | +" GetInfo : Initialize the script. {{{1 |
| 221 | +"------------------------------------------------------------------------------- |
| 222 | +function! mmtoolbox#helloworld#GetInfo () |
| 223 | + " |
| 224 | + " returns [ <prettyname>, <version>, <flag1>, <flag2>, ... ] |
| 225 | + " |
| 226 | + if s:WorldAvailable |
| 227 | + return [ 'Hello World', g:HelloWorld_Version ] |
| 228 | + " if you do not want to create a menu: |
| 229 | + " return [ 'Hello World', g:HelloWorld_Version, 'nomenu' ] |
| 230 | + else |
| 231 | + return [ 'Hello World', g:HelloWorld_Version, 'disabled' ] |
| 232 | + endif |
| 233 | +endfunction " ---------- end of function mmtoolbox#helloworld#GetInfo ---------- |
| 234 | +" |
| 235 | +"------------------------------------------------------------------------------- |
| 236 | +" AddMaps : Add maps. {{{1 |
| 237 | +"------------------------------------------------------------------------------- |
| 238 | +function! mmtoolbox#helloworld#AddMaps () |
| 239 | + " |
| 240 | + " create maps for the current buffer only |
| 241 | + " |
| 242 | + nmap <buffer> hi :echo "Hello world!"<CR>' |
| 243 | + " |
| 244 | + " TODO |
| 245 | + " |
| 246 | +endfunction " ---------- end of function mmtoolbox#helloworld#AddMaps ---------- |
| 247 | +" |
| 248 | +"------------------------------------------------------------------------------- |
| 249 | +" AddMenu : Add menus. {{{1 |
| 250 | +"------------------------------------------------------------------------------- |
| 251 | +function! mmtoolbox#helloworld#AddMenu ( root, esc_mapl ) |
| 252 | + " |
| 253 | + " create menus using the given 'root' |
| 254 | + " |
| 255 | + exe 'amenu '.a:root.'.&hello\ world<TAB>'.a:esc_mapl.'hi :echo "Hello world!"<CR>' |
| 256 | + " |
| 257 | + " TODO |
| 258 | + " |
| 259 | +endfunction " ---------- end of function mmtoolbox#helloworld#AddMenu ---------- |
| 260 | +" |
| 261 | +"------------------------------------------------------------------------------- |
| 262 | +" Property : Various settings. {{{1 |
| 263 | +"------------------------------------------------------------------------------- |
| 264 | +function! mmtoolbox#helloworld#Property ( mode, key, ... ) |
| 265 | + " |
| 266 | + " check the mode |
| 267 | + if a:mode !~ 'echo\|get\|set' |
| 268 | + return s:ErrorMsg ( 'HelloWorld : Unknown mode: '.a:mode ) |
| 269 | + endif |
| 270 | + " |
| 271 | + " check 3rd argument for 'set' |
| 272 | + if a:mode == 'set' |
| 273 | + if a:0 == 0 |
| 274 | + return s:ErrorMsg ( 'HelloWorld : Not enough arguments for mode "set".' ) |
| 275 | + endif |
| 276 | + let val = a:1 |
| 277 | + endif |
| 278 | + " |
| 279 | + " check the key |
| 280 | + if a:key == 'enabled' |
| 281 | + let var = 's:Enabled' |
| 282 | + else |
| 283 | + return s:ErrorMsg ( 'HelloWorld : Unknown option: '.a:key ) |
| 284 | + endif |
| 285 | + " |
| 286 | + " perform the action |
| 287 | + if a:mode == 'echo' |
| 288 | + exe 'echo '.var |
| 289 | + return |
| 290 | + elseif a:mode == 'get' |
| 291 | + exe 'return '.var |
| 292 | + else |
| 293 | + " action is 'set', but key is non of the above |
| 294 | + return s:ErrorMsg ( 'HelloWorld : Option is read-only, can not set: '.a:key ) |
| 295 | + endif |
| 296 | + " |
| 297 | +endfunction " ---------- end of function mmtoolbox#helloworld#Property ---------- |
| 298 | +" |
| 299 | +"------------------------------------------------------------------------------- |
| 300 | +" HelpPlugin : Plugin help. {{{1 |
| 301 | +"------------------------------------------------------------------------------- |
| 302 | +function! mmtoolbox#helloworld#HelpPlugin () |
| 303 | + " TODO: choose a topic other than 'toolbox' |
| 304 | + try |
| 305 | + help toolbox |
| 306 | + catch |
| 307 | + exe 'helptags '.s:plugin_dir.'/doc' |
| 308 | + help toolbox |
| 309 | + endtry |
| 310 | +endfunction " ---------- end of function mmtoolbox#helloworld#HelpPlugin ---------- |
| 311 | +" |
| 312 | +"------------------------------------------------------------------------------- |
| 313 | +" Modul setup (abort early?). {{{1 |
| 314 | +"------------------------------------------------------------------------------- |
| 315 | +if s:Enabled == 0 |
| 316 | + finish |
| 317 | +endif |
| 318 | +" |
| 319 | +"------------------------------------------------------------------------------- |
| 320 | +" Implement : Implement the tool. {{{1 |
| 321 | +"------------------------------------------------------------------------------- |
| 322 | +function! mmtoolbox#helloworld#Implement () |
| 323 | + " |
| 324 | + " TODO |
| 325 | + " |
| 326 | + return |
| 327 | +endfunction " ---------- end of function mmtoolbox#helloworld#Implement ---------- |
| 328 | +" }}}1 |
| 329 | +"------------------------------------------------------------------------------- |
| 330 | +" |
| 331 | +" ===================================================================================== |
| 332 | +" vim: foldmethod=marker |
0 commit comments