Question

I am looking for a general way for Emacs to do some checks, and (bing) when something is true or false.

I have 2 examples at the moment. There is an auction website, and I figured it would be nice for Emacs to check whether there has been an update. I did this using R (Emacs-ESS) to load source code of the website. Then I use a selfmade function in Emacs to:

Switch buffer, reload the URL info, send the info to the screen, (sleep-for ) to wait a little. Search for "Today". Then it has to check the value after this string, if it is higher than 0, then it means I have something new of interest, and it PINGS.

This is a long introduction, but I really like that this works.

I now do the same with Gnus, I have a timer that runs a function that opens Gnus every 10 seconds, searches for "Inbox", checks the value, and if it is higher than 0 then it notifies me (otherwise it just switches the buffer back). The only problem is that this takes around 0.5 seconds, in which you can see point move to another buffer and switch back.

Is there a general approach to have these automated things be done, without disturbing the user?

EDIT: Wouldn't it be nice for Emacs to perform a check to see whether there is a new post with our favorite tag here on Stackoverflow?

Was it helpful?

Solution

Emacs is single-threaded, so we are screwed - there is not way to do this without bothering the user at all.

The trick is to select a good balance between the frequency and cost of the check so that the user can bear it.

OTHER TIPS

Here is the code I used once:

(defvar sds-new-mail-line nil "cache")
(defun sds-new-mail-line (&optional arg)
  "add or remove the mode-line new-mail marker"
  (or sds-new-mail-line (error "sds-new-mail-line has not been initialized"))
  (let* ((mlf (default-value 'mode-line-format))
         (already-have (eq sds-new-mail-line (car mlf))))
    (if (or (eq arg nil) (< arg 0))
        (when already-have
          (setq-default mode-line-format (cdr mlf)))
        (unless already-have
          (setq-default mode-line-format (cons sds-new-mail-line mlf))))))
(defun sds-gnus-scan-mail ()
  "check for new mail, notify if there is some"
  (when (gnus-alive-p)
    (with-current-buffer gnus-group-buffer
      (gnus-group-get-new-news 3)
      (gnus-group-get-new-news 2)
      (goto-char (point-min))
      ;; look for new messages in groups of level 1 and 2
      (cond ((search-forward-regexp "^ *s[12] *[1-9][0-9]*n" nil t)
             (message "you have new mail! (%s)" (user-time-format))
             (sds-new-mail-line 1)
             (ding))
            (t (sds-new-mail-line -1)
               (message "no new mail (%s)" (user-time-format))))
      (goto-char (point-min)))))

(defun sds-gnus-load-hook ()
  (unless sds-new-mail-line     ; init
    (let ((str "mail") (map (make-sparse-keymap)))
      (define-key map [mode-line down-mouse-1] 'ignore)
      (define-key map [mode-line mouse-1] read-mail-command)
      (add-text-properties 0 (length str)
                           (list 'display gnus-mode-line-image-cache
                                 'help-echo "you have new mail - read it!"
                                 'local-map map)
                           str)
      (setq sds-new-mail-line str))
    (gnus-demon-add-handler 'sds-gnus-scan-mail 3 t))
  (add-hook 'gnus-summary-prepared-hook 'gnus-summary-first-unread-subject)
  (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-catchup))

;; cannot use gnus-load-hook here!
(eval-after-load "gnus-start" '(sds-gnus-load-hook))

I am sure you can adapt it to your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top