blob: 07b02d70769cf01c39620cb82aa93b3756837aa7 [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))
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
Bram Moolenaar8348ea62005-06-14 22:05:40 +000087# 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)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#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))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162# ifdef MSWIN /* has it's own mch_stat() function */
163# define mch_stat(n, p) vim_stat((n), (p))
164# else
165# ifdef STAT_IGNORES_SLASH
166 /* On Solaris stat() accepts "file/" as if it was "file". Return -1 if
167 * the name ends in "/" and it's not a directory. */
168# define mch_stat(n, p) (illegal_slash(n) ? -1 : stat((n), (p)))
169# else
170# define mch_stat(n, p) stat((n), (p))
171# endif
172# endif
173#endif
174
Bram Moolenaar269ec652004-07-29 08:43:53 +0000175#ifdef HAVE_LSTAT
176# define mch_lstat(n, p) lstat((n), (p))
177#else
178# define mch_lstat(n, p) mch_stat((n), (p))
179#endif
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#ifdef MACOS_CLASSIC
182/* MacOS classic doesn't support perm but MacOS X does. */
183# define mch_open(n, m, p) open((n), (m))
184#else
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 recomended, because it can destroy indexes etc.
190 */
191# define mch_open(n, m, p) open(vms_fixfilename(n), (m), (p))
192# else
193# if !(defined(FEAT_MBYTE) && defined(WIN3264))
194# define mch_open(n, m, p) open((n), (m), (p))
195# endif
196# endif
197#endif
198
199/* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
200#if defined(UNIX) || defined(VMS) /* open in rw------- mode */
201# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
202#else
203# if defined(MSDOS) || defined(MSWIN) || defined(OS2) /* open read/write */
204# define mch_open_rw(n, f) mch_open((n), (f), S_IREAD | S_IWRITE)
205# else
206# define mch_open_rw(n, f) mch_open((n), (f), 0)
207# endif
208#endif
209
210/*
211 * Encryption macros. Mohsin Ahmed, mosh@sasi.com 98-09-24
212 * Based on zip/crypt sources.
213 */
214
215#ifdef FEAT_CRYPT
216
217#ifndef __MINGW32__
218# define PWLEN 80
219#endif
220
221/* encode byte c, using temp t. Warning: c must not have side effects. */
222# define ZENCODE(c, t) (t = decrypt_byte(), update_keys(c), t^(c))
223
224/* decode byte c in place */
225# define ZDECODE(c) update_keys(c ^= decrypt_byte())
226
227#endif
228
229#ifdef STARTUPTIME
230# define TIME_MSG(s) time_msg(s, NULL)
231#else
232# define TIME_MSG(s)
233#endif
234
235#ifdef FEAT_VREPLACE
236# define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
237#else
238# define REPLACE_NORMAL(s) ((s) & REPLACE_FLAG)
239#endif
240
241#ifdef FEAT_ARABIC
242# define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
243#else
244# define UTF_COMPOSINGLIKE(p1, p2) utf_iscomposing(utf_ptr2char(p2))
245#endif
246
247#ifdef FEAT_RIGHTLEFT
248 /* Whether to draw the vertical bar on the right side of the cell. */
249# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
250#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000251
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000252/*
253 * mb_ptr_adv(): advance a pointer to the next character, taking care of
254 * multi-byte characters if needed.
255 * mb_ptr_back(): backup a pointer to the previous character, taking care of
256 * multi-byte characters if needed.
Bram Moolenaarfd371682005-01-14 21:42:54 +0000257 * MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000258 */
259#ifdef FEAT_MBYTE
260# define mb_ptr_adv(p) p += has_mbyte ? (*mb_ptr2len_check)(p) : 1
261# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1
Bram Moolenaarfd371682005-01-14 21:42:54 +0000262# define MB_COPY_CHAR(f, t) if (has_mbyte) mb_copy_char(&f, &t); else *t++ = *f++
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263# define MB_CHARLEN(p) mb_charlen(p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000264#else
265# define mb_ptr_adv(p) ++p
266# define mb_ptr_back(s, p) --p
Bram Moolenaarfd371682005-01-14 21:42:54 +0000267# define MB_COPY_CHAR(f, t) *t++ = *f++
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000268# define MB_CHARLEN(p) STRLEN(p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000269#endif