1 /* vifm
2 * Copyright (C) 2001 Ken Steen.
3 * Copyright (C) 2011 xaizek.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "undolist_menu.h"
21
22 #include <stddef.h> /* size_t */
23 #include <stdlib.h> /* realloc() */
24 #include <string.h> /* memmove() strdup() strlen() */
25 #include <wchar.h> /* wcscmp() */
26
27 #include "../ui/ui.h"
28 #include "../utils/string_array.h"
29 #include "../undo.h"
30 #include "menus.h"
31
32 static KHandlerResponse undolist_khandler(view_t *view, menu_data_t *m,
33 const wchar_t keys[]);
34 static void set_mark(menu_data_t *m, int pos);
35 static void unset_mark(menu_data_t *m, int pos);
36
37 int
show_undolist_menu(view_t * view,int with_details)38 show_undolist_menu(view_t *view, int with_details)
39 {
40 static menu_data_t m;
41 menus_init_data(&m, view, strdup("Undolist"), strdup("Undolist is empty"));
42 m.key_handler = &undolist_khandler;
43 m.extra_data = with_details;
44
45 m.items = un_get_list(with_details);
46 m.len = count_strings(m.items);
47
48 /* Add additional entry before setting position. */
49 if(m.len > 0)
50 {
51 m.len = add_to_string_array(&m.items, m.len, " <<< list end >>>");
52 }
53
54 menus_set_pos(m.state, un_get_list_pos(with_details));
55 set_mark(&m, m.pos);
56
57 return menus_enter(m.state, view);
58 }
59
60 /* Menu-specific shortcut handler. Returns code that specifies both taken
61 * actions and what should be done next. */
62 static KHandlerResponse
undolist_khandler(view_t * view,menu_data_t * m,const wchar_t keys[])63 undolist_khandler(view_t *view, menu_data_t *m, const wchar_t keys[])
64 {
65 if(wcscmp(keys, L"r") == 0)
66 {
67 const int with_details = m->extra_data;
68
69 unset_mark(m, un_get_list_pos(with_details));
70
71 un_set_pos(m->pos, with_details);
72 set_mark(m, un_get_list_pos(with_details));
73
74 menus_partial_redraw(m->state);
75 return KHR_REFRESH_WINDOW;
76 }
77 return KHR_UNHANDLED;
78 }
79
80 /* Adds current position mark to a menu item. */
81 static void
set_mark(menu_data_t * m,int pos)82 set_mark(menu_data_t *m, int pos)
83 {
84 if(m->len != 0 && m->items[pos] != NULL)
85 {
86 m->items[pos][0] = '*';
87 }
88 }
89
90 /* Removes current position mark from a menu item. */
91 static void
unset_mark(menu_data_t * m,int pos)92 unset_mark(menu_data_t *m, int pos)
93 {
94 if(m->len != 0 && m->items[pos] != NULL)
95 {
96 m->items[pos][0] = ' ';
97 }
98 }
99
100 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
101 /* vim: set cinoptions+=t0 filetype=c : */
102