Wednesday, November 20, 2024

Re: load two plugins for one file type

Hello,

Riza Dindir schrieb am 21.11.2024 um 04:14:
> Thank you. Have created a plugin file and put that into the ~/.vim/after/ftplugin. It is working.
>
> But one thing that I was doing in the plugin script was setting foldmethod, foldexpr, and foldtext with setlocal. But these were not being run because they were put after the "if exists" check I am doing in the plugin so that it is not loaded twice. I moved these before the "if exists" check. Is this the way to do it for plugins? Or should these be done in another file?
>
> Here is the start of the plugin...
>
> // Should these be moved somewhere else. This is a file type plugin.
> setlocal foldmethod=expr foldexpr=PodFold()
> setlocal foldtext=PodFoldText()
>
> if exists("g:loaded_podotl")
>         finish
> endif
> let g:loaded_podotl = 1

here you define a global variable when the filetype plugin is loaded for the first time.
For every file of the same type that is loaded after that, execution of the filetype plugin
will now be terminated directly. For global, not file type dependent plugins this makes sense,
but for filetype plugins you almost always don't want this behavior, especially if you set
local options.

If you really want to prevent a second loading of the filetype plugin for the same buffer
you should use a buffer-local variable as guard, e.g.

if exists("b:loaded_podotl")
finish
endif
let b:loaded_podotl = 1

This variable does not even have to have a name with the file type in it. For example, the
filetype plugins provided by Vim all use the buffer-local variable b:did_ftplugin as guard.

If you don't do expensive calculations in your after-filetype-plugin I wouldn't bother to
include that check. The filetype plugin will only be executed a second time if you reload
the buffer anyway, and then it might actually make sense to set the options like you did
when you loaded the file the first time. (For example, I have not a single instance of this
code in my ~/.vim/after/ftplugin directly -- admittedly, there are only 21 vim scripts, but
some of them set a lot of options or define many mappings and abbreviations.)

Regards,
Jürgen

--
~
~
~
:wq

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/dc00135b-437f-42f6-87cc-c00ee070ab74%40googlemail.com.

No comments:

Post a Comment