-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttene.el
98 lines (79 loc) · 3.45 KB
/
ttene.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;;; ttene.el --- さなちゃんttene.nvim移植するところ見てて…
;; Copyright (C) 2018 shibafu528
;; Author: shibafu <[email protected]>
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Inspired by https://github.com/himanoa/ttene.nvim
;;; Code:
(require 'magicalstick)
(defvar ttene:voice-dir
(expand-file-name "ttene-voices" user-emacs-directory)
"音声ファイルの格納ディレクトリ")
(defvar ttene:play-command
(cond ((eq system-type 'windows-nt) "mpg123")
(t "afplay"))
"音声ファイルの再生コマンド")
(defun ttene-install-voices ()
"音声ファイルをダウンロード"
(interactive)
(unless (file-directory-p ttene:voice-dir)
(make-directory ttene:voice-dir))
(dolist (voice (magicalstick))
(when (string-match "てねっ[0-9]" voice)
(with-current-buffer (url-retrieve-synchronously voice)
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(forward-char)
(delete-region (point-min) (point))
(set-buffer-file-coding-system 'binary)
(write-file (format "%s/%s_%s"
ttene:voice-dir
(file-name-nondirectory (directory-file-name (file-name-directory voice)))
(file-name-nondirectory voice)))
(kill-buffer)))))
(defun ttene-pick-voice ()
"音声ファイルをランダムにひとつ取得"
(cond ((file-directory-p ttene:voice-dir)
(let ((voices (directory-files ttene:voice-dir)))
(if (not voices)
nil
(concat (file-name-as-directory ttene:voice-dir) (nth (random (length voices)) voices)))))
(t nil)))
(defun ttene-play-command-line (voice)
"再生コマンドラインの生成"
(concat ttene:play-command
" "
(shell-quote-argument (if (eq system-type 'windows-nt)
;; NTEmacsだとエンコードしないと不幸になりそう?
(encode-coding-string voice 'sjis)
voice))))
(defun ttene-play-voice ()
"音声ファイルを再生"
(interactive)
(let ((voice (ttene-pick-voice)))
(unless voice
(error ("no voices installed. try M-x ttene-install-voices")))
(start-process-shell-command "ttene" "*async-process-ttene*" (ttene-play-command-line voice))
(newline)))
(defun ttene-mode-global-turn-on ()
(when (not (minibufferp))
(ttene-mode t)))
(easy-mmode-define-minor-mode ttene-mode
"ってね"
nil
" ttene"
'(("\C-m" . ttene-play-voice)))
(easy-mmode-define-global-mode global-ttene-mode
ttene-mode
ttene-mode-global-turn-on)
(provide 'ttene)
;;; ttene.el ends here