Function: ConTeXt-insert-environment

ConTeXt-insert-environment is a byte-compiled function defined in context.el.

Signature

(ConTeXt-insert-environment ENVIRONMENT &optional EXTRA)

Documentation

Insert ConTeXt ENVIRONMENT with optional argument EXTRA.

Source Code

;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/context.el
;;; Copy and adaptation of `LaTeX-insert-environment'.  (2022-08-13)
(defun ConTeXt-insert-environment (environment &optional extra)
  "Insert ConTeXt ENVIRONMENT with optional argument EXTRA."
  (let ((active-mark (and (TeX-active-mark) (not (eq (mark) (point)))))
        content-start env-start env-end additional-indent)
    (when (and active-mark (< (mark) (point))) (exchange-point-and-mark))
    ;; What to do with the line containing point.
    ;; - Open a new empty line for later insertion of "\startfoo" and
    ;;   put the point there.
    ;; - If there were at first any non-whitespace texts between the
    ;;   point and EOL, send them into their new own line.
    (cond (;; When the entire line consists of whitespaces...
           (save-excursion (beginning-of-line)
                           (looking-at "[ \t]*$"))
           ;; ...make the line empty and put the point there.
           (delete-region (match-beginning 0) (match-end 0)))
          (;; When there are only whitespaces between the point and
           ;; BOL (including the case the point is at BOL)...
           (TeX-looking-at-backward "^[ \t]*"
                                    (line-beginning-position))
           ;; ...in this case, we have non-whitespace texts between
           ;; the point and EOL, so send the entire line into a new
           ;; next line and put the point on the empty line just
           ;; created.
           (beginning-of-line)
           (newline)
           (beginning-of-line 0)
           ;; Take note that there are texts to be indented later
           ;; unless the region is activated.
           (unless active-mark
             (setq additional-indent t)))
          (;; In all other cases...
           t
           ;; ...insert a new empty line after deleting all
           ;; whitespaces around the point, put the point there...
           (delete-horizontal-space)
           (if (eolp)
               (newline)
             ;; ...and if there were at first any non-whitespace texts
             ;; between (the original position of) the point and EOL,
             ;; send them into a new next line.
             (newline 2)
             (beginning-of-line 0)
             ;; Take note that there are texts to be indented later
             ;; unless the region is activated.
             (unless active-mark
               (setq additional-indent t)))))
    ;; What to do with the line containing mark.
    ;; If there is active region...
    (when active-mark
      ;; - Open a new empty line for later insertion of "\stopfoo"
      ;;   and put the mark there.
      ;; - If there were at first any non-whitespace texts between the
      ;;   mark and EOL, pass them over the empty line and put them on
      ;;   their own line.
      (save-excursion
        (goto-char (mark))
        (cond (;; When the entire line consists of whitespaces...
               (save-excursion (beginning-of-line)
                               (looking-at "[ \t]*$"))
               ;; ...make the line empty and put the mark there.
               (delete-region (match-beginning 0) (match-end 0)))
              (;; When there are only whitespaces between the mark and
               ;; BOL (including the case the mark is at BOL)...
               (TeX-looking-at-backward "^[ \t]*"
                                        (line-beginning-position))
               ;; ...in this case, we have non-whitespace texts
               ;; between the mark and EOL, so send the entire line
               ;; into a new next line and put the mark on the empty
               ;; line just created.
               (beginning-of-line)
               (set-mark (point))
               (newline)
               ;; Take note that there are texts to be indented later.
               (setq additional-indent t))
              (;; In all other cases...
               t
               ;; ...make a new empty line after deleting all
               ;; whitespaces around the mark, put the mark there...
               (delete-horizontal-space)
               (insert-before-markers "\n")
               ;; ...and if there were at first any non-whitespace
               ;; texts between (the original position of) the mark
               ;; and EOL, send them into a new next line.
               (unless (eolp)
                 (newline)
                 ;; Take note that there are texts to be indented
                 ;; later.
                 (setq additional-indent t))))))
    ;; Now insert the environment.
    (setq env-start (point))
    (insert TeX-esc (ConTeXt-environment-start-name) environment)
    (indent-according-to-mode)
    (when extra (insert extra))
    (setq content-start (line-beginning-position 2))
    (unless active-mark
      (newline)
      (newline))
    (when active-mark (goto-char (mark)))
    (insert TeX-esc (ConTeXt-environment-stop-name) environment)
    (end-of-line 0)
    (if active-mark
        (progn
          ;; TODO: Do filling when context.el obtains
          ;; `ConTeXt-fill-region' in future.
          (indent-region content-start (line-beginning-position 2))
          (set-mark content-start))
      (indent-according-to-mode))
    ;; Indent \stopfoo.
    (save-excursion (beginning-of-line 2) (indent-according-to-mode)
                    (when additional-indent
                      ;; Indent texts sent after the inserted
                      ;; environment.
                      (forward-line 1) (indent-according-to-mode)))
    (setq env-end (save-excursion
                    (search-forward
                     (concat TeX-esc (ConTeXt-environment-stop-name)
                             environment))
                    (match-beginning 0)))
    (run-hook-with-args 'ConTeXt-after-insert-env-hook
                        environment env-start env-end)))