erc-ring.el 5.2 KB

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