al-comint.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; al-comint.el --- Additional functionality for comint
  2. ;; Copyright © 2015-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. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'comint)
  17. ;;;###autoload
  18. (defun al/comint-previous-matching-input-from-input (arg)
  19. "Search backwards through input history for match for current input.
  20. Unlike `comint-previous-matching-input-from-input', the matching
  21. input is not forced to begin with the current input."
  22. (interactive "p")
  23. (unless (memq last-command '(al/comint-previous-matching-input-from-input
  24. al/comint-next-matching-input-from-input))
  25. ;; Starting a new search.
  26. (setq comint-matching-input-from-input-string
  27. (buffer-substring
  28. (or (marker-position comint-accum-marker)
  29. (process-mark (get-buffer-process (current-buffer))))
  30. (point))
  31. comint-input-ring-index nil))
  32. (comint-previous-matching-input
  33. (regexp-quote comint-matching-input-from-input-string)
  34. arg))
  35. ;;;###autoload
  36. (defun al/comint-next-matching-input-from-input (arg)
  37. "Search forwards through input history for match for current input."
  38. (interactive "p")
  39. (al/comint-previous-matching-input-from-input (- arg)))
  40. (defun al/comint-input-at-point ()
  41. "Return comint input from the current input (command) line.
  42. Return nil, if the current line is not the input line."
  43. (let ((beg (field-beginning)))
  44. (unless (eq (get-char-property beg 'field)
  45. 'output)
  46. (buffer-substring-no-properties beg (field-end)))))
  47. ;;;###autoload
  48. (defun al/comint-send-input-maybe ()
  49. "Call `comint-send-input' if the point is on the command line."
  50. (interactive)
  51. (let ((proc (get-buffer-process (current-buffer))))
  52. (when proc
  53. (let ((prompt (marker-position (process-mark proc))))
  54. (when (< (point) prompt)
  55. (let ((input (al/comint-input-at-point)))
  56. (if (null input)
  57. (user-error (substitute-command-keys "\
  58. You don't want to do \"\\[al/comint-send-input-maybe]\" here"))
  59. (goto-char prompt)
  60. (delete-region prompt (point-max))
  61. (insert input)))))
  62. (comint-send-input))))
  63. ;;;###autoload
  64. (defun al/comint-toggle-move-point ()
  65. "Toggle moving point to the end of comint output."
  66. (interactive)
  67. (let ((default (default-value 'comint-move-point-for-output)))
  68. (setq-local comint-move-point-for-output
  69. (if (eq default comint-move-point-for-output)
  70. (not default)
  71. default))))
  72. (provide 'al-comint)
  73. ;;; al-comint.el ends here