[comp.emacs] C++-mode and abbrevs

fuchs@it.uka.de (Harald Fuchs) (11/30/90)

I have a problem with the version of c++-mode.el beginning like that:
  ;; C++ code editing commands for Emacs
  ;; 1987 Dave Detlefs  (dld@cs.cmu.edu)
  ;; and  Stewart Clamen (clamen@cs.cmu.edu).
  ;; Done by fairly faithful modification of:
  ;; c-mode.el, Copyright (C) 1985 Richard M. Stallman.
  ;;
  ;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu)
  ;;   Fixed electric-c++-terminator to handle double colons, at the
  ;;   request of John Hagerman.

I'm a lazy typist (seems that C++ is too verbose for me), so I defined
mode-abbrevs like "reg" for "register", "pub" for "public" etc.
This works fine except in one case:

class Foo {
  pub
     ^ cursor is here

When I hit the ":" key, "pub" is correctly expanded to "public:", but
it is not moved to the beginning of the line:

class Foo {
  public:

What I would like to see is:

class Foo {
public:

Being no LISPer, I can't fix this. Any help?

My ~/.emacs file contains (among other things):
  (setq c-argdecl-indent 2)
  (setq c-tab-always-indent nil)
  (setq c++-friend-offset 0)
  (quietly-read-abbrev-file (expand-file-name "~/.emacs_abbrevs"))
  (setq c-mode-hook '(lambda () (abbrev-mode 1)))
  (setq c++-mode-hook '(lambda () (abbrev-mode 1)))
--

Harald Fuchs <fuchs@it.uka.de> <fuchs%it.uka.de@relay.cs.net> ...
<fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*

jackr@dblues.wpd.sgi.com (John Repenning) (12/04/90)

In article <fuchs.659899292@t500m0>, fuchs@it.uka.de (Harald Fuchs) writes:
|> ...
|> When I hit the ":" key, "pub" is correctly expanded to "public:", but
|> it is not moved to the beginning of the line:
|> 
|> class Foo {
|>   public:

I had a similar problem in my c++ (with the same version info at the
top as yours).  I found it and fixed it - and yes, the change was
simple.

The problem also afflicts declaration of non-inline methods - for
example:

class Foo {
public:
    int i();
}

int
Foo::i() {
    return 3;
}

Here, when you get to the first colon in ``Foo::i()'', the defective
version shoves it in one space, rather than leaving it at the left
edge.

However, I haven't put many miles on the patch, yet (I only made it
last Friday!).  My experience with c-mode (on which this c++-mode is
based) makes me a tad wary: if you apply this, you may find it breaks
something else, either for you, or for someone else who likes their
indentation different from what you (and I) like.  If there were such
a problem, it would (almost certainly) involve some other situation
where a single word, beginning a line, got incorrect indentation.

If you find such problems, I'd be interested in hearing them.  But I
can't promise I'll have time (or even expertise!) to fix them.  But,
at least backing out the change is as easy as applying it....

Here's the rcsdiff of the change.  The change is in the defun for
c++-indent-line, and the corrected version is at the bottom (``(max
0'', not ``(max 1'').

Jack Repenning


RCS file: RCS/c++-mode.el,v
retrieving revision 1.1
diff -c -r1.1 c++-mode.el
*** /tmp/,RCSt1a12615	Mon Dec  3 09:40:15 1990
--- c++-mode.el	Mon Dec  3 09:39:26 1990
***************
*** 381,387 ****
  			   (save-excursion
  			     (forward-sexp 1)
  			     (looking-at ":[^:]"))))
! 		  (setq indent (max 1 (+ indent c-label-offset))))
  		 ((and (looking-at "else\\b")
  		       (not (looking-at "else\\s_")))
  		  (setq indent (save-excursion
--- 381,387 ----
  			   (save-excursion
  			     (forward-sexp 1)
  			     (looking-at ":[^:]"))))
! 		  (setq indent (max 0 (+ indent c-label-offset))))
  		 ((and (looking-at "else\\b")
  		       (not (looking-at "else\\s_")))
  		  (setq indent (save-excursion