<commit_before>;;; db-music.el -- Music related stuff -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

(require 'dash)
(require 'emms)

(defun db/-emms-playlist-from-files (files)
  "Generate EMMS playlist from FILES.

Shuffle it and start playing it afterwards."
  (when (seq-empty-p files)
    (user-error "List of files is empty, nothing to do"))
  (save-window-excursion
    (let ((music-buffer-name "*EMMS Playlist* -- Personal"))
      (unless (get-buffer music-buffer-name)
        (emms-playlist-new music-buffer-name))
      (with-current-buffer (get-buffer music-buffer-name)
        (emms-stop)
        (emms-playlist-set-playlist-buffer)
        (emms-playlist-current-clear)
        (dolist (track files)
          (emms-playlist-current-insert-source 'emms-insert-file track))
        (goto-char (point-min))
        (emms-shuffle)
        (emms-playlist-select-first)
        (emms-start)))))

(defun db/play-playlist-from-cache ()
  "Start playing songs from `db/playlist’"
  (interactive)
  (db/-emms-playlist-from-files
   (->> db/playlist
        (cl-remove-if-not #'(lambda (track)
                              (eq (cdr track) :include)))
        (mapcar #'car))))

(defun db/play-playlist-from-git-annex-find (match-expression)
  "Generate playlist from git annex find on MATCH-EXPRESSION.

Prompts for MATCH-EXPRESSION when called interactively.
Generates playlist that is comprised of exactly those files that
are match it.  Assumes `emms-source-file-default-directory’ to be
part of a git-annex repository, and will complain otherwise."
  (interactive "smatch expression: ")
  ;; XXX check for git-annex
  (let* ((default-directory emms-source-file-default-directory))
    (db/-emms-playlist-from-files
     (->> (split-string (with-output-to-string
                          (with-current-buffer standard-output
                            (apply #'call-process
                                   "git" nil t nil
                                   "annex" "find"
                                   (split-string match-expression))))
                        "\n")
          (cl-remove-if-not #'(lambda (path)
                                (and (not (string-empty-p path))
                                     (file-exists-p path)
                                     (file-readable-p path))))
          (mapcar #'(lambda (path)
                      (expand-file-name
                       path
                       emms-source-file-default-directory)))))))

(defun db/update-playlist-from-directory (directory)
  "Recursively traverse DIRECTORY and update `db/playlist’.

Files not present `db/playlist’ but that are found in DIRECTORY
are added to `db/playlist’ with tag :undecided, to show the user<commit_msg>[Music] Check return value of call to git-annex-find<commit_after>;;; db-music.el -- Music related stuff -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

(require 'dash)
(require 'emms)

(defun db/-emms-playlist-from-files (files)
  "Generate EMMS playlist from FILES.

Shuffle it and start playing it afterwards."
  (when (seq-empty-p files)
    (user-error "List of files is empty, nothing to do"))
  (save-window-excursion
    (let ((music-buffer-name "*EMMS Playlist* -- Personal"))
      (unless (get-buffer music-buffer-name)
        (emms-playlist-new music-buffer-name))
      (with-current-buffer (get-buffer music-buffer-name)
        (emms-stop)
        (emms-playlist-set-playlist-buffer)
        (emms-playlist-current-clear)
        (dolist (track files)
          (emms-playlist-current-insert-source 'emms-insert-file track))
        (goto-char (point-min))
        (emms-shuffle)
        (emms-playlist-select-first)
        (emms-start)))))

(defun db/play-playlist-from-cache ()
  "Start playing songs from `db/playlist’"
  (interactive)
  (db/-emms-playlist-from-files
   (->> db/playlist
        (cl-remove-if-not #'(lambda (track)
                              (eq (cdr track) :include)))
        (mapcar #'car))))

(defun db/play-playlist-from-git-annex-find (match-expression)
  "Generate playlist from git annex find on MATCH-EXPRESSION.

Prompts for MATCH-EXPRESSION when called interactively.
Generates playlist that is comprised of exactly those files that
are match it.  Assumes `emms-source-file-default-directory’ to be
part of a git-annex repository, and will complain otherwise."
  (interactive "smatch expression: ")
  (let* ((default-directory emms-source-file-default-directory))
    (db/-emms-playlist-from-files
     (->> (split-string (with-output-to-string
                          (with-current-buffer standard-output
                            (let ((return-value (apply #'call-process
                                                       "git" nil t nil
                                                       "annex" "find"
                                                       (split-string match-expression))))
                              (unless (zerop return-value)
                                (error "Call to `git-annex-find’ failed: %s"
                                       (buffer-string))))))
                        "\n")
          (cl-remove-if-not #'(lambda (path)
                                (and (not (string-empty-p path))
                                     (file-exists-p path)
                                     (file-readable-p path))))
          (mapcar #'(lambda (path)
                      (expand-file-name
                       path
                       emms-source-file-default-directory)))))))

(defun db/update-playlist-from-directory (directory)
  "Recursively traverse DIRECTORY and update `db/playlist’.

Files not present `db/playlist’ but that are found in DIRECTORY
are added to `db/playlist’ with tag :undecided, to show the user
{'fraction_non_alphanumeric': 0.12270998963014172, 'fraction_numerical': 0.0, 'mean_word_length': 1.864851485148515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3999168', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1667, 'n_words': 420}