;; server-x.el -- provide functions for starting emacsserver differently ;; Copyright 2008 by Chetan Pandya ;; ;; This file is NOT (yet) part of GNU Emacs. ;; ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program; if not, you can either send email to this ;; program's maintainer or write to: The Free Software Foundation, ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA. ;; ;;; Commentary: ;; ;; This emacs script provides functions starting emacsserver with ;; additional information than server-start. ;; ;; Installation: ;; ;; Simplest way is to `load' or `require' the file. ;; Customize the startup action, if desired. ;; ;;; Code: ;; User configurable variables: (if (< emacs-major-version 22) (error "Not supported on emacs version before 22.0")) (defcustom server-startup 'check "Deal with existing files at startup" :type '(choice (const :tag "Ignore - delete file. Default in absence of server-x" nil) (const :tag "Search - set server name" search) (const :tag "Abort" check)) :group 'server) (defvar server-file-name nil "Name of the server file") (defvar server-name-base nil "Internal: name of the server to use as base") (defvar server-name-prev "" "Internal: name of the server started earlier") (require 'server) (defadvice server-start (around sstart-around protect) (let ((kill-only (ad-get-arg 0)) ret bypass (i 0) (sname server-name) s-dir s-file) (when (not kill-only) ; killing the server (setq s-dir (if server-use-tcp server-auth-dir server-socket-dir)) (setq s-file (expand-file-name sname s-dir)) (cond ((eq server-startup 'check) (when (file-exists-p s-file) (message "File %s exists.\nPlease delete file if another emacs is not using it or change the value of server-name and try again." s-file) (setq bypass t))) ((eq server-startup 'search) (if (or (null server-name-base) (not (string= server-name server-name-prev))) (setq server-name-base server-name)) (while (file-exists-p s-file) (setq i (1+ i)) (setq sname (format "%s%d" server-name-base i)) (setq s-file (expand-file-name sname s-dir)))))) (let ((server-name sname)) (setq ret (if bypass nil ;; ad-do-it ;; ))) (cond (kill-only (if server-file-name (ignore-errors (delete-file server-file-name))) (setq server-process nil server-file-name nil) (remove-hook 'kill-emacs-hook 'server-kill-on-exit)) (t (cond (server-process (setq server-name sname server-name-prev sname ; if user changes it server-file-name s-file) (add-hook 'kill-emacs-hook 'server-kill-on-exit)) (t (setq server-file-name nil) (remove-hook 'kill-emacs-hook 'server-kill-on-exit))))) ret)) (defun server-kill-on-exit () (when (and server-file-name server-process) ;; killing the server tries to close connections, which could hang. ;; (server-start t) ; kill the server ;; Disable new connections (ignore-errors (delete-file server-file-name)))) (unless (featurep 'gnuserv) (ad-activate 'server-start)) ;;(provide 'server-x) ;; server-x.el ends here