emacs: Highlighting errors for c, python, and other languages

Hi ! Today we’ll see how to highlight syntax errors in emacs, with exemples for C and Python.

First you should learn about flymake-mode here : http://www.emacswiki.org/emacs/FlyMake
In a nutshell Flymake is a minor mode to perform on the fly checks on your files. It can run any external syntax checker by different means, I let you check out the documentation.

Quick and easy setup to highlight C syntax :
You already have a Makefile, that’s good, so you just have to add a new rule to your Makefile, named ‘check-syntax’ :

check-syntax:
    gcc -o /dev/null -D$(DEFINE) $(CFLAGS) -S ${CHK_SOURCES}

You can fix my -D$(DEFINE) $(CFLAGS) to match your compile options …
Then open a .c file in your project, and if you .emacs file don’t automatically start flymake-mode, type ‘M-x flymake-mode’ and enjoy error highlighting !

Next trick is, if you’re using a non graphical emacs, you don’t have, by default, the error message, so i’s a bit anying…
So you’ll add some lines in you .emacs and a file in you .emacs.d
First, download flymake-cursor.el here http://www.emacswiki.org/emacs/download/flymake-cursor.el and put it in your ~/.emacs.d/
Then in your .emacs, add :

(require 'cl)
(require 'flymake-cursor)

Now, when you stop your cursor on an error, the message should appear in the minibuffer.

Last trick, for Python developers, how to use flymake-mode with pyflakes ?
Just add this to you .emacs file, and tweak it if you want. You should install pyflakes in order to make it work.

;; aptitude install pyflakes to check python code                                                                                                                                  
(require 'flymake-cursor)
(global-set-key [f4] 'flymake-goto-next-error)
 
(when (load "flymake" t)
  (defun flymake-pyflakes-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                       'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list "pyflakes" (list local-file))))
 
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))
 
(add-hook 'find-file-hook 'flymake-find-file-hook)

Bonus trick you should try :
Replace “pyflakes” in (list “pyflakes” (list local-file)))) to a shell script of you own, running pyflakes, pep8, etc…as I just found here :
http://stackoverflow.com/questions/1259873/how-can-i-use-emacs-flymake-mode-for-python-with-pyflakes-and-pylint-checking-cod

#!/bin/bash
 
epylint "$1" 2>/dev/null
pyflakes "$1"
pep8 --ignore=E221,E701,E202 --repeat "$1"
true

Enjoy !

Posted in Code, emacs, Python | Leave a comment

Emacs: replace tabs with spaces

When you want to replace tab with spaces or vice versa don’t use M-% (query-replace) but M-x tabify or M-x untabify. They work on the current selection so if you want it to be applied to a whole buffer, try C-x h (mark-whole-buffer) before to select the whole buffer.

Posted in emacs | 2 Comments

Searching and replacing in emacs

Day two of my serie about emacs, about searching and replacing.

Function name : isearch-forward
Typical Key sequence : C-s
How to get help : C-h f isearch-forward
Usage : isearch-forward let you type a string to be searched incrementally in the current buffer, successive following C-s will jump to the next match.

Function name : isearch-forward-regexp
Typical Key sequence : C-M-s or C-u C-s
How to get help : C-h f isearch-forward-regexp
Usage : isearch-forward-regexp let you type a regexp to be searched incrementally in the current buffer, successive following C-s will jump to the next match.

Function name : query-replace
Typical Key sequence : M-%
How to get help : C-h f query-replace
Usage : M-% search RET replace RET
Then, for each term found, query-replace will ask you what to do : space or y to replace, delete or n to skip, RET or q to exit, ! for ‘yes for all’, ? to get help about how to enter recursive edit / delete match and recursive edit / edit replacement string / …

Then you should read about replace-string, replace-regexp, occur, list-matching-lines, multi-occur, multi-occur-in-matching-buffers, how-many, flush-lines, and keep-lines.

Case sensitivity :
A search is by defaut case insensitive, but if you input an upper case letter, it become case sensitive. M-c during a search toggle the case sensitivity.

Configuration variables :
You may consult the documentation about those variable typing :
C-h v variable
or
M-x apropos-variable RET case-fold-search RET

  • case-fold-search (Non-nil if searches and matches should ignore case.)
  • default-case-fold-search (Default value of `case-fold-search’)
  • tags-case-fold-search (Whether tags operations should be case-sensitive.)
Posted in emacs | Leave a comment

Numeric arguments in emacs

I’m starting a ’emacs trick of the day’ sequence with :

Function name : universal-argument
Typical Key sequence : C-u
How to get help : C-h f universal-argument
Usage : C-u receive a numeric argument that is given to the next called function, when no numeric argument is typed, the value defaults to 4.

So today you can try :
C-u 9 C-n # that move cursor vertically down 9 lines
C-u C-k # that kills 4 lines
C-u C-u C-k # that kills 4 * 4 = 16 lines
C-u 10 n # that enters nnnnnnnnnn
You may ask, what about if I want to input 25 ‘6’ ? C-u 256 can’t work … so you just have to separate with another C-u :
C-u 25 C-u 1 # enters 6666666666666666666666666

Some functions does not have the simple ‘repeating’ effect of receiving a numeric parameter, for example, running C-u C-l does not recenter 4 times your screen ! but the help page of recenter-top-bottom states that :
> A prefix argument is handled like `recenter':
> With numeric prefix ARG, move current line to window-line ARG.
> With plain `C-u’, move current line to window center.

A negative argument to C-l move the current line to the line ARG from the bottom of the screen.

Posted in emacs | Leave a comment

nfsmount : rpc failed: 2

For those, here on the internet, asking themselves what is this f*cking `rpc failed: 2` while mounting an NFS, i’ts possible that teh response is here :

Your NFS client, trying to mount the NFS share will use RPC to communicate with the serveur, il will go like :

> PORTMAP GETPORT(Program: NFS, Version: 3, Proto: TCP)
< PORTMAP Port: 2049 > PORTMAP GETPORT(Program: MOUNT, Version: 3, Proto: TCP)
< PORTMAP Port 49066 > MOUNT MNT(Program Version: 3, Path: /srv/nfsroot/ )
< MOUNT Reply error 2, "remote can't support version #", Program Version (Minimum): 1, Program Version (Maximum): 2 You can see that the response is "remote can't support version #" and we should have found this solution in the RFC 1831 (RPCv2) stating :

  Given that a call message was accepted, the following is the status
   of an attempt to call a remote procedure.

      enum accept_stat {
         SUCCESS       = 0, /* RPC executed successfully             */
         PROG_UNAVAIL  = 1, /* remote hasn't exported program        */
         PROG_MISMATCH = 2, /* remote can't support version #        */
         PROC_UNAVAIL  = 3, /* program can't support procedure       */
         GARBAGE_ARGS  = 4, /* procedure can't decode params         */
         SYSTEM_ERR    = 5  /* errors like memory allocation failure */
      };

So the problem is you client is asking for a NFS version greater that your server runs … but if your server is running NFS v3, check a `ps aux | grep [r]pc.mountd` for :

root 1411 0.0 0.0 18808 1036 ? Ss Apr15 0:00 /usr/sbin/rpc.mountd –manage-gids –no-nfs-version 3

Did you catch the –no-nfs-version 3 ? If your server is compiled with NFSv3 support, drop the –no-nfs-version 3 in your configuration and it should work !

Enjoy !

Posted in Code, Sysadmin, unix | 7 Comments