10 votes
Accepted

How to break out of a dolist loop?

One option is to use catch / throw: https://www.gnu.org/software/emacs/manual/html_node/elisp/Catch-and-Throw.html (catch 'foo (dolist (x '("a" "b" "c" "d" "e" "f" "g")) (when (equal x "d") ...
  • 18k
8 votes
Accepted

How to loop over every line in a buffer?

A slightly more idiomatic example: (goto-char (point-min)) (while (not (eobp)) ... (forward-line 1))
  • 21.3k
6 votes
Accepted

Iterate with both value and index

This is actually not really different from your "home-made" version, but uses a loop clause to handle the incrementing: (let ((my-list '(a b c))) (cl-loop for index from 0 for item in my-...
  • 8,853
6 votes
Accepted

How to map or iterate over a list of files and set the result to `org-agenda-files`?

Without looking up any of the Org variables or functions you refer to, here's a guess at what you're trying to do. They both do the same thing: iterate over a list of file names, expanding them in ...
  • 71k
6 votes
Accepted

How to mapcar uneven lists?

The two built-in "zip-with" functions: seq-mapn from seq.el (Emacs ≥ 25.1) cl-mapcar from cl-lib.el (Emacs ≥ 24.3; was mapcar* in cl.el before that) and the most prominent third-party one: -...
  • 11.5k
6 votes
Accepted

Loop through top-level headings in Emacs Org-mode

You can use org-map-entries to apply a function to all the entries in a org-mode buffer matching a search. In this case the search "LEVEL=1" will match all top level headlines. The function you ...
  • 12.3k
5 votes

How to loop over every line in a buffer?

You can make a while loop using the function forward-line as your test. If forward-line reaches the end of the buffer, it returns the number of lines it wanted to move but couldn't. That leads to the ...
  • 20.7k
5 votes
Accepted

How do I properly populate a function by iterating over an alist?

You want (cadr search-pair), not (cdr search-pair). Either that or use an alist whose entries are dotted pairs, with the cdrs being your replacement strings. The error message tells you that a ...
  • 71k
4 votes

How to distinguish input from the return value of body in while-no-input?

The Elisp manual (do C-h i g (elisp) RET i while-no-input RET for more details - learning to use Info and the Info manuals is a good investment of your time) suggests the following: If you want to be ...
  • 21.2k
3 votes
Accepted

How do I splat the arguments to the `or` function?

If you wanted this for a function you would use apply, but as or is a special form you can't do that. In particular, or only evaluates as many arguments as it needs to. You could write a macro: (...
  • 44.3k
3 votes
Accepted

Emacs loops mysteriously

As pointed out in the comments, the infinite loop is due to an identification of the call to (loop ...) as referring to the loop/cl-loop macro from Emacs' Common Lisp emulation library and its ...
  • 11.5k
3 votes

idiomatic way to lexically scope variables in a cl-loop body

Your problem is that the timer runs the function when the loop has finished. The lexically bound variables index and value have for all calls the values after the last iteration. One solution is to ...
  • 30.8k
3 votes
Accepted

how to do a next in a elisp dotimes loop

There is no such facility for loops. Considering next in Ruby is the idiom to avoid a nested if, you can easily work around this by using this advice the other way around. (dotimes (i 6) (unless (&...
  • 21.3k
2 votes

Standard emacs lisp function for "collect-if"

Emacs 25 will include the seq.el library, which will provide default functions to manipulate sequences. Right now you can download it from GNU ELPA for Emacs 24. The functions you are looking for are: ...
2 votes
Accepted

Standard emacs lisp function for "collect-if"

Okay remove-if-not makes a copy of the list it operates on. So this is the equivalent to collect-if above. From my perspective remove-if-not is a really tricky name for something like collect-if or ...
  • 30.8k
2 votes

How to mapcar uneven lists?

Or... you can just loop for yourself: (let ((long '("1" "2" "3" "4")) (short '("a" "b" "c")) ret) (while (or long short) (setq ret (cons (concat (car-safe long) ...
  • 30.8k
2 votes

How to mapcar uneven lists?

Append the needed number of empty strings to short: (let* ((long '("1" "2" "3" "4")) (short '("a" "b" "c")) (_short (append short (make-list (- (list-length long)...
  • 1,835
2 votes

idiomatic way to lexically scope variables in a cl-loop body

Another option is to use backquoting to substitute the values for each loop iteration directly into the lambda form: ;;; foo.el --- -*- lexical-binding: t; -*- (let ((my-...
  • 44.3k
2 votes
Accepted

Confusion about iter-defun

There is no error, the (error "unknown iterator operation %S" op) is part of the closure. Simply type (iter-next rectangles) repeatedly, you will see the expected result. cl-loop also provides a ...
  • 13.8k
2 votes
Accepted

Cycling through a list back and forth

There are many ways to do it, as you guessed. One simple way is to use a ring, which is a list used cyclicly. Standard library ring.el defines what you need to create and use rings. This is from the ...
  • 71k
2 votes
Accepted

Alternative way to disambiguate "dangling else" in a cl-loop

I think this is what you want: (let ((flag1 nil) (flag2 nil)) (cl-loop if flag1 if flag2 return 1 else return 0 else return 2)) if flag1 is nil, you get 2 if flag1 is t, and flag2 ...
  • 11.1k
2 votes

How do I splat the arguments to the `or` function?

How about this? (defun or-list (list) (cl-some #'identity list)) Here cl-some takes two arguments: a predicate and a list and returns non-nil if the predicate applied to some member of the list is ...
1 vote
Accepted

Simple recursive function freezes Emacs — how to correct the definition?

Elisp does not implement Tail-Call Optimization (TCO). Your first recursive solution fails with (error "Lisp nesting exceeds ‘max-lisp-eval-depth’") at least here. tco.el is a package, ...
  • 4,286
1 vote

Equivalent of `continue' in `cl-loop'?

As @NickD pointed out, you can also use cl-block and cl-return-from to simulate continue. (cl-loop for item in '(1 2 3 4) with items ;; declare variable `items' ...
1 vote

Equivalent of `continue' in `cl-loop'?

This "answer" is not meant to be an the answer to the question because it is not "using cl-loop". But rather provide an alternative (as many other answers have done already). I ...
  • 1,293
1 vote
Accepted

Query-replace stops after first item in while

If you try to match the form (while COND BODY) to what you have above, you will see that COND matches (let* ((pair (pop replacement-list)) (to-find (car pair)) (to-replace (cdr pair))) ...
  • 21.2k
1 vote

Query-replace stops after first item in while

You are using while, not dolist, while stops when the TEST condition returns nil, not sure about the value of query-replace, but it definitely does not do what you want.
  • 13.8k
1 vote
Accepted

Infinite repetition (iteration) in Elisp code

You really ask two questions: How to iterate forever? (while t ...) See the Elisp manual, node Iteration. The first argument of while is a sexp. It is evaluated to see if the body needs to be ...
  • 71k
1 vote

idiomatic way to lexically scope variables in a cl-loop body

One way is to create a macro: (defmacro lexical-save (vars &rest body) `(lexical-let ,(mapcar (lambda (var) (list var var)) vars) ,@body)) You can use it is follows: (let ((my-list '(a b ...
1 vote

How to mapcar uneven lists?

Emacs 26 or newer version of seq.el provides seq-map-indexed (seq-map-indexed (lambda (element index) (concat element (seq-elt '("a" "b" "c") index))) '("1" "2" "3" "4")) ;; => ("1a" "2b" "3c"...
  • 13.8k

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