Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indent-region is indenting date line. #58

Open
reteov opened this issue Nov 4, 2023 · 5 comments
Open

indent-region is indenting date line. #58

reteov opened this issue Nov 4, 2023 · 5 comments

Comments

@reteov
Copy link

reteov commented Nov 4, 2023

I'm not sure if this is specific to Emacs 29.1, or if it's just coincidental with the new version release, but it seems like indent-region is now causing the date line to indent as well as the account lines.

2023-11-04-140314_screenshot

@shivjm
Copy link
Collaborator

shivjm commented Nov 13, 2023

I’ve never tried indent-region on journal entries before, but I can confirm that this happens for me, too. May I ask when you last updated hledger-mode? My version is from about four months ago, so it doesn’t seem like a recent change.

@reteov
Copy link
Author

reteov commented Nov 18, 2023

The current version being used is 2023-10-01.834 (according to package-list). This was installed when I first installed Emacs 29.1 (I had performed a complete ground-up configuration of the config file, using the version in melpa).

For the time being, I'm using the following function to do the cleanup:

(defun hledger-clean-region ()
    "Indent all lines with leading space 8 spaces, and make sure there's only 3 spaces between an account and the dollar amount."
    (interactive)
    (save-excursion
      (goto-char (point-min))
      (replace-regexp "^ +" "        ")
      (goto-char (point-min))
      (replace-regexp " *\\$" "   $")))

I use this in order to make everything consistent between Emacs entries and Cone entries.

@narendraj9
Copy link
Owner

narendraj9 commented Nov 18, 2023

This looks like a bug in the function hledger-indent-region-function:

(defun hledger-indent-region-function (start end)
  "Indent region (START END) according to `hledger-mode'.
We need a separate function because we do different stuff while
interactively editing an entry."
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (beginning-of-line)
      (cond
       ((hledger-cur-has-datep) (indent-line-to 0))
       ((hledger-cur-starts-with-semicolp) (indent-line-to hledger-comments-column))
       ((hledger-cur-has-accp) (indent-line-to tab-width)))
      (forward-line 1))))

(defun hledger-cur-has-datep ()
  "Return true if current line has only date."
  (hledger-cur-line-matchesp hledger-date-only-regex))

(defvar hledger-date-only-regex "^\\s-*[0-9]\\{4\\}[-/][0-9]\\{2\\}[-/][0-9]\\{2\\}\\s-*$"
  "Regular expression for a line with date only.")

We need to adjust the regexp used to match the first line of an entry when executing indent-region. Normal indentation is more interactive, e.g. it removes and adds the current date, but this function used to indent regions needs to continue to avoid doing that. I have never used indent-region before so it's possible that this bug was introduced a long time ago.

@reteov
Copy link
Author

reteov commented Nov 18, 2023

Understood. The main purpose I use indent-region for is that I use the "Cone" app on my phone to add entries. It spaces things differently, where the accounts are closer to the margin (3 spaces) and there's a hefty amount of space between the account name and the monetary value. I was using the indent-region to fix the margin-spacing for the accounts so that it better-fits with the normal spacing used in Emacs.

@narendraj9
Copy link
Owner

I'll soon look into it and make a change to fix it. A PR is always welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants