0

I'm looking at the codebase of Emacs (for example gnus codebase) and see that the code indentation style is so weird.

For example, here is source code of the functions gnus-sort-threads-recursive in module gnus-sum.el.gz of Emacs 27.2. It's difficult to read the if statement in line 3 since the condition and the body in next lines are totally unaligned and unordered.

(defun gnus-sort-threads-recursive (threads func)
  ;; Responsible for sorting the root articles of threads.
  (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions  ;; why is 
                     'gnus-thread-sort-functions)                   ;; this
                 func                                               ;; if-expr
                   (gnus-make-sort-function                         ;; so difficult
                gnus-subthread-sort-functions))))                   ;; to read ???
    (sort (mapcar (lambda (thread)
            (cons (car thread)
              (and (cdr thread)
                   (gnus-sort-subthreads-recursive
                (cdr thread) subthread-sort-func))))
          threads)
          func)))

If I call indent-region by Emacs itself to indent this function, then it's much clearer to read.

(defun gnus-sort-threads-recursive (threads func)
  ;; Responsible for sorting the root articles of threads.
  (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions
                                             'gnus-thread-sort-functions)
                                         func
                                     (gnus-make-sort-function
                                        gnus-subthread-sort-functions))))
    (sort (mapcar (lambda (thread)
                        (cons (car thread)
                                (and (cdr thread)
                                     (gnus-sort-subthreads-recursive
                                        (cdr thread) subthread-sort-func))))
                      threads)
          func)))

I guess it might be due to a historical reason. But I'm curious if anybody can explain why code indentation of the original codebase is so difficult to read?

2
  • Please fix the indentation in your code as displayed here - especially as your question is about indentation. You've apparently copied and pasted code that contained TAB chars. Please fix this so the code as displayed here is faithful to how it appears in Emacs.
    – Drew
    Aug 20 '21 at 16:46
  • Copy the text from Emacs again, to a new buffer, then use M-x untabify to convert TAB chars properly to SPC chars. Then paste the result here.
    – Drew
    Aug 20 '21 at 16:47
3

It has mixed tabs and spaces for indentation, and assumes that tabs are 8 characters wide. You’ve probably configured your Emacs to display tabs as a different width.

4
  • Oh right! I wasn't aware of that. Since I set tab width to only 2, I didn't recognize the different when moving the cursor
    – Trung Ta
    Aug 20 '21 at 10:10
  • 2
    I recommend using whitespace-mode to make both tabs and spaces more obvious. Though the Emacs source ought to have file–local or directory–local variables that tell Emacs how the thing is to be indented.
    – db48x
    Aug 20 '21 at 11:30
  • Thanks for the recommendation!
    – Trung Ta
    Aug 20 '21 at 14:07
  • Consider setting tab-width in emacs-lisp-mode-hook as a simple solution, or perhaps only setting tab-width at all for modes where you need it to be different to the default.
    – phils
    Aug 21 '21 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.