color coding comment in f90-mode using "c" [SOLVED]

Discussion of Emacs Lisp
Efo
Posts: 6
Joined: Mon Apr 12, 2010 10:21 pm

color coding comment in f90-mode using "c" [SOLVED]

Post by Efo » Mon Apr 12, 2010 10:32 pm

Hi guys,

I am rather new to lisp, and I hope you will forgive me ignorance.

I have been a emacs user for a while now, and recently I converted a program from f77 to f90.
Here is the problem: if I use fortran-mode, all what is past the 72 columns is commented out, which is ugly to work with. On the other hand, I have a bunch of comments starting with "c" on the first column that were left from the f77 version, and if I use f90-mode they are not displayed as comment, which is also ugly to work with.

If there a way I can tell f90-mode to color-code "comment" all the lines that have a "c" on the first column?

Thanks,

Efo
Last edited by Efo on Wed Apr 14, 2010 5:19 pm, edited 1 time in total.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: color coding comment in f90-mode using "c"

Post by ramarren » Tue Apr 13, 2010 1:12 am

As I look at fortran.el, the F77 comment syntax is implemented as hack, as Emacs doesn't really support positional syntax. The correct thing to do would be to replace all old syntax comments with new, with for example M-x query replace<RET>^c<RET>!<RET> . The less correct thing to do is add to .emacs:

Code: Select all

