summaryrefslogtreecommitdiff
path: root/lisp/ledger-context.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/ledger-context.el')
-rw-r--r--lisp/ledger-context.el97
1 files changed, 43 insertions, 54 deletions
diff --git a/lisp/ledger-context.el b/lisp/ledger-context.el
index b1bcd870..0dfa4645 100644
--- a/lisp/ledger-context.el
+++ b/lisp/ledger-context.el
@@ -1,6 +1,6 @@
;;; ledger-context.el --- Helper code for use with the "ledger" command-line tool
-;; Copyright (C) 2003-2013 John Wiegley (johnw AT gnu DOT org)
+;; Copyright (C) 2003-2015 John Wiegley (johnw AT gnu DOT org)
;; This file is not part of GNU Emacs.
@@ -16,8 +16,8 @@
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-;; MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+;; MA 02110-1301 USA.
;;; Commentary:
@@ -28,56 +28,45 @@
(eval-when-compile
(require 'cl))
-;; *-string constants are assembled in the single-line-config macro to
-;; form the regex and list of elements
-(defconst indent-string "\\(^[ \t]+\\)")
-(defconst status-string "\\([*! ]?\\)")
-(defconst account-string "[\\[(]?\\(.*?\\)[])]?")
-(defconst amount-string "[ \t]?\\(-?[0-9]+\\.[0-9]*\\)")
-(defconst comment-string "[ \t]*;[ \t]*\\(.*?\\)")
-(defconst nil-string "\\([ \t]+\\)")
-(defconst commodity-string "\\(.+?\\)")
-(defconst date-string "^\\([0-9]\\{4\\}[/-][01]?[0-9][/-][0123]?[0-9]\\)")
-(defconst code-string "\\((.*)\\)?")
-(defconst payee-string "\\(.*\\)")
-
-(defmacro line-regex (&rest elements)
- (let (regex-string)
- (concat (dolist (e elements regex-string)
- (setq regex-string
- (concat regex-string
- (eval
- (intern
- (concat (symbol-name e) "-string")))))) "[ \t]*$")))
-
-(defmacro single-line-config2 (&rest elements)
-"Take list of ELEMENTS and return regex and element list for use in context-at-point"
- (let (regex-string)
- `'(,(concat (dolist (e elements regex-string)
- (setq regex-string
- (concat regex-string
- (eval
- (intern
- (concat (symbol-name e) "-string")))))) "[ \t]*$")
- ,elements)))
-
-(defmacro single-line-config (&rest elements)
+;; ledger-*-string constants are assembled in the
+;; `ledger-single-line-config' macro to form the regex and list of
+;; elements
+(defconst ledger-indent-string "\\(^[ \t]+\\)")
+(defconst ledger-status-string "\\([*! ]?\\)")
+(defconst ledger-account-string "[\\[(]?\\(.*?\\)[])]?")
+(defconst ledger-separator-string "\\s-\\s-")
+(defconst ledger-amount-string "\\(-?[0-9]+[\\.,][0-9]*\\)")
+(defconst ledger-comment-string "[ \t]*;[ \t]*\\(.*?\\)")
+(defconst ledger-nil-string "\\([ \t]\\)")
+(defconst ledger-commodity-string "\\(.+?\\)")
+(defconst ledger-date-string "^\\([0-9]\\{4\\}[/-][01]?[0-9][/-][0123]?[0-9]\\)")
+(defconst ledger-code-string "\\((.*)\\)?")
+(defconst ledger-payee-string "\\(.*\\)")
+
+(defun ledger-get-regex-str (name)
+ "Get the ledger regex of type NAME."
+ (symbol-value (intern (concat "ledger-" (symbol-name name) "-string"))))
+
+(defun ledger-line-regex (elements)
+ "Get a regex to match ELEMENTS on a single line."
+ (concat (apply 'concat (mapcar 'ledger-get-regex-str elements)) "[ \t]*$"))
+
+(defmacro ledger-single-line-config (&rest elements)
"Take list of ELEMENTS and return regex and element list for use in context-at-point"
- `'(,(eval `(line-regex ,@elements))
- ,elements))
+ `(list (ledger-line-regex (quote ,elements)) (quote ,elements)))
(defconst ledger-line-config
- (list (list 'xact (list (single-line-config date nil status nil code nil payee nil comment)
- (single-line-config date nil status nil code nil payee)
- (single-line-config date nil status nil payee)))
- (list 'acct-transaction (list (single-line-config indent comment)
- (single-line-config2 indent status account nil commodity amount nil comment)
- (single-line-config2 indent status account nil commodity amount)
- (single-line-config2 indent status account nil amount nil commodity comment)
- (single-line-config2 indent status account nil amount nil commodity)
- (single-line-config2 indent status account nil amount)
- (single-line-config2 indent status account nil comment)
- (single-line-config2 indent status account)))))
+ (list (list 'xact (list (ledger-single-line-config date nil status nil code nil payee nil comment)
+ (ledger-single-line-config date nil status nil code nil payee)
+ (ledger-single-line-config date nil status nil payee)))
+ (list 'acct-transaction (list (ledger-single-line-config indent comment)
+ (ledger-single-line-config indent status account separator commodity amount nil comment)
+ (ledger-single-line-config indent status account separator commodity amount)
+ (ledger-single-line-config indent status account separator amount nil commodity comment)
+ (ledger-single-line-config indent status account separator amount nil commodity)
+ (ledger-single-line-config indent status account separator amount)
+ (ledger-single-line-config indent status account separator comment)
+ (ledger-single-line-config indent status account)))))
(defun ledger-extract-context-info (line-type pos)
"Get context info for current line with LINE-TYPE.
@@ -111,9 +100,9 @@ Leave point at the beginning of the thing under point"
(let ((here (point)))
(goto-char (line-beginning-position))
(cond ((looking-at "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.+?)\\)?\\s-+")
- (goto-char (match-end 0))
+ (goto-char (match-end 0))
'transaction)
- ((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\(.\\)")
+ ((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\([^\\s-]\\)")
(goto-char (match-beginning 2))
'posting)
((looking-at "^\\(sun\\|mon\\|tue\\|wed\\|thu\\|fri\\|sat\\)\\s-+")
@@ -176,7 +165,7 @@ specified line, returns nil."
(let ((left (forward-line offset)))
(if (not (equal left 0))
nil
- (ledger-context-at-point)))))
+ (ledger-context-at-point)))))
(defun ledger-context-line-type (context-info)
(nth 0 context-info))
@@ -208,4 +197,4 @@ specified line, returns nil."
(provide 'ledger-context)
-;;; ledger-report.el ends here
+;;; ledger-context.el ends here