Function: sql-interactive-remove-continuation-prompt

sql-interactive-remove-continuation-prompt is a byte-compiled function defined in sql.el.gz.

Signature

(sql-interactive-remove-continuation-prompt OLINE)

Documentation

Strip out continuation prompts out of the OLINE.

Added to the comint-preoutput-filter-functions hook in a SQL interactive buffer. The complication to this filter is that the continuation prompts may arrive in multiple chunks. If they do, then the function saves any unfiltered output in a buffer and prepends that buffer to the next chunk to properly match the broken-up prompt.

The filter goes into play only if something is already accumulated, or we're waiting for continuation prompts (sql-output-newline-count is positive). In this case:
- Accumulate process output into sql-preoutput-hold.
- Remove any complete prompts / continuation prompts that we're waiting
  for.
- In case we're expecting more prompts - return all currently
  accumulated _complete_ lines, leaving the rest for the next
  invocation. They will appear in the output immediately. This way we
  don't accumulate large chunks of data for no reason.
- If we found all expected prompts - just return all current accumulated
  data.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/sql.el.gz
(defun sql-interactive-remove-continuation-prompt (oline)
  "Strip out continuation prompts out of the OLINE.

Added to the `comint-preoutput-filter-functions' hook in a SQL
interactive buffer.  The complication to this filter is that the
continuation prompts may arrive in multiple chunks.  If they do,
then the function saves any unfiltered output in a buffer and
prepends that buffer to the next chunk to properly match the
broken-up prompt.

The filter goes into play only if something is already
accumulated, or we're waiting for continuation
prompts (`sql-output-newline-count' is positive).  In this case:
- Accumulate process output into `sql-preoutput-hold'.
- Remove any complete prompts / continuation prompts that we're waiting
  for.
- In case we're expecting more prompts - return all currently
  accumulated _complete_ lines, leaving the rest for the next
  invocation.  They will appear in the output immediately.  This way we
  don't accumulate large chunks of data for no reason.
- If we found all expected prompts - just return all current accumulated
  data."
  (when (and comint-prompt-regexp
             ;; We either already have something held, or expect
             ;; prompts
             (or sql-preoutput-hold
                 (and sql-output-newline-count
                      (> sql-output-newline-count 0))))
    (save-match-data
      ;; Add this text to what's left from the last pass
      (setq oline (concat sql-preoutput-hold oline)
            sql-preoutput-hold nil)

      ;; If we are looking for prompts
      (when (and sql-output-newline-count
                 (> sql-output-newline-count 0))
        ;; Loop thru each starting prompt and remove it
        (while (and (not (string-empty-p oline))
                    (> sql-output-newline-count 0)
                    (string-match comint-prompt-regexp oline))
          (setq oline (replace-match "" nil nil oline)
                sql-output-newline-count (1- sql-output-newline-count)))

        ;; If we've found all the expected prompts, stop looking
        (if (= sql-output-newline-count 0)
            (setq sql-output-newline-count nil)
          ;; Still more possible prompts, leave them for the next pass
          (setq sql-preoutput-hold oline
                oline "")))

      ;; Lines that are now complete may be passed further
      (when sql-preoutput-hold
        (let ((last-nl 0))
          (while (string-match "\n" sql-preoutput-hold last-nl)
            (setq last-nl (match-end 0)))
          ;; Return up to last nl, hold after the last nl
          (setq oline (substring sql-preoutput-hold 0 last-nl)
                sql-preoutput-hold (substring sql-preoutput-hold last-nl))
          (when (string-empty-p sql-preoutput-hold)
            (setq sql-preoutput-hold nil))))))
  oline)