Discussion:
How to set Emacs indentation style in batch mode
(too old to reply)
Bob Whitaker
2004-10-26 01:28:54 UTC
Permalink
Hello,

My end goal is to create a "pretty print" utility to indent poorly
indented C++ code automatically.

I have been successful by using XEmacs in batch mode but the only
problem is that it defaults to "gnu" indentation style, whereas I
would like "stroustrup" indentation style. Does anyone know how to do
this?

Here is the simple "emacs_pretty_print.el" file which I use:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILE
;; ====
;; emacs_pretty_print.el
;;
;; USAGE
;; =====
;; Invoke "xemacs" as follows:
;;
;; xemacs -batch MyFile.cpp -load emacs_pretty_print.el
;;
(setq-default c-default-style "stroustrup")
(setq-default c-basic-offset 3)
(setq-default indent-tabs-mode nil)
(untabify (point-min) (point-max))
(indent-region (point-min) (point-max) nil)
(save-buffer)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Unfortunately this ".el" file does not work 100%. The formatted code
is in "gnu" style instead of "stroustrup" style. Notice that the other
"setq-default" parameters are set correctly (offset 3, and tabs nil).
I can change "c-basic-offset" to 2 or 4 and I can change
"indent-tabs-mode" to t or nil to verify correct operation. The only
problem is that "c-default-style" seems to be ignored. I have also
tried other modes like "ellemtel" without success.

I have searched Usenet archives and I have tried numerous other
combinations, such as follows:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
(setq-default c-set-style "stroustrup")
(setq c-set-style "stroustrup")
(setq-default c-default-style "stroustrup")
(setq c-default-style "stroustrup")
(add-hook 'c-mode-hook '(lambda() (c++-mode)))
(add-hook 'c-mode-common-hook '(lambda() (c-set-style "stroustrup")))
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

None of the above combinations works either. I have tried this using
the following version of Xemacs:

galaxy>xemacs -V
XEmacs 20.4 "Emerald" [Lucid] (sparc-sun-solaris2.6) of Wed May 16
2001 on guild

There must be a simple way to set the indentation style in batch mode,
but I just can't figure it out... Any help would be appreciated.

Thanks,

Bob
Aidan Kehoe
2004-10-26 09:26:30 UTC
Permalink
I have been successful by using XEmacs in batch mode but the only problem
is that it defaults to "gnu" indentation style, whereas I would like
"stroustrup" indentation style. Does anyone know how to do this?
Yes. Try '(c-set-style "stroustrup")' instead of '(setq-default
c-default-style "stroustrup")'; the former modifies the environment for the
current file, the latter changes a global default, which isn't much use to
you because it doesn't apply to the current file. (The current C indentation
style has already been initialised.)
--
Like the early Christians, Marx expected the millennium very soon; like
their successors, his have been disappointed--once more, the world has shown
itself recalcitrant to a tidy formula embodying the hopes of some section of
mankind. (Russell)
Bob Whitaker
2004-10-26 14:41:43 UTC
Permalink
Thanks to both!!! You were both right!!! The script now works great.
For the archives, here is my current implementation (works OK):

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILE
;; ====
;; emacs_pretty_print.el
;;
;; USAGE
;; =====
;; Invoke "xemacs" as follows:
;;
;; xemacs -batch MyFile.cpp -load emacs_pretty_print.el
;;
(c-set-style "stroustrup")
(setq-default c-basic-offset 3)
(setq-default indent-tabs-mode nil)
(untabify (point-min) (point-max))
(indent-region (point-min) (point-max) nil)
(save-buffer)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Loading...