auctex
[Top][All Lists]
Advanced

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

[AUCTeX] More on LateX-mark-environement


From: Vincent Belaïche
Subject: [AUCTeX] More on LateX-mark-environement
Date: Sun, 28 Jun 2009 00:28:07 +0200

Dear Ralf,

I would like to contribute again on LaTeX-mark-environment. I find it
practicle if an  argument <= 0 can be used to count environements from
top downwards.

Here is my contribution (its seems to be more than 11 lines, I can fill
in the GNU form if need be):

Best regards,
   Vincent.

;;---------------------------------------------------------------------
(defun LaTeX-mark-environment (&optional count)
  "Set mark to end of current environment and point to the matching begin.
If prefix argument COUNT is given and positive, mark the
respective number of enclosing environments starting from point
and counting outwards. If prefix argument COUNT is given and
negative or null, then a number 1-COUNT of environment is marked
starting from point-min/point-max and counting inwards.  The
command will not work properly if there are unbalanced begin-end
pairs in comments and verbatim environments."
  (interactive "p")
  (setq count (or count 1))
  (let ((cur (point)) beg end)
    (if (> count 0)
        ;; Only change point and mark after beginning and end were found.
        ;; Point should not end up in the middle of nowhere if the search
        ;; fails.
        (save-excursion
          (dotimes (c count) (LaTeX-find-matching-end))
          (setq end (line-beginning-position 2))
          (goto-char cur)
          (dotimes (c count) (LaTeX-find-matching-begin))
          (setq beg (point)))
      (setq count (- count))
      (let (outest-beg outest-end begs ends env-type env-count)
        (save-excursion
          (goto-char (point-min))
          (while (progn
                   (unless 
                       (re-search-forward (concat (regexp-quote TeX-esc)
                                                "begin"
                                                (regexp-quote TeX-grop)
                                                " *\\([a-zA-Z*]*\\)"
                                                (regexp-quote TeX-grcl))
                                        nil t)
                   (error "Can'tr find outest begin"))
                 (TeX-in-commented-line)))
        (setq outest-beg (match-beginning 0)
              env-type (match-string 1))
        (goto-char (point-max))
        (while (progn
                 (unless 
                     (re-search-backward (concat (regexp-quote TeX-esc)
                                                "end"
                                                (regexp-quote TeX-grop)
                                                " *"
                                                (regexp-quote 
                                                 (concat env-type TeX-grcl)))
                                        nil t)
                   (error "Can'tr find outest end matching outest begin"))
                 (TeX-in-commented-line)))
        (setq outest-end (match-end 0))
        (goto-char cur)
        (while (progn
                 (LaTeX-find-matching-begin)
                 (setq beg (point))
                 (push beg begs)
                 (> beg outest-beg)))
        (goto-char cur)
        (while (progn
                 (LaTeX-find-matching-end)
                 (setq end (line-beginning-position 2))
                 (push end ends)
                 (< end outest-end)))
        (setq env-count (length begs))
        (unless (= env-count (length ends))
          (error "Unbalanced begin/end"))
        (unless (< count env-count)
          (error "Invalid environment count"))
        (setq beg (nth count begs)
              end (nth count  ends)))))
    (set-mark end)
    (goto-char beg)
    (TeX-activate-region)))





reply via email to

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