ledger-navigate.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ;;; ledger-navigate.el --- Provide navigation services through the ledger buffer.
  2. ;; Copyright (C) 2014-2015 Craig Earls (enderw88 AT gmail DOT com)
  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. ;;
  20. ;;; Code:
  21. (require 'ledger-regex)
  22. (require 'ledger-context)
  23. (defun ledger-navigate-next-xact ()
  24. "Move point to beginning of next xact."
  25. ;; make sure we actually move to the next xact, even if we are the
  26. ;; beginning of one now.
  27. (if (looking-at ledger-payee-any-status-regex)
  28. (forward-line))
  29. (if (re-search-forward ledger-payee-any-status-regex nil t)
  30. (goto-char (match-beginning 0))
  31. (goto-char (point-max))))
  32. (defun ledger-navigate-start-xact-or-directive-p ()
  33. "Return t if at the beginning of an empty or all-whitespace line."
  34. (not (looking-at "[ \t]\\|\\(^$\\)")))
  35. (defun ledger-navigate-next-xact-or-directive ()
  36. "Move to the beginning of the next xact or directive."
  37. (interactive)
  38. (beginning-of-line)
  39. (if (ledger-navigate-start-xact-or-directive-p) ; if we are the start of an xact, move forward to the next xact
  40. (progn
  41. (forward-line)
  42. (if (not (ledger-navigate-start-xact-or-directive-p)) ; we have moved forward and are not at another xact, recurse forward
  43. (ledger-navigate-next-xact-or-directive)))
  44. (while (not (or (eobp) ; we didn't start off at the beginning of an xact
  45. (ledger-navigate-start-xact-or-directive-p)))
  46. (forward-line))))
  47. (defun ledger-navigate-prev-xact-or-directive ()
  48. "Move point to beginning of previous xact."
  49. (interactive)
  50. (let ((context (car (ledger-context-at-point))))
  51. (when (equal context 'acct-transaction)
  52. (ledger-navigate-beginning-of-xact))
  53. (beginning-of-line)
  54. (re-search-backward "^[[:graph:]]" nil t)))
  55. (defun ledger-navigate-beginning-of-xact ()
  56. "Move point to the beginning of the current xact."
  57. (interactive)
  58. ;; need to start at the beginning of a line incase we are in the first line of an xact already.
  59. (beginning-of-line)
  60. (let ((sreg (concat "^\\(=\\|~\\|" ledger-iso-date-regexp "\\)")))
  61. (unless (looking-at sreg)
  62. (re-search-backward sreg nil t)
  63. (beginning-of-line)))
  64. (point))
  65. (defun ledger-navigate-end-of-xact ()
  66. "Move point to end of xact."
  67. (interactive)
  68. (ledger-navigate-next-xact-or-directive)
  69. (re-search-backward ".$")
  70. (end-of-line)
  71. (point))
  72. (defun ledger-navigate-to-line (line-number)
  73. "Rapidly move point to line LINE-NUMBER."
  74. (goto-char (point-min))
  75. (forward-line (1- line-number)))
  76. (defun ledger-navigate-find-xact-extents (pos)
  77. "Return list containing point for beginning and end of xact containing POS.
  78. Requires empty line separating xacts."
  79. (interactive "d")
  80. (save-excursion
  81. (goto-char pos)
  82. (list (ledger-navigate-beginning-of-xact)
  83. (ledger-navigate-end-of-xact))))
  84. (defun ledger-navigate-find-directive-extents (pos)
  85. "Return the extents of the directive at POS."
  86. (goto-char pos)
  87. (let ((begin (progn (beginning-of-line)
  88. (point)))
  89. (end (progn (end-of-line)
  90. (+ 1 (point)))))
  91. ;; handle block comments here
  92. (beginning-of-line)
  93. (if (looking-at " *;")
  94. (progn
  95. (while (and (looking-at " *;")
  96. (> (point) (point-min)))
  97. (forward-line -1))
  98. ;; We are either at the beginning of the buffer, or we found
  99. ;; a line outside the comment. If we are not at the
  100. ;; beginning of the buffer then we need to move forward a
  101. ;; line.
  102. (if (> (point) (point-min))
  103. (progn (forward-line 1)
  104. (beginning-of-line)))
  105. (setq begin (point))
  106. (goto-char pos)
  107. (beginning-of-line)
  108. (while (and (looking-at " *;")
  109. (< (point) (point-max)))
  110. (forward-line 1))
  111. (setq end (point))))
  112. (list begin end)))
  113. (defun ledger-navigate-block-comment (pos)
  114. "Move past the block comment at POS, and return its extents."
  115. (interactive "d")
  116. (goto-char pos)
  117. (let ((begin (progn (beginning-of-line)
  118. (point)))
  119. (end (progn (end-of-line)
  120. (point))))
  121. ;; handle block comments here
  122. (beginning-of-line)
  123. (if (looking-at " *;")
  124. (progn
  125. (while (and (looking-at " *;")
  126. (> (point) (point-min)))
  127. (forward-line -1))
  128. (setq begin (point))
  129. (goto-char pos)
  130. (beginning-of-line)
  131. (while (and (looking-at " *;")
  132. (< (point) (point-max)))
  133. (forward-line 1))
  134. (setq end (point))))
  135. (list begin end)))
  136. (defun ledger-navigate-find-element-extents (pos)
  137. "Return list containing beginning and end of the entity surrounding POS."
  138. (interactive "d")
  139. (save-excursion
  140. (goto-char pos)
  141. (beginning-of-line)
  142. (if (looking-at "[ =~0-9\\[]")
  143. (ledger-navigate-find-xact-extents pos)
  144. (ledger-navigate-find-directive-extents pos))))
  145. (provide 'ledger-navigate)
  146. ;;; ledger-navigate.el ends here