buffer: move BUFEMPTY to a function

This commit is contained in:
Thomas Vigouroux
2020-11-24 08:48:05 +01:00
parent 763c852812
commit 7970631fa0
14 changed files with 28 additions and 21 deletions

View File

@@ -55,7 +55,6 @@
#include "nvim/mark.h"
#include "nvim/extmark.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
@@ -139,7 +138,7 @@ read_buffer(
if (read_stdin) {
// Set or reset 'modified' before executing autocommands, so that
// it can be changed there.
if (!readonlymode && !BUFEMPTY(curbuf)) {
if (!readonlymode && !buf_is_empty(curbuf)) {
changed();
} else if (retval != FAIL) {
unchanged(curbuf, false, true);
@@ -1921,7 +1920,7 @@ bool curbuf_reusable(void)
return (curbuf != NULL
&& curbuf->b_ffname == NULL
&& curbuf->b_nwindows <= 1
&& (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY(curbuf))
&& (curbuf->b_ml.ml_mfp == NULL || buf_is_empty(curbuf))
&& !bt_quickfix(curbuf)
&& !curbufIsChanged());
}
@@ -2061,7 +2060,7 @@ int buflist_getfile(int n, linenr_T lnum, int options, int forceit)
// If 'switchbuf' contains "split", "vsplit" or "newtab" and the
// current buffer isn't empty: open new tab or window
if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
&& !BUFEMPTY(curbuf)) {
&& !buf_is_empty(curbuf)) {
if (swb_flags & SWB_NEWTAB) {
tabpage_new();
} else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
@@ -4951,7 +4950,7 @@ do_arg_all(
win_enter(lastwin, false);
// ":tab drop file" should re-use an empty window to avoid "--remote-tab"
// leaving an empty tab page when executed locally.
if (keep_tabs && BUFEMPTY(curbuf) && curbuf->b_nwindows == 1
if (keep_tabs && buf_is_empty(curbuf) && curbuf->b_nwindows == 1
&& curbuf->b_ffname == NULL && !curbuf->b_changed) {
use_firstwin = true;
tab_drop_empty_window = true;
@@ -5696,3 +5695,4 @@ void buf_open_scratch(handle_T bufnr, char *bufname)
set_option_value("swf", 0L, NULL, OPT_LOCAL);
RESET_BINDING(curwin);
}

View File

@@ -9,6 +9,7 @@
#include "nvim/func_attr.h"
#include "nvim/eval.h"
#include "nvim/macros.h"
#include "nvim/memline.h"
// Values for buflist_getfile()
enum getf_values {
@@ -128,4 +129,10 @@ static inline void buf_inc_changedtick(buf_T *const buf)
buf_set_changedtick(buf, buf_get_changedtick(buf) + 1);
}
static inline bool buf_is_empty(buf_T *buf)
{
return buf->b_ml.ml_line_count == 1
&& *ml_get_buf(buf, (linenr_T)1, false) == '\0';
}
#endif // NVIM_BUFFER_H

View File

@@ -2669,7 +2669,7 @@ void ex_diffgetput(exarg_T *eap)
}
}
buf_empty = BUFEMPTY(curbuf);
buf_empty = buf_is_empty(curbuf);
added = 0;
for (i = 0; i < count; ++i) {

View File

@@ -8047,7 +8047,7 @@ static bool ins_bs(int c, int mode, int *inserted_space_p)
// can't backup past first character in buffer
// can't backup past starting point unless 'backspace' > 1
// can backup to a previous line if 'backspace' == 0
if (BUFEMPTY(curbuf)
if (buf_is_empty(curbuf)
|| (!revins_on
&& ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
|| (!can_bs(BS_START)

View File

@@ -5027,7 +5027,7 @@ void buf_reload(buf_T *buf, int orig_mode)
// buffer contents. But if reading the file fails we should keep
// the old contents. Can't use memory only, the file might be
// too big. Use a hidden buffer to move the buffer contents to.
if (BUFEMPTY(curbuf) || saved == FAIL) {
if (buf_is_empty(curbuf) || saved == FAIL) {
savebuf = NULL;
} else {
// Allocate a buffer without putting it in the buffer list.
@@ -5060,7 +5060,7 @@ void buf_reload(buf_T *buf, int orig_mode)
if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) {
// Put the text back from the save buffer. First
// delete any lines that readfile() added.
while (!BUFEMPTY(curbuf)) {
while (!buf_is_empty(curbuf)) {
if (ml_delete(buf->b_ml.ml_line_count, false) == FAIL) {
break;
}

View File

@@ -34,10 +34,6 @@
/// LINEEMPTY() - return TRUE if the line is empty
#define LINEEMPTY(p) (*ml_get(p) == NUL)
/// BUFEMPTY() - return TRUE if the current buffer is empty
#define BUFEMPTY(buf) ((buf)->b_ml.ml_line_count == 1 \
&& *ml_get_buf((buf), (linenr_T)1, false) == '\0')
// toupper() and tolower() that use the current locale.
// Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the
// range 0 - 255. toupper()/tolower() on some systems can't handle others.

View File

@@ -1446,7 +1446,7 @@ static void read_stdin(void)
set_buflisted(true);
// Create memfile and read from stdin.
(void)open_buffer(true, NULL, 0);
if (BUFEMPTY(curbuf) && curbuf->b_next != NULL) {
if (buf_is_empty(curbuf) && curbuf->b_next != NULL) {
// stdin was empty, go to buffer 2 (e.g. "echo file1 | xargs nvim"). #8561
do_cmdline_cmd("silent! bnext");
// Delete the empty stdin buffer.

View File

@@ -17,6 +17,7 @@
#include <stdbool.h>
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/move.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
@@ -172,7 +173,7 @@ void update_topline(win_T *wp)
old_topfill = wp->w_topfill;
// If the buffer is empty, always set topline to 1.
if (BUFEMPTY(curbuf)) { // special case - file is empty
if (buf_is_empty(curbuf)) { // special case - file is empty
if (wp->w_topline != 1) {
redraw_later(wp, NOT_VALID);
}

View File

@@ -3069,7 +3069,8 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags)
}
// In an empty buffer the empty line is going to be replaced, include
// it in the saved lines.
if ((BUFEMPTY(curbuf) ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) {
if ((buf_is_empty(curbuf) ?
u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) {
goto end;
}
if (dir == FORWARD) {

View File

@@ -880,7 +880,7 @@ void set_init_3(void)
xfree(p);
}
if (BUFEMPTY(curbuf)) {
if (buf_is_empty(curbuf)) {
int idx_ffs = findoption_len(S_LEN("ffs"));
// Apply the first entry of 'fileformats' to the initial buffer.

View File

@@ -9,6 +9,7 @@
#include <inttypes.h>
#include <stdbool.h>
#include "nvim/buffer.h"
#include "nvim/vim.h"
#include "nvim/api/private/helpers.h"
#include "nvim/ascii.h"
@@ -735,7 +736,7 @@ static int pum_set_selected(int n, int repeat)
&& (curbuf->b_p_bt[2] == 'f')
&& (curbuf->b_p_bh[0] == 'w')) {
// Already a "wipeout" buffer, make it empty.
while (!BUFEMPTY(curbuf)) {
while (!buf_is_empty(curbuf)) {
ml_delete((linenr_T)1, false);
}
} else {

View File

@@ -6672,7 +6672,7 @@ void ex_spelldump(exarg_T *eap)
set_option_value("spl", dummy, (char *)spl, OPT_LOCAL);
xfree(spl);
if (!BUFEMPTY(curbuf)) {
if (!buf_is_empty(curbuf)) {
return;
}

View File

@@ -2431,7 +2431,7 @@ static void u_undoredo(int undo, bool do_buf_event)
curhead->uh_entry = newlist;
curhead->uh_flags = new_flags;
if ((old_flags & UH_EMPTYBUF) && BUFEMPTY(curbuf)) {
if ((old_flags & UH_EMPTYBUF) && buf_is_empty(curbuf)) {
curbuf->b_ml.ml_flags |= ML_EMPTY;
}
if (old_flags & UH_CHANGED) {

View File

@@ -13,6 +13,7 @@
#include "nvim/api/private/helpers.h"
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/iconv.h"
#include "nvim/version.h"
#include "nvim/charset.h"
@@ -2190,7 +2191,7 @@ void list_version(void)
/// Show the intro message when not editing a file.
void maybe_intro_message(void)
{
if (BUFEMPTY(curbuf)
if (buf_is_empty(curbuf)
&& (curbuf->b_fname == NULL)
&& (firstwin->w_next == NULL)
&& (vim_strchr(p_shm, SHM_INTRO) == NULL)) {