xref: /vim-8.2.3635/src/macros.h (revision 592e0a2a)
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 #else
34 # define lt(a, b) (((a).lnum != (b).lnum) \
35 		   ? ((a).lnum < (b).lnum) : ((a).col < (b).col))
36 # define ltp(a, b) (((a)->lnum != (b)->lnum) \
37 		   ? ((a)->lnum < (b)->lnum) : ((a)->col < (b)->col))
38 # define equalpos(a, b) (((a).lnum == (b).lnum) && ((a).col == (b).col))
39 #endif
40 
41 #define ltoreq(a, b) (lt(a, b) || equalpos(a, b))
42 
43 /*
44  * lineempty() - return TRUE if the line is empty
45  */
46 #define lineempty(p) (*ml_get(p) == NUL)
47 
48 /*
49  * bufempty() - return TRUE if the current buffer is empty
50  */
51 #define bufempty() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
52 
53 /*
54  * toupper() and tolower() that use the current locale.
55  * On some systems toupper()/tolower() only work on lower/uppercase characters
56  * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
57  * range 0 - 255.  toupper()/tolower() on some systems can't handle others.
58  * Note: for UTF-8 use utf_toupper() and utf_tolower().
59  */
60 #ifdef MSWIN
61 #  define TOUPPER_LOC(c)	toupper_tab[(c) & 255]
62 #  define TOLOWER_LOC(c)	tolower_tab[(c) & 255]
63 #else
64 # ifdef BROKEN_TOUPPER
65 #  define TOUPPER_LOC(c)	(islower(c) ? toupper(c) : (c))
66 #  define TOLOWER_LOC(c)	(isupper(c) ? tolower(c) : (c))
67 # else
68 #  define TOUPPER_LOC		toupper
69 #  define TOLOWER_LOC		tolower
70 # endif
71 #endif
72 
73 /* toupper() and tolower() for ASCII only and ignore the current locale. */
74 #ifdef EBCDIC
75 # define TOUPPER_ASC(c)	(islower(c) ? toupper(c) : (c))
76 # define TOLOWER_ASC(c)	(isupper(c) ? tolower(c) : (c))
77 #else
78 # define TOUPPER_ASC(c)	(((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
79 # define TOLOWER_ASC(c)	(((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
80 #endif
81 
82 /*
83  * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters.  But
84  * don't use them for negative values.
85  */
86 #ifdef FEAT_MBYTE
87 # define MB_ISLOWER(c)	(enc_utf8 && (c) > 0x80 ? utf_islower(c) : (has_mbyte && c > 255 ? FALSE : islower(c)))
88 # define MB_ISUPPER(c)	(enc_utf8 && (c) > 0x80 ? utf_isupper(c) : (has_mbyte && c > 255 ? FALSE : isupper(c)))
89 # define MB_TOLOWER(c)	(enc_utf8 && (c) > 0x80 ? utf_tolower(c) : (has_mbyte && c > 255 ? c : TOLOWER_LOC(c)))
90 # define MB_TOUPPER(c)	(enc_utf8 && (c) > 0x80 ? utf_toupper(c) : (has_mbyte && c > 255 ? c : TOUPPER_LOC(c)))
91 #else
92 # define MB_ISLOWER(c)	islower(c)
93 # define MB_ISUPPER(c)	isupper(c)
94 # define MB_TOLOWER(c)	TOLOWER_LOC(c)
95 # define MB_TOUPPER(c)	TOUPPER_LOC(c)
96 #endif
97 
98 /* Like isalpha() but reject non-ASCII characters.  Can't be used with a
99  * special key (negative value). */
100 #ifdef EBCDIC
101 # define ASCII_ISALPHA(c) isalpha(c)
102 # define ASCII_ISALNUM(c) isalnum(c)
103 # define ASCII_ISLOWER(c) islower(c)
104 # define ASCII_ISUPPER(c) isupper(c)
105 #else
106 # define ASCII_ISALPHA(c) ((c) < 0x7f && isalpha(c))
107 # define ASCII_ISALNUM(c) ((c) < 0x7f && isalnum(c))
108 # define ASCII_ISLOWER(c) ((c) < 0x7f && islower(c))
109 # define ASCII_ISUPPER(c) ((c) < 0x7f && isupper(c))
110 #endif
111 
112 /* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
113  * non-zero for superscript 1.  Also avoids that isdigit() crashes for numbers
114  * below 0 and above 255.  For complicated arguments and in/decrement use
115  * vim_isdigit() instead. */
116 #define VIM_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
117 
118 /* macro version of chartab().
119  * Only works with values 0-255!
120  * Doesn't work for UTF-8 mode with chars >= 0x80. */
121 #define CHARSIZE(c)	(chartab[c] & CT_CELL_MASK)
122 
123 #ifdef FEAT_LANGMAP
124 /*
125  * Adjust chars in a language according to 'langmap' option.
126  * NOTE that there is NO overhead if 'langmap' is not set; but even
127  * when set we only have to do 2 ifs and an array lookup.
128  * Don't apply 'langmap' if the character comes from the Stuff buffer.
129  * The do-while is just to ignore a ';' after the macro.
130  */
131 # define LANGMAP_ADJUST(c, condition) do { \
132 	if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0 && (c) < 256) \
133 	    c = langmap_mapchar[c]; \
134     } while (0)
135 #endif
136 
137 /*
138  * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
139  * it work fast.
140  */
141 #define vim_isbreak(c) (breakat_flags[(char_u)(c)])
142 
143 /*
144  * On VMS file names are different and require a translation.
145  * On the Mac open() has only two arguments.
146  */
147 #ifdef VMS
148 # define mch_access(n, p)	access(vms_fixfilename(n), (p))
149 				/* see mch_open() comment */
150 # define mch_fopen(n, p)	fopen(vms_fixfilename(n), (p))
151 # define mch_fstat(n, p)	fstat(vms_fixfilename(n), (p))
152 	/* VMS does not have lstat() */
153 # define mch_stat(n, p)		stat(vms_fixfilename(n), (p))
154 #else
155 # ifndef WIN32
156 #   define mch_access(n, p)	access((n), (p))
157 # endif
158 # if !(defined(FEAT_MBYTE) && defined(WIN3264))
159 #  define mch_fopen(n, p)	fopen((n), (p))
160 # endif
161 # define mch_fstat(n, p)	fstat((n), (p))
162 # define mch_lstat(n, p)	lstat((n), (p))
163 # ifdef MSWIN	/* has it's own mch_stat() function */
164 #  define mch_stat(n, p)	vim_stat((n), (p))
165 # else
166 #  ifdef STAT_IGNORES_SLASH
167     /* On Solaris stat() accepts "file/" as if it was "file".  Return -1 if
168      * the name ends in "/" and it's not a directory. */
169 #   define mch_stat(n, p)	(illegal_slash(n) ? -1 : stat((n), (p)))
170 #  else
171 #   define mch_stat(n, p)	stat((n), (p))
172 #  endif
173 # endif
174 #endif
175 
176 #ifdef MACOS_CLASSIC
177 /* MacOS classic doesn't support perm but MacOS X does. */
178 # define mch_open(n, m, p)	open((n), (m))
179 #else
180 # ifdef VMS
181 /*
182  * It is possible to force some record format with:
183  * #  define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p)), "rat=cr", "rfm=stmlf", "mrs=0")
184  * but it is not recomended, because it can destroy indexes etc.
185  */
186 #  define mch_open(n, m, p)	open(vms_fixfilename(n), (m), (p))
187 # else
188 #  if !(defined(FEAT_MBYTE) && defined(WIN3264))
189 #   define mch_open(n, m, p)	open((n), (m), (p))
190 #  endif
191 # endif
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(MSDOS) || defined(MSWIN) || defined(OS2)  /* 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 /*
206  * Encryption macros.  Mohsin Ahmed, [email protected] 98-09-24
207  * Based on zip/crypt sources.
208  */
209 
210 #ifdef FEAT_CRYPT
211 
212 #ifndef __MINGW32__
213 # define PWLEN 80
214 #endif
215 
216 /* encode byte c, using temp t.  Warning: c must not have side effects. */
217 # define ZENCODE(c, t)  (t = decrypt_byte(), update_keys(c), t^(c))
218 
219 /* decode byte c in place */
220 # define ZDECODE(c)   update_keys(c ^= decrypt_byte())
221 
222 #endif
223 
224 #ifdef STARTUPTIME
225 # define TIME_MSG(s) time_msg(s, NULL)
226 #else
227 # define TIME_MSG(s)
228 #endif
229 
230 #ifdef FEAT_VREPLACE
231 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
232 #else
233 # define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
234 #endif
235 
236 #ifdef FEAT_ARABIC
237 # define UTF_COMPOSINGLIKE(p1, p2)  utf_composinglike((p1), (p2))
238 #else
239 # define UTF_COMPOSINGLIKE(p1, p2)  utf_iscomposing(utf_ptr2char(p2))
240 #endif
241 
242 #ifdef FEAT_RIGHTLEFT
243     /* Whether to draw the vertical bar on the right side of the cell. */
244 # define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
245 #endif
246 
247 #ifndef FEAT_LINEBREAK
248 /* Without the 'numberwidth' option line numbers are always 7 chars. */
249 # define number_width(x) 7
250 #endif
251