9

I have a hard time writing and reading cl-loops with they way they're indented by default as I can't easily tell what the control flow is:

(cl-loop for x below 10
         if (cl-oddp x)
         collect x into odds
         and if (memq x funny-numbers) return (cdr it) end
         else
         collect x into evens
         finally return (vector odds evens))

I'd like it to be indented like how it's shown in its documentation:

(cl-loop for x below 10
         if (cl-oddp x)
           collect x into odds
           and if (memq x funny-numbers) return (cdr it) end
         else
           collect x into evens
         finally return (vector odds evens))

I know this has to do with Indenting Macros manual page. And possibly this question on indenting a common lisp loop in emacs. Additionally, I've looked at a post on irreal and this question on the indentation of lisp forms.

The general idea I got from reading is to use (declare (indent n)) where n is an integer. I see this works for simple examples. However, how I want the cl-loop to be indented seems too complex for this. I need to tell emacs to recognize the specific if else keywords in the cl-loop.

I am confused as I don't see a clear way to properly indent cl-loop. Any ideas?

2
  • The declaration (debug ...) is for Edebug.
    – xuchunyang
    Commented Apr 1, 2018 at 12:27
  • Oh, ok. It doesn't have to do with indentation. I'll remove it from my question. Commented Apr 1, 2018 at 12:36

1 Answer 1

0

It seems that you can achieve your desired indentation style by doing

(setq lisp-indent-function 'common-lisp-indent-function)
4
  • I cannot reproduce your result in either 27.2 or 28. Does this also work for you in emacs -Q -l cl-lib ? If so, which version of Emacs are you using?
    – phils
    Commented Jul 29, 2021 at 8:26
  • I am using emacs 27.2 on Windows 10. This works for me when I invoke Emacs using emacs -Q -l cl-lib
    – shankar2k
    Commented Jul 29, 2021 at 9:58
  • Okay I tried the example above and it didn't work. But when I use this setting on my own code example it has better indentation than the default for elisp: (cl-loop for n below arg do (setq pt (point)) (when (org-at-table-p) (goto-char (org-table-end)) if (re-search-forward org-table-line-regexp nil t) do (when (org-invisible-p) (org-reveal t) (org-show-entry) (unless (org-at-table-p) (cl-decf n))) else return (goto-char pt))
    – shankar2k
    Commented Jul 29, 2021 at 10:06
  • 1
    There are some differences, but it doesn't look like your example behaves any differently WRT the if/then/else indentation (which was what the question was specifically about).
    – phils
    Commented Jul 29, 2021 at 10:48

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.