From 34c09ddc9883d6e9116001b068f5235381d420ce Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 19 Dec 2021 13:54:04 +0800 Subject: modeline: clean format and use truncate-string-to-width * modeline.el (modeline-format-main): Using string-width to calculate the widths of strings is more accurate than my previous hand-made function. (modeline-length): This is no longer needed and now marked as obsolete. It is not deleted entirely since per chance some other places are still using it. (modeline-format-minor-modes, modeline-format-buffer-name): Use truncate-string-to-width instead. --- modeline.el | 61 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/modeline.el b/modeline.el index 2d95adf..683bf3d 100644 --- a/modeline.el +++ b/modeline.el @@ -1,12 +1,36 @@ -;;; -*- lexical-binding: t; -*- +;;; modeline.el --- Configurations for the mode line. -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 李俊緯 + +;; Author: 李俊緯 +;; Keywords: emulations, tools + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; This configures my mode line. + +;;; Code: ;;;###autoload (defun modeline-format-main () "The main mode line format." (let* ((left (modeline-format-left)) (right (modeline-format-right)) - (left-len (modeline-length left)) - (right-len (modeline-length right)) + (left-len (string-width left)) + (right-len (string-width right)) (middle (propertize " " 'display (make-string (max (- (window-total-width) @@ -84,7 +108,8 @@ "Return the length of STR. Characters that take up more than one column will be counted with 1.7 columns." - (declare (side-effect-free t) (pure t)) + (declare (side-effect-free t) (pure t) + (obsolete string-width "2021-12-19 13:51:30.332234")) (let ((len 0)) (mapc (lambda (char) (let ((name (get-char-code-property char 'name)) @@ -322,18 +347,18 @@ MAP is the local keymap of the text." (cond ((modeline-active-window-p) 'mode-line) ('mode-line-inactive)))) + (ellipsis + (format-mode-line + "..." + (cond ((modeline-active-window-p) 'mode-line) + ('mode-line-inactive)))) (orig (cond ((and (not (string= raw "")) (= (aref raw 0) 32)) (substring raw 1)) (raw)))) - (cond - ((<= (length orig) modeline-minor-modes-name-len-max) - orig) - ((concat - (substring orig 0 (- modeline-minor-modes-name-len-max - 3)) - "...")))) + (truncate-string-to-width + orig modeline-minor-modes-name-len-max 0 nil ellipsis)) nil "Minor mode mouse-1: Display minor mode menu @@ -392,15 +417,15 @@ This will be displayed in the mode line." (t 'mode-line-inactive))) (name-max (min modeline-buffer-name-len-max (floor (window-width) 2))) + (ellipsis + (format-mode-line + "..." + (cond ((modeline-active-window-p) 'mode-line) + ('mode-line-inactive)))) (orig (format-mode-line "%b" face))) (concat (format-mode-line "%[" face) - (cond - ((> (length orig) name-max) - (concat - (substring orig 0 (max (- name-max 3) 1)) - "...")) - (t orig)) + (truncate-string-to-width orig name-max 0 nil ellipsis) (format-mode-line "%]" face))) nil (concat (buffer-file-name) @@ -520,3 +545,5 @@ This will be displayed in the mode line." ) (t ""))) +(provide 'modeline) +;;; modeline.el ends here -- cgit v1.2.3-18-g5258