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