This file is indexed.

/usr/share/emacs/site-lisp/riece/riece-commands.el is in riece 9.0.0-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
;;; riece-commands.el --- commands available in command buffer -*- lexical-binding: t -*-
;; Copyright (C) 1998-2003 Daiki Ueno

;; Author: Daiki Ueno <ueno@unixuser.org>
;; Created: 1998-09-28
;; Keywords: IRC, riece

;; This file is part of Riece.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Code:

(require 'riece-channel)
(require 'riece-complete)
(require 'riece-layout)
(require 'riece-display)
(require 'riece-server)
(require 'riece-misc)
(require 'riece-identity)
(require 'riece-message)
(require 'riece-mcat)

;;; Channel movement:
(defun riece-command-switch-to-channel (channel)
  (interactive (list (riece-completing-read-identity
		      (riece-mcat "Switch to channel/user: ")
		      riece-current-channels nil t)))
  (unless (equal channel riece-current-channel)
    (riece-switch-to-channel channel)))

(defun riece-command-switch-to-channel-by-number (number)
  (interactive
   (let ((command-name (symbol-name this-command)))
     (if (string-match "[0-9]+$" command-name)
	 (list (string-to-number (match-string 0 command-name)))
       (list (string-to-number (read-string (riece-mcat "Switch to number: ")))))))
  (let ((channel (nth (1- number) riece-current-channels)))
    (if channel
	(riece-command-switch-to-channel channel)
      (error "No such number!"))))

(eval-and-compile
  (let ((number 1))
    (while (<= number 20)
      (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
				(number-to-string number)))
	'riece-command-switch-to-channel-by-number)
      (setq number (1+ number)))))

(defun riece-command-next-channel ()
  "Select the next channel."
  (interactive)
  (when (> (length riece-current-channels) 1)
    (let ((pointer (cdr (riece-identity-member
			 riece-current-channel
			 riece-current-channels))))
      (while (and pointer
		  (null (car pointer)))
	(setq pointer (cdr pointer)))
      (when (null pointer)
	(setq pointer riece-current-channels)
	(while (and pointer
		    (null (car pointer)))
	  (setq pointer (cdr pointer))))
      (if (car pointer)
	  (riece-command-switch-to-channel (car pointer))
	(error "No such channel!")))))

(defun riece-command-previous-channel ()
  "Select the previous channel."
  (interactive)
  (when (> (length riece-current-channels) 1)
    (let ((pointer (riece-identity-member
		    riece-current-channel
		    riece-current-channels))
	  (start riece-current-channels)
	  channel)
      (while (and start (not (eq start pointer)))
	(if (car start)
	    (setq channel (car start)))
	(setq start (cdr start)))
      (when (null channel)
	(setq start (copy-sequence riece-current-channels))
	(setq start (delq nil start))
	(and (> (length start) 1)
	     (setq channel (nth (1- (length start)) start))))
      (if channel
	  (riece-command-switch-to-channel channel)
	(error "No such channel!")))))

(defun riece-command-reorder-channels ()
  "Reorder channel list."
  (interactive)
  (let ((binding (mapcar
		  (lambda (channel)
		    (if channel
			(riece-parse-identity channel)))
		  riece-default-channel-binding))
	(pointer riece-current-channels)
	channels)
    (while pointer
      (if (car pointer)
	  (setq channels (riece-identity-assign-binding (car pointer) channels
							binding)))
      (setq pointer (cdr pointer)))
    (setq riece-current-channels channels)
    (riece-emit-signal 'channel-list-changed)))

(defun riece-command-select-command-buffer ()
  "Select the command buffer."
  (interactive)
  (let ((window (get-buffer-window riece-command-buffer)))
    (if window
	(select-window window))))

(defun riece-command-configure-windows ()
  (interactive)
  "Reconfigure windows with the current layout."
  (riece-redisplay-buffers t))

(defun riece-command-suspend-resume ()
  (interactive)
  "Save or restore the current window configuration."
  (let ((entry (assq 'riece-window-configuration (frame-parameters))))
    (modify-frame-parameters (selected-frame)
			     (list (cons 'riece-window-configuration
					 (current-window-configuration))))
    (if (cdr entry)
	(set-window-configuration (cdr entry))
      (delete-other-windows))
    (message
     (substitute-command-keys
      "\\[riece-command-suspend-resume] to get back the last windows"))))

(defun riece-command-change-layout (name)
  "Select a layout-name from all current available layouts and change
the layout to the selected layout-name."
  (interactive (list (completing-read (riece-mcat "Change layout: ")
				      riece-layout-alist)))
  (setq riece-layout name
	riece-save-variables-are-dirty t)
  (riece-command-configure-windows))

(defun riece-command-toggle-channel-buffer-mode ()
  (interactive)
  (setq riece-channel-buffer-mode
	(not riece-channel-buffer-mode)
	riece-save-variables-are-dirty t)
  (riece-command-configure-windows))

(defun riece-command-toggle-others-buffer-mode ()
  (interactive)
  (setq riece-others-buffer-mode
	(not riece-others-buffer-mode)
	riece-save-variables-are-dirty t)
  (riece-command-configure-windows))

(defun riece-command-toggle-user-list-buffer-mode ()
  (interactive)
  (setq riece-user-list-buffer-mode
	(not riece-user-list-buffer-mode)
	riece-save-variables-are-dirty t)
  (riece-command-configure-windows))

(defun riece-command-toggle-channel-list-buffer-mode ()
  (interactive)
  (setq riece-channel-list-buffer-mode
	(not riece-channel-list-buffer-mode)
	riece-save-variables-are-dirty t)
  (riece-command-configure-windows))

(defun riece-command-finger (user &optional recurse)
  (interactive
   (let* ((completion-ignore-case t)
	  (user (riece-completing-read-identity
		 (riece-mcat "Finger user: ")
		 (riece-get-users-on-server (riece-current-server-name))
		 nil nil nil nil nil t)))
     (list user current-prefix-arg)))
  (if recurse
      (riece-send-string (format "WHOIS %s %s\r\n"
				 (riece-identity-prefix user)
				 (riece-identity-prefix user)))
    (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))

(defun riece-command-topic (topic)
  (interactive
   (progn
     (riece-check-channel-commands-are-usable t)
     (list (read-from-minibuffer
	    (riece-mcat "Set topic: ")
	    (cons (or (riece-with-server-buffer
			  (riece-identity-server
			   riece-current-channel)
			(riece-channel-get-topic
			 (riece-identity-prefix
			  riece-current-channel)))
		      "")
		  0)))))
  (riece-send-string (format "TOPIC %s :%s\r\n"
			     (riece-identity-prefix riece-current-channel)
			     topic)
		     riece-current-channel))

(defun riece-command-invite (user)
  (interactive
   (let ((completion-ignore-case t))
     (riece-check-channel-commands-are-usable t)
     (list (riece-completing-read-identity
	    (riece-mcat "Invite user: ")
	    (riece-get-users-on-server (riece-current-server-name))
	    nil nil nil nil nil t))))
  (riece-send-string (format "INVITE %s :%s\r\n"
			     (riece-identity-prefix user)
			     (riece-identity-prefix riece-current-channel))))

(defun riece-command-kick (user &optional message)
  (interactive
   (let ((completion-ignore-case t))
     (riece-check-channel-commands-are-usable t)
     (list (completing-read
	    (riece-mcat "Kick user: ")
	    (riece-with-server-buffer
		(riece-identity-server riece-current-channel)
	      (riece-channel-get-users (riece-identity-prefix
					riece-current-channel))))
	   (if current-prefix-arg
	       (read-string "Message: ")))))
  (riece-send-string
   (if message
       (format "KICK %s %s :%s\r\n"
	       (riece-identity-prefix riece-current-channel)
	       user message)
     (format "KICK %s %s\r\n"
	     (riece-identity-prefix riece-current-channel)
	     user))
   riece-current-channel))

(defun riece-command-kick-with-ban (user pattern &optional message)
  (interactive
   (let ((completion-ignore-case t)
	 user)
     (riece-check-channel-commands-are-usable t)
     (riece-with-server-buffer (riece-identity-server riece-current-channel)
       (setq user (completing-read
		   (riece-mcat "Kick user: ")
		   (riece-channel-get-users (riece-identity-prefix
					     riece-current-channel))))
       (list
	user
	(read-from-minibuffer
	 (riece-mcat "Ban pattern: ")
	 (concat user "!" (riece-user-get-user-at-host user)))
	(if current-prefix-arg
	    (read-string "Message: "))))))
  (riece-send-string (format "MODE %s :+b %s\r\n"
			     (riece-identity-prefix riece-current-channel)
			     pattern)
		     riece-current-channel)
  (riece-send-string
   (if message
       (format "KICK %s %s :%s\r\n"
	       (riece-identity-prefix riece-current-channel)
	       user message)
     (format "KICK %s %s\r\n"
	     (riece-identity-prefix riece-current-channel)
	     user))
   riece-current-channel))

(defun riece-command-names (pattern)
  (interactive
   (let ((completion-ignore-case t))
     (list (read-from-minibuffer
	    (riece-mcat "NAMES pattern: ")
	    (if (and riece-current-channel
		     (riece-channel-p (riece-identity-prefix
				       riece-current-channel)))
		(cons (riece-identity-prefix riece-current-channel)
		      0))))))
  (if (or (not (equal pattern ""))
	  (yes-or-no-p (riece-mcat
			"Really want to query NAMES without argument? ")))
      (riece-send-string (format "NAMES %s\r\n" pattern))))

(defun riece-command-who (pattern)
  (interactive
   (let ((completion-ignore-case t))
     (list (read-from-minibuffer
	    (riece-mcat "WHO pattern: ")
	    (if (and riece-current-channel
		     (riece-channel-p (riece-identity-prefix
				       riece-current-channel)))
		(cons (riece-identity-prefix riece-current-channel)
		      0))))))
  (if (or (not (equal pattern ""))
	  (yes-or-no-p (riece-mcat
			"Really want to query WHO without argument? ")))
      (riece-send-string (format "WHO %s\r\n" pattern))))

(defun riece-command-list (pattern)
  (interactive
   (let ((completion-ignore-case t))
     (list (read-from-minibuffer
	    (riece-mcat "LIST pattern: ")
	    (if (and riece-current-channel
		     (riece-channel-p (riece-identity-prefix
				       riece-current-channel)))
		(cons (riece-identity-prefix riece-current-channel)
		      0))))))
  (if (or (not (equal pattern ""))
	  (yes-or-no-p (riece-mcat
			"Really want to query LIST without argument? ")))
      (riece-send-string (format "LIST %s\r\n" pattern))))

(defvar riece-temp-minibuffer-message)
(defvar riece-overriding-server-name)
(defun riece-command-change-mode (channel change)
  (interactive
   (let* ((completion-ignore-case t)
	  (channel
	   (if current-prefix-arg
	       (riece-completing-read-identity
		(riece-mcat "Change mode for channel/user: ")
		(riece-get-identities-on-server (riece-current-server-name))
		nil nil nil nil nil t)
	     (riece-check-channel-commands-are-usable t)
	     riece-current-channel))
	  (riece-overriding-server-name (riece-identity-server channel))
	  (riece-temp-minibuffer-message
	   (concat (riece-mcat "[Available modes: ")
		   (riece-with-server-buffer (riece-identity-server channel)
		     (if (riece-channel-p (riece-identity-prefix channel))
			 (if riece-supported-channel-modes
			     (apply #'string riece-supported-channel-modes))
		       (if riece-supported-user-modes
			   (apply #'string riece-supported-user-modes))))
		   "]")))
     (list channel
	   (read-from-minibuffer
	    (concat (riece-concat-channel-modes
		     channel (riece-mcat "Mode (? for help)")) ": ")
	    nil riece-minibuffer-map))))
  (if (equal change "")
      (riece-send-string (format "MODE %s\r\n"
				 (riece-identity-prefix channel)))
    (riece-send-string (format "MODE %s %s\r\n"
			       (riece-identity-prefix channel)
			       change))))

(defun riece-command-set-operators (users &optional _arg)
  (interactive
   (progn
     (riece-check-channel-commands-are-usable t)
     (let ((completion-ignore-case t))
       (list (riece-completing-read-multiple
	      (if current-prefix-arg
		  (riece-mcat "Unset +o for users")
		(riece-mcat "Set +o for users"))
	      (riece-with-server-buffer
		  (riece-identity-server riece-current-channel)
		(riece-channel-get-users (riece-identity-prefix
					 riece-current-channel)))
	      (if current-prefix-arg
		  (lambda (user)
		    (memq ?o (cdr user)))
		(lambda (user)
		  (not (memq ?o (cdr user))))))
	     current-prefix-arg))))
  (let (group)
    (while users
      (setq group (cons (car users) group)
	    users (cdr users))
      (when (or (= (length group) 3)
		(null users))
	(riece-send-string
	 (format "MODE %s %c%s %s\r\n"
		 (riece-identity-prefix riece-current-channel)
		 (if current-prefix-arg
		     ?-
		   ?+)
		 (make-string (length group) ?o)
		 (mapconcat #'identity (nreverse group) " ")))
	(setq group nil)))))

(defun riece-command-set-speakers (users &optional _arg)
  (interactive
   (progn
     (riece-check-channel-commands-are-usable t)
     (let ((completion-ignore-case t))
       (list (riece-completing-read-multiple
	      (if current-prefix-arg
		  (riece-mcat "Unset +v for users")
		(riece-mcat "Set +v for users"))
	      (riece-with-server-buffer
		  (riece-identity-server riece-current-channel)
		(riece-channel-get-users (riece-identity-prefix
					  riece-current-channel)))
	      (if current-prefix-arg
		  (lambda (user)
		    (memq ?v (cdr user)))
		(lambda (user)
		  (not (memq ?v (cdr user))))))
	     current-prefix-arg))))
  (let (group)
    (while users
      (setq group (cons (car users) group)
	    users (cdr users))
      (when (or (= (length group) 3)
		(null users))
	(riece-send-string
	 (format "MODE %s %c%s %s\r\n"
		 (riece-identity-prefix riece-current-channel)
		 (if current-prefix-arg
		     ?-
		   ?+)
		 (make-string (length group) ?v)
		 (mapconcat #'identity (nreverse group) " ")))
	(setq group nil)))))

(defun riece-command-send-message (message notice)
  "Send MESSAGE to the current channel."
  (run-hooks 'riece-command-send-message-hook)
  (if (equal message "")
      (error (riece-mcat "No text to send")))
  (riece-check-channel-commands-are-usable)
  (if notice
      (progn
	(riece-send-string
	 (format "NOTICE %s :%s\r\n"
		 (riece-identity-prefix riece-current-channel)
		 message)
	 riece-current-channel)
	(riece-display-message
	 (riece-make-message (riece-current-nickname) riece-current-channel
			     message 'notice t)))
    (riece-send-string
     (format "PRIVMSG %s :%s\r\n"
	     (riece-identity-prefix riece-current-channel)
	     message)
     riece-current-channel)
    (riece-display-message
     (riece-make-message (riece-current-nickname) riece-current-channel
			 message nil t))))

(defun riece-command-enter-message ()
  "Send the current line to the current channel."
  (interactive)
  (riece-command-send-message (buffer-substring
			       (riece-line-beginning-position)
			       (riece-line-end-position))
			      nil)
  (forward-line 1)
  (when (eobp)
    (insert "\n")))


(defun riece-command-enter-message-as-notice ()
  "Send the current line to the current channel as NOTICE."
  (interactive)
  (riece-command-send-message (buffer-substring
			       (riece-line-beginning-position)
			       (riece-line-end-position))
			      t)
  (forward-line 1)
  (when (eobp)
    (insert "\n")))

(defun riece-command-enter-message-to-user (user)
  "Send the current line to USER."
  (interactive
   (if (and (bolp) (eolp))
       (error "No text to send")
     (let ((completion-ignore-case t))
       (list (riece-completing-read-identity
	      (riece-mcat "Message to user: ")
	      (riece-get-users-on-server (riece-current-server-name))
	      nil nil nil nil nil t)))))
  (let ((text (buffer-substring
	       (riece-line-beginning-position)
	       (riece-line-end-position))))
    (riece-send-string
     (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text)
     user)
    (riece-display-message
     (riece-make-message (riece-current-nickname) user text nil t)))
  (forward-line 1)
  (when (eobp)
    (insert "\n")))

(defun riece-command-join-channel (target key)
  (unless (riece-server-opened (riece-identity-server target))
    (error "%s" (substitute-command-keys
		 "Type \\[riece-command-open-server] to open server.")))
  (riece-send-string (if key
			 (format "JOIN %s :%s\r\n"
				 (riece-identity-prefix target)
				 key)
		       (format "JOIN %s\r\n"
			       (riece-identity-prefix target)))
		     target))

(defun riece-command-join-partner (target)
  (let ((pointer (riece-identity-member target riece-current-channels)))
    (if pointer
	(riece-command-switch-to-channel (car pointer))
      (riece-join-channel target)
      (riece-switch-to-channel target))))

(defun riece-command-join (targets)
  (interactive
   (let ((completion-ignore-case t)
	 (candidates (mapcar (lambda (channel)
			       (list (riece-format-identity channel)))
			     (delq nil (riece-get-identities-on-server
					(riece-current-server-name)))))
	 (default (if riece-join-channel-candidate
		      (riece-format-identity
		       riece-join-channel-candidate))))
     (list
      (mapcar #'riece-parse-identity
	      (riece-completing-read-multiple
	       (if default
		   (format (riece-mcat "Join channel/user (default %s)")
			   default)
		 (riece-mcat "Join channel/user"))
	       candidates nil nil nil nil default)))))
  (let (pointer)
    (if (and (= (length targets) 1)
	     (setq pointer (riece-identity-member (car targets)
						  riece-current-channels)))
	(riece-command-switch-to-channel (car pointer))
      (setq pointer targets)
      (while pointer
	(if (riece-channel-p (riece-identity-prefix (car pointer)))
	    (riece-command-join-channel (car pointer) nil)
	  (riece-command-join-partner (car pointer)))
	(setq pointer (cdr pointer))))))

(defun riece-command-part-channel (target message)
  (unless (riece-server-opened (riece-identity-server target))
    (error "%s" (substitute-command-keys
		 "Type \\[riece-command-open-server] to open server.")))
  (riece-send-string (if message
			 (format "PART %s :%s\r\n"
				 (riece-identity-prefix target)
				 message)
		       (format "PART %s\r\n"
			       (riece-identity-prefix target)))
		     target))

(defun riece-command-part (target &optional message)
  (interactive
   (progn
     (riece-check-channel-commands-are-usable)
     (let* ((completion-ignore-case t)
	    (target
	     (riece-completing-read-identity
	      (format (riece-mcat "Part from channel/user (default %s): ")
		      (riece-format-identity riece-current-channel))
	      riece-current-channels nil nil nil nil
	      (riece-format-identity riece-current-channel)))
	    (message
	     (if current-prefix-arg
		 (read-string (riece-mcat "Message: "))
	       riece-part-message)))
       (list target message))))
  (if (riece-identity-member target riece-current-channels)
      (if (riece-channel-p (riece-identity-prefix target))
	  (riece-command-part-channel target message)
	(riece-part-channel target))
    (error "You are not talking with %s" target)))

(defun riece-command-change-nickname (nickname)
  "Change your nickname to NICK."
  (interactive "sEnter your nickname: ")
  (riece-send-string (format "NICK %s\r\n" nickname)))

(defun riece-command-scroll-down (lines)
  "Scroll LINES down dialogue buffer from command buffer."
  (interactive "P")
  (let ((buffer (if (and riece-channel-buffer-mode
			 riece-current-channel)
		    riece-channel-buffer
		  riece-dialogue-buffer)))
    (if (get-buffer-window buffer)
	(condition-case nil
	    (let ((other-window-scroll-buffer buffer))
	      (scroll-other-window-down lines))
	  (beginning-of-buffer
	   (message (riece-mcat "Beginning of buffer")))))))

(defun riece-command-scroll-up (lines)
  "Scroll LINES up dialogue buffer from command buffer."
  (interactive "P")
  (let ((buffer (if (and riece-channel-buffer-mode
			 riece-current-channel)
		    riece-channel-buffer
		  riece-dialogue-buffer)))
    (if (get-buffer-window buffer)
	(condition-case nil
	    (let ((other-window-scroll-buffer buffer))
	      (scroll-other-window lines))
	  (end-of-buffer
	   (message (riece-mcat "End of buffer")))))))

(defun riece-command-user-list-scroll-down (lines)
  "Scroll LINES down user list buffer from command buffer."
  (interactive "P")
  (if (get-buffer-window riece-user-list-buffer)
      (condition-case nil
	  (let ((other-window-scroll-buffer riece-user-list-buffer))
	    (scroll-other-window-down lines))
	(beginning-of-buffer
	 (message (riece-mcat "Beginning of buffer"))))))

(defun riece-command-user-list-scroll-up (lines)
  "Scroll LINES up user list buffer from command buffer."
  (interactive "P")
  (if (get-buffer-window riece-user-list-buffer)
      (condition-case nil
	  (let ((other-window-scroll-buffer riece-user-list-buffer))
	    (scroll-other-window lines))
	(end-of-buffer
	 (message (riece-mcat "End of buffer"))))))

(defun riece-command-toggle-away (&optional message)
  "Mark yourself as being away."
  (interactive
   (if (and (not (riece-with-server-buffer (riece-identity-server
					    (riece-current-nickname))
		   (riece-user-get-away (riece-identity-prefix
					 (riece-current-nickname)))))
	    current-prefix-arg)
       (list (read-from-minibuffer
	      (riece-mcat "Away message: ") (cons (or riece-away-message "")
						  0)))))
  (if (riece-with-server-buffer (riece-identity-server
				 (riece-current-nickname))
	(riece-user-get-away (riece-identity-prefix
			      (riece-current-nickname))))
      (riece-send-string "AWAY\r\n")
    (riece-send-string (format "AWAY :%s\r\n" (or message
						  riece-away-message)))))

(defun riece-command-toggle-freeze (&optional arg)
  "Prevent automatic scrolling of the dialogue window.
If prefix argument ARG is non-nil, toggle frozen status."
  (interactive "P")
  (with-current-buffer (if (riece-derived-mode-p 'riece-dialogue-mode)
			   (current-buffer)
			 (if (and riece-channel-buffer-mode
				  riece-channel-buffer)
			     riece-channel-buffer
			   riece-dialogue-buffer))
    (setq riece-freeze (if arg
			   (< 0 (prefix-numeric-value arg))
			 (not riece-freeze)))
    (riece-emit-signal 'buffer-freeze-changed
		       (current-buffer) riece-freeze)))

(defun riece-command-toggle-own-freeze (&optional arg)
  "Prevent automatic scrolling of the dialogue window.
The difference from `riece-command-freeze' is that your messages are hidden.
If prefix argument ARG is non-nil, toggle frozen status."
  (interactive "P")
  (with-current-buffer (if (riece-derived-mode-p 'riece-dialogue-mode)
			   (current-buffer)
			 (if (and riece-channel-buffer-mode
				  riece-channel-buffer)
			     riece-channel-buffer
			   riece-dialogue-buffer))
    (if (if arg
	    (< 0 (prefix-numeric-value arg))
	  (not (eq riece-freeze 'own)))
	(setq riece-freeze 'own)
      (setq riece-freeze nil))
    (riece-emit-signal 'buffer-freeze-changed
		       (current-buffer) riece-freeze)))

(eval-when-compile
  (autoload 'riece-exit "riece"))
(defun riece-command-quit (&optional arg)
  "Quit IRC."
  (interactive "P")
  (if (null riece-server-process-alist)
      (progn
	(message (riece-mcat "No server process"))
	(ding))
    (if (y-or-n-p (riece-mcat "Really quit IRC? "))
	(let ((message
	       (if arg
		   (read-string (riece-mcat "Message: "))
		 riece-quit-message))
	      (alist riece-server-process-alist))
	  (while alist
	    (riece-quit-server-process (cdr (car alist)) message)
	    (setq alist (cdr alist)))))))

(defun riece-command-raw (command)
  "Enter raw IRC command, which is sent to the server."
  (interactive "sIRC command: ")
  (riece-send-string (concat command "\r\n")))

(defun riece-command-beginning-of-buffer ()
  "Scroll channel buffer to the beginning."
  (interactive)
  (let (buffer window)
    (setq buffer (if riece-channel-buffer-mode
		     riece-channel-buffer
		   riece-dialogue-buffer))
    (or (setq window (get-buffer-window buffer))
	(setq window (get-buffer-window riece-dialogue-buffer)
	      buffer riece-dialogue-buffer))
    (when window
      (save-selected-window
	(select-window window)
	(goto-char (point-min))))))

(defun riece-command-end-of-buffer ()
  "Scroll channel buffer to the end."
  (interactive)
  (let (buffer window)
    (setq buffer (if riece-channel-buffer-mode
		     riece-channel-buffer
		   riece-dialogue-buffer))
    (or (setq window (get-buffer-window buffer))
	(setq window (get-buffer-window riece-dialogue-buffer)
	      buffer riece-dialogue-buffer))
    (when window
      (save-selected-window
	(select-window window)
	(goto-char (point-max))))))

(defun riece-command-copy-region (start end)
  "Move current region between START and END to `kill-ring'."
  (interactive "r")
  (kill-new (buffer-substring-no-properties start end)))

(defun riece-command-complete-user ()
  "Complete a user name in the current buffer."
  (interactive)
  (let* ((completion-ignore-case t)
	 (table (mapcar (lambda (user)
			  (list (riece-format-identity user t)))
			(riece-get-users-on-server
			 (riece-current-server-name))))
	 (current (or (current-word) ""))
	 (completion (try-completion current table))
	 (all (all-completions current table)))
    (if (eq completion t)
	nil
      (if (null completion)
	  (message (riece-mcat "Can't find completion for \"%s\"") current)
	(if (equal current completion)
	    (with-output-to-temp-buffer "*Help*"
	      (display-completion-list all))
	  (re-search-forward "\\>" nil t)
	  (delete-region (point) (- (point) (length current)))
	  (insert completion))))))

(defun riece-command-open-server (server-name)
  (interactive
   (list (completing-read (riece-mcat "Open server: ") riece-server-alist)))
  (if (riece-server-process server-name)
      (error "%s is already opened" server-name))
  (riece-open-server
   (riece-server-name-to-server server-name)
   server-name))

(defun riece-command-close-server (server-name &optional message)
  (interactive
   (list (completing-read (riece-mcat "Close server: ")
			  riece-server-process-alist)
	 (if current-prefix-arg
	     (read-string (riece-mcat "Message: "))
	   riece-quit-message)))
  (let ((process (riece-server-process server-name)))
    (unless process
      (error "%s is not opened" server-name))
    (riece-quit-server-process process message)))

(defun riece-command-universal-server-name-argument ()
  (interactive)
  (let* ((riece-overriding-server-name
	  (completing-read (riece-mcat "Server: ") riece-server-process-alist))
	 (command
	  (key-binding (read-key-sequence
			(format (riece-mcat "Command to execute on \"%s\":")
				riece-overriding-server-name)))))
    (message "")
    (call-interactively command)))

(eval-when-compile
  (autoload 'riece-save-variables-files "riece"))
(defun riece-command-save-variables ()
  "Save `riece-variables-file'."
  (interactive)
  (if (or riece-save-variables-are-dirty
	  (y-or-n-p (riece-mcat "No changes made.  Save anyway? ")))
      (riece-save-variables-files)))

(provide 'riece-commands)

;;; riece-commands.el ends here