I decided to create a portable Emacs config and install packages I use if it's necessary on a fresh computer.
Here is what I've done:
; loop through names of packages and install them if needed
(let ((packages '('use-package 'yasnippet 'expand-region 'bm 'undo-tree))
(content-not-refreshed t)
)
(mapc
(lambda (name)
(unless (package-installed-p (eval name))
(when content-not-refreshed
(package-refresh-contents)
(setq content-not-refreshed nil))
(package-install (eval name))))
packages
))
; load packages
(require 'use-package)
(require 'yasnippet)
(require 'expand-region)
(require 'bm)
(require 'undo-tree)
Is this code good? Can it be improved?
1 Answer 1
When quoting a list of symbols, it's redundant to quote each symbol within the list.
There's no need to eval
a symbol.
This sequence looks strange:
(require 'use-package) (require 'yasnippet) (require 'expand-region) (require 'bm) (require 'undo-tree)
Since we're looping over those same names in the preceding loop, why not just (require name)
as the last form within the lambda?
Addressing these issues gives somewhat improved code:
; loop through names of packages: install if needed, then load
(let ((packages '(use-package yasnippet expand-region bm undo-tree))
(content-not-refreshed t)
)
(mapc
(lambda (name)
(unless (package-installed-p name)
(when content-not-refreshed
(package-refresh-contents)
(setq content-not-refreshed nil))
(package-install name))
(require name))
packages
))
Further improvement would maintain the list of packages as a customisable variable.
use-package
won't install it from melpa if it's missing. And I think, we won't need use-package for this case: we know the package is installed. \$\endgroup\$