-
Notifications
You must be signed in to change notification settings - Fork 12
/
netease-cloud-music-ui.el
677 lines (617 loc) · 29.3 KB
/
netease-cloud-music-ui.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
;;; netease-cloud-music-ui.el --- Netease Cloud Music client for Emacs -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; Version: 2.1
;; This file is not part of GNU Emacs
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Netease Cloud Music Client for Emacs.
;;; Code:
;;; This file is the TUI for client.
(require 'netease-cloud-music)
(defcustom netease-cloud-music-jump-function
'netease-cloud-music-jump-read-line
"FUnction for jumpping."
:type 'symbol
:group 'netease-cloud-music)
(defcustom netease-cloud-music-line-number-relative nil
"If the line number is relative."
:type 'boolean
:group 'netease-cloud-music)
(defcustom netease-cloud-music-fold-tips t
"If the tip for song's fold should be given."
:type 'boolean
:group 'netease-cloud-music)
(defface netease-cloud-music-head-title-face
'((t :height 1.1 :foreground "Red3"))
"The head title face."
:group 'netease-cloud-music)
(defface netease-cloud-music-user-name-face
'((t :inherit font-lock-constant-face :weight bold))
"The username face."
:group 'netease-cloud-music)
(defface netease-cloud-music-repeat-face
'((t :foreground "DeepSkyBlue"))
"The repeat title face."
:group 'netease-cloud-music)
(defface netease-cloud-music-repeat-mode-face
'((t :foreground "LightPink" :weight bold))
"The repeat mode face."
:group 'netease-cloud-music)
(defface netease-cloud-music-current-song-title-face
'((t :height 0.9 :weight bold))
"The current song title face."
:group 'netease-cloud-music)
(defface netease-cloud-music-playing-song-face
'((t :height 1.0 :foreground "MediumSpringGreen"))
"The playing song face."
:group 'netease-cloud-music)
(defface netease-cloud-music-play-status-face
'((t :foreground "OrangeRed"))
"The play status face."
:group 'netease-cloud-music)
(defface netease-cloud-music-playlists-face
'((t :height 1.05 :foreground "gold2"))
"The playlists face."
:group 'netease-cloud-music)
(defface netease-cloud-music-playlist-face
'((t :inherit font-lock-warning-face :weight bold))
"The playlist face."
:group 'netease-cloud-music)
(defvar netease-cloud-music-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "g" 'netease-cloud-music-goto)
(define-key map "Q" 'netease-cloud-music-close)
(define-key map "q" 'netease-cloud-music-back)
(define-key map (kbd "SPC") 'netease-cloud-music-pause-or-continue)
(define-key map (kbd "RET") 'netease-cloud-music-play-song-at-point)
(define-key map (kbd "TAB") 'netease-cloud-music-toggle-playlist-songs)
(define-key map "c" 'netease-cloud-music-change-lyric-type)
(define-key map "M" 'netease-cloud-music-delete-playlist)
(define-key map "m" 'netease-cloud-music-change-playlist-name)
(define-key map "C" 'netease-cloud-music-create-playlist)
(define-key map "s" 'netease-cloud-music-save-playlist)
(define-key map "u" 'netease-cloud-music-get-playlist-by-uid)
(define-key map "f" 'netease-cloud-music-search-song)
(define-key map "F" 'netease-cloud-music-search-playlist)
(define-key map "d" 'netease-cloud-music-delete-song-from-playlist)
(define-key map "D" 'netease-cloud-music-delete-playing-song)
(define-key map "P" 'netease-cloud-music-playlist-play)
(define-key map "p" 'netease-cloud-music-play-previous-song)
(define-key map "n" 'netease-cloud-music-play-next-song)
(define-key map "N" 'netease-cloud-music-random-play)
(define-key map "x" 'netease-cloud-music-kill-current-song)
(define-key map ">" 'netease-cloud-music-seek-forward)
(define-key map "<" 'netease-cloud-music-seek-backward)
(define-key map "r" 'netease-cloud-music-change-repeat-mode)
(define-key map "k" 'netease-cloud-music-clear-playlist)
(define-key map "R" 'netease-cloud-music-change-order)
(define-key map "w" 'netease-cloud-music-write-mode)
(define-key map "l" 'netease-cloud-music-login)
(define-key map "e" 'netease-cloud-music-get-recommend-songs)
(define-key map "E" 'netease-cloud-music-get-recommend-playlists)
(define-key map "j" 'netease-cloud-music-storage-song)
(define-key map "J" 'netease-cloud-music-add-storage-to-current-playlist)
(define-key map "o" 'netease-cloud-music-show-storage)
(define-key map "K" 'netease-cloud-music-clear-storage)
(define-key map "/" 'netease-cloud-music-ask-play)
(define-key map (kbd "<down>") 'netease-cloud-music-move-down)
(define-key map (kbd "<up>") 'netease-cloud-music-move-up)
(define-key map "?" 'describe-mode)
(define-key map "h" 'describe-mode)
map)
"Netease Music mode map.")
(defcustom netease-cloud-music-switch-song-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "q" 'netease-cloud-music-switch-close)
(define-key map "n" 'next-line)
(define-key map "p" 'netease-cloud-music-switch-previous)
(define-key map "f" 'netease-cloud-music-switch-next-page)
(define-key map "b" 'netease-cloud-music-switch-prev-page)
(define-key map (kbd "RET") 'netease-cloud-music-switch-enter)
(define-key map "P" 'netease-cloud-music-switch-play-page)
(define-key map "a" 'netease-cloud-music-switch-add-to-playlist)
(define-key map "A" 'netease-cloud-music-switch-add-page)
(define-key map "d" 'netease-cloud-music-delete-song-from-storage)
map)
"The Netease Cloud Music Songs Switch mode map."
:type 'keymap
:group 'netease-cloud-music)
(defcustom netease-cloud-music-switch-playlist-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "q" 'netease-cloud-music-switch-close)
(define-key map "n" 'next-line)
(define-key map "p" 'previous-line)
(define-key map "a" 'netease-cloud-music-playlist-add-all)
(define-key map "f" 'netease-cloud-music-switch-next-page)
(define-key map "b" 'netease-cloud-music-switch-prev-page)
(define-key map (kbd "RET") 'netease-cloud-music-playlist-enter)
(define-key map (kbd "TAB") 'netease-cloud-music-playlist-tab)
map)
"The Playlist switch mode map."
:type 'keymap
:group 'netease-cloud-music)
(defun netease-cloud-music-switch-previous ()
"Previous line or play all the songs current page."
(interactive)
(if (= 1 (line-number-at-pos))
(netease-cloud-music-switch-play-all)
(forward-line -1)))
;;;###autoload
(define-derived-mode netease-cloud-music-mode nil "Netease-Cloud-Music"
"The mode of Netease Music mode."
:group 'netease-cloud-music
:abbrev-table nil
:syntax-table nil
(if (fboundp 'linum-mode) (linum-mode -1))
(setq buffer-read-only t
truncate-lines t))
;;;###autoload
(define-derived-mode netease-cloud-music-switch-mode netease-cloud-music-mode "Netease-Cloud-Music:Switch"
"The child mode for `netease-cloud-music-mode' to do switch action."
:group 'netease-cloud-music
:abbrev-table nil
:syntax-table nil)
(defun netease-cloud-music-open-switch (buffer-name)
"Open the switch buffer by its BUFFER-NAME."
(split-window nil nil 'above)
(switch-to-buffer (format "*Netease-Cloud-Music:Switch->%s*"
buffer-name))
(netease-cloud-music-switch-mode))
(defun netease-cloud-music-switch-close ()
"Close the switch buffer."
(interactive)
(kill-buffer-and-window)) ; NOTE: Maybe will add new features.
(defun netease-cloud-music-switch-play-all ()
"Play all the songs in the search list."
(interactive)
(when netease-cloud-music-search-alist
(setq netease-cloud-music-playlist (cdr netease-cloud-music-search-alist))
(cond ((/= (line-number-at-pos) 1)
(setq netease-cloud-music-playlist-song-index (1- (line-number-at-pos))))
(t
(setq netease-cloud-music-playlist-song-index 0)))
(netease-cloud-music-playlist-play)
(netease-cloud-music-switch-close)))
(defun netease-cloud-music-switch-play-page (page)
"Play songs by PAGE."
(interactive (list (read-string "Enter the page[n-n]: " "1-")))
(when (yes-or-no-p "If you really want to do this? It'll replace all your songs!")
(if netease-cloud-music-use-local-playlist
(setq netease-cloud-music-playlist (netease-cloud-music--songs-by-page page))
(let (ids)
(dolist (song (netease-cloud-music--songs-by-page page))
(setq ids (append ids (list (car song)))))
(netease-cloud-music-update-songs-order-in-playlist
netease-cloud-music-playlist-id ids)))
(unless (= 0 netease-cloud-music-playlist-song-index)
(setq netease-cloud-music-playlist-song-index 0))
(netease-cloud-music-playlist-play)
(netease-cloud-music-save-playlist)
(netease-cloud-music-switch-close)))
(defun netease-cloud-music-play-song-at-point ()
"Play the song at point."
(interactive)
(when
(catch 'stop ;When the user only changed the playing mode, don't play any song.
(netease-cloud-music-change-playlist-mode)
(let ((song (nth (netease-cloud-music--current-song)
(if netease-cloud-music-use-local-playlist
netease-cloud-music-playlist
netease-cloud-music-playlists-songs))))
(when song
(when (string= netease-cloud-music-repeat-mode "")
(setq netease-cloud-music-repeat-mode "song"))
(netease-cloud-music-play
(car song)
(nth 1 song)
(nth 3 song)))))
(netease-cloud-music-interface-init)))
;;;###autoload
(defun netease-cloud-music ()
"Initialize the Netease Music buffer in `netease-cloud-music-mode'."
(interactive)
(setq netease-cloud-music-last-buffer (current-buffer)) ;Record the buffer which called this function
(if (get-buffer netease-cloud-music-buffer-name)
(switch-to-buffer netease-cloud-music-buffer-name)
(unless (buffer-live-p (get-buffer netease-cloud-music-buffer-name))
(switch-to-buffer netease-cloud-music-buffer-name))
(netease-cloud-music-mode)
(netease-cloud-music-get-playlist)
(netease-cloud-music-interface-init)
(unless (file-exists-p netease-cloud-music-cache-directory)
(make-directory netease-cloud-music-cache-directory))
(when (and (or (not (netease-cloud-music--api-need-downloaded))
(netease-cloud-music--api-downloaded))
(not (netease-cloud-music-api-process-live-p)))
(netease-cloud-music-start-api) ;Start third-party API
(when (file-exists-p netease-cloud-music-user-loginfo-file)
(let ((loginfo (netease-cloud-music-get-loginfo)))
(when loginfo
(setq netease-cloud-music-phone (car loginfo)
netease-cloud-music-user-password (cdr loginfo)
netease-cloud-music-login-timer
(run-with-timer
1 2
(lambda ()
(if (and netease-cloud-music-user-id
netease-cloud-music-username)
(progn
(cancel-timer netease-cloud-music-login-timer)
(setq netease-cloud-music-login-timer nil))
(netease-cloud-music--get-user-info)
(ignore-errors
(setq netease-cloud-music-playlists
(netease-cloud-music-get-user-playlist
netease-cloud-music-user-id)))
(netease-cloud-music-interface-init)))))))))))
(defun netease-cloud-music-close ()
"Close Netease Music and kill the process."
(interactive)
(netease-cloud-music-quit)
(kill-buffer netease-cloud-music-buffer-name))
(defun netease-cloud-music-search-song--open-switch (songs-info)
"Enter the `netease-cloud-music-switch-mode' to switch song from searched.
SONGS-INFO is the infos of the songs want to show."
(unless (eq major-mode 'netease-cloud-music-switch-song-mode)
(netease-cloud-music-open-switch "Songs"))
(with-current-buffer "*Netease-Cloud-Music:Switch->Songs*"
(use-local-map netease-cloud-music-switch-song-mode-map)
(setq-local major-mode 'netease-cloud-music-switch-song-mode)
(setq-local buffer-read-only nil)
(erase-buffer)
(let ((prefix (propertize "<<" 'face 'font-lock-comment-face))
(end (propertize ">>" 'face 'font-lock-comment-face)))
(mapc (lambda (s)
(insert prefix
(propertize (nth 1 s) 'face 'netease-cloud-music-song-face)
end " - "
(propertize (nth 3 s) 'face 'netease-cloud-music-artist-face)
"\n"))
(if (listp (car-safe songs-info))
songs-info
(list songs-info))))
(setq-local buffer-read-only t)
(goto-char (point-min))))
(defun netease-cloud-music-playlist--open-switch (playlists)
"Open switch buffer for PLAYLISTS."
(unless (eq major-mode 'netease-cloud-music-switch-playlist-mode)
(netease-cloud-music-open-switch "Playlist"))
(with-current-buffer "*Netease-Cloud-Music:Switch->Playlist*"
(use-local-map netease-cloud-music-switch-playlist-mode-map)
(setq-local major-mode 'netease-cloud-music-switch-playlist-mode)
(setq buffer-read-only nil)
(erase-buffer)
(dolist (playlist playlists)
(insert (propertize (car playlist)
'face 'netease-cloud-music-playlist-face)
"\n"))
(setq buffer-read-only t)
(goto-char (point-min))))
(defun netease-cloud-music-interface-init ()
"Initialize the Netease Music buffer interface."
(interactive)
(with-current-buffer netease-cloud-music-buffer-name
(unless (eq major-mode 'netease-cloud-music-write-mode)
(setq buffer-read-only nil)
(erase-buffer)
(insert (propertize
"Netease Cloud Music - 网易云音乐\n"
'face 'netease-cloud-music-head-title-face))
(when netease-cloud-music-username ;Show user name
(insert "你好,"
(propertize netease-cloud-music-username
'face 'netease-cloud-music-user-name-face) "\n"))
;; Show the repeat mode status
(insert (propertize "Repeat: "
'face 'netease-cloud-music-repeat-face)
(propertize (concat (upcase netease-cloud-music-repeat-mode) "\n")
'face 'netease-cloud-music-repeat-mode-face))
;; When the type is song, insert the current song info.
(when netease-cloud-music-current-song
(insert "\n")
(insert (propertize
"Current song: "
'face 'netease-cloud-music-current-song-title-face)
(propertize (format
"%s - %s"
(car netease-cloud-music-current-song)
(nth 1 netease-cloud-music-current-song))
'face 'netease-cloud-music-playing-song-face)
(if (string= netease-cloud-music-process-status "playing")
(propertize " [Playing]\n"
'face 'netease-cloud-music-play-status-face)
(propertize " [Paused]\n"
'face 'netease-cloud-music-play-status-face))))
;; User's Playlist
(when netease-cloud-music-playlists
(insert (propertize "\nUser's Playlists:\n"
'face 'netease-cloud-music-playlists-face))
(dolist (playlist netease-cloud-music-playlists)
(insert (propertize (car playlist)
'face 'netease-cloud-music-playlist-face)
"\n")
(when (and netease-cloud-music-playlist-id
(= netease-cloud-music-playlist-id (cdr playlist)))
(mapc (lambda (s)
(insert (number-to-string (car s)))
(overlay-put (make-overlay (line-beginning-position) (line-end-position))
'display
(format "%s - %s"
(propertize
(nth 1 s)
'face 'netease-cloud-music-song-face)
(propertize
(if (nth 3 s) (nth 3 s) "nil")
'face 'netease-cloud-music-artist-face)))
(insert "\n"))
(netease-cloud-music--limit-songs netease-cloud-music-playlists-songs)))))
;; Local Playlist
(insert (propertize "\nLocal Playlist:\n"
'face 'netease-cloud-music-playlists-face))
(when netease-cloud-music-playlist
(mapc (lambda (s)
(insert (number-to-string (car s)))
(overlay-put (make-overlay (line-beginning-position) (line-end-position))
'display
(format "%s - %s"
(propertize
(nth 1 s)
'face 'netease-cloud-music-song-face)
(propertize
(nth 3 s)
'face 'netease-cloud-music-artist-face)))
(insert "\n"))
(netease-cloud-music--limit-songs netease-cloud-music-playlist)))
(setq buffer-read-only t)
(goto-char (point-min))
(forward-line 3))))
(defun netease-cloud-music--limit-songs (songs)
"Limit the SONGS if its value is out of 40."
(let ((start (if netease-cloud-music-playlist-song-index
(- netease-cloud-music-playlist-song-index 20)
0))
(end (if netease-cloud-music-playlist-song-index
(+ netease-cloud-music-playlist-song-index 20)
39)))
(if (> (length songs) 40)
(prog1 (netease-cloud-music--slice songs start end)
(when netease-cloud-music-fold-tips
(progn
(message "[Netease-Cloud-Music]: Songs in current playlist is out of 40, so the others are folded.")
(setq netease-cloud-music-fold-tips nil))))
songs)))
(defun netease-cloud-music-toggle-playlist-songs (pid)
"Toggle the songs of playlist under cursor.
PID is the playlist's id."
(interactive (list
(let ((playlist-name (substring-no-properties
(thing-at-point 'line) 0 -1))
playlist)
(if (null
(setq playlist
(alist-get playlist-name netease-cloud-music-playlists
nil nil 'string-equal)))
(netease-cloud-music-error "The playlist is not exists!")
playlist))))
(with-current-buffer netease-cloud-music-buffer-name
(setq-local buffer-read-only nil)
(save-mark-and-excursion
(forward-line) ;To check the next line if it's a null string or songs' info.
(if (not (or (equal (get-text-property (point) 'face)
'netease-cloud-music-playlist-face)
(string-empty-p (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))))
(progn
(while (overlays-at (point))
(delete-region (line-beginning-position) (line-end-position))
(delete-char 1)))
(forward-line -1)
(goto-char (line-end-position))
(insert "\n")
(let ((songs (netease-cloud-music-get-playlist-songs pid)))
(if (null songs)
(message "[Netease-Cloud-Music]: The playlist is empty.")
(dolist (song songs)
(insert (number-to-string (car song)))
(overlay-put (make-overlay (line-beginning-position) (line-end-position))
'display
(format "%s - %s"
(propertize
(nth 1 song)
'face 'netease-cloud-music-song-face)
(propertize
(if (nth 3 song) (nth 3 song) "nil")
'face 'netease-cloud-music-artist-face)))
(insert "\n"))))
(delete-char -1)))
(setq-local buffer-read-only t)))
(defun netease-cloud-music--get-current-playlist (&optional move)
"Get the current playlist.
MOVE means do not care about the cursor's position."
(let ((get (lambda ()
(with-current-buffer netease-cloud-music-buffer-name
(while (overlays-at (point)) ;To get to the playlist's name
(forward-line -1))
(alist-get (buffer-substring-no-properties
(line-beginning-position) (line-end-position))
netease-cloud-music-playlists nil nil 'string-equal)))))
(if move
(funcall get)
(save-mark-and-excursion
(funcall get)))))
(defun netease-cloud-music-change-playlist-mode ()
"Scan the current situation and change the playlist mode accroding to it."
(with-current-buffer netease-cloud-music-buffer-name
(cond ((and (string-equal "Local Playlist:\n" (thing-at-point 'line))
(equal (get-text-property (point) 'face)
'netease-cloud-music-playlists-face))
(setq netease-cloud-music-use-local-playlist t
netease-cloud-music-playlists-songs nil
netease-cloud-music-playlist-id nil
netease-cloud-music-fold-tips t)
(when netease-cloud-music-playlist-refresh-timer
(cancel-timer netease-cloud-music-playlist-refresh-timer)
(setq netease-cloud-music-playlist-refresh-timer nil))
(throw 'stop t))
((equal (get-text-property (point) 'face)
'netease-cloud-music-playlist-face)
(let ((playlist (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(setq netease-cloud-music-fold-tips t)
(netease-cloud-music--switch-playlist playlist)
(throw 'stop t)))
((and (overlays-at (point))
(eq major-mode 'netease-cloud-music-mode))
(save-mark-and-excursion
(let* ((current-line (line-number-at-pos))
(current-mode ;If it's t, means that now is playing local playlist.Otherwise user's playlist.
(progn
(goto-char (point-max))
(search-backward "Local Playlist:")
(if (> current-line (line-number-at-pos))
t
nil)))
current-playlist)
(when (or (not (eq netease-cloud-music-use-local-playlist current-mode))
(and (null current-mode)
(not
(eq (progn
(goto-char (point-min))
(forward-line (1- current-line))
(setq current-playlist
(netease-cloud-music--get-current-playlist)))
netease-cloud-music-playlist-id))))
(setq netease-cloud-music-use-local-playlist current-mode
netease-cloud-music-fold-tips t)
(if current-mode
(setq netease-cloud-music-playlists-songs nil
netease-cloud-music-playlist-id nil)
(setq netease-cloud-music-playlist-id
(if current-playlist
current-playlist
(progn
(goto-char (point-min))
(forward-line (1- current-line))
(netease-cloud-music--get-current-playlist)))
netease-cloud-music-playlists-songs
(netease-cloud-music-get-playlist-songs
netease-cloud-music-playlist-id))))))))))
(defun netease-cloud-music--switch-playlist (name)
"Switch user playlist by its NAME."
(setq netease-cloud-music-use-local-playlist nil
netease-cloud-music-playlist-id
(alist-get name netease-cloud-music-playlists
nil nil 'string-equal)
netease-cloud-music-playlists-songs
(netease-cloud-music-get-playlist-songs
netease-cloud-music-playlist-id))
(when netease-cloud-music-process
(netease-cloud-music-kill-current-song)))
;;; Jump
(defun netease-cloud-music--keep-cursor-visible ()
"Keep cursor visible."
(set-window-point (get-buffer-window netease-cloud-music-buffer-name) (point)))
(defun netease-cloud-music-goto (position)
"Goto the POSITION."
(interactive (list (let ((face '((:inherit font-lock-warning-face :underline t))))
(read-char
(concat "Enter the position("
(propertize "g" 'face face)
" refresh, "
(propertize "u" 'face face)
"ser playlist, "
(propertize "l" 'face face)
"ocal playlist, "
(propertize "c" 'face face)
"urrent song, "
(propertize "s" 'face face)
"witch playlist, "
(propertize "p" 'face face)
"lay song, "
(propertize "f" 'face face)
" play at the first"
"):")))))
(with-current-buffer netease-cloud-music-buffer-name
(pcase position
(?g (netease-cloud-music-interface-init))
(?u
(goto-char (point-min))
(let ((case-fold-search t))
(search-forward "User's Playlists:\n"))
(netease-cloud-music--keep-cursor-visible))
(?l
(goto-char (point-min))
(let ((case-fold-search t))
(search-forward "Local Playlist:\n"))
(netease-cloud-music--keep-cursor-visible))
(?c
(let ((playlist-index (unless netease-cloud-music-use-local-playlist
(netease-cloud-music--cdr-index
netease-cloud-music-playlist-id
netease-cloud-music-playlists)))
(case-fold-search t))
(goto-char (point-min))
(if playlist-index
(progn
(search-forward "User's playlists:\n")
(forward-line (1+ playlist-index)))
(search-forward "Local Playlist:\n"))
(forward-line netease-cloud-music-playlist-song-index)
(netease-cloud-music--keep-cursor-visible)))
(?s
(let ((playlist (netease-cloud-music--jump)))
(when playlist
(netease-cloud-music--switch-playlist playlist))
(netease-cloud-music-interface-init)
(netease-cloud-music--keep-cursor-visible)))
(?p
(let ((song (netease-cloud-music--jump t)))
(when (/= (string-to-number (substring (car song) 1)) 0)
(goto-char (point-min))
(forward-line (1- (cdr song)))
(netease-cloud-music-play-song-at-point))))
(?f (setq netease-cloud-music-playlist-song-index 0)
(netease-cloud-music-playlist-play)))))
(defun netease-cloud-music--cdr-index (ele list)
"Get the index of item in LIST which cdr is equal to ELE."
(catch 'stop
(dotimes (i (length list))
(when (eq (cdr (nth i list)) ele)
(throw 'stop i)))))
(defun netease-cloud-music-jump-read-line ()
"The function for jump by reading line number."
(read-number "Enter the line number(negetive number means up): "))
(defun netease-cloud-music--jump (&optional with-line)
"Get the text from line number.
WHen WITH-LINE is non-nil, return the (content . line-number)."
(if (not (fboundp netease-cloud-music-jump-function))
(netease-cloud-music-error "The jump function cannot be found!")
(let ((line (funcall netease-cloud-music-jump-function))
result)
(save-excursion
(if netease-cloud-music-line-number-relative
(forward-line line)
(goto-char (point-min))
(forward-line (1- line)))
(setq result (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(if with-line
(cons result (if netease-cloud-music-line-number-relative
(+ (line-number-at-pos) line)
line))
result))))
(provide 'netease-cloud-music-ui)
;;; netease-cloud-music-ui.el ends here