mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Expand menu and popup -x and -y as a format, from teo_paul1 at yahoo dot
com in GitHub issue 2442.
This commit is contained in:
		@@ -61,7 +61,7 @@ const struct cmd_entry cmd_display_popup_entry = {
 | 
				
			|||||||
	.exec = cmd_display_popup_exec
 | 
						.exec = cmd_display_popup_exec
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static int
 | 
				
			||||||
cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
 | 
					cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
 | 
				
			||||||
    struct args *args, u_int *px, u_int *py, u_int w, u_int h)
 | 
					    struct args *args, u_int *px, u_int *py, u_int w, u_int h)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -71,16 +71,50 @@ cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
 | 
				
			|||||||
	struct session		*s = tc->session;
 | 
						struct session		*s = tc->session;
 | 
				
			||||||
	struct winlink		*wl = target->wl;
 | 
						struct winlink		*wl = target->wl;
 | 
				
			||||||
	struct window_pane	*wp = target->wp;
 | 
						struct window_pane	*wp = target->wp;
 | 
				
			||||||
	struct style_ranges	*ranges;
 | 
						struct style_ranges	*ranges = NULL;
 | 
				
			||||||
	struct style_range	*sr;
 | 
						struct style_range	*sr = NULL;
 | 
				
			||||||
	const char		*xp, *yp;
 | 
						const char		*xp, *yp;
 | 
				
			||||||
	u_int			 line, ox, oy, sx, sy, lines;
 | 
						char			*p;
 | 
				
			||||||
 | 
						int			 top;
 | 
				
			||||||
 | 
						u_int			 line, ox, oy, sx, sy, lines, position;
 | 
				
			||||||
 | 
						long			 n;
 | 
				
			||||||
 | 
						struct format_tree	*ft;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Work out the position from the -x and -y arguments. This is the
 | 
				
			||||||
 | 
						 * bottom-left position.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* If the popup is too big, stop now. */
 | 
				
			||||||
 | 
						if (w > tty->sx || h > tty->sy)
 | 
				
			||||||
 | 
							return (0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Create format with mouse position if any. */
 | 
				
			||||||
 | 
						ft = format_create_from_target(item);
 | 
				
			||||||
 | 
						if (event->m.valid) {
 | 
				
			||||||
 | 
							format_add(ft, "popup_mouse_x", "%u", event->m.x);
 | 
				
			||||||
 | 
							format_add(ft, "popup_mouse_y", "%u", event->m.y);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * If there are any status lines, add this window position and the
 | 
				
			||||||
 | 
						 * status line position.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						top = status_at_line(tc);
 | 
				
			||||||
 | 
						if (top != -1) {
 | 
				
			||||||
		lines = status_line_size(tc);
 | 
							lines = status_line_size(tc);
 | 
				
			||||||
 | 
							if (top == 0)
 | 
				
			||||||
 | 
								top = lines;
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								top = 0;
 | 
				
			||||||
 | 
							position = options_get_number(s->options, "status-position");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (line = 0; line < lines; line++) {
 | 
							for (line = 0; line < lines; line++) {
 | 
				
			||||||
			ranges = &tc->status.entries[line].ranges;
 | 
								ranges = &tc->status.entries[line].ranges;
 | 
				
			||||||
			TAILQ_FOREACH(sr, ranges, entry) {
 | 
								TAILQ_FOREACH(sr, ranges, entry) {
 | 
				
			||||||
			if (sr->type == STYLE_RANGE_WINDOW)
 | 
									if (sr->type != STYLE_RANGE_WINDOW)
 | 
				
			||||||
 | 
										continue;
 | 
				
			||||||
 | 
									if (sr->argument == (u_int)wl->idx)
 | 
				
			||||||
					break;
 | 
										break;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if (sr != NULL)
 | 
								if (sr != NULL)
 | 
				
			||||||
@@ -89,88 +123,132 @@ cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
 | 
				
			|||||||
		if (line == lines)
 | 
							if (line == lines)
 | 
				
			||||||
			ranges = &tc->status.entries[0].ranges;
 | 
								ranges = &tc->status.entries[0].ranges;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (sr != NULL) {
 | 
				
			||||||
 | 
								format_add(ft, "popup_window_status_line_x", "%u",
 | 
				
			||||||
 | 
								    sr->start);
 | 
				
			||||||
 | 
								if (position == 0) {
 | 
				
			||||||
 | 
									format_add(ft, "popup_window_status_line_y",
 | 
				
			||||||
 | 
									    "%u", line + 1 + h);
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									format_add(ft, "popup_window_status_line_y",
 | 
				
			||||||
 | 
									    "%u", tty->sy - lines + line);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (position == 0)
 | 
				
			||||||
 | 
								format_add(ft, "popup_status_line_y", "%u", lines + h);
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
 | 
								format_add(ft, "popup_status_line_y", "%u",
 | 
				
			||||||
 | 
								    tty->sy - lines);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else
 | 
				
			||||||
 | 
							top = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Popup width and height. */
 | 
				
			||||||
 | 
						format_add(ft, "popup_width", "%u", w);
 | 
				
			||||||
 | 
						format_add(ft, "popup_height", "%u", h);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Position so popup is in the centre. */
 | 
				
			||||||
 | 
						n = (long)(tty->sx - 1) / 2 - w / 2;
 | 
				
			||||||
 | 
						if (n < 0)
 | 
				
			||||||
 | 
							format_add(ft, "popup_centre_x", "%u", 0);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							format_add(ft, "popup_centre_x", "%ld", n);
 | 
				
			||||||
 | 
						n = (tty->sy - 1) / 2 + h / 2;
 | 
				
			||||||
 | 
						if (n + h >= tty->sy)
 | 
				
			||||||
 | 
							format_add(ft, "popup_centre_y", "%u", tty->sy - h);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							format_add(ft, "popup_centre_y", "%ld", n);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Position of popup relative to mouse. */
 | 
				
			||||||
 | 
						if (event->m.valid) {
 | 
				
			||||||
 | 
							n = (long)event->m.x - w / 2;
 | 
				
			||||||
 | 
							if (n < 0)
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_centre_x", "%u", 0);
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_centre_x", "%ld", n);
 | 
				
			||||||
 | 
							n = event->m.y - h / 2;
 | 
				
			||||||
 | 
							if (n + h >= tty->sy) {
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_centre_y", "%u",
 | 
				
			||||||
 | 
								    tty->sy - h);
 | 
				
			||||||
 | 
							} else
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_centre_y", "%ld", n);
 | 
				
			||||||
 | 
							n = (long)event->m.y + h;
 | 
				
			||||||
 | 
							if (n + h >= tty->sy)
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_top", "%u", tty->sy - h);
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_top", "%ld", n);
 | 
				
			||||||
 | 
							n = event->m.y - h;
 | 
				
			||||||
 | 
							if (n < 0)
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_bottom", "%u", 0);
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								format_add(ft, "popup_mouse_bottom", "%ld", n);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Position in pane. */
 | 
				
			||||||
 | 
						tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
 | 
				
			||||||
 | 
						n = top + wp->yoff - oy + h;
 | 
				
			||||||
 | 
						if (n >= tty->sy)
 | 
				
			||||||
 | 
							format_add(ft, "popup_pane_top", "%u", tty->sy - h);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							format_add(ft, "popup_pane_top", "%ld", n);
 | 
				
			||||||
 | 
						format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
 | 
				
			||||||
 | 
						format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
 | 
				
			||||||
 | 
						n = (long)wp->xoff + wp->sx - ox - w;
 | 
				
			||||||
 | 
						if (n < 0)
 | 
				
			||||||
 | 
							format_add(ft, "popup_pane_right", "%u", 0);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							format_add(ft, "popup_pane_right", "%ld", n);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Expand horizontal position. */
 | 
				
			||||||
	xp = args_get(args, 'x');
 | 
						xp = args_get(args, 'x');
 | 
				
			||||||
	if (xp == NULL || strcmp(xp, "C") == 0)
 | 
						if (xp == NULL || strcmp(xp, "C") == 0)
 | 
				
			||||||
		*px = (tty->sx - 1) / 2 - w / 2;
 | 
							xp = "#{popup_centre_x}";
 | 
				
			||||||
	else if (strcmp(xp, "R") == 0)
 | 
						else if (strcmp(xp, "R") == 0)
 | 
				
			||||||
		*px = tty->sx - 1;
 | 
							xp = "#{popup_right}";
 | 
				
			||||||
	else if (strcmp(xp, "P") == 0) {
 | 
						else if (strcmp(xp, "P") == 0)
 | 
				
			||||||
		tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
 | 
							xp = "#{popup_pane_left}";
 | 
				
			||||||
		if (wp->xoff >= ox)
 | 
						else if (strcmp(xp, "M") == 0)
 | 
				
			||||||
			*px = wp->xoff - ox;
 | 
							xp = "#{popup_mouse_centre_x}";
 | 
				
			||||||
		else
 | 
						else if (strcmp(xp, "W") == 0)
 | 
				
			||||||
			*px = 0;
 | 
							xp = "#{popup_window_status_line_x}";
 | 
				
			||||||
	} else if (strcmp(xp, "M") == 0) {
 | 
						p = format_expand(ft, xp);
 | 
				
			||||||
		if (event->m.valid && event->m.x > w / 2)
 | 
						n = strtol(p, NULL, 10);
 | 
				
			||||||
			*px = event->m.x - w / 2;
 | 
						if (n + w >= tty->sx)
 | 
				
			||||||
		else
 | 
							n = tty->sx - w;
 | 
				
			||||||
			*px = 0;
 | 
						else if (n < 0)
 | 
				
			||||||
	} else if (strcmp(xp, "W") == 0) {
 | 
							n = 0;
 | 
				
			||||||
		if (status_at_line(tc) == -1)
 | 
						*px = n;
 | 
				
			||||||
			*px = 0;
 | 
						log_debug("%s: -x: %s = %s = %u", __func__, xp, p, *px);
 | 
				
			||||||
		else {
 | 
						free(p);
 | 
				
			||||||
			TAILQ_FOREACH(sr, ranges, entry) {
 | 
					 | 
				
			||||||
				if (sr->type != STYLE_RANGE_WINDOW)
 | 
					 | 
				
			||||||
					continue;
 | 
					 | 
				
			||||||
				if (sr->argument == (u_int)wl->idx)
 | 
					 | 
				
			||||||
					break;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if (sr != NULL)
 | 
					 | 
				
			||||||
				*px = sr->start;
 | 
					 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				*px = 0;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else
 | 
					 | 
				
			||||||
		*px = strtoul(xp, NULL, 10);
 | 
					 | 
				
			||||||
	if ((*px) + w >= tty->sx)
 | 
					 | 
				
			||||||
		*px = tty->sx - w;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Expand vertical position  */
 | 
				
			||||||
	yp = args_get(args, 'y');
 | 
						yp = args_get(args, 'y');
 | 
				
			||||||
	if (yp == NULL || strcmp(yp, "C") == 0)
 | 
						if (yp == NULL || strcmp(yp, "C") == 0)
 | 
				
			||||||
		*py = (tty->sy - 1) / 2 + h / 2;
 | 
							yp = "#{popup_centre_y}";
 | 
				
			||||||
	else if (strcmp(yp, "P") == 0) {
 | 
						else if (strcmp(yp, "P") == 0)
 | 
				
			||||||
		tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
 | 
							yp = "#{popup_pane_bottom}";
 | 
				
			||||||
		if (wp->yoff + wp->sy >= oy)
 | 
						else if (strcmp(yp, "M") == 0)
 | 
				
			||||||
			*py = wp->yoff + wp->sy - oy;
 | 
							yp = "#{popup_mouse_top}";
 | 
				
			||||||
 | 
						else if (strcmp(yp, "S") == 0)
 | 
				
			||||||
 | 
							yp = "#{popup_status_line_y}";
 | 
				
			||||||
 | 
						else if (strcmp(yp, "W") == 0)
 | 
				
			||||||
 | 
							yp = "#{popup_window_status_line_y}";
 | 
				
			||||||
 | 
						p = format_expand(ft, yp);
 | 
				
			||||||
 | 
						n = strtol(p, NULL, 10);
 | 
				
			||||||
 | 
						if (n < h)
 | 
				
			||||||
 | 
							n = 0;
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
			*py = 0;
 | 
							n -= h;
 | 
				
			||||||
	} else if (strcmp(yp, "M") == 0) {
 | 
						if (n + h >= tty->sy)
 | 
				
			||||||
		if (event->m.valid)
 | 
							n = tty->sy - h;
 | 
				
			||||||
			*py = event->m.y + h;
 | 
						else if (n < 0)
 | 
				
			||||||
		else
 | 
							n = 0;
 | 
				
			||||||
			*py = 0;
 | 
						*py = n;
 | 
				
			||||||
	} else if (strcmp(yp, "S") == 0) {
 | 
						log_debug("%s: -y: %s = %s = %u", __func__, yp, p, *py);
 | 
				
			||||||
		if (options_get_number(s->options, "status-position") == 0) {
 | 
						free(p);
 | 
				
			||||||
			if (lines != 0)
 | 
					
 | 
				
			||||||
				*py = lines + h;
 | 
						return (1);
 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				*py = 0;
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			if (lines != 0)
 | 
					 | 
				
			||||||
				*py = tty->sy - lines;
 | 
					 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				*py = tty->sy;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else if (strcmp(yp, "W") == 0) {
 | 
					 | 
				
			||||||
		if (options_get_number(s->options, "status-position") == 0) {
 | 
					 | 
				
			||||||
			if (lines != 0)
 | 
					 | 
				
			||||||
				*py = line + 1 + h;
 | 
					 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				*py = 0;
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			if (lines != 0)
 | 
					 | 
				
			||||||
				*py = tty->sy - lines + line;
 | 
					 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				*py = tty->sy;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else
 | 
					 | 
				
			||||||
		*py = strtoul(yp, NULL, 10);
 | 
					 | 
				
			||||||
	if (*py < h)
 | 
					 | 
				
			||||||
		*py = 0;
 | 
					 | 
				
			||||||
	else
 | 
					 | 
				
			||||||
		*py -= h;
 | 
					 | 
				
			||||||
	if ((*py) + h >= tty->sy)
 | 
					 | 
				
			||||||
		*py = tty->sy - h;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static enum cmd_retval
 | 
					static enum cmd_retval
 | 
				
			||||||
@@ -226,8 +304,11 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
 | 
				
			|||||||
		menu_free(menu);
 | 
							menu_free(menu);
 | 
				
			||||||
		return (CMD_RETURN_NORMAL);
 | 
							return (CMD_RETURN_NORMAL);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	cmd_display_menu_get_position(tc, item, args, &px, &py, menu->width + 4,
 | 
						if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
 | 
				
			||||||
	    menu->count + 2);
 | 
						    menu->width + 4, menu->count + 2)) {
 | 
				
			||||||
 | 
							menu_free(menu);
 | 
				
			||||||
 | 
							return (CMD_RETURN_NORMAL);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (args_has(args, 'O'))
 | 
						if (args_has(args, 'O'))
 | 
				
			||||||
		flags |= MENU_STAYOPEN;
 | 
							flags |= MENU_STAYOPEN;
 | 
				
			||||||
@@ -296,7 +377,8 @@ cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
 | 
				
			|||||||
		w = tty->sx - 1;
 | 
							w = tty->sx - 1;
 | 
				
			||||||
	if (h > tty->sy - 1)
 | 
						if (h > tty->sy - 1)
 | 
				
			||||||
		h = tty->sy - 1;
 | 
							h = tty->sy - 1;
 | 
				
			||||||
	cmd_display_menu_get_position(tc, item, args, &px, &py, w, h);
 | 
						if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
 | 
				
			||||||
 | 
							return (CMD_RETURN_NORMAL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	value = args_get(args, 'd');
 | 
						value = args_get(args, 'd');
 | 
				
			||||||
	if (value != NULL)
 | 
						if (value != NULL)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								tmux.1
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								tmux.1
									
									
									
									
									
								
							@@ -5388,6 +5388,28 @@ Both may be a row or column number, or one of the following special values:
 | 
				
			|||||||
.It Li "S" Ta Fl y Ta "The line above or below the status line"
 | 
					.It Li "S" Ta Fl y Ta "The line above or below the status line"
 | 
				
			||||||
.El
 | 
					.El
 | 
				
			||||||
.Pp
 | 
					.Pp
 | 
				
			||||||
 | 
					Or a format, which is expanded including the following additional variables:
 | 
				
			||||||
 | 
					.Bl -column "XXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
 | 
				
			||||||
 | 
					.It Sy "Variable name" Ta Sy "Replaced with"
 | 
				
			||||||
 | 
					.It Li "popup_centre_x" Ta "Centered in the client"
 | 
				
			||||||
 | 
					.It Li "popup_centre_y" Ta "Centered in the client"
 | 
				
			||||||
 | 
					.It Li "popup_height" Ta "Height of menu or popup"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_bottom" Ta "Bottom of at the mouse"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_centre_x" Ta "Horizontal centre at the mouse"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_centre_y" Ta "Vertical centre at the mouse"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_top" Ta "Top at the mouse"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_x" Ta "Mouse X position"
 | 
				
			||||||
 | 
					.It Li "popup_mouse_y" Ta "Mouse Y position"
 | 
				
			||||||
 | 
					.It Li "popup_pane_bottom" Ta "Bottom of the pane"
 | 
				
			||||||
 | 
					.It Li "popup_pane_left" Ta "Left of the pane"
 | 
				
			||||||
 | 
					.It Li "popup_pane_right" Ta "Right of the pane"
 | 
				
			||||||
 | 
					.It Li "popup_pane_top" Ta "Top of the pane"
 | 
				
			||||||
 | 
					.It Li "popup_status_line_y" Ta "Above or below the status line"
 | 
				
			||||||
 | 
					.It Li "popup_width" Ta "Width of menu or popup"
 | 
				
			||||||
 | 
					.It Li "popup_window_status_line_x" Ta "At the window position in status line"
 | 
				
			||||||
 | 
					.It Li "popup_window_status_line_y" Ta "At the status line showing the window"
 | 
				
			||||||
 | 
					.El
 | 
				
			||||||
 | 
					.Pp
 | 
				
			||||||
Each menu consists of items followed by a key shortcut shown in brackets.
 | 
					Each menu consists of items followed by a key shortcut shown in brackets.
 | 
				
			||||||
If the menu is too large to fit on the terminal, it is not displayed.
 | 
					If the menu is too large to fit on the terminal, it is not displayed.
 | 
				
			||||||
Pressing the key shortcut chooses the corresponding item.
 | 
					Pressing the key shortcut chooses the corresponding item.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user