Skip to content

Commit aeea721

Browse files
committed
patch 8.2.0500: using the same loop in many places
Problem: Using the same loop in many places. Solution: Define more FOR_ALL macros. (Yegappan Lakshmanan, closes vim#5339)
1 parent f10806b commit aeea721

34 files changed

+176
-141
lines changed

src/arglist.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ do_arg_all(
10461046
// Move the already present window to below the current window
10471047
if (curwin->w_arg_idx != i)
10481048
{
1049-
for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
1049+
FOR_ALL_WINDOWS(wpnext)
10501050
{
10511051
if (wpnext->w_arg_idx == i)
10521052
{

src/autocmd.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ struct AutoPatCmd_S
235235

236236
static AutoPatCmd *active_apc_list = NULL; // stack of active autocommands
237237

238+
// Macro to loop over all the patterns for an autocmd event
239+
#define FOR_ALL_AUTOCMD_PATTERNS(event, ap) \
240+
for ((ap) = first_autopat[(int)(event)]; (ap) != NULL; (ap) = (ap)->next)
241+
238242
/*
239243
* augroups stores a list of autocmd group names.
240244
*/
@@ -456,7 +460,7 @@ aubuflocal_remove(buf_T *buf)
456460
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
457461
event = (event_T)((int)event + 1))
458462
// loop over all autocommand patterns
459-
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
463+
FOR_ALL_AUTOCMD_PATTERNS(event, ap)
460464
if (ap->buflocal_nr == buf->b_fnum)
461465
{
462466
au_remove_pat(ap);
@@ -519,7 +523,7 @@ au_del_group(char_u *name)
519523
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
520524
event = (event_T)((int)event + 1))
521525
{
522-
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
526+
FOR_ALL_AUTOCMD_PATTERNS(event, ap)
523527
if (ap->group == i && ap->pat != NULL)
524528
{
525529
give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE);
@@ -1041,7 +1045,7 @@ do_autocmd_event(
10411045
*/
10421046
if (*pat == NUL)
10431047
{
1044-
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
1048+
FOR_ALL_AUTOCMD_PATTERNS(event, ap)
10451049
{
10461050
if (forceit) // delete the AutoPat, if it's in the current group
10471051
{
@@ -2400,7 +2404,7 @@ has_autocmd(event_T event, char_u *sfname, buf_T *buf)
24002404
forward_slash(fname);
24012405
#endif
24022406

2403-
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
2407+
FOR_ALL_AUTOCMD_PATTERNS(event, ap)
24042408
if (ap->pat != NULL && ap->cmds != NULL
24052409
&& (ap->buflocal_nr == 0
24062410
? match_file_pat(NULL, &ap->reg_prog,

src/buffer.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -2911,7 +2911,7 @@ buflist_setfpos(
29112911
{
29122912
wininfo_T *wip;
29132913

2914-
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2914+
FOR_ALL_BUF_WININFO(buf, wip)
29152915
if (wip->wi_win == win)
29162916
break;
29172917
if (wip == NULL)
@@ -3004,7 +3004,7 @@ find_wininfo(
30043004
{
30053005
wininfo_T *wip;
30063006

3007-
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
3007+
FOR_ALL_BUF_WININFO(buf, wip)
30083008
if (wip->wi_win == curwin
30093009
#ifdef FEAT_DIFF
30103010
&& (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
@@ -3019,7 +3019,7 @@ find_wininfo(
30193019
#ifdef FEAT_DIFF
30203020
if (skip_diff_buffer)
30213021
{
3022-
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
3022+
FOR_ALL_BUF_WININFO(buf, wip)
30233023
if (!wininfo_other_tab_diff(wip))
30243024
break;
30253025
}
@@ -3132,7 +3132,7 @@ buflist_list(exarg_T *eap)
31323132
if (vim_strchr(eap->arg, 't'))
31333133
{
31343134
ga_init2(&buflist, sizeof(buf_T *), 50);
3135-
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3135+
FOR_ALL_BUFFERS(buf)
31363136
{
31373137
if (ga_grow(&buflist, 1) == OK)
31383138
((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;

src/change.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ check_recorded_changes(
172172
linenr_T prev_lnum;
173173
linenr_T prev_lnume;
174174

175-
for (li = buf->b_recorded_changes->lv_first; li != NULL;
176-
li = li->li_next)
175+
FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
177176
{
178177
prev_lnum = (linenr_T)dict_get_number(
179178
li->li_tv.vval.v_dict, (char_u *)"lnum");
@@ -362,8 +361,7 @@ invoke_listeners(buf_T *buf)
362361
argv[0].v_type = VAR_NUMBER;
363362
argv[0].vval.v_number = buf->b_fnum; // a:bufnr
364363

365-
366-
for (li = buf->b_recorded_changes->lv_first; li != NULL; li = li->li_next)
364+
FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
367365
{
368366
varnumber_T lnum;
369367

src/channel.c

+28-23
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ static ch_part_T channel_part_send(channel_T *channel);
6161
static ch_part_T channel_part_read(channel_T *channel);
6262
static void free_job_options(jobopt_T *opt);
6363

64+
#define FOR_ALL_CHANNELS(ch) \
65+
for ((ch) = first_channel; (ch) != NULL; (ch) = (ch)->ch_next)
66+
67+
#define FOR_ALL_JOBS(job) \
68+
for ((job) = first_job; (job) != NULL; (job) = (job)->jv_next)
69+
6470
// Whether a redraw is needed for appending a line to a buffer.
6571
static int channel_need_redraw = FALSE;
6672

@@ -476,7 +482,7 @@ free_unused_channels_contents(int copyID, int mask)
476482
// point.
477483
++safe_to_invoke_callback;
478484

479-
for (ch = first_channel; ch != NULL; ch = ch->ch_next)
485+
FOR_ALL_CHANNELS(ch)
480486
if (!channel_still_useful(ch)
481487
&& (ch->ch_copyID & mask) != (copyID & mask))
482488
{
@@ -520,8 +526,7 @@ channel_fd2channel(sock_T fd, ch_part_T *partp)
520526
ch_part_T part;
521527

522528
if (fd != INVALID_FD)
523-
for (channel = first_channel; channel != NULL;
524-
channel = channel->ch_next)
529+
FOR_ALL_CHANNELS(channel)
525530
{
526531
for (part = PART_SOCK; part < PART_IN; ++part)
527532
if (channel->ch_part[part].ch_fd == fd)
@@ -662,7 +667,7 @@ channel_gui_register_all(void)
662667
{
663668
channel_T *channel;
664669

665-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
670+
FOR_ALL_CHANNELS(channel)
666671
channel_gui_register(channel);
667672
}
668673

@@ -1569,7 +1574,7 @@ channel_buffer_free(buf_T *buf)
15691574
channel_T *channel;
15701575
ch_part_T part;
15711576

1572-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1577+
FOR_ALL_CHANNELS(channel)
15731578
for (part = PART_SOCK; part < PART_COUNT; ++part)
15741579
{
15751580
chanpart_T *ch_part = &channel->ch_part[part];
@@ -1610,7 +1615,7 @@ channel_write_any_lines(void)
16101615
{
16111616
channel_T *channel;
16121617

1613-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1618+
FOR_ALL_CHANNELS(channel)
16141619
channel_write_input(channel);
16151620
}
16161621

@@ -1625,7 +1630,7 @@ channel_write_new_lines(buf_T *buf)
16251630

16261631
// There could be more than one channel for the buffer, loop over all of
16271632
// them.
1628-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1633+
FOR_ALL_CHANNELS(channel)
16291634
{
16301635
chanpart_T *in_part = &channel->ch_part[PART_IN];
16311636
linenr_T lnum;
@@ -2604,7 +2609,7 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
26042609
// Find channels reading from this buffer and adjust their
26052610
// next-to-read line number.
26062611
buffer->b_write_to_channel = TRUE;
2607-
for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2612+
FOR_ALL_CHANNELS(ch)
26082613
{
26092614
chanpart_T *in_part = &ch->ch_part[PART_IN];
26102615

@@ -3180,7 +3185,7 @@ channel_free_all(void)
31803185
channel_T *channel;
31813186

31823187
ch_log(NULL, "channel_free_all()");
3183-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3188+
FOR_ALL_CHANNELS(channel)
31843189
channel_clear(channel);
31853190
}
31863191
#endif
@@ -3202,7 +3207,7 @@ channel_fill_wfds(int maxfd_arg, fd_set *wfds)
32023207
int maxfd = maxfd_arg;
32033208
channel_T *ch;
32043209

3205-
for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3210+
FOR_ALL_CHANNELS(ch)
32063211
{
32073212
chanpart_T *in_part = &ch->ch_part[PART_IN];
32083213

@@ -3227,7 +3232,7 @@ channel_fill_poll_write(int nfd_in, struct pollfd *fds)
32273232
int nfd = nfd_in;
32283233
channel_T *ch;
32293234

3230-
for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3235+
FOR_ALL_CHANNELS(ch)
32313236
{
32323237
chanpart_T *in_part = &ch->ch_part[PART_IN];
32333238

@@ -3821,7 +3826,7 @@ channel_handle_events(int only_keep_open)
38213826
ch_part_T part;
38223827
sock_T fd;
38233828

3824-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3829+
FOR_ALL_CHANNELS(channel)
38253830
{
38263831
if (only_keep_open && !channel->ch_keep_open)
38273832
continue;
@@ -3854,7 +3859,7 @@ channel_any_keep_open()
38543859
{
38553860
channel_T *channel;
38563861

3857-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3862+
FOR_ALL_CHANNELS(channel)
38583863
if (channel->ch_keep_open)
38593864
return TRUE;
38603865
return FALSE;
@@ -4234,7 +4239,7 @@ channel_poll_setup(int nfd_in, void *fds_in, int *towait)
42344239
struct pollfd *fds = fds_in;
42354240
ch_part_T part;
42364241

4237-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
4242+
FOR_ALL_CHANNELS(channel)
42384243
{
42394244
for (part = PART_SOCK; part < PART_IN; ++part)
42404245
{
@@ -4281,7 +4286,7 @@ channel_poll_check(int ret_in, void *fds_in)
42814286
int idx;
42824287
chanpart_T *in_part;
42834288

4284-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
4289+
FOR_ALL_CHANNELS(channel)
42854290
{
42864291
for (part = PART_SOCK; part < PART_IN; ++part)
42874292
{
@@ -4332,7 +4337,7 @@ channel_select_setup(
43324337
fd_set *wfds = wfds_in;
43334338
ch_part_T part;
43344339

4335-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
4340+
FOR_ALL_CHANNELS(channel)
43364341
{
43374342
for (part = PART_SOCK; part < PART_IN; ++part)
43384343
{
@@ -4381,7 +4386,7 @@ channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
43814386
ch_part_T part;
43824387
chanpart_T *in_part;
43834388

4384-
for (channel = first_channel; channel != NULL; channel = channel->ch_next)
4389+
FOR_ALL_CHANNELS(channel)
43854390
{
43864391
for (part = PART_SOCK; part < PART_IN; ++part)
43874392
{
@@ -5471,7 +5476,7 @@ job_any_running()
54715476
{
54725477
job_T *job;
54735478

5474-
for (job = first_job; job != NULL; job = job->jv_next)
5479+
FOR_ALL_JOBS(job)
54755480
if (job_still_useful(job))
54765481
{
54775482
ch_log(NULL, "GUI not forking because a job is running");
@@ -5570,7 +5575,7 @@ win32_build_cmd(list_T *l, garray_T *gap)
55705575
char_u *s;
55715576

55725577
range_list_materialize(l);
5573-
for (li = l->lv_first; li != NULL; li = li->li_next)
5578+
FOR_ALL_LIST_ITEMS(l, li)
55745579
{
55755580
s = tv_get_string_chk(&li->li_tv);
55765581
if (s == NULL)
@@ -5695,7 +5700,7 @@ free_unused_jobs_contents(int copyID, int mask)
56955700
int did_free = FALSE;
56965701
job_T *job;
56975702

5698-
for (job = first_job; job != NULL; job = job->jv_next)
5703+
FOR_ALL_JOBS(job)
56995704
if ((job->jv_copyID & mask) != (copyID & mask)
57005705
&& !job_still_useful(job))
57015706
{
@@ -5781,7 +5786,7 @@ job_stop_on_exit(void)
57815786
{
57825787
job_T *job;
57835788

5784-
for (job = first_job; job != NULL; job = job->jv_next)
5789+
FOR_ALL_JOBS(job)
57855790
if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
57865791
mch_signal_job(job, job->jv_stoponexit);
57875792
}
@@ -5795,7 +5800,7 @@ has_pending_job(void)
57955800
{
57965801
job_T *job;
57975802

5798-
for (job = first_job; job != NULL; job = job->jv_next)
5803+
FOR_ALL_JOBS(job)
57995804
// Only should check if the channel has been closed, if the channel is
58005805
// open the job won't exit.
58015806
if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
@@ -6589,7 +6594,7 @@ job_info_all(list_T *l)
65896594
job_T *job;
65906595
typval_T tv;
65916596

6592-
for (job = first_job; job != NULL; job = job->jv_next)
6597+
FOR_ALL_JOBS(job)
65936598
{
65946599
tv.v_type = VAR_JOB;
65956600
tv.vval.v_job = job;

src/cmdexpand.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,7 @@ ExpandUserList(
25872587

25882588
ga_init2(&ga, (int)sizeof(char *), 3);
25892589
// Loop over the items in the list.
2590-
for (li = retlist->lv_first; li != NULL; li = li->li_next)
2590+
FOR_ALL_LIST_ITEMS(retlist, li)
25912591
{
25922592
if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
25932593
continue; // Skip non-string items and empty strings

src/diff.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, li
9090
static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
9191
static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf);
9292

93+
#define FOR_ALL_DIFFBLOCKS_IN_TAB(tp, dp) \
94+
for ((dp) = (tp)->tp_first_diff; (dp) != NULL; (dp) = (dp)->df_next)
95+
9396
/*
9497
* Called when deleting or unloading a buffer: No longer make a diff with it.
9598
*/
@@ -1857,7 +1860,7 @@ diff_check(win_T *wp, linenr_T lnum)
18571860
#endif
18581861

18591862
// search for a change that includes "lnum" in the list of diffblocks.
1860-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
1863+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
18611864
if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
18621865
break;
18631866
if (dp == NULL || lnum < dp->df_lnum[idx])
@@ -2069,7 +2072,7 @@ diff_set_topline(win_T *fromwin, win_T *towin)
20692072
towin->w_topfill = 0;
20702073

20712074
// search for a change that includes "lnum" in the list of diffblocks.
2072-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2075+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
20732076
if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
20742077
break;
20752078
if (dp == NULL)
@@ -2374,7 +2377,7 @@ diff_find_change(
23742377
}
23752378

23762379
// search for a change that includes "lnum" in the list of diffblocks.
2377-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2380+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
23782381
if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
23792382
break;
23802383
if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
@@ -2508,7 +2511,7 @@ diff_infold(win_T *wp, linenr_T lnum)
25082511
if (curtab->tp_first_diff == NULL)
25092512
return TRUE;
25102513

2511-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2514+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
25122515
{
25132516
// If this change is below the line there can't be any further match.
25142517
if (dp->df_lnum[idx] - diff_context > lnum)
@@ -3001,7 +3004,7 @@ diff_get_corresponding_line_int(
30013004
if (curtab->tp_first_diff == NULL) // no diffs today
30023005
return lnum1;
30033006

3004-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
3007+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
30053008
{
30063009
if (dp->df_lnum[idx1] > lnum1)
30073010
return lnum1 - baseline;
@@ -3070,7 +3073,7 @@ diff_lnum_win(linenr_T lnum, win_T *wp)
30703073
ex_diffupdate(NULL); // update after a big change
30713074

30723075
// search for a change that includes "lnum" in the list of diffblocks.
3073-
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
3076+
FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp)
30743077
if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
30753078
break;
30763079

0 commit comments

Comments
 (0)