summaryrefslogtreecommitdiff
path: root/lisp/appearance/modeline.el
diff options
context:
space:
mode:
authorJakob L. Kreuze2020-06-01 16:53:50 -0400
committerJakob L. Kreuze2020-06-01 16:53:50 -0400
commit6c7f0b8ae2de30a808901b16b0d462fe833ea1b4 (patch)
tree2cc0a3e9022fc43c39e5b91f4e7832b5387636e9 /lisp/appearance/modeline.el
parent4d4f759881e310818055c31f286158da9db60d1c (diff)
Break config into files.
Diffstat (limited to 'lisp/appearance/modeline.el')
-rw-r--r--lisp/appearance/modeline.el87
1 files changed, 87 insertions, 0 deletions
diff --git a/lisp/appearance/modeline.el b/lisp/appearance/modeline.el
new file mode 100644
index 0000000..296a4f5
--- /dev/null
+++ b/lisp/appearance/modeline.el
@@ -0,0 +1,87 @@
+;;; modeline.el -*- lexical-binding: t; -*-
+;;;
+;;; Author: Jakob L. Kreuze <zerodaysfordays@sdf.org>
+;;; URL: https://git.sr.ht/~jakob/.emacs.d/
+;;;
+;;; My custom modeline for Emacs, which seeks to fit the dual role of a
+;;; functional status indicator for Emacs, as well as a "bar" in the sense of a
+;;; traditional tiling window manager.
+;;;
+
+;; 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.
+
+(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
+ (lambda ()
+ (force-mode-line-update))))
+
+;; For some time, I was using my own implementation of a modeline battery
+;; monitor. However, the constant calls to `battery-linux-sysfs' were
+;; interfering with the way that EXWM handles input focus, and I've since moved
+;; towards reusing `display-battery-mode'. Because strings in `mode-line-format'
+;; can contain control sequences, the percent symbol has to be escaped twice --
+;; once for `battery-format', and once for `mode-line-format'.
+;;
+;; Obviously, my tower lacks a battery, so I only enable this on my laptop.
+
+(when (string= (system-name) "Upsilon")
+ (require 'battery)
+ (display-battery-mode +1)
+ (setq battery-mode-line-format "%L: %t, %p%%%%"))
+
+(defun maybe-elide (s &optional max-length)
+ "Truncate `S' to `MAX-LENGTH'.
+Return a string derived from `S', possible elided, that is at
+most 1MAX-LENGTH characters long."
+ (let ((max-length (or max-length 30)))
+ (if (> (length s) max-length)
+ (concat (substring s 0 (- max-length 4)) " ...")
+ s)))
+
+(defun escape-mode-line-construct (s)
+ "Sanitize `S' for use in `mode-line-format'.
+Ensure that no part of S will be interpreted as a modeline format
+specifier."
+ (replace-regexp-in-string (regexp-quote "%") "%%" s t t))
+
+(defun mode-line-render (left right)
+ "Return a string of `window-width' length containing LEFT, and
+RIGHT aligned respectively."
+ (let* ((right (format-mode-line right))
+ (left (maybe-elide (format-mode-line left)
+ (- (window-width) (length right) 1)))
+ (padding (- (window-width) (length left) (length right) 1))
+ (padding (if (wholenump padding) (make-string padding ?\ ))))
+ (if padding
+ (escape-mode-line-construct (concat " " left padding right))
+ (concat " " left right))))
+
+(defun remove-unicode (s)
+ "Remove non-ASCII or non-printable ASCII characters from `S'."
+ (apply #'string (seq-filter #'(lambda (c) (and (>= c ? ) (<= c ?~))) s)))
+
+(setq-default mode-line-format
+ '((:eval
+ (mode-line-render
+ (cond ((eq major-mode 'exwm-mode)
+ (concat "%b: " (remove-unicode exwm-title)))
+ ((eq major-mode 'pdf-view-mode)
+ (concat "%b ["
+ (number-to-string (pdf-view-current-page))
+ "/"
+ (number-to-string (pdf-cache-number-of-pages))
+ "]"))
+ (t
+ (concat "%b [%m] [%*] Ln: %l/"
+ (number-to-string (1+ (count-lines 1 (point-max))))
+ " Col: %c")))
+ (cond ((string= (system-name) "Upsilon")
+ (format "%s | %s"
+ (mode-line-time)
+ battery-mode-line-string))
+ (t (mode-line-time)))))))

© 2015 - 2026 Jakob L. Kreuze