ledger-post.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; ledger-post.el --- Helper code for use with the "ledger" command-line tool
  2. ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)
  3. ;; This file is not part of GNU Emacs.
  4. ;; This is free software; you can redistribute it and/or modify it under
  5. ;; the terms of the GNU General Public License as published by the Free
  6. ;; Software Foundation; either version 2, or (at your option) any later
  7. ;; version.
  8. ;;
  9. ;; This is distributed in the hope that it will be useful, but WITHOUT
  10. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. ;; for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. ;; MA 02110-1301 USA.
  18. ;;; Commentary:
  19. ;; Utility functions for dealing with postings.
  20. (require 'ledger-regex)
  21. ;;; Code:
  22. (defgroup ledger-post nil
  23. "Options for controlling how Ledger-mode deals with postings and completion"
  24. :group 'ledger)
  25. (defcustom ledger-post-account-alignment-column 4
  26. "The column Ledger-mode attempts to align accounts to."
  27. :type 'integer
  28. :group 'ledger-post)
  29. (defcustom ledger-post-amount-alignment-column 52
  30. "The column Ledger-mode attempts to align amounts to."
  31. :type 'integer
  32. :group 'ledger-post)
  33. (defcustom ledger-post-amount-alignment-at :end
  34. "Position at which the amount is ailgned.
  35. Can be :end to align on the last number of the amount (can be
  36. followed by unaligned commodity) or :decimal to align at the
  37. decimal separator."
  38. :type '(radio (const :tag "align at the end of amount" :end)
  39. (const :tag "align at the decimal separator" :decimal))
  40. :group 'ledger-post)
  41. (defcustom ledger-post-use-completion-engine :built-in
  42. "Which completion engine to use, :iswitchb or :ido chose those engines.
  43. :built-in uses built-in Ledger-mode completion"
  44. :type '(radio (const :tag "built in completion" :built-in)
  45. (const :tag "ido completion" :ido)
  46. (const :tag "iswitchb completion" :iswitchb) )
  47. :group 'ledger-post)
  48. (declare-function iswitchb-read-buffer "iswitchb"
  49. (prompt &optional default require-match start matches-set))
  50. (defvar iswitchb-temp-buflist)
  51. (defun ledger-post-completing-read (prompt choices)
  52. "Use iswitchb as a `completing-read' replacement to choose from choices.
  53. PROMPT is a string to prompt with. CHOICES is a list of strings
  54. to choose from."
  55. (cond ((eq ledger-post-use-completion-engine :iswitchb)
  56. (let* ((iswitchb-use-virtual-buffers nil)
  57. (iswitchb-make-buflist-hook
  58. (lambda ()
  59. (setq iswitchb-temp-buflist choices))))
  60. (iswitchb-read-buffer prompt)))
  61. ((eq ledger-post-use-completion-engine :ido)
  62. (ido-completing-read prompt choices))
  63. (t
  64. (completing-read prompt choices))))
  65. (defun ledger-next-amount (&optional end)
  66. "Move point to the next amount, as long as it is not past END.
  67. Return the width of the amount field as an integer and leave
  68. point at beginning of the commodity."
  69. ;;(beginning-of-line)
  70. (let ((case-fold-search nil))
  71. (when (re-search-forward ledger-amount-regex end t)
  72. (goto-char (match-beginning 0))
  73. (skip-syntax-forward " ")
  74. (cond
  75. ((eq ledger-post-amount-alignment-at :end)
  76. (- (or (match-end 4) (match-end 3)) (point)))
  77. ((eq ledger-post-amount-alignment-at :decimal)
  78. (- (match-end 3) (point)))))))
  79. (defun ledger-next-account (&optional end)
  80. "Move to the beginning of the posting, or status marker, limit to END.
  81. Return the column of the beginning of the account and leave point
  82. at beginning of account"
  83. (if (> end (point))
  84. (when (re-search-forward ledger-account-any-status-regex (1+ end) t)
  85. ;; the 1+ is to make sure we can catch the newline
  86. (if (match-beginning 1)
  87. (goto-char (match-beginning 1))
  88. (goto-char (match-beginning 2)))
  89. (current-column))))
  90. (defun ledger-post-align-xact (pos)
  91. "Align all the posting in the xact at POS."
  92. (interactive "d")
  93. (let ((bounds (ledger-navigate-find-xact-extents pos)))
  94. (ledger-post-align-postings (car bounds) (cadr bounds))))
  95. (defun ledger-post-align-postings (beg end)
  96. "Align all accounts and amounts between BEG and END, or the current region, or, if no region, the current line."
  97. (interactive "r")
  98. (save-excursion
  99. (let ((inhibit-modification-hooks t)
  100. acct-start-column acct-end-column acct-adjust amt-width amt-adjust
  101. (lines-left 1))
  102. ;; Extend region to whole lines
  103. (let ((start-marker (set-marker (make-marker) (save-excursion
  104. (goto-char beg)
  105. (line-beginning-position))))
  106. (end-marker (set-marker (make-marker) (save-excursion
  107. (goto-char end)
  108. (line-end-position)))))
  109. (untabify start-marker end-marker)
  110. (goto-char start-marker)
  111. ;; This is the guts of the alignment loop
  112. (while (and (or (setq acct-start-column (ledger-next-account (line-end-position)))
  113. lines-left)
  114. (< (point) end-marker))
  115. (when acct-start-column
  116. (setq acct-end-column (save-excursion
  117. (goto-char (match-end 2))
  118. (current-column)))
  119. (when (/= (setq acct-adjust (- ledger-post-account-alignment-column acct-start-column)) 0)
  120. (setq acct-end-column (+ acct-end-column acct-adjust)) ;;adjust the account ending column
  121. (if (> acct-adjust 0)
  122. (insert (make-string acct-adjust ? ))
  123. (delete-char acct-adjust)))
  124. (when (setq amt-width (ledger-next-amount (line-end-position)))
  125. (if (/= 0 (setq amt-adjust (- (if (> (- ledger-post-amount-alignment-column amt-width)
  126. (+ 2 acct-end-column))
  127. ledger-post-amount-alignment-column ;;we have room
  128. (+ acct-end-column 2 amt-width))
  129. amt-width
  130. (current-column))))
  131. (if (> amt-adjust 0)
  132. (insert (make-string amt-adjust ? ))
  133. (delete-char amt-adjust)))))
  134. (forward-line)
  135. (setq lines-left (not (eobp)))))
  136. (setq inhibit-modification-hooks nil))))
  137. (defun ledger-post-align-dwim ()
  138. "Align all the posting of the current xact or the current region.
  139. If the point is in a comment, fill the comment paragraph as
  140. regular text."
  141. (interactive)
  142. (cond
  143. ((nth 4 (syntax-ppss))
  144. (call-interactively 'ledger-post-align-postings)
  145. (fill-paragraph))
  146. ((use-region-p) (call-interactively 'ledger-post-align-postings))
  147. (t (call-interactively 'ledger-post-align-xact))))
  148. (defun ledger-post-edit-amount ()
  149. "Call 'calc-mode' and push the amount in the posting to the top of stack."
  150. (interactive)
  151. (goto-char (line-beginning-position))
  152. (when (re-search-forward ledger-post-line-regexp (line-end-position) t)
  153. (goto-char (match-end ledger-regex-post-line-group-account)) ;; go to the and of the account
  154. (let ((end-of-amount (re-search-forward "[-.,0-9]+" (line-end-position) t)))
  155. ;; determine if there is an amount to edit
  156. (if end-of-amount
  157. (let ((val-string (match-string 0)))
  158. (goto-char (match-beginning 0))
  159. (delete-region (match-beginning 0) (match-end 0))
  160. (calc)
  161. (calc-eval val-string 'push)) ;; edit the amount
  162. (progn ;;make sure there are two spaces after the account name and go to calc
  163. (if (search-backward " " (- (point) 3) t)
  164. (goto-char (line-end-position))
  165. (insert " "))
  166. (calc))))))
  167. (provide 'ledger-post)
  168. ;;; ledger-post.el ends here