views:

73

answers:

1

Since it may be a good idea to have lines that are not wider than 80 characters in code files, what's the most effective way of recursively identify these lines in an existing project using Emacs?

UPDATE:

Using Trey's suggestion, I'm currently using the following code:

(defun find-long-lines (base-dir)
  "Recursively look for lines longer than 80 characters files"
  (interactive "DPath:")
  (grep-compute-defaults)
  (rgrep "^................................................................................." "*" base-dir))

Which works fine in combination with whitespace-mode.

+3  A: 

You could use igrep-find and use a regular expression that matches 81(+) characters like so:

M-x igrep-find ^.................................................................................. RET /path/to/area/to/search/* RET

And then you get a buffer in compilation mode that lets you easily jump to each of the offending lines (either via a mouse click, or C-x ` or M-x next-error).

Alternatively, you could use the built-in M-x grep-find and use the same regular expression.

To enter 81 ., type C-u 81 ..

And, if you want to have this all contained in a single command (that prompts you for the path to the files), you can use this:

(defun find-lines-longer-than-80 (files)
  "Recursively look for lines longer than 80 characters files"
  (interactive (list (igrep-read-files)))
  (igrep igrep-program "^................................................................................." files igrep-options))

There are some other tips available on the Emacs Wiki for Find Long Lines, including several options for highlighting lines that are longer than 80 characters when you're visiting a file.

Trey Jackson
And to enter the 81 dots, you can type `ESC 8 1 .` or `C-u 8 1 .`
Gilles
@Giles I should have mentioned that, it's exactly what I did.
Trey Jackson
Thanks for this. Using the whitespace-mode for current buffers and it works like a charme. Your solution doesn't recursively go through all subfolders, does it?
Roberto Aloi
@Trey: I've updated my question. That code seems to work fine. Thanks a lot for your suggestions.
Roberto Aloi
@RobertoAloi The solution does go through all the subfolders (that's the `-find` part of `igrep-find` and `grep-find`. You can limit it to just a single folder by using the `igrep` or `grep` commands.
Trey Jackson