1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-server)
33 (require 'riece-identity)
34 (require 'riece-message)
36 (autoload 'derived-mode-class "derived")
39 (defun riece-command-switch-to-channel (channel)
40 (interactive (list (riece-completing-read-identity
41 "Switch to channel/user: "
42 riece-current-channels nil t)))
43 (unless (equal channel riece-current-channel)
44 (riece-switch-to-channel channel)))
46 (defun riece-command-switch-to-channel-by-number (number)
48 (let ((command-name (symbol-name this-command)))
49 (if (string-match "[0-9]+$" command-name)
50 (list (string-to-number (match-string 0 command-name)))
51 (list (string-to-number (read-string "Switch to number: "))))))
52 (let ((channel (nth (1- number) riece-current-channels)))
54 (riece-command-switch-to-channel channel)
55 (error "No such number!"))))
60 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
61 (number-to-string number)))
62 'riece-command-switch-to-channel-by-number)
63 (setq number (1+ number)))))
65 (defun riece-command-next-channel ()
66 "Select the next channel."
68 (when (> (length riece-current-channels) 1)
69 (let ((pointer (cdr (riece-identity-member
71 riece-current-channels))))
74 (setq pointer (cdr pointer)))
76 (setq pointer riece-current-channels)
79 (setq pointer (cdr pointer))))
81 (riece-command-switch-to-channel (car pointer))
82 (error "No such channel!")))))
84 (defun riece-command-previous-channel ()
85 "Select the previous channel."
87 (when (> (length riece-current-channels) 1)
88 (let ((pointer (riece-identity-member
90 riece-current-channels))
91 (start riece-current-channels)
93 (while (and start (not (eq start pointer)))
95 (setq channel (car start)))
96 (setq start (cdr start)))
98 (setq start (copy-sequence riece-current-channels))
99 (setq start (delq nil start))
100 (and (> (length start) 1)
101 (setq channel (nth (1- (length start)) start))))
103 (riece-command-switch-to-channel channel)
104 (error "No such channel!")))))
106 (defun riece-command-select-command-buffer ()
107 "Select the command buffer."
109 (let ((window (get-buffer-window riece-command-buffer)))
111 (select-window window))))
113 (defun riece-command-configure-windows ()
115 (riece-redisplay-buffers t))
117 (defun riece-command-change-layout (name)
118 "Select a layout-name from all current available layouts and change
119 the layout to the selected layout-name."
120 (interactive (list (completing-read "Change layout: " riece-layout-alist)))
121 (setq riece-layout name
122 riece-save-variables-are-dirty t)
123 (riece-command-configure-windows))
125 (defun riece-command-toggle-channel-buffer-mode ()
127 (setq riece-channel-buffer-mode
128 (not riece-channel-buffer-mode)
129 riece-save-variables-are-dirty t)
130 (riece-command-configure-windows))
132 (defun riece-command-toggle-user-list-buffer-mode ()
134 (setq riece-user-list-buffer-mode
135 (not riece-user-list-buffer-mode)
136 riece-save-variables-are-dirty t)
137 (riece-command-configure-windows))
139 (defun riece-command-toggle-channel-list-buffer-mode ()
141 (setq riece-channel-list-buffer-mode
142 (not riece-channel-list-buffer-mode)
143 riece-save-variables-are-dirty t)
144 (riece-command-configure-windows))
146 (defun riece-command-finger (user &optional recurse)
148 (let* ((completion-ignore-case t)
149 (user (riece-completing-read-identity
151 (riece-get-users-on-server (riece-current-server-name)))))
152 (list user current-prefix-arg)))
154 (riece-send-string (format "WHOIS %s %s\r\n"
155 (riece-identity-prefix user)
156 (riece-identity-prefix user)))
157 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
159 (defun riece-command-topic (topic)
162 (riece-check-channel-commands-are-usable t)
163 (list (read-from-minibuffer
164 "Set topic: " (cons (or (riece-with-server-buffer
165 (riece-identity-server
166 riece-current-channel)
167 (riece-channel-get-topic
168 (riece-identity-prefix
169 riece-current-channel)))
172 (riece-send-string (format "TOPIC %s :%s\r\n"
173 (riece-identity-prefix riece-current-channel)
176 (defun riece-command-invite (user)
178 (let ((completion-ignore-case t))
179 (riece-check-channel-commands-are-usable t)
180 (list (riece-completing-read-identity
182 (riece-get-users-on-server (riece-current-server-name))))))
183 (riece-send-string (format "INVITE %s %s\r\n"
184 (riece-identity-prefix user)
185 (riece-identity-prefix riece-current-channel))))
187 (defun riece-command-kick (user &optional message)
189 (let ((completion-ignore-case t))
190 (riece-check-channel-commands-are-usable t)
191 (list (completing-read
193 (riece-with-server-buffer
194 (riece-identity-server riece-current-channel)
195 (riece-channel-get-users (riece-identity-prefix
196 riece-current-channel))))
197 (if current-prefix-arg
198 (read-string "Message: ")))))
201 (format "KICK %s %s :%s\r\n"
202 (riece-identity-prefix riece-current-channel)
204 (format "KICK %s %s\r\n"
205 (riece-identity-prefix riece-current-channel)
208 (defun riece-command-names (pattern)
210 (let ((completion-ignore-case t))
211 (list (read-from-minibuffer
213 (if (and riece-current-channel
214 (riece-channel-p (riece-identity-prefix
215 riece-current-channel)))
216 (cons (riece-identity-prefix riece-current-channel)
218 (if (or (not (equal pattern ""))
219 (yes-or-no-p "Really want to query NAMES without argument? "))
220 (riece-send-string (format "NAMES %s\r\n" pattern))))
222 (defun riece-command-who (pattern)
224 (let ((completion-ignore-case t))
225 (list (read-from-minibuffer
227 (if (and riece-current-channel
228 (riece-channel-p (riece-identity-prefix
229 riece-current-channel)))
230 (cons (riece-identity-prefix riece-current-channel)
232 (if (or (not (equal pattern ""))
233 (yes-or-no-p "Really want to query WHO without argument? "))
234 (riece-send-string (format "WHO %s\r\n" pattern))))
236 (defun riece-command-list (pattern)
238 (let ((completion-ignore-case t))
239 (list (read-from-minibuffer
241 (if (and riece-current-channel
242 (riece-channel-p (riece-identity-prefix
243 riece-current-channel)))
244 (cons (riece-identity-prefix riece-current-channel)
246 (if (or (not (equal pattern ""))
247 (yes-or-no-p "Really want to query LIST without argument? "))
248 (riece-send-string (format "LIST %s\r\n" pattern))))
250 (defun riece-command-change-mode (channel change)
252 (let* ((completion-ignore-case t)
254 (if current-prefix-arg
255 (riece-completing-read-identity
256 "Change mode for channel/user: "
257 (riece-get-identities-on-server (riece-current-server-name)))
258 (riece-check-channel-commands-are-usable t)
259 riece-current-channel))
260 (riece-overriding-server-name (riece-identity-server channel))
261 (riece-temp-minibuffer-message
262 (concat "[Available modes: "
263 (riece-with-server-buffer (riece-identity-server channel)
264 (if (riece-channel-p (riece-identity-prefix channel))
265 (if riece-supported-channel-modes
266 (apply #'string riece-supported-channel-modes))
267 (if riece-supported-user-modes
268 (apply #'string riece-supported-user-modes))))
271 (read-from-minibuffer
272 (concat (riece-concat-channel-modes
273 channel "Mode (? for help)") ": ")
274 nil riece-minibuffer-map))))
275 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
278 (defun riece-command-set-operators (users &optional arg)
281 (riece-check-channel-commands-are-usable t)
282 (let ((completion-ignore-case t))
283 (list (riece-completing-read-multiple
284 (if current-prefix-arg
287 (riece-with-server-buffer
288 (riece-identity-server riece-current-channel)
289 (riece-channel-get-users (riece-identity-prefix
290 riece-current-channel)))
291 (if current-prefix-arg
293 (memq ?o (cdr user)))
295 (not (memq ?o (cdr user))))))
296 current-prefix-arg))))
299 (setq group (cons (car users) group)
301 (when (or (= (length group) 3)
304 (format "MODE %s %c%s %s\r\n"
305 (riece-identity-prefix riece-current-channel)
306 (if current-prefix-arg
309 (make-string (length group) ?o)
310 (mapconcat #'identity (nreverse group) " ")))
313 (defun riece-command-set-speakers (users &optional arg)
316 (riece-check-channel-commands-are-usable t)
317 (let ((completion-ignore-case t))
318 (list (riece-completing-read-multiple
319 (if current-prefix-arg
322 (riece-with-server-buffer
323 (riece-identity-server riece-current-channel)
324 (riece-channel-get-users (riece-identity-prefix
325 riece-current-channel)))
326 (if current-prefix-arg
328 (memq ?v (cdr user)))
330 (not (memq ?v (cdr user))))))
331 current-prefix-arg))))
334 (setq group (cons (car users) group)
336 (when (or (= (length group) 3)
339 (format "MODE %s %c%s %s\r\n"
340 (riece-identity-prefix riece-current-channel)
341 (if current-prefix-arg
344 (make-string (length group) ?v)
345 (mapconcat #'identity (nreverse group) " ")))
348 (defun riece-command-send-message (message notice)
349 "Send MESSAGE to the current channel."
350 (if (equal message "")
351 (error "No text to send"))
352 (riece-check-channel-commands-are-usable)
356 (format "NOTICE %s :%s\r\n"
357 (riece-identity-prefix riece-current-channel)
359 (riece-display-message
360 (riece-make-message (riece-current-nickname) riece-current-channel
363 (format "PRIVMSG %s :%s\r\n"
364 (riece-identity-prefix riece-current-channel)
366 (riece-display-message
367 (riece-make-message (riece-current-nickname) riece-current-channel
370 (defun riece-command-enter-message ()
371 "Send the current line to the current channel."
373 (riece-command-send-message (buffer-substring
374 (riece-line-beginning-position)
375 (riece-line-end-position))
377 (let ((next-line-add-newlines t))
380 (defun riece-command-enter-message-as-notice ()
381 "Send the current line to the current channel as NOTICE."
383 (riece-command-send-message (buffer-substring
384 (riece-line-beginning-position)
385 (riece-line-end-position))
387 (let ((next-line-add-newlines t))
390 (defun riece-command-enter-message-to-user (user)
391 "Send the current line to USER."
393 (let ((completion-ignore-case t))
394 (list (riece-completing-read-identity
396 (riece-get-users-on-server (riece-current-server-name))))))
397 (let ((text (buffer-substring
398 (riece-line-beginning-position)
399 (riece-line-end-position))))
401 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
402 (riece-display-message
403 (riece-make-message (riece-current-nickname) user text nil t)))
404 (let ((next-line-add-newlines t))
407 (defun riece-command-join-channel (target key)
408 (let ((process (riece-server-process (riece-identity-server target))))
410 (error "%s" (substitute-command-keys
411 "Type \\[riece-command-open-server] to open server.")))
412 (riece-process-send-string process
414 (format "JOIN %s :%s\r\n"
415 (riece-identity-prefix target)
417 (format "JOIN %s\r\n"
418 (riece-identity-prefix target))))))
420 (defun riece-command-join-partner (target)
421 (let ((pointer (riece-identity-member target riece-current-channels)))
423 (riece-command-switch-to-channel (car pointer))
424 (riece-join-channel target)
425 (riece-switch-to-channel target))))
427 (defun riece-command-join (target &optional key)
429 (let* ((completion-ignore-case t)
431 (if riece-join-channel-candidate
432 (let ((default (riece-format-identity
433 riece-join-channel-candidate)))
434 (riece-completing-read-identity
435 (format "Join channel/user (default %s): " default)
436 (riece-get-identities-on-server (riece-current-server-name))
437 nil nil nil nil default))
438 (riece-completing-read-identity
439 "Join channel/user: "
440 (riece-get-identities-on-server (riece-current-server-name)))))
442 (if (and current-prefix-arg
443 (riece-channel-p (riece-identity-prefix target)))
445 (riece-read-passwd (format "Key for %s: "
446 (riece-format-identity target)))))
448 (let ((pointer (riece-identity-member target riece-current-channels)))
450 (riece-command-switch-to-channel (car pointer))
451 (if (riece-channel-p (riece-identity-prefix target))
452 (riece-command-join-channel target key)
453 (riece-command-join-partner target)))))
455 (defun riece-command-part-channel (target message)
456 (let ((process (riece-server-process (riece-identity-server target))))
457 (riece-process-send-string process
459 (format "PART %s :%s\r\n"
460 (riece-identity-prefix target)
462 (format "PART %s\r\n"
463 (riece-identity-prefix target))))))
465 (defun riece-command-part (target &optional message)
468 (riece-check-channel-commands-are-usable)
469 (let* ((completion-ignore-case t)
471 (riece-completing-read-identity
472 (format "Part from channel/user (default %s): "
473 (riece-format-identity riece-current-channel))
474 riece-current-channels nil nil nil nil
475 (riece-format-identity riece-current-channel)))
477 (if (and current-prefix-arg
478 (riece-channel-p (riece-identity-prefix target)))
479 (setq message (read-string "Message: ")))
480 (list target message))))
481 (if (riece-identity-member target riece-current-channels)
482 (if (riece-channel-p (riece-identity-prefix target))
483 (riece-command-part-channel target message)
484 (riece-part-channel target))
485 (error "You are not talking with %s" target)))
487 (defun riece-command-change-nickname (nickname)
488 "Change your nickname to NICK."
489 (interactive "sEnter your nickname: ")
490 (riece-send-string (format "NICK %s\r\n" nickname)))
492 (defun riece-command-scroll-down (lines)
493 "Scroll LINES down dialogue buffer from command buffer."
495 (let ((buffer (if (and riece-channel-buffer-mode
496 riece-current-channel)
498 riece-dialogue-buffer)))
499 (if (get-buffer-window buffer)
501 (let ((other-window-scroll-buffer buffer))
502 (scroll-other-window-down lines))
504 (message "Beginning of buffer"))))))
506 (defun riece-command-scroll-up (lines)
507 "Scroll LINES up dialogue buffer from command buffer."
509 (let ((buffer (if (and riece-channel-buffer-mode
510 riece-current-channel)
512 riece-dialogue-buffer)))
513 (if (get-buffer-window buffer)
515 (let ((other-window-scroll-buffer buffer))
516 (scroll-other-window lines))
518 (message "End of buffer"))))))
520 (defun riece-command-user-list-scroll-down (lines)
521 "Scroll LINES down user list buffer from command buffer."
523 (if (get-buffer-window riece-user-list-buffer)
525 (let ((other-window-scroll-buffer riece-user-list-buffer))
526 (scroll-other-window-down lines))
528 (message "Beginning of buffer")))))
530 (defun riece-command-user-list-scroll-up (lines)
531 "Scroll LINES up user list buffer from command buffer."
533 (if (get-buffer-window riece-user-list-buffer)
535 (let ((other-window-scroll-buffer riece-user-list-buffer))
536 (scroll-other-window lines))
538 (message "End of buffer")))))
540 (defun riece-command-toggle-away (&optional message)
541 "Mark yourself as being away."
543 (if (and (not (riece-with-server-buffer (riece-identity-server
544 (riece-current-nickname))
545 (riece-user-get-away (riece-identity-prefix
546 (riece-current-nickname)))))
548 (list (read-from-minibuffer
549 "Away message: " (cons (or riece-away-message "") 0)))))
550 (if (riece-with-server-buffer (riece-identity-server
551 (riece-current-nickname))
552 (riece-user-get-away (riece-identity-prefix
553 (riece-current-nickname))))
554 (riece-send-string "AWAY\r\n")
555 (riece-send-string (format "AWAY :%s\r\n" (or message
556 riece-away-message)))))
558 (defun riece-command-toggle-freeze (&optional arg)
559 "Prevent automatic scrolling of the dialogue window.
560 If prefix argument ARG is non-nil, toggle frozen status."
562 (with-current-buffer (if (eq (derived-mode-class major-mode)
563 'riece-dialogue-mode)
565 (if (and riece-channel-buffer-mode
566 riece-channel-buffer)
568 riece-dialogue-buffer))
569 (setq riece-freeze (if arg
570 (< 0 (prefix-numeric-value arg))
572 (riece-emit-signal 'buffer-freeze-changed
573 (current-buffer) riece-freeze)))
575 (defun riece-command-toggle-own-freeze (&optional arg)
576 "Prevent automatic scrolling of the dialogue window.
577 The difference from `riece-command-freeze' is that your messages are hidden.
578 If prefix argument ARG is non-nil, toggle frozen status."
580 (with-current-buffer (if (eq (derived-mode-class major-mode)
581 'riece-dialogue-mode)
583 (if (and riece-channel-buffer-mode
584 riece-channel-buffer)
586 riece-dialogue-buffer))
588 (< 0 (prefix-numeric-value arg))
589 (not (eq riece-freeze 'own)))
590 (setq riece-freeze 'own)
591 (setq riece-freeze nil))
592 (riece-emit-signal 'buffer-freeze-changed
593 (current-buffer) riece-freeze)))
596 (autoload 'riece-exit "riece"))
597 (defun riece-command-quit (&optional arg)
600 (if (null riece-server-process-alist)
602 (message "No server process")
604 (if (y-or-n-p "Really quit IRC? ")
607 (read-string "Message: ")
609 (alist riece-server-process-alist))
611 (riece-quit-server-process (cdr (car alist)) message)
612 (setq alist (cdr alist)))))))
614 (defun riece-command-raw (command)
615 "Enter raw IRC command, which is sent to the server."
616 (interactive "sIRC command: ")
617 (riece-send-string (concat command "\r\n")))
619 (defun riece-command-end-of-buffer ()
620 "Get end of the dialogue buffer."
623 (setq buffer (if riece-channel-buffer-mode
625 riece-dialogue-buffer))
626 (or (setq window (get-buffer-window buffer))
627 (setq window (get-buffer-window riece-dialogue-buffer)
628 buffer riece-dialogue-buffer))
630 (save-selected-window
631 (select-window window)
632 (goto-char (point-max))))))
634 (defun riece-command-copy-region (start end)
635 "Move current region between START and END to `kill-ring'."
637 (kill-new (buffer-substring-no-properties start end)))
639 (defun riece-command-complete-user ()
640 "Complete a user name in the current buffer."
642 (let* ((completion-ignore-case t)
643 (table (mapcar (lambda (user)
644 (list (riece-format-identity user t)))
645 (riece-get-users-on-server
646 (riece-current-server-name))))
647 (current (current-word))
648 (completion (try-completion current table))
649 (all (all-completions current table)))
650 (if (eq completion t)
652 (if (null completion)
653 (message "Can't find completion for \"%s\"" current)
654 (if (equal current completion)
655 (with-output-to-temp-buffer "*Help*"
656 (display-completion-list all))
657 (delete-region (point) (- (point) (length current)))
658 (insert completion))))))
660 (defun riece-command-open-server (server-name)
662 (list (completing-read "Open server: " riece-server-alist)))
663 (if (riece-server-process server-name)
664 (error "%s is already opened" server-name))
666 (riece-server-name-to-server server-name)
669 (defun riece-command-close-server (server-name &optional message)
671 (list (completing-read "Close server: " riece-server-process-alist)
672 (if current-prefix-arg
673 (read-string "Message: ")
674 riece-quit-message)))
675 (riece-quit-server-process (riece-server-process server-name) message))
677 (defun riece-command-universal-server-name-argument ()
679 (let* ((riece-overriding-server-name
680 (completing-read "Server: " riece-server-process-alist))
682 (key-binding (read-key-sequence
683 (format "Command to execute on \"%s\":"
684 riece-overriding-server-name)))))
686 (call-interactively command)))
688 (provide 'riece-commands)
690 ;;; riece-commands.el ends here