   (save-match-data
     (let ((expanded-cmd report-cmd))
       (set-match-data (list 0 0))
-      (while (string-match "%(\\([^)]*\\))" expanded-cmd (match-end 0))
+      (while (string-match "%(\\([^)]*\\))" expanded-cmd (if (> (length expanded-cmd) (match-end 0))
+							     (match-end 0)
+							     (1- (length expanded-cmd))))
 	(let* ((specifier (match-string 1 expanded-cmd))
 	       (f (cdr (assoc specifier ledger-report-format-specifiers))))
 	  (if f

     (define-key map [menu-bar ldg-rep vis] '("Visit Source" . ledger-report-visit-source))
     (define-key map [menu-bar ldg-rep lru] '("Scroll Up" . scroll-up))
     (define-key map [menu-bar ldg-rep s1] '("--"))
     (define-key map [menu-bar ldg-rep lrk] '("Kill Report" . ledger-report-kill))
     (define-key map [menu-bar ldg-rep lrr] '("Re-run Report" . ledger-report-redo))
     (define-key map [menu-bar ldg-rep lre] '("Edit Report" . ledger-report-edit))

(defcustom ledger-reports
  '(("bal" "ledger -f %(ledger-file) bal")
    ("reg" "ledger -f %(ledger-file) reg")
    ("payee" "ledger -f %(ledger-file) reg @%(payee)")
    ("account" "ledger -f %(ledger-file) reg %(account)"))
  "Definition of reports to run.

