0

I am using comment-dwim-line to comment out multiple lines. For this, I automatically move cursor always to the beginning of a line. I just want to use this behavior if the cursor is at beginning of a line or multiple line is selected.

If the cursor is in the middle of a line, I just want to add a comment character where the cursor is located.

The code I am using:

(defun comment-dwim-line (&optional arg)
  "Replacement for the comment-dwim command.
        If no region is selected and current line is not blank and we are not at the end of the line,
        then comment current line.
        Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
  ;; http://www.opensubscriber.com/message/[email protected]/10971693.html
  (interactive "*P")
  (comment-normalize-vars)
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (line-beginning-position) (line-end-position))
    (comment-dwim arg)))

(global-set-key (kbd "C-c C-;") 'comment-dwim-line)

Here it is possible to apply normal comment out behavior if the cursor is not at the beginning of a line:

def main():
    x = 10   
  (^)
   |__ cursor is here when `comment-dwim-line` is applied it becomes

This is ok.

def main():
    # x = 10

But for the following example:

def main():
    x = 10   my commment
          (^)
           |__ cursor is here when `comment-dwim-line` is applied
# x = 10   my commment

It becomes:

def main():
    # x = 10  my commment

wanted behavior would be: x = 10 # my commment as:

def main():
    x = 10 # my commment
NickD
  • 29,717
  • 3
  • 27
  • 44
alper
  • 1,370
  • 1
  • 13
  • 35

1 Answers1

1

Experimentally, changing (line-beginning-position) to (point) in the arg list of comment-or-uncomment-region gives the behaviour you describe:

(defun comment-dwim-line (&optional arg)
  (interactive "*P")
  (comment-normalize-vars)
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (point) (line-end-position))
    (let ((comment-column (current-column)))
      (comment-dwim arg))))
C4ffeine Add1ct
  • 492
  • 3
  • 10
  • This works close to what I wanted. But when I run this at the end of a line, it adds the comment character to very long distancle like: pid = os.getpid() # is this normal? – alper Feb 28 '23 at 17:58
  • Sounds a bit strange, what would you want to be commented out in this circumstance? – C4ffeine Add1ct Feb 28 '23 at 18:45
  • I just want # to be added to end of line right after the latest character but it adds . # instead of just #. Like if the cursor is at the end of the line and if there is no character after it ; it adds many empty space followed by # – alper Feb 28 '23 at 20:55
  • I've edited the function so it doesn't add extra whitespace when at the end of a line (by temporarily setting comment-column) – C4ffeine Add1ct Feb 28 '23 at 21:37
  • Thanks but the updated edit does still add extra whitespace in the case I try to explain :-( – alper Feb 28 '23 at 22:11
  • Thanks but the updated edit does still add extra whitespace and comments are not added to begining of the first character <cursor> first_character becomes <cursor># first_character instead of <cursor> # first_character – alper Feb 28 '23 at 22:22
  • I get pid = os.getpid()<cursor> -> pid = os.getpid() # <cursor> and <cursor> pid = os.getpid() -> # pid = os.getpid(), are both of these not what you expect? – C4ffeine Add1ct Mar 01 '23 at 09:19
  • Yeah but when there is tab before <tab>pid = os.getpid(), comment added before the tab not after. – alper Mar 01 '23 at 10:29
  • I can't reproduce that unfortunately - I find the comment character indented regardless of whether I use tabs or spaces – C4ffeine Add1ct Mar 01 '23 at 17:23