Codeformatter / beautifier for C for Linux for Emacs Users

I am a Linux user looking for code that will take files containing C code and format them to specification. In particular, I am looking for:

  • Change all indents to 8 spaces
  • Format code blocks sequentially
  • Add row rows sequentially

It would be nice if he had the default settings and the ability to configure. I prefer free open source solutions when they are available. Any suggestions?

UPDATE: I should also note that I am an emacs user, and therefore this may be a specific emacs question. I updated the tags to reflect this.

+1
source share
6 answers

Astyle is worth a look.

+12

"indent" "cb" , ...

+10
+7

Emacs? , , , , ...

+3

, () Emacs, , cc-mode indentation . styles, . , ( , ).

, , before-save-hook . - :

(add-hook 'c-mode-common-hook 
     (lambda ()) (add-hook (make-local-variable 'before-save-hook) 
                           'indent-it-all))
(defun indent-it-all ()
  "indent the buffer using indent"
  (shell-command-on-region (point-min) (point-max) "indent" t t))
+2

This is what I use personally, and I think it meets all your indentation needs, you can try it by adding code to your .emacs file. This function takes care of the correct indentation when I write the code:

(defun linux-c-indent ()
  "adjusted defaults for C/C++ mode use"
  (interactive)
  (setq tab-width 8)
  (setq indent-tabs-mode nil) ;;force spaces, to work with dumber editors
  (setq c-basic-offset 8)
  (setq c-set-style "K&R"))

(add-hook 'c-mode-common-hook 'linux-c-indent)

And if I want to retype files written by other people, I use the following:

(defun iwb ()
  "indent whole buffer"
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

The iwb function runs every time I open the "programming" file in emacs. Thus, I always see well-coded code. I can put this bit of code too, I want this.

0
source

Source: https://habr.com/ru/post/1791878/


All Articles