I like the idea of having a ledger section in my daily planner files.
Despite getting the cvs version of planner-ledger.el, it just didn't work
straight
out-of-the-box.

This was a surprise since planner-ledger doesn't do anything terribly
complicated and the file hasn't changed for over a year. Even basic
functionality of
the planner-ledger concept did not work, while ledger worked fine on its
own.

My system is emacs-snapshot, planner from mwolson latest package, and ledger
version 2.6. Ledger version 2.6 or 3 should not make any difference for
planner-ledger, correct ?

I hope you find these changes usefull and merge them to the package.

Anyway, made the following changes to planner-ledger.el:

  * Added a definition for a planner-ledger customize group  under the
planner
    group, for customization needs.
  * Changed variable name ledger-data-file to planner-ledger-data-file
  * Added a definition for variable planner-ledger-data-file to customize.
  * simplified the regular expressions planner-ledger-balance-regexp and
    planner-ledger-pending-regexp.
  * Added: clear text in ledger sections before an insert
  * Added: some extra documentation to functions
  * Bugfix: variable planner-ledger-balance-args for correct parsing
  * Modified: pass the planner-ledger-data-file as an option to the legder
    command, unless it is being used by an buffer. Previous behavior would
always
    load the file in an emacs buffer.
  * Modified functions, planner-ledger-insert-maybe,
    planner-ledger-insert-section-maybe, planner-ledger-add-entry-from-task
  * Added functions planner-ledger-insert-balance-maybe,
    planner-ledger-insert-pending-maybe, planner-ledger-clear-balance,
    planner-ledger-clear-pending, planner-ledger-clear-section,
    planner-ledger-goto-section-end, planner-ledger-run-ledger
  * Modified function planner-ledger-add-entry-from-task to zero pad date
and month
    entries.


For those interested to review the proposed changes, here is a more detailed
description:

  * a planner-ledger customize group was clearly missing
  * using a variable name planner-ledger-data-file instead of
ledger-data-file
    improves consistency and avoids any potential name clash with package
ledger.el
    The full path to the data-file must be given, which means no '~',
'$HOME', etc.
    This implies that a default file name like '~/money.ledger' can't be
used.
    Removing this restriction should be in a TODO list.
  * Simplified the section regular expressions from "^* Ledger\n\n$" to "^\*
Ledger *$".
    Bad practice of newlines in a regex, was used for formating (probably).
  * Clearing the text in ledger section is necessary for updating it to
reflect the
    latest ledger data file changes. Previous behaviour did not allow the
sections
    to be updated. Planner-ledger-clear-pending and
planner-ledger-clear-balance
  * changed "\"next month\"" in planner-ledger-balance-args to "next month".
The
    ledger balance command was choking on the extra quotes.
  * no need to open a buffer on planner-ledger-data-file if not already
loaded. If
    the data file is loaded in a buffer, pass the buffer to the ledger
command
    Otherwise pass the buffer name as an option to the the ledger command.
  *


Ledger.el

Also made a small change to ledger.el (Version: 3.0, Date: Thu 16-Apr-2007)
that
comes from the svn sources.

 - Added the key sequence  C-c C-e to ledger-toggle-current-entry. It is
convenient to
   directly toggle the cleared status of an entry.
 - version number assigned to variable ledger-version seems to be out of
sync with
   version number in the file header comments. Just noting, made no changes
to
   version numbers.


Here is the patch to planner-ledger:

