eh-org.el: org-super-agenda add :auto-eh-parent.
[emacs-helper.git] / eh-emms.el
blobd6d9d240e329292e251acc53af128cc3993011b4
1 ;;; eh-emms.el --- Tumashu's emacs configuation -*- lexical-binding: t; -*-
3 ;; * Header
4 ;; Copyright (c) 2011-2019, Feng Shu
6 ;; Author: Feng Shu <tumashu@163.com>
7 ;; URL: https://github.com/tumashu/emacs-helper
8 ;; Version: 0.0.1
10 ;; This file is not part of GNU Emacs.
12 ;;; License:
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License
16 ;; as published by the Free Software Foundation; either version 3
17 ;; of the License, or (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; * 简介 :README:
32 ;; 这个文件是tumashu个人专用的emacs配置文件,emacs中文用户可以参考。
34 ;;; Code:
36 ;; * 代码 :code:
37 (require 'emms)
38 (require 'emms-setup)
39 (require 'emms-info-exiftool)
40 (require 'emms-info-native)
41 (require 'emms-browser)
43 (emms-all)
44 (emms-default-players)
45 (emms-cache 1)
47 (global-set-key (kbd "<f12>") 'emms-smart-browse)
48 (global-set-key (kbd "<f11>") 'emms-next)
49 (global-set-key (kbd "<f10>") 'emms-pause)
51 ;; "+" 按起来麻烦,用 = 省事。
52 (define-key emms-playlist-mode-map (kbd "=") 'emms-volume-raise)
53 (define-key emms-browser-mode-map (kbd "=") 'emms-volume-raise)
55 (setq emms-directory (concat user-emacs-directory "emms"))
57 (setq emms-source-file-default-directory
58 (or emms-source-file-default-directory
59 (concat (file-name-as-directory emms-directory) "music")))
61 (when (file-writable-p "~")
62 (unless (file-directory-p emms-directory)
63 (make-directory
64 (file-name-as-directory emms-directory) t))
66 (unless (file-directory-p emms-source-file-default-directory)
67 (make-directory
68 (file-name-as-directory
69 emms-source-file-default-directory)
70 t)))
72 ;; 设定EMMS主模式为 Playlist 模式
73 (setq emms-playlist-default-major-mode 'emms-playlist-mode)
75 ;; 修复播放完后的 BUG
76 (setq emms-player-next-function 'emms-next-noerror)
78 ;; 设定音轨初始化信息
79 (add-to-list 'emms-track-initialize-functions
80 'emms-info-initialize-track)
82 ;; 关闭 EMMS 信息异步模式
83 (setq emms-info-asynchronously nil)
85 ;; 设定 EMMS 启动列表循环播放
86 (setq emms-repeat-playlist t)
88 ;; 排序方法: 艺术家 -> 专辑 -> 序号
89 (setq emms-playlist-sort-function
90 'emms-playlist-sort-by-natural-order)
92 ;; 在 minibuffer 中显示播放信息 (emms-show)
93 (setq emms-show-format "正在播放:%s")
94 (add-hook 'emms-player-started-hook #'emms-show)
96 ;; 不在 mode-line 中显示播放信息
97 (require 'emms-mode-line)
98 (emms-mode-line-mode -1)
100 ;; 如何显示 track
101 (setq emms-track-description-function
102 #'eh-emms-track-simple-description)
104 (defun eh-emms-track-simple-description (track)
105 (let* ((type (emms-track-type track))
106 (string (cond ((eq 'file type)
107 (emms-track-name track))
108 ((eq 'url type)
109 (emms-format-url-track-name (emms-track-name track)))
110 (t (concat (symbol-name type)
111 ": " (emms-track-name track))))))
112 (with-temp-buffer
113 (insert (concat " ♪ " string))
114 (eh-emms-wash-buffer)
115 (buffer-string))))
117 ;; Playlist 清洗功能
118 (defvar eh-emms-wash-config nil)
120 (defun eh-emms-wash-buffer ()
121 (dolist (x eh-emms-wash-config)
122 (goto-char (point-min))
123 (while (re-search-forward
124 (if (nth 2 x)
125 (nth 0 x)
126 (regexp-quote (nth 0 x)))
127 nil t)
128 (replace-match (propertize (nth 1 x) 'face (text-properties-at (point))) nil t))))
130 (defun eh-emms-wash-config ()
131 (interactive)
132 (let ((str1 (read-string "请输入需清洗的字符串:"))
133 (str2 (read-string "请输入清洗成的字符串:"))
134 (regexp-p (y-or-n-p "是否将待清洗的字符串作为 regexp 处理? ")))
135 (when (> (length str1) 0)
136 (push (list str1 str2 regexp-p) eh-emms-wash-config))
137 (customize-save-variable
138 'eh-emms-wash-config
139 (delete-dups eh-emms-wash-config))
140 (with-current-emms-playlist
141 (eh-emms-wash-buffer))))
143 ;; 显示歌词
144 (require 'emms-lyrics)
145 (emms-lyrics 1)
147 ;; Track information
148 ;; 1. emms-info-exiftool 使用 perl 程序 exiftool, 速度比较慢但兼容性很好。
149 ;; 2. emms-info-native 是用 elisp 实现的,安装比较省事,速度还可以。
151 ;; (setq emms-info-functions '(emms-info-exiftool))
152 (setq emms-info-functions '(eh-emms-info-native))
154 (defun eh-emms-info-native (track)
155 (let ((file (emms-track-name track)))
156 (condition-case nil
157 (emms-info-native track)
158 (error (message "EMMS: 无法从 %S 中读取音乐信息." file)))))
160 ;; 设置 Playlist 的显示方式
161 (setq emms-last-played-format-alist
162 '(((emms-last-played-seconds-today) . "%H:%M")
163 (604800 . "%H:%M")
164 ((emms-last-played-seconds-month) . "%d")
165 ((emms-last-played-seconds-year) . "%m-%d")
166 (t . "%Y")))
168 ;; 设置 EMMS 浏览器, 默认显示方式为: 显示所有
169 (emms-browser-set-filter (assoc "EVERYTHING" emms-browser-filters))
171 ;; filter: 显示所有
172 (emms-browser-make-filter "EVERYTHING" 'ignore)
174 ;; filter: 只显示文件
175 (emms-browser-make-filter
176 "ALL-FILES"
177 (emms-browser-filter-only-type 'file))
179 ;; filter: 最近一个星期播放的
180 (emms-browser-make-filter
181 "LAST-WEEK"
182 (emms-browser-filter-only-recent 7))
184 ;; filter: 最近一个月都没有播放的
185 (emms-browser-make-filter
186 "LAST-MONTH-NOT-PLAYED"
187 (lambda (track)
188 (not (funcall (emms-browser-filter-only-recent 30) track))))
190 ;; 设置 emms browser 和 playlist 中音乐的显示格式
192 (defun eh-emms-browser-format-line (orig_func bdata &optional target)
193 (let* ((type (emms-browser-bdata-type bdata))
194 (level (emms-browser-bdata-level bdata))
195 (indent (emms-browser-make-indent level))
196 (str (funcall orig_func bdata target)))
197 ;; 删除原来函数添加的 " ", 不然 playlist 对齐不好处理。
198 (when (and (eq type 'info-title)
199 (not (string= indent "")))
200 (setq str (string-remove-prefix " " str)))
201 str))
203 (advice-add 'emms-browser-format-line :around #'eh-emms-browser-format-line)
205 (defvar emms-browser-info-year-format "%i+ %n")
206 (defvar emms-browser-info-genre-format "%i+ %n")
207 (defvar emms-browser-info-performer-format "%i+ %n")
208 (defvar emms-browser-info-composer-format "%i+ %n")
209 (defvar emms-browser-info-artist-format "%i* %n")
210 (defvar emms-browser-info-album-format "%i- %n")
211 (defvar emms-browser-info-title-format "%i♪ %n")
212 (defvar emms-browser-playlist-info-year-format "%i%n")
213 (defvar emms-browser-playlist-info-genre-format "%i%n")
214 (defvar emms-browser-playlist-info-performer-format "%i%n")
215 (defvar emms-browser-playlist-info-composer-format "%i%n")
216 (defvar emms-browser-playlist-info-artist-format "%i%n")
217 (defvar emms-browser-playlist-info-album-format "%i%n")
218 (defvar emms-browser-playlist-info-title-format " ♪ %n")
220 (defun eh-emms-browser-wash-playlist (&optional _)
221 "简化 playlist, emms-browser 默认生成的 playlist 有缩进,看起来太花。"
222 (with-current-emms-playlist
223 (eh-emms-wash-buffer)
224 (goto-char (point-max))))
226 (add-hook 'emms-browser-tracks-added-hook #'eh-emms-browser-wash-playlist)
228 ;; 使用类似 org-mode 的快捷键
229 (define-key emms-browser-mode-map (kbd "<tab>") 'emms-browser-toggle-subitems-recursively)
230 (define-key emms-browser-mode-map (kbd "<backtab>") 'emms-browser-toggle-subitems-recursively)
231 (define-key emms-browser-mode-map (kbd "C-c C-c") 'emms-browser-add-tracks-and-play)
233 ;; 加载 playlist 历史
234 (require 'emms-history)
235 (require 'emms-tag-editor)
237 (add-hook 'after-init-hook #'emms-history-load)
239 (setq emms-tag-editor-pipe-config
240 '(("处理MP3中文标签乱码 (mid3iconv -e gbk <file>)"
241 :command "mid3iconv"
242 :arguments ("-e" "gbk" name))
243 ("Test1 (echo -a <name> -c <name>)"
244 :command "echo"
245 :arguments (("-a" name) ("-b" name1) ("-c" name)))
246 ("Test2 (echo <artist>-<title>)"
247 :command "echo"
248 :arguments
249 (lambda (track)
250 (list (format "%s-%s"
251 (emms-track-get track 'info-artist)
252 (emms-track-get track 'info-title)))))))
254 ;; * Footer
255 (provide 'eh-emms)
257 ;; Local Variables:
258 ;; coding: utf-8-unix
259 ;; End:
261 ;;; eh-emms.el ends here