On 04 February 2022 08:30, Tim Cross <theophil...@gmail.com> wrote: > I'm assuming it is, but I have to admit I'm still not 100% clear on > how Emacs handles the situation where you use a library that is both > built-in and available in ELPA. Does Emacs use the latest version > available or does it use the built-in version until you explicitly > select the ELPA versions?
Welp, I happened to ponder the same question after trying to implement a function to install packages that are not installed already (I'm not using any helper configuration such as use-package, which would already handle this). From what I gathered empirically, it appears that if something is built-in, Emacs' package.el won't try to install -- it checks with package-installed-p on any call to package-install, which checks if the package is built-in with package-built-in-p as a fallback on cond --, but you can force installation from the archives by passing a package description object (defined as package-desc on package.el) instead of a symbol (let ((pkg-desc (assq 'org package-archive-contents))) (package-install pkg-desc)) and Emacs seems to be loading the newest version when it is a dependency of something else. Though I'm not really sure, as most of the packages I have from ELPA have the same version as the ones built-in on Emacs 28.0.91. I'll try and install Emacs 27 to check this out. In the mean time, I guess this would be a good opportunity to share a couple of functions I have with the purpose of installing packages. (defun pkg-description (package) "Return the description for PACKAGE. If PACKAGE is installed, the will be present on `package-alist', otherwise look for it in `package-archive-contents'." (or (cadr (assoc package package-alist)) (cadr (assoc package package-archive-contents)))) (defun pkg-ensure-archive (package) "Install PACKAGE from the archives, if not already installed." (when-let ((pkg-desc (pkg-description package))) (unless (package-installed-p pkg-desc) (package-install-from-archive pkg-desc)))) (defun require-package (package &optional force) "Ensure that PACKAGE is installed. If FORCE is non-nil, force installation regardless if PACKAGE is built-in or not. First, use `package-installed-p' to check if PACKAGE was installed via the Emacs package manager, otherwise, try to `require' PACKAGE; this ensures that we don't require PACKAGE if it was installed using the package manager. If both of those fail, run `package-refresh-contents' and install PACKAGE." (unless (and (not force) (or (package-installed-p package) (require package nil 'no-error))) (unless (assoc package package-archive-contents) (package-refresh-contents)) (if force (pkg-ensure-archive package) (package-install package)))) On the last one, the main function that I use, if FORCE is non-nil it will download and install the package from the archives even if it is built-in. Best regards, -- João Pedro de Amorim Paula IT undergraduate at Universidade Federal do Rio Grande do Norte (UFRN)