summaryrefslogtreecommitdiff
path: root/lisp/ldg-complete.el
blob: 33a734b366c1d768200913b834b4f917c3013463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
;;(require 'esh-util)
;;(require 'esh-arg)
(require 'pcomplete)

;; In-place completion support

(defun ledger-thing-at-point ()
  (let ((here (point)))
    (goto-char (line-beginning-position))
    (cond ((looking-at "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.+?)\\)?\\s-+")
	   (goto-char (match-end 0))
	   'entry)
	  ((looking-at "^\\s-+\\([*!]\\s-+\\)?[[(]?\\(.\\)")
	   (goto-char (match-beginning 2))
	   'transaction)
	  ((looking-at "^\\(sun\\|mon\\|tue\\|wed\\|thu\\|fri\\|sat\\)\\s-+")
	   (goto-char (match-end 0))
	   'entry)
	  (t
	   (ignore (goto-char here))))))

(defun ledger-parse-arguments ()
  "Parse whitespace separated arguments in the current region."
  (let* ((info (save-excursion
		 (cons (ledger-thing-at-point) (point))))
	 (begin (cdr info))
	 (end (point))
	 begins args)
    (save-excursion
      (goto-char begin)
      (when (< (point) end)
	(skip-chars-forward " \t\n")
	(setq begins (cons (point) begins))
	(setq args (cons (buffer-substring-no-properties
			  (car begins) end)
			 args)))
      (cons (reverse args) (reverse begins)))))

(defun ledger-entries ()
  (let ((origin (point))
	entries-list)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward
	      (concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+"
		      "\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)") nil t)
	(unless (and (>= origin (match-beginning 0))
		     (< origin (match-end 0)))
	  (setq entries-list (cons (match-string-no-properties 3)
				   entries-list)))))
    (pcomplete-uniqify-list (nreverse entries-list))))

(defvar ledger-account-tree nil)

(defun ledger-find-accounts ()
  (let ((origin (point)) account-path elements)
    (save-excursion
      (setq ledger-account-tree (list t))
      (goto-char (point-min))
      (while (re-search-forward
	      "^[ \t]+\\([*!]\\s-+\\)?[[(]?\\(.+?\\)\\(\t\\|\n\\| [ \t]\\)" nil t)
	(unless (and (>= origin (match-beginning 0))
		     (< origin (match-end 0)))
	  (setq account-path (match-string-no-properties 2))
	  (setq elements (split-string account-path ":"))
	  (let ((root ledger-account-tree))
	    (while elements
	      (let ((entry (assoc (car elements) root)))
		(if entry
		    (setq root (cdr entry))
		  (setq entry (cons (car elements) (list t)))
		  (nconc root (list entry))
		  (setq root (cdr entry))))
	      (setq elements (cdr elements)))))))))

(defun ledger-accounts ()
  (ledger-find-accounts)
  (let* ((current (caar (ledger-parse-arguments)))
	 (elements (and current (split-string current ":")))
	 (root ledger-account-tree)
	 (prefix nil))
    (while (cdr elements)
      (let ((entry (assoc (car elements) root)))
	(if entry
	    (setq prefix (concat prefix (and prefix ":")
				 (car elements))
		  root (cdr entry))
	  (setq root nil elements nil)))
      (setq elements (cdr elements)))
    (and root
	 (sort
	  (mapcar (function
		   (lambda (x)
		     (let ((term (if prefix
				     (concat prefix ":" (car x))
				   (car x))))
		       (if (> (length (cdr x)) 1)
			   (concat term ":")
			 term))))
		  (cdr root))
	  'string-lessp))))

(defun ledger-complete-at-point ()
  "Do appropriate completion for the thing at point"
  (interactive)
  (while (pcomplete-here
	  (if (eq (save-excursion
		    (ledger-thing-at-point)) 'entry)
	      (if (null current-prefix-arg)
		  (ledger-entries)  ; this completes against entry names
		(progn
		  (let ((text (buffer-substring (line-beginning-position)
						(line-end-position))))
		    (delete-region (line-beginning-position)
				   (line-end-position))
		    (condition-case err
			(ledger-add-entry text t)
		      ((error)
		       (insert text))))
		  (forward-line)
		  (goto-char (line-end-position))
		  (search-backward ";" (line-beginning-position) t)
		  (skip-chars-backward " \t0123456789.,")
		  (throw 'pcompleted t)))
	    (ledger-accounts)))))

(defun ledger-fully-complete-entry ()
  "Do appropriate completion for the thing at point"
  (interactive)
  (let ((name (caar (ledger-parse-arguments)))
	xacts)
    (save-excursion
      (when (eq 'entry (ledger-thing-at-point))
	(when (re-search-backward
	       (concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+"
		       (regexp-quote name) "\\(\t\\|\n\\| [ \t]\\)") nil t)
	  (forward-line)
	  (while (looking-at "^\\s-+")
	    (setq xacts (cons (buffer-substring-no-properties
			       (line-beginning-position)
			       (line-end-position))
			      xacts))
	    (forward-line))
	  (setq xacts (nreverse xacts)))))
    (when xacts
      (save-excursion
	(insert ?\n)
	(while xacts
	  (insert (car xacts) ?\n)
	  (setq xacts (cdr xacts))))
      (forward-line)
      (goto-char (line-end-position))
      (if (re-search-backward "\\(\t\\| [ \t]\\)" nil t)
	  (goto-char (match-end 0))))))

(provide 'ldg-complete)