mirror of
https://github.com/vale981/emacs-jupyter
synced 2025-03-04 15:41:37 -05:00
Remove jupyter-ansi-color-apply-on-region
The only modification compared to `ansi-color-apply-on-region` was that it did not call `delete-and-extract-region` on the region containing ANSI escapes, instead the ANSI escaped were rendered invisible. This can be done by `let` binding `delete-and-extract-region` to a custom function while calling `ansi-color-apply-on-region` instead. The downside is that we are now depending on an implementation detail of `ansi-color-apply-on-region`.
This commit is contained in:
parent
53da538b66
commit
c9016ee143
2 changed files with 17 additions and 64 deletions
|
@ -186,69 +186,6 @@ function."
|
|||
(kill-buffer)))
|
||||
(funcall restore-mode)))
|
||||
|
||||
;;; Special handling of ANSI sequences
|
||||
|
||||
(defun jupyter-ansi-color-apply-on-region (begin end)
|
||||
"`ansi-color-apply-on-region' with Jupyter specific modifications.
|
||||
In particular, does not delete escape sequences between BEGIN and
|
||||
END from the buffer. Instead, an invisible text property with a
|
||||
value of t is added to render the escape sequences invisible.
|
||||
Also, the `ansi-color-apply-face-function' is hard-coded to a
|
||||
custom function that prepends to the face property of the text
|
||||
and also sets the font-lock-face to the prepended face.
|
||||
|
||||
For convenience, a jupyter-invisible property is also added with
|
||||
a value of t. This is mainly for modes like `org-mode' which
|
||||
strip invisible properties during fontification. In such cases,
|
||||
the jupyter-invisible property can act as an alias to the
|
||||
invisible property by adding it to `char-property-alias-alist'."
|
||||
(let ((codes (car ansi-color-context-region))
|
||||
(start-marker (or (cadr ansi-color-context-region)
|
||||
(copy-marker begin)))
|
||||
(end-marker (copy-marker end))
|
||||
(ansi-color-apply-face-function
|
||||
(lambda (beg end face)
|
||||
(when face
|
||||
(setq face (list face))
|
||||
(font-lock-prepend-text-property beg end 'face face)
|
||||
(put-text-property beg end 'font-lock-face face)))))
|
||||
(save-excursion
|
||||
(goto-char start-marker)
|
||||
;; Find the next escape sequence.
|
||||
(while (re-search-forward ansi-color-control-seq-regexp end-marker t)
|
||||
;; Remove escape sequence.
|
||||
(let ((esc-seq (prog1 (buffer-substring-no-properties
|
||||
(match-beginning 0) (point))
|
||||
;; FIXME: Not removing escape sequences adds in a lot
|
||||
;; of invisible characters that slows down Emacs on
|
||||
;; large ANSI coded regions and seems mostly related
|
||||
;; to redisplay since hiding the region behind an
|
||||
;; invisible overlay removes the slowdown.
|
||||
(add-text-properties
|
||||
(match-beginning 0) (point)
|
||||
'(invisible t jupyter-invisible t)))))
|
||||
;; Colorize the old block from start to end using old face.
|
||||
(funcall ansi-color-apply-face-function
|
||||
(prog1 (marker-position start-marker)
|
||||
;; Store new start position.
|
||||
(set-marker start-marker (point)))
|
||||
(match-beginning 0) (ansi-color--find-face codes))
|
||||
;; If this is a color sequence,
|
||||
(when (eq (aref esc-seq (1- (length esc-seq))) ?m)
|
||||
;; update the list of ansi codes.
|
||||
(setq codes (ansi-color-apply-sequence esc-seq codes)))))
|
||||
;; search for the possible start of a new escape sequence
|
||||
(if (re-search-forward "\033" end-marker t)
|
||||
(progn
|
||||
;; if the rest of the region should have a face, put it there
|
||||
(funcall ansi-color-apply-face-function
|
||||
start-marker end-marker (ansi-color--find-face codes))
|
||||
(setq ansi-color-context-region (if codes (list codes))))
|
||||
;; if the rest of the region should have a face, put it there
|
||||
(funcall ansi-color-apply-face-function
|
||||
start-marker end-marker (ansi-color--find-face codes))
|
||||
(setq ansi-color-context-region (if codes (list codes)))))))
|
||||
|
||||
;;; `jupyter-insert' method
|
||||
|
||||
(cl-defgeneric jupyter-insert (_mime _data &optional _metadata)
|
||||
|
|
|
@ -588,7 +588,23 @@ property."
|
|||
(setq begin1 next)))
|
||||
(t
|
||||
(put-text-property begin next 'jupyter-ansi t)
|
||||
(jupyter-ansi-color-apply-on-region begin next)
|
||||
(cl-letf (((symbol-function #'delete-and-extract-region)
|
||||
(lambda (beg end)
|
||||
(prog1 (buffer-substring-no-properties beg end)
|
||||
;; FIXME: Not removing escape sequences adds in a lot of
|
||||
;; invisible characters that slows down Emacs on large
|
||||
;; ANSI coded regions and seems mostly related to
|
||||
;; redisplay since hiding the region behind an invisible
|
||||
;; overlay removes the slowdown.
|
||||
(add-text-properties
|
||||
beg end '(invisible t jupyter-invisible t)))))
|
||||
(ansi-color-apply-face-function
|
||||
(lambda (beg end face)
|
||||
(when face
|
||||
(setq face (list face))
|
||||
(font-lock-prepend-text-property beg end 'face face)
|
||||
(put-text-property beg end 'font-lock-face face)))))
|
||||
(ansi-color-apply-on-region begin next))
|
||||
(setq begin next))))))
|
||||
|
||||
;; Adapted from `org-fontify-meta-lines-and-blocks-1'
|
||||
|
|
Loading…
Add table
Reference in a new issue