Home Explore Blog CI



neovim

23th chunk of `runtime/doc/usr_41.txt`
f534542ac639994f9ffb2a2b3dd3c711b5b4036dbdc2fe770000000100000fa0
 set will run into trouble here, they will get an error
message.  We can't just reset 'compatible', because that has a lot of side
effects.  To avoid this, we will set the 'cpoptions' option to its Vim default
value and restore it later.  That will allow the use of line-continuation and
make the script work for most people.  It is done like this: >

 11	let s:save_cpo = &cpo
 12	set cpo&vim
 ..
 42	let &cpo = s:save_cpo
 43	unlet s:save_cpo

We first store the old value of 'cpoptions' in the s:save_cpo variable.  At
the end of the plugin this value is restored.

Notice that a script-local variable is used |s:var|.  A global variable could
already be in use for something else.  Always use script-local variables for
things that are only used in the script.


NOT LOADING

It's possible that a user doesn't always want to load this plugin.  Or the
system administrator has dropped it in the system-wide plugin directory, but a
user has their own plugin they want to use.  Then the user must have a chance to
disable loading this specific plugin.  This will make it possible: >

  6	if exists("g:loaded_typecorr")
  7	  finish
  8	endif
  9	let g:loaded_typecorr = 1

This also avoids that when the script is loaded twice it would cause error
messages for redefining functions and cause trouble for autocommands that are
added twice.

The name is recommended to start with "loaded_" and then the file name of the
plugin, literally.  The "g:" is prepended just to avoid mistakes when using
the variable in a function (without "g:" it would be a variable local to the
function).

Using "finish" stops Vim from reading the rest of the file, it's much quicker
than using if-endif around the whole file.


MAPPING

Now let's make the plugin more interesting: We will add a mapping that adds a
correction for the word under the cursor.  We could just pick a key sequence
for this mapping, but the user might already use it for something else.  To
allow the user to define which keys a mapping in a plugin uses, the <Leader>
item can be used: >

 22	  map <unique> <Leader>a  <Plug>TypecorrAdd;

The "<Plug>TypecorrAdd;" thing will do the work, more about that further on.

The user can set the "mapleader" variable to the key sequence that they want
this mapping to start with.  Thus if the user has done: >

	let mapleader = "_"

the mapping will define "_a".  If the user didn't do this, the default value
will be used, which is a backslash.  Then a map for "\a" will be defined.

Note that <unique> is used, this will cause an error message if the mapping
already happened to exist. |:map-<unique>|

But what if the user wants to define their own key sequence?  We can allow that
with this mechanism: >

 21	if !hasmapto('<Plug>TypecorrAdd;')
 22	  map <unique> <Leader>a  <Plug>TypecorrAdd;
 23	endif

This checks if a mapping to "<Plug>TypecorrAdd;" already exists, and only
defines the mapping from "<Leader>a" if it doesn't.  The user then has a
chance of putting this in their vimrc file: >

	map ,c  <Plug>TypecorrAdd;

Then the mapped key sequence will be ",c" instead of "_a" or "\a".


PIECES

If a script gets longer, you often want to break up the work in pieces.  You
can use functions or mappings for this.  But you don't want these functions
and mappings to interfere with the ones from other scripts.  For example, you
could define a function Add(), but another script could try to define the same
function.  To avoid this, we define the function local to the script by
prepending it with "s:".

We will define a function that adds a new typing correction: >

 30	function s:Add(from, correct)
 31	  let to = input("type the correction for " .. a:from .. ": ")
 32	  exe ":iabbrev " .. a:from .. " " .. to
 ..
 36	endfunction

Now we can call the function s:Add() from within this script.  If another
script also defines s:Add(), it will be local to that script and can only
be called from the script it was defined in.  There can also be a global Add()
function (without the "s:"),

Title: Preventing Plugin Loading, Mapping Keys, and Defining Local Functions
Summary
This section discusses how to prevent a plugin from loading if a user doesn't want it, by checking for a global variable (e.g., `g:loaded_typecorr`). It explains the use of `<Leader>` for creating user-configurable mappings and how to check if a mapping to a specific `<Plug>` already exists to avoid conflicts. It also covers breaking up a plugin into smaller pieces using script-local functions (prefixed with `s:`) to prevent naming conflicts with other scripts.