You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

620 lines
22 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. ;;; package --- Summary
  2. ;;; Commentary:
  3. ;; A humble try to port leder-mode to beancount
  4. ;;; Code:
  5. ;(require 'beancount-regex)
  6. (require 'font-lock)
  7. (require 'company) ; for company-mode
  8. (require 'pcomplete)
  9. (require 'cl-lib)
  10. (defgroup beancount ()
  11. "Editing mode for beancount files."
  12. :group 'beancount)
  13. (defconst beancount-timestamped-directive-names
  14. '("balance"
  15. "open"
  16. "close"
  17. "pad"
  18. "document"
  19. "note"
  20. ;; the ones below are not followed by an account name.
  21. "event"
  22. "price"
  23. "commodity"
  24. "query"
  25. "txn")
  26. "Directive names that can appear after a date.")
  27. (defconst beancount-account-categories
  28. '("Assets"
  29. "Liabilities"
  30. "Equity"
  31. "Income"
  32. "Expenses"
  33. ; TODO: Fill changed root accounts automatically
  34. "Aktiva"
  35. "Verbindlichkeiten"
  36. "Eigenkapital"
  37. "Ertraege"
  38. "Aufwendungen"))
  39. (defconst beancount-nontimestamped-directive-names
  40. '("pushtag"
  41. "poptag"
  42. "option"
  43. "include"
  44. "plugin")
  45. "Directive names that can appear after a date.")
  46. (defconst beancount-option-names
  47. ;; this list has to be kept in sync with the options definied in
  48. ;; beancount/parser/options.py
  49. '("title"
  50. "name_assets"
  51. "name_equity"
  52. "name_income"
  53. "name_expenses"
  54. "bookin_algorithm"
  55. "bookin_method"
  56. "account_previous_balances"
  57. "account_previous_earnings"
  58. "account_previous_conversions"
  59. "account_current_earnings"
  60. "account_current_conversions"
  61. "account_rounding"
  62. "conversion_currency"
  63. "inferred_tolerance_default"
  64. "inferred_tolerance_multiplier"
  65. "infer_tolerance_from_cost"
  66. "documents"
  67. "operating_currency"
  68. "render_commas"
  69. "plugin_processing_mode"
  70. "plugin"
  71. "long_string_maxlines"
  72. ))
  73. (defconst beancount-directive-names
  74. (append beancount-nontimestamped-directive-names
  75. beancount-timestamped-directive-names)
  76. "A list of directive names.")
  77. (defconst beancount-tag-chars
  78. "[:alnum:]-_/."
  79. "Allowed tag characters.")
  80. (defconst beancount-account-chars
  81. "[:alnum:]-_:"
  82. "Allowed account characters.")
  83. (defconst beancount-account-regexp
  84. (concat (regexp-opt beancount-account-categories)
  85. "\\(?::[[:upper:]][" beancount-account-chars "]+\\)")
  86. "A regular expression to match account names.")
  87. ; TODO: currently shows all texts between ""
  88. (defconst beancount-payee-regexp
  89. "\"\\(.*?\\)\"")
  90. (defconst beancount-date-regexp
  91. "^[12][901][0-9]\\{2\\}-\\(\\(0[1-9]\\)\\|\\(1[012]\\)\\)-\\(\\([012][0-9]\\)\\|\\(3[01]\\)\\)"
  92. "Regular expression for dates.")
  93. (defconst beancount-number-regexp
  94. "[-+]?[0-9,]+\\(?:\\.[0-9]*\\)"
  95. "Regular expression to match decimal numbers.")
  96. (defconst beancount-tag-regexp
  97. (concat "#"
  98. "["
  99. beancount-tag-chars
  100. "]+")
  101. "Regular expression for valid tags.")
  102. (defconst beancount-currency-regexp
  103. "[A-Z][A-Z-_'.]*"
  104. "Regular expression to match currencies.")
  105. (defconst beancount-timestamped-accounts-regexp
  106. (concat beancount-date-regexp
  107. " " ;"\\(\\s-+\\)"
  108. (regexp-opt beancount-timestamped-directive-names)
  109. " ") ;"\\(\\s-+\\)")
  110. "A regular expression to match valid preceding characters before an account name.")
  111. (defconst beancount-amount-and-currency-regex
  112. "\s-*[-]?[0-9,]+[.][0-9]\\{2\\}\s[A-Za-z0-9.]+"
  113. "A regular expression for amounts including currency.")
  114. (defconst beancount-payee-any-status-regex
  115. "^[0-9]+[-/][-/.=0-9]+\\(\\s-+\\*\\)?\\(\\s-+(.*?)\\)?\\s-+\\(.+?\\)\\s-*\\(;\\|$\\)")
  116. (defconst beancount-date-and-status-regex
  117. (concat beancount-date-regexp
  118. "\\(\\s-+[\\*!]\\)")
  119. "Returns true for YYYY-MM-DD ! and YYYY-MM-DD *.")
  120. (defconst beancount-valid-prefix-for-directives-regex
  121. (concat "^"
  122. beancount-date-regexp
  123. "[ ]+"
  124. "[a-z]*$"))
  125. (defconst beancount-valid-prefix-for-payee-completion-regex
  126. (concat "\\("
  127. beancount-date-and-status-regex
  128. " \"" ; empty space and open quotes
  129. "[^\"\n]*" ; any number of chars except quotes and newline
  130. "\\(" ; start of optional second quoted term
  131. "\"[ ]+\"" ; closing quotes, whitespaces, opening quotes
  132. "[^\"\n]*" ; any number of chars except quotes and newline
  133. "\\)?" ; end of optional second quoted term
  134. "\\)$" ; the whole regex looks from the right side of the line
  135. ))
  136. (defconst beancount-valid-prefix-for-tag-completion-regex
  137. (concat ;"\\("
  138. beancount-date-and-status-regex
  139. " \"" ; empty space and open quotes
  140. "[^\"\n]*" ; any number of chars except quotes and newline
  141. "\\(" ; start of optional second quoted term
  142. "\"[ ]+\"" ; closing quotes, whitespaces, opening quotes
  143. "[^\"\n]*" ; any number of chars except quotes and newline
  144. "\\)?" ; end of optional second quoted term
  145. "\"[ ]+#"
  146. ;"\\)$" ; the whole regex looks from the right side of the line
  147. ))
  148. (defconst beancount-comments-regex
  149. (concat ";[^\"\n]*$")) ; right part of the line after a comment symbol if no quote or newline is included
  150. (defconst beancount-empty-line-regex
  151. "^\\(\\s-+\\)" ;; maybe "^[ \t]+" is better
  152. "Returns true for preceding whitespaces.")
  153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  154. ;; Faces
  155. (defgroup beancount-faces nil "Beancount mode highlighting" :group 'beancount)
  156. (defface beancount-font-default-face
  157. `((t :inherit default))
  158. "Default face"
  159. :group 'beancount-faces)
  160. (defface beancount-font-xact-cleared-face
  161. `((t :foreground "#AAAAAA" :weight normal))
  162. "Default face for cleared transactions"
  163. :group 'beancount-faces)
  164. (defface beancount-font-xact-pending-face
  165. `((t :foreground "#dc322f" :weight bold))
  166. "Default face for pending transactions"
  167. :group 'beancount-faces)
  168. (defface beancount-font-payee-cleared-face
  169. `((t :inherit beancount-font-other-face))
  170. "Default face for pending transactions"
  171. :group 'beancount-faces)
  172. (defface beancount-font-payee-pending-face
  173. `((t :foreground "#f24b61" :weight normal))
  174. "Default face for pending (!) transactions"
  175. :group 'beancount-faces)
  176. (defface beancount-font-other-face
  177. `((t :foreground "#657b83" :weight normal))
  178. "Default face for other transactions"
  179. :group 'beancount-faces)
  180. (defface beancount-font-posting-date-face
  181. `((t :foreground "#cb4b16" :weight normal))
  182. "Default face for dates"
  183. :group 'beancount-faces)
  184. (defface beancount-font-amount-face
  185. `((t :foreground "#cb4b16" :weight normal))
  186. "Default face for amounts"
  187. :group 'beancount-faces)
  188. (defvar beancount-font-lock-directives
  189. `(;; reserved keywords
  190. (,(regexp-opt beancount-directive-names) . font-lock-keyword-face)
  191. ;; tags & links
  192. ("[#\\^][A-Za-z0-9\-_/.]+" . font-lock-type-face)
  193. ;; comments
  194. (,beancount-comments-regex (0 font-lock-comment-face))
  195. ;; date
  196. (,beancount-date-regexp . 'beancount-font-posting-date-face)
  197. ;; account
  198. (,beancount-account-regexp . 'beancount-font-other-face)
  199. ;; payees
  200. ("\"\\(.*?\\)\"" . font-lock-comment-face)
  201. ;; txn flags
  202. ("! " . font-lock-warning-face)
  203. ))
  204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  205. ;; Alignments
  206. (defmacro beancount-for-line-in-region (begin end &rest exprs)
  207. "Iterate over each line in region from BEGIN to END (EXPRS)
  208. until an empty line is encountered."
  209. `(save-excursion
  210. (let ((end-marker (copy-marker ,end)))
  211. (goto-char ,begin)
  212. (beginning-of-line)
  213. (while (and (not (eobp)) (< (point) end-marker))
  214. (beginning-of-line)
  215. (progn ,@exprs)
  216. (forward-line 1)
  217. ))))
  218. (defun beancount-align-numbers (begin end)
  219. "Align all numbers in the current buffer from BEGIN to END."
  220. (interactive "r")
  221. ;; loop once in the buffer to find the length of the longest string before the
  222. ;; number.
  223. (let (prefix-widths
  224. number-widths
  225. (number-padding " "))
  226. (beancount-for-line-in-region
  227. begin end
  228. (let ((line (thing-at-point 'line)))
  229. (when (string-match (concat "\\(.*?\\)"
  230. "[ \t]+"
  231. "\\(" beancount-number-regexp "\\)"
  232. "[ \t]+"
  233. beancount-currency-regexp)
  234. line)
  235. (push (length (match-string 1 line)) prefix-widths)
  236. (push (length (match-string 2 line)) number-widths)
  237. )))
  238. (when prefix-widths
  239. ;; Loop again to make the adjustments to the numbers.
  240. (let* ((number-width (apply 'max number-widths))
  241. (number-format (format "%%%ss" number-width))
  242. ;; compute the rightmost column of prefix
  243. (max-prefix-width (apply 'max prefix-widths))
  244. (prefix-format (format "%%-%ss" max-prefix-width))
  245. )
  246. (beancount-for-line-in-region
  247. begin end
  248. (let ((line (thing-at-point 'line)))
  249. (when (string-match (concat "^\\([^\"]*?\\)"
  250. "[ \t]+"
  251. "\\(" beancount-number-regexp "\\)"
  252. "[ \t]+"
  253. "\\(.*\\)$")
  254. line)
  255. (delete-region (line-beginning-position) (line-end-position))
  256. (let* ((prefix (match-string 1 line))
  257. (number (match-string 2 line))
  258. (rest (match-string 3 line)))
  259. (insert (format prefix-format prefix))
  260. (insert number-padding)
  261. (insert (format number-format number))
  262. (insert " ")
  263. (insert rest)))))))))
  264. (defun beancount-hash-keys (hashtable)
  265. "Extract all the keys of the given HASHTABLE. Return a sorted list."
  266. (let (rlist)
  267. (maphash (lambda (k _v) (push k rlist)) hashtable)
  268. (sort rlist 'string<)))
  269. (defvar beancount-accounts nil
  270. "A list of the accounts available in this buffer.
  271. This is a cache of the value computed by `beancount-get-accounts'.")
  272. (make-variable-buffer-local 'beancount-accounts)
  273. (defun beancount-init-accounts ()
  274. "Initialize or reset the list of accounts."
  275. (interactive)
  276. (setq beancount-accounts (beancount-get-accounts-in-buffer)) ;-new
  277. (pcomplete-uniqify-list (nreverse beancount-accounts))
  278. ;; (setq beancount-accounts (beancount-get-accounts))
  279. (message "Accounts updated."))
  280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  281. ;; Navigation
  282. (defun beancount-navigate-start-xact-or-directive-p ()
  283. "Return t if at the beginning of an empty or all-whitespace line."
  284. (not (looking-at "[ \t]\\|\\(^$\\)")))
  285. (defun beancount-navigate-prev-xact-or-directive ()
  286. "Move to the beginning of the next xact or directive."
  287. (interactive)
  288. (beginning-of-line)
  289. (if (beancount-navigate-start-xact-or-directive-p) ;if we are at the start of an xact, move backward to the previous xact
  290. (progn
  291. (forward-line -1)
  292. (if (not (beancount-navigate-start-xact-or-directive-p)) ; we have moved backward and are not at another xact, recurse backward
  293. (beancount-navigate-prev-xact-or-directive)))
  294. (while (not (or (bobp)
  295. (beancount-navigate-start-xact-or-directive-p)))
  296. (forward-line -1))))
  297. (defun beancount-navigate-next-xact-or-directive ()
  298. "Move to the beginning of the next xact or directive."
  299. (interactive)
  300. (beginning-of-line)
  301. (if (beancount-navigate-start-xact-or-directive-p) ; if we are at the start of an xact, move forward to the next xact
  302. (progn
  303. (forward-line)
  304. (if (not (beancount-navigate-start-xact-or-directive-p)) ; we have moved forward and are not at another xact, recurse forward
  305. (beancount-navigate-next-xact-or-directive)))
  306. (while (not (or (eobp) ; we didn't stsrt off at the beginning of an xact
  307. (beancount-navigate-start-xact-or-directive-p)))
  308. (forward-line))))
  309. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  310. ;; Completion
  311. (defun beancount-thing-at-point ()
  312. "Describe thing at points. Return 'transaction, 'posting, or nil.
  313. Leave point at the beginning of the thing under point.")
  314. (defun beancount-trim-trailing-whitespace (str)
  315. "Replace trailing whitespaces in STR."
  316. (replace-regexp-in-string "[ \t]*$" "" str))
  317. ;; (defun beancount-fully-complete-xact ()
  318. ;; "Completes a transaction if there is another matching payee in the buffer."
  319. ;; (interactive)
  320. ;; (let* ((name (beancount-trim-trailing-whitespace (caar (beancount-parse-arguments))))
  321. ;; xacf)))
  322. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  323. ;; Functions for external programs
  324. (defvar beancount-filename-main buffer-file-name
  325. "File name of the main beancount file for beancount-check.")
  326. (defvar beancount-terminal-name "alacritty"
  327. "Name of the terminal emulator to run fava.")
  328. (defvar beancount-fava-exec "/usr/bin/fava"
  329. "Full path of fava executable.")
  330. (defvar beancount-install-dir nil
  331. "Directory in which Beancount's source is located.
  332. Only useful if you have not installed Beancount properly in your PATH")
  333. (defun beancount--run (prog &rest args)
  334. "Random text PROG ARGS."
  335. (let ((process-environment
  336. (if beancount-install-dir
  337. `(,(concat "PYTHONPATH=" beancount-install-dir)
  338. ,(concat "PATH="
  339. (expand-file-name "bin" beancount-install-dir)
  340. ":"
  341. (getenv "PATH"))
  342. ,@process-environment)
  343. process-environment))
  344. (compile-command (mapconcat (lambda (arg)
  345. (if (stringp arg)
  346. (shell-quote-argument arg) ""))
  347. (cons prog args)
  348. " ")))
  349. (call-interactively 'compile)))
  350. (defvar beancount-check-program "bean-check"
  351. "Program to run the parser and validator on an input file.")
  352. (defun beancount-check ()
  353. "Run `beancount-check-program'."
  354. (interactive)
  355. (let ((compilation-read-command nil))
  356. (beancount--run beancount-check-program beancount-filename-main)))
  357. ; (file-relative-name buffer-file-name))))
  358. (defun beancount-fava ()
  359. "Run `beancount-fava' and open the URL in the default browser."
  360. (interactive)
  361. (start-process "termx" nil beancount-terminal-name "-e " beancount-fava-exec beancount-filename-main)
  362. (sleep-for 0.5) ; necessary to prevent an error
  363. (browse-url "127.0.0.1:5000"))
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ;; Completions
  366. (defun beancount-get-accounts-in-buffer ()
  367. "Return a list of all accounts."
  368. (let ((origin (point))
  369. accounts-list)
  370. (save-excursion
  371. (goto-char (point-min))
  372. (while (re-search-forward
  373. beancount-account-regexp nil t)
  374. (setq accounts-list (cons (match-string-no-properties 0)
  375. accounts-list))))
  376. (pcomplete-uniqify-list (nreverse accounts-list))))
  377. (defun beancount-get-payees-in-buffer ()
  378. "Return a list of all payees."
  379. (let ((origin (point))
  380. payees-list)
  381. (save-excursion
  382. (goto-char (point-min))
  383. (while (re-search-forward
  384. "^[0-9- *]*\"\\(.*?\\)\" \"\\(.*?\\)\"" nil t) ; matches are in brackets
  385. (setq payees-list (cons (match-string-no-properties 1) ; get first match, generally the payee
  386. payees-list))
  387. (setq payees-list (cons (match-string-no-properties 2) ; get second match, generally a description
  388. payees-list))))
  389. (pcomplete-uniqify-list (nreverse payees-list))))
  390. (defun beancount-get-tags-in-buffer ()
  391. "Return a list of all tags."
  392. (let ((origin (point))
  393. tags-list)
  394. (save-excursion
  395. (goto-char (point-min))
  396. (while (re-search-forward
  397. beancount-tag-regexp nil t)
  398. (setq tags-list (cons (substring (match-string-no-properties 0) 1)
  399. tags-list))))
  400. (pcomplete-uniqify-list (nreverse tags-list))))
  401. (defun beancount-get-commodities-in-buffer ()
  402. "Return a list of all commodities / currencies."
  403. (let ((origin (point))
  404. commodities-list)
  405. (save-excursion
  406. (goto-char (point-min))
  407. (while (re-search-forward
  408. "^[ ]+[A-Za-z0-9-_:]+[ ]+[-]?[0-9,.]+[ ]+\\([A-Z][A-Z0-9-_.']*[A-Z0-9]\\)" nil t)
  409. (setq commodities-list (cons (match-string-no-properties 1)
  410. commodities-list))))
  411. (pcomplete-uniqify-list (nreverse commodities-list))))
  412. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  413. ;; Finishing setup
  414. (defvar beancount-mode-map
  415. (let ((map (make-sparse-keymap)))
  416. (define-key map [(meta ?p)] #'beancount-navigate-prev-xact-or-directive)
  417. (define-key map [(meta ?n)] #'beancount-navigate-next-xact-or-directive)
  418. map)
  419. "Keymap for `bean-mode'.")
  420. (easy-menu-define beancount-mode-menu beancount-mode-map
  421. "Beancount menu"
  422. '("Beancount"
  423. ["Customize Ledger Mode" (lambda () (interactive) (customize-group 'beanmode))]))
  424. ;; (defun beancount-get-accounts (&optional string)
  425. ;; "Return list of account names with STRING infix present.
  426. ;; STRING can be multiple words separated by a space."
  427. ;; (let* ((accounts-string (shell-command-to-string
  428. ;; (concat "bean-query -q -f csv "
  429. ;; "/home/marc/Archiv/Finanzen/transactions_ingdiba.beancount "
  430. ;; "'SELECT DISTINCT account ORDER BY ACCOUNT ASC'")))
  431. ;; (accounts-list (split-string accounts-string)))
  432. ;; accounts-list))
  433. (defvar beancount-accounts-cache nil
  434. "List of accounts cached for company mode.")
  435. (defvar beancount-payees-cache nil
  436. "List of payees cached for company mode.")
  437. (defvar beancount-tags-cache nil
  438. "List of tags cached for company mode.")
  439. (defvar beancount-commodities-cache nil
  440. "List of commodities / currencies cached for company mode.")
  441. (defun beancount-update-accounts-and-payees ()
  442. "Initialize or reset the list of accounts."
  443. (interactive)
  444. (setq beancount-accounts-cache (beancount-get-accounts-in-buffer)) ;-new
  445. (pcomplete-uniqify-list (nreverse beancount-accounts-cache))
  446. ;; (setq beancount-accounts (beancount-get-accounts))
  447. (message "Accounts updated.")
  448. (setq beancount-payees-cache (beancount-get-payees-in-buffer)) ;-new
  449. (pcomplete-uniqify-list (nreverse beancount-payees-cache))
  450. (message "Payees updated.")
  451. (setq beancount-commodities-cache (beancount-get-commodities-in-buffer))
  452. (pcomplete-uniqify-list (nreverse beancount-commodities-cache))
  453. (setq beancount-tags-cache (beancount-get-tags-in-buffer))
  454. (pcomplete-uniqify-list (nreverse beancount-tags-cache))
  455. (message "Tags updated."))
  456. ;; first test for conditional completions
  457. ;; e.g. only account names when completion is after a date
  458. ;; (defun beancount-company-candidates (prefix)
  459. ;; "Check what PREFIX does."
  460. ;; (let* (accounts (beancount-accounts-cache))
  461. ;; accounts))
  462. ;;;###autoload
  463. (defun company-beancount (command &optional arg &rest ignored)
  464. "Company backend for 'beancount-mode'.
  465. COMMAND, ARG, and IGNORED the regular meanings."
  466. (interactive (list 'interactive))
  467. (message arg)
  468. (pcase command
  469. (`interactive (company-begin-backend 'company-beancount))
  470. (`prefix (and (eq major-mode 'beancount-mode)
  471. (company-grab-symbol)))
  472. (`candidates
  473. (cond
  474. ;; if line ends with date and status, at max one quoted text, and an open quote, offer payees and explainations
  475. ((string-match-p beancount-valid-prefix-for-payee-completion-regex (thing-at-point 'line))
  476. (delq nil
  477. (mapcar (lambda (c)
  478. ; (and (string-prefix-p (substring arg 1) c) c)
  479. (and (string-prefix-p arg c) c))
  480. beancount-payees-cache)))
  481. ;; if line starts with date, offer directives
  482. ((string-match-p beancount-valid-prefix-for-directives-regex (thing-at-point 'line))
  483. (delq nil
  484. (mapcar (lambda (c)
  485. (and (string-prefix-p arg c) c))
  486. beancount-timestamped-directive-names)))
  487. ;; if line starts with date, status and payees, offer tags
  488. ((string-match-p beancount-valid-prefix-for-tag-completion-regex (thing-at-point 'line))
  489. (delq nil
  490. (mapcar (lambda (c)
  491. (and (string-prefix-p (substring arg 1) c) c))
  492. beancount-tags-cache)))
  493. ;; if line starts with accounts and amounts, offer commodities
  494. ((string-match-p (concat "^[ ]+[A-Za-z0-9-_:]+[ ]+[-]?[0-9,.]+[ ]+" beancount-currency-regexp) (thing-at-point 'line))
  495. (delq nil
  496. (mapcar (lambda (c)
  497. (and (string-prefix-p arg c) c))
  498. beancount-commodities-cache)))
  499. ;; if line is empty, offer accounts
  500. ((or (string-match-p "^\\s-+" (thing-at-point 'line))
  501. ;; if the preceding text is allowed before an account, offer accounts
  502. ;; TODO: Not yet working!
  503. (string-match-p beancount-timestamped-accounts-regexp (thing-at-point 'line)))
  504. (delq nil
  505. (mapcar (lambda (c)
  506. (message c)
  507. (and (string-prefix-p arg c) c))
  508. beancount-accounts-cache)))
  509. ))))
  510. ;;;###autoload
  511. (define-derived-mode beancount-mode text-mode "Beancount"
  512. "A mode for editing beancount files.
  513. \\{beancount-mode-map}"
  514. :init-value nil
  515. :lighter " Beancount"
  516. :group 'beancount
  517. ;; customize font-lock for beancount.
  518. (set-syntax-table beancount-mode-syntax-table)
  519. (font-lock-add-keywords nil beancount-font-lock-directives)
  520. (or beancount-accounts-cache
  521. (setq beancount-accounts-cache (beancount-get-accounts-in-buffer))) ;-new
  522. (or beancount-payees-cache
  523. (setq beancount-payees-cache (beancount-get-payees-in-buffer)))
  524. (or beancount-commodities-cache
  525. (setq beancount-commodities-cache (beancount-get-commodities-in-buffer)))
  526. (or beancount-tags-cache
  527. (setq beancount-tags-cache (beancount-get-tags-in-buffer)))
  528. )
  529. (provide 'beancount)
  530. ;;; beancount-mode.el ends here