Function: backtrace--reformat-sexp

backtrace--reformat-sexp is a byte-compiled function defined in backtrace.el.gz.

Signature

(backtrace--reformat-sexp FORMAT-FUNCTION)

Documentation

Reformat the top level sexp at point.

Locate the top level sexp at or following point on the same line, and reformat it with FORMAT-FUNCTION, preserving the location of point within the sexp. If no sexp is found before the end of the line or buffer, signal an error.

FORMAT-FUNCTION will be called without arguments, with the current buffer set to a temporary buffer containing only the content of the sexp.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/backtrace.el.gz
(defun backtrace--reformat-sexp (format-function)
  "Reformat the top level sexp at point.
Locate the top level sexp at or following point on the same line,
and reformat it with FORMAT-FUNCTION, preserving the location of
point within the sexp.  If no sexp is found before the end of
the line or buffer, signal an error.

FORMAT-FUNCTION will be called without arguments, with the
current buffer set to a temporary buffer containing only the
content of the sexp."
  (let* ((orig-pos (point))
         (pos (point))
         (tag (backtrace-get-form pos))
         (end (next-single-property-change pos 'backtrace-form))
         (begin (previous-single-property-change end 'backtrace-form
                                                 nil (point-min))))
    (unless tag
      (when (or (= end (point-max)) (> end (line-end-position)))
        (user-error "No form here to reformat"))
      (goto-char end)
      (setq pos end
            end (next-single-property-change pos 'backtrace-form)
            begin (previous-single-property-change end 'backtrace-form
                                                   nil (point-min))))
    (let* ((offset (when (>= orig-pos begin) (- orig-pos begin)))
           (offset-marker (when offset (make-marker)))
           (content (buffer-substring begin end))
           (props (backtrace-get-text-properties begin))
           (inhibit-read-only t))
      (delete-region begin end)
      (insert (with-temp-buffer
                (insert content)
                (when offset
                  (set-marker-insertion-type offset-marker t)
                  (set-marker offset-marker (+ (point-min) offset)))
                (funcall format-function)
                (when offset
                  (setq offset (- (marker-position offset-marker) (point-min))))
                (buffer-string)))
      (when offset
        (set-marker offset-marker (+ begin offset)))
      (save-excursion
        (goto-char begin)
        (indent-sexp))
      (add-text-properties begin (point) props)
      (if offset
          (goto-char (marker-position offset-marker))
        (goto-char orig-pos)))))