Tassilo, 2014-02-14 19:03 GMT+01:00 Mosè Giordano <giordano.m...@libero.it>: > 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).
Any comments about this feature? :-) I'm sending a revised version of the patch, the major change is that you aren't prompted for options if no package is supplied. Bye, Mosè
diff --git a/latex.el b/latex.el index b2dad4a..8780e47 100644 --- a/latex.el +++ b/latex.el @@ -771,7 +771,9 @@ 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 @@ -787,6 +789,9 @@ The compatibility argument IGNORE is ignored." (TeX-insert-macro "documentclass") (LaTeX-newline) (LaTeX-newline) + ;; Add a newline only if some `\usepackage' has been inserted. + (if (LaTeX-insert-usepackages) + (LaTeX-newline)) (LaTeX-newline) (end-of-line 0))) (LaTeX-insert-environment "document") @@ -1874,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) @@ -1891,37 +1899,68 @@ 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)) - (intern (format "LaTeX-%s-package-options" (car packages))) - ;; Something like `\usepackage[options]{pkg1,pkg2,pkg3,...}' is - ;; allowed (provided that pkg1, pkg2, pkg3, ... accept same - ;; options). When there is more than one package, set `var' to - ;; a dummy value so next `if' enters else form. - t)) - (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))) - ",")))) - (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))) + ;; Prompt for options only if at least one package has been given, return + ;; nil otherwise. + (unless (equal packages '("")) + (setq var (if (= 1 (length packages)) + (intern (format "LaTeX-%s-package-options" (car packages))) + ;; Something like `\usepackage[options]{pkg1,pkg2,pkg3,...}' is + ;; allowed (provided that pkg1, pkg2, pkg3, ... accept same + ;; options). When there is more than one package, set `var' to + ;; a dummy value so next `if' enters else form. + t)) + (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))) + ",")))) + (setq options (read-string "Options: "))) + (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. + +Return t if at least one \\usepackage has been inserted, nil +otherwise." + (let (packages-options packages options (inserted nil)) + (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) + (setq inserted t)) + inserted)) (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