Function: yank-in-context--transform

yank-in-context--transform is a byte-compiled function defined in simple.el.gz.

Signature

(yank-in-context--transform STRING)

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun yank-in-context--transform (string)
  (let ((ppss (syntax-ppss)))
    (cond
     ;; We're in a string.
     ((ppss-string-terminator ppss)
      (string-replace
       (string (ppss-string-terminator ppss))
       (concat (if (functionp escaped-string-quote)
                   (funcall escaped-string-quote
                            (ppss-string-terminator ppss))
                 escaped-string-quote)
               (string (ppss-string-terminator ppss)))
       string))
     ;; We're in a comment.
     ((or (ppss-comment-depth ppss)
          (and (bolp)
               (not (eobp))
               ;; If we're in the middle of a bunch of commented text,
               ;; we probably want to be commented.  This is quite DWIM.
               (or (bobp)
                   (save-excursion
                     (forward-line -1)
                     (forward-char 1)
                     (ppss-comment-depth (syntax-ppss))))
               (ppss-comment-depth
                (setq ppss (save-excursion
                             (forward-char 1)
                             (syntax-ppss))))))
      (cond
       ((and (eq (ppss-comment-depth ppss) t)
             (> (length comment-end) 0)
             (string-search comment-end string))
        (user-error "Can't insert a string containing a comment terminator in a comment"))
       ;; If this is a comment syntax that has an explicit end, then
       ;; we can just insert as is.
       ((> (length comment-end) 0) string)
       ;; Line-based comment formats.
       ((or (string-search "\n" string)
            (bolp))
        (let ((mode major-mode)
              (bolp (bolp))
              (eolp (eolp))
              (comment-style 'plain))
          (with-temp-buffer
            (funcall mode)
            (insert string)
            (when (string-match-p "\n\\'" string)
              (cond
               ((not eolp) (delete-char -1))
               (bolp (insert "\n"))))
            (comment-normalize-vars)
            (comment-region-default-1
             (if bolp
                 (point-min)
               (save-excursion
                 (goto-char (point-min))
                 (forward-line 1)
                 (point)))
             (point-max))
            (buffer-string))))
       (t string)))
     (t string))))