Skip to content

Commit f365482

Browse files
committed
patch 7.4.1486
Problem: ":loadplugin" is not optimal, some people find it confusing. Solution: Only use ":packadd" with an optional "!".
1 parent 014069a commit f365482

6 files changed

Lines changed: 107 additions & 103 deletions

File tree

runtime/doc/repeat.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ You would now have these files under ~/.vim:
433433
pack/my/ever/always/syntax/always.vim
434434
pack/my/opt/mydebug/plugin/debugger.vim
435435

436+
If you don't have a package but a single plugin, you need to create the extra
437+
directory level:
438+
% mkdir -p ~/.vim/pack/my/ever/always
439+
% cd ~/.vim/pack/my/ever/always
440+
% unzip /tmp/myplugin.zip
441+
436442
When Vim starts up it scans all directories in 'packpath' for plugins under the
437443
"ever" directory and loads them. When found that directory is added to
438444
'runtimepath'.
@@ -443,11 +449,11 @@ In the example Vim will find "my/ever/always/plugin/always.vim" and adds
443449
If the "always" plugin kicks in and sets the 'filetype' to "always", Vim will
444450
find the syntax/always.vim file, because its directory is in 'runtimepath'.
445451

446-
Vim will also load ftdetect files, like with |:loadplugin|.
452+
Vim will also load ftdetect files, like with |:packadd|.
447453

448-
*load-plugin*
449-
To load an optional plugin from a pack use the `:loadplugin` command: >
450-
:loadplugin mydebug
454+
*pack-add*
455+
To load an optional plugin from a pack use the `:packadd` command: >
456+
:packadd mydebug
451457
This could be done inside always.vim, if some conditions are met.
452458
Or you could add this command to your |.vimrc|.
453459

