Emacs: linux-c-mode and Line numbers

by

Tired of the slow ssh access to one of the servers located outside India, I finally figured it was time to start playing with emacs to reduce the number of keystrokes needed.

First on, I wanted the linux-c-mode enabled *by default* whenever I open a file whose extension is ".c" (Yeah Yeah, I know extensions are meaningless for Linux.. but emacs is intelligent πŸ™‚ ). For all the other type of files, linux-c-mode should be OFF, because it will mess-up their indentation.
Second, line numbers should be displayed on the left-hand-side panel of each file I open. Then to go a particular line I just "M-g g". Simple.
Third, All this should be accessible to my user account as well as root user.

So, I edit the ~/.emacs file (create if not already present) like this (for the first goal):

(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux kernel."
(interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))

(add-to-list 'auto-mode-alist '("\.c$" . linux-c-mode))

The first chunk defines the linux-c-mode (from Documentation/CodingStyle) and the last line enables it by default ONLY for *.c files. Simple!

For the second part, I got a script linum.el from google (Vedang says it has been integrated into emacs)
You can find the script here: http://jitesh.1337.googlepages.com/linum.el
Drop this script at /usr/share/emacs/site-lisp/

There’s a problem with this script. It doesn’t print a space after the line number. Sad!
Here is a patch I wrote to fix it: http://jitesh.1337.googlepages.com/linum.el.patch

Now I want to display line numbers for EACH file I open in emacs. So, I add the following lines in the .emacs file:
(require ‘linum)
(global-linum-mode 1)

(First line is needed to autoload linum.el. The second line enables it by default)
To toggle the linum mode, use "M-x linum-mode"
Simple!

Finally, I wanted to give access to root user too.
A simple solution would be to make a symlink/hardlink from /root/.emacs to /home/jitesh/.emacs.
However, I wasn’t too concerned about the other users on the system πŸ™‚ .So, I simply copied to .emacs file to /usr/share/emacs/site-lisp/site-start.d/ and renamed it as myinit.pl. So, now, these things get enabled by default for *all* users.

Now that everything is setup (and steps documented via this blog), let me go back to coding peacefully in emacs!

Advertisements

Tags:

7 Responses to “Emacs: linux-c-mode and Line numbers”

  1. vedang Says:

    linum.el comes inbuilt with Emacs from version 22 onwards. I don’t know about the spacing thing, since i don’t use linum myself, so people interested in this should simply get the latest copy of emacs (preferably from CVS) and then follow the steps mentioned by Jitesh from the patch point onwards.

  2. Vedang Says:

    I would also suggest changing the autoload line to:

    (add-to-list ‘auto-mode-alist ‘(“\\.\\(c\\|h\\)$” . linux-c-mode))

  3. Jitesh Says:

    yeahhh.. forgot .h files.. thank you thank you πŸ™‚

    About emacs,
    # rpm -q emacs-common
    emacs-common-22.3-11.fc11.i586

    # rpm -q –list emacs-common | grep linum

    May be its in CVS and not released yet?
    Had a look at the fedora repository. It doesn’t strip anything from upstream. Simply adds some of its own stuff. so, not really sure what happening here πŸ™‚

  4. Vedang Says:

    I was going through my elisp code, sprucing things up, when I noticed that the linux-c-mode function imposes c-mode on even the cpp files, thereby suppressing (so far undiscovered) advantages of c++-mode for cpp files. This is the solution I propose:

    ;function to implement Indentation style 
    ;Documentation/CodingStyle (kernel)
    (defun linux-c-indent ()
      "adjusted defaults for C/C++ mode use with the Linux kernel."
      (interactive)
      (setq tab-width 8)
      ;;force spaces, to work with dumber editors
      (setq indent-tabs-mode nil) 
      (setq c-basic-offset 8))
    
    (add-hook 'c-mode-hook 'linux-c-indent)
    (add-hook 'c-mode-hook (lambda() (c-set-style "K&R")))
    (add-hook 'c++-mode-hook 'linux-c-indent)
    
  5. EBo Says:

    I was not able to get the above to work properly (it kept giving me 6 space indents instead of 8). Starting off with Linus’s coding standards and hacking your stuff around a bit I came up with the following that worked:

    (defun linux-c-mode ()
    “C mode with adjusted defaults for use with the Linux kernel.”
    (interactive)
    (c-mode)
    (c-set-style “K&R”)
    (setq c-indent-level 8)
    (setq c-basic-offset 8)
    (setq c-brace-imaginary-offset 0)
    (setq c-brace-offset -8)
    (setq c-argdecl-indent 8)
    (setq c-label-offset -8)
    (setq c-continued-statement-offset 8)
    (setq indent-tabs-mode t)
    (setq tab-width 8)
    )
    (add-to-list ‘auto-mode-alist ‘(“\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'” . linux-c-mode))
    (add-to-list ‘auto-mode-alist ‘(“\\.[ch]\\'” . linux-c-mode))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: