0

I have been on the agenda of actually applying the formatting done by various programs within various types of Emacs major modes, e. g., the compilation major mode. I found this and added this to my configuration file:

(use-package ansi-color
  :config
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  :hook (compilation-filter . my-colorize-compilation-buffer))

This got me most of the colours! Although every now and then I still see some uninterpreted (what looks like) escape codes. Here I show an example in compilation mode buffer *Compile-Log*,

Escape Code Example

What is this "^L" and how can I apply its effects as desired rather just printing out some representation of the actual code.

1 Answer 1

2

Emacs displays the control characters 0 through 31 with a caret followed by another character. This convention dates back to the early teletype machines which were adopted by the first computers as input and output devices.

You can find out what ^L represents by looking at the table of ASCII characters:

Dec Hex    Dec Hex    Dec Hex  Dec Hex  Dec Hex  Dec Hex   Dec Hex   Dec Hex
  0 00 NUL  16 10 DLE  32 20    48 30 0  64 40 @  80 50 P   96 60 `  112 70 p
  1 01 SOH  17 11 DC1  33 21 !  49 31 1  65 41 A  81 51 Q   97 61 a  113 71 q
  2 02 STX  18 12 DC2  34 22 "  50 32 2  66 42 B  82 52 R   98 62 b  114 72 r
  3 03 ETX  19 13 DC3  35 23 #  51 33 3  67 43 C  83 53 S   99 63 c  115 73 s
  4 04 EOT  20 14 DC4  36 24 $  52 34 4  68 44 D  84 54 T  100 64 d  116 74 t
  5 05 ENQ  21 15 NAK  37 25 %  53 35 5  69 45 E  85 55 U  101 65 e  117 75 u
  6 06 ACK  22 16 SYN  38 26 &  54 36 6  70 46 F  86 56 V  102 66 f  118 76 v
  7 07 BEL  23 17 ETB  39 27 '  55 37 7  71 47 G  87 57 W  103 67 g  119 77 w
  8 08 BS   24 18 CAN  40 28 (  56 38 8  72 48 H  88 58 X  104 68 h  120 78 x
  9 09 HT   25 19 EM   41 29 )  57 39 9  73 49 I  89 59 Y  105 69 i  121 79 y
 10 0A LF   26 1A SUB  42 2A *  58 3A :  74 4A J  90 5A Z  106 6A j  122 7A z
 11 0B VT   27 1B ESC  43 2B +  59 3B ;  75 4B K  91 5B [  107 6B k  123 7B {
 12 0C FF   28 1C FS   44 2C ,  60 3C <  76 4C L  92 5C \  108 6C l  124 7C |
 13 0D CR   29 1D GS   45 2D -  61 3D =  77 4D M  93 5D ]  109 6D m  125 7D }
 14 0E SO   30 1E RS   46 2E .  62 3E >  78 4E N  94 5E ^  110 6E n  126 7E ~
 15 0F SI   31 1F US   47 2F /  63 3F ?  79 4F O  95 5F _  111 6F o  127 7F DEL

Find the row with L in it, then read the corresponding character in the first column, which is FF, or Form Feed. The form-feed character caused the teletype to feed enough paper that it could be torn off below the current line, so most terminals simply clear the screen and return the cursor position to the origin when they see a ^L. This is probably the behavior that your program is expecting.

These characters also informed the default key bindings for Emacs and many other programs, which is why C-l is used for clearing or redrawing the screen in so many programs. It's also why C-h is used for backspace, and C-i for tab.

I'm sure you could implement something that examines incoming text and clears the current buffer when it sees a form-feed character. Or you could arrange to run this program inside term-mode, which already implements this and many other terminal-like behaviors. You should be able to find instructions for that elsewhere.

2
  • If I understand this right, the unprintable range of characters [0-31] is index by characters in range [64-95]?
    – scribe
    Nov 22, 2020 at 22:37
  • That's correct. ^@ is the NUL character, ^B is Start of Header, and so on up to ^[ which is Escape.
    – db48x
    Nov 23, 2020 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.