1 ;; emoney.el --- A home finance package.
3 ;; Copyright (C) 2003 - 2011 Steve Youngs
5 ;; Author: Steve Youngs <steve@sxemacs.org>
6 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
7 ;; Created: <2003-06-04>
8 ;; Keywords: money finance banking cash
10 ;; This file is part of eMoney.
12 ;; Redistribution and use in source and binary forms, with or without
13 ;; modification, are permitted provided that the following conditions
16 ;; 1. Redistributions of source code must retain the above copyright
17 ;; notice, this list of conditions and the following disclaimer.
19 ;; 2. Redistributions in binary form must reproduce the above copyright
20 ;; notice, this list of conditions and the following disclaimer in the
21 ;; documentation and/or other materials provided with the distribution.
23 ;; 3. Neither the name of the author nor the names of any contributors
24 ;; may be used to endorse or promote products derived from this
25 ;; software without specific prior written permission.
27 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
28 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 ;; DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ;; eMoney is based on balance.el, originally by Jason Baietto
42 ;; <jason@ssd.csd.harris.com> and later maintained by Bob Newell
43 ;; <bnewell@alum.mit.edu>.
45 ;; eMoney tries to give the user a reasonably self-contained finance
46 ;; solution. The target audience is the home user and possibly the
47 ;; small (very small) business operator. It probably will never be
48 ;; compatible with any commercial finance packages available.
50 ;; Right now, eMoney is nothing more than a very simple cash book
51 ;; program. Use it to balance your cheque book. See the "Todo:"
52 ;; section for a partial list of possible upcoming features.
54 ;; Installation (from source):
56 ;; tar zxf emoney-x.xx.tar.gz
58 ;; check the paths in Makefile
60 ;; make install (you may need to be root for this)
62 ;; Installation (from XEmacs package tarball):
64 ;; cd /usr/local/share/sxemacs/site-packages
65 ;; (/usr/local/lib/xemacs/site-packages for XEmacs)
66 ;; tar zxf /path/to/emoney-x.xx-pkg.tar.gz
68 ;; (Re)start (S)XEmacs and do `M-x emoney RET'
70 ;; You can also invoke `emoney-mode' by simply visiting a file with a
73 ;; Please note that I use SXEmacs exclusively, I have no idea whether
74 ;; or not this will run or even byte-compile with GNU/Emacs (it _will_
75 ;; work fine with XEmacs). Also, I have no desire or intentions of
76 ;; making this package portable between XEmacs and GNU/Emacs. Harsh
77 ;; words? No, not really, I simply don't have the time or resources to
78 ;; invest in the extra work involved. And also there is no way that
79 ;; I'd be able to support GNU/Emacs users because I'm not familiar with
80 ;; that flavour of Emacs. If you want to port it, be my guest, this is
81 ;; an Open Source project.
83 ;; Another note: This package uses correct English spelling wherever
84 ;; possible. For example, "cheque" instead of "check", "summarise"
85 ;; instead of "summarize". Although I welcome and encourage _ALL_
86 ;; contributions, patches that s/que/ch/ or s/ise/ize/ will go straight
91 ;; This is just a place holder so `emoney-commentary' will work
92 ;; properly. See the ChangeLog file for changes.
96 ;; Drag in what we need.
98 (autoload 'calc "calc" nil t)
99 (autoload 'clear-rectangle "rect" nil t)
100 (autoload 'completing-read "minibuf")
101 (autoload 'customize-group "cus-edit")
102 (autoload 'lm-commentary "lisp-mnt")
103 (autoload 'sort-numeric-fields "sort" nil t)
104 (autoload 'untabify "tabify" nil t)
105 (autoload 'with-electric-help "ehelp")
109 (autoload 'eval-when "cl-macs" nil nil 'macro)
110 (autoload 'calc-quit "calc" nil t)
112 (defvar calc-was-split)
113 (autoload 'browse-url "browse-url" nil t)
114 (autoload 'regexp-opt "regexp-opt"))
118 "Customisations for `emoney-mode'."
122 (defcustom emoney-accounts-buffer-width 35
123 "How wide in columns for the accounts buffer."
127 (defcustom emoney-accounts-buffer-height 9
128 "How high in lines for the accounts buffer."
132 (defcustom emoney-header-buffer-height 4
133 "How high in lines for the header buffer."
137 (defcustom emoney-accounts-directory (file-name-as-directory
138 (expand-file-name ".emoney"
139 (user-home-directory)))
140 "*The directory where your eMoney account files are located."
144 (defcustom emoney-history-directory (file-name-as-directory
145 (expand-file-name "history"
146 emoney-accounts-directory))
147 "*Directory containing previous years account files."
151 (defcustom emoney-chart-of-accounts
152 (if (file-directory-p emoney-accounts-directory)
153 (directory-files emoney-accounts-directory nil "\\.emy$" nil t)
155 "*A list of eMoney accounts in `emoney-accounts-directory'."
157 (string :tag "Account Name"))
160 (defcustom emoney-default-account (or (car emoney-chart-of-accounts)
162 "*The default eMoney account to use.
164 This is the account that has the focus when you start eMoney."
168 (defcustom emoney-credit-transaction-types
169 '("autotellcr" "atmcr" "bankcredit" "bcr" "deposit" "dep" "directcr"
170 "dcr" "gencr" "internetcr" "netcr" "phonecr" "phcr")
171 "*List of valid credit transaction types.
173 The default types are:
175 autotellcr -- Automatic Teller Machine deposit
176 atmcr -- short version of 'autotellcr'
178 bankcredit -- Bank initiated credits like interest etc
179 bcr -- short version of 'bankcredit'
181 deposit -- Manual, non-direct deposits
182 dep -- short version of 'deposit'
184 directcr -- Direct credit transactions
185 dcr -- short version of 'directcr'
187 gencr -- General credit for things that don't fit like
190 internetcr -- Internet credit transactions
191 netcr -- short version of 'internetcr'
193 phonecr -- Telephone credit transactions
194 phcr -- short version of 'phonecr'"
195 :type '(repeat string)
198 (defcustom emoney-debit-transaction-types
199 '("autotelldb" "atmdb" "bankfee" "fee" "bpay" "cc" "directdb" "ddb"
200 "eftpos" "eft" "gendb" "internetdb" "netdb" "paypal" "phonedb"
201 "phdb" "venddb" "withdrawal" "wdl")
202 "*List of valid debit transaction types.
204 The default types are:
206 autotelldb -- Automatic Teller Machine transaction
207 atmdb -- short version of 'autoteller'
210 fee -- short version of 'bankfee'
216 directdb -- Direct debit transactions
217 ddb -- short version of 'directdb'
219 eftpos -- Electronic Funds Transfer Point Of Sale
220 eft -- short version of 'eftpos'
222 gendb -- For general debits that don't fit other types
225 internetdb -- Internet transactions
226 netdb -- short version of 'internet'
228 paypal -- PayPal transactions
230 phonedb -- Telephone transactions
231 phcr -- short version of 'phone'
233 venddb -- Vending machine transactions
235 withdrawal -- Over the counter withdrawal.
236 wdl -- short version of 'withdrawal'"
237 :type '(repeat string)
240 (defcustom emoney-cr-transfer-transaction-type "xfrcr"
241 "*Credit transfer transaction types."
245 (defcustom emoney-db-transfer-transaction-type "xfrdb"
246 "*Debit transfer transaction type."
250 (defcustom emoney-date-format "%Y-%m-%d"
251 "*The format that `emoney-mode' uses for dates."
253 (const :tag "yyyy-mm-dd"
255 (const :tag "yyyy/mm/dd"
257 (const :tag "mm/dd/yy"
259 (const :tag "mm/dd/yyyy"
261 (const :tag "mm-dd-yy"
263 (const :tag "mm-dd-yyyy"
265 (const :tag "dd/mm/yy"
267 (const :tag "dd/mm/yyyy"
269 (const :tag "dd-mm-yy"
271 (const :tag "dd-mm-yyyy"
273 (const :tag "yy/mm/dd"
275 (const :tag "yy-mm-dd"
279 (defcustom emoney-uk-cheque-spelling t
280 "*When non-nil, use UK spelling: \"chq\" instead of \"chk\"."
284 (defcustom emoney-save-after-recalculate t
285 "*If non-nil, save the buffer after a recalculate.
287 See `emoney-recalculate-buffer'."
291 (defcustom emoney-recalculate-on-quit nil
292 "*If non-nil, recalculate each eMoney account buffer when quitting eMoney."
296 (defcustom emoney-use-new-frame nil
297 "*If non-nil, eMoney will start in a new frame."
301 (defcustom emoney-bank-url "unset"
302 "The URL of your bank's web page."
307 (defgroup emoney-hooks nil
308 "Various hooks for eMoney."
312 (defcustom emoney-mode-hooks nil
313 "*Hooks run after `emoney-mode' is entered."
315 :group 'emoney-hooks)
317 (defcustom emoney-switch-account-hook nil
318 "*Hooks run after switching accounts."
320 :group 'emoney-hooks)
322 (defcustom emoney-setup-accounts-buffer-hook nil
323 "*Hooks run after setting up the accounts buffer."
325 :group 'emoney-hooks)
327 (defcustom emoney-setup-header-buffer-hook nil
328 "*Hooks run after setting up the header buffer."
330 :group 'emoney-hooks)
332 (defcustom emoney-setup-control-buffer-hook nil
333 "*Hooks run after setting up the control buffer."
335 :group 'emoney-hooks)
337 (defcustom emoney-transaction-hook nil
338 "*Hooks run after appending a new transaction.
340 These hooks are run after any new transaction, including cheque
341 and transfers. If you want to do additional things with cheque or
342 transfer transactions, see `emoney-transaction-cheque-hook' &
343 `emoney-transaction-transfer-hook'."
345 :group 'emoney-hooks)
347 (defcustom emoney-transaction-cheque-hook nil
348 "*Hooks run after appending a cheque transaction."
350 :group 'emoney-hooks)
352 (defcustom emoney-transaction-transfer-hook nil
353 "*Hooks run after appending a transfer transaction."
355 :group 'emoney-hooks)
357 (defcustom emoney-recalculate-before-hook nil
358 "*Hooks run just prior to recalculating an eMoney buffer.
360 See `emoney-recalculate-after-hook' for doing things after recalculating."
362 :group 'emoney-hooks)
364 (defcustom emoney-recalculate-after-hook nil
365 "*Hooks run just after recalculating an eMoney buffer.
367 See `emoney-recalculate-before-hook' for doing things before recalculating."
369 :group 'emoney-hooks)
371 (defcustom emoney-summarise-cheques-hook nil
372 "*Hooks run after doing `emoney-summarise-cheques'."
374 :group 'emoney-hooks)
376 (defcustom emoney-new-account-hook nil
377 "*Hooks run after creating a new account."
379 :group 'emoney-hooks)
381 (defcustom emoney-quit-before-hook nil
382 "*Hooks run just prior to eMoney exiting."
384 :group 'emoney-hooks)
386 (defcustom emoney-quit-after-hook nil
387 "*Hooks run as the last thing when eMoney exits."
389 :group 'emoney-hooks)
392 (defgroup emoney-faces nil
397 (make-face 'emoney-account-name-face)
398 (set-face-parent 'emoney-account-name-face 'font-lock-variable-name-face)
400 (make-face 'emoney-debit-face)
401 (set-face-parent 'emoney-debit-face 'font-lock-warning-face)
403 (make-face 'emoney-credit-face)
404 (set-face-parent 'emoney-credit-face 'font-lock-function-name-face)
406 (make-face 'emoney-date-face)
407 (set-face-parent 'emoney-date-face 'font-lock-keyword-face)
409 (make-face 'emoney-clear-tran-face)
410 (set-face-parent 'emoney-clear-tran-face 'font-lock-string-face)
412 (make-face 'emoney-unclear-tran-face)
413 (set-face-parent 'emoney-unclear-tran-face 'font-lock-comment-face)
415 (make-face 'emoney-header-face)
416 (set-face-parent 'emoney-header-face 'font-lock-comment-face)
418 (defcustom emoney-account-name-face 'emoney-account-name-face
419 "Face for highlighting eMoney account names."
421 :group 'emoney-faces)
423 (defcustom emoney-debit-face 'emoney-debit-face
424 "Face for highlighting debit amounts in eMoney."
426 :group 'emoney-faces)
428 (defcustom emoney-credit-face 'emoney-credit-face
429 "Face for highlighting credit amounts in eMoney."
431 :group 'emoney-faces)
433 (defcustom emoney-date-face 'emoney-date-face
434 "Face for highlighting dates in eMoney."
436 :group 'emoney-faces)
438 (defcustom emoney-clear-tran-face 'emoney-clear-tran-face
439 "Face for highlighting cleared transactions in eMoney."
441 :group 'emoney-faces)
443 (defcustom emoney-unclear-tran-face 'emoney-unclear-tran-face
444 "Face for highlighting uncleared transactions in eMoney."
446 :group 'emoney-faces)
448 (defcustom emoney-header-face 'emoney-header-face
449 "Face for highlighting the column header in eMoney."
451 :group 'emoney-faces)
453 ;;; Internal variables
454 (defconst emoney-codename "Finance"
455 "The codename of the current version of eMoney.")
457 (defconst emoney-is-beta t
458 "Non-nil if the current version of eMoney is beta.")
460 (eval-when (load eval)
461 (unless (file-directory-p emoney-accounts-directory)
462 (make-directory-path emoney-accounts-directory)))
464 (require 'emoney-version)
467 (defun emoney-version (&optional arg)
468 "*Display the current version information for eMoney.
470 Prefix Argument ARG, print version information at point
471 in the current buffer."
473 (let ((fmt-string "eMoney: %s, \"%s\""))
475 (setq fmt-string (concat fmt-string " [Beta]")))
477 (insert (format fmt-string emoney-version emoney-codename))
478 (message fmt-string emoney-version emoney-codename))))
482 (defun emoney-commentary ()
483 "*Display the commentary section of emoney.el."
490 (insert (lm-commentary (locate-library "emoney.el")))
491 (goto-char (point-min))
492 (while (re-search-forward "^;+ ?" nil t)
493 (replace-match "" nil nil))
494 (buffer-string (current-buffer)))))
495 "*eMoney Commentary*"))
498 (defun emoney-copyright ()
499 "*Display the copyright notice for eMoney."
506 (insert-file-contents (locate-library "emoney.el"))
507 (goto-char (point-min))
508 (re-search-forward ";;; Commentary" nil t)
510 (narrow-to-region (point-min) (point))
511 (while (re-search-backward "^;+ ?" nil t)
512 (replace-match "" nil nil))
513 (buffer-string (current-buffer)))))
514 "*eMoney Copyright Notice*"))
516 (defvar emoney-frame nil
517 "The frame where eMoney is displayed, if in a new frame.")
519 (defconst emoney-accounts-buffer "*eMoney A/C's*"
520 "The buffer that holds the list of eMoney accounts.")
522 (defconst emoney-control-buffer "*eMoney Control*"
523 "The buffer containing the eMoney control buttons.")
525 (defconst emoney-header-buffer "*eMoney Header*"
526 "The buffer for the eMoney account register header line.")
528 (defvar emoney-current-account-name
529 (file-name-sans-extension emoney-default-account))
531 (defun emoney-switch-to-account (account)
532 "Switch to account, ACCOUNT."
534 (list (emoney-completing-read "Switch to A/C: "
535 emoney-chart-of-accounts nil t)))
536 (select-window (get-buffer-window
537 (concat emoney-current-account-name ".emy")))
538 (switch-to-buffer account)
539 (goto-char (point-max))
540 (setq emoney-current-account-name
541 (file-name-sans-extension account))
542 (select-window (get-buffer-window emoney-header-buffer))
543 (emoney-setup-header-buffer)
544 (switch-to-buffer emoney-header-buffer)
545 (select-window (get-buffer-window
546 (concat emoney-current-account-name ".emy")))
547 (run-hooks 'emoney-switch-account-hook))
549 (defun emoney-mouse-switch-to-account (event)
550 "Switch to account under EVENT."
553 (set-buffer (window-buffer (event-window event)))
554 (let ((switch-acc (extent-string (extent-at-event event))))
555 (select-window (get-buffer-window
556 (concat emoney-current-account-name ".emy")))
557 (switch-to-buffer (concat switch-acc ".emy"))
558 (goto-char (point-max))
559 (setq emoney-current-account-name switch-acc)
560 (select-window (get-buffer-window emoney-header-buffer))
561 (emoney-setup-header-buffer)
562 (switch-to-buffer emoney-header-buffer)))
563 (select-window (get-buffer-window
564 (concat emoney-current-account-name ".emy")))
565 (run-hooks 'emoney-switch-account-hook))
567 (defconst emoney-accounts-buffer-map
568 (let* ((map (make-sparse-keymap 'emoney-accounts-buffer-map)))
569 (define-key map [button2] #'emoney-mouse-switch-to-account)
571 "A keymap for the extents in eMoney Accounts buffer.")
573 (defconst emoney-largest-balance "999999999.99"
574 "This is only used for formatting purposes.")
576 (defun emoney-setup-accounts-buffer ()
577 "Set up the eMoney \"Accounts\" buffer."
578 (let ((buf emoney-accounts-buffer)
579 (accounts emoney-chart-of-accounts)
582 (when (buffer-live-p (get-buffer-create buf))
584 (set-buffer (get-buffer-create buf))
586 (setq help-msg (concat "Switch to A/C: "
587 (file-name-sans-extension (car accounts))))
588 (insert (file-name-sans-extension (car accounts)))
589 (set-extent-properties
590 (make-extent (point-at-bol) (point))
591 `(face emoney-account-name-face
594 balloon-help ,help-msg
595 keymap ,emoney-accounts-buffer-map))
596 (with-current-buffer (car accounts)
597 (goto-char (point-max))
598 (re-search-backward "\\s-[\\+-=]\\s-" nil t)
599 (setq cbal (nth 2 (emoney-parse-transaction-data
600 (buffer-substring (point) (point-at-eol))))))
601 (move-to-column 18 'force)
606 (number-to-string (- (length emoney-largest-balance)
607 (length (format "%.2f" cbal))))
611 'emoney-credit-face))
612 (insert " Needs Recalc"))
615 (setq accounts (cdr accounts)))
616 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
617 (set-specifier has-modeline-p nil (current-buffer))
618 (goto-char (point-min))
619 (run-hooks 'emoney-setup-accounts-buffer-hook))))
621 (defconst emoney-header
622 "Date Type C Description Amount Balance
623 ===============================================================================\n"
624 "The header inserted at the top of a `emoney-mode' buffer.")
626 (defun emoney-setup-header-buffer ()
627 "Set up the eMoney \"Account Register Header\" buffer."
628 (let ((buf emoney-header-buffer))
630 (when (buffer-live-p (get-buffer-create buf))
632 (set-buffer (get-buffer-create buf))
634 (insert-face (upcase emoney-current-account-name)
635 'emoney-header-face))
637 (insert-face emoney-header 'emoney-header-face)
638 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
639 (set-specifier vertical-scrollbar-visible-p nil (current-buffer))
640 (set-specifier has-modeline-p nil (current-buffer))
641 (goto-char (point-min))
642 (run-hooks 'emoney-setup-header-buffer-hook))))
645 (defun emoney-customise ()
646 "*Convenience function to customise eMoney."
648 (customize-group 'emoney))
650 ;; Because lots of people in the world can't spell.
652 (defalias 'emoney-customize 'emoney-customise)
654 (defvar emoney-transaction-types '("init" "")
655 "A list of valid transaction types.
657 It is a combination of `emoney-credit-transaction-types',
658 `emoney-debit-transaction-types', and a blank type.")
660 (defconst emoney-cheque-type
661 (if emoney-uk-cheque-spelling
662 "chq[ \t]+\\([0-9]+\\)"
663 "chk[ \t]+\\([0-9]+\\)")
664 "Type field for cheque transactions.
666 This is defined separately from the other transaction types because
667 it is used by functions that perform special operations on cheque.
670 (defconst emoney-date-column 0
671 "Column where transaction date begins.")
673 (defconst emoney-type-column 11
674 "Column where transaction type begins.")
676 (defconst emoney-clear-column 22
677 "Column where status appears.")
679 (defconst emoney-description-column 24
680 "Column where transaction description begins.")
682 (defconst emoney-sign-column 52
683 "Column where transaction sign begins.")
685 (defconst emoney-amount-column 54
686 "Column where transaction amount begins.")
688 (defconst emoney-current-balance-column 65
689 "Column where current emoney begins.")
691 (defconst emoney-tab-stop-list
696 emoney-description-column
699 emoney-current-balance-column)
700 "List of tab stops that define the start of all transaction fields.")
702 (defvar emoney-mode-map
703 (let ((map (make-sparse-keymap 'emoney-mode-map)))
704 (define-key map [(control c) (control b)] #'emoney-backward-field)
705 (define-key map [(control c) (control c)] #'emoney-recalculate-buffer)
706 (define-key map [(control c) (control d)] #'emoney-clear-current-field)
707 (define-key map [(control c) (control f)] #'emoney-forward-field)
708 (define-key map [(control c) (control n)] #'emoney-append-next-cheque)
709 (define-key map [(control c) (control r)] #'emoney-summarise-cheques-region)
710 (define-key map [(control c) (control s)] #'emoney-summarise-cheques-buffer)
711 (define-key map [(control c) (control t)] #'emoney-append-transaction)
712 (define-key map [(control c) (control x)] #'emoney-transfer-funds)
713 (define-key map [tab] #'emoney-forward-field)
714 (define-key map [iso-left-tab] #'emoney-backward-field)
715 (define-key map [(control c) b] #'emoney-go-to-bank)
716 (define-key map [(control c) c] #'emoney-calc)
717 (define-key map [(control c) s] #'emoney-switch-to-account)
718 (define-key map [(control c) q] #'emoney-quit)
719 (define-key map [(control c) (control q)] #'emoney-recalc-and-exit)
721 "Keymap for emoney buffer.")
723 (defconst emoney-mode-menu
725 ["New A/C" emoney-new-account t]
728 (if emoney-uk-cheque-spelling
731 "Transaction") emoney-append-next-cheque t]
732 ["New Transaction" emoney-append-transaction t]
733 ["Transfer Funds" emoney-transfer-funds t]
735 ["Next Field" emoney-forward-field t]
736 ["Previous Field" emoney-backward-field t]
737 ["Clear Current Field" emoney-clear-current-field t]
739 [(concat "Summary of "
740 (if emoney-uk-cheque-spelling
743 "(buffer)") emoney-summarise-cheques-buffer t]
744 [(concat "Summary of "
745 (if emoney-uk-cheque-spelling
748 "(region)") emoney-summarise-cheques-region t]
750 ["Recalculate Buffer" emoney-recalculate-buffer t]
752 ["Go To The Bank!" emoney-go-to-bank t]
753 ["Calculator" emoney-calc t]
755 ["Recalc All A/C's and Exit" emoney-recalc-and-exit t]
756 ["Exit eMoney" emoney-quit t])
757 "Menu for `emoney-mode' buffers.")
760 emoney-mode-easymenu nil "eMoney" emoney-mode-menu)
762 (defvar emoney-credit-type-keywords
763 (regexp-opt (append emoney-credit-transaction-types
764 (list emoney-cr-transfer-transaction-type)))
765 "eMoney font lock keywords for credit tran types")
767 (defvar emoney-debit-type-keywords
768 (regexp-opt (append emoney-debit-transaction-types
769 (list emoney-db-transfer-transaction-type)))
770 "eMoney font lock keywords for debit tran types")
772 (defvar emoney-font-lock-keywords
773 `(("x\\s-\\(.*\\)[\\+-]\\s-" (1 emoney-clear-tran-face))
774 ("o\\s-\\(.*\\)[\\+-]\\s-" (1 emoney-unclear-tran-face))
775 (,emoney-credit-type-keywords . emoney-credit-face)
776 (,emoney-debit-type-keywords . emoney-debit-face)
777 ("\\(^[0-9]+\\(-\\|\\/\\)[0-9]+\\(-\\|\\/\\)[0-9]+\\)"
778 (1 emoney-date-face))
779 ("-\\s-+\\([0-9]+\\.[0-9][0-9]\\)" (1 emoney-debit-face))
780 ("-[0-9]+\\.[0-9]+" . emoney-debit-face)
781 ("[^-]\\([0-9]+\\.[0-9]+$\\)" (1 emoney-credit-face))
782 ("\\+\\s-+\\([0-9]+\\.[0-9][0-9]\\)" (1 emoney-credit-face)))
783 "Font lock keywords for `emoney-mode'.")
787 (defun emoney-mode ()
788 "Major mode for editing a buffer containing financial transactions.
789 The following bindings provide the main functionality of this mode:
793 Transactions occur on a single line and have the following fields (in
796 date The transaction date. See `emoney-date-format'.
797 type This field must either be blank or match one of the
798 expressions defined in `emoney-credit-transaction-types'
799 and `emoney-debit-transaction-types'.
800 clear Status of transaction, 'o' is open, 'x' is cleared. New
801 transactions default to 'o'.
802 description A possibly blank transaction description.
803 sign This field must either be '+', '-' or '='. '+' means
804 credit, '-' means debit, and '=' resets balance. eMoney
805 will usually guess the correct sign to use.
806 amount The transaction amount.
807 balance The balance after this transaction. This field will be
808 computed upon recalculation, you _don't_ need to fill it
809 in. Just do: \\[emoney-recalculate-buffer].
811 Any line in the buffer that does not begin with a date will be
812 considered a comment and ignored. Among other things, this allows
813 the transaction description to span several lines.
815 Changing any amount and recalculating again will update all visible
816 balances. Transactions may be commented out by putting a semi-colon
817 \(or any other non-numerical character\) at the beginning of the line.
819 Entering `emoney-mode' runs the `emoney-mode-hooks' if any exist."
821 (kill-all-local-variables)
822 (setq major-mode 'emoney-mode)
823 (setq mode-name "eMoney")
824 (use-local-map emoney-mode-map)
825 (easy-menu-add emoney-mode-easymenu)
826 (make-local-variable 'tab-stop-list)
827 (setq tab-stop-list emoney-tab-stop-list)
828 (make-local-variable 'indent-tabs-mode)
829 (setq indent-tabs-mode nil)
830 (setq indent-line-function 'emoney-forward-field)
832 (run-hooks 'emoney-mode-hooks))
834 (defun emoney-current-line ()
835 "Return the current buffer line at point."
838 (count-lines (point-min) (point))))
840 (defun emoney-completing-read (prompt table &optional predicate require-match
841 initial-contents history default)
842 "Like `completing-read', but also accepts strings.
844 Arguments PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-CONTENTS,
845 HISTORY, DEFAULT are as per `completing-read'."
850 (mapcar 'list table))
851 predicate require-match initial-contents history default))
853 (defun emoney-forward-field ()
854 "Move the cursor to the next field on the current line."
858 (defun emoney-go-to-bank ()
859 "Open your bank's URL with `browse-url'."
861 (if (or (equal emoney-bank-url "unset")
862 (not emoney-bank-url))
863 (message-or-box "Please customise `emoney-bank-url'.")
864 (browse-url emoney-bank-url)))
866 (defun emoney-calc ()
867 "Wrapper around `calc' to get around \"window-edges bug\"."
869 (add-hook 'calc-end-hook #'(lambda ()
870 (setq calc-was-split nil)))
873 (defun emoney-last (list)
874 "Return last element in LIST."
880 (t (emoney-last (cdr list)))))
882 (defun emoney-find-largest-less-than (list item)
883 "Search a sorted LIST of numbers, return the largest number that is < ITEM."
884 (let ((list-car (car list))
885 (list-cdr (cdr list))
887 (while (and list-car (< list-car item))
889 (setq list-car (car list-cdr))
890 (setq list-cdr (cdr list-cdr)))
893 (defun emoney-find-largest-less-than-equal (list item)
894 "Return cdr of LIST starting @ the largest number that is <= to ITEM."
895 (let ((list-car (car list))
896 (list-cdr (cdr list))
898 (while (and list-car (<= list-car item))
899 (setq last (cons list-car list-cdr))
900 (setq list-car (car list-cdr))
901 (setq list-cdr (cdr list-cdr)))
904 (defun emoney-find-field (column)
905 "Return a list of the start and end of the field around COLUMN.
907 End may be nil if column is after the last defined tab stop."
908 (let ((field (emoney-find-largest-less-than-equal
909 emoney-tab-stop-list column)))
910 (if (equal 1 (length field))
911 (list (car field) nil)
912 (list (nth 0 field) (nth 1 field)))))
914 (defun emoney-backward-field ()
915 "Move the cursor to the previous entry field on the current line."
917 (let* ((col (current-column))
918 (prev (emoney-find-largest-less-than emoney-tab-stop-list col)))
920 (move-to-column prev)
921 (move-to-column (emoney-last emoney-tab-stop-list)))))
923 (defun emoney-clear-current-field ()
924 "Fill the field around point with spaces, leave point at start of field."
926 (let* ((field (emoney-find-field (current-column)))
927 (line-start (progn (beginning-of-line) (point)))
930 (untabify line-start (point))
932 (field-start (+ line-start (nth 0 field)))
933 (field-end (if (nth 1 field)
934 (+ line-start (nth 1 field))
936 (clear-rectangle field-start field-end)
937 (goto-char field-start)))
939 (defsubst emoney-build-types-list ()
940 "Dynamically build a list of transaction types.
942 This is done dynamically so that the user can change or add to the
943 list of transaction types without having to reload eMoney."
944 ;; Initialise to ("init" "").
945 (setq emoney-transaction-types '("init" ""))
946 ;; Load the debit and credit transaction types.
947 (setq emoney-transaction-types
948 (append emoney-transaction-types
949 emoney-debit-transaction-types
950 emoney-credit-transaction-types
951 (list emoney-cr-transfer-transaction-type)
952 (list emoney-db-transfer-transaction-type))))
954 ;; Stolen from Gnus' time-date.el
955 (defun emoney-days-to-time (days)
956 "Convert DAYS into a time value."
957 (let* ((seconds (* 1.0 days 60 60 24))
959 (ms (condition-case nil (floor (/ seconds rest))
960 (range-error (expt 2 16)))))
961 (list ms (condition-case nil (round (- seconds (* ms rest)))
962 (range-error (expt 2 16))))))
964 ;; Stolen from Gnus' time-date.el
965 (defun emoney-time-add (t1 t2)
966 "Add two time values. One should represent a time difference."
967 (let ((high (car t1))
968 (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1)))
969 (micro (if (numberp (car-safe (cdr-safe (cdr t1))))
973 (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2)))
974 (micro2 (if (numberp (car-safe (cdr-safe (cdr t2))))
978 (setq micro (+ micro micro2))
979 (setq low (+ low low2))
980 (setq high (+ high high2))
983 ;; `/' rounds towards zero while `mod' returns a positive number,
984 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
985 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
986 (setq micro (mod micro 1000000))
987 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
988 (setq low (logand low 65535))
990 (list high low micro)))
992 (defun emoney-append-transaction (&optional trans-type description amount)
993 "Add a transaction to the end of current buffer using today's date."
995 (goto-char (point-max))
996 (if (not (equal 0 (current-column)))
998 (let* ((date-variance
999 (read-number "Date (RET for current; -DAYS past; DAYS future): "
1000 'integers-only "0"))
1002 (format-time-string emoney-date-format
1005 (emoney-days-to-time date-variance)))))
1008 (emoney-build-types-list)
1009 (let* ((type (or trans-type
1010 (emoney-completing-read "Transaction type: "
1011 emoney-transaction-types))))
1016 (let ((before-descript (point))
1017 (start-column (current-column))
1018 (fill-column (- emoney-sign-column
1019 emoney-description-column
1022 (insert (or description
1023 (read-string "Description: ")))
1025 (narrow-to-region before-descript (point))
1026 (goto-char before-descript)
1027 (while (progn (move-to-column fill-column) (not (eobp)))
1028 (search-forward " " nil t)
1031 (goto-char before-descript)
1033 (while (progn (beginning-of-line) (not (eobp)))
1034 (indent-to-column start-column)
1035 (forward-line 1)))))
1037 (cond ((or (member type emoney-credit-transaction-types)
1038 (string= type emoney-cr-transfer-transaction-type))
1040 ((or (member type emoney-debit-transaction-types)
1041 (string= type emoney-db-transfer-transaction-type))
1043 ((string= type "init")
1045 (t (if (y-or-n-p "Is this a credit transaction? ")
1047 (if (y-or-n-p "Is this a debit transaction? ")
1049 (if (y-or-n-p "Is this an initialising transaction? ")
1051 (warn "Couldn't determine +/-/=, leaving blank"))))))
1054 (number-to-string amount)
1055 (read-string "Amount: ")))
1056 (run-hooks 'emoney-transaction-hook)))
1058 (defun emoney-append-next-cheque ()
1059 "Add a cheque transaction to the end of the current buffer using today's date.
1061 Inserts the cheque number following the last cheque number written into the
1062 transaction type column. Loses if you write cheques out of order."
1064 (goto-char (point-max))
1065 (if (not (equal 0 (current-column)))
1067 (insert (format-time-string emoney-date-format))
1069 (let (cheque cheque-number noinit)
1071 (if (search-backward-regexp emoney-cheque-type 0 t)
1073 (setq cheque (buffer-substring (match-beginning 1) (match-end 1)))
1075 (move-to-column emoney-type-column)
1076 (if (< (current-column) emoney-type-column)
1077 (indent-to-column emoney-type-column))
1078 (if emoney-uk-cheque-spelling
1079 (insert "chq 000001")
1080 (insert "chk 000001"))
1083 (setq cheque-number (1+ (string-to-number cheque)))
1084 (move-to-column emoney-type-column)
1085 (if (< (current-column) emoney-type-column)
1086 (indent-to-column emoney-type-column))
1087 (if emoney-uk-cheque-spelling
1088 (insert (format "chq %06d" cheque-number))
1089 (insert (format "chk %06d" cheque-number))))
1093 (let ((before-descript (point))
1094 (start-column (current-column))
1095 (fill-column (- emoney-sign-column
1096 emoney-description-column
1099 (insert (read-string "Payable To: "))
1101 (narrow-to-region before-descript (point))
1102 (goto-char before-descript)
1103 (while (progn (move-to-column fill-column) (not (eobp)))
1104 (search-forward " " nil t)
1107 (goto-char before-descript)
1109 (while (progn (beginning-of-line) (not (eobp)))
1110 (indent-to-column start-column)
1111 (forward-line 1)))))
1115 (insert (read-string "Amount: "))
1116 (run-hooks 'emoney-transaction-hook)
1117 (run-hooks 'emoney-transaction-cheque-hook)))
1119 (defvar emoney-transfer-account-history nil)
1121 (defun emoney-transfer-funds (from to amount)
1122 "Transfer funds from one eMoney account to another.
1124 Argument FROM is the account to transfer from.
1125 Argument To is the account to transfer to.
1126 Argument AMOUNT is how much to transfer."
1128 (list (emoney-completing-read "Transfer from: "
1129 emoney-chart-of-accounts nil t
1130 (concat emoney-current-account-name
1132 emoney-transfer-account-history)
1133 (emoney-completing-read "Transfer to: "
1134 emoney-chart-of-accounts nil t nil
1135 emoney-transfer-account-history)
1136 (read-number "Amount: ")))
1137 (let ((current-ac (concat emoney-current-account-name ".emy")))
1138 (with-current-buffer from
1139 (emoney-append-transaction
1140 emoney-db-transfer-transaction-type
1141 (concat "T'fer to " (file-name-sans-extension to))
1143 (with-current-buffer to
1144 (emoney-append-transaction
1145 emoney-cr-transfer-transaction-type
1146 (concat "T'fer from " (file-name-sans-extension from))
1148 (loop for buf in '(from to)
1149 do (emoney-switch-to-account (symbol-value buf))
1150 do (emoney-recalculate-buffer))
1151 (run-hooks 'emoney-transaction-transfer-hook)
1152 (emoney-switch-to-account current-ac)))
1154 (defsubst emoney-build-type-regexp ()
1155 "Return a regular expression that will match any valid transaction type.
1157 This is done dynamically so users can redefine the valid transactions in
1158 their `user-init-file' even after this file has been loaded."
1159 (emoney-build-types-list)
1160 (let ((types (append emoney-transaction-types
1161 (list emoney-cheque-type))))
1165 #'(lambda(x) x) types "\\|")
1168 (defun emoney-check-transaction-type (line-start)
1169 "Check to make sure a valid transaction type has been used.
1171 Argument LINE-START is the starting point.
1173 Please note that the word \"check\" here means \"verify\" and it has
1174 nothing to do with the American spelling of the word \"cheque\"."
1175 (let* ((type-regexp (emoney-build-type-regexp))
1176 (type-start (+ line-start emoney-type-column))
1177 (type-end (+ line-start emoney-clear-column))
1178 (type-string (buffer-substring type-start type-end)))
1179 (if (string-match type-regexp type-string)
1181 (error "Line %d, invalid type: %s"
1182 (1+ (emoney-current-line)) type-string))))
1184 (defun emoney-find-next-transaction ()
1185 "Find the next line that is a complete transaction.
1187 Return a list of the line start, numeric data start and line end
1191 "^\\([0-90-9]\\)+.*$")
1197 (search-forward-regexp line-regexp (point-max) t))
1198 (setq line-start (match-beginning 0))
1199 (setq line-end (progn (end-of-line) (point)))
1200 (setq data-start (+ line-start emoney-sign-column))
1201 (if (> line-end data-start)
1204 (list line-start data-start line-end)
1207 (defun emoney-parse-transaction-data (data)
1208 "Return a list of floating point numbers from DATA.
1210 DATA is a string representing the sign, amount and optionally balance of a
1211 transaction. Balance is nil if not present."
1212 (let ((data-regexp "\\([-+=]\\)[ \t]*\\([0-9.]+\\)?[ \t]*\\([-]?[0-9.]+\\)?")
1216 (string-match data-regexp data)
1217 (if (match-beginning 1)
1218 (setq sign (substring data (match-beginning 1) (match-end 1)))
1219 (error "Line %d, missing sign" (1+ (emoney-current-line))))
1220 (if (equal "=" sign)
1224 (if (match-beginning 2)
1226 (setq string (substring data (match-beginning 2) (match-end 2)))
1227 (setq amount (string-to-number (concat sign string))))
1228 (error "Line %d, missing amount" (1+ (emoney-current-line))))
1229 (if (match-beginning 3)
1231 (setq string (substring data (match-beginning 3) (match-end 3)))
1232 (setq balance (string-to-number string))))
1233 (if reset (setq sign "="))
1234 (list sign amount balance)))
1236 (defun emoney-same (amount1 amount2)
1237 "Compare two dollar amounts, AMOUNT1 AMOUNT2, for equivalence."
1238 (let ((string1 (format "%10.2f" amount1))
1239 (string2 (format "%10.2f" amount2)))
1240 (equal string1 string2)))
1242 (defun emoney-form-transaction-data (sign amount balance)
1243 "Given SIGN, AMOUNT and a BALANCE, return a string.
1245 The string is suitable for placing in the numeric region of a
1246 transaction, based on the defined input columns."
1247 (let* ((amount (abs amount))
1248 (width1 (- emoney-amount-column emoney-sign-column))
1249 (width2 (- emoney-current-balance-column emoney-amount-column))
1250 (len (length emoney-largest-balance))
1251 (gap (- width2 len))
1252 (value (concat "%" (number-to-string len) ".2f"))
1253 (format-string (concat "%-" (number-to-string width1)
1255 "%" (number-to-string gap) "s" value)))
1256 (format format-string sign amount "" balance)))
1258 (defun emoney-recalculate (start end)
1259 "Recalculate the balances for region START END.
1261 The final balance, uncleared total, and the number of balances that
1262 changed, and the transaction count are returned in a list."
1263 (run-hooks 'emoney-recalculate-before-hook)
1264 (let ((current-balance 0)
1271 (narrow-to-region start end)
1272 (untabify (point-min) (point-max))
1273 (goto-char (point-min))
1274 (while (setq line-points (emoney-find-next-transaction))
1275 (setq transactions (1+ transactions))
1276 (let* ((line-start (nth 0 line-points))
1277 (data-start (nth 1 line-points))
1278 (data-end (nth 2 line-points))
1279 (clear-flag (buffer-substring
1280 (+ line-start emoney-clear-column)
1281 (+ 1 line-start emoney-clear-column)))
1282 (data-string (buffer-substring data-start data-end))
1283 (data-values (emoney-parse-transaction-data data-string))
1284 (sign (nth 0 data-values))
1285 (amount (nth 1 data-values))
1286 (balance (nth 2 data-values))
1287 (new-balance (if (equal sign "=")
1289 (+ current-balance amount)))
1290 (new-uncleared (if (equal clear-flag "x")
1292 (+ uncleared amount)))
1294 (emoney-form-transaction-data sign amount new-balance)))
1295 (emoney-check-transaction-type line-start)
1296 (setq current-balance new-balance)
1297 (setq uncleared new-uncleared)
1298 (if (or (null balance) (not (emoney-same balance new-balance)))
1299 (setq changes (1+ changes)))
1300 (if (not (equal data-string new-string))
1302 (delete-region data-start data-end)
1303 (goto-char data-start)
1304 (insert new-string)))))
1306 (run-hooks 'emoney-recalculate-after-hook)
1307 (list current-balance uncleared changes transactions)))
1309 (defvar emoney-is-exiting nil
1310 "Non-nil when eMoney is in the process of quitting.")
1312 (defun emoney-update-acc-buf-bal (account balance)
1313 "Update ACCOUNT BALANCE in accounts buffer."
1314 (with-current-buffer emoney-accounts-buffer
1315 (goto-char (point-min))
1316 (search-forward account)
1318 (move-to-column 18 'force)
1321 (number-to-string (- (length emoney-largest-balance)
1322 (length (format "%.2f" balance))))
1323 "s$%.2f") "" balance)
1326 'emoney-credit-face))))
1328 (defun emoney-recalculate-buffer ()
1329 "Recalculate the current buffer.
1331 See `emoney-recalculate'."
1333 (let* ((result (emoney-recalculate (point-min) (point-max)))
1334 (balance (nth 0 result))
1335 (uncleared (nth 1 result))
1336 (changes (nth 2 result))
1337 (total (nth 3 result)))
1338 (when emoney-save-after-recalculate
1339 (save-buffer (current-buffer)))
1340 (emoney-update-acc-buf-bal emoney-current-account-name balance)
1341 (if emoney-is-exiting
1343 (format "book bal %.2f unclrd %.2f bank bal %.2f (%d/%d recalcs)"
1344 balance uncleared (- balance uncleared) changes total))
1346 (format "book bal %.2f unclrd %.2f bank bal %.2f (%d/%d recalcs)"
1347 balance uncleared (- balance uncleared) changes total)))
1351 (defun emoney-recalculate-region (start end)
1352 "Recalculate the current region, START END.
1354 See `emoney-recalculate'."
1356 (let* ((result (emoney-recalculate start end))
1357 (balance (nth 0 result))
1358 (uncleared (nth 1 result))
1359 (changes (nth 2 result))
1360 (total (nth 3 result)))
1361 (message-or-box (format "Region balance %.2f uncleared %.2f (%d/%d recalcs)"
1362 balance uncleared changes total))
1366 (defun emoney-summarise-cheques (start end)
1367 "Create a buffer that lists only the cheques in the specified region.
1369 The region is denoted by START END.
1371 The list is sorted on cheque number. Breaks in sequence are denoted by lines
1372 containing an asterisk between the cheques where the break occurs. The buffer
1373 is also recalculated, thus showing to total of the cheques summarised."
1374 (let ((emoney-buffer (current-buffer))
1375 (summary-buffer (get-buffer-create "*cheque summary*"))
1381 (set-buffer summary-buffer)
1382 (delete-region (point-min) (point-max))
1383 (set-buffer emoney-buffer)
1384 (narrow-to-region start end)
1385 (goto-char (point-min))
1386 (while (setq line-points (emoney-find-next-transaction))
1387 (let* ((line-start (nth 0 line-points))
1388 (line-end (nth 2 line-points))
1389 (type-start (+ line-start emoney-type-column))
1390 (type-end (+ line-start emoney-description-column))
1391 (type-string (buffer-substring type-start type-end)))
1392 (if (string-match emoney-cheque-type type-string)
1395 summary-buffer line-start (1+ line-end))
1396 (setq cheque-count (1+ cheque-count))))))
1398 (set-buffer summary-buffer)
1399 (sort-numeric-fields 3 (point-min) (point-max))
1400 (setq sequence-breaks (emoney-find-sequence-breaks))
1401 (goto-char (point-max))
1402 (insert (format "\n%d cheque%s summarised, %d sequence break%s\n"
1404 (if (equal 1 cheque-count) "" "s")
1406 (if (equal 1 sequence-breaks) "" "s")))
1407 (set-buffer emoney-buffer)))
1413 (insert-buffer summary-buffer)
1414 (buffer-string (current-buffer)))))
1415 "*cheque summary*"))
1416 (run-hooks 'emoney-summarise-cheques-hook))
1418 (defun emoney-find-sequence-breaks ()
1419 "Find cheque sequence breaks in the current cheque summary buffer.
1421 Mark breaks in sequence by inserting a line with an asterisk between
1422 the offending cheques. Return the count of sequence breaks found."
1423 (let ((last-cheque nil)
1424 (sequence-breaks 0))
1425 (goto-char (point-min))
1426 (while (search-forward-regexp
1427 (concat "\\([0-9/]+\\)[ \t]+" emoney-cheque-type)
1429 (let* ((cheque-start (match-beginning 2))
1430 (cheque-end (match-end 2))
1431 (cheque-string (buffer-substring cheque-start cheque-end))
1432 (cheque-number (string-to-int cheque-string)))
1433 (if (not last-cheque)
1434 (setq last-cheque cheque-number)
1435 (if (not (equal cheque-number (1+ last-cheque)))
1437 (setq sequence-breaks (1+ sequence-breaks))
1442 (setq last-cheque cheque-number))))
1445 (defun emoney-summarise-cheques-buffer ()
1446 "Summarise the cheque transactions in the current buffer."
1448 (emoney-summarise-cheques (point-min) (point-max))
1449 (emoney-recalculate (point-min) (point-max)))
1451 (defun emoney-summarise-cheques-region (start end)
1452 "Summarise the cheque transactions in region START - END."
1454 (emoney-summarise-cheques start end)
1455 (emoney-recalculate (point-min) (point-max)))
1457 (defun emoney-new-account (new-acc bal)
1458 "*Create a new A/C named NEW-ACC with initial balance BAL."
1460 (list (concat (read-string "New A/C Name: ") ".emy")
1461 (read-number "Initial Balance: " nil 0)))
1463 (expand-file-name new-acc emoney-accounts-directory))
1464 (setq emoney-chart-of-accounts
1465 (push new-acc emoney-chart-of-accounts))
1466 (select-window (get-buffer-window
1467 (concat emoney-current-account-name ".emy")))
1468 (switch-to-buffer new-acc)
1469 (setq emoney-current-account-name
1470 (file-name-sans-extension new-acc))
1471 (emoney-append-transaction "init" "Opening Balance" bal)
1472 (emoney-recalculate-buffer)
1473 (emoney-show-buffers)
1474 (switch-to-buffer new-acc)
1475 (goto-char (point-max))
1476 (run-hooks 'emoney-new-account-hook))
1478 (defun emoney-setup-control-buffer ()
1479 "Set up the eMoney \"Control\" buffer."
1480 (let ((buf emoney-control-buffer))
1482 (when (buffer-live-p (get-buffer-create buf))
1484 (set-buffer (get-buffer-create buf))
1485 (widget-create 'push-button
1486 :notify (lambda (&rest ignore)
1487 (call-interactively 'emoney-new-account))
1488 :help-echo "Create a new eMoney account."
1491 (widget-create 'push-button
1492 :notify (lambda (&rest ignore)
1495 (concat emoney-current-account-name ".emy"))
1496 (emoney-append-transaction)))
1497 :help-echo "Add a new transaction\n
1498 If you want to add a cheque transaction
1499 use \"Add Chq\" instead."
1502 (widget-create 'push-button
1503 :notify (lambda (&rest ignore)
1506 (concat emoney-current-account-name ".emy"))
1507 (emoney-append-next-cheque)))
1508 :help-echo (if emoney-uk-cheque-spelling
1509 "Add a new cheque transaction."
1510 "Add a new check transaction.")
1511 (if emoney-uk-cheque-spelling
1515 (widget-create 'push-button
1516 :notify (lambda (&rest ignore)
1519 (concat emoney-current-account-name ".emy"))
1520 (emoney-recalculate-buffer)))
1521 :help-echo "Record the last transaction.\n
1522 Also recalculates the buffer."
1524 (widget-insert "\n\n")
1525 (widget-create 'push-button
1526 :notify (lambda (&rest ignore)
1529 (concat emoney-current-account-name ".emy"))
1530 (call-interactively 'emoney-transfer-funds)))
1531 :help-echo "Transfer funds between accounts."
1534 (widget-create 'push-button
1535 :notify (lambda (&rest ignore)
1538 (concat emoney-current-account-name ".emy"))
1539 (emoney-summarise-cheques-buffer)))
1540 :help-echo (if emoney-uk-cheque-spelling
1541 "Display a summary of cheques."
1542 "Display a summary of checks.")
1543 (if emoney-uk-cheque-spelling
1547 (widget-create 'push-button
1548 :notify (lambda (&rest ignore)
1550 :help-echo "Start the Emacs Calculator.\n
1551 So you can count up all
1555 (widget-create 'push-button
1556 :notify (lambda (&rest ignore)
1559 "eMoney: %s, \"%s\" [Beta]"
1560 emoney-version emoney-codename)
1562 "eMoney: %s, \"%s\""
1563 emoney-version emoney-codename)))
1564 :help-echo "Display eMoney version info."
1566 (widget-insert "\n\n")
1567 (widget-create 'push-button
1568 :notify (lambda (&rest ignore)
1569 (emoney-go-to-bank))
1570 :help-echo "Open your bank's web site in your browser."
1573 (widget-create 'push-button
1574 :notify (lambda (&rest ignore)
1576 :help-echo "Exit eMoney."
1578 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
1579 (set-specifier vertical-scrollbar-visible-p nil (current-buffer))
1580 (set-specifier has-modeline-p nil (current-buffer))
1581 (run-hooks 'emoney-setup-control-buffer-hook))))
1583 (defun emoney-show-buffers ()
1584 "Display all the eMoney buffers."
1585 (emoney-setup-accounts-buffer)
1586 (emoney-setup-control-buffer)
1587 (emoney-setup-header-buffer)
1588 (delete-other-windows nil)
1589 (switch-to-buffer emoney-accounts-buffer)
1590 (split-window nil emoney-accounts-buffer-height)
1591 (split-window nil emoney-accounts-buffer-width t)
1593 (switch-to-buffer emoney-control-buffer)
1595 (switch-to-buffer emoney-header-buffer)
1596 (split-window nil emoney-header-buffer-height)
1599 (defun emoney-quit ()
1600 "*Exit from eMoney, optionally recalculating all accounts first.
1602 To have all of your accounts recalculated before eMoney exits set
1603 `emoney-recalculate-on-quit' to `t'
1605 If `emoney-save-after-recalculate' is also `t' the account buffers
1606 will be saved before eMoney exits."
1608 (run-hooks 'emoney-quit-before-hook)
1609 (let ((accounts emoney-chart-of-accounts))
1610 (setq emoney-is-exiting t)
1611 (dolist (buf accounts)
1612 (when emoney-recalculate-on-quit
1614 (emoney-recalculate-buffer))
1616 (kill-buffer emoney-accounts-buffer)
1617 (kill-buffer emoney-control-buffer)
1618 (kill-buffer emoney-header-buffer)
1619 (run-hooks 'emoney-quit-after-hook)
1620 (when (and emoney-use-new-frame
1621 (frame-live-p emoney-frame))
1622 (delete-frame emoney-frame))
1623 (setq emoney-frame nil)
1624 (unless emoney-use-new-frame
1625 (jump-to-register ?\$))
1626 (setq emoney-is-exiting nil)))
1628 (defun emoney-recalc-and-exit ()
1629 "*Exit form eMoney, recalculating all accounts first."
1631 (let ((old-recalc-val emoney-recalculate-on-quit))
1632 (setq emoney-recalculate-on-quit t)
1634 (setq emoney-recalculate-on-quit old-recalc-val)))
1638 "*Start a new eMoney session."
1640 (unless emoney-use-new-frame
1641 (window-configuration-to-register ?\$))
1642 (unless (frame-live-p emoney-frame)
1644 (if emoney-use-new-frame
1645 ;; FIXME: make the frame props customisable
1646 (make-frame '((name . "eMoney")
1650 (select-frame emoney-frame)
1651 (let ((accounts emoney-chart-of-accounts)
1652 (dir emoney-accounts-directory))
1654 (find-file (expand-file-name (car accounts) dir))
1655 (unless (eq major-mode 'emoney-mode)
1657 (setq accounts (cdr accounts)))
1658 (setq emoney-current-account-name
1659 (file-name-sans-extension emoney-default-account))
1660 (emoney-show-buffers)
1661 (switch-to-buffer emoney-default-account)
1662 (goto-char (point-max)))
1663 (focus-frame emoney-frame))
1665 ;; Work around a problem with Emacs calc. If you start calc in a
1666 ;; frame with multiple buffers visible when calc exits it doesn't
1667 ;; return point to the place it was when you called calc. These
1668 ;; advices overcome that.
1669 (defadvice calc (before em-calc-win-save first activate)
1670 "Before starting calc, save the window config.
1671 This is so we can restore the window config when calc exits because
1672 calc doesn't DTRT in this regard by itself."
1673 (push-window-configuration))
1675 (defadvice calc-quit (after em-calc-win-restore last activate)
1676 "Restore the \"pre calc\" window config on calc exit."
1677 (pop-window-configuration))
1680 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.emy$" . emoney-mode))
1684 ;;; emoney.el ends here