2

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)))
Drew
  • 29,895
  • 7
  • 74
  • 104
Adobe
  • 12,967
  • 10
  • 85
  • 126

2 Answers2

2

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)
Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
  • It seems like `"\(?:[a-z]+ \)\{2\}\([a-z]+\)"` has nothing to do with a column number. It just marks all words longer then 3 `[a-z]` chars. – Adobe Jul 25 '12 at 14:35
  • When you select the first group you will have the 3rd column. The first 2 columns are in shy groups and will not be counted. – Nicolas Dudebout Jul 25 '12 at 15:54
  • (font-lock-add-keywords nil '(("\\(?:[a-z]+ \\)\\{2\\}\\([a-z]+\\)" 1 font-lock-warning-face))) works for example – Nicolas Dudebout Jul 25 '12 at 15:59
  • Did you get any luck with that last comment? – Nicolas Dudebout Aug 07 '12 at 12:16
  • No - I inserted that into buffer, evaluated that - but it doesn't color my dummy buffer. I did [that](https://github.com/bk322/bk-dotfiles/blob/master/.emacs.d/site-lisp/bk/bk-pdb-mode.el) - but it's far from ideal. For example - If I comment some lines - the coloring remains. Does Your solution works for You? – Adobe Aug 07 '12 at 12:24
  • It does for me. Do you evaluate the line in your dummy buffer? – Nicolas Dudebout Aug 07 '12 at 13:28
  • I do. Here is a [screenshot](http://dl.dropbox.com/u/61989972/Misc/borisATvasilisa_021.jpg) right after the avaluation. I also tried evaluating it with whitespace-mode turned off. – Adobe Aug 07 '12 at 13:56
  • You need to replace what is in the square brackets with the characters you need. I put in my answer only lower case letters for clarity: `[a-z]`. Replace that part with whatever characters can be in each column: maybe `[^ ]` if it is anything but a space. – Nicolas Dudebout Aug 07 '12 at 14:31
  • `(font-lock-add-keywords nil '(("^.\\{2\\}\\(.\\{2\\}\\)" 1 font-lock-warning-face)))` works! – Adobe Aug 08 '12 at 17:02
0

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)
)))
Adobe
  • 12,967
  • 10
  • 85
  • 126