1. Introduction

First and foremost, a word of warning: while the previous articles were centered around the beginner, this article is for more advanced users, that already "speak" a programming language or two, and want to customize their editor towards it being ideal for the task. So you are expected to be more or less proficient in the ways of emacs, to be able to use it for day to day tasks and have the ability and desire to learn something new. After all, it will be for your own reward, and your programming tasks will become more efficient. Our approach will be to take a few popular languages, show you how to configure emacs for the perfect development environment, then move on to the next language. Emacs configuration is made in a Lisp dialect called Elisp, but don't worry if you don't know it yet, we'll tell you what you need.

2. The languages

First, some background. This article is about emacs, not about any derivative like mg or jed that might or might not offer the desired functionality. That's because many derivatives were born from the need of creating a smaller emacs, since the original is pretty big, admittedly. So in the process of removing functionality there might just be just some functionality that's getting removed and we probably will need here. In short, emacs-only. Second, the files. In our examples, besides customizing the ~/.emacs file, we will create a directory named ~/.emacs.d/ where we will place our modes. Just as emacs knows what kind of syntax highlighting, indentation, etc. to use for several types of text, like HTML, TeX, C source code, and others, via modes, we can add/modify modes to our liking, to this is what we'll do. Practically speaking, a mode is a file with a .el extension (from Elisp) that will be dropped in ~/.emacs.d, then ~/.emacs will be altered for the editor to "know" about the new extension. You'll see in a few moments, depending on how fast you read.

2.1. C/C++

It seems obvious that we will start with C, because it's the lingua franca of Unix and Linux. Emacs is also written (partially) in C, so there's another reason for you. Although emacs knows how to detect the language you're writing in, based on file extension, you may need further customization. In our article we said that when in C mode, emacs already conforms to the GNU coding guidelines regarding tabs, indentation and so forth. But if you want to contribute code to the Linux kernel, the situation changes. Obviously you already have the kernel try downloaded somewhere, but if you don't, here's an excerpt from Documentation/CodingStyle (but if you're working with the kernel guys, this file must be under your pillow):

