* riece.el (riece-command-mode): Don't display
[riece] / lisp / riece-commands.el
1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-server)
32 (require 'riece-misc)
33 (require 'riece-identity)
34 (require 'riece-message)
35
36 (autoload 'derived-mode-class "derived")
37
38 ;;; Channel movement:
39 (defun riece-command-switch-to-channel (channel)
40   (interactive (list (riece-completing-read-identity
41                       "Channel/User: " riece-current-channels nil t)))
42   (unless (equal channel riece-current-channel)
43     (riece-switch-to-channel channel)))
44
45 (defun riece-command-switch-to-channel-by-number (number)
46   (interactive
47    (let ((command-name (symbol-name this-command)))
48      (if (string-match "[0-9]+$" command-name)
49          (list (string-to-number (match-string 0 command-name)))
50        (list (string-to-number (read-string "Number: "))))))
51   (let ((channel (nth (1- number) riece-current-channels)))
52     (if channel
53         (riece-command-switch-to-channel channel)
54       (error "No such number!"))))
55
56 (eval-and-compile
57   (let ((number 1))
58     (while (<= number 20)
59       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
60                                 (number-to-string number)))
61         'riece-command-switch-to-channel-by-number)
62       (setq number (1+ number)))))
63
64 (defun riece-command-next-channel ()
65   "Select the next channel."
66   (interactive)
67   (when (> (length riece-current-channels) 1)
68     (let ((pointer (cdr (riece-identity-member
69                          riece-current-channel
70                          riece-current-channels))))
71       (while (and pointer
72                   (null (car pointer)))
73         (setq pointer (cdr pointer)))
74       (when (null pointer)
75         (setq pointer riece-current-channels)
76         (while (and pointer
77                     (null (car pointer)))
78           (setq pointer (cdr pointer))))
79       (if (car pointer)
80           (riece-command-switch-to-channel (car pointer))
81         (error "No such channel!")))))
82
83 (defun riece-command-previous-channel ()
84   "Select the previous channel."
85   (interactive)
86   (when (> (length riece-current-channels) 1)
87     (let ((pointer (riece-identity-member
88                     riece-current-channel
89                     riece-current-channels))
90           (start riece-current-channels)
91           channel)
92       (while (and start (not (eq start pointer)))
93         (if (car start)
94             (setq channel (car start)))
95         (setq start (cdr start)))
96       (when (null channel)
97         (setq start (copy-sequence riece-current-channels))
98         (setq start (delq nil start))
99         (and (> (length start) 1)
100              (setq channel (nth (1- (length start)) start))))
101       (if channel
102           (riece-command-switch-to-channel channel)
103         (error "No such channel!")))))
104
105 (defun riece-command-select-command-buffer ()
106   "Select the command buffer."
107   (interactive)
108   (let ((window (get-buffer-window riece-command-buffer)))
109     (if window
110         (select-window window))))
111
112 (defun riece-command-configure-windows ()
113   (interactive)
114   (riece-redisplay-buffers t))
115
116 (defun riece-command-change-layout (name)
117   "Select a layout-name from all current available layouts and change
118 the layout to the selected layout-name."
119   (interactive (list (completing-read "Layout: " riece-layout-alist)))
120   (setq riece-layout name
121         riece-save-variables-are-dirty t)
122   (riece-command-configure-windows))
123
124 (defun riece-command-toggle-channel-buffer-mode ()
125   (interactive)
126   (setq riece-channel-buffer-mode
127         (not riece-channel-buffer-mode)
128         riece-save-variables-are-dirty t)
129   (riece-command-configure-windows))
130
131 (defun riece-command-toggle-user-list-buffer-mode ()
132   (interactive)
133   (setq riece-user-list-buffer-mode
134         (not riece-user-list-buffer-mode)
135         riece-save-variables-are-dirty t)
136   (riece-command-configure-windows))
137
138 (defun riece-command-toggle-channel-list-buffer-mode ()
139   (interactive)
140   (setq riece-channel-list-buffer-mode
141         (not riece-channel-list-buffer-mode)
142         riece-save-variables-are-dirty t)
143   (riece-command-configure-windows))
144
145 (defun riece-command-finger (user &optional recurse)
146   (interactive
147    (let* ((completion-ignore-case t)
148           (user (riece-completing-read-identity
149                  "User: "
150                  (riece-get-users-on-server (riece-current-server-name)))))
151      (list user current-prefix-arg)))
152   (if recurse
153       (riece-send-string (format "WHOIS %s %s\r\n"
154                                  (riece-identity-prefix user)
155                                  (riece-identity-prefix user)))
156     (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
157
158 (defun riece-command-topic (topic)
159   (interactive
160    (progn
161      (riece-check-channel-commands-are-usable t)
162      (list (read-from-minibuffer
163             "Topic: " (cons (or (riece-with-server-buffer
164                                     (riece-identity-server
165                                      riece-current-channel)
166                                   (riece-channel-get-topic
167                                    (riece-identity-prefix
168                                     riece-current-channel)))
169                                 "")
170                             0)))))
171   (riece-send-string (format "TOPIC %s :%s\r\n"
172                              (riece-identity-prefix riece-current-channel)
173                              topic)))
174
175 (defun riece-command-invite (user)
176   (interactive
177    (let ((completion-ignore-case t))
178      (riece-check-channel-commands-are-usable t)
179      (list (riece-completing-read-identity
180             "User: "
181             (riece-get-users-on-server (riece-current-server-name))))))
182   (riece-send-string (format "INVITE %s %s\r\n"
183                              (riece-identity-prefix user)
184                              (riece-identity-prefix riece-current-channel))))
185
186 (defun riece-command-kick (user &optional message)
187   (interactive
188    (let ((completion-ignore-case t))
189      (riece-check-channel-commands-are-usable t)
190      (list (completing-read
191             "User: "
192             (riece-with-server-buffer
193                 (riece-identity-server riece-current-channel)
194               (riece-channel-get-users (riece-identity-prefix
195                                         riece-current-channel))))
196            (if current-prefix-arg
197                (read-string "Message: ")))))
198   (riece-send-string
199    (if message
200        (format "KICK %s %s :%s\r\n"
201                (riece-identity-prefix riece-current-channel)
202                user message)
203      (format "KICK %s %s\r\n"
204              (riece-identity-prefix riece-current-channel)
205              user))))
206
207 (defun riece-command-names (pattern)
208   (interactive
209    (let ((completion-ignore-case t))
210      (list (read-from-minibuffer
211             "Pattern: "
212             (if (and riece-current-channel
213                      (riece-channel-p (riece-identity-prefix
214                                        riece-current-channel)))
215                 (cons (riece-identity-prefix riece-current-channel)
216                       0))))))
217   (if (or (not (equal pattern ""))
218           (yes-or-no-p "Really want to query NAMES without argument? "))
219       (riece-send-string (format "NAMES %s\r\n" pattern))))
220
221 (defun riece-command-who (pattern)
222   (interactive
223    (let ((completion-ignore-case t))
224      (list (read-from-minibuffer
225             "Pattern: "
226             (if (and riece-current-channel
227                      (riece-channel-p (riece-identity-prefix
228                                        riece-current-channel)))
229                 (cons (riece-identity-prefix riece-current-channel)
230                       0))))))
231   (if (or (not (equal pattern ""))
232           (yes-or-no-p "Really want to query WHO without argument? "))
233       (riece-send-string (format "WHO %s\r\n" pattern))))
234
235 (defun riece-command-list (pattern)
236   (interactive
237    (let ((completion-ignore-case t))
238      (list (read-from-minibuffer
239             "Pattern: "
240             (if (and riece-current-channel
241                      (riece-channel-p (riece-identity-prefix
242                                        riece-current-channel)))
243                 (cons (riece-identity-prefix riece-current-channel)
244                       0))))))
245   (if (or (not (equal pattern ""))
246           (yes-or-no-p "Really want to query LIST without argument? "))
247       (riece-send-string (format "LIST %s\r\n" pattern))))
248
249 (defun riece-command-change-mode (channel change)
250   (interactive
251    (let* ((completion-ignore-case t)
252           (channel
253            (if current-prefix-arg
254                (riece-completing-read-identity
255                 "Channel/User: "
256                 (riece-get-identities-on-server (riece-current-server-name)))
257              (riece-check-channel-commands-are-usable t)
258              riece-current-channel))
259           (riece-overriding-server-name (riece-identity-server channel))
260           (riece-temp-minibuffer-message
261            (concat "[Available modes: "
262                    (riece-with-server-buffer (riece-identity-server channel)
263                      (if (riece-channel-p (riece-identity-prefix channel))
264                          (if riece-supported-channel-modes
265                              (apply #'string riece-supported-channel-modes))
266                        (if riece-supported-user-modes
267                            (apply #'string riece-supported-user-modes))))
268                    "]")))
269      (list channel
270            (read-from-minibuffer
271             (concat (riece-concat-channel-modes
272                      channel "Mode (? for help)") ": ")
273             nil riece-minibuffer-map))))
274   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
275                              change)))
276
277 (defun riece-command-set-operators (users &optional arg)
278   (interactive
279    (progn
280      (riece-check-channel-commands-are-usable t)
281      (let ((completion-ignore-case t))
282        (list (riece-completing-read-multiple
283               "Users"
284               (riece-with-server-buffer
285                   (riece-identity-server riece-current-channel)
286                 (riece-channel-get-users (riece-identity-prefix
287                                          riece-current-channel)))
288               (if current-prefix-arg
289                   (lambda (user)
290                     (memq ?o (cdr user)))
291                 (lambda (user)
292                   (not (memq ?o (cdr user))))))
293              current-prefix-arg))))
294   (let (group)
295     (while users
296       (setq group (cons (car users) group)
297             users (cdr users))
298       (when (or (= (length group) 3)
299                 (null users))
300         (riece-send-string
301          (format "MODE %s %c%s %s\r\n"
302                  (riece-identity-prefix riece-current-channel)
303                  (if current-prefix-arg
304                      ?-
305                    ?+)
306                  (make-string (length group) ?o)
307                  (mapconcat #'identity (nreverse group) " ")))
308         (setq group nil)))))
309
310 (defun riece-command-set-speakers (users &optional arg)
311   (interactive
312    (progn
313      (riece-check-channel-commands-are-usable t)
314      (let ((completion-ignore-case t))
315        (list (riece-completing-read-multiple
316               "Users"
317               (riece-with-server-buffer
318                   (riece-identity-server riece-current-channel)
319                 (riece-channel-get-users (riece-identity-prefix
320                                           riece-current-channel)))
321               (if current-prefix-arg
322                   (lambda (user)
323                     (memq ?v (cdr user)))
324                 (lambda (user)
325                   (not (memq ?v (cdr user))))))
326              current-prefix-arg))))
327   (let (group)
328     (while users
329       (setq group (cons (car users) group)
330             users (cdr users))
331       (when (or (= (length group) 3)
332                 (null users))
333         (riece-send-string
334          (format "MODE %s %c%s %s\r\n"
335                  (riece-identity-prefix riece-current-channel)
336                  (if current-prefix-arg
337                      ?-
338                    ?+)
339                  (make-string (length group) ?v)
340                  (mapconcat #'identity (nreverse group) " ")))
341         (setq group nil)))))
342
343 (defun riece-command-send-message (message notice)
344   "Send MESSAGE to the current channel."
345   (if (equal message "")
346       (error "No text to send"))
347   (riece-check-channel-commands-are-usable)
348   (if notice
349       (progn
350         (riece-send-string
351          (format "NOTICE %s :%s\r\n"
352                  (riece-identity-prefix riece-current-channel)
353                  message))
354         (riece-display-message
355          (riece-make-message (riece-current-nickname) riece-current-channel
356                              message 'notice t)))
357     (riece-send-string
358      (format "PRIVMSG %s :%s\r\n"
359              (riece-identity-prefix riece-current-channel)
360              message))
361     (riece-display-message
362      (riece-make-message (riece-current-nickname) riece-current-channel
363                          message nil t))))
364
365 (defun riece-command-enter-message ()
366   "Send the current line to the current channel."
367   (interactive)
368   (riece-command-send-message (buffer-substring
369                                (riece-line-beginning-position)
370                                (riece-line-end-position))
371                               nil)
372   (let ((next-line-add-newlines t))
373     (next-line 1)))
374
375 (defun riece-command-enter-message-as-notice ()
376   "Send the current line to the current channel as NOTICE."
377   (interactive)
378   (riece-command-send-message (buffer-substring
379                                (riece-line-beginning-position)
380                                (riece-line-end-position))
381                               t)
382   (let ((next-line-add-newlines t))
383     (next-line 1)))
384
385 (defun riece-command-enter-message-to-user (user)
386   "Send the current line to USER."
387   (interactive
388    (let ((completion-ignore-case t))
389      (list (riece-completing-read-identity
390             "User: "
391             (riece-get-users-on-server (riece-current-server-name))))))
392   (let ((text (buffer-substring
393                (riece-line-beginning-position)
394                (riece-line-end-position))))
395     (riece-send-string
396      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
397     (riece-display-message
398      (riece-make-message (riece-current-nickname) user text nil t)))
399   (let ((next-line-add-newlines t))
400     (next-line 1)))
401
402 (defun riece-command-join-channel (target key)
403   (let ((process (riece-server-process (riece-identity-server target))))
404     (unless process
405       (error "%s" (substitute-command-keys
406                    "Type \\[riece-command-open-server] to open server.")))
407     (riece-process-send-string process
408                                (if key
409                                    (format "JOIN %s :%s\r\n"
410                                            (riece-identity-prefix target)
411                                            key)
412                                  (format "JOIN %s\r\n"
413                                          (riece-identity-prefix target))))))
414
415 (defun riece-command-join-partner (target)
416   (let ((pointer (riece-identity-member target riece-current-channels)))
417     (if pointer
418         (riece-command-switch-to-channel (car pointer))
419       (riece-join-channel target)
420       (riece-switch-to-channel target))))
421
422 (defun riece-command-join (target &optional key)
423   (interactive
424    (let* ((completion-ignore-case t)
425           (target
426            (if riece-join-channel-candidate
427                (let ((default (riece-format-identity
428                                riece-join-channel-candidate)))
429                  (riece-completing-read-identity
430                   (format "Channel/User (default %s): " default)
431                   (riece-get-identities-on-server (riece-current-server-name))
432                   nil nil nil nil default))
433              (riece-completing-read-identity
434               "Channel/User: "
435               (riece-get-identities-on-server (riece-current-server-name)))))
436           key)
437      (if (and current-prefix-arg
438               (riece-channel-p (riece-identity-prefix target)))
439          (setq key
440                (riece-read-passwd (format "Key for %s: "
441                                           (riece-format-identity target)))))
442      (list target key)))
443   (let ((pointer (riece-identity-member target riece-current-channels)))
444     (if pointer
445         (riece-command-switch-to-channel (car pointer))
446       (if (riece-channel-p (riece-identity-prefix target))
447           (riece-command-join-channel target key)
448         (riece-command-join-partner target)))))
449
450 (defun riece-command-part-channel (target message)
451   (let ((process (riece-server-process (riece-identity-server target))))
452     (riece-process-send-string process
453                                (if message
454                                    (format "PART %s :%s\r\n"
455                                            (riece-identity-prefix target)
456                                            message)
457                                  (format "PART %s\r\n"
458                                          (riece-identity-prefix target))))))
459
460 (defun riece-command-part (target &optional message)
461   (interactive
462    (progn
463      (riece-check-channel-commands-are-usable)
464      (let* ((completion-ignore-case t)
465             (target
466              (riece-completing-read-identity
467               (format "Channel/User (default %s): "
468                       (riece-format-identity riece-current-channel))
469               riece-current-channels nil nil nil nil
470               (riece-format-identity riece-current-channel)))
471             message)
472        (if (and current-prefix-arg
473                 (riece-channel-p (riece-identity-prefix target)))
474            (setq message (read-string "Message: ")))
475        (list target message))))
476   (if (riece-identity-member target riece-current-channels)
477       (if (riece-channel-p (riece-identity-prefix target))
478           (riece-command-part-channel target message)
479         (riece-part-channel target))
480     (error "You are not talking with %s" target)))
481
482 (defun riece-command-change-nickname (nickname)
483   "Change your nickname to NICK."
484   (interactive "sEnter your nickname: ")
485   (riece-send-string (format "NICK %s\r\n" nickname)))
486
487 (defun riece-command-scroll-down (lines)
488   "Scroll LINES down dialogue buffer from command buffer."
489   (interactive "P")
490   (let ((buffer (if (and riece-channel-buffer-mode
491                          riece-current-channel)
492                     riece-channel-buffer
493                   riece-dialogue-buffer)))
494     (if (get-buffer-window buffer)
495         (condition-case nil
496             (let ((other-window-scroll-buffer buffer))
497               (scroll-other-window-down lines))
498           (beginning-of-buffer
499            (message "Beginning of buffer"))))))
500
501 (defun riece-command-scroll-up (lines)
502   "Scroll LINES up dialogue buffer from command buffer."
503   (interactive "P")
504   (let ((buffer (if (and riece-channel-buffer-mode
505                          riece-current-channel)
506                     riece-channel-buffer
507                   riece-dialogue-buffer)))
508     (if (get-buffer-window buffer)
509         (condition-case nil
510             (let ((other-window-scroll-buffer buffer))
511               (scroll-other-window lines))
512           (end-of-buffer
513            (message "End of buffer"))))))
514
515 (defun riece-command-user-list-scroll-down (lines)
516   "Scroll LINES down user list buffer from command buffer."
517   (interactive "P")
518   (if (get-buffer-window riece-user-list-buffer)
519       (condition-case nil
520           (let ((other-window-scroll-buffer riece-user-list-buffer))
521             (scroll-other-window-down lines))
522         (beginning-of-buffer
523          (message "Beginning of buffer")))))
524
525 (defun riece-command-user-list-scroll-up (lines)
526   "Scroll LINES up user list buffer from command buffer."
527   (interactive "P")
528   (if (get-buffer-window riece-user-list-buffer)
529       (condition-case nil
530           (let ((other-window-scroll-buffer riece-user-list-buffer))
531             (scroll-other-window lines))
532         (end-of-buffer
533          (message "End of buffer")))))
534
535 (defun riece-command-toggle-away (&optional message)
536   "Mark yourself as being away."
537   (interactive
538    (if (and (not (riece-with-server-buffer (riece-identity-server
539                                             (riece-current-nickname))
540                    (riece-user-get-away (riece-identity-prefix
541                                          (riece-current-nickname)))))
542             current-prefix-arg)
543        (list (read-from-minibuffer
544               "Away message: " (cons (or riece-away-message "") 0)))))
545   (if (riece-with-server-buffer (riece-identity-server
546                                  (riece-current-nickname))
547         (riece-user-get-away (riece-identity-prefix
548                               (riece-current-nickname))))
549       (riece-send-string "AWAY\r\n")
550     (riece-send-string (format "AWAY :%s\r\n" (or message
551                                                   riece-away-message)))))
552
553 (defun riece-command-toggle-freeze (&optional arg)
554   "Prevent automatic scrolling of the dialogue window.
555 If prefix argument ARG is non-nil, toggle frozen status."
556   (interactive "P")
557   (with-current-buffer (if (eq (derived-mode-class major-mode)
558                                'riece-dialogue-mode)
559                            (current-buffer)
560                          (if (and riece-channel-buffer-mode
561                                   riece-channel-buffer)
562                              riece-channel-buffer
563                            riece-dialogue-buffer))
564     (setq riece-freeze (if arg
565                            (< 0 (prefix-numeric-value arg))
566                          (not riece-freeze)))
567     (riece-emit-signal 'buffer-freeze-changed
568                        (current-buffer) riece-freeze)))
569
570 (defun riece-command-toggle-own-freeze (&optional arg)
571   "Prevent automatic scrolling of the dialogue window.
572 The difference from `riece-command-freeze' is that your messages are hidden.
573 If prefix argument ARG is non-nil, toggle frozen status."
574   (interactive "P")
575   (with-current-buffer (if (eq (derived-mode-class major-mode)
576                                'riece-dialogue-mode)
577                            (current-buffer)
578                          (if (and riece-channel-buffer-mode
579                                   riece-channel-buffer)
580                              riece-channel-buffer
581                            riece-dialogue-buffer))
582     (if (if arg
583             (< 0 (prefix-numeric-value arg))
584           (not (eq riece-freeze 'own)))
585         (setq riece-freeze 'own)
586       (setq riece-freeze nil))
587     (riece-emit-signal 'buffer-freeze-changed
588                        (current-buffer) riece-freeze)))
589
590 (eval-when-compile
591   (autoload 'riece-exit "riece"))
592 (defun riece-command-quit (&optional arg)
593   "Quit IRC."
594   (interactive "P")
595   (if (y-or-n-p "Really quit IRC? ")
596       (if riece-server-process-alist
597           (let ((message
598                  (if arg
599                      (read-string "Message: ")
600                    riece-quit-message))
601                 (alist riece-server-process-alist))
602             (while alist
603               (riece-quit-server-process (cdr (car alist)) message)
604               (setq alist (cdr alist))))
605         ;; If no server process is available, exit immediately.
606         (riece-exit))))
607
608 (defun riece-command-raw (command)
609   "Enter raw IRC command, which is sent to the server."
610   (interactive "sIRC command: ")
611   (riece-send-string (concat command "\r\n")))
612
613 (defun riece-command-end-of-buffer ()
614   "Get end of the dialogue buffer."
615   (interactive)
616   (let (buffer window)
617     (setq buffer (if riece-channel-buffer-mode
618                      riece-channel-buffer
619                    riece-dialogue-buffer))
620     (or (setq window (get-buffer-window buffer))
621         (setq window (get-buffer-window riece-dialogue-buffer)
622               buffer riece-dialogue-buffer))
623     (when window
624       (save-selected-window
625         (select-window window)
626         (goto-char (point-max))))))
627
628 (defun riece-command-copy-region (start end)
629   "Move current region between START and END to `kill-ring'."
630   (interactive "r")
631   (kill-new (buffer-substring-no-properties start end)))
632
633 (defun riece-command-open-server (server-name)
634   (interactive
635    (list (completing-read "Server: " riece-server-alist)))
636   (if (riece-server-process server-name)
637       (error "%s is already opened" server-name))
638   (riece-open-server
639    (riece-server-name-to-server server-name)
640    server-name))
641
642 (defun riece-command-close-server (server-name &optional message)
643   (interactive
644    (list (completing-read "Server: " riece-server-process-alist)
645          (if current-prefix-arg
646              (read-string "Message: ")
647            riece-quit-message)))
648   (riece-quit-server-process (riece-server-process server-name) message))
649
650 (defun riece-command-universal-server-name-argument ()
651   (interactive)
652   (let* ((riece-overriding-server-name
653           (completing-read "Server: " riece-server-process-alist))
654          (command
655           (key-binding (read-key-sequence
656                         (format "Command to execute on \"%s\":"
657                                 riece-overriding-server-name)))))
658     (message "")
659     (call-interactively command)))
660
661 (provide 'riece-commands)
662
663 ;;; riece-commands.el ends here