2014-02-14 16:31 GMT+01:00 Mosè Giordano <giordano.m...@libero.it>:
> You might want to insert the `\usepackage's after the `\documentclass'
> inside the `LaTeX-env-document' and I might agree, but that would
> require some care and work.  Is this what you want?  I can try to
> write down a patch for this.

Ok, I wrote a small patch jsut to show what I mean.  It is a
proof-of-concept, can be improved and suggestions are welcome.  The
main changes are:
- the body of `LaTeX-arg-usepackage' has been split into two new
functions, one (LaTeX-arg-usepackage-get-packages-options) to get the
arguments of `\usepackage' and another (LaTeX-arg-usepackage-insert)
to actually insert them.  Apart from splitting its body, nothing has
been changed inside `LaTeX-arg-usepackage'
- a new function (LaTeX-insert-usepackages) has been defined, which
prompts for a new `\usepackage' until empty input
- `LaTeX-env-document' uses `LaTeX-insert-usepackages'.

If this feature will be accepted, possible improvements come to my mind are:
- in `LaTeX-arg-usepackage-get-packages-options', don't prompt for
options if no package has been given
- in `LaTeX-env-document', make autoinsertion of `\documentclass' and
`\usepackage' optional but on by default (maybe not everybody likes
this kind of automatisms).

Bye,
Mosè
diff --git a/latex.el b/latex.el
index 974ea42..355b82d 100644
--- a/latex.el
+++ b/latex.el
@@ -775,10 +775,12 @@ To insert a hook here, you must insert it in the appropiate style file.")
 
 (defun LaTeX-env-document (&optional ignore)
   "Create new LaTeX document.
-Also inserts a \\documentclass macro if there's none already
+Also inserts a \\documentclass macro if there's none already and
+prompt for the insertion of \\usepackage macros.
+
 The compatibility argument IGNORE is ignored."
-  ;; just assume a single valid \\documentclass, i.e., one not in a
-  ;; commented line
+  ;; Just assume a single valid \\documentclass, i.e., one not in a
+  ;; commented line.
   (let ((found nil))
     (save-excursion
       (while (and (not found)
@@ -791,6 +793,8 @@ The compatibility argument IGNORE is ignored."
       (TeX-insert-macro "documentclass")
       (LaTeX-newline)
       (LaTeX-newline)
+      (LaTeX-insert-usepackages)
+      (LaTeX-newline)
       (LaTeX-newline)
       (end-of-line 0)))
   (LaTeX-insert-environment "document")
@@ -1875,9 +1879,12 @@ OPTIONAL and IGNORE are ignored."
 
 To insert a hook here, you must insert it in the appropiate style file.")
 
-(defun LaTeX-arg-usepackage (optional)
-  "Insert arguments to usepackage.
-OPTIONAL is ignored."
+(defun LaTeX-arg-usepackage-get-packages-options ()
+  "Get the packages and the options for the usepackage macro.
+
+This function returns nil if no package is provided, a cons cell
+otherwise, whose CAR is the list of packages, and the CDR is the
+string of the options."
   (let* ((TeX-file-extensions '("sty"))
 	 (crm-separator ",")
 	 packages var options)
@@ -1892,7 +1899,7 @@ OPTIONAL is ignored."
 				 'texinputs 'global t t))))))
     (setq packages (TeX-completing-read-multiple
 		    "Packages: " TeX-global-input-files))
-    ;; Clean up hook before use.
+    ;; Clean up hook before use in `LaTeX-arg-usepackage-insert'.
     (setq LaTeX-after-usepackage-hook nil)
     (mapc 'TeX-run-style-hooks packages)
     (setq var (if (= 1 (length packages))
@@ -1914,15 +1921,39 @@ OPTIONAL is ignored."
 			      "Options: " (mapcar 'list (symbol-value var)))
 			     ","))))
       (setq options (read-string "Options: ")))
-    (unless (zerop (length options))
-      (let ((opts (LaTeX-listify-package-options options)))
-	(mapc (lambda (elt)
-		(TeX-add-to-alist 'LaTeX-provided-package-options
-				  (list (cons elt opts))))
-	      packages))
-      (insert LaTeX-optop options LaTeX-optcl))
-    (insert TeX-grop (mapconcat 'identity packages ",") TeX-grcl)
-    (run-hooks 'LaTeX-after-usepackage-hook)))
+    (unless (equal packages '(""))
+      (cons packages options))))
+
+(defun LaTeX-arg-usepackage-insert (packages options)
+  "Actually insert arguments to usepackage."
+  (unless (zerop (length options))
+    (let ((opts (LaTeX-listify-package-options options)))
+      (mapc (lambda (elt)
+	      (TeX-add-to-alist 'LaTeX-provided-package-options
+				(list (cons elt opts))))
+	    packages))
+    (insert LaTeX-optop options LaTeX-optcl))
+  (insert TeX-grop (mapconcat 'identity packages ",") TeX-grcl)
+  (run-hooks 'LaTeX-after-usepackage-hook))
+
+(defun LaTeX-arg-usepackage (optional)
+  "Insert arguments to usepackage.
+OPTIONAL is ignored."
+  (let* ((packages-options (LaTeX-arg-usepackage-get-packages-options))
+	 (packages (car packages-options))
+	 (options (cdr packages-options)))
+    (LaTeX-arg-usepackage-insert packages options)))
+
+(defun LaTeX-insert-usepackages ()
+  "Prompt for the insertion of usepackage macros until empty
+input is reached."
+  (let (packages-options packages options)
+    (while (setq packages-options (LaTeX-arg-usepackage-get-packages-options))
+      (setq packages (car packages-options))
+      (setq options (cdr packages-options))
+      (insert TeX-esc "usepackage")
+      (LaTeX-arg-usepackage-insert packages options)
+      (LaTeX-newline))))
 
 (defcustom LaTeX-search-files-type-alist
   '((texinputs "${TEXINPUTS.latex}" ("tex/generic/" "tex/latex/")
_______________________________________________
auctex-devel mailing list
auctex-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/auctex-devel

Reply via email to