summaryrefslogtreecommitdiff
path: root/src/ops.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-01-03 22:49:16 +0100
committerBram Moolenaar <Bram@vim.org>2016-01-03 22:49:16 +0100
commited767a2073ef150971b0439a58e7ee582af6984e (patch)
treefac1ce186731fb31fc9e4e9f689b25284ca5c6c3 /src/ops.c
parent022b896592721838e387e99fd785d3ded7b68be7 (diff)
downloadvim-git-ed767a2073ef150971b0439a58e7ee582af6984e.tar.gz
patch 7.4.1042v7.4.1042
Problem: g-CTRL-G shows the word count, but there is no way to get the word count in a script. Solution: Add the wordcount() function. (Christian Brabandt)
Diffstat (limited to 'src/ops.c')
-rw-r--r--src/ops.c141
1 files changed, 86 insertions, 55 deletions
diff --git a/src/ops.c b/src/ops.c
index 31d7eb110..e710f66ef 100644
--- a/src/ops.c
+++ b/src/ops.c
@@ -6963,15 +6963,18 @@ line_count_info(line, wc, cc, limit, eol_size)
* Give some info about the position of the cursor (for "g CTRL-G").
* In Visual mode, give some info about the selected region. (In this case,
* the *_count_cursor variables store running totals for the selection.)
+ * When "dict" is not NULL store the info there instead of showing it.
*/
void
-cursor_pos_info()
+cursor_pos_info(dict)
+ dict_T *dict;
{
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;
@@ -6989,7 +6992,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
{
@@ -7122,74 +7129,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);
- }
- 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);
+ }
}
+ /* Don't shorten this message, the user asked for it. */
#ifdef FEAT_MBYTE
- byte_count = bomb_size();
- if (byte_count > 0)
+ bom_count = bomb_size();
+ if (bom_count > 0)
sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
- byte_count);
+ bom_count);
#endif
- /* Don't shorten this message, the user asked for it. */
- p = p_shm;
- p_shm = (char_u *)"";
- msg(IObuff);
- p_shm = p;
+ 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
+ {
+ 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);
+ }
}
}