From ddaa9718eaef4e4bfd79fd2ce6fa086967e786de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mos=C3=A8=20Giordano?= <giordano.mose@libero.it>
Date: Fri, 5 Apr 2013 16:31:47 +0200
Subject: [PATCH 1/2] provide completion for options in `TeX-arg-document'

* latex.el (LaTeX-global-class-files): New variable.
(TeX-arg-document): Provide completion for class options, based on
`LaTeX-arg-usepackage'.  Use `LaTeX-global-class-files'.
(LaTeX-style-list): Mention that if `TeX-arg-input-file-search' is
set to `t' this variable will be ignored.

* tex.el (TeX-normal-mode): Reset `LaTeX-global-class-files' when
ARG is non-nil.

* style/article.el (LaTeX-article-class-options): New variable.

* style/book.el (LaTeX-book-class-options): New variable.

* style/report.el (LaTeX-report-class-options): New variable.
---
 ChangeLog        |   17 +++++++++++++++
 latex.el         |   63 ++++++++++++++++++++++++++++++++++++++----------------
 style/article.el |    6 ++++++
 style/book.el    |    7 ++++++
 style/report.el  |    7 ++++++
 tex.el           |    3 ++-
 6 files changed, 84 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3ab0320..75ea747 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2013-04-05  Mosè Giordano  <giordano.mose@libero.it>
+
+	* latex.el (LaTeX-global-class-files): New variable.
+	(TeX-arg-document): Provide completion for class options, based on
+	`LaTeX-arg-usepackage'.  Use `LaTeX-global-class-files'.
+	(LaTeX-style-list): Mention that if `TeX-arg-input-file-search' is
+	set to `t' this variable will be ignored.
+
+	* tex.el (TeX-normal-mode): Reset `LaTeX-global-class-files' when
+	ARG is non-nil.
+
+	* style/article.el (LaTeX-article-class-options): New variable.
+
+	* style/book.el (LaTeX-book-class-options): New variable.
+
+	* style/report.el (LaTeX-report-class-options): New variable.
+
 2013-04-03  Mosè Giordano  <giordano.mose@libero.it>
 
 	* latex.el (LaTeX-provided-class-options): New buffer-local
diff --git a/latex.el b/latex.el
index a91d5f2..2ebddef 100644
--- a/latex.el
+++ b/latex.el
@@ -1770,34 +1770,61 @@ string."
 			      ("scrlttr2")
 			      ("scrreprt")
 			      ("slides"))
-  "List of document classes offered when inserting a document environment."
+  "List of document classes offered when inserting a document class.
+
+If `TeX-arg-input-file-search' is set to `t', you will get
+completion with all LaTeX classes available in your distribution
+and this variable will be ignored."
   :group 'LaTeX-environment
   :type '(repeat (group (string :format "%v"))))
 
