emacs question: removing "self" keyword in python-mode

Steven F. LeBrun steven at lebruns.com
Fri Apr 10 09:24:35 UTC 2009


Evan Klitzke wrote:
> I know, this is an emacs question and not a fedora question per se. But
> I've asked my question on the emacs help news group without a response,
> and I know there are some emacs users lurking around these parts, so I
> thought I'd try here :-)
>
> My question is stated previously at:
> http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/785fb0e6542eda80/1ad000e4fa700e1e
>
> Basically, emacs uses a big complex regular expression to figure out
> what words to highlight in different modes. There seems to be a command
> font-lock-remove-keyword that can be used to safely edit the regular
> expression and remove a keyword from it; but the documentation is pretty
> vague, and the source code for the function is way over my head.
>
> Thanks for any help!
>
>   
The elisp function that you should try is font-lock-remove-keywords as 
you mentioned.  The Emacs help for this function shows the syntax as:
    (font-lock-remove-keywords   mode   keywords)

I have not used this function, though I have used its counterpart 
font-lock-add-keywords.  Try the following and see if it does what you want:

    ;;; Define a list of keywords to be removed from the list of Python 
keywords
    ;;; note: your list may consist of just one keyword or multiple
    (defconst my-python-non-keywords                                     
  ;; constant name
       '("\\bself\\b" "\\bTrue\\b" "\\bFalse\\b")                        
     ;; constant value
       "My list of Python Keywords that I do not highlighted")  ;; 
constant description/comment

    ;;; Remove Keywords from Python font-lock-keywords-alist
    (font-lock-remove-keywords   'python-mode   my-python-non-keywords)

You may have to play around with the list of keywords.  I am not sure 
what the regular expression should look like.  It may be that you only 
need the keywords without the extra regular expressions to remove them; 
I have only added keywords so I am not sure about this point.  In other 
words, if "\\bself\\b" does not work, try "self" next.  If neither of 
them work, then you are going to have to experiment and/or seek more 
help in the forums.

The example that I used above, "\\bTEXT\\b", works for C++ keywords.  
The "\\b" causes the regular expression to select TEXT when it is a word 
and ignore it when it part of another word like "anyTEXT".

You do not need to do the two step code that I wrote.  You can replace 
the my-python-non-keywords variable/constant name with the actual value 
'("\\bself\\b").  I prefer creating the constant first because then I 
can list the constant to verify that the list is actual what I think it 
is using the command:
     Control-H   v   my-python-non-keywords

I used a constant in my original C++ keyword code.  You can use a 
constant or a variable. 
    (defvar name value comment)
The only difference is that if you use a variable you can change the value.

When I tried something similar, many years ago, I actually created a 
complete list of C++ keywords used by the compiler that I was using then 
and replaced the entire list:
    (font-lock-add-keywords 'c++-mode my-c++-keywords nil)

The last argument is set to nil to replace or t to append.  I no longer 
recommend the complete replacement of the keyword list since you must 
remain active to keep the list up to date.  But it is an option that can 
be used when all else fails.

Good luck and let us know when you are successful.

-- 
  Steven F. LeBrun

Quote: /"The objection to fairy stories is that they tell children there 
are dragons. But children have always known there are dragons. Fairy 
stories tell children that dragons can be killed."/
     -- G.K. Chesterton

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.fedoraproject.org/pipermail/users/attachments/20090410/5fac91d2/attachment-0001.html 


More information about the users mailing list