Cleanup the ldg-post code a little bit
This commit is contained in:
parent
fff15425d8
commit
eb99efd2dd
1 changed files with 80 additions and 33 deletions
113
lisp/ldg-post.el
113
lisp/ldg-post.el
|
|
@ -9,51 +9,70 @@
|
||||||
:type 'boolean
|
:type 'boolean
|
||||||
:group 'ledger-post)
|
:group 'ledger-post)
|
||||||
|
|
||||||
|
(defcustom ledger-post-amount-alignment-column 52
|
||||||
|
"If non-nil, ."
|
||||||
|
:type 'integer
|
||||||
|
:group 'ledger-post)
|
||||||
|
|
||||||
|
(defcustom ledger-post-use-iswitchb nil
|
||||||
|
"If non-nil, ."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'ledger-post)
|
||||||
|
|
||||||
|
(defcustom ledger-post-use-ido nil
|
||||||
|
"If non-nil, ."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'ledger-post)
|
||||||
|
|
||||||
|
(defun ledger-post-all-accounts ()
|
||||||
|
(let ((origin (point))
|
||||||
|
(ledger-post-list nil)
|
||||||
|
account elements)
|
||||||
|
(save-excursion
|
||||||
|
(goto-char (point-min))
|
||||||
|
(while (re-search-forward ledger-post-line-regexp nil t)
|
||||||
|
(unless (and (>= origin (match-beginning 0))
|
||||||
|
(< origin (match-end 0)))
|
||||||
|
(add-to-list 'ledger-post-list (ledger-regex-post-line-account))))
|
||||||
|
(nreverse ledger-post-list))))
|
||||||
|
|
||||||
(declare-function iswitchb-read-buffer "iswitchb"
|
(declare-function iswitchb-read-buffer "iswitchb"
|
||||||
(prompt &optional default require-match start matches-set))
|
(prompt &optional default require-match start matches-set))
|
||||||
(defvar iswitchb-temp-buflist)
|
(defvar iswitchb-temp-buflist)
|
||||||
|
|
||||||
(defvar ledger-post-current-list nil)
|
|
||||||
|
|
||||||
(defun ledger-post-find-all ()
|
|
||||||
(let ((origin (point))
|
|
||||||
(ledger-post-list nil)
|
|
||||||
account-path elements)
|
|
||||||
(save-excursion
|
|
||||||
(goto-char (point-min))
|
|
||||||
(while (re-search-forward
|
|
||||||
"^[ \t]+\\([*!]\\s-+\\)?[[(]?\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)" nil t)
|
|
||||||
(unless (and (>= origin (match-beginning 0))
|
|
||||||
(< origin (match-end 0)))
|
|
||||||
(setq account-path (match-string-no-properties 2))
|
|
||||||
(unless (string-match "\\`\\s-*;" account-path)
|
|
||||||
(add-to-list 'ledger-post-list account-path))))
|
|
||||||
(setq ledger-post-current-list
|
|
||||||
(nreverse ledger-post-list)))))
|
|
||||||
|
|
||||||
(defun ledger-post-completing-read (prompt choices)
|
(defun ledger-post-completing-read (prompt choices)
|
||||||
"Use iswitchb as a completing-read replacement to choose from choices.
|
"Use iswitchb as a completing-read replacement to choose from choices.
|
||||||
PROMPT is a string to prompt with. CHOICES is a list of strings
|
PROMPT is a string to prompt with. CHOICES is a list of strings
|
||||||
to choose from."
|
to choose from."
|
||||||
(let* ((iswitchb-use-virtual-buffers nil)
|
(cond
|
||||||
(iswitchb-make-buflist-hook
|
(ledger-post-use-iswitchb
|
||||||
(lambda ()
|
(let* ((iswitchb-use-virtual-buffers nil)
|
||||||
(setq iswitchb-temp-buflist choices))))
|
(iswitchb-make-buflist-hook
|
||||||
(iswitchb-read-buffer prompt)))
|
(lambda ()
|
||||||
|
(setq iswitchb-temp-buflist choices))))
|
||||||
|
(iswitchb-read-buffer prompt)))
|
||||||
|
(ledger-post-use-ido
|
||||||
|
(ido-completing-read prompt choices))
|
||||||
|
(t
|
||||||
|
(completing-read prompt choices))))
|
||||||
|
|
||||||
|
(defvar ledger-post-current-list nil)
|
||||||
|
|
||||||
(defun ledger-post-pick-account ()
|
(defun ledger-post-pick-account ()
|
||||||
(interactive)
|
(interactive)
|
||||||
(let* ((account
|
(let* ((account
|
||||||
(ledger-post-completing-read "Account: "
|
(ledger-post-completing-read
|
||||||
(or ledger-post-current-list
|
"Account: " (or ledger-post-current-list
|
||||||
(ledger-post-find-all))))
|
(setq ledger-post-current-list
|
||||||
|
(ledger-post-all-accounts)))))
|
||||||
(account-len (length account))
|
(account-len (length account))
|
||||||
(pos (point)))
|
(pos (point)))
|
||||||
(goto-char (line-beginning-position))
|
(goto-char (line-beginning-position))
|
||||||
(when (re-search-forward ledger-post-line-regexp (line-end-position) t)
|
(when (re-search-forward ledger-post-line-regexp (line-end-position) t)
|
||||||
(let ((existing-len (length (match-string 3))))
|
(let ((existing-len (length (ledger-regex-post-line-account))))
|
||||||
(goto-char (match-beginning 3))
|
(goto-char (match-beginning ledger-regex-post-line-group-account))
|
||||||
(delete-region (match-beginning 3) (match-end 3))
|
(delete-region (match-beginning ledger-regex-post-line-group-account)
|
||||||
|
(match-end ledger-regex-post-line-group-account))
|
||||||
(insert account)
|
(insert account)
|
||||||
(cond
|
(cond
|
||||||
((> existing-len account-len)
|
((> existing-len account-len)
|
||||||
|
|
@ -64,6 +83,33 @@ to choose from."
|
||||||
(delete-char 1)))))))
|
(delete-char 1)))))))
|
||||||
(goto-char pos)))
|
(goto-char pos)))
|
||||||
|
|
||||||
|
(defun ledger-align-amounts (&optional column)
|
||||||
|
"Align amounts in the current region.
|
||||||
|
This is done so that the last digit falls in COLUMN, which defaults to 52."
|
||||||
|
(interactive "p")
|
||||||
|
(if (or (null column) (= column 1))
|
||||||
|
(setq column ledger-post-amount-alignment-column))
|
||||||
|
(save-excursion
|
||||||
|
(let* ((mark-first (< (mark) (point)))
|
||||||
|
(begin (if mark-first (mark) (point)))
|
||||||
|
(end (if mark-first (point-marker) (mark-marker)))
|
||||||
|
offset)
|
||||||
|
(goto-char begin)
|
||||||
|
(while (setq offset (ledger-next-amount end))
|
||||||
|
(let ((col (current-column))
|
||||||
|
(target-col (- column offset))
|
||||||
|
adjust)
|
||||||
|
(setq adjust (- target-col col))
|
||||||
|
(if (< col target-col)
|
||||||
|
(insert (make-string (- target-col col) ? ))
|
||||||
|
(move-to-column target-col)
|
||||||
|
(if (looking-back " ")
|
||||||
|
(delete-char (- col target-col))
|
||||||
|
(skip-chars-forward "^ \t")
|
||||||
|
(delete-horizontal-space)
|
||||||
|
(insert " ")))
|
||||||
|
(forward-line))))))
|
||||||
|
|
||||||
(defun ledger-post-align-amount ()
|
(defun ledger-post-align-amount ()
|
||||||
(interactive)
|
(interactive)
|
||||||
(save-excursion
|
(save-excursion
|
||||||
|
|
@ -83,7 +129,7 @@ to choose from."
|
||||||
(interactive)
|
(interactive)
|
||||||
(goto-char (line-beginning-position))
|
(goto-char (line-beginning-position))
|
||||||
(when (re-search-forward ledger-post-line-regexp (line-end-position) t)
|
(when (re-search-forward ledger-post-line-regexp (line-end-position) t)
|
||||||
(goto-char (match-end 3))
|
(goto-char (match-end ledger-regex-post-line-group-account))
|
||||||
(when (re-search-forward "[-.,0-9]+" (line-end-position) t)
|
(when (re-search-forward "[-.,0-9]+" (line-end-position) t)
|
||||||
(let ((val (match-string 0)))
|
(let ((val (match-string 0)))
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
|
|
@ -99,14 +145,14 @@ to choose from."
|
||||||
(when (re-search-backward ledger-xact-line-regexp nil t)
|
(when (re-search-backward ledger-xact-line-regexp nil t)
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
(re-search-forward ledger-post-line-regexp)
|
(re-search-forward ledger-post-line-regexp)
|
||||||
(goto-char (match-end 3))))
|
(goto-char (match-end ledger-regex-post-line-group-account))))
|
||||||
|
|
||||||
(defun ledger-post-next-xact ()
|
(defun ledger-post-next-xact ()
|
||||||
(interactive)
|
(interactive)
|
||||||
(when (re-search-forward ledger-xact-line-regexp nil t)
|
(when (re-search-forward ledger-xact-line-regexp nil t)
|
||||||
(goto-char (match-beginning 0))
|
(goto-char (match-beginning 0))
|
||||||
(re-search-forward ledger-post-line-regexp)
|
(re-search-forward ledger-post-line-regexp)
|
||||||
(goto-char (match-end 3))))
|
(goto-char (match-end ledger-regex-post-line-group-account))))
|
||||||
|
|
||||||
(defun ledger-post-setup ()
|
(defun ledger-post-setup ()
|
||||||
(let ((map (current-local-map)))
|
(let ((map (current-local-map)))
|
||||||
|
|
@ -115,7 +161,8 @@ to choose from."
|
||||||
(define-key map [(control ?c) (control ?c)] 'ledger-post-pick-account)
|
(define-key map [(control ?c) (control ?c)] 'ledger-post-pick-account)
|
||||||
(define-key map [(control ?c) (control ?e)] 'ledger-post-edit-amount))
|
(define-key map [(control ?c) (control ?e)] 'ledger-post-edit-amount))
|
||||||
(if ledger-post-auto-adjust-amounts
|
(if ledger-post-auto-adjust-amounts
|
||||||
(add-hook 'after-change-functions 'ledger-post-maybe-align t t)))
|
(add-hook 'after-change-functions 'ledger-post-maybe-align t t))
|
||||||
|
(add-hook 'after-save-hook #'(lambda () (setq ledger-post-current-list nil))))
|
||||||
|
|
||||||
(add-hook 'ledger-mode-hook 'ledger-post-setup)
|
(add-hook 'ledger-mode-hook 'ledger-post-setup)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue