blob: ca54cb42e01fdda77c20799a4bf62e07b6aed6fa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
11 */
12
13/*
14 * pchar(lp, c) - put character 'c' at position 'lp'
15 */
16#define pchar(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
17
18/*
19 * Position comparisons
20 */
21#ifdef FEAT_VIRTUALEDIT
22# define lt(a, b) (((a).lnum != (b).lnum) \
23 ? (a).lnum < (b).lnum \
24 : (a).col != (b).col \
25 ? (a).col < (b).col \
26 : (a).coladd < (b).coladd)
27# define ltp(a, b) (((a)->lnum != (b)->lnum) \
28 ? (a)->lnum < (b)->lnum \
29 : (a)->col != (b)->col \
30 ? (a)->col < (b)->col \
31 : (a)->coladd < (b)->coladd)
32# define equalpos(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col) && ((a).coladd == (b).coladd))
Bram Moolenaare1438bb2006-03-01 22:01:55 +000033# define clearpos(a) {(a)->lnum = 0; (a)->col = 0; (a)->coladd = 0;}
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#else
35# define lt(a, b) (((a).lnum != (b).lnum) \
36 ? ((a).lnum < (b).lnum) : ((a).col < (b).col))
37# define ltp(a, b) (((a)->lnum != (b)->lnum) \
38 ? ((a)->lnum < (b)->lnum) : ((a)->col < (b)->col))
39# define equalpos(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col))
Bram Moolenaare1438bb2006-03-01 22:01:55 +000040# define clearpos(a) {(a)->lnum = 0; (a)->col = 0;}
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
42
43#define ltoreq(a, b) (lt(a, b) || equalpos(a, b))
44
45/*
46 * lineempty() - return TRUE if the line is empty
47 */
48#define lineempty(p) (*ml_get(p) == NUL)
49
50/*
51 * bufempty() - return TRUE if the current buffer is empty
52 */
53#define bufempty() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
54
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 */
90#ifdef FEAT_MBYTE
Bram Moolenaar78622822005-08-23 21:00:13 +000091# define MB_ISLOWER(c) vim_islower(c)
92# define MB_ISUPPER(c) vim_isupper(c)
93# define MB_TOLOWER(c) vim_tolower(c)
94# define MB_TOUPPER(c) vim_toupper(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#else
96# define MB_ISLOWER(c) islower(c)
97# define MB_ISUPPER(c) isupper(c)
98# define MB_TOLOWER(c) TOLOWER_LOC(c)
99# define MB_TOUPPER(c) TOUPPER_LOC(c)
100#endif
101
Bram Moolenaar2d473ab2013-06-12 17:12:24 +0200102/* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
103 * non-zero for superscript 1. Also avoids that isdigit() crashes for numbers
104 * below 0 and above 255. */
105#define VIM_ISDIGIT(c) ((unsigned)(c) - '0' < 10)
106
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107/* Like isalpha() but reject non-ASCII characters. Can't be used with a
108 * special key (negative value). */
109#ifdef EBCDIC
110# define ASCII_ISALPHA(c) isalpha(c)
111# define ASCII_ISALNUM(c) isalnum(c)
112# define ASCII_ISLOWER(c) islower(c)
113# define ASCII_ISUPPER(c) isupper(c)
114#else
Bram Moolenaar0ea4a6b2013-06-12 14:10:26 +0200115# define ASCII_ISLOWER(c) ((unsigned)(c) - 'a' < 26)
116# define ASCII_ISUPPER(c) ((unsigned)(c) - 'A' < 26)
Bram Moolenaar2d473ab2013-06-12 17:12:24 +0200117# define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
118# define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120
Bram Moolenaar42a45122015-07-10 17:56:23 +0200121/* Returns empty string if it is NULL. */
Bram Moolenaar669cac02016-02-25 15:25:03 +0100122#define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")
Bram Moolenaar42a45122015-07-10 17:56:23 +0200123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_LANGMAP
125/*
126 * Adjust chars in a language according to 'langmap' option.
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000127 * NOTE that there is no noticeable overhead if 'langmap' is not set.
128 * When set the overhead for characters < 256 is small.
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100129 * Don't apply 'langmap' if the character comes from the Stuff buffer or from
130 * a mapping and the langnoremap option was set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 * The do-while is just to ignore a ';' after the macro.
132 */
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000133# ifdef FEAT_MBYTE
134# define LANGMAP_ADJUST(c, condition) \
135 do { \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100136 if (*p_langmap \
137 && (condition) \
138 && (!p_lnr || (p_lnr && typebuf_maplen() == 0)) \
139 && !KeyStuffed \
140 && (c) >= 0) \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000141 { \
142 if ((c) < 256) \
143 c = langmap_mapchar[c]; \
144 else \
145 c = langmap_adjust_mb(c); \
146 } \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 } while (0)
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000148# else
149# define LANGMAP_ADJUST(c, condition) \
150 do { \
Bram Moolenaar4391cf92014-11-05 17:44:52 +0100151 if (*p_langmap \
152 && (condition) \
153 && (!p_lnr || (p_lnr && typebuf_maplen() == 0)) \
154 && !KeyStuffed \
155 && (c) >= 0 && (c) < 256) \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200156 c = langmap_mapchar[c]; \
Bram Moolenaar28e8d272009-02-21 19:28:48 +0000157 } while (0)
158# endif
159#else
160# define LANGMAP_ADJUST(c, condition) /* nop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#endif
162
163/*
164 * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
165 * it work fast.
166 */
167#define vim_isbreak(c) (breakat_flags[(char_u)(c)])
168
169/*
170 * On VMS file names are different and require a translation.
171 * On the Mac open() has only two arguments.
172 */
173#ifdef VMS
174# define mch_access(n, p) access(vms_fixfilename(n), (p))
175 /* see mch_open() comment */
176# define mch_fopen(n, p) fopen(vms_fixfilename(n), (p))
177# define mch_fstat(n, p) fstat(vms_fixfilename(n), (p))
178 /* VMS does not have lstat() */
179# define mch_stat(n, p) stat(vms_fixfilename(n), (p))
180#else
181# ifndef WIN32
182# define mch_access(n, p) access((n), (p))
183# endif
184# if !(defined(FEAT_MBYTE) && defined(WIN3264))
185# define mch_fopen(n, p) fopen((n), (p))
186# endif
187# define mch_fstat(n, p) fstat((n), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188# ifdef MSWIN /* has it's own mch_stat() function */
189# define mch_stat(n, p) vim_stat((n), (p))
190# else
191# ifdef STAT_IGNORES_SLASH
192 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
193 * the name ends in "/" and it's not a directory. */
194# define mch_stat(n, p) (illegal_slash(n) ? -1 : stat((n), (p)))
195# else
196# define mch_stat(n, p) stat((n), (p))
197# endif
198# endif
199#endif
200
Bram Moolenaar269ec652004-07-29 08:43:53 +0000201#ifdef HAVE_LSTAT
202# define mch_lstat(n, p) lstat((n), (p))
203#else
204# define mch_lstat(n, p) mch_stat((n), (p))
205#endif
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef MACOS_CLASSIC
208/* MacOS classic doesn't support perm but MacOS X does. */
209# define mch_open(n, m, p) open((n), (m))
210#else
211# ifdef VMS
212/*
213 * It is possible to force some record format with:
214 * # 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 +0000215 * but it is not recommended, because it can destroy indexes etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 */
217# define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
218# else
219# if !(defined(FEAT_MBYTE) && defined(WIN3264))
220# define mch_open(n, m, p) open((n), (m), (p))
221# endif
222# endif
223#endif
224
225/* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
226#if defined(UNIX) || defined(VMS) /* open in rw------- mode */
227# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
228#else
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100229# if defined(MSWIN) /* open read/write */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
231# else
232# define mch_open_rw(n, f) mch_open((n), (f), 0)
233# endif
234#endif
235
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000237# define TIME_MSG(s) { if (time_fd != NULL) time_msg(s, NULL); }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#else
239# define TIME_MSG(s)
240#endif
241
242#ifdef FEAT_VREPLACE
243# define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
244#else
245# define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
246#endif
247
248#ifdef FEAT_ARABIC
249# define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
250#else
251# define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
252#endif
253
254#ifdef FEAT_RIGHTLEFT
255 /* Whether to draw the vertical bar on the right side of the cell. */
256# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
257#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000258
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000259/*
260 * mb_ptr_adv(): advance a pointer to the next character, taking care of
261 * multi-byte characters if needed.
262 * mb_ptr_back(): backup a pointer to the previous character, taking care of
263 * multi-byte characters if needed.
Bram Moolenaarfd371682005-01-14 21:42:54 +0000264 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
Bram Moolenaar78984f52005-08-01 07:19:10 +0000265 * PTR2CHAR(): get character from pointer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000266 */
267#ifdef FEAT_MBYTE
Bram Moolenaar94c465c2012-07-19 17:18:26 +0200268/* Get the length of the character p points to */
269# define MB_PTR2LEN(p) (has_mbyte ? (*mb_ptr2len)(p) : 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270/* Advance multi-byte pointer, skip over composing chars. */
271# define mb_ptr_adv(p) p += has_mbyte ? (*mb_ptr2len)(p) : 1
272/* Advance multi-byte pointer, do not skip over composing chars. */
273# define mb_cptr_adv(p) p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
Bram Moolenaar24dc2302014-05-13 20:19:58 +0200274/* Backup multi-byte pointer. Only use with "p" > "s" ! */
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000275# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000276/* get length of multi-byte char, not including composing chars */
277# define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
278
Bram Moolenaarfd371682005-01-14 21:42:54 +0000279# define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000280# define MB_CHARLEN(p) (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
Bram Moolenaar473de612013-06-08 18:19:48 +0200281# define MB_CHAR2LEN(c) (has_mbyte ? mb_char2len(c) : 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000282# define PTR2CHAR(p) (has_mbyte ? mb_ptr2char(p) : (int)*(p))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000283#else
Bram Moolenaar94c465c2012-07-19 17:18:26 +0200284# define MB_PTR2LEN(p) 1
Bram Moolenaar78984f52005-08-01 07:19:10 +0000285# define mb_ptr_adv(p) ++p
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000286# define mb_cptr_adv(p) ++p
Bram Moolenaar78984f52005-08-01 07:19:10 +0000287# define mb_ptr_back(s, p) --p
288# define MB_COPY_CHAR(f, t) *t++ = *f++
289# define MB_CHARLEN(p) STRLEN(p)
Bram Moolenaar473de612013-06-08 18:19:48 +0200290# define MB_CHAR2LEN(c) 1
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000291# define PTR2CHAR(p) ((int)*(p))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000292#endif
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000293
294#ifdef FEAT_AUTOCHDIR
295# define DO_AUTOCHDIR if (p_acd) do_autochdir();
296#else
297# define DO_AUTOCHDIR
298#endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200299
300#if defined(FEAT_SCROLLBIND) && defined(FEAT_CURSORBIND)
301# define RESET_BINDING(wp) (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
302#else
303# if defined(FEAT_SCROLLBIND)
304# define RESET_BINDING(wp) (wp)->w_p_scb = FALSE
305# else
306# if defined(FEAT_CURSORBIND)
307# define RESET_BINDING(wp) (wp)->w_p_crb = FALSE
308# else
309# define RESET_BINDING(wp)
310# endif
311# endif
312#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +0200313
314#ifdef FEAT_DIFF
315# define PLINES_NOFILL(x) plines_nofill(x)
316#else
317# define PLINES_NOFILL(x) plines(x)
318#endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200319
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100320#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200321# define MESSAGE_QUEUE
322#endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100323
324#if defined(FEAT_EVAL) && defined(FEAT_FLOAT)
325# include <float.h>
326# if defined(HAVE_MATH_H)
327 /* for isnan() and isinf() */
328# include <math.h>
329# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100330# ifdef USING_FLOAT_STUFF
331# if defined(WIN32)
332# ifndef isnan
333# define isnan(x) _isnan(x)
Bram Moolenaar0c171712016-03-04 22:57:20 +0100334 static __inline int isinf(double x) { return !_finite(x) && !_isnan(x); }
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100335# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100336# else
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100337# ifndef HAVE_ISNAN
338 static inline int isnan(double x) { return x != x; }
339# endif
340# ifndef HAVE_ISINF
341 static inline int isinf(double x) { return !isnan(x) && isnan(x - x); }
342# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100343# endif
Bram Moolenaarfefecb02016-02-27 21:27:20 +0100344# if !defined(INFINITY)
345# if defined(DBL_MAX)
346# define INFINITY (DBL_MAX+DBL_MAX)
347# else
348# define INFINITY (1.0 / 0.0)
349# endif
350# endif
351# if !defined(NAN)
352# define NAN (INFINITY-INFINITY)
353# endif
Bram Moolenaar136f29a2016-02-27 20:14:15 +0100354# endif
355#endif