[STUMP] How can I generate an unique file for emacsclient --eval '(load file)'

Feng Shu <[email protected]>
Newsgroups gmane.comp.window-managers.stumpwm.devel
Message-ID <[email protected]>
Hi everyone,

The below configure is stumpwm configure for emacs,
My question is that how can I write a function which
can generate an unique file, like emacs lisp function `with-temp-file',
so I can generate a temp file instead use *emacsclient-load-file*


#+begin_src common-lisp

(setq *stumpwmrc-file* "~/project/stumpwm-config/stumpwmrc")
(set-contrib-dir "~/project/stumpwm-contrib/")
(setq *swank-loader-file* "~/project/emacs-packages/slime/swank-loader.lisp")
(setq *emacs-daemon-name* "default")
(setq *emacsclient-load-file* "~/.stumpwm.d/emacsclient-load-file.el")


;; load Swank so we can connect with SLIME
(load *swank-loader-file*)
(swank-loader:init)
(defvar *swank-p* nil)

;; define swank command to start swank server on port 4005
(defcommand swank () ()
  "Starts a swank server on port 4005 and notifies the user."
  (setf stumpwm:*top-level-error-action* :break)
  (if *swank-p*
      (message "Swank server already running.")
      (progn
	(swank:create-server :port 4005
			     :style swank:*communication-style*
			     :dont-close t)
	(setf *swank-p* t)
	(message "Starting swank on port 4005."))))

(defcommand slime () ()
  (swank)
  (emacsclient-eval
   `(progn
      (require 'slime)
      (slime-connect "127.0.0.1" "4005"))))

(defun my-run-or-raise (cmd props &optional (all-groups *run-or-raise-all-groups*)
				    (all-screens *run-or-raise-all-screens*))
  (let* ((group (current-group))
	 (frames (when (eq (type-of group) 'tile-group)
		   (group-frames group))))
    (if (> (length frames) 1)
	(run-or-pull cmd props all-groups all-screens)
	(run-or-raise cmd props all-groups all-screens))))

(defcommand emacs-daemon-kill-soft () ()
  (emacsclient-eval
   '(progn
     (when (not (eq window-system 'x))
       (message "Initializing x windows system.")
       (x-initialize-window-system)
       (when (not x-display-name) (setq x-display-name (getenv "DISPLAY")))
       (select-frame (make-frame-on-display x-display-name '((window-system . x)))))
     (let ((last-nonmenu-event nil)(window-system "x"))(save-buffers-kill-emacs)))
   t))

(defcommand emacs-daemon-kill-hard () ()
  (run-shell-command
   (format nil "for i in `ps ax | grep emacs | grep =~A |cut -d ' ' -f 1`; do kill -9 $i; done"
	   *emacs-daemon-name*))
  (message (format nil "emacs daemon which name is (~A) has been killed" *emacs-daemon-name*)))

(defun escape-instance-name (str)
  (let (buf)
    (map nil #'(lambda (ch)
		 (if (or (char= ch #\()
			 (char= ch #\))
			 (char= ch #\")
			 (char= ch #\ )
			 (char= ch #\Newline))
		     (push #\_ buf)
		     (push ch buf)))
	 str)
    (coerce (reverse buf) 'string)))

(defun emacs-daemon-run-p ()
  (string-equal
   (remove #\Newline
	   (run-shell-command
	    (concat
	     "emacsclient "
	     "--socket-name=" *emacs-daemon-name*
	     " --eval 't'") t)) "t"))

(defun generate-elisp-expression-string (expression)
  (cl-ppcre:regex-replace-all
   "stumpwm::"
   (write-to-string expression :case :downcase) ""))

(defcommand bare-emacs-run (expression) ()
  (let ((expression-string
	 (generate-elisp-expression-string expression)))
    (run-shell-command (concat "emacs -Q  --eval '" expression-string "'"))))

(defcommand emacs-daemon-start () ()
  (when (not (emacs-daemon-run-p))
    (run-shell-command
     (concat
      "x-terminal-emulator -T emacs-daemon-start -e emacs "
      (if *emacs-daemon-name* (concat "--daemon=" *emacs-daemon-name*)
	  "--daemon")))))

(defun emacsclient-eval (&optional expression create-frame frame-name file)
  "Eval an emacs lisp expression which emacsclient"
  (let* ((expression-string
	  (generate-elisp-expression-string expression))
	 (name (or frame-name
		   (escape-instance-name expression-string))))
    ;; write emacs lisp sexp to a file, all symbol names
    ;; will be converted to downcase when save.
    (with-open-file
	(stream *emacsclient-load-file*
		:direction :output
		:if-exists :supersede)
      (with-standard-io-syntax
	(format stream "~A" expression-string)))

    ;; start emacs daemon
    (emacs-daemon-start)

    ;; run emacs client
    (my-run-or-raise
     (format nil
	     (concat "for i in {1..20};"
		     "do emacsclient --socket-name=~A --eval 't'  > /dev/null 2>&1 && break;"
		     "sleep 1;"
		     "done;"
		     "emacsclient ~@[--socket-name=~A ~]"
		     "~:[ ~;-c~] ~@[-F '((name . \"~A\"))' ~]"
		     "~@[--eval '(load \"~A\")' ~] ~@[~A~]")
	     *emacs-daemon-name*
	     *emacs-daemon-name*
	     create-frame
	     name
	     *emacsclient-load-file*
	     file)
     `(:instance ,name))))

(defcommand emacs () ()
  (emacsclient-eval '(recentf-open-files) t "emacs"))

(defcommand org-agenda () ()
  (emacsclient-eval '(progn (org-agenda nil "l") (delete-other-windows)) t))

(defcommand gnus () ()
  (emacsclient-eval '(gnus) t))

(defcommand edit-stumpwmrc () ()
  (emacsclient-eval
   `(find-file ,*stumpwmrc-file*)
   t "edit-stumpwmrc"))

#+end_src

--
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.