From d9648318a6b992a01750de803baaa6d9e75e0c15 Mon Sep 17 00:00:00 2001 From: Jakob L. Kreuze Date: Thu, 23 May 2019 11:24:50 -0400 Subject: Update modeline --- jakob.org | 95 +++++++++++++++++++++++++-------------------------------------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/jakob.org b/jakob.org index 5f7e80d..7659720 100644 --- a/jakob.org +++ b/jakob.org @@ -94,6 +94,7 @@ A handful of the Elisp snippets in this file depend on third-party libraries which aren't loaded by default. #+BEGIN_SRC elisp +(require 'battery) (require 'cl-lib) (require 'cl-extra) #+END_SRC @@ -677,54 +678,13 @@ they look like." ** Modeline -This is essentially the vanilla modeline, just without the handful of components -I consider unimportant. - -#+BEGIN_SRC elisp -(setq-default mode-line-format - '("%e" - mode-line-front-space - mode-line-client - mode-line-modified - " " - mode-line-directory - mode-line-buffer-identification - " " - mode-line-position - " " - mode-line-modes - mode-line-misc-info - mode-line-end-spaces)) -#+END_SRC - -To make things a little easier on my eyes, I've chosen to add visual separation -between my additions to the modeline. - -#+BEGIN_SRC elisp -(defconst mode-line-separator " | " - "A separator to place between components of the modeline.") -#+END_SRC - -A function to simplify the addition of new =:eval= forms to the end of -=mode-line-format=. - -#+BEGIN_SRC elisp -(defun add-to-mode-line (widget) - "Appends a `MODE-LINE-SEPARATOR' and `WIDGET' onto the end of -`MODE-LINE-FORMAT'." - (let ((eval-form `(:eval (funcall ,widget)))) - (setq-default mode-line-format - (append mode-line-format - (list mode-line-separator eval-form))))) -#+END_SRC - My implementation of a clock for the modeline. The modeline is refreshed by a timer to ensure that it doesn't go too stale, as it could theoretically go several minutes without being updated. #+BEGIN_SRC elisp -(add-to-mode-line - (lambda () (format-time-string "%Y-%m-%d %H:%M"))) +(defun mode-line-time () + (format-time-string "%Y-%m-%d %H:%M")) (let ((timer (run-at-time t 60.0 #'ignore))) (timer-set-function timer @@ -737,21 +697,40 @@ My implementation of a battery monitor for the modeline. Because strings in escaped twice -- once for =battery-format=, and once for =mode-line-format=. #+BEGIN_SRC elisp -(use-package battery - :straight nil - :config - (add-to-mode-line - (lambda () - (let* ((status (battery-linux-sysfs)) - (fullness (string-to-number (cdr (assoc 112 status)))) - (text (battery-format "%L: %t, %p%%%%" status)) - (low-battery-face '(:foreground "white" - :background "red"))) - (when (< fullness 10.0) - (put-text-property 0 (length text) - 'font-lock-face low-battery-face - text)) - text)))) +(defun mode-line-battery-status () + (let* ((status (battery-linux-sysfs)) + (fullness (string-to-number (cdr (assoc 112 status)))) + (text (battery-format "%L: %t, %p%%%%" status)) + (low-battery-face '(:foreground "white" + :background "red"))) + (when (< fullness 10.0) + (put-text-property 0 (length text) + 'font-lock-face low-battery-face + text)) + text)) +#+END_SRC + +Use an =:eval= form to align modeline contents. + +#+BEGIN_SRC elisp +(defun mode-line-render (left right) + "Return a string of `window-width' length containing LEFT, and +RIGHT aligned respectively." + (let* ((available-width (- (window-width) (length left) (length right) 1)) + (spacing (make-string available-width ?\ ))) + (concat " " left spacing right))) + +(setq-default mode-line-format + '((:eval + (mode-line-render + (concat (format-mode-line "%b [%m] [%*] Ln: %l/") + (number-to-string (if (= (buffer-size) 0) + 1 + (count-lines 1 (- (buffer-size) 1)))) + (format-mode-line " Col: %c")) + (format "%s | %s" + (mode-line-time) + (mode-line-battery-status)))))) #+END_SRC ** Pretty Symbols -- cgit v1.3