]> njoseph.me Git - nimcoon.git/blobdiff - nimcoon.el
Allow searching from Emacs
[nimcoon.git] / nimcoon.el
index 5fdb2c9ad8c5bdd5d173ad43dc3ee924994a95b7..5146a9198ddaa42bbafc01c3e91ef043afeda68b 100644 (file)
@@ -5,8 +5,8 @@
 ;;; (load! "nimcoon")
 
 ;;; Non-interactive functions to respond to URL clicks
-(defun nimcoon-play-url (url)
-  "Play given url in NimCoon"
+(defun nimcoon-play-url (url &rest args)
+  "Play given URL in NimCoon."
   (start-process "nimcoon" nil "nimcoon" url))
 
 ;; Play all YouTube URLs in NimCoon
        (("youtu\\.?be" . nimcoon-play-url)
         ("." . browse-url-default-browser))))
 
+(defun run-nimcoon(args query)
+  "Search by QUERY and play in NimCoon."
+  (call-process "nimcoon" nil 0 nil args query))
+
 ;;; Interactive functions
 (defun nimcoon-feeling-lucky-music(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-m" "-l" (format "'%s'" query)))
+  (run-nimcoon "-ml" query))
 
 (defun nimcoon-feeling-lucky-video(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-l" (format "'%s'" query)))
+  (run-nimcoon "-l" query))
 
 (defun nimcoon-download-video(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-d" "-l" (format "'%s'" query)))
+  (run-nimcoon "-dl" query))
 
 (defun nimcoon-download-music(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-d" "-l" "-m" (format "'%s'" query)))
+  (run-nimcoon "-dlm" query))
+
+;; Assumes only one process exists. Must capture the pid of the running NimCoon process and only kill it.
+(defun nimcoon-kill-background-processes()
+  "Kill NimCoon process running in the background. Useful for stopping background music."
+  (interactive)
+  (shell-command "kill `pgrep nimcoon` `pgrep mpv` `pgrep vlc`"))
+
+(defun nimcoon-search-video(query)
+  "Search for a video by QUERY."
+  (interactive "sSearch query: ")
+  (with-output-to-temp-buffer "*NimCoon search results*"
+    (call-process "nimcoon" nil "*NimCoon search results*" t "--non-interactive" query)
+    (with-current-buffer "*NimCoon search results*"
+      (org-mode))))
+
+(defun nimcoon-search-music(query)
+  "Search for a video by QUERY."
+  (interactive "sSearch query: ")
+  (with-output-to-temp-buffer "*NimCoon search results*"
+    (call-process "nimcoon" nil "*NimCoon search results*" t "--music" "--non-interactive" query)
+    (with-current-buffer "*NimCoon search results*"
+      (org-mode))))
 
 ;;; Keybindings
 (map! :leader
@@ -39,5 +65,9 @@
        (:prefix ("d" . "Download")
         :desc "Video" "v" #'nimcoon-download-video
         :desc "Music" "m" #'nimcoon-download-music)
+       (:prefix ("s" . "Search")
+        :desc "Video" "v" #'nimcoon-search-video
+        :desc "Music" "m" #'nimcoon-search-music)
+       :desc "Kill"  "k" #'nimcoon-kill-process
        :desc "Video" "v" #'nimcoon-feeling-lucky-video
        :desc "Music" "m" #'nimcoon-feeling-lucky-music))