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

  1. ;From ark1!nems!mimsy!haven!purdue!tut.cis.ohio-state.edu!cs.utexas.edu!uwm.edu!rpi!netserv2!deven Wed Dec  6 12:18:06 1989
  2. ;Article 778 of gnu.emacs:
  3. ;Path: ark1!nems!mimsy!haven!purdue!tut.cis.ohio-state.edu!cs.utexas.edu!uwm.edu!rpi!netserv2!deven
  4. ;From deven@rpi.edu (Deven T. Corzine)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: kill-buffer.el package (deals with running processes)
  7. ;Message-ID: <DEVEN.89Nov26155742@netserv2.rpi.edu>
  8. ;Date: 26 Nov 89 20:57:01 GMT
  9. ;References: <MRD.89Nov15011910@image.clarkson.edu>
  10. ;Distribution: gnu
  11. ;Organization: Rensselaer Polytechnic Institute, Troy, NY
  12. ;Lines: 148
  13. ;
  14. ;
  15. ;On 15 Nov 89 06:19:15 GMT, mrd@sun.soe.clarkson.edu (Michael DeCorte) said:
  16. ;
  17. ;Michael> Is there some way to get emacs to say 'hey you sure you want
  18. ;Michael> to do this?' when I try to kill a buffer with a process
  19. ;Michael> accociated with it (eg. *shell*).
  20. ;
  21. ;As it happens, I have written a kill-buffer package to deal with this
  22. ;exact problem.  It replaces the original kill-buffer (saving the subr
  23. ;definition, of course) and if there is a process running in a buffer
  24. ;which is being killed, it asks the user if to kill the process, and
  25. ;also if to save the file, (y-or-n-p's) and if buffer is still unsaved
  26. ;or has running processes, yes-or-no-p on whether to really kill it.
  27. ;
  28. ;I have been using it without problems since making a few bug fixes and
  29. ;enhancements...  been intending to post it, just prompted to by seeing
  30. ;your message...
  31. ;
  32. ;Enjoy.  (watch for .sig)
  33. ;
  34. ;Deven
  35.  
  36. ;From ark1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!netserv2!deven Wed Dec  6 12:20:14 1989
  37. ;Article 823 of gnu.emacs:
  38. ;Path: ark1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!netserv2!deven
  39. ;From deven@rpi.edu (Deven T. Corzine)
  40. ;Newsgroups: gnu.emacs
  41. ;Subject: kill-buffer.el update
  42. ;Message-ID: <DEVEN.89Dec5194359@daniel.rpi.edu>
  43. ;Date: 6 Dec 89 00:43:25 GMT
  44. ;Distribution: gnu
  45. ;Organization: Rensselaer Polytechnic Institute, Troy, NY
  46. ;Lines: 160
  47. ;
  48. ;
  49. ;Okay, so maybe it wasn't completely debugged.  This version should be
  50. ;a bit cleaner...  (watch for .sig!)
  51.  
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
  53. ;; kill-buffer.el --- enhancements to kill-buffer function
  54. ;; Author          : Deven Corzine
  55. ;; Created On      : Fri Oct 20 09:01:39 1989
  56. ;; Last Modified By: Deven Corzine
  57. ;; Last Modified On: Tue Dec  5 18:46:56 1989
  58. ;; Update Count    : 15
  59. ;; Status          : Complete, no known bugs.
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61.  
  62. ;; History         
  63. ;; 5-Dec-1989        Deven Corzine    
  64. ;;    Last Modified: Sun Nov 26 16:17:45 1989 #14 (Deven Corzine)
  65. ;;    Modified to recursively call kill-buffer instead of spacial-casing
  66. ;;      when passed t to kill current buffer.
  67. ;;    Modified back to not erase buffer, and to reset buffer-modified-p
  68. ;;      to original state when a buffer is not actually killed.
  69. ;;    Modified to correctly state whether buffer was killed or not.
  70.  
  71. ;; 26-Nov-1989        Deven Corzine    
  72. ;;    Last Modified: Thu Nov  2 11:57:50 1989 #11 (Deven Corzine)
  73. ;;    Modified to erase buffer before killing.  (so killing *scratch*
  74. ;;      when only buffer will (effectively) kill it.)
  75.  
  76. ;; 2-Nov-1989        Deven Corzine    
  77. ;;    Last Modified: Mon Oct 30 10:34:54 1989 #10 (Deven Corzine)
  78. ;;    Modified to work properly for already-killed buffers.
  79. ;;    Modified to catch errors and call original-kill-buffer instead
  80. ;;      of breaking.
  81.  
  82. ;; 30-Oct-1989        Deven Corzine    
  83. ;;    Last Modified: Mon Oct 30 09:57:42 1989 #6 (Deven Corzine)
  84. ;;    Modified to print a message when buffer is killed interactively.
  85. ;;    Fixed typo in format string when buffer needs to be saved but is
  86. ;;      not bound to a file.
  87.  
  88. ;; 20-Oct-1989        Deven Corzine    
  89. ;;    Last Modified: Fri Oct 20 10:40:17 1989 #3 (Deven Corzine)
  90. ;;    Changed wording on buffer kill confirmation for when a process
  91. ;;      remains running to make it clear the question refers to killing
  92. ;;      the buffer, not the process.
  93.  
  94. ;; 20-Oct-1989        Deven Corzine    
  95. ;;    Last Modified: Fri Oct 20 10:17:40 1989 #2 (Deven Corzine)
  96. ;;    Changed wording on process kill query to be less ambiguous.
  97.  
  98. ;; 20-Oct-1989        Deven Corzine    
  99. ;;    Last Modified: Fri Oct 20 09:19:48 1989 #1 (Deven Corzine)
  100. ;;    Modified to honor buffer-offer-save and check for abbrevs to save.
  101.  
  102. ;; 20-Oct-1989        Deven Corzine    
  103. ;;     #0
  104. ;;    Wrote new kill-buffer function to check for processes, and to ask
  105. ;;      whether to save buffer (if bound to a file and modified) and to
  106. ;;      ask whether to kill each process running in the buffer.
  107. ;;    Companion function kill-current-buffer for a convenient keybinding.
  108.  
  109. ;; save original kill-buffer primitive
  110. (or (fboundp 'original-kill-buffer)
  111.     (fset 'original-kill-buffer
  112.           (symbol-function 'kill-buffer)))
  113.  
  114. (defun kill-buffer (&optional buffer)
  115.   "One arg, a string or a buffer.  Get rid of the specified buffer.
  116.  
  117. Modified to check for processes, and offer option of killing running
  118. processes or saving buffer if changed.  If BUFFER is t, then use the
  119. current-buffer.  If nil, (as in interactively) prompt for buffer."
  120.   (interactive)
  121.   (let ((buf (if buffer
  122.                  (if (eq buffer t)
  123.                      (current-buffer)
  124.                    (get-buffer buffer))))
  125.         name mod proc unsaved)
  126.     (if (eq buffer t)
  127.         (let ((name (buffer-name buf)))
  128.           (kill-buffer buf)
  129.           (if (buffer-name buf)
  130.               (message "Buffer %s not killed." name)
  131.             (message "Buffer %s killed." name))))
  132.     (condition-case nil
  133.         (progn
  134.           (setq buf
  135.                 (or buf
  136.                     (get-buffer
  137.                      (read-buffer "Kill buffer: " (current-buffer) t))))
  138.           (if (setq name (buffer-name buf))
  139.               (progn
  140.                 (setq mod (buffer-modified-p buf))
  141.                 (set-buffer buf)
  142.                 (setq proc (get-buffer-process buf)
  143.                       unsaved (and (buffer-modified-p buf)
  144.                                    (or (buffer-file-name buf)
  145.                                        (and buffer-offer-save
  146.                                             (> (buffer-size) 0)))))
  147.                 (while (and proc
  148.                             (y-or-n-p (format
  149.                                        "Kill process %s running in buffer %s? "
  150.                                        (process-name proc) name)))
  151.                   (condition-case nil
  152.                       (kill-process proc)
  153.                     (error nil))
  154.                   (delete-process proc)
  155.                   (setq proc (get-buffer-process buf)))
  156.                 (if (and unsaved
  157.                          (y-or-n-p (if (buffer-file-name buf)
  158.                                        (format "Save file %s? " unsaved)
  159.                                      (format "Save buffer %s? "
  160.                                              name))))
  161.                     (setq unsaved (condition-case nil
  162.                                       (save-buffer buf)
  163.                                     (error t))))
  164.                 (if (and save-abbrevs abbrevs-changed
  165.                          (y-or-n-p (format "Save abbrevs in %s? "
  166.                                            abbrev-file-name)))
  167.                     (condition-case nil
  168.                         (write-abbrev-file nil)
  169.                       (error nil)))
  170.                 (if (or (not (or proc unsaved))
  171.                         (yes-or-no-p
  172.                          (format "%s%s%s%s; kill anyway? "
  173.                                  (if proc "Process(es) " "Buffer ")
  174.                                  (if proc "" name)
  175.                                  (if proc
  176.                                      (if unsaved
  177.                                          "running in modified buffer "
  178.                                        "running in buffer ")
  179.                                    " modified")
  180.                                  (if proc name ""))))
  181.                     (progn
  182.                       (set-buffer-modified-p nil)
  183.                       (original-kill-buffer buf)
  184.                       (if (buffer-name buf)
  185.                           (progn
  186.                             (set-buffer buf)
  187.                             (set-buffer-modified-p mod)))
  188.                       (if (interactive-p)
  189.                           (if (not (buffer-name buf))
  190.                               (message "Buffer %s killed." name)
  191.                             (message "Buffer %s not killed." name))))))))
  192.       (error (original-kill-buffer buffer)))))
  193.  
  194. (defun kill-current-buffer ()
  195.   "Kills the current buffer."
  196.   (interactive)
  197.   (kill-buffer t))
  198.  
  199.  
  200. ;Cheers!
  201. ;
  202. ;Deven
  203. ;-- 
  204. ;Deven T. Corzine        Internet:  deven@rpi.edu, shadow@pawl.rpi.edu
  205. ;Snail:  2151 12th St. Apt. 4, Troy, NY 12180   Phone:  (518) 274-0327
  206. ;Bitnet:  deven@rpitsmts, userfxb6@rpitsmts     UUCP:  uunet!rpi!deven
  207. ;Simple things should be simple and complex things should be possible.
  208.