diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit ed467795aaa78361b19d771bed959ec49fdc354b
parent 5ed50cfac7df72a1c5c94b5c90f591f1a556cb8d
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Sun, 17 Jan 2021 16:09:16 +0100

add CONFIG.editor and editor option argument

* Add '-e' / '--editor' options
* Add CONFIG.editor to config file with empty default
* Improve the documentation on variable precedence

Diffstat:
MREADME.md | 21+++++++++++++++++----
Mdiary.1 | 41++++++++++++++++++++++++++++++++++++++++-
Mdiary.c | 33++++++++++++++++++++++-----------
Mdiary.cfg | 3+++
Mdiary.h | 5++++-
5 files changed, 86 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md @@ -17,7 +17,7 @@ This is a text based diary, inspired by [khal](https://github.com/pimutils/khal) Instead of this, you can also set the environment variable `DIARY_DIR` to the desired directory. If both an argument and the environment - variable are given, the argument takes precedence. + variable are given, the argument takes precedence (see [Variable Precedence Rules](#precedence_rules)). The text files in this folder will be named 'yyyy-mm-dd'. @@ -83,7 +83,20 @@ The file `~/.config/diary/diary.cfg` should adhere to a basic `key = value` form | Command Line Option | Config Key | Example Config Value | Default Config Value | Description | | --- | --- | --- | --- | --- | -| `--dir`, `-d`, or first non-option argument | `dir` | ~/diary | n/a | Diary directory. Path that holds the journal text files. | +| `--dir`, `-d`, or first non-option argument | `dir` | ~/diary | n/a | Diary directory. Path that holds the journal text files. If unset, defaults to environment variable `$DIARY_DIR`.| | `--range` or `-r` | `range` | 10 | 1 | Number of years to show before/after todays date | | `--weekday` or `-w` | `weekday` | 0 | 1 | First weekday, `0` = Sunday, `1` = Monday, ..., `6` = Saturday. Use `0` to display week beginning at Sunday ("S-M-T-W-T-F-S"), or `1` for "M-T-W-T-F-S-S" (default) | -| `--fmt` or `-f` | `fmt` | %d_%b_%y | %Y-%m-%d | Date format and file name for the files inside the `dir`. For the format specifiers, see [`man strftime`](https://man7.org/linux/man-pages/man3/strftime.3.html). Be careful: If you change this, you might no longer find your existing diary entries, because the diary assumes to find the journal files under another file name. Hence, a change in FMT shows an empty diary, at first. Rename all files in the DIARY_DIR to migrate to a new FMT. | -\ No newline at end of file +| `--fmt` or `-f` | `fmt` | %d_%b_%y | %Y-%m-%d | Date format and file name for the files inside the `dir`. For the format specifiers, see [`man strftime`](https://man7.org/linux/man-pages/man3/strftime.3.html). Be careful: If you change this, you might no longer find your existing diary entries, because the diary assumes to find the journal files under another file name. Hence, a change in FMT shows an empty diary, at first. Rename all files in the DIARY_DIR to migrate to a new FMT. | +| `--editor` or `-e` | `editor` | "vim" | "" | Editor to open journal files with. If unset, defaults to environment variable `$EDITOR`. If no editor is provided, the diary is opened read-only. | + +## Precedence Rules +<a name="precedence_rules"></a> + +The variable defaults, for instance, for the variables `EDITOR` and `DIARY_DIR`, are populated with values in the following order: +1. No default for `DIARY_DIR`. Defaults for `range`, `weekday`, `fmt` and `editor` are provided in [diary.h](diary.h) +* If `EDITOR` is unset and no editor is provided in the config file or via the `-e` option, the diary works read-only. Journal files cannot be opened. +* If `DIARY_DIR` is not provided, the diary won't open. +2. **Config file** (empty default for `CONFIG.editor`, no default for `CONFIG.dir`) +3. **Environment** variables `$DIARY_DIR` / `$EDITOR` +4. **Option arguments** `-d` / `-e` +5. First non-option argument is interpreted as `DIARY_DIR` +\ No newline at end of file diff --git a/diary.1 b/diary.1 @@ -36,6 +36,9 @@ applied to newly saved entries. Existing entries with the old FMT are not automatically migrated to the new FMT and do not show up with a new FMT specifier. Consequently, a change in FMT shows an empty diary, at first. Rename all files in the DIARY_DIR to migrate to a new FMT. +.TP +\fB\-e\fR, \fB\-\-editor\fR=\fI\,EDITOR\/\fR +EDITOR is the text editor used for opening the journal files. .SH ENVIRONMENT @@ -83,7 +86,9 @@ Install an example config file with defaults: # 0 = Sunday, 1 = Monday, ..., 6 = Saturday weekday = 1 # Date and file format, change with care - fmt = %Y-%m-%d' | sed 's/^[[:space:]]*//' > ~/.config/diary/diary.cfg + fmt = %Y-%m-%d + # Editor to open journal files with + editor = ""' | sed 's/^[[:space:]]*//' > ~/.config/diary/diary.cfg .fi Use characters '#' or ';' for commenting lines. @@ -116,6 +121,40 @@ t | jump to today s | jump to specific day .TE +.SH PRECEDENCE RULES + +The variable defaults, for instance, for the variables 'EDITOR' and 'DIARY_DIR', are populated with values in the following order: + +.TP +1. +No default for 'DIARY_DIR'. Defaults for 'range', 'weekday', 'fmt' and 'editor' are provided in 'diary.h'. If 'EDITOR' is unset and no editor is provided in the config file or via the '-e' option, the +.B +diary +works read-only. Journal files cannot be opened. If 'DIARY_DIR' is not provided, the +.B +diary +won't open. +.TP +2. +.B +Config file +(empty default for 'CONFIG.editor', no default for 'CONFIG.dir') +.TP +3. +.B +Environment +variables '$DIARY_DIR' / '$EDITOR' +.TP +4. +.B +Option +arguments, see section +.B +OPTIONS +'-d' / '-e' +.TP +5. +First non-option argument \fIDIRECTORY\fR is interpreted as 'DIARY_DIR' .SH DEVELOPMENT All source code is available in this github repository: diff --git a/diary.c b/diary.c @@ -159,21 +159,13 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND /* Writes edit command for 'date' entry to 'rcmd'. '*rcmd' is NULL on error. */ void edit_cmd(const char* dir, size_t dir_size, const struct tm* date, char** rcmd, size_t rcmd_size) { - // get editor from environment - char* editor = getenv("EDITOR"); - if (editor == NULL) { - fprintf(stderr, "'EDITOR' environment variable not set"); - *rcmd = NULL; - return; - } - // set the edit command to env editor - if (strlen(editor) + 2 > rcmd_size) { + if (strlen(CONFIG.editor) + 2 > rcmd_size) { fprintf(stderr, "Binary path of default editor too long"); *rcmd = NULL; return; } - strcpy(*rcmd, editor); + strcpy(*rcmd, CONFIG.editor); strcat(*rcmd, " "); // get path of entry @@ -330,6 +322,9 @@ bool read_config(const char* file_path) } else if (strcmp("fmt", key_buf) == 0) { CONFIG.fmt = (char *) malloc(strlen(value_buf) + 1 * sizeof(char)); strcpy(CONFIG.fmt, value_buf); + } else if (strcmp("editor", key_buf) == 0) { + CONFIG.editor = (char *) malloc(strlen(value_buf) + 1 * sizeof(char)); + strcpy(CONFIG.editor, value_buf); } } } @@ -350,6 +345,7 @@ void usage() { printf(" -r, --range RANGE : RANGE is the number of years to show before/after today's date\n"); printf(" -w, --weekday DAY : First day of the week, 0 = Sun, 1 = Mon, ..., 6 = Sat\n"); printf(" -f, --fmt FMT : Date and file format, change with care\n"); + printf(" -e, --editor EDITOR : Editor to open journal files with\n"); printf("\n"); printf("Full docs and keyboard shortcuts: DIARY(1)\n"); printf("or online via: <https://github.com/in0rdr/diary>\n"); @@ -402,6 +398,7 @@ int main(int argc, char** argv) { // read config from config file path read_config(config_file_path); + // get diary directory from environment env_var = getenv("DIARY_DIR"); if (env_var != NULL) { // if available, overwrite the diary directory with the environment variable @@ -409,6 +406,14 @@ int main(int argc, char** argv) { strcpy(CONFIG.dir, env_var); } + // get editor from environment + env_var = getenv("EDITOR"); + if (env_var != NULL) { + // if available, overwrite the editor with the environments EDITOR + CONFIG.editor = (char *) calloc(strlen(env_var) + 1, sizeof(char)); + strcpy(CONFIG.editor, env_var); + } + // get the diary directory via argument, this takes precedence over env/config if (argc < 2) { if (CONFIG.dir == NULL) { @@ -428,12 +433,13 @@ int main(int argc, char** argv) { { "range", required_argument, 0, 'r' }, { "weekday", required_argument, 0, 'w' }, { "fmt", required_argument, 0, 'f' }, + { "editor", required_argument, 0, 'e' }, { 0, 0, 0, 0 } }; // read option characters while (1) { - option_char = getopt_long(argc, argv, "vhd:r:w:f:", long_options, &option_index); + option_char = getopt_long(argc, argv, "vhd:r:w:f:e:", long_options, &option_index); if (option_char == -1) { break; @@ -470,6 +476,11 @@ int main(int argc, char** argv) { CONFIG.fmt = (char *) calloc(strlen(optarg) + 1, sizeof(char)); strcpy(CONFIG.fmt, optarg); break; + case 'e': + // set default editor from option character + CONFIG.editor = (char *) calloc(strlen(optarg) + 1, sizeof(char)); + strcpy(CONFIG.editor, optarg); + break; default: printf("?? getopt returned character code 0%o ??\n", option_char); } diff --git a/diary.cfg b/diary.cfg @@ -6,3 +6,5 @@ range = 1 weekday = 1 # Date and file format, change with care fmt = %Y-%m-%d +# Editor to open journal files with +editor = "" +\ No newline at end of file diff --git a/diary.h b/diary.h @@ -51,12 +51,15 @@ typedef struct int weekday; // 2020-12-31 char* fmt; + // Editor to open journal files with + char* editor; } config; config CONFIG = { .range = 1, .weekday = 1, - .fmt = "%Y-%m-%d" + .fmt = "%Y-%m-%d", + .editor = "" }; #endif