home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / server-num.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  15.7 KB  |  632 lines

  1. ;From ark1!nems!mimsy!haven!udel!wuarchive!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!att.att.com!ho5cad!msb Fri Oct 27 08:55:32 1989
  2. ;Article 331 of gnu.emacs.bug
  3. ;Path: ark1!nems!mimsy!haven!udel!wuarchive!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!att.att.com!ho5cad!msb
  4. ;From ho5cad!msb@att.att.com
  5. ;Newsgroups: gnu.emacs.bug
  6. ;Subject: emacsclient.c & server.el
  7. ;Message-ID: <8910262247.AA21884@life.ai.mit.edu>
  8. ;Date: 26 Oct 89 18:25:00 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Reply-To: msb@ho5cad.att.com (Mike Balenger)
  11. ;Distribution: gnu
  12. ;Organization: GNUs Not Usenet
  13. ;Lines: 379
  14. ;
  15. ;
  16. ;(insert (emacs-version))
  17. ;GNU Emacs 18.55.1 of Sun Aug 27 1989 on ho5cad (usg-unix-v)
  18. ;
  19. ;
  20. ;If I invoke emacsclient with the following arguments, 
  21. ;    +20 .profile +30 /etc/passwd .env
  22. ;
  23. ;On a SysV machine, emacsclient effectively sends 
  24. ;    $PWD/+20 $PWD/.profile $PWD/+30 /etc/passwd $PWD/.env
  25. ;
  26. ;but on a BSD machine, emacsclient effectively sends
  27. ;    +20 $PWD/.profile +30 /etc/passwd $PWD/.env
  28. ;
  29. ;That is, the BSD main() is smart enough to recognize the difference
  30. ;between filename arguments and line number arguments, and prefix only
  31. ;(non-absolute) filenames with the current working directory.
  32. ;
  33. ;The fact that emacsclient handles line numbers is strange, since
  34. ;server.el doesn't handle them.  There was a posting that I call
  35. ;server_num.el which does handle the \+[0-9]+ regexp recognition of
  36. ;line numbers.  I have included a copy of it, in case you didn't catch
  37. ;it on the way through.  It is a minor change to the server.el code.
  38. ;Server_num.el was apparently written by someone working on a SysV
  39. ;machine, because the regexp was looking for /\+[0-9]+, that is a
  40. ;leading "/" before the "+". 
  41. ;
  42. ;Removing the "/" from the regep, and fixing the previous difference in
  43. ;emacsclient will allow the two packages to work together on either
  44. ;system.
  45. ;
  46. ;There was also another annoyance in the revert-buffer function of the
  47. ;server.el such that second and subsequent emacsclient requests for the
  48. ;same file would require user input to revert the file.  That behavior
  49. ;is changed in the enclosed server_num.el
  50. ;
  51. ;A request for a future enhancement -- could you add code to have the
  52. ;server-edit do a kill-buffer on the current buffer.  This action
  53. ;should be based on a prefix argument, since I don't always want this
  54. ;action.
  55. ;
  56. ;I have EDITOR set to emacsclient.  At some times, this creates a lot
  57. ;of /tmp/$$ files from stuff like mailers, MR entry tools, and DB
  58. ;update tools.  In these cases, the temporary files and their backup
  59. ;files are useless trash after finishing the file modification with
  60. ;C-#.  Having them stick around clutters up emacs buffers, AND the file
  61. ;system.  The application removes them, but emacs comes along later and
  62. ;auto-saves them and the backup versions of them.
  63. ;
  64. ;At other times, I have files brought into buffers from a code
  65. ;searching tool, or more(1).  In this case, it would be nice to have
  66. ;the files stick around, and have normal auto-saving, since these are
  67. ;"REAL" files.  The other ones were "TEMPORARY" files.
  68. ;
  69. ;If you have any questions about this, please call me (collect) or send
  70. ;E-mail.
  71. ;
  72. ;Thanks,
  73. ;Mike Balenger
  74. ;----------------------------------------------------------------------
  75. ;<cute quote>            Michael S. Balenger             (201) 949-8789
  76. ;<cute disclaimer>       AT&T Bell Labs
  77. ;                        Room 1L-405
  78. ;msb@ho5cad.att.com      Crawfords Corner Road
  79. ;att!ho5cad!msb          Holmdel, NJ   07733
  80. ;
  81. ;================================================================
  82. ;begin server_num.el
  83. ;================================================================
  84. ;;From ilham@athena.mit.edu Fri Aug  5 09:36:45 1988
  85. ;;From: ilham@athena.mit.edu (Ilhamuddin Ahmed)
  86. ;;Newsgroups: comp.emacs
  87. ;;Subject: Emacsclient taking line-number as an argument
  88. ;;Keywords: emacsclient, arguments
  89. ;;Date: 4 Aug 88 22:27:07 GMT
  90. ;;Reply-To: ilham@juicy-juice.lcs.mit.edu (Ilhamuddin Ahmed)
  91. ;;Organization: MIT Laboratory for Computer Science
  92. ;;
  93. ;;
  94. ;;I sent a message before saying I had an emacs lisp code which allows
  95. ;;emacsclient to take linenumber as an argument. Well, at first I posted
  96. ;;about 6 of them directly but there seems to be a pretty high demand so I
  97. ;;am posting it to the net.
  98. ;;
  99. ;;                        - Ilham
  100. ;;
  101. ;;
  102. ;;==============================================================================
  103. ;;
  104. ;;            \\\!///             From  :   Ilhamuddin Ahmed
  105. ;;             _   _              UUCP  :   {backbone}!mit-eddie!athena!ilham
  106. ;;           ( Q   Q )            Arpa  :   ilham@juicy-juice.lcs.mit.edu
  107. ;; ---,,,,-------U-------,,,,---  USnail:   MIT Laboratory for Computer Science
  108. ;;                                          545 Technology Square, Room 213
  109. ;;            HELLO!!!                      Cambridge, MA 02139.
  110. ;; -----------------------------  Phone :   (617)-253-3578
  111. ;;
  112. ;;==============================================================================
  113.  
  114.  
  115.  
  116. ;; server.el     (emacsclient +number filename)
  117. ;;
  118.  
  119. ;; Michael Platoff
  120. ;; Siemens Research and Technology Labs
  121. ;; 105 College Road East
  122. ;; Princeton, NJ 08540-6668
  123. ;; (609) 734-3354
  124. ;; EMAIL: map@cadillac.siemens.com
  125. ;;        {allegra,princeton}!siemens!cadillac.siemens.com!map
  126.  
  127. ;; Lisp code for GNU Emacs running as server process.
  128. ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  129. ;; Author William Sommerfeld, wesommer@athena.mit.edu.
  130. ;; Changes by peck@sun.com and by rms.
  131.  
  132. ;; This file is part of GNU Emacs.
  133.  
  134. ;; GNU Emacs is distributed in the hope that it will be useful,
  135. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  136. ;; accepts responsibility to anyone for the consequences of using it
  137. ;; or for whether it serves any particular purpose or works at all,
  138. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  139. ;; License for full details.
  140.  
  141. ;; Everyone is granted permission to copy, modify and redistribute
  142. ;; GNU Emacs, but only under the conditions described in the
  143. ;; GNU Emacs General Public License.   A copy of this license is
  144. ;; supposed to have been given to you along with GNU Emacs so you
  145. ;; can know your rights and responsibilities.  It should be in a
  146. ;; file named COPYING.  Among other things, the copyright notice
  147. ;; and this notice must be preserved on all copies.
  148.  
  149.  
  150. ;;; This Lisp code is run in Emacs when it is to operate as
  151. ;;; a server for other processes.
  152.  
  153. ;;; Load this library and do M-x server-edit to enable Emacs as a server.
  154. ;;; Emacs runs the program ../etc/server as a subprocess
  155. ;;; for communication with clients.  If there are no client buffers to edit, 
  156. ;;; server-edit acts like (switch-to-buffer (other-buffer))
  157.  
  158. ;;; When some other program runs "the editor" to edit a file,
  159. ;;; "the editor" can be the Emacs client program ../etc/emacsclient.
  160. ;;; This program transmits the file names to Emacs through
  161. ;;; the server subprocess, and Emacs visits them and lets you edit them.
  162.  
  163. ;;; Note that any number of clients may dispatch files to emacs to be edited.
  164.  
  165. ;;; When you finish editing a Server buffer, again call server-edit
  166. ;;; to mark that buffer as done for the client and switch to the next 
  167. ;;; Server buffer.  When all the buffers for a client have been edited 
  168. ;;; and exited with server-edit, the client "editor" will return
  169. ;;; to the program that invoked it.  
  170.  
  171. ;;; Your editing commands and Emacs's display output go to and from
  172. ;;; the terminal in the usual way.  Thus, server operation is possible
  173. ;;; only when Emacs can talk to the terminal at the time you invoke
  174. ;;; the client.  This is possible in two cases:
  175.  
  176. ;;; 1. On a window system, where Emacs runs in one window and the
  177. ;;; program that wants to use "the editor" runs in another.
  178.  
  179. ;;; 2. When the program that wants to use "the editor" is running
  180. ;;; as a subprocess of Emacs.
  181.  
  182. ;;; The buffer local variable "server-buffer-clients" lists 
  183. ;;; the clients who are waiting for this buffer to be edited.  
  184. ;;; The global variable "server-clients" lists all the waiting clients,
  185. ;;; and which files are yet to be edited for each.
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. (defvar server-program "server"
  247.   "*The program to use as the edit server")
  248.  
  249. (defvar server-process nil 
  250.   "the current server process")
  251.  
  252. (defvar server-clients nil
  253.   "List of current server clients.
  254. Each element is (CLIENTID FILES...) where CLIENTID is a string
  255. that can be given to the server process to identify a client.
  256. When a buffer is marked as \"done\", it is removed from this list.")
  257.  
  258. (defvar server-buffer-clients nil
  259.   "List of clientids for clients requesting editing of current buffer.")
  260.  
  261. (make-variable-buffer-local 'server-buffer-clients)
  262. (setq-default server-buffer-clients nil)
  263. (or (assq 'server-buffer-clients minor-mode-alist)
  264.     (setq minor-mode-alist (cons '(server-buffer-clients " Server") minor-mode-alist)))
  265.  
  266. ;; If a *server* buffer exists,
  267. ;; write STRING to it for logging purposes.
  268. (defun server-log (string)
  269.   (if (get-buffer "*server*")
  270.       (save-excursion
  271.     (set-buffer "*server*")
  272.     (goto-char (point-max))
  273.     (insert string)
  274.     (or (bobp) (newline)))))
  275.  
  276. (defun server-sentinel (proc msg)
  277.   (cond ((eq (process-status proc) 'exit)
  278.      (server-log (message "Server subprocess exited")))
  279.     ((eq (process-status proc) 'signal)
  280.      (server-log (message "Server subprocess killed")))))
  281.  
  282. (defun server-start (&optional leave-dead)
  283.   "Allow this Emacs process to be a server for client processes.
  284. This starts a server communications subprocess through which
  285. client \"editors\" can send your editing commands to this Emacs job.
  286. To use the server, set up the program `etc/emacsclient' in the
  287. Emacs distribution as your standard \"editor\".
  288.  
  289. Prefix arg means just kill any existing server communications subprocess."
  290.   (interactive "P")
  291.   ;; kill it dead!
  292.   (if server-process
  293.       (progn
  294.     (set-process-sentinel server-process nil)
  295.     (condition-case () (delete-process server-process) (error nil))))
  296.   (condition-case () (delete-file "~/.emacs_server") (error nil))
  297.   ;; If we already had a server, clear out associated status.
  298.   (while server-clients
  299.     (let ((buffer (nth 1 (car server-clients))))
  300.       (server-buffer-done buffer)))
  301.   (if leave-dead
  302.       nil
  303.     (server-log (message "Restarting server"))
  304.     (setq server-process (start-process "server" nil server-program))
  305.     (set-process-sentinel server-process 'server-sentinel)
  306.     (set-process-filter server-process 'server-process-filter)
  307.     (process-kill-without-query server-process)))
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368. ;Process a request from the server to edit some files.
  369. ;Format of STRING is "Client: CLIENTID PATH PATH PATH... \n"
  370. (defun server-process-filter (proc string)
  371.   (server-log string)
  372.   (if (not (eq 0 (string-match "Client: " string)))
  373.       nil
  374.     (setq string (substring string (match-end 0)))
  375.     (let ((client (list (substring string 0 (string-match " " string))))
  376.       (filenames nil)
  377.       (filename nil)
  378.       (lineno nil))
  379.       (setq string (substring string (match-end 0)))
  380.       (while (string-match "[^ ]+ " string)
  381.     (setq filename
  382.           (substring string (match-beginning 0) (1- (match-end 0))))
  383.     (if (string-match "/\\(\\+[0-9]+\\)" filename)
  384.         (progn
  385.           (setq lineno
  386.             (string-to-int (substring filename (match-beginning 1))))
  387.           (string-match "[^ ]* \\([^ ]+\\) " string)
  388.           (setq filename (substring string
  389.                     (match-beginning 1)
  390.                     (match-end 1))))
  391.       (setq lineno 1))
  392.     (setq filenames
  393.           (cons (list filename lineno)
  394.             filenames))
  395.     (setq string (substring string (match-end 0))))
  396.       (server-visit-files filenames client)
  397.       ;; CLIENT is now a list (CLIENTNUM BUFFERS...)
  398.       (setq server-clients (cons client server-clients))
  399.       (switch-to-buffer (nth 1 client))
  400.       (message (substitute-command-keys
  401.         "When done with a buffer, type \\[server-edit].")))))
  402.  
  403. (defun server-visit-files (filenames client)
  404.   "Finds FILES and returns the list CLIENT with the buffers nconc'd."
  405.   (let (client-record)
  406.     (while filenames
  407.       (save-excursion
  408.     ;; If there is an existing buffer that's not modified, revert it.
  409.     ;; If there is an existing buffer with deleted file, offer to write it.
  410.      (let* ((filename (car (car filenames)))
  411.            (lineno (car (cdr (car filenames))))
  412.            (obuf (get-file-buffer filename)))
  413.        (if (and obuf (set-buffer obuf))
  414.            (if (file-exists-p filename)
  415.            (if (buffer-modified-p obuf) nil
  416.             (revert-buffer t t)) ; was t nil -- msb
  417.          (if (y-or-n-p
  418.               (concat "File no longer exists: "
  419.                   filename
  420.                   ", write buffer to file? "))
  421.              (write-file filename)))
  422.         (set-buffer (find-file-noselect filename))))
  423.       (goto-line lineno)
  424.       (setq server-buffer-clients (cons (car client) server-buffer-clients))
  425.       (setq client-record (cons (current-buffer) client-record)))
  426.         (setq filenames (cdr filenames)))
  427.     (nconc client client-record)))
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488. (defun server-buffer-done (buffer)
  489.   "Mark BUFFER as \"done\" for its client(s).
  490. Buries the buffer, and returns another server buffer
  491. as a suggestion for what to select next."
  492.   (let ((running (eq (process-status server-process) 'run))
  493.     (next-buffer nil)
  494.     (old-clients server-clients))
  495.     (while old-clients
  496.       (let ((client (car old-clients)))
  497.     (or next-buffer 
  498.         (setq next-buffer (nth 1 (memq buffer client))))
  499.     (delq buffer client)
  500.     ;; If client now has no pending buffers,
  501.     ;; tell it that it is done, and forget it entirely.
  502.     (if (cdr client) nil
  503.       (if running
  504.           (progn
  505.         (send-string server-process 
  506.                  (format "Close: %s Done\n" (car client)))
  507.         (server-log (format "Close: %s Done\n" (car client)))))
  508.       (setq server-clients (delq client server-clients))))
  509.       (setq old-clients (cdr old-clients)))
  510.     (if (buffer-name buffer)
  511.     (save-excursion
  512.       (set-buffer buffer)
  513.       (setq server-buffer-clients nil)))
  514.     (bury-buffer buffer)
  515.     next-buffer))
  516.  
  517. (defun mh-draft-p (buffer)
  518.   "Return non-nil if this BUFFER is an mh <draft> file.
  519. Since MH deletes draft *BEFORE* it is edited, the server treats them specially."
  520.  ;; This may not be appropriately robust for all cases.
  521.   (string= (buffer-name buffer) "draft"))
  522.  
  523. (defun server-done ()
  524.   "Offer to save current buffer, mark it as \"done\" for clients,
  525. bury it, and return a suggested buffer to select next."
  526.   (let ((buffer (current-buffer)))
  527.     (if server-buffer-clients
  528.     (progn
  529.        (if (mh-draft-p buffer)
  530.            (progn (save-buffer)
  531.              (write-region (point-min) (point-max)
  532.                    (concat buffer-file-name "~"))
  533.              (kill-buffer buffer))
  534.         (if (and (buffer-modified-p)
  535.              (y-or-n-p (concat "Save file" buffer-file-name "? ")))
  536.         (save-buffer buffer)))
  537.       (server-buffer-done buffer)))))
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. (defun server-edit (&optional arg)
  599.   "Switch to next server editing buffer; say \"Done\" for current buffer.
  600. If a server buffer is current, it is marked \"done\" and optionally saved.
  601. MH <draft> files are always saved and backed up, no questions asked.
  602. When all of a client's buffers are marked as \"done\", the client is notified.
  603.  
  604. If invoked with a prefix argument, or if there is no server process running, 
  605. starts server process and that is all.  Invoked by \\[server-edit]."
  606.   (interactive "P")
  607.   (if (or arg
  608.       (not server-process)
  609.       (memq (process-status server-process) '(signal exit)))
  610.       (server-start nil)
  611.     (server-switch-buffer (server-done))))
  612.  
  613. (defun server-switch-buffer (next-buffer)
  614.   "Switch to another buffer, preferably one that has a client.
  615. Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it."
  616.   (if next-buffer
  617.       (if (and (bufferp next-buffer)
  618.            (buffer-name next-buffer))
  619.       (switch-to-buffer next-buffer)
  620.     ;; If NEXT-BUFFER is a dead buffer,
  621.     ;; remove the server records for it
  622.     ;; and try the next surviving server buffer.
  623.     (server-switch-buffer
  624.      (server-buffer-done next-buffer)))
  625.     (if server-clients
  626.     (server-switch-buffer (nth 1 (car server-clients)))
  627.       (switch-to-buffer (other-buffer)))))
  628.  
  629. (global-set-key "\C-x#" 'server-edit)
  630.  
  631.  
  632.