From 6dbb36f525d1d5d35784e4f79b89d90ca1a3db93 Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Mon, 30 Dec 2013 00:22:43 +0100 Subject: ledger-mode: Add `ledger-remove-effective-date'. Signed-off-by: Moritz Ulrich --- lisp/ledger-mode.el | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lisp/ledger-mode.el') diff --git a/lisp/ledger-mode.el b/lisp/ledger-mode.el index 8ae04934..79b6f0ec 100644 --- a/lisp/ledger-mode.el +++ b/lisp/ledger-mode.el @@ -145,6 +145,29 @@ Can indent, complete or align depending on context." (defvar ledger-mode-abbrev-table) +(defun ledger-remove-effective-date () + "Removes the effective date from a transaction or posting." + (interactive) + (let ((context (car (ledger-context-at-point)))) + (save-excursion + (save-restriction + (narrow-to-region (point-at-bol) (point-at-eol)) + (beginning-of-line) + (cond ((eq 'xact context) + (re-search-forward ledger-iso-date-regexp) + (when (= (char-after) ?=) + (let ((eq-pos (point))) + (delete-region + eq-pos + (re-search-forward ledger-iso-date-regexp))))) + ((eq 'acct-transaction context) + ;; Match "; [=date]" & delete string + (when (re-search-forward + (concat ledger-comment-regex + "\\[=" ledger-iso-date-regexp "\\]") + nil 'noerr) + (replace-match "")))))))) + (defun ledger-insert-effective-date () (interactive) (let ((context (car (ledger-context-at-point))) -- cgit v1.2.3 From 7dd82f8bacf8dfb8db42a462452c114209a72c9c Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Mon, 30 Dec 2013 00:50:48 +0100 Subject: Improve `ledger-insert-effective-date'. The function now replaces already existing effective dates in the same line. With a prefix (C-u C-c C-t), remove the current effective date. Signed-off-by: Moritz Ulrich --- lisp/ledger-mode.el | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'lisp/ledger-mode.el') diff --git a/lisp/ledger-mode.el b/lisp/ledger-mode.el index 79b6f0ec..b1df4a6b 100644 --- a/lisp/ledger-mode.el +++ b/lisp/ledger-mode.el @@ -168,16 +168,33 @@ Can indent, complete or align depending on context." nil 'noerr) (replace-match "")))))))) -(defun ledger-insert-effective-date () - (interactive) - (let ((context (car (ledger-context-at-point))) - (date-string (format-time-string (cdr (assoc "date-format" ledger-environment-alist))))) - (cond ((eq 'xact context) - (beginning-of-line) - (insert date-string "=")) - ((eq 'acct-transaction context) - (end-of-line) - (insert " ; [=" date-string "]"))))) +(defun ledger-insert-effective-date (&optional arg) + "Insert an effective date to the transaction or posting. + +Replace the current effective date if there's one in the same +line. + +With a prefix argument, remove the effective date. " + (interactive "P") + (if (and (listp arg) + (= 4 (prefix-numeric-value arg))) + (ledger-remove-effective-date) + (let ((context (car (ledger-context-at-point))) + (date-string (format-time-string + (cdr (assoc "date-format" ledger-environment-alist))))) + (save-restriction + (narrow-to-region (point-at-bol) (point-at-eol)) + (cond + ((eq 'xact context) + (beginning-of-line) + (re-search-forward ledger-iso-date-regexp) + (when (= (char-after) ?=) + (ledger-remove-effective-date)) + (insert "=" date-string)) + ((eq 'acct-transaction context) + (end-of-line) + (ledger-remove-effective-date) + (insert " ; [=" date-string "]"))))))) (defun ledger-mode-remove-extra-lines () (goto-char (point-min)) -- cgit v1.2.3 From 9f694169a87d74f204967b68f3822685f5450bad Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Mon, 30 Dec 2013 01:41:31 +0100 Subject: ledger-mode: Introduce `ledger-default-date-format'. Some elisp functions depend on a date-format supplied in ~/.ledgerrc. Change these by introducing a default ("%Y/%m/%d"). Signed-off-by: Moritz Ulrich --- lisp/ledger-init.el | 2 ++ lisp/ledger-mode.el | 8 +++++--- lisp/ledger-reconcile.el | 10 +++++----- lisp/ledger-schedule.el | 5 ++++- 4 files changed, 16 insertions(+), 9 deletions(-) (limited to 'lisp/ledger-mode.el') diff --git a/lisp/ledger-init.el b/lisp/ledger-init.el index d7eeb3f6..f7b65e96 100644 --- a/lisp/ledger-init.el +++ b/lisp/ledger-init.el @@ -30,6 +30,8 @@ (defvar ledger-environment-alist nil) +(defvar ledger-default-date-format "%Y/%m/%d") + (defun ledger-init-parse-initialization (buffer) (with-current-buffer buffer (let (environment-alist) diff --git a/lisp/ledger-mode.el b/lisp/ledger-mode.el index b1df4a6b..28856bd4 100644 --- a/lisp/ledger-mode.el +++ b/lisp/ledger-mode.el @@ -179,9 +179,11 @@ With a prefix argument, remove the effective date. " (if (and (listp arg) (= 4 (prefix-numeric-value arg))) (ledger-remove-effective-date) - (let ((context (car (ledger-context-at-point))) - (date-string (format-time-string - (cdr (assoc "date-format" ledger-environment-alist))))) + (let* ((context (car (ledger-context-at-point))) + (date-format (or + (cdr (assoc "date-format" ledger-environment-alist)) + ledger-default-date-format)) + (date-string (format-time-string date-format))) (save-restriction (narrow-to-region (point-at-bol) (point-at-eol)) (cond diff --git a/lisp/ledger-reconcile.el b/lisp/ledger-reconcile.el index 2e8e1ef9..44e60532 100644 --- a/lisp/ledger-reconcile.el +++ b/lisp/ledger-reconcile.el @@ -28,6 +28,7 @@ ;;; Code: (require 'easymenu) +(require 'ledger-init) (defvar ledger-buf nil) (defvar ledger-bufs nil) @@ -64,7 +65,7 @@ reconcile-finish will mark all pending posting cleared." :type 'boolean :group 'ledger-reconcile) -(defcustom ledger-reconcile-default-date-format "%Y/%m/%d" +(defcustom ledger-reconcile-default-date-format ledger-default-date-format "Default date format for the reconcile buffer" :type 'string :group 'ledger-reconcile) @@ -306,15 +307,14 @@ POSTING is used in `ledger-clear-whole-transactions' is nil." (if (looking-at "(") (read (current-buffer)))))))) ;current-buffer is the *temp* created above (if (and ledger-success (> (length xacts) 0)) - (let ((date-format (cdr (assoc "date-format" ledger-environment-alist)))) + (let ((date-format (or (cdr (assoc "date-format" ledger-environment-alist)) + ledger-default-date-format))) (dolist (xact xacts) (dolist (posting (nthcdr 5 xact)) (let ((beg (point)) (where (ledger-marker-where-xact-is xact posting))) (insert (format "%s %-4s %-30s %-30s %15s\n" - (format-time-string (if date-format - date-format - ledger-reconcile-default-date-format) (nth 2 xact)) + (format-time-string date-format (nth 2 xact)) (if (nth 3 xact) (nth 3 xact) "") diff --git a/lisp/ledger-schedule.el b/lisp/ledger-schedule.el index ce77f26b..c9ee4392 100644 --- a/lisp/ledger-schedule.el +++ b/lisp/ledger-schedule.el @@ -30,6 +30,8 @@ ;; function slot of the symbol VARNAME. Then use VARNAME as the ;; function without have to use funcall. +(require 'ledger-init) + (defgroup ledger-schedule nil "Support for automatically recommendation transactions." :group 'ledger) @@ -288,7 +290,8 @@ returns true if the date meets the requirements" "Format CANDIDATE-ITEMS for display." (let ((candidates (ledger-schedule-list-upcoming-xacts candidate-items early horizon)) (schedule-buf (get-buffer-create ledger-schedule-buffer-name)) - (date-format (cdr (assoc "date-format" ledger-environment-alist)))) + (date-format (or (cdr (assoc "date-format" ledger-environment-alist)) + ledger-default-date-format))) (with-current-buffer schedule-buf (erase-buffer) (dolist (candidate candidates) -- cgit v1.2.3