-
Notifications
You must be signed in to change notification settings - Fork 12
/
simple-mpc-query.el
152 lines (131 loc) · 6.43 KB
/
simple-mpc-query.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
;;; simple-mpc-query.el --- part of simple-mpc
;;
;; Copyright (C) 2015,2016 Joren Van Onder <[email protected]>
;; Copyright (C) 2020 Sean Farley <[email protected]>
;; Author: Joren Van Onder <[email protected]>
;; Maintainer: Joren Van Onder <[email protected]>
;; Keywords: multimedia, mpd, mpc
;; Version: 1.0
;; This file is not part of GNU Emacs.
;;
;; 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:
;;; Code:
(require 's)
(require 'simple-mpc-mode)
(require 'simple-mpc-vars)
(require 'simple-mpc-utils)
(defvar simple-mpc-query-current-result-alist nil
"A list containing a pair for every result in the latest query.
The list is an association list of the
form (`simple-mpc-playlist-format'. %file%).")
(define-minor-mode simple-mpc-query-mode
"Minor mode for the simple-mpc-query screen.
\\{simple-mpc-query-mode-map}."
:lighter " simple-mpc-query-mode"
:keymap (let ((map (make-sparse-keymap)))
(define-key map "q" 'simple-mpc-query-quit)
(define-key map "S" 'simple-mpc-query-sort)
(define-key map (kbd "<return>") 'simple-mpc-query-add)
(define-key map (kbd "<S-return>") 'simple-mpc-query-add-and-play)
map)
(set (make-local-variable 'require-final-newline) nil))
(defun simple-mpc-query-quit ()
"Quit the current playlist mode and go back to main."
(interactive)
(kill-buffer simple-mpc-query-buffer-name)
(simple-mpc-switch-to-main-buffer))
(defun simple-mpc-query-get-%file%-for-result (result)
"Helper function to get the %file% from RESULT."
(cdr (assoc result simple-mpc-query-current-result-alist)))
(defun simple-mpc-query-build-result-alist (search-type search-query)
"Builds a list according to SEARCH-TYPE and SEARCH-QUERY.
The list is stored in `simple-mpc-query-current-result-alist'."
(setq simple-mpc-query-current-result-alist
(let* ((file-metadata-delimiter "(simple-mpc)")
(query-format (concat (simple-mpc-get-playlist-format) file-metadata-delimiter "%file%" file-metadata-delimiter)))
(mapcar (lambda (mpc-result)
(let* ((matches (s-match (format "^\\(.*\\)%s\\(.*\\)%s" file-metadata-delimiter file-metadata-delimiter) mpc-result))
(full-match (nth 0 matches))
(user-specified-format (nth 1 matches))
(file (nth 2 matches)))
(cons user-specified-format file)))
(split-string
(simple-mpc-format-as-table (simple-mpc-call-mpc-string
(list "--format" query-format "search" search-type search-query)))
"\n" t)))))
(defun simple-mpc-query (search-type search-query)
"Perform an mpc search.
SEARCH-TYPE is a tag type, SEARCH-QUERY is the actual query."
(interactive
(list
(completing-read "Search type: " '("artist" "album" "title" "track"
"name" "genre" "date" "composer"
"performer" "comment" "disc" "filename"
"any") nil t)
(read-string "Query: ")))
(simple-mpc-query-build-result-alist search-type search-query)
(let ((buf (get-buffer-create simple-mpc-query-buffer-name)))
(with-current-buffer buf
(read-only-mode -1)
(erase-buffer)
(insert (mapconcat (lambda (result) (car result)) simple-mpc-query-current-result-alist "\n"))
(goto-char (point-max))
(goto-char (point-min))
(simple-mpc-mode)
(simple-mpc-query-mode)
(hl-line-mode)
(switch-to-buffer buf))))
(defun simple-mpc-query-add-and-play ()
"Wrapper for (`simple-mpc-query-add' t)."
(interactive)
(simple-mpc-query-add t))
(defun simple-mpc-query-add (&optional play)
"Add the song on the current line to the current playlist.
When a region is active, add all the songs in the region to the
current playlist. When PLAY is non-nil, immediately play them."
(interactive)
(let ((current-amount-in-playlist (simple-mpc-get-amount-of-songs-in-playlist)))
(if (use-region-p)
(let ((first-line-region (line-number-at-pos (region-beginning)))
(last-line-region (if (eq (region-end) (point-max))
(line-number-at-pos (region-end))
(1- (line-number-at-pos (region-end))))) ; usually point is on the next line so 1-
(beginning-first-line-region)
(end-last-line-region))
(save-excursion
(simple-mpc-goto-line first-line-region)
(setq beginning-first-line-region (line-beginning-position))
(simple-mpc-goto-line last-line-region)
(setq end-last-line-region (line-end-position)))
(simple-mpc-call-mpc nil (cons "add" (mapcar (lambda (result)
(simple-mpc-query-get-%file%-for-result result))
(split-string (buffer-substring-no-properties beginning-first-line-region
end-last-line-region)
"\n" t))))
(deactivate-mark))
(simple-mpc-call-mpc nil (list "add" (simple-mpc-query-get-%file%-for-result
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))))
(forward-line))
(if play
(simple-mpc-call-mpc nil (list "play" (number-to-string (1+ current-amount-in-playlist)))))))
(defun simple-mpc-query-sort (&optional reverse)
"Sort all query results alphabetically.
REVERSE means descending order."
(interactive)
(read-only-mode -1)
(sort-lines reverse (point-min) (point-max))
(read-only-mode 1))
(provide 'simple-mpc-query)
;;; simple-mpc-query.el ends here