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 # define mch_fopen(n, p) fopen((n), (p))
90 
91 /* mch_open_rw(): invoke os_open() with third argument for user R/W. */
92 #if defined(UNIX) /* open in rw------- mode */
93 # define mch_open_rw(n, f) os_open((n), (f), (mode_t)0600)
94 #elif defined(WIN32)
95 # define mch_open_rw(n, f) os_open((n), (f), S_IREAD | S_IWRITE)
96 #else
97 # define mch_open_rw(n, f) os_open((n), (f), 0)
98 #endif
99 
100 # define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))
101 
102 # define UTF_COMPOSINGLIKE(p1, p2) utf_composinglike((p1), (p2))
103 
104 /* Whether to draw the vertical bar on the right side of the cell. */
105 # define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))
106 
107 // MB_PTR_ADV(): advance a pointer to the next character, taking care of
108 // multi-byte characters if needed.
109 // MB_PTR_BACK(): backup a pointer to the previous character, taking care of
110 // multi-byte characters if needed.
111 // MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
112 // PTR2CHAR(): get character from pointer.
113 
114 // Get the length of the character p points to
115 # define MB_PTR2LEN(p) mb_ptr2len(p)
116 // Advance multi-byte pointer, skip over composing chars.
117 # define MB_PTR_ADV(p) (p += mb_ptr2len((char_u *)p))
118 // Advance multi-byte pointer, do not skip over composing chars.
119 # define MB_CPTR_ADV(p) (p += utf_ptr2len(p))
120 // Backup multi-byte pointer. Only use with "p" > "s" !
121 # define MB_PTR_BACK(s, p) \
122  (p -= utf_head_off((char_u *)s, (char_u *)p - 1) + 1)
123 // get length of multi-byte char, not including composing chars
124 # define MB_CPTR2LEN(p) utf_ptr2len(p)
125 
126 # define MB_COPY_CHAR(f, t) mb_copy_char((const char_u **)(&f), &t);
127 
128 # define MB_CHARLEN(p) mb_charlen(p)
129 # define MB_CHAR2LEN(c) mb_char2len(c)
130 # define PTR2CHAR(p) utf_ptr2char(p)
131 
132 # define RESET_BINDING(wp) (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE
133 
140 #define ARRAY_SIZE(arr) \
141  ((sizeof(arr)/sizeof((arr)[0])) \
142  / ((size_t)(!(sizeof(arr) % sizeof((arr)[0])))))
143 
148 #define ARRAY_LAST_ENTRY(arr) (arr)[ARRAY_SIZE(arr) - 1]
149 
150 // Duplicated in os/win_defs.h to avoid include-order sensitivity.
151 #define RGB_(r, g, b) ((r << 16) | (g << 8) | b)
152 
153 #define STR_(x) #x
154 #define STR(x) STR_(x)
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 # define FALLTHROUGH __attribute__((fallthrough))
170 #else
171 # define FALLTHROUGH
172 #endif
173 
174 // -V:STRUCT_CAST:641
175 
184 #define STRUCT_CAST(Type, obj) ((Type *)(obj))
185 
186 // Type of uv_buf_t.len is platform-dependent.
187 // Related: https://github.com/libuv/libuv/pull/1236
188 #if defined(WIN32)
189 # define UV_BUF_LEN(x) (ULONG)(x)
190 #else
191 # define UV_BUF_LEN(x) (x)
192 #endif
193 
194 // Type of read()/write() `count` param is platform-dependent.
195 #if defined(WIN32)
196 # define IO_COUNT(x) (unsigned)(x)
197 #else
198 # define IO_COUNT(x) (x)
199 #endif
200 
201 #endif // NVIM_MACROS_H