diff options
Diffstat (limited to 'src/todo.c')
-rw-r--r-- | src/todo.c | 497 |
1 files changed, 231 insertions, 266 deletions
@@ -1,7 +1,7 @@ /* * Calcurse - text-based organizer * - * Copyright (c) 2004-2011 calcurse Development Team <misc@calcurse.org> + * Copyright (c) 2004-2012 calcurse Development Team <misc@calcurse.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -47,171 +47,161 @@ static int first = 1; static char *msgsav; /* Returns a structure containing the selected item. */ -static struct todo * -todo_get_item (int item_number) +static struct todo *todo_get_item(int item_number) { - return LLIST_GET_DATA (LLIST_NTH (&todolist, item_number - 1)); + return LLIST_GET_DATA(LLIST_NTH(&todolist, item_number - 1)); } /* Sets which todo is highlighted. */ -void -todo_hilt_set (int highlighted) +void todo_hilt_set(int highlighted) { hilt = highlighted; } -void -todo_hilt_decrease (void) +void todo_hilt_decrease(int n) { - hilt--; + hilt -= n; } -void -todo_hilt_increase (void) +void todo_hilt_increase(int n) { - hilt++; + hilt += n; } /* Return which todo is highlighted. */ -int -todo_hilt (void) +int todo_hilt(void) { - return (hilt); + return hilt; } /* Return the number of todos. */ -int -todo_nb (void) +int todo_nb(void) { - return (todos); + return todos; } /* Set the number of todos. */ -void -todo_set_nb (int nb) +void todo_set_nb(int nb) { todos = nb; } /* Set which one is the first todo to be displayed. */ -void -todo_set_first (int nb) +void todo_set_first(int nb) { first = nb; } -void -todo_first_increase (void) +void todo_first_increase(int n) { - first++; + first += n; } -void -todo_first_decrease (void) +void todo_first_decrease(int n) { - first--; + first -= n; } /* * Return the position of the hilghlighted item, relative to the first one * displayed. */ -int -todo_hilt_pos (void) +int todo_hilt_pos(void) { - return (hilt - first); + return hilt - first; } /* Return the last visited todo. */ -char * -todo_saved_mesg (void) +char *todo_saved_mesg(void) { - return (msgsav); + return msgsav; } /* Request user to enter a new todo item. */ -void -todo_new_item (void) +void todo_new_item(void) { int ch = 0; - char *mesg = _("Enter the new ToDo item : "); - char *mesg_id = _("Enter the ToDo priority [1 (highest) - 9 (lowest)] :"); + const char *mesg = _("Enter the new ToDo item : "); + const char *mesg_id = + _("Enter the ToDo priority [1 (highest) - 9 (lowest)] :"); char todo_input[BUFSIZ] = ""; - status_mesg (mesg, ""); - if (getstring (win[STA].p, todo_input, BUFSIZ, 0, 1) == GETSTRING_VALID) - { - while ((ch < '1') || (ch > '9')) - { - status_mesg (mesg_id, ""); - ch = wgetch (win[STA].p); - } - todo_add (todo_input, ch - '0', NULL); - todos++; + status_mesg(mesg, ""); + if (getstring(win[STA].p, todo_input, BUFSIZ, 0, 1) == GETSTRING_VALID) { + while ((ch < '1') || (ch > '9')) { + status_mesg(mesg_id, ""); + ch = wgetch(win[STA].p); } + todo_add(todo_input, ch - '0', NULL); + todos++; + } } -static int -todo_cmp_id (struct todo *a, struct todo *b) +static int todo_cmp_id(struct todo *a, struct todo *b) { /* * As of version 2.6, todo items can have a negative id, which means they * were completed. To keep them sorted, we need to consider the absolute id * value. */ - int abs_a = abs (a->id); - int abs_b = abs (b->id); + int abs_a = abs(a->id); + int abs_b = abs(b->id); - return (abs_a < abs_b ? -1 : (abs_a == abs_b ? 0 : 1)); + return abs_a < abs_b ? -1 : (abs_a == abs_b ? 0 : 1); } /* * Add an item in the todo linked list. */ -struct todo * -todo_add (char *mesg, int id, char *note) +struct todo *todo_add(char *mesg, int id, char *note) { struct todo *todo; - todo = mem_malloc (sizeof (struct todo)); - todo->mesg = mem_strdup (mesg); + todo = mem_malloc(sizeof(struct todo)); + todo->mesg = mem_strdup(mesg); todo->id = id; - todo->note = (note != NULL && note[0] != '\0') ? mem_strdup (note) : NULL; + todo->note = (note != NULL && note[0] != '\0') ? mem_strdup(note) : NULL; - LLIST_ADD_SORTED (&todolist, todo, todo_cmp_id); + LLIST_ADD_SORTED(&todolist, todo, todo_cmp_id); return todo; } +void todo_write(struct todo *todo, FILE * f) +{ + if (todo->note) + fprintf(f, "[%d]>%s %s\n", todo->id, todo->note, todo->mesg); + else + fprintf(f, "[%d] %s\n", todo->id, todo->mesg); +} + /* Delete a note previously attached to a todo item. */ -static void -todo_delete_note_bynum (unsigned num) +static void todo_delete_note_bynum(unsigned num) { - llist_item_t *i = LLIST_NTH (&todolist, num); + llist_item_t *i = LLIST_NTH(&todolist, num); if (!i) - EXIT (_("no such todo")); - struct todo *todo = LLIST_TS_GET_DATA (i); + EXIT(_("no such todo")); + struct todo *todo = LLIST_TS_GET_DATA(i); if (!todo->note) - EXIT (_("no note attached")); - erase_note (&todo->note, ERASE_FORCE_ONLY_NOTE); + EXIT(_("no note attached")); + erase_note(&todo->note); } /* Delete an item from the todo linked list. */ -static void -todo_delete_bynum (unsigned num, enum eraseflg flag) +static void todo_delete_bynum(unsigned num) { - llist_item_t *i = LLIST_NTH (&todolist, num); + llist_item_t *i = LLIST_NTH(&todolist, num); if (!i) - EXIT (_("no such todo")); - struct todo *todo = LLIST_TS_GET_DATA (i); + EXIT(_("no such todo")); + struct todo *todo = LLIST_TS_GET_DATA(i); - LLIST_REMOVE (&todolist, i); - mem_free (todo->mesg); - erase_note (&todo->note, flag); - mem_free (todo); + LLIST_REMOVE(&todolist, i); + mem_free(todo->mesg); + erase_note(&todo->note); + mem_free(todo); } /* @@ -220,186 +210,164 @@ todo_delete_bynum (unsigned num, enum eraseflg flag) * This way, it is easy to retrive its original priority if the user decides * that in fact it was not completed. */ -void -todo_flag (void) +void todo_flag(void) { struct todo *t; - t = todo_get_item (hilt); + t = todo_get_item(hilt); t->id = -t->id; } /* Delete an item from the ToDo list. */ -void -todo_delete (struct conf *conf) +void todo_delete(void) { - char *choices = "[y/n] "; - char *del_todo_str = _("Do you really want to delete this task ?"); - char *erase_warning = + const char *del_todo_str = _("Do you really want to delete this task ?"); + const char *erase_warning = _("This item has a note attached to it. " "Delete (t)odo or just its (n)ote ?"); - char *erase_choice = _("[t/n] "); - unsigned go_for_todo_del = 0; - int answer, has_note; - - if (conf->confirm_delete) - { - status_mesg (del_todo_str, choices); - answer = wgetch (win[STA].p); - if ((answer == 'y') && (todos > 0)) - { - go_for_todo_del = 1; - } - else - { - wins_erase_status_bar (); - return; - } - } - else if (todos > 0) - go_for_todo_del = 1; - - if (go_for_todo_del == 0) - { - wins_erase_status_bar (); - return; - } - - answer = -1; - has_note = (todo_get_item (hilt)->note != NULL) ? 1 : 0; - if (has_note == 0) - answer = 't'; + const char *erase_choice = _("[tn]"); + const int nb_erase_choice = 2; + int answer; - while (answer != 't' && answer != 'n' && answer != KEY_GENERIC_CANCEL) - { - status_mesg (erase_warning, erase_choice); - answer = wgetch (win[STA].p); - } + if ((todos <= 0) || + (conf.confirm_delete && (status_ask_bool(del_todo_str) != 1))) { + wins_erase_status_bar(); + return; + } - switch (answer) - { - case 't': - todo_delete_bynum (hilt - 1, ERASE_FORCE); - todos--; - if (hilt > 1) - hilt--; - if (todos == 0) - hilt = 0; - if (hilt - first < 0) - first--; - break; - case 'n': - todo_delete_note_bynum (hilt - 1); - break; - default: - wins_erase_status_bar (); - return; - } + /* This todo item doesn't have any note associated. */ + if (todo_get_item(hilt)->note == NULL) + answer = 1; + else + answer = status_ask_choice(erase_warning, erase_choice, nb_erase_choice); + + switch (answer) { + case 1: + todo_delete_bynum(hilt - 1); + todos--; + if (hilt > 1) + hilt--; + if (todos == 0) + hilt = 0; + if (hilt - first < 0) + first--; + break; + case 2: + todo_delete_note_bynum(hilt - 1); + break; + default: + wins_erase_status_bar(); + return; + } } /* * Returns the position into the linked list corresponding to the * given todo item. */ -static int -todo_get_position (struct todo *needle) +static int todo_get_position(struct todo *needle) { llist_item_t *i; int n = 0; - LLIST_FOREACH (&todolist, i) - { - n++; - if (LLIST_TS_GET_DATA (i) == needle) - return n; - } + LLIST_FOREACH(&todolist, i) { + n++; + if (LLIST_TS_GET_DATA(i) == needle) + return n; + } - EXIT (_("todo not found")); - return -1; /* avoid compiler warnings */ + EXIT(_("todo not found")); + return -1; /* avoid compiler warnings */ } /* Change an item priority by pressing '+' or '-' inside TODO panel. */ -void -todo_chg_priority (int action) +void todo_chg_priority(int action) { struct todo *backup; char backup_mesg[BUFSIZ]; int backup_id; - char backup_note[NOTESIZ + 1]; - int do_chg = 1; + char backup_note[MAX_NOTESIZ + 1]; - backup = todo_get_item (hilt); - (void)strncpy (backup_mesg, backup->mesg, strlen (backup->mesg) + 1); + backup = todo_get_item(hilt); + strncpy(backup_mesg, backup->mesg, strlen(backup->mesg) + 1); backup_id = backup->id; if (backup->note) - (void)strncpy (backup_note, backup->note, NOTESIZ + 1); + strncpy(backup_note, backup->note, MAX_NOTESIZ + 1); else backup_note[0] = '\0'; - switch (action) - { - case KEY_RAISE_PRIORITY: - (backup_id > 1) ? backup_id-- : do_chg--; - break; - case KEY_LOWER_PRIORITY: - (backup_id > 0 && backup_id < 9) ? backup_id++ : do_chg--; - break; - default: - EXIT (_("no such action")); - /* NOTREACHED */ - } - if (do_chg) - { - todo_delete_bynum (hilt - 1, ERASE_FORCE_KEEP_NOTE); - backup = todo_add (backup_mesg, backup_id, backup_note); - hilt = todo_get_position (backup); - } + switch (action) { + case KEY_RAISE_PRIORITY: + if (backup_id > 1) + backup_id--; + else + return; + break; + case KEY_LOWER_PRIORITY: + if (backup_id > 0 && backup_id < 9) + backup_id++; + else + return; + break; + default: + EXIT(_("no such action")); + /* NOTREACHED */ + } + + todo_delete_bynum(hilt - 1); + backup = todo_add(backup_mesg, backup_id, backup_note); + hilt = todo_get_position(backup); } /* Edit the description of an already existing todo item. */ -void -todo_edit_item (void) +void todo_edit_item(void) { struct todo *i; - char *mesg = _("Enter the new ToDo description :"); + const char *mesg = _("Enter the new ToDo description :"); - status_mesg (mesg, ""); - i = todo_get_item (hilt); - updatestring (win[STA].p, &i->mesg, 0, 1); + status_mesg(mesg, ""); + i = todo_get_item(hilt); + updatestring(win[STA].p, &i->mesg, 0, 1); } /* Display todo items in the corresponding panel. */ static void -display_todo_item (int incolor, char *msg, int prio, int note, int len, int y, - int x) +display_todo_item(int incolor, char *msg, int prio, int note, int width, int y, + int x) { WINDOW *w; int ch_note; - char buf[len], priostr[2]; + char buf[width * UTF8_MAXLEN], priostr[2]; + int i; w = win[TOD].p; ch_note = (note) ? '>' : '.'; if (prio > 0) - snprintf (priostr, sizeof priostr, "%d", prio); + snprintf(priostr, sizeof priostr, "%d", prio); else - snprintf (priostr, sizeof priostr, "X"); + strncpy(priostr, "X", sizeof priostr); if (incolor == 0) - custom_apply_attr (w, ATTR_HIGHEST); - if (strlen (msg) < len) - mvwprintw (w, y, x, "%s%c %s", priostr, ch_note, msg); - else - { - (void)strncpy (buf, msg, len - 1); - buf[len - 1] = '\0'; - mvwprintw (w, y, x, "%s%c %s...", priostr, ch_note, buf); + custom_apply_attr(w, ATTR_HIGHEST); + if (utf8_strwidth(msg) < width) + mvwprintw(w, y, x, "%s%c %s", priostr, ch_note, msg); + else { + for (i = 0; msg[i] && width > 0; i++) { + if (!UTF8_ISCONT(msg[i])) + width -= utf8_width(&msg[i]); + buf[i] = msg[i]; } + if (i) + buf[i - 1] = 0; + else + buf[0] = 0; + mvwprintw(w, y, x, "%s%c %s...", priostr, ch_note, buf); + } if (incolor == 0) - custom_remove_attr (w, ATTR_HIGHEST); + custom_remove_attr(w, ATTR_HIGHEST); } /* Updates the ToDo panel. */ -void -todo_update_panel (int which_pan) +void todo_update_panel(int which_pan) { llist_item_t *i; int len = win[TOD].w - 8; @@ -412,97 +380,94 @@ todo_update_panel (int which_pan) int incolor = -1; /* Print todo item in the panel. */ - erase_window_part (win[TOD].p, 1, title_lines, win[TOD].w - 2, - win[TOD].h - 2); - LLIST_FOREACH (&todolist, i) - { - struct todo *todo = LLIST_TS_GET_DATA (i); - num_todo++; - t_realpos = num_todo - first; - incolor = num_todo - hilt; - if (incolor == 0) - msgsav = todo->mesg; - if (t_realpos >= 0 && t_realpos < max_items) - { - display_todo_item (incolor, todo->mesg, todo->id, - (todo->note != NULL) ? 1 : 0, len, y_offset, - x_offset); - y_offset = y_offset + todo_lines; - } + erase_window_part(win[TOD].p, 1, title_lines, win[TOD].w - 2, win[TOD].h - 2); + LLIST_FOREACH(&todolist, i) { + struct todo *todo = LLIST_TS_GET_DATA(i); + num_todo++; + t_realpos = num_todo - first; + incolor = (which_pan == TOD) ? num_todo - hilt : num_todo; + if (incolor == 0) + msgsav = todo->mesg; + if (t_realpos >= 0 && t_realpos < max_items) { + display_todo_item(incolor, todo->mesg, todo->id, + (todo->note != NULL) ? 1 : 0, len, y_offset, x_offset); + y_offset = y_offset + todo_lines; } + } /* Draw the scrollbar if necessary. */ - if (todos > max_items) - { - float ratio = ((float) max_items) / ((float) todos); - int sbar_length = (int) (ratio * (max_items + 1)); - int highend = (int) (ratio * first); - unsigned hilt_bar = (which_pan == TOD) ? 1 : 0; - int sbar_top = highend + title_lines; - - if ((sbar_top + sbar_length) > win[TOD].h - 1) - sbar_length = win[TOD].h - 1 - sbar_top; - draw_scrollbar (win[TOD].p, sbar_top, win[TOD].w - 2, - sbar_length, title_lines, win[TOD].h - 1, hilt_bar); - } - - wnoutrefresh (win[TOD].p); + if (todos > max_items) { + float ratio = ((float)max_items) / ((float)todos); + int sbar_length = (int)(ratio * (max_items + 1)); + int highend = (int)(ratio * first); + unsigned hilt_bar = (which_pan == TOD) ? 1 : 0; + int sbar_top = highend + title_lines; + + if ((sbar_top + sbar_length) > win[TOD].h - 1) + sbar_length = win[TOD].h - 1 - sbar_top; + draw_scrollbar(win[TOD].p, sbar_top, win[TOD].w - 2, + sbar_length, title_lines, win[TOD].h - 1, hilt_bar); + } + + wnoutrefresh(win[TOD].p); } /* Attach a note to a todo */ -void -todo_edit_note (char *editor) +void todo_edit_note(const char *editor) { - struct todo *i; - char fullname[BUFSIZ]; - char *filename; - - i = todo_get_item (hilt); - if (i->note == NULL) - { - if ((filename = new_tempfile (path_notes, NOTESIZ)) != NULL) - i->note = filename; - else - return; - } - (void)snprintf (fullname, BUFSIZ, "%s%s", path_notes, i->note); - wins_launch_external (fullname, editor); - - if (io_file_is_empty (fullname) > 0) - erase_note (&i->note, ERASE_FORCE); + struct todo *i = todo_get_item(hilt); + edit_note(&i->note, editor); } /* View a note previously attached to a todo */ -void -todo_view_note (char *pager) +void todo_view_note(const char *pager) { - struct todo *i; - char fullname[BUFSIZ]; + struct todo *i = todo_get_item(hilt); + view_note(i->note, pager); +} - i = todo_get_item (hilt); - if (i->note == NULL) +/* Pipe a todo item to an external program. */ +void todo_pipe_item(void) +{ + char cmd[BUFSIZ] = ""; + char const *arg[] = { cmd, NULL }; + int pout; + int pid; + FILE *fpout; + struct todo *todo; + + status_mesg(_("Pipe item to external command:"), ""); + if (getstring(win[STA].p, cmd, BUFSIZ, 0, 1) != GETSTRING_VALID) return; - (void)snprintf (fullname, BUFSIZ, "%s%s", path_notes, i->note); - wins_launch_external (fullname, pager); + + wins_prepare_external(); + if ((pid = shell_exec(NULL, &pout, *arg, arg))) { + fpout = fdopen(pout, "w"); + + todo = todo_get_item(hilt); + todo_write(todo, fpout); + + fclose(fpout); + child_wait(NULL, &pout, pid); + press_any_key(); + } + wins_unprepare_external(); } -void -todo_free (struct todo *todo) +static void todo_free(struct todo *todo) { - mem_free (todo->mesg); - erase_note (&todo->note, ERASE_FORCE_KEEP_NOTE); - mem_free (todo); + mem_free(todo->mesg); + erase_note(&todo->note); + mem_free(todo); } -void -todo_init_list (void) +void todo_init_list(void) { - LLIST_INIT (&todolist); + LLIST_INIT(&todolist); } -void -todo_free_list (void) +void todo_free_list(void) { - LLIST_FREE_INNER (&todolist, todo_free); - LLIST_FREE (&todolist); + LLIST_FREE_INNER(&todolist, todo_free); + LLIST_FREE(&todolist); } |