vmsproc.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;; Run asynchronous VMS subprocesses under Emacs
  2. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. ;; Written by Mukesh Prasad.
  18. (defvar display-subprocess-window nil
  19. "If non-nil, the suprocess window is displayed whenever input is received.")
  20. (defvar command-prefix-string "$ "
  21. "String to insert to distinguish commands entered by user.")
  22. (defvar subprocess-running nil)
  23. (defvar command-mode-map nil)
  24. (if command-mode-map
  25. nil
  26. (setq command-mode-map (make-sparse-keymap))
  27. (define-key command-mode-map "\C-m" 'command-send-input)
  28. (define-key command-mode-map "\C-u" 'command-kill-line))
  29. (defun subprocess-input (name str)
  30. "Handles input from a subprocess. Called by Emacs."
  31. (if display-subprocess-window
  32. (display-buffer subprocess-buf))
  33. (let ((old-buffer (current-buffer)))
  34. (set-buffer subprocess-buf)
  35. (goto-char (point-max))
  36. (insert str)
  37. (insert ?\n)
  38. (set-buffer old-buffer)))
  39. (defun subprocess-exit (name)
  40. "Called by Emacs upon subprocess exit."
  41. (setq subprocess-running nil))
  42. (defun start-subprocess ()
  43. "Spawns an asynchronous subprocess with output redirected to
  44. the buffer *COMMAND*. Within this buffer, use C-m to send
  45. the last line to the subprocess or to bring another line to
  46. the end."
  47. (if subprocess-running
  48. (return t))
  49. (setq subprocess-buf (get-buffer-create "*COMMAND*"))
  50. (save-excursion
  51. (set-buffer subprocess-buf)
  52. (use-local-map command-mode-map))
  53. (setq subprocess-running (spawn-subprocess 1 'subprocess-input
  54. 'subprocess-exit))
  55. ;; Initialize subprocess so it doesn't panic and die upon
  56. ;; encountering the first error.
  57. (and subprocess-running
  58. (send-command-to-subprocess 1 "ON SEVERE_ERROR THEN CONTINUE")))
  59. (defun subprocess-command ()
  60. "Starts asynchronous subprocess if not running and switches to its window."
  61. (interactive)
  62. (if (not subprocess-running)
  63. (start-subprocess))
  64. (and subprocess-running
  65. (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
  66. (defun command-send-input ()
  67. "If at last line of buffer, sends the current line to
  68. the spawned subprocess. Otherwise brings back current
  69. line to the last line for resubmission."
  70. (interactive)
  71. (beginning-of-line)
  72. (let ((current-line (buffer-substring (point)
  73. (progn (end-of-line) (point)))))
  74. (if (eobp)
  75. (progn
  76. (if (not subprocess-running)
  77. (start-subprocess))
  78. (if subprocess-running
  79. (progn
  80. (beginning-of-line)
  81. (send-command-to-subprocess 1 current-line)
  82. (if command-prefix-string
  83. (progn (beginning-of-line) (insert command-prefix-string)))
  84. (next-line 1))))
  85. ;; else -- if not at last line in buffer
  86. (end-of-buffer)
  87. (backward-char)
  88. (next-line 1)
  89. (if (string-equal command-prefix-string
  90. (substring current-line 0 (length command-prefix-string)))
  91. (insert (substring current-line (length command-prefix-string)))
  92. (insert current-line)))))
  93. (defun command-kill-line()
  94. "Kills the current line. Used in command mode."
  95. (interactive)
  96. (beginning-of-line)
  97. (kill-line))
  98. (define-key esc-map "$" 'subprocess-command)