[emacs] Parse transaction leading lines more robustly

This began with noticing that the code didn't support the (ugly, yet
valid) case of a tab between the date and txn description. I took the
opportunity to make the regexes more consistent along the way.
This commit is contained in:
Steve Purcell 2014-12-09 16:26:51 +00:00
parent b623306591
commit 01c91130d7
3 changed files with 30 additions and 27 deletions

View file

@ -76,26 +76,27 @@
(forward-line)))) (forward-line))))
(defun ledger-fontify-xact-start (pos) (defun ledger-fontify-xact-start (pos)
"POS should be at the beginning of a line starting an xact. "POS should be at the beginning of a line starting an xact.
Fontify the first line of an xact" Fontify the first line of an xact"
(goto-char pos) (goto-char pos)
(beginning-of-line) (let ((line-start (line-beginning-position)))
(let ((state nil) (goto-char line-start)
(cur-point (point))) (re-search-forward "[ \t]")
(re-search-forward " ") (ledger-fontify-set-face (list line-start (match-beginning 0)) 'ledger-font-posting-date-face)
(ledger-fontify-set-face (list cur-point (point)) 'ledger-font-posting-date-face) (goto-char line-start)
(beginning-of-line) (re-search-forward ledger-xact-after-date-regex)
(re-search-forward ledger-xact-after-date-regex) (let ((state (save-match-data (ledger-state-from-string (match-string 1)))))
(save-match-data (setq state (ledger-state-from-string (match-string 1)))) (ledger-fontify-set-face (list (match-beginning 3) (match-end 3))
(ledger-fontify-set-face (list (match-beginning 1) (match-end 3)) (cond ((eq state 'pending)
(cond ((eq state 'pending) 'ledger-font-payee-pending-face)
'ledger-font-payee-pending-face) ((eq state 'cleared)
((eq state 'cleared) 'ledger-font-payee-cleared-face)
'ledger-font-payee-cleared-face) (t
(t 'ledger-font-payee-uncleared-face))))
'ledger-font-payee-uncleared-face))) (when (match-beginning 4)
(ledger-fontify-set-face (list (match-beginning 4) (ledger-fontify-set-face (list (match-beginning 4)
(match-end 4)) 'ledger-font-comment-face))) (match-end 4)) 'ledger-font-comment-face))
(forward-line)))
(defun ledger-fontify-posting (pos) (defun ledger-fontify-posting (pos)
"Fontify the posting at POS." "Fontify the posting at POS."

View file

@ -338,10 +338,10 @@
)) ))
(defconst ledger-xact-after-date-regex (defconst ledger-xact-after-date-regex
(concat " ?\\([ *!]\\)" ;; mark, subexp 1 (concat "\\([ \t]+[*!]\\)?" ;; mark, subexp 1
" ?\\((.*)\\)?" ;; code, subexp 2 "\\([ \t]+(.*?)\\)?" ;; code, subexp 2
" ?\\([^;\n]+\\)" ;; desc, subexp 3 "\\([ \t]+[^;\n]+\\)" ;; desc, subexp 3
"\\(\n\\|;.*\\)" ;; comment, subexp 4 "\\(;[^\n]*\\)?" ;; comment, subexp 4
)) ))
(defconst ledger-posting-regex (defconst ledger-posting-regex

View file

@ -68,10 +68,12 @@
(defun ledger-state-from-string (state-string) (defun ledger-state-from-string (state-string)
"Get state from STATE-CHAR." "Get state from STATE-CHAR."
(cond ((string-match "\\!" state-string) 'pending) (when state-string
((string-match "\\*" state-string) 'cleared) (cond
((string-match ";" state-string) 'comment) ((string-match "\\!" state-string) 'pending)
(t nil))) ((string-match "\\*" state-string) 'cleared)
((string-match ";" state-string) 'comment)
(t nil))))
(defun ledger-toggle-current-posting (&optional style) (defun ledger-toggle-current-posting (&optional style)
"Toggle the cleared status of the transaction under point. "Toggle the cleared status of the transaction under point.