comparison lisp/progmodes/c-mode.el @ 5689:4a3125e60737

(c-fill-paragraph): Handle C++ mode.
author Richard M. Stallman <rms@gnu.org>
date Sat, 29 Jan 1994 01:27:32 +0000
parents d147979e10f4
children 7bea55d86ff6
comparison
equal deleted inserted replaced
5688:a0f01b43a459 5689:4a3125e60737
284 (save-excursion 284 (save-excursion
285 (beginning-of-line) 285 (beginning-of-line)
286 (skip-chars-forward " \t\n") 286 (skip-chars-forward " \t\n")
287 (and (looking-at comment-start-skip) 287 (and (looking-at comment-start-skip)
288 (setq comment-start-place (point)))))) 288 (setq comment-start-place (point))))))
289 (if (or first-line 289 (if (and (eq major-mode 'c++-mode)
290 ;; t if we enter a comment between start of function and this line. 290 (save-excursion
291 (eq (calculate-c-indent) t) 291 (beginning-of-line)
292 ;; t if this line contains a comment starter. 292 (looking-at ".*//")))
293 (setq first-line 293 (let (fill-prefix
294 (save-excursion
295 (beginning-of-line)
296 (prog1
297 (re-search-forward comment-start-skip
298 (save-excursion (end-of-line)
299 (point))
300 t)
301 (setq comment-start-place (point))))))
302 ;; Inside a comment: fill one comment paragraph.
303 (let ((fill-prefix
304 ;; The prefix for each line of this paragraph
305 ;; is the appropriate part of the start of this line,
306 ;; up to the column at which text should be indented.
307 (save-excursion
308 (beginning-of-line)
309 (if (looking-at "[ \t]*/\\*.*\\*/")
310 (progn (re-search-forward comment-start-skip)
311 (make-string (current-column) ?\ ))
312 (if first-line (forward-line 1))
313
314 (let ((line-width (progn (end-of-line) (current-column))))
315 (beginning-of-line)
316 (prog1
317 (buffer-substring
318 (point)
319
320 ;; How shall we decide where the end of the
321 ;; fill-prefix is?
322 ;; calculate-c-indent-within-comment bases its value
323 ;; on the indentation of previous lines; if they're
324 ;; indented specially, it could return a column
325 ;; that's well into the current line's text. So
326 ;; we'll take at most that many space, tab, or *
327 ;; characters, and use that as our fill prefix.
328 (let ((max-prefix-end
329 (progn
330 (move-to-column
331 (calculate-c-indent-within-comment t)
332 t)
333 (point))))
334 (beginning-of-line)
335 (skip-chars-forward " \t*" max-prefix-end)
336 (point)))
337
338 ;; If the comment is only one line followed by a blank
339 ;; line, calling move-to-column above may have added
340 ;; some spaces and tabs to the end of the line; the
341 ;; fill-paragraph function will then delete it and the
342 ;; newline following it, so we'll lose a blank line
343 ;; when we shouldn't. So delete anything
344 ;; move-to-column added to the end of the line. We
345 ;; record the line width instead of the position of the
346 ;; old line end because move-to-column might break a
347 ;; tab into spaces, and the new characters introduced
348 ;; there shouldn't be deleted.
349
350 ;; If you can see a better way to do this, please make
351 ;; the change. This seems very messy to me.
352 (delete-region (progn (move-to-column line-width)
353 (point))
354 (progn (end-of-line) (point))))))))
355
356 (paragraph-start 294 (paragraph-start
357 ;; Lines containing just a comment start or just an end 295 ;; Lines containing just a comment start or just an end
358 ;; should not be filled into paragraphs they are next to. 296 ;; should not be filled into paragraphs they are next to.
359 (concat 297 (concat
360 paragraph-start 298 paragraph-start
361 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$")) 299 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
362 (paragraph-separate 300 (paragraph-separate
363 (concat 301 (concat
364 paragraph-separate 302 paragraph-separate
365 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$")) 303 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$")))
366 (chars-to-delete 0)) 304 (save-excursion
367 (save-restriction 305 (beginning-of-line)
368 ;; Don't fill the comment together with the code following it. 306 ;; Move up to first line of this comment.
369 ;; So temporarily exclude everything before the comment start, 307 (while (and (not (bobp)) (looking-at "[ \t]*//"))
370 ;; and everything after the line where the comment ends. 308 (forward-line -1))
371 ;; If comment-start-place is non-nil, the comment starter is there. 309 (if (not (looking-at ".*//"))
372 ;; Otherwise, point is inside the comment. 310 (forward-line 1))
373 (narrow-to-region (save-excursion 311 ;; Find the comment start in this line.
374 (if comment-start-place 312 (re-search-forward "[ \t]*//[ \t]*")
375 (goto-char comment-start-place) 313 ;; Set the fill-prefix to be what all lines except the first
376 (search-backward "/*")) 314 ;; should start with.
377 ;; Protect text before the comment start 315 (let ((endcol (current-column)))
378 ;; by excluding it. Add spaces to bring back 316 (skip-chars-backward " \t")
379 ;; proper indentation of that point. 317 (setq fill-prefix
380 (let ((column (current-column))) 318 (concat (make-string (- (current-column) 2) ?\ )
381 (prog1 (point) 319 "//"
382 (setq chars-to-delete column) 320 (make-string (- endcol (current-column)) ?\ ))))
383 (insert-char ?\ column)))) 321 (save-restriction
384 (save-excursion 322 ;; Narrow down to just the lines of this comment.
385 (if comment-start-place 323 (narrow-to-region (point)
386 (goto-char (+ comment-start-place 2))) 324 (save-excursion
387 (search-forward "*/" nil 'move) 325 (forward-line 1)
388 (forward-line 1) 326 (while (looking-at "[ \t]*//"))
389 (point))) 327 (forward-line 1))
390 328 (point)))
391 (fill-paragraph arg) 329 (insert fill-prefix)
392 (save-excursion 330 (fill-paragraph arg)
393 ;; Delete the chars we inserted to avoid clobbering 331 (delete-region (point-min)
394 ;; the stuff before the comment start. 332 (+ (point-min) (length fill-prefix))))))
395 (goto-char (point-min)) 333 (if (or first-line
396 (if (> chars-to-delete 0) 334 ;; t if we enter a comment between start of function and this line.
397 (delete-region (point) (+ (point) chars-to-delete))) 335 (eq (calculate-c-indent) t)
398 ;; Find the comment ender (should be on last line of buffer, 336 ;; t if this line contains a comment starter.
399 ;; given the narrowing) and don't leave it on its own line. 337 (setq first-line
400 (goto-char (point-max)) 338 (save-excursion
401 (forward-line -1) 339 (beginning-of-line)
402 (search-forward "*/" nil 'move) 340 (prog1
403 (beginning-of-line) 341 (re-search-forward comment-start-skip
404 (if (looking-at "[ \t]*\\*/") 342 (save-excursion (end-of-line)
405 (delete-indentation))))) 343 (point))
406 ;; Outside of comments: do ordinary filling. 344 t)
407 (fill-paragraph arg)))) 345 (setq comment-start-place (point))))))
346 ;; Inside a comment: fill one comment paragraph.
347 (let ((fill-prefix
348 ;; The prefix for each line of this paragraph
349 ;; is the appropriate part of the start of this line,
350 ;; up to the column at which text should be indented.
351 (save-excursion
352 (beginning-of-line)
353 (if (looking-at "[ \t]*/\\*.*\\*/")
354 (progn (re-search-forward comment-start-skip)
355 (make-string (current-column) ?\ ))
356 (if first-line (forward-line 1))
357
358 (let ((line-width (progn (end-of-line) (current-column))))
359 (beginning-of-line)
360 (prog1
361 (buffer-substring
362 (point)
363
364 ;; How shall we decide where the end of the
365 ;; fill-prefix is?
366 ;; calculate-c-indent-within-comment bases its value
367 ;; on the indentation of previous lines; if they're
368 ;; indented specially, it could return a column
369 ;; that's well into the current line's text. So
370 ;; we'll take at most that many space, tab, or *
371 ;; characters, and use that as our fill prefix.
372 (let ((max-prefix-end
373 (progn
374 (move-to-column
375 (calculate-c-indent-within-comment t)
376 t)
377 (point))))
378 (beginning-of-line)
379 (skip-chars-forward " \t*" max-prefix-end)
380 (point)))
381
382 ;; If the comment is only one line followed by a blank
383 ;; line, calling move-to-column above may have added
384 ;; some spaces and tabs to the end of the line; the
385 ;; fill-paragraph function will then delete it and the
386 ;; newline following it, so we'll lose a blank line
387 ;; when we shouldn't. So delete anything
388 ;; move-to-column added to the end of the line. We
389 ;; record the line width instead of the position of the
390 ;; old line end because move-to-column might break a
391 ;; tab into spaces, and the new characters introduced
392 ;; there shouldn't be deleted.
393
394 ;; If you can see a better way to do this, please make
395 ;; the change. This seems very messy to me.
396 (delete-region (progn (move-to-column line-width)
397 (point))
398 (progn (end-of-line) (point))))))))
399
400 (paragraph-start
401 ;; Lines containing just a comment start or just an end
402 ;; should not be filled into paragraphs they are next to.
403 (concat
404 paragraph-start
405 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
406 (paragraph-separate
407 (concat
408 paragraph-separate
409 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
410 (chars-to-delete 0))
411 (save-restriction
412 ;; Don't fill the comment together with the code following it.
413 ;; So temporarily exclude everything before the comment start,
414 ;; and everything after the line where the comment ends.
415 ;; If comment-start-place is non-nil, the comment starter is there.
416 ;; Otherwise, point is inside the comment.
417 (narrow-to-region (save-excursion
418 (if comment-start-place
419 (goto-char comment-start-place)
420 (search-backward "/*"))
421 ;; Protect text before the comment start
422 ;; by excluding it. Add spaces to bring back
423 ;; proper indentation of that point.
424 (let ((column (current-column)))
425 (prog1 (point)
426 (setq chars-to-delete column)
427 (insert-char ?\ column))))
428 (save-excursion
429 (if comment-start-place
430 (goto-char (+ comment-start-place 2)))
431 (search-forward "*/" nil 'move)
432 (forward-line 1)
433 (point)))
434 (fill-paragraph arg)
435 (save-excursion
436 ;; Delete the chars we inserted to avoid clobbering
437 ;; the stuff before the comment start.
438 (goto-char (point-min))
439 (if (> chars-to-delete 0)
440 (delete-region (point) (+ (point) chars-to-delete)))
441 ;; Find the comment ender (should be on last line of buffer,
442 ;; given the narrowing) and don't leave it on its own line.
443 (goto-char (point-max))
444 (forward-line -1)
445 (search-forward "*/" nil 'move)
446 (beginning-of-line)
447 (if (looking-at "[ \t]*\\*/")
448 (delete-indentation)))))
449 ;; Outside of comments: do ordinary filling.
450 (fill-paragraph arg)))))
408 451
409 (defun electric-c-brace (arg) 452 (defun electric-c-brace (arg)
410 "Insert character and correct line's indentation." 453 "Insert character and correct line's indentation."
411 (interactive "P") 454 (interactive "P")
412 (let (insertpos) 455 (let (insertpos)