blob: 400f771281b7e3826ce879238d2e3be451f045f3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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))
Bram Moolenaare1438bb2006-03-01 22:01:55 +000033# define clearpos(a) {(a)->lnum = 0; (a)->col = 0; (a)->coladd = 0;}
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#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))
Bram Moolenaare1438bb2006-03-01 22:01:55 +000040# define clearpos(a) {(a)->lnum = 0; (a)->col = 0;}
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#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 characters
58 * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
59 * range 0 - 255. toupper()/tolower() on some systems can't handle others.
60 * Note: for UTF-8 use utf_toupper() and utf_tolower().
61 */
62#ifdef MSWIN
63# define TOUPPER_LOC(c) toupper_tab[(c) & 255]
64# define TOLOWER_LOC(c) tolower_tab[(c) & 255]
65#else
66# ifdef BROKEN_TOUPPER
67# define TOUPPER_LOC(c) (islower(c) ? toupper(c) : (c))
68# define TOLOWER_LOC(c) (isupper(c) ? tolower(c) : (c))
69# else
70# define TOUPPER_LOC toupper
71# define TOLOWER_LOC tolower
72# endif
73#endif
74
75/* toupper() and tolower() for ASCII only and ignore the current locale. */
76#ifdef EBCDIC
77# define TOUPPER_ASC(c) (islower(c) ? toupper(c) : (c))
78# define TOLOWER_ASC(c) (isupper(c) ? tolower(c) : (c))
79#else
80# define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
81# define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
82#endif
83
84/*
85 * MB_ISLOWER() and MB_ISUPPER() are to be used on multi-byte characters. But
Bram Moolenaar78622822005-08-23 21:00:13 +000086 * don't use them for negative values!
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 */
88#ifdef FEAT_MBYTE
Bram Moolenaar78622822005-08-23 21:00:13 +000089# define MB_ISLOWER(c) vim_islower(c)
90# define MB_ISUPPER(c) vim_isupper(c)
91# define MB_TOLOWER(c) vim_tolower(c)
92# define MB_TOUPPER(c) vim_toupper(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#else
94# define MB_ISLOWER(c) islower(c)
95# define MB_ISUPPER(c) isupper(c)
96# define MB_TOLOWER(c) TOLOWER_LOC(c)
97# define MB_TOUPPER(c) TOUPPER_LOC(c)
98#endif
99
100/* Like isalpha() but reject non-ASCII characters. Can't be used with a
101 * special key (negative value). */
102#ifdef EBCDIC
103# define ASCII_ISALPHA(c) isalpha(c)
104# define ASCII_ISALNUM(c) isalnum(c)
105# define ASCII_ISLOWER(c) islower(c)
106# define ASCII_ISUPPER(c) isupper(c)
107#else
108# define ASCII_ISALPHA(c) ((c) < 0x7f && isalpha(c))
109# define ASCII_ISALNUM(c) ((c) < 0x7f && isalnum(c))
110# define ASCII_ISLOWER(c) ((c) < 0x7f && islower(c))
111# define ASCII_ISUPPER(c) ((c) < 0x7f && isupper(c))
112#endif
113
114/* Use our own isdigit() replacement, because on MS-Windows isdigit() returns
115 * non-zero for superscript 1. Also avoids that isdigit() crashes for numbers
116 * below 0 and above 255. For complicated arguments and in/decrement use
117 * vim_isdigit() instead. */
118#define VIM_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
119
120/* macro version of chartab().
121 * Only works with values 0-255!
122 * Doesn't work for UTF-8 mode with chars >= 0x80. */
123#define CHARSIZE(c) (chartab[c] & CT_CELL_MASK)
124
125#ifdef FEAT_LANGMAP
126/*
127 * Adjust chars in a language according to 'langmap' option.
128 * NOTE that there is NO overhead if 'langmap' is not set; but even
129 * when set we only have to do 2 ifs and an array lookup.
130 * Don't apply 'langmap' if the character comes from the Stuff buffer.
131 * The do-while is just to ignore a ';' after the macro.
132 */
133# define LANGMAP_ADJUST(c, condition) do { \
134 if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0 && (c) < 256) \
135 c = langmap_mapchar[c]; \
136 } while (0)
137#endif
138
139/*
140 * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
141 * it work fast.
142 */
143#define vim_isbreak(c) (breakat_flags[(char_u)(c)])
144
145/*
146 * On VMS file names are different and require a translation.
147 * On the Mac open() has only two arguments.
148 */
149#ifdef VMS
150# define mch_access(n, p) access(vms_fixfilename(n), (p))
151 /* see mch_open() comment */
152# define mch_fopen(n, p) fopen(vms_fixfilename(n), (p))
153# define mch_fstat(n, p) fstat(vms_fixfilename(n), (p))
154 /* VMS does not have lstat() */
155# define mch_stat(n, p) stat(vms_fixfilename(n), (p))
156#else
157# ifndef WIN32
158# define mch_access(n, p) access((n), (p))
159# endif
160# if !(defined(FEAT_MBYTE) && defined(WIN3264))
161# define mch_fopen(n, p) fopen((n), (p))
162# endif
163# define mch_fstat(n, p) fstat((n), (p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# ifdef MSWIN /* has it's own mch_stat() function */
165# define mch_stat(n, p) vim_stat((n), (p))
166# else
167# ifdef STAT_IGNORES_SLASH
168 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
169 * the name ends in "/" and it's not a directory. */
170# define mch_stat(n, p) (illegal_slash(n) ? -1 : stat((n), (p)))
171# else
172# define mch_stat(n, p) stat((n), (p))
173# endif
174# endif
175#endif
176
Bram Moolenaar269ec652004-07-29 08:43:53 +0000177#ifdef HAVE_LSTAT
178# define mch_lstat(n, p) lstat((n), (p))
179#else
180# define mch_lstat(n, p) mch_stat((n), (p))
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifdef MACOS_CLASSIC
184/* MacOS classic doesn't support perm but MacOS X does. */
185# define mch_open(n, m, p) open((n), (m))
186#else
187# ifdef VMS
188/*
189 * It is possible to force some record format with:
190 * # define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p)), "rat=cr", "rfm=stmlf", "mrs=0")
Bram Moolenaarff1d0d42007-05-10 17:24:16 +0000191 * but it is not recommended, because it can destroy indexes etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193# define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
194# else
195# if !(defined(FEAT_MBYTE) && defined(WIN3264))
196# define mch_open(n, m, p) open((n), (m), (p))
197# endif
198# endif
199#endif
200
201/* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
202#if defined(UNIX) || defined(VMS) /* open in rw------- mode */
203# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
204#else
205# if defined(MSDOS) || defined(MSWIN) || defined(OS2) /* open read/write */
206# define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
207# else
208# define mch_open_rw(n, f) mch_open((n), (f), 0)
209# endif
210#endif
211
212/*
213 * Encryption macros. Mohsin Ahmed, mosh@sasi.com 98-09-24
214 * Based on zip/crypt sources.
215 */
216
217#ifdef FEAT_CRYPT
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* encode byte c, using temp t. Warning: c must not have side effects. */
220# define ZENCODE(c, t) (t = decrypt_byte(), update_keys(c), t^(c))
221
222/* decode byte c in place */
223# define ZDECODE(c) update_keys(c ^= decrypt_byte())
224
225#endif
226
227#ifdef STARTUPTIME
228# define TIME_MSG(s) time_msg(s, NULL)
229#else
230# define TIME_MSG(s)
231#endif
232
233#ifdef FEAT_VREPLACE
234# define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
235#else
236# define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
237#endif
238
239#ifdef FEAT_ARABIC
240# define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
241#else
242# define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
243#endif
244
245#ifdef FEAT_RIGHTLEFT
246 /* Whether to draw the vertical bar on the right side of the cell. */
247# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
248#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000249
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000250/*
251 * mb_ptr_adv(): advance a pointer to the next character, taking care of
252 * multi-byte characters if needed.
253 * mb_ptr_back(): backup a pointer to the previous character, taking care of
254 * multi-byte characters if needed.
Bram Moolenaarfd371682005-01-14 21:42:54 +0000255 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
Bram Moolenaar78984f52005-08-01 07:19:10 +0000256 * PTR2CHAR(): get character from pointer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000257 */
258#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000259/* Advance multi-byte pointer, skip over composing chars. */
260# define mb_ptr_adv(p) p += has_mbyte ? (*mb_ptr2len)(p) : 1
261/* Advance multi-byte pointer, do not skip over composing chars. */
262# define mb_cptr_adv(p) p += enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
263/* Backup multi-byte pointer. */
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000264# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000265/* get length of multi-byte char, not including composing chars */
266# define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))
267
Bram Moolenaarfd371682005-01-14 21:42:54 +0000268# define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
Bram Moolenaare6b165e2005-06-30 21:56:01 +0000269# define MB_CHARLEN(p) (has_mbyte ? mb_charlen(p) : STRLEN(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270# define PTR2CHAR(p) (has_mbyte ? mb_ptr2char(p) : (int)*(p))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000271#else
Bram Moolenaar78984f52005-08-01 07:19:10 +0000272# define mb_ptr_adv(p) ++p
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000273# define mb_cptr_adv(p) ++p
Bram Moolenaar78984f52005-08-01 07:19:10 +0000274# define mb_ptr_back(s, p) --p
275# define MB_COPY_CHAR(f, t) *t++ = *f++
276# define MB_CHARLEN(p) STRLEN(p)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000277# define PTR2CHAR(p) ((int)*(p))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000278#endif
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000279
280#ifdef FEAT_AUTOCHDIR
281# define DO_AUTOCHDIR if (p_acd) do_autochdir();
282#else
283# define DO_AUTOCHDIR
284#endif