xref: /vim-8.2.3635/src/macros.h (revision 2bf24176)
1 /* 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))
33 # define clearpos(a) {(a)->lnum = 0; (a)->col = 0; (a)->coladd = 0;}
34 #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))
40 # define clearpos(a) {(a)->lnum = 0; (a)->col = 0;}
41 #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.
57  * On some systems toupper()/tolower() only work on lower/uppercase
58  * characters, first use islower() or isupper() then.
59  * 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.
61  * Note: It is often better to use MB_TOLOWER() and MB_TOUPPER(), because many
62  * toupper() and tolower() implementations only work for ASCII.
63  */
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
88  * don't use them for negative values!
89  */
90 #ifdef FEAT_MBYTE
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 #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 
102 /* 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 
107 /* 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
115 # define ASCII_ISLOWER(c) ((unsigned)(c) - 'a' < 26)
116 # define ASCII_ISUPPER(c) ((unsigned)(c) - 'A' < 26)
117 # define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
118 # define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || VIM_ISDIGIT(c))
119 #endif
120 
121 /* Returns empty string if it is NULL. */
122 #define EMPTY_IF_NULL(x) ((x) ? (x) : (u_char *)"")
123 
124 /* macro version of chartab().
125  * Only works with values 0-255!
126  * Doesn't work for UTF-8 mode with chars >= 0x80. */
127 #define CHARSIZE(c)	(chartab[c] & CT_CELL_MASK)
128 
129 #ifdef FEAT_LANGMAP
130 /*
131  * Adjust chars in a language according to 'langmap' option.
132  * NOTE that there is no noticeable overhead if 'langmap' is not set.
133  * When set the overhead for characters < 256 is small.
134  * Don't apply 'langmap' if the character comes from the Stuff buffer or from
135  * a mapping and the langnoremap option was set.
136  * The do-while is just to ignore a ';' after the macro.
137  */
138 # ifdef FEAT_MBYTE
139 #  define LANGMAP_ADJUST(c, condition) \
140     do { \
141 	if (*p_langmap \
142 		&& (condition) \
143 		&& (!p_lnr || (p_lnr && typebuf_maplen() == 0)) \
144 		&& !KeyStuffed \
145 		&& (c) >= 0) \
146 	{ \
147 	    if ((c) < 256) \
148 		c = langmap_mapchar[c]; \
149 	    else \
150 		c = langmap_adjust_mb(c); \
151 	} \
152     } while (0)
153 # else
154 #  define LANGMAP_ADJUST(c, condition) \
155     do { \
156 	if (*p_langmap \
157 		&& (condition) \
158 		&& (!p_lnr || (p_lnr && typebuf_maplen() == 0)) \
159 		&& !KeyStuffed \
160 		&& (c) >= 0 && (c) < 256) \
161 	    c = langmap_mapchar[c]; \
162     } while (0)
163 # endif
164 #else
165 # define LANGMAP_ADJUST(c, condition) /* nop */
166 #endif
167 
168 /*
169  * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
170  * it work fast.
171  */
172 #define vim_isbreak(c) (breakat_flags[(char_u)(c)])
173 
174 /*
175  * On VMS file names are different and require a translation.
176  * On the Mac open() has only two arguments.
177  */
178 #ifdef VMS
179 # define mch_access(n, p)	access(vms_fixfilename(n), (p))
180 				/* see mch_open() comment */
181 # define mch_fopen(n, p)	fopen(vms_fixfilename(n), (p))
182 # define mch_fstat(n, p)	fstat(vms_fixfilename(n), (p))
183 	/* VMS does not have lstat() */
184 # define mch_stat(n, p)		stat(vms_fixfilename(n), (p))
185 #else
186 # ifndef WIN32
187 #   define mch_access(n, p)	access((n), (p))
188 # endif
189 # if !(defined(FEAT_MBYTE) && defined(WIN3264))
190 #  define mch_fopen(n, p)	fopen((n), (p))
191 # endif
192 # define mch_fstat(n, p)	fstat((n), (p))
193 # ifdef MSWIN	/* has it's own mch_stat() function */
194 #  define mch_stat(n, p)	vim_stat((n), (p))
195 # else
196 #  ifdef STAT_IGNORES_SLASH
197     /* On Solaris stat() accepts "file/" as if it was "file".  Return -1 if
198      * the name ends in "/" and it's not a directory. */
199 #   define mch_stat(n, p)	(illegal_slash(n) ? -1 : 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 # else
224 #  if !(defined(FEAT_MBYTE) && defined(WIN3264))
225 #   define mch_open(n, m, p)	open((n), (m), (p))
226 #  endif
227 # endif
228 #endif
229 
230 /* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
231 #if defined(UNIX) || defined(VMS)  /* open in rw------- mode */
232 # define mch_open_rw(n, f)	mch_open((n), (f), (mode_t)0600)
233 #else
234 # if defined(MSDOS) || defined(MSWIN)  /* open read/write */
235 #  define mch_open_rw(n, f)	mch_open((n), (f), S_IREAD | S_IWRITE)
236 # else
237 #  define mch_open_rw(n, f)	mch_open((n), (f), 0)
238 # endif
239 #endif
240 
241 #ifdef STARTUPTIME
242 # define TIME_MSG(s) { if (time_fd != NULL) time_msg(s, NULL); }
243 #else
244 # define TIME_MSG(s)
245 #endif
246 
247 #ifdef FEAT_VREPLACE
248 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
249 #else
250 # define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
251 #endif
252 
253 #ifdef FEAT_ARABIC
254 # define UTF_COMPOSINGLIKE(p1, p2)  utf_composinglike((p1), (p2))
255 #else
256 # define UTF_COMPOSINGLIKE(p1, p2)  utf_iscomposing(utf_ptr2char(p2))
257 #endif
258 
259 #ifdef FEAT_RIGHTLEFT
260     /* Whether to draw the vertical bar on the right side of the cell. */
261 # define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
262 #endif
263 
264 /*
265  * mb_ptr_adv(): advance a pointer to the next character, taking care of
266  * multi-byte characters if needed.
267  * mb_ptr_back(): backup a pointer to the previous character, taking care of
268  * multi-byte characters if needed.
269  * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
270  * PTR2CHAR(): get character from pointer.
271  */
272 #ifdef FEAT_MBYTE
273 /* Get the length of the character p points to */
274 # define MB_PTR2LEN(p)		(has_mbyte ? (*mb_ptr2len)(p) : 1)
275 /* Advance multi-byte pointer, skip over composing chars. */
276 # define mb_ptr_adv(p)	    p += has_mbyte ? (*mb_ptr2len)(p) : 1
277 /* Advance multi-byte pointer, do not skip over composing chars. */
278 # define mb_cptr_adv(p)	    p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
279 /* Backup multi-byte pointer. Only use with "p" > "s" ! */
280 # define mb_ptr_back(s, p)  p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
281 /* get length of multi-byte char, not including composing chars */
282 # define mb_cptr2len(p)	    (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
283 
284 # define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
285 # define MB_CHARLEN(p)	    (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
286 # define MB_CHAR2LEN(c)	    (has_mbyte ? mb_char2len(c) : 1)
287 # define PTR2CHAR(p)	    (has_mbyte ? mb_ptr2char(p) : (int)*(p))
288 #else
289 # define MB_PTR2LEN(p)		1
290 # define mb_ptr_adv(p)		++p
291 # define mb_cptr_adv(p)		++p
292 # define mb_ptr_back(s, p)	--p
293 # define MB_COPY_CHAR(f, t)	*t++ = *f++
294 # define MB_CHARLEN(p)		STRLEN(p)
295 # define MB_CHAR2LEN(c)		1
296 # define PTR2CHAR(p)		((int)*(p))
297 #endif
298 
299 #ifdef FEAT_AUTOCHDIR
300 # define DO_AUTOCHDIR if (p_acd) do_autochdir();
301 #else
302 # define DO_AUTOCHDIR
303 #endif
304 
305 #if defined(FEAT_SCROLLBIND) && defined(FEAT_CURSORBIND)
306 # define RESET_BINDING(wp)  (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
307 #else
308 # if defined(FEAT_SCROLLBIND)
309 #  define RESET_BINDING(wp)  (wp)->w_p_scb = FALSE
310 # else
311 #  if defined(FEAT_CURSORBIND)
312 #   define RESET_BINDING(wp)  (wp)->w_p_crb = FALSE
313 #  else
314 #   define RESET_BINDING(wp)
315 #  endif
316 # endif
317 #endif
318 
319 #ifdef FEAT_DIFF
320 # define PLINES_NOFILL(x) plines_nofill(x)
321 #else
322 # define PLINES_NOFILL(x) plines(x)
323 #endif
324 
325 #if defined(FEAT_NETBEANS_INTG) || defined(FEAT_CLIENTSERVER)
326 # define MESSAGE_QUEUE
327 #endif
328