-
Notifications
You must be signed in to change notification settings - Fork 78
Respect year directive when sorting transactions #467
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,13 +59,41 @@ | |
| (beginning-of-line) | ||
| (insert "\n; Ledger-mode: End sort\n\n")) | ||
|
|
||
| (defconst ledger-sort--year-directive-regex | ||
| "^\\(?:Y\\|year\\)\\s-+\\([0-9]+\\)\\s-*$" | ||
| "Regex matching a `year NNNN' or `Y NNNN' directive at the start of a line. | ||
| The year number is captured in group 1.") | ||
|
|
||
| (defun ledger-sort--preceding-year () | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This algorithm is very quadratic. On my personal ledger file with 13k transactions (3 MB file), if I convert it to use year directives instead of YYYY-MM-DD, Perhaps a better approach would be to, before sorting, scan once from the beginning of the buffer until the end of the sort region for year directives, and look up the year for a transaction based on its position in the buffer relative to the year directives found (which ought to be a pretty small number). |
||
| "Return the year from the most recent `year NNNN' or `Y NNNN' directive. | ||
| Searches backward from the start of the current line, ignoring any | ||
| restriction so that directives above a narrowed sort region are still | ||
| consulted. Returns a number, or nil if no such directive exists." | ||
| (save-excursion | ||
| (save-restriction | ||
| (widen) | ||
| (beginning-of-line) | ||
| (when (re-search-backward ledger-sort--year-directive-regex nil t) | ||
| (string-to-number (match-string 1)))))) | ||
|
|
||
| (defun ledger-sort-startkey () | ||
| "Return a numeric sort key based on the date of the xact beginning at point." | ||
| "Return a numeric sort key based on the date of the xact beginning at point. | ||
| Dates with a full four-digit year are parsed directly. Short dates of the | ||
| form M/D or MM/DD are interpreted relative to the most recent `year NNNN' | ||
| directive preceding the current transaction, falling back to the current | ||
| calendar year if no such directive exists." | ||
| ;; Can use `time-convert' to return an integer instead of a floating-point | ||
| ;; number, starting in Emacs 27. | ||
| (float-time | ||
| (ledger-parse-iso-date | ||
| (buffer-substring-no-properties (point) (+ 10 (point)))))) | ||
| (cond | ||
| ((looking-at ledger-iso-date-regexp) | ||
| (ledger-parse-iso-date (match-string 0))) | ||
| ((looking-at "\\([0-9]\\{1,2\\}\\)[-/]\\([0-9]\\{1,2\\}\\)\\(?:[^-/0-9]\\|$\\)") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is more-or-less equivalent to ledger-incomplete-date-regexp. |
||
| (let ((month (string-to-number (match-string 1))) | ||
| (day (string-to-number (match-string 2))) | ||
| (year (or (ledger-sort--preceding-year) | ||
| (nth 5 (decode-time))))) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems very strange for this function to depend on the actual current wall time. Is that compatible with the actual semantics of ledger here? (admittedly I do not personally use year directives) |
||
| (encode-time 0 0 0 day month year)))))) | ||
|
|
||
| (defun ledger-sort-region (beg end) | ||
| "Sort the region from BEG to END in chronological order." | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably go in
ledger-regex.elfor consistency.