all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using indent program as filter to automatically view read-only C files
@ 2006-02-03 11:46 juanleon1
  2006-02-03 18:46 ` Kevin Rodgers
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: juanleon1 @ 2006-02-03 11:46 UTC (permalink / raw)


Hi,

I have to work with C/C++ files with a very ugly and inconsistent
indentation (many developers adding things with no style guide). This
is very distracting, and since I cannot change them (to avoid conflicts
when taking/carryng changes from/to other branches), I had think that
for read-only files (those that I have not opened in the revision
control system), it would be nice if emacs could run automagically the
"indent" program so I can see the code "beatyfully" indented.

I didn't find anything obvious on the net for that. I do not wanna
modify files in disk, only to make emacs to put the indent output in
the buffer without changing variables like `buffer-file-name' and so. I
would like this to be done in a transparent way when opening a C/C++
file (via find-file, find-tag or whatever).

Before trying to code something like that myself (and most probably
reinvent the wheel), I would like to know if anybody knows if this (or
something similar enough I could reuse/modify) is already available.

Any ideas welcome.

Thanks
juanleon

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C   files
  2006-02-03 11:46 Using indent program as filter to automatically view read-only C files juanleon1
@ 2006-02-03 18:46 ` Kevin Rodgers
  2006-02-03 19:06 ` François Gannaz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2006-02-03 18:46 UTC (permalink / raw)


juanleon1@gmail.com wrote:
> I have to work with C/C++ files with a very ugly and inconsistent
> indentation (many developers adding things with no style guide). This
> is very distracting, and since I cannot change them (to avoid conflicts
> when taking/carryng changes from/to other branches), I had think that
> for read-only files (those that I have not opened in the revision
> control system), it would be nice if emacs could run automagically the
> "indent" program so I can see the code "beatyfully" indented.
> 
> I didn't find anything obvious on the net for that. I do not wanna
> modify files in disk, only to make emacs to put the indent output in
> the buffer without changing variables like `buffer-file-name' and so. I
> would like this to be done in a transparent way when opening a C/C++
> file (via find-file, find-tag or whatever).
> 
> Before trying to code something like that myself (and most probably
> reinvent the wheel), I would like to know if anybody knows if this (or
> something similar enough I could reuse/modify) is already available.
> 
> Any ideas welcome.

(add-hook 'find-file-hooks
           (lambda ()
             (when (and buffer-read-only
                        (memq major-mode '(c-mode c++-mode)))
               (let ((buffer-modified-p (buffer-modified-p))
                     (inhibit-read-only t))
                 (shell-command-on-region (point-min) (point-max) "indent"
                                          nil t nil)
                 (set-buffer-modified-p buffer-modified-p)
                 ;; (set-visited-file-modtime)
                 ))))

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C files
  2006-02-03 11:46 Using indent program as filter to automatically view read-only C files juanleon1
  2006-02-03 18:46 ` Kevin Rodgers
@ 2006-02-03 19:06 ` François Gannaz
  2006-02-03 19:39 ` Eli Zaretskii
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: François Gannaz @ 2006-02-03 19:06 UTC (permalink / raw)


Le ven 03 fév 03:46, juanleon1@gmail.com a écrit :
> Hi,
> 
> I have to work with C/C++ files with a very ugly and inconsistent
> indentation (many developers adding things with no style guide). This
> is very distracting, and since I cannot change them (to avoid conflicts
> when taking/carryng changes from/to other branches), I had think that
> for read-only files (those that I have not opened in the revision
> control system), it would be nice if emacs could run automagically the
> "indent" program so I can see the code "beatyfully" indented.

Why would you use the indent program when emacs can do it itself? You
can use indent-region, bounded to C-M-\

> I didn't find anything obvious on the net for that. I do not wanna
> modify files in disk, only to make emacs to put the indent output in
> the buffer without changing variables like `buffer-file-name' and so. I
> would like this to be done in a transparent way when opening a C/C++
> file (via find-file, find-tag or whatever).

You might want to add an indent-region call (with the right params) to
your c-mode-hook to make it transparent. Following it with
(set-buffer-modified-p nil) will avoid emacs asking for saving the
buffer on quitting.
If you don't know yet how to program in lisp, it's a good way to
learn :) And if you don't want to, ask for help here.

If you still want to use the indent program, have a look at
shell-command-on-region that should allow you to achieve what you
described.

Hope it helps
--
François Gannaz

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C files
  2006-02-03 11:46 Using indent program as filter to automatically view read-only C files juanleon1
  2006-02-03 18:46 ` Kevin Rodgers
  2006-02-03 19:06 ` François Gannaz
@ 2006-02-03 19:39 ` Eli Zaretskii
  2006-02-03 23:10   ` Harry Putnam
       [not found] ` <mailman.59.1138996411.2860.help-gnu-emacs@gnu.org>
       [not found] ` <mailman.36.1138992933.2860.help-gnu-emacs@gnu.org>
  4 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2006-02-03 19:39 UTC (permalink / raw)


> From: juanleon1@gmail.com
> Date: 3 Feb 2006 03:46:40 -0800
> 
> I have to work with C/C++ files with a very ugly and inconsistent
> indentation (many developers adding things with no style guide). This
> is very distracting, and since I cannot change them (to avoid conflicts
> when taking/carryng changes from/to other branches), I had think that
> for read-only files (those that I have not opened in the revision
> control system), it would be nice if emacs could run automagically the
> "indent" program so I can see the code "beatyfully" indented.
> 
> I didn't find anything obvious on the net for that. I do not wanna
> modify files in disk, only to make emacs to put the indent output in
> the buffer without changing variables like `buffer-file-name' and so. I
> would like this to be done in a transparent way when opening a C/C++
> file (via find-file, find-tag or whatever).
> 
> Before trying to code something like that myself (and most probably
> reinvent the wheel), I would like to know if anybody knows if this (or
> something similar enough I could reuse/modify) is already available.

