al-process.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; al-process.el --- Additional functionality for working with processs
  2. ;; Copyright © 2013-2016 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (defun al/start-process (program &rest args)
  16. "Same as `start-process', but don't bother about name and buffer."
  17. (let ((process-name (concat program "_process"))
  18. (buffer-name (generate-new-buffer-name
  19. (concat program "_output"))))
  20. (apply #'start-process
  21. process-name buffer-name program args)))
  22. ;; Idea from
  23. ;; <http://stackoverflow.com/questions/11572934/how-do-i-kill-a-running-process-in-emacs>.
  24. ;;;###autoload
  25. (defun al/kill-process (process)
  26. "Kill PROCESS.
  27. See `delete-process' for the meaning of PROCESS.
  28. Interactively prompt for PROCESS name."
  29. (interactive
  30. (list (get-process (completing-read
  31. "Kill process: "
  32. (mapcar #'process-name (process-list))))))
  33. (delete-process process))
  34. (defun al/process-is-program (args name)
  35. "Return non-nil, if process defined by ARGS has program NAME."
  36. (let ((prog (car args)))
  37. (when prog
  38. (or (string= prog name)
  39. (and (string-match-p "sh\\'" prog) ; if it is bash/sh/...
  40. (string= (cl-second args) "-c")
  41. (string-match-p (regexp-quote name)
  42. (cl-third args)))))))
  43. ;; Hooks for starting/calling processes
  44. (defvar al/before-process-functions '(al/process-message)
  45. "Functions to be called before Emacs starts an external process.
  46. Each function is called by applying to ARGS. The first element
  47. of ARGS is a program name of the process, and the rest are
  48. program arguments.")
  49. (defun al/process-message (&rest args)
  50. "Display message about ARGS."
  51. (message "Process to run: %S" args))
  52. (defun al/run-before-process-hook (program args)
  53. "Run `al/before-process-functions' using PROGRAM and ARGS."
  54. (apply #'run-hook-with-args
  55. 'al/before-process-functions program args))
  56. (defun al/run-before-call-process-hook
  57. (program &optional infile destination display &rest args)
  58. "Run `al/before-process-functions' using PROGRAM and ARGS."
  59. (al/run-before-process-hook program args))
  60. (defun al/run-before-start-process-hook
  61. (name buffer program &rest args)
  62. "Run `al/before-process-functions' using PROGRAM and ARGS."
  63. (al/run-before-process-hook program args))
  64. ;;;###autoload
  65. (defun al/enable-process-hooks ()
  66. "Make `al/before-process-functions' active."
  67. (interactive)
  68. (advice-add 'call-process :before #'al/run-before-call-process-hook)
  69. (advice-add 'start-process :before #'al/run-before-start-process-hook))
  70. ;;;###autoload
  71. (defun al/disable-process-hooks ()
  72. "Make `al/before-process-functions' inactive."
  73. (interactive)
  74. (advice-remove 'call-process #'al/run-before-call-process-hook)
  75. (advice-remove 'start-process #'al/run-before-start-process-hook))
  76. (provide 'al-process)
  77. ;;; al-process.el ends here