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