Update nodes.
[riece] / lisp / riece-yank.el
1 ;;; riece-yank.el --- enter the element in kill-ring
2 ;; Copyright (C) 2004 Masatake YAMATO
3
4 ;; Author: Masatake YAMATO <jet@gyve.org>
5 ;; Keywords: IRC, riece
6
7 ;; This program 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 2, or (at your option)
10 ;; any later version.
11
12 ;; This program 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
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23 ;;
24 ;; In riece's command buffer, you can send the top element of kill-ring
25 ;; by C-c y. 
26 ;; Don't forget do (riece-command-enable-addon 'riece-yank) to test.
27 ;;
28 ;;; Code:
29 (require 'riece-commands)
30
31 (defgroup riece-yank nil
32   "Enter the element of `kill-ring'"
33   :tag "Yank"
34   :prefix "riece-"
35   :group 'riece)
36
37 (defcustom riece-yank-tick 1
38   "Time span in second to send multiple lines."
39   :type 'number
40   :group 'riece-yank)
41
42 (defcustom riece-yank-strip-space nil
43   "If non-nil, strip common spaces in front of lines and blank lines
44 before/after the first/last non-blank line."
45   :type 'boolean
46   :group 'riece-yank)
47
48 (defvar riece-yank-enabled nil)
49
50 (defun riece-yank-insinuate ()
51   )
52
53 (defun riece-yank-enable ()
54   (define-key riece-command-mode-map "\C-cy" 'riece-command-yank)
55   (setq riece-yank-enabled t))
56 (defun riece-yank-disable ()
57   (define-key riece-command-mode-map "\C-cy" 'undefined)
58   (setq riece-yank-enabled nil))
59
60 (defun riece-yank-strip-space (string)
61   (with-temp-buffer
62     (insert string)
63     (untabify (point-min) (point-max))
64     ;; Delete blank lines before the first non-blank line.
65     (goto-char (point-min))
66     (while (looking-at " *$")
67       (delete-region (point) (progn (forward-line) (point))))
68     ;; Delete blank lines after the last non-blank line.
69     (goto-char (point-max))
70     (while (progn (beginning-of-line) (looking-at " *$"))
71       (delete-region (point) (progn (end-of-line 0) (point))))
72     ;; Delete common spaces in front of lines.
73     (let ((space-width (point-max)))
74       (while (looking-at " +")
75         (setq space-width (min space-width (length (match-string 0))))
76         (forward-line))
77       (goto-char (point-min))
78       (while (not (eobp))
79         (delete-char space-width)
80         (forward-line)))
81     (buffer-string)))
82
83 (defun riece-command-yank (arg prefix)
84   (interactive "P\nsPrefix: ")
85   (when (or (not prefix)
86             (string= prefix ""))
87     (setq prefix " "))
88   (let* ((kill (current-kill 0))
89          msg)
90     (unless kill
91       (error "Nothing to send in kill-ring"))
92     (if riece-yank-strip-space
93         (setq kill (riece-yank-strip-space kill)))
94     (setq msg (split-string kill "\n"))
95     (when (y-or-n-p (format "Send \"%s\"\n? " kill))
96       (mapcar
97        (lambda (x) 
98          (riece-command-send-message (concat prefix x) arg)
99          ;; Without next line, you will be kicked out from ircd.
100          ;; It may mean "Don't send much data at once."
101          (sit-for riece-yank-tick))
102        msg))))
103
104 (provide 'riece-yank)
105 ;;; riece-yank.el ends here