macros.h
Go to the documentation of this file.
1 #ifndef NVIM_MACROS_H
2 #define NVIM_MACROS_H
3 
4 #include "auto/config.h"
5 
6 // EXTERN is only defined in main.c. That's where global variables are
7 // actually defined and initialized.
8 #ifndef EXTERN
9 # define EXTERN extern
10 # define INIT(...)
11 #else
12 # ifndef INIT
13 # define INIT(...) __VA_ARGS__
14 # define COMMA ,
15 # endif
16 #endif
17 
18 #ifndef MIN
19 # define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
20 #endif
21 #ifndef MAX
22 # define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
23 #endif
24 
32 #define S_LEN(s) (s), (sizeof(s) - 1)
33 
35 #define LINEEMPTY(p) (*ml_get(p) == NUL)
36 
37 // toupper() and tolower() that use the current locale.
38 // Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
39 // range 0 - 255. toupper()/tolower() on some systems can't handle others.
40 // Note: It is often better to use mb_tolower() and mb_toupper(), because many
41 // toupper() and tolower() implementations only work for ASCII.
42 #define TOUPPER_LOC toupper
43 #define TOLOWER_LOC tolower
44 
45 // toupper() and tolower() for ASCII only and ignore the current locale.
46 # define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
47 # define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))
48 
49 // Like isalpha() but reject non-ASCII characters. Can't be used with a
50 // special key (negative value).
51 # define ASCII_ISLOWER(c) ((unsigned)(c) >= 'a' && (unsigned)(c) <= 'z')
52 # define ASCII_ISUPPER(c) ((unsigned)(c) >= 'A' && (unsigned)(c) <= 'Z')
53 # define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
54 # define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || ascii_isdigit(c))
55 
56 // Returns empty string if it is NULL.
57 #define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")
58 
59 // Adjust chars in a language according to 'langmap' option.
60 // NOTE that there is no noticeable overhead if 'langmap' is not set.
61 // When set the overhead for characters < 256 is small.
62 // Don't apply 'langmap' if the character comes from the Stuff buffer or from a
63 // mapping and the langnoremap option was set.
64 // The do-while is just to ignore a ';' after the macro.
65 # define LANGMAP_ADJUST(c, condition) \
66  do { \
67  if (*p_langmap \
68  && (condition) \
69  && (p_lrm || (vgetc_busy ? typebuf_maplen() == 0 : KeyTyped)) \
70  && !KeyStuffed \
71  && (c) >= 0) \
72  { \
73  if ((c) < 256) \
74  c = langmap_mapchar[c]; \
75  else \
76  c = langmap_adjust_mb(c); \
77  } \
78  } while (0)
79 
80 #define WRITEBIN "wb" // no CR-LF translation
81 #define READBIN "rb"
82 #define APPENDBIN "ab"
83 
84 // mch_open_rw(): invoke os_open() with third argument for user R/W.
85 #if defined(UNIX) // open in rw------- mode
86 # define mch_open_rw(n, f) os_open((n), (f), (mode_t)0600)
87 #elif defined(WIN32)
88 # define mch_open_rw(n, f) os_open((n), (f), S_IREAD | S_IWRITE)
89 #else
90 # define mch_open_rw(n, f) os_open((n), (f), 0)
91 #endif
92 
93 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
94 
95 # define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
96 
97 // MB_PTR_ADV(): advance a pointer to the next character, taking care of
98 // multi-byte characters if needed.
99 // MB_PTR_BACK(): backup a pointer to the previous character, taking care of
100 // multi-byte characters if needed.
101 // MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
102 // PTR2CHAR(): get character from pointer.
103 
104 // Advance multi-byte pointer, skip over composing chars.
105 # define MB_PTR_ADV(p) (p += mb_ptr2len((char_u *)p))
106 // Advance multi-byte pointer, do not skip over composing chars.
107 # define MB_CPTR_ADV(p) (p += utf_ptr2len(p))
108 // Backup multi-byte pointer. Only use with "p" > "s" !
109 # define MB_PTR_BACK(s, p) \
110  (p -= utf_head_off((char_u *)s, (char_u *)p - 1) + 1)
111 // get length of multi-byte char, not including composing chars
112 # define MB_CPTR2LEN(p) utf_ptr2len(p)
113 
114 # define MB_COPY_CHAR(f, t) mb_copy_char((const char_u **)(&f), &t);
115 
116 # define MB_CHARLEN(p) mb_charlen(p)
117 # define MB_CHAR2LEN(c) mb_char2len(c)
118 # define PTR2CHAR(p) utf_ptr2char(p)
119 
120 # define RESET_BINDING(wp) \
121  do { \
122  (wp)->w_p_scb = false; \
123  (wp)->w_p_crb = false; \
124  } while (0)
125 
134 #define ARRAY_SIZE(arr) \
135  ((sizeof(arr)/sizeof((arr)[0])) \
136  / ((size_t)(!(sizeof(arr) % sizeof((arr)[0])))))
137 
142 #define ARRAY_LAST_ENTRY(arr) (arr)[ARRAY_SIZE(arr) - 1]
143 
144 // Duplicated in os/win_defs.h to avoid include-order sensitivity.
145 #define RGB_(r, g, b) ((r << 16) | (g << 8) | b)
146 
147 #define STR_(x) #x
148 #define STR(x) STR_(x)
149 
150 #ifndef __has_include
151 # define NVIM_HAS_INCLUDE(x) 0
152 #else
153 # define NVIM_HAS_INCLUDE __has_include
154 #endif
155 
156 #ifndef __has_attribute
157 # define NVIM_HAS_ATTRIBUTE(x) 0
158 #elif defined(__clang__) && __clang__ == 1 \
159  && (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ <= 5))
160 // Starting in Clang 3.6, __has_attribute was fixed to only report true for
161 // GNU-style attributes. Prior to that, it reported true if _any_ backend
162 // supported the attribute.
163 # define NVIM_HAS_ATTRIBUTE(x) 0
164 #else
165 # define NVIM_HAS_ATTRIBUTE __has_attribute
166 #endif
167 
168 #if NVIM_HAS_ATTRIBUTE(fallthrough) \
169  && (!defined(__apple_build_version__) || __apple_build_version__ >= 7000000)
170 # define FALLTHROUGH {} __attribute__((fallthrough))
171 #else
172 # define FALLTHROUGH
173 #endif
174 
175 // -V:STRUCT_CAST:641
176 
185 #define STRUCT_CAST(Type, obj) ((Type *)(obj))
186 
187 // Type of uv_buf_t.len is platform-dependent.
188 // Related: https://github.com/libuv/libuv/pull/1236
189 #if defined(WIN32)
190 # define UV_BUF_LEN(x) (ULONG)(x)
191 #else
192 # define UV_BUF_LEN(x) (x)
193 #endif
194 
195 // Type of read()/write() `count` param is platform-dependent.
196 #if defined(WIN32)
197 # define IO_COUNT(x) (unsigned)(x)
198 #else
199 # define IO_COUNT(x) (x)
200 #endif
201 
205 #if defined(__clang__) && __clang__ == 1
206 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \
207  _Pragma("clang diagnostic push") \
208  _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"")
209 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG
210 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
211  _Pragma("clang diagnostic push") \
212  _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"")
213 # else
214 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
215  _Pragma("clang diagnostic push")
216 # endif
217 # define PRAGMA_DIAG_POP \
218  _Pragma("clang diagnostic pop")
219 #elif defined(__GNUC__)
220 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \
221  _Pragma("GCC diagnostic push") \
222  _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
223 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG
224 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
225  _Pragma("GCC diagnostic push") \
226  _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
227 # else
228 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
229  _Pragma("GCC diagnostic push")
230 # endif
231 # define PRAGMA_DIAG_POP \
232  _Pragma("GCC diagnostic pop")
233 #else
234 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES
235 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH
236 # define PRAGMA_DIAG_POP
237 #endif
238 
239 #define EMPTY_POS(a) ((a).lnum == 0 && (a).col == 0 && (a).coladd == 0)
240 
241 #endif // NVIM_MACROS_H