Hot answers tagged

16

The first is the old library, the second is the new one. The old one defines things like defstruct, symbol-macrolet, incf, whereas the second defines instead cl-defstruct, cl-symbol-macrolet, and cl-incf. The old one is deprecated because it does not obey the general rule that packages should use a "package prefix" for all their definitions to try and ...


13

To get the same behavior you're used to with flet, use cl-letf to change a symbols function value. (cl-letf (((symbol-function 'pop-to-buffer) (lambda (buffer &rest _) (switch-to-buffer buffer)))) (compilation-next-error-function n reset)) If you were to roll your own flet, the easy way would be macro that expands to a cl-letf form. Edit: ...


12

Artur Malabarba wrote this up recently, in Understanding letf and how it replaces flet. letf is an alias for cl-letf, and while it's most likely what you want, there's a minor caveat which Artur points out: Unfortunately, cl-flet is not identical to the original flet—it's lexical, not dynamic. (Nic's noflet package provides extended functionality along ...


7

There is no constantly equivalent in Emacs (except ignore is like (constantly nil)), but dash provides one called -const as part of the dash-functional package. -const (c) Return a function that returns c ignoring any additional arguments. In types: a -> b -> a (funcall (-const 2) 1 3 "foo") ;; => 2 (-map (-const 1) '("a" "b" "c" "d")) ;; =&...


7

The function cl-macroexpand-all has been obsoleted in Emacs 24.3; you are now supposed to use macroexpand-all instead, which is part of core Emacs, not the CL library. The CL library defines cl-macroexpand-all as an alias for macroexpand-all, so in principle you could do (require 'cl) in your init file to fix the problem. However, the cl library itself ...


7

Emacs lisp is a programming language designed to be used to provide Emacs extensions and to program Emacs. Common Lisp is a programming language that was designed to be practical Lisp useful as production language. Therefore, Emacs Lisp is sometimes "a bit strange"(this may be an opinion), since you have to care about environment - Emacs - while Common Lisp ...


6

Emacs Lisp doesn't really have "Best Practices". Judging from all the code I've read so far, people instead go for what is most convenient for them. And given that Emacs Lisp and Common Lisp are reasonably close (both are of the Lisp-2 variety), it is no wonder either the older and unprefixed cl.el or the newer and prefixed cl-lib library are used when the ...


5

Simply require library cl at compile time, to get the use of its macros (and not get any runtime load). That is where macro lexical-let is defined. So all you need is this, to use lexical-let: (eval-when-compile (require 'cl)) ;; lexical-let (I put the stuff I use from the library in a comment like that, just to let me know what I'm using from it.) Oh, ...


5

cl and cl-lib are not "either or". The former requires the latter and defines lexical-let. If you want lexical bindings, you can turn them on using the variable lexical-binding. You can also set it on a per-file basis using file variables.


5

No, it's definitely not crucial: A quick sed | sort | uniq -c says that within Emacs's own code we have: [...] 20 (cl-ecase 24 (cl-labels 26 (cl-callf 26 (cl-defmacro 26 (cl-dolist 26 (cl-remove-if 31 (cl-flet 43 (cl-return 55 (cl-destructuring-bind 57 (cl-defun 58 (cl-decf 75 (cl-letf 91 (cl-check-type 120 (cl-case 123 (cl-pushnew 132 (cl-...


4

It's bug #18845 in Emacs-24.4. To work around it, you need to add (require 'cl) somewhere before you load php-mode.


3

While waiting for a better answer, I wrote a with-slots macro that seems to work with defstruct instances: (defmacro my-with-slots (class-name slots obj &rest body) "Bind slot names SLOTS in an instance OBJ of class CLASS-NAME, and execute BODY." (declare (indent 3)) `(cl-symbol-macrolet ,(cl-loop for slot in slots collect `...


3

It's an internal autoload of the cl-lib package, not a global autoload. cl- has no global autoloads. To use cl- functions, require cl-lib. To use cl- macros, eval-when-compile require cl-lib.


3

Actually, the cl-defstruct macro does preserve enough info to make such a function possible, but I don't think anyone has written such a beast yet. The way this info is stored has changed in Emacs-25: now the macro expands to various other things plus a call to cl-struct-define which creates a class object (itself a struct). You can inspect it with C-h o ...


3

An attempt... (require 'cl-generic) ;; Checking accounts can have a negative balance ;; Savings accounts can't (cl-defstruct checking-account balance) (cl-defstruct savings-account balance) ;; A generic `withdraw' method (cl-defgeneric withdraw (account amount) (:documentation "Withdraw AMOUNT from ACCOUNT balance. Give an error for savings accounts ...


2

Thanks to bpalmer in #emacs for helping me solve this. The issue was with the and after for start...; instead, it needed to be another for (or as). I chose to use as instead of the other fors since it seems a bit more intuitive, but either will work. The correct code is: (cl-loop for ref across (cdr (assq 'refs data)) as start = (1+ (cdr (assq ...


2

It looks like you ultimately want the characters between % and & in the buffer name? You might do this: (let ((s (buffer-name))) (string-match "%\\(.*?\\)&" s) (match-string 1 s))


1

It's not necessary to use cl-labels, but note that recursing over *hx/group-recurse imposes some overhead in that it must test (> 0 group-size) every call, when obviously that is not needed beyond the original call. Using labels keeps the recursive part simpler, which is generally desirable. For such reasons it's very common to see a pair of functions ...


1

This is not possible with the current codebase, there is an open bug for it. Fixing it should be a matter of copying the functionality from cl-indent.el without getting the misindenting of defcustom, define-minor-mode, etc.


1

Define the indentation for a given function/macro etc. by putting the indentation spec on the function etc. symbol as property common-lisp-indent-function. As one possible example: (put 'cl-flet 'common-lisp-indent-function '((&whole 4 &rest (&whole 1 &lambda &body)) &body))


1

As ocoh2 points out, cl-lib is not autoloaded, the only way to get it is to require it or have it indirectly autoloaded (some autoloaded package requires it). It must be the Debugger that loads cl-lib. With emacs -Q --eval '(cl-evenp 2)', there is an error but no debugger and cl-lib is not loaded. cl-lib only gets loaded after the Debugger pops up once ...


1

Edebug is arguably right: The tag is not evaluated and needs to be a symbol, not (quote foo).


1

You can usually avoid the use of constantly by writing the equivalent lambda-term inline. For example, in Common Lisp you could generate a list of 42s of the same length as a given list with (mapcar (constantly 42) '(1 2 3)) In Emacs Lisp, you can do just the same with an explicit lambda-term: (mapcar #'(lambda (_) 42) '(1 2 3)) Barely longer, and much ...


1

There is no predefined Emacs-Lisp function for this, as far as I know. Here is a definition - the same as is shown in the Common Lisp standard: (defun constantly (object) (lambda (&rest arguments) object)) You need to have lexical-binding turned on (non-nil) for that definition. If you have it turned off (nil) then you can use this definition ...


Only top voted, non community-wiki answers of a minimum length are eligible