Skip to content

Commit 00d253e

Browse files
committed
patch 8.2.0523: loops are repeated
Problem: Loops are repeated. Solution: Use FOR_ALL_ macros. (Yegappan Lakshmanan, closes #5882)
1 parent ee4e0c1 commit 00d253e

19 files changed

Lines changed: 72 additions & 44 deletions

src/buffer.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ static void clear_wininfo(buf_T *buf);
5252
# define dev_T unsigned
5353
#endif
5454

55+
#define FOR_ALL_BUFS_FROM_LAST(buf) \
56+
for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
57+
5558
#if defined(FEAT_QUICKFIX)
5659
static char *msg_loclist = N_("[Location List]");
5760
static char *msg_qflist = N_("[Quickfix List]");
@@ -415,7 +418,7 @@ buf_valid(buf_T *buf)
415418

416419
// Assume that we more often have a recent buffer, start with the last
417420
// one.
418-
for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
421+
FOR_ALL_BUFS_FROM_LAST(bp)
419422
if (bp == buf)
420423
return TRUE;
421424
return FALSE;
@@ -2510,7 +2513,7 @@ buflist_findname_stat(
25102513
buf_T *buf;
25112514

25122515
// Start at the last buffer, expect to find a match sooner.
2513-
for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
2516+
FOR_ALL_BUFS_FROM_LAST(buf)
25142517
if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
25152518
#ifdef UNIX
25162519
, stp
@@ -2593,7 +2596,7 @@ buflist_findpat(
25932596
return -1;
25942597
}
25952598

2596-
for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
2599+
FOR_ALL_BUFS_FROM_LAST(buf)
25972600
if (buf->b_p_bl == find_listed
25982601
#ifdef FEAT_DIFF
25992602
&& (!diffmode || diff_mode_buf(buf))

src/drawscreen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ redraw_win_toolbar(win_T *wp)
963963
int button_attr = syn_name2attr((char_u *)"ToolbarButton");
964964

965965
vim_free(wp->w_winbar_items);
966-
for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
966+
FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
967967
++item_count;
968968
wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
969969

src/evalfunc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,7 +5008,7 @@ f_inputlist(typval_T *argvars, typval_T *rettv)
50085008

50095009
l = argvars[0].vval.v_list;
50105010
range_list_materialize(l);
5011-
FOR_ALL_LIST_ITEMS(argvars[0].vval.v_list, li)
5011+
FOR_ALL_LIST_ITEMS(l, li)
50125012
{
50135013
msg_puts((char *)tv_get_string(&li->li_tv));
50145014
msg_putchar('\n');
@@ -7317,7 +7317,7 @@ f_setreg(typval_T *argvars, typval_T *rettv)
73177317
if (ll != NULL)
73187318
{
73197319
range_list_materialize(ll);
7320-
for (li = ll->lv_first; li != NULL; li = li->li_next)
7320+
FOR_ALL_LIST_ITEMS(ll, li)
73217321
{
73227322
strval = tv_get_string_buf_chk(&li->li_tv, buf);
73237323
if (strval == NULL)

src/evalwindow.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ get_tabpage_info(tabpage_T *tp, int tp_idx)
444444
l = list_alloc();
445445
if (l != NULL)
446446
{
447-
for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
448-
wp != NULL; wp = wp->w_next)
447+
FOR_ALL_WINDOWS_IN_TAB(tp, wp)
449448
list_append_number(l, (varnumber_T)wp->w_id);
450449
dict_add_list(dict, "windows", l);
451450
}

src/globals.h

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ EXTERN vimmenu_T *root_menu INIT(= NULL);
574574
* overruling of menus that the user already defined.
575575
*/
576576
EXTERN int sys_menu INIT(= FALSE);
577+
578+
#define FOR_ALL_MENUS(m) for ((m) = root_menu; (m) != NULL; (m) = (m)->next)
579+
#define FOR_ALL_CHILD_MENUS(p, c) \
580+
for ((c) = (p)->children; (c) != NULL; (c) = (c)->next)
577581
#endif
578582

579583
#ifdef FEAT_GUI
@@ -724,7 +728,8 @@ EXTERN buf_T *firstbuf INIT(= NULL); // first buffer
724728
EXTERN buf_T *lastbuf INIT(= NULL); // last buffer
725729
EXTERN buf_T *curbuf INIT(= NULL); // currently active buffer
726730

727-
#define FOR_ALL_BUFFERS(buf) for (buf = firstbuf; buf != NULL; buf = buf->b_next)
731+
#define FOR_ALL_BUFFERS(buf) \
732+
for ((buf) = firstbuf; (buf) != NULL; (buf) = (buf)->b_next)
728733

729734
#define FOR_ALL_BUF_WININFO(buf, wip) \
730735
for ((wip) = (buf)->b_wininfo; (wip) != NULL; (wip) = (wip)->wi_next)
@@ -1484,7 +1489,7 @@ EXTERN disptick_T display_tick INIT(= 0);
14841489
EXTERN linenr_T spell_redraw_lnum INIT(= 0);
14851490

14861491
#define FOR_ALL_SPELL_LANGS(slang) \
1487-
for ((slang) = first_lang; (slang) != NULL; (slang) = slang->sl_next)
1492+
for ((slang) = first_lang; (slang) != NULL; (slang) = (slang)->sl_next)
14881493
#endif
14891494

14901495
#ifdef FEAT_CONCEAL
@@ -1545,20 +1550,20 @@ EXTERN char e_font[] INIT(= N_("E235: Unknown font: %s"));
15451550
EXTERN char e_fontwidth[] INIT(= N_("E236: Font \"%s\" is not fixed-width"));
15461551
#endif
15471552
EXTERN char e_internal[] INIT(= N_("E473: Internal error"));
1548-
EXTERN char e_intern2[] INIT(= N_("E685: Internal error: %s"));
1549-
EXTERN char e_interr[] INIT(= N_("Interrupted"));
1550-
EXTERN char e_invarg[] INIT(= N_("E474: Invalid argument"));
1551-
EXTERN char e_invarg2[] INIT(= N_("E475: Invalid argument: %s"));
1552-
EXTERN char e_duparg2[] INIT(= N_("E983: Duplicate argument: %s"));
1553+
EXTERN char e_intern2[] INIT(= N_("E685: Internal error: %s"));
1554+
EXTERN char e_interr[] INIT(= N_("Interrupted"));
1555+
EXTERN char e_invarg[] INIT(= N_("E474: Invalid argument"));
1556+
EXTERN char e_invarg2[] INIT(= N_("E475: Invalid argument: %s"));
1557+
EXTERN char e_duparg2[] INIT(= N_("E983: Duplicate argument: %s"));
15531558
EXTERN char e_invargval[] INIT(= N_("E475: Invalid value for argument %s"));
15541559
EXTERN char e_invargNval[] INIT(= N_("E475: Invalid value for argument %s: %s"));
15551560
#ifdef FEAT_EVAL
15561561
EXTERN char e_invexpr2[] INIT(= N_("E15: Invalid expression: %s"));
15571562
#endif
15581563
EXTERN char e_invrange[] INIT(= N_("E16: Invalid range"));
1559-
EXTERN char e_invcmd[] INIT(= N_("E476: Invalid command"));
1564+
EXTERN char e_invcmd[] INIT(= N_("E476: Invalid command"));
15601565
#if defined(UNIX) || defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
1561-
EXTERN char e_isadir2[] INIT(= N_("E17: \"%s\" is a directory"));
1566+
EXTERN char e_isadir2[] INIT(= N_("E17: \"%s\" is a directory"));
15621567
#endif
15631568
#ifdef FEAT_LIBCALL
15641569
EXTERN char e_libcall[] INIT(= N_("E364: Library call failed for \"%s()\""));
@@ -1614,8 +1619,8 @@ EXTERN char e_noserver[] INIT(= N_("E247: no registered server named \"%s\""));
16141619
#endif
16151620
EXTERN char e_notcreate[] INIT(= N_("E482: Can't create file %s"));
16161621
EXTERN char e_notmp[] INIT(= N_("E483: Can't get temp file name"));
1617-
EXTERN char e_notopen[] INIT(= N_("E484: Can't open file %s"));
1618-
EXTERN char e_notread[] INIT(= N_("E485: Can't read file %s"));
1622+
EXTERN char e_notopen[] INIT(= N_("E484: Can't open file %s"));
1623+
EXTERN char e_notread[] INIT(= N_("E485: Can't read file %s"));
16191624
EXTERN char e_null[] INIT(= N_("E38: Null argument"));
16201625
#if defined(FEAT_DIGRAPHS) || defined(FEAT_TIMERS) || defined(FEAT_EVAL)
16211626
EXTERN char e_number_exp[] INIT(= N_("E39: Number expected"));
@@ -1837,6 +1842,16 @@ EXTERN HINSTANCE g_hinst INIT(= NULL);
18371842
EXTERN int did_repeated_msg INIT(= 0);
18381843
# define REPEATED_MSG_LOOKING 1
18391844
# define REPEATED_MSG_SAFESTATE 2
1845+
1846+
#define FOR_ALL_CHANNELS(ch) \
1847+
for ((ch) = first_channel; (ch) != NULL; (ch) = (ch)->ch_next)
1848+
#define FOR_ALL_JOBS(job) \
1849+
for ((job) = first_job; (job) != NULL; (job) = (job)->jv_next)
1850+
#endif
1851+
1852+
#if defined(FEAT_DIFF)
1853+
#define FOR_ALL_DIFFBLOCKS_IN_TAB(tp, dp) \
1854+
for ((dp) = (tp)->tp_first_diff; (dp) != NULL; (dp) = (dp)->df_next)
18401855
#endif
18411856

18421857
#define FOR_ALL_LIST_ITEMS(l, li) \

src/gui_athena.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ gui_mch_new_menu_font(void)
954954
vimmenu_T *mp;
955955
int max_height = 9999;
956956

957-
for (mp = root_menu; mp != NULL; mp = mp->next)
957+
FOR_ALL_MENUS(mp)
958958
{
959959
if (menu_is_menubar(mp->dname))
960960
{
@@ -1280,7 +1280,7 @@ gui_mch_show_toolbar(int showit)
12801280
vimmenu_T *toolbar;
12811281
vimmenu_T *cur;
12821282

1283-
for (toolbar = root_menu; toolbar; toolbar = toolbar->next)
1283+
FOR_ALL_MENUS(toolbar)
12841284
if (menu_is_toolbar(toolbar->dname))
12851285
break;
12861286
// Assumption: toolbar is NULL if there is no toolbar,
@@ -1632,7 +1632,7 @@ gui_athena_popup_callback(
16321632
{
16331633
vimmenu_T *i;
16341634

1635-
for (i = menu->parent->children; i != NULL; i = i->next)
1635+
FOR_ALL_CHILD_MENUS(menu->parent, i)
16361636
{
16371637
if (i->submenu_id != NULL && XtIsManaged(i->submenu_id))
16381638
XtPopdown(i->submenu_id);

src/gui_gtk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ gui_gtk_set_mnemonics(int enable)
824824
vimmenu_T *menu;
825825
char_u *name;
826826

827-
for (menu = root_menu; menu != NULL; menu = menu->next)
827+
FOR_ALL_MENUS(menu)
828828
{
829829
if (menu->id == NULL)
830830
continue;

src/gui_motif.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ do_set_mnemonics(int enable)
889889
{
890890
vimmenu_T *menu;
891891

892-
for (menu = root_menu; menu != NULL; menu = menu->next)
892+
FOR_ALL_MENUS(menu)
893893
if (menu->id != (Widget)0)
894894
XtVaSetValues(menu->id,
895895
XmNmnemonic, enable ? menu->mnemonic : NUL,
@@ -1094,7 +1094,7 @@ gui_mch_compute_menu_height(
10941094

10951095
// Find any menu Widget, to be able to call XtManageChild()
10961096
else
1097-
for (mp = root_menu; mp != NULL; mp = mp->next)
1097+
FOR_ALL_MENUS(mp)
10981098
if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
10991099
{
11001100
id = mp->id;
@@ -1112,7 +1112,7 @@ gui_mch_compute_menu_height(
11121112
* Now find the menu item that is the furthest down, and get its position.
11131113
*/
11141114
maxy = 0;
1115-
for (mp = root_menu; mp != NULL; mp = mp->next)
1115+
FOR_ALL_MENUS(mp)
11161116
{
11171117
if (mp->id != (Widget)0 && menu_is_menubar(mp->name))
11181118
{
@@ -2928,7 +2928,7 @@ gui_mch_show_toolbar(int showit)
29282928
vimmenu_T *toolbar;
29292929
vimmenu_T *cur;
29302930

2931-
for (toolbar = root_menu; toolbar; toolbar = toolbar->next)
2931+
FOR_ALL_MENUS(toolbar)
29322932
if (menu_is_toolbar(toolbar->dname))
29332933
break;
29342934
// Assumption: toolbar is NULL if there is no toolbar,

src/gui_w32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7591,7 +7591,7 @@ gui_mch_tearoff(
75917591
for (col = 0; col < 2; col++)
75927592
{
75937593
columnWidths[col] = 0;
7594-
for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7594+
FOR_ALL_CHILD_MENUS(menu, pmenu)
75957595
{
75967596
// Use "dname" here to compute the width of the visible text.
75977597
text = (col == 0) ? pmenu->dname : pmenu->actext;

src/list.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
2020
// List heads for garbage collection.
2121
static list_T *first_list = NULL; // list of all lists
2222

23+
#define FOR_ALL_WATCHERS(l, lw) \
24+
for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25+
2326
static void list_free_item(list_T *l, listitem_T *item);
2427

2528
/*
@@ -42,7 +45,7 @@ list_rem_watch(list_T *l, listwatch_T *lwrem)
4245
listwatch_T *lw, **lwp;
4346

4447
lwp = &l->lv_watch;
45-
for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
48+
FOR_ALL_WATCHERS(l, lw)
4649
{
4750
if (lw == lwrem)
4851
{
@@ -62,7 +65,7 @@ list_fix_watch(list_T *l, listitem_T *item)
6265
{
6366
listwatch_T *lw;
6467

65-
for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
68+
FOR_ALL_WATCHERS(l, lw)
6669
if (lw->lw_item == item)
6770
lw->lw_item = item->li_next;
6871
}

0 commit comments

Comments
 (0)