In cc mode, I like to customize <tab> behavior as follows.
If the pointer is before a statement, then <tab> is bind to c-indent- command, otherwise (or if the pointer is after a statement), <tab> is bind to indent-relative.
whzhao@gmail com writes: > If the pointer is before a statement, then <tab> is bind to c-indent- > command, otherwise (or if the pointer is after a statement), <tab> is > bind to indent-relative. > How to do this???
Nice idea. The code bellow is barely tested and works not only in C-mode:
(defun indent-dwim () "Indent line or move point to the next indent point." (interactive "*") (if (looking-back "^\\s-*") (indent-for-tab-command) (indent-relative)))
-- Emílio C. Lopes Ich leb und weiß nit wie lang, Munich, Germany ich stirb und weiß nit wann, ich fahr und weiß nit wohin, (Martinus von Biberach) mich wundert, dass ich fröhlich bin!
"whz...@gmail.com" <whz...@gmail.com> writes: > In cc mode, I like to customize <tab> behavior as follows.
What version of Emacs?
In the current CVS emacs (about to be released as Emacs 22), the document for c-indent-command (bounded to TAB) says:
TAB (translated from <tab>) runs the command c-indent-command ... Indent current line as C code, and/or insert some whitespace.
If `c-tab-always-indent' is t, always just indent the current line. If nil, indent the current line only if point is at the left margin or in the line's indentation; otherwise insert some whitespace[*]. If other than nil or t, then some whitespace[*] is inserted only within literals (comments and strings), but the line is always reindented. ... [*] The amount and kind of whitespace inserted is controlled by the variable `c-insert-tab-function', which is called to do the actual insertion of whitespace. Normally the function in this variable just inserts a tab character, or the equivalent number of spaces, depending on the variable `indent-tabs-mode'.
-Miles
-- P.S. All information contained in the above letter is false, for reasons of military security.
On 24 May 2007 15:17:31 -0700, "whz...@gmail.com" <whz...@gmail.com> wrote:
> Hi, > In cc mode, I like to customize <tab> behavior as follows.
> If the pointer is before a statement, then <tab> is bind to c-indent- > command, otherwise (or if the pointer is after a statement), <tab> is > bind to indent-relative.
> How to do this???
Look at the documentation of `c-indent-command'. It contains several useful hints. Unless I misunderstood what you wrote, one of them is exactly what you are looking for:
,---[ C-h f c-indent-command ]------------------------------------------ | c-indent-command is an interactive compiled Lisp function in `cc-cmds.el'. | (c-indent-command &optional ARG) | | Indent current line as C code, and/or insert some whitespace. | | If `c-tab-always-indent' is t, always just indent the current line. | If nil, indent the current line only if point is at the left margin or | in the line's indentation; otherwise insert some whitespace[*]. If | other than nil or t, then some whitespace[*] is inserted only within | literals (comments and strings), but the line is always reindented. | | If `c-syntactic-indentation' is t, indentation is done according to | the syntactic context. A numeric argument, regardless of its value, | means indent rigidly all the lines of the expression starting after | point so that this line becomes properly indented. The relative | indentation among the lines of the expression is preserved. | | If `c-syntactic-indentation' is nil, the line is just indented one | step according to `c-basic-offset'. In this mode, a numeric argument | indents a number of such steps, positive or negative, and an empty | prefix argument is equivalent to -1. | | [*] The amount and kind of whitespace inserted is controlled by the | variable `c-insert-tab-function', which is called to do the actual | insertion of whitespace. Normally the function in this variable | just inserts a tab character, or the equivalent number of spaces, | depending on the variable `indent-tabs-mode'. `-----------------------------------------------------------------------
> ,---[ C-h f c-indent-command ]------------------------------------------ > | c-indent-command is an interactive compiled Lisp function in `cc-cmds.el'. > | (c-indent-command &optional ARG) > | > | Indent current line as C code, and/or insert some whitespace. > | > | If `c-tab-always-indent' is t, always just indent the current line.