This file is indexed.

/usr/share/emacs/site-lisp/elpa-src/ledger-3.1.2/ledger-complete.el is in elpa-ledger 3.1.2~pre1+g3a00e1c+dfsg1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;; ledger-complete.el --- Helper code for use with the "ledger" command-line tool

;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)

;; This file is not part of GNU Emacs.

;; This is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;; for more details.
;;
;; 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., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301 USA.

;;; Commentary:
;; Functions providing payee and account auto complete.

(require 'pcomplete)

;; In-place completion support

;;; Code:

(declare-function ledger-thing-at-point "ledger-context" nil)
(declare-function ledger-add-transaction "ledger-xact" (transaction-text &optional insert-at-point))
(declare-function between "ledger-schedule" (val low high))

(defun ledger-parse-arguments ()
  "Parse whitespace separated arguments in the current region."
  ;; this is more complex than it appears to need, so that it can work
  ;; with pcomplete.  See pcomplete-parse-arguments-function for
  ;; details
  (let* ((begin (save-excursion
                  (ledger-thing-at-point) ;; leave point at beginning of thing under point
                  (point)))
         (end (point))
         begins args)
    ;; to support end of line metadata
    (save-excursion
      (when (search-backward ";"
                             (line-beginning-position) t)
        (setq begin (match-beginning 0))))
    (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-payees-in-buffer ()
  "Scan buffer and return list of all payees."
  (let ((origin (point))
        payees-list)
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward
              ledger-payee-any-status-regex nil t)  ;; matches first line
        (unless (and (>= origin (match-beginning 0))
                     (< origin (match-end 0)))
          (setq payees-list (cons (match-string-no-properties 3)
                                  payees-list)))))  ;; add the payee
    ;; to the list
    (pcomplete-uniqify-list (nreverse payees-list))))


(defun ledger-find-accounts-in-buffer ()
  (interactive)
  (let ((origin (point))
        accounts
        (account-tree (list t))
        (account-elements nil)
        (seed-regex (ledger-account-any-status-with-seed-regex
                     (regexp-quote (car pcomplete-args)))))
    (save-excursion
      (goto-char (point-min))

      (dolist (account
               (delete-dups
                (progn
                  (while (re-search-forward seed-regex nil t)
                    (unless (between origin (match-beginning 0) (match-end 0))
                      (setq accounts (cons (match-string-no-properties 2) accounts))))
                  accounts)))
        (let ((root account-tree))
          (setq account-elements
                (split-string
                 account ":"))
          (while account-elements
            (let ((xact (assoc (car account-elements) root)))
              (if xact
                  (setq root (cdr xact))
                (setq xact (cons (car account-elements) (list t)))
                (nconc root (list xact))
                (setq root (cdr xact))))
            (setq account-elements (cdr account-elements))))))
    account-tree))

(defun ledger-accounts ()
  "Return a tree of all accounts in the buffer."
  (let* ((current (caar (ledger-parse-arguments)))
         (elements (and current (split-string current ":")))
         (root (ledger-find-accounts-in-buffer))
         (prefix nil))
    (while (cdr elements)
      (let ((xact (assoc (car elements) root)))
        (if xact
            (setq prefix (concat prefix (and prefix ":")
                                 (car elements))
                  root (cdr xact))
          (setq root nil elements nil)))
      (setq elements (cdr elements)))
    (setq root (delete (list (car elements) t) root))
    (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)) 'transaction)
              (if (null current-prefix-arg)
                  (delete
                   (caar (ledger-parse-arguments))
                   (ledger-payees-in-buffer)) ;; this completes against payee names
                (progn
                  (let ((text (buffer-substring-no-properties
                               (line-beginning-position)
                               (line-end-position))))
                    (delete-region (line-beginning-position)
                                   (line-end-position))
                    (condition-case nil
                        (ledger-add-transaction text t)
                      (error nil)))
                  (forward-line)
                  (goto-char (line-end-position))
                  (search-backward ";" (line-beginning-position) t)
                  (skip-chars-backward " \t0123456789.,")
                  (throw 'pcompleted t)))
            (ledger-accounts)))))

(defun ledger-trim-trailing-whitespace (str)
  (replace-regexp-in-string "[ \t]*$" "" str))

(defun ledger-fully-complete-xact ()
  "Completes a transaction if there is another matching payee in the buffer.
Does not use ledger xact"
  (interactive)
  (let* ((name (ledger-trim-trailing-whitespace (caar (ledger-parse-arguments))))
         (rest-of-name name)
         xacts)
    (save-excursion
      (when (eq 'transaction (ledger-thing-at-point))
        (delete-region (point) (+ (length name) (point)))
        ;; Search backward for a matching payee
        (when (re-search-backward
               (concat "^[0-9/.=-]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+\\(.*"
                       (regexp-quote name) ".*\\)" ) nil t)
          (setq rest-of-name (match-string 3))
          ;; Start copying the postings
          (forward-line)
          (while (looking-at ledger-account-any-status-regex)
            (setq xacts (cons (buffer-substring-no-properties
                               (line-beginning-position)
                               (line-end-position))
                              xacts))
            (forward-line))
          (setq xacts (nreverse xacts)))))
    ;; Insert rest-of-name and the postings
    (when xacts
      (save-excursion
        (insert rest-of-name ?\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))))))


(defcustom ledger-complete-ignore-case t
  "Non-nil means that ledger-complete-at-point will be case-insensitive"
  :type 'boolean
  :group 'ledger)

(defun ledger-pcomplete (&optional interactively)
  "Complete rip-off of pcomplete from pcomplete.el, only added
ledger-magic-tab in the previous commands list so that
ledger-magic-tab would cycle properly"
  (interactive "p")
  (let ((pcomplete-ignore-case ledger-complete-ignore-case))
    (if (and interactively
             pcomplete-cycle-completions
             pcomplete-current-completions
             (memq last-command '(ledger-magic-tab
                                  ledger-pcomplete
                                  pcomplete-expand-and-complete
                                  pcomplete-reverse)))
        (progn
          (delete-char (* -1 pcomplete-last-completion-length))
          (if (eq this-command 'pcomplete-reverse)
              (progn
                (push (car (last pcomplete-current-completions))
                      pcomplete-current-completions)
                (setcdr (last pcomplete-current-completions 2) nil))
            (nconc pcomplete-current-completions
                   (list (car pcomplete-current-completions)))
            (setq pcomplete-current-completions
                  (cdr pcomplete-current-completions)))
          (pcomplete-insert-entry pcomplete-last-completion-stub
                                  (car pcomplete-current-completions)
                                  nil pcomplete-last-completion-raw))
      (setq pcomplete-current-completions nil
            pcomplete-last-completion-raw nil)
      (catch 'pcompleted
        (let* (pcomplete-stub
               pcomplete-seen pcomplete-norm-func
               pcomplete-args pcomplete-last pcomplete-index
               pcomplete-autolist
               (completions (pcomplete-completions))
               (result (pcomplete-do-complete pcomplete-stub completions))
               (pcomplete-termination-string ""))
          (and result
               (not (eq (car result) 'listed))
               (cdr result)
               (pcomplete-insert-entry pcomplete-stub (cdr result)
                                       (memq (car result)
                                             '(sole shortest))
                                       pcomplete-last-completion-raw)))))))

(provide 'ledger-complete)

;;; ledger-complete.el ends here