Question

There are so-called strict formats, like pdb - where the meaning of the symbol is defined by the colomn number of the symbol. For example here is a specification of the above mentioned pdb format.

Is there a way I can apply face colour basing on the column range?

One can normally add a regexp to be highlighted, for example for the current session in the following way:

(font-lock-add-keywords nil '(("\\[\\(.+?\\)\\]" . font-lock-keyword-face)))

So is there a way to specify that face at columns, say 7-11 - should be, say - red?

Edit:

So the answer is:

(font-lock-add-keywords nil '(("^.\\{2\\}\\(.\\{2\\}\\)" 1 font-lock-warning-face)))
Was it helpful?

Solution

Define a regexp that will select the appropriate column. For example to select the 3rd column (assuming the columns contains only letter for the clarity of the example) you can do something like "\(?:[a-z]+ \)\{2\}\([a-z]+\)" and then match on the first group.

Of course you can create such a string by using format and passing it some arguments for more flexibility.

(font-lock-add-keywords nil '(("\\(?:[a-z]+ \\)\\{2\\}\\([a-z]+\\)" 1 font-lock-warning-face)))

As an other direction, the following code was found in whitespace-mode which highlights the characters over a certain number:

;; Show "long" lines
(list
(let ((line-column (or whitespace-line-column fill-column)))
   (format
    "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
    whitespace-tab-width
    (1- whitespace-tab-width)
    (/ line-column whitespace-tab-width)
    (let ((rem (% line-column whitespace-tab-width)))
      (if (zerop rem)
      ""
    (format ".\\{%d\\}" rem)))))
 (if (memq 'lines whitespace-active-style)
     0              ; whole line
   2)               ; line tail
 whitespace-line t)

OTHER TIPS

The following applyies the coloring initially to the text from 7th to 11th column:

(defun bk-pdb-color-ATOM-initially()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward-regexp "^ATOM  \\(.\\{5\\}\\).\\(.\\{4\\}\\).\\(.\\{3\\}\\).\\(.\\{5\\}\\).\\{4\\}\\(.\\{24\\}\\)" nil t)
      (progn
        (overlay-put
         (make-overlay (match-beginning 1) (match-end 1))
         'face '(:foreground "blue"))
        (overlay-put
         (make-overlay (match-beginning 2) (match-end 2))
         'face '(:foreground "red"))
        (overlay-put
         (make-overlay (match-beginning 3) (match-end 3))
         'face '(:foreground "purple1"))
        (overlay-put
         (make-overlay (match-beginning 4) (match-end 4))
         'face '(:foreground "orange"))
        (overlay-put
         (make-overlay (match-beginning 5) (match-end 5))
         'face '(:foreground "green"))
        t nil)
)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top