@@ -176,34 +176,75 @@ the balance into that."
176176 nil 'noerr )
177177 (replace-match " " ))))))))
178178
179- (defun ledger-insert-effective-date (&optional date )
179+ (defun ledger-insert-effective-date (&optional start end date )
180180 " Insert effective date `DATE' to the transaction or posting.
181181
182- If `DATE' is nil, prompt the user a date.
182+ If `DATE' is nil, prompt the user for a date.
183183
184184Replace the current effective date if there's one in the same
185185line.
186186
187- With a prefix argument, remove the effective date."
188- (interactive )
189- (if (and (listp current-prefix-arg)
190- (= 4 (prefix-numeric-value current-prefix-arg)))
191- (ledger-remove-effective-date)
192- (let* ((context (car (ledger-context-at-point)))
193- (date-string (or date (ledger-read-date " Effective date: " ))))
194- (save-restriction
195- (narrow-to-region (line-beginning-position ) (line-end-position ))
196- (cond
197- ((eq 'xact context)
198- (beginning-of-line )
199- (re-search-forward ledger-iso-date-regexp)
200- (when (= (char-after ) ?= )
201- (ledger-remove-effective-date))
202- (insert " =" date-string))
203- ((eq 'acct-transaction context)
204- (end-of-line )
205- (ledger-remove-effective-date)
206- (insert " ; [=" date-string " ]" )))))))
187+ With a prefix argument, remove the effective date.
188+
189+ With an active region (`START' and `END' non-nil), insert or
190+ remove for all transactions starting within the region."
191+ (interactive
192+ (if (use-region-p )
193+ (list (region-beginning ) (region-end ))
194+ (list nil nil )))
195+
196+ (if (and start end) (ledger-insert-effective-date-region start end date)
197+ (if (and (listp current-prefix-arg)
198+ (= 4 (prefix-numeric-value current-prefix-arg)))
199+ (ledger-remove-effective-date)
200+ (let* ((context (car (ledger-context-at-point)))
201+ (date-string (or date (ledger-read-date " Effective date: " ))))
202+ (save-restriction
203+ (narrow-to-region (line-beginning-position ) (line-end-position ))
204+ (cond
205+ ((eq 'xact context)
206+ (beginning-of-line )
207+ (re-search-forward ledger-iso-date-regexp)
208+ (when (= (char-after ) ?= )
209+ (ledger-remove-effective-date))
210+ (insert " =" date-string))
211+ ((eq 'acct-transaction context)
212+ (end-of-line )
213+ (ledger-remove-effective-date)
214+ (insert " ; [=" date-string " ]" ))))))))
215+
216+ (defun ledger-insert-effective-date-region (start end &optional date )
217+ " Insert effective date `DATE' to all transactions starting within
218+ the region.
219+
220+ If `DATE' is nil, prompt the user for a date.
221+
222+ Replace the current effective date if there is one.
223+
224+ With a prefix argument, remove any effective dates."
225+ (interactive " r" )
226+ (let* ((should-remove
227+ (and (listp current-prefix-arg)
228+ (= 4 (prefix-numeric-value current-prefix-arg))))
229+ (date-string
230+ (unless should-remove
231+ (or date (ledger-read-date " Effective date: " )))))
232+ (save-excursion
233+ (setq end (copy-marker end))
234+ (goto-char start)
235+ (while (< (point ) end)
236+ (let ((context (car (ledger-context-at-point))))
237+ (save-restriction
238+ (narrow-to-region (line-beginning-position ) (line-end-position ))
239+ (when (eq 'xact context)
240+ (if should-remove
241+ (ledger-remove-effective-date)
242+ (beginning-of-line )
243+ (re-search-forward ledger-iso-date-regexp)
244+ (when (= (char-after ) ?= )
245+ (ledger-remove-effective-date))
246+ (insert " =" date-string)))))
247+ (forward-line 1 )))))
207248
208249(defun ledger-mode-remove-extra-lines ()
209250 " Get rid of multiple empty lines."
0 commit comments