Hi Mosè,

Mosè Giordano <m...@gnu.org> writes:

> 2017-01-12 18:35 GMT+01:00 Arash Esbati <ar...@gnu.org>:
>> some packages like listings.sty have a label key since one cannot put
>> \label in the body of the environment.  I wrote a function which uses
>> the standard interface of AUCTeX and/or RefTeX for inserting a \label
>> and then moves the result into the optional argument.
> I like the problem you want to solve, am not sure your way is the most
> effective one to do it, though, see below.

As always, many thanks for checking and your comments.  Indeed, the code
and probably the idea of how it should work aren't nearly mature; I
started thinking about this feature after seeing this requirement in the
doc of breqn package.

> Some comments on the code:
> * I think that
> (and keyvals
>      (not (string= keyvals ""))
>      (save-excursion
>        (re-search-backward "caption[ \t\n\r%]*="
>                (save-excursion
>                  (re-search-backward
>                   (concat "\\\\begin{" env "}[ \t\n\r%]*\\[") nil t))
>                t))
>      (assoc env LaTeX-label-alist)
>      (save-excursion (LaTeX-label env 'environment)))
> can be simplified to
> (and (string-match "caption[ \t\n\r%]*=" keyvals)
>      (assoc env LaTeX-label-alist)
>      (save-excursion (LaTeX-label env 'environment)))
> or did you see corner cases where this wouldn't work?

Thanks.  This is the remainder of my first attempt in breqn.el.  But
yes, much easier.

> * I don't really like inserting the label and then deleting it.  How
> about adding an optional no-insert argument to `LaTeX-label', just
> like `reftex-label'?  `LaTeX-label' already returns the label, we only
> need not to insert it.

I agree, but I didn't dare to ask for changing `LaTeX-label'; that would
be great relief and the change is also easy:

--8<---------------cut here---------------start------------->8---
(defun LaTeX-label (name &optional type no-insert)
  "Insert a label for NAME at point.
The optional TYPE argument can be either environment or section:
in the former case this function looks up `LaTeX-label-alist' to
choose which prefix to use for the label, in the latter case
`LaTeX-section-label' will be looked up instead.  If TYPE is nil,
you will be always prompted for a label, with an empty default

If `LaTeX-label-function' is a valid function, LaTeX label will
transfer the job to this function.

If the optional NO-INSERT is non-nil, only the label is returned
and no insertion happens.  Otherwise the inserted label is
returned, nil if it is empty."
  (let ((TeX-read-label-prefix
          ((eq type 'environment)
           (cdr (assoc name LaTeX-label-alist)))
          ((eq type 'section)
           (if (assoc name LaTeX-section-list)
               (if (stringp LaTeX-section-label)
                 (and (listp LaTeX-section-label)
                      (cdr (assoc name LaTeX-section-label))))
          ((null type)
    (when (symbolp TeX-read-label-prefix)
      (setq TeX-read-label-prefix (symbol-value TeX-read-label-prefix)))
    (when TeX-read-label-prefix
      (if (and (boundp 'LaTeX-label-function)
               (fboundp LaTeX-label-function))
          (setq label (funcall LaTeX-label-function name))
        ;; Use completing-read as we do with `C-c C-m \label RET'
        (setq label (TeX-read-label t "What label" t))
        ;; No label or empty string entered?
        (if (or (string= TeX-read-label-prefix label)
                (string= "" label))
            (setq label nil)
          ;; <change>
          ;; If NO-INSERT, return only the label for further
          ;; utilization, otherwise insert \label{label} in the buffer
          (if no-insert
            (insert TeX-esc "label" TeX-grop label TeX-grcl)))
          ;; </change>
        (if label
              (LaTeX-add-labels label)
--8<---------------cut here---------------end--------------->8---

> * Why don't you use (save-excursion (LaTeX-find-matching-begin)
> (point)) to get the position of the beginning of current environment?
> * I'm not sure whether the markers should be erased before exiting
> (they're defined in a `let', they should automatically go away), but
> making them point nowhere doesn't harm.

Thanks.  If you agree on the change above, I will think about it more
thoroughly and cook up a new function which covers other cases as well.

Best, Arash

auctex-devel mailing list

Reply via email to