emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multi- and single-line comment default


From: Andreas Roehler
Subject: Multi- and single-line comment default
Date: Thu, 09 Mar 2006 10:18:31 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050921

Presently commenting of single lines in C-Mode and
others per default uses multiline comment signs as
shown below

/* Example code */

Seems no way to change this via  customization, also after changing
comment-style-Var to `plain' or `aligned', same result.

Better default would be

// Example code

in the case, there is just a single line to comment.

BTW:

Suggest to change the default from
region-transient-mark-mode-requiring to an
line-oriented behavior, which respects region per
default if transient-mark-mode is on.

A draft of such an line-oriented implemention is
given below. (Please check it and report bugs or
further suggestions, if there are some).

There should be a customization-utility, what to do if
single-line-comments follow each other: to replace by a
multiline-sign silently or not.

Also needed: single-line-comments-only (nil or t).  Per
default it should switch to multiline-signs, if an
active regions spreads over.

So far.

Andreas Roehler
+49306927863


;;; ar-comment-uncomment --- comments or uncomments a line or region
according to state before. With key pressed, continues with next line.
With arg it copies and reinserts last line. Works on region if region is
active and no arg given.

;; author: Andreas Roehler,

;; Todo: replaces in C (and others probably)
;;       // Zeilenkommentar, neu ab C99-ANSI
;;      with
;;       /* Zeilenkommentar, neu ab C99-ANSI */
;;       Should change that.

(require 'newcomment)

(global-set-key [M-kp-6] 'ar-comment-uncomment)

(defun ar-comment-uncomment (&optional arg)
  "Comments or uncomments a line according to state
before.
Calls `comment-region' (unless line is already commented, in which case
it calls `uncomment-region').
With key pressed, continues with next line.
Works on region if mark and transient-mark-mode are active.
With arg greater than 1 works on arg lines, neglecting
region.
With arg equal 1 copies, comments/uncomments
and reinserts line."
  (interactive "*P")
  (comment-normalize-vars)
  (let ((arg (if arg (prefix-numeric-value arg) 0))
        (start (if (and mark-active transient-mark-mode)
                   (region-beginning)))
        (startline (count-lines 1 (point)))
        (end (if (and mark-active transient-mark-mode)
                 (region-end)))
        (endline (if (and mark-active transient-mark-mode)
                     (count-lines 1 (region-end))
                   (count-lines 1
                                (save-excursion
                                    (forward-line arg)
                                    (line-end-position)))))
        (line-to-comment-or-uncomment (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
    (cond ((eq 1 arg) ;; comment and reinsert
           (progn
             (comment-or-uncomment-region (line-beginning-position)
(line-end-position))
             (indent-according-to-mode)
             (end-of-line)
             (newline)
             (insert line-to-comment-or-uncomment)
             (indent-according-to-mode)))
          ((< 1 arg) ;; comment/uncomment as lines are given
           (while (<= 1 (prefix-numeric-value arg))
             (comment-or-uncomment-region (line-beginning-position)
(line-end-position))
             (indent-according-to-mode)
             (end-of-line)
             (forward-line 1)
             (indent-according-to-mode)
             (setq arg (1- arg))))
          ((and start end) ;; region is active
           (comment-or-uncomment-region start end)
           (indent-according-to-mode)
           (if (eobp)
               (progn (newline)
                      (indent-according-to-mode))
             (progn
               (forward-line 1)
               (indent-according-to-mode))))
          (t ;; just one line
           (progn (comment-or-uncomment-region (line-beginning-position)
(line-end-position))
                  (indent-according-to-mode)
                  (if (eobp)
                      (progn (newline)
                             (indent-according-to-mode))
                    (progn
                      (forward-line 1)
                      (indent-according-to-mode))))))))

;; ar-comment-uncomment.el ends here







reply via email to

[Prev in Thread] Current Thread [Next in Thread]