Improve and simplify a few interactive defuns
[emoney] / emoney.el
index 407fcea..5850ce9 100644 (file)
--- a/emoney.el
+++ b/emoney.el
@@ -452,20 +452,16 @@ See `emoney-recalculate-before-hook' for doing things before recalculating."
 (defun emoney-version (&optional arg)
   "*Display the current version information for eMoney.
 
-Optional prefix Argument ARG, print version information at point
+Prefix Argument ARG, print version information at point
 in the current buffer."
   (interactive "P")
-  (if emoney-is-beta
-      (if arg
-         (insert (format "eMoney: %s, \"%s\" [Beta]"
-                         emoney-version emoney-codename))
-       (message (format "eMoney: %s, \"%s\" [Beta]"
-                        emoney-version emoney-codename)))
+  (let ((fmt-string "eMoney: %s, \"%s\""))
+    (when emoney-is-beta
+      (setq fmt-string (concat fmt-string " [Beta]")))
     (if arg
-       (insert (format "eMoney: %s, \"%s\""
-                       emoney-version emoney-codename))
-      (message (format "eMoney: %s, \"%s\""
-                      emoney-version emoney-codename)))))
+       (insert (format fmt-string emoney-version emoney-codename))
+      (message fmt-string emoney-version emoney-codename))))
+
 
 ;;;###autoload
 (defun emoney-commentary ()
@@ -517,24 +513,23 @@ in the current buffer."
 (defvar emoney-current-account-name 
   (file-name-sans-extension emoney-default-account))
 
-(defun emoney-switch-to-account (&optional account)
+(defun emoney-switch-to-account (account)
   "Switch to account, ACCOUNT."
-  (interactive)
-  (let ((switch-acc (or account
-                       (emoney-completing-read "Switch to A/C: "
-                                               emoney-chart-of-accounts nil t))))
-    (select-window (get-buffer-window
-                   (concat emoney-current-account-name ".emy")))
-    (switch-to-buffer switch-acc)
-    (goto-char (point-max))
-    (setq emoney-current-account-name
-         (file-name-sans-extension switch-acc))
-    (select-window (get-buffer-window emoney-header-buffer))
-    (emoney-setup-header-buffer)
-    (switch-to-buffer emoney-header-buffer)
-    (select-window (get-buffer-window
-                   (concat emoney-current-account-name ".emy")))
-    (run-hooks 'emoney-switch-account-hook)))
+  (interactive
+   (list (emoney-completing-read "Switch to A/C: "
+                                emoney-chart-of-accounts nil t)))
+  (select-window (get-buffer-window
+                 (concat emoney-current-account-name ".emy")))
+  (switch-to-buffer account)
+  (goto-char (point-max))
+  (setq emoney-current-account-name
+       (file-name-sans-extension account))
+  (select-window (get-buffer-window emoney-header-buffer))
+  (emoney-setup-header-buffer)
+  (switch-to-buffer emoney-header-buffer)
+  (select-window (get-buffer-window
+                 (concat emoney-current-account-name ".emy")))
+  (run-hooks 'emoney-switch-account-hook))
 
 (defun emoney-mouse-switch-to-account (event)
   "Switch to account under EVENT."
@@ -701,6 +696,7 @@ transactions.")
     (define-key map [(control c) (control t)] #'emoney-append-transaction)
     (define-key map [(control c) (control x)] #'emoney-transfer-funds)
     (define-key map [tab]                     #'emoney-forward-field)
+    (define-key map [iso-left-tab]            #'emoney-backward-field)
     (define-key map [(control c) b]           #'emoney-go-to-bank)
     (define-key map [(control c) c]           #'emoney-calc)
     (define-key map [(control c) s]           #'emoney-switch-to-account)
@@ -844,15 +840,13 @@ HISTORY, DEFAULT are as per `completing-read'."
   (interactive)
   (move-to-tab-stop))
 
