Merge branch 'vim'
[vim_extended.git] / src / macros.h
blob51e4dd423e9dcd889ddd70dfa0112de3df4b4b06
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
9 /*
10 * macros.h: macro definitions for often used code
14 * pchar(lp, c) - put character 'c' at position 'lp'
16 #define pchar(lp, c) (*(ml_get_buf(curbuf, (lp).lnum, TRUE) + (lp).col) = (c))
19 * Position comparisons
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
43 #define ltoreq(a, b) (lt(a, b) || equalpos(a, b))
46 * lineempty() - return TRUE if the line is empty
48 #define lineempty(p) (*ml_get(p) == NUL)
51 * bufempty() - return TRUE if the current buffer is empty
53 #define bufempty() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == NUL)
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.
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
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
87 * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters. But
88 * don't use them for negative values!
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
102 /* Like isalpha() but reject non-ASCII characters. Can't be used with a
103 * special key (negative value). */
104 #ifdef EBCDIC
105 # define ASCII_ISALPHA(c) isalpha(c)
106 # define ASCII_ISALNUM(c) isalnum(c)
107 # define ASCII_ISLOWER(c) islower(c)
108 # define ASCII_ISUPPER(c) isupper(c)
109 #else
110 # define ASCII_ISALPHA(c) ((c) < 0x7f && isalpha(c))
111 # define ASCII_ISALNUM(c) ((c) < 0x7f && isalnum(c))
112 # define ASCII_ISLOWER(c) ((c) < 0x7f && islower(c))
113 # define ASCII_ISUPPER(c) ((c) < 0x7f && isupper(c))
114 #endif
116 /* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
117 * non-zero for superscript 1. Also avoids that isdigit() crashes for numbers
118 * below 0 and above 255. For complicated arguments and in/decrement use
119 * vim_isdigit() instead. */
120 #define VIM_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
122 /* macro version of chartab().
123 * Only works with values 0-255!
124 * Doesn't work for UTF-8 mode with chars >= 0x80. */
125 #define CHARSIZE(c) (chartab[c] & CT_CELL_MASK)
127 #ifdef FEAT_LANGMAP
129 * Adjust chars in a language according to 'langmap' option.
130 * NOTE that there is no noticeable overhead if 'langmap' is not set.
131 * When set the overhead for characters < 256 is small.
132 * Don't apply 'langmap' if the character comes from the Stuff buffer.
133 * The do-while is just to ignore a ';' after the macro.
135 # ifdef FEAT_MBYTE
136 # define LANGMAP_ADJUST(c, condition) \
137 do { \
138 if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0) \
140 if ((c) < 256) \
141 c = langmap_mapchar[c]; \
142 else \
143 c = langmap_adjust_mb(c); \
145 } while (0)
146 # else
147 # define LANGMAP_ADJUST(c, condition) \
148 do { \
149 if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0 && (c) < 256) \
150 c = langmap_mapchar[c]; \
151 } while (0)
152 # endif
153 #else
154 # define LANGMAP_ADJUST(c, condition) /* nop */
155 #endif
158 * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
159 * it work fast.
161 #define vim_isbreak(c) (breakat_flags[(char_u)(c)])
164 * On VMS file names are different and require a translation.
165 * On the Mac open() has only two arguments.
167 #ifdef VMS
168 # define mch_access(n, p) access(vms_fixfilename(n), (p))
169 /* see mch_open() comment */
170 # define mch_fopen(n, p) fopen(vms_fixfilename(n), (p))
171 # define mch_fstat(n, p) fstat(vms_fixfilename(n), (p))
172 /* VMS does not have lstat() */
173 # define mch_stat(n, p) stat(vms_fixfilename(n), (p))
174 #else
175 # ifndef WIN32
176 # define mch_access(n, p) access((n), (p))
177 # endif
178 # if !(defined(FEAT_MBYTE) && defined(WIN3264))
179 # define mch_fopen(n, p) fopen((n), (p))
180 # endif
181 # define mch_fstat(n, p) fstat((n), (p))
182 # ifdef MSWIN /* has it's own mch_stat() function */
183 # define mch_stat(n, p) vim_stat((n), (p))
184 # else
185 # ifdef STAT_IGNORES_SLASH
186 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
187 * the name ends in "/" and it's not a directory. */
188 # define mch_stat(n, p) (illegal_slash(n) ? -1 : stat((n), (p)))
189 # else
190 # define mch_stat(n, p) stat((n), (p))
191 # endif
192 # endif
193 #endif
195 #ifdef HAVE_LSTAT
196 # define mch_lstat(n, p) lstat((n), (p))
197 #else
198 # define mch_lstat(n, p) mch_stat((n), (p))
199 #endif
201 #ifdef MACOS_CLASSIC
202 /* MacOS classic doesn't support perm but MacOS X does. */
203 # define mch_open(n, m, p) open((n), (m))
204 #else
205 # ifdef VMS
207 * It is possible to force some record format with:
208 * # define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p)), "rat=cr", "rfm=stmlf", "mrs=0")
209 * but it is not recommended, because it can destroy indexes etc.
211 # define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
212 # else
213 # if !(defined(FEAT_MBYTE) && defined(WIN3264))
214 # define mch_open(n, m, p) open((n), (m), (p))
215 # endif
216 # endif
217 #endif
219 /* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
220 #if defined(UNIX) || defined(VMS) /* open in rw------- mode */
221 # define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
222 #else
223 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) /* open read/write */
224 # define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
225 # else
226 # define mch_open_rw(n, f) mch_open((n), (f), 0)
227 # endif
228 #endif
231 * Encryption macros. Mohsin Ahmed, mosh@sasi.com 98-09-24
232 * Based on zip/crypt sources.
235 #ifdef FEAT_CRYPT
237 /* encode byte c, using temp t. Warning: c must not have side effects. */
238 # define ZENCODE(c, t) (t = decrypt_byte(), update_keys(c), t^(c))
240 /* decode byte c in place */
241 # define ZDECODE(c) update_keys(c ^= decrypt_byte())
243 #endif
245 #ifdef STARTUPTIME
246 # define TIME_MSG(s) { if (time_fd != NULL) time_msg(s, NULL); }
247 #else
248 # define TIME_MSG(s)
249 #endif
251 #ifdef FEAT_VREPLACE
252 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
253 #else
254 # define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
255 #endif
257 #ifdef FEAT_ARABIC
258 # define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
259 #else
260 # define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
261 #endif
263 #ifdef FEAT_RIGHTLEFT
264 /* Whether to draw the vertical bar on the right side of the cell. */
265 # define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
266 #endif
269 * mb_ptr_adv(): advance a pointer to the next character, taking care of
270 * multi-byte characters if needed.
271 * mb_ptr_back(): backup a pointer to the previous character, taking care of
272 * multi-byte characters if needed.
273 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
274 * PTR2CHAR(): get character from pointer.
276 #ifdef FEAT_MBYTE
277 /* Advance multi-byte pointer, skip over composing chars. */
278 # define mb_ptr_adv(p) p += has_mbyte ? (*mb_ptr2len)(p) : 1
279 /* Advance multi-byte pointer, do not skip over composing chars. */
280 # define mb_cptr_adv(p) p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
281 /* Backup multi-byte pointer. */
282 # define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
283 /* get length of multi-byte char, not including composing chars */
284 # define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
286 # define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
287 # define MB_CHARLEN(p) (has_mbyte ? mb_charlen(p) : (int)STRLEN(p))
288 # define PTR2CHAR(p) (has_mbyte ? mb_ptr2char(p) : (int)*(p))
289 #else
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 PTR2CHAR(p) ((int)*(p))
296 #endif
298 #ifdef FEAT_AUTOCHDIR
299 # define DO_AUTOCHDIR if (p_acd) do_autochdir();
300 #else
301 # define DO_AUTOCHDIR
302 #endif