From 2445d279604d7be38c00db60ffde4279a3c75459 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Sun, 2 Apr 2017 14:38:33 -0600 Subject: Impliment i3-style marks This commit adds three commands to sway: `show_marks`, `mark` and `unmark`. Marks are displayed right-aligned in the window border as i3 does. Marks may be found using criteria. Fixes #1007 --- sway/commands/mark.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 sway/commands/mark.c (limited to 'sway/commands/mark.c') diff --git a/sway/commands/mark.c b/sway/commands/mark.c new file mode 100644 index 00000000..68a84af7 --- /dev/null +++ b/sway/commands/mark.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include "sway/commands.h" +#include "list.h" +#include "stringop.h" + +struct cmd_results *cmd_mark(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); + if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) { + return error; + } + + swayc_t *view = get_focused_container(&root_container); + bool add = false; + bool toggle = false; + + if (strcmp(argv[0], "--add") == 0) { + --argc; ++argv; + add = true; + } else if (strcmp(argv[0], "--replace") == 0) { + --argc; ++argv; + } + + if (argc && strcmp(argv[0], "--toggle") == 0) { + --argc; ++argv; + toggle = true; + } + + if (argc) { + char *mark = join_args(argv, argc); + if (view->marks) { + if (add) { + int index; + if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + if (toggle) { + free(view->marks->items[index]); + list_del(view->marks, index); + + if (0 == view->marks->length) { + list_free(view->marks); + view->marks = NULL; + } + } + free(mark); + } else { + list_add(view->marks, mark); + } + } else { + if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) { + // Delete the list + list_foreach(view->marks, free); + list_free(view->marks); + view->marks = NULL; + } else { + // Delete and replace with a new list + list_foreach(view->marks, free); + list_free(view->marks); + + view->marks = create_list(); + list_add(view->marks, mark); + } + } + } else { + view->marks = create_list(); + list_add(view->marks, mark); + } + } else { + return cmd_results_new(CMD_FAILURE, "mark", + "Expected 'mark [--add|--replace] [--toggle] '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} -- cgit v1.2.3 From 069d37f987c4e323cdb9396f0d80ac83d00566ff Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Tue, 4 Apr 2017 21:20:27 -0600 Subject: Improve criteria handling This commit changes how commands decide what container to act on. Commands get the current container though `current_container`, a global defined in sway/commands.c. If a criteria is given before a command, then the following command will be run once for every container the criteria matches with a reference to the matching container in 'current_container'. Commands should use this instead of `get_focused_container()` from now on. This commit also fixes a few (minor) mistakes made in implementing marks such as non-escaped arrows in sway(5) and calling the "mark" command "floating" by accident. It also cleans up `criteria.c` in a few places. --- sway/commands/mark.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sway/commands/mark.c') diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 68a84af7..919883b0 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -8,11 +8,11 @@ struct cmd_results *cmd_mark(int argc, char **argv) { struct cmd_results *error = NULL; if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); - if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { return error; } - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; bool add = false; bool toggle = false; -- cgit v1.2.3 From 154c6718c1f0e34e0f217150ba2770ee100e5b38 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Fri, 7 Apr 2017 11:37:51 -0600 Subject: Add `-t get_marks` and use more i3-like marks In i3 every mark is unique and one mark cannot be used in more than one window, sway behavior has been amended to match this. `swaymsg -t get_marks` will now return an array of all marks used in sway. See #98 --- sway/commands/mark.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'sway/commands/mark.c') diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 919883b0..c1d959df 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -5,6 +5,15 @@ #include "list.h" #include "stringop.h" +static void find_marks_callback(swayc_t *container, void *_mark) { + char *mark = (char *)_mark; + + int index; + if (container->marks && ((index = list_seq_find(container->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1)) { + list_del(container->marks, index); + } +} + struct cmd_results *cmd_mark(int argc, char **argv) { struct cmd_results *error = NULL; if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); @@ -30,6 +39,10 @@ struct cmd_results *cmd_mark(int argc, char **argv) { if (argc) { char *mark = join_args(argv, argc); + + // Remove all existing marks of this type + container_map(&root_container, find_marks_callback, mark); + if (view->marks) { if (add) { int index; -- cgit v1.2.3