hledger icon indicating copy to clipboard operation
hledger copied to clipboard

ledger-mode setup for hledger needs documenting

Open simonmichael opened this issue 8 years ago • 33 comments


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

simonmichael avatar Jul 13 '16 21:07 simonmichael

Since I'm working on getting this working, I don't mind writing something up once I got it working reasonably well.

taksuyu avatar Jul 13 '16 21:07 taksuyu

FYI:

; Simon's ledger-mode tweaks

(custom-set-variables

 ; neutralise some ledgerisms
 '(ledger-binary-path (expand-file-name "~/.local/bin/hledger"))
 '(ledger-mode-should-check-version nil)
 '(ledger-init-file-name " ")

 ; move default amount position right allowing longer account names
 '(ledger-post-amount-alignment-column 64)

 ; disable distracting highlight
 '(ledger-highlight-xact-under-point nil)

 )

; automatically show new transactions from hledger add or hledger-web
(add-hook 'ledger-mode-hook 'auto-revert-tail-mode)

; M-1 to collapse all transactions to one line, M-0 to reset. Useful for quickly scanning
(global-set-key "\M-0" (lambda () (interactive) (set-selective-display (* tab-width 0))))
(global-set-key "\M-1" (lambda () (interactive) (set-selective-display (* tab-width 1))))
(add-hook 'ledger-mode-hook (lambda () (setq tab-width 4)))

; enable some highlighting for CSV rules files
(add-to-list 'auto-mode-alist '("\\.rules$" . conf-mode))

; useful when running reports in a shell buffer
(defun highlight-negative-amounts nil (interactive)
  (highlight-regexp "\\(\\$-\\|-\\$\\)[.,0-9]+" (quote hi-red-b))
)

simonmichael avatar Jul 13 '16 21:07 simonmichael

; recognise more file suffixes
(add-to-list 'auto-mode-alist '("\\.\\(h?ledger\\|journal\\|j\\)$" . ledger-mode))

; enable orgstruct minor mode, TAB while on a * comment expands/collapses org node
; (can get slow though)
(add-hook 'ledger-mode-hook 'orgstruct-mode)

simonmichael avatar Jul 13 '16 21:07 simonmichael

https://github.com/narendraj9/dotfiles/tree/master/.emacs.d/packages/rest/hledger-mode should be investigated too, I haven't tried it.

simonmichael avatar Jul 19 '16 19:07 simonmichael

Other editor modes like https://github.com/ledger/vim-ledger would also be nice to know about.

simonmichael avatar Jul 19 '16 20:07 simonmichael

I seem to be comfortable using this configuration. I imagine there is more to look into and fixing little things.

(setq ledger-binary-path "hledger")
(setq ledger-mode-should-check-version nil)
(add-to-list 'auto-mode-alist '("\\.\\(h?ledger\\|journal\\|j\\)$" . ledger-mode))

(defvar ledger-report-balance
  (list "bal" (concat ledger-binary-path " -f %(ledger-file) bal")))

(defvar ledger-report-reg
  (list "reg" (concat ledger-binary-path " -f %(ledger-file) reg")))

(defvar ledger-report-payee
  (list "payee" (concat ledger-binary-path " -f %(ledger-file) reg @%(payee)")))

(defvar ledger-report-account
  (list "account" (concat ledger-binary-path " -f %(ledger-file) reg %(account)")))

(setq ledger-reports
      (list ledger-report-balance
            ledger-report-reg
            ledger-report-payee
            ledger-report-account))

Having hledger specific reports may also benefit.

taksuyu avatar Aug 11 '16 21:08 taksuyu

The C-c C-a with hledger gives me hledger: command xact is not recognized, run with no command to see a list what did I miss?

arademaker avatar Nov 21 '17 19:11 arademaker

ledger-mode's C-c C-a runs ledger-add-transaction. On my setup this is somehow working, I get a Transaction: prompt in minibuffer with today's date filled in, and enter inserts the date in the journal. Not terribly useful.

On yours, I guess it assumes the ledger-binary-path program has a xact command, and expects that to produce a journal entry on stdout. xact is an alias for Ledger's entry command.

hledger doesn't have the entry/xact command, though related functionality exists in several other commands (print --match, register-match, find, add). You could possibly make a good enough version with something like this in an executable hledger-xact.sh script in $PATH:

#!/bin/sh
hledger print --match $@

simonmichael avatar Nov 21 '17 20:11 simonmichael

PS ledger-mode 2017-09-02 24b43e34 here.

simonmichael avatar Nov 21 '17 20:11 simonmichael

Thanks , but I guess I will also need to redefine the command ledger-add-transaction or create a new one bound to C-c C-a right ?

arademaker avatar Nov 21 '17 23:11 arademaker

Hi @simonmichael , it took me a while to try your suggestion but it didn't work as I expected. Can you please help me? Using Ledger (prompt) I can have a new transaction with a new value given other fields automatically filled. For instance, using the xact I give a date, description (payee in my case), a new amount and the partial name of account 2. Like a magic, Ledger deduce the account 1 for me and complete the name of the account 2 also.

$ ledger -f lixo.dat xact 2018/08/10 Recanto R$\ 100 real:cc
2018/08/10 Recanto
    Expenses:Educacao:Escola:Sofia         R$ 100,00
    Assets:AR:Real:CC

If I give more info, it will also work. In the following case, housing is used to find the account Expense:Housing and real:cc to find Assets:AR:Real:CC

$ ledger -f lixo.dat xact 2018/08/10 Recanto housing R$\ 100 real:cc
2018/08/10 Recanto
    Expenses:Housing                       R$ 100,00
    Assets:AR:Real:CC

I haven't figured out how to have similar behaviour with hledger!

arademaker avatar Aug 07 '18 12:08 arademaker

Hi @arademaker, let's handle that question on the mail list and/or IRC channel. Perhaps you can repost there, and add all data needed for others to reproduce it and understand.

simonmichael avatar Aug 16 '18 11:08 simonmichael

When using ledger-report, I'm running into hledger's complaints about unsupported options --columns and --color. Ledger mode is passing those options here: https://github.com/ledger/ledger-mode/blob/d6a67d177bada0a5913019893dfc6015590bcb76/ledger-report.el#L438

So in order for me to use hledger's reports, I've had to:

(setq ledger-report-auto-width nil
      ledger-report-use-native-highlighting nil)

livtanong avatar Oct 28 '18 05:10 livtanong

Running into trouble with reconciliation. ledger still uses the old "uncleared" instead of "unmarked". Unsure how to modify reconcile-mode to use the new "unmarked". Any ideas?

livtanong avatar Jan 27 '19 18:01 livtanong

-U ?

simonmichael avatar Jan 27 '19 21:01 simonmichael

I'm aware of -U, but I don't know where to set it in emacs. 😅 is there something like ledger-reconcile-cli-command?

livtanong avatar Jan 28 '19 01:01 livtanong

It doesn't look like it. I think you'd have to submit a patch to ledger-mode changing --uncleared to -U (in ledger-do-reconcile).

I was thinking -U was a spelling that both tools would recognise. But, now I remember hledger's -U/--unmarked is different from ledger's -U/--uncleared. The exact match for ledger --uncleared is hledger --unmarked --pending (hledger -UP).

simonmichael avatar Jan 28 '19 17:01 simonmichael

PS I would be open to a PR making --uncleared a compatibility synonym for --unmarked --pending.

simonmichael avatar Jan 28 '19 17:01 simonmichael

Alas, I have no experience in haskell, so I won't be much help here. :(

livtanong avatar Feb 03 '19 19:02 livtanong

I don't think we are going to reach compatibility on this. The apis seem to have drifted too far apart.

As I attempted to fix the --uncleared flag, I continued running into other flags that were incompatible. First it was the --order flag, then the --limit flag.

@simonmichael There should be a warning that hledger is not compatible with ledger-mode. The only way this could be resolved is either having full backwards compatibility to ledger, or major refactoring to ledger-mode to support two different programs.

hackeryarn avatar Jul 25 '19 00:07 hackeryarn

Perhaps this was meant for #1002 ?

simonmichael avatar Jul 25 '19 06:07 simonmichael

Anyway: I'm not sure what problems you are having exactly.. is it that hard to make ledger-mode adapt to hledger here ?

"Not compatible" is too strong - I've used ledger-mode with hledger since the beginning. Some of ledger-mode's features indeed depend on Ledger's specific ui, which we will never match 100%. I mean if someone cared enough they could add a compatibility mode and try to reach 100% ui compatibility. I think it'd be much easier and less costly to generalise ledger-mode a little.

simonmichael avatar Jul 25 '19 06:07 simonmichael

Getting back to the main issue, perhaps most of all we need to start by documenting ledger-mode + hledger issues better. Could someone work on this ? I guess https://hledger.org/editors.html#ledger-mode is the most likely place.

simonmichael avatar Jul 25 '19 06:07 simonmichael

So I just saw that multiple commands needed to be updated, and couldn't just be generalized. e.g., both modes share the -U flag but it acts different, the --sort flag doesn't exist in hledger. Also ledger-mode has no hledger specific logic, currently.

I think the file format editing work perfectly, but most of the command like get balance and reconcile have issues.

I would also be interested in working on adding hledger support to ledger-mode. I can open up an issue and see if they are ok with adding new logic to handle hledger.

hackeryarn avatar Jul 25 '19 12:07 hackeryarn

Cool. Probably a small pull request will be a good way.

simonmichael avatar Jul 25 '19 15:07 simonmichael

I just sent a PR to ledger-mode to make it possible to get colors from hledger. As a result, it is not required to set ledger-report-use-native-highlighting to nil anymore as @levitanong suggested above. It is required to configure a new variable though:

(setq ledger-report-native-highlighting-arguments '("--color=always"))

DamienCassou avatar Jan 02 '21 10:01 DamienCassou

I tried to do the same for ledger-report-auto-width but failed because of the way ledger-mode passes arguments. I opened an issue there: https://github.com/ledger/ledger-mode/issues/279.

DamienCassou avatar Jan 02 '21 10:01 DamienCassou

From srk:

(setq
  ledger-binary-path "hledger"
  ledger-mode-should-check-version nil
  ledger-report-links-in-register nil
  ledger-report-auto-width nil 
  ledger-report-use-native-highlighting nil
  )

More notes:

  • run/create/edit a named report with [C-u] C-c C-o C-r (SPC m r in doom emacs)
  • commands can use expansions, like %(binary) -f %(ledger-file) bs (interpolates ledger-binary-path and current buffer's file path)
  • in the report buffer: space/backspace to scroll, e to edit the report command, s to save the report command permanently
  • the report buffer updates automatically as you save changes to the journal, but the window will disappear unless you make it dedicated (M-x toggle-window-dedicated) [seems to be specific to my setup]
  • reports can also be edited via M-x customize-var, ledger-reports
  • a nice thing a ledger-mode + hledger how-to or elisp shim could do is add all hledger commands & common reports to ledger-reports

simonmichael avatar Apr 14 '21 18:04 simonmichael

From @DamienCassou:

my Emacs configuration for hledger: https://github.com/DamienCassou/emacs.d/blob/master/init.el#L677 my ledger reports are defined here: https://github.com/DamienCassou/emacs.d/blob/master/init.el#L650 and my flycheck-hledger configuration: https://github.com/DamienCassou/emacs.d/blob/master/init.el#L623

simonmichael avatar Apr 14 '21 18:04 simonmichael

  • A hard-coded use of Ledger's --date-format flag was added to ledger-mode's ledger-exec.el in 2021-04. Reportedly (setq ledger-mode-should-check-version nil) gets around this. Bug report / PR to ledger-mode welcome!

simonmichael avatar Sep 25 '21 07:09 simonmichael

I've expanded my ledger proxy script given in #1721 to deal with ledger-mode's current C-c C-a ledger-add-transaction behavior. This assumes ISO dates.

ledger.sh:

#!/bin/sh

iargs=("$@")
oargs=()
j=0;
date=;
for((i=0; i<${#iargs[@]}; ++i)); do
    case ${iargs[i]} in
        --date-format)
            # drop --date-format and the next arg
            i=$((i+1));
            ;;
        xact)
            # convert "xact" to "print --match"
            oargs[j]=print; oargs[j+1]=--match; j=$((j+2));
            # drop xact argument and stash the date argument
            i=$((i+1));
            date=${iargs[i]};
            ;;
        *)
            # keep any other args:
            oargs[j]=${iargs[i]};
            j=$((j+1));
            ;;
    esac
done

if test "$date"
then
    # substitute the given date for the old date:
    hledger "${oargs[@]}" | sed "1s/....-..-../$date/"
else
    hledger "${oargs[@]}"
fi

Use setq ledger-binary-path ledger.sh to install your script.

acarrico avatar Nov 01 '21 17:11 acarrico

Thank you for your script @acarrico, I think it could be very useful to me. I can't make it work though.

For the sed to work at the end, the content of $date shouldn't contain any slash. But, ledger dates contain slashes by default (see the value of ledger-default-date-format which is used by ledger-format-date which is the function used to format the date from the minibuffer interactive input). Do you have a specific value for "input-date-format" in the variable ledger-environment-alist?

The workaround is easy: just change occurrences of "/" in the sed argument to something different, e.g., "&".

DamienCassou avatar Nov 04 '21 19:11 DamienCassou

@DamienCassou, correct, you will have to adjust the script if you don't use ISO standard dates, but maybe you should try them? See: https://github.com/simonmichael/hledger/issues/933

acarrico avatar Nov 10 '21 14:11 acarrico