xref: /vim-8.2.3635/src/macros.h (revision e16b00a1)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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  * Macros should be ALL_CAPS.  An exception is for where a function is
13  * replaced and an argument is not used more than once.
14  */
15 
16 /*
17  * PCHAR(lp, c) - put character 'c' at position 'lp'
18  */
19 #define PCHAR(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
20 
21 /*
22  * Position comparisons
23  */
24 #ifdef FEAT_VIRTUALEDIT
25 # define LT_POS(a, b) (((a).lnum != (b).lnum) \
26 		   ? (a).lnum < (b).lnum \
27 		   : (a).col != (b).col \
28 		       ? (a).col < (b).col \
29 		       : (a).coladd < (b).coladd)
30 # define LT_POSP(a, b) (((a)->lnum != (b)->lnum) \
31 		   ? (a)->lnum < (b)->lnum \
32 		   : (a)->col != (b)->col \
33 		       ? (a)->col < (b)->col \
34 		       : (a)->coladd < (b)->coladd)
35 # define EQUAL_POS(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col) && ((a).coladd == (b).coladd))
36 # define CLEAR_POS(a) {(a)->lnum = 0; (a)->col = 0; (a)->coladd = 0;}
37 #else
38 # define LT_POS(a, b) (((a).lnum != (b).lnum) \
39 		   ? ((a).lnum < (b).lnum) : ((a).col < (b).col))
40 # define LT_POSP(a, b) (((a)->lnum != (b)->lnum) \
41 		   ? ((a)->lnum < (b)->lnum) : ((a)->col < (b)->col))
42 # define EQUAL_POS(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col))
43 # define CLEAR_POS(a) {(a)->lnum = 0; (a)->col = 0;}
44 #endif
45 
46 #define LTOREQ_POS(a, b) (LT_POS(a, b) || EQUAL_POS(a, b))
47 
48 /*
49  * VIM_ISWHITE() is used for "^" and the like. It differs from isspace()
50  * because it doesn't include <CR> and <LF> and the like.
51  */
52 #define VIM_ISWHITE(x)	((x) == ' ' || (x) == '\t')
53 
54 /*
55  * LINEEMPTY() - return TRUE if the line is empty
56  */
57 #define LINEEMPTY(p) (*ml_get(p) == NUL)
58 
59 /*
60  * BUFEMPTY() - return TRUE if the current buffer is empty
61  */
62 #define BUFEMPTY() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
63 
64 /*
65  * toupper() and tolower() that use the current locale.
66  * On some systems toupper()/tolower() only work on lower/uppercase
67  * characters, first use islower() or isupper() then.
68  * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
69  * range 0 - 255.  toupper()/tolower() on some systems can't handle others.
70  * Note: It is often better to use MB_TOLOWER() and MB_TOUPPER(), because many
71  * toupper() and tolower() implementations only work for ASCII.
72  */
73 #ifdef MSWIN
74 #  define TOUPPER_LOC(c)	toupper_tab[(c) & 255]
75 #  define TOLOWER_LOC(c)	tolower_tab[(c) & 255]
76 #else
77 # ifdef BROKEN_TOUPPER
78 #  define TOUPPER_LOC(c)	(islower(c) ? toupper(c) : (c))
79 #  define TOLOWER_LOC(c)	(isupper(c) ? tolower(c) : (c))
80 # else
81 #  define TOUPPER_LOC		toupper
82 #  define TOLOWER_LOC		tolower
83 # endif
84 #endif
85 
86 /* toupper() and tolower() for ASCII only and ignore the current locale. */
87 #ifdef EBCDIC
88 # define TOUPPER_ASC(c)	(islower(c) ? toupper(c) : (c))
89 # define TOLOWER_ASC(c)	(isupper(c) ? tolower(c) : (c))
90 #else
91 # define TOUPPER_ASC(c)	(((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
92 # define TOLOWER_ASC(c)	(((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
93 #endif
94 
95 /*
96  * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters.  But
97  * don't use them for negative values!
98  */
99 #ifdef FEAT_MBYTE
100 # define MB_ISLOWER(c)	vim_islower(c)
101 # define MB_ISUPPER(c)	vim_isupper(c)
102 # define MB_TOLOWER(c)	vim_tolower(c)
103 # define MB_TOUPPER(c)	vim_toupper(c)
104 #else
105 # define MB_ISLOWER(c)	islower(c)
106 # define MB_ISUPPER(c)	isupper(c)
107 # define MB_TOLOWER(c)	TOLOWER_LOC(c)
108 # define MB_TOUPPER(c)	TOUPPER_LOC(c)
109 #endif
110 
111 /* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
112  * non-zero for superscript 1.  Also avoids that isdigit() crashes for numbers
113  * below 0 and above 255.  */
114 #define VIM_ISDIGIT(c) ((unsigned)(c) - '0' < 10)
115 
116 /* Like isalpha() but reject non-ASCII characters.  Can't be used with a
117  * special key (negative value). */
118 #ifdef EBCDIC
119 # define ASCII_ISALPHA(c) isalpha(c)
120 # define ASCII_ISALNUM(c) isalnum(c)
121 # define ASCII_ISLOWER(c) islower(c)
122 # define ASCII_ISUPPER(c) isupper(c)
123 #else
124 # define ASCII_ISLOWER(c) ((unsigned)(c) - 'a' < 26)
125 # define ASCII_ISUPPER(c) ((unsigned)(c) - 'A' < 26)
126 # define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
127 # define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
128 #endif
129 
130 /* Returns empty string if it is NULL. */
131 #define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")
132 
133 #ifdef FEAT_LANGMAP
134 /*
135  * Adjust chars in a language according to 'langmap' option.
136  * NOTE that there is no noticeable overhead if 'langmap' is not set.
137  * When set the overhead for characters < 256 is small.
138  * Don't apply 'langmap' if the character comes from the Stuff buffer or from
139  * a mapping and the langnoremap option was set.
140  * The do-while is just to ignore a ';' after the macro.
141  */
142 # ifdef FEAT_MBYTE
143 #  define LANGMAP_ADJUST(c, condition) \
144     do { \
145 	if (*p_langmap \
146 		&& (condition) \
147 		&& (p_lrm || (!p_lrm && KeyTyped)) \
148 		&& !KeyStuffed \
149 		&& (c) >= 0) \
150 	{ \
151 	    if ((c) < 256) \
152 		c = langmap_mapchar[c]; \
153 	    else \
154 		c = langmap_adjust_mb(c); \
155 	} \
156     } while (0)
157 # else
158 #  define LANGMAP_ADJUST(c, condition) \
159     do { \
160 	if (*p_langmap \
161 		&& (condition) \
162 		&& (p_lrm || (!p_lrm && KeyTyped)) \
163 		&& !KeyStuffed \
164 		&& (c) >= 0 && (c) < 256) \
165 	    c = langmap_mapchar[c]; \
166     } while (0)
167 # endif
168 #else
169 # define LANGMAP_ADJUST(c, condition) /* nop */
170 #endif
171 
172 /*
173  * VIM_ISBREAK() is used very often if 'linebreak' is set, use a macro to make
174  * it work fast.  Only works for single byte characters!
175  */
176 #define VIM_ISBREAK(c) ((c) < 256 && breakat_flags[(char_u)(c)])
177 
178 /*
179  * On VMS file names are different and require a translation.
180  * On the Mac open() has only two arguments.
181  */
182 #ifdef VMS
183 # define mch_access(n, p)	access(vms_fixfilename(n), (p))
184 				/* see mch_open() comment */
185 # define mch_fopen(n, p)	fopen(vms_fixfilename(n), (p))
186 # define mch_fstat(n, p)	fstat(vms_fixfilename(n), (p))
187 	/* VMS does not have lstat() */
188 # define mch_stat(n, p)		stat(vms_fixfilename(n), (p))
189 # define mch_rmdir(n)		rmdir(vms_fixfilename(n))
190 #else
191 # ifndef WIN32
192 #   define mch_access(n, p)	access((n), (p))
193 # endif
194 # define mch_fstat(n, p)	fstat((n), (p))
195 # ifdef MSWIN	/* has it's own mch_stat() function */
196 #  define mch_stat(n, p)	vim_stat((n), (p))
197 # else
198 #  ifdef STAT_IGNORES_SLASH
199 #   define mch_stat(n, p)	vim_stat((n), (p))
200 #  else
201 #   define mch_stat(n, p)	stat((n), (p))
202 #  endif
203 # endif
204 #endif
205 
206 #ifdef HAVE_LSTAT
207 # define mch_lstat(n, p)	lstat((n), (p))
208 #else
209 # define mch_lstat(n, p)	mch_stat((n), (p))
210 #endif
211 
212 #ifdef MACOS_CLASSIC
213 /* MacOS classic doesn't support perm but MacOS X does. */
214 # define mch_open(n, m, p)	open((n), (m))
215 #else
216 # ifdef VMS
217 /*
218  * It is possible to force some record format with:
219  * #  define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p)), "rat=cr", "rfm=stmlf", "mrs=0")
220  * but it is not recommended, because it can destroy indexes etc.
221  */
222 #  define mch_open(n, m, p)	open(vms_fixfilename(n), (m), (p))
223 # endif
224 #endif
225 
226 /* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
227 #if defined(UNIX) || defined(VMS)  /* open in rw------- mode */
228 # define mch_open_rw(n, f)	mch_open((n), (f), (mode_t)0600)
229 #else
230 # if defined(MSWIN)  /* open read/write */
231 #  define mch_open_rw(n, f)	mch_open((n), (f), S_IREAD | S_IWRITE)
232 # else
233 #  define mch_open_rw(n, f)	mch_open((n), (f), 0)
234 # endif
235 #endif
236 
237 #ifdef STARTUPTIME
238 # define TIME_MSG(s) { if (time_fd != NULL) time_msg(s, NULL); }
239 #else
240 # define TIME_MSG(s)
241 #endif
242 
243 #ifdef FEAT_VREPLACE
244 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
245 #else
246 # define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
247 #endif
248 
249 #ifdef FEAT_ARABIC
250 # define UTF_COMPOSINGLIKE(p1, p2)  utf_composinglike((p1), (p2))
251 #else
252 # define UTF_COMPOSINGLIKE(p1, p2)  utf_iscomposing(utf_ptr2char(p2))
253 #endif
254 
255 #ifdef FEAT_RIGHTLEFT
256     /* Whether to draw the vertical bar on the right side of the cell. */
257 # define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
258 #endif
259 
260 /*
261  * MB_PTR_ADV(): advance a pointer to the next character, taking care of
262  * multi-byte characters if needed.
263  * MB_PTR_BACK(): backup a pointer to the previous character, taking care of
264  * multi-byte characters if needed.
265  * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
266  * PTR2CHAR(): get character from pointer.
267  */
268 #ifdef FEAT_MBYTE
269 /* Get the length of the character p points to */
270 # define MB_PTR2LEN(p)	    (has_mbyte ? (*mb_ptr2len)(p) : 1)
271 /* Advance multi-byte pointer, skip over composing chars. */
272 # define MB_PTR_ADV(p)	    p += has_mbyte ? (*mb_ptr2len)(p) : 1
273 /* Advance multi-byte pointer, do not skip over composing chars. */
274 # define MB_CPTR_ADV(p)	    p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
275 /* Backup multi-byte pointer. Only use with "p" > "s" ! */
276 # define MB_PTR_BACK(s, p)  p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
277 /* get length of multi-byte char, not including composing chars */
278 # define MB_CPTR2LEN(p)	    (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
279 
280 # define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
281 # define MB_CHARLEN(p)	    (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
282 # define MB_CHAR2LEN(c)	    (has_mbyte ? mb_char2len(c) : 1)
283 # define PTR2CHAR(p)	    (has_mbyte ? mb_ptr2char(p) : (int)*(p))
284 #else
285 # define MB_PTR2LEN(p)		1
286 # define MB_CPTR2LEN(p)		1
287 # define MB_PTR_ADV(p)		++p
288 # define MB_CPTR_ADV(p)		++p
289 # define MB_PTR_BACK(s, p)	--p
290 # define MB_COPY_CHAR(f, t)	*t++ = *f++
291 # define MB_CHARLEN(p)		STRLEN(p)
292 # define MB_CHAR2LEN(c)		1
293 # define PTR2CHAR(p)		((int)*(p))
294 #endif
295 
296 #ifdef FEAT_AUTOCHDIR
297 # define DO_AUTOCHDIR if (p_acd) do_autochdir();
298 #else
299 # define DO_AUTOCHDIR
300 #endif
301 
302 #if defined(FEAT_SCROLLBIND) && defined(FEAT_CURSORBIND)
303 # define RESET_BINDING(wp)  (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
304 #else
305 # if defined(FEAT_SCROLLBIND)
306 #  define RESET_BINDING(wp)  (wp)->w_p_scb = FALSE
307 # else
308 #  if defined(FEAT_CURSORBIND)
309 #   define RESET_BINDING(wp)  (wp)->w_p_crb = FALSE
310 #  else
311 #   define RESET_BINDING(wp)
312 #  endif
313 # endif
314 #endif
315 
316 #ifdef FEAT_DIFF
317 # define PLINES_NOFILL(x) plines_nofill(x)
318 #else
319 # define PLINES_NOFILL(x) plines(x)
320 #endif
321 
322 #if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
323 # define MESSAGE_QUEUE
324 #endif
325 
326 #if defined(FEAT_EVAL) && defined(FEAT_FLOAT)
327 # include <float.h>
328 # if defined(HAVE_MATH_H)
329    /* for isnan() and isinf() */
330 #  include <math.h>
331 # endif
332 # ifdef USING_FLOAT_STUFF
333 #  if defined(WIN32)
334 #   ifndef isnan
335 #    define isnan(x) _isnan(x)
336      static __inline int isinf(double x) { return !_finite(x) && !_isnan(x); }
337 #   endif
338 #  else
339 #   ifndef HAVE_ISNAN
340      static inline int isnan(double x) { return x != x; }
341 #   endif
342 #   ifndef HAVE_ISINF
343      static inline int isinf(double x) { return !isnan(x) && isnan(x - x); }
344 #   endif
345 #  endif
346 #  if !defined(INFINITY)
347 #   if defined(DBL_MAX)
348 #    ifdef VMS
349 #     define INFINITY DBL_MAX
350 #    else
351 #     define INFINITY (DBL_MAX+DBL_MAX)
352 #    endif
353 #   else
354 #    define INFINITY (1.0 / 0.0)
355 #   endif
356 #  endif
357 #  if !defined(NAN)
358 #   define NAN (INFINITY-INFINITY)
359 #  endif
360 #  if !defined(DBL_EPSILON)
361 #   define DBL_EPSILON 2.2204460492503131e-16
362 #  endif
363 # endif
364 #endif
365 
366 /*
367  * In a hashtab item "hi_key" points to "di_key" in a dictitem.
368  * This avoids adding a pointer to the hashtab item.
369  * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
370  * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
371  * HI2DI() converts a hashitem pointer to a dictitem pointer.
372  */
373 # define DI2HIKEY(di) ((di)->di_key)
374 # define HIKEY2DI(p)  ((dictitem_T *)(p - offsetof(dictitem_T, di_key)))
375 # define HI2DI(hi)     HIKEY2DI((hi)->hi_key)
376