Subject: Re: 23.1.96; [PATCH] rcirc-complete for nicks and commands

From: Leo <[email-masked]>
Date: Mon, 23 Aug 2010 11:36:25 +0100

I have regenerated the patch against emacs-23 branch with a small tweak
that is nick completion no longer inserts a space (same as in the
original completion) if it is in the middle of an input.

I wonder if it is possible to apply this patch first and then think
about using the new completion-in-region-functions. I think this is a
safer step. completion-in-region-functions can be problematic when more
than one mode customise it though I haven't looked into this in full.
But I currently have TeX and a minor mode uses
completion-in-region-functions and I have been surprised a few times.

>From af9423a2d099f615a3ff45c0bfa67ae58856681d Mon Sep 17 00:00:00 2001
From: Leo <[email-masked]>
Date: Mon, 23 Aug 2010 11:14:05 +0100
Subject: [PATCH] New command rcirc-complete (#6076)

which completes both nicks and irc commands.
---
 lisp/net/rcirc.el |  100 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 63 insertions(+), 37 deletions(-)

diff --git a/lisp/net/rcirc.el b/lisp/net/rcirc.el
index ec67c3e..373e1b3 100644
--- a/lisp/net/rcirc.el
+++ b/lisp/net/rcirc.el
@@ -771,42 +771,66 @@ If SILENT is non-nil, do not print the message in any irc buffer."
     (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
     (insert (rcirc-prev-input-string -1))))
 
-(defvar rcirc-nick-completions nil)
-(defvar rcirc-nick-completion-start-offset nil)
-
-(defun rcirc-complete-nick ()
-  "Cycle through nick completions from list of nicks in channel."
+(defvar rcirc-server-commands
+  '("/admin"   "/away"   "/connect" "/die"      "/error"   "/info"
+    "/invite"  "/ison"   "/join"    "/kick"     "/kill"    "/links"
+    "/list"    "/lusers" "/mode"    "/motd"     "/names"   "/nick"
+    "/notice"  "/oper"   "/part"    "/pass"     "/ping"    "/pong"
+    "/privmsg" "/quit"   "/rehash"  "/restart"  "/service" "/servlist"
+    "/server"  "/squery" "/squit"   "/stats"    "/summon"  "/time"
+    "/topic"   "/trace"  "/user"    "/userhost" "/users"   "/version"
+    "/wallops" "/who"    "/whois"   "/whowas")
+  "A list of user commands by IRC server.
+The value defaults to RFCs 1459 and 2812.")
+
+;; /me and /ctcp are not defined by `defun-rcirc-command'.
+(defvar rcirc-client-commands '("/me" "/ctcp")
+  "A list of user commands defined by IRC client rcirc.
+The list is updated automatically by `defun-rcirc-command'.")
+
+(defvar rcirc-completions nil)
+(defvar rcirc-completion-start-offset nil)
+
+(defun rcirc-complete ()
+  "Cycle through completions from list of nicks in channel or IRC commands.
+IRC command completion is performed only if '/' is the first input char."
   (interactive)
   (if (eq last-command this-command)
-      (setq rcirc-nick-completions
-            (append (cdr rcirc-nick-completions)
-                    (list (car rcirc-nick-completions))))
-    (setq rcirc-nick-completion-start-offset
-          (- (save-excursion
-               (if (re-search-backward " " rcirc-prompt-end-marker t)
-                   (1+ (point))
-                 rcirc-prompt-end-marker))
-             rcirc-prompt-end-marker))
-    (setq rcirc-nick-completions
-          (let ((completion-ignore-case t))
-            (all-completions
+      (setq rcirc-completions
+	    (append (cdr rcirc-completions)
+		    (list (car rcirc-completions))))
+    (setq rcirc-completion-start-offset
+	  (- (save-excursion
+	       (if (re-search-backward " " rcirc-prompt-end-marker t)
+		   (1+ (point))
+		 rcirc-prompt-end-marker))
+	     rcirc-prompt-end-marker))
+    (let ((completion-ignore-case t))
+      (setq rcirc-completions
+	    (all-completions
 	     (buffer-substring
 	      (+ rcirc-prompt-end-marker
-		 rcirc-nick-completion-start-offset)
+		 rcirc-completion-start-offset)
 	      (point))
-	     (mapcar (lambda (x) (cons x nil))
-		     (rcirc-channel-nicks (rcirc-buffer-process)
-					  rcirc-target))))))
-  (let ((completion (car rcirc-nick-completions)))
+	     (if (and (zerop rcirc-completion-start-offset)
+		      (char-after rcirc-prompt-end-marker)
+		      (= (char-after rcirc-prompt-end-marker) ?/))
+		 (delete-dups
+		  (append (sort (copy-sequence rcirc-client-commands) 'string-lessp)
+			  (sort (copy-sequence rcirc-server-commands) 'string-lessp)))
+	       (mapcar (lambda (x) (cons x nil))
+		       (rcirc-channel-nicks (rcirc-buffer-process)
+					    rcirc-target)))))))
+  (let ((completion (car rcirc-completions)))
     (when completion
       (delete-region (+ rcirc-prompt-end-marker
-			rcirc-nick-completion-start-offset)
+			rcirc-completion-start-offset)
 		     (point))
       (insert (concat completion
-                      (if (= (+ rcirc-prompt-end-marker
-                                rcirc-nick-completion-start-offset)
-                             rcirc-prompt-end-marker)
-                          ": "))))))
+		      (cond
+		       ((= (aref completion 0) ?/) " ")
+		       ((zerop rcirc-completion-start-offset) ": ")
+		       (t "")))))))
 
 (defun set-rcirc-decode-coding-system (coding-system)
   "Set the decode coding system used in this channel."
@@ -824,7 +848,7 @@ If SILENT is non-nil, do not print the message in any irc buffer."
 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
-(define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick)
+(define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete)
 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
@@ -1962,16 +1986,18 @@ activity.  Only run if the buffer is not visible and
 ;; containing the text following the /cmd.
 
 (defmacro defun-rcirc-command (command argument docstring interactive-form
-                                       &rest body)
+				       &rest body)
   "Define a command."
-  `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
-     (,@argument &optional process target)
-     ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
-              "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
-     ,interactive-form
-     (let ((process (or process (rcirc-buffer-process)))
-           (target (or target rcirc-target)))
-       ,@body)))
+  `(progn
+     (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
+     (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
+       (,@argument &optional process target)
+       ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
+		"\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
+       ,interactive-form
+       (let ((process (or process (rcirc-buffer-process)))
+	     (target (or target rcirc-target)))
+	 ,@body))))
 
 (defun-rcirc-command msg (message)
   "Send private MESSAGE to TARGET."
-- 
1.7.2

Leo