blob: d86097ced3a526a83d38ef233b168f88d373d82e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * macros.h: macro definitions for often used code
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011 *
12 * Macros should be ALL_CAPS. An exception is for where a function is
13 * replaced and an argument is not used more than once.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
16/*
Bram Moolenaar630afe82018-06-28 19:26:28 +020017 * PBYTE(lp, c) - put byte 'c' at position 'lp'
Bram Moolenaar071d4272004-06-13 20:20:40 +000018 */
Bram Moolenaar630afe82018-06-28 19:26:28 +020019#define PBYTE(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
21/*
22 * Position comparisons
23 */
Bram Moolenaar29ddebe2019-01-26 17:28:26 +010024#define LT_POS(a, b) (((a).lnum != (b).lnum) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000025 ? (a).lnum < (b).lnum \
26 : (a).col != (b).col \
27 ? (a).col < (b).col \
28 : (a).coladd < (b).coladd)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +010029#define LT_POSP(a, b) (((a)->lnum != (b)->lnum) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 ? (a)->lnum < (b)->lnum \
31 : (a)->col != (b)->col \
32 ? (a)->col < (b)->col \
33 : (a)->coladd < (b)->coladd)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +010034#define EQUAL_POS(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col) && ((a).coladd == (b).coladd))
Bram Moolenaarabab0b02019-03-30 18:47:01 +010035#define CLEAR_POS(a) do {(a)->lnum = 0; (a)->col = 0; (a)->coladd = 0;} while (0)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020036#define EMPTY_POS(a) ((a).lnum == 0 && (a).col == 0 && (a).coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010038#define LTOREQ_POS(a, b) (LT_POS(a, b) || EQUAL_POS(a, b))
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +020041 * VIM_ISWHITE() differs from isspace() because it doesn't include <CR> and
42 * <LF> and the like.
Bram Moolenaar1c465442017-03-12 20:10:05 +010043 */
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +020044#define VIM_ISWHITE(x) ((x) == ' ' || (x) == '\t')
45#define IS_WHITE_OR_NUL(x) ((x) == ' ' || (x) == '\t' || (x) == NUL)
Christian Brabandt00cb2472023-09-05 20:46:25 +020046#define IS_WHITE_NL_OR_NUL(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +010047
48/*
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010049 * LINEEMPTY() - return TRUE if the line is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010051#define LINEEMPTY(p) (*ml_get(p) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010054 * BUFEMPTY() - return TRUE if the current buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010056#define BUFEMPTY() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
59 * toupper() and tolower() that use the current locale.
Bram Moolenaardeefb632007-08-15 18:41:34 +000060 * On some systems toupper()/tolower() only work on lower/uppercase
61 * characters, first use islower() or isupper() then.
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
63 * range 0 - 255. toupper()/tolower() on some systems can't handle others.
Bram Moolenaardeefb632007-08-15 18:41:34 +000064 * Note: It is often better to use MB_TOLOWER() and MB_TOUPPER(), because many
65 * toupper() and tolower() implementations only work for ASCII.
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 */
67#ifdef MSWIN
68# define TOUPPER_LOC(c) toupper_tab[(c) & 255]
69# define TOLOWER_LOC(c) tolower_tab[(c) & 255]
70#else
71# ifdef BROKEN_TOUPPER
72# define TOUPPER_LOC(c) (islower(c) ? toupper(c) : (c))
73# define TOLOWER_LOC(c) (isupper(c) ? tolower(c) : (c))
74# else
75# define TOUPPER_LOC toupper
76# define TOLOWER_LOC tolower
77# endif
78#endif
79
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010080// toupper() and tolower() for ASCII only and ignore the current locale.
Bram Moolenaar424bcae2022-01-31 14:59:41 +000081#define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
82#define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
84/*
85 * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters. But
Bram Moolenaar78622822005-08-23 21:00:13 +000086 * don't use them for negative values!
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +010088#define MB_ISLOWER(c) vim_islower(c)
89#define MB_ISUPPER(c) vim_isupper(c)
90#define MB_TOLOWER(c) vim_tolower(c)
91#define MB_TOUPPER(c) vim_toupper(c)
Bram Moolenaar59de4172020-06-09 19:34:54 +020092#define MB_CASEFOLD(c) (enc_utf8 ? utf_fold(c) : MB_TOLOWER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010094// Use our own isdigit() replacement, because on MS-Windows isdigit() returns
95// non-zero for superscript 1. Also avoids that isdigit() crashes for numbers
96// below 0 and above 255.
Bram Moolenaar2d473ab2013-06-12 17:12:24 +020097#define VIM_ISDIGIT(c) ((unsigned)(c) - '0' < 10)
98
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010099// Like isalpha() but reject non-ASCII characters. Can't be used with a
100// special key (negative value).
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000101#define ASCII_ISLOWER(c) ((unsigned)(c) - 'a' < 26)
102#define ASCII_ISUPPER(c) ((unsigned)(c) - 'A' < 26)
103#define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
104#define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100106// Returns empty string if it is NULL.
Bram Moolenaar669cac02016-02-25 15:25:03 +0100107#define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")
Bram Moolenaar42a45122015-07-10 17:56:23 +0200108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#ifdef FEAT_LANGMAP
110/*
111 * Adjust chars in a language according to 'langmap' option.
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000112 * NOTE that there is no noticeable overhead if 'langmap' is not set.
113 * When set the overhead for characters < 256 is small.
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100114 * Don't apply 'langmap' if the character comes from the Stuff buffer or from
115 * a mapping and the langnoremap option was set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 * The do-while is just to ignore a ';' after the macro.
117 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100118# define LANGMAP_ADJUST(c, condition) \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000119 do { \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100120 if (*p_langmap \
121 && (condition) \
Bram Moolenaar920694c2016-08-21 17:45:02 +0200122 && (p_lrm || (!p_lrm && KeyTyped)) \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100123 && !KeyStuffed \
124 && (c) >= 0) \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000125 { \
126 if ((c) < 256) \
127 c = langmap_mapchar[c]; \
128 else \
129 c = langmap_adjust_mb(c); \
130 } \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 } while (0)
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000132#else
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100133# define LANGMAP_ADJUST(c, condition) // nop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
135
136/*
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100137 * VIM_ISBREAK() is used very often if 'linebreak' is set, use a macro to make
138 * it work fast. Only works for single byte characters!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100140#define VIM_ISBREAK(c) ((c) < 256 && breakat_flags[(char_u)(c)])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142/*
143 * On VMS file names are different and require a translation.
144 * On the Mac open() has only two arguments.
145 */
146#ifdef VMS
147# define mch_access(n, p) access(vms_fixfilename(n), (p))
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100148 // see mch_open() comment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define mch_fopen(n, p) fopen(vms_fixfilename(n), (p))
Bram Moolenaar467676d2020-12-30 13:14:45 +0100150# define mch_fstat(n, p) fstat((n), (p))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100151# undef HAVE_LSTAT // VMS does not have lstat()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152# define mch_stat(n, p) stat(vms_fixfilename(n), (p))
153#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100154# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155# define mch_access(n, p) access((n), (p))
156# endif
Bram Moolenaarc7534782020-08-05 12:10:50 +0200157
K.Takatac351dc12022-01-24 11:24:08 +0000158// Use 64-bit fstat function on MS-Windows.
Bram Moolenaarc7534782020-08-05 12:10:50 +0200159// NOTE: This condition is the same as for the stat_T type.
K.Takatac351dc12022-01-24 11:24:08 +0000160# ifdef MSWIN
Bram Moolenaarc7534782020-08-05 12:10:50 +0200161# define mch_fstat(n, p) _fstat64((n), (p))
162# else
163# define mch_fstat(n, p) fstat((n), (p))
164# endif
165
Bram Moolenaar4f974752019-02-17 17:44:42 +0100166# ifdef MSWIN // has its own mch_stat() function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167# define mch_stat(n, p) vim_stat((n), (p))
168# else
169# ifdef STAT_IGNORES_SLASH
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100170# define mch_stat(n, p) vim_stat((n), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171# else
172# define mch_stat(n, p) stat((n), (p))
173# endif
174# endif
175#endif
176
Bram Moolenaar269ec652004-07-29 08:43:53 +0000177#ifdef HAVE_LSTAT
178# define mch_lstat(n, p) lstat((n), (p))
179#else
180# define mch_lstat(n, p) mch_stat((n), (p))
181#endif
182
Bram Moolenaard0573012017-10-28 21:11:06 +0200183#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * It is possible to force some record format with:
186 * # define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p)), "rat=cr", "rfm=stmlf", "mrs=0")
Bram Moolenaarff1d0d42007-05-10 17:24:16 +0000187 * but it is not recommended, because it can destroy indexes etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 */
Bram Moolenaard0573012017-10-28 21:11:06 +0200189# define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
191
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100192// mch_open_rw(): invoke mch_open() with third argument for user R/W.
193#if defined(UNIX) || defined(VMS) // open in rw------- mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
195#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100196# if defined(MSWIN) // open read/write
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
198# else
199# define mch_open_rw(n, f) mch_open((n), (f), 0)
200# endif
201#endif
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifdef STARTUPTIME
Bram Moolenaar6f470022018-04-10 18:47:20 +0200204# define TIME_MSG(s) do { if (time_fd != NULL) time_msg(s, NULL); } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200206# define TIME_MSG(s) do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
208
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200209#define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211#ifdef FEAT_ARABIC
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100212# define ARABIC_CHAR(ch) (((ch) & 0xFF00) == 0x0600)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
214#else
215# define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
216#endif
217
218#ifdef FEAT_RIGHTLEFT
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100219 // Whether to draw the vertical bar on the right side of the cell.
Bram Moolenaar24959102022-05-07 20:01:16 +0100220# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & MODE_CMDLINE) || cmdmsg_rl))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000222
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000223/*
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100224 * MB_PTR_ADV(): advance a pointer to the next character, taking care of
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000225 * multi-byte characters if needed.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100226 * MB_PTR_BACK(): backup a pointer to the previous character, taking care of
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000227 * multi-byte characters if needed.
Bram Moolenaarfd371682005-01-14 21:42:54 +0000228 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
Bram Moolenaar78984f52005-08-01 07:19:10 +0000229 * PTR2CHAR(): get character from pointer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000230 */
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100231// Advance multi-byte pointer, skip over composing chars.
Bram Moolenaar1614a142019-10-06 22:00:13 +0200232#define MB_PTR_ADV(p) p += (*mb_ptr2len)(p)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100233// Advance multi-byte pointer, do not skip over composing chars.
Bram Moolenaar1614a142019-10-06 22:00:13 +0200234#define MB_CPTR_ADV(p) p += enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100235// Backup multi-byte pointer. Only use with "p" > "s" !
kylo2529dac9b12022-03-27 20:05:17 +0100236#define MB_PTR_BACK(s, p) p -= has_mbyte ? ((*mb_head_off)(s, (p) - 1) + 1) : 1
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100237// get length of multi-byte char, not including composing chars
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100238#define MB_CPTR2LEN(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000239
kylo2529dac9b12022-03-27 20:05:17 +0100240#define MB_COPY_CHAR(f, t) do { if (has_mbyte) mb_copy_char(&(f), &(t)); else *(t)++ = *(f)++; } while (0)
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100241#define MB_CHARLEN(p) (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
242#define MB_CHAR2LEN(c) (has_mbyte ? mb_char2len(c) : 1)
243#define PTR2CHAR(p) (has_mbyte ? mb_ptr2char(p) : (int)*(p))
Bram Moolenaarc9471b12023-05-09 15:00:00 +0100244#define MB_CHAR2BYTES(c, b) do { if (has_mbyte) (b) += (*mb_char2bytes)((c), (b)); else *(b)++ = (c); } while (0)
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000245
246#ifdef FEAT_AUTOCHDIR
Bram Moolenaar6f470022018-04-10 18:47:20 +0200247# define DO_AUTOCHDIR do { if (p_acd) do_autochdir(); } while (0)
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000248#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200249# define DO_AUTOCHDIR do { /**/ } while (0)
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000250#endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200251
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100252#define RESET_BINDING(wp) do { (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE; \
253 } while (0)
Bram Moolenaar43335ea2015-09-09 20:59:37 +0200254
255#ifdef FEAT_DIFF
256# define PLINES_NOFILL(x) plines_nofill(x)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000257# define PLINES_WIN_NOFILL(w, l, h) plines_win_nofill((w), (l), (h))
Bram Moolenaar43335ea2015-09-09 20:59:37 +0200258#else
259# define PLINES_NOFILL(x) plines(x)
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000260# define PLINES_WIN_NOFILL(w, l, h) plines_win((w), (l), (h))
Bram Moolenaar43335ea2015-09-09 20:59:37 +0200261#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200262
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100263#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200264# define MESSAGE_QUEUE
265#endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100266
Bram Moolenaar96caa552022-09-17 21:57:43 +0100267#include <float.h>
268#if defined(HAVE_MATH_H)
269 // for isnan() and isinf()
270# include <math.h>
271#endif
272
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100273#if defined(FEAT_EVAL)
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100274# ifdef USING_FLOAT_STUFF
Bram Moolenaar4f974752019-02-17 17:44:42 +0100275# ifdef MSWIN
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100276# ifndef isnan
277# define isnan(x) _isnan(x)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000278 static __inline int isinf(double x)
279 { return !_finite(x) && !_isnan(x); }
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100280# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100281# else
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100282# ifndef HAVE_ISNAN
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000283 static inline int isnan(double x)
284 { return x != x; }
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100285# endif
286# ifndef HAVE_ISINF
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000287 static inline int isinf(double x)
288 { return !isnan(x) && isnan(x - x); }
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100289# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100290# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100291# if !defined(INFINITY)
292# if defined(DBL_MAX)
Bram Moolenaar98500fd2016-11-06 14:17:16 +0100293# ifdef VMS
294# define INFINITY DBL_MAX
295# else
296# define INFINITY (DBL_MAX+DBL_MAX)
297# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100298# else
299# define INFINITY (1.0 / 0.0)
300# endif
301# endif
302# if !defined(NAN)
303# define NAN (INFINITY-INFINITY)
304# endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +0200305# if !defined(DBL_EPSILON)
306# define DBL_EPSILON 2.2204460492503131e-16
307# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100308# endif
309#endif
Bram Moolenaar84026842016-07-17 20:37:43 +0200310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311#ifdef FEAT_EVAL
312# define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
313#endif
314
Bram Moolenaar84026842016-07-17 20:37:43 +0200315/*
316 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
317 * This avoids adding a pointer to the hashtab item.
318 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
319 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
320 * HI2DI() converts a hashitem pointer to a dictitem pointer.
321 */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100322#define DI2HIKEY(di) ((di)->di_key)
kylo2529dac9b12022-03-27 20:05:17 +0100323#define HIKEY2DI(p) ((dictitem_T *)((p) - offsetof(dictitem_T, di_key)))
Bram Moolenaara338adc2018-01-31 20:51:47 +0100324#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
325
326/*
327 * Flush control functions.
328 */
329#ifdef FEAT_GUI
330# define mch_enable_flush() gui_enable_flush()
331# define mch_disable_flush() gui_disable_flush()
332#else
333# define mch_enable_flush()
334# define mch_disable_flush()
335#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +0100336
337/*
338 * Like vim_free(), and also set the pointer to NULL.
339 */
340#define VIM_CLEAR(p) \
341 do { \
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000342 if ((p) != NULL) \
343 { \
Bram Moolenaard23a8232018-02-10 18:45:26 +0100344 vim_free(p); \
345 (p) = NULL; \
346 } \
347 } while (0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200348
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100349// Whether a command index indicates a user command.
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200350#define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200351
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200352// Give an error in curwin is a popup window and evaluate to TRUE.
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100353#ifdef FEAT_PROP_POPUP
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100354# define WIN_IS_POPUP(wp) ((wp)->w_popup_flags != 0)
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100355# define ERROR_IF_POPUP_WINDOW error_if_popup_window(FALSE)
356# define ERROR_IF_ANY_POPUP_WINDOW error_if_popup_window(TRUE)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200357#else
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100358# define WIN_IS_POPUP(wp) 0
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200359# define ERROR_IF_POPUP_WINDOW 0
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100360# define ERROR_IF_ANY_POPUP_WINDOW 0
Bram Moolenaar284d1c22020-02-01 22:39:32 +0100361#endif
362#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
363# define ERROR_IF_TERM_POPUP_WINDOW error_if_term_popup_window()
364#else
Bram Moolenaar219c7d02020-02-01 21:57:29 +0100365# define ERROR_IF_TERM_POPUP_WINDOW 0
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200366#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +0100367
368
369#ifdef ABORT_ON_INTERNAL_ERROR
ichizok7e5fe382023-04-15 13:17:50 +0100370# define ESTACK_CHECK_DECLARATION int estack_len_before
371# define ESTACK_CHECK_SETUP do { estack_len_before = exestack.ga_len; } while (0)
372# define ESTACK_CHECK_NOW \
373 do { \
374 if (estack_len_before != exestack.ga_len) \
375 siemsg("Exestack length expected: %d, actual: %d", estack_len_before, exestack.ga_len); \
376 } while (0)
377# define CHECK_CURBUF \
378 do { \
379 if (curwin != NULL && curwin->w_buffer != curbuf) \
380 iemsg("curbuf != curwin->w_buffer"); \
381 } while (0)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100382#else
ichizok7e5fe382023-04-15 13:17:50 +0100383# define ESTACK_CHECK_DECLARATION do { /**/ } while (0)
384# define ESTACK_CHECK_SETUP do { /**/ } while (0)
385# define ESTACK_CHECK_NOW do { /**/ } while (0)
386# define CHECK_CURBUF do { /**/ } while (0)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100387#endif
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200388
389// Inline the condition for performance.
ichizok7e5fe382023-04-15 13:17:50 +0100390#define CHECK_LIST_MATERIALIZE(l) \
391 do { \
392 if ((l)->lv_first == &range_list_item) \
393 range_list_materialize(l); \
394 } while (0)
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200395
Bram Moolenaar35578162021-08-02 19:10:38 +0200396// Inlined version of ga_grow() with optimized condition that it fails.
kylo2529dac9b12022-03-27 20:05:17 +0100397#define GA_GROW_FAILS(gap, n) unlikely((((gap)->ga_maxlen - (gap)->ga_len < (n)) ? ga_grow_inner((gap), (n)) : OK) == FAIL)
Bram Moolenaar35578162021-08-02 19:10:38 +0200398// Inlined version of ga_grow() with optimized condition that it succeeds.
kylo2529dac9b12022-03-27 20:05:17 +0100399#define GA_GROW_OK(gap, n) likely((((gap)->ga_maxlen - (gap)->ga_len < (n)) ? ga_grow_inner((gap), (n)) : OK) == OK)
Bram Moolenaar4fa11752021-03-03 13:26:02 +0100400
401#ifndef MIN
402# define MIN(a, b) ((a) < (b) ? (a) : (b))
403#endif
404#ifndef MAX
405# define MAX(a, b) ((a) > (b) ? (a) : (b))
406#endif
K.Takataeeec2542021-06-02 13:28:16 +0200407
408// Length of the array.
409#define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000410
411#ifdef FEAT_MENU
412#define FOR_ALL_MENUS(m) \
413 for ((m) = root_menu; (m) != NULL; (m) = (m)->next)
414#define FOR_ALL_CHILD_MENUS(p, c) \
415 for ((c) = (p)->children; (c) != NULL; (c) = (c)->next)
416#endif
417
418#define FOR_ALL_WINDOWS(wp) \
419 for ((wp) = firstwin; (wp) != NULL; (wp) = (wp)->w_next)
420#define FOR_ALL_FRAMES(frp, first_frame) \
421 for ((frp) = first_frame; (frp) != NULL; (frp) = (frp)->fr_next)
422#define FOR_ALL_TABPAGES(tp) \
423 for ((tp) = first_tabpage; (tp) != NULL; (tp) = (tp)->tp_next)
424#define FOR_ALL_WINDOWS_IN_TAB(tp, wp) \
425 for ((wp) = ((tp) == NULL || (tp) == curtab) \
426 ? firstwin : (tp)->tp_firstwin; (wp); (wp) = (wp)->w_next)
427/*
428 * When using this macro "break" only breaks out of the inner loop. Use "goto"
429 * to break out of the tabpage loop.
430 */
431#define FOR_ALL_TAB_WINDOWS(tp, wp) \
432 for ((tp) = first_tabpage; (tp) != NULL; (tp) = (tp)->tp_next) \
433 for ((wp) = ((tp) == curtab) \
434 ? firstwin : (tp)->tp_firstwin; (wp); (wp) = (wp)->w_next)
435
436#define FOR_ALL_POPUPWINS(wp) \
437 for ((wp) = first_popupwin; (wp) != NULL; (wp) = (wp)->w_next)
438#define FOR_ALL_POPUPWINS_IN_TAB(tp, wp) \
439 for ((wp) = (tp)->tp_first_popupwin; (wp) != NULL; (wp) = (wp)->w_next)
440
441#define FOR_ALL_BUFFERS(buf) \
442 for ((buf) = firstbuf; (buf) != NULL; (buf) = (buf)->b_next)
443
444#define FOR_ALL_BUF_WININFO(buf, wip) \
445 for ((wip) = (buf)->b_wininfo; (wip) != NULL; (wip) = (wip)->wi_next)
446
447// Iterate through all the signs placed in a buffer
448#define FOR_ALL_SIGNS_IN_BUF(buf, sign) \
449 for ((sign) = (buf)->b_signlist; (sign) != NULL; (sign) = (sign)->se_next)
450
451#ifdef FEAT_SPELL
452#define FOR_ALL_SPELL_LANGS(slang) \
453 for ((slang) = first_lang; (slang) != NULL; (slang) = (slang)->sl_next)
454#endif
455
456// Iterate over all the items in a List
457#define FOR_ALL_LIST_ITEMS(l, li) \
458 for ((li) = (l) == NULL ? NULL : (l)->lv_first; (li) != NULL; (li) = (li)->li_next)
459
460// Iterate over all the items in a hash table
461#define FOR_ALL_HASHTAB_ITEMS(ht, hi, todo) \
462 for ((hi) = (ht)->ht_array; (todo) > 0; ++(hi))