(add-hook f90-mode-hook
          (lambda ()
            (set (make-local-variable 'font-lock-syntactic-keywords)
                 (fortran-font-lock-syntactic-keywords))))
which seems to work, but I don't guarantee anything.

Efo
Posts: 6
Joined: Mon Apr 12, 2010 10:21 pm

Re: color coding comment in f90-mode using "c"

Post by Efo » Tue Apr 13, 2010 7:35 am

Hi Ramarren,

Thank you for the prompt response.
The first solution seems to be a little cumbersome given the different files and lines of code... but I guess I could do that as I go, even though I am not sure if other people using subversion are going to like that.
I suppose the "^" indicates the first line, I tried it and I get "Replaced 0 occurences"

The second, less correct, solution seems to fit me better (I put it into .emac), but I get:
Í‚Í‚
Warning (initialization): An error occurred while loading `/home/efo/.emacs':
Symbol's value as variable is void: f90-mode-hook
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.

With --debug-init:

Debugger entered--Lisp error: (void-variable f90-mode-hook)
(add-hook f90-mode-hook (lambda nil (set ... ...)))
eval-buffer(#<buffer *load*> nil "/home/efo/.emacs" nil t) ; Reading at buffer position 7334
load-with-code-conversion("/home/efo/.emacs" "/home/efo/.emacs" t t)
load("~/.emacs" t t)
#[nil …´
*** bunch of symbols dont paste correctly such as "^H\205\264\^@" displayed as "‰" ***
#))‡" [init-file-user system-type user-init-file-1 user-init-file otherfile source ms-dos "~" "/_emacs" windows-nt directory-files nil "^\\.emacs\\(\\.elc?\\)?$" "~/.emacs" "^_emacs\\(\\.elc?\\)?$" "~/_emacs" "/.emacs" t load expand-file-name "init" file-name-as-directory "/.emacs.d" file-name-extension "elc" file-name-sans-extension ".el" file-exists-p file-newer-than-file-p message "Warning: %s is newer than %s" sit-for 1 "default" alt inhibit-default-init inhibit-startup-screen] 7]()
command-line()
normal-top-level()

Am I missing some package?

Thanks again.

Efo

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: color coding comment in f90-mode using "c"

Post by ramarren » Tue Apr 13, 2010 8:56 am

Efo wrote:The first solution seems to be a little cumbersome given the different files and lines of code... but I guess I could do that as I go, even though I am not sure if other people using subversion are going to like that.
You could use findr which can do query-replace recursively in a directory.
Efo wrote:I suppose the "^" indicates the first line, I tried it and I get "Replaced 0 occurences"
Sorry, I meant query-replace-regexp as a command. "^" means beginning of line. You could also use just replace-regexp, but I suppose confirming the changes would be better in this case.
Efo wrote:The second, less correct, solution seems to fit me better (I put it into .emac), but I get:
You have to (require 'f90) first, or use:

Code: Select all

(eval-after-load "f90"
  (add-hook f90-mode-hook
            (lambda ()
              (set (make-local-variable 'font-lock-syntactic-keywords)
                   (fortran-font-lock-syntactic-keywords)))))
The proper way would be to use the customization mechanism to set font-lock-defaults properly, but I am not quite sure how that works.

Efo
Posts: 6
Joined: Mon Apr 12, 2010 10:21 pm

Re: color coding comment in f90-mode using "c"

Post by Efo » Tue Apr 13, 2010 9:29 pm

Thanks Ramarren,

I meant "^" first column (or beginning of line), not line... it was late at night :)
Anyway, query-replace-regexp worked. But I still have the problem that the code is shared by different developers, and I dont want to mess the comments up for a color coding problem.

I tried the new piece of code you provided, but Istill get:

Debugger entered--Lisp error: (void-variable f90-mode-hook)
(add-hook f90-mode-hook (lambda nil (set ... ...)))
(eval-after-load "f90" (add-hook f90-mode-hook (lambda nil ...)))
eval-buffer(#<buffer *load*> nil "/home/efo/.emacs" nil t) ; Reading at buffer position 7366
load-with-code-conversion("/home/efo/.emacs" "/home/efo/.emacs" t t)
load("~/.emacs" t t)
#[nil
....
" [init-file-user system-type user-init-file-1 user-init-file otherfile source ms-dos "~" "/_emacs" windows-nt directory-files nil "^\\.emacs\\(\\.elc?\\)?$" "~/.emacs" "^_emacs\\(\\.elc?\\)?$" "~/_emacs" "/.emacs" t load expand-file-name "init" file-name-as-directory "/.emacs.d" file-name-extension "elc" file-name-sans-extension ".el" file-exists-p file-newer-than-file-p message "Warning: %s is newer than %s" sit-for 1 "default" alt inhibit-default-init inhibit-startup-screen] 7]()
command-line()
normal-top-level()

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: color coding comment in f90-mode using "c"

Post by ramarren » Tue Apr 13, 2010 11:18 pm

Efo wrote:I tried the new piece of code you provided, but Istill get:
Sorry again, I am not that used to Emacs Lisp and keep doing mistakes... you have to quote the form. Hopefully this works, I haven't really understood syntax highlighting in Emacs.

Code: Select all

(eval-after-load "f90"
      '(add-hook f90-mode-hook
                (lambda ()
                  (set (make-local-variable 'font-lock-syntactic-keywords)
                       (fortran-font-lock-syntactic-keywords)))))

Efo
Posts: 6
Joined: Mon Apr 12, 2010 10:21 pm

Re: color coding comment in f90-mode using "c"

Post by Efo » Wed Apr 14, 2010 12:10 am

No problem, you are much more knowledgeable than I am, and I certainly appreciate your help.

No errors with the new code, but now the f90 file has none of its color coding. If I M-x f90-mode, then I am back to the original problem (i.e. color coding ok except for comments starting with "c").

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: color coding comment in f90-mode using "c"

Post by ramarren » Wed Apr 14, 2010 12:33 am

This is getting embarrassing... well, testing this properly requires restarting Emacs, since when I tested that before I already had loaded most of the modules, and I tested only the inner form so I got quoting wrong when wrapping. Anyway, what I just tested and hopefully will work this time:

Code: Select all

(eval-after-load "f90"
  '(progn
     (require 'fortran)
     (add-hook 'f90-mode-hook
               '(lambda ()
                  (set (make-local-variable 'font-lock-syntactic-keywords)
                       (fortran-font-lock-syntactic-keywords))))))

Efo
Posts: 6
Joined: Mon Apr 12, 2010 10:21 pm

Re: color coding comment in f90-mode using "c"

Post by Efo » Wed Apr 14, 2010 7:26 am

Now I have color-coding back, but it looks like I am in fortran-mode. Everything past the 72nd column is commented.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: color coding comment in f90-mode using "c"

Post by ramarren » Wed Apr 14, 2010 7:48 am

I should probably give up, since obviously I have a rather vague idea of what I am doing, but...

Code: Select all

(eval-after-load "f90"
  '(progn
     (add-hook 'f90-mode-hook
               '(lambda ()
                  (set (make-local-variable 'font-lock-syntactic-keywords)
                       '(("^[cd\\*]" 0 (11))))))))

Post Reply