synchronisation
18 \ synchronization
19 let s:count = 4
20
21 if !hasmapto('<Plug>TypecorrAdd;')
22 map <unique> <Leader>a <Plug>TypecorrAdd;
23 endif
24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add
25
26 noremenu <script> Plugin.Add\ Correction <SID>Add
27
28 noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
29
30 function s:Add(from, correct)
31 let to = input("type the correction for " .. a:from .. ": ")
32 exe ":iabbrev " .. a:from .. " " .. to
33 if a:correct | exe "normal viws\<C-R>\" \b\e" | endif
34 let s:count = s:count + 1
35 echo s:count .. " corrections now"
36 endfunction
37
38 if !exists(":Correct")
39 command -nargs=1 Correct :call s:Add(<q-args>, 0)
40 endif
41
42 let &cpo = s:save_cpo
43 unlet s:save_cpo
Line 33 wasn't explained yet. It applies the new correction to the word under
the cursor. The |:normal| command is used to use the new abbreviation. Note
that mappings and abbreviations are expanded here, even though the function
was called from a mapping defined with ":noremap".
Using "unix" for the 'fileformat' option is recommended. The Vim scripts will
then work everywhere. Scripts with 'fileformat' set to "dos" do not work on
Unix. Also see |:source_crnl|. To be sure it is set right, do this before
writing the file: >
:set fileformat=unix
DOCUMENTATION *write-local-help*
It's a good idea to also write some documentation for your plugin. Especially
when its behavior can be changed by the user. See |add-local-help| for how
they are installed.
Here is a simple example for a plugin help file, called "typecorr.txt": >
1 *typecorr.txt* Plugin for correcting typing mistakes
2
3 If you make typing mistakes, this plugin will have them corrected
4 automatically.
5
6 There are currently only a few corrections. Add your own if you like.
7
8 Mappings:
9 <Leader>a or <Plug>TypecorrAdd;
10 Add a correction for the word under the cursor.
11
12 Commands:
13 :Correct {word}
14 Add a correction for {word}.
15
16 *typecorr-settings*
17 This plugin doesn't have any settings.
The first line is actually the only one for which the format matters. It will
be extracted from the help file to be put in the "LOCAL ADDITIONS:" section of
help.txt |local-additions|. The first "*" must be in the first column of the
first line. After adding your help file do ":help" and check that the entries
line up nicely.
You can add more tags inside ** in your help file. But be careful not to use
existing help tags. You would probably use the name of your plugin in most of
them, like "typecorr-settings" in the example.
Using references to other parts of the help in || is recommended. This makes
it easy for the user to find associated help.
FILETYPE DETECTION *plugin-filetype*
If your filetype is not already detected by Vim, you should create a filetype
detection snippet in a separate file. It is usually in the form of an
autocommand that sets the filetype when the file name matches a pattern.
Example: >
au BufNewFile,BufRead *.foo set filetype=foofoo
Write this single-line file as "ftdetect/foofoo.vim" in the first directory
that appears in 'runtimepath'. For Unix that would be
"~/.config/nvim/ftdetect/foofoo.vim". The convention is to use the name of
the filetype for the script name.
You can make more complicated checks if you like, for example to inspect the
contents of the file to recognize the language. Also see |new-filetype|.
SUMMARY *plugin-special*
Summary of special things to use in a plugin:
s:name Variables local to the script.
<SID> Script-ID, used for mappings and functions local to
the script.
hasmapto() Function to test if the user already defined a mapping
for functionality the script offers.
<Leader> Value of "mapleader", which the user defines as the
keys that plugin mappings start with.
:map <unique> Give a warning if a mapping already exists.
:noremap