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

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

Re: comment-kill can't deal with following situation


From: lgfang
Subject: Re: comment-kill can't deal with following situation
Date: Mon, 03 Mar 2008 20:04:29 +0800
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (berkeley-unix)

>>>>> "Stefan" == Stefan Monnier <monnier@iro.umontreal.ca> writes:
    Stefan> If you do not provide a prefix arg, comment-kill should
    Stefan> only kill 1 comment.  Indeed when providing a prefix arg,

    >> The document says:

    >> Kill the comment on this line, if any. With prefix ARG, kill
    >> comments on that many lines starting with this one.

    >> So, take following C code for example: int j; /* comment 1 */
    >> int k; /* comment 2*/

    >> I think comment-kill should kill both comment 1 and comment 2
    >> even without prefix ARG.

    Stefan> Here I disagree.  I find it more useful to only kill one
    Stefan> comment at a time (you can always choose to run the
    Stefan> command 2 times, but you cannot run it 1/2 a time).  Also

I agree with you.  I didn't think of that.  So, it is not a bug.  I
just misunderstood the document.

    Stefan> the docstring says "kill *the comment* on this line",
    Stefan> which admittedly is ambiguous when there are more than 1
    Stefan> comments, but indicates a clear intention to only kill 1
    Stefan> comment.

Maybe need to update document ? :)

    Stefan> So please reply to my previous request:

    Stefan>    I don't know of anybody who uses the prefix arg of
    Stefan> comment-kill, so if you could explain how you use it, it
    Stefan> might help decide what the behavior should be.

In case you are interested, I'm NOT calling comment-kill
interactively.  I wrote a elisp script to count NCSL.  The basic idea
is to delete all comment lines using comment-kill and delete all blank
lines using flush-lines. Then, the number of lines is just the number
of NCSL.

(let ((file nil) (sum 0))
  (condition-case nil
      (while (setq file (read-string ""))
        (find-file file)
        (let ((buffer-read-only nil))
          (goto-char (point-min))
          (comment-kill (line-number-at-pos (point-max)))
          (flush-lines "^[[:blank:]]*$" (point-min) (point-max))
          (let ((linum (- (line-number-at-pos (point-max)) 1)))
            ;; -1 since always end with empty line
              (princ (format "%d %s\n" linum file))
              (setq sum (+ sum linum))))
        (kill-buffer nil))
    (error (princ (format "%s total\n" sum)))))

But it turns out that that script is slow and in-correct.  At last, I
end up with something like following:

(require 'hideif)
(require 'newcomment)

(let ((file nil) (sum 0) (ncsl 0))
  (condition-case nil
      (while (setq file (read-string "")) ; for each file
        (find-file file)
        (setq ncsl 0)

        ;; for C/c++ files, hide if 0
        (when (or (equal 'c-mode major-mode) (equal 'c++-mode
        major-mode))
          (goto-char (point-min))
          (while (re-search-forward "^[ \t]*#if[ \t]*0" nil t)
            (hide-ifdef-block)))

        (goto-char (point-min))
        (while (not (eobp))
          ;; suppose there won't be 10000 consecutive comments
          (forward-comment 10000)
          (setq ncsl (+ 1 ncsl))
          ;; In case a comment starts at current line and spans several
          ;; lines.
          (let* ((eol (line-end-position)) (cs (comment-search-forward
        eol t)))
            (while cs
              (goto-char cs)
              (forward-comment 10000)
              (setq cs (comment-search-forward eol t)))
            (unless (> (point) eol)     ; unless already a new line
              (forward-visible-line 1))))
        (setq sum (+ sum ncsl))
        (kill-buffer nil)
        (princ (format "%d %s\n" ncsl file)))
    (error (princ (format "%s total\n" sum)))))

-- 
Fang, lungang





reply via email to

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