summary refs log tree commit diff stats
path: root/lisp/org-word-count.el
blob: d6d2598bff749a6634b44aa408327e0198f47d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
;;; org-word-count.el --- org-word-count in the modeline -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'org)
(require 'cl-lib)

(defgroup org-word-count nil
  "Extra fast word-counting in `org-mode'."
  :group 'org)

(defvar-local org-word-count-word-count nil
  "Running total of words in this buffer.")

(defvar-local org-word-count-string nil
  "String for the modeline.")

(defcustom org-word-count-format "%sw "
  "Format for org word count in modeline."
  :type 'string)

(defcustom org-word-count-huge-string "huge"
  "String to display with a huge buffer."
  :type 'string)

(defcustom org-word-count-update-after-funcs '(org-narrow-to-subtree
                                               org-narrow-to-block
                                               org-narrow-to-element
                                               org-capture-narrow)
  "Functions after which to update the word count."
  :type '(repeat function))

(defcustom org-word-count-deletion-idle-timer 0.25
  "Length of time, in seconds, to wait before updating word-count."
  :type 'number)

(defcustom org-word-count-huge-change 5000
  "Number of characters that constitute a \"huge\" insertion."
  :type 'number)

(defcustom org-word-count-huge-buffer 10000
  "Number of words past which we're not going to try to count."
  :type 'number)

(defvar org-word-count-correction -5
  "Number to add to `org-word-count-word-count', for some reason?
`org-word-count-word-count' seems to consistently be off by 5.  Thus
this correction.  (At some point I should correct the underlying
code... probably).")

(defvar-local org-word-count-update-timer nil)

;;; Variables from org-wc

(defun org-word-count-list-of-strings-p (arg)
  (cl-every #'stringp arg))

(defun org-word-count--downcase-list-of-strings-set-default (var val)
  (set-default var (mapcar #'downcase val)))

(defcustom org-word-count-ignored-tags '("nowc" "noexport" "ARCHIVE")
  "List of tags for which subtrees will be ignored in word counts"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-ignore-commented-trees t
  "Ignore trees with COMMENT-prefix if non-nil."
  :type 'boolean
  :safe #'booleanp)

(defcustom org-word-count-default-link-count 'description-or-path
  "Default way of counting words in links.
This is applied to any link type not specified in any of
‘org-word-count-ignored-link-types’,‘org-word-count-one-word-link-types’, or
‘org-word-count-only-description-link-types’ "
  :type '(choice
          (const :tag "Count words in description or else path part of links" description-or-path)
          (const :tag "Count words only in description part of links" description)
          (const :tag "Count links as 0 words" ignore)
          (const :tag "Count links as 1 word" oneword)
          (const :tag "Count words only in path part of links" path))
  :safe 'symbolp)

(defcustom org-word-count-ignored-link-types nil
  "Link types which won't be counted as a word"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-one-word-link-types '("zotero")
  "Link types which will be counted as one word"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-description-or-path-link-types '()
  "Link types for which the description or the path should be counted"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-only-description-link-types '("note")
  "Link types for which only the description should be counted"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-only-path-link-types '()
  "Link types for which only the path should be counted"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p)

(defcustom org-word-count-blocks-to-count '("quote" "verse")
  "List of blocks which should be included in word count.

Use lower case block names"
  :type '(repeat string)
  :safe #'org-word-count-list-of-strings-p
  :set #'org-word-count--downcase-list-of-strings-set-default)

(defun org-word-count-delayed-update (&rest _)
  (if org-word-count-update-timer
      (setq org-word-count-update-timer nil)
    (setq org-word-count-update-timer
          (run-with-idle-timer org-word-count-deletion-idle-timer nil
                               #'org-word-count-update))))

(defun org-word-count-force-update ()
  (interactive)
  (message "Counting words...")
  (when (timerp org-word-count-update-timer)
    (cancel-timer org-word-count-update-timer))
  (org-word-count-update)
  (message "Counting words...done"))

(defun org-word-count-update (&rest _)         ; Needs variadic parameters, since it's advice
  (dlet ((org-word-count-counting t))
    (org-word-count-buffer)
    (org-word-count-modeline)
    (setq org-word-count-update-timer nil)))

(defun org-word-count-changed (start end length)
  (org-word-count-delayed-update))

(defun org-word-count-buffer ()
  "Count the words in the buffer."
  (when (and (derived-mode-p 'org-mode)
             (not (eq org-word-count-word-count 'huge)))
    (setq org-word-count-word-count
          (cond
           ((> (count-words (point-min) (point-max))
               org-word-count-huge-buffer)
            'huge)
           (t (org-word-count-aux (point-min) (point-max)))))))

;;; From org-wc.el:
;; https://github.com/tesujimath/org-wc/
(defun org-word-count-aux (beg end)
  "Return the number of words between BEG and END."
  (let ((wc 0)
        subtreecount
        (latex-macro-regexp "\\\\[A-Za-z]+\\(\\[[^]]*\\]\\|\\){\\([^}]*\\)}"))
    (save-excursion
      (goto-char beg)
      ;; Handle the case where we start in a drawer
      (when (org-at-drawer-p)
        (org-end-of-meta-data t))
      (while (< (point) end)
        (cond
         ;; Handle headlines and subtrees
         ((org-at-heading-p)
          (cond
           ;; Ignore commented and org-wc-ignored-tags trees
           ((or (and org-word-count-ignore-commented-trees (org-in-commented-heading-p))
                (cl-intersection org-word-count-ignored-tags (org-get-tags) :test #'string=))
            (org-end-of-subtree t t))
           ;; Re-use count for subtrees already counted
           ((setq subtreecount (get-text-property (point) :org-wc))
            (cl-incf wc subtreecount)
            (org-end-of-subtree t t))
           ;; Skip counting words in headline
           (t (org-word-count--goto-char (point-at-eol) end))))
         ;; Ignore most blocks.
         ((when (save-excursion
                  (beginning-of-line 1)
                  (looking-at org-block-regexp))
            (if (member (downcase (match-string 1)) org-word-count-blocks-to-count)
                (progn ;; go inside block and subtract count of end line
                  (org-word-count--goto-char (match-beginning 4) end)
                  (cl-decf wc))
              (org-word-count--goto-char (match-end 0) end))))
         ;; Ignore comments.
         ((org-at-comment-p)
          (org-word-count--goto-char (point-at-eol) end))
         ;; Ignore drawers.
         ((org-at-drawer-p)
          (org-end-of-meta-data t))
         ;; Ignore all other #+ lines
         ((looking-at "#+")
          (org-word-count--goto-char (point-at-eol) end))
         ;; Handle links
         ((save-excursion
            (when (< (1+ (point-min)) (point)) (backward-char 2))
            (looking-at org-link-bracket-re))
          (let* ((type (car (save-match-data (split-string (match-string 1) ":"))))
                 (pathstart (+ 1 (length type) (match-beginning 1))))
            (cl-case (cond ((member type org-word-count-ignored-link-types) 'ignore)
                           ((member type org-word-count-one-word-link-types) 'oneword)
                           ((member type org-word-count-only-description-link-types)
                            'description)
                           ((member type org-word-count-only-path-link-types) 'path)
                           ((member type org-word-count-description-or-path-link-types)
                            'description-or-path)
                           (t org-word-count-default-link-count))
              (ignore (org-word-count--goto-char (match-end 0) end))
              (oneword (org-word-count--goto-char (match-end 0) end)
                       (cl-incf wc))
              (description (if (match-beginning 2)
                               (goto-char (match-beginning 2))
                             (org-word-count--goto-char
                              (match-end 0) end)))
              (path (cl-incf wc (count-words-region pathstart
                                                    (match-end 1)))
                    (org-word-count--goto-char (match-end 0) end))
              (description-or-path
               (if (match-beginning 2)
                   (goto-char (match-beginning 2))
                 (cl-incf wc (count-words-region pathstart
                                                 (match-end 1)))
                 (org-word-count--goto-char (match-end 0) end)))
              (t (user-error "Error in org-word-count link configuration")))))
         ;; Count latex macros as 1 word, ignoring their arguments.
         ((save-excursion
            (when (< (point-min) (point)) (backward-char))
            (looking-at latex-macro-regexp))
          (org-word-count--goto-char (match-end 0) end)
          (cl-incf wc))
         (t
          (and (re-search-forward "\\w+\\W*" end 'skip)
               (cl-incf wc))))))
    wc))

(defun org-word-count--goto-char (char end)
  "Moves point to CHAR and from there passes 0+ non-word characters.
Searchers to end as a maximum.

This ensures that we are in an expected state (at the first word
character after some non-word characters) after moving beyond
headlines, links etc."
  (goto-char char)
  (re-search-forward "\\W*" end 'skip))

(defvar org-word-count-counting nil
  "Are we currently counting?")

(defun org-word-count-recount-widen (&rest _)
  (when (and (not org-word-count-counting))
    (org-word-count-update)))

(defun org-word-count-modeline ()
  (setq org-word-count-string
        (cond
         ((eq org-word-count-word-count 'huge)
          org-word-count-huge-string)
         (org-word-count-word-count
          (format org-word-count-format
                  (max 0 (+ org-word-count-word-count
                            org-word-count-correction))))))
  (force-mode-line-update))

(define-minor-mode org-word-count-mode
  "Count words in `org-mode' buffers in the mode-line."
  :lighter ""
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "C-c C-.") #'org-word-count-force-update)
            map)
  (cond (org-word-count-mode
         (org-word-count-buffer)
         (add-hook 'after-change-functions
                   #'org-word-count-delayed-update nil t)
         (unless (member '(org-word-count-mode org-word-count-string)
                         mode-line-misc-info)
           (add-to-list 'mode-line-misc-info
                        '(org-word-count-mode org-word-count-string)
                        nil
                        #'equal))
         (dolist (fn org-word-count-update-after-funcs)
           (advice-add fn :after #'org-word-count-update)))
        (:else
         (remove-hook 'after-change-functions
                      #'org-word-count-delayed-update t)
         (setf mode-line-misc-info
               (delete '(org-word-count-mode org-word-count-string)
                       mode-line-misc-info))
         (dolist (fn org-word-count-update-after-funcs)
           (advice-remove fn #'org-word-count-update)))))

(provide 'org-word-count)
;;; org-word-count.el ends here