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
cmd_mark(int argc,char ** argv)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 = config->handler_context.container;
22 if (!container) {
23 return cmd_results_new(CMD_INVALID, "Only containers can have marks");
24 }
25
26 bool add = false, toggle = false;
27 while (argc > 0 && strncmp(*argv, "--", 2) == 0) {
28 if (strcmp(*argv, "--add") == 0) {
29 add = true;
30 } else if (strcmp(*argv, "--replace") == 0) {
31 add = false;
32 } else if (strcmp(*argv, "--toggle") == 0) {
33 toggle = true;
34 } else {
35 return cmd_results_new(CMD_INVALID,
36 "Unrecognized argument '%s'", *argv);
37 }
38 ++argv;
39 --argc;
40 }
41
42 if (!argc) {
43 return cmd_results_new(CMD_INVALID,
44 "Expected '[--add|--replace] [--toggle] <identifier>'");
45 }
46
47 char *mark = join_args(argv, argc);
48 bool had_mark = container_has_mark(container, mark);
49
50 if (!add) {
51 // Replacing
52 container_clear_marks(container);
53 }
54
55 container_find_and_unmark(mark);
56
57 if (!toggle || !had_mark) {
58 container_add_mark(container, mark);
59 }
60
61 free(mark);
62 container_update_marks_textures(container);
63 if (container->view) {
64 view_execute_criteria(container->view);
65 }
66
67 return cmd_results_new(CMD_SUCCESS, NULL);
68 }
69