Ihor Radchenko writes: > Nathaniel Nicandro writes: > >>> Ideally, fontifying ANSI sequences should be fully controlled by users: >>> 1. We may not want to touch src blocks by default, when >>> `org-src-fontify-natively' is set to t. Only, maybe, provide an >>> option. Or you may better publish a minor mode that does this for >>> shell scripts. >>> 2. We may allow all the ANSI sequences to be fontified in the whole >>> buffer. >> >> I've updated my patch to be a combination of (1) and (2), see the >> attached patch. Essentially every sequence is fontified except those in >> source blocks and a minor mode has been created to allow users to >> disable or enable fontification whenever they want. >> >> I've also attached an example Org file with some ANSI sequences in it >> for testing purposes that you can try out. > > Thanks! > >> One issue that remains is how to handle sequences within inline source >> blocks. Those don't have a src-block property so any sequences within >> an inline source block are currently fontified. > > You should not use 'src-block property at all. There are scenarios when > jit-lock defers source block fontification (in particular, when source > block spans beyond the screen) and 'src-block property is not yet > applied. > > Instead, you should query `org-element-at-point' or > `org-element-context'. The attached patch now uses `org-element-at-point' and `org-element-context' to query for the bounds of elements. Note, I've also attached an updated example file which shows that the escape sequences in inline source blocks are now handled similarly to regular source blocks, i.e. they are not fontified. > >> +*** ANSI escape sequences are now highlighted in the whole buffer >> + >> +A new customization ~org-fontify-ansi-sequences~ is available which >> +tells Org to highlight all ANSI sequences in the buffer if non-nil and >> +the new minor mode ~org-ansi-mode~ is enabled. >> + >> +To disable highlighting of the sequences you can either >> +disable ~org-ansi-mode~ or set ~org-fontify-ansi-sequences~ to ~nil~ >> +and =M-x revert-buffer RET=. Doing the latter will disable >> +highlighting of sequences in all newly opened Org buffers whereas >> +doing the former disables highlighting locally to the current buffer. > > Rather than asking to use revert-buffer, we usually suggest M-x > org-mode-restart. Done. > >> +(defun org-fontify-ansi-sequences (limit) >> + "Fontify ANSI sequences." >> + (when (and org-fontify-ansi-sequences org-ansi-mode) >> + (let (end) >> + (while (/= (point) limit) > > Instead of this strict condition and later juggle with > `narrow-to-region', just use the usual (while (< (point) limit) ...). > Done. >> + (cond >> + ((get-text-property (point) 'src-block) > > As I mentioned above, please use `org-element-at-point'. This function > will also give you information about the block boundaries. > >> + (ansi-color-apply-on-region (point) end t) > > We should probably limit ANSI colour pairs to a single Org element. It > does not make much sense to have text in-between the quotes below > coloured: > > #+begin_quote > ... ... > #+end_quote > > > .... > > #+begin_quote > ... ... > #+end_quote > Makes sense. Done. >> + ;; Reset the context before every fontification cycle. This >> + ;; avoids issues where `ansi-color-apply-on-region' attempts to >> + ;; use an old starting point that may be from a different part >> + ;; of the buffer, leading to "wrong side of point" errors. >> + (setq ansi-color-context-region nil) > > This looks fragile. AFAIU, `ansi-color-context-region' is used to track > currently active ANSI colour settings. Since your fontification function > may be called with various LIMITs, depending on what is displayed on the > user screen, the fontification results might be unpredictable for ANSI > defs spanning across multiple screens. > It seems to be safe to reset `ansi-color-context-region' now given that org-element is used to find the bounds of the element at `point'. Although the fontification limits are dependent on screen size, the org-element functions are not and so the bounds used when applying the fontification for the ANSI sequences won't depend on screen size either. Also, re-setting `ansi-color-context-region' has the effect of not propagating previously applied color settings to other Org elements. >> +(defvar org-ansi-colors >> + '(black red green yellow blue purple cyan white)) >> + >> +(defun org-ansi-highlight (beg end seq) >> + (save-excursion >> + (goto-char end) >> + (insert "\e") >> + (insert "[0m") >> + (goto-char beg) >> + (insert "\e") >> + (insert (format "[%sm" seq)))) >> + >> +(defun org-ansi-highlight-foreground (beg end color) >> + "Highlight the foreground between BEG and END with COLOR." >> + (interactive >> + (let ((bounds (car (region-bounds)))) >> + (list (car bounds) (cdr bounds) >> + (completing-read "Color: " org-ansi-colors nil t)))) >> + (let ((n (- (length org-ansi-colors) >> + (length (memq color org-ansi-colors))))) >> + (org-ansi-highlight beg end (+ 30 n)))) >> + >> +(defun org-ansi-highlight-background (beg end color) >> + "Highlight the background between BEG and END with COLOR." >> + (interactive >> + (let ((bounds (car (region-bounds)))) >> + (list (car bounds) (cdr bounds) >> + (completing-read "Color: " org-ansi-colors nil t)))) >> + (let ((n (- (length org-ansi-colors) >> + (length (memq color org-ansi-colors))))) >> + (org-ansi-highlight beg end (+ 40 n)))) > > The above has no relation to fontification and does not belong to Org in > general. Org syntax has no notion of ANSI escapes. We may support them > as a useful feature, but no more. Editing ANSI escapes would make more > sense in shell-script-mode or similar. Removed. > >> + :lighter " OANSI" >> + (cond >> + ((and org-fontify-ansi-sequences org-ansi-mode) >> + (remove-text-properties (point-min) (point-max) '(fontified t)) >> + (font-lock-ensure)) > > Just use `org-restart-font-lock'. > Thanks. Done. >> + (t >> + (dolist (ov (overlays-in (point-min) (point-max))) >> + ;; Attempt to find ANSI specific overlays. See >> + ;; `ansi-color-make-extent'. >> + (when (eq (car-safe (overlay-get ov 'insert-behind-hooks)) >> + 'ansi-color-freeze-overlay) > > This is extremely awkward and relies on internal implementation details > of ansi-color. Moreover, we must avoid overlays, if possible - they do > not scale well. I recommend re-defining `ansi-color-apply-face-function' > to something that uses text properties. Using text properties will also > make restarting font-lock sufficient to clear the fontification. I've re-defined `ansi-color-apply-face-function' as you've said.