-(defun emoney-go-to-bank (&optional url)
+(defun emoney-go-to-bank ()
   "Open your bank's URL with `browse-url'."
   (interactive)
-  (let ((url (or url
-                emoney-bank-url)))
-    (if (or (equal url "unset")
-           (not url))
-       (message-or-box "Please customise `emoney-bank-url'.")
-      (browse-url url))))
+  (if (or (equal emoney-bank-url "unset")
+         (not emoney-bank-url))
+      (message-or-box "Please customise `emoney-bank-url'.")
+    (browse-url emoney-bank-url)))
 
 (defun emoney-calc ()
   "Wrapper around `calc' to get around \"window-edges bug\"."
@@ -1109,28 +1103,23 @@ transaction type column.  Loses if you write cheques out of order."
 
 (defvar emoney-transfer-account-history nil)
 
-(defun emoney-transfer-funds (&optional from to amount)
+(defun emoney-transfer-funds (from to amount)
   "Transfer funds from one eMoney account to another.
 
 Argument FROM is the account to transfer from.
 Argument To is the account to transfer to.
 Argument AMOUNT is how much to transfer."
-  (interactive)
-  (let ((from (or from
-                 (emoney-completing-read "Transfer from: "
-                                         emoney-chart-of-accounts
-                                         nil
-                                         t
-                                         (concat emoney-current-account-name
-                                                 ".emy")
-                                         emoney-transfer-account-history)))
-       (to (or to
-               (emoney-completing-read "Transfer to: "
-                                       emoney-chart-of-accounts nil t nil
-                                       emoney-transfer-account-history)))
-       (amount (or amount
-                   (read-number "Amount: ")))
-       (current-ac (concat emoney-current-account-name ".emy")))
+  (interactive
+   (list (emoney-completing-read "Transfer from: "
+                                emoney-chart-of-accounts nil t
+                                (concat emoney-current-account-name
+                                        ".emy")
+                                emoney-transfer-account-history)
+        (emoney-completing-read "Transfer to: "
+                                emoney-chart-of-accounts nil t nil
+                                emoney-transfer-account-history)
+        (read-number "Amount: ")))
+  (let ((current-ac (concat emoney-current-account-name ".emy")))
     (with-current-buffer from
       (emoney-append-transaction 
        emoney-db-transfer-transaction-type
@@ -1450,28 +1439,26 @@ the offending cheques.  Return the count of sequence breaks found."
   (emoney-summarise-cheques start end)
   (emoney-recalculate (point-min) (point-max)))
 
-(defun emoney-new-account (&optional name balance)
-  "*Create a new A/C named NAME for use with eMoney."
-  (interactive)
-  (let ((new-acc (concat (or name
-                            (read-string "New A/C Name: ")) ".emy"))
-       (bal (or balance
-                (read-number "Initial Balance: " nil 0))))
-    (find-file-noselect 
-     (expand-file-name new-acc emoney-accounts-directory))
-    (setq emoney-chart-of-accounts
-         (push new-acc emoney-chart-of-accounts))
-    (select-window (get-buffer-window 
-                   (concat emoney-current-account-name ".emy")))
-    (switch-to-buffer new-acc)
-    (setq emoney-current-account-name 
-         (file-name-sans-extension new-acc))
-    (emoney-append-transaction "init" "Opening Balance" bal)
-    (emoney-recalculate-buffer)
-    (emoney-show-buffers)
-    (switch-to-buffer new-acc)
-    (goto-char (point-max))
-    (run-hooks 'emoney-new-account-hook)))
+(defun emoney-new-account (new-acc bal)
+  "*Create a new A/C named NEW-ACC with initial balance BAL."
+  (interactive
+   (list (concat (read-string "New A/C Name: ") ".emy")
+        (read-number "Initial Balance: " nil 0)))
+  (find-file-noselect 
+   (expand-file-name new-acc emoney-accounts-directory))
+  (setq emoney-chart-of-accounts
+       (push new-acc emoney-chart-of-accounts))
+  (select-window (get-buffer-window 
+                 (concat emoney-current-account-name ".emy")))
+  (switch-to-buffer new-acc)
+  (setq emoney-current-account-name 
+       (file-name-sans-extension new-acc))
+  (emoney-append-transaction "init" "Opening Balance" bal)
+  (emoney-recalculate-buffer)
+  (emoney-show-buffers)
+  (switch-to-buffer new-acc)
+  (goto-char (point-max))
+  (run-hooks 'emoney-new-account-hook))
 
 (defun emoney-setup-control-buffer ()
   "Set up the eMoney \"Control\" buffer."
@@ -1482,7 +1469,7 @@ the offending cheques.  Return the count of sequence breaks found."
       (set-buffer (get-buffer-create buf))
       (widget-create 'push-button
                     :notify (lambda (&rest ignore)
-                              (emoney-new-account))
+                              (call-interactively 'emoney-new-account))
                     :help-echo "Create a new eMoney account."
                     " New A/C ")
       (widget-insert " ")
@@ -1525,7 +1512,7 @@ Also recalculates the buffer."
                               (save-excursion
                                 (set-buffer
                                  (concat emoney-current-account-name ".emy"))
-                                (emoney-transfer-funds)))
+                                (call-interactively 'emoney-transfer-funds)))
                     :help-echo "Transfer funds between accounts."
                     "Transfer ")
       (widget-insert " ")