summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCédric Barreteau <unknown>2020-08-22 15:38:41 +0200
committerJohn Wiegley <johnw@newartisans.com>2020-08-28 11:05:34 -0700
commitc79674649dee7577d6061e3d0776922257520fd0 (patch)
tree305e466debaeb36e3ea49a37e952498602eb88e4
parent718ba0e3139d365ffb7883cb2c937cfe23656506 (diff)
downloadfork-ledger-c79674649dee7577d6061e3d0776922257520fd0.tar.gz
fork-ledger-c79674649dee7577d6061e3d0776922257520fd0.tar.bz2
fork-ledger-c79674649dee7577d6061e3d0776922257520fd0.zip
Add support for `$XDG_CONFIG_HOME`
`$XDG_CONFIG_HOME/ledger/ledgerrc` and `$HOME/.config/ledger/ledgerrc` are tried first when looking for an init file.
-rw-r--r--doc/NEWS.md2
-rw-r--r--doc/ledger.110
-rw-r--r--doc/ledger3.texi13
-rw-r--r--src/global.cc21
4 files changed, 28 insertions, 18 deletions
diff --git a/doc/NEWS.md b/doc/NEWS.md
index c310b087..2cf8d4a5 100644
--- a/doc/NEWS.md
+++ b/doc/NEWS.md
@@ -3,6 +3,8 @@
## 3.2.x (unreleased)
- Use $PAGER when environment variable is set (bug #1674)
+- Try to use `$XDG_HOME_CONFIG/ledger/ledgerrc` or `~/.config/ledger/ledgerrc`
+ first
## 3.2.1 (2020-05-18)
diff --git a/doc/ledger.1 b/doc/ledger.1
index 032cff02..f1fad3f6 100644
--- a/doc/ledger.1
+++ b/doc/ledger.1
@@ -965,16 +965,12 @@ report.
Allow the user to specify what account name should be used for
unrealized gains. Defaults to
.Sy "Equity:Unrealized Gains" .
-Often set in one's
-.Pa ~/.ledgerrc
-file to change the default.
+Often set in one's init file to change the default.
.It Fl \-unrealized-losses
Allow the user to specify what account name should be used for
unrealized losses. Defaults to
.Sy "Equity:Unrealized Losses" .
-Often set in one's
-.Pa ~/.ledgerrc
-file to change the default.
+Often set in one's init file to change the default.
.It Fl \-unround
Perform all calculations without rounding and display results to full
precision.
@@ -1445,6 +1441,8 @@ on the command-line. Options on the command-line always take precedence over
environment variable settings, however.
.Sh FILES
.Bl -tag -width -indent
+.It Pa $XDG_CONFIG_HOME/ledger/ledgerrc
+.It Pa ~/.config/local/ledgerrc
.It Pa ~/.ledgerrc
Your personal
.Nm
diff --git a/doc/ledger3.texi b/doc/ledger3.texi
index ce31f9aa..55dcf57e 100644
--- a/doc/ledger3.texi
+++ b/doc/ledger3.texi
@@ -5978,9 +5978,9 @@ the run.
Display the man page for @file{ledger}.
@item --init-file @var{FILE}
-Specify the location of the init file. The default is home directory
-@file{~/.ledgerrc}, or current directory @file{./.ledgerrc} if not found
-in home directory.
+Specify the location of the init file. By default, @file{$XDG_CONFIG_HOME},
+@file{~/.config/ledger/lederrc}, @file{~/.ledgerrc} and @file{./.ledgerrc} are
+tried in order.
@item --options
Display the options in effect for this Ledger invocation, along with
@@ -6882,12 +6882,12 @@ report.
@item --unrealized-gains @var{STR}
Allow the user to specify what account name should be used for
unrealized gains. Defaults to @samp{"Equity:Unrealized Gains"}.
-Often set in one's @file{~/.ledgerrc} file to change the default.
+Often set in one's init file to change the default.
@item --unrealized-losses @var{STR}
Allow the user to specify what account name should be used for
unrealized losses. Defaults to @samp{"Equity:Unrealized Losses"}.
-Often set in one's @file{~/.ledgerrc} file to change the default.
+Often set in one's init file to change the default.
@item --unround
Perform all calculations without rounding and display results to full
@@ -7275,8 +7275,7 @@ Change the basic date format used by reports. The default uses a date
like @samp{2004/08/01}, which represents the default date format of
@code{%Y/%m/%d}. To change the way dates are printed in general, the
easiest way is to put @option{--date-format @var{DATE_FORMAT}} in the
-Ledger initialization file @file{~/.ledgerrc} (or the file referred to
-by @env{LEDGER_INIT}).
+Ledger init file (or the file referred to by @env{LEDGER_INIT}).
@item --format @var{FORMAT_STRING}
@itemx -F @var{FORMAT_STRING}
diff --git a/src/global.cc b/src/global.cc
index 32d3cc5a..5a5df6f5 100644
--- a/src/global.cc
+++ b/src/global.cc
@@ -136,17 +136,28 @@ void global_scope_t::read_init()
path init_file;
if (HANDLED(init_file_)) {
init_file=HANDLER(init_file_).str();
- if (!exists(init_file)) {
+ if (! exists(init_file)) {
throw_(parse_error, _f("Could not find specified init file %1%") % init_file);
}
} else {
- if (const char * home_var = std::getenv("HOME")) {
- init_file = (path(home_var) / ".ledgerrc");
+ // in order, try to read the init file from:
+ // - $XDG_CONFIG_HOME/ledger/ledgerrc
+ // - $HOME/.config/ledger/ledgerrc
+ // - $HOME/.ledgerrc
+ // - ./.ledgerrc
+ if (const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME")) {
+ init_file = (path(xdg_config_home) / "ledger" / "ledgerrc");
+ }
+ if (! exists(init_file)) {
+ if (const char * home_var = std::getenv("HOME")) {
+ init_file = (path(home_var) / ".config" / "ledger" / "ledgerrc");
+ if (! exists(init_file)) {
+ init_file = (path(home_var) / ".ledgerrc");
+ }
+ }
if (! exists(init_file)) {
init_file = ("./.ledgerrc");
}
- } else {
- init_file = ("./.ledgerrc");
}
}
if (exists(init_file)) {