comparison src/ops.c @ 7480:a49163681559 v7.4.1042

commit https://github.com/vim/vim/commit/ed767a2073ef150971b0439a58e7ee582af6984e Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 3 22:49:16 2016 +0100 patch 7.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)
author Christian Brabandt <cb@256bit.org>
date Sun, 03 Jan 2016 23:00:04 +0100
parents ad432f8f68fb
children 1fe988587423
comparison
equal deleted inserted replaced
7479:cae508519cf6 7480:a49163681559
6961 6961
6962 /* 6962 /*
6963 * Give some info about the position of the cursor (for "g CTRL-G"). 6963 * Give some info about the position of the cursor (for "g CTRL-G").
6964 * In Visual mode, give some info about the selected region. (In this case, 6964 * In Visual mode, give some info about the selected region. (In this case,
6965 * the *_count_cursor variables store running totals for the selection.) 6965 * the *_count_cursor variables store running totals for the selection.)
6966 * When "dict" is not NULL store the info there instead of showing it.
6966 */ 6967 */
6967 void 6968 void
6968 cursor_pos_info() 6969 cursor_pos_info(dict)
6970 dict_T *dict;
6969 { 6971 {
6970 char_u *p; 6972 char_u *p;
6971 char_u buf1[50]; 6973 char_u buf1[50];
6972 char_u buf2[40]; 6974 char_u buf2[40];
6973 linenr_T lnum; 6975 linenr_T lnum;
6974 long byte_count = 0; 6976 long byte_count = 0;
6977 long bom_count = 0;
6975 long byte_count_cursor = 0; 6978 long byte_count_cursor = 0;
6976 long char_count = 0; 6979 long char_count = 0;
6977 long char_count_cursor = 0; 6980 long char_count_cursor = 0;
6978 long word_count = 0; 6981 long word_count = 0;
6979 long word_count_cursor = 0; 6982 long word_count_cursor = 0;
6987 /* 6990 /*
6988 * Compute the length of the file in characters. 6991 * Compute the length of the file in characters.
6989 */ 6992 */
6990 if (curbuf->b_ml.ml_flags & ML_EMPTY) 6993 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6991 { 6994 {
6992 MSG(_(no_lines_msg)); 6995 if (dict == NULL)
6996 {
6997 MSG(_(no_lines_msg));
6998 return;
6999 }
6993 } 7000 }
6994 else 7001 else
6995 { 7002 {
6996 if (get_fileformat(curbuf) == EOL_DOS) 7003 if (get_fileformat(curbuf) == EOL_DOS)
6997 eol_size = 2; 7004 eol_size = 2;
7120 7127
7121 /* Correction for when last line doesn't have an EOL. */ 7128 /* Correction for when last line doesn't have an EOL. */
7122 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol)) 7129 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
7123 byte_count -= eol_size; 7130 byte_count -= eol_size;
7124 7131
7125 if (VIsual_active) 7132 if (dict == NULL)
7126 { 7133 {
7127 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) 7134 if (VIsual_active)
7128 { 7135 {
7129 getvcols(curwin, &min_pos, &max_pos, &min_pos.col, 7136 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7130 &max_pos.col); 7137 {
7131 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), 7138 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7132 (long)(oparg.end_vcol - oparg.start_vcol + 1)); 7139 &max_pos.col);
7140 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7141 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7142 }
7143 else
7144 buf1[0] = NUL;
7145
7146 if (char_count_cursor == byte_count_cursor
7147 && char_count == byte_count)
7148 vim_snprintf((char *)IObuff, IOSIZE,
7149 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
7150 buf1, line_count_selected,
7151 (long)curbuf->b_ml.ml_line_count,
7152 word_count_cursor, word_count,
7153 byte_count_cursor, byte_count);
7154 else
7155 vim_snprintf((char *)IObuff, IOSIZE,
7156 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
7157 buf1, line_count_selected,
7158 (long)curbuf->b_ml.ml_line_count,
7159 word_count_cursor, word_count,
7160 char_count_cursor, char_count,
7161 byte_count_cursor, byte_count);
7133 } 7162 }
7134 else 7163 else
7135 buf1[0] = NUL; 7164 {
7136 7165 p = ml_get_curline();
7137 if (char_count_cursor == byte_count_cursor 7166 validate_virtcol();
7138 && char_count == byte_count) 7167 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7139 vim_snprintf((char *)IObuff, IOSIZE, 7168 (int)curwin->w_virtcol + 1);
7140 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"), 7169 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7141 buf1, line_count_selected, 7170 linetabsize(p));
7171
7172 if (char_count_cursor == byte_count_cursor
7173 && char_count == byte_count)
7174 vim_snprintf((char *)IObuff, IOSIZE,
7175 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
7176 (char *)buf1, (char *)buf2,
7177 (long)curwin->w_cursor.lnum,
7142 (long)curbuf->b_ml.ml_line_count, 7178 (long)curbuf->b_ml.ml_line_count,
7143 word_count_cursor, word_count, 7179 word_count_cursor, word_count,
7144 byte_count_cursor, byte_count); 7180 byte_count_cursor, byte_count);
7145 else 7181 else
7146 vim_snprintf((char *)IObuff, IOSIZE, 7182 vim_snprintf((char *)IObuff, IOSIZE,
7147 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"), 7183 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
7148 buf1, line_count_selected, 7184 (char *)buf1, (char *)buf2,
7185 (long)curwin->w_cursor.lnum,
7149 (long)curbuf->b_ml.ml_line_count, 7186 (long)curbuf->b_ml.ml_line_count,
7150 word_count_cursor, word_count, 7187 word_count_cursor, word_count,
7151 char_count_cursor, char_count, 7188 char_count_cursor, char_count,
7152 byte_count_cursor, byte_count); 7189 byte_count_cursor, byte_count);
7190 }
7191 }
7192
7193 /* Don't shorten this message, the user asked for it. */
7194 #ifdef FEAT_MBYTE
7195 bom_count = bomb_size();
7196 if (bom_count > 0)
7197 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
7198 bom_count);
7199 #endif
7200 if (dict == NULL)
7201 {
7202 p = p_shm;
7203 p_shm = (char_u *)"";
7204 msg(IObuff);
7205 p_shm = p;
7206 }
7207 }
7208 if (dict != NULL)
7209 {
7210 dict_add_nr_str(dict, "words", (long)word_count, NULL);
7211 dict_add_nr_str(dict, "chars", (long)char_count, NULL);
7212 dict_add_nr_str(dict, "bytes", (long)byte_count + bom_count, NULL);
7213 if (VIsual_active)
7214 {
7215 dict_add_nr_str(dict, "visual_bytes", (long)byte_count_cursor, NULL);
7216 dict_add_nr_str(dict, "visual_chars", (long)char_count_cursor, NULL);
7217 dict_add_nr_str(dict, "visual_words", (long)word_count_cursor, NULL);
7153 } 7218 }
7154 else 7219 else
7155 { 7220 {
7156 p = ml_get_curline(); 7221 dict_add_nr_str(dict, "cursor_bytes", (long)byte_count_cursor, NULL);
7157 validate_virtcol(); 7222 dict_add_nr_str(dict, "cursor_chars", (long)char_count_cursor, NULL);
7158 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, 7223 dict_add_nr_str(dict, "cursor_words", (long)word_count_cursor, NULL);
7159 (int)curwin->w_virtcol + 1); 7224 }
7160 col_print(buf2, sizeof(buf2), (int)STRLEN(p), 7225 }
7161 linetabsize(p)); 7226 }
7162
7163 if (char_count_cursor == byte_count_cursor
7164 && char_count == byte_count)
7165 vim_snprintf((char *)IObuff, IOSIZE,
7166 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
7167 (char *)buf1, (char *)buf2,
7168 (long)curwin->w_cursor.lnum,
7169 (long)curbuf->b_ml.ml_line_count,
7170 word_count_cursor, word_count,
7171 byte_count_cursor, byte_count);
7172 else
7173 vim_snprintf((char *)IObuff, IOSIZE,
7174 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
7175 (char *)buf1, (char *)buf2,
7176 (long)curwin->w_cursor.lnum,
7177 (long)curbuf->b_ml.ml_line_count,
7178 word_count_cursor, word_count,
7179 char_count_cursor, char_count,
7180 byte_count_cursor, byte_count);
7181 }
7182
7183 #ifdef FEAT_MBYTE
7184 byte_count = bomb_size();
7185 if (byte_count > 0)
7186 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
7187 byte_count);
7188 #endif
7189 /* Don't shorten this message, the user asked for it. */
7190 p = p_shm;
7191 p_shm = (char_u *)"";
7192 msg(IObuff);
7193 p_shm = p;
7194 }
7195 }