Code Monkey home page Code Monkey logo

beancount-mode's Introduction

Emacs major-mode to work with Beancount ledger files

This package provides beancount-mode an Emacs major-mode implementing syntax highlighting, indentation, completion , and other facilities to edit and work with Beancount ledger files.

To instruct Emacs to activate beancount-mode when opening files with a .beancount extension, you can add this code to your Emacs configuration, typically in the ~/.emacs.d/init.el file:

(add-to-list 'load-path "/path/to/beancount-mode/")
(require 'beancount)
(add-to-list 'auto-mode-alist '("\\.beancount\\'" . beancount-mode))

Most facilities commonly provided by Emacs major modes are implemented by beancount-mode. Documentation on the provided functionality and on the default keybindings can be obtained with the describe-mode command in a buffer with beancount-mode active.

In a nutshell, when beancount-mode is active:

  • The “TAB” key either indents, completes, or folds the heading at point, depending on the context.
  • Amounts in postings are indented so that the decimal point is at the beancount-number-alignment-column column. Setting this variable to 0 will cause the alignment column to be determined from file content.
  • Postings in transactions, as well as metadata, links, and tags following directives, and are indented with beancount-transaction-indent spaces.
  • Pressing the “RET” key causes the current line to be automatically indented. If the current line is a posting, the amount will be indented as described above.

The automatic indentation behavior is defined by Emacs auto indent mechanism, however, it can be surprising or undesired. It can be disabled setting electric-indent-chars to nil after loading beancount-mode, for example like this:

(add-hook 'beancount-mode-hook
  (lambda () (setq-local electric-indent-chars nil)))

Beancount ledger files can grow very large. It is thus often practical to structure them in sections and subsections. To support this, beancount-mode leverages Outline minor-mode to enable navigation of the document structure to fold and unfold the document sections, similarly to what is possible in Org mode. Lines starting with one asterisks ”*” or three or more semicolons ”;;;” are interpreted as section headings. In Beancount, the semicolon starts a comment and all lines starting with an asterisks are ignored. The number of semicolons or asterisks determines the heading level.

To enable this functionality, outline-minor-mode should be explicitly activated. It is possible to do so automatically when beancout-mode is activated:

(add-hook 'beancount-mode-hook #'outline-minor-mode)

Outline minor mode uses a rather peculiar choice of keybindings. It is possible to map the most used functionality to keys more familiar to org-mode users adding a few lines to the Emacs configuration:

(define-key beancount-mode-map (kbd "C-c C-n") #'outline-next-visible-heading)
(define-key beancount-mode-map (kbd "C-c C-p") #'outline-previous-visible-heading)

Alternatively the keybindings for outline-minor-mode can be globally remapped. Please refer to the outline-minor-mode documentation in the Emacs manual for more details.

You can enable on-the-fly checks on your ledger file using bean-check via flymake:

(add-hook 'beancount-mode-hook #'flymake-bean-check-enable)

The etc/emacsrc file contains some example configuration for beancount-mode and some experiments that may find their way into the main codebase.

Keybinding compatibility

In mid-2023 the default keybindings for many commands in beancount-mode were changed to become compliant with the Emacs keybinding conventions. However, if you are accustomed to the old keybindings and would prefer to continue using them, just put this into your Emacs configuration before beancount.el is loaded:

(setq beancount-mode-old-style-keybindings t)

beancount-mode's People

Contributors

akirak avatar anks avatar blais avatar debanjum avatar dgchurchill avatar dnicolodi avatar hlissner avatar iostapyshyn avatar kenkangxgwe avatar kfogel avatar mbrase avatar monnier avatar ranjeethmahankali avatar starr-dust avatar tecosaur avatar twrightsman avatar visika avatar vkazanov avatar yyymeow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beancount-mode's Issues

`.gitignore`, `defadvice`, `flymake`, ...

I think we should fold the flymake code into beancount.el.
At the very least beancount-mode should add itself to flymake-diagnostic-functions so people who want to use Flymake in Beancount can just enable flyamke-mode rather than having to call some ad-hoc function.

Also, in etc/emacsrc there's a very weird advice, which I don't understand.
The patch below summarizes the kind of changes I think we should do.

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..bdc4ea6af6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.elc
+/beancount-autoloads.el
+/beancount-pkg.el
diff --git a/beancount.el b/beancount.el
index 50c8cd07e2..9d89a86314 100644
--- a/beancount.el
+++ b/beancount.el
@@ -1,7 +1,7 @@
 ;;; beancount.el --- A major mode to edit Beancount input files. -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2013 Martin Blais <[email protected]>
-;; Copyright (C) 2015 Free Software Foundation, Inc.
+;; Copyright (C) 2015-2024 Free Software Foundation, Inc.
 ;; Copyright (C) 2019 Daniele Nicolodi <[email protected]>
 
 ;; Version: 0
@@ -398,6 +398,8 @@ are reserved for the mode anyway.)")
   (setq-local outline-regexp beancount-outline-regexp)
   (setq-local outline-level #'beancount-outline-level)
 
+  (add-hook 'flymake-diagnostic-functions #'flymake-bean-check--run nil t)
+
   (setq imenu-generic-expression
 	(list (list nil (concat "^" beancount-outline-regexp "\\s-+\\(.*\\)$") 2))))
 
diff --git a/etc/emacsrc b/etc/emacsrc
index 6d3f366423..c4d2bceb8b 100644
--- a/etc/emacsrc
+++ b/etc/emacsrc
@@ -1,4 +1,4 @@
-;; -*- mode: emacs-lisp -*-
+;; -*- mode: emacs-lisp; lexical-binding: t -*-
 ;;
 ;; Emacs setup for Ledger.
 ;;
@@ -8,7 +8,8 @@
 (require 'beancount)
 
 ;; Automatically open .beancount files in beancount-mode.
-(add-to-list 'auto-mode-alist '("\\.beancount$" . beancount-mode))
+;; FIXME: Shouldn't that be in `beancount.el' with an ;;;###autoload ?
+(add-to-list 'auto-mode-alist '("\\.beancount\\'" . beancount-mode))
 
 
 ;; Support parsing Python logging errors, with a suitable logging.basicConfig()
@@ -73,13 +74,13 @@
 ;; alignment column to be determined from file content.  Postings in
 ;; transactions are indented with `beancount-transaction-indent` spaces.
 
-(defadvice shell-quote-argument (around dont-quote-already-quoted-args activate)
-  "Avoid quoting argument if it's already quoted."
-  (let ((arg (ad-get-arg 0)))
-    (setq ad-return-value
-          (if (or (string-match "\".*\"$" arg)
-                  (string-match "\'.*\'$" arg))
-              arg ad-do-it))))
+;; (defadvice shell-quote-argument (around dont-quote-already-quoted-args activate)
+;;   "Avoid quoting argument if it's already quoted."
+;;   (let ((arg (ad-get-arg 0)))
+;;     (setq ad-return-value
+;;           (if (or (string-match "\".*\"$" arg)
+;;                   (string-match "\'.*\'$" arg))
+;;               arg ad-do-it))))
 
 (defvar beancount-journal-command
   (concat
@@ -90,7 +91,7 @@
   "Run a journal command for the account at point."
   (interactive)
   (let* ((account (thing-at-point 'beancount-account))
-         (sql (concat "\"" (format beancount-journal-command account) "\"")))
+         (sql (format beancount-journal-command account) "\""))
     (beancount--run beancount-query-program
                     (file-relative-name buffer-file-name)
                     sql)))
@@ -110,7 +111,7 @@
   "Run a balance command for the account at point."
   (interactive)
   (let* ((account (thing-at-point 'beancount-account))
-         (sql (concat "\"" (format beancount-balance-command account) "\"")))
+         (sql (format beancount-balance-command account)))
     (beancount--run beancount-query-program
                     (file-relative-name buffer-file-name)
                     sql)))

Error with dependency against thingatpt in Emacs 26

On L847 of beancount.el, there is this line:

(define-thing-chars beancount-link beancount-link-chars)

This command define-thing-chars is no longer part of thingatpt. I do not know what the correct one should be. I have commented it out with success but do not know what won't work because of it.

bug report on windows

bug 1: if beancount file is located in a path with Chinese characters

then flymake-bean-check will tell me this file does not exist

image

bug 2: cannot use /dev/stdin on windows

In commit #40 flymake-bean-check.el line 90 /dev/stdin, this will cause a bug on windows 11

Support complete with split accounts?

Hi, I have split accounts into different files, such as accounts/income.beancount, accounts/expenses.beancount...

Then, I write transactions in transactions/xxx.beancount, however, I can't complete accounts name which defined under accounts directory

How can I get completion ?

I find a repository that support beancount-accounts-files custom variable , which can defined A list of files to provide candidates for accounts completion.

Maybe can support in this repo ?

English is not my native language; please excuse typing errors.

beancount-align-numbers can be confused by transaction title.

If I put the following in a beancount-mode buffer, select it then run beancount-align-numbers,

2020-02-18 * "abc abc abc abc abc abc * 0.5 abc"
  Equity:A 1.00 CAD

then I get this:

2020-02-18 * "abc abc abc abc abc abc * 0.5 abc"
  Equity:A                               1.00 CAD

Without the "* 0.5", it keeps the 1.00 CAD closer to Equity:A, which is better. I'm not sure what exactly it is in that title that's confusing it. (I encountered something like this in a real life but changed the text for privacy.)

Padding accounts don't autocomplete

The second account, i.e. padding is not autocompleted at the moment. I tested by adding the following lines to beancount-completion-at-point function, between the conditions corresponding to ;; timestamped directives followed by account and ;; posting:

         ;; pad directive followed by two accounts
         ((beancount-looking-at
           (concat "^" beancount-date-regexp
                   "\\s-+" (regexp-opt '("pad"))
                   "\\s-+\\([" beancount-account-chars "]*\\)"
                   "\\s-+\\([" beancount-account-chars "]*\\)") 2 pos)
          (setq beancount-accounts nil)
          (list (match-beginning 2) (match-end 2) #'beancount-account-completion-table))

This works as expected and autocompletes the padding account. But I am not familiar enough with the repo to fork / PR it myself. Thank you so much!

beancount.el Warning (initialization): An error occurred while loading

Hello,

Sorry to bother you, trying to install beancount-mode for my emacs file

I loaded and pasted the code from your website into the init file.
I placed the beancount.el file into a folder
When I try beancount mode: Alt-x beancount-mode I get an error:
(beancount-highlight-transaction-at-point): (void-function beancount-highlight-transaction-at-point)

When I open emacs:
Warning (initialization): An error occurred while loading `/home/family281/.emacs.d/init.el':
Symbol's value as variable is void: completion-category-default

Could you please assist me? I am not computer savvy.
Thank You

The default keybindings of the Emacs package

(Recreated ticket from emails)

May I reopen? I think there is still an issue here. The default, out-of-the-box beancount.el violates the Emacs key-binding conventions because it maps beancount-mode-map-prefix to C-c and within that keymap takes over some plain alphabetic keys. Sure, the user could map to some other prefix, but since C-c is reserved as a mode prefix anyway why don't we just do what Emacs recommends?:

Sequences consisting of C-c followed by a control character or a digit are reserved for major modes.
Sequences consisting of C-c followed by {, }, <, >, : or ; are also reserved for major modes.
Thus the most direct solution would be to replace each plain LETTER with Control-LETTER. Other solutions are possible too, but that seems like the easiest one.

(By the way, yes, this bug is affecting me.)

--Karl Fogel

Is it OK if I submit beancount.el to MELPA?

I use beancount.el regularly and I manage most of my emacs configuration using use-package, which is designed to support fetching packages from official sources like ELPA and MELPA. I thought it might be fun to try to submit beancount to MELPA. I have a Per https://github.com/melpa/melpa/blob/master/CONTRIBUTING.org, I should "please notify the authors prior to submitting and include them in the pull request process".

I'm not exactly what the process is going to be like. It seems like the MELPA maintainers do a code review, not only on the "recipe", but on the elisp code of the package in question. I can try to turn any feedback into PRs on beancount.el although I understand you may not be interested in merging it just to please the maintainers of some other project.

I have a branch at https://github.com/glasserc/melpa/tree/add-beancount which seems to work, so if you're OK with it, I'll open a PR on the main MELPA repository!

-- Ethan Glasser-Camp

Custom account names not recognized by beancount-mode

New issue 183: Custom account names not recognized by beancount-mode

https://bitbucket.org/blais/beancount/issues/183/custom-account-names-not-recognized-by

Carlos José Ruiz-Henestrosa Ruiz:

The const beancount-account-categories in editors/emacs/beancount.el is defined to the default account names, but it doesn't take into account the options name_*. Therefore, if those options are defined to something different than the default, beancount-mode becomes incapable of recognising or completing account names. Instead, it should scan the file for those options and set the aforementioned const accordingly.

Payees do not autocomplete

As far as I can tell there is no completion for payees. I think this is a useful feature because it helps keep consistent naming with payees, which in turn helps with grouping expenses by payee string. I used it all the time with vim-beancount.

I'm not familiar with Elisp yet but I could try and implement this sometime.

Flymake integration

I have implemented a basic flymake support: flymake-bean-check.

If you wish, I will contribute the file to this repository or transfer the repository to your organization.

What option would you prefer?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.