refactor(misc1): move out misc functions which obviously belong elsewhere

Also make some function names more descriptive/regular.
This commit is contained in:
Björn Linse
2021-12-09 21:23:03 +01:00
parent 51822f0655
commit d9c1669a54
10 changed files with 73 additions and 82 deletions

View File

@@ -2719,7 +2719,7 @@ void buflist_list(exarg_T *eap)
IObuff[len++] = ' ';
} while (--i > 0 && len < IOSIZE - 18);
if (vim_strchr(eap->arg, 't') && buf->b_last_used) {
add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
undo_fmt_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
} else {
vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
_("line %" PRId64),

View File

@@ -131,7 +131,7 @@ void changed_internal(void)
curbuf->b_changed = true;
curbuf->b_changed_invalid = true;
ml_setflags(curbuf);
check_status(curbuf);
redraw_buf_status_later(curbuf);
redraw_tabline = true;
need_maketitle = true; // set window title later
}
@@ -517,7 +517,7 @@ void unchanged(buf_T *buf, int ff, bool always_inc_changedtick)
if (ff) {
save_file_ff(buf);
}
check_status(buf);
redraw_buf_status_later(buf);
redraw_tabline = true;
need_maketitle = true; // set window title later
buf_inc_changedtick(buf);

View File

@@ -4,6 +4,13 @@
#include "nvim/buffer_defs.h" // for buf_T
#include "nvim/pos.h" // for linenr_T
// flags for open_line()
#define OPENLINE_DELSPACES 1 // delete spaces after cursor
#define OPENLINE_DO_COM 2 // format comments
#define OPENLINE_KEEPTRAIL 4 // keep trailing spaces
#define OPENLINE_MARKFIX 8 // fix mark positions
#define OPENLINE_COM_LIST 16 // format comments with list/2nd line indent
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "change.h.generated.h"
#endif

View File

@@ -1841,6 +1841,17 @@ char_u *ml_get_pos(const pos_T *pos)
return ml_get_buf(curbuf, pos->lnum, false) + pos->col;
}
/// get codepoint at pos. pos must be either valid or have col set to MAXCOL!
int gchar_pos(pos_T *pos)
FUNC_ATTR_NONNULL_ARG(1)
{
// When searching columns is sometimes put at the end of a line.
if (pos->col == MAXCOL) {
return NUL;
}
return utf_ptr2char(ml_get_pos(pos));
}
/// 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)

View File

