# Extending (ledger-mode-clean-buffer) with (ledger-post-align-dwim) for description/comments

**URL:** <https://forum.plaintextaccounting.org/t/extending-ledger-mode-clean-buffer-with-ledger-post-align-dwim-for-description-comments/538>\
**Category:** General\
**Tags:** hledger, ledger, emacs\
**Created:** [April 15, 2025, 8:26am UTC](https://forum.plaintextaccounting.org/t/extending-ledger-mode-clean-buffer-with-ledger-post-align-dwim-for-description-comments/538 "2025-04-15T08:26:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jonas](https://forum.plaintextaccounting.org/u/jonas)\
**Post date:** [April 15, 2025, 8:26am UTC](https://forum.plaintextaccounting.org/t/extending-ledger-mode-clean-buffer-with-ledger-post-align-dwim-for-description-comments/538/1 "2025-04-15T08:26:44Z")

</div>

Hi all,

for a nice and tidy ledger I do two cleaning steps after having my entries in the ledger file and I am wondering how I could combine them to a single step.

First, I run `(ledger-mode-clean-buffer)` from within emacs to have all my manual edits aligned and everything chronological in order.

Second, I have a macro that jumps to the next comment and runs (ledger-post-align-dwim).

Does anyone know, how I can extend `(ledger-mode-clean-buffer)` to aling comments as well?

For context, this is how it looks right after csv import:

```auto
2023-02-01 GOODWORKS CORP
    ; Really long text with lots of numbers and descrition and a lot of whitespace actually and all the things that make my ledger not so pleasant to look at
    assets:bank:checking $1000 
    income:salary $1000

```

and after cleaning

```auto
2023-02-01 GOODWORKS CORP
    ; Really long text with lots of numbers and descrition and a lot
    ; of whitespace actually and all the things that make my ledger
    ; not so pleasant to look at
    assets:bank:checking $1000 
    income:salary $1000

```

Does anyone know enough elisp to add the function to ledger-mode-clean-buffer?  
(If you have another approach, tidying your ledger, I am also interested in hearing it 🙂 )

Best  
Jonas

---

<div class="post-metadata">

**Author:** ![leugrady](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@leugrady](https://forum.plaintextaccounting.org/u/leugrady)\
**Post date:** [April 20, 2025, 12:37am UTC](https://forum.plaintextaccounting.org/t/extending-ledger-mode-clean-buffer-with-ledger-post-align-dwim-for-description-comments/538/2 "2025-04-20T00:37:06Z")

</div>

Slightly modifying the function `ledger-post-align-postings` in `ledger-mode-clean-buffer` can get what you want.

```lisp
(defun ledger-post-align-postings (beg end)
  "Align all accounts and amounts between BEG and END.
The current region is used, or, if no region, the current line."
  (interactive "r")
  (save-match-data
    (save-excursion
      (let ((inhibit-modification-hooks t)
            ;; Extend region to whole lines
            (beg (save-excursion (goto-char beg) (line-beginning-position)))
            (end (save-excursion (goto-char end) (move-end-of-line 1) (point-marker))))
        (untabify beg end)
        (goto-char beg)
        (while (< (point) end)
          (when (looking-at-p "^\s+; ") ; Added
            (fill-paragraph)) ;Added
          (when (looking-at-p " ")
            ;; fix spaces at beginning of line:
            (just-one-space ledger-post-account-alignment-column)
            ;; fix spaces before amount if any:
            (when (re-search-forward "\t\\| \\| \t" (line-end-position) t)
              (goto-char (match-beginning 0))
              (let ((acct-end-column (current-column))
                    (amt-width (ledger-next-amount (line-end-position)))
                    amt-adjust)
                (when amt-width
                  (if (/= 0 (setq amt-adjust (- (if (> (- ledger-post-amount-alignment-column amt-width)
                                                       (+ 2 acct-end-column))
                                                    ledger-post-amount-alignment-column ;;we have room
                                                  (+ acct-end-column 2 amt-width))
                                                amt-width
                                                (current-column))))
                      (if (> amt-adjust 0)
                          (insert (make-string amt-adjust ? ))
                        (delete-char amt-adjust)))))))
          (forward-line 1))))))

```

---

<div class="post-metadata">

**Author:** ![jonas](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jonas](https://forum.plaintextaccounting.org/u/jonas)\
**Post date:** [April 28, 2025, 10:59am UTC](https://forum.plaintextaccounting.org/t/extending-ledger-mode-clean-buffer-with-ledger-post-align-dwim-for-description-comments/538/3 "2025-04-28T10:59:55Z")

</div>

Thanks for your suggestion. It almost does what I want. However, there seems to be edge cases where it doesn't work.  
Having a heading with a tag fills the collumn with the comment below... For example:

```auto
-2025-01-03 SHOPNAME ; tag:ABC
- ; some more information
+2025-01-03 SHOPNAME ; tag:ABC some
+ ; more information
     Assests -16,00 €
     Expenses 16,00 €

```

Not sure if it is worth the trouble to try to fix this. I think I should (and can) live with the burden of running two functions here.... 😃

Best  
Jonas
