| 1 | ;;; emms-mode-line.el --- Mode-Line and titlebar infos for emms -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Mario Domgörgen <kanaldrache@gmx.de> |
| 6 | ;; Keywords: multimedia |
| 7 | |
| 8 | ;; This file is part of EMMS. |
| 9 | |
| 10 | ;; EMMS is free software; you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation; either version 3, or (at your option) |
| 13 | ;; any later version. |
| 14 | |
| 15 | ;; EMMS is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with EMMS; see the file COPYING. If not, write to the |
| 22 | ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | ;; Boston, MA 02110-1301, USA. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | ;; |
| 27 | ;; To activate put simply the following line in your Emacs: |
| 28 | ;; |
| 29 | ;; (require 'emms-mode-line) |
| 30 | ;; (emms-mode-line 1) |
| 31 | |
| 32 | ;;; Code: |
| 33 | |
| 34 | (require 'emms) |
| 35 | |
| 36 | (defgroup emms-mode-line nil |
| 37 | "Showing information on mode-line and titlebar" |
| 38 | :prefix "emms-mode-line-" |
| 39 | :group 'emms) |
| 40 | |
| 41 | (defcustom emms-mode-line-mode-line-function #'emms-mode-line-playlist-current |
| 42 | "Function for showing infos in mode-line or nil if don't want to." |
| 43 | :type '(choice (const :tag "Don't show info on mode-line" nil) function)) |
| 44 | |
| 45 | (defcustom emms-mode-line-titlebar-function nil |
| 46 | "Function for showing infos in titlebar or nil if you don't want to." |
| 47 | :type '(choice (const :tag "Don't show info on titlebar" nil) function)) |
| 48 | |
| 49 | (defcustom emms-mode-line-format " [ %s ] " |
| 50 | "String used for displaying the current track in mode-line and titlebar." |
| 51 | :type 'string |
| 52 | :group 'emms) |
| 53 | |
| 54 | (defcustom emms-mode-line-length-limit 70 |
| 55 | "Maximum length before track information is truncated." |
| 56 | :type 'natnum |
| 57 | :group 'emms) |
| 58 | |
| 59 | |
| 60 | (defun emms-mode-line-playlist-current () |
| 61 | "Format the currently playing song." |
| 62 | (let ((track-desc (emms-track-description |
| 63 | (emms-playlist-current-selected-track)))) |
| 64 | (format emms-mode-line-format |
| 65 | (if (< (string-width track-desc) emms-mode-line-length-limit) |
| 66 | track-desc |
| 67 | (truncate-string-to-width track-desc emms-mode-line-length-limit 0 nil t))))) |
| 68 | |
| 69 | |
| 70 | (define-obsolete-variable-alias 'emms-mode-line-active-p |
| 71 | 'emms-mode-line-mode "Apr 2021") |
| 72 | (defvar emms-mode-line-string "") |
| 73 | |
| 74 | (defvar emms-mode-line-initial-titlebar frame-title-format) |
| 75 | |
| 76 | (defun emms-mode-line (arg) |
| 77 | (declare (obsolete emms-mode-line-mode "Apr 2021")) |
| 78 | (emms-mode-line-mode (if (and arg (> arg 0)) 1 -1))) |
| 79 | |
| 80 | ;;;###autoload |
| 81 | (define-minor-mode emms-mode-line-mode |
| 82 | "Turn on `emms-mode-line' if ARG is positive, off otherwise." |
| 83 | :global t |
| 84 | (or global-mode-string (setq global-mode-string '(""))) |
| 85 | (if emms-mode-line-mode |
| 86 | (progn |
| 87 | (add-hook 'emms-track-updated-functions #'emms-mode-line-alter) |
| 88 | (add-hook 'emms-player-finished-hook #'emms-mode-line-blank) |
| 89 | (add-hook 'emms-player-stopped-hook #'emms-mode-line-blank) |
| 90 | (add-hook 'emms-player-started-hook #'emms-mode-line-alter) |
| 91 | (when (and emms-mode-line-mode-line-function |
| 92 | (not (member 'emms-mode-line-string global-mode-string))) |
| 93 | (setq global-mode-string |
| 94 | (append global-mode-string |
| 95 | '(emms-mode-line-string)))) |
| 96 | (when emms-player-playing-p (emms-mode-line-alter))) |
| 97 | (remove-hook 'emms-track-updated-functions #'emms-mode-line-alter) |
| 98 | (remove-hook 'emms-player-finished-hook #'emms-mode-line-blank) |
| 99 | (remove-hook 'emms-player-stopped-hook #'emms-mode-line-blank) |
| 100 | (remove-hook 'emms-player-started-hook #'emms-mode-line-alter) |
| 101 | (emms-mode-line-restore-titlebar) |
| 102 | (emms-mode-line-restore-mode-line))) |
| 103 | |
| 104 | ;;;###autoload |
| 105 | (defun emms-mode-line-enable () |
| 106 | "Turn on `emms-mode-line'." |
| 107 | (declare (obsolete emms-mode-line-mode "Apr 2021")) |
| 108 | (interactive) |
| 109 | (emms-mode-line-mode 1)) |
| 110 | |
| 111 | ;;;###autoload |
| 112 | (defun emms-mode-line-disable () |
| 113 | "Turn off `emms-mode-line'." |
| 114 | (interactive) |
| 115 | (emms-mode-line-mode -1)) |
| 116 | |
| 117 | ;;;###autoload |
| 118 | (defun emms-mode-line-toggle () |
| 119 | "Toggle `emms-mode-line'." |
| 120 | (declare (obsolete emms-mode-line-mode "Apr 2021")) |
| 121 | (interactive) |
| 122 | (emms-mode-line-mode 'toggle)) |
| 123 | |
| 124 | (defun emms-mode-line-alter (&optional track) |
| 125 | "Alter mode-line/titlebar. |
| 126 | |
| 127 | Optional TRACK is used to be compatible with |
| 128 | `emms-track-updated-functions'. It's simply ignored currently." |
| 129 | (ignore track) |
| 130 | (emms-mode-line-alter-mode-line) |
| 131 | (emms-mode-line-alter-titlebar)) |
| 132 | |
| 133 | (defun emms-mode-line-alter-mode-line () |
| 134 | "Update the mode-line with song info." |
| 135 | (when (and emms-mode-line-mode-line-function |
| 136 | emms-player-playing-p) |
| 137 | (setq emms-mode-line-string |
| 138 | (funcall emms-mode-line-mode-line-function)) |
| 139 | (force-mode-line-update))) |
| 140 | |
| 141 | (defun emms-mode-line-alter-titlebar () |
| 142 | "Update the titlebar with song info." |
| 143 | (when emms-mode-line-titlebar-function |
| 144 | (setq frame-title-format |
| 145 | (list "" emms-mode-line-initial-titlebar (funcall emms-mode-line-titlebar-function))))) |
| 146 | |
| 147 | |
| 148 | (defun emms-mode-line-blank () |
| 149 | "Blank mode-line and titlebar but not quit `emms-mode-line'." |
| 150 | (setq emms-mode-line-string nil) |
| 151 | (force-mode-line-update) |
| 152 | (emms-mode-line-restore-titlebar)) |
| 153 | |
| 154 | (defun emms-mode-line-restore-mode-line () |
| 155 | "Restore the mode-line." |
| 156 | (when emms-mode-line-mode-line-function |
| 157 | (setq global-mode-string |
| 158 | (remove 'emms-mode-line-string global-mode-string)) |
| 159 | (force-mode-line-update))) |
| 160 | |
| 161 | (defun emms-mode-line-restore-titlebar () |
| 162 | "Restore the mode-line." |
| 163 | (when emms-mode-line-titlebar-function |
| 164 | (setq frame-title-format |
| 165 | (list emms-mode-line-initial-titlebar)))) |
| 166 | |
| 167 | (provide 'emms-mode-line) |
| 168 | ;;; emms-mode-line.el ends here |