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) (char *)((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 
96 // MB_PTR_ADV(): advance a pointer to the next character, taking care of
97 // multi-byte characters if needed. Skip over composing chars.
98 #define MB_PTR_ADV(p) (p += utfc_ptr2len((char_u *)p))
99 
100 // Advance multi-byte pointer, do not skip over composing chars.
101 #define MB_CPTR_ADV(p) (p += utf_ptr2len(p))
102 
103 // MB_PTR_BACK(): backup a pointer to the previous character, taking care of
104 // multi-byte characters if needed. Only use with "p" > "s" !
105 #define MB_PTR_BACK(s, p) \
106  (p -= utf_head_off((char_u *)s, (char_u *)p - 1) + 1)
107 
108 #define RESET_BINDING(wp) \
109  do { \
110  (wp)->w_p_scb = false; \
111  (wp)->w_p_crb = false; \
112  } while (0)
113 
122 #define ARRAY_SIZE(arr) \
123  ((sizeof(arr)/sizeof((arr)[0])) \
124  / ((size_t)(!(sizeof(arr) % sizeof((arr)[0])))))
125 
130 #define ARRAY_LAST_ENTRY(arr) (arr)[ARRAY_SIZE(arr) - 1]
131 
132 // Duplicated in os/win_defs.h to avoid include-order sensitivity.
133 #define RGB_(r, g, b) ((r << 16) | (g << 8) | b)
134 
135 #define STR_(x) #x
136 #define STR(x) STR_(x)
137 
138 #ifndef __has_include
139 # define NVIM_HAS_INCLUDE(x) 0
140 #else
141 # define NVIM_HAS_INCLUDE __has_include
142 #endif
143 
144 #ifndef __has_attribute
145 # define NVIM_HAS_ATTRIBUTE(x) 0
146 #elif defined(__clang__) && __clang__ == 1 \
147  && (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ <= 5))
148 // Starting in Clang 3.6, __has_attribute was fixed to only report true for
149 // GNU-style attributes. Prior to that, it reported true if _any_ backend
150 // supported the attribute.
151 # define NVIM_HAS_ATTRIBUTE(x) 0
152 #else
153 # define NVIM_HAS_ATTRIBUTE __has_attribute
154 #endif
155 
156 #if NVIM_HAS_ATTRIBUTE(fallthrough) \
157  && (!defined(__apple_build_version__) || __apple_build_version__ >= 7000000)
158 # define FALLTHROUGH {} __attribute__((fallthrough))
159 #else
160 # define FALLTHROUGH
161 #endif
162 
163 // -V:STRUCT_CAST:641
164 
173 #define STRUCT_CAST(Type, obj) ((Type *)(obj))
174 
175 // Type of uv_buf_t.len is platform-dependent.
176 // Related: https://github.com/libuv/libuv/pull/1236
177 #if defined(WIN32)
178 # define UV_BUF_LEN(x) (ULONG)(x)
179 #else
180 # define UV_BUF_LEN(x) (x)
181 #endif
182 
183 // Type of read()/write() `count` param is platform-dependent.
184 #if defined(WIN32)
185 # define IO_COUNT(x) (unsigned)(x)
186 #else
187 # define IO_COUNT(x) (x)
188 #endif
189 
193 #if defined(__clang__) && __clang__ == 1
194 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \
195  _Pragma("clang diagnostic push") \
196  _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"")
197 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG
198 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
199  _Pragma("clang diagnostic push") \
200  _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"")
201 # else
202 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
203  _Pragma("clang diagnostic push")
204 # endif
205 # define PRAGMA_DIAG_POP \
206  _Pragma("clang diagnostic pop")
207 #elif defined(__GNUC__)
208 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \
209  _Pragma("GCC diagnostic push") \
210  _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
211 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG
212 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
213  _Pragma("GCC diagnostic push") \
214  _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
215 # else
216 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \
217  _Pragma("GCC diagnostic push")
218 # endif
219 # define PRAGMA_DIAG_POP \
220  _Pragma("GCC diagnostic pop")
221 #else
222 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES
223 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH
224 # define PRAGMA_DIAG_POP
225 #endif
226 
227 #define EMPTY_POS(a) ((a).lnum == 0 && (a).col == 0 && (a).coladd == 0)
228 
229 #endif // NVIM_MACROS_H