blob: 571fed0a7edc1052e9a168838e25c35dec1741b1 [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 Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010037#define LTOREQ_POS(a, b) (LT_POS(a, b) || EQUAL_POS(a, b))
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
39/*
Bram Moolenaar1c465442017-03-12 20:10:05 +010040 * VIM_ISWHITE() is used for "^" and the like. It differs from isspace()
41 * because it doesn't include <CR> and <LF> and the like.
42 */
43#define VIM_ISWHITE(x) ((x) == ' ' || (x) == '\t')
44
45/*
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010046 * LINEEMPTY() - return TRUE if the line is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010048#define LINEEMPTY(p) (*ml_get(p) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
50/*
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010051 * BUFEMPTY() - return TRUE if the current buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010053#define BUFEMPTY() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55/*
56 * toupper() and tolower() that use the current locale.
Bram Moolenaardeefb632007-08-15 18:41:34 +000057 * On some systems toupper()/tolower() only work on lower/uppercase
58 * characters, first use islower() or isupper() then.
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
60 * range 0 - 255. toupper()/tolower() on some systems can't handle others.
Bram Moolenaardeefb632007-08-15 18:41:34 +000061 * Note: It is often better to use MB_TOLOWER() and MB_TOUPPER(), because many
62 * toupper() and tolower() implementations only work for ASCII.
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 */
64#ifdef MSWIN
65# define TOUPPER_LOC(c) toupper_tab[(c) & 255]
66# define TOLOWER_LOC(c) tolower_tab[(c) & 255]
67#else
68# ifdef BROKEN_TOUPPER
69# define TOUPPER_LOC(c) (islower(c) ? toupper(c) : (c))
70# define TOLOWER_LOC(c) (isupper(c) ? tolower(c) : (c))
71# else
72# define TOUPPER_LOC toupper
73# define TOLOWER_LOC tolower
74# endif
75#endif
76
77/* toupper() and tolower() for ASCII only and ignore the current locale. */
78#ifdef EBCDIC
79# define TOUPPER_ASC(c) (islower(c) ? toupper(c) : (c))
80# define TOLOWER_ASC(c) (isupper(c) ? tolower(c) : (c))
81#else
82# define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
83# define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
84#endif
85
86/*
87 * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters. But
Bram Moolenaar78622822005-08-23 21:00:13 +000088 * don't use them for negative values!
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +010090#define MB_ISLOWER(c) vim_islower(c)
91#define MB_ISUPPER(c) vim_isupper(c)
92#define MB_TOLOWER(c) vim_tolower(c)
93#define MB_TOUPPER(c) vim_toupper(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar2d473ab2013-06-12 17:12:24 +020095/* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
96 * non-zero for superscript 1. Also avoids that isdigit() crashes for numbers
97 * below 0 and above 255. */
98#define VIM_ISDIGIT(c) ((unsigned)(c) - '0' < 10)
99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100/* Like isalpha() but reject non-ASCII characters. Can't be used with a
101 * special key (negative value). */
102#ifdef EBCDIC
103# define ASCII_ISALPHA(c) isalpha(c)
104# define ASCII_ISALNUM(c) isalnum(c)
105# define ASCII_ISLOWER(c) islower(c)
106# define ASCII_ISUPPER(c) isupper(c)
107#else
Bram Moolenaar0ea4a6b2013-06-12 14:10:26 +0200108# define ASCII_ISLOWER(c) ((unsigned)(c) - 'a' < 26)
109# define ASCII_ISUPPER(c) ((unsigned)(c) - 'A' < 26)
Bram Moolenaar2d473ab2013-06-12 17:12:24 +0200110# define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
111# define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
Bram Moolenaar42a45122015-07-10 17:56:23 +0200114/* Returns empty string if it is NULL. */
Bram Moolenaar669cac02016-02-25 15:25:03 +0100115#define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")
Bram Moolenaar42a45122015-07-10 17:56:23 +0200116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_LANGMAP
118/*
119 * Adjust chars in a language according to 'langmap' option.
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000120 * NOTE that there is no noticeable overhead if 'langmap' is not set.
121 * When set the overhead for characters < 256 is small.
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100122 * Don't apply 'langmap' if the character comes from the Stuff buffer or from
123 * a mapping and the langnoremap option was set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 * The do-while is just to ignore a ';' after the macro.
125 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100126# define LANGMAP_ADJUST(c, condition) \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000127 do { \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100128 if (*p_langmap \
129 && (condition) \
Bram Moolenaar920694c2016-08-21 17:45:02 +0200130 && (p_lrm || (!p_lrm && KeyTyped)) \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100131 && !KeyStuffed \
132 && (c) >= 0) \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000133 { \
134 if ((c) < 256) \
135 c = langmap_mapchar[c]; \
136 else \
137 c = langmap_adjust_mb(c); \
138 } \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 } while (0)
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000140#else
141# define LANGMAP_ADJUST(c, condition) /* nop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#endif
143
144/*
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100145 * VIM_ISBREAK() is used very often if 'linebreak' is set, use a macro to make
146 * it work fast. Only works for single byte characters!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100148#define VIM_ISBREAK(c) ((c) < 256 && breakat_flags[(char_u)(c)])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150/*
151 * On VMS file names are different and require a translation.
152 * On the Mac open() has only two arguments.
153 */
154#ifdef VMS
155# define mch_access(n, p) access(vms_fixfilename(n), (p))
156 /* see mch_open() comment */
157# define mch_fopen(n, p) fopen(vms_fixfilename(n), (p))
158# define mch_fstat(n, p) fstat(vms_fixfilename(n), (p))
159 /* VMS does not have lstat() */
160# define mch_stat(n, p) stat(vms_fixfilename(n), (p))
Bram Moolenaarde5e2c22016-11-04 20:35:31 +0100161# define mch_rmdir(n) rmdir(vms_fixfilename(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100163# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# define mch_access(n, p) access((n), (p))
165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166# define mch_fstat(n, p) fstat((n), (p))
Bram Moolenaar4f974752019-02-17 17:44:42 +0100167# ifdef MSWIN // has its own mch_stat() function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# define mch_stat(n, p) vim_stat((n), (p))
169# else
170# ifdef STAT_IGNORES_SLASH
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100171# define mch_stat(n, p) vim_stat((n), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172# else
173# define mch_stat(n, p) stat((n), (p))
174# endif
175# endif
176#endif
177
Bram Moolenaar269ec652004-07-29 08:43:53 +0000178#ifdef HAVE_LSTAT
179# define mch_lstat(n, p) lstat((n), (p))
180#else
181# define mch_lstat(n, p) mch_stat((n), (p))
182#endif
183
Bram Moolenaard0573012017-10-28 21:11:06 +0200184#ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185/*
186 * It is possible to force some record format with:
187 * # 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 +0000188 * but it is not recommended, because it can destroy indexes etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 */
Bram Moolenaard0573012017-10-28 21:11:06 +0200190# define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif
192
193/* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
194#if defined(UNIX) || defined(VMS) /* open in rw------- mode */
195# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
196#else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100197# if defined(MSWIN) // open read/write
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198# define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
199# else
200# define mch_open_rw(n, f) mch_open((n), (f), 0)
201# endif
202#endif
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifdef STARTUPTIME
Bram Moolenaar6f470022018-04-10 18:47:20 +0200205# define TIME_MSG(s) do { if (time_fd != NULL) time_msg(s, NULL); } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200207# define TIME_MSG(s) do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#endif
209
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200210#define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
212#ifdef FEAT_ARABIC
Bram Moolenaardc4fa192019-03-22 16:33:15 +0100213# define ARABIC_CHAR(ch) (((ch) & 0xFF00) == 0x0600)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
215#else
216# define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
217#endif
218
219#ifdef FEAT_RIGHTLEFT
220 /* Whether to draw the vertical bar on the right side of the cell. */
221# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
222#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000223
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000224/*
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100225 * MB_PTR_ADV(): advance a pointer to the next character, taking care of
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000226 * multi-byte characters if needed.
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100227 * MB_PTR_BACK(): backup a pointer to the previous character, taking care of
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000228 * multi-byte characters if needed.
Bram Moolenaarfd371682005-01-14 21:42:54 +0000229 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
Bram Moolenaar78984f52005-08-01 07:19:10 +0000230 * PTR2CHAR(): get character from pointer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000231 */
Bram Moolenaard0573012017-10-28 21:11:06 +0200232/* Get the length of the character p points to, including composing chars */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100233#define MB_PTR2LEN(p) (has_mbyte ? (*mb_ptr2len)(p) : 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000234/* Advance multi-byte pointer, skip over composing chars. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100235#define MB_PTR_ADV(p) p += has_mbyte ? (*mb_ptr2len)(p) : 1
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000236/* Advance multi-byte pointer, do not skip over composing chars. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100237#define MB_CPTR_ADV(p) p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
Bram Moolenaar24dc2302014-05-13 20:19:58 +0200238/* Backup multi-byte pointer. Only use with "p" > "s" ! */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100239#define MB_PTR_BACK(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000240/* get length of multi-byte char, not including composing chars */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100241#define MB_CPTR2LEN(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000242
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100243#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 +0100244#define MB_CHARLEN(p) (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
245#define MB_CHAR2LEN(c) (has_mbyte ? mb_char2len(c) : 1)
246#define PTR2CHAR(p) (has_mbyte ? mb_ptr2char(p) : (int)*(p))
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000247
248#ifdef FEAT_AUTOCHDIR
Bram Moolenaar6f470022018-04-10 18:47:20 +0200249# define DO_AUTOCHDIR do { if (p_acd) do_autochdir(); } while (0)
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000250#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200251# define DO_AUTOCHDIR do { /**/ } while (0)
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000252#endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200253
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100254#define RESET_BINDING(wp) do { (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE; \
255 } while (0)
Bram Moolenaar43335ea2015-09-09 20:59:37 +0200256
257#ifdef FEAT_DIFF
258# define PLINES_NOFILL(x) plines_nofill(x)
259#else
260# define PLINES_NOFILL(x) plines(x)
261#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
267#if defined(FEAT_EVAL) && defined(FEAT_FLOAT)
268# include <float.h>
269# if defined(HAVE_MATH_H)
270 /* for isnan() and isinf() */
271# include <math.h>
272# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100273# ifdef USING_FLOAT_STUFF
Bram Moolenaar4f974752019-02-17 17:44:42 +0100274# ifdef MSWIN
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100275# ifndef isnan
276# define isnan(x) _isnan(x)
Bram Moolenaar0c171712016-03-04 22:57:20 +0100277 static __inline int isinf(double x) { return !_finite(x) && !_isnan(x); }
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100278# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100279# else
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100280# ifndef HAVE_ISNAN
281 static inline int isnan(double x) { return x != x; }
282# endif
283# ifndef HAVE_ISINF
284 static inline int isinf(double x) { return !isnan(x) && isnan(x - x); }
285# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100286# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100287# if !defined(INFINITY)
288# if defined(DBL_MAX)
Bram Moolenaar98500fd2016-11-06 14:17:16 +0100289# ifdef VMS
290# define INFINITY DBL_MAX
291# else
292# define INFINITY (DBL_MAX+DBL_MAX)
293# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100294# else
295# define INFINITY (1.0 / 0.0)
296# endif
297# endif
298# if !defined(NAN)
299# define NAN (INFINITY-INFINITY)
300# endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +0200301# if !defined(DBL_EPSILON)
302# define DBL_EPSILON 2.2204460492503131e-16
303# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100304# endif
305#endif
Bram Moolenaar84026842016-07-17 20:37:43 +0200306
307/*
308 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
309 * This avoids adding a pointer to the hashtab item.
310 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
311 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
312 * HI2DI() converts a hashitem pointer to a dictitem pointer.
313 */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100314#define DI2HIKEY(di) ((di)->di_key)
315#define HIKEY2DI(p) ((dictitem_T *)(p - offsetof(dictitem_T, di_key)))
316#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
317
318/*
319 * Flush control functions.
320 */
321#ifdef FEAT_GUI
322# define mch_enable_flush() gui_enable_flush()
323# define mch_disable_flush() gui_disable_flush()
324#else
325# define mch_enable_flush()
326# define mch_disable_flush()
327#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +0100328
329/*
330 * Like vim_free(), and also set the pointer to NULL.
331 */
332#define VIM_CLEAR(p) \
333 do { \
334 if ((p) != NULL) { \
335 vim_free(p); \
336 (p) = NULL; \
337 } \
338 } while (0)