summary refs log tree commit diff stats
path: root/lisp/+org-wc.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/+org-wc.el')
-rw-r--r--lisp/+org-wc.el112
1 files changed, 0 insertions, 112 deletions
diff --git a/lisp/+org-wc.el b/lisp/+org-wc.el deleted file mode 100644 index 89b2708..0000000 --- a/lisp/+org-wc.el +++ /dev/null
@@ -1,112 +0,0 @@
1;;; +org-wc.el --- org-wc in the modeline -*- lexical-binding: t; -*-
2
3;;; Commentary:
4
5;;; Code:
6
7(require 'org-wc)
8(require '+modeline)
9(require 'cl-lib)
10
11(defgroup +org-wc nil
12 "Extra fast word-counting in `org-mode'"
13 :group 'org-wc
14 :group 'org)
15
16(defvar-local +org-wc-word-count nil
17 "Running total of words in this buffer.")
18
19(defcustom +org-wc-update-after-funcs '(org-narrow-to-subtree
20 org-narrow-to-block
21 org-narrow-to-element
22 org-capture-narrow)
23 "Functions after which to update the word count."
24 :type '(repeat function))
25
26(defcustom +org-wc-deletion-idle-timer 0.25
27 "Length of time, in seconds, to wait before updating word-count."
28 :type 'number)
29
30(defcustom +org-wc-huge-change 5000
31 "Number of characters that constitute a \"huge\" insertion."
32 :type 'number)
33
34(defcustom +org-wc-huge-buffer 10000
35 "Number of words past which we're not going to try to count."
36 :type 'number)
37
38(defvar +org-wc-correction -5
39 "Number to add to `+org-wc-word-count', for some reason?
40`+org-wc-word-count' seems to consistently be off by 5. Thus
41this correction. (At some point I should correct the underlying
42code... probably).")
43
44(defvar-local +org-wc-update-timer nil)
45
46(defun +org-wc-delayed-update (&rest _)
47 (if +org-wc-update-timer
48 (setq +org-wc-update-timer nil)
49 (setq +org-wc-update-timer
50 (run-with-idle-timer +org-wc-deletion-idle-timer nil #'+org-wc-update))))
51
52(defun +org-wc-force-update ()
53 (interactive)
54 (message "Counting words...")
55 (when (timerp +org-wc-update-timer)
56 (cancel-timer +org-wc-update-timer))
57 (+org-wc-update)
58 (message "Counting words...done"))
59
60(defun +org-wc-update (&rest _) ; Needs variadic parameters, since it's advice
61 (dlet ((+org-wc-counting t))
62 (+org-wc-buffer)
63 (force-mode-line-update)
64 (setq +org-wc-update-timer nil)))
65
66(defun +org-wc-changed (start end length)
67 (+org-wc-delayed-update))
68
69(defun +org-wc-buffer ()
70 "Count the words in the buffer."
71 (when (and (derived-mode-p 'org-mode)
72 (not (eq +org-wc-word-count 'huge)))
73 (setq +org-wc-word-count
74 (cond
75 ((> (count-words (point-min) (point-max))
76 +org-wc-huge-buffer)
77 'huge)
78 (t (org-word-count-aux (point-min) (point-max)))))))
79
80(defvar +org-wc-counting nil
81 "Are we currently counting?")
82
83(defun +org-wc-recount-widen (&rest _)
84 (when (and (not +org-wc-counting))
85 (+org-wc-update)))
86
87(defun +org-wc-modeline ()
88 (cond
89 ((eq +org-wc-word-count 'huge) "huge")
90 (+org-wc-word-count (format "%sw" (max 0 (+ +org-wc-word-count +org-wc-correction))))))
91
92(define-minor-mode +org-wc-mode
93 "Count words in `org-mode' buffers in the mode-line."
94 :lighter ""
95 :keymap (let ((map (make-sparse-keymap)))
96 (define-key map (kbd "C-c C-.") #'+org-wc-force-update)
97 map)
98 (if +org-wc-mode
99 (progn ; turn on
100 (+org-wc-buffer)
101 (add-hook 'after-change-functions #'+org-wc-delayed-update nil t)
102 (setq-local +modeline-position-function #'+org-wc-modeline)
103 (dolist (fn +org-wc-update-after-funcs)
104 (advice-add fn :after #'+org-wc-update)))
105 (progn ; turn off
106 (remove-hook 'after-change-functions #'+org-wc-delayed-update t)
107 (kill-local-variable '+modeline-position-function)
108 (dolist (fn +org-wc-update-after-funcs)
109 (advice-remove fn #'+org-wc-update)))))
110
111(provide '+org-wc)
112;;; +org-wc.el ends here