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?

Aquaactress
  • 1,393
  • 8
  • 11

1 Answers1

0

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

(setq lisp-indent-function 'common-lisp-indent-function)
shankar2k
  • 173
  • 1
  • 6
  • 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 Jul 29 '21 at 08: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 Jul 29 '21 at 09: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 Jul 29 '21 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 Jul 29 '21 at 10:48