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