IDEs
Tab Completion for Custom Commands
A lot of commands in Emacs take input from the minibuffer, the little one-line area at the bottom of the editor. When you are writing your own commands you will often make use of the same facility. In this article I want to show you one way of doing that which will provide tab completion for your custom functions. I’m going to use a simple example out of my own dot Emacs file. This is the function that was in my last email, my function for opening org-mode log files. I keep one org-mode file for each project, in a log directory, split up by year. So I wanted a simple command I could bind to a key that would let me type in a project name and open the file. So I have this: (defun open-project-log-file (project) "Opens a ...
Real-Time PHP Error Messages in Emacs
I was frustrated yesterday while working on a project because of a bug caused by a syntax error. In the year 2010 we really should not be having bugs because of syntax. I say this not to blame anyone—in fact the syntax mistake was mine—but to make the point that this is not the type of bug that should ever waste our time. Flymake is a package for Emacs (I think it’s standard these days) which performs automatic syntax checking. Like most things in Emacs, it is extensively configurable. Flymake has to be this way by design, since it is intended to perform real-time syntax checking for all languages. One way Flymake does this is by deferring to external tools. PHP happens to have a syntax checker built-in. If you run php -l foo.php you will be told whether or not foo.php has any syntax errors, and ...
Indent on Save, Maybe
While working on a .NET project, I have noticed that when I do many things, Visual Studio takes that time to re-indent large amounts of my code. But most of the time I’m writing it out in Emacs, so this makes my diffs a pain in the ass. But it got me to thinking about indenting on save. And then—lo—the very subject came up today on the Emacs mailing list. A teacher was asking for a way to make C++ mode automatically re-indent the whole file on save, to make his students use. I don’t know if he was sick of seeing horrible indentation or what. There is a hook in Emacs called the before-save-hook, to which you can add functions to be called just before saving the contents of a buffer. A great example: (add-hook 'before-save-hook 'delete-trailing-whitespace) Will call delete-trailing-whitespace whenever you save any file. So ...
SQL Movement Commands
Most programming modes in Emacs support two commands, beginning-of-defun and end-of-defun. These move you to the beginning and end of the nearest function, and by default are bound to C-M-a and C-M-e, respectively. However, sql-mode does not have anything similar. I have the above two commands bound to C-up and C-down for quickly moving around functions, but I find myself often hitting them when viewing an SQL file to move through tables---which doesn't work. Fortunately that functionality is pretty easy to add. For my fellow Emacs users, here is some code I whipped up that you can use to easily move between tables and views. (defconst sql-beginning-of-table-or-view-regexp "^\\s-*create\\s-+\\(?:table\\|view\\)" "Regular expression for matching the start of an SQL table or view definition.") (defun sql-prev-table-or-view () "Move backwards to the beginning of the nearest table or view from point." (interactive) (re-search-backward sql-beginning-of-table-or-view-regexp nil 'noerror)) (defun sql-next-table-or-view () "Move forwards to the beginning of the nearest table or view from point." (interactive) (re-search-forward sql-beginning-of-table-or-view-regexp nil 'noerror)) Then you ...
