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 30
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" "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 internetcr -- Internet credit transactions
188 netcr -- short version of 'internetcr'
190 phonecr -- Telephone credit transactions
191 phcr -- short version of 'phonecr'"
192 :type '(repeat string)
195 (defcustom emoney-debit-transaction-types
196 '("autotelldb" "atmdb" "bankfee" "fee" "directdb" "ddb" "eftpos"
197 "eft" "internetdb" "netdb" "phonedb" "phdb" "withdrawal" "wdl")
198 "*List of valid debit transaction types.
200 The default types are:
202 autotelldb -- Automatic Teller Machine transaction
203 atmdb -- short version of 'autoteller'
206 fee -- short version of 'bankfee'
208 directdb -- Direct debit transactions
209 ddb -- short version of 'directdb'
211 eftpos -- Electronic Funds Transfer Point Of Sale
212 eft -- short version of 'eftpos'
214 internetdb -- Internet transactions
215 netdb -- short version of 'internet'
217 phonedb -- Telephone transactions
218 phcr -- short version of 'phone'
220 withdrawal -- Over the counter withdrawal.
221 wdl -- short version of 'withdrawal'"
222 :type '(repeat string)
225 (defcustom emoney-cr-transfer-transaction-type "xfrcr"
226 "*Credit transfer transaction types."
230 (defcustom emoney-db-transfer-transaction-type "xfrdb"
231 "*Debit transfer transaction type."
235 (defcustom emoney-date-format "%Y-%m-%d"
236 "*The format that `emoney-mode' uses for dates."
238 (const :tag "yyyy-mm-dd"
240 (const :tag "yyyy/mm/dd"
242 (const :tag "mm/dd/yy"
244 (const :tag "mm/dd/yyyy"
246 (const :tag "mm-dd-yy"
248 (const :tag "mm-dd-yyyy"
250 (const :tag "dd/mm/yy"
252 (const :tag "dd/mm/yyyy"
254 (const :tag "dd-mm-yy"
256 (const :tag "dd-mm-yyyy"
258 (const :tag "yy/mm/dd"
260 (const :tag "yy-mm-dd"
264 (defcustom emoney-uk-cheque-spelling t
265 "*When non-nil, use UK spelling: \"chq\" instead of \"chk\"."
269 (defcustom emoney-save-after-recalculate t
270 "*If non-nil, save the buffer after a recalculate.
272 See `emoney-recalculate-buffer'."
276 (defcustom emoney-recalculate-on-quit nil
277 "*If non-nil, recalculate each eMoney account buffer when quitting eMoney."
281 (defcustom emoney-use-new-frame nil
282 "*If non-nil, eMoney will start in a new frame."
286 (defcustom emoney-bank-url "unset"
287 "The URL of your bank's web page."
292 (defgroup emoney-hooks nil
293 "Various hooks for eMoney."
297 (defcustom emoney-mode-hooks nil
298 "*Hooks run after `emoney-mode' is entered."
300 :group 'emoney-hooks)
302 (defcustom emoney-switch-account-hook nil
303 "*Hooks run after switching accounts."
305 :group 'emoney-hooks)
307 (defcustom emoney-setup-accounts-buffer-hook nil
308 "*Hooks run after setting up the accounts buffer."
310 :group 'emoney-hooks)
312 (defcustom emoney-setup-header-buffer-hook nil
313 "*Hooks run after setting up the header buffer."
315 :group 'emoney-hooks)
317 (defcustom emoney-setup-control-buffer-hook nil
318 "*Hooks run after setting up the control buffer."
320 :group 'emoney-hooks)
322 (defcustom emoney-transaction-hook nil
323 "*Hooks run after appending a new transaction.
325 These hooks are run after any new transaction, including cheque
326 and transfers. If you want to do additional things with cheque or
327 transfer transactions, see `emoney-transaction-cheque-hook' &
328 `emoney-transaction-transfer-hook'."
330 :group 'emoney-hooks)
332 (defcustom emoney-transaction-cheque-hook nil
333 "*Hooks run after appending a cheque transaction."
335 :group 'emoney-hooks)
337 (defcustom emoney-transaction-transfer-hook nil
338 "*Hooks run after appending a transfer transaction."
340 :group 'emoney-hooks)
342 (defcustom emoney-recalculate-before-hook nil
343 "*Hooks run just prior to recalculating an eMoney buffer.
345 See `emoney-recalculate-after-hook' for doing things after recalculating."
347 :group 'emoney-hooks)
349 (defcustom emoney-recalculate-after-hook nil
350 "*Hooks run just after recalculating an eMoney buffer.
352 See `emoney-recalculate-before-hook' for doing things before recalculating."
354 :group 'emoney-hooks)
356 (defcustom emoney-summarise-cheques-hook nil
357 "*Hooks run after doing `emoney-summarise-cheques'."
359 :group 'emoney-hooks)
361 (defcustom emoney-new-account-hook nil
362 "*Hooks run after creating a new account."
364 :group 'emoney-hooks)
366 (defcustom emoney-quit-before-hook nil
367 "*Hooks run just prior to eMoney exiting."
369 :group 'emoney-hooks)
371 (defcustom emoney-quit-after-hook nil
372 "*Hooks run as the last thing when eMoney exits."
374 :group 'emoney-hooks)
377 (defgroup emoney-faces nil
382 (make-face 'emoney-account-name-face)
383 (set-face-parent 'emoney-account-name-face 'font-lock-variable-name-face)
385 (make-face 'emoney-debit-face)
386 (set-face-parent 'emoney-debit-face 'font-lock-warning-face)
388 (make-face 'emoney-credit-face)
389 (set-face-parent 'emoney-credit-face 'font-lock-function-name-face)
391 (make-face 'emoney-date-face)
392 (set-face-parent 'emoney-date-face 'font-lock-keyword-face)
394 (make-face 'emoney-clear-tran-face)
395 (set-face-parent 'emoney-clear-tran-face 'font-lock-string-face)
397 (make-face 'emoney-unclear-tran-face)
398 (set-face-parent 'emoney-unclear-tran-face 'font-lock-comment-face)
400 (make-face 'emoney-header-face)
401 (set-face-parent 'emoney-header-face 'font-lock-comment-face)
403 (defcustom emoney-account-name-face 'emoney-account-name-face
404 "Face for highlighting eMoney account names."
406 :group 'emoney-faces)
408 (defcustom emoney-debit-face 'emoney-debit-face
409 "Face for highlighting debit amounts in eMoney."
411 :group 'emoney-faces)
413 (defcustom emoney-credit-face 'emoney-credit-face
414 "Face for highlighting credit amounts in eMoney."
416 :group 'emoney-faces)
418 (defcustom emoney-date-face 'emoney-date-face
419 "Face for highlighting dates in eMoney."
421 :group 'emoney-faces)
423 (defcustom emoney-clear-tran-face 'emoney-clear-tran-face
424 "Face for highlighting cleared transactions in eMoney."
426 :group 'emoney-faces)
428 (defcustom emoney-unclear-tran-face 'emoney-unclear-tran-face
429 "Face for highlighting uncleared transactions in eMoney."
431 :group 'emoney-faces)
433 (defcustom emoney-header-face 'emoney-header-face
434 "Face for highlighting the column header in eMoney."
436 :group 'emoney-faces)
438 ;;; Internal variables
439 (defconst emoney-codename "Finance"
440 "The codename of the current version of eMoney.")
442 (defconst emoney-is-beta t
443 "Non-nil if the current version of eMoney is beta.")
445 (eval-when (load eval)
446 (unless (file-directory-p emoney-accounts-directory)
447 (make-directory-path emoney-accounts-directory)))
449 (require 'emoney-version)
452 (defun emoney-version (&optional arg)
453 "*Display the current version information for eMoney.
455 Optional prefix Argument ARG, print version information at point
456 in the current buffer."
460 (insert (format "eMoney: %s, \"%s\" [Beta]"
461 emoney-version emoney-codename))
462 (message (format "eMoney: %s, \"%s\" [Beta]"
463 emoney-version emoney-codename)))
465 (insert (format "eMoney: %s, \"%s\""
466 emoney-version emoney-codename))
467 (message (format "eMoney: %s, \"%s\""
468 emoney-version emoney-codename)))))
471 (defun emoney-commentary ()
472 "*Display the commentary section of emoney.el."
479 (insert (lm-commentary (locate-library "emoney.el")))
480 (goto-char (point-min))
481 (while (re-search-forward "^;+ ?" nil t)
482 (replace-match "" nil nil))
483 (buffer-string (current-buffer)))))
484 "*eMoney Commentary*"))
487 (defun emoney-copyright ()
488 "*Display the copyright notice for eMoney."
495 (insert-file-contents (locate-library "emoney.el"))
496 (goto-char (point-min))
497 (re-search-forward ";;; Commentary" nil t)
499 (narrow-to-region (point-min) (point))
500 (while (re-search-backward "^;+ ?" nil t)
501 (replace-match "" nil nil))
502 (buffer-string (current-buffer)))))
503 "*eMoney Copyright Notice*"))
505 (defvar emoney-frame nil
506 "The frame where eMoney is displayed, if in a new frame.")
508 (defconst emoney-accounts-buffer "*eMoney A/C's*"
509 "The buffer that holds the list of eMoney accounts.")
511 (defconst emoney-control-buffer "*eMoney Control*"
512 "The buffer containing the eMoney control buttons.")
514 (defconst emoney-header-buffer "*eMoney Header*"
515 "The buffer for the eMoney account register header line.")
517 (defvar emoney-current-account-name
518 (file-name-sans-extension emoney-default-account))
520 (defun emoney-switch-to-account (&optional account)
521 "Switch to account, ACCOUNT."
523 (let ((switch-acc (or account
524 (emoney-completing-read "Switch to A/C: "
525 emoney-chart-of-accounts nil t))))
526 (select-window (get-buffer-window
527 (concat emoney-current-account-name ".emy")))
528 (switch-to-buffer switch-acc)
529 (goto-char (point-max))
530 (setq emoney-current-account-name
531 (file-name-sans-extension switch-acc))
532 (select-window (get-buffer-window emoney-header-buffer))
533 (emoney-setup-header-buffer)
534 (switch-to-buffer emoney-header-buffer)
535 (select-window (get-buffer-window
536 (concat emoney-current-account-name ".emy")))
537 (run-hooks 'emoney-switch-account-hook)))
539 (defun emoney-mouse-switch-to-account (event)
540 "Switch to account under EVENT."
543 (set-buffer (window-buffer (event-window event)))
544 (let ((switch-acc (extent-string (extent-at-event event))))
545 (select-window (get-buffer-window
546 (concat emoney-current-account-name ".emy")))
547 (switch-to-buffer (concat switch-acc ".emy"))
548 (goto-char (point-max))
549 (setq emoney-current-account-name switch-acc)
550 (select-window (get-buffer-window emoney-header-buffer))
551 (emoney-setup-header-buffer)
552 (switch-to-buffer emoney-header-buffer)))
553 (select-window (get-buffer-window
554 (concat emoney-current-account-name ".emy")))
555 (run-hooks 'emoney-switch-account-hook))
557 (defconst emoney-accounts-buffer-map
558 (let* ((map (make-sparse-keymap 'emoney-accounts-buffer-map)))
559 (define-key map [button2] #'emoney-mouse-switch-to-account)
561 "A keymap for the extents in eMoney Accounts buffer.")
563 (defconst emoney-largest-balance "999999999.99"
564 "This is only used for formatting purposes.")
566 (defun emoney-setup-accounts-buffer ()
567 "Set up the eMoney \"Accounts\" buffer."
568 (let ((buf emoney-accounts-buffer)
569 (accounts emoney-chart-of-accounts)
572 (when (buffer-live-p (get-buffer-create buf))
574 (set-buffer (get-buffer-create buf))
576 (setq help-msg (concat "Switch to A/C: "
577 (file-name-sans-extension (car accounts))))
578 (insert (file-name-sans-extension (car accounts)))
579 (set-extent-properties
580 (make-extent (point-at-bol) (point))
581 `(face emoney-account-name-face
584 balloon-help ,help-msg
585 keymap ,emoney-accounts-buffer-map))
586 (with-current-buffer (car accounts)
587 (goto-char (point-max))
588 (re-search-backward "\\s-[\\+-=]\\s-" nil t)
589 (setq cbal (nth 2 (emoney-parse-transaction-data
590 (buffer-substring (point) (point-at-eol))))))
591 (move-to-column 13 'force)
596 (number-to-string (- (length emoney-largest-balance)
597 (length (format "%.2f" cbal))))
601 'emoney-credit-face))
602 (insert " Needs Recalc"))
605 (setq accounts (cdr accounts)))
606 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
607 (set-specifier has-modeline-p nil (current-buffer))
608 (run-hooks 'emoney-setup-accounts-buffer-hook))))
610 (defconst emoney-header
611 "Date Type C Description Amount Balance
612 ===============================================================================\n"
613 "The header inserted at the top of a `emoney-mode' buffer.")
615 (defun emoney-setup-header-buffer ()
616 "Set up the eMoney \"Account Register Header\" buffer."
617 (let ((buf emoney-header-buffer))
619 (when (buffer-live-p (get-buffer-create buf))
621 (set-buffer (get-buffer-create buf))
622 (insert-face emoney-current-account-name 'emoney-account-name-face)
624 (insert-face emoney-header 'emoney-header-face)
625 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
626 (set-specifier vertical-scrollbar-visible-p nil (current-buffer))
627 (set-specifier has-modeline-p nil (current-buffer))
628 (goto-char (point-min))
629 (run-hooks 'emoney-setup-header-buffer-hook))))
632 (defun emoney-customise ()
633 "*Convenience function to customise eMoney."
635 (customize-group 'emoney))
637 ;; Because lots of people in the world can't spell.
639 (defalias 'emoney-customize 'emoney-customise)
641 (defvar emoney-transaction-types '("init" "")
642 "A list of valid transaction types.
644 It is a combination of `emoney-credit-transaction-types',
645 `emoney-debit-transaction-types', and a blank type.")
647 (defconst emoney-cheque-type
648 (if emoney-uk-cheque-spelling
649 "chq[ \t]+\\([0-9]+\\)"
650 "chk[ \t]+\\([0-9]+\\)")
651 "Type field for cheque transactions.
653 This is defined separately from the other transaction types because
654 it is used by functions that perform special operations on cheque.
657 (defconst emoney-date-column 0
658 "Column where transaction date begins.")
660 (defconst emoney-type-column 11
661 "Column where transaction type begins.")
663 (defconst emoney-clear-column 22
664 "Column where status appears.")
666 (defconst emoney-description-column 24
667 "Column where transaction description begins.")
669 (defconst emoney-sign-column 52
670 "Column where transaction sign begins.")
672 (defconst emoney-amount-column 54
673 "Column where transaction amount begins.")
675 (defconst emoney-current-balance-column 65
676 "Column where current emoney begins.")
678 (defconst emoney-tab-stop-list
683 emoney-description-column
686 emoney-current-balance-column)
687 "List of tab stops that define the start of all transaction fields.")
689 (defvar emoney-mode-map
690 (let ((map (make-sparse-keymap 'emoney-mode-map)))
691 (define-key map [(control c) (control b)] #'emoney-backward-field)
692 (define-key map [(control c) (control c)] #'emoney-recalculate-buffer)
693 (define-key map [(control c) (control d)] #'emoney-clear-current-field)
694 (define-key map [(control c) (control f)] #'emoney-forward-field)
695 (define-key map [(control c) (control n)] #'emoney-append-next-cheque)
696 (define-key map [(control c) (control r)] #'emoney-summarise-cheques-region)
697 (define-key map [(control c) (control s)] #'emoney-summarise-cheques-buffer)
698 (define-key map [(control c) (control t)] #'emoney-append-transaction)
699 (define-key map [(control c) (control x)] #'emoney-transfer-funds)
700 (define-key map [tab] #'emoney-forward-field)
701 (define-key map [(control c) b] #'emoney-go-to-bank)
702 (define-key map [(control c) c] #'emoney-calc)
703 (define-key map [(control c) s] #'emoney-switch-to-account)
704 (define-key map [(control c) q] #'emoney-quit)
705 (define-key map [(control c) (control q)] #'emoney-recalc-and-exit)
707 "Keymap for emoney buffer.")
709 (defconst emoney-mode-menu
711 ["New A/C" emoney-new-account t]
714 (if emoney-uk-cheque-spelling
717 "Transaction") emoney-append-next-cheque t]
718 ["New Transaction" emoney-append-transaction t]
719 ["Transfer Funds" emoney-transfer-funds t]
721 ["Next Field" emoney-forward-field t]
722 ["Previous Field" emoney-backward-field t]
723 ["Clear Current Field" emoney-clear-current-field t]
725 [(concat "Summary of "
726 (if emoney-uk-cheque-spelling
729 "(buffer)") emoney-summarise-cheques-buffer t]
730 [(concat "Summary of "
731 (if emoney-uk-cheque-spelling
734 "(region)") emoney-summarise-cheques-region t]
736 ["Recalculate Buffer" emoney-recalculate-buffer t]
738 ["Go To The Bank!" emoney-go-to-bank t]
739 ["Calculator" emoney-calc t]
741 ["Recalc All A/C's and Exit" emoney-recalc-and-exit t]
742 ["Exit eMoney" emoney-quit t])
743 "Menu for `emoney-mode' buffers.")
746 emoney-mode-easymenu nil "eMoney" emoney-mode-menu)
748 (defvar emoney-credit-type-keywords
749 (regexp-opt (append emoney-credit-transaction-types
750 (list emoney-cr-transfer-transaction-type)))
751 "eMoney font lock keywords for credit tran types")
753 (defvar emoney-debit-type-keywords
754 (regexp-opt (append emoney-debit-transaction-types
755 (list emoney-db-transfer-transaction-type)))
756 "eMoney font lock keywords for debit tran types")
758 (defvar emoney-font-lock-keywords
759 `(("x\\s-\\(.*\\)[\\+-]\\s-" (1 emoney-clear-tran-face))
760 ("o\\s-\\(.*\\)[\\+-]\\s-" (1 emoney-unclear-tran-face))
761 (,emoney-credit-type-keywords . emoney-credit-face)
762 (,emoney-debit-type-keywords . emoney-debit-face)
763 ("\\(^[0-9]+\\(-\\|\\/\\)[0-9]+\\(-\\|\\/\\)[0-9]+\\)"
764 (1 emoney-date-face))
765 ("-\\s-+\\([0-9]+\\.[0-9][0-9]\\)" (1 emoney-debit-face))
766 ("-[0-9]+\\.[0-9]+" . emoney-debit-face)
767 ("[^-]\\([0-9]+\\.[0-9]+$\\)" (1 emoney-credit-face))
768 ("\\+\\s-+\\([0-9]+\\.[0-9][0-9]\\)" (1 emoney-credit-face)))
769 "Font lock keywords for `emoney-mode'.")
773 (defun emoney-mode ()
774 "Major mode for editing a buffer containing financial transactions.
775 The following bindings provide the main functionality of this mode:
779 Transactions occur on a single line and have the following fields (in
782 date The transaction date. See `emoney-date-format'.
783 type This field must either be blank or match one of the
784 expressions defined in `emoney-credit-transaction-types'
785 and `emoney-debit-transaction-types'.
786 clear Status of transaction, 'o' is open, 'x' is cleared. New
787 transactions default to 'o'.
788 description A possibly blank transaction description.
789 sign This field must either be '+', '-' or '='. '+' means
790 credit, '-' means debit, and '=' resets balance. eMoney
791 will usually guess the correct sign to use.
792 amount The transaction amount.
793 balance The balance after this transaction. This field will be
794 computed upon recalculation, you _don't_ need to fill it
795 in. Just do: \\[emoney-recalculate-buffer].
797 Any line in the buffer that does not begin with a date will be
798 considered a comment and ignored. Among other things, this allows
799 the transaction description to span several lines.
801 Changing any amount and recalculating again will update all visible
802 balances. Transactions may be commented out by putting a semi-colon
803 \(or any other non-numerical character\) at the beginning of the line.
805 Entering `emoney-mode' runs the `emoney-mode-hooks' if any exist."
807 (kill-all-local-variables)
808 (setq major-mode 'emoney-mode)
809 (setq mode-name "eMoney")
810 (use-local-map emoney-mode-map)
811 (easy-menu-add emoney-mode-easymenu)
812 (make-local-variable 'tab-stop-list)
813 (setq tab-stop-list emoney-tab-stop-list)
814 (make-local-variable 'indent-tabs-mode)
815 (setq indent-tabs-mode nil)
816 (setq indent-line-function 'emoney-forward-field)
818 (run-hooks 'emoney-mode-hooks))
820 (defun emoney-current-line ()
821 "Return the current buffer line at point."
824 (count-lines (point-min) (point))))
826 (defun emoney-completing-read (prompt table &optional predicate require-match
827 initial-contents history default)
828 "Like `completing-read', but also accepts strings.
830 Arguments PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-CONTENTS,
831 HISTORY, DEFAULT are as per `completing-read'."
836 (mapcar 'list table))
837 predicate require-match initial-contents history default))
839 (defun emoney-forward-field ()
840 "Move the cursor to the next field on the current line."
844 (defun emoney-go-to-bank (&optional url)
845 "Open your bank's URL with `browse-url'."
849 (if (or (equal url "unset")
851 (message-or-box "Please customise `emoney-bank-url'.")
854 (defun emoney-calc ()
855 "Wrapper around `calc' to get around \"window-edges bug\"."
857 (add-hook 'calc-end-hook #'(lambda ()
858 (setq calc-was-split nil)))
861 (defun emoney-last (list)
862 "Return last element in LIST."
868 (t (emoney-last (cdr list)))))
870 (defun emoney-find-largest-less-than (list item)
871 "Search a sorted LIST of numbers, return the largest number that is < ITEM."
872 (let ((list-car (car list))
873 (list-cdr (cdr list))
875 (while (and list-car (< list-car item))
877 (setq list-car (car list-cdr))
878 (setq list-cdr (cdr list-cdr)))
881 (defun emoney-find-largest-less-than-equal (list item)
882 "Return cdr of LIST starting @ the largest number that is <= to ITEM."
883 (let ((list-car (car list))
884 (list-cdr (cdr list))
886 (while (and list-car (<= list-car item))
887 (setq last (cons list-car list-cdr))
888 (setq list-car (car list-cdr))
889 (setq list-cdr (cdr list-cdr)))
892 (defun emoney-find-field (column)
893 "Return a list of the start and end of the field around COLUMN.
895 End may be nil if column is after the last defined tab stop."
896 (let ((field (emoney-find-largest-less-than-equal
897 emoney-tab-stop-list column)))
898 (if (equal 1 (length field))
899 (list (car field) nil)
900 (list (nth 0 field) (nth 1 field)))))
902 (defun emoney-backward-field ()
903 "Move the cursor to the previous entry field on the current line."
905 (let* ((col (current-column))
906 (prev (emoney-find-largest-less-than emoney-tab-stop-list col)))
908 (move-to-column prev)
909 (move-to-column (emoney-last emoney-tab-stop-list)))))
911 (defun emoney-clear-current-field ()
912 "Fill the field around point with spaces, leave point at start of field."
914 (let* ((field (emoney-find-field (current-column)))
915 (line-start (progn (beginning-of-line) (point)))
918 (untabify line-start (point))
920 (field-start (+ line-start (nth 0 field)))
921 (field-end (if (nth 1 field)
922 (+ line-start (nth 1 field))
924 (clear-rectangle field-start field-end)
925 (goto-char field-start)))
927 (defsubst emoney-build-types-list ()
928 "Dynamically build a list of transaction types.
930 This is done dynamically so that the user can change or add to the
931 list of transaction types without having to reload eMoney."
932 ;; Initialise to ("init" "").
933 (setq emoney-transaction-types '("init" ""))
934 ;; Load the debit and credit transaction types.
935 (setq emoney-transaction-types
936 (append emoney-transaction-types
937 emoney-debit-transaction-types
938 emoney-credit-transaction-types
939 (list emoney-cr-transfer-transaction-type)
940 (list emoney-db-transfer-transaction-type))))
942 ;; Stolen from Gnus' time-date.el
943 (defun emoney-days-to-time (days)
944 "Convert DAYS into a time value."
945 (let* ((seconds (* 1.0 days 60 60 24))
947 (ms (condition-case nil (floor (/ seconds rest))
948 (range-error (expt 2 16)))))
949 (list ms (condition-case nil (round (- seconds (* ms rest)))
950 (range-error (expt 2 16))))))
952 ;; Stolen from Gnus' time-date.el
953 (defun emoney-time-add (t1 t2)
954 "Add two time values. One should represent a time difference."
955 (let ((high (car t1))
956 (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1)))
957 (micro (if (numberp (car-safe (cdr-safe (cdr t1))))
961 (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2)))
962 (micro2 (if (numberp (car-safe (cdr-safe (cdr t2))))
966 (setq micro (+ micro micro2))
967 (setq low (+ low low2))
968 (setq high (+ high high2))
971 ;; `/' rounds towards zero while `mod' returns a positive number,
972 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
973 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
974 (setq micro (mod micro 1000000))
975 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
976 (setq low (logand low 65535))
978 (list high low micro)))
980 (defun emoney-append-transaction (&optional trans-type description amount)
981 "Add a transaction to the end of current buffer using today's date."
983 (goto-char (point-max))
984 (if (not (equal 0 (current-column)))
986 (let* ((date-variance
987 (read-number "Date (RET for current; -DAYS past; DAYS future): "
990 (format-time-string emoney-date-format
993 (emoney-days-to-time date-variance)))))
996 (emoney-build-types-list)
997 (let* ((type (or trans-type
998 (emoney-completing-read "Transaction type: "
999 emoney-transaction-types))))
1004 (let ((before-descript (point))
1005 (start-column (current-column))
1006 (fill-column (- emoney-sign-column
1007 emoney-description-column
1010 (insert (or description
1011 (read-string "Description: ")))
1013 (narrow-to-region before-descript (point))
1014 (goto-char before-descript)
1015 (while (progn (move-to-column fill-column) (not (eobp)))
1016 (search-forward " " nil t)
1019 (goto-char before-descript)
1021 (while (progn (beginning-of-line) (not (eobp)))
1022 (indent-to-column start-column)
1023 (forward-line 1)))))
1025 (cond ((or (member type emoney-credit-transaction-types)
1026 (string= type emoney-cr-transfer-transaction-type))
1028 ((or (member type emoney-debit-transaction-types)
1029 (string= type emoney-db-transfer-transaction-type))
1031 ((string= type "init")
1033 (t (if (y-or-n-p "Is this a credit transaction? ")
1035 (if (y-or-n-p "Is this a debit transaction? ")
1037 (if (y-or-n-p "Is this an initialising transaction? ")
1039 (warn "Couldn't determine +/-/=, leaving blank"))))))
1042 (number-to-string amount)
1043 (read-string "Amount: ")))
1044 (run-hooks 'emoney-transaction-hook)))
1046 (defun emoney-append-next-cheque ()
1047 "Add a cheque transaction to the end of the current buffer using today's date.
1049 Inserts the cheque number following the last cheque number written into the
1050 transaction type column. Loses if you write cheques out of order."
1052 (goto-char (point-max))
1053 (if (not (equal 0 (current-column)))
1055 (insert (format-time-string emoney-date-format))
1057 (let (cheque cheque-number noinit)
1059 (if (search-backward-regexp emoney-cheque-type 0 t)
1061 (setq cheque (buffer-substring (match-beginning 1) (match-end 1)))
1063 (move-to-column emoney-type-column)
1064 (if (< (current-column) emoney-type-column)
1065 (indent-to-column emoney-type-column))
1066 (if emoney-uk-cheque-spelling
1067 (insert "chq 000001")
1068 (insert "chk 000001"))
1071 (setq cheque-number (1+ (string-to-number cheque)))
1072 (move-to-column emoney-type-column)
1073 (if (< (current-column) emoney-type-column)
1074 (indent-to-column emoney-type-column))
1075 (if emoney-uk-cheque-spelling
1076 (insert (format "chq %06d" cheque-number))
1077 (insert (format "chk %06d" cheque-number))))
1081 (let ((before-descript (point))
1082 (start-column (current-column))
1083 (fill-column (- emoney-sign-column
1084 emoney-description-column
1087 (insert (read-string "Payable To: "))
1089 (narrow-to-region before-descript (point))
1090 (goto-char before-descript)
1091 (while (progn (move-to-column fill-column) (not (eobp)))
1092 (search-forward " " nil t)
1095 (goto-char before-descript)
1097 (while (progn (beginning-of-line) (not (eobp)))
1098 (indent-to-column start-column)
1099 (forward-line 1)))))
1103 (insert (read-string "Amount: "))
1104 (run-hooks 'emoney-transaction-hook)
1105 (run-hooks 'emoney-transaction-cheque-hook)))
1107 (defvar emoney-transfer-account-history nil)
1109 (defun emoney-transfer-funds (&optional from to amount)
1110 "Transfer funds from one eMoney account to another.
1112 Argument FROM is the account to transfer from.
1113 Argument To is the account to transfer to.
1114 Argument AMOUNT is how much to transfer."
1116 (let ((from (or from
1117 (emoney-completing-read "Transfer from: "
1118 emoney-chart-of-accounts
1121 (concat emoney-current-account-name
1123 emoney-transfer-account-history)))
1125 (emoney-completing-read "Transfer to: "
1126 emoney-chart-of-accounts nil t nil
1127 emoney-transfer-account-history)))
1129 (read-number "Amount: ")))
1130 (current-ac (concat emoney-current-account-name ".emy")))
1131 (with-current-buffer from
1132 (emoney-append-transaction
1133 emoney-db-transfer-transaction-type
1134 (concat "T'fer to " (file-name-sans-extension to))
1136 (with-current-buffer to
1137 (emoney-append-transaction
1138 emoney-cr-transfer-transaction-type
1139 (concat "T'fer from " (file-name-sans-extension from))
1141 (loop for buf in '(from to)
1142 do (emoney-switch-to-account (symbol-value buf))
1143 do (emoney-recalculate-buffer))
1144 (run-hooks 'emoney-transaction-transfer-hook)
1145 (emoney-switch-to-account current-ac)))
1147 (defsubst emoney-build-type-regexp ()
1148 "Return a regular expression that will match any valid transaction type.
1150 This is done dynamically so users can redefine the valid transactions in
1151 their `user-init-file' even after this file has been loaded."
1152 (emoney-build-types-list)
1153 (let ((types (append emoney-transaction-types
1154 (list emoney-cheque-type))))
1158 #'(lambda(x) x) types "\\|")
1161 (defun emoney-check-transaction-type (line-start)
1162 "Check to make sure a valid transaction type has been used.
1164 Argument LINE-START is the starting point.
1166 Please note that the word \"check\" here means \"verify\" and it has
1167 nothing to do with the American spelling of the word \"cheque\"."
1168 (let* ((type-regexp (emoney-build-type-regexp))
1169 (type-start (+ line-start emoney-type-column))
1170 (type-end (+ line-start emoney-clear-column))
1171 (type-string (buffer-substring type-start type-end)))
1172 (if (string-match type-regexp type-string)
1174 (error "Line %d, invalid type: %s"
1175 (1+ (emoney-current-line)) type-string))))
1177 (defun emoney-find-next-transaction ()
1178 "Find the next line that is a complete transaction.
1180 Return a list of the line start, numeric data start and line end
1184 "^\\([0-90-9]\\)+.*$")
1190 (search-forward-regexp line-regexp (point-max) t))
1191 (setq line-start (match-beginning 0))
1192 (setq line-end (progn (end-of-line) (point)))
1193 (setq data-start (+ line-start emoney-sign-column))
1194 (if (> line-end data-start)
1197 (list line-start data-start line-end)
1200 (defun emoney-parse-transaction-data (data)
1201 "Return a list of floating point numbers from DATA.
1203 DATA is a string representing the sign, amount and optionally balance of a
1204 transaction. Balance is nil if not present."
1205 (let ((data-regexp "\\([-+=]\\)[ \t]*\\([0-9.]+\\)?[ \t]*\\([-]?[0-9.]+\\)?")
1209 (string-match data-regexp data)
1210 (if (match-beginning 1)
1211 (setq sign (substring data (match-beginning 1) (match-end 1)))
1212 (error "Line %d, missing sign" (1+ (emoney-current-line))))
1213 (if (equal "=" sign)
1217 (if (match-beginning 2)
1219 (setq string (substring data (match-beginning 2) (match-end 2)))
1220 (setq amount (string-to-number (concat sign string))))
1221 (error "Line %d, missing amount" (1+ (emoney-current-line))))
1222 (if (match-beginning 3)
1224 (setq string (substring data (match-beginning 3) (match-end 3)))
1225 (setq balance (string-to-number string))))
1226 (if reset (setq sign "="))
1227 (list sign amount balance)))
1229 (defun emoney-same (amount1 amount2)
1230 "Compare two dollar amounts, AMOUNT1 AMOUNT2, for equivalence."
1231 (let ((string1 (format "%10.2f" amount1))
1232 (string2 (format "%10.2f" amount2)))
1233 (equal string1 string2)))
1235 (defun emoney-form-transaction-data (sign amount balance)
1236 "Given SIGN, AMOUNT and a BALANCE, return a string.
1238 The string is suitable for placing in the numeric region of a
1239 transaction, based on the defined input columns."
1240 (let* ((amount (abs amount))
1241 (width1 (- emoney-amount-column emoney-sign-column))
1242 (width2 (- emoney-current-balance-column emoney-amount-column))
1243 (len (length emoney-largest-balance))
1244 (gap (- width2 len))
1245 (value (concat "%" (number-to-string len) ".2f"))
1246 (format-string (concat "%-" (number-to-string width1)
1248 "%" (number-to-string gap) "s" value)))
1249 (format format-string sign amount "" balance)))
1251 (defun emoney-recalculate (start end)
1252 "Recalculate the balances for region START END.
1254 The final balance, uncleared total, and the number of balances that
1255 changed, and the transaction count are returned in a list."
1256 (run-hooks 'emoney-recalculate-before-hook)
1257 (let ((current-balance 0)
1264 (narrow-to-region start end)
1265 (untabify (point-min) (point-max))
1266 (goto-char (point-min))
1267 (while (setq line-points (emoney-find-next-transaction))
1268 (setq transactions (1+ transactions))
1269 (let* ((line-start (nth 0 line-points))
1270 (data-start (nth 1 line-points))
1271 (data-end (nth 2 line-points))
1272 (clear-flag (buffer-substring
1273 (+ line-start emoney-clear-column)
1274 (+ 1 line-start emoney-clear-column)))
1275 (data-string (buffer-substring data-start data-end))
1276 (data-values (emoney-parse-transaction-data data-string))
1277 (sign (nth 0 data-values))
1278 (amount (nth 1 data-values))
1279 (balance (nth 2 data-values))
1280 (new-balance (if (equal sign "=")
1282 (+ current-balance amount)))
1283 (new-uncleared (if (equal clear-flag "x")
1285 (+ uncleared amount)))
1287 (emoney-form-transaction-data sign amount new-balance)))
1288 (emoney-check-transaction-type line-start)
1289 (setq current-balance new-balance)
1290 (setq uncleared new-uncleared)
1291 (if (or (null balance) (not (emoney-same balance new-balance)))
1292 (setq changes (1+ changes)))
1293 (if (not (equal data-string new-string))
1295 (delete-region data-start data-end)
1296 (goto-char data-start)
1297 (insert new-string)))))
1299 (run-hooks 'emoney-recalculate-after-hook)
1300 (list current-balance uncleared changes transactions)))
1302 (defvar emoney-is-exiting nil
1303 "Non-nil when eMoney is in the process of quitting.")
1305 (defun emoney-update-acc-buf-bal (account balance)
1306 "Update ACCOUNT BALANCE in accounts buffer."
1307 (with-current-buffer emoney-accounts-buffer
1308 (goto-char (point-min))
1309 (search-forward account)
1311 (move-to-column 13 'force)
1314 (number-to-string (- (length emoney-largest-balance)
1315 (length (format "%.2f" balance))))
1316 "s$%.2f") "" balance)
1319 'emoney-credit-face))))
1321 (defun emoney-recalculate-buffer ()
1322 "Recalculate the current buffer.
1324 See `emoney-recalculate'."
1326 (let* ((result (emoney-recalculate (point-min) (point-max)))
1327 (balance (nth 0 result))
1328 (uncleared (nth 1 result))
1329 (changes (nth 2 result))
1330 (total (nth 3 result)))
1331 (when emoney-save-after-recalculate
1332 (save-buffer (current-buffer)))
1333 (emoney-update-acc-buf-bal emoney-current-account-name balance)
1334 (if emoney-is-exiting
1336 (format "book bal %.2f unclrd %.2f bank bal %.2f (%d/%d recalcs)"
1337 balance uncleared (- balance uncleared) changes total))
1339 (format "book bal %.2f unclrd %.2f bank bal %.2f (%d/%d recalcs)"
1340 balance uncleared (- balance uncleared) changes total)))
1344 (defun emoney-recalculate-region (start end)
1345 "Recalculate the current region, START END.
1347 See `emoney-recalculate'."
1349 (let* ((result (emoney-recalculate start end))
1350 (balance (nth 0 result))
1351 (uncleared (nth 1 result))
1352 (changes (nth 2 result))
1353 (total (nth 3 result)))
1354 (message-or-box (format "Region balance %.2f uncleared %.2f (%d/%d recalcs)"
1355 balance uncleared changes total))
1359 (defun emoney-summarise-cheques (start end)
1360 "Create a buffer that lists only the cheques in the specified region.
1362 The region is denoted by START END.
1364 The list is sorted on cheque number. Breaks in sequence are denoted by lines
1365 containing an asterisk between the cheques where the break occurs. The buffer
1366 is also recalculated, thus showing to total of the cheques summarised."
1367 (let ((emoney-buffer (current-buffer))
1368 (summary-buffer (get-buffer-create "*cheque summary*"))
1374 (set-buffer summary-buffer)
1375 (delete-region (point-min) (point-max))
1376 (set-buffer emoney-buffer)
1377 (narrow-to-region start end)
1378 (goto-char (point-min))
1379 (while (setq line-points (emoney-find-next-transaction))
1380 (let* ((line-start (nth 0 line-points))
1381 (line-end (nth 2 line-points))
1382 (type-start (+ line-start emoney-type-column))
1383 (type-end (+ line-start emoney-description-column))
1384 (type-string (buffer-substring type-start type-end)))
1385 (if (string-match emoney-cheque-type type-string)
1388 summary-buffer line-start (1+ line-end))
1389 (setq cheque-count (1+ cheque-count))))))
1391 (set-buffer summary-buffer)
1392 (sort-numeric-fields 3 (point-min) (point-max))
1393 (setq sequence-breaks (emoney-find-sequence-breaks))
1394 (goto-char (point-max))
1395 (insert (format "\n%d cheque%s summarised, %d sequence break%s\n"
1397 (if (equal 1 cheque-count) "" "s")
1399 (if (equal 1 sequence-breaks) "" "s")))
1400 (set-buffer emoney-buffer)))
1406 (insert-buffer summary-buffer)
1407 (buffer-string (current-buffer)))))
1408 "*cheque summary*"))
1409 (run-hooks 'emoney-summarise-cheques-hook))
1411 (defun emoney-find-sequence-breaks ()
1412 "Find cheque sequence breaks in the current cheque summary buffer.
1414 Mark breaks in sequence by inserting a line with an asterisk between
1415 the offending cheques. Return the count of sequence breaks found."
1416 (let ((last-cheque nil)
1417 (sequence-breaks 0))
1418 (goto-char (point-min))
1419 (while (search-forward-regexp
1420 (concat "\\([0-9/]+\\)[ \t]+" emoney-cheque-type)
1422 (let* ((cheque-start (match-beginning 2))
1423 (cheque-end (match-end 2))
1424 (cheque-string (buffer-substring cheque-start cheque-end))
1425 (cheque-number (string-to-int cheque-string)))
1426 (if (not last-cheque)
1427 (setq last-cheque cheque-number)
1428 (if (not (equal cheque-number (1+ last-cheque)))
1430 (setq sequence-breaks (1+ sequence-breaks))
1435 (setq last-cheque cheque-number))))
1438 (defun emoney-summarise-cheques-buffer ()
1439 "Summarise the cheque transactions in the current buffer."
1441 (emoney-summarise-cheques (point-min) (point-max))
1442 (emoney-recalculate (point-min) (point-max)))
1444 (defun emoney-summarise-cheques-region (start end)
1445 "Summarise the cheque transactions in region START - END."
1447 (emoney-summarise-cheques start end)
1448 (emoney-recalculate (point-min) (point-max)))
1450 (defun emoney-new-account (&optional name balance)
1451 "*Create a new A/C named NAME for use with eMoney."
1453 (let ((new-acc (concat (or name
1454 (read-string "New A/C Name: ")) ".emy"))
1456 (read-number "Initial Balance: " nil 0))))
1458 (expand-file-name new-acc emoney-accounts-directory))
1459 (setq emoney-chart-of-accounts
1460 (push new-acc emoney-chart-of-accounts))
1461 (select-window (get-buffer-window
1462 (concat emoney-current-account-name ".emy")))
1463 (switch-to-buffer new-acc)
1464 (setq emoney-current-account-name
1465 (file-name-sans-extension new-acc))
1466 (emoney-append-transaction "init" "Opening Balance" bal)
1467 (emoney-recalculate-buffer)
1468 (emoney-show-buffers)
1469 (switch-to-buffer new-acc)
1470 (goto-char (point-max))
1471 (run-hooks 'emoney-new-account-hook)))
1473 (defun emoney-setup-control-buffer ()
1474 "Set up the eMoney \"Control\" buffer."
1475 (let ((buf emoney-control-buffer))
1477 (when (buffer-live-p (get-buffer-create buf))
1479 (set-buffer (get-buffer-create buf))
1480 (widget-create 'push-button
1481 :notify (lambda (&rest ignore)
1482 (emoney-new-account))
1483 :help-echo "Create a new eMoney account."
1486 (widget-create 'push-button
1487 :notify (lambda (&rest ignore)
1490 (concat emoney-current-account-name ".emy"))
1491 (emoney-append-transaction)))
1492 :help-echo "Add a new transaction\n
1493 If you want to add a cheque transaction
1494 use \"Add Chq\" instead."
1497 (widget-create 'push-button
1498 :notify (lambda (&rest ignore)
1501 (concat emoney-current-account-name ".emy"))
1502 (emoney-append-next-cheque)))
1503 :help-echo (if emoney-uk-cheque-spelling
1504 "Add a new cheque transaction."
1505 "Add a new check transaction.")
1506 (if emoney-uk-cheque-spelling
1510 (widget-create 'push-button
1511 :notify (lambda (&rest ignore)
1514 (concat emoney-current-account-name ".emy"))
1515 (emoney-recalculate-buffer)))
1516 :help-echo "Record the last transaction.\n
1517 Also recalculates the buffer."
1519 (widget-insert "\n\n")
1520 (widget-create 'push-button
1521 :notify (lambda (&rest ignore)
1524 (concat emoney-current-account-name ".emy"))
1525 (emoney-transfer-funds)))
1526 :help-echo "Transfer funds between accounts."
1529 (widget-create 'push-button
1530 :notify (lambda (&rest ignore)
1533 (concat emoney-current-account-name ".emy"))
1534 (emoney-summarise-cheques-buffer)))
1535 :help-echo (if emoney-uk-cheque-spelling
1536 "Display a summary of cheques."
1537 "Display a summary of checks.")
1538 (if emoney-uk-cheque-spelling
1542 (widget-create 'push-button
1543 :notify (lambda (&rest ignore)
1545 :help-echo "Start the Emacs Calculator.\n
1546 So you can count up all
1550 (widget-create 'push-button
1551 :notify (lambda (&rest ignore)
1554 "eMoney: %s, \"%s\" [Beta]"
1555 emoney-version emoney-codename)
1557 "eMoney: %s, \"%s\""
1558 emoney-version emoney-codename)))
1559 :help-echo "Display eMoney version info."
1561 (widget-insert "\n\n")
1562 (widget-create 'push-button
1563 :notify (lambda (&rest ignore)
1564 (emoney-go-to-bank))
1565 :help-echo "Open your bank's web site in your browser."
1568 (widget-create 'push-button
1569 :notify (lambda (&rest ignore)
1571 :help-echo "Exit eMoney."
1573 (set-specifier horizontal-scrollbar-visible-p nil (current-buffer))
1574 (set-specifier vertical-scrollbar-visible-p nil (current-buffer))
1575 (set-specifier has-modeline-p nil (current-buffer))
1576 (run-hooks 'emoney-setup-control-buffer-hook))))
1578 (defun emoney-show-buffers ()
1579 "Display all the eMoney buffers."
1580 (emoney-setup-accounts-buffer)
1581 (emoney-setup-control-buffer)
1582 (emoney-setup-header-buffer)
1583 (delete-other-windows nil)
1584 (switch-to-buffer emoney-accounts-buffer)
1585 (split-window nil emoney-accounts-buffer-height)
1586 (split-window nil emoney-accounts-buffer-width t)
1588 (switch-to-buffer emoney-control-buffer)
1590 (switch-to-buffer emoney-header-buffer)
1591 (split-window nil emoney-header-buffer-height)
1594 (defun emoney-quit ()
1595 "*Exit from eMoney, optionally recalculating all accounts first.
1597 To have all of your accounts recalculated before eMoney exits set
1598 `emoney-recalculate-on-quit' to `t'
1600 If `emoney-save-after-recalculate' is also `t' the account buffers
1601 will be saved before eMoney exits."
1603 (run-hooks 'emoney-quit-before-hook)
1604 (let ((accounts emoney-chart-of-accounts))
1605 (setq emoney-is-exiting t)
1606 (dolist (buf accounts)
1607 (when emoney-recalculate-on-quit
1609 (emoney-recalculate-buffer))
1611 (kill-buffer emoney-accounts-buffer)
1612 (kill-buffer emoney-control-buffer)
1613 (kill-buffer emoney-header-buffer)
1614 (run-hooks 'emoney-quit-after-hook)
1615 (when (and emoney-use-new-frame
1616 (frame-live-p emoney-frame))
1617 (delete-frame emoney-frame))
1618 (setq emoney-frame nil)
1619 (unless emoney-use-new-frame
1620 (jump-to-register ?\$))
1621 (setq emoney-is-exiting nil)))
1623 (defun emoney-recalc-and-exit ()
1624 "*Exit form eMoney, recalculating all accounts first."
1626 (let ((old-recalc-val emoney-recalculate-on-quit))
1627 (setq emoney-recalculate-on-quit t)
1629 (setq emoney-recalculate-on-quit old-recalc-val)))
1633 "*Start a new eMoney session."
1635 (unless emoney-use-new-frame
1636 (window-configuration-to-register ?\$))
1637 (unless (frame-live-p emoney-frame)
1639 (if emoney-use-new-frame
1640 (make-frame '((name . "eMoney")))
1642 (select-frame emoney-frame)
1643 (let ((accounts emoney-chart-of-accounts)
1644 (dir emoney-accounts-directory))
1646 (find-file (expand-file-name (car accounts) dir))
1647 (unless (eq major-mode 'emoney-mode)
1649 (setq accounts (cdr accounts)))
1650 (setq emoney-current-account-name
1651 (file-name-sans-extension emoney-default-account))
1652 (emoney-show-buffers)
1653 (switch-to-buffer emoney-default-account)
1654 (goto-char (point-max)))
1655 (focus-frame emoney-frame))
1657 ;; Work around a problem with Emacs calc. If you start calc in a
1658 ;; frame with multiple buffers visible when calc exits it doesn't
1659 ;; return point to the place it was when you called calc. These
1660 ;; advices overcome that.
1661 (defadvice calc (before em-calc-win-save first activate)
1662 "Before starting calc, save the window config.
1663 This is so we can restore the window config when calc exits because
1664 calc doesn't DTRT in this regard by itself."
1665 (push-window-configuration))
1667 (defadvice calc-quit (after em-calc-win-restore last activate)
1668 "Restore the \"pre calc\" window config on calc exit."
1669 (pop-window-configuration))
1672 ;;;###autoload(add-to-list 'auto-mode-alist '("\\.emy$" . emoney-mode))
1676 ;;; emoney.el ends here