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/")