Anthony Ge
Anthony Ge

Reputation: 21

How to highlight floating numbers and integers differently in .F90 files in Emacs?

How to highlight floating numbers and integers differently in .F90 files in Emacs? In my version 24.4.1, there is no difference between floating numbers and integers. How can I display them in different colors?

Upvotes: 1

Views: 281

Answers (2)

Anthony Ge
Anthony Ge

Reputation: 21

I posted this question two weeks ago. Now I have had my own solution. So, I am posting it here as a reference for others who might encountered the same.

First of all, I'd love to thank @Justin for answering before and suggesting the highlight-number-mode. His solution probably works. However, I have a slightly more complicated requirement, so I decided to do it differently.

Also, I'd have to admit that I'm a very beginner in programming. So, you might find some stupid lines in my codes. Feel free to let me know if you think there's a better way.

Here is my solution.

  1. Install highlight-number-mode and parent-mode in emacs. (The second one is required by the first one.) In my case, they are installed under /~/.emcas.d/elpa/.

  2. As far as I see, the original highlight-number-mode does not support f90-mode, and it highlights all numbers (eg. 2, 3.4, 8e-2) equally, by changing fond color. This does not meet my requirement. Specifically, I wish to highlight only floating numbers, and I denoting them either as 2. or .5 in my .f90 script. In Fortran 90, floats and integers have different division rules. So, I hope to visualize them differently in order to reduce the risk of introducing such bugs. Therefore, I changed the following part in the source code (highlight-numbers.el):

(defconst highlight-numbers-generic-regexp
  (rx (or
       "."
       (? (and
           symbol-start
           digit
           symbol-end)
          "."
          (* digit))))
  "Generic regexp for number highlighting.
It is used when no mode-specific one is available.")

(defvar highlight-numbers-modelist
  (copy-hash-table
   (eval-when-compile
     (let ((table (make-hash-table :test 'eq)))

       (puthash 'f90-mode
                (rx (or
                     "."
                     (? (and
                         symbol-start
                         (or (and (+ digit) (? (or "." "e" ".e" "E" ".E")) (+ digit))
                         (and (+ digit) (? (or "e-" ".e-" "E-" ".E-")) (+ digit))
                         (and (+ digit) "." (+ digit) (? (or "e" "E")) (+ digit))
                         (* digit))        
                         symbol-end)
                        "."
                        (* digit))))
                 table)

If you compare the above code with the original one by Fanael Linithien, you will notice that I have added two ".". As Fanael said (in private discussions), this is probably due to that "." is taken as a punctuation in f90-mode in emacs. So, I have to modify the regexp and the table accordingly.

  1. Once the above has been done, I put the line (add-hook 'f90-mode-hook 'highlight-numbers-mode) in my init.el to load this package via elpa.

  2. Afterwards, the floats in my code will be highlighted, such as

example

And you can see the difference in floats and integers.

In the end, I'd add that I have been using this for quite a few days. No problems happen so far. So, I guess it is working! :)

Upvotes: 1

Justin Schell
Justin Schell

Reputation: 592

The default emacs fortran mode doesn't differentiate the font faces for floats and integers. However, you could use the highlight-numbers package (via M-x package-list-packages or https://github.com/Fanael/highlight-numbers).

Add the following to your .emacs to highlight floats (change the foreground and background colors as you see fit:

(add-hook 'fortran-mode-hook 'highlight-numbers-mode)
(add-hook 'after-init-hook
      (lambda ()
        (puthash 'fortran-mode
                 (rx (and symbol-start
                          (? "-")
                          (+ digit)
                          "."
                          (+ digit)
                          (*? any)
                          symbol-end))
                 highlight-numbers-modelist)
        (set-face-attribute 'highlight-numbers-number nil
                            :foreground "gray60" :background "black")))

Upvotes: 2

Related Questions