commit 3cf9c57520f48efe677bb8ad44b1866495c6ed8c
parent 95be084fb145784268c9a48adbd0396c3b3536a8
Author: Wim Dupont <wim@wimdupont.com>
Date: Tue, 19 Nov 2024 19:12:26 +0100
added edit notes
Diffstat:
M | config.h | | | 1 | + |
M | scal.c | | | 72 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- |
2 files changed, 62 insertions(+), 11 deletions(-)
diff --git a/config.h b/config.h
@@ -17,6 +17,7 @@
#define KEY_VLEFT 'h'
#define KEY_VLEFT_ABS 'H'
#define KEY_BOT 'G'
+#define KEY_EDIT_NOTES 'E'
#define KEY_COMBO_GO 'g'
#define KEY_COMBO_GO_TODAY 'h'
diff --git a/scal.c b/scal.c
@@ -37,6 +37,7 @@ static void combo_key(int keypress);
static void combo_go(int keypress);
static void combo_make(int keypress);
static void add_note(struct tm *tm);
+static void edit_notes(void);
static void rm_note(struct tm *tm);
static int get_curnotes(void);
static int get_note_view_col(Recurring recurring);
@@ -55,6 +56,7 @@ static void curdate(void);
static char * cpstr(char *dest, const char *src);
static char * replace_digits(char *buf, const char *mask, int size);
static char * replace_home(char *buf, const char *path, const char *userhome);
+static void free_notes(void);
static void clean(void);
static void fatal(const char *fmt, ...);
@@ -65,7 +67,7 @@ static struct tm curtm, highltm;
static WINDOW *wwin, *vwin;
static int maxy, maxx, wmaxy, wmaxx, vmaxy, vmaxx, highlight, note_count=0, curnote_count=0;
static Note **notes, **curnotes = NULL;
-static char file[PATH_MAX];
+static char file[PATH_MAX], *editor;
int
main(int argc, char **argv)
@@ -73,6 +75,8 @@ main(int argc, char **argv)
char *userhome;
struct passwd userinf;
+ editor = getenv("EDITOR");
+
userinf = *getpwuid(getuid());
userhome = strdup(userinf.pw_dir);
@@ -246,6 +250,9 @@ start(void)
case ctrl('d'):
move_page_down();
break;
+ case KEY_EDIT_NOTES:
+ edit_notes();
+ break;
case KEY_COMBO_GO:
case KEY_COMBO_MAKE:
combo_key(c);
@@ -418,7 +425,6 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch
curs_set(0);
buf[phlen > 0 ? phlen : len] = '\0';
- //mvprintw(maxy-2, 1, "%d: %s",len, buf);
}
void
@@ -457,6 +463,39 @@ add_note(struct tm *tm) {
}
void
+edit_notes(void)
+{
+ if (editor == NULL)
+ fatal("no editor found; set EDITOR environment variable.");
+
+ sigset_t set;
+ char cmd[PATH_MAX + strlen(editor) + 4];
+
+ endwin();
+
+ if (sigemptyset (&set) == -1)
+ fatal("Sigemptyset failed.");
+ if (sigaddset(&set, SIGWINCH) == -1)
+ fatal("Sigaddset failed.");
+ if (sigprocmask(SIG_BLOCK, &set, NULL) != 0)
+ fatal("Blocking sigprocmask failed.");
+
+ sprintf(cmd, "%s \"%s\"", editor, file);
+ system(cmd);
+
+ if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0)
+ fatal("Unblocking sigprocmask failed.");
+
+ clear();
+ noecho();
+ cbreak();
+
+ free_notes();
+ get_notes();
+ refresh();
+}
+
+void
rm_note(struct tm *tm) {
int c, y, x;
@@ -850,6 +889,25 @@ curdate(void)
}
void
+free_notes(void)
+{
+ for (int i = 0; i < note_count; i++) {
+ free(notes[i]->description);
+ free(notes[i]);
+ }
+ if (notes != NULL)
+ free(notes);
+
+ if (curnotes != NULL)
+ free(curnotes);
+
+ notes = NULL;
+ curnotes = NULL;
+ note_count = 0;
+ curnote_count = 0;
+}
+
+void
clean(void)
{
clear();
@@ -859,15 +917,7 @@ clean(void)
delwin(stdscr);
endwin();
-
- for (int i = 0; i < note_count; i++) {
- free(notes[i]->description);
- free(notes[i]);
- }
- free(notes);
-
- if (curnotes != NULL)
- free(curnotes);
+ free_notes();
}
void