On Fr, 13 Nov 2015, Christian Brabandt wrote:
> On Di, 10 Nov 2015, [email protected] wrote:
> > Could we have a countword() function that would avoid such dirty tricks?
> I'll look into this.
Attached is a patch.
Best,
Christian
--
Niemand spricht eine Wahrheit aus, die er nicht mit einem Irrtum
verzollen müßte.
-- Christian Friedrich Hebbel
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -6686,6 +6686,27 @@ winwidth({nr}) *winwidth()*
: exe "normal 50\<C-W>|"
:endif
<
+wordcount() *wordcount()*
+ The result is a dictionary of byte/chars/word statistics for
+ the current buffer.
+ The return value includes:
+ chars Number of chars in the buffer
+ words Number of words in the buffer
+ bytes Number of bytes in the buffer
+ cursor_chars Number of chars until cursor position
+ (not in Visual mode)
+ cursor_bytes Number of bytes until cursor position
+ (not in Visual mode)
+ cursor_words Number of words until cursor position
+ (not in Visual mode)
+ visual_chars Number of chars visually selected
+ (only in Visual mode)
+ visual_bytes Number of bytes visually selected
+ (only in Visual mode)
+ visual_words Number of chars visually selected
+ (only in Visual mode)
+
+
*writefile()*
writefile({list}, {fname} [, {flags}])
Write |List| {list} to file {fname}. Each list item is
diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt
--- a/runtime/doc/usr_41.txt
+++ b/runtime/doc/usr_41.txt
@@ -918,6 +918,7 @@ Various: *various-functions*
mzeval() evaluate |MzScheme| expression
py3eval() evaluate Python expression (|+python3|)
pyeval() evaluate Python expression (|+python|)
+ wordcount() get byte/word/char count of buffer
==============================================================================
*41.7* Defining a function
diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -776,6 +776,7 @@ static void f_winrestview __ARGS((typval
static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
@@ -8377,6 +8378,7 @@ static struct fst
{"winrestview", 1, 1, f_winrestview},
{"winsaveview", 0, 0, f_winsaveview},
{"winwidth", 1, 1, f_winwidth},
+ {"wordcount", 0, 0, f_wordcount},
{"writefile", 2, 3, f_writefile},
{"xor", 2, 2, f_xor},
};
@@ -20031,6 +20033,22 @@ f_winwidth(argvars, rettv)
}
/*
+ * "wordcount()" function
+ */
+ static void
+f_wordcount(argvars, rettv)
+ typval_T *argvars UNUSED;
+ typval_T *rettv;
+{
+ dict_T *dict;
+
+ if (rettv_dict_alloc(rettv) == FAIL)
+ return;
+ dict = rettv->vval.v_dict;
+ cursor_pos_info(dict);
+}
+
+/*
* Write list of strings to file
*/
static int
diff --git a/src/normal.c b/src/normal.c
--- a/src/normal.c
+++ b/src/normal.c
@@ -8267,7 +8267,7 @@ nv_g_cmd(cap)
* "g CTRL-G": display info about cursor position
*/
case Ctrl_G:
- cursor_pos_info();
+ cursor_pos_info(NULL);
break;
/*
diff --git a/src/ops.c b/src/ops.c
--- a/src/ops.c
+++ b/src/ops.c
@@ -6943,13 +6943,15 @@ line_count_info(line, wc, cc, limit, eol
* the *_count_cursor variables store running totals for the selection.)
*/
void
-cursor_pos_info()
+cursor_pos_info(dict)
+ dict_T *dict; /* do not print output, just return info for wordcount() function */
{
char_u *p;
char_u buf1[50];
char_u buf2[40];
linenr_T lnum;
long byte_count = 0;
+ long bom_count = 0;
long byte_count_cursor = 0;
long char_count = 0;
long char_count_cursor = 0;
@@ -6967,7 +6969,11 @@ cursor_pos_info()
*/
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
- MSG(_(no_lines_msg));
+ if (dict == NULL)
+ {
+ MSG(_(no_lines_msg));
+ return;
+ }
}
else
{
@@ -7100,74 +7106,98 @@ cursor_pos_info()
if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
byte_count -= eol_size;
- if (VIsual_active)
+ if (dict != NULL)
{
- if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
+ if (VIsual_active)
{
- getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
- &max_pos.col);
- vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
- (long)(oparg.end_vcol - oparg.start_vcol + 1));
+ if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
+ {
+ getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
+ &max_pos.col);
+ vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
+ (long)(oparg.end_vcol - oparg.start_vcol + 1));
+ }
+ else
+ buf1[0] = NUL;
+
+ if (char_count_cursor == byte_count_cursor
+ && char_count == byte_count)
+ vim_snprintf((char *)IObuff, IOSIZE,
+ _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
+ buf1, line_count_selected,
+ (long)curbuf->b_ml.ml_line_count,
+ word_count_cursor, word_count,
+ byte_count_cursor, byte_count);
+ else
+ vim_snprintf((char *)IObuff, IOSIZE,
+ _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
+ buf1, line_count_selected,
+ (long)curbuf->b_ml.ml_line_count,
+ word_count_cursor, word_count,
+ char_count_cursor, char_count,
+ byte_count_cursor, byte_count);
}
else
- buf1[0] = NUL;
-
- if (char_count_cursor == byte_count_cursor
- && char_count == byte_count)
- vim_snprintf((char *)IObuff, IOSIZE,
- _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
- buf1, line_count_selected,
+ {
+ p = ml_get_curline();
+ validate_virtcol();
+ col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
+ (int)curwin->w_virtcol + 1);
+ col_print(buf2, sizeof(buf2), (int)STRLEN(p),
+ linetabsize(p));
+
+ if (char_count_cursor == byte_count_cursor
+ && char_count == byte_count)
+ vim_snprintf((char *)IObuff, IOSIZE,
+ _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
+ (char *)buf1, (char *)buf2,
+ (long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
byte_count_cursor, byte_count);
- else
- vim_snprintf((char *)IObuff, IOSIZE,
- _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
- buf1, line_count_selected,
+ else
+ vim_snprintf((char *)IObuff, IOSIZE,
+ _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
+ (char *)buf1, (char *)buf2,
+ (long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
char_count_cursor, char_count,
byte_count_cursor, byte_count);
+ }
+ }
+
+ /* Don't shorten this message, the user asked for it. */
+#ifdef FEAT_MBYTE
+ bom_count = bomb_size();
+ if (bom_count > 0)
+ sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
+ bom_count);
+#endif
+ if (dict == NULL)
+ {
+ p = p_shm;
+ p_shm = (char_u *)"";
+ msg(IObuff);
+ p_shm = p;
+ }
+ }
+ if (dict != NULL)
+ {
+ dict_add_nr_str(dict, "words", (long)word_count, NULL);
+ dict_add_nr_str(dict, "chars", (long)char_count, NULL);
+ dict_add_nr_str(dict, "bytes", (long)byte_count + bom_count, NULL);
+ if (VIsual_active)
+ {
+ dict_add_nr_str(dict, "visual_bytes", (long)byte_count_cursor, NULL);
+ dict_add_nr_str(dict, "visual_chars", (long)char_count_cursor, NULL);
+ dict_add_nr_str(dict, "visual_words", (long)word_count_cursor, NULL);
}
else
{
- p = ml_get_curline();
- validate_virtcol();
- col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
- (int)curwin->w_virtcol + 1);
- col_print(buf2, sizeof(buf2), (int)STRLEN(p),
- linetabsize(p));
-
- if (char_count_cursor == byte_count_cursor
- && char_count == byte_count)
- vim_snprintf((char *)IObuff, IOSIZE,
- _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
- (char *)buf1, (char *)buf2,
- (long)curwin->w_cursor.lnum,
- (long)curbuf->b_ml.ml_line_count,
- word_count_cursor, word_count,
- byte_count_cursor, byte_count);
- else
- vim_snprintf((char *)IObuff, IOSIZE,
- _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
- (char *)buf1, (char *)buf2,
- (long)curwin->w_cursor.lnum,
- (long)curbuf->b_ml.ml_line_count,
- word_count_cursor, word_count,
- char_count_cursor, char_count,
- byte_count_cursor, byte_count);
+ dict_add_nr_str(dict, "cursor_bytes", (long)byte_count_cursor, NULL);
+ dict_add_nr_str(dict, "cursor_chars", (long)char_count_cursor, NULL);
+ dict_add_nr_str(dict, "cursor_words", (long)word_count_cursor, NULL);
}
-
-#ifdef FEAT_MBYTE
- byte_count = bomb_size();
- if (byte_count > 0)
- sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
- byte_count);
-#endif
- /* Don't shorten this message, the user asked for it. */
- p = p_shm;
- p_shm = (char_u *)"";
- msg(IObuff);
- p_shm = p;
}
}
diff --git a/src/proto/ops.pro b/src/proto/ops.pro
--- a/src/proto/ops.pro
+++ b/src/proto/ops.pro
@@ -58,5 +58,5 @@ void write_reg_contents __ARGS((int name
void write_reg_contents_lst __ARGS((int name, char_u **strings, int maxlen, int must_append, int yank_type, long block_len));
void write_reg_contents_ex __ARGS((int name, char_u *str, int maxlen, int must_append, int yank_type, long block_len));
void clear_oparg __ARGS((oparg_T *oap));
-void cursor_pos_info __ARGS((void));
+void cursor_pos_info __ARGS((dict_T *eval));
/* vim: set ft=c : */