From: Jaakko H. <jhe...@us...> - 2005-10-26 19:29:26
|
Update of /cvsroot/abook/abook In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8245 Modified Files: database.c database.h edit.c filter.c list.c list.h ui.c Log Message: - don't use items variable outside of database.c - curitem is now directly used only from database.c and list.c Index: database.c =================================================================== RCS file: /cvsroot/abook/abook/database.c,v retrieving revision 1.31 retrieving revision 1.32 diff -u -d -r1.31 -r1.32 --- database.c 26 Oct 2005 16:38:35 -0000 1.31 +++ database.c 26 Oct 2005 19:29:16 -0000 1.32 @@ -26,9 +26,10 @@ int fields_count = 0; list_item *database = NULL; -int items = 0; +static int items = 0; #define ITEM_SIZE (fields_count * sizeof(char *)) +#define LAST_ITEM (items - 1) #define INITIAL_LIST_CAPACITY 30 static int list_capacity = 0; @@ -660,6 +661,17 @@ return item <= LAST_ITEM && item >= 0; } +int +last_item() +{ + return LAST_ITEM; +} + +int +db_n_items() +{ + return items; +} int real_db_enumerate_items(struct db_enumerator e) Index: ui.c =================================================================== RCS file: /cvsroot/abook/abook/ui.c,v retrieving revision 1.55 retrieving revision 1.56 diff -u -d -r1.55 -r1.56 --- ui.c 25 Oct 2005 23:27:24 -0000 1.55 +++ ui.c 26 Oct 2005 19:29:17 -0000 1.56 @@ -38,8 +38,6 @@ * external variables */ -extern int curitem; -extern int items; extern char *datafile; extern bool alternative_datafile; @@ -434,7 +432,6 @@ */ extern char *selected; -extern int curitem; void get_commands() @@ -455,7 +452,7 @@ case 'q': return; case 'Q': quit_abook(QUIT_DONTSAVE); break; case 'P': print_stderr(selected_items() ? - -1 : list_current_item()); + -1 : list_get_curitem()); return; case '?': display_help(HELP_MAIN); @@ -499,8 +496,8 @@ case '/': ui_find(0); break; case '\\': ui_find(1); break; - case ' ': if(curitem >= 0) { - selected[curitem] = !selected[curitem]; + case ' ': if(list_get_curitem() >= 0) { + list_invert_curitem_selection(); ui_print_number_of_items(); refresh_list(); } @@ -520,13 +517,13 @@ break; case 'm': launch_mutt(selected_items() ? - -1 : list_current_item()); + -1 : list_get_curitem()); refresh_screen(); break; case 'p': ui_print_database(); break; - case 'v': launch_wwwbrowser(list_current_item()); + case 'v': launch_wwwbrowser(list_get_curitem()); refresh_screen(); break; } @@ -575,12 +572,13 @@ refresh_screen(); } - if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 && + if( (item = find_item(findstr, list_get_curitem() + !!next, + search_fields)) < 0 && (item = find_item(findstr, 0, search_fields)) >= 0) statusline_addstr(_("Search hit bottom, continuing at top")); if(item >= 0) { - curitem = item; + list_set_curitem(item); refresh_list(); } } @@ -588,7 +586,8 @@ void ui_print_number_of_items() { - char *str = strdup_printf(" " "|%3d/%3d", selected_items(), items); + char *str = strdup_printf(" " "|%3d/%3d", + selected_items(), db_n_items()); mvaddstr(0, COLS-strlen(str), str); @@ -600,7 +599,7 @@ { char *msg; - if(items > 0) { + if(!list_is_empty()) { msg = strdup_printf(_("Your current data will be lost - " "Press '%c' to continue"), *(S_("keybinding for yes|y"))); @@ -675,7 +674,7 @@ load_database(filename); - if(items == 0) { + if(list_is_empty()) { statusline_msg(_("Sorry, the specified file appears not to be a valid abook addressbook")); load_database(datafile); } else { Index: list.c =================================================================== RCS file: /cvsroot/abook/abook/list.c,v retrieving revision 1.28 retrieving revision 1.29 diff -u -d -r1.28 -r1.29 --- list.c 25 Oct 2005 23:27:24 -0000 1.28 +++ list.c 26 Oct 2005 19:29:16 -0000 1.29 @@ -28,7 +28,6 @@ int extra_column = -1; int extra_alternative = -1; -extern int items; extern abook_field_list *fields_list; static WINDOW *list = NULL; @@ -99,8 +98,9 @@ else if(curitem > LAST_LIST_ITEM) first_list_item = max(curitem - LIST_LINES + 1, 0); - for( line = 0, i = first_list_item ; i <= LAST_LIST_ITEM && i < items; - line++, i++ ) { + for(line = 0, i = first_list_item; + i <= LAST_LIST_ITEM && i < db_n_items(); + line++, i++) { print_list_line(i, line, i == curitem); } @@ -188,7 +188,7 @@ void scroll_down() { - if(curitem > items - 2) + if(curitem > db_n_items() - 2) return; curitem++; @@ -212,12 +212,15 @@ void page_down() { - if(curitem > items - 2) + if(curitem > db_n_items() - 2) return; - curitem = curitem == LAST_LIST_ITEM ? - ((curitem += LIST_LINES) > LAST_ITEM ? LAST_ITEM : curitem) : - min(LAST_LIST_ITEM, LAST_ITEM); + if(curitem == LAST_LIST_ITEM) { + if((curitem += LIST_LINES) > last_item()) + curitem = last_item(); + } else { + curitem = min(LAST_LIST_ITEM, last_item()); + } refresh_list(); } @@ -225,13 +228,29 @@ void select_none() { - memset(selected, 0, items); + memset(selected, 0, db_n_items()); } void select_all() { - memset(selected, 1, items); + memset(selected, 1, db_n_items()); +} + +void +list_set_selection(int item, int value) +{ + assert(is_valid_item(item)); + + selected[item] = !!value; +} + +void +list_invert_curitem_selection() +{ + assert(is_valid_item(curitem)); + + selected[curitem] = !selected[curitem]; } void @@ -239,7 +258,7 @@ { list_item tmp; - if( curitem < 0 || curitem > LAST_ITEM ) + if(curitem < 0 || curitem > last_item()) return; tmp = item_create(); @@ -256,7 +275,7 @@ break; case MOVE_ITEM_DOWN: - if( curitem >= LAST_ITEM ) + if(curitem >= last_item()) goto out_move; item_copy(db_item_get(curitem), db_item_get(curitem + 1)); @@ -272,7 +291,7 @@ void goto_home() { - if(items > 0) + if(db_n_items() > 0) curitem = 0; refresh_list(); @@ -281,8 +300,8 @@ void goto_end() { - if(items > 0) - curitem = LAST_ITEM; + if(db_n_items() > 0) + curitem = last_item(); refresh_list(); } @@ -319,7 +338,7 @@ { int i, n = 0; - for(i = 0; i < items; i++) + for(i = 0; i < db_n_items(); i++) if(selected[i]) n++; @@ -331,23 +350,29 @@ { int i; - if(items < 1) + if(list_is_empty()) return; - for(i = 0; i < items; i++) + for(i = 0; i < db_n_items(); i++) selected[i] = !selected[i]; } int -list_current_item() +list_is_empty() { - return curitem; + return db_n_items() < 1; } int -list_is_empty() +list_get_curitem() { - return items < 1; + return curitem; +} + +void +list_set_curitem(int i) +{ + curitem = i; } int @@ -366,7 +391,7 @@ } item_free(&item); - curitem = LAST_ITEM; + curitem = last_item(); refresh_list(); return 0; Index: database.h =================================================================== RCS file: /cvsroot/abook/abook/database.h,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- database.h 25 Oct 2005 23:27:24 -0000 1.12 +++ database.h 26 Oct 2005 19:29:16 -0000 1.13 @@ -88,6 +88,8 @@ int find_item(char *str, int start, int search_fields[]); int is_selected(int item); int is_valid_item(int item); +int last_item(); +int db_n_items(); int real_db_enumerate_items(struct db_enumerator e); struct db_enumerator init_db_enumerator(int mode); @@ -134,8 +136,6 @@ * Various macros */ -#define LAST_ITEM (items - 1) - #define have_multiple_emails(item) \ strchr(db_email_get(item), ',') Index: list.h =================================================================== RCS file: /cvsroot/abook/abook/list.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- list.h 25 Oct 2005 23:27:24 -0000 1.7 +++ list.h 26 Oct 2005 19:29:16 -0000 1.8 @@ -15,14 +15,17 @@ void page_down(); void select_none(); void select_all(); +void set_selection(int item, int value); +void list_invert_curitem_selection(); void move_curitem(int direction); void goto_home(); void goto_end(); void highlight_line(WINDOW *win, int line); int selected_items(); void invert_selection(); -int list_current_item(); int list_is_empty(); +int list_get_curitem(); +void list_set_curitem(int i); int duplicate_item(); @@ -45,6 +48,6 @@ #define EMAILLEN (EXTRAPOS - EMAILPOS - 1) #define EXTRALEN (COLS - EXTRAPOS) -#define LAST_LIST_ITEM ( first_list_item + LIST_LINES - 1 ) +#define LAST_LIST_ITEM (first_list_item + LIST_LINES - 1) #endif Index: filter.c =================================================================== RCS file: /cvsroot/abook/abook/filter.c,v retrieving revision 1.49 retrieving revision 1.50 diff -u -d -r1.49 -r1.50 --- filter.c 25 Oct 2005 23:27:24 -0000 1.49 +++ filter.c 26 Oct 2005 19:29:16 -0000 1.50 @@ -27,7 +27,6 @@ #include "xmalloc.h" #include <assert.h> -extern int items; extern abook_field_list *fields_list; extern int fields_count; @@ -194,7 +193,7 @@ { int filter; char *filename; - int tmp = items; + int tmp = db_n_items(); import_screen(); @@ -215,7 +214,7 @@ if(i_read_file(filename, i_filters[filter].func )) statusline_msg(_("Error occured while opening the file")); - else if(tmp == items) + else if(tmp == db_n_items()) statusline_msg(_("File does not seem to be a valid addressbook")); refresh_screen(); @@ -246,7 +245,7 @@ import_file(char filtname[FILTNAME_LEN], char *filename) { int i; - int tmp = items; + int tmp = db_n_items(); int ret = 0; for(i=0;; i++) { @@ -271,7 +270,7 @@ } else ret = i_read_file(filename, i_filters[i].func); - if(tmp == items) + if(tmp == db_n_items()) ret = 1; return ret; @@ -842,7 +841,7 @@ char tmp[MAX_EMAILSTR_LEN]; int extra_column; - if(items < 1) + if(list_is_empty()) return 2; extra_column = init_extra_field(STR_EXTRA_COLUMN); Index: edit.c =================================================================== RCS file: /cvsroot/abook/abook/edit.c,v retrieving revision 1.45 retrieving revision 1.46 diff -u -d -r1.45 -r1.46 --- edit.c 26 Oct 2005 15:28:57 -0000 1.45 +++ edit.c 26 Oct 2005 19:29:16 -0000 1.46 @@ -29,10 +29,7 @@ * some extern variables */ - -extern int curitem; extern int views_count; -extern int items; WINDOW *editw; @@ -510,17 +507,17 @@ void edit_item(int item) { - if( item < 0 ) { - if( curitem < 0 ) + if(item < 0) { + if(list_get_curitem() < 0) return; else - item = curitem; + item = list_get_curitem(); } init_editor(); while((item = edit_loop(item)) >= 0) - curitem = item; /* hmm, this is not very clean way to go */ + list_set_curitem(item); /* this is not very clean way to go */ close_editor(); } @@ -541,8 +538,8 @@ add_item2database(item); item_free(&item); - curitem = LAST_ITEM; + list_set_curitem(last_item()); - edit_item(LAST_ITEM); + edit_item(last_item()); } |