Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active February 11, 2021 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmouel/c8a901ce7739d63854be3bf314bd596a to your computer and use it in GitHub Desktop.
Save chmouel/c8a901ce7739d63854be3bf314bd596a to your computer and use it in GitHub Desktop.
Integrate GNUS with Argos
(defun gnus-demon-scan-mail-or-news-and-update (level)
"Scan for new mail, updating the *Group* buffer."
(let ((win (current-window-configuration)))
(unwind-protect
(save-window-excursion
(save-excursion
(when (gnus-alive-p)
(save-excursion
(set-buffer gnus-group-buffer)
(gnus-group-get-new-news level)))))
(message "scanning for new mail done")
(set-window-configuration win))))
(defun gnus-demon-scan-news-and-update ()
"Scan for new mail, updating the *Group* buffer."
(gnus-demon-scan-mail-or-news-and-update 1))
;;; ** command
(gnus-demon-add-handler 'gnus-demon-scan-news-and-update 1 nil)
(gnus-demon-add-handler 'gnus-demon-scan-mail 1 nil)
(gnus-demon-init)
;; Export the unread inbox as json to plug into external command line
;; emacsclient --eval "(my-is-there-any-mail-out-there-json)"|sed -e \
;; 's/^"//;s/"$//' -e 's/\\"/"/g'|jq
(defconst my-is-there-any-mail-out-there-limit-to ".*")
(defun my-is-there-any-mail-out-there-json ()
(let ((win (current-window-configuration))
(hashtb (make-hash-table :size 10 :test #'equal))
unread current)
(unwind-protect
(save-window-excursion
(when (gnus-alive-p)
(with-current-buffer gnus-group-buffer
(beginning-of-buffer)
(while (and (not (eobp)))
(beginning-of-line)
(when (and
(get-text-property (point) 'gnus-group)
(string-match
my-is-there-any-mail-out-there-limit-to
(gnus-group-name-at-point)))
(setq unread (get-text-property (point) 'gnus-unread))
(when (and (numberp unread) (> unread 0))
(puthash (gnus-group-name-at-point) unread hashtb)))
(forward-line)))))
(set-window-configuration win))
(json-serialize hashtb)))
(defun my-is-there-any-mail-out-there-focus-group (group-regexp)
(let ((win (current-window-configuration))
found)
(unwind-protect
(save-window-excursion
(save-excursion
(when (gnus-alive-p)
(save-excursion
(set-buffer gnus-group-buffer)
(goto-char (point-min))
(when (re-search-forward group-regexp)
(setq found (point))
))))))
(if found (progn
(switch-to-buffer gnus-group-buffer)
(goto-char found)
(gnus-group-select-group))
(set-window-configuration win))))
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
notify_me_on_inbox = re.compile("INBOX")
only_show_inbox = re.compile(r".*")
remove_gmail_folder = True
def get_inbox_statuses():
try:
emacsclient_output = subprocess.run(
"emacsclient --eval \"(my-is-there-any-mail-out-there-json)\"",
stderr=subprocess.PIPE,
shell=True,
check=True,
stdout=subprocess.PIPE)
except subprocess.CalledProcessError:
return {}
return json.loads(
(emacsclient_output.stdout.decode()[1:-2].replace('\\"', '"')))
def main():
jeez = get_inbox_statuses()
meself = os.path.realpath(__file__)
if not jeez:
print("⬛")
return
icon = "📪"
ret = ["---"]
for inbox, number in jeez.items():
if remove_gmail_folder:
inbox = inbox.split("/")[-1]
if not only_show_inbox.match(inbox):
continue
if notify_me_on_inbox.match(inbox):
icon = "💌"
ret.append(
f"{inbox} ({number}) | bash=\"{meself} {inbox}\" terminal=false")
if len(ret) == 1:
print("⬛")
return
ret.insert(0, f"{icon}")
print("\n".join(ret))
def goto_inbox(inbox):
subprocess.run(
f"emacsclient --eval '(my-is-there-any-mail-out-there-focus-group \"{inbox}\")'",
stderr=subprocess.PIPE,
shell=True,
check=True,
stdout=subprocess.PIPE)
# TO be able to focus the emacs windows get something like jumpapp https://github.com/mkropat/jumpapp
subprocess.run("jumpapp emacs27",
stderr=subprocess.PIPE,
shell=True,
check=True,
stdout=subprocess.PIPE)
if __name__ == '__main__':
if len(sys.argv) > 1:
try:
goto_inbox(sys.argv[1])
except subprocess.CalledProcessError:
pass
else:
main()
@chmouel
Copy link
Author

chmouel commented Feb 11, 2021

This will show a icon in gnome for all your gnus groups.

  • eval the emacs lisp and have it somewhere in your init files
  • Don't forget to add a gnus-demon to check email periodically, i have added some of my configuration in here too.
  • Don't forget to start your emacs server/daemon
  • put the python script in ~/.config/argos
  • you can click on entry and it will focus that group name, you may want to install jumpapp so the wm focus emacs after it selected.

You don't have necessary need to use the gnome argos stuff you can plug it in anything you want, you can just get the json output from the emacsclient, i.e: :

emacsclient --eval "(my-is-there-any-mail-out-there-json)"|sed -e 's/^"//;s/"$//' -e 's/\\"/"/g'|jq 'to_entries|map_values(.key)[]|contains("INBOX")'|grep -q true && echo "You got mail"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment