From c74c4bbca776e4428080db7f30ea14cc1589cc98 Mon Sep 17 00:00:00 2001
From: Peter Liljenberg <peter.liljenberg@gmail.com>
Date: Sun, 11 Jan 2015 23:26:56 +0100
Subject: [PATCH] Terminate keep-alive in kill-buffer-hook.

Since lyskom-keep-alive-timers is a buffer-local variable,
lyskom-keep-alive-callback can't stop the keep-alive timer
if the buffer has been killed.  As a result, it keeps saying ERROR! in
the mini buffer.

On an idea from Christer Ekholm, terminate the timer explicitly with a
kill-buffer-hook instead.  The error logging in
lyskom-keep-alive-callback has been improved too, to provide more
information on what went wrong if there are other error situations in
the future.
---
 src/ChangeLog    |  5 +++++
 src/commands2.el | 10 ++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 73f832fe..578402b5 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2015-12-11  Peter Liljenberg  <peter.liljenberg@gmail.com>
+	* commands2.el: Terminate keep-alive with kill-buffer-hook, as
+	lyskom-keep-alive-callback can't do it once the buffer is killed.
+	Also improve error logging in the callback.
+
 2014-10-30  Christer Ekholm  <che@chrekh.se>
 
 	* Makefile: Use cp -f to copy files to build-directory for target
diff --git a/src/commands2.el b/src/commands2.el
index 0e77cad7..e8368e72 100644
--- a/src/commands2.el
+++ b/src/commands2.el
@@ -2373,13 +2373,14 @@ This command accepts text number prefix arguments \(see
   local)
 
 (defun lyskom-keep-alive-callback (buffer)
-  (condition-case nil
+  (condition-case callback-error
     (save-current-buffer (set-buffer buffer)
                          (if (eq (process-status lyskom-proc) 'open)
                              (initiate-get-time 'keep nil)
                            (lyskom-stop-keep-alive)
                            (message "Terminating keep-alive")))
-    (error (message "ERROR!") (lyskom-stop-keep-alive))))
+    (error (message "Error during keep-alive: %s" callback-error)
+           (lyskom-stop-keep-alive))))
 
 (def-kom-command kom-keep-alive ()
   "Keep the LysKOM session alive by sending a request every once in a
@@ -2423,7 +2424,12 @@ See `kom-keep-alive' for more information."
   (mapc 'disable-timeout lyskom-keep-alive-timers)
   (setq lyskom-keep-alive-timers nil))
 
+(defun lyskom-buffer-kill-keep-alive-hook ()
+  (if lyskom-keep-alive-timers
+      (message "Terminating keep-alive for buffer %s" (buffer-name (current-buffer))))
+  (lyskom-stop-keep-alive))
 
+(add-hook 'kill-buffer-hook 'lyskom-buffer-kill-keep-alive-hook)
 
 ;;; ========================================================================
 ;;;   Check (if Person is a) member (of Conference)
-- 
GitLab