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 "Reconfigure windows with the current layout."
116 (riece-redisplay-buffers t))
118 (defun riece-command-suspend-resume ()
120 "Save or restore the current window configuration."
121 (let ((entry (assq 'riece-window-configuration (frame-parameters))))
122 (modify-frame-parameters (selected-frame)
123 (list (cons 'riece-window-configuration
124 (current-window-configuration))))
126 (set-window-configuration (cdr entry))
127 (delete-other-windows))
129 (substitute-command-keys
130 "\\[riece-command-suspend-resume] to get back the last windows"))))
132 (defun riece-command-change-layout (name)
133 "Select a layout-name from all current available layouts and change
134 the layout to the selected layout-name."
135 (interactive (list (completing-read "Change layout: " riece-layout-alist)))
136 (setq riece-layout name
137 riece-save-variables-are-dirty t)
138 (riece-command-configure-windows))
140 (defun riece-command-toggle-channel-buffer-mode ()
142 (setq riece-channel-buffer-mode
143 (not riece-channel-buffer-mode)
144 riece-save-variables-are-dirty t)
145 (riece-command-configure-windows))
147 (defun riece-command-toggle-user-list-buffer-mode ()
149 (setq riece-user-list-buffer-mode
150 (not riece-user-list-buffer-mode)
151 riece-save-variables-are-dirty t)
152 (riece-command-configure-windows))
154 (defun riece-command-toggle-channel-list-buffer-mode ()
156 (setq riece-channel-list-buffer-mode
157 (not riece-channel-list-buffer-mode)
158 riece-save-variables-are-dirty t)
159 (riece-command-configure-windows))
161 (defun riece-command-finger (user &optional recurse)
163 (let* ((completion-ignore-case t)
164 (user (riece-completing-read-identity
166 (riece-get-users-on-server (riece-current-server-name))
167 nil nil nil nil nil t)))
168 (list user current-prefix-arg)))
170 (riece-send-string (format "WHOIS %s %s\r\n"
171 (riece-identity-prefix user)
172 (riece-identity-prefix user)))
173 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
175 (defun riece-command-topic (topic)
178 (riece-check-channel-commands-are-usable t)
179 (list (read-from-minibuffer
180 "Set topic: " (cons (or (riece-with-server-buffer
181 (riece-identity-server
182 riece-current-channel)
183 (riece-channel-get-topic
184 (riece-identity-prefix
185 riece-current-channel)))
188 (riece-send-string (format "TOPIC %s :%s\r\n"
189 (riece-identity-prefix riece-current-channel)
192 (defun riece-command-invite (user)
194 (let ((completion-ignore-case t))
195 (riece-check-channel-commands-are-usable t)
196 (list (riece-completing-read-identity
198 (riece-get-users-on-server (riece-current-server-name))
199 nil nil nil nil nil t))))
200 (riece-send-string (format "INVITE %s %s\r\n"
201 (riece-identity-prefix user)
202 (riece-identity-prefix riece-current-channel))))
204 (defun riece-command-kick (user &optional message)
206 (let ((completion-ignore-case t))
207 (riece-check-channel-commands-are-usable t)
208 (list (completing-read
210 (riece-with-server-buffer
211 (riece-identity-server riece-current-channel)
212 (riece-channel-get-users (riece-identity-prefix
213 riece-current-channel))))
214 (if current-prefix-arg
215 (read-string "Message: ")))))
218 (format "KICK %s %s :%s\r\n"
219 (riece-identity-prefix riece-current-channel)
221 (format "KICK %s %s\r\n"
222 (riece-identity-prefix riece-current-channel)
225 (defun riece-command-names (pattern)
227 (let ((completion-ignore-case t))
228 (list (read-from-minibuffer
230 (if (and riece-current-channel
231 (riece-channel-p (riece-identity-prefix
232 riece-current-channel)))
233 (cons (riece-identity-prefix riece-current-channel)
235 (if (or (not (equal pattern ""))
236 (yes-or-no-p "Really want to query NAMES without argument? "))
237 (riece-send-string (format "NAMES %s\r\n" pattern))))
239 (defun riece-command-who (pattern)
241 (let ((completion-ignore-case t))
242 (list (read-from-minibuffer
244 (if (and riece-current-channel
245 (riece-channel-p (riece-identity-prefix
246 riece-current-channel)))
247 (cons (riece-identity-prefix riece-current-channel)
249 (if (or (not (equal pattern ""))
250 (yes-or-no-p "Really want to query WHO without argument? "))
251 (riece-send-string (format "WHO %s\r\n" pattern))))
253 (defun riece-command-list (pattern)
255 (let ((completion-ignore-case t))
256 (list (read-from-minibuffer
258 (if (and riece-current-channel
259 (riece-channel-p (riece-identity-prefix
260 riece-current-channel)))
261 (cons (riece-identity-prefix riece-current-channel)
263 (if (or (not (equal pattern ""))
264 (yes-or-no-p "Really want to query LIST without argument? "))
265 (riece-send-string (format "LIST %s\r\n" pattern))))
267 (defun riece-command-change-mode (channel change)
269 (let* ((completion-ignore-case t)
271 (if current-prefix-arg
272 (riece-completing-read-identity
273 "Change mode for channel/user: "
274 (riece-get-identities-on-server (riece-current-server-name))
275 nil nil nil nil nil t)
276 (riece-check-channel-commands-are-usable t)
277 riece-current-channel))
278 (riece-overriding-server-name (riece-identity-server channel))
279 (riece-temp-minibuffer-message
280 (concat "[Available modes: "
281 (riece-with-server-buffer (riece-identity-server channel)
282 (if (riece-channel-p (riece-identity-prefix channel))
283 (if riece-supported-channel-modes
284 (apply #'string riece-supported-channel-modes))
285 (if riece-supported-user-modes
286 (apply #'string riece-supported-user-modes))))
289 (read-from-minibuffer
290 (concat (riece-concat-channel-modes
291 channel "Mode (? for help)") ": ")
292 nil riece-minibuffer-map))))
293 (if (equal change "")
294 (riece-send-string (format "MODE %s\r\n"
295 (riece-identity-prefix channel)))
296 (riece-send-string (format "MODE %s %s\r\n"
297 (riece-identity-prefix channel)
300 (defun riece-command-set-operators (users &optional arg)
303 (riece-check-channel-commands-are-usable t)
304 (let ((completion-ignore-case t))
305 (list (riece-completing-read-multiple
306 (if current-prefix-arg
309 (riece-with-server-buffer
310 (riece-identity-server riece-current-channel)
311 (riece-channel-get-users (riece-identity-prefix
312 riece-current-channel)))
313 (if current-prefix-arg
315 (memq ?o (cdr user)))
317 (not (memq ?o (cdr user))))))
318 current-prefix-arg))))
321 (setq group (cons (car users) group)
323 (when (or (= (length group) 3)
326 (format "MODE %s %c%s %s\r\n"
327 (riece-identity-prefix riece-current-channel)
328 (if current-prefix-arg
331 (make-string (length group) ?o)
332 (mapconcat #'identity (nreverse group) " ")))
335 (defun riece-command-set-speakers (users &optional arg)
338 (riece-check-channel-commands-are-usable t)
339 (let ((completion-ignore-case t))
340 (list (riece-completing-read-multiple
341 (if current-prefix-arg
344 (riece-with-server-buffer
345 (riece-identity-server riece-current-channel)
346 (riece-channel-get-users (riece-identity-prefix
347 riece-current-channel)))
348 (if current-prefix-arg
350 (memq ?v (cdr user)))
352 (not (memq ?v (cdr user))))))
353 current-prefix-arg))))
356 (setq group (cons (car users) group)
358 (when (or (= (length group) 3)
361 (format "MODE %s %c%s %s\r\n"
362 (riece-identity-prefix riece-current-channel)
363 (if current-prefix-arg
366 (make-string (length group) ?v)
367 (mapconcat #'identity (nreverse group) " ")))
370 (defun riece-command-send-message (message notice)
371 "Send MESSAGE to the current channel."
372 (run-hooks 'riece-command-send-message-hook)
373 (if (equal message "")
374 (error "No text to send"))
375 (riece-check-channel-commands-are-usable)
379 (format "NOTICE %s :%s\r\n"
380 (riece-identity-prefix riece-current-channel)
382 (riece-display-message
383 (riece-make-message (riece-current-nickname) riece-current-channel
386 (format "PRIVMSG %s :%s\r\n"
387 (riece-identity-prefix riece-current-channel)
389 (riece-display-message
390 (riece-make-message (riece-current-nickname) riece-current-channel
393 (defun riece-command-enter-message ()
394 "Send the current line to the current channel."
396 (riece-command-send-message (buffer-substring
397 (riece-line-beginning-position)
398 (riece-line-end-position))
400 (let ((next-line-add-newlines t))
403 (defun riece-command-enter-message-as-notice ()
404 "Send the current line to the current channel as NOTICE."
406 (riece-command-send-message (buffer-substring
407 (riece-line-beginning-position)
408 (riece-line-end-position))
410 (let ((next-line-add-newlines t))
413 (defun riece-command-enter-message-to-user (user)
414 "Send the current line to USER."
416 (if (and (bolp) (eolp))
417 (error "No text to send")
418 (let ((completion-ignore-case t))
419 (list (riece-completing-read-identity
421 (riece-get-users-on-server (riece-current-server-name))
422 nil nil nil nil nil t)))))
423 (let ((text (buffer-substring
424 (riece-line-beginning-position)
425 (riece-line-end-position))))
427 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
428 (riece-display-message
429 (riece-make-message (riece-current-nickname) user text nil t)))
430 (let ((next-line-add-newlines t))
433 (defun riece-command-join-channel (target key)
434 (let ((process (riece-server-process (riece-identity-server target))))
436 (error "%s" (substitute-command-keys
437 "Type \\[riece-command-open-server] to open server.")))
438 (riece-process-send-string process
440 (format "JOIN %s :%s\r\n"
441 (riece-identity-prefix target)
443 (format "JOIN %s\r\n"
444 (riece-identity-prefix target))))))
446 (defun riece-command-join-partner (target)
447 (let ((pointer (riece-identity-member target riece-current-channels)))
449 (riece-command-switch-to-channel (car pointer))
450 (riece-join-channel target)
451 (riece-switch-to-channel target))))
453 (defun riece-command-join (target)
455 (let ((completion-ignore-case t))
457 (if riece-join-channel-candidate
458 (let ((default (riece-format-identity
459 riece-join-channel-candidate)))
460 (riece-completing-read-identity
461 (format "Join channel/user (default %s): " default)
462 (riece-get-identities-on-server (riece-current-server-name))
463 nil nil nil nil default))
464 (riece-completing-read-identity
465 "Join channel/user: "
466 (riece-get-identities-on-server (riece-current-server-name)))))))
467 (let ((pointer (riece-identity-member target riece-current-channels)))
469 (riece-command-switch-to-channel (car pointer))
470 (if (riece-channel-p (riece-identity-prefix target))
471 (riece-command-join-channel target nil)
472 (riece-command-join-partner target)))))
474 (defun riece-command-part-channel (target message)
475 (let ((process (riece-server-process (riece-identity-server target))))
476 (riece-process-send-string process
478 (format "PART %s :%s\r\n"
479 (riece-identity-prefix target)
481 (format "PART %s\r\n"
482 (riece-identity-prefix target))))))
484 (defun riece-command-part (target &optional message)
487 (riece-check-channel-commands-are-usable)
488 (let* ((completion-ignore-case t)
490 (riece-completing-read-identity
491 (format "Part from channel/user (default %s): "
492 (riece-format-identity riece-current-channel))
493 riece-current-channels nil nil nil nil
494 (riece-format-identity riece-current-channel)))
496 (if current-prefix-arg
497 (read-string "Message: ")
498 riece-part-message)))
499 (list target message))))
500 (if (riece-identity-member target riece-current-channels)
501 (if (riece-channel-p (riece-identity-prefix target))
502 (riece-command-part-channel target message)
503 (riece-part-channel target))
504 (error "You are not talking with %s" target)))
506 (defun riece-command-change-nickname (nickname)
507 "Change your nickname to NICK."
508 (interactive "sEnter your nickname: ")
509 (riece-send-string (format "NICK %s\r\n" nickname)))
511 (defun riece-command-scroll-down (lines)
512 "Scroll LINES down dialogue buffer from command buffer."
514 (let ((buffer (if (and riece-channel-buffer-mode
515 riece-current-channel)
517 riece-dialogue-buffer)))
518 (if (get-buffer-window buffer)
520 (let ((other-window-scroll-buffer buffer))
521 (scroll-other-window-down lines))
523 (message "Beginning of buffer"))))))
525 (defun riece-command-scroll-up (lines)
526 "Scroll LINES up dialogue buffer from command buffer."
528 (let ((buffer (if (and riece-channel-buffer-mode
529 riece-current-channel)
531 riece-dialogue-buffer)))
532 (if (get-buffer-window buffer)
534 (let ((other-window-scroll-buffer buffer))
535 (scroll-other-window lines))
537 (message "End of buffer"))))))
539 (defun riece-command-user-list-scroll-down (lines)
540 "Scroll LINES down user list buffer from command buffer."
542 (if (get-buffer-window riece-user-list-buffer)
544 (let ((other-window-scroll-buffer riece-user-list-buffer))
545 (scroll-other-window-down lines))
547 (message "Beginning of buffer")))))
549 (defun riece-command-user-list-scroll-up (lines)
550 "Scroll LINES up user list buffer from command buffer."
552 (if (get-buffer-window riece-user-list-buffer)
554 (let ((other-window-scroll-buffer riece-user-list-buffer))
555 (scroll-other-window lines))
557 (message "End of buffer")))))
559 (defun riece-command-toggle-away (&optional message)
560 "Mark yourself as being away."
562 (if (and (not (riece-with-server-buffer (riece-identity-server
563 (riece-current-nickname))
564 (riece-user-get-away (riece-identity-prefix
565 (riece-current-nickname)))))
567 (list (read-from-minibuffer
568 "Away message: " (cons (or riece-away-message "") 0)))))
569 (if (riece-with-server-buffer (riece-identity-server
570 (riece-current-nickname))
571 (riece-user-get-away (riece-identity-prefix
572 (riece-current-nickname))))
573 (riece-send-string "AWAY\r\n")
574 (riece-send-string (format "AWAY :%s\r\n" (or message
575 riece-away-message)))))
577 (defun riece-command-toggle-freeze (&optional arg)
578 "Prevent automatic scrolling of the dialogue window.
579 If prefix argument ARG is non-nil, toggle frozen status."
581 (with-current-buffer (if (eq (derived-mode-class major-mode)
582 'riece-dialogue-mode)
584 (if (and riece-channel-buffer-mode
585 riece-channel-buffer)
587 riece-dialogue-buffer))
588 (setq riece-freeze (if arg
589 (< 0 (prefix-numeric-value arg))
591 (riece-emit-signal 'buffer-freeze-changed
592 (current-buffer) riece-freeze)))
594 (defun riece-command-toggle-own-freeze (&optional arg)
595 "Prevent automatic scrolling of the dialogue window.
596 The difference from `riece-command-freeze' is that your messages are hidden.
597 If prefix argument ARG is non-nil, toggle frozen status."
599 (with-current-buffer (if (eq (derived-mode-class major-mode)
600 'riece-dialogue-mode)
602 (if (and riece-channel-buffer-mode
603 riece-channel-buffer)
605 riece-dialogue-buffer))
607 (< 0 (prefix-numeric-value arg))
608 (not (eq riece-freeze 'own)))
609 (setq riece-freeze 'own)
610 (setq riece-freeze nil))
611 (riece-emit-signal 'buffer-freeze-changed
612 (current-buffer) riece-freeze)))
615 (autoload 'riece-exit "riece"))
616 (defun riece-command-quit (&optional arg)
619 (if (null riece-server-process-alist)
621 (message "No server process")
623 (if (y-or-n-p "Really quit IRC? ")
626 (read-string "Message: ")
628 (alist riece-server-process-alist))
630 (riece-quit-server-process (cdr (car alist)) message)
631 (setq alist (cdr alist)))))))
633 (defun riece-command-raw (command)
634 "Enter raw IRC command, which is sent to the server."
635 (interactive "sIRC command: ")
636 (riece-send-string (concat command "\r\n")))
638 (defun riece-command-beginning-of-buffer ()
639 "Scroll channel buffer to the beginning."
642 (setq buffer (if riece-channel-buffer-mode
644 riece-dialogue-buffer))
645 (or (setq window (get-buffer-window buffer))
646 (setq window (get-buffer-window riece-dialogue-buffer)
647 buffer riece-dialogue-buffer))
649 (save-selected-window
650 (select-window window)
651 (goto-char (point-min))))))
653 (defun riece-command-end-of-buffer ()
654 "Scroll channel buffer to the end."
657 (setq buffer (if riece-channel-buffer-mode
659 riece-dialogue-buffer))
660 (or (setq window (get-buffer-window buffer))
661 (setq window (get-buffer-window riece-dialogue-buffer)
662 buffer riece-dialogue-buffer))
664 (save-selected-window
665 (select-window window)
666 (goto-char (point-max))))))
668 (defun riece-command-copy-region (start end)
669 "Move current region between START and END to `kill-ring'."
671 (kill-new (buffer-substring-no-properties start end)))
673 (defun riece-command-complete-user ()
674 "Complete a user name in the current buffer."
676 (let* ((completion-ignore-case t)
677 (table (mapcar (lambda (user)
678 (list (riece-format-identity user t)))
679 (riece-get-users-on-server
680 (riece-current-server-name))))
681 (current (or (current-word) ""))
682 (completion (try-completion current table))
683 (all (all-completions current table)))
684 (if (eq completion t)
686 (if (null completion)
687 (message "Can't find completion for \"%s\"" current)
688 (if (equal current completion)
689 (with-output-to-temp-buffer "*Help*"
690 (display-completion-list all))
691 (re-search-forward "\\>" nil t)
692 (delete-region (point) (- (point) (length current)))
693 (insert completion))))))
695 (defun riece-command-open-server (server-name)
697 (list (completing-read "Open server: " riece-server-alist)))
698 (if (riece-server-process server-name)
699 (error "%s is already opened" server-name))
701 (riece-server-name-to-server server-name)
704 (defun riece-command-close-server (server-name &optional message)
706 (list (completing-read "Close server: " riece-server-process-alist)
707 (if current-prefix-arg
708 (read-string "Message: ")
709 riece-quit-message)))
710 (riece-quit-server-process (riece-server-process server-name) message))
712 (defun riece-command-universal-server-name-argument ()
714 (let* ((riece-overriding-server-name
715 (completing-read "Server: " riece-server-process-alist))
717 (key-binding (read-key-sequence
718 (format "Command to execute on \"%s\":"
719 riece-overriding-server-name)))))
721 (call-interactively command)))
723 (provide 'riece-commands)
725 ;;; riece-commands.el ends here