Mercurial > emacs
comparison lisp/newcomment.el @ 33705:273d5b4aba82
(comment-indent): Insert comment before calling
comment-indent-function. Don't insert in column 0.
(comment-dwim): Indent before inserting comment.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Tue, 21 Nov 2000 21:31:16 +0000 |
parents | decc878ec612 |
children | 3aa61201a0ab |
comparison
equal
deleted
inserted
replaced
33704:f14d787f4b33 | 33705:273d5b4aba82 |
---|---|
4 | 4 |
5 ;; Author: code extracted from Emacs-20's simple.el | 5 ;; Author: code extracted from Emacs-20's simple.el |
6 ;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> | 6 ;; Maintainer: Stefan Monnier <monnier@cs.yale.edu> |
7 ;; Keywords: comment uncomment | 7 ;; Keywords: comment uncomment |
8 ;; Version: $Name: $ | 8 ;; Version: $Name: $ |
9 ;; Revision: $Id: newcomment.el,v 1.23 2000/11/14 10:03:56 monnier Exp $ | 9 ;; Revision: $Id: newcomment.el,v 1.24 2000/11/14 15:09:40 monnier Exp $ |
10 | 10 |
11 ;; This file is part of GNU Emacs. | 11 ;; This file is part of GNU Emacs. |
12 | 12 |
13 ;; GNU Emacs is free software; you can redistribute it and/or modify | 13 ;; GNU Emacs is free software; you can redistribute it and/or modify |
14 ;; it under the terms of the GNU General Public License as published by | 14 ;; it under the terms of the GNU General Public License as published by |
416 (looking-at "[ \t]*$"))) | 416 (looking-at "[ \t]*$"))) |
417 (starter (or (and continue comment-continue) | 417 (starter (or (and continue comment-continue) |
418 (and empty block-comment-start) comment-start)) | 418 (and empty block-comment-start) comment-start)) |
419 (ender (or (and continue comment-continue "") | 419 (ender (or (and continue comment-continue "") |
420 (and empty block-comment-end) comment-end))) | 420 (and empty block-comment-end) comment-end))) |
421 (cond | 421 (unless starter (error "No comment syntax defined")) |
422 ((null starter) | 422 (beginning-of-line) |
423 (error "No comment syntax defined")) | 423 (let* ((eolpos (line-end-position)) |
424 (t (let* ((eolpos (line-end-position)) | 424 (begpos (comment-search-forward eolpos t)) |
425 cpos indent begpos) | 425 cpos indent) |
426 (beginning-of-line) | 426 ;; An existing comment? |
427 (if (not (setq begpos (comment-search-forward eolpos t))) | 427 (if begpos (setq cpos (point-marker)) |
428 (setq begpos (point)) | 428 ;; If none, insert one. |
429 (save-excursion | |
430 ;; Some comment-indent-function insist on not moving comments that | |
431 ;; are in column 0, so we insert a space to avoid this special case | |
432 (insert " ") | |
433 (setq begpos (point)) | |
434 (insert starter) | |
429 (setq cpos (point-marker)) | 435 (setq cpos (point-marker)) |
430 (goto-char begpos)) | 436 (insert ender))) |
431 ;; Compute desired indent. | 437 (goto-char begpos) |
432 (setq indent (funcall comment-indent-function)) | 438 ;; Compute desired indent. |
433 (if (not indent) | 439 (setq indent (funcall comment-indent-function)) |
434 ;; comment-indent-function refuses delegates to indent. | 440 (if (not indent) |
435 (indent-according-to-mode) | 441 ;; comment-indent-function refuses delegates to indent. |
436 ;; Avoid moving comments past the fill-column. | 442 (indent-according-to-mode) |
437 (setq indent | 443 ;; Avoid moving comments past the fill-column. |
438 (min indent | 444 (setq indent |
439 (+ (current-column) | 445 (min indent |
440 (- fill-column | 446 (+ (current-column) |
441 (save-excursion (end-of-line) (current-column)))))) | 447 (- fill-column |
442 (if (= (current-column) indent) | 448 (save-excursion (end-of-line) (current-column)))))) |
443 (goto-char begpos) | 449 (if (= (current-column) indent) |
444 ;; If that's different from current, change it. | 450 (goto-char begpos) |
445 (skip-chars-backward " \t") | 451 ;; If that's different from current, change it. |
446 (delete-region (point) begpos) | 452 (skip-chars-backward " \t") |
447 (indent-to (if (bolp) indent | 453 (delete-region (point) begpos) |
448 (max indent (1+ (current-column))))))) | 454 (indent-to (if (bolp) indent |
449 ;; An existing comment? | 455 (max indent (1+ (current-column))))))) |
450 (if cpos | 456 (goto-char cpos) |
451 (progn (goto-char cpos) (set-marker cpos nil)) | 457 (set-marker cpos nil)))) |
452 ;; No, insert one. | |
453 (insert starter) | |
454 (save-excursion | |
455 (insert ender)))))))) | |
456 | 458 |
457 ;;;###autoload | 459 ;;;###autoload |
458 (defun comment-set-column (arg) | 460 (defun comment-set-column (arg) |
459 "Set the comment column based on point. | 461 "Set the comment column based on point. |
460 With no ARG, set the comment column to the current column. | 462 With no ARG, set the comment column to the current column. |
882 ;; FIXME: If there's no comment to kill on this line and ARG is | 884 ;; FIXME: If there's no comment to kill on this line and ARG is |
883 ;; specified, calling comment-kill is not very clever. | 885 ;; specified, calling comment-kill is not very clever. |
884 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent)) | 886 (if arg (comment-kill (and (integerp arg) arg)) (comment-indent)) |
885 (let ((add (if arg (prefix-numeric-value arg) | 887 (let ((add (if arg (prefix-numeric-value arg) |
886 (if (= (length comment-start) 1) comment-add 0)))) | 888 (if (= (length comment-start) 1) comment-add 0)))) |
889 ;; Some modes insist on keeping column 0 comment in column 0 | |
890 ;; so we need to move away from it before inserting the comment. | |
891 (indent-according-to-mode) | |
887 (insert (comment-padright comment-start add)) | 892 (insert (comment-padright comment-start add)) |
888 (save-excursion | 893 (save-excursion |
889 (unless (string= "" comment-end) | 894 (unless (string= "" comment-end) |
890 (insert (comment-padleft comment-end add))) | 895 (insert (comment-padleft comment-end add))) |
891 (indent-according-to-mode)))))) | 896 (indent-according-to-mode)))))) |