src/ex_cmds.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,6 @@ EX(CMD_loadview, "loadview", ex_loadview,
810810
EX(CMD_loadkeymap, "loadkeymap", ex_loadkeymap,
811811
CMDWIN,
812812
ADDR_LINES),
813-
EX(CMD_loadplugin, "loadplugin", ex_loadplugin,
814-
BANG|FILE1|TRLBAR|SBOXOK|CMDWIN,
815-
ADDR_LINES),
816813
EX(CMD_lockmarks, "lockmarks", ex_wrongmodifier,
817814
NEEDARG|EXTRA|NOTRLCOM,
818815
ADDR_LINES),

src/ex_cmds2.c

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,88 +3057,75 @@ do_in_runtimepath(
30573057
return do_in_path(p_rtp, name, all ? DIP_ALL : 0, callback, cookie);
30583058
}
30593059

3060-
#ifdef FEAT_AUTOCMD
30613060
/*
3062-
* Source filetype detection scripts, if filetype.vim was already done.
3061+
* Expand wildcards in "pat" and invoke do_source() for each match.
30633062
*/
30643063
static void
3065-
may_do_filetypes(char_u *pat)
3064+
source_all_matches(char_u *pat)
30663065
{
3067-
char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
3066+
int num_files;
3067+
char_u **files;
3068+
int i;
30683069

3069-
/* If runtime/filetype.vim wasn't loaded yet, the scripts will be found
3070-
* when it loads. */
3071-
if (cmd != NULL && eval_to_number(cmd) > 0)
3070+
if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
30723071
{
3073-
do_cmdline_cmd((char_u *)"augroup filetypedetect");
3074-
do_in_path(p_pp, pat, DIP_ALL, source_callback, NULL);
3075-
do_cmdline_cmd((char_u *)"augroup END");
3072+
for (i = 0; i < num_files; ++i)
3073+
(void)do_source(files[i], FALSE, DOSO_NONE);
3074+
FreeWild(num_files, files);
30763075
}
3077-
vim_free(cmd);
30783076
}
3079-
#endif
30803077

30813078
static void
30823079
add_pack_plugin(char_u *fname, void *cookie)
30833080
{
3084-
char_u *p6, *p5, *p4, *p3, *p2, *p1, *p;
3081+
char_u *p4, *p3, *p2, *p1, *p;
3082+
char_u *insp;
30853083
int c;
30863084
char_u *new_rtp;
30873085
int keep;
30883086
int oldlen;
30893087
int addlen;
30903088
char_u *ffname = fix_fname(fname);
3091-
int load_file = cookie != NULL;
3089+
int load_files = cookie != NULL;
30923090

30933091
if (ffname == NULL)
30943092
return;
3095-
p6 = p5 = p4 = p3 = p2 = p1 = get_past_head(ffname);
3096-
for (p = p1; *p; mb_ptr_adv(p))
3097-
if (vim_ispathsep_nocolon(*p))
3098-
{
3099-
p6 = p5; p5 = p4; p4 = p3; p3 = p2; p2 = p1; p1 = p;
3100-
}
3101-
3102-
/* now we have, load_file == TRUE:
3103-
* rtp/pack/name/ever/name/plugin/name.vim
3104-
* p6 p5 p4 p3 p2 p1
3105-
*
3106-
* with load_file == FALSE:
3107-
* rtp/pack/name/ever/name
3108-
* p4 p3 p2 p1
3109-
*/
3110-
if (load_file)
3111-
p4 = p6;
3112-
3113-
/* find the part up to "pack" in 'runtimepath' */
3114-
c = *p4;
3115-
*p4 = NUL;
3116-
p = (char_u *)strstr((char *)p_rtp, (char *)ffname);
3117-
if (p == NULL)
3118-
/* not found, append at the end */
3119-
p = p_rtp + STRLEN(p_rtp);
3120-
else
3121-
/* append after the matching directory. */
3122-
p += STRLEN(ffname);
3123-
*p4 = c;
3124-
3125-
if (load_file)
3126-
{
3127-
c = *p2;
3128-
*p2 = NUL;
3129-
}
31303093
if (strstr((char *)p_rtp, (char *)ffname) == NULL)
31313094
{
31323095
/* directory not in 'runtimepath', add it */
3096+
p4 = p3 = p2 = p1 = get_past_head(ffname);
3097+
for (p = p1; *p; mb_ptr_adv(p))
3098+
if (vim_ispathsep_nocolon(*p))
3099+
{
3100+
p4 = p3; p3 = p2; p2 = p1; p1 = p;
3101+
}
3102+
3103+
/* now we have:
3104+
* rtp/pack/name/ever/name
3105+
* p4 p3 p2 p1
3106+
*
3107+
* find the part up to "pack" in 'runtimepath' */
3108+
c = *p4;
3109+
*p4 = NUL;
3110+
insp = (char_u *)strstr((char *)p_rtp, (char *)ffname);
3111+
if (insp == NULL)
3112+
/* not found, append at the end */
3113+
insp = p_rtp + STRLEN(p_rtp);
3114+
else
3115+
{
3116+
/* append after the matching directory. */
3117+
insp += STRLEN(ffname);
3118+
while (*insp != NUL && *insp != ',')
3119+
++insp;
3120+
}
3121+
*p4 = c;
3122+
31333123
oldlen = (int)STRLEN(p_rtp);
31343124
addlen = (int)STRLEN(ffname);
31353125
new_rtp = alloc(oldlen + addlen + 2);
31363126
if (new_rtp == NULL)
3137-
{
3138-
*p2 = c;
3139-
return;
3140-
}
3141-
keep = (int)(p - p_rtp);
3127+
goto theend;
3128+
keep = (int)(insp - p_rtp);
31423129
mch_memmove(new_rtp, p_rtp, keep);
31433130
new_rtp[keep] = ',';
31443131
mch_memmove(new_rtp + keep + 1, ffname, addlen + 1);
@@ -3148,53 +3135,55 @@ add_pack_plugin(char_u *fname, void *cookie)
31483135
set_option_value((char_u *)"rtp", 0L, new_rtp, 0);
31493136
vim_free(new_rtp);
31503137
}
3151-
vim_free(ffname);
31523138

3153-
if (load_file)
3154-
(void)do_source(fname, FALSE, DOSO_NONE);
3155-
}
3139+
if (load_files)
3140+
{
3141+
static char *plugpat = "%s/plugin/*.vim";
3142+
static char *ftpat = "%s/ftdetect/*.vim";
3143+
int len;
3144+
char_u *pat;
3145+
3146+
len = (int)STRLEN(ffname) + (int)STRLEN(ftpat);
3147+
pat = alloc(len);
3148+
if (pat == NULL)
3149+
goto theend;
3150+
vim_snprintf((char *)pat, len, plugpat, ffname);
3151+
source_all_matches(pat);
31563152

3157-
/*
3158-
* Source the plugins in the package directories.
3159-
*/
3160-
void
3161-
source_packages()
3162-
{
3163-
do_in_path(p_pp, (char_u *)"pack/*/ever/*/plugin/*.vim",
3164-
DIP_ALL, add_pack_plugin, p_pp);
31653153
#ifdef FEAT_AUTOCMD
3166-
may_do_filetypes((char_u *)"pack/*/ever/*/ftdetect/*.vim");
3154+
{
3155+
char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes");
3156+
3157+
/* If runtime/filetype.vim wasn't loaded yet, the scripts will be
3158+
* found when it loads. */
3159+
if (cmd != NULL && eval_to_number(cmd) > 0)
3160+
{
3161+
do_cmdline_cmd((char_u *)"augroup filetypedetect");
3162+
vim_snprintf((char *)pat, len, ftpat, ffname);
3163+
source_all_matches(pat);
3164+
do_cmdline_cmd((char_u *)"augroup END");
3165+
}
3166+
vim_free(cmd);
3167+
}
31673168
#endif
3169+
}
3170+
3171+
theend:
3172+
vim_free(ffname);
31683173
}
31693174

31703175
/*
3171-
* ":loadplugin {name}"
3176+
* Find plugins in the package directories and source them.
31723177
*/
31733178
void
3174-
ex_loadplugin(exarg_T *eap)
3179+
source_packages()
31753180
{
3176-
static char *plugpat = "pack/*/opt/%s/plugin/*.vim";
3177-
static char *ftpat = "pack/*/opt/%s/ftdetect/*.vim";
3178-
int len;
3179-
char *pat;
3180-
3181-
len = (int)STRLEN(ftpat) + (int)STRLEN(eap->arg);
3182-
pat = (char *)alloc(len);
3183-
if (pat == NULL)
3184-
return;
3185-
vim_snprintf(pat, len, plugpat, eap->arg);
3186-
do_in_path(p_pp, (char_u *)pat, DIP_ALL, add_pack_plugin, p_pp);
3187-
3188-
#ifdef FEAT_AUTOCMD
3189-
vim_snprintf(pat, len, ftpat, eap->arg);
3190-
may_do_filetypes((char_u *)pat);
3191-
#endif
3192-
3193-
vim_free(pat);
3181+
do_in_path(p_pp, (char_u *)"pack/*/ever/*",
3182+
DIP_ALL + DIP_DIR, add_pack_plugin, p_pp);
31943183
}
31953184

31963185
/*
3197-
* ":packadd {name}"
3186+
* ":packadd[!] {name}"
31983187
*/
31993188
void
32003189
ex_packadd(exarg_T *eap)
@@ -3208,7 +3197,8 @@ ex_packadd(exarg_T *eap)
32083197
if (pat == NULL)
32093198
return;
32103199
vim_snprintf(pat, len, plugpat, eap->arg);
3211-
do_in_path(p_pp, (char_u *)pat, DIP_ALL + DIP_DIR, add_pack_plugin, NULL);
3200+
do_in_path(p_pp, (char_u *)pat, DIP_ALL + DIP_DIR, add_pack_plugin,
3201+
eap->forceit ? NULL : p_pp);
32123202
vim_free(pat);
32133203
}
32143204

