bnbeckwith / wc-mode

Wordcount minor mode for Emacs
68 stars 11 forks source link

How to show wc-mode counts in a custom mode-line? #7

Closed stardiviner closed 7 years ago

stardiviner commented 8 years ago

I customize mode-line like this:

(setq-default
 mode-line-format
 (quote
  (
   (:eval (if (frame-parameter nil 'client)
              (propertize " あ "
                          'face '(:foreground "#333333" :background "yellow" :weight bold :height 120)
                          'help-echo "emacsclient frame")))))

Is there wc-mode has a function or variable to show counts in the custom mode-line?

holtzermann17 commented 8 years ago

Try something like (setq wc-modeline-format "[Words: %tw, Chars: %tc]").

stardiviner commented 8 years ago

@holtzermann17 Not just change wc-modeline-format. It's about the function or string variable which generated by wc-mdoe that can be used in my upper custom mode-line format.

Like the following command will not work.

(setq-default
 mode-line-format
 (quote
  (
   ;; wc-mode (word count) `wc-modeline-format'
   (:eval
    (if wc-mode
        (propertize " WC:[%tw]"
                    'face '(:foreground "cyan" :weight bold))))
)))
bnbeckwith commented 8 years ago

@stardiviner, You can look at the wc-format-modeline-string function in wc-mode.el line 168. It accepts a format as its argument.

I think in your example, it becomes:

(setq-default
 mode-line-format
 (quote
  (
   ;; wc-mode (word count) `wc-modeline-format'
   (:eval
    (if wc-mode
        (propertize (wc-format-modeline-string " WC:[%tw]")
                    'face '(:foreground "cyan" :weight bold))))
)))

Or something like that.

stardiviner commented 8 years ago

I got an error when I executing (wc-format-modeline-string " WC:[%tw]")

(wrong-type-argument number-or-marker-p nil)
  +(nil 0)
  (number-to-string (+ wc-orig-words wc-words-delta))
  eval((number-to-string (+ wc-orig-words wc-words-delta)))
  wc-format-modeline-string(" WC:[%tw]")
  eval((wc-format-modeline-string " WC:[%tw]") nil)
boudiccas commented 8 years ago

I have this in my modeline code and its working very well

'(:eval
    (if wc-mode
        (propertize (wc-format-modeline-string " Word Count:[%tw]")
                    'face '(:foreground "cyan" :weight bold))))
stardiviner commented 8 years ago

I figure out why now. I use edebug to debug function wc-format-modeline-string. then found in alist wc-modeline-format-alist. the pair ("%tw" . (number-to-string (+ wc-orig-words wc-words-delta)))'s value wc-orig-words is nil even the wc-mode is enabled, and the value of wc-mode is t. Then I check out wc-mode minor mode definition. Found :lighter (:eval (wc-mode-update)). Because I use custom mode-line, so the :light is not active. Is there other way to update with (wc-mode-update)? Like:

(add-hook 'wc-mode-hook #'wc-mode-update)

But this is not instantly. Maybe a timer is a solution? (Or maybe there is a way to add lighter into my custom mode-line solution?)

BTW: for wc-orig-words default value maybe should be 0 instead of nil?

stardiviner commented 7 years ago

@bnbeckwith How about my suggestion?

stardiviner commented 7 years ago

I found a solution which use an idle timer to run wc-mode-update.

(use-package wc-mode
  :ensure t
  :init
  (add-hook 'org-mode-hook 'wc-mode)
  :config
  (run-with-idle-timer (* 60 1) nil
                       'wc-mode-update)
  )