1
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ If you use use-package - then you don't need to list the other packages in this list - just use-package to load the packages \$\endgroup\$
    – mmmmmm
    Jan 19, 2022 at 14:28
  • \$\begingroup\$ emacs.stackexchange.com/tags/elisp/info \$\endgroup\$
    – Drew
    Jan 19, 2022 at 15:08
  • \$\begingroup\$ @mmmmmm 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\$
    – user4035
    Jan 19, 2022 at 15:25
  • 2
    \$\begingroup\$ @user4035 use-package does see the :ensure keyword in the manual jwiegley.github.io/use-package/keywords It even explicitl;y says " This is particularly useful if you share your .emacs among several machines; the relevant packages are downloaded automatically once declared in your .emacs. " \$\endgroup\$
    – mmmmmm
    Jan 19, 2022 at 15:58

1 Answer 1

4
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.