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

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:

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

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 :slight_smile: )

Best
Jonas

1 Like

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

(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))))))
1 Like

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:

-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.... :smiley:

Best
Jonas