598 |
|
|
599 |
(defun LaTeX-insert-environment (environment &optional extra) |
(defun LaTeX-insert-environment (environment &optional extra) |
600 |
"Insert LaTeX ENVIRONMENT with optional argument EXTRA." |
"Insert LaTeX ENVIRONMENT with optional argument EXTRA." |
601 |
(let ((active-mark (and (TeX-active-mark) |
(let ((active-mark (and (TeX-active-mark) (not (eq (mark) (point))))) |
602 |
(not (eq (mark) (point))))) |
(macrocode-p (and (eq major-mode 'doctex-mode) |
603 |
comment-flag prefix) |
(string-match "\\`macrocode\\*?\\'" environment))) |
604 |
(when active-mark |
prefix content-start) |
605 |
(if (< (mark) (point)) |
(when (and active-mark (< (mark) (point))) (exchange-point-and-mark)) |
606 |
(exchange-point-and-mark))) |
;; Compute the prefix. |
607 |
(unless (TeX-looking-at-backward |
(when (and LaTeX-insert-into-comments (TeX-in-commented-line)) |
608 |
(if (and LaTeX-insert-into-comments |
(save-excursion |
609 |
(TeX-in-commented-line) |
(beginning-of-line) |
610 |
(not (bolp))) |
(looking-at |
611 |
(concat "^[ \t]*" TeX-comment-start-regexp "+[ \t]*") |
(concat "^\\([ \t]*" TeX-comment-start-regexp "+\\)+[ \t]*")) |
612 |
"^[ \t]*")) |
(setq prefix (match-string 0)))) |
613 |
(LaTeX-newline)) |
;; What to do with the line containing point. |
614 |
;; Insert a linebreak at the end of the marked region if necessary. |
(cond ((save-excursion (beginning-of-line) |
615 |
;; Do this before we insert anything which might alter the prefix. |
(looking-at (concat prefix "[ \t]*$"))) |
616 |
|
(kill-region (match-beginning 0) (match-end 0))) |
617 |
|
((TeX-looking-at-backward (concat "^" prefix "[ \t]*") |
618 |
|
(line-beginning-position)) |
619 |
|
(beginning-of-line) |
620 |
|
(newline) |
621 |
|
(beginning-of-line 0)) |
622 |
|
((bolp) |
623 |
|
(delete-horizontal-space) |
624 |
|
(newline) |
625 |
|
(beginning-of-line 0)) |
626 |
|
(t |
627 |
|
(delete-horizontal-space) |
628 |
|
(newline 2) |
629 |
|
(when prefix (insert prefix)) |
630 |
|
(beginning-of-line 0))) |
631 |
|
;; What to do with the line containing mark. |
632 |
(when active-mark |
(when active-mark |
633 |
(save-excursion |
(save-excursion |
634 |
(goto-char (mark)) |
(goto-char (mark)) |
635 |
(unless (progn (skip-chars-forward " \t") (eolp)) |
(cond ((save-excursion (beginning-of-line) |
636 |
;; Use `(insert "\n")' instead of `(newline)' because in contrast |
(or (looking-at (concat prefix "[ \t]*$")) |
637 |
;; to Emacs, XEmacs moves the mark if `newline' is used. |
(looking-at "[ \t]*$"))) |
638 |
(if (bolp) (insert "\n") (LaTeX-newline)) |
(kill-region (match-beginning 0) (match-end 0))) |
639 |
(indent-according-to-mode)))) |
((TeX-looking-at-backward (concat "^" prefix "[ \t]*") |
640 |
(when (and LaTeX-insert-into-comments |
(line-beginning-position)) |
641 |
(looking-at |
(beginning-of-line) |
642 |
(concat "[ \t]*\\(" TeX-comment-start-regexp "+\\)"))) |
(newline) |
643 |
(setq comment-flag t) |
(beginning-of-line 0)) |
644 |
(insert (setq prefix (match-string 1)) (TeX-comment-padding-string))) |
(t |
645 |
|
(delete-horizontal-space) |
646 |
|
(insert-before-markers "\n") |
647 |
|
(newline) |
648 |
|
(when prefix (insert prefix)))))) |
649 |
|
;; Now insert the environment. |
650 |
|
(if macrocode-p (insert "%") (when prefix (insert prefix))) |
651 |
(insert TeX-esc "begin" TeX-grop environment TeX-grcl) |
(insert TeX-esc "begin" TeX-grop environment TeX-grcl) |
652 |
(indent-according-to-mode) |
(indent-according-to-mode) |
653 |
(if extra (insert extra)) |
(when extra (insert extra)) |
654 |
(LaTeX-newline) |
(setq content-start (line-beginning-position 2)) |
655 |
(if active-mark |
(unless active-mark |
656 |
(progn |
(newline) |
657 |
(goto-char (mark)) |
(when prefix (insert prefix)) |
658 |
(unless (TeX-looking-at-backward |
(newline)) |
659 |
(if (and LaTeX-insert-into-comments |
(when active-mark (goto-char (mark))) |
660 |
(TeX-in-commented-line) |
(if macrocode-p (insert "%") (when prefix (insert prefix))) |
|
(not (bolp))) |
|
|
(concat "^" comment-start-skip "[ \t]*") |
|
|
"^[ \t]*")) |
|
|
(LaTeX-newline))) |
|
|
(LaTeX-newline)) |
|
|
(when comment-flag |
|
|
(insert prefix (TeX-comment-padding-string))) |
|
661 |
(insert TeX-esc "end" TeX-grop environment TeX-grcl) |
(insert TeX-esc "end" TeX-grop environment TeX-grcl) |
|
(indent-according-to-mode) |
|
662 |
(end-of-line 0) |
(end-of-line 0) |
663 |
(if active-mark |
(if active-mark |
664 |
(or (assoc environment LaTeX-indent-environment-list) |
(or (assoc environment LaTeX-indent-environment-list) |
665 |
(LaTeX-fill-environment nil)) |
(LaTeX-fill-region content-start (line-beginning-position 2))) |
666 |
(indent-according-to-mode))) |
(indent-according-to-mode)) |
667 |
|
(save-excursion (beginning-of-line 2) (indent-according-to-mode))) |
668 |
(TeX-math-input-method-off)) |
(TeX-math-input-method-off)) |
669 |
|
|
670 |
(defun LaTeX-modify-environment (environment) |
(defun LaTeX-modify-environment (environment) |