mirror of
				https://github.com/swaywm/sway.git
				synced 2025-10-29 05:40:18 -04:00 
			
		
		
		
	set variable changes
This commit is contained in:
		
							parent
							
								
									936e4ca288
								
							
						
					
					
						commit
						d673a72705
					
				
					 5 changed files with 75 additions and 39 deletions
				
			
		|  | @ -13,5 +13,7 @@ void list_add(list_t *list, void *item); | ||||||
| void list_insert(list_t *list, int index, void *item); | void list_insert(list_t *list, int index, void *item); | ||||||
| void list_del(list_t *list, int index); | void list_del(list_t *list, int index); | ||||||
| void list_cat(list_t *list, list_t *source); | void list_cat(list_t *list, list_t *source); | ||||||
|  | // See qsort
 | ||||||
|  | void list_sort(list_t *list, int compare(const void *left, const void *right)); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | @ -85,6 +85,11 @@ Commands | ||||||
| 	Sets the layout mode of the focused container. _mode_ can be one of _splith_, | 	Sets the layout mode of the focused container. _mode_ can be one of _splith_, | ||||||
| 	_splitv_, or _toggle split_. | 	_splitv_, or _toggle split_. | ||||||
| 
 | 
 | ||||||
|  | **mode** <mode_name>:: | ||||||
|  | 	Switches to the given mode_name. the default mode is simply _default_. To | ||||||
|  | 	create a new mode in config append _{_ to this command, the following lines | ||||||
|  | 	will be keybinds for that mode, and _}_ on its own line to close the block. | ||||||
|  | 
 | ||||||
