home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / resume.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  8.1 KB  |  204 lines

  1. ;From ark1!uakari.primate.wisc.edu!uwm.edu!lll-winken!bu.edu!bu-cs!nntp-read!jbw Wed Feb  7 16:16:14 1990
  2. ;Article 1081 of gnu.emacs:
  3. ;Path: ark1!uakari.primate.wisc.edu!uwm.edu!lll-winken!bu.edu!bu-cs!nntp-read!jbw
  4. ;From jbw@bucsf.bu.edu (Joe Wells)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: Re: resuming suspended Emacs
  7. ;Message-ID: <JBW.90Feb5151002@bucsf.bu.edu>
  8. ;Date: 5 Feb 90 20:10:02 GMT
  9. ;References: <9002051421.AA06553@life.ai.mit.edu>
  10. ;Sender: news@bu.edu.bu.edu
  11. ;Distribution: gnu
  12. ;Organization: Boston University Computer Science Department
  13. ;Lines: 186
  14. ;In-reply-to: DRB@MATH.AMS.COM's message of 5 Feb 90 14:21:23 GMT
  15. ;
  16. ;In article <9002051421.AA06553@life.ai.mit.edu> DRB@MATH.AMS.COM (Drew Burton) writes:
  17. ;
  18. ;       This is a novice question.  We've just brought up Emacs
  19. ;   (18.51) on a DECstation 3100 running Ultrix (BSD 4.3).  I need to 
  20. ;   know how people resume a suspended (exit with ctrl-Z) Emacs.  I assume
  21. ;   that there must be a simple script (Bourne shell if possible) that
  22. ;   allows a single command to fire up emacs if necessary or to bring
  23. ;   a suspended job to the foreground if it exists.  Or perhaps people
  24. ;   use some other tactic entirely.
  25. ;
  26. ;Well, I imagine most people just use "fg", but you may want fancier
  27. ;functionality.  I'm including a package I wrote to handle this.  The idea
  28. ;is that the first time you start emacs, command line arguments are handled
  29. ;normally.  Then, you suspend your emacs job.  When you want to edit
  30. ;something else, you type "emacs filename" as usual, but instead of
  31. ;starting a new emacs job, the old job is resumed instead, and the command
  32. ;line arguments are placed in a file where the old emacs job looks for
  33. ;them.
  34. ;
  35. ;Enjoy,
  36. ;
  37. ;-- 
  38. ;Joe Wells <jbw@bu.edu>
  39. ;jbw%bucsf.bu.edu@bu-it.bu.edu
  40. ;...!harvard!bu-cs!bucsf!jbw
  41. ;(617) 375-6893
  42. ;200-202 Bay State Rd., Box 1486, Boston, MA 02215, USA
  43. ;----------------------------------------------------------------------
  44. ;; Process command line arguments from within a suspended Emacs job
  45. ;; Copyright (C) 1988 Free Software Foundation, Inc.
  46.  
  47. ;; This file is not yet part of GNU Emacs, but soon will be.
  48.  
  49. ;; GNU Emacs is distributed in the hope that it will be useful,
  50. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  51. ;; accepts responsibility to anyone for the consequences of using it
  52. ;; or for whether it serves any particular purpose or works at all,
  53. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  54. ;; License for full details.
  55.  
  56. ;; Everyone is granted permission to copy, modify and redistribute
  57. ;; GNU Emacs, but only under the conditions described in the
  58. ;; GNU Emacs General Public License.   A copy of this license is
  59. ;; supposed to have been given to you along with GNU Emacs so you
  60. ;; can know your rights and responsibilities.  It should be in a
  61. ;; file named COPYING.  Among other things, the copyright notice
  62. ;; and this notice must be preserved on all copies.
  63.  
  64. ;; Author: Joe Wells
  65. ;; Last changed: Tue Jun 13 12:37:19 1989 by jbw ( Joe Wells #360 x2392) on ketchum
  66. ;; jbw@bucsf.bu.edu (school year)
  67. ;; joew%uswest@boulder.colorado.edu (summer)
  68.  
  69. ;; Stephan Gildea suggested bug fix (gildea@bbn.com).
  70. ;; Ideas from Michael DeCorte and other people.
  71.  
  72. ;; For csh users, insert the following alias in your .cshrc file
  73. ;; (after removing the leading double semicolons, of course):
  74. ;;
  75. ;;# The following line could be just EMACS_CMD=emacs, but this depends on
  76. ;;# your site.
  77. ;;if (! $?EMACS_CMD) set EMACS_CMD=emacs
  78. ;;set JOBS_FILE=/tmp/jobs.$USER.$$
  79. ;;set ARGS_FILE=~/.emacs_args
  80. ;;set STOP_PATT='^\[[0-9]*\] *[ +-] Stopped ............ '
  81. ;;set SUNVIEW_CMD='emacstool -nw -f emacstool-init -f server-start'
  82. ;;set X_CMD=\'\''$EMACS_CMD -i -f server-start'
  83. ;;alias emacs \
  84. ;;' \\
  85. ;;   jobs >! "$JOBS_FILE" \\
  86. ;;   && grep "$STOP_PATT$EMACS_CMD" "$JOBS_FILE" >& /dev/null \\
  87. ;;   && echo `pwd` \!* >! "$ARGS_FILE" && ""fg %$EMACS_CMD \\
  88. ;;|| if (! -e ~/.emacs_server || -f ~/.emacs_server) set status=1 \\
  89. ;;   && emacsclient \!* \\
  90. ;;|| @ status=1 - $?DISPLAY && eval "$X_CMD -i \!* &" \\
  91. ;;|| @ status=1 - $?WINDOW_PARENT && eval "$SUNVIEW_CMD \!* &" \\
  92. ;;|| ""$EMACS_CMD -nw \!* \\
  93. ;;'
  94. ;;
  95. ;; The alias works as follows:
  96. ;; 1. If there is a suspended Emacs job that is a child of the
  97. ;; current shell, place its arguments in the ~/.emacs_args file and
  98. ;; resume it.
  99. ;; 2. Else if the ~/.emacs_server socket has been created, presume an
  100. ;; Emacs server is running and attempt to connect to it.  If no Emacs
  101. ;; server is listening on the socket, this will fail.
  102. ;; 3. Else if the DISPLAY environment variable is set, presume we are
  103. ;; running under X Windows and start a new GNU Emacs process in the
  104. ;; background as an X client.
  105. ;; 4. Else if the WINDOW_PARENT environment variable is set, presume we
  106. ;; are running under SunView and start an emacstool process in the
  107. ;; background.
  108. ;; 5. Else start a regular Emacs process.
  109. ;;
  110. ;; Notes:
  111. ;; The output of the "jobs" command is not piped directly into "grep"
  112. ;; because that would run the "jobs" command in a subshell.
  113. ;; Before resuming a suspended emacs, the current directory and all
  114. ;; command line arguments are placed in a file name ~/.emacs_args.
  115. ;; The "-nw" switch to Emacs means no windowing system.
  116.  
  117. ;; Insert this in your .emacs file:
  118. ;;(setq suspend-resume-hook 'resume-process-args)
  119. ;;(setq suspend-hook 'empty-args-file)
  120. ;;(autoload 'empty-args-file "resume")
  121. ;;(autoload 'resume-process-args "resume")
  122.  
  123. ;; Finally, put the rest in a file named "resume.el" in a lisp library
  124. ;; directory.
  125.  
  126. (defvar emacs-args-file (expand-file-name "~/.emacs_args")
  127.   "*This file is where arguments are placed for a suspended emacs job.")
  128.  
  129. (defvar emacs-args-buffer " *Command Line Args*"
  130.   "Buffer that is used by resume-process-args.")
  131.  
  132. (defun resume-process-args ()
  133.   "This should be called from inside of suspend-resume-hook.  This
  134. grabs the contents of the file whose name is stored in
  135. emacs-args-file, and processes these arguments like command line
  136. options."
  137.   (let ((start-buffer (current-buffer))
  138.     (args-buffer (get-buffer-create emacs-args-buffer))
  139.     length args)
  140.     (unwind-protect
  141.     (progn
  142.       (set-buffer args-buffer)
  143.       (erase-buffer)
  144.       ;; get the contents of emacs-args-file
  145.       (condition-case ()
  146.           (let ((result (insert-file-contents emacs-args-file)))
  147.         (setq length (car (cdr result))))
  148.         ;; the file doesn't exist, ergo no arguments
  149.         (file-error
  150.           (erase-buffer)
  151.           (setq length 0)))
  152.       (if (<= length 0)
  153.           (setq args nil)
  154.         ;; get the arguments from the buffer
  155.         (goto-char (point-min))
  156.         (while (not (eobp))
  157.           (skip-chars-forward " \t\n")
  158.           (let ((begin (point)))
  159.         (skip-chars-forward "^ \t\n")
  160.         (setq args (cons (buffer-substring begin (point)) args)))
  161.           (skip-chars-forward " \t\n"))
  162.         ;; arguments are now in reverse order
  163.         (setq args (nreverse args))
  164.         ;; make sure they're not read again
  165.         (erase-buffer))        
  166.       (write-buffer-to-file (current-buffer) emacs-args-file)
  167.       ;; if nothing was in buffer, args will be null
  168.       (or (null args)
  169.           (setq default-directory (file-name-as-directory (car args))
  170.             args (cdr args)))
  171.       ;; actually process the arguments
  172.       (command-line-1 args))
  173.       ;; If the command line args don't result in a find-file, the
  174.       ;; buffer will be left in args-buffer.  So we change back to the
  175.       ;; original buffer.  The reason I don't just use
  176.       ;; (let ((default-directory foo))
  177.       ;;    (command-line-1 args))
  178.       ;; in the context of the original buffer is because let does not
  179.       ;; work properly with buffer-local variables.
  180.       (if (eq (current-buffer) args-buffer)
  181.       (set-buffer start-buffer)))))
  182.  
  183. (defun empty-args-file ()
  184.   "This empties the contents of the file whose name is specified by
  185. emacs-args-file."
  186.   (save-excursion
  187.     (set-buffer (get-buffer-create emacs-args-buffer))
  188.     (erase-buffer)
  189.     (write-buffer-to-file (current-buffer) emacs-args-file)))
  190.  
  191. (defun write-buffer-to-file (buffer file)
  192.   "Writes the contents of BUFFER into FILE, if permissions allow."
  193.   (if (not (file-writable-p file))
  194.       (error "No permission to write file %s" file))
  195.   (save-excursion
  196.     (set-buffer buffer)
  197.     (clear-visited-file-modtime)
  198.     (save-restriction
  199.       (widen)
  200.       (write-region (point-min) (point-max) file nil 'quiet))
  201.     (set-buffer-modified-p nil)))
  202.  
  203.  
  204.