changelog shortlog graph tags branches changeset files file revisions raw help

Mercurial > infra > home / annotate .emacs.d/lib/stumpwm-utils.el

changeset 86: a86cb552d6df
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 21 Aug 2024 20:29:13 -0400
permissions: -rw-r--r--
description: stumpwm utils
86
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
1
 (defun stumpwm-connect ()
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
2
   "Start slime and connect to the lisp image that is running the
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
3
   swank server.  Must have \"(require 'swank) 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
4
   (swank:create-server)\" in your .stumpwmrc "
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
5
   (interactive)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
6
   (slime-connect "127.0.0.1"  4005)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
7
   (with-current-buffer (current-buffer)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
8
     (rename-buffer "*sbcl-stumpwm-repl*")
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
9
     (slime-eval '(in-package :stumpwm))))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
10
 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
11
 (defun stumpwm-disconnect ()
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
12
   "Disconnects from the swank server currently open."
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
13
   (with-current-buffer 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
14
       (switch-to-buffer "*sbcl-stumpwm-repl*")
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
15
     (slime-disconnect)))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
16
 ;;Lifted (with stylistic changes) from http://www.emacswiki.org/emacs/EmacsAsDaemon#toc9
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
17
 (defun modified-buffers-exist () 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
18
   "This function will check to see if there are any buffers
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
19
 that have been modified.  It will return true if there are
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
20
 and nil otherwise. Buffers that have buffer-offer-save set to
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
21
 nil are ignored."
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
22
   (let (modified-found)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
23
     (dolist (buffer (buffer-list))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
24
       (when (and (buffer-live-p buffer)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
25
 		 (buffer-modified-p buffer)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
26
 		 (not (buffer-base-buffer buffer))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
27
 		 (or
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
28
 		  (buffer-file-name buffer)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
29
 		  (progn
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
30
 		    (set-buffer buffer)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
31
 		    (and buffer-offer-save (> (buffer-size) 0)))))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
32
 	(setq modified-found t)))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
33
     modified-found))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
34
 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
35
 (defun stumpwm-daemon-shutdown-emacs (&optional display)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
36
   " This is a function that can bu used to shutdown save buffers and 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
37
 shutdown the emacs daemon. It should be called using 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
38
 emacsclient -e '(stumpwm-daemon-shutdown-emacs)'.  This function will
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
39
 check to see if there are any modified buffers or active clients
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
40
 or frame.  If so an x window will be opened and the user will
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
41
 be prompted."
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
42
 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
43
   (let (new-frame modified-buffers active-clients-or-frames)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
44
     ; Check if there are modified buffers or active clients or frames.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
45
     (setq modified-buffers (modified-buffers-exist))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
46
     (setq active-clients-or-frames ( or (> (length server-clients) 1)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
47
 					(> (length (frame-list)) 1)))  
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
48
     ; Create a new frame if prompts are needed.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
49
     (when (or modified-buffers active-clients-or-frames)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
50
       (when (not (eq window-system 'x))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
51
 	(message "Initializing x windows system.")
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
52
 	(x-initialize-window-system))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
53
       (when (not display) (setq display (getenv "DISPLAY")))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
54
       (message "Opening frame on display: %s" display)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
55
       (select-frame (make-frame-on-display display '((window-system . x)))))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
56
     ; Save the current frame.  
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
57
     (setq new-frame (selected-frame))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
58
     ; When displaying the number of clients and frames: 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
59
     ; subtract 1 from the clients for this client.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
60
     ; subtract 2 from the frames this frame (that we just created) and the default frame.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
61
     (when ( or (not active-clients-or-frames)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
62
 	       (yes-or-no-p (format "There are currently %d clients and %d frames. Exit anyway?" 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
63
                                     (- (length server-clients) 1) (- (length (frame-list)) 2)))) 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
64
       ; If the user quits during the save dialog then don't exit emacs.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
65
       ; Still close the terminal though.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
66
       (let((inhibit-quit t))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
67
              ; Save buffers
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
68
 	(with-local-quit
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
69
 	  (save-some-buffers)) 
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
70
 	(if quit-flag
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
71
 	  (setq quit-flag nil)  
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
72
           ; Kill all remaining clients
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
73
 	  (progn
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
74
 	    (dolist (client server-clients)
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
75
 	      (server-delete-client client))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
76
 		 ; Exit emacs
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
77
 	    (kill-emacs)))))
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
78
     ; If we made a frame then kill it.
a86cb552d6df stumpwm utils
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
79
     (when (or modified-buffers active-clients-or-frames) (delete-frame new-frame))))