Tabs are 8 characters, and thus indentations are also 8 characters.
There are heretic movements that try to make indentations 4 (or even 2!)
characters deep, and that is akin to trying to define the value of PI to
be 3.
The other issue that always comes up in C styling is the placement of
braces.  Unlike the indent size, there are few technical reasons to
choose one placement strategy over the other, but the preferred way, as
shown to us by the prophets Kernighan and Ritchie, is to put the opening
brace last on the line, and put the closing brace first, thusly:

        if (x is true) {
                we do y

So you see that this already conflicts with the way emacs does stuff by default, plus you'll want to have extra functionality, like maybe line numbering or different colors with highlighting. So, here we go. Because there is already a C mode, we need only to customize it by editing ~/.emacs:

;; This is how comments are used, but it's better to read
(setq standard-indent 8)
(line-number-mode 1)
(column-number-mode 1)
(set-background-color "black")
(set-cursor-color "red")

Regarding opening braces not being placed on their own row, there's nothing to worry about: emacs will act as you expect it to if you prefer that style of coding.

2.2. Python

Thanks to Pedro Kroger, we have a simple idea (and want to share it) on how to configure your emacs if you are a Python developer. Needless to say, all you're reading here is assuming that you're running the latest stable version, that being 23.x . Else, you're on your own, as compatibility issues may arise.

Many distributions offer a python-mode packaged. On Debian, Gentoo and derivatives it's called python-mode, on Fedora it seems that it's integrated in the emacs package, Arch has it under the name emacs-python-mode, and OpenSUSE has the same situation as Fedora. Now let's edit ~/.emacs so it knows about it, after we install it, of course:

(add-to-list 'load-path "~/.emacs.d")
(require 'python-mode)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))

There are lots of other modes for Python, packaged or not, and we recommend Pedro's blog for more information. The emacs wiki also is a good place to look, not only for Python information, as it's a wealthy resource. With python-mode, you can execute the code you just wrote in the very same window, owing to the interpreted nature of the language. So after opening/creating/editing your .py file, type C-c ! to get a Python shell. Move back to the source with C-x o, then type C-c C-c to get the interpreter to ... well, interpret the contents of the buffer. You can see the results in the lower part of the window.

2.3. PHP

Since, and this is also available for the Python mode above, various distributions tend to name these modes differently, and some even don't have them at all, we would recommend you save yourself some time and find the respective mode's homepage, save the .el file in ~/.emacs.d and tell emacs about it. In our case, the PHP mode is easy to find, so download it and then type the following in ~/.emacs :

(add-to-list 'load-path "~/.emacs.d")
;; Note the ' - it's not a typo!
(require 'php-mode)

You may want to add a third line, just like the one in the Python part, so that emacs will use php-mode whenever it opens a .php file. We will not repeat already-mentioned commands, so now that you know what to add to your ~/.emacs file, we will only concentrate on new information. Since we spoke of extending existing modes, now that you have the PHP mode in place, here's how to extend it inside ~/.emacs for Drupal-specific coding (thanks to the guys at Drupal, of course)

(defun drupal-mode ()
  "Drupal php-mode."
  (message "Drupal mode activated.")
  (set 'tab-width 2)
  (set 'c-basic-offset 2)
  (set 'indent-tabs-mode nil)
  (c-set-offset 'case-label '+)
  (c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
  (c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values
  ; More Drupal-specific customizations here

(defun setup-php-drupal ()
  ; Drupal
  (add-to-list 'auto-mode-alist '("\\.\\(module\\|test\\|install\\|theme\\)$" . drupal-mode))
  (add-to-list 'auto-mode-alist '("/drupal.*\\.\\(php\\|inc\\)$" . drupal-mode))
  (add-to-list 'auto-mode-alist '("\\.info" . conf-windows-mode))



Although in real life I might be a little messy, when it comes to my computer-related tasks I like order. This is why I'd put the above code inside a separate file in ~/emacs.d, then tell ~/.emacs how to use it. This allows you not to clutter your ~/.emacs file and separate language-specific instructions with more generic ones. For example, as you've seen above, there are C circles when you're asked to go the 8-character tab way. But if I am a C and also a Python programmer, I certainly wouldn't want to use anything else than 4-character tabs (see PEP 8 for coding guidelines in Python).

2.4. SQL

Now SQL is a somewhat different beast. It hasn't got as many coding guidelines as other languages, but at the same time every vendor, commercial (Oracle, Microsoft) or not (SQLite, Postgres) extends the ANSI standard language with specific parts. So a good SQL mode must know how to deal with various implementations in order to be useful. Right, so via the same emacs wiki we found SqlMode, which is apparently exactly what we wanted. It knows Postgres, MySQL, Ingres, db2, Oracle, Informix and Sybase, just to name a few. You can simply get it, save it and use it. You can also have a look at the more bleeding-edge Lisp:sql, but we will just look at SqlMode.

Let's get to work. To get to the SQL interpreter from a window already in SQL mode, do this: M-x sql-set-sqli-buffer Return *SQL* Return. What follows is a very practical code snippet for saving history separately for each SQL dialect (e.g. Sybase, MSSQL, and so on). In order for this to work, just create a sql directory under ~/.emacs.d, then add this hook to ~/.emacs:

(defun my-sql-save-history-hook ()
    (let ((lval 'sql-input-ring-file-name)
          (rval 'sql-product))
      (if (symbol-value rval)
          (let ((filename 
                 (concat "~/.emacs.d/sql/"
                         (symbol-name (symbol-value rval))
            (set (make-local-variable lval) filename))
         (format "SQL history will not be saved because %s is nil"
                 (symbol-name rval))))))
  (add-hook 'sql-interactive-mode-hook 'my-sql-save-history-hook)

2.4.1. Other useful SQL modes

  • PlSqlMode
  • SqlIndent
  • SqlTransform
  • SqlPlus
  • SqlReplace

All these can be found using the emacs wiki, but those aren't the only ones. Using the wiki's search function will make a happy emacs user, as you will find lots of practical modes.

3. Conclusion

These are only a few of the languages that emacs supports, whether out-of-the-box or via extensions. Sadly, due to lack of space the above list had to be a short one, but with the wealth of information available online, you can, with a little effort, find a mode for your language or, even better, learn how to write your own.

Free Linux eBooks

Go to top