auctex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [AUCTeX] Defining env with key-val in opt. argument


From: Tassilo Horn
Subject: Re: [AUCTeX] Defining env with key-val in opt. argument
Date: Sun, 05 Oct 2014 09:39:57 +0200
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Hi Arash,

> Thanks for your response and clarification.  With your code, I get the
> following in my tex document:
>
> \begin{itemize*}[align=left]
>   <cursor position>
> \end{itemize*}
>
> I want AUCTeX to insert the first `\item' as well.  Any idea how I can
> achieve this?

Ah, sorry, I've missed that.  I think in that case you are better off
with your own hook function.  I've adapted `LaTeX-env-item' a bit for
your use-case:

--8<---------------cut here---------------start------------->8---
(defvar LaTeX-enumitem-key-val-options
  '(;;
    ("topsep")
    ("partopsep")
    ("label")
    ("label*")
    ("align" ("left" "right" "parleft")))
  "Key=value options for enumitem macros and environments.")

(defun LaTeX-enumitem-env-with-opts (env)
  (LaTeX-insert-environment
   env
   (let ((opts (TeX-read-key-val t LaTeX-enumitem-key-val-options)))
     (when (and opts (not (string-equal opts "")))
       (format "[%s]" opts))))
  (if (TeX-active-mark)
      (progn
        (LaTeX-find-matching-begin)
        (end-of-line 1))
    (end-of-line 0))
  (delete-char 1)
  (when (looking-at (concat "^[ \t]+$\\|"
                            "^[ \t]*" TeX-comment-start-regexp "+[ \t]*$"))
    (delete-region (point) (line-end-position)))
  (delete-horizontal-space)
  ;; Deactivate the mark here in order to prevent `TeX-parse-macro'
  ;; from swapping point and mark and the \item ending up right after
  ;; \begin{...}.
  (TeX-deactivate-mark)
  (LaTeX-insert-item)
  ;; The inserted \item may have outdented the first line to the
  ;; right.  Fill it, if appropriate.
  (when (and (not (looking-at "$"))
             (not (assoc environment LaTeX-indent-environment-list))
             (> (- (line-end-position) (line-beginning-position))
                (current-fill-column)))
    (LaTeX-fill-paragraph nil)))

(TeX-add-style-hook
 "enumitem"
 (lambda ()
   ;; New environments
   (LaTeX-add-environments
    '("itemize" LaTeX-enumitem-env-with-opts)
    '("enumerate" LaTeX-enumitem-env-with-opts)
    '("itemize*" LaTeX-enumitem-env-with-opts)
    '("enumerate*" LaTeX-enumitem-env-with-opts))))
--8<---------------cut here---------------end--------------->8---

It's basically similar to your first approach except that it uses
`TeX-read-key-val' which only promts for the key-val options and returns
them as a string instead of inserting them.

Bye,
Tassilo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]