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