src/testdir/Make_all.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ NEW_TESTS = test_arglist.res \
178178
test_increment.res \
179179
test_json.res \
180180
test_langmap.res \
181-
test_loadplugin.res \
181+
test_packadd.res \
182182
test_perl.res \
183183
test_quickfix.res \
184184
test_syntax.res \
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
" Tests for :loadplugin
1+
" Tests for 'packpath' and :packadd
22

33
func SetUp()
44
let s:topdir = expand('%:h') . '/Xdir'
@@ -10,7 +10,7 @@ func TearDown()
1010
call delete(s:topdir, 'rf')
1111
endfunc
1212

13-
func Test_loadplugin()
13+
func Test_packadd()
1414
call mkdir(s:plugdir . '/plugin', 'p')
1515
call mkdir(s:plugdir . '/ftdetect', 'p')
1616
set rtp&
@@ -25,24 +25,33 @@ func Test_loadplugin()
2525
call setline(1, 'let g:ftdetect_works = 17')
2626
wq
2727

28-
loadplugin mytest
28+
packadd mytest
2929

3030
call assert_equal(42, g:plugin_works)
3131
call assert_equal(17, g:ftdetect_works)
3232
call assert_true(len(&rtp) > len(rtp))
3333
call assert_true(&rtp =~ 'testdir/Xdir/pack/mine/opt/mytest\($\|,\)')
3434
endfunc
3535

36-
func Test_packadd()
36+
func Test_packadd_noload()
37+
call mkdir(s:plugdir . '/plugin', 'p')
3738
call mkdir(s:plugdir . '/syntax', 'p')
3839
set rtp&
3940
let rtp = &rtp
40-
packadd mytest
41+
42+
exe 'split ' . s:plugdir . '/plugin/test.vim'
43+
call setline(1, 'let g:plugin_works = 42')
44+
wq
45+
let g:plugin_works = 0
46+
47+
packadd! mytest
48+
4149
call assert_true(len(&rtp) > len(rtp))
4250
call assert_true(&rtp =~ 'testdir/Xdir/pack/mine/opt/mytest\($\|,\)')
51+
call assert_equal(0, g:plugin_works)
4352

4453
" check the path is not added twice
4554
let new_rtp = &rtp
46-
packadd mytest
55+
packadd! mytest
4756
call assert_equal(new_rtp, &rtp)
4857
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ static char *(features[]) =
743743

744744
static int included_patches[] =
745745
{ /* Add new patch number below this line */
746+
/**/
747+
1486,
746748
/**/
747749
1485,
748750
/**/

0 commit comments

Comments
 (0)