3775f50c907184ace80f07ac45c118e24c327b6e
[riece] / lisp / riece-yank.el
1 ;;; riece-yank.el --- enter the element of 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 ;; NOTE: This is an add-on module for Riece.
25
26 ;;; Code:
27 (require 'riece-commands)
28
29 (defgroup riece-yank nil
30   "Enter the element of kill-ring."
31   :tag "Yank"
32   :prefix "riece-"
33   :group 'riece)
34
35 (defcustom riece-yank-tick 1
36   "Time span in second to send multiple lines."
37   :type 'number
38   :group 'riece-yank)
39
40 (defcustom riece-yank-strip-space nil
41   "If non-nil, strip common spaces in front of lines and blank lines
42 before/after the first/last non-blank line."
43   :type 'boolean
44   :group 'riece-yank)
45
46 (defconst riece-yank-description
47   "Enter the element of kill-ring.")
48
49 (defvar riece-yank-enabled nil)
50
51 (defun riece-yank-insinuate ()
52   )
53
54 (defvar riece-command-mode-map)
55 (defun riece-yank-enable ()
56   (define-key riece-command-mode-map "\C-cy" 'riece-command-yank)
57   (setq riece-yank-enabled t))
58 (defun riece-yank-disable ()
59   (define-key riece-command-mode-map "\C-cy" 'undefined)
60   (setq riece-yank-enabled nil))
61
62 (defun riece-yank-strip-space (string)
63   (with-temp-buffer
64     (insert string)
65     (untabify (point-min) (point-max))
66     ;; Delete blank lines before the first non-blank line.
67     (goto-char (point-min))
68     (while (looking-at " *$")
69       (delete-region (point) (progn (forward-line) (point))))
70     ;; Delete blank lines after the last non-blank line.
71     (goto-char (point-max))
72     (while (progn (beginning-of-line) (looking-at " *$"))
73       (delete-region (point) (progn (end-of-line 0) (point))))
74     ;; Delete common spaces in front of lines.
75     (let ((space-width (point-max)))
76       (while (looking-at " +")
77         (setq space-width (min space-width (length (match-string 0))))
78         (forward-line))
79       (goto-char (point-min))
80       (while (not (eobp))
81         (delete-char space-width)
82         (forward-line)))
83     (buffer-string)))
84
85 (defun riece-command-yank (arg prefix)
86   (interactive "P\nsPrefix: ")
87   (when (or (not prefix)
88             (string= prefix ""))
89     (setq prefix " "))
90   (let* ((kill (current-kill 0))
91          msg)
92     (unless kill
93       (error "Nothing to send in kill-ring"))
94     (if riece-yank-strip-space
95         (setq kill (riece-yank-strip-space kill)))
96     (setq msg (split-string kill "\n"))
97     (when (y-or-n-p (format "Send \"%s\"\n? " kill))
98       (mapcar
99        (lambda (x) 
100          (riece-command-send-message (concat prefix x) arg)
101          ;; Without next line, you will be kicked out from ircd.
102          ;; It may mean "Don't send much data at once."
103          (sit-for riece-yank-tick))
104        msg))))
105
106 (provide 'riece-yank)
107 ;;; riece-yank.el ends here