branch: externals/sxhkdrc-mode
commit 26db393a4e0bc6e9bc3c6a5bde0cd5dc721e00d1
Merge: 82816999f8 f4254c0ba8
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #2 from jneidel/main
    
    Add deamon restart and document usage
---
 README.md       | 19 +++++++++++++++++++
 sxhkdrc-mode.el | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/README.md b/README.md
index 610609f65e..b1bb6bf3c5 100644
--- a/README.md
+++ b/README.md
@@ -12,3 +12,22 @@ technicalities, read the man page `sxhkd(1)`.
   + GitLab: <https://gitlab.com/protesilaos/sxhkdrc-mode>
 + Backronym: Such Xenotropic Hot Keys Demonstrate Robustness and
   Configurability ... mode.
+
+## Usage
+
+Install the mode and use with any `sxhkdrc` file:
+
+```elisp
+(use-package sxhkdrc-mode
+    :ensure t
+    :mode "sxhkdrc.*")
+```
+
+Restart the sxhkd daemon:
+```elisp
+; once
+(sxhkdrc-mode-restart)
+
+; automatically on file write
+(sxhkdrc-mode-auto-restart)
+```
diff --git a/sxhkdrc-mode.el b/sxhkdrc-mode.el
index d47182dfd1..5d5fe1bf84 100644
--- a/sxhkdrc-mode.el
+++ b/sxhkdrc-mode.el
@@ -39,6 +39,8 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'subr-x))
+
 (defgroup sxhkdrc nil
   "Major mode for editing sxhkdrc files.
 SXHKD is the Simple X Hotkey Daemon which is commonly used in
@@ -152,6 +154,44 @@ key chord chain (demarcated by a colon or semicolon)."
           (indent-to indent))
       'no-indent)))
 
+(defun sxhkdrc-mode-restart-process ()
+  "Restart the sxhkd process."
+  (when-let* ((pid (shell-command-to-string "pidof sxhkd")))
+    (call-process "kill" nil 0 nil "-USR1" (string-trim pid))
+    t))
+
+(declare-function notifications-notify "notifications" (&rest params))
+
+(defun sxhkdrc-mode-restart-notify ()
+  "Notify that the sxhkd process has been restarted.
+Read Info node `(elisp) Desktop Notifications' for details."
+  (when (featurep 'dbusbind)
+    (unless (fboundp 'notifications-notify)
+      (require 'notifications))
+    (notifications-notify
+     :title "sxhkdrc-mode"
+     :body "Restarted the SXHKD process"
+     :app-name "Emacs"
+     :app-icon 'emacs
+     :urgency 'normal
+     :sound-file nil)))
+
+(defun sxhkdrc-mode-restart ()
+  "Restart the sxhkd process."
+  (interactive)
+  (when (sxhkdrc-mode-restart-process)
+    (sxhkdrc-mode-restart-notify)))
+
+(define-minor-mode sxhkdrc-mode-auto-restart
+  "Automatically restart sxhkd after saving its file.
+To set this minor mode up when opening a file that uses the
+`sxhkdrc-mode', use the hook `sxhkdrc-mode-hook'."
+  :global nil
+  :init-value nil
+  (if sxhkdrc-mode-auto-restart
+      (add-hook 'after-save-hook #'sxhkdrc-mode-restart nil :local-only)
+    (remove-hook 'after-save-hook #'sxhkdrc-mode-restart :local-only)))
+
 (defvar sxhkdrc-mode-map (make-sparse-keymap)
   "Local keymap for `sxhkdrc-mode' buffers.")
 

Reply via email to