diff options
Diffstat (limited to 'sway/commands/mark.c')
-rw-r--r-- | sway/commands/mark.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/sway/commands/mark.c b/sway/commands/mark.c new file mode 100644 index 00000000..782e8ab9 --- /dev/null +++ b/sway/commands/mark.c | |||
@@ -0,0 +1,68 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <string.h> | ||
3 | #include "sway/commands.h" | ||
4 | #include "sway/config.h" | ||
5 | #include "sway/tree/view.h" | ||
6 | #include "list.h" | ||
7 | #include "log.h" | ||
8 | #include "stringop.h" | ||
9 | |||
10 | // mark foo Same as mark --replace foo | ||
11 | // mark --add foo Add this mark to view's list | ||
12 | // mark --replace foo Replace view's marks with this single one | ||
13 | // mark --add --toggle foo Toggle current mark and persist other marks | ||
14 | // mark --replace --toggle foo Toggle current mark and remove other marks | ||
15 | |||
16 | struct cmd_results *cmd_mark(int argc, char **argv) { | ||
17 | struct cmd_results *error = NULL; | ||
18 | if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { | ||
19 | return error; | ||
20 | } | ||
21 | struct sway_container *container = | ||
22 | config->handler_context.current_container; | ||
23 | if (container->type != C_VIEW) { | ||
24 | return cmd_results_new(CMD_INVALID, "mark", | ||
25 | "Only views can have marks"); | ||
26 | } | ||
27 | struct sway_view *view = container->sway_view; | ||
28 | |||
29 | bool add = false, toggle = false; | ||
30 | while (argc > 0 && strncmp(*argv, "--", 2) == 0) { | ||
31 | if (strcmp(*argv, "--add") == 0) { | ||
32 | add = true; | ||
33 | } else if (strcmp(*argv, "--replace") == 0) { | ||
34 | add = false; | ||
35 | } else if (strcmp(*argv, "--toggle") == 0) { | ||
36 | toggle = true; | ||
37 | } else { | ||
38 | return cmd_results_new(CMD_INVALID, "mark", | ||
39 | "Unrecognized argument '%s'", *argv); | ||
40 | } | ||
41 | ++argv; | ||
42 | --argc; | ||
43 | } | ||
44 | |||
45 | if (!argc) { | ||
46 | return cmd_results_new(CMD_INVALID, "mark", | ||
47 | "Expected '[--add|--replace] [--toggle] <identifier>'"); | ||
48 | } | ||
49 | |||
50 | char *mark = join_args(argv, argc); | ||
51 | bool had_mark = view_has_mark(view, mark); | ||
52 | |||
53 | if (!add) { | ||
54 | // Replacing | ||
55 | view_clear_marks(view); | ||
56 | } | ||
57 | |||
58 | view_find_and_unmark(mark); | ||
59 | |||
60 | if (!toggle || (toggle && !had_mark)) { | ||
61 | list_add(view->marks, strdup(mark)); | ||
62 | } | ||
63 | |||
64 | free(mark); | ||
65 | view_execute_criteria(view); | ||
66 | |||
67 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
68 | } | ||