diff options
author | Roosembert Palacios <roosembert.palacios@epfl.ch> | 2016-06-05 23:36:27 +0200 |
---|---|---|
committer | Roosembert Palacios <roosembert.palacios@epfl.ch> | 2016-06-05 23:36:27 +0200 |
commit | e8c0ef98b1c1068783350c76b44c4e40b7534137 (patch) | |
tree | c026688e9ff26b7ea5e591cc71631e89f580edbd | |
parent | Merge pull request #697 from zandrmartin/assign-command (diff) | |
download | sway-e8c0ef98b1c1068783350c76b44c4e40b7534137.tar.gz sway-e8c0ef98b1c1068783350c76b44c4e40b7534137.tar.zst sway-e8c0ef98b1c1068783350c76b44c4e40b7534137.zip |
Sway: Configuration: Support for escaping line breaks.
Escape line return in configuration file with the '\' character. Similar
to shell scripts.
Signed-off-by: Roosembert Palacios <roosembert.palacios@epfl.ch>
-rw-r--r-- | sway/config.c | 42 | ||||
-rw-r--r-- | sway/sway.5.txt | 6 |
2 files changed, 47 insertions, 1 deletions
diff --git a/sway/config.c b/sway/config.c index 15108123..c1eec22f 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -455,10 +455,11 @@ bool load_include_configs(const char *path, struct sway_config *config) { | |||
455 | 455 | ||
456 | bool read_config(FILE *file, struct sway_config *config) { | 456 | bool read_config(FILE *file, struct sway_config *config) { |
457 | bool success = true; | 457 | bool success = true; |
458 | bool multiline = false; | ||
458 | enum cmd_status block = CMD_BLOCK_END; | 459 | enum cmd_status block = CMD_BLOCK_END; |
459 | 460 | ||
460 | int line_number = 0; | 461 | int line_number = 0; |
461 | char *line; | 462 | char *line, *mlinebuf = NULL; |
462 | while (!feof(file)) { | 463 | while (!feof(file)) { |
463 | line = read_line(file); | 464 | line = read_line(file); |
464 | line_number++; | 465 | line_number++; |
@@ -467,6 +468,45 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
467 | free(line); | 468 | free(line); |
468 | continue; | 469 | continue; |
469 | } | 470 | } |
471 | size_t length = strlen(line); | ||
472 | if (line[length-1] == '\\') { | ||
473 | // Start of multiline | ||
474 | if (feof(file)){ | ||
475 | sway_log(L_ERROR, "Error on line %i '%s': Unexpected EOF on "\ | ||
476 | "multiline command", line_number, line); | ||
477 | free(line); | ||
478 | continue; | ||
479 | } | ||
480 | line[length-1] = '\0'; | ||
481 | multiline = true; | ||
482 | } else | ||
483 | multiline = false; | ||
484 | |||
485 | if (multiline || mlinebuf){ | ||
486 | size_t mlinebuf_length; | ||
487 | if (mlinebuf) | ||
488 | mlinebuf_length = strlen(mlinebuf); | ||
489 | else | ||
490 | mlinebuf_length = 0; | ||
491 | |||
492 | char *tmp = malloc(mlinebuf_length+length+1); // + '\0' | ||
493 | tmp[0]='\0'; // if mlinebuf_length==0 strncpy won't do anything. Make a null string. | ||
494 | strncpy(tmp, mlinebuf, mlinebuf_length); | ||
495 | tmp[mlinebuf_length]='\0'; // strncpy won't add '\0' at the end... | ||
496 | strcat(tmp, line); | ||
497 | if (mlinebuf) | ||
498 | free(mlinebuf); | ||
499 | free(line); | ||
500 | mlinebuf = tmp; | ||
501 | if (multiline) // The following line is part of a multi line config. | ||
502 | continue; | ||
503 | else { // This is the last line of a multi line config. | ||
504 | line = mlinebuf; | ||
505 | sway_log(L_INFO, "Processing parsed multiline command '%s'", line); | ||
506 | mlinebuf = NULL; | ||
507 | } | ||
508 | } | ||
509 | |||
470 | struct cmd_results *res = config_command(line, block); | 510 | struct cmd_results *res = config_command(line, block); |
471 | switch(res->status) { | 511 | switch(res->status) { |
472 | case CMD_FAILURE: | 512 | case CMD_FAILURE: |
diff --git a/sway/sway.5.txt b/sway/sway.5.txt index bd2de12d..397b6d87 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt | |||
@@ -16,6 +16,12 @@ on startup. These commands usually consist of setting your preferences and | |||
16 | setting key bindings. An example config is likely present in /etc/sway/config | 16 | setting key bindings. An example config is likely present in /etc/sway/config |
17 | for you to check out. | 17 | for you to check out. |
18 | 18 | ||
19 | Lines in the configuration file might be extended through multiple lines by | ||
20 | adding a '\' character at the end of line. e.g.: | ||
21 | |||
22 | bindsym Shift+XF86AudioRaiseVolume exec pactl set-sink-volume \ | ||
23 | $(pactl list sinks | grep -B 1 RUNNING | sed '1q;d' | sed 's/[^0-9]\+//g') +5% | ||
24 | |||
19 | These commands can be executed in your config file, via **sway-msg**(1), or via | 25 | These commands can be executed in your config file, via **sway-msg**(1), or via |
20 | the bindsym command. | 26 | the bindsym command. |
21 | 27 | ||