help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: best to have max width of 79 or 80 in source code?


From: Xah Lee
Subject: Re: best to have max width of 79 or 80 in source code?
Date: Sun, 30 Nov 2008 18:54:39 -0800 (PST)
User-agent: G2/1.0

On Nov 30, 5:45 pm, "seber...@spawar.navy.mil"
<seber...@spawar.navy.mil> wrote:
> On Nov 30, 12:50 pm,XahLee<xah...@gmail.com> wrote:
>
> > in my opinion, hard wrappnig lines or in general any manual code
> > formatting is a major damage in computing, to the degree that
> > influenced bad tools and language design which forces humans to think
> > and do what dumb machines can easily automate. The line wrapping
> > convention largely spurred the perpetual time wasted on arguing about
> > code formatting and tools about it.
>
> I read some of the links you posted.  I like idea of Emacs
> automagically handling wrapping but one of those links seemed to
> suggest that C and Python and Perl weren't amenable to this.

One cannot auto-format Python source code due to the fact that
formatting is part of the syntax, but C and Perl can to a large
extent. (the automatting needn't be something like 100% “perfect” to
be useful. (but it should be 100% correct. That is, the reformatted
code should not break the code))

actually, having learned a lot elisp in the past couple of years, i
think it's rather trivial to do. Basically, the command will just need
to inseart line breaks in the right places in the source code, then
call indent-region. (i.e. utilizing emacs's existing functions that
handle indentation for each lang.)

The tricky part would be to not break lines if it is inside string.
But there are existing function(s) to determine if cursor is inside
string. Also, lines must not be cut when inside comment block.

Overall, the bulk of effort for this to work is massive amount of
testing. e.g. Apply this to all source code in a large project, and
compile and test if the program still runs. (this can be done for
example for lisp mode on all elisp source code in emacs)

-----------------------
So, in summary, each lang mode should have these functions:

format-block
format-buffer (current buffer)

The format-block will just reformat the block of code the cursor is
on. (a block can be defined as between 2 newlines) The fomat-block
will have a easy keyboard shortcut of course.

Also, for each lang mode, it'll have a local variable “format-style”,
whose value can be something like: logical, BSD-style, GNU-style,
condensed (all white spaces removed).

So, a user can just set one of these. (as opposed to setting 10 or so
variables related to indentation style and brackets placement style
and tabs/spaces style.)

-------------------------

with the above, coder simply press a button and have his current block
or buffer or files all reformatted to a style he likes. Instead of
each coder pressing tab or delete or fill-* etc on a line-by-line
basis as is currently practiced.

this format-block can be tied to Enter key, so that it becomes
automatic. (similar to, for example, how word processors auto wrap
lines, started in about early 1990) Today, the Mathematica language
autoformat code, even its source code is perhaps the most complex of
all computer languages, since the source code also handles math
formulas and on-the-fly rendering to traditional math notation (e.g.
TeX). Mathematica has this auto format feature since ~1997.

----------------

the above sounds fancy, but it's is rather very trivial to do with
respect to today's technology. (for most lang's source code anyway,
such as C, Java, C++, Perl, Ruby, lisp etc) If one looks at IDEs, lots
more much more harder problems have been solved. As far as i know,
there are also standalone tools that reformat source code. I know Java
has, and i think C has something called lint. There's also HTML::Lint.

As examples of much harder problem being solved... e.g. Steve Yegge
recently implement Javascript in elisp. And google implemneted
Javascript that compiles to native code, for their Google Chrome
browser.

Emacs being what it is, this as a programing problem is easily handled
in elisp, and this feature is the most fitting for emacs.

-------------------

i will code this eventually just for my own use. (using a simple
implementation on top of emacs's various indentation functions, as
described above) In any case, for those elisp developer reading this,
i think it would be quite a killer app for emacs.

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]