reddit / emacs

Indentation of code in org.el

pragmat1c1 · 2022-05-26 14:06
I am reviewing the code in `org.el`, and I am wondering that the indentation is somehow not correct. Marking a function and hitting TAB will re-indent correctly. Here's the original code (example) from `org.el`: (defun org-hide-drawer-all () "Fold all drawers in the current buffer." (save-excursion (goto-char (point-min)) (while (re-search-forward org-drawer-regexp nil t) (let* ((pair (get-char-property-and-overlay (line-beginning-position) 'invisible)) (o (cdr-safe pair))) (if (overlayp o) (goto-char (overlay-end o)) ;invisible drawer (pcase (get-char-property-and-overlay (point) 'invisible) (`(outline . ,o) (goto-char (overlay-end o))) ;already folded (_ (let* ((drawer (org-element-at-point)) (type (org-element-type drawer))) (when (memq type '(drawer property-drawer)) (org-hide-drawer-toggle t nil drawer) ;; Make sure to skip drawer entirely or we might flag it ;; another time when matching its ending line with ;; `org-drawer-regexp'. (goto-char (org-element-property :end drawer))))))))))) Look at the "let\*" expression for instance. Here is the same code correctly indented via TAB: (defun org-hide-drawer-all () "Fold all drawers in the current buffer." (save-excursion (goto-char (point-min)) (while (re-search-forward org-drawer-regexp nil t) (let* ((pair (get-char-property-and-overlay (line-beginning-position) 'invisible)) (o (cdr-safe pair))) (if (overlayp o) (goto-char (overlay-end o)) ;invisible drawer (pcase (get-char-property-and-overlay (point) 'invisible) (`(outline . ,o) (goto-char (overlay-end o))) ;already folded (_ (let* ((drawer (org-element-at-point)) (type (org-element-type drawer))) (when (memq type '(drawer property-drawer)) (org-hide-drawer-toggle t nil drawer) ;; Make sure to skip drawer entirely or we might flag it ;; another time when matching its ending line with ;; `org-drawer-regexp'. (goto-char (org-element-property :end drawer))))))))))) I am wondering why this is the case? Don't get me wrong: I am very thankful for what the maintainers offer with orgmode, just wondering why they follow a different kind of indentation? ## Update Ok, I get it. The original code has TABS and spaces mixed in the same file. See attached screenshot. Tabs marked in green. https://preview.redd.it/q2ka2wczvt191.png?width=669&format=png&auto=webp&s=e0b332d2318e4a6efdeff839e9dec392f663a9c5 ​ ​
kaushalmodi · 2022-05-26 15:59
Yeah, `org.el` has the notorious TABS at many places.
[deleted] · 2022-05-26 16:32
[removed]
usaoc · 2022-05-26 17:26
You should have `tab-width` set to the default value when you read Elisp files. Some Elisp hackers seem to leave both `indent-tabs-mode` and `tab-width` untouched, which causes the Elisp files to be formatted like that.
pragmat1c1 · 2022-05-26 17:56
Thank you for the pointer, that helps. - I am puzzled though why the devs have even mixed tabs with spaces on the same line.
usaoc · 2022-05-26 18:04
The devs don’t. Emacs does. Every `tab-width` spaces are formatted to a tab when `indent-tabs-mode` is non-`nil`.
pragmat1c1 · 2022-05-26 18:45
These are my settings: ;;;; tab related ;; https://emacs.stackexchange.com/a/45063/29404 (setq-default indent-tabs-mode nil) (setq-default tab-width 4) ;;(setq indent-line-function 'insert-tab) Should I not be able to see a correctly indented `org.el` code then?
usaoc · 2022-05-26 18:51
No. That’s why I said that you should have `tab-width` set to the default value, which is 8 ;). The problem here is that 8 spaces are converted to a tab when the file is formatted, but you have a tab displayed as 4 spaces instead.