aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/macros.h
blob: 26d4f74b6a424e4f0282b28e4c60c1e5fee41423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef NVIM_MACROS_H
#define NVIM_MACROS_H

// EXTERN is only defined in main.c. That's where global variables are
// actually defined and initialized.
#ifndef EXTERN
# define EXTERN extern
# define INIT(...)
#else
# ifndef INIT
#  define INIT(...) __VA_ARGS__
# endif
#endif

#ifndef MIN
# define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#endif
#ifndef MAX
# define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#endif

/// String with length
///
/// For use in functions which accept (char *s, size_t len) pair in arguments.
///
/// @param[in]  s  Static string.
///
/// @return `s, sizeof(s) - 1`
#define S_LEN(s) (s), (sizeof(s) - 1)

/*
 * lineempty() - return TRUE if the line is empty
 */
#define lineempty(p) (*ml_get(p) == NUL)

/*
 * bufempty() - return TRUE if the current buffer is empty
 */
#define bufempty() (curbuf->b_ml.ml_line_count == 1 && *ml_get((linenr_T)1) == \
                    NUL)

/*
 * toupper() and tolower() that use the current locale.
 * Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
 * range 0 - 255.  toupper()/tolower() on some systems can't handle others.
 * Note: It is often better to use mb_tolower() and mb_toupper(), because many
 * toupper() and tolower() implementations only work for ASCII.
 */
#define TOUPPER_LOC toupper
#define TOLOWER_LOC tolower

/* toupper() and tolower() for ASCII only and ignore the current locale. */
# define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A'))
# define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A'))

/* Like isalpha() but reject non-ASCII characters.  Can't be used with a
 * special key (negative value). */
# define ASCII_ISLOWER(c) ((unsigned)(c) >= 'a' && (unsigned)(c) <= 'z')
# define ASCII_ISUPPER(c) ((unsigned)(c) >= 'A' && (unsigned)(c) <= 'Z')
# define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
# define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || ascii_isdigit(c))

/* Returns empty string if it is NULL. */
#define EMPTY_IF_NULL(x) ((x) ? (x) : (char_u *)"")

/*
 * Adjust chars in a language according to 'langmap' option.
 * NOTE that there is no noticeable overhead if 'langmap' is not set.
 * When set the overhead for characters < 256 is small.
 * Don't apply 'langmap' if the character comes from the Stuff buffer or from a
 * mapping and the langnoremap option was set.
 * The do-while is just to ignore a ';' after the macro.
 */
#  define LANGMAP_ADJUST(c, condition) \
  do { \
    if (*p_langmap \
        && (condition) \
        && (p_lrm || KeyTyped) \
        && !KeyStuffed \
        && (c) >= 0) \
    { \
      if ((c) < 256) \
        c = langmap_mapchar[c]; \
      else \
        c = langmap_adjust_mb(c); \
    } \
  } while (0)

/*
 * vim_isbreak() is used very often if 'linebreak' is set, use a macro to make
 * it work fast.
 */
#define vim_isbreak(c) (breakat_flags[(char_u)(c)])

#define WRITEBIN   "wb"        /* no CR-LF translation */
#define READBIN    "rb"
#define APPENDBIN  "ab"

#  define mch_fopen(n, p)       fopen((n), (p))

/* mch_open_rw(): invoke os_open() with third argument for user R/W. */
#if defined(UNIX)  /* open in rw------- mode */
# define mch_open_rw(n, f)      os_open((n), (f), (mode_t)0600)
#elif defined(WIN32)
# define mch_open_rw(n, f)      os_open((n), (f), S_IREAD | S_IWRITE)
#else
# define mch_open_rw(n, f)      os_open((n), (f), 0)
#endif

# define REPLACE_NORMAL(s) (((s) & REPLACE_FLAG) && !((s) & VREPLACE_FLAG))

# define UTF_COMPOSINGLIKE(p1, p2)  utf_composinglike((p1), (p2))

/* Whether to draw the vertical bar on the right side of the cell. */
# define CURSOR_BAR_RIGHT (curwin->w_p_rl && (!(State & CMDLINE) || cmdmsg_rl))

// mb_ptr_adv(): advance a pointer to the next character, taking care of
// multi-byte characters if needed.
// mb_ptr_back(): backup a pointer to the previous character, taking care of
// multi-byte characters if needed.
// MB_COPY_CHAR(f, t): copy one char from "f" to "t" and advance the pointers.
// PTR2CHAR(): get character from pointer.

// Get the length of the character p points to
# define MB_PTR2LEN(p)          mb_ptr2len(p)
// Advance multi-byte pointer, skip over composing chars.
# define mb_ptr_adv(p)      (p += mb_ptr2len((char_u *)p))
// Advance multi-byte pointer, do not skip over composing chars.
# define mb_cptr_adv(p)     (p += utf_ptr2len(p))
// Backup multi-byte pointer. Only use with "p" > "s" !
# define mb_ptr_back(s, p)  (p -= mb_head_off((char_u *)s, (char_u *)p - 1) + 1)
// get length of multi-byte char, not including composing chars
# define MB_CPTR2LEN(p)     utf_ptr2len(p)

# define MB_COPY_CHAR(f, t) mb_copy_char((const char_u **)(&f), &t);

# define MB_CHARLEN(p)      mb_charlen(p)
# define MB_CHAR2LEN(c)     mb_char2len(c)
# define PTR2CHAR(p)        mb_ptr2char(p)

# define RESET_BINDING(wp)  (wp)->w_p_scb = FALSE; (wp)->w_p_crb = FALSE

/// Calculate the length of a C array.
///
/// This should be called with a real array. Calling this with a pointer is an
/// error. A mechanism to detect many (though not all) of those errors at compile
/// time is implemented. It works by the second division producing a division by
/// zero in those cases (-Wdiv-by-zero in GCC).
#define ARRAY_SIZE(arr) ((sizeof(arr)/sizeof((arr)[0])) / ((size_t)(!(sizeof(arr) % sizeof((arr)[0])))))

#define RGB(r, g, b) ((r << 16) | (g << 8) | b)

#define STR_(x) #x
#define STR(x) STR_(x)

#ifndef __has_attribute
# define NVIM_HAS_ATTRIBUTE(x) 0
#elif defined(__clang__) && __clang__ == 1 \
    && (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ <= 5))
// Starting in Clang 3.6, __has_attribute was fixed to only report true for
// GNU-style attributes.  Prior to that, it reported true if _any_ backend
// supported the attribute.
# define NVIM_HAS_ATTRIBUTE(x) 0
#else
# define NVIM_HAS_ATTRIBUTE __has_attribute
#endif

#if NVIM_HAS_ATTRIBUTE(fallthrough)
# define FALLTHROUGH __attribute__((fallthrough))
#else
# define FALLTHROUGH
#endif

// -V:STRUCT_CAST:641

/// Change type of structure pointers: cast `struct a *` to `struct b *`
///
/// Used to silence PVS errors.
///
/// @param  Type  Structure to cast to.
/// @param  obj  Object to cast.
///
/// @return ((Type *)obj).
#define STRUCT_CAST(Type, obj) ((Type *)(obj))

#endif  // NVIM_MACROS_H