@@ -3640,13 +3640,11 @@ source_all_matches(char_u *pat)
36403640 }
36413641}
36423642
3643- /* used for "cookie" of add_pack_plugin() */
3644- static int APP_ADD_DIR ;
3645- static int APP_LOAD ;
3646- static int APP_BOTH ;
3647-
3648- static void
3649- add_pack_plugin (char_u * fname , void * cookie )
3643+ /*
3644+ * Add the package directory to 'runtimepath'.
3645+ */
3646+ static int
3647+ add_pack_dir_to_rtp (char_u * fname )
36503648{
36513649 char_u * p4 , * p3 , * p2 , * p1 , * p ;
36523650 char_u * insp ;
@@ -3655,125 +3653,154 @@ add_pack_plugin(char_u *fname, void *cookie)
36553653 int keep ;
36563654 size_t oldlen ;
36573655 size_t addlen ;
3658- char_u * afterdir ;
3656+ char_u * afterdir = NULL ;
36593657 size_t afterlen = 0 ;
3660- char_u * ffname = fix_fname ( fname ) ;
3658+ char_u * ffname = NULL ;
36613659 size_t fname_len ;
36623660 char_u * buf = NULL ;
36633661 char_u * rtp_ffname ;
36643662 int match ;
3663+ int retval = FAIL ;
36653664
3666- if (ffname == NULL )
3667- return ;
3668- if (cookie != & APP_LOAD && strstr ((char * )p_rtp , (char * )ffname ) == NULL )
3669- {
3670- /* directory is not yet in 'runtimepath', add it */
3671- p4 = p3 = p2 = p1 = get_past_head (ffname );
3672- for (p = p1 ; * p ; MB_PTR_ADV (p ))
3673- if (vim_ispathsep_nocolon (* p ))
3674- {
3675- p4 = p3 ; p3 = p2 ; p2 = p1 ; p1 = p ;
3676- }
3677-
3678- /* now we have:
3679- * rtp/pack/name/start/name
3680- * p4 p3 p2 p1
3681- *
3682- * find the part up to "pack" in 'runtimepath' */
3683- c = * p4 ;
3684- * p4 = NUL ;
3685-
3686- /* Find "ffname" in "p_rtp", ignoring '/' vs '\' differences. */
3687- fname_len = STRLEN (ffname );
3688- insp = p_rtp ;
3689- buf = alloc (MAXPATHL );
3690- if (buf == NULL )
3691- goto theend ;
3692- while (* insp != NUL )
3665+ p4 = p3 = p2 = p1 = get_past_head (fname );
3666+ for (p = p1 ; * p ; MB_PTR_ADV (p ))
3667+ if (vim_ispathsep_nocolon (* p ))
36933668 {
3694- copy_option_part (& insp , buf , MAXPATHL , "," );
3695- add_pathsep (buf );
3696- rtp_ffname = fix_fname (buf );
3697- if (rtp_ffname == NULL )
3698- goto theend ;
3699- match = vim_fnamencmp (rtp_ffname , ffname , fname_len ) == 0 ;
3700- vim_free (rtp_ffname );
3701- if (match )
3702- break ;
3669+ p4 = p3 ; p3 = p2 ; p2 = p1 ; p1 = p ;
37033670 }
37043671
3705- if (* insp == NUL )
3706- /* not found, append at the end */
3707- insp = p_rtp + STRLEN (p_rtp );
3708- else
3709- /* append after the matching directory. */
3710- -- insp ;
3711- * p4 = c ;
3712-
3713- /* check if rtp/pack/name/start/name/after exists */
3714- afterdir = concat_fnames (ffname , (char_u * )"after" , TRUE);
3715- if (afterdir != NULL && mch_isdir (afterdir ))
3716- afterlen = STRLEN (afterdir ) + 1 ; /* add one for comma */
3717-
3718- oldlen = STRLEN (p_rtp );
3719- addlen = STRLEN (ffname ) + 1 ; /* add one for comma */
3720- new_rtp = alloc ((int )(oldlen + addlen + afterlen + 1 ));
3721- /* add one for NUL */
3722- if (new_rtp == NULL )
3672+ /* now we have:
3673+ * rtp/pack/name/start/name
3674+ * p4 p3 p2 p1
3675+ *
3676+ * find the part up to "pack" in 'runtimepath' */
3677+ c = * ++ p4 ; /* append pathsep in order to expand symlink */
3678+ * p4 = NUL ;
3679+ ffname = fix_fname (fname );
3680+ * p4 = c ;
3681+ if (ffname == NULL )
3682+ return FAIL ;
3683+
3684+ /* Find "ffname" in "p_rtp", ignoring '/' vs '\' differences. */
3685+ fname_len = STRLEN (ffname );
3686+ insp = p_rtp ;
3687+ buf = alloc (MAXPATHL );
3688+ if (buf == NULL )
3689+ goto theend ;
3690+ while (* insp != NUL )
3691+ {
3692+ copy_option_part (& insp , buf , MAXPATHL , "," );
3693+ add_pathsep (buf );
3694+ rtp_ffname = fix_fname (buf );
3695+ if (rtp_ffname == NULL )
37233696 goto theend ;
3724- keep = (int )(insp - p_rtp );
3725- mch_memmove (new_rtp , p_rtp , keep );
3726- new_rtp [keep ] = ',' ;
3727- mch_memmove (new_rtp + keep + 1 , ffname , addlen );
3728- if (p_rtp [keep ] != NUL )
3729- mch_memmove (new_rtp + keep + addlen , p_rtp + keep ,
3730- oldlen - keep + 1 );
3731- if (afterlen > 0 )
3732- {
3733- STRCAT (new_rtp , "," );
3734- STRCAT (new_rtp , afterdir );
3735- }
3736- set_option_value ((char_u * )"rtp" , 0L , new_rtp , 0 );
3737- vim_free (new_rtp );
3738- vim_free (afterdir );
3697+ match = vim_fnamencmp (rtp_ffname , ffname , fname_len ) == 0 ;
3698+ vim_free (rtp_ffname );
3699+ if (match )
3700+ break ;
37393701 }
37403702
3741- if (cookie != & APP_ADD_DIR )
3742- {
3743- static char * plugpat = "%s/plugin/**/*.vim" ;
3744- static char * ftpat = "%s/ftdetect/*.vim" ;
3745- int len ;
3746- char_u * pat ;
3703+ if (* insp == NUL )
3704+ /* not found, append at the end */
3705+ insp = p_rtp + STRLEN (p_rtp );
3706+ else
3707+ /* append after the matching directory. */
3708+ -- insp ;
3709+
3710+ /* check if rtp/pack/name/start/name/after exists */
3711+ afterdir = concat_fnames (fname , (char_u * )"after" , TRUE);
3712+ if (afterdir != NULL && mch_isdir (afterdir ))
3713+ afterlen = STRLEN (afterdir ) + 1 ; /* add one for comma */
3714+
3715+ oldlen = STRLEN (p_rtp );
3716+ addlen = STRLEN (fname ) + 1 ; /* add one for comma */
3717+ new_rtp = alloc ((int )(oldlen + addlen + afterlen + 1 ));
3718+ /* add one for NUL */
3719+ if (new_rtp == NULL )
3720+ goto theend ;
3721+ keep = (int )(insp - p_rtp );
3722+ mch_memmove (new_rtp , p_rtp , keep );
3723+ new_rtp [keep ] = ',' ;
3724+ mch_memmove (new_rtp + keep + 1 , fname , addlen );
3725+ if (p_rtp [keep ] != NUL )
3726+ mch_memmove (new_rtp + keep + addlen , p_rtp + keep , oldlen - keep + 1 );
3727+ if (afterlen > 0 )
3728+ {
3729+ STRCAT (new_rtp , "," );
3730+ STRCAT (new_rtp , afterdir );
3731+ }
3732+ set_option_value ((char_u * )"rtp" , 0L , new_rtp , 0 );
3733+ vim_free (new_rtp );
3734+ retval = OK ;
37473735
3748- len = (int )STRLEN (ffname ) + (int )STRLEN (ftpat );
3749- pat = alloc (len );
3750- if (pat == NULL )
3751- goto theend ;
3752- vim_snprintf ((char * )pat , len , plugpat , ffname );
3753- source_all_matches (pat );
3736+ theend :
3737+ vim_free (buf );
3738+ vim_free (ffname );
3739+ vim_free (afterdir );
3740+ return retval ;
3741+ }
3742+
3743+ /*
3744+ * Load scripts in "plugin" and "ftdetect" directories of the package.
3745+ */
3746+ static int
3747+ load_pack_plugin (char_u * fname )
3748+ {
3749+ static char * plugpat = "%s/plugin/**/*.vim" ;
3750+ static char * ftpat = "%s/ftdetect/*.vim" ;
3751+ int len ;
3752+ char_u * ffname = fix_fname (fname );
3753+ char_u * pat = NULL ;
3754+ int retval = FAIL ;
3755+
3756+ if (ffname == NULL )
3757+ return FAIL ;
3758+ len = (int )STRLEN (ffname ) + (int )STRLEN (ftpat );
3759+ pat = alloc (len );
3760+ if (pat == NULL )
3761+ goto theend ;
3762+ vim_snprintf ((char * )pat , len , plugpat , ffname );
3763+ source_all_matches (pat );
37543764
37553765#ifdef FEAT_AUTOCMD
3756- {
3757- char_u * cmd = vim_strsave ((char_u * )"g:did_load_filetypes" );
3766+ {
3767+ char_u * cmd = vim_strsave ((char_u * )"g:did_load_filetypes" );
37583768
3759- /* If runtime/filetype.vim wasn't loaded yet, the scripts will be
3760- * found when it loads. */
3761- if (cmd != NULL && eval_to_number (cmd ) > 0 )
3762- {
3763- do_cmdline_cmd ((char_u * )"augroup filetypedetect" );
3764- vim_snprintf ((char * )pat , len , ftpat , ffname );
3765- source_all_matches (pat );
3766- do_cmdline_cmd ((char_u * )"augroup END" );
3767- }
3768- vim_free (cmd );
3769+ /* If runtime/filetype.vim wasn't loaded yet, the scripts will be
3770+ * found when it loads. */
3771+ if (cmd != NULL && eval_to_number (cmd ) > 0 )
3772+ {
3773+ do_cmdline_cmd ((char_u * )"augroup filetypedetect" );
3774+ vim_snprintf ((char * )pat , len , ftpat , ffname );
3775+ source_all_matches (pat );
3776+ do_cmdline_cmd ((char_u * )"augroup END" );
37693777 }
3770- #endif
3771- vim_free (pat );
3778+ vim_free (cmd );
37723779 }
3780+ #endif
3781+ vim_free (pat );
3782+ retval = OK ;
37733783
37743784theend :
3775- vim_free (buf );
37763785 vim_free (ffname );
3786+ return retval ;
3787+ }
3788+
3789+ /* used for "cookie" of add_pack_plugin() */
3790+ static int APP_ADD_DIR ;
3791+ static int APP_LOAD ;
3792+ static int APP_BOTH ;
3793+
3794+ static void
3795+ add_pack_plugin (char_u * fname , void * cookie )
3796+ {
3797+ if (cookie != & APP_LOAD && strstr ((char * )p_rtp , (char * )fname ) == NULL )
3798+ /* directory is not yet in 'runtimepath', add it */
3799+ if (add_pack_dir_to_rtp (fname ) == FAIL )
3800+ return ;
3801+
3802+ if (cookie != & APP_ADD_DIR )
3803+ load_pack_plugin (fname );
37773804}
37783805
37793806/*
0 commit comments