summaryrefslogtreecommitdiffstats
path: root/sway/commands
diff options
context:
space:
mode:
authorLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-02 14:38:33 -0600
committerLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-03 11:48:37 -0600
commit2445d279604d7be38c00db60ffde4279a3c75459 (patch)
tree030844d9fb13be7814dd760a6ffedbec8f5264cd /sway/commands
parentUpdate man page (diff)
downloadsway-2445d279604d7be38c00db60ffde4279a3c75459.tar.gz
sway-2445d279604d7be38c00db60ffde4279a3c75459.tar.zst
sway-2445d279604d7be38c00db60ffde4279a3c75459.zip
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
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/mark.c74
-rw-r--r--sway/commands/show_marks.c13
-rw-r--r--sway/commands/unmark.c31
3 files changed, 118 insertions, 0 deletions
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 @@
1#include <string.h>
2#include <strings.h>
3#include <stdbool.h>
4#include "sway/commands.h"
5#include "list.h"
6#include "stringop.h"
7
8struct cmd_results *cmd_mark(int argc, char **argv) {
9 struct cmd_results *error = NULL;
10 if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file.");
11 if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) {
12 return error;
13 }
14
15 swayc_t *view = get_focused_container(&root_container);
16 bool add = false;
17 bool toggle = false;
18
19 if (strcmp(argv[0], "--add") == 0) {
20 --argc; ++argv;
21 add = true;
22 } else if (strcmp(argv[0], "--replace") == 0) {
23 --argc; ++argv;
24 }
25
26 if (argc && strcmp(argv[0], "--toggle") == 0) {
27 --argc; ++argv;
28 toggle = true;
29 }
30
31 if (argc) {
32 char *mark = join_args(argv, argc);
33 if (view->marks) {
34 if (add) {
35 int index;
36 if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) {
37 if (toggle) {
38 free(view->marks->items[index]);
39 list_del(view->marks, index);
40
41 if (0 == view->marks->length) {
42 list_free(view->marks);
43 view->marks = NULL;
44 }
45 }
46 free(mark);
47 } else {
48 list_add(view->marks, mark);
49 }
50 } else {
51 if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) {
52 // Delete the list
53 list_foreach(view->marks, free);
54 list_free(view->marks);
55 view->marks = NULL;
56 } else {
57 // Delete and replace with a new list
58 list_foreach(view->marks, free);
59 list_free(view->marks);
60
61 view->marks = create_list();
62 list_add(view->marks, mark);
63 }
64 }
65 } else {
66 view->marks = create_list();
67 list_add(view->marks, mark);
68 }
69 } else {
70 return cmd_results_new(CMD_FAILURE, "mark",
71 "Expected 'mark [--add|--replace] [--toggle] <mark>'");
72 }
73 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
74}
diff --git a/sway/commands/show_marks.c b/sway/commands/show_marks.c
new file mode 100644
index 00000000..ed56d9e5
--- /dev/null
+++ b/sway/commands/show_marks.c
@@ -0,0 +1,13 @@
1#include <string.h>
2#include <strings.h>
3#include "sway/commands.h"
4
5struct cmd_results *cmd_show_marks(int argc, char **argv) {
6 struct cmd_results *error = NULL;
7 if ((error = checkarg(argc, "show_marks", EXPECTED_EQUAL_TO, 1))) {
8 return error;
9 }
10
11 config->show_marks = !strcasecmp(argv[0], "on");
12 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
13}
diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c
new file mode 100644
index 00000000..34a2ae44
--- /dev/null
+++ b/sway/commands/unmark.c
@@ -0,0 +1,31 @@
1#include <string.h>
2#include <strings.h>
3#include "sway/commands.h"
4#include "list.h"
5#include "stringop.h"
6
7struct cmd_results *cmd_unmark(int argc, char **argv) {
8 swayc_t *view = get_focused_container(&root_container);
9
10 if (view->marks) {
11 if (argc) {
12 char *mark = join_args(argv, argc);
13 int index;
14 if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) {
15 free(view->marks->items[index]);
16 list_del(view->marks, index);
17
18 if (view->marks->length == 0) {
19 list_free(view->marks);
20 view->marks = NULL;
21 }
22 }
23 free(mark);
24 } else {
25 list_foreach(view->marks, free);
26 list_free(view->marks);
27 view->marks = NULL;
28 }
29 }
30 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
31}