+(defvar LaTeX-global-class-files nil
+  "List of the LaTeX class files.
+Initialized once at the first time you prompt for a LaTeX class.
+May be reset with `\\[universal-argument] \\[TeX-normal-mode]'.")
+
 (defun TeX-arg-document (optional &optional ignore)
   "Insert arguments to documentclass.
 OPTIONAL and IGNORE are ignored."
   (let* ((TeX-file-extensions '("cls"))
-	 (search (if (eq TeX-arg-input-file-search 'ask)
-		     (not (y-or-n-p "Find class yourself? "))
-		   TeX-arg-input-file-search))
-	 (LaTeX-style-list
-	  (if search
-	      (mapcar 'identity (TeX-search-files-by-type 'texinputs 'global t t))
-	    LaTeX-style-list))
-	 (style (completing-read
+	 (crm-separator ",")
+	 style var options)
+    (unless LaTeX-global-class-files
+      (if (if (eq TeX-arg-input-file-search 'ask)
+	      (not (y-or-n-p "Find class yourself? "))
+	    TeX-arg-input-file-search)
+	  (progn
+	    (message "Searching for LaTeX classes...")
+	    (setq LaTeX-global-class-files
+		  (mapcar 'identity (TeX-search-files-by-type 'texinputs 'global t t))))
+	LaTeX-style-list))
+    (setq style (completing-read
 		 (concat "Document class: (default " LaTeX-default-style ") ")
-		 LaTeX-style-list))
-	 (options (read-string "Options: "
-			       (if (stringp LaTeX-default-options)
-				   LaTeX-default-options
-				 (mapconcat 'identity
-					    LaTeX-default-options
-					    ",")))))
+		 LaTeX-global-class-files))
     (if (zerop (length style))
 	(setq style LaTeX-default-style))
-    (if (not (zerop (length options)))
-	(insert LaTeX-optop options LaTeX-optcl))
+    (TeX-run-style-hooks style)
+    (setq var (intern (format "LaTeX-%s-class-options" style)))
+    (if (or (and (boundp var)
+		 (listp (symbol-value var)))
+	    (fboundp var))
+	(if (functionp var)
+	    (setq options (funcall var))
+	  (when (symbol-value var)
+	    (setq options
+		  (mapconcat 'identity
+			     (TeX-completing-read-multiple
+			      "Options: " (mapcar 'list (symbol-value var)) nil nil
+			      (if (stringp LaTeX-default-options)
+				  LaTeX-default-options
+				(mapconcat 'identity LaTeX-default-options ",")))
+			     ","))))
+      (setq options (read-string "Options: ")))
+    (unless (zerop (length options))
+      (insert LaTeX-optop options LaTeX-optcl)
+      (let ((opts (LaTeX-listify-package-options options)))
+	(TeX-add-to-alist 'LaTeX-provided-class-options
+			  (list (cons style opts)))))
     (insert TeX-grop style TeX-grcl))
 
   ;; remove old information
diff --git a/style/article.el b/style/article.el
index 2acb566..fafab26 100644
--- a/style/article.el
+++ b/style/article.el
@@ -4,6 +4,12 @@
 
 ;;; Code:
 
+(defvar LaTeX-article-class-options
+  '("a4paper" "a5paper" "b5paper" "letterpaper" "legalpaper" "executivepaper"
+    "landscape" "10pt" "11pt" "12pt" "oneside" "twoside" "draft" "final"
+    "titlepage" "notitlepage" "onecolumn" "twocolumn" "leqno" "fleqn" "openbib")
+  "Package options for the article class.")
+
 (TeX-add-style-hook
  "article"
  (lambda ()
diff --git a/style/book.el b/style/book.el
index 829fa4e..111a1e2 100644
--- a/style/book.el
+++ b/style/book.el
@@ -4,6 +4,13 @@
 
 ;;; Code:
 
+(defvar LaTeX-book-class-options
+  '("a4paper" "a5paper" "b5paper" "letterpaper" "legalpaper" "executivepaper"
+    "landscape" "10pt" "11pt" "12pt" "oneside" "twoside" "draft" "final"
+    "titlepage" "notitlepage" "openright" "openany" "onecolumn" "twocolumn"
+    "leqno" "fleqn" "openbib")
+  "Package options for the book class.")
+
 (TeX-add-style-hook
  "book"
  (lambda () 
diff --git a/style/report.el b/style/report.el
index 6791f42..e3f154a 100644
--- a/style/report.el
+++ b/style/report.el
@@ -4,6 +4,13 @@
 
 ;;; Code:
 
+(defvar LaTeX-report-class-options
+  '("a4paper" "a5paper" "b5paper" "letterpaper" "legalpaper" "executivepaper"
+    "landscape" "10pt" "11pt" "12pt" "oneside" "twoside" "draft" "final"
+    "titlepage" "notitlepage" "openright" "openany" "onecolumn" "twocolumn"
+    "leqno" "fleqn" "openbib")
+  "Package options for the report class.")
+
 (TeX-add-style-hook
  "report"
  (lambda () 
diff --git a/tex.el b/tex.el
index 6881509..8a90e83 100644
--- a/tex.el
+++ b/tex.el
@@ -5262,7 +5262,8 @@ With optional argument ARG, also reload the style hooks."
 	    BibTeX-global-files nil
 	    BibLaTeX-global-style-files nil
 	    TeX-Biber-global-files nil
-	    TeX-global-input-files nil))
+	    TeX-global-input-files nil
+	    LaTeX-global-class-files nil))
   (let ((TeX-auto-save t))
     (if (buffer-modified-p)
 	(save-buffer)
-- 
1.7.10.4

