ledger-context.el (9976B)
1 ;;; ledger-context.el --- Helper code for use with the "ledger" command-line tool -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org) 4 5 ;; This file is not part of GNU Emacs. 6 7 ;; This is free software; you can redistribute it and/or modify it under 8 ;; the terms of the GNU General Public License as published by the Free 9 ;; Software Foundation; either version 2, or (at your option) any later 10 ;; version. 11 ;; 12 ;; This is distributed in the hope that it will be useful, but WITHOUT 13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 ;; for more details. 16 ;; 17 ;; You should have received a copy of the GNU General Public License 18 ;; along with GNU Emacs; see the file COPYING. If not, write to the 19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 20 ;; MA 02110-1301 USA. 21 22 23 ;;; Commentary: 24 ;; Provide facilities for reflection in ledger buffers 25 26 ;;; Code: 27 28 (require 'ledger-regex) 29 30 ;; ledger-*-string constants are assembled in the 31 ;; `ledger-single-line-config' macro to form the regex and list of 32 ;; elements 33 (defconst ledger-indent-string "\\(^[ \t]+\\)") 34 (defconst ledger-status-string "\\(*\\|!\\)?") 35 (defconst ledger-account-string "[\\[(]?\\(.*?\\)[])]?") 36 (defconst ledger-separator-string "\\(\\s-\\s-+\\)") 37 (defconst ledger-amount-string ledger-amount-regexp) 38 (defconst ledger-commoditized-amount-string ledger-commoditized-amount-regexp) 39 (defconst ledger-cost-string ledger-cost-regexp) 40 (defconst ledger-balance-assertion-string ledger-balance-assertion-regexp) 41 (defconst ledger-comment-string "\\(?:[ \t]*\n\\)?[ \t]*;[ \t]*\\(.*?\\)") 42 (defconst ledger-nil-string "\\([ \t]+\\)") 43 (defconst ledger-date-string "^\\([0-9]\\{4\\}[/-][01]?[0-9][/-][0123]?[0-9]\\)\\(?:=[0-9]\\{4\\}[/-][01]?[0-9][/-][0123]?[0-9]\\)?") 44 (defconst ledger-code-string "\\((.*)\\)?") 45 (defconst ledger-payee-string "\\(.*[^[:space:]\n]\\)") 46 47 48 (defun ledger-get-regex-str (name) 49 "Get the ledger regex of type NAME." 50 (symbol-value (intern (concat "ledger-" (symbol-name name) "-string")))) 51 52 (defun ledger-line-regex (elements) 53 "Get a regex to match ELEMENTS on a single line." 54 (concat (apply 'concat (mapcar 'ledger-get-regex-str elements)) "[ \t]*$")) 55 56 (defmacro ledger-single-line-config (&rest elements) 57 "Take ELEMENTS and return regex and element list for use in context-at-point." 58 `(list (ledger-line-regex (quote ,elements)) (quote ,elements))) 59 60 (defconst ledger-line-config 61 (list (list 'xact (list (ledger-single-line-config date nil status nil code nil payee comment) 62 (ledger-single-line-config date nil status nil code nil payee) 63 (ledger-single-line-config date nil status nil payee comment) 64 (ledger-single-line-config date nil status nil payee) 65 (ledger-single-line-config date nil code nil payee comment) 66 (ledger-single-line-config date nil code nil payee) 67 (ledger-single-line-config date nil payee comment) 68 (ledger-single-line-config date nil payee))) 69 (list 'acct-transaction (list (ledger-single-line-config indent comment) 70 (ledger-single-line-config indent status nil account separator commoditized-amount nil cost nil balance-assertion) 71 (ledger-single-line-config indent status nil account separator commoditized-amount nil balance-assertion) 72 (ledger-single-line-config indent status nil account separator commoditized-amount nil cost comment) 73 (ledger-single-line-config indent status nil account separator commoditized-amount nil cost) 74 (ledger-single-line-config indent status nil account separator commoditized-amount comment) 75 (ledger-single-line-config indent status nil account separator commoditized-amount) 76 (ledger-single-line-config indent status nil account separator amount) 77 (ledger-single-line-config indent status nil account comment) 78 (ledger-single-line-config indent status nil account) 79 (ledger-single-line-config indent account separator commoditized-amount comment) 80 (ledger-single-line-config indent account separator commoditized-amount) 81 (ledger-single-line-config indent account separator amount) 82 (ledger-single-line-config indent account comment) 83 (ledger-single-line-config indent account))))) 84 85 (defun ledger-extract-context-info (line-type pos) 86 "Get context info for current line with LINE-TYPE. 87 88 Assumes point is at beginning of line, and the POS argument specifies 89 where the \"users\" point was." 90 (let ((linfo (assoc line-type ledger-line-config)) 91 found field fields) 92 (dolist (re-info (nth 1 linfo)) 93 (let ((re (nth 0 re-info)) 94 (names (nth 1 re-info))) 95 (unless found 96 (when (looking-at re) 97 (setq found t) 98 (dotimes (i (length names)) 99 (when (nth i names) 100 (setq fields (append fields 101 (list 102 (list (nth i names) 103 (match-string-no-properties (1+ i)) 104 (match-beginning (1+ i)))))))) 105 (dolist (f fields) 106 (and (nth 1 f) 107 (>= pos (nth 2 f)) 108 (setq field (nth 0 f)))))))) 109 (list line-type field fields))) 110 111 (defun ledger-thing-at-point () 112 "Describe thing at point. Return \\='transaction, \\='posting, \\='day, or nil. 113 114 Leave point at the beginning of the thing at point, otherwise do not move point." 115 (let ((here (point))) 116 (goto-char (line-beginning-position)) 117 (cond ((looking-at "^\\(?:[~=][ \t]\\|[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.+?)\\)?\\s-+\\)") 118 (goto-char (match-end 0)) 119 'transaction) 120 ((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\([^\\s-]\\)") 121 (goto-char (match-beginning 2)) 122 'posting) 123 ((looking-at "^\\(sun\\|mon\\|tue\\|wed\\|thu\\|fri\\|sat\\)\\s-+") 124 (goto-char (match-end 0)) 125 'day) 126 (t 127 (ignore (goto-char here)))))) 128 129 (defun ledger-context-at-point () 130 "Return a list describing the context around point. 131 132 The contents of the list are the line type, the name of the field 133 containing point, and for selected line types, the content of 134 the fields in the line in a association list." 135 (let ((pos (point))) 136 (save-excursion 137 (beginning-of-line) 138 (let ((first-char (char-after))) 139 (cond ((equal (point) (line-end-position)) 140 '(empty-line nil nil)) 141 ((memq first-char '(?\ ?\t)) 142 (ledger-extract-context-info 'acct-transaction pos)) 143 ((memq first-char '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)) 144 (ledger-extract-context-info 'xact pos)) 145 ((equal first-char ?\=) 146 '(automated-xact nil nil)) 147 ((equal first-char ?\~) 148 '(period-xact nil nil)) 149 ((equal first-char ?\!) 150 '(command-directive)) 151 ((equal first-char ?\;) 152 '(comment nil nil)) 153 ((equal first-char ?Y) 154 '(default-year nil nil)) 155 ((equal first-char ?P) 156 '(commodity-price nil nil)) 157 ((equal first-char ?N) 158 '(price-ignored-commodity nil nil)) 159 ((equal first-char ?D) 160 '(default-commodity nil nil)) 161 ((equal first-char ?C) 162 '(commodity-conversion nil nil)) 163 ((equal first-char ?i) 164 '(timeclock-i nil nil)) 165 ((equal first-char ?o) 166 '(timeclock-o nil nil)) 167 ((equal first-char ?b) 168 '(timeclock-b nil nil)) 169 ((equal first-char ?h) 170 '(timeclock-h nil nil)) 171 (t 172 '(unknown nil nil))))))) 173 174 (defun ledger-context-other-line (offset) 175 "Return a list describing context of line OFFSET from existing position. 176 177 Offset can be positive or negative. If run out of buffer before reaching 178 specified line, returns nil." 179 (save-excursion 180 (let ((left (forward-line offset))) 181 (if (not (equal left 0)) 182 nil 183 (ledger-context-at-point))))) 184 185 (defun ledger-context-line-type (context-info) 186 (nth 0 context-info)) 187 188 (defun ledger-context-current-field (context-info) 189 (nth 1 context-info)) 190 191 (defun ledger-context-field-info (context-info field-name) 192 (assoc field-name (nth 2 context-info))) 193 194 (defun ledger-context-field-present-p (context-info field-name) 195 (not (null (ledger-context-field-info context-info field-name)))) 196 197 (defun ledger-context-field-value (context-info field-name) 198 (nth 1 (ledger-context-field-info context-info field-name))) 199 200 (defun ledger-context-field-position (context-info field-name) 201 (nth 2 (ledger-context-field-info context-info field-name))) 202 203 (defun ledger-context-field-end-position (context-info field-name) 204 (+ (ledger-context-field-position context-info field-name) 205 (length (ledger-context-field-value context-info field-name)))) 206 207 (defun ledger-context-goto-field-start (context-info field-name) 208 (goto-char (ledger-context-field-position context-info field-name))) 209 210 (defun ledger-context-goto-field-end (context-info field-name) 211 (goto-char (ledger-context-field-end-position context-info field-name))) 212 213 (provide 'ledger-context) 214 215 ;;; ledger-context.el ends here