Question

I have to work in a large C codebase that is very old (decades) and suffers multiple-author syndrome. Different coding styles (spaces vs tabs; curly braces on new line, same line, new line indented etc.) are all existing in the codebase. For non-technical reasons, I will not be able to re-indent and commit the source files.

Is there a way by which, before opening any of the .C file I can run Lindent (the linux kernel indentation script) on the file prior to opening ? That way, I will be able to see the file in a way I want it. The file close should not save the indentation changes. Nor should I be prompted with a file-changed message. Usage of a temporary file on each open will not be possible as things like cscope will break that way.

Think of this like a CSS but for your editor and code. Is such a setup possible at all with vim ? I am using gvim on a linux box, but I am ready to switch to any vim variant + plugin.

Also, it should be obvious that the setup should also allow me to open the file unindented. Is there a glimmer of hope for this complex requirement ? I am fine to switch editor to (listen emacs, sublimetext people) if this can be done in any other editor.

Was it helpful?

Solution

Assuming the lindent can be invoked like a normal filter (:%! {cmd}), the following commands will process the current buffer and reset the modification flag (so that the file appears as unchanged):

:%!lindent
:setlocal nomodified

You may want to :setlocal nomodifiable, too, to avoid that you accidentally persist the reformatted file.

Put these commands into ~/.vim/after/ftplugin/c.vim. (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/c.vim.) Alternatively, you could define an :autocmd FileType c ... directly in your ~/.vimrc, but this tends to become unwieldy once you have many customizations.

OTHER TIPS

This is trivial to do in Emacs. Here's a command that I use sometimes:

(defun cpp-astyle()
  (interactive)
  (let (beg end)
    (if (region-active-p)
        (setq beg (region-beginning)
              end (region-end))
      (setq beg (point-min)
            end (point-max)))
    (shell-command-on-region
     beg end "astyle -A3 -U -p -k3" nil t)))

This will run astyle on current file. You can bind this action to a key:

(global-set-key (kbd "f8") 'cpp-astyle)

It's also possible to have this command run automatically for all C/C++ sources but that could be annoying. A key binding is better.

UPD

Per request of @Tom:

(defun cpp-astyle()
  (interactive)
  (let ((buf (get-buffer-create
              (format "--%s" (buffer-name))))
        (mode major-mode)
        beg end)
    (if (region-active-p)
        (setq beg (region-beginning)
              end (region-end))
      (setq beg (point-min)
            end (point-max)))
    (shell-command-on-region
     beg end "astyle -A3 -U -p -k3"
     buf)
    (switch-to-buffer buf)
    (funcall mode)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top