--- planner-ledger.el.orig    2007-08-23 13:29:18.000000000 +0300
+++ planner-ledger.el    2007-08-23 11:45:23.000000000 +0300
@@ -55,14 +55,25 @@
 (require 'planner)

 ;;; Code:
+(defgroup planner-ledger nil
+  "Planner-ledger provides integration between planner and
+John Wiegley's ledger accounting program"
+  :group 'planner)
+
+(defcustom planner-ledger-data-file
+  nil
+  "Ledger file to use. The full path to the data file."
+  :type  '(file :must-match t)
+  :group 'planner-ledger)
+
 (defcustom planner-ledger-balance-regexp
-  "^* Ledger\n\n$"
+  "^\* Ledger *$"
   "Section marker for insertion of ledger balance."
   :type 'regexp
   :group 'planner-ledger)

 (defcustom planner-ledger-pending-regexp
-  "^** Pending Transactions\n\n$"
+  "^\*\* Pending Transactions *$"
   "Section marker for insertion of pending ledger transactions."
   :type 'regexp
   :group 'planner-ledger)
@@ -74,7 +85,7 @@
   :group 'planner-ledger)

 (defcustom planner-ledger-balance-args
-  '("-s" "-e" "\"next month\"" "balance")
+  '("-s" "-e" "next month" "balance")
   "Command line arguments for ledger balance."
   :type '(repeat string)
   :group 'planner-ledger)
@@ -95,12 +106,27 @@
 (defun planner-ledger-insert-maybe ()
   "Maybe insert ledger sections into a planner page."
   (interactive)
-  (apply 'planner-ledger-insert-section-maybe
-         planner-ledger-balance-regexp
-         (append planner-ledger-balance-args
-                planner-ledger-balance-accounts))
+  (planner-ledger-insert-balance-maybe)
+  (planner-ledger-insert-pending-maybe))
+
+;;;###autoload
+  (defun planner-ledger-insert-balance-maybe ()
+    "Maybe insert ledger account balances a planner page.
+The accounts are specified in planner-ledger-balance-accounts."
+    (interactive)
+    (planner-ledger-clear-section-balance)
+    (apply 'planner-ledger-insert-section-maybe
+       planner-ledger-balance-regexp
+       (append planner-ledger-balance-args
+           planner-ledger-balance-accounts)))
+
+;;;###autoload
+(defun planner-ledger-insert-pending-maybe ()
+  "Maybe insert ledger pending transaction into a planner page."
+  (interactive)
+  (planner-ledger-clear-section-pending)
   (planner-ledger-insert-section-maybe planner-ledger-pending-regexp
-                                       "-U" "register" "."))
+                       "-U" "register" ))

 (defun planner-ledger-insert-section-maybe (regexp &rest ledger-args)
   "Maybe insert a ledger section into a planner page.
@@ -108,8 +134,50 @@
 LEDGER-ARGS contains the arguments to pass to
 `ledger-run-ledger'."
   (save-excursion
+    (goto-char (point-min))
     (when (re-search-forward regexp nil t)
-      (apply 'ledger-run-ledger ledger-args))))
+      (progn
+    (newline 2)
+    (apply 'planner-ledger-run-ledger ledger-args)))))
+
+(defun planner-ledger-clear-section-balance()
+  "Clear the planner-ledger section for Ledger balance."
+  (interactive)
+  (save-excursion
+    (planner-ledger-clear-section planner-ledger-balance-regexp "^\\*")
+    )
+  )
+
+(defun planner-ledger-clear-section-pending()
+  "Clear the planner-ledger section for pending transactons"
+  (interactive)
+  (save-excursion
+    (planner-ledger-clear-section planner-ledger-pending-regexp "^\\*")
+    )
+  )
+
+(defun planner-ledger-clear-section (regexp-start  regexp-end)
+  "Clear a planner ledger section"
+  (goto-char (point-min))
+  (when (re-search-forward regexp-start nil t)
+    (progn
+      (forward-line)
+      (delete-region (point) (if (re-search-forward regexp-end nil t)
+                 (line-beginning-position)
+                   (point-max))
+             )
+      )
+    )
+  )
+
+(defun planner-ledger-goto-section-end(regexp-start)
+  "Goto the end of the current section or end of buffer.
+Assumes that sections are marked with an asterisk."
+  (if (re-search-forward regexp-start nil t)
+      (line-beginning-position)
+    (point-max))
+  )
+

 (defun planner-ledger-add-entry-from-task ()
   "Add a new ledger entry from the task at point."
@@ -122,10 +190,10 @@
         (let* ((payee  (match-string 1))
                (amount (match-string 2))
                (date   (planner-filename-to-calendar-date (buffer-name)))
-               (buffer (find-buffer-visiting ledger-data-file)))
-          (unless buffer (setq buffer (find-file ledger-data-file)))
+               (buffer (find-buffer-visiting planner-ledger-data-file)))
+          (unless buffer (setq buffer (find-file
planner-ledger-data-file)))
           (pop-to-buffer buffer)
-          (ledger-add-entry (format "%d/%d/%d %s %s"
+          (ledger-add-entry (format "%d/%02d/%02d %s %s"
                                     (extract-calendar-year date)
                                     (extract-calendar-month date)
                                     (extract-calendar-day date)
@@ -133,6 +201,20 @@
                                     amount)))
       (message "Not in a ledger payment task"))))

+(defun planner-ledger-run-ledger (&rest ledger-args)
+   "Run ledger for planner-ledger.
+Run the ledger binary with ledger-run-ledger using the
planner-ledger-data-file.
+If the file is open in a buffer, use the buffer. Otherwisem specify the
file as an
+option to the ledger binary command and avoid loading it in emacs."
+   (let ((buffer (get-file-buffer planner-ledger-data-file)))
+     (if buffer
+     (apply 'ledger-run-ledger buffer ledger-args)
+     (apply #'call-process
+        (append (list ledger-binary-path nil t nil
+              "-f" planner-ledger-data-file)
+            ledger-args)))
+     ))
+
 (provide 'planner-ledger)

 ;;; planner-ledger.el ends here


And here is the patch for ledger.el:

--- ledger.el.orig    2007-08-23 13:30:07.000000000 +0300
+++ ledger.el    2007-08-23 09:53:52.000000000 +0300
@@ -36,6 +36,7 @@
 ;; type M-x ledger-mode.  Once this is done, you can type:
 ;;
 ;;   C-c C-a       add a new entry, based on previous entries
+;;   C-c C-e       toggle cleared status of an entry
 ;;   C-c C-y       set default year for entry mode
 ;;   C-c C-m       set default month for entry mode
 ;;   C-c C-r       reconcile uncleared entries related to an account
@@ -395,6 +396,7 @@
     (define-key map [(control ?c) (control ?y)] 'ledger-set-year)
     (define-key map [(control ?c) (control ?m)] 'ledger-set-month)
     (define-key map [(control ?c) (control ?c)] 'ledger-toggle-current)
+    (define-key map [(control ?c) (control ?e)]
'ledger-toggle-current-entry)
     (define-key map [(control ?c) (control ?r)] 'ledger-reconcile)
     (define-key map [(control ?c) (control ?s)] 'ledger-sort)
     (define-key map [tab] 'pcomplete)
@@ -402,16 +404,11 @@
     (define-key map [(control ?c) tab] 'ledger-fully-complete-entry)
     (define-key map [(control ?c) (control ?i)]
'ledger-fully-complete-entry)
     (define-key map [(control ?c) (control ?o) (control ?r)]
'ledger-report)
-    (define-key map [(control ?c) (control ?o) (control ?g)]
-      'ledger-report-goto)
-    (define-key map [(control ?c) (control ?o) (control ?a)]
-      'ledger-report-redo)
-    (define-key map [(control ?c) (control ?o) (control ?s)]
-      'ledger-report-save)
-    (define-key map [(control ?c) (control ?o) (control ?e)]
-      'ledger-report-edit)
-    (define-key map [(control ?c) (control ?o) (control ?k)]
-      'ledger-report-kill)))
+    (define-key map [(control ?c) (control ?o) (control ?g)]
'ledger-report-goto)
+    (define-key map [(control ?c) (control ?o) (control ?a)]
'ledger-report-redo)
+    (define-key map [(control ?c) (control ?o) (control ?s)]
'ledger-report-save)
+    (define-key map [(control ?c) (control ?o) (control ?e)]
'ledger-report-edit)
+    (define-key map [(control ?c) (control ?o) (control ?k)]
'ledger-report-kill)))

 ;; Reconcile mode

@@ -1201,7 +1198,9 @@
 (defvar ledger-delete-after nil)

 (defun ledger-run-ledger (buffer &rest args)
-  "run ledger with supplied arguments"
+  "Run Ledger with supplied arguments and place results in
+the current. The argument buffer is the lerger data buffer.
+Results are displayed in the current buffer before point."
   (cond
    ((null ledger-binary-path)
     (error "The variable `ledger-binary-path' has not been set"))
_______________________________________________
Planner-el-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/planner-el-discuss

Reply via email to