Skip to content

Commit

Permalink
add safe-shutdown-emacs.el
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycode committed Dec 22, 2010
1 parent 8449c2f commit 2454b11
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 80 additions & 0 deletions config/01base/safe-shutdown-emacs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(defun client-save-kill-emacs(&optional display)
" This is a function that can bu used to shutdown save buffers and
shutdown the emacs daemon. It should be called using
emacsclient -e '(client-save-kill-emacs)'. This function will
check to see if there are any modified buffers or active clients
or frame. If so an x window will be opened and the user will
be prompted."

(let (new-frame modified-buffers active-clients-or-frames)

; if there are modified buffers or active clients or frames.
(setq modified-buffers (modified-buffers-exist))
(setq active-clients-or-frames ( or (> (length server-clients) 1)
(> (length (frame-list)) 1)
))

; Create a new frame if prompts are needed.
(when (or modified-buffers active-clients-or-frames)
(when (not (eq window-system 'x))
(message "Initializing x windows system.")
(x-initialize-window-system))
(when (not display) (setq display (getenv "DISPLAY")))
(message "Opening frame on display: %s" display)
(select-frame (make-frame-on-display display '((window-system . x)))))

; Save the current frame.
(setq new-frame (selected-frame))


; When displaying the number of clients and frames:
; subtract 1 from the clients for this client.
; subtract 2 from the frames this frame (that we just created) and the default frame.
(when ( or (not active-clients-or-frames)
(yes-or-no-p (format "There are currently %d clients and %d frames. Exit anyway?" (- (length server-clients) 1) (- (length (frame-list)) 2))))

; If the user quits during the save dialog then don't exit emacs.
; Still close the terminal though.
(let((inhibit-quit t))
; Save buffers
(with-local-quit
(save-some-buffers))

(if quit-flag
(setq quit-flag nil)
; Kill all remaining clients
(progn
(dolist (client server-clients)
(server-delete-client client))
; Exit emacs
(kill-emacs)))
))

; If we made a frame then kill it.
(when (or modified-buffers active-clients-or-frames) (delete-frame new-frame))
)
)


(defun modified-buffers-exist()
"This function will check to see if there are any buffers
that have been modified. It will return true if there are
and nil otherwise. Buffers that have buffer-offer-save set to
nil are ignored."
(let (modified-found)
(dolist (buffer (buffer-list))
(when (and (buffer-live-p buffer)
(buffer-modified-p buffer)
(not (buffer-base-buffer buffer))
(or
(buffer-file-name buffer)
(progn
(set-buffer buffer)
(and buffer-offer-save (> (buffer-size) 0))))
)
(setq modified-found t)
)
)
modified-found
)
)
2 changes: 1 addition & 1 deletion config/99post/function-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
(switch-to-buffer (other-buffer)))

(global-set-key (kbd "C-`") 'other-window)
(global-set-key (kbd "C-;") 'ido-switch-buffer)
(global-set-key (kbd "C-M-;") 'ido-switch-buffer)
(global-set-key "\M-`" 'my-switch-to-last-buffer)

0 comments on commit 2454b11

Please sign in to comment.