diff src/terminal.c @ 31825:0d27ddce621d v9.0.1245

patch 9.0.1245: code is indented more than necessary Commit: https://github.com/vim/vim/commit/032713f8299abd92fcfb1e490d1ae5c1ecadde41 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Jan 25 21:05:38 2023 +0000 patch 9.0.1245: code is indented more than necessary Problem: Code is indented more than necessary. Solution: Use an early return where it makes sense. (Yegappan Lakshmanan, closes #11879)
author Bram Moolenaar <Bram@vim.org>
date Wed, 25 Jan 2023 22:15:03 +0100
parents d8e7d725a666
children 4545f58c8490
line wrap: on
line diff
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -259,19 +259,19 @@ parse_termwinsize(win_T *wp, int *rows, 
     *rows = 0;
     *cols = 0;
 
-    if (*wp->w_p_tws != NUL)
-    {
-	char_u *p = vim_strchr(wp->w_p_tws, 'x');
-
-	// Syntax of value was already checked when it's set.
-	if (p == NULL)
-	{
-	    minsize = TRUE;
-	    p = vim_strchr(wp->w_p_tws, '*');
-	}
-	*rows = atoi((char *)wp->w_p_tws);
-	*cols = atoi((char *)p + 1);
-    }
+    if (*wp->w_p_tws == NUL)
+	return FALSE;
+
+    char_u *p = vim_strchr(wp->w_p_tws, 'x');
+
+    // Syntax of value was already checked when it's set.
+    if (p == NULL)
+    {
+	minsize = TRUE;
+	p = vim_strchr(wp->w_p_tws, '*');
+    }
+    *rows = atoi((char *)wp->w_p_tws);
+    *cols = atoi((char *)p + 1);
     return minsize;
 }
 
