(load-library "ledger") (setenv "LEDGER_FILE" (expand-file-name "~/personal/ledger")) (defun sacha/ledger-edit () "Open my ledger." (interactive) (find-file (getenv "LEDGER_FILE")) (ledger-find-slot (current-time))) (defun sacha/ledger-add-entry (entry-text) "Create a new entry based on previous transactions." (interactive (list (read-string "Entry: " (concat ledger-year "/" ledger-month "/")))) (sacha/ledger-edit) (ledger-add-entry entry-text)) (defun sacha/ledger-monthly-report (show-balance) "Show monthly report. C-u reports budget, too." (interactive "P") (if show-balance (shell-command "ledger -M -R -c --add-budget reg") (shell-command "ledger -M -R -c reg"))) (defun sacha/ledger-monthly-expenses (show-balance) "Show monthly report. C-u reports budget, too." (interactive "P") (shell-command "ledger -M -R -c reg exp")) (defun sacha/ledger-show-current-month-budget (account) "Show current month's entries for ACCOUNT." (sacha/ledger-command "--budget" "-p 'this month'" "reg" account)) (defun sacha/ledger-show-current-month-balance (account) "Show current month's entries for ACCOUNT." (sacha/ledger-command "--budget" "-p 'this month'" "bal" account)) (defun sacha/ledger-command (&rest args) "Execute command." (shell-command (concat "ledger " (mapconcat 'identity args " ")) (get-buffer-create "*Ledger Output*")) (display-buffer (get-buffer-create "*Ledger Output*")) nil) (defun sacha/ledger-show-current-month-running-total (account) (sacha/ledger-command "-c -d " (shell-quote-argument "d>=[last month]") "--sort d reg" account) nil) (defun sacha/ledger-show-current-month-actual-running-total (account) (sacha/ledger-command "-R -C -c -d " (shell-quote-argument "d>=[last month]") "--sort d reg" account) nil) (defun sacha/ledger-show-current-month-pending-total (account) (sacha/ledger-command "-c -P -d " (shell-quote-argument "d>=[last month]") "--sort d reg" account) nil) (defun sacha/ledger-dump-report (account) (interactive) (sacha/ledger-command "-f ~/personal/combined.ledger -M -V -C -R -F '%[%Y/%m],%[%Y],%[%m],%A,%t\n' reg") nil) (defun sacha/ledger-check-savings-target (balance current-savings) (interactive "nCurrent balance: \nnCurrent savings: ") (let* ((goal 50000) (goal-date '(7 2 2010)) (days-left (- (calendar-absolute-from-gregorian goal-date) (time-to-days (current-time))))) ; days left (message "Save at least %.2f each paycheck\nAt current rate: %.2f on %s\nAchieve goal by %s" (/ (* 14 (- goal balance)) (- days-left 13)) ;; money I have to save each paycheck in order to reach my goal (+ balance (* current-savings (/ (- days-left 13) 14))) (calendar-date-string goal-date) (calendar-date-string (calendar-gregorian-from-absolute (+ (time-to-days (current-time)) (round (/ (* 14 (- goal balance)) current-savings))))))))