diff options
author | 2018-05-14 22:47:10 +1000 | |
---|---|---|
committer | 2018-05-15 11:18:27 +1000 | |
commit | 22d38600d0edbb35029b3076c14e0e119dbf3dd2 (patch) | |
tree | ad548731ca215e2ec0f56cde86a6ff16df8cb323 /sway/commands | |
parent | Merge pull request #1978 from RedSoxFan/fix-1975 (diff) | |
download | sway-22d38600d0edbb35029b3076c14e0e119dbf3dd2.tar.gz sway-22d38600d0edbb35029b3076c14e0e119dbf3dd2.tar.zst sway-22d38600d0edbb35029b3076c14e0e119dbf3dd2.zip |
Implement marks
Diffstat (limited to 'sway/commands')
-rw-r--r-- | sway/commands/mark.c | 68 | ||||
-rw-r--r-- | sway/commands/unmark.c | 32 |
2 files changed, 100 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 | } | ||
diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c new file mode 100644 index 00000000..a7d39432 --- /dev/null +++ b/sway/commands/unmark.c | |||
@@ -0,0 +1,32 @@ | |||
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 | struct cmd_results *cmd_unmark(int argc, char **argv) { | ||
11 | if (argc == 0) { | ||
12 | // Remove all marks from the current container | ||
13 | struct sway_container *container = | ||
14 | config->handler_context.current_container; | ||
15 | if (container->type != C_VIEW) { | ||
16 | return cmd_results_new(CMD_INVALID, "unmark", | ||
17 | "Only views can have marks"); | ||
18 | } | ||
19 | view_clear_marks(container->sway_view); | ||
20 | } else { | ||
21 | // Remove a single mark from whichever container has it | ||
22 | char *mark = join_args(argv, argc); | ||
23 | if (!view_find_and_unmark(mark)) { | ||
24 | free(mark); | ||
25 | return cmd_results_new(CMD_INVALID, "unmark", | ||
26 | "No view exists with that mark"); | ||
27 | } | ||
28 | free(mark); | ||
29 | } | ||
30 | |||
31 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
32 | } | ||