Lisp code cleanup.

Mostly went through and clarified variable names.  Rather than "entry" for everything, use "transaction" and "posting" as appropriate to improve readability.
This commit is contained in:
Craig Earls 2013-02-12 10:35:27 -07:00
parent 36a00113d9
commit e3b37ac19e
8 changed files with 60 additions and 46 deletions

View file

@ -30,10 +30,10 @@
(goto-char (line-beginning-position)) (goto-char (line-beginning-position))
(cond ((looking-at "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.+?)\\)?\\s-+") (cond ((looking-at "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.+?)\\)?\\s-+")
(goto-char (match-end 0)) (goto-char (match-end 0))
'entry) 'transaction)
((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\(.\\)") ((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\(.\\)")
(goto-char (match-beginning 2)) (goto-char (match-beginning 2))
'transaction) 'posting)
((looking-at "^\\(sun\\|mon\\|tue\\|wed\\|thu\\|fri\\|sat\\)\\s-+") ((looking-at "^\\(sun\\|mon\\|tue\\|wed\\|thu\\|fri\\|sat\\)\\s-+")
(goto-char (match-end 0)) (goto-char (match-end 0))
'entry) 'entry)
@ -57,24 +57,26 @@
args))) args)))
(cons (reverse args) (reverse begins))))) (cons (reverse args) (reverse begins)))))
(defun ledger-entries () (defun ledger-payees ()
(let ((origin (point)) (let ((origin (point))
entries-list) payees-list)
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward (while (re-search-forward
(concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+" (concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+"
"\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)") nil t) "\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)") nil t) ;matches first line of transaction
(unless (and (>= origin (match-beginning 0)) (unless (and (>= origin (match-beginning 0))
(< origin (match-end 0))) (< origin (match-end 0)))
(setq entries-list (cons (match-string-no-properties 3) (setq payees-list (cons (match-string-no-properties 3)
entries-list))))) payees-list))))) ;add the payee to the list
(pcomplete-uniqify-list (nreverse entries-list)))) (pcomplete-uniqify-list (nreverse payees-list))))
(defvar ledger-account-tree nil) (defvar ledger-account-tree nil)
(defun ledger-find-accounts () (defun ledger-find-accounts ()
(let ((origin (point)) account-path elements) (let ((origin (point))
account-path
elements)
(save-excursion (save-excursion
(setq ledger-account-tree (list t)) (setq ledger-account-tree (list t))
(goto-char (point-min)) (goto-char (point-min))
@ -126,16 +128,16 @@
(interactive) (interactive)
(while (pcomplete-here (while (pcomplete-here
(if (eq (save-excursion (if (eq (save-excursion
(ledger-thing-at-point)) 'entry) (ledger-thing-at-point)) 'transaction)
(if (null current-prefix-arg) (if (null current-prefix-arg)
(ledger-entries) ; this completes against entry names (ledger-payees) ; this completes against payee names
(progn (progn
(let ((text (buffer-substring (line-beginning-position) (let ((text (buffer-substring (line-beginning-position)
(line-end-position)))) (line-end-position))))
(delete-region (line-beginning-position) (delete-region (line-beginning-position)
(line-end-position)) (line-end-position))
(condition-case err (condition-case err
(ledger-add-entry text t) (ledger-add-transaction text t)
((error) ((error)
(insert text)))) (insert text))))
(forward-line) (forward-line)
@ -151,7 +153,7 @@
(let ((name (caar (ledger-parse-arguments))) (let ((name (caar (ledger-parse-arguments)))
xacts) xacts)
(save-excursion (save-excursion
(when (eq 'entry (ledger-thing-at-point)) (when (eq 'transaction (ledger-thing-at-point))
(when (re-search-backward (when (re-search-backward
(concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+" (concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+"
(regexp-quote name) "\\(\t\\|\n\\| [ \t]\\)") nil t) (regexp-quote name) "\\(\t\\|\n\\| [ \t]\\)") nil t)

View file

@ -70,8 +70,8 @@ customizable to ease retro-entry.")
(make-variable-buffer-local 'highlight-overlay) (make-variable-buffer-local 'highlight-overlay)
(let ((map (current-local-map))) (let ((map (current-local-map)))
(define-key map [(control ?c) (control ?a)] 'ledger-add-entry) (define-key map [(control ?c) (control ?a)] 'ledger-add-transaction)
(define-key map [(control ?c) (control ?d)] 'ledger-delete-current-entry) (define-key map [(control ?c) (control ?d)] 'ledger-delete-current-transaction)
(define-key map [(control ?c) (control ?y)] 'ledger-set-year) (define-key map [(control ?c) (control ?y)] 'ledger-set-year)
(define-key map [(control ?c) (control ?m)] 'ledger-set-month) (define-key map [(control ?c) (control ?m)] 'ledger-set-month)
(define-key map [(control ?c) (control ?c)] 'ledger-toggle-current) (define-key map [(control ?c) (control ?c)] 'ledger-toggle-current)
@ -119,8 +119,8 @@ customizable to ease retro-entry.")
(define-key map [sep4] '(menu-item "--")) (define-key map [sep4] '(menu-item "--"))
(define-key map [edit-amount] '(menu-item "Calc on Amount" ledger-post-edit-amount)) (define-key map [edit-amount] '(menu-item "Calc on Amount" ledger-post-edit-amount))
(define-key map [sep] '(menu-item "--")) (define-key map [sep] '(menu-item "--"))
(define-key map [delete-xact] '(menu-item "Delete Entry" ledger-delete-current-entry)) (define-key map [delete-xact] '(menu-item "Delete Entry" ledger-delete-current-transaction))
(define-key map [add-xact] '(menu-item "Add Entry" ledger-add-entry :enable ledger-works)) (define-key map [add-xact] '(menu-item "Add Transaction" ledger-add-transaction :enable ledger-works))
(define-key map [sep3] '(menu-item "--")) (define-key map [sep3] '(menu-item "--"))
(define-key map [reconcile] '(menu-item "Reconcile Account" ledger-reconcile :enable ledger-works)) (define-key map [reconcile] '(menu-item "Reconcile Account" ledger-reconcile :enable ledger-works))
(define-key map [reconcile] '(menu-item "Hide Xacts" ledger-occur)))) (define-key map [reconcile] '(menu-item "Hide Xacts" ledger-occur))))
@ -140,13 +140,13 @@ Return the difference in the format of a time value."
(defun ledger-find-slot (moment) (defun ledger-find-slot (moment)
(catch 'found (catch 'found
(ledger-iterate-entries (ledger-iterate-transactions
(function (function
(lambda (start date mark desc) (lambda (start date mark desc)
(if (ledger-time-less-p moment date) (if (ledger-time-less-p moment date)
(throw 'found t))))))) (throw 'found t)))))))
(defun ledger-iterate-entries (callback) (defun ledger-iterate-transactions (callback)
(goto-char (point-min)) (goto-char (point-min))
(let* ((now (current-time)) (let* ((now (current-time))
(current-year (nth 5 (decode-time now)))) (current-year (nth 5 (decode-time now))))
@ -187,11 +187,11 @@ Return the difference in the format of a time value."
(setq ledger-month (read-string "Month: " (ledger-current-month))) (setq ledger-month (read-string "Month: " (ledger-current-month)))
(setq ledger-month (format "%02d" newmonth)))) (setq ledger-month (format "%02d" newmonth))))
(defun ledger-add-entry (entry-text &optional insert-at-point) (defun ledger-add-transaction (transaction-text &optional insert-at-point)
(interactive (list (interactive (list
(read-string "Entry: " (concat ledger-year "/" ledger-month "/")))) (read-string "Transaction: " (concat ledger-year "/" ledger-month "/"))))
(let* ((args (with-temp-buffer (let* ((args (with-temp-buffer
(insert entry-text) (insert transaction-text)
(eshell-parse-arguments (point-min) (point-max)))) (eshell-parse-arguments (point-min) (point-max))))
(ledger-buf (current-buffer)) (ledger-buf (current-buffer))
exit-code) exit-code)
@ -208,7 +208,7 @@ Return the difference in the format of a time value."
(insert (insert
(with-temp-buffer (with-temp-buffer
(setq exit-code (setq exit-code
(apply #'ledger-exec-ledger ledger-buf ledger-buf "entry" (apply #'ledger-exec-ledger ledger-buf ledger-buf "xact"
(mapcar 'eval args))) (mapcar 'eval args)))
(goto-char (point-min)) (goto-char (point-min))
(if (looking-at "Error: ") (if (looking-at "Error: ")
@ -219,7 +219,7 @@ Return the difference in the format of a time value."
(insert (car args) " \n\n") (insert (car args) " \n\n")
(end-of-line -1))))) (end-of-line -1)))))
(defun ledger-current-entry-bounds () (defun ledger-current-transaction-bounds ()
(save-excursion (save-excursion
(when (or (looking-at "^[0-9]") (when (or (looking-at "^[0-9]")
(re-search-backward "^[0-9]" nil t)) (re-search-backward "^[0-9]" nil t))
@ -228,9 +228,9 @@ Return the difference in the format of a time value."
(forward-line)) (forward-line))
(cons (copy-marker beg) (point-marker)))))) (cons (copy-marker beg) (point-marker))))))
(defun ledger-delete-current-entry () (defun ledger-delete-current-transaction ()
(interactive) (interactive)
(let ((bounds (ledger-current-entry-bounds))) (let ((bounds (ledger-current-transaction-bounds)))
(delete-region (car bounds) (cdr bounds)))) (delete-region (car bounds) (cdr bounds))))
(provide 'ldg-mode) (provide 'ldg-mode)

View file

@ -47,10 +47,6 @@
(require 'ldg-fonts) (require 'ldg-fonts)
(require 'ldg-occur) (require 'ldg-occur)
;(autoload #'ledger-mode "ldg-mode" nil t)
;(autoload #'ledger-fully-complete-entry "ldg-complete" nil t)
;(autoload #'ledger-toggle-current "ldg-state" nil t)
(autoload #'ledger-texi-update-test "ldg-texi" nil t) (autoload #'ledger-texi-update-test "ldg-texi" nil t)
(autoload #'ledger-texi-update-examples "ldg-texi" nil t) (autoload #'ledger-texi-update-examples "ldg-texi" nil t)

View file

@ -158,7 +158,7 @@ This is done so that the last digit falls in COLUMN, which defaults to 52."
(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 ledger-regex-post-line-group-account)) ;go to the and of the account (goto-char (match-end ledger-regex-post-line-group-account)) ;go to the and of the account
(let ((end-of-amount (re-search-forward "[-.,0-9]+" (line-end-position) t))) ;determine if the is an amount to edit (let ((end-of-amount (re-search-forward "[-.,0-9]+" (line-end-position) t))) ;determine if there is an amount to edit
(if end-of-amount (if end-of-amount
(let ((val (match-string 0))) (let ((val (match-string 0)))
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))
@ -171,8 +171,7 @@ This is done so that the last digit falls in COLUMN, which defaults to 52."
(if (search-backward " " (- (point) 3) t) (if (search-backward " " (- (point) 3) t)
(goto-char (line-end-position)) (goto-char (line-end-position))
(insert " ")) (insert " "))
(calc)) (calc))))))
))))
(defun ledger-post-prev-xact () (defun ledger-post-prev-xact ()
(interactive) (interactive)

View file

@ -119,7 +119,7 @@
(defun ledger-reconcile-add () (defun ledger-reconcile-add ()
(interactive) (interactive)
(with-current-buffer ledger-buf (with-current-buffer ledger-buf
(call-interactively #'ledger-add-entry)) (call-interactively #'ledger-add-transaction))
(ledger-reconcile-refresh)) (ledger-reconcile-refresh))
(defun ledger-reconcile-delete () (defun ledger-reconcile-delete ()
@ -128,7 +128,7 @@
(when (ledger-reconcile-get-buffer where) (when (ledger-reconcile-get-buffer where)
(with-current-buffer (ledger-reconcile-get-buffer where) (with-current-buffer (ledger-reconcile-get-buffer where)
(goto-char (cdr where)) (goto-char (cdr where))
(ledger-delete-current-entry)) (ledger-delete-current-transaction))
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(goto-char (line-beginning-position)) (goto-char (line-beginning-position))
(delete-region (point) (1+ (line-end-position))) (delete-region (point) (1+ (line-end-position)))
@ -159,6 +159,23 @@
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(ledger-display-balance)) (ledger-display-balance))
(defun ledger-reconcile-finish ()
"Mark all pending transactions as cleared, save the buffers and exit reconcile mode"
(interactive)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(let ((where (get-text-property (point) 'where))
(face (get-text-property (point) 'face)))
(if (and (eq face 'bold)
(when (is-stdin (car where))))
(with-current-buffer ledger-buf
(goto-char (cdr where))
(ledger-toggle-current 'cleared))))
(forward-line 1)))
(ledger-reconcile-save))
(defun ledger-reconcile-quit () (defun ledger-reconcile-quit ()
(interactive) (interactive)
(ledger-reconcile-quit-cleanup) (ledger-reconcile-quit-cleanup)
@ -191,7 +208,7 @@
(cons (cons
buf buf
(save-excursion (save-excursion
(if ledger-clear-whole-entries (if ledger-clear-whole-transactions
(goto-line (nth 1 emacs-xact)) (goto-line (nth 1 emacs-xact))
(goto-line (nth 0 posting))) (goto-line (nth 0 posting)))
(1+ (point-marker))))))) ;Add 1 to make sure the marker is within the transaction (1+ (point-marker))))))) ;Add 1 to make sure the marker is within the transaction

View file

@ -51,7 +51,7 @@
(with-current-buffer data-buffer (with-current-buffer data-buffer
(cons (cons
(nth 0 post) (nth 0 post)
(if ledger-clear-whole-entries (if ledger-clear-whole-transactions
(save-excursion (save-excursion
(goto-line (nth 1 post)) (goto-line (nth 1 post))
(point-marker)) (point-marker))

View file

@ -216,7 +216,7 @@ end of a ledger file which is included in some other file."
The user is prompted to enter a payee and that is substitued. If The user is prompted to enter a payee and that is substitued. If
point is in an entry, the payee for that entry is used as the point is in an entry, the payee for that entry is used as the
default." default."
;; It is intended copmletion should be available on existing ;; It is intended completion should be available on existing
;; payees, but the list of possible completions needs to be ;; payees, but the list of possible completions needs to be
;; developed to allow this. ;; developed to allow this.
(ledger-read-string-with-default "Payee" (regexp-quote (ledger-entry-payee)))) (ledger-read-string-with-default "Payee" (regexp-quote (ledger-entry-payee))))

View file

@ -19,8 +19,8 @@
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA. ;; MA 02111-1307, USA.
(defcustom ledger-clear-whole-entries nil (defcustom ledger-clear-whole-transactions nil
"If non-nil, clear whole entries, not individual transactions." "If non-nil, clear whole transactions, not individual postings."
:type 'boolean :type 'boolean
:group 'ledger) :group 'ledger)
@ -32,7 +32,7 @@
'pending 'pending
'cleared))) 'cleared)))
(defun ledger-entry-state () (defun ledger-transaction-state ()
(save-excursion (save-excursion
(when (or (looking-at "^[0-9]") (when (or (looking-at "^[0-9]")
(re-search-backward "^[0-9]" nil t)) (re-search-backward "^[0-9]" nil t))
@ -42,13 +42,13 @@
((looking-at "\\*\\s-*") 'cleared) ((looking-at "\\*\\s-*") 'cleared)
(t nil))))) (t nil)))))
(defun ledger-transaction-state () (defun ledger-posting-state ()
(save-excursion (save-excursion
(goto-char (line-beginning-position)) (goto-char (line-beginning-position))
(skip-syntax-forward " ") (skip-syntax-forward " ")
(cond ((looking-at "!\\s-*") 'pending) (cond ((looking-at "!\\s-*") 'pending)
((looking-at "\\*\\s-*") 'cleared) ((looking-at "\\*\\s-*") 'cleared)
(t (ledger-entry-state))))) (t (ledger-transaction-state)))))
(defun ledger-toggle-current-transaction (&optional style) (defun ledger-toggle-current-transaction (&optional style)
"Toggle the cleared status of the transaction under point. "Toggle the cleared status of the transaction under point.
@ -172,15 +172,15 @@ dropped."
(defun ledger-toggle-current (&optional style) (defun ledger-toggle-current (&optional style)
(interactive) (interactive)
(if (or ledger-clear-whole-entries (if (or ledger-clear-whole-transactions
(eq 'entry (ledger-thing-at-point))) (eq 'transaction (ledger-thing-at-point)))
(progn (progn
(save-excursion (save-excursion
(forward-line) (forward-line)
(goto-char (line-beginning-position)) (goto-char (line-beginning-position))
(while (and (not (eolp)) (while (and (not (eolp))
(save-excursion (save-excursion
(not (eq 'entry (ledger-thing-at-point))))) (not (eq 'transaction (ledger-thing-at-point)))))
(if (looking-at "\\s-+[*!]") (if (looking-at "\\s-+[*!]")
(ledger-toggle-current-transaction nil)) (ledger-toggle-current-transaction nil))
(forward-line) (forward-line)