From ad509f2fc3dbbe7303887a55d9a69140a8887857 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 2 Sep 2021 15:35:18 +0800 Subject: bongo: bulk replace song names in playlists * bongo.el (durand-bongo-replace-name-in-playlist-files): When I moved some songs to sub-directories, I would like to quickly change the song names in the playlist files quickly, otherwise when playing songs from the playlists, they will become broken links. This function provides exactly this functionality. For the usage see the documentation of this function. --- bongo.el | 59 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/bongo.el b/bongo.el index 8234c07..09919e5 100644 --- a/bongo.el +++ b/bongo.el @@ -186,26 +186,7 @@ Adapted from Protesilaos' dotemacs by Durand." (playlist (directory-files path t dotless))) (with-bongo-playlist-buffer (bongo-insert-playlist-contents - (completing-read "Select playlist: " playlist nil t path)) - ;; (ivy-read "Choose a playlist file: " playlist - ;; :require-match t - ;; :caller 'durand-bongo-insert-delete-playlist - ;; :action '(1 - ;; ("i" bongo-insert-playlist-contents "insert playlist") - ;; ("d" (lambda (file) - ;; (delete-file file) - ;; (setf (ivy-state-collection ivy-last) - ;; (directory-files - ;; (file-name-as-directory - ;; (expand-file-name - ;; "playlists" - ;; bongo-default-directory)) - ;; t directory-files-no-dot-files-regexp)) - ;; (ivy--reset-state ivy-last)) - ;; "delete playlist") - ;; ("e" find-file "edit"))) - ;; (prot/bongo-play-random) - ))) + (completing-read "Select playlist: " playlist nil t path))))) ;;;###autoload ;; (defun durand-bongo-dired-ivy-find-to-add () @@ -714,6 +695,44 @@ Afterwards just stop the annoying timer." 'mpv)) (durand-bongo-seek-start-timer bongo-player))))) +;;; replace names in playlist files + +(defun durand-bongo-replace-name-in-playlist-files (old new) + "Replace names in the playlist files containing OLD by the name in \ +NEW subdirectory. +If one moves songs containing the string NAME to the subdirectory +NAME in its original directory, where NAME is the name of the +singer, then running this function with parameters both equal to NAME +will do the right renaming." + (interactive "sOld name: \nsNew name: ") + (let ((files (directory-files + (expand-file-name + "playlists" bongo-default-directory) + nil "m3u$" t))) + (mapc + (lambda (file) + (with-temp-buffer + (insert-file-contents file) + (goto-char (point-min)) + (cond + ((save-excursion (re-search-forward old nil t)) + (while (re-search-forward old nil t) + (let* ((file-name + (buffer-substring-no-properties + (line-beginning-position) (line-end-position))) + (expanded + (expand-file-name + (file-name-nondirectory file-name) + (expand-file-name + new (file-name-directory file-name)))) + (po (line-beginning-position))) + (delete-region (line-beginning-position) (line-end-position)) + (goto-char po) + (insert expanded) + (goto-char (+ po (length expanded))))) + (write-region nil nil file))))) + files))) + ;; Normally we should do this, but I override the function by my ;; custom function, so I can just call this function there. -- cgit v1.2.3-18-g5258