all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Luis Gerhorst <privat@luisgerhorst.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Mac: Don't quit Emacs when window is closed
Date: Tue, 19 Jan 2016 22:46:39 +0100	[thread overview]
Message-ID: <A579A77E-FA74-4BFB-AC44-A29E27120CD3@luisgerhorst.de> (raw)
In-Reply-To: <5870E7A7-DF82-484A-95BB-985DF8F419AF@luisgerhorst.de>

I now found a solution that works pretty neat

;; Directly copied from frame.el but now hide Emacs instead of killing
;; it when last frame will be closed.
(defun handle-delete-frame-without-kill-emacs (event)
  "Handle delete-frame events from the X server."
  (interactive "e")
  (let ((frame (posn-window (event-start event)))
        (i 0)
        (tail (frame-list)))
    (while tail
      (and (frame-visible-p (car tail))
           (not (eq (car tail) frame))
           (setq i (1+ i)))
      (setq tail (cdr tail)))
    (if (> i 0)
        (delete-frame frame t)
      ;; Not (save-buffers-kill-emacs) but instead:
      (ns-do-hide-emacs))))

(when (eq system-type 'darwin)
  (advice-add 'handle-delete-frame :override
              #'handle-delete-frame-without-kill-emacs))

This code overrides that functions that handles the close-window button and makes it hide Emacs instead of quitting the application when the last Emacs frame is closed. C-x C-c and everything else still works as expected. It's not completely optimal since it doesn't really close the frame and also does not ask you if you want to save any changed buffers (it does however when you kill Emacs).

> On 19.01.2016, at 19:35, Luis Gerhorst <privat@luisgerhorst.de> wrote:
> 
> Thanks! I tried it but it gives me some error in GNU Emacs. For the beginning I instead added this code wich make C-z no longer painful on OS X:
> 
> (when (eq system-type 'darwin)
>  (global-set-key [remap suspend-frame] 'ns-do-hide-emacs))
> 
> Without it the Emacs window minimizes slowly into the dock on my iMac 27" Retina.
> 
>> On 18.01.2016, at 08:15, Nick Helm <nick@tenpoint.co.nz> wrote:
>> 
>> 
>>> I have the problem that I always quit Emacs by mistake when I am done
>>> with a task by clicking the button to close the Emacs window. I have a
>>> few ideas / questions:
>>> 
>>> - Wouldn't it be a good idea to make Emacs act like most Mac
>>> application and not quit the application when the window is closed? Is
>>> there some reason why this is not possible?
>>> - How can I disable the button to close the window or make it call
>>> suspend-frame instead of save-buffers-kill-emacs?
>> 
>> I wanted to do something similar on my mac-port emacs a while back and
>> added this to my init.el:
>> 
>>  (defun nick-mac-hide-last-frame (orig-fun &rest args)
>>     "Check if last visible frame is being closed and hide it instead."
>>     (if (and (featurep 'mac)
>>              (display-graphic-p nil)
>>              (= 1 (length (frame-list)))) (progn 
>>        (when (eq (frame-parameter (selected-frame) 'fullscreen) 'fullscreen)
>>           (set-frame-parameter (selected-frame) 'fullscreen nil) 
>>           (sit-for 1.2))
>>        (mac-do-applescript "tell application \"System Events\" \
>>           to tell process \"Emacs\" to set visible to false")
>>        (sit-for 1.5)
>>        (modify-frame-parameters (selected-frame) default-frame-alist)
>>        (delete-other-windows)
>>        (switch-to-buffer "*scratch*"))
>>       (apply orig-fun args)))
>> 
>>  (defun nick-handle-delete-frame (event)
>>     "Hide last visible frame when clicking frame close button."
>>     (interactive "e")
>>     (let ((frame (posn-window (event-start event))))
>>        (delete-frame frame t)))
>> 
>>  (defun nick-save-buffers-kill-terminal (&optional arg)
>>     "Hide last visible frame instead of closing Emacs."
>>     (interactive "P")
>>     (delete-frame (selected-frame) t))
>> 
>>  (advice-add 'delete-frame :around #'nick-mac-hide-last-frame)
>>  (advice-add 'handle-delete-frame :override #'nick-handle-delete-frame)
>>  (advice-add 'save-buffers-kill-terminal :override 
>>     #'nick-save-buffers-kill-terminal)
>> 
>> This uses applescript to hide the last frame instead of closing it. The
>> OS takes care of the unhiding when you click on the dock icon, relaunch
>> the app, double click a file, etc. Do Emacs > Quit Emacs or `kill-emacs'
>> to exit.
>> 
>> It doesn't leave the emacs menubar visible after a frame is closed, but
>> I've not found that to be a problem in practice. 
>> 
>> If you don't use the mac-port you can probably do the same thing by
>> dropping in ns-do-applescript, although I haven't tried it. 
>> 
>> Nick
>> 
> 
> 




  reply	other threads:[~2016-01-19 21:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-17 20:07 Mac: Don't quit Emacs when window is closed Luis Gerhorst
2016-01-17 22:40 ` Marcin Borkowski
2016-01-18  4:21   ` Random832
2016-01-19 18:31     ` Luis Gerhorst
     [not found]   ` <mailman.2484.1453090928.843.help-gnu-emacs@gnu.org>
2016-01-18 17:05     ` Barry Margolin
2016-01-18 18:03       ` Philipp Stephani
2016-01-21 17:36       ` repeating a search a certain number of times Timur Aydin
2016-01-21 18:10         ` Drew Adams
2016-01-18  7:15 ` Mac: Don't quit Emacs when window is closed Nick Helm
2016-01-19 18:35   ` Luis Gerhorst
2016-01-19 21:46     ` Luis Gerhorst [this message]
2016-01-19 22:04     ` Nick Helm
2016-01-18  9:11 ` tomas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A579A77E-FA74-4BFB-AC44-A29E27120CD3@luisgerhorst.de \
    --to=privat@luisgerhorst.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.