Thanks for the nifty code, Klaus. Your `my-cc-mode-return' function led me to wonder if there was a way to make auto-fill/filladapt obey string semantics. It'd be slick if hitting space after the fill-column would close the current string and re-open it in the proper way on the next line. I looked at the lisp code for auto-fill/filladapt but did not see any obvious hooks to make this work. Anyone have an idea?
/Nick -----Original Message----- From: Berndl, Klaus [mailto:[EMAIL PROTECTED]] Sent: Thursday, September 12, 2002 3:10 AM To: 'Matthias Papesch '; '[EMAIL PROTECTED] ' Subject: RE: Javadoc comments Hi! Smart ENTER in all cc-mode based major-modes (so also for jde-mode): ==================================================================== Here is what i have bound to RET in my c-mode-common-hook (jde-mode inherits all this!): (defun my-cc-mode-return () "Intelligent line breaking in all cc-modes. Handles strings in a smart way" (interactive) (cond ((eq (c-in-literal) 'string) (if (or (eq major-mode 'java-mode) (eq major-mode 'jde-mode)) (insert-and-inherit "\"+") (insert-and-inherit "\"")) (c-context-line-break) (insert "\" ") ;; cause of strings in c will be concatenated we ;; must add a single space. (end-of-line)) (t (c-context-line-break)))) With this function you will get best and smartest results after hitting ENTER regardless if in code, if within a string or within a comment. Some more maybe usefull settings: ================================= Here are some of my other cc-mode common settings which should be also useful for java (all this code is added to c-mode-common-hook!): ;; comments should be treated as whitespace in paren-matching, jumping ;; etc. (setq parse-sexp-ignore-comments t) ;; we like auto-newline and hungry-delete (c-toggle-auto-hungry-state 1) ;; keybindings for C, C++, and Objective-C. We can put these in ;; c-mode-base-map because c++-mode-map and java-mode-map and ;; c-mode-map inherit it. (define-key c-mode-base-map "\C-m" 'my-cc-mode-return) ;; lines of a c-block-comment should begin with a "" (set-variable 'c-block-comment-prefix "") ;; we want no autofilling within cpp-makros string-literals and normal code (set-variable 'c-ignore-auto-fill '(string cpp code)) ;; turn on adaptive auto-fill-mode for max. easy writing comments (auto-fill-mode 1) (c-setup-filladapt) (turn-on-filladapt-mode) ;; no automatic newlines after ';' if following line is non blank or ;; inside oneline inline methods (add-to-list 'c-hanging-semi&comma-criteria 'c-semi&comma-no-newlines-before-nonblanks) (add-to-list 'c-hanging-semi&comma-criteria 'c-semi&comma-no-newlines-for-oneline-inliners) Maybe someone find these settings also useful. Block closing comments: ======================= Ok, and here comes codes which add automatically after every closing } auto. after hitting } a block-closing comment so you see for large blocks which statement is closed by }: To get the following code below working you have to add (block-close . my-c-snug-do-while) to c-hanging-braces-alist, so e.g. a sensefull setting could be: (c-hanging-braces-alist . ((substatement-open . my-set-begin-block) (brace-list-open) (brace-list-close) (brace-list-intro) (brace-entry-open) (block-close . my-c-snug-do-while))) Here comes the code: ;; ------------------------------------------------------------------------ ;; comment block-close ;; The communication-channel between `my-c-snug-do-while' and ;; `comment-block-close': `block-close-data' is set during the call of the ;; ACTION-function `my-c-snug-do-while' and evaluated within the ;; `c-special-indent-hook' (here `comment-block-close', s.b.) ;; If not nil it is a list of the following elements: ;; 1. The text of the comment to be inserted behind the closing brace of ;; the block. ;; 2. The exact position at which the closing brace was inserted ;; 3. The size of the block in lines (defvar block-close-data nil) ;; define here how many lines a block at least must contain so the ;; block-close-comment will be added. (defcustom c-block-close-min-size-to-comment 15 "*Define how many lines a block at least must contain so the block-close-comment will be added." :type 'integer :group 'c) (defun comment-block-close () "For all blocks unless do-while-blocks this function adds after the block-close-brace the block-begin-statement as comment, for example: for \(i = 1; i < 6; i++\) \{ cout << i; ... \} // for \(i = 1; i < 6; i++\) Already hitting the '\}' inserts the comment! This works only if the autonewline-feature of cc-mode is turned on!" (if (and block-close-data (> (nth 2 block-close-data) c-block-close-min-size-to-comment)) ;; Because this function is hooked to c-special-indent-hook and ;; therefore called after EVERY indentation, we do the ;; comment-insert only if we have a block and itīs not a do-while and ;; block is larger the 'block-close-min-size-to-comment' lines (save-excursion ;; frirst we must do some stuff to go to the right position for the ;; block-close-comment: (goto-char (nth 1 block-close-data)) (if (not (looking-at "[ \t]*}")) (next-line 1)) (end-of-line) (forward-sexp -1) (forward-sexp 1) ;; now we are on the right position to insert the comment (when (not (looking-at (concat "[ \t]*" comment-start))) ;; only inserting the comment if not already one exists (insert (concat " " comment-start (progn ;; remove the open { from the comment if there ;; is one (string-match "^\\([^{]+\\){?" (nth 0 block-close-data)) (match-string 1 (nth 0 block-close-data))) comment-end))))) ;; Because this function is called after EVERY (not only in combination ;; with block-closes!) indentation, we must set this variable ;; always to nil at the end! (setq block-close-data nil)) ;; like c-snug-do-while but in addition this one sets the variable ;; block-close-data (s.a.) appropriate. Only during the call of an ;; ACTION-function we have the information about the syntactic symbol of ;; current line (argument 'syntax') and the insert-position of the ;; block-close-symbol ((argument 'pos'). But we canīt insert any text ;; within an ACTION function, so we must do this outside in the ;; c-special-indent-hook called after EVERY indentation. To supply this ;; hook with the necessary information we set the variable ;; block-close-data (s.a.) within the ACTION function. This code is a only ;; slightly modified version of the original c-snug-do-while and works ;; exactly like the original one! (defun my-c-snug-do-while (syntax pos) (save-excursion (let ((curr-point (point)) langelem bc-comment-text bc-brace-inserted-pos bc-blocksize) (if (and (eq syntax 'block-close) (setq langelem (assq 'block-close c-syntactic-context)) (progn (goto-char (cdr langelem)) (setq bc-blocksize (count-lines curr-point (point))) (if (= (following-char) ?{) (forward-sexp -1)) (if (looking-at "\\<do\\>[^_]") ;; set block-close-data to nil but return t ;; because we are in a do-while-loop (not (setq block-close-data nil)) (end-of-line) (let ((end (point))) (beginning-of-line) (skip-chars-forward " \t") (setq bc-comment-text (buffer-substring (point) end))) (setq bc-brace-inserted-pos pos) ;; set block-close-data to the computed data-list ;; but return nil because we are NOT in a ;; do-while-loop. (not (setq block-close-data `(,bc-comment-text ,bc-brace-inserted-pos ,bc-blocksize)))))) ;; the if-condition is only t if we are in a do-while-loop (if (memq 'before c-end-block-brace-newline) '(before) nil) c-end-block-brace-newline)))) Works at least with cc-mode 5.28 and 5.27! Ciao, klaus -----Original Message----- From: Matthias Papesch To: [EMAIL PROTECTED] Sent: 12.09.02 08:21 Subject: Re: Javadoc comments Hi, have you tried M-j ? C-h k yields: M-j runs the command c-indent-new-comment-line which is an interactive compiled Lisp function in `cc-cmds'. (c-indent-new-comment-line &optional SOFT) Break line at point and indent, continuing comment if within one. If inside a comment and `comment-multi-line' is non-nil, the indentation and line prefix are preserved (see the `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for details). If inside a single line comment and `comment-multi-line' is nil, a new comment of the same type is started on the next line and indented as appropriate for comments. If a fill prefix is specified, it overrides all the above. Regards, Matthias "Daniel Hegyi" <[EMAIL PROTECTED]> writes: > I know. However, is there a way of getting this functionality when > Starting a new line (i.e., hitting ENTER)? I'd like to get an asterisk > Then as well. > > Daniel > > > > > I discovered by accident that if you turn on auto-fill-mode and > > Begin typing > > Within a javadoc comment, the line will automatically wrap, add an > > Asterisk, > >And indent to the appropriate place within the comment. > > > >- david > > > >-----original Message----- > >From: Daniel Hegyi [mailto:[EMAIL PROTECTED]] > >Sent: Wednesday, September 11, 2002 4:23 PM > >To: [EMAIL PROTECTED] > >Subject: Javadoc comments > > > > > >Hi, > > > >When I'm typing a javadoc comment I'd like to have every new line > >Automatically start with an asterisk. How can I do this? > > > >Thanks, > >Daniel > > > >_________________________________________________________________ > >Msn Photos is the easiest way to share and print your photos: > >Http://photos.msn.com/support/worldwide.aspx > > _________________________________________________________________ > Send and receive Hotmail on your mobile device: http://mobile.msn.com -- Registered Linux User #229038 http://counter.li.org