From: Lars Ingebrigtsen Date: Sun, 13 Feb 2022 09:56:20 +0000 (+0100) Subject: Make `C-u M-x count-words' also give totals X-Git-Tag: emacs-29.0.90~2340 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=242a2765d3970641887be7a6dedcc14b07fade7e;p=emacs.git Make `C-u M-x count-words' also give totals * lisp/simple.el (count-words-region): Adjust callers. (count-words): If given a prefix, give totals (bug#9959). (count-words--buffer-format, count-words--format): Rename and don't message, but return the string. --- diff --git a/etc/NEWS b/etc/NEWS index 1b0e26da9b9..169208d94fc 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -144,6 +144,9 @@ An autoload definition appears just as a '(defun . NAME)' and the * Changes in Emacs 29.1 +--- +** 'count-lines' will now report buffer totals if given a prefix. + --- ** New user option 'find-library-include-other-files'. If set to nil, commands like 'find-library' will only include library diff --git a/lisp/simple.el b/lisp/simple.el index af51c99b285..bd1138ac856 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1477,46 +1477,59 @@ START and END." (cond ((not (called-interactively-p 'any)) (count-words start end)) (arg - (count-words--buffer-message)) + (message "%s" (count-words--buffer-format))) (t - (count-words--message "Region" start end)))) + (message "%s" (count-words--format "Region" start end))))) -(defun count-words (start end) +(defun count-words (start end &optional totals) "Count words between START and END. If called interactively, START and END are normally the start and end of the buffer; but if the region is active, START and END are the start and end of the region. Print a message reporting the -number of lines, words, and chars. +number of lines, words, and chars. If given a prefix, also +include the data for the total (un-narrowed) buffer. If called from Lisp, return the number of words between START and -END, without printing any message." - (interactive (list nil nil)) - (cond ((not (called-interactively-p 'any)) - (let ((words 0) - ;; Count across field boundaries. (Bug#41761) - (inhibit-field-text-motion t)) - (save-excursion - (save-restriction - (narrow-to-region start end) - (goto-char (point-min)) - (while (forward-word-strictly 1) - (setq words (1+ words))))) - words)) - ((use-region-p) - (call-interactively 'count-words-region)) - (t - (count-words--buffer-message)))) - -(defun count-words--buffer-message () - (count-words--message +END, without printing any message. TOTAL is ignored when called +from Lisp." + (interactive (list nil nil current-prefix-arg)) + ;; When called from Lisp, return the data. + (if (not (called-interactively-p 'any)) + (let ((words 0) + ;; Count across field boundaries. (Bug#41761) + (inhibit-field-text-motion t)) + (save-excursion + (save-restriction + (narrow-to-region start end) + (goto-char (point-min)) + (while (forward-word-strictly 1) + (setq words (1+ words))))) + words) + ;; When called interactively, message the data. + (let ((totals (if (and totals + (or (use-region-p) + (buffer-narrowed-p))) + (save-restriction + (widen) + (count-words--format "; buffer in total" + (point-min) (point-max))) + ""))) + (if (use-region-p) + (message "%s%s" (count-words--format + "Region" (region-beginning) (region-end)) + totals) + (message "%s%s" (count-words--buffer-format) totals))))) + +(defun count-words--buffer-format () + (count-words--format (if (buffer-narrowed-p) "Narrowed part of buffer" "Buffer") (point-min) (point-max))) -(defun count-words--message (str start end) +(defun count-words--format (str start end) (let ((lines (count-lines start end)) (words (count-words start end)) (chars (- end start))) - (message "%s has %d line%s, %d word%s, and %d character%s." + (format "%s has %d line%s, %d word%s, and %d character%s" str lines (if (= lines 1) "" "s") words (if (= words 1) "" "s")