Re: [vim/vim] PackAdded autocmd-event (Issue #18714)

ubaldot (Vim Github Repository) <[email protected]>
Newsgroups gmane.editors.vim.devel
Message-ID <vim/vim/issues/18714/[email protected]>
ubaldot left a comment (vim/vim#18714)

> I'd say, that even if you seldomly use my_pack then it makes sense to set those variables. Yes those variables are polluting the global > namespace, but for the reason to configure your pack, whether or not you are using that pack during the current session does not > matter.

It has been spent quite lot to time and effort to prevent sourcing script when not necessary (think for example to `autoload` mechanisms) and this approach goes towards the opposite direction, isn't it? 

Also, if you consider Vim itself, it source .vimrc at startup. I am debating about using the same philosophy for starting packages.  

> Alternatively: create a custom :packadd command, that sets those variables first and loads the pack then.

This is what I actually did and it is extremely handy feature I must say. I am sharing my solution in case someone wants to get inspired (Mind that I have all the packages in `opt`): 

```vim
export def LoadedPackages(): list<string>
  #
  # Return the list of loaded packages in opt and start, both bundled (dist/)
  # and user-defined
  #
  const runtimepath_normalized = split(&runtimepath, ',')
    # Adjust for Windows paths
    ->mapnew((_, val) => substitute(val, '\\', '/', 'g'))

  # Pick the string between .../start and $ or /*$
  const start_packages = runtimepath_normalized
    ->mapnew((_, val) => matchstr(val, 'start/\zs[^/]*$'))

  # Pick the string between .../opt and $ or /*$
  const opt_packages = runtimepath_normalized
    ->mapnew((_, val) => matchstr(val, 'opt/\zs[^/]*$'))

  return copy(start_packages + opt_packages)->filter((_, val) => !empty(val))
enddef

command! -nargs=0 PackLoaded echo LoadedPackages()

export def Packadd(package: string)
  # It works as follow:
  #
  #   1. source a config script
  #   2. load package through :packadd
  #
  # Assume that the config script and the package have the same name, and it
  # assumes that the config scripts are in $'{g:dotvim}/lib/config/' and have the same name of the package

  if v:vim_did_init && index(getcompletion('', 'packadd'), package) == -1
    Echoerr($"Package '{package}' not installed")
    return
  endif

  const packages_dir = $'{g:dotvim}/lib/config/'

  if !empty(readdir(packages_dir, (dir) => dir ==# $'{package}.vim'))
    execute $'source {g:dotvim}/lib/config/{package}.vim'
    # echom $'sourced {g:dotvim}/lib/config/{package}.vim'
  endif

  if v:vim_did_init
    execute $'packadd {package}'
  else
    # echom "package: " .. package
    execute $'packadd! {package}'
  endif
enddef

def PackConfig_CompleteList(arglead: string,
    command_line: string,
    cursor_position: number): list<string>

  var loaded_packages = LoadedPackages()

  var all_packages = getcompletion('', 'packadd')
  var leftovers = copy(all_packages)->filter((_, val) => index(loaded_packages, val) == -1)
  return leftovers->filter($'v:val =~ "^{arglead}"')
enddef

command! -nargs=1 -complete=customlist,PackConfig_CompleteList Packadd Packadd(<f-args>)

# What is in my .vimrc

const packages_to_load = [
  'comment',
  'hlyank',
  'vim-outline',
  'fern.vim',
  'vim-calendar',
  # 'minpac',
  # 'copilot-chat.vim',
  'vim-helpme',
  'vim-markdown-extras',
  'vim-poptools',
]

for pack in packages_to_load
  # No need to reload if a package is already loaded
  if index(myfunctions.LoadedPackages(), pack) == -1
    myfunctions.Packadd(pack)
  endif
endfor
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/vim/vim/issues/18714#issuecomment-5379367334
You are receiving this because you are subscribed to this thread.

Message ID: <vim/vim/issues/18714/[email protected]>

-- 
-- 
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/18714/5379367334%40github.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.