Emacs: void: loop character function definition

I am testing package loading in emacs init.el (Emacs 24.3). I automatically executed the author’s blog emacs prelude to automatically download packages and copied the code there to my init.el , as below. However, I received an error message about the loop function / symbol function, which says:

 Symbol function definition is void: loop 

Can someone explain how to fix the code?

I searched on the Internet and it seems that loop is a macro in cl-lib . I assume that the definition for loop missing, and I tried to fix the problem by adding (require 'cl-lib) as shown in the code, but the error remains. There are also other questions about similar error messages, for example: ELisp: cl-loop for "The value of a character as a variable is void" , The definition of a character function is void: declare-function . But the error messages are different from what is missing, and the answers mostly suggest alternative routes, such as using a newer version of emacs.

- code -

 (require 'package) (add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/") t) (package-initialize) ;;; check & load packages (defvar prelude-packages '( haskell-mode ) "A list of packages to ensure are installed at launch.") ;;(require 'cl-lib) ;debug (defun prelude-packages-installed-p () (loop for p in prelude-packages when (not (package-installed-p p)) do (return nil) finally (return t))) (unless (prelude-packages-installed-p) ;; check for new packages (package versions) (message "%s" "Emacs Prelude is now refreshing its package database...") (package-refresh-contents) (message "%s" " done.") ;; install the missing packages (dolist (p prelude-packages) (when (not (package-installed-p p)) (package-install p)))) (provide 'prelude-packages) ;;; end load packages 
+5
source share
1 answer

The cl-lib package was introduced in Emacs 24.3 and contains a number of common Lisp functions, all with the cl- prefix. Prior to this, the only way to use these functions was to require the cl library and use unsigned names, for example. loop instead of cl-loop . However, this was discouraged by the possibility of name conflicts, and prefix names are generally recommended if possible.

Since you are using Emacs 24.3, the “correct” way to fix this would be to replace loop with cl-loop and return with cl-return :

 (defun prelude-packages-installed-p () (cl-loop for p in prelude-packages when (not (package-installed-p p)) do (cl-return nil) finally (cl-return t))) 

(You do not need (require 'cl-lib) because cl-loop and cl-return automatically loaded.)

Alternatively, you can add (require 'cl) to access unsigned names and leave the code as it is. This will also work with earlier versions of Emacs.


I cannot but notice that this function can be written more briefly:

 (defun prelude-packages-installed-p () (cl-every 'package-installed-p prelude-packages)) 

The same goes for cl-every plus cl-lib vs every plus cl .

+9
source

All Articles