@@ -373,32 +373,6 @@ int get_last_leader_offset(char_u *line, char_u **flags)
return result;
}
int gchar_pos(pos_T *pos)
FUNC_ATTR_NONNULL_ARG(1)
{
// When searching columns is sometimes put at the end of a line.
if (pos->col == MAXCOL) {
return NUL;
}
return utf_ptr2char(ml_get_pos(pos));
}
/*
* check_status: called when the status bars for the buffer 'buf'
* need to be updated
*/
void check_status(buf_T *buf)
{
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_buffer == buf && wp->w_status_height) {
wp->w_redr_status = TRUE;
if (must_redraw < VALID) {
must_redraw = VALID;
}
}
}
}
/// Ask for a reply from the user, 'y' or 'n'
///
/// No other characters are accepted, the message is repeated until a valid
@@ -971,21 +945,6 @@ done:
return buffer;
}
/*
* Free the list of files returned by expand_wildcards() or other expansion
* functions.
*/
void FreeWild(int count, char_u **files)
{
if (count <= 0 || files == NULL) {
return;
}
while (count--) {
xfree(files[count]);
}
xfree(files);
}
/*
* Return TRUE when need to go to Insert mode because of 'insertmode'.
* Don't do this when still processing a command or a mapping.
@@ -995,26 +954,3 @@ int goto_im(void)
{
return p_im && stuff_empty() && typebuf_typed();
}
/// Put the timestamp of an undo header in "buf[buflen]" in a nice format.
void add_time(char_u *buf, size_t buflen, time_t tt)
{
struct tm curtime;
if (time(NULL) - tt >= 100) {
os_localtime_r(&tt, &curtime);
if (time(NULL) - tt < (60L * 60L * 12L)) {
// within 12 hours
(void)strftime((char *)buf, buflen, "%H:%M:%S", &curtime);
} else {
// longer ago
(void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", &curtime);
}
} else {
int64_t seconds = time(NULL) - tt;
vim_snprintf((char *)buf, buflen,
NGETTEXT("%" PRId64 " second ago",
"%" PRId64 " seconds ago", (uint32_t)seconds),
seconds);
}
}

View File

@@ -4,13 +4,6 @@
#include "nvim/os/shell.h"
#include "nvim/vim.h"
// flags for open_line()
#define OPENLINE_DELSPACES 1 // delete spaces after cursor
#define OPENLINE_DO_COM 2 // format comments
#define OPENLINE_KEEPTRAIL 4 // keep trailing spaces
#define OPENLINE_MARKFIX 8 // fix mark positions
#define OPENLINE_COM_LIST 16 // format comments with list/2nd line indent
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "misc1.h.generated.h"
#endif

View File

@@ -7898,7 +7898,7 @@ void set_fileformat(int eol_style, int opt_flags)
}
// This may cause the buffer to become (un)modified.
check_status(curbuf);
redraw_buf_status_later(curbuf);
redraw_tabline = true;
need_maketitle = true; // Set window title later.
}

View File

@@ -1337,6 +1337,17 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***fil
return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? OK : FAIL;
}
/// Free the list of files returned by expand_wildcards() or other expansion functions.
void FreeWild(int count, char_u **files)
{
if (count <= 0 || files == NULL) {
return;
}
while (count--) {
xfree(files[count]);
}
xfree(files);
}
/*
* Return TRUE if we can expand this backtick thing here.

View File

@@ -314,6 +314,19 @@ void update_curbuf(int type)
update_screen(type);
}
/// called when the status bars for the buffer 'buf' need to be updated
void redraw_buf_status_later(buf_T *buf)
{
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_buffer == buf && wp->w_status_height) {
wp->w_redr_status = true;
if (must_redraw < VALID) {
must_redraw = VALID;
}
}
}
}
/// Redraw the parts of the screen that is marked for redraw.
///
/// Most code shouldn't call this directly, rather use redraw_later() and

View File

@@ -2622,7 +2622,7 @@ static void u_undo_end(bool did_undo, bool absolute, bool quiet)
if (uhp == NULL) {
*msgbuf = NUL;
} else {
add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
undo_fmt_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
}
{
@@ -2642,6 +2642,29 @@ static void u_undo_end(bool did_undo, bool absolute, bool quiet)
msgbuf);
}
/// Put the timestamp of an undo header in "buf[buflen]" in a nice format.
void undo_fmt_time(char_u *buf, size_t buflen, time_t tt)
{
struct tm curtime;
if (time(NULL) - tt >= 100) {
os_localtime_r(&tt, &curtime);
if (time(NULL) - tt < (60L * 60L * 12L)) {
// within 12 hours
(void)strftime((char *)buf, buflen, "%H:%M:%S", &curtime);
} else {
// longer ago
(void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", &curtime);
}
} else {
int64_t seconds = time(NULL) - tt;
vim_snprintf((char *)buf, buflen,
NGETTEXT("%" PRId64 " second ago",
"%" PRId64 " seconds ago", (uint32_t)seconds),
seconds);
}
}
/// u_sync: stop adding to the current entry list
///
/// @param force if true, also sync when no_u_sync is set.
@@ -2684,16 +2707,13 @@ void ex_undolist(exarg_T *eap)
while (uhp != NULL) {
if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
&& uhp->uh_walk != mark) {
vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
uhp->uh_seq, changes);
add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
uhp->uh_time);
vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ", uhp->uh_seq, changes);
undo_fmt_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), uhp->uh_time);
if (uhp->uh_save_nr > 0) {
while (STRLEN(IObuff) < 33) {
STRCAT(IObuff, " ");
}
vim_snprintf_add((char *)IObuff, IOSIZE,
" %3ld", uhp->uh_save_nr);
vim_snprintf_add((char *)IObuff, IOSIZE, " %3ld", uhp->uh_save_nr);
}
GA_APPEND(char_u *, &ga, vim_strsave(IObuff));
}