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 nil nil nil nil nil t)))
153 (list user current-prefix-arg)))
155 (riece-send-string (format "WHOIS %s %s\r\n"
156 (riece-identity-prefix user)
157 (riece-identity-prefix user)))
158 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
160 (defun riece-command-topic (topic)
163 (riece-check-channel-commands-are-usable t)
164 (list (read-from-minibuffer
165 "Set topic: " (cons (or (riece-with-server-buffer
166 (riece-identity-server
167 riece-current-channel)
168 (riece-channel-get-topic
169 (riece-identity-prefix
170 riece-current-channel)))
173 (riece-send-string (format "TOPIC %s :%s\r\n"
174 (riece-identity-prefix riece-current-channel)
177 (defun riece-command-invite (user)
179 (let ((completion-ignore-case t))
180 (riece-check-channel-commands-are-usable t)
181 (list (riece-completing-read-identity
183 (riece-get-users-on-server (riece-current-server-name))
184 nil nil nil nil nil t))))
185 (riece-send-string (format "INVITE %s %s\r\n"
186 (riece-identity-prefix user)
187 (riece-identity-prefix riece-current-channel))))
189 (defun riece-command-kick (user &optional message)
191 (let ((completion-ignore-case t))
192 (riece-check-channel-commands-are-usable t)
193 (list (completing-read
195 (riece-with-server-buffer
196 (riece-identity-server riece-current-channel)
197 (riece-channel-get-users (riece-identity-prefix
198 riece-current-channel))))
199 (if current-prefix-arg
200 (read-string "Message: ")))))
203 (format "KICK %s %s :%s\r\n"
204 (riece-identity-prefix riece-current-channel)
206 (format "KICK %s %s\r\n"
207 (riece-identity-prefix riece-current-channel)
210 (defun riece-command-names (pattern)
212 (let ((completion-ignore-case t))
213 (list (read-from-minibuffer
215 (if (and riece-current-channel
216 (riece-channel-p (riece-identity-prefix
217 riece-current-channel)))
218 (cons (riece-identity-prefix riece-current-channel)
220 (if (or (not (equal pattern ""))
221 (yes-or-no-p "Really want to query NAMES without argument? "))
222 (riece-send-string (format "NAMES %s\r\n" pattern))))
224 (defun riece-command-who (pattern)
226 (let ((completion-ignore-case t))
227 (list (read-from-minibuffer
229 (if (and riece-current-channel
230 (riece-channel-p (riece-identity-prefix
231 riece-current-channel)))
232 (cons (riece-identity-prefix riece-current-channel)
234 (if (or (not (equal pattern ""))
235 (yes-or-no-p "Really want to query WHO without argument? "))
236 (riece-send-string (format "WHO %s\r\n" pattern))))
238 (defun riece-command-list (pattern)
240 (let ((completion-ignore-case t))
241 (list (read-from-minibuffer
243 (if (and riece-current-channel
244 (riece-channel-p (riece-identity-prefix
245 riece-current-channel)))
246 (cons (riece-identity-prefix riece-current-channel)
248 (if (or (not (equal pattern ""))
249 (yes-or-no-p "Really want to query LIST without argument? "))
250 (riece-send-string (format "LIST %s\r\n" pattern))))
252 (defun riece-command-change-mode (channel change)
254 (let* ((completion-ignore-case t)
256 (if current-prefix-arg
257 (riece-completing-read-identity
258 "Change mode for channel/user: "
259 (riece-get-identities-on-server (riece-current-server-name))
260 nil nil nil nil nil t)
261 (riece-check-channel-commands-are-usable t)
262 riece-current-channel))
263 (riece-overriding-server-name (riece-identity-server channel))
264 (riece-temp-minibuffer-message
265 (concat "[Available modes: "
266 (riece-with-server-buffer (riece-identity-server channel)
267 (if (riece-channel-p (riece-identity-prefix channel))
268 (if riece-supported-channel-modes
269 (apply #'string riece-supported-channel-modes))
270 (if riece-supported-user-modes
271 (apply #'string riece-supported-user-modes))))
274 (read-from-minibuffer
275 (concat (riece-concat-channel-modes
276 channel "Mode (? for help)") ": ")
277 nil riece-minibuffer-map))))
278 (if (equal change "")
279 (riece-send-string (format "MODE %s\r\n"
280 (riece-identity-prefix channel)))
281 (riece-send-string (format "MODE %s %s\r\n"
282 (riece-identity-prefix channel)
285 (defun riece-command-set-operators (users &optional arg)
288 (riece-check-channel-commands-are-usable t)
289 (let ((completion-ignore-case t))
290 (list (riece-completing-read-multiple
291 (if current-prefix-arg
294 (riece-with-server-buffer
295 (riece-identity-server riece-current-channel)
296 (riece-channel-get-users (riece-identity-prefix
297 riece-current-channel)))
298 (if current-prefix-arg
300 (memq ?o (cdr user)))
302 (not (memq ?o (cdr user))))))
303 current-prefix-arg))))
306 (setq group (cons (car users) group)
308 (when (or (= (length group) 3)
311 (format "MODE %s %c%s %s\r\n"
312 (riece-identity-prefix riece-current-channel)
313 (if current-prefix-arg
316 (make-string (length group) ?o)
317 (mapconcat #'identity (nreverse group) " ")))
320 (defun riece-command-set-speakers (users &optional arg)
323 (riece-check-channel-commands-are-usable t)
324 (let ((completion-ignore-case t))
325 (list (riece-completing-read-multiple
326 (if current-prefix-arg
329 (riece-with-server-buffer
330 (riece-identity-server riece-current-channel)
331 (riece-channel-get-users (riece-identity-prefix
332 riece-current-channel)))
333 (if current-prefix-arg
335 (memq ?v (cdr user)))
337 (not (memq ?v (cdr user))))))
338 current-prefix-arg))))
341 (setq group (cons (car users) group)
343 (when (or (= (length group) 3)
346 (format "MODE %s %c%s %s\r\n"
347 (riece-identity-prefix riece-current-channel)
348 (if current-prefix-arg
351 (make-string (length group) ?v)
352 (mapconcat #'identity (nreverse group) " ")))
355 (defun riece-command-send-message (message notice)
356 "Send MESSAGE to the current channel."
357 (run-hooks 'riece-command-send-message-hook)
358 (if (equal message "")
359 (error "No text to send"))
360 (riece-check-channel-commands-are-usable)
364 (format "NOTICE %s :%s\r\n"
365 (riece-identity-prefix riece-current-channel)
367 (riece-display-message
368 (riece-make-message (riece-current-nickname) riece-current-channel
371 (format "PRIVMSG %s :%s\r\n"
372 (riece-identity-prefix riece-current-channel)
374 (riece-display-message
375 (riece-make-message (riece-current-nickname) riece-current-channel
378 (defun riece-command-enter-message ()
379 "Send the current line to the current channel."
381 (riece-command-send-message (buffer-substring
382 (riece-line-beginning-position)
383 (riece-line-end-position))
385 (let ((next-line-add-newlines t))
388 (defun riece-command-enter-message-as-notice ()
389 "Send the current line to the current channel as NOTICE."
391 (riece-command-send-message (buffer-substring
392 (riece-line-beginning-position)
393 (riece-line-end-position))
395 (let ((next-line-add-newlines t))
398 (defun riece-command-enter-message-to-user (user)
399 "Send the current line to USER."
401 (if (and (bolp) (eolp))
402 (error "No text to send")
403 (let ((completion-ignore-case t))
404 (list (riece-completing-read-identity
406 (riece-get-users-on-server (riece-current-server-name))
407 nil nil nil nil nil t)))))
408 (let ((text (buffer-substring
409 (riece-line-beginning-position)
410 (riece-line-end-position))))
412 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
413 (riece-display-message
414 (riece-make-message (riece-current-nickname) user text nil t)))
415 (let ((next-line-add-newlines t))
418 (defun riece-command-join-channel (target key)
419 (let ((process (riece-server-process (riece-identity-server target))))
421 (error "%s" (substitute-command-keys
422 "Type \\[riece-command-open-server] to open server.")))
423 (riece-process-send-string process
425 (format "JOIN %s :%s\r\n"
426 (riece-identity-prefix target)
428 (format "JOIN %s\r\n"
429 (riece-identity-prefix target))))))
431 (defun riece-command-join-partner (target)
432 (let ((pointer (riece-identity-member target riece-current-channels)))
434 (riece-command-switch-to-channel (car pointer))
435 (riece-join-channel target)
436 (riece-switch-to-channel target))))
438 (defun riece-command-join (target &optional key)
440 (let* ((completion-ignore-case t)
442 (if riece-join-channel-candidate
443 (let ((default (riece-format-identity
444 riece-join-channel-candidate)))
445 (riece-completing-read-identity
446 (format "Join channel/user (default %s): " default)
447 (riece-get-identities-on-server (riece-current-server-name))
448 nil nil nil nil default))
449 (riece-completing-read-identity
450 "Join channel/user: "
451 (riece-get-identities-on-server (riece-current-server-name)))))
453 (if (and current-prefix-arg
454 (riece-channel-p (riece-identity-prefix target)))
456 (riece-read-passwd (format "Key for %s: "
457 (riece-format-identity target)))))
459 (let ((pointer (riece-identity-member target riece-current-channels)))
461 (riece-command-switch-to-channel (car pointer))
462 (if (riece-channel-p (riece-identity-prefix target))
463 (riece-command-join-channel target key)
464 (riece-command-join-partner target)))))
466 (defun riece-command-part-channel (target message)
467 (let ((process (riece-server-process (riece-identity-server target))))
468 (riece-process-send-string process
470 (format "PART %s :%s\r\n"
471 (riece-identity-prefix target)
473 (format "PART %s\r\n"
474 (riece-identity-prefix target))))))
476 (defun riece-command-part (target &optional message)
479 (riece-check-channel-commands-are-usable)
480 (let* ((completion-ignore-case t)
482 (riece-completing-read-identity
483 (format "Part from channel/user (default %s): "
484 (riece-format-identity riece-current-channel))
485 riece-current-channels nil nil nil nil
486 (riece-format-identity riece-current-channel)))
488 (if (and current-prefix-arg
489 (riece-channel-p (riece-identity-prefix target)))
490 (setq message (read-string "Message: ")))
491 (list target message))))
492 (if (riece-identity-member target riece-current-channels)
493 (if (riece-channel-p (riece-identity-prefix target))
494 (riece-command-part-channel target message)
495 (riece-part-channel target))
496 (error "You are not talking with %s" target)))
498 (defun riece-command-change-nickname (nickname)
499 "Change your nickname to NICK."
500 (interactive "sEnter your nickname: ")
501 (riece-send-string (format "NICK %s\r\n" nickname)))
503 (defun riece-command-scroll-down (lines)
504 "Scroll LINES down dialogue buffer from command buffer."
506 (let ((buffer (if (and riece-channel-buffer-mode
507 riece-current-channel)
509 riece-dialogue-buffer)))
510 (if (get-buffer-window buffer)
512 (let ((other-window-scroll-buffer buffer))
513 (scroll-other-window-down lines))
515 (message "Beginning of buffer"))))))
517 (defun riece-command-scroll-up (lines)
518 "Scroll LINES up dialogue buffer from command buffer."
520 (let ((buffer (if (and riece-channel-buffer-mode
521 riece-current-channel)
523 riece-dialogue-buffer)))
524 (if (get-buffer-window buffer)
526 (let ((other-window-scroll-buffer buffer))
527 (scroll-other-window lines))
529 (message "End of buffer"))))))
531 (defun riece-command-user-list-scroll-down (lines)
532 "Scroll LINES down user list buffer from command buffer."
534 (if (get-buffer-window riece-user-list-buffer)
536 (let ((other-window-scroll-buffer riece-user-list-buffer))
537 (scroll-other-window-down lines))
539 (message "Beginning of buffer")))))
541 (defun riece-command-user-list-scroll-up (lines)
542 "Scroll LINES up user list buffer from command buffer."
544 (if (get-buffer-window riece-user-list-buffer)
546 (let ((other-window-scroll-buffer riece-user-list-buffer))
547 (scroll-other-window lines))
549 (message "End of buffer")))))
551 (defun riece-command-toggle-away (&optional message)
552 "Mark yourself as being away."
554 (if (and (not (riece-with-server-buffer (riece-identity-server
555 (riece-current-nickname))
556 (riece-user-get-away (riece-identity-prefix
557 (riece-current-nickname)))))
559 (list (read-from-minibuffer
560 "Away message: " (cons (or riece-away-message "") 0)))))
561 (if (riece-with-server-buffer (riece-identity-server
562 (riece-current-nickname))
563 (riece-user-get-away (riece-identity-prefix
564 (riece-current-nickname))))
565 (riece-send-string "AWAY\r\n")
566 (riece-send-string (format "AWAY :%s\r\n" (or message
567 riece-away-message)))))
569 (defun riece-command-toggle-freeze (&optional arg)
570 "Prevent automatic scrolling of the dialogue window.
571 If prefix argument ARG is non-nil, toggle frozen status."
573 (with-current-buffer (if (eq (derived-mode-class major-mode)
574 'riece-dialogue-mode)
576 (if (and riece-channel-buffer-mode
577 riece-channel-buffer)
579 riece-dialogue-buffer))
580 (setq riece-freeze (if arg
581 (< 0 (prefix-numeric-value arg))
583 (riece-emit-signal 'buffer-freeze-changed
584 (current-buffer) riece-freeze)))
586 (defun riece-command-toggle-own-freeze (&optional arg)
587 "Prevent automatic scrolling of the dialogue window.
588 The difference from `riece-command-freeze' is that your messages are hidden.
589 If prefix argument ARG is non-nil, toggle frozen status."
591 (with-current-buffer (if (eq (derived-mode-class major-mode)
592 'riece-dialogue-mode)
594 (if (and riece-channel-buffer-mode
595 riece-channel-buffer)
597 riece-dialogue-buffer))
599 (< 0 (prefix-numeric-value arg))
600 (not (eq riece-freeze 'own)))
601 (setq riece-freeze 'own)
602 (setq riece-freeze nil))
603 (riece-emit-signal 'buffer-freeze-changed
604 (current-buffer) riece-freeze)))
607 (autoload 'riece-exit "riece"))
608 (defun riece-command-quit (&optional arg)
611 (if (null riece-server-process-alist)
613 (message "No server process")
615 (if (y-or-n-p "Really quit IRC? ")
618 (read-string "Message: ")
620 (alist riece-server-process-alist))
622 (riece-quit-server-process (cdr (car alist)) message)
623 (setq alist (cdr alist)))))))
625 (defun riece-command-raw (command)
626 "Enter raw IRC command, which is sent to the server."
627 (interactive "sIRC command: ")
628 (riece-send-string (concat command "\r\n")))
630 (defun riece-command-beginning-of-buffer ()
631 "Scroll channel buffer to the beginning."
634 (setq buffer (if riece-channel-buffer-mode
636 riece-dialogue-buffer))
637 (or (setq window (get-buffer-window buffer))
638 (setq window (get-buffer-window riece-dialogue-buffer)
639 buffer riece-dialogue-buffer))
641 (save-selected-window
642 (select-window window)
643 (goto-char (point-min))))))
645 (defun riece-command-end-of-buffer ()
646 "Scroll channel buffer to the end."
649 (setq buffer (if riece-channel-buffer-mode
651 riece-dialogue-buffer))
652 (or (setq window (get-buffer-window buffer))
653 (setq window (get-buffer-window riece-dialogue-buffer)
654 buffer riece-dialogue-buffer))
656 (save-selected-window
657 (select-window window)
658 (goto-char (point-max))))))
660 (defun riece-command-copy-region (start end)
661 "Move current region between START and END to `kill-ring'."
663 (kill-new (buffer-substring-no-properties start end)))
665 (defun riece-command-complete-user ()
666 "Complete a user name in the current buffer."
668 (let* ((completion-ignore-case t)
669 (table (mapcar (lambda (user)
670 (list (riece-format-identity user t)))
671 (riece-get-users-on-server
672 (riece-current-server-name))))
673 (current (or (current-word) ""))
674 (completion (try-completion current table))
675 (all (all-completions current table)))
676 (if (eq completion t)
678 (if (null completion)
679 (message "Can't find completion for \"%s\"" current)
680 (if (equal current completion)
681 (with-output-to-temp-buffer "*Help*"
682 (display-completion-list all))
683 (re-search-forward "\\>" nil t)
684 (delete-region (point) (- (point) (length current)))
685 (insert completion))))))
687 (defun riece-command-open-server (server-name)
689 (list (completing-read "Open server: " riece-server-alist)))
690 (if (riece-server-process server-name)
691 (error "%s is already opened" server-name))
693 (riece-server-name-to-server server-name)
696 (defun riece-command-close-server (server-name &optional message)
698 (list (completing-read "Close server: " riece-server-process-alist)
699 (if current-prefix-arg
700 (read-string "Message: ")
701 riece-quit-message)))
702 (riece-quit-server-process (riece-server-process server-name) message))
704 (defun riece-command-universal-server-name-argument ()
706 (let* ((riece-overriding-server-name
707 (completing-read "Server: " riece-server-process-alist))
709 (key-binding (read-key-sequence
710 (format "Command to execute on \"%s\":"
711 riece-overriding-server-name)))))
713 (call-interactively command)))
715 (provide 'riece-commands)
717 ;;; riece-commands.el ends here