@@ -1620,21 +1620,20 @@ term_job_running_check(term_T *term, int
 {
     // Also consider the job finished when the channel is closed, to avoid a
     // race condition when updating the title.
-    if (term != NULL
-	&& term->tl_job != NULL
-	&& channel_is_open(term->tl_job->jv_channel))
-    {
-	job_T *job = term->tl_job;
-
-	// Careful: Checking the job status may invoke callbacks, which close
-	// the buffer and terminate "term".  However, "job" will not be freed
-	// yet.
-	if (check_job_status)
-	    job_status(job);
-	return (job->jv_status == JOB_STARTED
-		|| (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
-    }
-    return FALSE;
+    if (term == NULL
+	|| term->tl_job == NULL
+	|| !channel_is_open(term->tl_job->jv_channel))
+	return FALSE;
+
+    job_T *job = term->tl_job;
+
+    // Careful: Checking the job status may invoke callbacks, which close
+    // the buffer and terminate "term".  However, "job" will not be freed
+    // yet.
+    if (check_job_status)
+	job_status(job);
+    return (job->jv_status == JOB_STARTED
+	    || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
 }
 
 /*
@@ -1807,28 +1806,27 @@ equal_celattr(cellattr_T *a, cellattr_T 
     static int
 add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
 {
-    if (ga_grow(&term->tl_scrollback, 1) == OK)
-    {
-	sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
-				      + term->tl_scrollback.ga_len;
-
-	if (lnum > 0)
+    if (ga_grow(&term->tl_scrollback, 1) == FAIL)
+	return FALSE;
+
+    sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
+	+ term->tl_scrollback.ga_len;
+
+    if (lnum > 0)
+    {
+	int i;
+
+	for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
 	{
-	    int i;
-
-	    for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
-	    {
-		*line = *(line - 1);
-		--line;
-	    }
+	    *line = *(line - 1);
+	    --line;
 	}
-	line->sb_cols = 0;
-	line->sb_cells = NULL;
-	line->sb_fill_attr = *fill_attr;
-	++term->tl_scrollback.ga_len;
-	return OK;
-    }
-    return FALSE;
+    }
+    line->sb_cols = 0;
+    line->sb_cells = NULL;
+    line->sb_fill_attr = *fill_attr;
+    ++term->tl_scrollback.ga_len;
+    return OK;
 }
 
 /*
@@ -2409,43 +2407,43 @@ term_paste_register(int prev_c UNUSED)
 	return;
 
     l = (list_T *)get_reg_contents(c, GREG_LIST);
-    if (l != NULL)
-    {
-	type = get_reg_type(c, &reglen);
-	FOR_ALL_LIST_ITEMS(l, item)
+    if (l == NULL)
+	return;
+
+    type = get_reg_type(c, &reglen);
+    FOR_ALL_LIST_ITEMS(l, item)
+    {
+	char_u *s = tv_get_string(&item->li_tv);
+#ifdef MSWIN
+	char_u *tmp = s;
+
+	if (!enc_utf8 && enc_codepage > 0)
 	{
-	    char_u *s = tv_get_string(&item->li_tv);
-#ifdef MSWIN
-	    char_u *tmp = s;
-
-	    if (!enc_utf8 && enc_codepage > 0)
+	    WCHAR   *ret = NULL;
+	    int	length = 0;
+
+	    MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
+		    (int)STRLEN(s), &ret, &length);
+	    if (ret != NULL)
 	    {
-		WCHAR   *ret = NULL;
-		int	length = 0;
-
-		MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
-						(int)STRLEN(s), &ret, &length);
-		if (ret != NULL)
-		{
-		    WideCharToMultiByte_alloc(CP_UTF8, 0,
-				    ret, length, (char **)&s, &length, 0, 0);
-		    vim_free(ret);
-		}
+		WideCharToMultiByte_alloc(CP_UTF8, 0,
+			ret, length, (char **)&s, &length, 0, 0);
+		vim_free(ret);
 	    }
+	}
 #endif
-	    channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
-						      s, (int)STRLEN(s), NULL);
+	channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
+		s, (int)STRLEN(s), NULL);
 #ifdef MSWIN
-	    if (tmp != s)
-		vim_free(s);
+	if (tmp != s)
+	    vim_free(s);
 #endif
 
-	    if (item->li_next != NULL || type == MLINE)
-		channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
-						      (char_u *)"\r", 1, NULL);
-	}
-	list_free(l);
-    }
+	if (item->li_next != NULL || type == MLINE)
+	    channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
+		    (char_u *)"\r", 1, NULL);
+    }
+    list_free(l);
 }
 
 /*
@@ -2615,18 +2613,18 @@ term_win_entered()
 {
     term_T *term = curbuf->b_term;
 
-    if (term != NULL)
-    {
-	if (term_use_loop_check(TRUE))
-	{
-	    reset_VIsual_and_resel();
-	    if (State & MODE_INSERT)
-		stop_insert_mode = TRUE;
-	}
-	mouse_was_outside = FALSE;
-	enter_mouse_col = mouse_col;
-	enter_mouse_row = mouse_row;
-    }
+    if (term == NULL)
+	return;
+
+    if (term_use_loop_check(TRUE))
+    {
+	reset_VIsual_and_resel();
+	if (State & MODE_INSERT)
+	    stop_insert_mode = TRUE;
+    }
+    mouse_was_outside = FALSE;
+    enter_mouse_col = mouse_col;
+    enter_mouse_row = mouse_row;
 }
 
     void
@@ -2634,16 +2632,16 @@ term_focus_change(int in_focus)
 {
     term_T *term = curbuf->b_term;
 
-    if (term != NULL && term->tl_vterm != NULL)
-    {
-	VTermState	*state = vterm_obtain_state(term->tl_vterm);
-
-	if (in_focus)
-	    vterm_state_focus_in(state);
-	else
-	    vterm_state_focus_out(state);
-	term_forward_output(term);
-    }
+    if (term == NULL || term->tl_vterm == NULL)
+	return;
+
+    VTermState	*state = vterm_obtain_state(term->tl_vterm);
+
+    if (in_focus)
+	vterm_state_focus_in(state);
+    else
+	vterm_state_focus_out(state);
+    term_forward_output(term);
 }
 
 /*
@@ -2886,13 +2884,13 @@ theend:
     static void
 may_toggle_cursor(term_T *term)
 {
-    if (in_terminal_loop == term)
-    {
-	if (term->tl_cursor_visible)
-	    cursor_on();
-	else
-	    cursor_off();
-    }
+    if (in_terminal_loop != term)
+	return;
+
+    if (term->tl_cursor_visible)
+	cursor_on();
+    else
+	cursor_off();
 }
 
 /*
@@ -3320,27 +3318,27 @@ handle_resize(int rows, int cols, void *
     static void
 limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
 {
-    if (gap->ga_len >= term->tl_buffer->b_p_twsl)
-    {
-	int	todo = term->tl_buffer->b_p_twsl / 10;
-	int	i;
-
-	curbuf = term->tl_buffer;
-	for (i = 0; i < todo; ++i)
-	{
-	    vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
-	    if (update_buffer)
-		ml_delete(1);
-	}
-	curbuf = curwin->w_buffer;
-
-	gap->ga_len -= todo;
-	mch_memmove(gap->ga_data,
-		    (sb_line_T *)gap->ga_data + todo,
-		    sizeof(sb_line_T) * gap->ga_len);
+    if (gap->ga_len < term->tl_buffer->b_p_twsl)
+	return;
+
+    int	todo = term->tl_buffer->b_p_twsl / 10;
+    int	i;
+
+    curbuf = term->tl_buffer;
+    for (i = 0; i < todo; ++i)
+    {
+	vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
 	if (update_buffer)
-	    term->tl_scrollback_scrolled -= todo;
-    }
+	    ml_delete(1);
+    }
+    curbuf = curwin->w_buffer;
+
+    gap->ga_len -= todo;
+    mch_memmove(gap->ga_data,
+	    (sb_line_T *)gap->ga_data + todo,
+	    sizeof(sb_line_T) * gap->ga_len);
+    if (update_buffer)
+	term->tl_scrollback_scrolled -= todo;
 }
 
 /*
@@ -3371,78 +3369,78 @@ handle_pushline(int cols, const VTermScr
 
     limit_scrollback(term, gap, update_buffer);
 
-    if (ga_grow(gap, 1) == OK)
-    {
-	cellattr_T	*p = NULL;
-	int		len = 0;
-	int		i;
-	int		c;
-	int		col;
-	int		text_len;
-	char_u		*text;
-	sb_line_T	*line;
-	garray_T	ga;
-	cellattr_T	fill_attr = term->tl_default_color;
-
-	// do not store empty cells at the end
-	for (i = 0; i < cols; ++i)
-	    if (cells[i].chars[0] != 0)
-		len = i + 1;
-	    else
-		cell2cellattr(&cells[i], &fill_attr);
-
-	ga_init2(&ga, 1, 100);
-	if (len > 0)
-	    p = ALLOC_MULT(cellattr_T, len);
-	if (p != NULL)
+    if (ga_grow(gap, 1) == FAIL)
+	return 0;
+
+    cellattr_T	*p = NULL;
+    int		len = 0;
+    int		i;
+    int		c;
+    int		col;
+    int		text_len;
+    char_u		*text;
+    sb_line_T	*line;
+    garray_T	ga;
+    cellattr_T	fill_attr = term->tl_default_color;
+
+    // do not store empty cells at the end
+    for (i = 0; i < cols; ++i)
+	if (cells[i].chars[0] != 0)
+	    len = i + 1;
+	else
+	    cell2cellattr(&cells[i], &fill_attr);
+
+    ga_init2(&ga, 1, 100);
+    if (len > 0)
+	p = ALLOC_MULT(cellattr_T, len);
+    if (p != NULL)
+    {
+	for (col = 0; col < len; col += cells[col].width)
 	{
-	    for (col = 0; col < len; col += cells[col].width)
+	    if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
 	    {
-		if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
-		{
-		    ga.ga_len = 0;
-		    break;
-		}
-		for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
-		    ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
-					     (char_u *)ga.ga_data + ga.ga_len);
-		cell2cellattr(&cells[col], &p[col]);
+		ga.ga_len = 0;
+		break;
 	    }
+	    for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
+		ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
+			(char_u *)ga.ga_data + ga.ga_len);
+	    cell2cellattr(&cells[col], &p[col]);
 	}
-	if (ga_grow(&ga, 1) == FAIL)
-	{
-	    if (update_buffer)
-		text = (char_u *)"";
-	    else
-		text = vim_strsave((char_u *)"");
-	    text_len = 0;
-	}
+    }
+    if (ga_grow(&ga, 1) == FAIL)
+    {
+	if (update_buffer)
+	    text = (char_u *)"";
 	else
-	{
-	    text = ga.ga_data;
-	    text_len = ga.ga_len;
-	    *(text + text_len) = NUL;
-	}
-	if (update_buffer)
-	    add_scrollback_line_to_buffer(term, text, text_len);
-
-	line = (sb_line_T *)gap->ga_data + gap->ga_len;
-	line->sb_cols = len;
-	line->sb_cells = p;
-	line->sb_fill_attr = fill_attr;
-	if (update_buffer)
-	{
-	    line->sb_text = NULL;
-	    ++term->tl_scrollback_scrolled;
-	    ga_clear(&ga);  // free the text
-	}
-	else
-	{
-	    line->sb_text = text;
-	    ga_init(&ga);  // text is kept in tl_scrollback_postponed
-	}
-	++gap->ga_len;
-    }
+	    text = vim_strsave((char_u *)"");
+	text_len = 0;
+    }
+    else
+    {
+	text = ga.ga_data;
+	text_len = ga.ga_len;
+	*(text + text_len) = NUL;
+    }
+    if (update_buffer)
+	add_scrollback_line_to_buffer(term, text, text_len);
+
+    line = (sb_line_T *)gap->ga_data + gap->ga_len;
+    line->sb_cols = len;
+    line->sb_cells = p;
+    line->sb_fill_attr = fill_attr;
+    if (update_buffer)
+    {
+	line->sb_text = NULL;
+	++term->tl_scrollback_scrolled;
+	ga_clear(&ga);  // free the text
+    }
+    else
+    {
+	line->sb_text = text;
+	ga_init(&ga);  // text is kept in tl_scrollback_postponed
+    }
+    ++gap->ga_len;
     return 0; // ignored
 }
 
@@ -3612,17 +3610,16 @@ term_after_channel_closed(term_T *term)
     int
 may_close_term_popup(void)
 {
-    if (popup_is_popup(curwin) && curbuf->b_term != NULL
-				 && !term_job_running_not_none(curbuf->b_term))
-    {
-	win_T *pwin = curwin;
-
-	if (win_valid(prevwin))
-	    win_enter(prevwin, FALSE);
-	popup_close_with_retval(pwin, 0);
-	return OK;
-    }
-    return FAIL;
+    if (!popup_is_popup(curwin) || curbuf->b_term == NULL
+				 || term_job_running_not_none(curbuf->b_term))
+	return FAIL;
+
+    win_T *pwin = curwin;
+
+    if (win_valid(prevwin))
+	win_enter(prevwin, FALSE);
+    popup_close_with_retval(pwin, 0);
+    return OK;
 }
 #endif
 
@@ -4002,12 +3999,12 @@ term_did_update_window(win_T *wp)
 {
     term_T	*term = wp->w_buffer->b_term;
 
-    if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
-						       && wp->w_redr_type == 0)
-    {
-	term->tl_dirty_row_start = MAX_ROW;
-	term->tl_dirty_row_end = 0;
-    }
+    if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode
+						|| wp->w_redr_type != 0)
+	return;
+
+    term->tl_dirty_row_start = MAX_ROW;
+    term->tl_dirty_row_end = 0;
 }
 
 /*
@@ -4040,16 +4037,16 @@ term_change_in_curbuf(void)
 {
     term_T *term = curbuf->b_term;
 
-    if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
-    {
-	free_scrollback(term);
-	redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
-
-	// The buffer is now like a normal buffer, it cannot be easily
-	// abandoned when changed.
-	set_string_option_direct((char_u *)"buftype", -1,
-					  (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
-    }
+    if (!term_is_finished(curbuf) || term->tl_scrollback.ga_len <= 0)
+	return;
+
+    free_scrollback(term);
+    redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
+
+    // The buffer is now like a normal buffer, it cannot be easily
+    // abandoned when changed.
+    set_string_option_direct((char_u *)"buftype", -1,
+	    (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
 }
 
 /*
@@ -4908,34 +4905,34 @@ term_update_colors_all(void)
     char_u *
 term_get_status_text(term_T *term)
 {
-    if (term->tl_status_text == NULL)
-    {
-	char_u *txt;
-	size_t len;
-	char_u *fname;
-
-	if (term->tl_normal_mode)
-	{
-	    if (term_job_running(term))
-		txt = (char_u *)_("Terminal");
-	    else
-		txt = (char_u *)_("Terminal-finished");
-	}
-	else if (term->tl_title != NULL)
-	    txt = term->tl_title;
-	else if (term_none_open(term))
-	    txt = (char_u *)_("active");
-	else if (term_job_running(term))
-	    txt = (char_u *)_("running");
+    if (term->tl_status_text != NULL)
+	return term->tl_status_text;
+
+    char_u *txt;
+    size_t len;
+    char_u *fname;
+
+    if (term->tl_normal_mode)
+    {
+	if (term_job_running(term))
+	    txt = (char_u *)_("Terminal");
 	else
-	    txt = (char_u *)_("finished");
-	fname = buf_get_fname(term->tl_buffer);
-	len = 9 + STRLEN(fname) + STRLEN(txt);
-	term->tl_status_text = alloc(len);
-	if (term->tl_status_text != NULL)
-	    vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
-								   fname, txt);
-    }
+	    txt = (char_u *)_("Terminal-finished");
+    }
+    else if (term->tl_title != NULL)
+	txt = term->tl_title;
+    else if (term_none_open(term))
+	txt = (char_u *)_("active");
+    else if (term_job_running(term))
+	txt = (char_u *)_("running");
+    else
+	txt = (char_u *)_("finished");
+    fname = buf_get_fname(term->tl_buffer);
+    len = 9 + STRLEN(fname) + STRLEN(txt);
+    term->tl_status_text = alloc(len);
+    if (term->tl_status_text != NULL)
+	vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
+		fname, txt);
     return term->tl_status_text;
 }
 
@@ -5236,11 +5233,11 @@ dump_is_corrupt(garray_T *gap)
     static void
 append_cell(garray_T *gap, cellattr_T *cell)
 {
-    if (ga_grow(gap, 1) == OK)
-    {
-	*(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
-	++gap->ga_len;
-    }
+    if (ga_grow(gap, 1) == FAIL)
+	return;
+
+    *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
+    ++gap->ga_len;
 }
 
     static void
@@ -6036,15 +6033,15 @@ f_term_getcursor(typval_T *argvars, typv
     list_append_number(l, term->tl_cursor_pos.col + 1);
 
     d = dict_alloc();
-    if (d != NULL)
-    {
-	dict_add_number(d, "visible", term->tl_cursor_visible);
-	dict_add_number(d, "blink", blink_state_is_inverted()
-			    ? !term->tl_cursor_blink : term->tl_cursor_blink);
-	dict_add_number(d, "shape", term->tl_cursor_shape);
-	dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
-	list_append_dict(l, d);
-    }
+    if (d == NULL)
+	return;
+
+    dict_add_number(d, "visible", term->tl_cursor_visible);
+    dict_add_number(d, "blink", blink_state_is_inverted()
+	    ? !term->tl_cursor_blink : term->tl_cursor_blink);
+    dict_add_number(d, "shape", term->tl_cursor_shape);
+    dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
+    list_append_dict(l, d);
 }
 
 /*
@@ -7692,20 +7689,20 @@ term_free_vterm(term_T *term)
 term_report_winsize(term_T *term, int rows, int cols)
 {
     // Use an ioctl() to report the new window size to the job.
-    if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
-    {
-	int fd = -1;
-	int part;
-
-	for (part = PART_OUT; part < PART_COUNT; ++part)
-	{
-	    fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
-	    if (mch_isatty(fd))
-		break;
-	}
-	if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
-	    mch_signal_job(term->tl_job, (char_u *)"winch");
-    }
+    if (term->tl_job == NULL || term->tl_job->jv_channel == NULL)
+	return;
+
+    int fd = -1;
+    int part;
+
+    for (part = PART_OUT; part < PART_COUNT; ++part)
+    {
+	fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
+	if (mch_isatty(fd))
+	    break;
+    }
+    if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
+	mch_signal_job(term->tl_job, (char_u *)"winch");
 }
 
 # endif