aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-08-16 19:08:54 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-08-16 19:08:54 +0100
commitc6ce270ee1e54fc7ae9c4029923bccedbcac5ad2 (patch)
tree4a5158d82b0a566edab9d94ad49a2b3885fa1a0a
parent505340f9fdd7e8d8356c25035b254c8f9ee367a6 (diff)
downloadimv-c6ce270ee1e54fc7ae9c4029923bccedbcac5ad2.tar.gz
imv: Add index/all argument to close command
-rw-r--r--doc/imv.1.txt5
-rw-r--r--src/imv.c17
-rw-r--r--src/list.c5
-rw-r--r--src/list.h2
-rw-r--r--src/navigator.c38
-rw-r--r--src/navigator.h7
6 files changed, 69 insertions, 5 deletions
diff --git a/doc/imv.1.txt b/doc/imv.1.txt
index 98b4498..eb00c62 100644
--- a/doc/imv.1.txt
+++ b/doc/imv.1.txt
@@ -98,8 +98,9 @@ Commands can be entered by pressing *:*. imv supports the following commands:
specified, do so recursively. Shell expansions may be used.
Aliased to 'o'.
-*close*::
- Close the currently selected image.
+*close* [index|'all']::
+ Close the currently selected image, or the image at the given index, or
+ all images.
*fullscreen*::
Toggle fullscreen.
diff --git a/src/imv.c b/src/imv.c
index 9dbc7d5..991de70 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -1536,9 +1536,20 @@ static void command_close(struct list *args, const char *argstr, void *data)
(void)args;
(void)argstr;
struct imv *imv = data;
- char* path = strdup(imv_navigator_selection(imv->navigator));
- imv_navigator_remove(imv->navigator, path);
- free(path);
+ size_t index = imv_navigator_index(imv->navigator);
+
+ if (args->len == 2) {
+ const char *arg = args->items[1];
+ if (!strcmp("all", arg)) {
+ imv_navigator_remove_all(imv->navigator);
+ imv->slideshow.elapsed = 0;
+ return;
+ }
+
+ index = (size_t)strtol(arg, NULL, 10) - 1;
+ }
+
+ imv_navigator_remove_at(imv->navigator, index);
imv->slideshow.elapsed = 0;
}
diff --git a/src/list.c b/src/list.c
index b719cf0..ba5ce87 100644
--- a/src/list.c
+++ b/src/list.c
@@ -66,6 +66,11 @@ void list_insert(struct list *list, size_t index, void *item)
list->len += 1;
}
+void list_clear(struct list *list)
+{
+ list->len = 0;
+}
+
struct list *list_from_string(const char *string, char delim)
{
struct list *list = list_create();
diff --git a/src/list.h b/src/list.h
index bb88c2c..abb8b85 100644
--- a/src/list.h
+++ b/src/list.h
@@ -24,6 +24,8 @@ void list_remove(struct list *list, size_t index);
void list_insert(struct list *list, size_t index, void *item);
+void list_clear(struct list *list);
+
struct list *list_from_string(const char *string, char delim);
int list_find(
diff --git a/src/navigator.c b/src/navigator.c
index 98c1976..6b77643 100644
--- a/src/navigator.c
+++ b/src/navigator.c
@@ -215,6 +215,44 @@ void imv_navigator_remove(struct imv_navigator *nav, const char *path)
nav->changed = 1;
}
+void imv_navigator_remove_at(struct imv_navigator *nav, size_t index)
+{
+ if (index >= nav->paths->len) {
+ return;
+ }
+ struct nav_item *item = nav->paths->items[index];
+ free(item->path);
+ free(item);
+ list_remove(nav->paths, index);
+
+ if (nav->cur_path == index) {
+ /* We just removed the current path */
+ if (nav->last_move_direction < 0) {
+ /* Move left */
+ imv_navigator_select_rel(nav, -1);
+ } else {
+ /* Try to stay where we are, unless we ran out of room */
+ if (nav->cur_path == nav->paths->len) {
+ nav->cur_path = 0;
+ nav->wrapped = 1;
+ }
+ }
+ }
+ nav->changed = 1;
+}
+
+void imv_navigator_remove_all(struct imv_navigator *nav)
+{
+ for (size_t i = 0; i < nav->paths->len; ++i) {
+ struct nav_item *item = nav->paths->items[i];
+ free(item->path);
+ free(item);
+ }
+ list_clear(nav->paths);
+ nav->cur_path = 0;
+ nav->changed = 1;
+}
+
ssize_t imv_navigator_find_path(struct imv_navigator *nav, const char *path)
{
/* first try to match the exact path */
diff --git a/src/navigator.h b/src/navigator.h
index 06b5c92..6234206 100644
--- a/src/navigator.h
+++ b/src/navigator.h
@@ -34,6 +34,13 @@ void imv_navigator_select_abs(struct imv_navigator *nav, ssize_t index);
* based on the last direction the selection moved. */
void imv_navigator_remove(struct imv_navigator *nav, const char *path);
+/* Removes the given index. The current selection is updated if necessary,
+ * based on the last direction the selection moved. */
+void imv_navigator_remove_at(struct imv_navigator *nav, size_t index);
+
+/* Removes all paths */
+void imv_navigator_remove_all(struct imv_navigator *nav);
+
/* Return the index of the path given. Returns -1 if not found. */
ssize_t imv_navigator_find_path(struct imv_navigator *nav, const char *path);