Take a look at format.el, it has a machinery to filter a file through
a program when Emacs visits the file.  That machinery was invented for
a different purpose, but perhaps it is general enough to serve yours.

Btw, Emacs itself can reindent, so one easy way og doing what you want
is to copy the contents of the file into another buffer, set its major
mode to C, and then reindent there.  If the buffer has no file name,
you don't risk to overwrite the original files.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C files
  2006-02-03 19:39 ` Eli Zaretskii
@ 2006-02-03 23:10   ` Harry Putnam
  0 siblings, 0 replies; 7+ messages in thread
From: Harry Putnam @ 2006-02-03 23:10 UTC (permalink / raw)


Eli Zaretskii <eliz@gnu.org> writes:

> Take a look at format.el, it has a machinery to filter a file through
> a program when Emacs visits the file.  That machinery was invented for
> a different purpose, but perhaps it is general enough to serve yours.
>
> Btw, Emacs itself can reindent, so one easy way og doing what you want
> is to copy the contents of the file into another buffer, set its major
> mode to C, and then reindent there.  If the buffer has no file name,
> you don't risk to overwrite the original files.

This may be a little off the wall but after looking at format.el I
could not tell if it can do something like format a log file when emacs
opens one.  Something like just formating each line to column 72 or
such.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C files
       [not found] ` <mailman.59.1138996411.2860.help-gnu-emacs@gnu.org>
@ 2006-02-06  4:02   ` Paul Whitfield
  0 siblings, 0 replies; 7+ messages in thread
From: Paul Whitfield @ 2006-02-06  4:02 UTC (permalink / raw)


François Gannaz wrote:
> Le ven 03 fév 03:46, juanleon1@gmail.com a écrit :
>> Hi,
>>
>> I have to work with C/C++ files with a very ugly and inconsistent
>> indentation (many developers adding things with no style guide). This
>> is very distracting, and since I cannot change them (to avoid conflicts
>> when taking/carryng changes from/to other branches), I had think that
>> for read-only files (those that I have not opened in the revision
>> control system), it would be nice if emacs could run automagically the
>> "indent" program so I can see the code "beatyfully" indented.
> 
> Why would you use the indent program when emacs can do it itself? You
> can use indent-region, bounded to C-M-\

Because there are limits to the indention in emacs...
namely that it will not fix misplaced braces in C/C++

I.e. if you prefered style is K&R

if ( thing ) {
	....
}

and the code is indented as

if ( thing )
{
	...
}


(Or vice-versa).


I generally find that Gnu Indent with a suitably setup rc file
is very useful for doing this type of indenting / conversion.

(It is also a lot quicker to run it over large numbers
of files).

Perhaps it can be combined with the other suggestions of format.el
to do what you require.

Regards

Paul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using indent program as filter to automatically view read-only C files
       [not found] ` <mailman.36.1138992933.2860.help-gnu-emacs@gnu.org>
@ 2006-02-06 15:27   ` juanleon1
  0 siblings, 0 replies; 7+ messages in thread
From: juanleon1 @ 2006-02-06 15:27 UTC (permalink / raw)


Simple, elegant and it works fine.

Thanks!

I forgot to say this, but some of the files I need to work with are
huge, so indent-region was not the optimal solution for me because I
like emacs opening files very fast.

This solution based on "indent" is fine for me (indent is fast), as
writing a indentrc file that mimic my c-offset and other preferences I
have in my emacs config is easy.

Thanks again
juanleon

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-02-06 15:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-03 11:46 Using indent program as filter to automatically view read-only C files juanleon1
2006-02-03 18:46 ` Kevin Rodgers
2006-02-03 19:06 ` François Gannaz
2006-02-03 19:39 ` Eli Zaretskii
2006-02-03 23:10   ` Harry Putnam
     [not found] ` <mailman.59.1138996411.2860.help-gnu-emacs@gnu.org>
2006-02-06  4:02   ` Paul Whitfield
     [not found] ` <mailman.36.1138992933.2860.help-gnu-emacs@gnu.org>
2006-02-06 15:27   ` juanleon1

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.