aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/memline.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/memline.c')
-rw-r--r--src/nvim/memline.c69
1 files changed, 31 insertions, 38 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index 5acf4f0c37..92e2ef2b55 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -84,7 +84,7 @@
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
-#include "nvim/os/process.h"
+#include "nvim/os/proc.h"
#include "nvim/os/time.h"
#include "nvim/os/time_defs.h"
#include "nvim/path.h"
@@ -743,7 +743,7 @@ static void add_b0_fenc(ZeroBlock *b0p, buf_T *buf)
/// @param swap_fname Name of the swapfile. If it's from before a reboot, the result is 0.
///
/// @return PID, or 0 if process is not running or the swapfile is from before a reboot.
-static int swapfile_process_running(const ZeroBlock *b0p, const char *swap_fname)
+static int swapfile_proc_running(const ZeroBlock *b0p, const char *swap_fname)
{
FileInfo st;
double uptime;
@@ -905,12 +905,9 @@ void ml_recover(bool checkext)
msg_end();
goto theend;
}
- off_T size;
- if ((size = vim_lseek(mfp->mf_fd, 0, SEEK_END)) <= 0) {
- mfp->mf_blocknr_max = 0; // no file or empty file
- } else {
- mfp->mf_blocknr_max = size / mfp->mf_page_size;
- }
+ off_T size = vim_lseek(mfp->mf_fd, 0, SEEK_END);
+ // 0 means no file or empty file
+ mfp->mf_blocknr_max = size <= 0 ? 0 : size / mfp->mf_page_size;
mfp->mf_infile_count = mfp->mf_blocknr_max;
// need to reallocate the memory used to store the data
@@ -1217,7 +1214,7 @@ void ml_recover(bool checkext)
msg(_("Recovery completed. Buffer contents equals file contents."), 0);
}
msg_puts(_("\nYou may want to delete the .swp file now."));
- if (swapfile_process_running(b0p, fname_used)) {
+ if (swapfile_proc_running(b0p, fname_used)) {
// Warn there could be an active Vim on the same file, the user may
// want to kill it.
msg_puts(_("\nNote: process STILL RUNNING: "));
@@ -1253,7 +1250,7 @@ theend:
/// with the 'directory' option.
///
/// Used to:
-/// - list the swapfiles for "vim -r"
+/// - list the swapfiles for "nvim -r"
/// - count the number of swapfiles when recovering
/// - list the swapfiles when recovering
/// - list the swapfiles for swapfilelist()
@@ -1325,11 +1322,9 @@ int recover_names(char *fname, bool do_list, list_T *ret_list, int nr, char **fn
} else {
int len = (int)strlen(dir_name);
p = dir_name + len;
- if (after_pathsep(dir_name, p)
- && len > 1
- && p[-1] == p[-2]) {
+ if (after_pathsep(dir_name, p) && len > 1 && p[-1] == p[-2]) {
// Ends with '//', Use Full path for swap name
- tail = make_percent_swname(dir_name, fname_res);
+ tail = make_percent_swname(dir_name, p, fname_res);
} else {
tail = path_tail(fname_res);
tail = concat_fnames(dir_name, tail, true);
@@ -1440,8 +1435,11 @@ int recover_names(char *fname, bool do_list, list_T *ret_list, int nr, char **fn
/// Append the full path to name with path separators made into percent
/// signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"")
-char *make_percent_swname(const char *dir, const char *name)
- FUNC_ATTR_NONNULL_ARG(1)
+/// signs, to "dir". An unnamed buffer is handled as "" (<currentdir>/"")
+/// The last character in "dir" must be an extra slash or backslash, it is
+/// removed.
+char *make_percent_swname(char *dir, char *dir_end, const char *name)
+ FUNC_ATTR_NONNULL_ARG(1, 2)
{
char *d = NULL;
char *f = fix_fname(name != NULL ? name : "");
@@ -1455,6 +1453,8 @@ char *make_percent_swname(const char *dir, const char *name)
*d = '%';
}
}
+
+ dir_end[-1] = NUL; // remove one trailing slash
d = concat_fnames(dir, s, true);
xfree(s);
xfree(f);
@@ -1462,7 +1462,7 @@ char *make_percent_swname(const char *dir, const char *name)
}
// PID of swapfile owner, or zero if not running.
-static int process_running;
+static int proc_running;
/// For Vimscript "swapinfo()".
///
@@ -1488,7 +1488,7 @@ void swapfile_dict(const char *fname, dict_T *d)
tv_dict_add_str_len(d, S_LEN("fname"), b0.b0_fname,
B0_FNAME_SIZE_ORG);
- tv_dict_add_nr(d, S_LEN("pid"), swapfile_process_running(&b0, fname));
+ tv_dict_add_nr(d, S_LEN("pid"), swapfile_proc_running(&b0, fname));
tv_dict_add_nr(d, S_LEN("mtime"), char_to_long(b0.b0_mtime));
tv_dict_add_nr(d, S_LEN("dirty"), b0.b0_dirty ? 1 : 0);
tv_dict_add_nr(d, S_LEN("inode"), char_to_long(b0.b0_ino));
@@ -1572,7 +1572,7 @@ static time_t swapfile_info(char *fname)
if (char_to_long(b0.b0_pid) != 0) {
msg_puts(_("\n process ID: "));
msg_outnum((int)char_to_long(b0.b0_pid));
- if ((process_running = swapfile_process_running(&b0, fname))) {
+ if ((proc_running = swapfile_proc_running(&b0, fname))) {
msg_puts(_(" (STILL RUNNING)"));
}
}
@@ -1640,7 +1640,7 @@ static bool swapfile_unchanged(char *fname)
}
// process must be known and not running.
- if (char_to_long(b0.b0_pid) == 0 || swapfile_process_running(&b0, fname)) {
+ if (char_to_long(b0.b0_pid) == 0 || swapfile_proc_running(&b0, fname)) {
ret = false;
}
@@ -1895,9 +1895,7 @@ errorret:
buf->b_ml.ml_line_lnum = lnum;
return questions;
}
- if (lnum <= 0) { // pretend line 0 is line 1
- lnum = 1;
- }
+ lnum = MAX(lnum, 1); // pretend line 0 is line 1
if (buf->b_ml.ml_mfp == NULL) { // there are no lines
buf->b_ml.ml_line_len = 1;
@@ -2108,12 +2106,8 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char *line, colnr_T len, boo
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.
- int offset;
- if (db_idx < 0) {
- offset = (int)dp->db_txt_end;
- } else {
- offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK);
- }
+ int offset = db_idx < 0 ? (int)dp->db_txt_end
+ : (int)((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 + (size_t)len));
@@ -3192,11 +3186,10 @@ char *makeswapname(char *fname, char *ffname, buf_T *buf, char *dir_name)
int len = (int)strlen(dir_name);
char *s = dir_name + len;
- if (after_pathsep(dir_name, s)
- && len > 1
- && s[-1] == s[-2]) { // Ends with '//', Use Full path
+ if (after_pathsep(dir_name, s) && len > 1 && s[-1] == s[-2]) {
+ // Ends with '//', Use Full path
char *r = NULL;
- s = make_percent_swname(dir_name, fname_res);
+ s = make_percent_swname(dir_name, s, fname_res);
if (s != NULL) {
r = modname(s, ".swp", false);
xfree(s);
@@ -3287,7 +3280,7 @@ static void attention_message(buf_T *buf, char *fname)
" instances of the same\n file when making changes."
" Quit, or continue with caution.\n"));
msg_puts(_("(2) An edit session for this file crashed.\n"));
- msg_puts(_(" If this is the case, use \":recover\" or \"vim -r "));
+ msg_puts(_(" If this is the case, use \":recover\" or \"nvim -r "));
msg_outtrans(buf->b_fname, 0);
msg_puts(_("\"\n to recover the changes (see \":help recovery\").\n"));
msg_puts(_(" If you did this already, delete the swap file \""));
@@ -3406,7 +3399,7 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, bool *found_
fd = os_open(fname, O_RDONLY, 0);
if (fd >= 0) {
if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) {
- process_running = swapfile_process_running(&b0, fname);
+ proc_running = swapfile_proc_running(&b0, fname);
// If the swapfile has the same directory as the
// buffer don't compare the directory names, they can
@@ -3466,7 +3459,7 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, bool *found_
choice = SEA_CHOICE_READONLY;
}
- process_running = 0; // Set by attention_message..swapfile_info.
+ proc_running = 0; // Set by attention_message..swapfile_info.
if (choice == SEA_CHOICE_NONE) {
// Show info about the existing swapfile.
attention_message(buf, fname);
@@ -3498,12 +3491,12 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, bool *found_
= do_dialog(VIM_WARNING,
_("VIM - ATTENTION"),
name,
- process_running
+ proc_running
? _("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort")
: _("&Open Read-Only\n&Edit anyway\n&Recover\n&Delete it\n&Quit\n&Abort"),
1, NULL, false);
- if (process_running && dialog_result >= 4) {
+ if (proc_running && dialog_result >= 4) {
// compensate for missing "Delete it" button
dialog_result++;
}