| **move** <left|right|up|down>:: | **move** <left|right|up|down>:: | ||||||
| 	Moves the focused container _left_, _right_, _up_, or _down_. | 	Moves the focused container _left_, _right_, _up_, or _down_. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -139,8 +139,7 @@ static bool cmd_bindsym(int argc, char **argv) { | ||||||
| 	// TODO: Check if there are other commands with this key binding
 | 	// TODO: Check if there are other commands with this key binding
 | ||||||
| 	struct sway_mode *mode = config->current_mode; | 	struct sway_mode *mode = config->current_mode; | ||||||
| 	list_add(mode->bindings, binding); | 	list_add(mode->bindings, binding); | ||||||
| 	qsort(mode->bindings->items, mode->bindings->length, | 	list_sort(mode->bindings, bindsym_sort); | ||||||
| 			sizeof(mode->bindings->items[0]), bindsym_sort); |  | ||||||
| 
 | 
 | ||||||
| 	sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); | 	sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command); | ||||||
| 	return true; | 	return true; | ||||||
|  | @ -828,14 +827,36 @@ static bool cmd_scratchpad(int argc, char **argv) { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // sort in order of longest->shortest
 | ||||||
|  | static int compare_set(const void *_l, const void *_r) { | ||||||
|  | 	struct sway_variable * const *l = _l; | ||||||
|  | 	struct sway_variable * const *r = _r; | ||||||
|  | 	return strlen((*r)->name) - strlen((*l)->name); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static bool cmd_set(int argc, char **argv) { | static bool cmd_set(int argc, char **argv) { | ||||||
| 	if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { | 	if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	struct sway_variable *var = malloc(sizeof(struct sway_variable)); | 	struct sway_variable *var = NULL; | ||||||
|  | 	// Find old variable if it exists
 | ||||||
|  | 	int i; | ||||||
|  | 	for (i = 0; i < config->symbols->length; ++i) { | ||||||
|  | 		var = config->symbols->items[i]; | ||||||
|  | 		if (strcmp(var->name, argv[0]) == 0) { | ||||||
|  | 			break; | ||||||
|  | 		} | ||||||
|  | 		var = NULL; | ||||||
|  | 	} | ||||||
|  | 	if (var) { | ||||||
|  | 		free(var->value); | ||||||
|  | 	} else { | ||||||
|  | 		var = malloc(sizeof(struct sway_variable)); | ||||||
| 		var->name = strdup(argv[0]); | 		var->name = strdup(argv[0]); | ||||||
| 	var->value = strdup(argv[1]); |  | ||||||
| 		list_add(config->symbols, var); | 		list_add(config->symbols, var); | ||||||
|  | 		list_sort(config->symbols, compare_set); | ||||||
|  | 	} | ||||||
|  | 	var->value = strdup(argv[1]); | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -1048,7 +1069,8 @@ bool handle_command(char *exec) { | ||||||
| 	bool exec_success = false; | 	bool exec_success = false; | ||||||
| 	if (handler) { | 	if (handler) { | ||||||
| 		int i; | 		int i; | ||||||
| 		for (i = 1; i < argc; ++i) { | 		// Skip var replacement for first value of cmd_set
 | ||||||
|  | 		for (i = (handler->handle == cmd_set ? 2 : 1); i < argc; ++i) { | ||||||
| 			argv[i] = do_var_replacement(argv[i]); | 			argv[i] = do_var_replacement(argv[i]); | ||||||
| 		} | 		} | ||||||
| 		exec_success = handler->handle(argc - 1, argv + 1); | 		exec_success = handler->handle(argc - 1, argv + 1); | ||||||
|  |  | ||||||
|  | @ -230,19 +230,17 @@ bool read_config(FILE *file, bool is_active) { | ||||||
| 	char *line; | 	char *line; | ||||||
| 	while (!feof(file)) { | 	while (!feof(file)) { | ||||||
| 		line = read_line(file); | 		line = read_line(file); | ||||||
| 		line = strip_whitespace(line); |  | ||||||
| 		line = strip_comments(line); | 		line = strip_comments(line); | ||||||
| 		if (line[0] == '\0') { |  | ||||||
| 			goto _continue; |  | ||||||
| 		} |  | ||||||
| 		if (line[0] == '}') { |  | ||||||
| 			config->current_mode = default_mode; |  | ||||||
| 			goto _continue; |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		// Any command which would require wlc to be initialized
 |  | ||||||
| 		// should be queued for later execution
 |  | ||||||
| 		list_t *args = split_string(line, whitespace); | 		list_t *args = split_string(line, whitespace); | ||||||
|  | 		if (!args->length) { | ||||||
|  | 			goto cleanup; | ||||||
|  | 		} | ||||||
|  | 		//TODO make this better, it only handles modes right now, and very
 | ||||||
|  | 		//simply at that
 | ||||||
|  | 		if (strncmp(args->items[0], "}", 1) == 0) { | ||||||
|  | 			config->current_mode = default_mode; | ||||||
|  | 			goto cleanup; | ||||||
|  | 		} | ||||||
| 		struct cmd_handler *handler; | 		struct cmd_handler *handler; | ||||||
| 		if ((handler = find_handler(args->items[0]))) { | 		if ((handler = find_handler(args->items[0]))) { | ||||||
| 			if (handler->config_type == CMD_KEYBIND) { | 			if (handler->config_type == CMD_KEYBIND) { | ||||||
|  | @ -259,9 +257,8 @@ bool read_config(FILE *file, bool is_active) { | ||||||
| 		} else { | 		} else { | ||||||
| 			sway_log(L_ERROR, "Invalid command ``%s''", line); | 			sway_log(L_ERROR, "Invalid command ``%s''", line); | ||||||
| 		} | 		} | ||||||
|  | 		cleanup: | ||||||
| 		free_flat_list(args); | 		free_flat_list(args); | ||||||
| 
 |  | ||||||
| _continue: |  | ||||||
| 		free(line); | 		free(line); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -277,28 +274,34 @@ _continue: | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| char *do_var_replacement(char *str) { | char *do_var_replacement(char *str) { | ||||||
| 	// TODO: Handle escaping $ and using $ in string literals
 |  | ||||||
| 	int i; | 	int i; | ||||||
| 	for (i = 0; str[i]; ++i) { | 	char *find = str; | ||||||
| 		if (str[i] == '$') { | 	while ((find = strchr(find, '$'))) { | ||||||
| 			// Try for match (note: this could be faster)
 | 		// Skip if escaped.
 | ||||||
| 			int j; | 		if (find > str + 1 && find[-1] == '\\') { | ||||||
| 			for (j = 0; j < config->symbols->length; ++j) { | 			if (!(find > str + 2 && find[-2] == '\\')) { | ||||||
| 				struct sway_variable *var = config->symbols->items[j]; | 				continue; | ||||||
| 				if (strstr(str + i, var->name) == str + i) { |  | ||||||
| 					// Match, do replacement
 |  | ||||||
| 					char *new_string = malloc( |  | ||||||
| 						strlen(str) - |  | ||||||
| 						strlen(var->name) + |  | ||||||
| 						strlen(var->value) + 1); |  | ||||||
| 					strncpy(new_string, str, i); |  | ||||||
| 					new_string[i] = 0; |  | ||||||
| 					strcat(new_string, var->value); |  | ||||||
| 					strcat(new_string, str + i + strlen(var->name)); |  | ||||||
| 					free(str); |  | ||||||
| 					str = new_string; |  | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | 		// Find matching variable
 | ||||||
|  | 		for (i = 0; i < config->symbols->length; ++i) { | ||||||
|  | 			struct sway_variable *var = config->symbols->items[i]; | ||||||
|  | 			int vnlen = strlen(var->name); | ||||||
|  | 			if (strncmp(find, var->name, vnlen) == 0) { | ||||||
|  | 				int vvlen = strlen(var->value); | ||||||
|  | 				char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); | ||||||
|  | 				char *newptr = newstr; | ||||||
|  | 				int offset = find - str; | ||||||
|  | 				strncpy(newptr, str, offset); | ||||||
|  | 				newptr += offset; | ||||||
|  | 				strncpy(newptr, var->value, vvlen); | ||||||
|  | 				newptr += vvlen; | ||||||
|  | 				strcpy(newptr, find + vnlen); | ||||||
|  | 				free(str); | ||||||
|  | 				str = newstr; | ||||||
|  | 				find = str + offset + vvlen; | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return str; | 	return str; | ||||||
|  |  | ||||||
|  | @ -49,3 +49,7 @@ void list_cat(list_t *list, list_t *source) { | ||||||
| 		list_add(list, source->items[i]); | 		list_add(list, source->items[i]); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | void list_sort(list_t *list, int compare(const void *left, const void *right)) { | ||||||
|  | 	qsort(list->items, list->length, sizeof(void *), compare); | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 taiyu
						taiyu