diff options
Diffstat (limited to 'src/nvim/memline.c')
-rw-r--r-- | src/nvim/memline.c | 1500 |
1 files changed, 793 insertions, 707 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 4ccbb31e2c..69598bbdda 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -38,29 +38,31 @@ #include <assert.h> #include <errno.h> +#include <fcntl.h> #include <inttypes.h> -#include <string.h> #include <stdbool.h> -#include <fcntl.h> +#include <string.h> #include "nvim/ascii.h" -#include "nvim/vim.h" -#include "nvim/memline.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/cursor.h" #include "nvim/eval.h" -#include "nvim/getchar.h" #include "nvim/fileio.h" #include "nvim/func_attr.h" +#include "nvim/getchar.h" #include "nvim/main.h" #include "nvim/mark.h" #include "nvim/mbyte.h" #include "nvim/memfile.h" +#include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" #include "nvim/option.h" +#include "nvim/os/input.h" +#include "nvim/os/os.h" +#include "nvim/os/process.h" #include "nvim/os_unix.h" #include "nvim/path.h" #include "nvim/screen.h" @@ -68,44 +70,42 @@ #include "nvim/spell.h" #include "nvim/strings.h" #include "nvim/ui.h" -#include "nvim/version.h" #include "nvim/undo.h" +#include "nvim/version.h" +#include "nvim/vim.h" #include "nvim/window.h" -#include "nvim/os/os.h" -#include "nvim/os/process.h" -#include "nvim/os/input.h" -#ifndef UNIX /* it's in os/unix_defs.h for Unix */ +#ifndef UNIX // it's in os/unix_defs.h for Unix # include <time.h> #endif -typedef struct block0 ZERO_BL; /* contents of the first block */ -typedef struct pointer_block PTR_BL; /* contents of a pointer block */ -typedef struct data_block DATA_BL; /* contents of a data block */ -typedef struct pointer_entry PTR_EN; /* block/line-count pair */ +typedef struct block0 ZERO_BL; // contents of the first block +typedef struct pointer_block PTR_BL; // contents of a pointer block +typedef struct data_block DATA_BL; // contents of a data block +typedef struct pointer_entry PTR_EN; // block/line-count pair -#define DATA_ID (('d' << 8) + 'a') /* data block id */ -#define PTR_ID (('p' << 8) + 't') /* pointer block id */ -#define BLOCK0_ID0 'b' /* block 0 id 0 */ -#define BLOCK0_ID1 '0' /* block 0 id 1 */ +#define DATA_ID (('d' << 8) + 'a') // data block id +#define PTR_ID (('p' << 8) + 't') // pointer block id +#define BLOCK0_ID0 'b' // block 0 id 0 +#define BLOCK0_ID1 '0' // block 0 id 1 /* * pointer to a block, used in a pointer block */ struct pointer_entry { - blocknr_T pe_bnum; /* block number */ - linenr_T pe_line_count; /* number of lines in this branch */ - linenr_T pe_old_lnum; /* lnum for this block (for recovery) */ - int pe_page_count; /* number of pages in block pe_bnum */ + blocknr_T pe_bnum; // block number + linenr_T pe_line_count; // number of lines in this branch + linenr_T pe_old_lnum; // lnum for this block (for recovery) + int pe_page_count; // number of pages in block pe_bnum }; /* * A pointer block contains a list of branches in the tree. */ struct pointer_block { - uint16_t pb_id; /* ID for pointer block: PTR_ID */ - uint16_t pb_count; /* number of pointers in this block */ - uint16_t pb_count_max; /* maximum value for pb_count */ + uint16_t pb_id; // ID for pointer block: PTR_ID + uint16_t pb_count; // number of pointers in this block + uint16_t pb_count_max; // maximum value for pb_count PTR_EN pb_pointer[1]; /* list of pointers to blocks (actually longer) * followed by empty space until end of page */ }; @@ -140,12 +140,12 @@ struct data_block { #define DB_MARKED ((unsigned)1 << ((sizeof(unsigned) * 8) - 1)) #define DB_INDEX_MASK (~DB_MARKED) -#define INDEX_SIZE (sizeof(unsigned)) /* size of one db_index entry */ -#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) /* size of data block header */ +#define INDEX_SIZE (sizeof(unsigned)) // size of one db_index entry +#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) // size of data block header -#define B0_FNAME_SIZE_ORG 900 /* what it was in older versions */ -#define B0_FNAME_SIZE_NOCRYPT 898 /* 2 bytes used for other things */ -#define B0_FNAME_SIZE_CRYPT 890 /* 10 bytes used for other things */ +#define B0_FNAME_SIZE_ORG 900 // what it was in older versions +#define B0_FNAME_SIZE_NOCRYPT 898 // 2 bytes used for other things +#define B0_FNAME_SIZE_CRYPT 890 // 10 bytes used for other things #define B0_UNAME_SIZE 40 #define B0_HNAME_SIZE 40 /* @@ -172,18 +172,18 @@ struct data_block { */ struct block0 { char_u b0_id[2]; ///< ID for block 0: BLOCK0_ID0 and BLOCK0_ID1. - char_u b0_version[10]; /* Vim version string */ - char_u b0_page_size[4]; /* number of bytes per page */ - char_u b0_mtime[4]; /* last modification time of file */ - char_u b0_ino[4]; /* inode of b0_fname */ - char_u b0_pid[4]; /* process id of creator (or 0) */ - char_u b0_uname[B0_UNAME_SIZE]; /* name of user (uid if no name) */ - char_u b0_hname[B0_HNAME_SIZE]; /* host name (if it has a name) */ - char_u b0_fname[B0_FNAME_SIZE_ORG]; /* name of file being edited */ - long b0_magic_long; /* check for byte order of long */ - int b0_magic_int; /* check for byte order of int */ - short b0_magic_short; /* check for byte order of short */ - char_u b0_magic_char; /* check for last char */ + char_u b0_version[10]; // Vim version string + char_u b0_page_size[4]; // number of bytes per page + char_u b0_mtime[4]; // last modification time of file + char_u b0_ino[4]; // inode of b0_fname + char_u b0_pid[4]; // process id of creator (or 0) + char_u b0_uname[B0_UNAME_SIZE]; // name of user (uid if no name) + char_u b0_hname[B0_HNAME_SIZE]; // host name (if it has a name) + char_u b0_fname[B0_FNAME_SIZE_ORG]; // name of file being edited + long b0_magic_long; // check for byte order of long + int b0_magic_int; // check for byte order of int + short b0_magic_short; // check for byte order of short + char_u b0_magic_char; // check for last char }; /* @@ -213,7 +213,7 @@ struct block0 { * When empty there is only the NUL. */ #define B0_HAS_FENC 8 -#define STACK_INCR 5 /* nr of entries added to ml_stack at a time */ +#define STACK_INCR 5 // nr of entries added to ml_stack at a time /* * The line number where the first mark may be is remembered. @@ -226,16 +226,16 @@ static linenr_T lowest_marked = 0; /* * arguments for ml_find_line() */ -#define ML_DELETE 0x11 /* delete line */ -#define ML_INSERT 0x12 /* insert line */ -#define ML_FIND 0x13 /* just find the line */ -#define ML_FLUSH 0x02 /* flush locked block */ -#define ML_SIMPLE(x) (x & 0x10) /* DEL, INS or FIND */ +#define ML_DELETE 0x11 // delete line +#define ML_INSERT 0x12 // insert line +#define ML_FIND 0x13 // just find the line +#define ML_FLUSH 0x02 // flush locked block +#define ML_SIMPLE(x) (x & 0x10) // DEL, INS or FIND -/* argument for ml_upd_block0() */ +// argument for ml_upd_block0() typedef enum { - UB_FNAME = 0 /* update timestamp and filename */ - , UB_SAME_DIR /* update the B0_SAME_DIR flag */ + UB_FNAME = 0 // update timestamp and filename + , UB_SAME_DIR // update the B0_SAME_DIR flag } upd_block0_T; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -249,10 +249,10 @@ typedef enum { */ int ml_open(buf_T *buf) { - bhdr_T *hp = NULL; - ZERO_BL *b0p; - PTR_BL *pp; - DATA_BL *dp; + bhdr_T *hp = NULL; + ZERO_BL *b0p; + PTR_BL *pp; + DATA_BL *dp; /* * init fields in memline struct @@ -307,7 +307,7 @@ int ml_open(buf_T *buf) b0p->b0_magic_int = (int)B0_MAGIC_INT; b0p->b0_magic_short = (short)B0_MAGIC_SHORT; b0p->b0_magic_char = B0_MAGIC_CHAR; - xstrlcpy(xstpcpy((char *) b0p->b0_version, "VIM "), Version, 6); + xstrlcpy(xstpcpy((char *)b0p->b0_version, "VIM "), Version, 6); long_to_char((long)mfp->mf_page_size, b0p->b0_page_size); if (!buf->b_spell) { @@ -329,14 +329,16 @@ int ml_open(buf_T *buf) * is created. */ mf_put(mfp, hp, true, false); - if (!buf->b_help && !B_SPELL(buf)) + if (!buf->b_help && !B_SPELL(buf)) { (void)mf_sync(mfp, 0); + } /* * Fill in root pointer block and write page 1. */ - if ((hp = ml_new_ptr(mfp)) == NULL) + if ((hp = ml_new_ptr(mfp)) == NULL) { goto error; + } if (hp->bh_bnum != 1) { IEMSG(_("E298: Didn't get block nr 1?")); goto error; @@ -346,7 +348,7 @@ int ml_open(buf_T *buf) pp->pb_pointer[0].pe_bnum = 2; pp->pb_pointer[0].pe_page_count = 1; pp->pb_pointer[0].pe_old_lnum = 1; - pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */ + pp->pb_pointer[0].pe_line_count = 1; // line count after insertion mf_put(mfp, hp, true, false); /* @@ -359,10 +361,10 @@ int ml_open(buf_T *buf) } dp = hp->bh_data; - dp->db_index[0] = --dp->db_txt_start; /* at end of block */ + dp->db_index[0] = --dp->db_txt_start; // at end of block dp->db_free -= 1 + INDEX_SIZE; dp->db_line_count = 1; - *((char_u *)dp + dp->db_txt_start) = NUL; /* empty line */ + *((char_u *)dp + dp->db_txt_start) = NUL; // empty line return OK; @@ -384,18 +386,18 @@ error: void ml_setname(buf_T *buf) { bool success = false; - memfile_T *mfp; - char_u *fname; - char_u *dirp; + memfile_T *mfp; + char_u *fname; + char_u *dirp; mfp = buf->b_ml.ml_mfp; - if (mfp->mf_fd < 0) { /* there is no swap file yet */ + if (mfp->mf_fd < 0) { // there is no swap file yet /* * When 'updatecount' is 0 and 'noswapfile' there is no swap file. * For help files we will make a swap file now. */ if (p_uc != 0 && !cmdmod.noswapfile) { - ml_open_file(buf); /* create a swap file */ + ml_open_file(buf); // create a swap file } return; } @@ -406,29 +408,32 @@ void ml_setname(buf_T *buf) dirp = p_dir; bool found_existing_dir = false; for (;; ) { - if (*dirp == NUL) /* tried all directories, fail */ + if (*dirp == NUL) { // tried all directories, fail break; + } fname = (char_u *)findswapname(buf, (char **)&dirp, (char *)mfp->mf_fname, &found_existing_dir); - /* alloc's fname */ - if (dirp == NULL) /* out of memory */ + // alloc's fname + if (dirp == NULL) { // out of memory break; - if (fname == NULL) /* no file name found for this dir */ + } + if (fname == NULL) { // no file name found for this dir continue; + } - /* if the file name is the same we don't have to do anything */ + // if the file name is the same we don't have to do anything if (fnamecmp(fname, mfp->mf_fname) == 0) { xfree(fname); success = true; break; } - /* need to close the swap file before renaming */ + // need to close the swap file before renaming if (mfp->mf_fd >= 0) { close(mfp->mf_fd); mfp->mf_fd = -1; } - /* try to rename the swap file */ + // try to rename the swap file if (vim_rename(mfp->mf_fname, fname) == 0) { success = true; mf_free_fnames(mfp); @@ -436,20 +441,21 @@ void ml_setname(buf_T *buf) ml_upd_block0(buf, UB_SAME_DIR); break; } - xfree(fname); /* this fname didn't work, try another */ + xfree(fname); // this fname didn't work, try another } - if (mfp->mf_fd == -1) { /* need to (re)open the swap file */ + if (mfp->mf_fd == -1) { // need to (re)open the swap file mfp->mf_fd = os_open((char *)mfp->mf_fname, O_RDWR, 0); if (mfp->mf_fd < 0) { - /* could not (re)open the swap file, what can we do???? */ + // could not (re)open the swap file, what can we do???? EMSG(_("E301: Oops, lost the swap file!!!")); return; } (void)os_set_cloexec(mfp->mf_fd); } - if (!success) + if (!success) { EMSG(_("E302: Could not rename swap file")); + } } /* @@ -473,21 +479,22 @@ void ml_open_files(void) */ void ml_open_file(buf_T *buf) { - memfile_T *mfp; - char_u *fname; - char_u *dirp; + memfile_T *mfp; + char_u *fname; + char_u *dirp; mfp = buf->b_ml.ml_mfp; if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf || cmdmod.noswapfile || buf->terminal) { - return; /* nothing to do */ + return; // nothing to do } - /* For a spell buffer use a temp file name. */ + // For a spell buffer use a temp file name. if (buf->b_spell) { fname = vim_tempname(); - if (fname != NULL) - (void)mf_open_file(mfp, fname); /* consumes fname! */ + if (fname != NULL) { + (void)mf_open_file(mfp, fname); // consumes fname! + } buf->b_may_swap = false; return; } @@ -498,21 +505,24 @@ void ml_open_file(buf_T *buf) dirp = p_dir; bool found_existing_dir = false; for (;; ) { - if (*dirp == NUL) + if (*dirp == NUL) { break; + } // There is a small chance that between choosing the swap file name // and creating it, another Vim creates the file. In that case the // creation will fail and we will use another directory. fname = (char_u *)findswapname(buf, (char **)&dirp, NULL, &found_existing_dir); - if (dirp == NULL) - break; /* out of memory */ - if (fname == NULL) + if (dirp == NULL) { + break; // out of memory + } + if (fname == NULL) { continue; - if (mf_open_file(mfp, fname) == OK) { /* consumes fname! */ + } + if (mf_open_file(mfp, fname) == OK) { // consumes fname! ml_upd_block0(buf, UB_SAME_DIR); - /* Flush block zero, so others can read it */ + // Flush block zero, so others can read it if (mf_sync(mfp, MFS_ZERO) == OK) { /* Mark all blocks that should be in the swapfile as dirty. * Needed for when the 'swapfile' option was reset, so that @@ -520,7 +530,7 @@ void ml_open_file(buf_T *buf) mf_set_dirty(mfp); break; } - /* Writing block 0 failed: close the file and try another dir */ + // Writing block 0 failed: close the file and try another dir mf_close_file(buf, false); } } @@ -528,13 +538,12 @@ void ml_open_file(buf_T *buf) if (*p_dir != NUL && mfp->mf_fname == NULL) { need_wait_return = true; // call wait_return later no_wait_return++; - (void)EMSG2(_( - "E303: Unable to open swap file for \"%s\", recovery impossible"), - buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname); + (void)EMSG2(_("E303: Unable to open swap file for \"%s\", recovery impossible"), + buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname); --no_wait_return; } - /* don't try to open a swap file again */ + // don't try to open a swap file again buf->b_may_swap = false; } @@ -560,11 +569,13 @@ void check_need_swap(bool newfile) */ void ml_close(buf_T *buf, int del_file) { - if (buf->b_ml.ml_mfp == NULL) /* not open */ + if (buf->b_ml.ml_mfp == NULL) { // not open return; - mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */ - if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) + } + mf_close(buf->b_ml.ml_mfp, del_file); // close the .swp file + if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) { xfree(buf->b_ml.ml_line_ptr); + } xfree(buf->b_ml.ml_stack); XFREE_CLEAR(buf->b_ml.ml_chunksize); buf->b_ml.ml_mfp = NULL; @@ -585,8 +596,8 @@ void ml_close_all(int del_file) FOR_ALL_BUFFERS(buf) { ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0)); } - spell_delete_wordlist(); /* delete the internal wordlist */ - vim_deltempdir(); /* delete created temp directory */ + spell_delete_wordlist(); // delete the internal wordlist + vim_deltempdir(); // delete created temp directory } /* @@ -597,7 +608,7 @@ void ml_close_notmod(void) { FOR_ALL_BUFFERS(buf) { if (!bufIsChanged(buf)) { - ml_close(buf, TRUE); /* close all not-modified buffers */ + ml_close(buf, TRUE); // close all not-modified buffers } } } @@ -633,13 +644,14 @@ static bool ml_check_b0_strings(ZERO_BL *b0p) */ static void ml_upd_block0(buf_T *buf, upd_block0_T what) { - memfile_T *mfp; - bhdr_T *hp; - ZERO_BL *b0p; + memfile_T *mfp; + bhdr_T *hp; + ZERO_BL *b0p; mfp = buf->b_ml.ml_mfp; - if (mfp == NULL || (hp = mf_get(mfp, 0, 1)) == NULL) + if (mfp == NULL || (hp = mf_get(mfp, 0, 1)) == NULL) { return; + } b0p = hp->bh_data; if (ml_check_b0_id(b0p) == FAIL) { IEMSG(_("E304: ml_upd_block0(): Didn't get block 0??")); @@ -660,9 +672,9 @@ static void ml_upd_block0(buf_T *buf, upd_block0_T what) */ static void set_b0_fname(ZERO_BL *b0p, buf_T *buf) { - if (buf->b_ffname == NULL) + if (buf->b_ffname == NULL) { b0p->b0_fname[0] = NUL; - else { + } else { char uname[B0_UNAME_SIZE]; /* @@ -673,9 +685,9 @@ static void set_b0_fname(ZERO_BL *b0p, buf_T *buf) * Then insert the user name to get "~user/". */ home_replace(NULL, buf->b_ffname, b0p->b0_fname, - B0_FNAME_SIZE_CRYPT, TRUE); + B0_FNAME_SIZE_CRYPT, TRUE); if (b0p->b0_fname[0] == '~') { - /* If there is no user name or it is too long, don't use "~/" */ + // If there is no user name or it is too long, don't use "~/" int retval = os_get_user_name(uname, B0_UNAME_SIZE); size_t ulen = STRLEN(uname); size_t flen = STRLEN(b0p->b0_fname); @@ -702,7 +714,7 @@ static void set_b0_fname(ZERO_BL *b0p, buf_T *buf) } } - /* Also add the 'fileencoding' if there is room. */ + // Also add the 'fileencoding' if there is room. add_b0_fenc(b0p, curbuf); } @@ -714,10 +726,11 @@ static void set_b0_fname(ZERO_BL *b0p, buf_T *buf) */ static void set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf) { - if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname)) + if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname)) { b0p->b0_flags |= B0_SAME_DIR; - else + } else { b0p->b0_flags &= ~B0_SAME_DIR; + } } /* @@ -729,11 +742,11 @@ static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf) int size = B0_FNAME_SIZE_NOCRYPT; n = (int)STRLEN(buf->b_p_fenc); - if ((int)STRLEN(b0p->b0_fname) + n + 1 > size) + if ((int)STRLEN(b0p->b0_fname) + n + 1 > size) { b0p->b0_flags &= ~B0_HAS_FENC; - else { + } else { memmove((char *)b0p->b0_fname + size - n, - (char *)buf->b_p_fenc, (size_t)n); + (char *)buf->b_p_fenc, (size_t)n); *(b0p->b0_fname + size - n - 1) = NUL; b0p->b0_flags |= B0_HAS_FENC; } @@ -745,23 +758,23 @@ static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf) /// swap file. void ml_recover(bool checkext) { - buf_T *buf = NULL; - memfile_T *mfp = NULL; - char_u *fname; - char_u *fname_used = NULL; - bhdr_T *hp = NULL; - ZERO_BL *b0p; + buf_T *buf = NULL; + memfile_T *mfp = NULL; + char_u *fname; + char_u *fname_used = NULL; + bhdr_T *hp = NULL; + ZERO_BL *b0p; int b0_ff; - char_u *b0_fenc = NULL; - PTR_BL *pp; - DATA_BL *dp; - infoptr_T *ip; + char_u *b0_fenc = NULL; + PTR_BL *pp; + DATA_BL *dp; + infoptr_T *ip; blocknr_T bnum; int page_count; int len; bool directly; linenr_T lnum; - char_u *p; + char_u *p; int i; long error; bool cannot_open; @@ -784,8 +797,9 @@ void ml_recover(bool checkext) // If the file name ends in ".s[a-w][a-z]" we assume this is the swap file. // Otherwise a search is done to find the swap file(s). fname = curbuf->b_fname; - if (fname == NULL) /* When there is no file name */ + if (fname == NULL) { // When there is no file name fname = (char_u *)""; + } len = (int)STRLEN(fname); if (checkext && len >= 4 && STRNICMP(fname + len - 4, ".s", 2) == 0 @@ -797,32 +811,34 @@ void ml_recover(bool checkext) } else { directly = false; - /* count the number of matching swap files */ + // count the number of matching swap files len = recover_names(fname, FALSE, 0, NULL); - if (len == 0) { /* no swap files found */ + if (len == 0) { // no swap files found EMSG2(_("E305: No swap file found for %s"), fname); goto theend; } - if (len == 1) /* one swap file found, use it */ + if (len == 1) { // one swap file found, use it i = 1; - else { /* several swap files found, choose */ - /* list the names of the swap files */ + } else { // several swap files found, choose + // list the names of the swap files (void)recover_names(fname, TRUE, 0, NULL); msg_putchar('\n'); MSG_PUTS(_("Enter number of swap file to use (0 to quit): ")); i = get_number(FALSE, NULL); - if (i < 1 || i > len) + if (i < 1 || i > len) { goto theend; + } } - /* get the swap file name that will be used */ + // get the swap file name that will be used (void)recover_names(fname, FALSE, i, &fname_used); } - if (fname_used == NULL) + if (fname_used == NULL) { goto theend; // user chose invalid number. - - /* When called from main() still need to initialize storage structure */ - if (called_from_main && ml_open(curbuf) == FAIL) + } + // When called from main() still need to initialize storage structure + if (called_from_main && ml_open(curbuf) == FAIL) { getout(1); + } /* * Allocate a buffer structure for the swap file that is used for recovery. @@ -869,9 +885,8 @@ void ml_recover(bool checkext) msg_start(); MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST); msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); - MSG_PUTS_ATTR(_( - "\nMaybe no changes were made or Vim did not update the swap file."), - attr | MSG_HIST); + MSG_PUTS_ATTR(_("\nMaybe no changes were made or Vim did not update the swap file."), + attr | MSG_HIST); msg_end(); goto theend; } @@ -880,7 +895,7 @@ void ml_recover(bool checkext) msg_start(); msg_outtrans_attr(mfp->mf_fname, MSG_HIST); MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), - MSG_HIST); + MSG_HIST); MSG_PUTS_ATTR(_("Use Vim version 3.0.\n"), MSG_HIST); msg_end(); goto theend; @@ -893,9 +908,9 @@ void ml_recover(bool checkext) msg_start(); msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); MSG_PUTS_ATTR(_(" cannot be used on this computer.\n"), - attr | MSG_HIST); + attr | MSG_HIST); MSG_PUTS_ATTR(_("The file was created on "), attr | MSG_HIST); - /* avoid going past the end of a corrupted hostname */ + // avoid going past the end of a corrupted hostname b0p->b0_fname[0] = NUL; MSG_PUTS_ATTR(b0p->b0_hname, attr | MSG_HIST); MSG_PUTS_ATTR(_(",\nor the file has been damaged."), attr | MSG_HIST); @@ -914,9 +929,8 @@ void ml_recover(bool checkext) if (mfp->mf_page_size < previous_page_size) { msg_start(); msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); - MSG_PUTS_ATTR(_( - " has been damaged (page size is smaller than minimum value).\n"), - attr | MSG_HIST); + MSG_PUTS_ATTR(_(" has been damaged (page size is smaller than minimum value).\n"), + attr | MSG_HIST); msg_end(); goto theend; } @@ -927,7 +941,7 @@ void ml_recover(bool checkext) } mfp->mf_infile_count = mfp->mf_blocknr_max; - /* need to reallocate the memory used to store the data */ + // need to reallocate the memory used to store the data p = xmalloc(mfp->mf_page_size); memmove(p, hp->bh_data, previous_page_size); xfree(hp->bh_data); @@ -948,10 +962,11 @@ void ml_recover(bool checkext) home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE); smsg(_("Using swap file \"%s\""), NameBuff); - if (buf_spname(curbuf) != NULL) + if (buf_spname(curbuf) != NULL) { STRLCPY(NameBuff, buf_spname(curbuf), MAXPATHL); - else + } else { home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE); + } smsg(_("Original file \"%s\""), NameBuff); msg_putchar('\n'); @@ -965,13 +980,13 @@ void ml_recover(bool checkext) && os_fileinfo((char *)curbuf->b_ffname, &org_file_info) && ((os_fileinfo((char *)mfp->mf_fname, &swp_file_info) && org_file_info.stat.st_mtim.tv_sec - > swp_file_info.stat.st_mtim.tv_sec) + > swp_file_info.stat.st_mtim.tv_sec) || org_file_info.stat.st_mtim.tv_sec != mtime)) { EMSG(_("E308: Warning: Original file may have been changed")); } ui_flush(); - /* Get the 'fileformat' and 'fileencoding' from block zero. */ + // Get the 'fileformat' and 'fileencoding' from block zero. b0_ff = (b0p->b0_flags & B0_FF_MASK); if (b0p->b0_flags & B0_HAS_FENC) { int fnsize = B0_FNAME_SIZE_NOCRYPT; @@ -981,7 +996,7 @@ void ml_recover(bool checkext) b0_fenc = vim_strnsave(p, b0p->b0_fname + fnsize - p); } - mf_put(mfp, hp, false, false); /* release block 0 */ + mf_put(mfp, hp, false, false); // release block 0 hp = NULL; /* @@ -996,28 +1011,30 @@ void ml_recover(bool checkext) * Try reading the original file to obtain the values of 'fileformat', * 'fileencoding', etc. Ignore errors. The text itself is not used. */ - if (curbuf->b_ffname != NULL) + if (curbuf->b_ffname != NULL) { orig_file_status = readfile(curbuf->b_ffname, NULL, (linenr_T)0, - (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW); + (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW); + } - /* Use the 'fileformat' and 'fileencoding' as stored in the swap file. */ - if (b0_ff != 0) + // Use the 'fileformat' and 'fileencoding' as stored in the swap file. + if (b0_ff != 0) { set_fileformat(b0_ff - 1, OPT_LOCAL); + } if (b0_fenc != NULL) { set_option_value("fenc", 0L, (char *)b0_fenc, OPT_LOCAL); xfree(b0_fenc); } unchanged(curbuf, true, true); - bnum = 1; /* start with block 1 */ - page_count = 1; /* which is 1 page */ - lnum = 0; /* append after line 0 in curbuf */ + bnum = 1; // start with block 1 + page_count = 1; // which is 1 page + lnum = 0; // append after line 0 in curbuf line_count = 0; - idx = 0; /* start with first index in block 1 */ + idx = 0; // start with first index in block 1 error = 0; buf->b_ml.ml_stack_top = 0; buf->b_ml.ml_stack = NULL; - buf->b_ml.ml_stack_size = 0; /* no stack yet */ + buf->b_ml.ml_stack_size = 0; // no stack yet if (curbuf->b_ffname == NULL) { cannot_open = true; @@ -1027,9 +1044,9 @@ void ml_recover(bool checkext) serious_error = false; for (; !got_int; line_breakcheck()) { - if (hp != NULL) - mf_put(mfp, hp, false, false); /* release previous block */ - + if (hp != NULL) { + mf_put(mfp, hp, false, false); // release previous block + } /* * get block */ @@ -1043,11 +1060,12 @@ void ml_recover(bool checkext) (colnr_T)0, true); } else { // there is a block pp = hp->bh_data; - if (pp->pb_id == PTR_ID) { /* it is a pointer block */ - /* check line count when using pointer block first time */ + if (pp->pb_id == PTR_ID) { // it is a pointer block + // check line count when using pointer block first time if (idx == 0 && line_count != 0) { - for (i = 0; i < (int)pp->pb_count; ++i) + for (i = 0; i < (int)pp->pb_count; ++i) { line_count -= pp->pb_pointer[i].pe_line_count; + } if (line_count != 0) { ++error; ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"), @@ -1081,7 +1099,7 @@ void ml_recover(bool checkext) ml_append(lnum++, (char_u *)_("???LINES MISSING"), (colnr_T)0, true); } - ++idx; /* get same block again for next index */ + ++idx; // get same block again for next index continue; } @@ -1099,12 +1117,12 @@ void ml_recover(bool checkext) idx = 0; continue; } - } else { /* not a pointer block */ + } else { // not a pointer block dp = hp->bh_data; - if (dp->db_id != DATA_ID) { /* block id wrong */ + if (dp->db_id != DATA_ID) { // block id wrong if (bnum == 1) { EMSG2(_("E310: Block 1 ID wrong (%s not a .swp file?)"), - mfp->mf_fname); + mfp->mf_fname); goto theend; } ++error; @@ -1117,17 +1135,16 @@ void ml_recover(bool checkext) // check length of block // if wrong, use length in pointer block if (page_count * mfp->mf_page_size != dp->db_txt_end) { - ml_append( - lnum++, - (char_u *)_("??? from here until ???END lines" - " may be messed up"), - (colnr_T)0, true); + ml_append(lnum++, + (char_u *)_("??? from here until ???END lines" + " may be messed up"), + (colnr_T)0, true); error++; has_error = true; dp->db_txt_end = page_count * mfp->mf_page_size; } - /* make sure there is a NUL at the end of the block */ + // make sure there is a NUL at the end of the block *((char_u *)dp + dp->db_txt_end - 1) = NUL; /* @@ -1135,11 +1152,10 @@ void ml_recover(bool checkext) * if wrong, use count in data block */ if (line_count != dp->db_line_count) { - ml_append( - lnum++, - (char_u *)_("??? from here until ???END lines" - " may have been inserted/deleted"), - (colnr_T)0, true); + ml_append(lnum++, + (char_u *)_("??? from here until ???END lines" + " may have been inserted/deleted"), + (colnr_T)0, true); error++; has_error = true; } @@ -1150,8 +1166,9 @@ void ml_recover(bool checkext) || txt_start >= (int)dp->db_txt_end) { p = (char_u *)"???"; ++error; - } else + } else { p = (char_u *)dp + txt_start; + } ml_append(lnum++, p, (colnr_T)0, true); } if (has_error) { @@ -1161,15 +1178,16 @@ void ml_recover(bool checkext) } } - if (buf->b_ml.ml_stack_top == 0) /* finished */ + if (buf->b_ml.ml_stack_top == 0) { // finished break; + } /* * go one block up in the tree */ ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]); bnum = ip->ip_bnum; - idx = ip->ip_index + 1; /* go to next index */ + idx = ip->ip_index + 1; // go to next index page_count = 1; } @@ -1189,7 +1207,7 @@ void ml_recover(bool checkext) } } else { for (idx = 1; idx <= lnum; ++idx) { - /* Need to copy one line, fetching the other one may flush it. */ + // Need to copy one line, fetching the other one may flush it. p = vim_strsave(ml_get(idx)); i = STRCMP(p, ml_get(idx + lnum)); xfree(p); @@ -1206,30 +1224,30 @@ void ml_recover(bool checkext) * empty buffer. These will now be after the last line in the buffer. */ while (curbuf->b_ml.ml_line_count > lnum - && !(curbuf->b_ml.ml_flags & ML_EMPTY)) + && !(curbuf->b_ml.ml_flags & ML_EMPTY)) { ml_delete(curbuf->b_ml.ml_line_count, false); + } curbuf->b_flags |= BF_RECOVERED; check_cursor(); recoverymode = FALSE; - if (got_int) + if (got_int) { EMSG(_("E311: Recovery Interrupted")); - else if (error) { + } else if (error) { ++no_wait_return; MSG(">>>>>>>>>>>>>"); - EMSG(_( - "E312: Errors detected while recovering; look for lines starting with ???")); + EMSG(_( "E312: Errors detected while recovering; look for lines starting with ???")); --no_wait_return; MSG(_("See \":help E312\" for more information.")); MSG(">>>>>>>>>>>>>"); } else { if (curbuf->b_changed) { MSG(_("Recovery completed. You should check if everything is OK.")); - MSG_PUTS(_( - "\n(You might want to write out this file under another name\n")); + MSG_PUTS(_("\n(You might want to write out this file under another name\n")); MSG_PUTS(_("and run diff with the original file to check for changes)")); - } else + } else { MSG(_("Recovery completed. Buffer contents equals file contents.")); + } MSG_PUTS(_("\nYou may want to delete the .swp file now.\n\n")); cmdline_row = msg_row; } @@ -1239,51 +1257,49 @@ theend: xfree(fname_used); recoverymode = FALSE; if (mfp != NULL) { - if (hp != NULL) + if (hp != NULL) { mf_put(mfp, hp, false, false); - mf_close(mfp, false); /* will also xfree(mfp->mf_fname) */ + } + mf_close(mfp, false); // will also xfree(mfp->mf_fname) } if (buf != NULL) { //may be NULL if swap file not found. xfree(buf->b_ml.ml_stack); xfree(buf); } - if (serious_error && called_from_main) + if (serious_error && called_from_main) { ml_close(curbuf, TRUE); - else { + } else { apply_autocmds(EVENT_BUFREADPOST, NULL, curbuf->b_fname, FALSE, curbuf); apply_autocmds(EVENT_BUFWINENTER, NULL, curbuf->b_fname, FALSE, curbuf); } return; } -/* - * Find the names of swap files in current directory and the directory given - * with the 'directory' option. - * - * Used to: - * - list the swap files for "vim -r" - * - count the number of swap files when recovering - * - list the swap files when recovering - * - find the name of the n'th swap file when recovering - */ -int -recover_names ( - char_u *fname, /* base for swap file name */ - int list, /* when TRUE, list the swap file names */ - int nr, /* when non-zero, return nr'th swap file name */ - char_u **fname_out /* result when "nr" > 0 */ -) +/// Find the names of swap files in current directory and the directory given +/// with the 'directory' option. +/// +/// Used to: +/// - list the swap files for "vim -r" +/// - count the number of swap files when recovering +/// - list the swap files when recovering +/// - find the name of the n'th swap file when recovering +/// +/// @param fname base for swap file name +/// @param list when TRUE, list the swap file names +/// @param nr when non-zero, return nr'th swap file name +/// @param fname_out result when "nr" > 0 +int recover_names(char_u *fname, int list, int nr, char_u **fname_out) { int num_names; - char_u *(names[6]); - char_u *tail; - char_u *p; + char_u *(names[6]); + char_u *tail; + char_u *p; int num_files; int file_count = 0; - char_u **files; - char_u *dirp; - char_u *dir_name; - char_u *fname_res = NULL; + char_u **files; + char_u *dirp; + char_u *dir_name; + char_u *fname_res = NULL; #ifdef HAVE_READLINK char_u fname_buf[MAXPATHL]; #endif @@ -1292,15 +1308,15 @@ recover_names ( #ifdef HAVE_READLINK /* Expand symlink in the file name, because the swap file is created * with the actual file instead of with the symlink. */ - if (resolve_symlink(fname, fname_buf) == OK) + if (resolve_symlink(fname, fname_buf) == OK) { fname_res = fname_buf; - else + } else #endif fname_res = fname; } if (list) { - /* use msg() to start the scrolling properly */ + // use msg() to start the scrolling properly msg((char_u *)_("Swap files found:")); msg_putchar('\n'); } @@ -1315,7 +1331,7 @@ recover_names ( // Advance dirp to next directory name. (void)copy_option_part(&dirp, dir_name, 31000, ","); - if (dir_name[0] == '.' && dir_name[1] == NUL) { /* check current dir */ + if (dir_name[0] == '.' && dir_name[1] == NUL) { // check current dir if (fname == NULL) { names[0] = vim_strsave((char_u *)"*.sw?"); /* For Unix names starting with a dot are special. MS-Windows @@ -1323,9 +1339,10 @@ recover_names ( names[1] = vim_strsave((char_u *)".*.sw?"); names[2] = vim_strsave((char_u *)".sw?"); num_names = 3; - } else + } else { num_names = recov_file_names(names, fname_res, TRUE); - } else { /* check directory dir_name */ + } + } else { // check directory dir_name if (fname == NULL) { names[0] = (char_u *)concat_fnames((char *)dir_name, "*.sw?", TRUE); /* For Unix names starting with a dot are special. MS-Windows @@ -1351,11 +1368,12 @@ recover_names ( } } - if (num_names == 0) + if (num_names == 0) { num_files = 0; - else if (expand_wildcards(num_names, names, &num_files, &files, - EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL) + } else if (expand_wildcards(num_names, names, &num_files, &files, + EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL) { num_files = 0; + } /* * When no swap file found, wildcard expansion might have failed (e.g. @@ -1388,27 +1406,29 @@ recover_names ( // down. When the array becomes empty free it here, since // FreeWild() won't be called below. xfree(files[i]); - if (--num_files == 0) + if (--num_files == 0) { xfree(files); - else - for (; i < num_files; ++i) + } else { + for (; i < num_files; ++i) { files[i] = files[i + 1]; + } + } } } } if (nr > 0) { file_count += num_files; if (nr <= file_count) { - *fname_out = vim_strsave( - files[nr - 1 + num_files - file_count]); - dirp = (char_u *)""; /* stop searching */ + *fname_out = vim_strsave(files[nr - 1 + num_files - file_count]); + dirp = (char_u *)""; // stop searching } } else if (list) { if (dir_name[0] == '.' && dir_name[1] == NUL) { - if (fname == NULL) + if (fname == NULL) { MSG_PUTS(_(" In current directory:\n")); - else + } else { MSG_PUTS(_(" Using specified name:\n")); + } } else { MSG_PUTS(_(" In directory ")); msg_home_replace(dir_name); @@ -1417,23 +1437,27 @@ recover_names ( if (num_files) { for (int i = 0; i < num_files; ++i) { - /* print the swap file name */ + // print the swap file name msg_outnum((long)++file_count); msg_puts(". "); msg_puts((const char *)path_tail(files[i])); msg_putchar('\n'); (void)swapfile_info(files[i]); } - } else + } else { MSG_PUTS(_(" -- none --\n")); + } ui_flush(); - } else + } else { file_count += num_files; + } - for (int i = 0; i < num_names; ++i) + for (int i = 0; i < num_names; ++i) { xfree(names[i]); - if (num_files > 0) + } + if (num_files > 0) { FreeWild(num_files, files); + } } xfree(dir_name); return file_count; @@ -1544,10 +1568,11 @@ static time_t swapfile_info(char_u *fname) MSG_PUTS(_(" [garbled strings (not nul terminated)]")); } else { MSG_PUTS(_(" file name: ")); - if (b0.b0_fname[0] == NUL) + if (b0.b0_fname[0] == NUL) { MSG_PUTS(_("[No Name]")); - else + } else { msg_outtrans(b0.b0_fname); + } MSG_PUTS(_("\n modified: ")); MSG_PUTS(b0.b0_dirty ? _("YES") : _("no")); @@ -1558,10 +1583,11 @@ static time_t swapfile_info(char_u *fname) } if (*(b0.b0_hname) != NUL) { - if (*(b0.b0_uname) != NUL) + if (*(b0.b0_uname) != NUL) { MSG_PUTS(_(" host name: ")); - else + } else { MSG_PUTS(_("\n host name: ")); + } msg_outtrans(b0.b0_hname); } @@ -1578,11 +1604,13 @@ static time_t swapfile_info(char_u *fname) MSG_PUTS(_("\n [not usable on this computer]")); } } - } else + } else { MSG_PUTS(_(" [cannot be read]")); + } close(fd); - } else + } else { MSG_PUTS(_(" [cannot be opened]")); + } msg_putchar('\n'); return x; @@ -1650,18 +1678,20 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot) // Form the normal swap file name pattern by appending ".sw?". names[num_names] = (char_u *)concat_fnames((char *)path, ".sw?", FALSE); - if (num_names >= 1) { /* check if we have the same name twice */ + if (num_names >= 1) { // check if we have the same name twice char_u *p = names[num_names - 1]; int i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); - if (i > 0) - p += i; /* file name has been expanded to full path */ - - if (STRCMP(p, names[num_names]) != 0) + if (i > 0) { + p += i; // file name has been expanded to full path + } + if (STRCMP(p, names[num_names]) != 0) { ++num_names; - else + } else { xfree(names[num_names]); - } else + } + } else { ++num_names; + } return num_names; } @@ -1676,11 +1706,11 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot) void ml_sync_all(int check_file, int check_char, bool do_fsync) { FOR_ALL_BUFFERS(buf) { - if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL) - continue; /* no file */ - - ml_flush_line(buf); /* flush buffered line */ - /* flush locked block */ + if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL) { + continue; // no file + } + ml_flush_line(buf); // flush buffered line + // flush locked block (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); if (bufIsChanged(buf) && check_file && mf_need_trans(buf->b_ml.ml_mfp) && buf->b_ffname != NULL) { @@ -1719,15 +1749,16 @@ void ml_sync_all(int check_file, int check_char, bool do_fsync) */ void ml_preserve(buf_T *buf, int message, bool do_fsync) { - bhdr_T *hp; + bhdr_T *hp; linenr_T lnum; - memfile_T *mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; int status; int got_int_save = got_int; if (mfp == NULL || mfp->mf_fname == NULL) { - if (message) + if (message) { EMSG(_("E313: Cannot preserve, there is no swap file")); + } return; } @@ -1739,7 +1770,7 @@ void ml_preserve(buf_T *buf, int message, bool do_fsync) (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); // flush locked block status = mf_sync(mfp, MFS_ALL | (do_fsync ? MFS_FLUSH : 0)); - /* stack is invalid after mf_sync(.., MFS_ALL) */ + // stack is invalid after mf_sync(.., MFS_ALL) buf->b_ml.ml_stack_top = 0; /* @@ -1776,10 +1807,11 @@ theend: got_int |= got_int_save; if (message) { - if (status == OK) + if (status == OK) { MSG(_("File preserved")); - else + } else { EMSG(_("E314: Preserve failed")); + } } } @@ -1787,7 +1819,7 @@ theend: * NOTE: The pointer returned by the ml_get_*() functions only remains valid * until the next call! * line1 = ml_get(1); - * line2 = ml_get(2); // line1 is now invalid! + * line2 = ml_get(2); // line1 is now invalid! * Make a copy of the line if necessary. */ /* @@ -1810,26 +1842,18 @@ char_u *ml_get_pos(const pos_T *pos) return ml_get_buf(curbuf, pos->lnum, false) + pos->col; } -/* - * Return a pointer to a line in a specific buffer - * - * "will_change": if TRUE mark the buffer dirty (chars in the line will be - * changed) - */ -char_u * -ml_get_buf ( - buf_T *buf, - linenr_T lnum, - bool will_change // line will be changed -) +/// Return a pointer to a line in a specific buffer +/// +/// @param will_change true mark the buffer dirty (chars in the line will be changed) +char_u *ml_get_buf(buf_T *buf, linenr_T lnum, bool will_change) FUNC_ATTR_NONNULL_ALL { - bhdr_T *hp; - DATA_BL *dp; - char_u *ptr; + bhdr_T *hp; + DATA_BL *dp; + char_u *ptr; static int recursive = 0; - if (lnum > buf->b_ml.ml_line_count) { /* invalid line number */ + if (lnum > buf->b_ml.ml_line_count) { // invalid line number if (recursive == 0) { // Avoid giving this message for a recursive call, may happen when // the GUI redraws part of the text. @@ -1841,11 +1865,13 @@ errorret: STRCPY(IObuff, "???"); return IObuff; } - if (lnum <= 0) /* pretend line 0 is line 1 */ + if (lnum <= 0) { // pretend line 0 is line 1 lnum = 1; + } - if (buf->b_ml.ml_mfp == NULL) /* there are no lines */ + if (buf->b_ml.ml_mfp == NULL) { // there are no lines return (char_u *)""; + } /* * See if it is the same line as requested last time. @@ -1900,88 +1926,89 @@ int ml_line_alloced(void) return curbuf->b_ml.ml_flags & ML_LINE_DIRTY; } -/* - * Append a line after lnum (may be 0 to insert a line in front of the file). - * "line" does not need to be allocated, but can't be another line in a - * buffer, unlocking may make it invalid. - * - * newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum - * will be set for recovery - * Check: The caller of this function should probably also call - * appended_lines(). - * - * return FAIL for failure, OK otherwise - */ -int ml_append( - linenr_T lnum, // append after this line (can be 0) - char_u *line, // text of the new line - colnr_T len, // length of new line, including NUL, or 0 - bool newfile // flag, see above -) +/// Append a line after lnum (may be 0 to insert a line in front of the file). +/// "line" does not need to be allocated, but can't be another line in a +/// buffer, unlocking may make it invalid. +/// +/// newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum +/// will be set for recovery +/// Check: The caller of this function should probably also call +/// appended_lines(). +/// +/// @param lnum append after this line (can be 0) +/// @param line text of the new line +/// @param len length of new line, including NUL, or 0 +/// @param newfile flag, see above +/// +/// @return FAIL for failure, OK otherwise +int ml_append(linenr_T lnum, char_u *line, colnr_T len, bool newfile) { - /* When starting up, we might still need to create the memfile */ - if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL) + // When starting up, we might still need to create the memfile + if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL) { return FAIL; + } - if (curbuf->b_ml.ml_line_lnum != 0) + if (curbuf->b_ml.ml_line_lnum != 0) { ml_flush_line(curbuf); + } return ml_append_int(curbuf, lnum, line, len, newfile, FALSE); } -/* - * Like ml_append() but for an arbitrary buffer. The buffer must already have - * a memline. - */ -int ml_append_buf( - buf_T *buf, - linenr_T lnum, // append after this line (can be 0) - char_u *line, // text of the new line - colnr_T len, // length of new line, including NUL, or 0 - bool newfile // flag, see above -) +/// Like ml_append() but for an arbitrary buffer. The buffer must already have +/// a memline. +/// +/// @param lnum append after this line (can be 0) +/// @param line text of the new line +/// @param len length of new line, including NUL, or 0 +/// @param newfile flag, see above +int ml_append_buf(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, bool newfile) FUNC_ATTR_NONNULL_ARG(1) { - if (buf->b_ml.ml_mfp == NULL) + if (buf->b_ml.ml_mfp == NULL) { return FAIL; + } - if (buf->b_ml.ml_line_lnum != 0) + if (buf->b_ml.ml_line_lnum != 0) { ml_flush_line(buf); + } return ml_append_int(buf, lnum, line, len, newfile, FALSE); } -static int ml_append_int( - buf_T *buf, - linenr_T lnum, // append after this line (can be 0) - char_u *line, // text of the new line - colnr_T len, // length of line, including NUL, or 0 - bool newfile, // flag, see above - int mark // mark the new line -) +/// @param lnum append after this line (can be 0) +/// @param line text of the new line +/// @param len length of line, including NUL, or 0 +/// @param newfile flag, see above +/// @param mark mark the new line +static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, bool newfile, + int mark) { int i; - int line_count; /* number of indexes in current block */ + int line_count; // number of indexes in current block int offset; int from, to; - int space_needed; /* space needed for new line */ + int space_needed; // space needed for new line int page_size; int page_count; - int db_idx; /* index for lnum in data block */ - bhdr_T *hp; - memfile_T *mfp; - DATA_BL *dp; - PTR_BL *pp; - infoptr_T *ip; - - /* lnum out of range */ - if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL) + int db_idx; // index for lnum in data block + bhdr_T *hp; + memfile_T *mfp; + DATA_BL *dp; + PTR_BL *pp; + infoptr_T *ip; + + // lnum out of range + if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL) { return FAIL; + } - if (lowest_marked && lowest_marked > lnum) + if (lowest_marked && lowest_marked > lnum) { lowest_marked = lnum + 1; + } - if (len == 0) - len = (colnr_T)STRLEN(line) + 1; /* space needed for the text */ - space_needed = len + INDEX_SIZE; /* space needed for text + index */ + if (len == 0) { + len = (colnr_T)STRLEN(line) + 1; // space needed for the text + } + space_needed = len + INDEX_SIZE; // space needed for text + index mfp = buf->b_ml.ml_mfp; page_size = mfp->mf_page_size; @@ -1992,16 +2019,18 @@ static int ml_append_int( * This also releases any locked block. */ if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum, - ML_INSERT)) == NULL) + ML_INSERT)) == NULL) { return FAIL; + } buf->b_ml.ml_flags &= ~ML_EMPTY; - if (lnum == 0) /* got line one instead, correct db_idx */ - db_idx = -1; /* careful, it is negative! */ - else + if (lnum == 0) { // got line one instead, correct db_idx + db_idx = -1; // careful, it is negative! + } else { db_idx = lnum - buf->b_ml.ml_locked_low; - /* get line count before the insertion */ + } + // get line count before the insertion line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; dp = hp->bh_data; @@ -2022,11 +2051,12 @@ static int ml_append_int( */ --(buf->b_ml.ml_locked_lineadd); --(buf->b_ml.ml_locked_high); - if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL) + if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL) { return FAIL; + } - db_idx = -1; /* careful, it is negative! */ - /* get line count before the insertion */ + db_idx = -1; // careful, it is negative! + // get line count before the insertion line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1"); @@ -2035,7 +2065,7 @@ static int ml_append_int( ++buf->b_ml.ml_line_count; - if ((int)dp->db_free >= space_needed) { /* enough room in data block */ + if ((int)dp->db_free >= space_needed) { // enough room in data block /* * Insert new line in existing data block, or in data block allocated above. */ @@ -2047,38 +2077,43 @@ static int ml_append_int( * move the text of the lines that follow to the front * adjust the indexes of the lines that follow */ - if (line_count > db_idx + 1) { /* if there are following lines */ + if (line_count > db_idx + 1) { // if there are following lines /* * Offset is the start of the previous line. * This will become the character just after the new line. */ - if (db_idx < 0) + if (db_idx < 0) { offset = dp->db_txt_end; - else + } else { offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK); + } memmove((char *)dp + dp->db_txt_start, - (char *)dp + dp->db_txt_start + len, - (size_t)(offset - (dp->db_txt_start + len))); - for (i = line_count - 1; i > db_idx; --i) + (char *)dp + dp->db_txt_start + len, + (size_t)(offset - (dp->db_txt_start + len))); + for (i = line_count - 1; i > db_idx; --i) { dp->db_index[i + 1] = dp->db_index[i] - len; + } dp->db_index[db_idx + 1] = offset - len; - } else /* add line at the end */ + } else { // add line at the end dp->db_index[db_idx + 1] = dp->db_txt_start; + } /* * copy the text into the block */ memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len); - if (mark) + if (mark) { dp->db_index[db_idx + 1] |= DB_MARKED; + } /* * Mark the block dirty. */ buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; - if (!newfile) + if (!newfile) { buf->b_ml.ml_flags |= ML_LOCKED_POS; - } else { /* not enough space in data block */ + } + } else { // not enough space in data block /* * If there is not enough room we have to create a new data block and copy some * lines into it. @@ -2090,20 +2125,20 @@ static int ml_append_int( */ long line_count_left, line_count_right; int page_count_left, page_count_right; - bhdr_T *hp_left; - bhdr_T *hp_right; - bhdr_T *hp_new; + bhdr_T *hp_left; + bhdr_T *hp_right; + bhdr_T *hp_new; int lines_moved; - int data_moved = 0; /* init to shut up gcc */ - int total_moved = 0; /* init to shut up gcc */ - DATA_BL *dp_right, *dp_left; + int data_moved = 0; // init to shut up gcc + int total_moved = 0; // init to shut up gcc + DATA_BL *dp_right, *dp_left; int stack_idx; bool in_left; int lineadd; blocknr_T bnum_left, bnum_right; linenr_T lnum_left, lnum_right; int pb_idx; - PTR_BL *pp_new; + PTR_BL *pp_new; /* * We are going to allocate a new data block. Depending on the @@ -2113,7 +2148,7 @@ static int ml_append_int( * also put in the right block. This method is more efficient when * inserting a lot of lines at one place. */ - if (db_idx < 0) { /* left block is new, right block is existing */ + if (db_idx < 0) { // left block is new, right block is existing lines_moved = 0; in_left = true; // space_needed does not change @@ -2138,12 +2173,12 @@ static int ml_append_int( page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size; hp_new = ml_new_data(mfp, newfile, page_count); - if (db_idx < 0) { /* left block is new */ + if (db_idx < 0) { // left block is new hp_left = hp_new; hp_right = hp; line_count_left = 0; line_count_right = line_count; - } else { /* right block is new */ + } else { // right block is new hp_left = hp; hp_right = hp_new; line_count_left = line_count; @@ -2163,11 +2198,12 @@ static int ml_append_int( dp_right->db_txt_start -= len; dp_right->db_free -= len + INDEX_SIZE; dp_right->db_index[0] = dp_right->db_txt_start; - if (mark) + if (mark) { dp_right->db_index[0] |= DB_MARKED; + } memmove((char *)dp_right + dp_right->db_txt_start, - line, (size_t)len); + line, (size_t)len); ++line_count_right; } /* @@ -2179,8 +2215,8 @@ static int ml_append_int( dp_right->db_txt_start -= data_moved; dp_right->db_free -= total_moved; memmove((char *)dp_right + dp_right->db_txt_start, - (char *)dp_left + dp_left->db_txt_start, - (size_t)data_moved); + (char *)dp_left + dp_left->db_txt_start, + (size_t)data_moved); offset = dp_right->db_txt_start - dp_left->db_txt_start; dp_left->db_txt_start += data_moved; dp_left->db_free += total_moved; @@ -2189,8 +2225,9 @@ static int ml_append_int( * update indexes in the new block */ for (to = line_count_right, from = db_idx + 1; - from < line_count_left; ++from, ++to) + from < line_count_left; ++from, ++to) { dp_right->db_index[to] = dp->db_index[from] + offset; + } line_count_right += lines_moved; line_count_left -= lines_moved; } @@ -2202,22 +2239,24 @@ static int ml_append_int( dp_left->db_txt_start -= len; dp_left->db_free -= len + INDEX_SIZE; dp_left->db_index[line_count_left] = dp_left->db_txt_start; - if (mark) + if (mark) { dp_left->db_index[line_count_left] |= DB_MARKED; + } memmove((char *)dp_left + dp_left->db_txt_start, - line, (size_t)len); + line, (size_t)len); ++line_count_left; } - if (db_idx < 0) { /* left block is new */ + if (db_idx < 0) { // left block is new lnum_left = lnum + 1; lnum_right = 0; - } else { /* right block is new */ + } else { // right block is new lnum_left = 0; - if (in_left) + if (in_left) { lnum_right = lnum + 2; - else + } else { lnum_right = lnum + 1; + } } dp_left->db_line_count = line_count_left; dp_right->db_line_count = line_count_right; @@ -2228,10 +2267,12 @@ static int ml_append_int( * The old one (hp, in ml_locked) gets a positive blocknumber if * we changed it and we are not editing a new file. */ - if (lines_moved || in_left) + if (lines_moved || in_left) { buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; - if (!newfile && db_idx >= 0 && in_left) + } + if (!newfile && db_idx >= 0 && in_left) { buf->b_ml.ml_flags |= ML_LOCKED_POS; + } mf_put(mfp, hp_new, true, false); /* @@ -2250,9 +2291,10 @@ static int ml_append_int( --stack_idx) { ip = &(buf->b_ml.ml_stack[stack_idx]); pb_idx = ip->ip_index; - if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) + if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { return FAIL; - pp = hp->bh_data; /* must be pointer block */ + } + pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { IEMSG(_("E317: pointer block id wrong 3")); mf_put(mfp, hp, false, false); @@ -2262,12 +2304,13 @@ static int ml_append_int( * TODO: If the pointer block is full and we are adding at the end * try to insert in front of the next block */ - /* block not full, add one entry */ + // block not full, add one entry if (pp->pb_count < pp->pb_count_max) { - if (pb_idx + 1 < (int)pp->pb_count) + if (pb_idx + 1 < (int)pp->pb_count) { memmove(&pp->pb_pointer[pb_idx + 2], - &pp->pb_pointer[pb_idx + 1], - (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN)); + &pp->pb_pointer[pb_idx + 1], + (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN)); + } ++pp->pb_count; pp->pb_pointer[pb_idx].pe_line_count = line_count_left; pp->pb_pointer[pb_idx].pe_bnum = bnum_left; @@ -2276,19 +2319,21 @@ static int ml_append_int( pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; - if (lnum_left != 0) + if (lnum_left != 0) { pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; - if (lnum_right != 0) + } + if (lnum_right != 0) { pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; + } mf_put(mfp, hp, true, false); - buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */ + buf->b_ml.ml_stack_top = stack_idx + 1; // truncate stack if (lineadd) { --(buf->b_ml.ml_stack_top); - /* fix line count for rest of blocks in the stack */ + // fix line count for rest of blocks in the stack ml_lineadd(buf, lineadd); - /* fix stack itself */ + // fix stack itself buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += lineadd; ++(buf->b_ml.ml_stack_top); @@ -2298,21 +2343,23 @@ static int ml_append_int( * We are finished, break the loop here. */ break; - } else { /* pointer block full */ + } else { // pointer block full /* * split the pointer block * allocate a new pointer block * move some of the pointer into the new block * prepare for updating the parent block */ - for (;; ) { /* do this twice when splitting block 1 */ + for (;; ) { // do this twice when splitting block 1 hp_new = ml_new_ptr(mfp); - if (hp_new == NULL) /* TODO: try to fix tree */ + if (hp_new == NULL) { // TODO: try to fix tree return FAIL; + } pp_new = hp_new->bh_data; - if (hp->bh_bnum != 1) + if (hp->bh_bnum != 1) { break; + } /* * if block 1 becomes full the tree is given an extra level @@ -2326,12 +2373,12 @@ static int ml_append_int( pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count; pp->pb_pointer[0].pe_old_lnum = 1; pp->pb_pointer[0].pe_page_count = 1; - mf_put(mfp, hp, true, false); /* release block 1 */ - hp = hp_new; /* new block is to be split */ + mf_put(mfp, hp, true, false); // release block 1 + hp = hp_new; // new block is to be split pp = pp_new; CHECK(stack_idx != 0, _("stack_idx should be 0")); ip->ip_index = 0; - ++stack_idx; /* do block 1 again later */ + ++stack_idx; // do block 1 again later } /* * move the pointers after the current one to the new block @@ -2340,15 +2387,16 @@ static int ml_append_int( total_moved = pp->pb_count - pb_idx - 1; if (total_moved) { memmove(&pp_new->pb_pointer[0], - &pp->pb_pointer[pb_idx + 1], - (size_t)(total_moved) * sizeof(PTR_EN)); + &pp->pb_pointer[pb_idx + 1], + (size_t)(total_moved) * sizeof(PTR_EN)); pp_new->pb_count = total_moved; pp->pb_count -= total_moved - 1; pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right; pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; - if (lnum_right) + if (lnum_right) { pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; + } } else { pp_new->pb_count = 1; pp_new->pb_pointer[0].pe_bnum = bnum_right; @@ -2359,8 +2407,9 @@ static int ml_append_int( pp->pb_pointer[pb_idx].pe_bnum = bnum_left; pp->pb_pointer[pb_idx].pe_line_count = line_count_left; pp->pb_pointer[pb_idx].pe_page_count = page_count_left; - if (lnum_left) + if (lnum_left) { pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; + } lnum_left = 0; lnum_right = 0; @@ -2368,11 +2417,13 @@ static int ml_append_int( * recompute line counts */ line_count_right = 0; - for (i = 0; i < (int)pp_new->pb_count; ++i) + for (i = 0; i < (int)pp_new->pb_count; ++i) { line_count_right += pp_new->pb_pointer[i].pe_line_count; + } line_count_left = 0; - for (i = 0; i < (int)pp->pb_count; ++i) + for (i = 0; i < (int)pp->pb_count; ++i) { line_count_left += pp->pb_pointer[i].pe_line_count; + } bnum_left = hp->bh_bnum; bnum_right = hp_new->bh_bnum; @@ -2392,7 +2443,7 @@ static int ml_append_int( } } - /* The line was inserted below 'lnum' */ + // The line was inserted below 'lnum' ml_updatechunk(buf, lnum + 1, (long)len, ML_CHNK_ADDLINE); return OK; } @@ -2439,8 +2490,9 @@ int ml_replace(linenr_T lnum, char_u *line, bool copy) // return FAIL for failure, OK otherwise int ml_replace_buf(buf_T *buf, linenr_T lnum, char_u *line, bool copy) { - if (line == NULL) /* just checking... */ + if (line == NULL) { // just checking... return FAIL; + } // When starting up, we might still need to create the memfile if (buf->b_ml.ml_mfp == NULL && open_buffer(false, NULL, 0) == FAIL) { @@ -2487,12 +2539,12 @@ int ml_delete(linenr_T lnum, bool message) static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) { - bhdr_T *hp; - memfile_T *mfp; - DATA_BL *dp; - PTR_BL *pp; - infoptr_T *ip; - int count; /* number of entries in block */ + bhdr_T *hp; + memfile_T *mfp; + DATA_BL *dp; + PTR_BL *pp; + infoptr_T *ip; + int count; // number of entries in block int idx; int stack_idx; int text_start; @@ -2500,19 +2552,22 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) long line_size; int i; - if (lnum < 1 || lnum > buf->b_ml.ml_line_count) + if (lnum < 1 || lnum > buf->b_ml.ml_line_count) { return FAIL; + } - if (lowest_marked && lowest_marked > lnum) + if (lowest_marked && lowest_marked > lnum) { lowest_marked--; + } /* * If the file becomes empty the last line is replaced by an empty line. */ - if (buf->b_ml.ml_line_count == 1) { /* file becomes empty */ + if (buf->b_ml.ml_line_count == 1) { // file becomes empty if (message - ) + ) { set_keep_msg((char_u *)_(no_lines_msg), 0); + } i = ml_replace((linenr_T)1, (char_u *)"", true); buf->b_ml.ml_flags |= ML_EMPTY; @@ -2526,14 +2581,16 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) * This also releases any locked block. */ mfp = buf->b_ml.ml_mfp; - if (mfp == NULL) + if (mfp == NULL) { return FAIL; + } - if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL) + if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL) { return FAIL; + } dp = hp->bh_data; - /* compute line count before the delete */ + // compute line count before the delete count = (long)(buf->b_ml.ml_locked_high) - (long)(buf->b_ml.ml_locked_low) + 2; idx = lnum - buf->b_ml.ml_locked_low; @@ -2541,10 +2598,11 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) --buf->b_ml.ml_line_count; line_start = ((dp->db_index[idx]) & DB_INDEX_MASK); - if (idx == 0) /* first line in block, text at the end */ + if (idx == 0) { // first line in block, text at the end line_size = dp->db_txt_end - line_start; - else + } else { line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start; + } // Line should always have an NL char internally (represented as NUL), // even if 'noeol' is set. @@ -2560,33 +2618,35 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) * ml_find_line(). */ if (count == 1) { - mf_free(mfp, hp); /* free the data block */ + mf_free(mfp, hp); // free the data block buf->b_ml.ml_locked = NULL; for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; --stack_idx) { - buf->b_ml.ml_stack_top = 0; /* stack is invalid when failing */ + buf->b_ml.ml_stack_top = 0; // stack is invalid when failing ip = &(buf->b_ml.ml_stack[stack_idx]); idx = ip->ip_index; - if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) + if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { return FAIL; - pp = hp->bh_data; /* must be pointer block */ + } + pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { IEMSG(_("E317: pointer block id wrong 4")); mf_put(mfp, hp, false, false); return FAIL; } count = --(pp->pb_count); - if (count == 0) /* the pointer block becomes empty! */ + if (count == 0) { // the pointer block becomes empty! mf_free(mfp, hp); - else { - if (count != idx) /* move entries after the deleted one */ + } else { + if (count != idx) { // move entries after the deleted one memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1], - (size_t)(count - idx) * sizeof(PTR_EN)); + (size_t)(count - idx) * sizeof(PTR_EN)); + } mf_put(mfp, hp, true, false); - buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */ - /* fix line count for rest of blocks in the stack */ + buf->b_ml.ml_stack_top = stack_idx; // truncate stack + // fix line count for rest of blocks in the stack if (buf->b_ml.ml_locked_lineadd != 0) { ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += @@ -2604,14 +2664,15 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) */ text_start = dp->db_txt_start; memmove((char *)dp + text_start + line_size, - (char *)dp + text_start, (size_t)(line_start - text_start)); + (char *)dp + text_start, (size_t)(line_start - text_start)); /* * delete the index by moving the next indexes backwards * Adjust the indexes for the text movement. */ - for (i = idx; i < count - 1; ++i) + for (i = idx; i < count - 1; ++i) { dp->db_index[i] = dp->db_index[i + 1] + line_size; + } dp->db_free += line_size + INDEX_SIZE; dp->db_txt_start += line_size; @@ -2632,24 +2693,25 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) */ void ml_setmarked(linenr_T lnum) { - bhdr_T *hp; + bhdr_T *hp; DATA_BL *dp; - /* invalid line number */ + // invalid line number if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count - || curbuf->b_ml.ml_mfp == NULL) - return; /* give error message? */ - - if (lowest_marked == 0 || lowest_marked > lnum) + || curbuf->b_ml.ml_mfp == NULL) { + return; // give error message? + } + if (lowest_marked == 0 || lowest_marked > lnum) { lowest_marked = lnum; + } /* * find the data block containing the line * This also fills the stack with the blocks from the root to the data block * This also releases any locked block. */ - if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) - return; /* give error message? */ - + if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { + return; // give error message? + } dp = hp->bh_data; dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED; curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; @@ -2660,13 +2722,14 @@ void ml_setmarked(linenr_T lnum) */ linenr_T ml_firstmarked(void) { - bhdr_T *hp; - DATA_BL *dp; + bhdr_T *hp; + DATA_BL *dp; linenr_T lnum; int i; - if (curbuf->b_ml.ml_mfp == NULL) - return (linenr_T) 0; + if (curbuf->b_ml.ml_mfp == NULL) { + return (linenr_T)0; + } /* * The search starts with lowest_marked line. This is the last line where @@ -2678,22 +2741,23 @@ linenr_T ml_firstmarked(void) * This also fills the stack with the blocks from the root to the data * block This also releases any locked block. */ - if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) - return (linenr_T)0; /* give error message? */ - + if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { + return (linenr_T)0; // give error message? + } dp = hp->bh_data; for (i = lnum - curbuf->b_ml.ml_locked_low; - lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) + lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) { if ((dp->db_index[i]) & DB_MARKED) { (dp->db_index[i]) &= DB_INDEX_MASK; curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; lowest_marked = lnum + 1; return lnum; } + } } - return (linenr_T) 0; + return (linenr_T)0; } /* @@ -2701,13 +2765,14 @@ linenr_T ml_firstmarked(void) */ void ml_clearmarked(void) { - bhdr_T *hp; - DATA_BL *dp; + bhdr_T *hp; + DATA_BL *dp; linenr_T lnum; int i; - if (curbuf->b_ml.ml_mfp == NULL) /* nothing to do */ + if (curbuf->b_ml.ml_mfp == NULL) { // nothing to do return; + } /* * The search starts with line lowest_marked. @@ -2718,17 +2783,18 @@ void ml_clearmarked(void) * This also fills the stack with the blocks from the root to the data * block and releases any locked block. */ - if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) - return; /* give error message? */ - + if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { + return; // give error message? + } dp = hp->bh_data; for (i = lnum - curbuf->b_ml.ml_locked_low; - lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) + lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) { if ((dp->db_index[i]) & DB_MARKED) { (dp->db_index[i]) &= DB_INDEX_MASK; curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; } + } } lowest_marked = 0; @@ -2751,11 +2817,11 @@ size_t ml_flush_deleted_bytes(buf_T *buf, size_t *codepoints, size_t *codeunits) */ static void ml_flush_line(buf_T *buf) { - bhdr_T *hp; - DATA_BL *dp; + bhdr_T *hp; + DATA_BL *dp; linenr_T lnum; - char_u *new_line; - char_u *old_line; + char_u *new_line; + char_u *old_line; colnr_T new_len; int old_len; int extra; @@ -2765,13 +2831,14 @@ static void ml_flush_line(buf_T *buf) int i; static bool entered = false; - if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL) - return; /* nothing to do */ - + if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL) { + return; // nothing to do + } if (buf->b_ml.ml_flags & ML_LINE_DIRTY) { - /* This code doesn't work recursively. */ - if (entered) + // This code doesn't work recursively. + if (entered) { return; + } entered = true; buf->flush_count++; @@ -2787,39 +2854,41 @@ static void ml_flush_line(buf_T *buf) idx = lnum - buf->b_ml.ml_locked_low; start = ((dp->db_index[idx]) & DB_INDEX_MASK); old_line = (char_u *)dp + start; - if (idx == 0) /* line is last in block */ + if (idx == 0) { // line is last in block old_len = dp->db_txt_end - start; - else /* text of previous line follows */ + } else { // text of previous line follows old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start; + } new_len = (colnr_T)STRLEN(new_line) + 1; - extra = new_len - old_len; /* negative if lines gets smaller */ + extra = new_len - old_len; // negative if lines gets smaller /* * if new line fits in data block, replace directly */ if ((int)dp->db_free >= extra) { - /* if the length changes and there are following lines */ + // if the length changes and there are following lines count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1; if (extra != 0 && idx < count - 1) { - /* move text of following lines */ + // move text of following lines memmove((char *)dp + dp->db_txt_start - extra, - (char *)dp + dp->db_txt_start, - (size_t)(start - dp->db_txt_start)); + (char *)dp + dp->db_txt_start, + (size_t)(start - dp->db_txt_start)); - /* adjust pointers of this and following lines */ - for (i = idx + 1; i < count; ++i) + // adjust pointers of this and following lines + for (i = idx + 1; i < count; ++i) { dp->db_index[i] -= extra; + } } dp->db_index[idx] -= extra; - /* adjust free space */ + // adjust free space dp->db_free -= extra; dp->db_txt_start -= extra; - /* copy new line into the data block */ + // copy new line into the data block memmove(old_line - extra, new_line, (size_t)new_len); buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); - /* The else case is already covered by the insert and delete */ + // The else case is already covered by the insert and delete ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE); } else { // Cannot do it in one data block: Delete and append. @@ -2876,8 +2945,8 @@ static bhdr_T *ml_new_ptr(memfile_T *mfp) * lookup line 'lnum' in a memline * * action: if ML_DELETE or ML_INSERT the line count is updated while searching - * if ML_FLUSH only flush a locked block - * if ML_FIND just find the line + * if ML_FLUSH only flush a locked block + * if ML_FIND just find the line * * If the block was found it is locked and put in ml_locked. * The stack is updated to lead to the locked block. The ip_high field in @@ -2889,11 +2958,11 @@ static bhdr_T *ml_new_ptr(memfile_T *mfp) */ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) { - DATA_BL *dp; - PTR_BL *pp; - infoptr_T *ip; - bhdr_T *hp; - memfile_T *mfp; + DATA_BL *dp; + PTR_BL *pp; + infoptr_T *ip; + bhdr_T *hp; + memfile_T *mfp; linenr_T t; blocknr_T bnum, bnum2; int dirty; @@ -2927,58 +2996,63 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) } mf_put(mfp, buf->b_ml.ml_locked, buf->b_ml.ml_flags & ML_LOCKED_DIRTY, - buf->b_ml.ml_flags & ML_LOCKED_POS); + buf->b_ml.ml_flags & ML_LOCKED_POS); buf->b_ml.ml_locked = NULL; /* * If lines have been added or deleted in the locked block, need to * update the line count in pointer blocks. */ - if (buf->b_ml.ml_locked_lineadd != 0) + if (buf->b_ml.ml_locked_lineadd != 0) { ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); + } } - if (action == ML_FLUSH) /* nothing else to do */ + if (action == ML_FLUSH) { // nothing else to do return NULL; + } - bnum = 1; /* start at the root of the tree */ + bnum = 1; // start at the root of the tree page_count = 1; low = 1; high = buf->b_ml.ml_line_count; - if (action == ML_FIND) { /* first try stack entries */ + if (action == ML_FIND) { // first try stack entries for (top = buf->b_ml.ml_stack_top - 1; top >= 0; --top) { ip = &(buf->b_ml.ml_stack[top]); if (ip->ip_low <= lnum && ip->ip_high >= lnum) { bnum = ip->ip_bnum; low = ip->ip_low; high = ip->ip_high; - buf->b_ml.ml_stack_top = top; /* truncate stack at prev entry */ + buf->b_ml.ml_stack_top = top; // truncate stack at prev entry break; } } - if (top < 0) - buf->b_ml.ml_stack_top = 0; /* not found, start at the root */ - } else /* ML_DELETE or ML_INSERT */ - buf->b_ml.ml_stack_top = 0; /* start at the root */ - + if (top < 0) { + buf->b_ml.ml_stack_top = 0; // not found, start at the root + } + } else { // ML_DELETE or ML_INSERT + buf->b_ml.ml_stack_top = 0; // start at the root + } /* * search downwards in the tree until a data block is found */ for (;; ) { - if ((hp = mf_get(mfp, bnum, page_count)) == NULL) + if ((hp = mf_get(mfp, bnum, page_count)) == NULL) { goto error_noblock; + } /* * update high for insert/delete */ - if (action == ML_INSERT) + if (action == ML_INSERT) { ++high; - else if (action == ML_DELETE) + } else if (action == ML_DELETE) { --high; + } dp = hp->bh_data; - if (dp->db_id == DATA_ID) { /* data block */ + if (dp->db_id == DATA_ID) { // data block buf->b_ml.ml_locked = hp; buf->b_ml.ml_locked_low = low; buf->b_ml.ml_locked_high = high; @@ -2987,7 +3061,7 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) return hp; } - pp = (PTR_BL *)(dp); /* must be pointer block */ + pp = (PTR_BL *)(dp); // must be pointer block if (pp->pb_id != PTR_ID) { IEMSG(_("E317: pointer block id wrong")); goto error_block; @@ -2998,7 +3072,7 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) ip->ip_bnum = bnum; ip->ip_low = low; ip->ip_high = high; - ip->ip_index = -1; /* index not known yet */ + ip->ip_index = -1; // index not known yet dirty = FALSE; for (idx = 0; idx < (int)pp->pb_count; ++idx) { @@ -3030,7 +3104,6 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) if (lnum > buf->b_ml.ml_line_count) { IEMSGN(_("E322: line number out of range: %" PRId64 " past the end"), lnum - buf->b_ml.ml_line_count); - } else { IEMSGN(_("E323: line count wrong in block %" PRId64), bnum); } @@ -3054,10 +3127,11 @@ error_noblock: * the incremented/decremented line counts, because there won't be a line * inserted/deleted after all. */ - if (action == ML_DELETE) + if (action == ML_DELETE) { ml_lineadd(buf, 1); - else if (action == ML_INSERT) + } else if (action == ML_INSERT) { ml_lineadd(buf, -1); + } buf->b_ml.ml_stack_top = 0; return NULL; } @@ -3071,9 +3145,9 @@ static int ml_add_stack(buf_T *buf) { int top = buf->b_ml.ml_stack_top; - /* may have to increase the stack size */ + // may have to increase the stack size if (top == buf->b_ml.ml_stack_size) { - CHECK(top > 0, _("Stack size increases")); /* more than 5 levels??? */ + CHECK(top > 0, _("Stack size increases")); // more than 5 levels??? buf->b_ml.ml_stack_size += STACK_INCR; size_t new_size = sizeof(infoptr_T) * buf->b_ml.ml_stack_size; @@ -3097,16 +3171,17 @@ static int ml_add_stack(buf_T *buf) static void ml_lineadd(buf_T *buf, int count) { int idx; - infoptr_T *ip; - PTR_BL *pp; - memfile_T *mfp = buf->b_ml.ml_mfp; - bhdr_T *hp; + infoptr_T *ip; + PTR_BL *pp; + memfile_T *mfp = buf->b_ml.ml_mfp; + bhdr_T *hp; for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; --idx) { ip = &(buf->b_ml.ml_stack[idx]); - if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) + if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { break; - pp = hp->bh_data; /* must be pointer block */ + } + pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { mf_put(mfp, hp, false, false); IEMSG(_("E317: pointer block id wrong 2")); @@ -3132,14 +3207,15 @@ int resolve_symlink(const char_u *fname, char_u *buf) int ret; int depth = 0; - if (fname == NULL) + if (fname == NULL) { return FAIL; + } - /* Put the result so far in tmp[], starting with the original name. */ + // Put the result so far in tmp[], starting with the original name. STRLCPY(tmp, fname, MAXPATHL); for (;; ) { - /* Limit symlink depth to 100, catch recursive loops. */ + // Limit symlink depth to 100, catch recursive loops. if (++depth == 100) { EMSG2(_("E773: Symlink loop for \"%s\""), fname); return FAIL; @@ -3151,14 +3227,15 @@ int resolve_symlink(const char_u *fname, char_u *buf) /* Found non-symlink or not existing file, stop here. * When at the first level use the unmodified name, skip the * call to vim_FullName(). */ - if (depth == 1) + if (depth == 1) { return FAIL; + } - /* Use the resolved name in tmp[]. */ + // Use the resolved name in tmp[]. break; } - /* There must be some error reading links, use original name. */ + // There must be some error reading links, use original name. return FAIL; } buf[ret] = NUL; @@ -3193,8 +3270,8 @@ int resolve_symlink(const char_u *fname, char_u *buf) */ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name) { - char_u *r, *s; - char_u *fname_res = fname; + char_u *r, *s; + char_u *fname_res = fname; #ifdef HAVE_READLINK char_u fname_buf[MAXPATHL]; @@ -3221,46 +3298,43 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name // Prepend a '.' to the swap file name for the current directory. r = (char_u *)modname((char *)fname_res, ".swp", - dir_name[0] == '.' && dir_name[1] == NUL); - if (r == NULL) /* out of memory */ + dir_name[0] == '.' && dir_name[1] == NUL); + if (r == NULL) { // out of memory return NULL; + } s = get_file_in_dir(r, dir_name); xfree(r); return s; } -/* - * Get file name to use for swap file or backup file. - * Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir' - * option "dname". - * - If "dname" is ".", return "fname" (swap file in dir of file). - * - If "dname" starts with "./", insert "dname" in "fname" (swap file - * relative to dir of file). - * - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific - * dir). - * - * The return value is an allocated string and can be NULL. - */ -char_u * -get_file_in_dir ( - char_u *fname, - char_u *dname /* don't use "dirname", it is a global for Alpha */ -) +/// Get file name to use for swap file or backup file. +/// Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir' +/// option "dname". +/// - If "dname" is ".", return "fname" (swap file in dir of file). +/// - If "dname" starts with "./", insert "dname" in "fname" (swap file +/// relative to dir of file). +/// - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific +/// dir). +/// +/// The return value is an allocated string and can be NULL. +/// +/// @param dname don't use "dirname", it is a global for Alpha +char_u *get_file_in_dir(char_u *fname, char_u *dname) { - char_u *t; - char_u *tail; - char_u *retval; + char_u *t; + char_u *tail; + char_u *retval; int save_char; tail = path_tail(fname); - if (dname[0] == '.' && dname[1] == NUL) + if (dname[0] == '.' && dname[1] == NUL) { retval = vim_strsave(fname); - else if (dname[0] == '.' && vim_ispathsep(dname[1])) { - if (tail == fname) /* no path before file name */ + } else if (dname[0] == '.' && vim_ispathsep(dname[1])) { + if (tail == fname) { // no path before file name retval = (char_u *)concat_fnames((char *)dname + 2, (char *)tail, TRUE); - else { + } else { save_char = *tail; *tail = NUL; t = (char_u *)concat_fnames((char *)fname, (char *)dname + 2, TRUE); @@ -3276,14 +3350,11 @@ get_file_in_dir ( } -/* - * Print the ATTENTION message: info about an existing swap file. - */ -static void -attention_message ( - buf_T *buf, /* buffer being edited */ - char_u *fname /* swap file name */ -) +/// Print the ATTENTION message: info about an existing swap file. +/// +/// @param buf buffer being edited +/// @param fname swap file name +static void attention_message(buf_T *buf, char_u *fname) { assert(buf->b_fname != NULL); @@ -3339,7 +3410,7 @@ attention_message ( */ static int do_swapexists(buf_T *buf, char_u *fname) { - set_vim_var_string(VV_SWAPNAME, (char *) fname, -1); + set_vim_var_string(VV_SWAPNAME, (char *)fname, -1); set_vim_var_string(VV_SWAPCHOICE, NULL, -1); // Trigger SwapExists autocommands with <afile> set to the file being @@ -3351,12 +3422,18 @@ static int do_swapexists(buf_T *buf, char_u *fname) set_vim_var_string(VV_SWAPNAME, NULL, -1); switch (*get_vim_var_str(VV_SWAPCHOICE)) { - case 'o': return 1; - case 'e': return 2; - case 'r': return 3; - case 'd': return 4; - case 'q': return 5; - case 'a': return 6; + case 'o': + return 1; + case 'e': + return 2; + case 'r': + return 3; + case 'd': + return 4; + case 'q': + return 5; + case 'a': + return 6; } return 0; @@ -3385,14 +3462,13 @@ static int do_swapexists(buf_T *buf, char_u *fname) /// never set to false. /// /// @return [allocated] Name of the swap file. -static char *findswapname(buf_T *buf, char **dirp, char *old_fname, - bool *found_existing_dir) +static char *findswapname(buf_T *buf, char **dirp, char *old_fname, bool *found_existing_dir) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1, 2, 4) { char *fname; size_t n; char *dir_name; - char *buf_fname = (char *) buf->b_fname; + char *buf_fname = (char *)buf->b_fname; /* * Isolate a directory name from *dirp and put it in dir_name. @@ -3400,7 +3476,7 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, */ const size_t dir_len = strlen(*dirp) + 1; dir_name = xmalloc(dir_len); - (void)copy_option_part((char_u **) dirp, (char_u *) dir_name, dir_len, ","); + (void)copy_option_part((char_u **)dirp, (char_u *)dir_name, dir_len, ","); /* * we try different names until we find one that does not exist yet @@ -3459,7 +3535,7 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, // inode too. expand_env(b0.b0_fname, NameBuff, MAXPATHL); if (fnamecmp_ino(buf->b_ffname, NameBuff, - char_to_long(b0.b0_ino))) { + char_to_long(b0.b0_ino))) { differ = TRUE; } } @@ -3468,7 +3544,7 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, // "~user/path/file". Expand it first. expand_env(b0.b0_fname, NameBuff, MAXPATHL); if (fnamecmp_ino(buf->b_ffname, NameBuff, - char_to_long(b0.b0_ino))) { + char_to_long(b0.b0_ino))) { differ = TRUE; } } @@ -3532,12 +3608,10 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, choice = do_dialog(VIM_WARNING, (char_u *)_("VIM - ATTENTION"), (char_u *)name, process_still_running - ? (char_u *)_( - "&Open Read-Only\n&Edit anyway\n&Recover" - "\n&Quit\n&Abort") : - (char_u *)_( - "&Open Read-Only\n&Edit anyway\n&Recover" - "\n&Delete it\n&Quit\n&Abort"), + ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover" + "\n&Quit\n&Abort") : + (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover" + "\n&Delete it\n&Quit\n&Abort"), 1, NULL, false); if (process_still_running && choice >= 4) { @@ -3582,7 +3656,6 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, need_wait_return = true; } } - } } } @@ -3593,19 +3666,19 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, * If that still isn't enough decrement the last but one char: ".svz" * Can happen when editing many "No Name" buffers. */ - if (fname[n - 1] == 'a') { /* ".s?a" */ - if (fname[n - 2] == 'a') { /* ".saa": tried enough, give up */ + if (fname[n - 1] == 'a') { // ".s?a" + if (fname[n - 2] == 'a') { // ".saa": tried enough, give up EMSG(_("E326: Too many swap files found")); XFREE_CLEAR(fname); break; } - --fname[n - 2]; /* ".svz", ".suz", etc. */ + --fname[n - 2]; // ".svz", ".suz", etc. fname[n - 1] = 'z' + 1; } - --fname[n - 1]; /* ".swo", ".swn", etc. */ + --fname[n - 1]; // ".swo", ".swn", etc. } - if (os_isdir((char_u *) dir_name)) { + if (os_isdir((char_u *)dir_name)) { *found_existing_dir = true; } else if (!*found_existing_dir && **dirp == NUL) { int ret; @@ -3630,66 +3703,62 @@ static int b0_magic_wrong(ZERO_BL *b0p) || b0p->b0_magic_char != B0_MAGIC_CHAR; } -/* - * Compare current file name with file name from swap file. - * Try to use inode numbers when possible. - * Return non-zero when files are different. - * - * When comparing file names a few things have to be taken into consideration: - * - When working over a network the full path of a file depends on the host. - * We check the inode number if possible. It is not 100% reliable though, - * because the device number cannot be used over a network. - * - When a file does not exist yet (editing a new file) there is no inode - * number. - * - The file name in a swap file may not be valid on the current host. The - * "~user" form is used whenever possible to avoid this. - * - * This is getting complicated, let's make a table: - * - * ino_c ino_s fname_c fname_s differ = - * - * both files exist -> compare inode numbers: - * != 0 != 0 X X ino_c != ino_s - * - * inode number(s) unknown, file names available -> compare file names - * == 0 X OK OK fname_c != fname_s - * X == 0 OK OK fname_c != fname_s - * - * current file doesn't exist, file for swap file exist, file name(s) not - * available -> probably different - * == 0 != 0 FAIL X TRUE - * == 0 != 0 X FAIL TRUE - * - * current file exists, inode for swap unknown, file name(s) not - * available -> probably different - * != 0 == 0 FAIL X TRUE - * != 0 == 0 X FAIL TRUE - * - * current file doesn't exist, inode for swap unknown, one file name not - * available -> probably different - * == 0 == 0 FAIL OK TRUE - * == 0 == 0 OK FAIL TRUE - * - * current file doesn't exist, inode for swap unknown, both file names not - * available -> compare file names - * == 0 == 0 FAIL FAIL fname_c != fname_s - * - * Only the last 32 bits of the inode will be used. This can't be changed - * without making the block 0 incompatible with 32 bit versions. - */ - -static bool fnamecmp_ino( - char_u *fname_c, // current file name - char_u *fname_s, // file name from swap file - long ino_block0 -) +/// Compare current file name with file name from swap file. +/// Try to use inode numbers when possible. +/// Return non-zero when files are different. +/// +/// When comparing file names a few things have to be taken into consideration: +/// - When working over a network the full path of a file depends on the host. +/// We check the inode number if possible. It is not 100% reliable though, +/// because the device number cannot be used over a network. +/// - When a file does not exist yet (editing a new file) there is no inode +/// number. +/// - The file name in a swap file may not be valid on the current host. The +/// "~user" form is used whenever possible to avoid this. +/// +/// This is getting complicated, let's make a table: +/// +/// ino_c ino_s fname_c fname_s differ = +/// +/// both files exist -> compare inode numbers: +/// != 0 != 0 X X ino_c != ino_s +/// +/// inode number(s) unknown, file names available -> compare file names +/// == 0 X OK OK fname_c != fname_s +/// X == 0 OK OK fname_c != fname_s +/// +/// current file doesn't exist, file for swap file exist, file name(s) not +/// available -> probably different +/// == 0 != 0 FAIL X TRUE +/// == 0 != 0 X FAIL TRUE +/// +/// current file exists, inode for swap unknown, file name(s) not +/// available -> probably different +/// != 0 == 0 FAIL X TRUE +/// != 0 == 0 X FAIL TRUE +/// +/// current file doesn't exist, inode for swap unknown, one file name not +/// available -> probably different +/// == 0 == 0 FAIL OK TRUE +/// == 0 == 0 OK FAIL TRUE +/// +/// current file doesn't exist, inode for swap unknown, both file names not +/// available -> compare file names +/// == 0 == 0 FAIL FAIL fname_c != fname_s +/// +/// Only the last 32 bits of the inode will be used. This can't be changed +/// without making the block 0 incompatible with 32 bit versions. +/// +/// @param fname_c current file name +/// @param fname_s file name from swap file +static bool fnamecmp_ino(char_u *fname_c, char_u *fname_s, long ino_block0) { - uint64_t ino_c = 0; /* ino of current file */ - uint64_t ino_s; /* ino of file from swap file */ - char_u buf_c[MAXPATHL]; /* full path of fname_c */ - char_u buf_s[MAXPATHL]; /* full path of fname_s */ - int retval_c; /* flag: buf_c valid */ - int retval_s; /* flag: buf_s valid */ + uint64_t ino_c = 0; // ino of current file + uint64_t ino_s; // ino of file from swap file + char_u buf_c[MAXPATHL]; // full path of fname_c + char_u buf_s[MAXPATHL]; // full path of fname_s + int retval_c; // flag: buf_c valid + int retval_s; // flag: buf_s valid FileInfo file_info; if (os_fileinfo((char *)fname_c, &file_info)) { @@ -3707,8 +3776,9 @@ static bool fnamecmp_ino( ino_s = (uint64_t)ino_block0; } - if (ino_c && ino_s) + if (ino_c && ino_s) { return ino_c != ino_s; + } /* * One of the inode numbers is unknown, try a forced vim_FullName() and @@ -3716,8 +3786,9 @@ static bool fnamecmp_ino( */ retval_c = vim_FullName((char *)fname_c, (char *)buf_c, MAXPATHL, TRUE); retval_s = vim_FullName((char *)fname_s, (char *)buf_s, MAXPATHL, TRUE); - if (retval_c == OK && retval_s == OK) + if (retval_c == OK && retval_s == OK) { return STRCMP(buf_c, buf_s) != 0; + } /* * Can't compare inodes or file names, guess that the files are different, @@ -3768,11 +3839,12 @@ static long char_to_long(char_u *s) */ void ml_setflags(buf_T *buf) { - bhdr_T *hp; - ZERO_BL *b0p; + bhdr_T *hp; + ZERO_BL *b0p; - if (!buf->b_ml.ml_mfp) + if (!buf->b_ml.ml_mfp) { return; + } for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) { if (hp->bh_bnum == 0) { b0p = hp->bh_data; @@ -3787,19 +3859,19 @@ void ml_setflags(buf_T *buf) } } -#define MLCS_MAXL 800 /* max no of lines in chunk */ -#define MLCS_MINL 400 /* should be half of MLCS_MAXL */ +#define MLCS_MAXL 800 // max no of lines in chunk +#define MLCS_MINL 400 // should be half of MLCS_MAXL /* * Keep information for finding byte offset of a line, updtype may be one of: * ML_CHNK_ADDLINE: Add len to parent chunk, possibly splitting it - * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called. + * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called. * ML_CHNK_DELLINE: Subtract len from parent chunk, possibly deleting it * ML_CHNK_UPDLINE: Add len to parent chunk, as a signed entity. */ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) { - static buf_T *ml_upd_lastbuf = NULL; + static buf_T *ml_upd_lastbuf = NULL; static linenr_T ml_upd_lastline; static linenr_T ml_upd_lastcurline; static int ml_upd_lastcurix; @@ -3807,13 +3879,14 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) linenr_T curline = ml_upd_lastcurline; int curix = ml_upd_lastcurix; long size; - chunksize_T *curchnk; + chunksize_T *curchnk; int rest; - bhdr_T *hp; - DATA_BL *dp; + bhdr_T *hp; + DATA_BL *dp; - if (buf->b_ml.ml_usedchunks == -1 || len == 0) + if (buf->b_ml.ml_usedchunks == -1 || len == 0) { return; + } if (buf->b_ml.ml_chunksize == NULL) { buf->b_ml.ml_chunksize = xmalloc(sizeof(chunksize_T) * 100); buf->b_ml.ml_numchunks = 100; @@ -3841,8 +3914,8 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) || updtype != ML_CHNK_ADDLINE) { for (curline = 1, curix = 0; curix < buf->b_ml.ml_usedchunks - 1 - && line >= curline + - buf->b_ml.ml_chunksize[curix].mlcs_numlines; + && line >= curline + + buf->b_ml.ml_chunksize[curix].mlcs_numlines; curix++) { curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; } @@ -3854,31 +3927,31 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) } curchnk = buf->b_ml.ml_chunksize + curix; - if (updtype == ML_CHNK_DELLINE) + if (updtype == ML_CHNK_DELLINE) { len = -len; + } curchnk->mlcs_totalsize += len; if (updtype == ML_CHNK_ADDLINE) { curchnk->mlcs_numlines++; - /* May resize here so we don't have to do it in both cases below */ + // May resize here so we don't have to do it in both cases below if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks) { buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2; - buf->b_ml.ml_chunksize = xrealloc( - buf->b_ml.ml_chunksize, - sizeof(chunksize_T) * buf->b_ml.ml_numchunks); + buf->b_ml.ml_chunksize = xrealloc(buf->b_ml.ml_chunksize, + sizeof(chunksize_T) * buf->b_ml.ml_numchunks); } if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL) { - int count; /* number of entries in block */ + int count; // number of entries in block int idx; int text_end; int linecnt; memmove(buf->b_ml.ml_chunksize + curix + 1, - buf->b_ml.ml_chunksize + curix, - (buf->b_ml.ml_usedchunks - curix) * - sizeof(chunksize_T)); - /* Compute length of first half of lines in the split chunk */ + buf->b_ml.ml_chunksize + curix, + (buf->b_ml.ml_usedchunks - curix) * + sizeof(chunksize_T)); + // Compute length of first half of lines in the split chunk size = 0; linecnt = 0; while (curline < buf->b_ml.ml_line_count @@ -3892,11 +3965,12 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) (long)(buf->b_ml.ml_locked_low) + 1; idx = curline - buf->b_ml.ml_locked_low; curline = buf->b_ml.ml_locked_high + 1; - if (idx == 0) /* first line in block, text at the end */ + if (idx == 0) { // first line in block, text at the end text_end = dp->db_txt_end; - else + } else { text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); - /* Compute index of last line to use in this MEMLINE */ + } + // Compute index of last line to use in this MEMLINE rest = count - idx; if (linecnt + rest > MLCS_MINL) { idx += MLCS_MINL - linecnt - 1; @@ -3912,7 +3986,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) buf->b_ml.ml_chunksize[curix].mlcs_totalsize = size; buf->b_ml.ml_chunksize[curix + 1].mlcs_totalsize -= size; buf->b_ml.ml_usedchunks++; - ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ + ml_upd_lastbuf = NULL; // Force recalc of curix & curline return; } else if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MINL && curix == buf->b_ml.ml_usedchunks - 1 @@ -3937,12 +4011,13 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) return; } dp = hp->bh_data; - if (dp->db_line_count == 1) + if (dp->db_line_count == 1) { rest = dp->db_txt_end - dp->db_txt_start; - else + } else { rest = ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK) - dp->db_txt_start; + } curchnk->mlcs_totalsize = rest; curchnk->mlcs_numlines = 1; curchnk[-1].mlcs_totalsize -= rest; @@ -3951,7 +4026,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) } } else if (updtype == ML_CHNK_DELLINE) { curchnk->mlcs_numlines--; - ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ + ml_upd_lastbuf = NULL; // Force recalc of curix & curline if (curix < (buf->b_ml.ml_usedchunks - 1) && (curchnk->mlcs_numlines + curchnk[1].mlcs_numlines) <= MLCS_MINL) { @@ -3960,7 +4035,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) } else if (curix == 0 && curchnk->mlcs_numlines <= 0) { buf->b_ml.ml_usedchunks--; memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1, - buf->b_ml.ml_usedchunks * sizeof(chunksize_T)); + buf->b_ml.ml_usedchunks * sizeof(chunksize_T)); return; } else if (curix == 0 || (curchnk->mlcs_numlines > 10 && (curchnk->mlcs_numlines + @@ -3969,15 +4044,15 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype) return; } - /* Collapse chunks */ + // Collapse chunks curchnk[-1].mlcs_numlines += curchnk->mlcs_numlines; curchnk[-1].mlcs_totalsize += curchnk->mlcs_totalsize; buf->b_ml.ml_usedchunks--; if (curix < buf->b_ml.ml_usedchunks) { memmove(buf->b_ml.ml_chunksize + curix, - buf->b_ml.ml_chunksize + curix + 1, - (buf->b_ml.ml_usedchunks - curix) * - sizeof(chunksize_T)); + buf->b_ml.ml_chunksize + curix + 1, + (buf->b_ml.ml_usedchunks - curix) * + sizeof(chunksize_T)); } return; } @@ -4002,9 +4077,9 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) linenr_T curline; int curix; long size; - bhdr_T *hp; - DATA_BL *dp; - int count; /* number of entries in block */ + bhdr_T *hp; + DATA_BL *dp; + int count; // number of entries in block int idx; int start_idx; int text_end; @@ -4032,15 +4107,18 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) if (buf->b_ml.ml_usedchunks == -1 || buf->b_ml.ml_chunksize == NULL - || lnum < 0) + || lnum < 0) { return -1; + } - if (offp == NULL) + if (offp == NULL) { offset = 0; - else + } else { offset = *offp; - if (lnum == 0 && offset <= 0) - return 1; /* Not a "find offset" and offset 0 _must_ be in line 1 */ + } + if (lnum == 0 && offset <= 0) { + return 1; // Not a "find offset" and offset 0 _must_ be in line 1 + } /* * Find the last chunk before the one containing our line. Last chunk is * special because it will never qualify @@ -4056,36 +4134,41 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines))) { curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize; - if (offset && ffdos) + if (offset && ffdos) { size += buf->b_ml.ml_chunksize[curix].mlcs_numlines; + } curix++; } while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset)) { if (curline > buf->b_ml.ml_line_count - || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL) + || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL) { return -1; + } dp = hp->bh_data; count = (long)(buf->b_ml.ml_locked_high) - (long)(buf->b_ml.ml_locked_low) + 1; start_idx = idx = curline - buf->b_ml.ml_locked_low; - if (idx == 0) /* first line in block, text at the end */ + if (idx == 0) { // first line in block, text at the end text_end = dp->db_txt_end; - else + } else { text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); - /* Compute index of last line to use in this MEMLINE */ + } + // Compute index of last line to use in this MEMLINE if (lnum != 0) { - if (curline + (count - idx) >= lnum) + if (curline + (count - idx) >= lnum) { idx += lnum - curline - 1; - else + } else { idx = count - 1; + } } else { extra = 0; while (offset >= size + text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK) + ffdos) { - if (ffdos) + if (ffdos) { size++; + } if (idx == count - 1) { extra = 1; break; @@ -4096,25 +4179,28 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK); size += len; if (offset != 0 && size >= offset) { - if (size + ffdos == offset) + if (size + ffdos == offset) { *offp = 0; - else if (idx == start_idx) + } else if (idx == start_idx) { *offp = offset - size + len; - else + } else { *offp = offset - size + len - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK)); + } curline += idx - start_idx + extra; - if (curline > buf->b_ml.ml_line_count) - return -1; /* exactly one byte beyond the end */ + if (curline > buf->b_ml.ml_line_count) { + return -1; // exactly one byte beyond the end + } return curline; } curline = buf->b_ml.ml_locked_high + 1; } if (lnum != 0) { - /* Count extra CR characters. */ - if (ffdos) + // Count extra CR characters. + if (ffdos) { size += lnum - 1; + } /* Don't count the last line break if 'noeol' and ('bin' or * 'nofixeol'). */ |