aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt4
-rw-r--r--runtime/doc/api.txt40
-rwxr-xr-xscripts/gen_api_vimdoc.py21
-rwxr-xr-xsrc/clint.py7
-rw-r--r--src/nvim/api/buffer.c52
-rw-r--r--src/nvim/buffer_defs.h3
-rw-r--r--src/nvim/diff.c22
-rw-r--r--src/nvim/edit.c20
-rw-r--r--src/nvim/ex_cmds.c162
-rw-r--r--src/nvim/ex_docmd.c4
-rw-r--r--src/nvim/fold.c431
-rw-r--r--src/nvim/globals.h8
-rw-r--r--src/nvim/hardcopy.c139
-rw-r--r--src/nvim/hardcopy.h13
-rw-r--r--src/nvim/menu.c25
-rw-r--r--src/nvim/misc1.c56
-rw-r--r--src/nvim/normal.c6
-rw-r--r--src/nvim/ops.c2
-rw-r--r--src/nvim/screen.c94
-rw-r--r--src/nvim/search.c106
-rw-r--r--src/nvim/state.c6
-rw-r--r--src/nvim/syntax.c674
-rw-r--r--src/nvim/syntax.h1
-rw-r--r--src/nvim/syntax_defs.h32
-rw-r--r--src/nvim/window.c15
-rw-r--r--test/functional/api/buffer_spec.lua57
26 files changed, 1043 insertions, 957 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3c2e40ab8b..02b555d540 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -113,9 +113,9 @@ set(NVIM_VERSION_PATCH 2)
set(NVIM_VERSION_PRERELEASE "-dev") # for package maintainers
# API level
-set(NVIM_API_LEVEL 4) # Bump this after any API change.
+set(NVIM_API_LEVEL 5) # Bump this after any API change.
set(NVIM_API_LEVEL_COMPAT 0) # Adjust this after a _breaking_ API change.
-set(NVIM_API_PRERELEASE false)
+set(NVIM_API_PRERELEASE true)
file(TO_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git FORCED_GIT_DIR)
include(GetGitRevisionDescription)
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt
index 21f818cb8d..c260101e92 100644
--- a/runtime/doc/api.txt
+++ b/runtime/doc/api.txt
@@ -836,10 +836,26 @@ nvim_get_proc({pid}) *nvim_get_proc()*
Return: ~
Map of process properties, or NIL if process not found.
+nvim__inspect_cell({row}, {col}) *nvim__inspect_cell()*
+ TODO: Documentation
+
==============================================================================
Buffer Functions *api-buffer*
+Unloaded Buffers:~
+
+Buffers may be unloaded by the |:bunload| command or the
+buffer's |'bufhidden'| option. When a buffer is unloaded its
+file contents are freed from memory and vim cannot operate on
+the buffer lines until it is reloaded (usually by opening the
+buffer again in a new window). API methods such as
+|nvim_buf_get_lines()| and |nvim_buf_line_count()| will be
+affected.
+
+You can use |nvim_buf_is_loaded()| or |nvim_buf_line_count()|
+to check whether a buffer is loaded.
+
nvim_buf_line_count({buffer}) *nvim_buf_line_count()*
Gets the buffer line count
@@ -847,7 +863,8 @@ nvim_buf_line_count({buffer}) *nvim_buf_line_count()*
{buffer} Buffer handle
Return: ~
- Line count
+ Line count, or `0` if the buffer has been unloaded (see
+ |api-buffer|).
nvim_buf_attach({buffer}, {send_buffer}, {opts}) *nvim_buf_attach()*
Activate updates from this buffer to the current channel.
@@ -896,7 +913,8 @@ nvim_buf_get_lines({buffer}, {start}, {end}, {strict_indexing})
error.
Return: ~
- Array of lines
+ Array of lines. If the buffer has been unloaded then an
+ empty array will be returned instead. (See |api-buffer|.)
*nvim_buf_set_lines()*
nvim_buf_set_lines({buffer}, {start}, {end}, {strict_indexing},
@@ -1013,14 +1031,28 @@ nvim_buf_set_name({buffer}, {name}) *nvim_buf_set_name()*
{buffer} Buffer handle
{name} Buffer name
+nvim_buf_is_loaded({buffer}) *nvim_buf_is_loaded()*
+ Checks if a buffer is valid and loaded. See |api-buffer| for
+ more info about unloaded buffers.
+
+ Parameters: ~
+ {buffer} Buffer handle
+
+ Return: ~
+ true if the buffer is valid and loaded, false otherwise.
+
nvim_buf_is_valid({buffer}) *nvim_buf_is_valid()*
- Checks if a buffer is valid
+ Checks if a buffer is valid.
+
+ Note:
+ Even if a buffer is valid it may have been unloaded. See
+ |api-buffer| for more info about unloaded buffers.
Parameters: ~
{buffer} Buffer handle
Return: ~
- true if the buffer is valid, false otherwise
+ true if the buffer is valid, false otherwise.
nvim_buf_get_mark({buffer}, {name}) *nvim_buf_get_mark()*
Return a tuple (row,col) representing the position of the
diff --git a/scripts/gen_api_vimdoc.py b/scripts/gen_api_vimdoc.py
index 0bbc3706c6..4e86f15b37 100755
--- a/scripts/gen_api_vimdoc.py
+++ b/scripts/gen_api_vimdoc.py
@@ -413,10 +413,26 @@ def gen_docs(config):
sys.exit(p.returncode)
sections = {}
+ intros = {}
sep = '=' * text_width
base = os.path.join(out_dir, 'xml')
dom = minidom.parse(os.path.join(base, 'index.xml'))
+
+ # generate docs for section intros
+ for compound in dom.getElementsByTagName('compound'):
+ if compound.getAttribute('kind') != 'group':
+ continue
+
+ groupname = get_text(find_first(compound, 'name'))
+ groupxml = os.path.join(base, '%s.xml' % compound.getAttribute('refid'))
+
+ desc = find_first(minidom.parse(groupxml), 'detaileddescription')
+ if desc:
+ doc = parse_parblock(desc)
+ if doc:
+ intros[groupname] = doc
+
for compound in dom.getElementsByTagName('compound'):
if compound.getAttribute('kind') != 'file':
continue
@@ -437,6 +453,11 @@ def gen_docs(config):
name = name.title()
doc = ''
+
+ intro = intros.get('api-%s' % name.lower())
+ if intro:
+ doc += '\n\n' + intro
+
if functions:
doc += '\n\n' + functions
diff --git a/src/clint.py b/src/clint.py
index c0cedc0e5b..9fd93ce143 100755
--- a/src/clint.py
+++ b/src/clint.py
@@ -3299,6 +3299,13 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
error(filename, linenum, 'readability/bool', 4,
'Use %s instead of %s.' % (token.lower(), token))
+ # Detect MAYBE
+ match = Search(r'\b(MAYBE)\b', line)
+ if match:
+ token = match.group(1)
+ error(filename, linenum, 'readability/bool', 4,
+ 'Use kNONE from TriState instead of %s.' % token)
+
# Detect preincrement/predecrement
match = Match(r'^\s*(?:\+\+|--)', line)
if match:
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 8ff24b877e..67a4b70c9e 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -31,11 +31,27 @@
# include "api/buffer.c.generated.h"
#endif
+
+/// \defgroup api-buffer
+///
+/// Unloaded Buffers:~
+///
+/// Buffers may be unloaded by the |:bunload| command or the buffer's
+/// |'bufhidden'| option. When a buffer is unloaded its file contents are freed
+/// from memory and vim cannot operate on the buffer lines until it is reloaded
+/// (usually by opening the buffer again in a new window). API methods such as
+/// |nvim_buf_get_lines()| and |nvim_buf_line_count()| will be affected.
+///
+/// You can use |nvim_buf_is_loaded()| or |nvim_buf_line_count()| to check
+/// whether a buffer is loaded.
+
+
/// Gets the buffer line count
///
/// @param buffer Buffer handle
/// @param[out] err Error details, if any
-/// @return Line count
+/// @return Line count, or \`0` if the buffer has been unloaded (see
+/// |api-buffer|).
Integer nvim_buf_line_count(Buffer buffer, Error *err)
FUNC_API_SINCE(1)
{
@@ -45,6 +61,11 @@ Integer nvim_buf_line_count(Buffer buffer, Error *err)
return 0;
}
+ // return sentinel value if the buffer isn't loaded
+ if (buf->b_ml.ml_mfp == NULL) {
+ return 0;
+ }
+
return buf->b_ml.ml_line_count;
}
@@ -205,7 +226,8 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer,
/// @param end Last line index (exclusive)
/// @param strict_indexing Whether out-of-bounds should be an error.
/// @param[out] err Error details, if any
-/// @return Array of lines
+/// @return Array of lines. If the buffer has been unloaded then an empty array
+/// will be returned instead. (See |api-buffer|.)
ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
Buffer buffer,
Integer start,
@@ -221,6 +243,11 @@ ArrayOf(String) nvim_buf_get_lines(uint64_t channel_id,
return rv;
}
+ // return sentinel value if the buffer isn't loaded
+ if (buf->b_ml.ml_mfp == NULL) {
+ return rv;
+ }
+
bool oob = false;
start = normalize_index(buf, start, &oob);
end = normalize_index(buf, end, &oob);
@@ -745,10 +772,27 @@ void nvim_buf_set_name(Buffer buffer, String name, Error *err)
}
}
-/// Checks if a buffer is valid
+/// Checks if a buffer is valid and loaded. See |api-buffer| for more info
+/// about unloaded buffers.
+///
+/// @param buffer Buffer handle
+/// @return true if the buffer is valid and loaded, false otherwise.
+Boolean nvim_buf_is_loaded(Buffer buffer)
+ FUNC_API_SINCE(5)
+{
+ Error stub = ERROR_INIT;
+ buf_T *buf = find_buffer_by_handle(buffer, &stub);
+ api_clear_error(&stub);
+ return buf && buf->b_ml.ml_mfp != NULL;
+}
+
+/// Checks if a buffer is valid.
+///
+/// @note Even if a buffer is valid it may have been unloaded. See |api-buffer|
+/// for more info about unloaded buffers.
///
/// @param buffer Buffer handle
-/// @return true if the buffer is valid, false otherwise
+/// @return true if the buffer is valid, false otherwise.
Boolean nvim_buf_is_valid(Buffer buffer)
FUNC_API_SINCE(1)
{
diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h
index 057f99e341..9ff62a80af 100644
--- a/src/nvim/buffer_defs.h
+++ b/src/nvim/buffer_defs.h
@@ -94,6 +94,7 @@ typedef struct {
typedef struct window_S win_T;
typedef struct wininfo_S wininfo_T;
typedef struct frame_S frame_T;
+typedef uint16_t disptick_T; // display tick type
// for struct memline (it needs memfile_T)
#include "nvim/memline_defs.h"
@@ -425,7 +426,7 @@ typedef struct {
synstate_T *b_sst_firstfree;
int b_sst_freecount;
linenr_T b_sst_check_lnum;
- uint16_t b_sst_lasttick; /* last display tick */
+ disptick_T b_sst_lasttick; // last display tick
// for spell checking
garray_T b_langp; // list of pointers to slang_T, see spell.c
diff --git a/src/nvim/diff.c b/src/nvim/diff.c
index 61e0b76558..8699c16351 100644
--- a/src/nvim/diff.c
+++ b/src/nvim/diff.c
@@ -48,9 +48,9 @@ static int diff_flags = DIFF_FILLER;
#define LBUFLEN 50 // length of line in diff file
-// TRUE when "diff -a" works, FALSE when it doesn't work, MAYBE when not
-// checked yet
-static int diff_a_works = MAYBE;
+// kTrue when "diff -a" works, kFalse when it doesn't work,
+// kNone when not checked yet
+static TriState diff_a_works = kNone;
#ifdef INCLUDE_GENERATED_DECLARATIONS
@@ -686,9 +686,9 @@ void ex_diffupdate(exarg_T *eap)
// there are differences.
// May try twice, first with "-a" and then without.
int io_error = false;
- bool ok = false;
+ TriState ok = kFalse;
for (;;) {
- ok = false;
+ ok = kFalse;
FILE *fd = mch_fopen(tmp_orig, "w");
if (fd == NULL) {
@@ -722,7 +722,7 @@ void ex_diffupdate(exarg_T *eap)
}
if (STRNCMP(linebuf, "1c1", 3) == 0) {
- ok = TRUE;
+ ok = kTrue;
}
}
fclose(fd);
@@ -739,7 +739,7 @@ void ex_diffupdate(exarg_T *eap)
}
// If we checked if "-a" works already, break here.
- if (diff_a_works != MAYBE) {
+ if (diff_a_works != kNone) {
break;
}
diff_a_works = ok;
@@ -755,7 +755,7 @@ void ex_diffupdate(exarg_T *eap)
EMSG(_("E810: Cannot read or write temp files"));
}
EMSG(_("E97: Cannot create diffs"));
- diff_a_works = MAYBE;
+ diff_a_works = kNone;
goto theend;
}
@@ -830,7 +830,7 @@ static void diff_file(const char *const tmp_orig, const char *const tmp_new,
* differences are of no use. Ignore errors, diff returns
* non-zero when differences have been found. */
vim_snprintf(cmd, len, "diff %s%s%s%s%s %s",
- diff_a_works ? "-a " : "",
+ diff_a_works == kFalse ? "" : "-a ",
"",
(diff_flags & DIFF_IWHITE) ? "-b " : "",
(diff_flags & DIFF_ICASE) ? "-i " : "",
@@ -1486,7 +1486,7 @@ int diff_check(win_T *wp, linenr_T lnum)
}
// A closed fold never has filler lines.
- if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL)) {
+ if (hasFoldingWin(wp, lnum, NULL, NULL, true, NULL)) {
return 0;
}
@@ -1793,7 +1793,7 @@ void diff_set_topline(win_T *fromwin, win_T *towin)
check_topfill(towin, false);
(void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
- NULL, TRUE, NULL);
+ NULL, true, NULL);
}
/// This is called when 'diffopt' is changed.
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index bf80f12bd0..cdccb57eee 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -242,8 +242,8 @@ static int ins_need_undo; /* call u_save() before inserting a
static int did_add_space = FALSE; /* auto_format() added an extra space
under the cursor */
-static int dont_sync_undo = false; // CTRL-G U prevents syncing undo
- // for the next left/right cursor
+static TriState dont_sync_undo = kFalse; // CTRL-G U prevents syncing undo
+ // for the next left/right cursor
static linenr_T o_lnum = 0;
@@ -595,10 +595,10 @@ static int insert_check(VimState *state)
s->lastc = s->c; // remember previous char for CTRL-D
// After using CTRL-G U the next cursor key will not break undo.
- if (dont_sync_undo == MAYBE) {
- dont_sync_undo = true;
+ if (dont_sync_undo == kNone) {
+ dont_sync_undo = kTrue;
} else {
- dont_sync_undo = false;
+ dont_sync_undo = kFalse;
}
return 1;
@@ -997,7 +997,7 @@ static int insert_handle_key(InsertState *s)
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) {
ins_s_left();
} else {
- ins_left(dont_sync_undo == false);
+ ins_left(dont_sync_undo == kFalse);
}
break;
@@ -1010,7 +1010,7 @@ static int insert_handle_key(InsertState *s)
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) {
ins_s_right();
} else {
- ins_right(dont_sync_undo == false);
+ ins_right(dont_sync_undo == kFalse);
}
break;
@@ -7174,7 +7174,7 @@ static void ins_ctrl_g(void)
case 'U':
// Allow one left/right cursor movement with the next char,
// without breaking undo.
- dont_sync_undo = MAYBE;
+ dont_sync_undo = kNone;
break;
/* Unknown CTRL-G command, reserved for future expansion. */
@@ -7954,7 +7954,7 @@ static void ins_left(bool end_change)
} else {
vim_beep(BO_CRSR);
}
- dont_sync_undo = false;
+ dont_sync_undo = kFalse;
}
static void ins_home(int c)
@@ -8039,7 +8039,7 @@ static void ins_right(bool end_change)
} else {
vim_beep(BO_CRSR);
}
- dont_sync_undo = false;
+ dont_sync_undo = kFalse;
}
static void ins_s_right(void)
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 176df58fb9..4ef51b72b7 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -4913,11 +4913,7 @@ void fix_help_buffer(void)
{
linenr_T lnum;
char_u *line;
- int in_example = FALSE;
- int len;
- char_u *fname;
- char_u *p;
- char_u *rt;
+ bool in_example = false;
// Set filetype to "help".
if (STRCMP(curbuf->b_p_ft, "help") != 0) {
@@ -4927,9 +4923,9 @@ void fix_help_buffer(void)
}
if (!syntax_present(curwin)) {
- for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) {
- line = ml_get_buf(curbuf, lnum, FALSE);
- len = (int)STRLEN(line);
+ for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; lnum++) {
+ line = ml_get_buf(curbuf, lnum, false);
+ const size_t len = STRLEN(line);
if (in_example && len > 0 && !ascii_iswhite(line[0])) {
/* End of example: non-white or '<' in first column. */
if (line[0] == '<') {
@@ -4937,14 +4933,14 @@ void fix_help_buffer(void)
line = ml_get_buf(curbuf, lnum, TRUE);
line[0] = ' ';
}
- in_example = FALSE;
+ in_example = false;
}
if (!in_example && len > 0) {
if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) {
/* blank-out a '>' in the last column (start of example) */
line = ml_get_buf(curbuf, lnum, TRUE);
line[len - 1] = ' ';
- in_example = TRUE;
+ in_example = true;
} else if (line[len - 1] == '~') {
/* blank-out a '~' at the end of line (header marker) */
line = ml_get_buf(curbuf, lnum, TRUE);
@@ -4958,7 +4954,7 @@ void fix_help_buffer(void)
* In the "help.txt" and "help.abx" file, add the locally added help
* files. This uses the very first line in the help file.
*/
- fname = path_tail(curbuf->b_fname);
+ char_u *const fname = path_tail(curbuf->b_fname);
if (fnamecmp(fname, "help.txt") == 0
|| (fnamencmp(fname, "help.", 5) == 0
&& ASCII_ISALPHA(fname[5])
@@ -4973,17 +4969,15 @@ void fix_help_buffer(void)
/* Go through all directories in 'runtimepath', skipping
* $VIMRUNTIME. */
- p = p_rtp;
+ char_u *p = p_rtp;
while (*p != NUL) {
copy_option_part(&p, NameBuff, MAXPATHL, ",");
- rt = (char_u *)vim_getenv("VIMRUNTIME");
+ char_u *const rt = (char_u *)vim_getenv("VIMRUNTIME");
if (rt != NULL
&& path_full_compare(rt, NameBuff, false) != kEqualFiles) {
int fcount;
char_u **fnames;
- FILE *fd;
char_u *s;
- int fi;
vimconv_T vc;
char_u *cp;
@@ -5001,29 +4995,24 @@ void fix_help_buffer(void)
if (gen_expand_wildcards(1, buff_list, &fcount,
&fnames, EW_FILE|EW_SILENT) == OK
&& fcount > 0) {
- int i1;
- int i2;
- char_u *f1;
- char_u *f2;
- char_u *t1;
- char_u *e1;
- char_u *e2;
-
- /* If foo.abx is found use it instead of foo.txt in
- * the same directory. */
- for (i1 = 0; i1 < fcount; ++i1) {
- for (i2 = 0; i2 < fcount; ++i2) {
- if (i1 == i2)
+ // If foo.abx is found use it instead of foo.txt in
+ // the same directory.
+ for (int i1 = 0; i1 < fcount; i1++) {
+ for (int i2 = 0; i2 < fcount; i2++) {
+ if (i1 == i2) {
continue;
- if (fnames[i1] == NULL || fnames[i2] == NULL)
+ }
+ if (fnames[i1] == NULL || fnames[i2] == NULL) {
continue;
- f1 = fnames[i1];
- f2 = fnames[i2];
- t1 = path_tail(f1);
- if (fnamencmp(f1, f2, t1 - f1) != 0)
+ }
+ const char_u *const f1 = fnames[i1];
+ const char_u *const f2 = fnames[i2];
+ const char_u *const t1 = path_tail(f1);
+ if (fnamencmp(f1, f2, t1 - f1) != 0) {
continue;
- e1 = vim_strrchr(t1, '.');
- e2 = vim_strrchr(path_tail(f2), '.');
+ }
+ const char_u *const e1 = vim_strrchr(t1, '.');
+ const char_u *const e2 = vim_strrchr(path_tail(f2), '.');
if (e1 == NULL || e2 == NULL) {
continue;
}
@@ -5044,10 +5033,12 @@ void fix_help_buffer(void)
}
}
}
- for (fi = 0; fi < fcount; ++fi) {
- if (fnames[fi] == NULL)
+ for (int fi = 0; fi < fcount; fi++) {
+ if (fnames[fi] == NULL) {
continue;
- fd = mch_fopen((char *)fnames[fi], "r");
+ }
+
+ FILE *const fd = mch_fopen((char *)fnames[fi], "r");
if (fd == NULL) {
continue;
}
@@ -5055,9 +5046,9 @@ void fix_help_buffer(void)
if (IObuff[0] == '*'
&& (s = vim_strchr(IObuff + 1, '*'))
!= NULL) {
- int this_utf = MAYBE;
- /* Change tag definition to a
- * reference and remove <CR>/<NL>. */
+ TriState this_utf = kNone;
+ // Change tag definition to a
+ // reference and remove <CR>/<NL>.
IObuff[0] = '|';
*s = '|';
while (*s != NUL) {
@@ -5067,13 +5058,12 @@ void fix_help_buffer(void)
* above 127 is found and no
* illegal byte sequence is found.
*/
- if (*s >= 0x80 && this_utf != FALSE) {
- int l;
-
- this_utf = TRUE;
- l = utf_ptr2len(s);
- if (l == 1)
- this_utf = FALSE;
+ if (*s >= 0x80 && this_utf != kFalse) {
+ this_utf = kTrue;
+ const int l = utf_ptr2len(s);
+ if (l == 1) {
+ this_utf = kFalse;
+ }
s += l - 1;
}
++s;
@@ -5082,18 +5072,20 @@ void fix_help_buffer(void)
* conversion to the current
* 'encoding' may be required. */
vc.vc_type = CONV_NONE;
- convert_setup(&vc, (char_u *)(
- this_utf == TRUE ? "utf-8"
- : "latin1"), p_enc);
- if (vc.vc_type == CONV_NONE)
- /* No conversion needed. */
+ convert_setup(
+ &vc,
+ (char_u *)(this_utf == kTrue ? "utf-8" : "latin1"),
+ p_enc);
+ if (vc.vc_type == CONV_NONE) {
+ // No conversion needed.
cp = IObuff;
- else {
- /* Do the conversion. If it fails
- * use the unconverted text. */
+ } else {
+ // Do the conversion. If it fails
+ // use the unconverted text.
cp = string_convert(&vc, IObuff, NULL);
- if (cp == NULL)
+ if (cp == NULL) {
cp = IObuff;
+ }
}
convert_setup(&vc, NULL, NULL);
@@ -5138,22 +5130,16 @@ void ex_viusage(exarg_T *eap)
/// @param tagname Name of the tags file ("tags" for English, "tags-fr" for
/// French)
/// @param add_help_tags Whether to add the "help-tags" tag
-static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
- bool add_help_tags)
+static void helptags_one(char_u *const dir, const char_u *const ext,
+ const char_u *const tagfname, const bool add_help_tags)
{
- FILE *fd_tags;
- FILE *fd;
garray_T ga;
int filecount;
char_u **files;
char_u *p1, *p2;
- int fi;
char_u *s;
- char_u *fname;
- int utf8 = MAYBE;
- int this_utf8;
- int firstline;
- int mix = FALSE; /* detected mixed encodings */
+ TriState utf8 = kNone;
+ bool mix = false; // detected mixed encodings
// Find all *.txt files.
size_t dirlen = STRLCPY(NameBuff, dir, sizeof(NameBuff));
@@ -5186,7 +5172,8 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
EMSG(_(e_fnametoolong));
return;
}
- fd_tags = mch_fopen((char *)NameBuff, "w");
+
+ FILE *const fd_tags = mch_fopen((char *)NameBuff, "w");
if (fd_tags == NULL) {
EMSG2(_("E152: Cannot open %s for writing"), NameBuff);
FreeWild(filecount, files);
@@ -5209,44 +5196,44 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
/*
* Go over all the files and extract the tags.
*/
- for (fi = 0; fi < filecount && !got_int; ++fi) {
- fd = mch_fopen((char *)files[fi], "r");
+ for (int fi = 0; fi < filecount && !got_int; fi++) {
+ FILE *const fd = mch_fopen((char *)files[fi], "r");
if (fd == NULL) {
EMSG2(_("E153: Unable to open %s for reading"), files[fi]);
continue;
}
- fname = files[fi] + dirlen + 1;
+ const char_u *const fname = files[fi] + dirlen + 1;
- firstline = TRUE;
+ bool firstline = true;
while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) {
if (firstline) {
- /* Detect utf-8 file by a non-ASCII char in the first line. */
- this_utf8 = MAYBE;
- for (s = IObuff; *s != NUL; ++s)
+ // Detect utf-8 file by a non-ASCII char in the first line.
+ TriState this_utf8 = kNone;
+ for (s = IObuff; *s != NUL; s++) {
if (*s >= 0x80) {
- int l;
-
- this_utf8 = TRUE;
- l = utf_ptr2len(s);
+ this_utf8 = kTrue;
+ const int l = utf_ptr2len(s);
if (l == 1) {
- /* Illegal UTF-8 byte sequence. */
- this_utf8 = FALSE;
+ // Illegal UTF-8 byte sequence.
+ this_utf8 = kFalse;
break;
}
s += l - 1;
}
- if (this_utf8 == MAYBE) /* only ASCII characters found */
- this_utf8 = FALSE;
- if (utf8 == MAYBE) /* first file */
+ }
+ if (this_utf8 == kNone) { // only ASCII characters found
+ this_utf8 = kFalse;
+ }
+ if (utf8 == kNone) { // first file
utf8 = this_utf8;
- else if (utf8 != this_utf8) {
+ } else if (utf8 != this_utf8) {
EMSG2(_(
"E670: Mix of help file encodings within a language: %s"),
files[fi]);
mix = !got_int;
got_int = TRUE;
}
- firstline = FALSE;
+ firstline = false;
}
p1 = vim_strchr(IObuff, '*'); /* find first '*' */
while (p1 != NULL) {
@@ -5316,8 +5303,9 @@ static void helptags_one(char_u *dir, char_u *ext, char_u *tagfname,
}
}
- if (utf8 == TRUE)
+ if (utf8 == kTrue) {
fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n");
+ }
/*
* Write the tags into the file.
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index 97369c50d9..0ab0178d24 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -7395,7 +7395,7 @@ static void ex_operators(exarg_T *eap)
oa.end.lnum = eap->line2;
oa.line_count = eap->line2 - eap->line1 + 1;
oa.motion_type = kMTLineWise;
- virtual_op = false;
+ virtual_op = kFalse;
if (eap->cmdidx != CMD_yank) { // position cursor for undo
setpcmark();
curwin->w_cursor.lnum = eap->line1;
@@ -7426,7 +7426,7 @@ static void ex_operators(exarg_T *eap)
op_shift(&oa, FALSE, eap->amount);
break;
}
- virtual_op = MAYBE;
+ virtual_op = kNone;
ex_may_print(eap);
}
diff --git a/src/nvim/fold.c b/src/nvim/fold.c
index ef8750d061..1ed34ef124 100644
--- a/src/nvim/fold.c
+++ b/src/nvim/fold.c
@@ -44,14 +44,14 @@
* The info stored in both growarrays is the same: An array of fold_T.
*/
typedef struct {
- linenr_T fd_top; /* first line of fold; for nested fold
- * relative to parent */
- linenr_T fd_len; /* number of lines in the fold */
- garray_T fd_nested; /* array of nested folds */
- char fd_flags; /* see below */
- char fd_small; /* TRUE, FALSE or MAYBE: fold smaller than
- 'foldminlines'; MAYBE applies to nested
- folds too */
+ linenr_T fd_top; // first line of fold; for nested fold
+ // relative to parent
+ linenr_T fd_len; // number of lines in the fold
+ garray_T fd_nested; // array of nested folds
+ char fd_flags; // see below
+ TriState fd_small; // kTrue, kFalse, or kNone: fold smaller than
+ // 'foldminlines'; kNone applies to nested
+ // folds too
} fold_T;
#define FD_OPEN 0 /* fold is open (nested ones can be closed) */
@@ -76,8 +76,8 @@ typedef struct {
this line (copy of "end" of prev. line) */
} fline_T;
-/* Flag is set when redrawing is needed. */
-static int fold_changed;
+// Flag is set when redrawing is needed.
+static bool fold_changed;
/* Function used by foldUpdateIEMSRecurse */
typedef void (*LevelGetter)(fline_T *);
@@ -147,29 +147,27 @@ int hasAnyFolding(win_T *win)
*/
bool hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp)
{
- return hasFoldingWin(curwin, lnum, firstp, lastp, TRUE, NULL);
+ return hasFoldingWin(curwin, lnum, firstp, lastp, true, NULL);
}
/* hasFoldingWin() {{{2 */
bool hasFoldingWin(
- win_T *win,
- linenr_T lnum,
- linenr_T *firstp,
- linenr_T *lastp,
- int cache, /* when TRUE: use cached values of window */
- foldinfo_T *infop /* where to store fold info */
+ win_T *const win,
+ const linenr_T lnum,
+ linenr_T *const firstp,
+ linenr_T *const lastp,
+ const bool cache, // when true: use cached values of window
+ foldinfo_T *const infop // where to store fold info
)
{
- int had_folded = FALSE;
+ bool had_folded = false;
linenr_T first = 0;
linenr_T last = 0;
linenr_T lnum_rel = lnum;
- int x;
fold_T *fp;
int level = 0;
- int use_level = FALSE;
- int maybe_small = FALSE;
- garray_T *gap;
+ bool use_level = false;
+ bool maybe_small = false;
int low_level = 0;
checkupdate(win);
@@ -187,7 +185,7 @@ bool hasFoldingWin(
* First look in cached info for displayed lines. This is probably
* the fastest, but it can only be used if the entry is still valid.
*/
- x = find_wl_entry(win, lnum);
+ const int x = find_wl_entry(win, lnum);
if (x >= 0) {
first = win->w_lines[x].wl_lnum;
last = win->w_lines[x].wl_lastlnum;
@@ -199,7 +197,7 @@ bool hasFoldingWin(
/*
* Recursively search for a fold that contains "lnum".
*/
- gap = &win->w_folds;
+ garray_T *gap = &win->w_folds;
for (;; ) {
if (!foldFind(gap, lnum_rel, &fp))
break;
@@ -274,14 +272,11 @@ int foldLevel(linenr_T lnum)
return foldLevelWin(curwin, lnum);
}
-/* lineFolded() {{{2 */
-/*
- * Low level function to check if a line is folded. Doesn't use any caching.
- * Return TRUE if line is folded.
- * Return FALSE if line is not folded.
- * Return MAYBE if the line is folded when next to a folded line.
- */
-int lineFolded(win_T *win, linenr_T lnum)
+// lineFolded() {{{2
+// Low level function to check if a line is folded. Doesn't use any caching.
+// Return true if line is folded.
+// Return false if line is not folded.
+bool lineFolded(win_T *const win, const linenr_T lnum)
{
return foldedCount(win, lnum, NULL) != 0;
}
@@ -299,8 +294,9 @@ long foldedCount(win_T *win, linenr_T lnum, foldinfo_T *infop)
{
linenr_T last;
- if (hasFoldingWin(win, lnum, NULL, &last, FALSE, infop))
+ if (hasFoldingWin(win, lnum, NULL, &last, false, infop)) {
return (long)(last - lnum + 1);
+ }
return 0;
}
@@ -649,7 +645,7 @@ void foldCreate(linenr_T start, linenr_T end)
if (!use_level)
curwin->w_fold_manual = true;
fp->fd_flags = FD_CLOSED;
- fp->fd_small = MAYBE;
+ fp->fd_small = kNone;
/* redraw */
changed_window_setting();
@@ -663,36 +659,31 @@ void foldCreate(linenr_T start, linenr_T end)
* When "end" is not 0, delete all folds from "start" to "end".
* When "recursive" is TRUE delete recursively.
*/
-void
-deleteFold(
- linenr_T start,
- linenr_T end,
- int recursive,
- int had_visual // TRUE when Visual selection used
+void deleteFold(
+ const linenr_T start,
+ const linenr_T end,
+ const int recursive,
+ const bool had_visual // true when Visual selection used
)
{
- garray_T *gap;
fold_T *fp;
- garray_T *found_ga;
fold_T *found_fp = NULL;
linenr_T found_off = 0;
- int use_level;
- int maybe_small = FALSE;
+ bool maybe_small = false;
int level = 0;
linenr_T lnum = start;
- linenr_T lnum_off;
- int did_one = FALSE;
+ bool did_one = false;
linenr_T first_lnum = MAXLNUM;
linenr_T last_lnum = 0;
checkupdate(curwin);
while (lnum <= end) {
- /* Find the deepest fold for "start". */
- gap = &curwin->w_folds;
- found_ga = NULL;
- lnum_off = 0;
- use_level = FALSE;
+ // Find the deepest fold for "start".
+ garray_T *gap = &curwin->w_folds;
+ garray_T *found_ga = NULL;
+ linenr_T lnum_off = 0;
+ bool use_level = false;
for (;; ) {
if (!foldFind(gap, lnum - lnum_off, &fp))
break;
@@ -728,7 +719,7 @@ deleteFold(
parseMarker(curwin);
deleteFoldMarkers(found_fp, recursive, found_off);
}
- did_one = TRUE;
+ did_one = true;
/* redraw window */
changed_window_setting();
@@ -787,8 +778,8 @@ void foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
(void)foldFind(&wp->w_folds, top, &fp);
while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len
&& fp->fd_top < bot) {
- fp->fd_small = MAYBE;
- ++fp;
+ fp->fd_small = kNone;
+ fp++;
}
if (foldmethodIsIndent(wp)
@@ -831,44 +822,34 @@ void foldUpdateAll(win_T *win)
redraw_win_later(win, NOT_VALID);
}
-/* foldMoveTo() {{{2 */
-/*
- * If "updown" is FALSE: Move to the start or end of the fold.
- * If "updown" is TRUE: move to fold at the same level.
- * If not moved return FAIL.
- */
-int
-foldMoveTo(
- int updown,
- int dir, // FORWARD or BACKWARD
- long count
+// foldMoveTo() {{{2
+//
+// If "updown" is false: Move to the start or end of the fold.
+// If "updown" is true: move to fold at the same level.
+// If not moved return FAIL.
+int foldMoveTo(
+ const bool updown,
+ const int dir, // FORWARD or BACKWARD
+ const long count
)
{
- long n;
int retval = FAIL;
- linenr_T lnum_off;
- linenr_T lnum_found;
linenr_T lnum;
- int use_level;
- int maybe_small;
- garray_T *gap;
fold_T *fp;
- int level;
- int last;
checkupdate(curwin);
- /* Repeat "count" times. */
- for (n = 0; n < count; ++n) {
- /* Find nested folds. Stop when a fold is closed. The deepest fold
- * that moves the cursor is used. */
- lnum_off = 0;
- gap = &curwin->w_folds;
- use_level = FALSE;
- maybe_small = FALSE;
- lnum_found = curwin->w_cursor.lnum;
- level = 0;
- last = FALSE;
+ // Repeat "count" times.
+ for (long n = 0; n < count; n++) {
+ // Find nested folds. Stop when a fold is closed. The deepest fold
+ // that moves the cursor is used.
+ linenr_T lnum_off = 0;
+ garray_T *gap = &curwin->w_folds;
+ bool use_level = false;
+ bool maybe_small = false;
+ linenr_T lnum_found = curwin->w_cursor.lnum;
+ int level = 0;
+ bool last = false;
for (;; ) {
if (!foldFind(gap, curwin->w_cursor.lnum - lnum_off, &fp)) {
if (!updown)
@@ -886,14 +867,15 @@ foldMoveTo(
}
/* don't look for contained folds, they will always move
* the cursor too far. */
- last = TRUE;
+ last = true;
}
if (!last) {
/* Check if this fold is closed. */
if (check_closed(curwin, fp, &use_level, level,
- &maybe_small, lnum_off))
- last = TRUE;
+ &maybe_small, lnum_off)) {
+ last = true;
+ }
/* "[z" and "]z" stop at closed fold */
if (last && !updown)
@@ -1306,25 +1288,21 @@ static void foldOpenNested(fold_T *fpr)
}
}
-/* deleteFoldEntry() {{{2 */
-/*
- * Delete fold "idx" from growarray "gap".
- * When "recursive" is TRUE also delete all the folds contained in it.
- * When "recursive" is FALSE contained folds are moved one level up.
- */
-static void deleteFoldEntry(garray_T *gap, int idx, int recursive)
+// deleteFoldEntry() {{{2
+// Delete fold "idx" from growarray "gap".
+// When "recursive" is true also delete all the folds contained in it.
+// When "recursive" is false contained folds are moved one level up.
+static void deleteFoldEntry(garray_T *const gap, const int idx,
+ const bool recursive)
{
- fold_T *fp;
- int i;
- fold_T *nfp;
-
- fp = (fold_T *)gap->ga_data + idx;
+ fold_T *fp = (fold_T *)gap->ga_data + idx;
if (recursive || GA_EMPTY(&fp->fd_nested)) {
- /* recursively delete the contained folds */
+ // recursively delete the contained folds
deleteFoldRecurse(&fp->fd_nested);
- --gap->ga_len;
- if (idx < gap->ga_len)
- memmove(fp, fp + 1, sizeof(fold_T) * (size_t)(gap->ga_len - idx));
+ gap->ga_len--;
+ if (idx < gap->ga_len) {
+ memmove(fp, fp + 1, sizeof(*fp) * (size_t)(gap->ga_len - idx));
+ }
} else {
/* Move nested folds one level up, to overwrite the fold that is
* deleted. */
@@ -1334,22 +1312,24 @@ static void deleteFoldEntry(garray_T *gap, int idx, int recursive)
/* Get "fp" again, the array may have been reallocated. */
fp = (fold_T *)gap->ga_data + idx;
- /* adjust fd_top and fd_flags for the moved folds */
- nfp = (fold_T *)fp->fd_nested.ga_data;
- for (i = 0; i < moved; ++i) {
+ // adjust fd_top and fd_flags for the moved folds
+ fold_T *nfp = (fold_T *)fp->fd_nested.ga_data;
+ for (int i = 0; i < moved; i++) {
nfp[i].fd_top += fp->fd_top;
if (fp->fd_flags == FD_LEVEL)
nfp[i].fd_flags = FD_LEVEL;
- if (fp->fd_small == MAYBE)
- nfp[i].fd_small = MAYBE;
+ if (fp->fd_small == kNone) {
+ nfp[i].fd_small = kNone;
+ }
}
- /* move the existing folds down to make room */
- if (idx + 1 < gap->ga_len)
+ // move the existing folds down to make room
+ if (idx + 1 < gap->ga_len) {
memmove(fp + moved, fp + 1,
- sizeof(fold_T) * (size_t)(gap->ga_len - (idx + 1)));
- /* move the contained folds one level up */
- memmove(fp, nfp, sizeof(fold_T) * (size_t)moved);
+ sizeof(*fp) * (size_t)(gap->ga_len - (idx + 1)));
+ }
+ // move the contained folds one level up
+ memmove(fp, nfp, sizeof(*fp) * (size_t)moved);
xfree(nfp);
gap->ga_len += moved - 1;
}
@@ -1429,14 +1409,15 @@ static void foldMarkAdjustRecurse(garray_T *gap, linenr_T line1, linenr_T line2,
fp->fd_top += amount_after;
} else {
if (fp->fd_top >= top && last <= line2) {
- /* 4. fold completely contained in range */
+ // 4. fold completely contained in range
if (amount == MAXLNUM) {
- /* Deleting lines: delete the fold completely */
- deleteFoldEntry(gap, i, TRUE);
- --i; /* adjust index for deletion */
- --fp;
- } else
+ // Deleting lines: delete the fold completely
+ deleteFoldEntry(gap, i, true);
+ i--; // adjust index for deletion
+ fp--;
+ } else {
fp->fd_top += amount;
+ }
} else {
if (fp->fd_top < top) {
/* 2 or 3: need to correct nested folds too */
@@ -1505,36 +1486,40 @@ static int getDeepestNestingRecurse(garray_T *gap)
/*
* Check if a fold is closed and update the info needed to check nested folds.
*/
-static int
-check_closed(
- win_T *win,
- fold_T *fp,
- int *use_levelp, // TRUE: outer fold had FD_LEVEL
- int level, // folding depth
- int *maybe_smallp, // TRUE: outer this had fd_small == MAYBE
- linenr_T lnum_off // line number offset for fp->fd_top
+static bool check_closed(
+ win_T *const win,
+ fold_T *const fp,
+ bool *const use_levelp, // true: outer fold had FD_LEVEL
+ const int level, // folding depth
+ bool *const maybe_smallp, // true: outer this had fd_small == kNone
+ const linenr_T lnum_off // line number offset for fp->fd_top
)
{
- int closed = FALSE;
+ bool closed = false;
/* Check if this fold is closed. If the flag is FD_LEVEL this
* fold and all folds it contains depend on 'foldlevel'. */
if (*use_levelp || fp->fd_flags == FD_LEVEL) {
- *use_levelp = TRUE;
- if (level >= win->w_p_fdl)
- closed = TRUE;
- } else if (fp->fd_flags == FD_CLOSED)
- closed = TRUE;
-
- /* Small fold isn't closed anyway. */
- if (fp->fd_small == MAYBE)
- *maybe_smallp = TRUE;
+ *use_levelp = true;
+ if (level >= win->w_p_fdl) {
+ closed = true;
+ }
+ } else if (fp->fd_flags == FD_CLOSED) {
+ closed = true;
+ }
+
+ // Small fold isn't closed anyway.
+ if (fp->fd_small == kNone) {
+ *maybe_smallp = true;
+ }
if (closed) {
- if (*maybe_smallp)
- fp->fd_small = MAYBE;
+ if (*maybe_smallp) {
+ fp->fd_small = kNone;
+ }
checkSmall(win, fp, lnum_off);
- if (fp->fd_small == TRUE)
- closed = FALSE;
+ if (fp->fd_small == kTrue) {
+ closed = false;
+ }
}
return closed;
}
@@ -1545,43 +1530,38 @@ check_closed(
*/
static void
checkSmall(
- win_T *wp,
- fold_T *fp,
- linenr_T lnum_off // offset for fp->fd_top
+ win_T *const wp,
+ fold_T *const fp,
+ const linenr_T lnum_off // offset for fp->fd_top
)
{
- int count;
- int n;
-
- if (fp->fd_small == MAYBE) {
- /* Mark any nested folds to maybe-small */
+ if (fp->fd_small == kNone) {
+ // Mark any nested folds to maybe-small
setSmallMaybe(&fp->fd_nested);
- if (fp->fd_len > curwin->w_p_fml)
- fp->fd_small = FALSE;
- else {
- count = 0;
- for (n = 0; n < fp->fd_len; ++n) {
+ if (fp->fd_len > curwin->w_p_fml) {
+ fp->fd_small = kFalse;
+ } else {
+ int count = 0;
+ for (int n = 0; n < fp->fd_len; n++) {
count += plines_win_nofold(wp, fp->fd_top + lnum_off + n);
if (count > curwin->w_p_fml) {
- fp->fd_small = FALSE;
+ fp->fd_small = kFalse;
return;
}
}
- fp->fd_small = TRUE;
+ fp->fd_small = kTrue;
}
}
}
-/* setSmallMaybe() {{{2 */
-/*
- * Set small flags in "gap" to MAYBE.
- */
+// setSmallMaybe() {{{2
+// Set small flags in "gap" to kNone.
static void setSmallMaybe(garray_T *gap)
{
fold_T *fp = (fold_T *)gap->ga_data;
- for (int i = 0; i < gap->ga_len; ++i) {
- fp[i].fd_small = MAYBE;
+ for (int i = 0; i < gap->ga_len; i++) {
+ fp[i].fd_small = kNone;
}
}
@@ -1896,13 +1876,10 @@ void foldtext_cleanup(char_u *str)
* Update the folding for window "wp", at least from lines "top" to "bot".
* Return TRUE if any folds did change.
*/
-static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
+static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
{
- linenr_T start;
- linenr_T end;
fline_T fline;
void (*getlevel)(fline_T *);
- int level;
fold_T *fp;
/* Avoid problems when being called recursively. */
@@ -1928,12 +1905,13 @@ static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
bot += diff_context;
}
- /* When deleting lines at the end of the buffer "top" can be past the end
- * of the buffer. */
- if (top > wp->w_buffer->b_ml.ml_line_count)
+ // When deleting lines at the end of the buffer "top" can be past the end
+ // of the buffer.
+ if (top > wp->w_buffer->b_ml.ml_line_count) {
top = wp->w_buffer->b_ml.ml_line_count;
+ }
- fold_changed = FALSE;
+ fold_changed = false;
fline.wp = wp;
fline.off = 0;
fline.lvl = 0;
@@ -1954,8 +1932,8 @@ static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
/* Need to get the level of the line above top, it is used if there is
* no marker at the top. */
if (top > 1) {
- /* Get the fold level at top - 1. */
- level = foldLevelWin(wp, top - 1);
+ // Get the fold level at top - 1.
+ const int level = foldLevelWin(wp, top - 1);
/* The fold may end just above the top, check for that. */
fline.lnum = top - 1;
@@ -2031,11 +2009,12 @@ static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
}
}
- start = fline.lnum;
- end = bot;
- /* Do at least one line. */
- if (start > end && end < wp->w_buffer->b_ml.ml_line_count)
+ linenr_T start = fline.lnum;
+ linenr_T end = bot;
+ // Do at least one line.
+ if (start > end && end < wp->w_buffer->b_ml.ml_line_count) {
end = start;
+ }
while (!got_int) {
/* Always stop at the end of the file ("end" can be past the end of
* the file). */
@@ -2126,22 +2105,21 @@ static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
* Returns bot, which may have been increased for lines that also need to be
* updated as a result of a detected change in the fold.
*/
-static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
- linenr_T startlnum, fline_T *flp,
- LevelGetter getlevel,
- linenr_T bot,
- char topflags /* containing fold flags */
- )
+static linenr_T foldUpdateIEMSRecurse(
+ garray_T *const gap, const int level, const linenr_T startlnum,
+ fline_T *const flp, LevelGetter getlevel, linenr_T bot,
+ const char topflags // containing fold flags
+)
{
linenr_T ll;
fold_T *fp = NULL;
fold_T *fp2;
int lvl = level;
linenr_T startlnum2 = startlnum;
- linenr_T firstlnum = flp->lnum; /* first lnum we got */
+ const linenr_T firstlnum = flp->lnum; // first lnum we got
int i;
- int finish = FALSE;
- linenr_T linecount = flp->wp->w_buffer->b_ml.ml_line_count - flp->off;
+ bool finish = false;
+ const linenr_T linecount = flp->wp->w_buffer->b_ml.ml_line_count - flp->off;
int concat;
/*
@@ -2309,8 +2287,9 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
* found. */
if (getlevel == foldlevelMarker
|| getlevel == foldlevelExpr
- || getlevel == foldlevelSyntax)
- finish = TRUE;
+ || getlevel == foldlevelSyntax) {
+ finish = true;
+ }
}
if (fp->fd_top == startlnum && concat) {
i = (int)(fp - (fold_T *)gap->ga_data);
@@ -2325,19 +2304,18 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
break;
}
if (fp->fd_top >= startlnum) {
- /* A fold that starts at or after startlnum and stops
- * before the new fold must be deleted. Continue
- * looking for the next one. */
- deleteFoldEntry(gap,
- (int)(fp - (fold_T *)gap->ga_data), TRUE);
+ // A fold that starts at or after startlnum and stops
+ // before the new fold must be deleted. Continue
+ // looking for the next one.
+ deleteFoldEntry(gap, (int)(fp - (fold_T *)gap->ga_data), true);
} else {
/* A fold has some lines above startlnum, truncate it
* to stop just above startlnum. */
fp->fd_len = startlnum - fp->fd_top;
foldMarkAdjustRecurse(&fp->fd_nested,
- (linenr_T)fp->fd_len, (linenr_T)MAXLNUM,
- (linenr_T)MAXLNUM, 0L);
- fold_changed = TRUE;
+ (linenr_T)fp->fd_len, (linenr_T)MAXLNUM,
+ (linenr_T)MAXLNUM, 0L);
+ fold_changed = true;
}
} else {
/* Insert new fold. Careful: ga_data may be NULL and it
@@ -2361,14 +2339,15 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
flp->wp->w_fold_manual = true;
} else
fp->fd_flags = (fp - 1)->fd_flags;
- fp->fd_small = MAYBE;
- /* If using the "marker", "expr" or "syntax" method, we
- * need to continue until the end of the fold is found. */
+ fp->fd_small = kNone;
+ // If using the "marker", "expr" or "syntax" method, we
+ // need to continue until the end of the fold is found.
if (getlevel == foldlevelMarker
|| getlevel == foldlevelExpr
- || getlevel == foldlevelSyntax)
- finish = TRUE;
- fold_changed = TRUE;
+ || getlevel == foldlevelSyntax) {
+ finish = true;
+ }
+ fold_changed = true;
break;
}
}
@@ -2387,12 +2366,11 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
* Check "fp" for safety.
*/
if (lvl > level && fp != NULL) {
- /*
- * There is a nested fold, handle it recursively.
- */
- /* At least do one line (can happen when finish is TRUE). */
- if (bot < flp->lnum)
+ // There is a nested fold, handle it recursively.
+ // At least do one line (can happen when finish is true).
+ if (bot < flp->lnum) {
bot = flp->lnum;
+ }
/* Line numbers in the nested fold are relative to the start of
* this fold. */
@@ -2454,8 +2432,8 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
/* Current fold at least extends until lnum. */
if (fp->fd_len < flp->lnum - fp->fd_top) {
fp->fd_len = flp->lnum - fp->fd_top;
- fp->fd_small = MAYBE;
- fold_changed = TRUE;
+ fp->fd_small = kNone;
+ fold_changed = true;
}
/* Delete contained folds from the end of the last one found until where
@@ -2482,8 +2460,9 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
foldSplit(gap, i, flp->lnum, bot);
fp = (fold_T *)gap->ga_data + i;
}
- } else
+ } else {
fp->fd_len = flp->lnum - fp->fd_top;
+ }
fold_changed = true;
}
}
@@ -2502,7 +2481,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
(linenr_T)MAXLNUM, (long)(fp2->fd_top - flp->lnum));
fp2->fd_len -= flp->lnum - fp2->fd_top;
fp2->fd_top = flp->lnum;
- fold_changed = TRUE;
+ fold_changed = true;
}
if (lvl >= level) {
@@ -2511,8 +2490,8 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level,
}
break;
}
- fold_changed = TRUE;
- deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), TRUE);
+ fold_changed = true;
+ deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), true);
}
/* Need to redraw the lines we inspected, which might be further down than
@@ -2548,36 +2527,32 @@ static void foldInsert(garray_T *gap, int i)
* The caller must first have taken care of any nested folds from "top" to
* "bot"!
*/
-static void foldSplit(garray_T *gap, int i, linenr_T top, linenr_T bot)
+static void foldSplit(garray_T *const gap, const int i, const linenr_T top,
+ const linenr_T bot)
{
- fold_T *fp;
fold_T *fp2;
- garray_T *gap1;
- garray_T *gap2;
- int idx;
- int len;
/* The fold continues below bot, need to split it. */
foldInsert(gap, i + 1);
- fp = (fold_T *)gap->ga_data + i;
+ fold_T *const fp = (fold_T *)gap->ga_data + i;
fp[1].fd_top = bot + 1;
// check for wrap around (MAXLNUM, and 32bit)
assert(fp[1].fd_top > bot);
fp[1].fd_len = fp->fd_len - (fp[1].fd_top - fp->fd_top);
fp[1].fd_flags = fp->fd_flags;
- fp[1].fd_small = MAYBE;
- fp->fd_small = MAYBE;
+ fp[1].fd_small = kNone;
+ fp->fd_small = kNone;
/* Move nested folds below bot to new fold. There can't be
* any between top and bot, they have been removed by the caller. */
- gap1 = &fp->fd_nested;
- gap2 = &fp[1].fd_nested;
+ garray_T *const gap1 = &fp->fd_nested;
+ garray_T *const gap2 = &fp[1].fd_nested;
(void)(foldFind(gap1, bot + 1 - fp->fd_top, &fp2));
- len = (int)((fold_T *)gap1->ga_data + gap1->ga_len - fp2);
+ const int len = (int)((fold_T *)gap1->ga_data + gap1->ga_len - fp2);
if (len > 0) {
ga_grow(gap2, len);
- for (idx = 0; idx < len; ++idx) {
+ for (int idx = 0; idx < len; idx++) {
((fold_T *)gap2->ga_data)[idx] = fp2[idx];
((fold_T *)gap2->ga_data)[idx].fd_top
-= fp[1].fd_top - fp->fd_top;
@@ -2586,7 +2561,7 @@ static void foldSplit(garray_T *gap, int i, linenr_T top, linenr_T bot)
gap1->ga_len -= len;
}
fp->fd_len = top - fp->fd_top;
- fold_changed = TRUE;
+ fold_changed = true;
}
/* foldRemove() {{{2 */
@@ -2848,8 +2823,8 @@ static void foldMerge(fold_T *fp1, garray_T *gap, fold_T *fp2)
}
fp1->fd_len += fp2->fd_len;
- deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), TRUE);
- fold_changed = TRUE;
+ deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), true);
+ fold_changed = true;
}
/* foldlevelIndent() {{{2 */
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
index d9103f516c..3efc0838aa 100644
--- a/src/nvim/globals.h
+++ b/src/nvim/globals.h
@@ -191,7 +191,7 @@ EXTERN int cmdline_star INIT(= FALSE); /* cmdline is crypted */
EXTERN int exec_from_reg INIT(= FALSE); /* executing register */
-EXTERN int screen_cleared INIT(= FALSE); /* screen has been cleared */
+EXTERN TriState screen_cleared INIT(= kFalse); // screen has been cleared
/*
* When '$' is included in 'cpoptions' option set:
@@ -952,9 +952,9 @@ EXTERN char psepcN INIT(= '/'); // abnormal path separator character
EXTERN char pseps[2] INIT(= { '\\', 0 }); // normal path separator string
#endif
-/* Set to TRUE when an operator is being executed with virtual editing, MAYBE
- * when no operator is being executed, FALSE otherwise. */
-EXTERN int virtual_op INIT(= MAYBE);
+// Set to kTrue when an operator is being executed with virtual editing
+// kNone when no operator is being executed, kFalse otherwise.
+EXTERN TriState virtual_op INIT(= kNone);
/* Display tick, incremented for each call to update_screen() */
EXTERN disptick_T display_tick INIT(= 0);
diff --git a/src/nvim/hardcopy.c b/src/nvim/hardcopy.c
index e1eb8f0251..a2f48f46aa 100644
--- a/src/nvim/hardcopy.c
+++ b/src/nvim/hardcopy.c
@@ -134,9 +134,9 @@ static int current_syn_id;
#define PRCOLOR_BLACK 0
#define PRCOLOR_WHITE 0xffffff
-static int curr_italic;
-static int curr_bold;
-static int curr_underline;
+static TriState curr_italic;
+static TriState curr_bold;
+static TriState curr_underline;
static uint32_t curr_bg;
static uint32_t curr_fg;
static int page_count;
@@ -417,7 +417,8 @@ static void prt_set_bg(uint32_t bg)
}
}
-static void prt_set_font(int bold, int italic, int underline)
+static void prt_set_font(const TriState bold, const TriState italic,
+ const TriState underline)
{
if (curr_bold != bold
|| curr_italic != italic
@@ -429,34 +430,32 @@ static void prt_set_font(int bold, int italic, int underline)
}
}
-/*
- * Print the line number in the left margin.
- */
-static void prt_line_number(prt_settings_T *psettings, int page_line, linenr_T lnum)
+// Print the line number in the left margin.
+static void prt_line_number(prt_settings_T *const psettings,
+ const int page_line, const linenr_T lnum)
{
- int i;
- char_u tbuf[20];
-
prt_set_fg(psettings->number.fg_color);
prt_set_bg(psettings->number.bg_color);
prt_set_font(psettings->number.bold, psettings->number.italic,
- psettings->number.underline);
- mch_print_start_line(TRUE, page_line);
+ psettings->number.underline);
+ mch_print_start_line(true, page_line);
- /* Leave two spaces between the number and the text; depends on
- * PRINT_NUMBER_WIDTH. */
- sprintf((char *)tbuf, "%6ld", (long)lnum);
- for (i = 0; i < 6; i++)
+ // Leave two spaces between the number and the text; depends on
+ // PRINT_NUMBER_WIDTH.
+ char_u tbuf[20];
+ snprintf((char *)tbuf, sizeof(tbuf), "%6ld", (long)lnum);
+ for (int i = 0; i < 6; i++) {
(void)mch_print_text_out(&tbuf[i], 1);
+ }
- if (psettings->do_syntax)
- /* Set colors for next character. */
+ if (psettings->do_syntax) {
+ // Set colors for next character.
current_syn_id = -1;
- else {
- /* Set colors and font back to normal. */
+ } else {
+ // Set colors and font back to normal.
prt_set_fg(PRCOLOR_BLACK);
prt_set_bg(PRCOLOR_WHITE);
- prt_set_font(FALSE, FALSE, FALSE);
+ prt_set_font(kFalse, kFalse, kFalse);
}
}
@@ -499,22 +498,20 @@ int prt_get_unit(int idx)
return u;
}
-/*
- * Print the page header.
- */
-static void prt_header(prt_settings_T *psettings, int pagenum, linenr_T lnum)
+// Print the page header.
+static void prt_header(prt_settings_T *const psettings, const int pagenum,
+ const linenr_T lnum)
{
int width = psettings->chars_per_line;
- int page_line;
- char_u *tbuf;
- char_u *p;
- /* Also use the space for the line number. */
- if (prt_use_number())
+ // Also use the space for the line number.
+ if (prt_use_number()) {
width += PRINT_NUMBER_WIDTH;
+ }
assert(width >= 0);
- tbuf = xmalloc((size_t)width + IOSIZE);
+ const size_t tbuf_size = (size_t)width + IOSIZE;
+ char_u *tbuf = xmalloc(tbuf_size);
if (*p_header != NUL) {
linenr_T tmp_lnum, tmp_topline, tmp_botline;
@@ -543,38 +540,40 @@ static void prt_header(prt_settings_T *psettings, int pagenum, linenr_T lnum)
curwin->w_cursor.lnum = tmp_lnum;
curwin->w_topline = tmp_topline;
curwin->w_botline = tmp_botline;
- } else
- sprintf((char *)tbuf, _("Page %d"), pagenum);
+ } else {
+ snprintf((char *)tbuf, tbuf_size, _("Page %d"), pagenum);
+ }
prt_set_fg(PRCOLOR_BLACK);
prt_set_bg(PRCOLOR_WHITE);
- prt_set_font(TRUE, FALSE, FALSE);
+ prt_set_font(kTrue, kFalse, kFalse);
- /* Use a negative line number to indicate printing in the top margin. */
- page_line = 0 - prt_header_height();
- mch_print_start_line(TRUE, page_line);
- for (p = tbuf; *p != NUL; ) {
- int l = (*mb_ptr2len)(p);
+ // Use a negative line number to indicate printing in the top margin.
+ int page_line = 0 - prt_header_height();
+ mch_print_start_line(true, page_line);
+ for (char_u *p = tbuf; *p != NUL; ) {
+ const int l = (*mb_ptr2len)(p);
assert(l >= 0);
if (mch_print_text_out(p, (size_t)l)) {
- ++page_line;
- if (page_line >= 0) /* out of room in header */
+ page_line++;
+ if (page_line >= 0) { // out of room in header
break;
- mch_print_start_line(TRUE, page_line);
+ }
+ mch_print_start_line(true, page_line);
}
p += l;
}
xfree(tbuf);
- if (psettings->do_syntax)
- /* Set colors for next character. */
+ if (psettings->do_syntax) {
+ // Set colors for next character.
current_syn_id = -1;
- else {
- /* Set colors and font back to normal. */
+ } else {
+ // Set colors and font back to normal.
prt_set_fg(PRCOLOR_BLACK);
prt_set_bg(PRCOLOR_WHITE);
- prt_set_font(FALSE, FALSE, FALSE);
+ prt_set_font(kFalse, kFalse, kFalse);
}
}
@@ -640,21 +639,19 @@ void ex_hardcopy(exarg_T *eap)
else
settings.do_syntax = settings.has_color;
- /* Set up printing attributes for line numbers */
+ // Set up printing attributes for line numbers
settings.number.fg_color = PRCOLOR_BLACK;
settings.number.bg_color = PRCOLOR_WHITE;
- settings.number.bold = FALSE;
- settings.number.italic = TRUE;
- settings.number.underline = FALSE;
- /*
- * Syntax highlighting of line numbers.
- */
- if (prt_use_number() && settings.do_syntax) {
- int id;
+ settings.number.bold = kFalse;
+ settings.number.italic = kTrue;
+ settings.number.underline = kFalse;
- id = syn_name2id((char_u *)"LineNr");
- if (id > 0)
+ // Syntax highlighting of line numbers.
+ if (prt_use_number() && settings.do_syntax) {
+ int id = syn_name2id((char_u *)"LineNr");
+ if (id > 0) {
id = syn_get_final_id(id);
+ }
prt_get_attr(id, &settings.number, settings.modec);
}
@@ -672,13 +669,13 @@ void ex_hardcopy(exarg_T *eap)
/* Set colors and font to normal. */
curr_bg = 0xffffffff;
curr_fg = 0xffffffff;
- curr_italic = MAYBE;
- curr_bold = MAYBE;
- curr_underline = MAYBE;
+ curr_italic = kNone;
+ curr_bold = kNone;
+ curr_underline = kNone;
prt_set_fg(PRCOLOR_BLACK);
prt_set_bg(PRCOLOR_WHITE);
- prt_set_font(FALSE, FALSE, FALSE);
+ prt_set_font(kFalse, kFalse, kFalse);
current_syn_id = -1;
jobsplit = (printer_opts[OPT_PRINT_JOBSPLIT].present
@@ -841,7 +838,7 @@ static colnr_T hardcopy_line(prt_settings_T *psettings, int page_line, prt_pos_T
tab_spaces = ppos->lead_spaces;
}
- mch_print_start_line(0, page_line);
+ mch_print_start_line(false, page_line);
line = ml_get(ppos->file_line);
/*
@@ -1266,8 +1263,8 @@ static int prt_do_moveto;
static int prt_need_font;
static int prt_font;
static int prt_need_underline;
-static int prt_underline;
-static int prt_do_underline;
+static TriState prt_underline;
+static TriState prt_do_underline;
static int prt_need_fgcol;
static uint32_t prt_fgcol;
static int prt_need_bgcol;
@@ -2855,7 +2852,7 @@ int mch_print_begin_page(char_u *str)
/* We have reset the font attributes, force setting them again. */
curr_bg = 0xffffffff;
curr_fg = 0xffffffff;
- curr_bold = MAYBE;
+ curr_bold = kNone;
return !prt_file_error;
}
@@ -2868,11 +2865,12 @@ int mch_print_blank_page(void)
static double prt_pos_x = 0;
static double prt_pos_y = 0;
-void mch_print_start_line(int margin, int page_line)
+void mch_print_start_line(const bool margin, const int page_line)
{
prt_pos_x = prt_left_margin;
- if (margin)
+ if (margin) {
prt_pos_x -= prt_number_width;
+ }
prt_pos_y = prt_top_margin - prt_first_line_height -
page_line * prt_line_height;
@@ -3059,7 +3057,8 @@ int mch_print_text_out(char_u *const textp, size_t len)
return need_break;
}
-void mch_print_set_font(int iBold, int iItalic, int iUnderline)
+void mch_print_set_font(const TriState iBold, const TriState iItalic,
+ const TriState iUnderline)
{
int font = 0;
diff --git a/src/nvim/hardcopy.h b/src/nvim/hardcopy.h
index a70b20e6f5..c6a3321b08 100644
--- a/src/nvim/hardcopy.h
+++ b/src/nvim/hardcopy.h
@@ -2,10 +2,11 @@
#define NVIM_HARDCOPY_H
#include <stdint.h>
-#include <stdlib.h> // for size_t
+#include <stdlib.h> // for size_t
-#include "nvim/types.h" // for char_u
-#include "nvim/ex_cmds_defs.h" // for exarg_T
+#include "nvim/globals.h" // for TriState
+#include "nvim/types.h" // for char_u
+#include "nvim/ex_cmds_defs.h" // for exarg_T
/*
* Structure to hold printing color and font attributes.
@@ -13,9 +14,9 @@
typedef struct {
uint32_t fg_color;
uint32_t bg_color;
- int bold;
- int italic;
- int underline;
+ TriState bold;
+ TriState italic;
+ TriState underline;
int undercurl;
} prt_text_attr_T;
diff --git a/src/nvim/menu.c b/src/nvim/menu.c
index 968962c894..a56aa82f39 100644
--- a/src/nvim/menu.c
+++ b/src/nvim/menu.c
@@ -63,8 +63,8 @@ ex_menu(exarg_T *eap)
char_u *p;
int i;
long pri_tab[MENUDEPTH + 1];
- int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
- * disable */
+ TriState enable = kNone; // kTrue for "menu enable",
+ // kFalse for "menu disable
vimmenu_T menuarg;
modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu);
@@ -133,10 +133,10 @@ ex_menu(exarg_T *eap)
* Check for "disable" or "enable" argument.
*/
if (STRNCMP(arg, "enable", 6) == 0 && ascii_iswhite(arg[6])) {
- enable = TRUE;
+ enable = kTrue;
arg = skipwhite(arg + 6);
} else if (STRNCMP(arg, "disable", 7) == 0 && ascii_iswhite(arg[7])) {
- enable = FALSE;
+ enable = kFalse;
arg = skipwhite(arg + 7);
}
@@ -160,22 +160,21 @@ ex_menu(exarg_T *eap)
/*
* If there is only a menu name, display menus with that name.
*/
- if (*map_to == NUL && !unmenu && enable == MAYBE) {
+ if (*map_to == NUL && !unmenu && enable == kNone) {
show_menus(menu_path, modes);
goto theend;
- } else if (*map_to != NUL && (unmenu || enable != MAYBE)) {
+ } else if (*map_to != NUL && (unmenu || enable != kNone)) {
EMSG(_(e_trailing));
goto theend;
}
- if (enable != MAYBE) {
- /*
- * Change sensitivity of the menu.
- * For the PopUp menu, remove a menu for each mode separately.
- * Careful: menu_nable_recurse() changes menu_path.
- */
- if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
+ if (enable != kNone) {
+ // Change sensitivity of the menu.
+ // For the PopUp menu, remove a menu for each mode separately.
+ // Careful: menu_nable_recurse() changes menu_path.
+ if (STRCMP(menu_path, "*") == 0) { // meaning: do all menus
menu_path = (char_u *)"";
+ }
if (menu_is_popup(menu_path)) {
for (i = 0; i < MENU_INDEX_TIP; ++i)
diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c
index 684f486c04..da2d8f4e7c 100644
--- a/src/nvim/misc1.c
+++ b/src/nvim/misc1.c
@@ -1203,16 +1203,15 @@ int get_last_leader_offset(char_u *line, char_u **flags)
/*
* Return the number of window lines occupied by buffer line "lnum".
*/
-int plines(linenr_T lnum)
+int plines(const linenr_T lnum)
{
- return plines_win(curwin, lnum, TRUE);
+ return plines_win(curwin, lnum, true);
}
-int
-plines_win (
- win_T *wp,
- linenr_T lnum,
- int winheight /* when TRUE limit to window height */
+int plines_win(
+ win_T *const wp,
+ const linenr_T lnum,
+ const bool winheight // when true limit to window height
)
{
/* Check for filler lines above this buffer line. When folded the result
@@ -1220,34 +1219,34 @@ plines_win (
return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
}
-int plines_nofill(linenr_T lnum)
+int plines_nofill(const linenr_T lnum)
{
- return plines_win_nofill(curwin, lnum, TRUE);
+ return plines_win_nofill(curwin, lnum, true);
}
-int
-plines_win_nofill (
- win_T *wp,
- linenr_T lnum,
- int winheight /* when TRUE limit to window height */
+int plines_win_nofill(
+ win_T *const wp,
+ const linenr_T lnum,
+ const bool winheight // when true limit to window height
)
{
- int lines;
-
- if (!wp->w_p_wrap)
+ if (!wp->w_p_wrap) {
return 1;
+ }
- if (wp->w_width == 0)
+ if (wp->w_width == 0) {
return 1;
+ }
- /* A folded lines is handled just like an empty line. */
- /* NOTE: Caller must handle lines that are MAYBE folded. */
- if (lineFolded(wp, lnum) == TRUE)
+ // A folded lines is handled just like an empty line.
+ if (lineFolded(wp, lnum)) {
return 1;
+ }
- lines = plines_win_nofold(wp, lnum);
- if (winheight > 0 && lines > wp->w_height)
+ const int lines = plines_win_nofold(wp, lnum);
+ if (winheight && lines > wp->w_height) {
return wp->w_height;
+ }
return lines;
}
@@ -1347,11 +1346,12 @@ int plines_m_win(win_T *wp, linenr_T first, linenr_T last)
++count; /* count 1 for "+-- folded" line */
first += x;
} else {
- if (first == wp->w_topline)
- count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
- else
- count += plines_win(wp, first, TRUE);
- ++first;
+ if (first == wp->w_topline) {
+ count += plines_win_nofill(wp, first, true) + wp->w_topfill;
+ } else {
+ count += plines_win(wp, first, true);
+ }
+ first++;
}
}
return count;
diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index 570368991a..a695620801 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -2015,7 +2015,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank)
default:
clearopbeep(oap);
}
- virtual_op = MAYBE;
+ virtual_op = kNone;
if (!gui_yank) {
/*
* if 'sol' not set, go back to old column for some commands
@@ -2090,7 +2090,7 @@ static void op_colon(oparg_T *oap)
*/
static void op_function(oparg_T *oap)
{
- int save_virtual_op = virtual_op;
+ const TriState save_virtual_op = virtual_op;
if (*p_opfunc == NUL)
EMSG(_("E774: 'operatorfunc' is empty"));
@@ -2113,7 +2113,7 @@ static void op_function(oparg_T *oap)
// Reset virtual_op so that 'virtualedit' can be changed in the
// function.
- virtual_op = MAYBE;
+ virtual_op = kNone;
(void)call_func_retnr(p_opfunc, 1, argv, false);
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 67171cb27e..ba4f3cb431 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -5383,7 +5383,7 @@ void cursor_pos_info(dict_T *dict)
case Ctrl_V:
virtual_op = virtual_active();
block_prep(&oparg, &bd, lnum, 0);
- virtual_op = MAYBE;
+ virtual_op = kNone;
s = bd.textstart;
len = (long)bd.textlen;
break;
diff --git a/src/nvim/screen.c b/src/nvim/screen.c
index 9f0d8a5080..8e7a84c6b1 100644
--- a/src/nvim/screen.c
+++ b/src/nvim/screen.c
@@ -783,16 +783,18 @@ static void win_update(win_T *wp)
}
}
- (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
- if (mod_top > lnumt)
+ (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, true, NULL);
+ if (mod_top > lnumt) {
mod_top = lnumt;
+ }
- /* Now do the same for the bottom line (one above mod_bot). */
- --mod_bot;
- (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
- ++mod_bot;
- if (mod_bot < lnumb)
+ // Now do the same for the bottom line (one above mod_bot).
+ mod_bot--;
+ (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, true, NULL);
+ mod_bot++;
+ if (mod_bot < lnumb) {
mod_bot = lnumb;
+ }
}
/* When a change starts above w_topline and the end is below
@@ -833,12 +835,13 @@ static void win_update(win_T *wp)
type = VALID;
}
- /* Trick: we want to avoid clearing the screen twice. screenclear() will
- * set "screen_cleared" to TRUE. The special value MAYBE (which is still
- * non-zero and thus not FALSE) will indicate that screenclear() was not
- * called. */
- if (screen_cleared)
- screen_cleared = MAYBE;
+ // Trick: we want to avoid clearing the screen twice. screenclear() will
+ // set "screen_cleared" to kTrue. The special value kNone (which is still
+ // non-zero and thus not kFalse) will indicate that screenclear() was not
+ // called.
+ if (screen_cleared) {
+ screen_cleared = kNone;
+ }
/*
* If there are no changes on the screen that require a complete redraw,
@@ -875,7 +878,7 @@ static void win_update(win_T *wp)
++j;
if (j >= wp->w_height - 2)
break;
- (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
+ (void)hasFoldingWin(wp, ln, NULL, &ln, true, NULL);
}
} else
j = wp->w_lines[0].wl_lnum - wp->w_topline;
@@ -987,7 +990,7 @@ static void win_update(win_T *wp)
* when it won't get updated below. */
if (wp->w_p_diff && bot_start > 0)
wp->w_lines[0].wl_size =
- plines_win_nofill(wp, wp->w_topline, TRUE)
+ plines_win_nofill(wp, wp->w_topline, true)
+ wp->w_topfill;
}
}
@@ -999,14 +1002,16 @@ static void win_update(win_T *wp)
if (mid_start == 0) {
mid_end = wp->w_height;
if (ONE_WINDOW) {
- /* Clear the screen when it was not done by win_del_lines() or
- * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
- * then. */
- if (screen_cleared != TRUE)
+ // Clear the screen when it was not done by win_del_lines() or
+ // win_ins_lines() above, "screen_cleared" is kFalse or kNone
+ // then.
+ if (screen_cleared != kTrue) {
screenclear();
- /* The screen was cleared, redraw the tab pages line. */
- if (redraw_tabline)
+ }
+ // The screen was cleared, redraw the tab pages line.
+ if (redraw_tabline) {
draw_tabline();
+ }
}
}
@@ -1014,8 +1019,9 @@ static void win_update(win_T *wp)
* cleared (only happens for the first window) or when screenclear()
* was called directly above, "must_redraw" will have been set to
* NOT_VALID, need to reset it here to avoid redrawing twice. */
- if (screen_cleared == TRUE)
+ if (screen_cleared == kTrue) {
must_redraw = 0;
+ }
} else {
/* Not VALID or INVERTED: redraw all lines. */
mid_start = 0;
@@ -1303,15 +1309,15 @@ static void win_update(win_T *wp)
/* Able to count old number of rows: Count new window
* rows, and may insert/delete lines */
j = idx;
- for (l = lnum; l < mod_bot; ++l) {
- if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
- ++new_rows;
- else if (l == wp->w_topline)
- new_rows += plines_win_nofill(wp, l, TRUE)
- + wp->w_topfill;
- else
- new_rows += plines_win(wp, l, TRUE);
- ++j;
+ for (l = lnum; l < mod_bot; l++) {
+ if (hasFoldingWin(wp, l, NULL, &l, true, NULL)) {
+ new_rows++;
+ } else if (l == wp->w_topline) {
+ new_rows += plines_win_nofill(wp, l, true) + wp->w_topfill;
+ } else {
+ new_rows += plines_win(wp, l, true);
+ }
+ j++;
if (new_rows > wp->w_height - row - 2) {
/* it's getting too much, must redraw the rest */
new_rows = 9999;
@@ -1441,12 +1447,13 @@ static void win_update(win_T *wp)
}
wp->w_lines[idx].wl_lnum = lnum;
- wp->w_lines[idx].wl_valid = TRUE;
- if (row > wp->w_height) { /* past end of screen */
- /* we may need the size of that too long line later on */
- if (dollar_vcol == -1)
- wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
- ++idx;
+ wp->w_lines[idx].wl_valid = true;
+ if (row > wp->w_height) { // past end of screen
+ // we may need the size of that too long line later on
+ if (dollar_vcol == -1) {
+ wp->w_lines[idx].wl_size = plines_win(wp, lnum, true);
+ }
+ idx++;
break;
}
if (dollar_vcol == -1)
@@ -3294,8 +3301,7 @@ win_line (
did_emsg = FALSE;
syntax_attr = get_syntax_attr((colnr_T)v - 1,
- has_spell ? &can_spell :
- NULL, FALSE);
+ has_spell ? &can_spell : NULL, false);
if (did_emsg) {
wp->w_s->b_syn_error = TRUE;
@@ -5521,10 +5527,12 @@ static void prepare_search_hl(win_T *wp, linenr_T lnum)
&& re_multiline(shl->rm.regprog)) {
if (shl->first_lnum == 0) {
for (shl->first_lnum = lnum;
- shl->first_lnum > wp->w_topline; --shl->first_lnum)
- if (hasFoldingWin(wp, shl->first_lnum - 1,
- NULL, NULL, TRUE, NULL))
+ shl->first_lnum > wp->w_topline;
+ shl->first_lnum--) {
+ if (hasFoldingWin(wp, shl->first_lnum - 1, NULL, NULL, true, NULL)) {
break;
+ }
+ }
}
if (cur != NULL) {
cur->pos.cur = 0;
@@ -6068,7 +6076,7 @@ static void screenclear2(void)
ui_call_grid_clear(1); // clear the display
clear_cmdline = false;
mode_displayed = false;
- screen_cleared = true; // can use contents of ScreenLines now
+ screen_cleared = kTrue; // can use contents of ScreenLines now
win_rest_invalid(firstwin);
redraw_cmdline = TRUE;
diff --git a/src/nvim/search.c b/src/nvim/search.c
index 578d1fd35a..472b98a501 100644
--- a/src/nvim/search.c
+++ b/src/nvim/search.c
@@ -1549,37 +1549,31 @@ static bool find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
{
- static pos_T pos; /* current search position */
- int findc = 0; /* matching brace */
- int c;
- int count = 0; /* cumulative number of braces */
- int backwards = false; /* init for gcc */
- int raw_string = false; /* search for raw string */
- int inquote = false; /* true when inside quotes */
- char_u *linep; /* pointer to current line */
+ static pos_T pos; // current search position
+ int findc = 0; // matching brace
+ int count = 0; // cumulative number of braces
+ int backwards = false; // init for gcc
+ bool raw_string = false; // search for raw string
+ bool inquote = false; // true when inside quotes
char_u *ptr;
- int do_quotes; /* check for quotes in current line */
- int at_start; /* do_quotes value at start position */
- int hash_dir = 0; /* Direction searched for # things */
- int comment_dir = 0; /* Direction searched for comments */
- pos_T match_pos; /* Where last slash-star was found */
- int start_in_quotes; /* start position is in quotes */
- int traveled = 0; /* how far we've searched so far */
- int ignore_cend = FALSE; /* ignore comment end */
- int cpo_match; /* vi compatible matching */
- int cpo_bsl; /* don't recognize backslashes */
- int match_escaped = 0; /* search for escaped match */
- int dir; /* Direction to search */
- int comment_col = MAXCOL; /* start of / / comment */
- int lispcomm = FALSE; /* inside of Lisp-style comment */
- int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
+ int hash_dir = 0; // Direction searched for # things
+ int comment_dir = 0; // Direction searched for comments
+ int traveled = 0; // how far we've searched so far
+ bool ignore_cend = false; // ignore comment end
+ int match_escaped = 0; // search for escaped match
+ int dir; // Direction to search
+ int comment_col = MAXCOL; // start of / / comment
+ bool lispcomm = false; // inside of Lisp-style comment
+ bool lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
pos = curwin->w_cursor;
pos.coladd = 0;
- linep = ml_get(pos.lnum);
+ char_u *linep = ml_get(pos.lnum); // pointer to current line
- cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
- cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
+ // vi compatible matching
+ bool cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
+ // don't recognize backslashes
+ bool cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
/* Direction to search when initc is '/', '*' or '#' */
if (flags & FM_BACKWARD)
@@ -1748,13 +1742,16 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
}
}
- /* This is just guessing: when 'rightleft' is set, search for a matching
- * paren/brace in the other direction. */
- if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
+ // This is just guessing: when 'rightleft' is set, search for a matching
+ // paren/brace in the other direction.
+ if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL) {
backwards = !backwards;
+ }
- do_quotes = -1;
- start_in_quotes = MAYBE;
+ int do_quotes = -1; // check for quotes in current line
+ int at_start; // do_quotes value at start position
+ TriState start_in_quotes = kNone; // start position is in quotes
+ pos_T match_pos; // Where last slash-star was found
clearpos(&match_pos);
/* backward search: Check if this line contains a single-line comment */
@@ -1762,8 +1759,9 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
|| lisp
)
comment_col = check_linecomment(linep);
- if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
- lispcomm = TRUE; /* find match inside this comment */
+ if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col) {
+ lispcomm = true; // find match inside this comment
+ }
while (!got_int) {
/*
* Go to the next position, forward or backward. We could use
@@ -1925,26 +1923,29 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
* one for a '\' at the end.
*/
if (!do_quotes) {
- inquote = FALSE;
+ inquote = false;
if (ptr[-1] == '\\') {
do_quotes = 1;
- if (start_in_quotes == MAYBE) {
- /* Do we need to use at_start here? */
- inquote = TRUE;
- start_in_quotes = TRUE;
- } else if (backwards)
- inquote = TRUE;
+ if (start_in_quotes == kNone) {
+ // Do we need to use at_start here?
+ inquote = true;
+ start_in_quotes = kTrue;
+ } else if (backwards) {
+ inquote = true;
+ }
}
if (pos.lnum > 1) {
ptr = ml_get(pos.lnum - 1);
if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\') {
do_quotes = 1;
- if (start_in_quotes == MAYBE) {
+ if (start_in_quotes == kNone) {
inquote = at_start;
- if (inquote)
- start_in_quotes = TRUE;
- } else if (!backwards)
- inquote = TRUE;
+ if (inquote) {
+ start_in_quotes = kTrue;
+ }
+ } else if (!backwards) {
+ inquote = true;
+ }
}
/* ml_get() only keeps one line, need to get linep again */
@@ -1952,8 +1953,9 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
}
}
}
- if (start_in_quotes == MAYBE)
- start_in_quotes = FALSE;
+ if (start_in_quotes == kNone) {
+ start_in_quotes = kFalse;
+ }
/*
* If 'smartmatch' is set:
@@ -1966,13 +1968,13 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
* inquote if the number of quotes in a line is even, unless this
* line or the previous one ends in a '\'. Complicated, isn't it?
*/
- c = PTR2CHAR(linep + pos.col);
+ const int c = PTR2CHAR(linep + pos.col);
switch (c) {
case NUL:
/* at end of line without trailing backslash, reset inquote */
if (pos.col == 0 || linep[pos.col - 1] != '\\') {
- inquote = FALSE;
- start_in_quotes = FALSE;
+ inquote = false;
+ start_in_quotes = kFalse;
}
break;
@@ -1987,7 +1989,7 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
break;
if ((((int)pos.col - 1 - col) & 1) == 0) {
inquote = !inquote;
- start_in_quotes = FALSE;
+ start_in_quotes = kFalse;
}
}
break;
@@ -2039,7 +2041,7 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel)
/* Check for match outside of quotes, and inside of
* quotes when the start is also inside of quotes. */
- if ((!inquote || start_in_quotes == TRUE)
+ if ((!inquote || start_in_quotes == kTrue)
&& (c == initc || c == findc)) {
int col, bslcnt = 0;
diff --git a/src/nvim/state.c b/src/nvim/state.c
index a4b394c9e4..5921bd45bf 100644
--- a/src/nvim/state.c
+++ b/src/nvim/state.c
@@ -73,13 +73,13 @@ getkey:
}
}
-/// Return TRUE if in the current mode we need to use virtual.
-int virtual_active(void)
+/// Return true if in the current mode we need to use virtual.
+bool virtual_active(void)
{
// While an operator is being executed we return "virtual_op", because
// VIsual_active has already been reset, thus we can't check for "block"
// being used.
- if (virtual_op != MAYBE) {
+ if (virtual_op != kNone) {
return virtual_op;
}
return ve_flags == VE_ALL
diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c
index b475ca16c8..bd590f53c0 100644
--- a/src/nvim/syntax.c
+++ b/src/nvim/syntax.c
@@ -115,42 +115,42 @@ static int hl_attr_table[] =
{HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_UNDERCURL, HL_ITALIC, HL_INVERSE,
HL_INVERSE, 0};
-/*
- * The patterns that are being searched for are stored in a syn_pattern.
- * A match item consists of one pattern.
- * A start/end item consists of n start patterns and m end patterns.
- * A start/skip/end item consists of n start patterns, one skip pattern and m
- * end patterns.
- * For the latter two, the patterns are always consecutive: start-skip-end.
- *
- * A character offset can be given for the matched text (_m_start and _m_end)
- * and for the actually highlighted text (_h_start and _h_end).
- */
+// The patterns that are being searched for are stored in a syn_pattern.
+// A match item consists of one pattern.
+// A start/end item consists of n start patterns and m end patterns.
+// A start/skip/end item consists of n start patterns, one skip pattern and m
+// end patterns.
+// For the latter two, the patterns are always consecutive: start-skip-end.
+//
+// A character offset can be given for the matched text (_m_start and _m_end)
+// and for the actually highlighted text (_h_start and _h_end).
+//
+// Note that ordering of members is optimized to reduce padding.
typedef struct syn_pattern {
- char sp_type; /* see SPTYPE_ defines below */
- char sp_syncing; /* this item used for syncing */
- int sp_flags; /* see HL_ defines below */
- int sp_cchar; /* conceal substitute character */
- struct sp_syn sp_syn; /* struct passed to in_id_list() */
- short sp_syn_match_id; /* highlight group ID of pattern */
- char_u *sp_pattern; /* regexp to match, pattern */
- regprog_T *sp_prog; /* regexp to match, program */
+ char sp_type; // see SPTYPE_ defines below
+ bool sp_syncing; // this item used for syncing
+ int16_t sp_syn_match_id; // highlight group ID of pattern
+ int16_t sp_off_flags; // see below
+ int sp_offsets[SPO_COUNT]; // offsets
+ int sp_flags; // see HL_ defines below
+ int sp_cchar; // conceal substitute character
+ int sp_ic; // ignore-case flag for sp_prog
+ int sp_sync_idx; // sync item index (syncing only)
+ int sp_line_id; // ID of last line where tried
+ int sp_startcol; // next match in sp_line_id line
+ int16_t *sp_cont_list; // cont. group IDs, if non-zero
+ int16_t *sp_next_list; // next group IDs, if non-zero
+ struct sp_syn sp_syn; // struct passed to in_id_list()
+ char_u *sp_pattern; // regexp to match, pattern
+ regprog_T *sp_prog; // regexp to match, program
syn_time_T sp_time;
- int sp_ic; /* ignore-case flag for sp_prog */
- short sp_off_flags; /* see below */
- int sp_offsets[SPO_COUNT]; /* offsets */
- short *sp_cont_list; /* cont. group IDs, if non-zero */
- short *sp_next_list; /* next group IDs, if non-zero */
- int sp_sync_idx; /* sync item index (syncing only) */
- int sp_line_id; /* ID of last line where tried */
- int sp_startcol; /* next match in sp_line_id line */
} synpat_T;
typedef struct syn_cluster_S {
- char_u *scl_name; /* syntax cluster name */
- char_u *scl_name_u; /* uppercase of scl_name */
- short *scl_list; /* IDs in this syntax cluster */
+ char_u *scl_name; // syntax cluster name
+ char_u *scl_name_u; // uppercase of scl_name
+ int16_t *scl_list; // IDs in this syntax cluster
} syn_cluster_T;
/*
@@ -159,27 +159,27 @@ typedef struct syn_cluster_S {
* (The end positions have the column number of the next char)
*/
typedef struct state_item {
- int si_idx; /* index of syntax pattern or
- KEYWORD_IDX */
- int si_id; /* highlight group ID for keywords */
- int si_trans_id; /* idem, transparency removed */
- int si_m_lnum; /* lnum of the match */
- int si_m_startcol; /* starting column of the match */
- lpos_T si_m_endpos; /* just after end posn of the match */
- lpos_T si_h_startpos; /* start position of the highlighting */
- lpos_T si_h_endpos; /* end position of the highlighting */
- lpos_T si_eoe_pos; /* end position of end pattern */
- int si_end_idx; /* group ID for end pattern or zero */
- int si_ends; /* if match ends before si_m_endpos */
- int si_attr; /* attributes in this state */
- long si_flags; /* HL_HAS_EOL flag in this state, and
- * HL_SKIP* for si_next_list */
- int si_seqnr; /* sequence number */
- int si_cchar; /* substitution character for conceal */
- short *si_cont_list; /* list of contained groups */
- short *si_next_list; /* nextgroup IDs after this item ends */
- reg_extmatch_T *si_extmatch; /* \z(...\) matches from start
- * pattern */
+ int si_idx; // index of syntax pattern or
+ // KEYWORD_IDX
+ int si_id; // highlight group ID for keywords
+ int si_trans_id; // idem, transparency removed
+ int si_m_lnum; // lnum of the match
+ int si_m_startcol; // starting column of the match
+ lpos_T si_m_endpos; // just after end posn of the match
+ lpos_T si_h_startpos; // start position of the highlighting
+ lpos_T si_h_endpos; // end position of the highlighting
+ lpos_T si_eoe_pos; // end position of end pattern
+ int si_end_idx; // group ID for end pattern or zero
+ int si_ends; // if match ends before si_m_endpos
+ int si_attr; // attributes in this state
+ long si_flags; // HL_HAS_EOL flag in this state, and
+ // HL_SKIP* for si_next_list
+ int si_seqnr; // sequence number
+ int si_cchar; // substitution character for conceal
+ int16_t *si_cont_list; // list of contained groups
+ int16_t *si_next_list; // nextgroup IDs after this item ends
+ reg_extmatch_T *si_extmatch; // \z(...\) matches from start
+ // pattern
} stateitem_T;
/*
@@ -187,14 +187,14 @@ typedef struct state_item {
* very often.
*/
typedef struct {
- int flags; /* flags for contained and transparent */
- int keyword; /* TRUE for ":syn keyword" */
- int *sync_idx; /* syntax item for "grouphere" argument, NULL
- if not allowed */
- char has_cont_list; /* TRUE if "cont_list" can be used */
- short *cont_list; /* group IDs for "contains" argument */
- short *cont_in_list; /* group IDs for "containedin" argument */
- short *next_list; /* group IDs for "nextgroup" argument */
+ int flags; // flags for contained and transparent
+ bool keyword; // true for ":syn keyword"
+ int *sync_idx; // syntax item for "grouphere" argument, NULL
+ // if not allowed
+ bool has_cont_list; // true if "cont_list" can be used
+ int16_t *cont_list; // group IDs for "contains" argument
+ int16_t *cont_in_list; // group IDs for "containedin" argument
+ int16_t *next_list; // group IDs for "nextgroup" argument
} syn_opt_arg_T;
typedef struct {
@@ -318,9 +318,10 @@ static int keepend_level = -1;
static char msg_no_items[] = N_("No Syntax items defined for this buffer");
-#define KEYWORD_IDX -1 /* value of si_idx for keywords */
-#define ID_LIST_ALL (short *)-1 /* valid of si_cont_list for containing all
- but contained groups */
+// value of si_idx for keywords
+#define KEYWORD_IDX -1
+// valid of si_cont_list for containing all but contained groups
+#define ID_LIST_ALL (int16_t *)-1
static int next_seqnr = 1; /* value to use for si_seqnr */
@@ -363,9 +364,9 @@ static int current_state_stored = 0; /* TRUE if stored current state
static int current_finished = 0; /* current line has been finished */
static garray_T current_state /* current stack of state_items */
= GA_EMPTY_INIT_VALUE;
-static short *current_next_list = NULL; /* when non-zero, nextgroup list */
-static int current_next_flags = 0; /* flags for current_next_list */
-static int current_line_id = 0; /* unique number for current line */
+static int16_t *current_next_list = NULL; // when non-zero, nextgroup list
+static int current_next_flags = 0; // flags for current_next_list
+static int current_line_id = 0; // unique number for current line
#define CUR_STATE(idx) ((stateitem_T *)(current_state.ga_data))[idx]
@@ -1525,32 +1526,26 @@ int syntax_check_changed(linenr_T lnum)
*/
static bool
syn_finish_line(
- bool syncing // called for syncing
+ const bool syncing // called for syncing
)
{
- stateitem_T *cur_si;
- colnr_T prev_current_col;
-
while (!current_finished) {
- (void)syn_current_attr(syncing, FALSE, NULL, FALSE);
- /*
- * When syncing, and found some item, need to check the item.
- */
+ (void)syn_current_attr(syncing, false, NULL, false);
+
+ // When syncing, and found some item, need to check the item.
if (syncing && current_state.ga_len) {
- /*
- * Check for match with sync item.
- */
- cur_si = &CUR_STATE(current_state.ga_len - 1);
+ // Check for match with sync item.
+ const stateitem_T *const cur_si = &CUR_STATE(current_state.ga_len - 1);
if (cur_si->si_idx >= 0
&& (SYN_ITEMS(syn_block)[cur_si->si_idx].sp_flags
& (HL_SYNC_HERE|HL_SYNC_THERE))) {
return true;
}
- /* syn_current_attr() will have skipped the check for an item
- * that ends here, need to do that now. Be careful not to go
- * past the NUL. */
- prev_current_col = current_col;
+ // syn_current_attr() will have skipped the check for an item
+ // that ends here, need to do that now. Be careful not to go
+ // past the NUL.
+ const colnr_T prev_current_col = current_col;
if (syn_getcurline()[current_col] != NUL) {
current_col++;
}
@@ -1573,9 +1568,9 @@ syn_finish_line(
*/
int
get_syntax_attr(
- colnr_T col,
- bool *can_spell,
- int keep_state /* keep state of char at "col" */
+ const colnr_T col,
+ bool *const can_spell,
+ const bool keep_state // keep state of char at "col"
)
{
int attr = 0;
@@ -1609,9 +1604,9 @@ get_syntax_attr(
* Skip from the current column to "col", get the attributes for "col".
*/
while (current_col <= col) {
- attr = syn_current_attr(FALSE, TRUE, can_spell,
- current_col == col ? keep_state : FALSE);
- ++current_col;
+ attr = syn_current_attr(false, true, can_spell,
+ current_col == col ? keep_state : false);
+ current_col++;
}
return attr;
@@ -1620,42 +1615,37 @@ get_syntax_attr(
/*
* Get syntax attributes for current_lnum, current_col.
*/
-static int
-syn_current_attr(
- int syncing, // When 1: called for syncing
- int displaying, // result will be displayed
- bool *can_spell, // return: do spell checking
- int keep_state // keep syntax stack afterwards
+static int syn_current_attr(
+ const bool syncing, // When true: called for syncing
+ const bool displaying, // result will be displayed
+ bool *const can_spell, // return: do spell checking
+ const bool keep_state // keep syntax stack afterwards
)
{
- int syn_id;
- lpos_T endpos; /* was: char_u *endp; */
- lpos_T hl_startpos; /* was: int hl_startcol; */
+ lpos_T endpos; // was: char_u *endp;
+ lpos_T hl_startpos; // was: int hl_startcol;
lpos_T hl_endpos;
- lpos_T eos_pos; /* end-of-start match (start region) */
- lpos_T eoe_pos; /* end-of-end pattern */
- int end_idx; /* group ID for end pattern */
- synpat_T *spp;
+ lpos_T eos_pos; // end-of-start match (start region)
+ lpos_T eoe_pos; // end-of-end pattern
+ int end_idx; // group ID for end pattern
stateitem_T *cur_si, *sip = NULL;
int startcol;
int endcol;
long flags;
int cchar;
- short *next_list;
- int found_match; /* found usable match */
- static int try_next_column = FALSE; /* must try in next col */
- int do_keywords;
+ int16_t *next_list;
+ bool found_match; // found usable match
+ static bool try_next_column = false; // must try in next col
regmmatch_T regmatch;
lpos_T pos;
- int lc_col;
reg_extmatch_T *cur_extmatch = NULL;
char_u buf_chartab[32]; // chartab array for syn iskeyword
char_u *line; // current line. NOTE: becomes invalid after
// looking for a pattern match!
- /* variables for zero-width matches that have a "nextgroup" argument */
- int keep_next_list;
- int zero_width_next_list = FALSE;
+ // variables for zero-width matches that have a "nextgroup" argument
+ bool keep_next_list;
+ bool zero_width_next_list = false;
garray_T zero_width_next_ga;
/*
@@ -1690,13 +1680,13 @@ syn_current_attr(
*/
if (try_next_column) {
next_match_idx = -1;
- try_next_column = FALSE;
+ try_next_column = false;
}
- /* Only check for keywords when not syncing and there are some. */
- do_keywords = !syncing
- && (syn_block->b_keywtab.ht_used > 0
- || syn_block->b_keywtab_ic.ht_used > 0);
+ // Only check for keywords when not syncing and there are some.
+ const bool do_keywords = !syncing
+ && (syn_block->b_keywtab.ht_used > 0
+ || syn_block->b_keywtab_ic.ht_used > 0);
/* Init the list of zero-width matches with a nextlist. This is used to
* avoid matching the same item in the same position twice. */
@@ -1711,9 +1701,9 @@ syn_current_attr(
* column.
*/
do {
- found_match = FALSE;
- keep_next_list = FALSE;
- syn_id = 0;
+ found_match = false;
+ keep_next_list = false;
+ int syn_id = 0;
/*
* 1. Check for a current state.
@@ -1806,7 +1796,7 @@ syn_current_attr(
next_match_idx = 0; /* no match in this line yet */
next_match_col = MAXCOL;
for (int idx = syn_block->b_syn_patterns.ga_len; --idx >= 0; ) {
- spp = &(SYN_ITEMS(syn_block)[idx]);
+ synpat_T *const spp = &(SYN_ITEMS(syn_block)[idx]);
if ( spp->sp_syncing == syncing
&& (displaying || !(spp->sp_flags & HL_DISPLAY))
&& (spp->sp_type == SPTYPE_MATCH
@@ -1827,13 +1817,14 @@ syn_current_attr(
continue;
spp->sp_line_id = current_line_id;
- lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
- if (lc_col < 0)
+ colnr_T lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
+ if (lc_col < 0) {
lc_col = 0;
+ }
regmatch.rmm_ic = spp->sp_ic;
regmatch.regprog = spp->sp_prog;
- int r = syn_regexec(&regmatch, current_lnum, (colnr_T)lc_col,
+ int r = syn_regexec(&regmatch, current_lnum, lc_col,
IF_SYN_TIME(&spp->sp_time));
spp->sp_prog = regmatch.regprog;
if (!r) {
@@ -1872,7 +1863,7 @@ syn_current_attr(
* column, because it may match from there.
*/
if (did_match_already(idx, &zero_width_next_ga)) {
- try_next_column = TRUE;
+ try_next_column = true;
continue;
}
@@ -1934,9 +1925,9 @@ syn_current_attr(
* If an empty string is matched, may need
* to try matching again at next column.
*/
- if (regmatch.startpos[0].col
- == regmatch.endpos[0].col)
- try_next_column = TRUE;
+ if (regmatch.startpos[0].col == regmatch.endpos[0].col) {
+ try_next_column = true;
+ }
continue;
}
}
@@ -1981,8 +1972,8 @@ syn_current_attr(
&& lspp->sp_next_list != NULL) {
current_next_list = lspp->sp_next_list;
current_next_flags = lspp->sp_flags;
- keep_next_list = TRUE;
- zero_width_next_list = TRUE;
+ keep_next_list = true;
+ zero_width_next_list = true;
/* Add the index to a list, so that we can check
* later that we don't match it again (and cause an
@@ -2025,8 +2016,9 @@ syn_current_attr(
*/
current_next_list = NULL;
next_match_idx = -1;
- if (!zero_width_next_list)
- found_match = TRUE;
+ if (!zero_width_next_list) {
+ found_match = true;
+ }
}
} while (found_match);
@@ -2931,39 +2923,35 @@ static int syn_regexec(regmmatch_T *rmp, linenr_T lnum, colnr_T col, syn_time_T
* The caller must check if a keyword can start at startcol.
* Return its ID if found, 0 otherwise.
*/
-static int
-check_keyword_id(
- char_u *line,
- int startcol, /* position in line to check for keyword */
- int *endcolp, /* return: character after found keyword */
- long *flagsp, /* return: flags of matching keyword */
- short **next_listp, /* return: next_list of matching keyword */
- stateitem_T *cur_si, /* item at the top of the stack */
- int *ccharp /* conceal substitution char */
+static int check_keyword_id(
+ char_u *const line,
+ const int startcol, // position in line to check for keyword
+ int *const endcolp, // return: character after found keyword
+ long *const flagsp, // return: flags of matching keyword
+ int16_t **const next_listp, // return: next_list of matching keyword
+ stateitem_T *const cur_si, // item at the top of the stack
+ int *const ccharp // conceal substitution char
)
{
- char_u *kwp;
- int kwlen;
- char_u keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
-
- /* Find first character after the keyword. First character was already
- * checked. */
- kwp = line + startcol;
- kwlen = 0;
+ // Find first character after the keyword. First character was already
+ // checked.
+ char_u *const kwp = line + startcol;
+ int kwlen = 0;
do {
- if (has_mbyte)
+ if (has_mbyte) {
kwlen += (*mb_ptr2len)(kwp + kwlen);
- else
- ++kwlen;
+ } else {
+ kwlen++;
+ }
} while (vim_iswordp_buf(kwp + kwlen, syn_buf));
- if (kwlen > MAXKEYWLEN)
+ if (kwlen > MAXKEYWLEN) {
return 0;
+ }
- /*
- * Must make a copy of the keyword, so we can add a NUL and make it
- * lowercase.
- */
+ // Must make a copy of the keyword, so we can add a NUL and make it
+ // lowercase.
+ char_u keyword[MAXKEYWLEN + 1]; // assume max. keyword len is 80
STRLCPY(keyword, kwp, kwlen + 1);
keyentry_T *kp = NULL;
@@ -3321,12 +3309,10 @@ static void syn_cmd_clear(exarg_T *eap, int syncing)
EMSG2(_("E391: No such syntax cluster: %s"), arg);
break;
} else {
- /*
- * We can't physically delete a cluster without changing
- * the IDs of other clusters, so we do the next best thing
- * and make it empty.
- */
- short scl_id = id - SYNID_CLUSTER;
+ // We can't physically delete a cluster without changing
+ // the IDs of other clusters, so we do the next best thing
+ // and make it empty.
+ int scl_id = id - SYNID_CLUSTER;
xfree(SYN_CLSTR(curwin->w_s)[scl_id].scl_list);
SYN_CLSTR(curwin->w_s)[scl_id].scl_list = NULL;
@@ -3349,7 +3335,7 @@ static void syn_cmd_clear(exarg_T *eap, int syncing)
/*
* Clear one syntax group for the current buffer.
*/
-static void syn_clear_one(int id, int syncing)
+static void syn_clear_one(const int id, const bool syncing)
{
synpat_T *spp;
@@ -3491,8 +3477,8 @@ syn_cmd_list(
/*
* No argument: List all group IDs and all syntax clusters.
*/
- for (int id = 1; id <= highlight_ga.ga_len && !got_int; ++id) {
- syn_list_one(id, syncing, FALSE);
+ for (int id = 1; id <= highlight_ga.ga_len && !got_int; id++) {
+ syn_list_one(id, syncing, false);
}
for (int id = 0; id < curwin->w_s->b_syn_clusters.ga_len && !got_int; ++id) {
syn_list_cluster(id);
@@ -3511,10 +3497,11 @@ syn_cmd_list(
syn_list_cluster(id - SYNID_CLUSTER);
} else {
int id = syn_namen2id(arg, (int)(arg_end - arg));
- if (id == 0)
+ if (id == 0) {
EMSG2(_(e_nogroup), arg);
- else
- syn_list_one(id, syncing, TRUE);
+ } else {
+ syn_list_one(id, syncing, true);
+ }
}
arg = skipwhite(arg_end);
}
@@ -3558,14 +3545,12 @@ static int last_matchgroup;
*/
static void
syn_list_one(
- int id,
- int syncing, /* when TRUE: list syncing items */
- int link_only /* when TRUE; list link-only too */
+ const int id,
+ const bool syncing, // when true: list syncing items
+ const bool link_only // when true; list link-only too
)
{
- int attr;
- int did_header = FALSE;
- synpat_T *spp;
+ bool did_header = false;
static struct name_list namelist1[] =
{
{HL_DISPLAY, "display"},
@@ -3588,23 +3573,26 @@ syn_list_one(
{0, NULL}
};
- attr = HL_ATTR(HLF_D); // highlight like directories
+ const int attr = HL_ATTR(HLF_D); // highlight like directories
- /* list the keywords for "id" */
+ // list the keywords for "id"
if (!syncing) {
- did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, FALSE, attr);
+ did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab, false, attr);
did_header = syn_list_keywords(id, &curwin->w_s->b_keywtab_ic,
- did_header, attr);
+ did_header, attr);
}
- /* list the patterns for "id" */
- for (int idx = 0; idx < curwin->w_s->b_syn_patterns.ga_len && !got_int; ++idx) {
- spp = &(SYN_ITEMS(curwin->w_s)[idx]);
- if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
+ // list the patterns for "id"
+ for (int idx = 0;
+ idx < curwin->w_s->b_syn_patterns.ga_len && !got_int;
+ idx++) {
+ const synpat_T *const spp = &(SYN_ITEMS(curwin->w_s)[idx]);
+ if (spp->sp_syn.id != id || spp->sp_syncing != syncing) {
continue;
+ }
(void)syn_list_header(did_header, 999, id);
- did_header = TRUE;
+ did_header = true;
last_matchgroup = 0;
if (spp->sp_type == SPTYPE_MATCH) {
put_pattern("match", ' ', spp, attr);
@@ -3695,15 +3683,13 @@ static void syn_list_cluster(int id)
}
}
-static void put_id_list(const char *name,
- short *list, // NOLINT(runtime/int)
- int attr)
+static void put_id_list(const char *const name,
+ const int16_t *const list,
+ const int attr)
{
- short *p;
-
msg_puts_attr(name, attr);
msg_putchar('=');
- for (p = list; *p; ++p) {
+ for (const int16_t *p = list; *p; p++) {
if (*p >= SYNID_ALLBUT && *p < SYNID_TOP) {
if (p[1]) {
msg_puts("ALLBUT");
@@ -3715,7 +3701,7 @@ static void put_id_list(const char *name,
} else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER) {
msg_puts("CONTAINED");
} else if (*p >= SYNID_CLUSTER) {
- short scl_id = *p - SYNID_CLUSTER;
+ int scl_id = *p - SYNID_CLUSTER;
msg_putchar('@');
msg_outtrans(SYN_CLSTR(curwin->w_s)[scl_id].scl_name);
@@ -3727,12 +3713,10 @@ static void put_id_list(const char *name,
msg_putchar(' ');
}
-static void put_pattern(char *s, int c, synpat_T *spp, int attr)
+static void put_pattern(const char *const s, const int c,
+ const synpat_T *const spp, const int attr)
{
- long n;
- int mask;
- int first;
- static char *sepchars = "/+=-#@\"|'^&";
+ static const char *const sepchars = "/+=-#@\"|'^&";
int i;
/* May have to write "matchgroup=group" */
@@ -3761,10 +3745,10 @@ static void put_pattern(char *s, int c, synpat_T *spp, int attr)
msg_outtrans(spp->sp_pattern);
msg_putchar(sepchars[i]);
- /* output any pattern options */
- first = TRUE;
- for (i = 0; i < SPO_COUNT; ++i) {
- mask = (1 << i);
+ // output any pattern options
+ bool first = true;
+ for (i = 0; i < SPO_COUNT; i++) {
+ const int mask = (1 << i);
if (!(spp->sp_off_flags & (mask + (mask << SPO_COUNT)))) {
continue;
}
@@ -3772,7 +3756,7 @@ static void put_pattern(char *s, int c, synpat_T *spp, int attr)
msg_putchar(','); // Separate with commas.
}
msg_puts(spo_name_tab[i]);
- n = spp->sp_offsets[i];
+ const long n = spp->sp_offsets[i];
if (i != SPO_LC_OFF) {
if (spp->sp_off_flags & mask)
msg_putchar('s');
@@ -3781,47 +3765,40 @@ static void put_pattern(char *s, int c, synpat_T *spp, int attr)
if (n > 0)
msg_putchar('+');
}
- if (n || i == SPO_LC_OFF)
+ if (n || i == SPO_LC_OFF) {
msg_outnum(n);
- first = FALSE;
+ }
+ first = false;
}
msg_putchar(' ');
}
-/*
- * List or clear the keywords for one syntax group.
- * Return TRUE if the header has been printed.
- */
-static int
-syn_list_keywords(
- int id,
- hashtab_T *ht,
- int did_header, /* header has already been printed */
- int attr
+// List or clear the keywords for one syntax group.
+// Return true if the header has been printed.
+static bool syn_list_keywords(
+ const int id,
+ const hashtab_T *const ht,
+ bool did_header, // header has already been printed
+ const int attr
)
{
int outlen;
- hashitem_T *hi;
- keyentry_T *kp;
- int todo;
int prev_contained = 0;
- short *prev_next_list = NULL;
- short *prev_cont_in_list = NULL;
+ const int16_t *prev_next_list = NULL;
+ const int16_t *prev_cont_in_list = NULL;
int prev_skipnl = 0;
int prev_skipwhite = 0;
int prev_skipempty = 0;
- /*
- * Unfortunately, this list of keywords is not sorted on alphabet but on
- * hash value...
- */
- todo = (int)ht->ht_used;
- for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) {
+ // Unfortunately, this list of keywords is not sorted on alphabet but on
+ // hash value...
+ size_t todo = ht->ht_used;
+ for (const hashitem_T *hi = ht->ht_array; todo > 0 && !got_int; hi++) {
if (HASHITEM_EMPTY(hi)) {
continue;
}
- --todo;
- for (kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next) {
+ todo--;
+ for (keyentry_T *kp = HI2KE(hi); kp != NULL && !got_int; kp = kp->ke_next) {
if (kp->k_syn.id == id) {
if (prev_contained != (kp->flags & HL_CONTAINED)
|| prev_skipnl != (kp->flags & HL_SKIPNL)
@@ -3841,7 +3818,7 @@ syn_list_keywords(
prev_skipwhite = 0;
prev_skipempty = 0;
}
- did_header = TRUE;
+ did_header = true;
if (prev_contained != (kp->flags & HL_CONTAINED)) {
msg_puts_attr("contained", attr);
msg_putchar(' ');
@@ -3952,19 +3929,19 @@ static void clear_keywtab(hashtab_T *ht)
/// @param flags flags for this keyword
/// @param cont_in_list containedin for this keyword
/// @param next_list nextgroup for this keyword
-static void add_keyword(char_u *name,
- int id,
- int flags,
- short *cont_in_list,
- short *next_list,
- int conceal_char)
+static void add_keyword(char_u *const name,
+ const int id,
+ const int flags,
+ int16_t *const cont_in_list,
+ int16_t *const next_list,
+ const int conceal_char)
{
char_u name_folded[MAXKEYWLEN + 1];
- char_u *name_ic = (curwin->w_s->b_syn_ic)
- ? str_foldcase(name, (int)STRLEN(name), name_folded, sizeof(name_folded))
- : name;
+ const char_u *const name_ic = (curwin->w_s->b_syn_ic)
+ ? str_foldcase(name, (int)STRLEN(name), name_folded, sizeof(name_folded))
+ : name;
- keyentry_T *kp = xmalloc(sizeof(keyentry_T) + STRLEN(name_ic));
+ keyentry_T *const kp = xmalloc(sizeof(keyentry_T) + STRLEN(name_ic));
STRCPY(kp->keyword, name_ic);
kp->k_syn.id = id;
kp->k_syn.inc_tag = current_syn_inc_tag;
@@ -3976,11 +3953,12 @@ static void add_keyword(char_u *name,
}
kp->next_list = copy_id_list(next_list);
- hash_T hash = hash_hash(kp->keyword);
- hashtab_T *ht = (curwin->w_s->b_syn_ic) ? &curwin->w_s->b_keywtab_ic
- : &curwin->w_s->b_keywtab;
- hashitem_T *hi = hash_lookup(ht, (const char *)kp->keyword,
- STRLEN(kp->keyword), hash);
+ const hash_T hash = hash_hash(kp->keyword);
+ hashtab_T *const ht = (curwin->w_s->b_syn_ic)
+ ? &curwin->w_s->b_keywtab_ic
+ : &curwin->w_s->b_keywtab;
+ hashitem_T *const hi = hash_lookup(ht, (const char *)kp->keyword,
+ STRLEN(kp->keyword), hash);
// even though it looks like only the kp->keyword member is
// being used here, vim uses some pointer trickery to get the orignal
@@ -4191,8 +4169,8 @@ static void syn_incl_toplevel(int id, int *flagsp)
return;
*flagsp |= HL_CONTAINED;
if (curwin->w_s->b_syn_topgrp >= SYNID_CLUSTER) {
- /* We have to alloc this, because syn_combine_list() will free it. */
- short *grp_list = xmalloc(2 * sizeof(short));
+ // We have to alloc this, because syn_combine_list() will free it.
+ int16_t *grp_list = xmalloc(2 * sizeof(*grp_list));
int tlg_id = curwin->w_s->b_syn_topgrp - SYNID_CLUSTER;
grp_list[0] = id;
@@ -4407,9 +4385,9 @@ syn_cmd_match(
/* Get options before the pattern */
syn_opt_arg.flags = 0;
- syn_opt_arg.keyword = FALSE;
+ syn_opt_arg.keyword = false;
syn_opt_arg.sync_idx = syncing ? &sync_idx : NULL;
- syn_opt_arg.has_cont_list = TRUE;
+ syn_opt_arg.has_cont_list = true;
syn_opt_arg.cont_list = NULL;
syn_opt_arg.cont_in_list = NULL;
syn_opt_arg.next_list = NULL;
@@ -4529,9 +4507,9 @@ syn_cmd_region(
init_syn_patterns();
syn_opt_arg.flags = 0;
- syn_opt_arg.keyword = FALSE;
+ syn_opt_arg.keyword = false;
syn_opt_arg.sync_idx = NULL;
- syn_opt_arg.has_cont_list = TRUE;
+ syn_opt_arg.has_cont_list = true;
syn_opt_arg.cont_list = NULL;
syn_opt_arg.cont_in_list = NULL;
syn_opt_arg.next_list = NULL;
@@ -4709,30 +4687,25 @@ syn_cmd_region(
}
}
-/*
- * A simple syntax group ID comparison function suitable for use in qsort()
- */
-static int syn_compare_stub(const void *v1, const void *v2)
+// A simple syntax group ID comparison function suitable for use in qsort()
+static int syn_compare_stub(const void *const v1, const void *const v2)
{
- const short *s1 = v1;
- const short *s2 = v2;
+ const int16_t *const s1 = v1;
+ const int16_t *const s2 = v2;
return *s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0;
}
-/*
- * Combines lists of syntax clusters.
- * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
- */
-static void syn_combine_list(short **clstr1, short **clstr2, int list_op)
+// Combines lists of syntax clusters.
+// *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
+static void syn_combine_list(int16_t **const clstr1, int16_t **const clstr2,
+ const int list_op)
{
- int count1 = 0;
- int count2 = 0;
- short *g1;
- short *g2;
- short *clstr = NULL;
- int count;
- int round;
+ size_t count1 = 0;
+ size_t count2 = 0;
+ const int16_t *g1;
+ const int16_t *g2;
+ int16_t *clstr = NULL;
/*
* Handle degenerate cases.
@@ -4749,27 +4722,25 @@ static void syn_combine_list(short **clstr1, short **clstr2, int list_op)
return;
}
- for (g1 = *clstr1; *g1; g1++)
- ++count1;
- for (g2 = *clstr2; *g2; g2++)
- ++count2;
+ for (g1 = *clstr1; *g1; g1++) {
+ count1++;
+ }
+ for (g2 = *clstr2; *g2; g2++) {
+ count2++;
+ }
- /*
- * For speed purposes, sort both lists.
- */
- qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
- qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
+ // For speed purposes, sort both lists.
+ qsort(*clstr1, count1, sizeof(**clstr1), syn_compare_stub);
+ qsort(*clstr2, count2, sizeof(**clstr2), syn_compare_stub);
- /*
- * We proceed in two passes; in round 1, we count the elements to place
- * in the new list, and in round 2, we allocate and populate the new
- * list. For speed, we use a mergesort-like method, adding the smaller
- * of the current elements in each list to the new list.
- */
- for (round = 1; round <= 2; round++) {
+ // We proceed in two passes; in round 1, we count the elements to place
+ // in the new list, and in round 2, we allocate and populate the new
+ // list. For speed, we use a mergesort-like method, adding the smaller
+ // of the current elements in each list to the new list.
+ for (int round = 1; round <= 2; round++) {
g1 = *clstr1;
g2 = *clstr2;
- count = 0;
+ int count = 0;
/*
* First, loop through the lists until one of them is empty.
@@ -4821,7 +4792,7 @@ static void syn_combine_list(short **clstr1, short **clstr2, int list_op)
clstr = NULL;
break;
}
- clstr = xmalloc((count + 1) * sizeof(short));
+ clstr = xmalloc((count + 1) * sizeof(*clstr));
clstr[count] = 0;
}
}
@@ -4926,9 +4897,7 @@ static void syn_cmd_cluster(exarg_T *eap, int syncing)
char_u *arg = eap->arg;
char_u *group_name_end;
char_u *rest;
- int scl_id;
- short *clstr_list;
- int got_clstr = FALSE;
+ bool got_clstr = false;
int opt_len;
int list_op;
@@ -4939,9 +4908,10 @@ static void syn_cmd_cluster(exarg_T *eap, int syncing)
rest = get_group_name(arg, &group_name_end);
if (rest != NULL) {
- scl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
- if (scl_id == 0)
+ int scl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
+ if (scl_id == 0) {
return;
+ }
scl_id -= SYNID_CLUSTER;
for (;; ) {
@@ -4960,7 +4930,7 @@ static void syn_cmd_cluster(exarg_T *eap, int syncing)
} else
break;
- clstr_list = NULL;
+ int16_t *clstr_list = NULL;
if (get_id_list(&rest, opt_len, &clstr_list, eap->skip) == FAIL) {
EMSG2(_(e_invarg2), rest);
break;
@@ -5218,35 +5188,28 @@ static void syn_cmd_sync(exarg_T *eap, int syncing)
*/
static int
get_id_list(
- char_u **arg,
- int keylen, // length of keyword
- int16_t **list, // where to store the resulting list, if not
- // NULL, the list is silently skipped!
- int skip
+ char_u **const arg,
+ const int keylen, // length of keyword
+ int16_t **const list, // where to store the resulting list, if not
+ // NULL, the list is silently skipped!
+ const bool skip
)
{
char_u *p = NULL;
char_u *end;
- int round;
- int count;
int total_count = 0;
- short *retval = NULL;
- char_u *name;
+ int16_t *retval = NULL;
regmatch_T regmatch;
int id;
- int failed = FALSE;
-
- /*
- * We parse the list twice:
- * round == 1: count the number of items, allocate the array.
- * round == 2: fill the array with the items.
- * In round 1 new groups may be added, causing the number of items to
- * grow when a regexp is used. In that case round 1 is done once again.
- */
- for (round = 1; round <= 2; ++round) {
- /*
- * skip "contains"
- */
+ bool failed = false;
+
+ // We parse the list twice:
+ // round == 1: count the number of items, allocate the array.
+ // round == 2: fill the array with the items.
+ // In round 1 new groups may be added, causing the number of items to
+ // grow when a regexp is used. In that case round 1 is done once again.
+ for (int round = 1; round <= 2; round++) {
+ // skip "contains"
p = skipwhite(*arg + keylen);
if (*p != '=') {
EMSG2(_("E405: Missing equal sign: %s"), *arg);
@@ -5258,14 +5221,12 @@ get_id_list(
break;
}
- /*
- * parse the arguments after "contains"
- */
- count = 0;
+ // parse the arguments after "contains"
+ int count = 0;
do {
- for (end = p; *end && !ascii_iswhite(*end) && *end != ','; ++end)
- ;
- name = xmalloc((int)(end - p + 3)); /* leave room for "^$" */
+ for (end = p; *end && !ascii_iswhite(*end) && *end != ','; end++) {
+ }
+ char_u *const name = xmalloc((int)(end - p + 3)); // leave room for "^$"
STRLCPY(name + 1, p, end - p + 1);
if ( STRCMP(name + 1, "ALLBUT") == 0
|| STRCMP(name + 1, "ALL") == 0
@@ -5273,7 +5234,7 @@ get_id_list(
|| STRCMP(name + 1, "CONTAINED") == 0) {
if (TOUPPER_ASC(**arg) != 'C') {
EMSG2(_("E407: %s not allowed here"), name + 1);
- failed = TRUE;
+ failed = true;
xfree(name);
break;
}
@@ -5309,7 +5270,7 @@ get_id_list(
STRCAT(name, "$");
regmatch.regprog = vim_regcomp(name, RE_MAGIC);
if (regmatch.regprog == NULL) {
- failed = TRUE;
+ failed = true;
xfree(name);
break;
}
@@ -5340,7 +5301,7 @@ get_id_list(
xfree(name);
if (id == 0) {
EMSG2(_("E409: Unknown group name: %s"), p);
- failed = TRUE;
+ failed = true;
break;
}
if (id > 0) {
@@ -5363,8 +5324,8 @@ get_id_list(
if (failed)
break;
if (round == 1) {
- retval = xmalloc((count + 1) * sizeof(short));
- retval[count] = 0; /* zero means end of the list */
+ retval = xmalloc((count + 1) * sizeof(*retval));
+ retval[count] = 0; // zero means end of the list
total_count = count;
}
}
@@ -5386,20 +5347,18 @@ get_id_list(
/*
* Make a copy of an ID list.
*/
-static short *copy_id_list(short *list)
+static int16_t *copy_id_list(const int16_t *const list)
{
- int len;
- int count;
- short *retval;
-
- if (list == NULL)
+ if (list == NULL) {
return NULL;
+ }
- for (count = 0; list[count]; ++count)
- ;
- len = (count + 1) * sizeof(short);
- retval = xmalloc(len);
- memmove(retval, list, (size_t)len);
+ int count;
+ for (count = 0; list[count]; count++) {
+ }
+ const size_t len = (count + 1) * sizeof(int16_t);
+ int16_t *const retval = xmalloc(len);
+ memmove(retval, list, len);
return retval;
}
@@ -7039,12 +6998,10 @@ static void highlight_clear(int idx)
#define LIST_INT 3
/// @}
-static void highlight_list_one(int id)
+static void highlight_list_one(const int id)
{
- struct hl_group *sgp;
- int didh = FALSE;
-
- sgp = &HL_TABLE()[id - 1]; /* index is ID minus one */
+ struct hl_group *const sgp = &HL_TABLE()[id - 1]; // index is ID minus one
+ bool didh = false;
didh = highlight_list_arg(id, didh, LIST_ATTR,
sgp->sg_cterm, NULL, "cterm");
@@ -7081,24 +7038,24 @@ static void highlight_list_one(int id)
/// @param type one of \ref LIST_XXX
/// @param iarg integer argument used if \p type == LIST_INT
/// @param sarg string used if \p type == LIST_STRING
-static int highlight_list_arg(int id, int didh, int type, int iarg,
- char_u *sarg, const char *name)
+static bool highlight_list_arg(
+ const int id, bool didh, const int type, int iarg,
+ char_u *const sarg, const char *const name)
{
char_u buf[100];
- char_u *ts;
- int i;
- if (got_int)
- return FALSE;
+ if (got_int) {
+ return false;
+ }
if (type == LIST_STRING ? (sarg != NULL) : (iarg != 0)) {
- ts = buf;
- if (type == LIST_INT)
- sprintf((char *)buf, "%d", iarg - 1);
- else if (type == LIST_STRING)
+ char_u *ts = buf;
+ if (type == LIST_INT) {
+ snprintf((char *)buf, sizeof(buf), "%d", iarg - 1);
+ } else if (type == LIST_STRING) {
ts = sarg;
- else { /* type == LIST_ATTR */
+ } else { // type == LIST_ATTR
buf[0] = NUL;
- for (i = 0; hl_attr_table[i] != 0; ++i) {
+ for (int i = 0; hl_attr_table[i] != 0; i++) {
if (iarg & hl_attr_table[i]) {
if (buf[0] != NUL)
xstrlcat((char *)buf, ",", 100);
@@ -7108,9 +7065,8 @@ static int highlight_list_arg(int id, int didh, int type, int iarg,
}
}
- (void)syn_list_header(didh,
- (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
- didh = TRUE;
+ (void)syn_list_header(didh, (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
+ didh = true;
if (!got_int) {
if (*name != NUL) {
MSG_PUTS_ATTR(name, HL_ATTR(HLF_D));
@@ -7228,11 +7184,11 @@ const char *highlight_color(const int id, const char *const what,
/// @param outlen length of string that comes
/// @param id highlight group id
/// @return true when started a new line.
-static int
-syn_list_header(int did_header, int outlen, int id)
+static bool syn_list_header(const bool did_header, const int outlen,
+ const int id)
{
int endcol = 19;
- int newline = TRUE;
+ bool newline = true;
if (!did_header) {
msg_putchar('\n');
@@ -7243,11 +7199,13 @@ syn_list_header(int did_header, int outlen, int id)
endcol = 15;
} else if (msg_col + outlen + 1 >= Columns) {
msg_putchar('\n');
- if (got_int)
- return TRUE;
+ if (got_int) {
+ return true;
+ }
} else {
- if (msg_col >= endcol) /* wrap around is like starting a new line */
- newline = FALSE;
+ if (msg_col >= endcol) { // wrap around is like starting a new line
+ newline = false;
+ }
}
if (msg_col >= endcol) /* output at least one space */
diff --git a/src/nvim/syntax.h b/src/nvim/syntax.h
index 9fbad74f64..f8282955a6 100644
--- a/src/nvim/syntax.h
+++ b/src/nvim/syntax.h
@@ -3,7 +3,6 @@
#include <stdbool.h>
-#include "nvim/globals.h"
#include "nvim/buffer_defs.h"
#include "nvim/ex_cmds_defs.h"
diff --git a/src/nvim/syntax_defs.h b/src/nvim/syntax_defs.h
index 63089a62af..5ee0611d58 100644
--- a/src/nvim/syntax_defs.h
+++ b/src/nvim/syntax_defs.h
@@ -14,13 +14,11 @@ typedef struct syn_state synstate_T;
#include "nvim/buffer_defs.h"
#include "nvim/regexp_defs.h"
-typedef unsigned short disptick_T; /* display tick type */
-
/* struct passed to in_id_list() */
struct sp_syn {
- int inc_tag; /* ":syn include" unique tag */
- short id; /* highlight group ID of item */
- short *cont_in_list; /* cont.in group IDs, if non-zero */
+ int inc_tag; // ":syn include" unique tag
+ int16_t id; // highlight group ID of item
+ int16_t *cont_in_list; // cont.in group IDs, if non-zero
};
/*
@@ -29,12 +27,12 @@ struct sp_syn {
typedef struct keyentry keyentry_T;
struct keyentry {
- keyentry_T *ke_next; /* next entry with identical "keyword[]" */
- struct sp_syn k_syn; /* struct passed to in_id_list() */
- short *next_list; /* ID list for next match (if non-zero) */
+ keyentry_T *ke_next; // next entry with identical "keyword[]"
+ struct sp_syn k_syn; // struct passed to in_id_list()
+ int16_t *next_list; // ID list for next match (if non-zero)
int flags;
- int k_char; /* conceal substitute character */
- char_u keyword[1]; /* actually longer */
+ int k_char; // conceal substitute character
+ char_u keyword[1]; // actually longer
};
/*
@@ -59,13 +57,13 @@ struct syn_state {
bufstate_T sst_stack[SST_FIX_STATES]; /* short state stack */
garray_T sst_ga; /* growarray for long state stack */
} sst_union;
- int sst_next_flags; /* flags for sst_next_list */
- int sst_stacksize; /* number of states on the stack */
- short *sst_next_list; /* "nextgroup" list in this state
- * (this is a copy, don't free it! */
- disptick_T sst_tick; /* tick when last displayed */
- linenr_T sst_change_lnum; /* when non-zero, change in this line
- * may have made the state invalid */
+ int sst_next_flags; // flags for sst_next_list
+ int sst_stacksize; // number of states on the stack
+ int16_t *sst_next_list; // "nextgroup" list in this state
+ // (this is a copy, don't free it!
+ disptick_T sst_tick; // tick when last displayed
+ linenr_T sst_change_lnum; // when non-zero, change in this line
+ // may have made the state invalid
};
#endif // NVIM_SYNTAX_DEFS_H
diff --git a/src/nvim/window.c b/src/nvim/window.c
index 274bf72f3b..7582c837c8 100644
--- a/src/nvim/window.c
+++ b/src/nvim/window.c
@@ -4848,8 +4848,8 @@ void scroll_to_fraction(win_T *wp, int prev_height)
sline = wp->w_wrow - line_size;
if (sline >= 0) {
- /* Make sure the whole cursor line is visible, if possible. */
- int rows = plines_win(wp, lnum, FALSE);
+ // Make sure the whole cursor line is visible, if possible.
+ const int rows = plines_win(wp, lnum, false);
if (sline > wp->w_height - rows) {
sline = wp->w_height - rows;
@@ -4884,12 +4884,13 @@ void scroll_to_fraction(win_T *wp, int prev_height)
--sline;
break;
}
- --lnum;
- if (lnum == wp->w_topline)
- line_size = plines_win_nofill(wp, lnum, TRUE)
+ lnum--;
+ if (lnum == wp->w_topline) {
+ line_size = plines_win_nofill(wp, lnum, true)
+ wp->w_topfill;
- else
- line_size = plines_win(wp, lnum, TRUE);
+ } else {
+ line_size = plines_win(wp, lnum, true);
+ }
sline -= line_size;
}
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua
index 823c134ebd..958e2f68fc 100644
--- a/test/functional/api/buffer_spec.lua
+++ b/test/functional/api/buffer_spec.lua
@@ -35,8 +35,37 @@ describe('api/buf', function()
-- There's always at least one line
eq(1, curbuf_depr('line_count'))
end)
- end)
+ it('line_count has defined behaviour for unloaded buffers', function()
+ -- we'll need to know our bufnr for when it gets unloaded
+ local bufnr = curbuf('get_number')
+ -- replace the buffer contents with these three lines
+ request('nvim_buf_set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"})
+ -- check the line count is correct
+ eq(4, request('nvim_buf_line_count', bufnr))
+ -- force unload the buffer (this will discard changes)
+ command('new')
+ command('bunload! '..bufnr)
+ -- line count for an unloaded buffer should always be 0
+ eq(0, request('nvim_buf_line_count', bufnr))
+ end)
+
+ it('get_lines has defined behaviour for unloaded buffers', function()
+ -- we'll need to know our bufnr for when it gets unloaded
+ local bufnr = curbuf('get_number')
+ -- replace the buffer contents with these three lines
+ buffer('set_lines', bufnr, 0, -1, 1, {"line1", "line2", "line3", "line4"})
+ -- confirm that getting lines works
+ eq({"line2", "line3"}, buffer('get_lines', bufnr, 1, 3, 1))
+ -- force unload the buffer (this will discard changes)
+ command('new')
+ command('bunload! '..bufnr)
+ -- attempting to get lines now always gives empty list
+ eq({}, buffer('get_lines', bufnr, 1, 3, 1))
+ -- it's impossible to get out-of-bounds errors for an unloaded buffer
+ eq({}, buffer('get_lines', bufnr, 8888, 9999, 1))
+ end)
+ end)
describe('{get,set,del}_line', function()
it('works', function()
@@ -70,7 +99,6 @@ describe('api/buf', function()
end)
end)
-
describe('{get,set}_line_slice', function()
it('get_line_slice: out-of-bounds returns empty array', function()
curbuf_depr('set_line_slice', 0, 0, true, true, {'a', 'b', 'c'})
@@ -345,6 +373,31 @@ describe('api/buf', function()
end)
end)
+ describe('is_loaded', function()
+ it('works', function()
+ -- record our buffer number for when we unload it
+ local bufnr = curbuf('get_number')
+ -- api should report that the buffer is loaded
+ ok(buffer('is_loaded', bufnr))
+ -- hide the current buffer by switching to a new empty buffer
+ -- Careful! we need to modify the buffer first or vim will just reuse it
+ buffer('set_lines', bufnr, 0, -1, 1, {'line1'})
+ command('hide enew')
+ -- confirm the buffer is hidden, but still loaded
+ local infolist = nvim('eval', 'getbufinfo('..bufnr..')')
+ eq(1, #infolist)
+ eq(1, infolist[1].hidden)
+ eq(1, infolist[1].loaded)
+ -- now force unload the buffer
+ command('bunload! '..bufnr)
+ -- confirm the buffer is unloaded
+ infolist = nvim('eval', 'getbufinfo('..bufnr..')')
+ eq(0, infolist[1].loaded)
+ -- nvim_buf_is_loaded() should also report the buffer as unloaded
+ eq(false, buffer('is_loaded', bufnr))
+ end)
+ end)
+
describe('is_valid', function()
it('works', function()
nvim('command', 'new')