erc-ring.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;; erc-ring.el -- Command history handling for erc using ring.el
  2. ;; Copyright (C) 2001-2004, 2006-2017 Free Software Foundation, Inc.
  3. ;; Author: Alex Schroeder <alex@gnu.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: comm
  6. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file implements an input ring -- a history of the stuff you
  20. ;; wrote. To activate:
  21. ;;
  22. ;; (require 'erc-auto) or (require 'erc-ring)
  23. ;; (erc-ring-mode 1)
  24. ;;
  25. ;; Use M-n and M-p to navigate the ring
  26. ;;; Code:
  27. (require 'erc)
  28. (require 'comint)
  29. (require 'ring)
  30. (defgroup erc-ring nil
  31. "An input ring for ERC."
  32. :group 'erc)
  33. ;;;###autoload (autoload 'erc-ring-mode "erc-ring" nil t)
  34. (define-erc-module ring nil
  35. "Stores input in a ring so that previous commands and messages can
  36. be recalled using M-p and M-n."
  37. ((add-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
  38. (define-key erc-mode-map "\M-p" 'erc-previous-command)
  39. (define-key erc-mode-map "\M-n" 'erc-next-command))
  40. ((remove-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
  41. (define-key erc-mode-map "\M-p" 'undefined)
  42. (define-key erc-mode-map "\M-n" 'undefined)))
  43. (defvar erc-input-ring nil "Input ring for erc.")
  44. (make-variable-buffer-local 'erc-input-ring)
  45. (defvar erc-input-ring-index nil
  46. "Position in the input ring for erc.
  47. If nil, the input line is blank and the user is conceptually after
  48. the most recently added item in the ring. If an integer, the input
  49. line is non-blank and displays the item from the ring indexed by this
  50. variable.")
  51. (make-variable-buffer-local 'erc-input-ring-index)
  52. (defun erc-input-ring-setup ()
  53. "Do the setup required so that we can use comint style input rings.
  54. Call this function when setting up the mode."
  55. (unless (ring-p erc-input-ring)
  56. (setq erc-input-ring (make-ring comint-input-ring-size)))
  57. (setq erc-input-ring-index nil))
  58. (defun erc-add-to-input-ring (s)
  59. "Add string S to the input ring and reset history position."
  60. (unless erc-input-ring (erc-input-ring-setup))
  61. (ring-insert erc-input-ring s)
  62. (setq erc-input-ring-index nil))
  63. (defun erc-clear-input-ring ()
  64. "Remove all entries from the input ring, then call garbage-collect.
  65. You might use this for security purposes if you have typed a command
  66. containing a password."
  67. (interactive)
  68. (setq erc-input-ring (make-ring comint-input-ring-size)
  69. erc-input-ring-index nil)
  70. (garbage-collect)
  71. (message "ERC input ring cleared."))
  72. (defun erc-previous-command ()
  73. "Replace current command with the previous one from the history."
  74. (interactive)
  75. (unless erc-input-ring (erc-input-ring-setup))
  76. ;; if the ring isn't empty
  77. (when (> (ring-length erc-input-ring) 0)
  78. (if (and erc-input-ring-index
  79. (= (ring-length erc-input-ring) (1+ erc-input-ring-index)))
  80. (progn
  81. (erc-replace-current-command "")
  82. (setq erc-input-ring-index nil))
  83. ;; If we are not viewing old input and there's text in the input
  84. ;; area, push it on the history ring before moving back through
  85. ;; the input history, so it will be there when we return to the
  86. ;; front.
  87. (if (null erc-input-ring-index)
  88. (when (> (point-max) erc-input-marker)
  89. (erc-add-to-input-ring (buffer-substring erc-input-marker
  90. (point-max)))
  91. (setq erc-input-ring-index 0)))
  92. (setq erc-input-ring-index (if erc-input-ring-index
  93. (ring-plus1 erc-input-ring-index
  94. (ring-length erc-input-ring))
  95. 0))
  96. (erc-replace-current-command (ring-ref erc-input-ring
  97. erc-input-ring-index)))))
  98. (defun erc-next-command ()
  99. "Replace current command with the next one from the history."
  100. (interactive)
  101. (unless erc-input-ring (erc-input-ring-setup))
  102. ;; if the ring isn't empty
  103. (when (> (ring-length erc-input-ring) 0)
  104. (if (and erc-input-ring-index
  105. (= 0 erc-input-ring-index))
  106. (progn
  107. (erc-replace-current-command "")
  108. (setq erc-input-ring-index nil))
  109. (setq erc-input-ring-index (ring-minus1 (or erc-input-ring-index 0)
  110. (ring-length erc-input-ring)))
  111. (erc-replace-current-command (ring-ref erc-input-ring
  112. erc-input-ring-index)))))
  113. (defun erc-replace-current-command (s)
  114. "Replace current command with string S."
  115. ;; delete line
  116. (let ((inhibit-read-only t))
  117. (delete-region
  118. (progn (goto-char erc-insert-marker) (erc-bol))
  119. (goto-char (point-max)))
  120. (insert s)))
  121. (provide 'erc-ring)
  122. ;;; erc-ring.el ends here
  123. ;; Local Variables:
  124. ;; indent-tabs-mode: nil
  125. ;; End: