summaryrefslogtreecommitdiff
path: root/view-conf.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-01-29 12:33:03 +0800
committerJSDurand <mmemmew@gmail.com>2021-01-29 12:33:03 +0800
commitc8d4224e42a710b262e8ba67d720f6eff4e89fcb (patch)
treebb631456e910e1aeb8984e00db8e3a605f438561 /view-conf.el
parent43afe6c5db55704f787aaee4cedd7f4c69206b8b (diff)
Change the directory to .emacs.d now.
Now I don't have to use a special command to launch Emacs. And the configuration files are placed in the normal position now.
Diffstat (limited to 'view-conf.el')
-rw-r--r--view-conf.el95
1 files changed, 95 insertions, 0 deletions
diff --git a/view-conf.el b/view-conf.el
index 5465b89..0f0ef29 100644
--- a/view-conf.el
+++ b/view-conf.el
@@ -223,3 +223,98 @@ options to choose from."
(kill-new
(concat "https://www.youtube.com/watch?v="
(substring-no-properties video-name -11))))))
+
+;;; Wifi and Bluetooth handling
+
+;;;###autoload
+(defvar durand-wifi-on-p nil
+ "If WIFI is on or not.
+This is defined in \"/Users/durand/.emacs.d/view-conf.el\"")
+
+;;;###autoload
+(defvar durand-bluetooth-on-p nil
+ "If BLUETOOTH is on or not.
+This is defined in \"/Users/durand/.emacs.d/view-conf.el\"")
+
+;;;###autoload
+(defun durand-wifi-filter (proc output)
+ "Filter function to set the wifi variable.
+This should only be used for the process \"durand-wifi\".
+This is defined in \"/Users/durand/.emacs.d/view-conf.el\""
+ (cond
+ ((string= (process-name proc) "durand-wifi"))
+ ((user-error "Filter function associated with a wrong process.")))
+ (setq durand-wifi-on-p (string-match "On$" output)))
+
+;;;###autoload
+(defun durand-bluetooth-filter (proc output)
+ "Filter function to set the bluetooth variable.
+This should only be used for the process \"durand-bluetooth\".
+This is defined in \"/Users/durand/.emacs.d/view-conf.el\""
+ (cond
+ ((string= (process-name proc) "durand-bluetooth"))
+ ((user-error "Filter function associated with a wrong process.")))
+ (setq durand-bluetooth-on-p (string-match "1" output)))
+
+;;;###autoload
+(defun durand-wifi-or-bluetooth (&optional arg)
+ "Check if WIFI is enabled, then ask for confirmation to toggle WIFI.
+If ARG is non-nil, do the same for Bluetooth."
+ (interactive "P")
+ (cond
+ ((null arg)
+ (make-process
+ :name "durand-wifi"
+ :buffer nil
+ :command '("networksetup" "-getairportpower" "en0")
+ :filter #'durand-wifi-filter
+ :sentinel #'ignore)
+ (accept-process-output (get-process "durand-wifi"))
+ (let* ((prompt (format "WIFI is %s. Do you want to turn WIFI %s"
+ (cond (durand-wifi-on-p "on")
+ ("off"))
+ (cond (durand-wifi-on-p "off?")
+ ("on?"))))
+ (decision (y-or-n-p prompt)))
+ (cond
+ (decision
+ (let ((new-state (cond (durand-wifi-on-p "off")
+ ("on"))))
+ (make-process
+ :name "durand-toggle-wifi"
+ :buffer nil
+ :command (list
+ "networksetup" "-setairportpower"
+ "en0" new-state)
+ :sentinel #'ignore
+ :filter #'ignore)
+ (message "WIFI turned %s" new-state))))))
+ (t
+ (make-process
+ :name "durand-bluetooth"
+ :buffer nil
+ :command '("blueutil" "-p")
+ :filter #'durand-bluetooth-filter
+ :sentinel 'ignore)
+ (accept-process-output (get-process "durand-bluetooth"))
+ (let* ((prompt (format "BLUETOOTH is %s. Do you want to turn BLUETOOTH %s"
+ (cond (durand-bluetooth-on-p "on")
+ ("off"))
+ (cond (durand-bluetooth-on-p "off?")
+ ("on?"))))
+ (decision (y-or-n-p prompt)))
+ (cond
+ (decision
+ (let ((new-state (cond (durand-bluetooth-on-p "0")
+ ("1"))))
+ (make-process
+ :name "durand-toggle-bluetooth"
+ :buffer nil
+ :command (list "blueutil" "-p" new-state)
+ :sentinel 'ignore
+ :filter 'ignore)
+ (message "BLUETOOTH turned %s"
+ (cond (durand-bluetooth-on-p "off")
+ ("on"))))))))))
+
+(define-key global-map (vector 3 ?w) #'durand-wifi-or-bluetooth)