While configuring powerline, I use my own format for the mode-line. I now want to apply a different face to the battery load string, depending on the battery status (dis/charging). However, when I compute the charging status within my mode-line-format construct, Emacs gets really slow and I can't mark region with shift + arrow anymore.

You can see the mode-line definition in my configuration that I keep on Bitbucket: init-mode-line.el:102.

The idea behind it was to have a boolean describing the battery status, and use it in the manner of active, that I already use.

Here is the output of profile-report:

Profile report

  • 1
    Did you try profiling battery-status-function? gnu.org/software/emacs/manual/html_node/elisp/Profiling.html (here's the reference for how to do it, in case you need it). – wvxvw Jun 30 '15 at 14:01
  • This is a very good advice, I did not know that thank you :). Not sure how it can help me though. I have attached a screenshot of the profile-report output. I guess each redraw of the mode-line opens the battery file and read from it, which seems to be resource demanding. – Mathieu Marques Jun 30 '15 at 14:15
  • I don't know what battery-pmset does, but I would try memoisation, i.e. I'd try to store the old value somewhere and only poll the device once every couple of seconds or so. – wvxvw Jun 30 '15 at 14:42
  • Ended up using an advice, idea taken from smart-mode-line.el, but I'm depending on battery-update-interval. – Mathieu Marques Jun 30 '15 at 17:05

Your code calls battery-status-function for every mode line update. This function inherently slow, because it has to invoke external programs to obtain battery information. Since Emacs is single-threaded, each invocation freezes Emacs until the function returns. As the mode line is updated frequently, by almost every command, and often multiple times per command, these small delays amount to a very noticeable lag in all operations of Emacs, which renders Emacs almost entirely unusable.

I had the very same issue when I tried to customize the battery status appearance in my mode line, and wrote fancy-battery.el to address it. This package queries and caches the battery status independently of mode line updates, and thus reduces the frequency of invocations of battery-status-function, removing the lag entirely. It's written for the standard mode line, but it should work well with powerline, too.

| improve this answer | |

Shamelessly taken from smart-mode-line.el:1592. The defadvice will colorize the battery load string before every call to battery-update.

(defadvice battery-update (before my/pl-colorize-battery-advice activate)
  "Colorize the battery load string depending on its status (dis/charging)."

  (if (string-equal "AC" (cdr (assoc 76 (funcall battery-status-function))))
      (copy-face 'my/pl-battery-charging-face 'my/pl-battery-face)
    (copy-face 'my/pl-battery-discharging-face 'my/pl-battery-face)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.