auctex
[Top][All Lists]
Advanced

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

Re: [AUCTeX] make a newcommand and use it immediately


From: Ivan Andrus
Subject: Re: [AUCTeX] make a newcommand and use it immediately
Date: Wed, 22 Feb 2012 10:43:27 +0100

On Feb 21, 2012, at 6:42 PM, Nicolas Richard wrote:

> Hello,
> 
> I'd like to define an emacs function which would:
> - add a \newcommand to define a new macro in the preamble (just before 
> \begin{document})
> - use the newly defined macro at point (using TeX-insert-macro)
> 
> I cannot just use (TeX-insert-macro "newcommand") followed by another
> (TeX-insert-macro newmacro) because I don't know which macro was just
> created by (TeX-insert-macro "newcommand") : it asks for the name
> interactively.
> 
> This is what I do now.
> 
> (defun LaTeX-create-newcommand (symbol)
>  "Define a new macro with \\newcommand in the preamble, and use it at 
> point"
>  (interactive "sNew command name : \\")
>  (save-excursion
>    (goto-char (point-min))
>    (re-search-forward "\\\\begin{document}")
>    (beginning-of-line)
>    (insert TeX-esc "newcommand" TeX-grop TeX-esc symbol TeX-grcl)
>    (TeX-parse-arguments '(["Number of arguments"] ["Default value for 
> first argument"]))
> 
>    (let
>       ((TeX-arg-closing-brace TeX-grcl) (TeX-arg-opening-brace TeX-
> grop))
>     (TeX-argument-insert (read-string "Definition of the macro (use #1, 
> #2, etc. for arguments): ") nil)
>      )
>    (TeX-normal-mode nil)
>    (newline)
>    )
>  (TeX-insert-macro symbol)
>  )

> It somehow works, but I thought I would ask for comments and
> suggestions. One thing I'd like to know : if I use this function then
> change my mind, I wish saying "M-x undo" just once would make the
> whole thing disappear, but that won't work. How should I do that ?

I think that is, unfortunately, a hard problem or at least one that doesn't get 
much attention (that I have found).  I have something that seems to work, but 
it's probably fragile and might mess up the undo information of the buffer.  I 
have used it a couple times without any obvious adverse effects.  I don't use 
any "undo hacks" which might interfere.  I don't mean to scare you unduly, but 
this sort of thing seems quite hackish.

-Ivan

(defun LaTeX-create-newcommand (symbol)
  "Define a new macro with \\newcommand in the preamble, and use it at point"
  (interactive "sNew command name : \\")
  (let ((orig-buffer-undo-list buffer-undo-list))
    (undo-boundary)
    (save-excursion
      (goto-char (point-min))
      (re-search-forward "\\\\begin{document}")
      (beginning-of-line)
      (insert TeX-esc "newcommand" TeX-grop TeX-esc symbol TeX-grcl)
      (TeX-parse-arguments '(["Number of arguments"] ["Default value for first 
argument"]))
      (let ((TeX-arg-closing-brace TeX-grcl)
            (TeX-arg-opening-brace TeX-grop))
        (TeX-argument-insert (read-string "Definition of the macro (use #1, #2, 
etc. for arguments): ") nil))
      (newline)
      (TeX-normal-mode nil))
    (TeX-insert-macro symbol)
    ;; Make it one "undo group" -- this might be fragile
    (let ((head buffer-undo-list))
      (while (and head
                  (not (eq head orig-buffer-undo-list))
                  (not (eq (cdr head) orig-buffer-undo-list)))
        (if (null (cadr head))
            (setcdr head (cddr head))
          (setq head (cdr head)))))
    (undo-boundary)))




reply via email to

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