diff options
author | Steve Purcell <steve@sanityinc.com> | 2019-07-05 08:30:52 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-05 08:30:52 +1200 |
commit | 88a8a580b95fbd33a16b2f10d01f21d581a5b213 (patch) | |
tree | 5f604c89b62bc9b91d54656e593e690d20574960 | |
parent | 78ab0095621150833292dfb2aa0e1338f9044ea7 (diff) | |
parent | eac13e8600fdb07e2dd172928c5aca6a3fe4f306 (diff) |
Merge pull request #183 from purcell/account-completion-fix
Match account names more liberally for the sake of completion
-rw-r--r-- | ledger-complete.el | 43 | ||||
-rw-r--r-- | ledger-regex.el | 24 | ||||
-rw-r--r-- | test/complete-test.el | 55 |
3 files changed, 65 insertions, 57 deletions
diff --git a/ledger-complete.el b/ledger-complete.el index a62883f..1964818 100644 --- a/ledger-complete.el +++ b/ledger-complete.el @@ -24,12 +24,6 @@ (require 'cl-lib) -;; Emacs 24.3 compatibility -(defun ledger-string-greaterp (string1 string2) - "Return non-nil if STRING1 is greater than STRING2 in lexicographic order. -Case is significant." - (string-lessp string2 string1)) - ;; In-place completion support ;;; Code: @@ -101,15 +95,6 @@ that payee in the buffer." ;; to the list (sort (delete-dups payees-list) #'string-lessp))) -(defun ledger-accounts-deduplicate-sorted (l) - "Remove duplicates from a sorted list of strings L." - (let ((current l)) - (while (consp current) - (if (string= (car current) (cadr current)) - (setcdr current (cddr current)) - (pop current))) - l)) - (defun ledger-accounts-list-in-buffer () "Return a list of all known account names in the current buffer as strings. Considers both accounts listed in postings and those declared with \"account\" directives." @@ -117,9 +102,8 @@ Considers both accounts listed in postings and those declared with \"account\" d (goto-char (point-min)) (let (results) (while (re-search-forward ledger-account-name-or-directive-regex nil t) - (setq results (cons (match-string-no-properties 2) results))) - (ledger-accounts-deduplicate-sorted - (sort results #'ledger-string-greaterp))))) + (setq results (cons (match-string-no-properties 1) results))) + (sort (delete-dups results) #'string-lessp)))) (defun ledger-accounts-list () "Return a list of all known account names as strings. @@ -264,16 +248,19 @@ Looks in `ledger-accounts-file' if set, otherwise the current buffer." #'ledger-accounts-tree #'ledger-accounts-list)))) (when collection - (list start end - (if (functionp collection) - (completion-table-dynamic (lambda (_) (funcall collection))) - collection) - :exit-function (lambda (&rest _) - (when delete-suffix - (delete-char delete-suffix)) - (when (and realign-after ledger-post-auto-align) - (ledger-post-align-postings (line-beginning-position) (line-end-position)))) - 'ignore)))) + (let ((prefix (buffer-substring-no-properties start end))) + (list start end + (if (functionp collection) + (completion-table-dynamic + (lambda (_) + (cl-remove-if (apply-partially 'string= prefix) (funcall collection)))) + collection) + :exit-function (lambda (&rest _) + (when delete-suffix + (delete-char delete-suffix)) + (when (and realign-after ledger-post-auto-align) + (ledger-post-align-postings (line-beginning-position) (line-end-position)))) + 'ignore))))) (defun ledger-trim-trailing-whitespace (str) (replace-regexp-in-string "[ \t]*$" "" str)) diff --git a/ledger-regex.el b/ledger-regex.el index 9d858c8..c3c431f 100644 --- a/ledger-regex.el +++ b/ledger-regex.el @@ -71,28 +71,28 @@ (defconst ledger-init-string-regex "^--.+?\\($\\|[ ]\\)") +(defconst ledger-account-name-regex + "\\(?1:[^][();[:space:]\r\n]+\\(?: [^][();[:space:]]\r\n]+\\)*\\)") + (defconst ledger-account-directive-regex - "^account [ \t]*\\(?2:[^;]+?\\)\\(?3:[ \t]*\\)\\(;.*\\)?$") + (concat "^account[ \t]+" ledger-account-name-regex)) -(defconst ledger-account-any-status-no-trailing-spaces-regex - "^[ \t]+\\(?1:[*!]\\s-+\\)?[[(]?\\(?2:[^; ].+?\\)[])]?") +(defconst ledger-account-name-maybe-virtual-regex + (concat "[[(]?" ledger-account-name-regex "[])]?")) (defconst ledger-account-any-status-regex - (format "%s%s" - ledger-account-any-status-no-trailing-spaces-regex - "\\(?3:\t\\| [ \t]\\|$\\)")) + (concat "^[[:space:]]+\\(?:[!*][[:space:]]*\\)?" ledger-account-name-maybe-virtual-regex)) +;; This would incorrectly match "account (foo)", but writing the regexp this way +;; allows us to have just one match result (defconst ledger-account-name-or-directive-regex - (format "\\(?:%s\\|%s\\(?3:\t\\| [ \t]\\)\\)" - ledger-account-directive-regex - ledger-account-any-status-no-trailing-spaces-regex)) + (format "\\(?:%s\\|%s\\)" ledger-account-any-status-regex ledger-account-directive-regex)) (defconst ledger-account-pending-regex - "\\(^[ \t]+\\)\\(!\\s-*[^ ].*?\\)\\( \\|\t\\|$\\)") + (concat "\\(^[[:space:]]+\\)!" ledger-account-name-maybe-virtual-regex)) (defconst ledger-account-cleared-regex - "\\(^[ \t]+\\)\\(*\\s-*[^ ].*?\\)\\( \\|\t\\|$\\)") - + (concat "\\(^[[:space:]]+\\)*" ledger-account-name-maybe-virtual-regex)) (defmacro ledger-define-regexp (name regex docs &rest args) "Simplify the creation of a Ledger regex and helper functions." diff --git a/test/complete-test.el b/test/complete-test.el index a215549..87ff248 100644 --- a/test/complete-test.el +++ b/test/complete-test.el @@ -63,17 +63,17 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=252" :tags '(complete regress) (ledger-tests-with-temp-file - "2010/04/08 payee + "2010/04/08 payee account1 1 € account2 " - (goto-char (point-max)) - (newline) - (insert "2016/09/01 payee") - (ledger-fully-complete-xact) - (should - (equal (buffer-string) - "2010/04/08 payee + (goto-char (point-max)) + (newline) + (insert "2016/09/01 payee") + (ledger-fully-complete-xact) + (should + (equal (buffer-string) + "2010/04/08 payee account1 1 € account2 @@ -82,14 +82,35 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=252" account2 ")))) +(ert-deftest ledger-complete/test-complete-account-without-amount () + "https://github.com/ledger/ledger-mode/issues/141" + :tags '(complete regress) + (ledger-tests-with-temp-file + "2010/04/08 payee + blah 1 € + bloop + +2010/04/09 payee + blo" + (goto-char (point-max)) + (call-interactively 'completion-at-point) + (should + (equal (buffer-string) + "2010/04/08 payee + blah 1 € + bloop + +2010/04/09 payee + bloop")))) + (ert-deftest ledger-complete/test-find-accounts-in-buffer () (let ((ledger "*** Expenses account Expenses:Accomodation account Assets:Cash ; some comment account Assets:Current - alias 1187465S022 +; alias 1187465S022 -- Ideally this line could be uncommented commodity EUR - format 1,000.00 EUR +; format 1,000.00 EUR -- Ideally this line could be uncommented tag ofxid 2018/05/07 * Company Assets:Current -38.33 EUR @@ -104,14 +125,14 @@ tag ofxid (insert ledger) (should (equal (ledger-accounts-list-in-buffer) - (list ; I don't know why accounts are sorted in reverse order - "Something" - "Expenses:Utilities:Insurance" - "Expenses:Accomodation" - "Dimensions:Foo" - "Dimensions:Equity" + (list + "Assets:Cash" "Assets:Current" - "Assets:Cash")))))) + "Dimensions:Equity" + "Dimensions:Foo" + "Expenses:Accomodation" + "Expenses:Utilities:Insurance" + "Something")))))) (provide 'complete-test) |