Note: this question led to the inclusion of the following fix in AUCTeX. It'll be available in the ELPA version 11.90.3 and the stable version 11.91.
Warning: this answer does almost what you asked.
Add the following function definition to your init file:
(with-eval-after-load "context"
(defun ConTeXt-find-indent (&optional virtual)
"Find the proper indentation of text after point.
VIRTUAL if non-nil indicates that we're only trying to find the
indentation in order to determine the indentation of something
else. There might be text before point."
(save-excursion
(skip-chars-forward " \t")
(or
;; Trust the current indentation, if such info is applicable.
(and virtual (>= (current-indentation) (current-column))
(current-indentation))
;; Put leading close-paren where the matching open brace would be.
(condition-case nil
(and (eq (char-syntax (char-after)) ?\))
(save-excursion
(skip-syntax-forward " )")
(backward-sexp 1)
(ConTeXt-find-indent 'virtual)))
(error nil))
;; Default (maybe an argument)
(let ((pos (point))
(char (char-after))
(indent 0)
up-list-pos)
;; Look for macros to be outdented
(cond ((looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-stop-name)))
(setq indent (- indent ConTeXt-indent-basic)))
((looking-at ConTeXt-indent-item-re)
(setq indent (- indent ConTeXt-indent-item))))
;; Find the previous point which determines our current indentation.
(condition-case err
(progn
(backward-sexp 1)
(while (or (> (current-column) (current-indentation))
;; Continue going back if we are
;; at a hanging optional group.
(looking-at (regexp-quote ConTeXt-optop)))
(backward-sexp 1)))
(scan-error
(setq up-list-pos (nth 2 err))))
(cond
((= (point-min) pos) 0) ; We're really just indenting the first line.
((integerp up-list-pos)
;; Have to indent relative to the open-paren.
(goto-char up-list-pos)
(if (and (not ConTeXt-indent-allhanging)
(> pos (progn (down-list 1)
(forward-comment (point-max))
(point))))
;; Align with the first element after the open-paren.
(current-column)
;; We're the first element after a hanging brace.
(goto-char up-list-pos)
(+ indent ConTeXt-indent-basic (ConTeXt-find-indent 'virtual))))
;; We're now at the "beginning" of a line.
((not (and (not virtual) (eq (char-after) ?\\)))
;; Nothing particular here: just keep the same indentation.
(+ indent (current-column)))
;; We're now looking at an item.
((looking-at ConTeXt-indent-item-re)
;; Indenting relative to an item, have to re-add the outdenting.
(+ indent (current-column) ConTeXt-indent-item))
;; We're looking at an environment starter.
((and (looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)))
(not (looking-at (concat (regexp-quote TeX-esc)
(ConTeXt-environment-start-name)
ConTeXt-text)))) ; other environments?
(+ indent (current-column) ConTeXt-indent-basic))
(t
(let ((col (current-column)))
(if (not (and char (eq (char-syntax char) ?\()))
;; If the first char was not an open-paren, there's
;; a risk that this is really not an argument to the
;; macro at all.
(+ indent col)
(forward-sexp 1)
(if (< (line-end-position)
(save-excursion (forward-comment (point-max))
(point)))
;; we're indenting the first argument.
(min (current-column) (+ ConTeXt-indent-arg col))
(skip-syntax-forward " ")
(current-column)))))))))))
This is a fixed version of the indenting function provided by AUCTeX. There is only a minor change (the (looking-at (regexp-quote ConTeXt-optop))
line) to adjust indentation after [...]
.
Now your original code is indented as
\setuphead
[section]
[before={\blank[big]},
after={\blank[medium]}]
\setuphead
[subsection]
[
before={\blank[big]},
after={\blank[medium]},
]
The only thing that I couldn't fix was indentation of after={\blank[medium]}]
line, but for multiline settings inside [...]
I'd suggest you to do like you did in the second case.
Please, tell me if you find this solution useful in the long run, so that I can install this change upstream.
[section]
is in the same line as\setuphead
, then indentation works correctly. I.e.,\setuphead[section][
and then you break the line here. I followed the example code at: wiki.contextgarden.net/Command/setuphead. Maybe this is intended to work this way? – giordano Jan 20 '17 at 13:32