From 41b48c481471406470268209220019a4c3331a3f Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 7 Aug 2021 15:07:04 -0500 Subject: Add `acdw-org/count-words-stupidly' To count words in an Org-mode buffer, disregarding headings and drawers completely. --- init.el | 3 ++- lisp/acdw-org.el | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index afd58ad..880986a 100644 --- a/init.el +++ b/init.el @@ -1161,7 +1161,8 @@ if ripgrep is installed, otherwise `consult-grep'." "" acdw-org/org-table-copy-down "M-SPC M-SPC" insert-zero-width-space "C-c C-l" org-insert-link-dwim - "M-w" kill-ring-save-unfilled) + "M-w" kill-ring-save-unfilled + "M-=" acdw-org/count-words-stupidly) (with-eval-after-load 'org-export (add-to-list 'org-export-filter-final-output-functions diff --git a/lisp/acdw-org.el b/lisp/acdw-org.el index 41073a9..6a11c4d 100644 --- a/lisp/acdw-org.el +++ b/lisp/acdw-org.el @@ -304,6 +304,42 @@ the deletion might narrow the column." (message "%d words in buffer" (acdw-org/count-words (point-min) (point-max)))))) +;; This isn't the best code, but it'll do. +(defun acdw-org/count-words-stupidly (start end) + "Count words between START and END, ignoring a lot." + (interactive (list nil nil)) + (cond ((not (called-interactively-p 'any)) + (let ((words 0)) + (save-excursion + (save-restriction + (narrow-to-region start end) + (goto-char (point-min)) + (while (< (point) (point-max)) + (cond + ;; Ignore comments + ((or (org-at-comment-p) + (org-in-commented-heading-p)) + (forward-line)) + ;; Ignore headings + ((or (org-at-heading-p)) + (forward-line)) + ;; Ignore drawers + ((or (looking-at org-drawer-regexp) + (looking-at org-clock-drawer-re)) + (search-forward ":END:")) + ;; Count everything else + (t (setq words (1+ words)) + (forward-word-strictly)))))) + words)) + ((use-region-p) + (message "%d words in region" + (acdw-org/count-words-stupidly (region-beginning) + (region-end)))) + (t + (message "%d words in buffer" + (acdw-org/count-words-stupidly (point-min) + (point-max)))))) + ;;; Zero-width spaces ;; https://blog.tecosaur.com/tmio/2021-05-31-async.html#easy-zero-width -- cgit 1.4.1-21-gabe81