diff options
Diffstat (limited to 'src/nvim/fileio.c')
-rw-r--r-- | src/nvim/fileio.c | 205 |
1 files changed, 98 insertions, 107 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index bd78818ab2..cf4037308b 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -224,8 +224,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski bool keep_dest_enc = false; // don't retry when char doesn't fit // in destination encoding int bad_char_behavior = BAD_REPLACE; - /* BAD_KEEP, BAD_DROP or character to - * replace with */ + // BAD_KEEP, BAD_DROP or character to + // replace with char_u *tmpname = NULL; // name of 'charconvert' output file int fio_flags = 0; char_u *fenc; // fileencoding to use @@ -280,8 +280,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski || (sfname == curbuf->b_ffname); using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); - /* After reading a file the cursor line changes but we don't want to - * display the line. */ + // After reading a file the cursor line changes but we don't want to + // display the line. ex_no_reprint = true; // don't display the file info for another buffer now @@ -454,9 +454,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski // been created by someone else, a ":w" will complain. curbuf->b_flags |= BF_NEW; - /* Create a swap file now, so that other Vims are warned - * that we are editing this file. Don't do this for a - * "nofile" or "nowrite" buffer type. */ + // Create a swap file now, so that other Vims are warned + // that we are editing this file. Don't do this for a + // "nofile" or "nowrite" buffer type. if (!bt_dontwrite(curbuf)) { check_need_swap(newfile); // SwapExists autocommand may mess things up @@ -465,7 +465,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski && (old_b_ffname != curbuf->b_ffname)) || (using_b_fname && (old_b_fname != curbuf->b_fname))) { - EMSG(_(e_auchangedbuf)); + emsg(_(e_auchangedbuf)); return FAIL; } } @@ -524,16 +524,16 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski curbuf->b_start_bomb = FALSE; } - /* Create a swap file now, so that other Vims are warned that we are - * editing this file. - * Don't do this for a "nofile" or "nowrite" buffer type. */ + // Create a swap file now, so that other Vims are warned that we are + // editing this file. + // Don't do this for a "nofile" or "nowrite" buffer type. if (!bt_dontwrite(curbuf)) { check_need_swap(newfile); if (!read_stdin && (curbuf != old_curbuf || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) || (using_b_fname && (old_b_fname != curbuf->b_fname)))) { - EMSG(_(e_auchangedbuf)); + emsg(_(e_auchangedbuf)); if (!read_buffer) { close(fd); } @@ -643,9 +643,9 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski --no_wait_return; msg_scroll = msg_save; if (fd < 0) { - EMSG(_("E200: *ReadPre autocommands made the file unreadable")); + emsg(_("E200: *ReadPre autocommands made the file unreadable")); } else { - EMSG(_("E201: *ReadPre autocommands must not change current buffer")); + emsg(_("E201: *ReadPre autocommands must not change current buffer")); } curbuf->b_p_ro = TRUE; // must use "w!" now return FAIL; @@ -832,7 +832,6 @@ retry: } - #ifdef HAVE_ICONV // Try using iconv() if we can't convert internally. if (fio_flags == 0 @@ -854,8 +853,8 @@ retry: #ifdef HAVE_ICONV did_iconv = false; #endif - /* Skip conversion when it's already done (retry for wrong - * "fileformat"). */ + // Skip conversion when it's already done (retry for wrong + // "fileformat"). if (tmpname == NULL) { tmpname = readfile_charconvert(fname, fenc, &fd); if (tmpname == NULL) { @@ -863,7 +862,7 @@ retry: advance_fenc = true; if (fd < 0) { // Re-opening the original file failed! - EMSG(_("E202: Conversion made file unreadable!")); + emsg(_("E202: Conversion made file unreadable!")); error = true; goto failed; } @@ -876,17 +875,17 @@ retry: && iconv_fd == (iconv_t)-1 #endif ) { - /* Conversion wanted but we can't. - * Try the next conversion in 'fileencodings' */ + // Conversion wanted but we can't. + // Try the next conversion in 'fileencodings' advance_fenc = true; goto retry; } } } - /* Set "can_retry" when it's possible to rewind the file and try with - * another "fenc" value. It's FALSE when no other "fenc" to try, reading - * stdin or fixed at a specific encoding. */ + // Set "can_retry" when it's possible to rewind the file and try with + // another "fenc" value. It's FALSE when no other "fenc" to try, reading + // stdin or fixed at a specific encoding. can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc && !read_fifo); if (!skip_read) { @@ -1014,9 +1013,9 @@ retry: read_buf_col += n; break; } else { - /* Append whole line and new-line. Change NL - * to NUL to reverse the effect done below. */ - for (ni = 0; ni < n; ++ni) { + // Append whole line and new-line. Change NL + // to NUL to reverse the effect done below. + for (ni = 0; ni < n; ni++) { if (p[ni] == NL) { ptr[tlen++] = NUL; } else { @@ -1076,10 +1075,10 @@ retry: *(ptr - conv_restlen) = NUL; conv_restlen = 0; } else { - /* Replace the trailing bytes with the replacement - * character if we were converting; if we weren't, - * leave the UTF8 checking code to do it, as it - * works slightly differently. */ + // Replace the trailing bytes with the replacement + // character if we were converting; if we weren't, + // leave the UTF8 checking code to do it, as it + // works slightly differently. if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 #ifdef HAVE_ICONV || iconv_fd != (iconv_t)-1 @@ -1212,8 +1211,8 @@ retry: } if (from_size > 0) { - /* Some remaining characters, keep them for the next - * round. */ + // Some remaining characters, keep them for the next + // round. memmove(conv_rest, (char_u *)fromp, from_size); conv_restlen = (int)from_size; } @@ -1519,7 +1518,6 @@ rewind_retry: try_mac = 1; try_unix = 1; for (; p >= ptr && *p != CAR; p--) { - ; } if (p >= ptr) { for (p = ptr; p < ptr + size; ++p) { @@ -1752,11 +1750,11 @@ failed: } if (newfile || read_buffer) { redraw_curbuf_later(NOT_VALID); - /* After reading the text into the buffer the diff info needs to - * be updated. */ + // After reading the text into the buffer the diff info needs to + // be updated. diff_invalidate(curbuf); - /* All folds in the window are invalid now. Mark them for update - * before triggering autocommands. */ + // All folds in the window are invalid now. Mark them for update + // before triggering autocommands. foldUpdateAll(curwin); } else if (linecnt) { // appended at least one line appended_lines_mark(from, linecnt); @@ -1849,7 +1847,7 @@ failed: msg_scrolled_ign = true; if (!read_stdin && !read_buffer) { - p = msg_trunc_attr(IObuff, FALSE, 0); + p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0); } if (read_stdin || read_buffer || restart_edit != 0 @@ -2111,27 +2109,27 @@ static char_u *next_fenc(char_u **pp, bool *alloced) static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp) { char_u *tmpname; - char_u *errmsg = NULL; + char *errmsg = NULL; tmpname = vim_tempname(); if (tmpname == NULL) { - errmsg = (char_u *)_("Can't find temp file for conversion"); + errmsg = _("Can't find temp file for conversion"); } else { close(*fdp); // close the input file, ignore errors *fdp = -1; if (eval_charconvert((char *)fenc, "utf-8", (char *)fname, (char *)tmpname) == FAIL) { - errmsg = (char_u *)_("Conversion with 'charconvert' failed"); + errmsg = _("Conversion with 'charconvert' failed"); } if (errmsg == NULL && (*fdp = os_open((char *)tmpname, O_RDONLY, 0)) < 0) { - errmsg = (char_u *)_("can't read output of 'charconvert'"); + errmsg = _("can't read output of 'charconvert'"); } } if (errmsg != NULL) { - /* Don't use emsg(), it breaks mappings, the retry with - * another type of conversion might still work. */ - MSG(errmsg); + // Don't use emsg(), it breaks mappings, the retry with + // another type of conversion might still work. + msg(errmsg); if (tmpname != NULL) { os_remove((char *)tmpname); // delete converted file XFREE_CLEAR(tmpname); @@ -2257,7 +2255,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (buf->b_ml.ml_mfp == NULL) { /* This can happen during startup when there is a stray "w" in the * vimrc file. */ - EMSG(_(e_emptybuf)); + emsg(_(e_emptybuf)); return FAIL; } @@ -2271,7 +2269,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ // Avoid a crash for a long name. if (STRLEN(fname) >= MAXPATHL) { - EMSG(_(e_longname)); + emsg(_(e_longname)); return FAIL; } @@ -2433,7 +2431,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ --no_wait_return; msg_scroll = msg_save; if (nofile_err) { - EMSG(_("E676: No matching autocommands for acwrite buffer")); + emsg(_("E676: No matching autocommands for acwrite buffer")); } if (nofile_err @@ -2466,7 +2464,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ return OK; } if (!aborting()) { - EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); + emsg(_("E203: Autocommands deleted or unloaded buffer to be written")); } return FAIL; } @@ -2487,7 +2485,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (end < start) { --no_wait_return; msg_scroll = msg_save; - EMSG(_("E204: Autocommand changed number of lines in unexpected way")); + emsg(_("E204: Autocommand changed number of lines in unexpected way")); return FAIL; } } @@ -2772,7 +2770,7 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ int ret; char *failed_dir; if ((ret = os_mkdir_recurse((char *)IObuff, 0755, &failed_dir)) != 0) { - EMSG3(_("E303: Unable to create directory \"%s\" for backup file: %s"), + semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), failed_dir, os_strerror(ret)); xfree(failed_dir); } @@ -2936,7 +2934,7 @@ nobackup: int ret; char *failed_dir; if ((ret = os_mkdir_recurse((char *)IObuff, 0755, &failed_dir)) != 0) { - EMSG3(_("E303: Unable to create directory \"%s\" for backup file: %s"), + semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), failed_dir, os_strerror(ret)); xfree(failed_dir); } @@ -3083,7 +3081,6 @@ nobackup: } - if (converted && wb_flags == 0) { #ifdef HAVE_ICONV // Use iconv() conversion when conversion is needed and it's not done @@ -3154,8 +3151,8 @@ nobackup: O_WRONLY | (append ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) - : (O_CREAT | O_TRUNC)) - , perm < 0 ? 0666 : (perm & 0777))) < 0) { + : (O_CREAT | O_TRUNC)), + perm < 0 ? 0666 : (perm & 0777))) < 0) { // A forced write will try to create a new file if the old one // is still readonly. This may also happen when the directory // is read-only. In that case the mch_remove() will fail. @@ -3474,7 +3471,7 @@ restore_backup: // This may take a while, if we were interrupted let the user // know we got the message. if (got_int) { - MSG(_(e_interr)); + msg(_(e_interr)); ui_flush(); } @@ -3539,7 +3536,7 @@ restore_backup: } } - set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); + set_keep_msg((char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0), 0); } /* When written everything correctly: reset 'modified'. Unless not @@ -3584,7 +3581,7 @@ restore_backup: * the current backup file becomes the original file */ if (org == NULL) { - EMSG(_("E205: Patchmode: can't save original file")); + emsg(_("E205: Patchmode: can't save original file")); } else if (!os_path_exists((char_u *)org)) { vim_rename(backup, (char_u *)org); XFREE_CLEAR(backup); // don't delete the file @@ -3606,7 +3603,7 @@ restore_backup: || (empty_fd = os_open(org, O_CREAT | O_EXCL | O_NOFOLLOW, perm < 0 ? 0666 : (perm & 0777))) < 0) { - EMSG(_("E206: patchmode: can't touch empty original file")); + emsg(_("E206: patchmode: can't touch empty original file")); } else { close(empty_fd); } @@ -3623,7 +3620,7 @@ restore_backup: if (!p_bk && backup != NULL && !write_info.bw_conv_error && os_remove((char *)backup) != 0) { - EMSG(_("E207: Can't delete backup file")); + emsg(_("E207: Can't delete backup file")); } goto nofail; @@ -3663,14 +3660,14 @@ nofail: #endif if (errnum != NULL) { if (errmsgarg != 0) { - emsgf("%s: %s%s: %s", errnum, IObuff, errmsg, os_strerror(errmsgarg)); + semsg("%s: %s%s: %s", errnum, IObuff, errmsg, os_strerror(errmsgarg)); } else { - emsgf("%s: %s%s", errnum, IObuff, errmsg); + semsg("%s: %s%s", errnum, IObuff, errmsg); } } else if (errmsgarg != 0) { - emsgf(errmsg, os_strerror(errmsgarg)); + semsg(errmsg, os_strerror(errmsgarg)); } else { - EMSG(errmsg); + emsg(errmsg); } if (errmsg_allocated) { xfree(errmsg); @@ -3679,9 +3676,9 @@ nofail: retval = FAIL; if (end == 0) { const int attr = HL_ATTR(HLF_E); // Set highlight for error messages. - MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), + msg_puts_attr(_("\nWARNING: Original file may be lost or damaged\n"), attr | MSG_HIST); - MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), + msg_puts_attr(_("don't quit the editor until the file is successfully written!"), attr | MSG_HIST); /* Update the timestamp to avoid an "overwrite changed file" @@ -3764,7 +3761,7 @@ static int set_rw_fname(char_u *fname, char_u *sfname) } if (curbuf != buf) { // We are in another buffer now, don't do the renaming. - EMSG(_(e_auchangedbuf)); + emsg(_(e_auchangedbuf)); return FAIL; } @@ -3852,20 +3849,16 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) *p++ = ' '; } if (shortmess(SHM_LINES)) { - sprintf((char *)p, "%" PRId64 "L, %" PRId64 "C", - (int64_t)lnum, (int64_t)nchars); + vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "C", + (int64_t)lnum, (int64_t)nchars); } else { - if (lnum == 1) { - STRCPY(p, _("1 line, ")); - } else { - sprintf((char *)p, _("%" PRId64 " lines, "), (int64_t)lnum); - } + vim_snprintf((char *)p, IOSIZE - (p - IObuff), + NGETTEXT("%" PRId64 " line, ", "%" PRId64 " lines, ", lnum), + (int64_t)lnum); p += STRLEN(p); - if (nchars == 1) { - STRCPY(p, _("1 character")); - } else { - sprintf((char *)p, _("%" PRId64 " characters"), (int64_t)nchars); - } + vim_snprintf((char *)p, IOSIZE - (p - IObuff), + NGETTEXT("%" PRId64 " character", "%" PRId64 " characters", nchars), + (int64_t)nchars); } } @@ -3908,9 +3901,9 @@ static int check_mtime(buf_T *buf, FileInfo *file_info) static bool time_differs(long t1, long t2) FUNC_ATTR_CONST { #if defined(__linux__) || defined(MSWIN) - /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store - * the seconds. Since the roundoff is done when flushing the inode, the - * time may change unexpectedly by one second!!! */ + // On a FAT filesystem, esp. under Linux, there are only 5 bits to store + // the seconds. Since the roundoff is done when flushing the inode, the + // time may change unexpectedly by one second!!! return t1 - t2 > 1 || t2 - t1 > 1; #else return t1 != t2; @@ -4192,8 +4185,8 @@ static bool need_conversion(const char_u *fenc) same_encoding = TRUE; fenc_flags = 0; } else { - /* Ignore difference between "ansi" and "latin1", "ucs-4" and - * "ucs-4be", etc. */ + // Ignore difference between "ansi" and "latin1", "ucs-4" and + // "ucs-4be", etc. enc_flags = get_fio_flags(p_enc); fenc_flags = get_fio_flags(fenc); same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); @@ -4203,8 +4196,8 @@ static bool need_conversion(const char_u *fenc) return false; } - /* Encodings differ. However, conversion is not needed when 'enc' is any - * Unicode encoding and the file is UTF-8. */ + // Encodings differ. However, conversion is not needed when 'enc' is any + // Unicode encoding and the file is UTF-8. return !(fenc_flags == FIO_UTF8); } @@ -4250,7 +4243,6 @@ static int get_fio_flags(const char_u *name) } - /* * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. * "size" must be at least 2. @@ -4684,7 +4676,7 @@ int vim_rename(const char_u *from, const char_u *to) } STRCPY(tempname, from); for (n = 123; n < 99999; n++) { - char * tail = (char *)path_tail(tempname); + char *tail = (char *)path_tail(tempname); snprintf(tail, (MAXPATHL + 1) - (tail - (char *)tempname - 1), "%d", n); if (!os_path_exists(tempname)) { @@ -4697,8 +4689,8 @@ int vim_rename(const char_u *from, const char_u *to) (void)os_rename(tempname, from); return -1; } - /* If it fails for one temp name it will most likely fail - * for any temp name, give up. */ + // If it fails for one temp name it will most likely fail + // for any temp name, give up. return -1; } } @@ -4783,7 +4775,7 @@ int vim_rename(const char_u *from, const char_u *to) mch_free_acl(acl); #endif if (errmsg != NULL) { - EMSG2(errmsg, to); + semsg(errmsg, to); return -1; } os_remove((char *)from); @@ -4954,8 +4946,8 @@ int buf_check_timestamp(buf_T *buf) buf_store_file_info(buf, &file_info); } - /* Don't do anything for a directory. Might contain the file - * explorer. */ + // Don't do anything for a directory. Might contain the file + // explorer. if (os_isdir(buf->b_fname)) { } else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) && !bufIsChanged(buf) && file_info_ok) { @@ -4989,7 +4981,7 @@ int buf_check_timestamp(buf_T *buf) busy = false; if (n) { if (!bufref_valid(&bufref)) { - EMSG(_("E246: FileChangedShell autocommand deleted buffer")); + emsg(_("E246: FileChangedShell autocommand deleted buffer")); } s = get_vim_var_str(VV_FCS_CHOICE); if (STRCMP(s, "reload") == 0 && *reason != 'd') { @@ -5065,7 +5057,7 @@ int buf_check_timestamp(buf_T *buf) xstrlcat(tbuf, "; ", tbuf_len - 1); xstrlcat(tbuf, mesg2, tbuf_len - 1); } - EMSG(tbuf); + emsg(tbuf); retval = 2; } else { if (!autocmd_busy) { @@ -5170,7 +5162,7 @@ void buf_reload(buf_T *buf, int orig_mode) } if (savebuf == NULL || saved == FAIL || buf != curbuf || move_lines(buf, savebuf) == FAIL) { - EMSG2(_("E462: Could not prepare for reloading \"%s\""), + semsg(_("E462: Could not prepare for reloading \"%s\""), buf->b_fname); saved = FAIL; } @@ -5182,7 +5174,7 @@ void buf_reload(buf_T *buf, int orig_mode) if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, &ea, flags) != OK) { if (!aborting()) { - EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); + semsg(_("E321: Could not reload \"%s\""), buf->b_fname); } if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) { // Put the text back from the save buffer. First @@ -5217,8 +5209,8 @@ void buf_reload(buf_T *buf, int orig_mode) // Invalidate diff info if necessary. diff_invalidate(curbuf); - /* Restore the topline and cursor position and check it (lines may - * have been removed). */ + // Restore the topline and cursor position and check it (lines may + // have been removed). if (old_topline > curbuf->b_ml.ml_line_count) { curwin->w_topline = curbuf->b_ml.ml_line_count; } else { @@ -5237,9 +5229,9 @@ void buf_reload(buf_T *buf, int orig_mode) } } - /* If the mode didn't change and 'readonly' was set, keep the old - * value; the user probably used the ":view" command. But don't - * reset it, might have had a read error. */ + // If the mode didn't change and 'readonly' was set, keep the old + // value; the user probably used the ":view" command. But don't + // reset it, might have had a read error. if (orig_mode == curbuf->b_orig_mode) { curbuf->b_p_ro |= old_ro; } @@ -5436,7 +5428,6 @@ char_u *vim_tempname(void) } - /// Tries matching a filename with a "pattern" ("prog" is NULL), or use the /// precompiled regprog "prog" ("pattern" is NULL). That avoids calling /// vim_regcomp() often. @@ -5718,9 +5709,9 @@ char_u *file_pat_to_reg_pat(const char_u *pat, const char_u *pat_end, char *allo reg_pat[i] = NUL; if (nested != 0) { if (nested < 0) { - EMSG(_("E219: Missing {.")); + emsg(_("E219: Missing {.")); } else { - EMSG(_("E220: Missing }.")); + emsg(_("E220: Missing }.")); } XFREE_CLEAR(reg_pat); } @@ -5754,8 +5745,8 @@ long write_eintr(int fd, void *buf, size_t bufsize) long ret = 0; long wlen; - /* Repeat the write() so long it didn't fail, other than being interrupted - * by a signal. */ + // Repeat the write() so long it didn't fail, other than being interrupted + // by a signal. while (ret < (long)bufsize) { wlen = write(fd, (char *)buf + ret, bufsize - ret); if (wlen < 0) { |