ergoemacs / ergoemacs-mode

ergoemacs-mode
ergoemacs.github.io/
GNU General Public License v3.0
293 stars 35 forks source link

Ctrl + w (ergoemacs-close-current-buffer) don't close '--no-wait' client buffers or already existing buffers. #416

Closed nerditation closed 8 years ago

nerditation commented 8 years ago

when emacsclient is called with the -n option, it would send the file(s) to the server and exit immediately without waiting for the server to finish the editing work. the server side simply don't add that client process to the client list. when I finished editing the file and wanted to close that buffer with Ctrl + w, the command ergoemacs-close-current-buffer will call server-edit function, which actually don't close the buffer. see the documentation of variable server-kill-new-buffers and function server-visit-files.

after reading some of the server code, I think we don't need the ergoemacs-started-emacsclient variable and the hooks (defined in ergoemacs-functions.el) -- we could instead check the variable server-buffer-clients combined with server-existing-buffer. if server-buffer-clients is non-nil, the buffer might be started by a client, so we first call server-edit to inform that we are done, then if server-existing-buffer is non-nil, the buffer won't be killed by the server, we then call kill-buffer to close it.

mattfidler commented 8 years ago

Thanks, I will look into fixing this soon.

mattfidler commented 8 years ago

This should be fixed. Let me know if it works for you.

nerditation commented 8 years ago

great. glad you fixed it. it solved my problem at hand.

actually I made my own attempt to work around this problem before I can get an update from elpa. I almost made exactly the same changes as yours, with minor differences. but I am not very familiar with emacs lisp programming, thus I am not quite confident to do a PR.

here is what changes I made:

--- ergoemacs-functions.orig.el 2016-04-14 11:15:07.794162100 +0800
+++ ergoemacs-functions.el  2016-04-17 10:28:24.580573900 +0800
@@ -1590,20 +1590,6 @@
       (call-interactively 'undo-tree-redo)))))

-(defvar ergoemacs-started-emacsclient nil
-  "Tells if the emacsclient was called.")
-
-(defun ergoemacs-server-switch-hook ()
-  "Turns on `ergoemacs-started-emacsclient' for use with `ergoemacs-close-current-buffer'"
-  (set (make-local-variable 'ergoemacs-started-emacsclient) t))
-
-(defun ergoemacs-server-visit-hook ()
-  "Turns on `ergoemacs-started-emacsclient' for use with `ergoemacs-close-current-buffer'"
-  (unless ergoemacs-started-emacsclient
-    (set (make-local-variable 'ergoemacs-started-emacsclient) t)))
-
-(add-hook 'server-switch-hook 'ergoemacs-server-switch-hook)
-
 (defun ergoemacs-keyboard-quit ()
   "Quit the current command/process.
 Similar to `keyboard-quit', with the following changes:
@@ -1655,19 +1641,21 @@
 • If the buffer is editing a CAPTUREd task in an org-mode file, prompt the user to save before closing.
 • If the buffer is editing a magit commit, prompt the user to save the commit before closing.
 • If it is the minibuffer, exit the minibuffer.
-• If the buffer was created by `server-execute', call `server-edit' instead of `kill-buffer'
+• If the buffer was created by `server-execute':
+    call `server-edit' to notify the waiting clients, and
+    call `kill-buffer' if the server itself did not kill it.

 A emacs buffer is one who's name starts with *.
 Else it is a user buffer."
   (interactive)
   (let (emacs-buff-p
         is-emacs-buffer-after-p
+        server-not-killed-buffer-p
         (org-p (string-match "^[*]Org Src" (buffer-name)))
         (org-capture-p (string-match "CAPTURE-.*\\.org" (buffer-name)))
         (git-commit-p (eq major-mode 'git-commit-mode)))
     (setq emacs-buff-p (if (string-match "^*" (buffer-name)) t nil))
     (cond
-     (ergoemacs-started-emacsclient
+     (server-buffer-clients
       (when (and (buffer-modified-p)
                  (if (equal (buffer-file-name) nil)
@@ -1676,8 +1664,12 @@
         (if (y-or-n-p (format "Buffer %s modified; Do you want to save? " (buffer-name)))
             (save-buffer)
           (set-buffer-modified-p nil)))
-      (server-edit))
+      (setq server-not-killed-buffer-p
+            (or (not server-kill-new-buffers)
+                server-existing-buffer))
+      (server-edit)
+      (when server-not-killed-buffer-p
+        (kill-buffer (current-buffer))))
      ((minibufferp)
       (minibuffer-keyboard-quit))
      (org-capture-p
@@ -1707,13 +1699,13 @@
                  org-p)
         (if (y-or-n-p (format "Buffer %s modified; Do you want to save? " (buffer-name)))
             (org-edit-src-save)
-          (set-buffer-modified-p nil)))
-      ;; if emacs buffer, switch to a user buffer
-      (if (string-match "^*" (buffer-name))
-          (setq is-emacs-buffer-after-p t)
-        (setq is-emacs-buffer-after-p nil))
-      (when is-emacs-buffer-after-p
-        (ergoemacs-next-user-buffer))))))
+          (set-buffer-modified-p nil))))
+     ;; if emacs buffer, switch to a user buffer
+     (if (string-match "^*" (buffer-name))
+       (setq is-emacs-buffer-after-p t)
+       (setq is-emacs-buffer-after-p nil))
+     (when is-emacs-buffer-after-p
+       (ergoemacs-next-user-buffer)))))

 (defun ergoemacs-open-last-closed ()
   "Open the last closed file."
mattfidler commented 8 years ago

Thanks for the diff. It would have worked just fine. I noticed I forgot to check the server-kill-new-buffers variable and have updated the code for this.