5

I have a sentence. For example:

foo bar

When I do gl in Evil normal modus, that sentence turns into:

;; EDH: foo bar
;; ------------

So I need to bind the keys gl to a function, that will turn the sentence into the above.

Eventually I came up with:

(evil-define-operator my-comment (beg end)
  "Comment selected lines."
  :motion evil-line  ;; in normal mode, no motion is read but the current line is used
  :type line         ;; this operator always works on whole lines
  (goto-char beg)
  (insert "EDH: ")
  (goto-char (+ 4 end))
  (let ((n (- (line-end-position) (line-beginning-position))))
    (newline)
    (insert-char ?- n)
    (comment-region beg (point))))

(define-key evil-normal-state-map "gl" #'my-comment)

This allows, e.g., to visually select some lines and then gl comments those lines and adds EDH: before the first.

Later I noticed another bug with this function. For example, I have the code formatted as following.

start
    foo []

[] = cursor. So I toggle my-comment on foo

Then it will be:

start
;; EDH:    foo
------------

Which is wrong, I need to make it like:

start
     ;; EDH: foo
     ------------

The culprit is the (line-end-position) and (line-beginning-position), with no respect for the indentation. So I'm wondering if is there a Evil alternative or a built-in Emacs command, that returns the position of the (evil-first-non-blank) and (evil-end-of-line), as well the postion of the first character in the line and the last characther in the line?

Any suggestion would be appreciated.

7

The function current-indentation will return the column number of the first non whitespace character on the current line.

(current-indentation)

Return the indentation of the current line. This is the horizontal position of the character following any initial whitespace.

If you want the character position of the first non whitespace character on the line you could use (save-excursion (back-to-indentation) (point))

(backward-to-indentation &optional ARG)

Move backward ARG lines and position at first nonblank character.

  • Thanks for your answer, I replaced the (- (line-end [..] beginning postion) with your (save-excursion (back-to-indentation) (point) but it didn't helped in my case. I assume I placed it on the wrong place? – ReneFroger Jun 10 '15 at 18:44
  • any suggestion? – ReneFroger Jun 15 '15 at 20:59
  • @ReneFrogertjuh: this post provides an answer to the question you stated (non-whitespace bounds on a line). If you have a different question, either edit your post or ask a new question that states precisely what the problem is. – Dan Jun 15 '15 at 21:11
  • If anyone is interested, here is the position of the last non-whitespace character of the line:(save-excursion (end-of-line) (skip-syntax-backward "-") (point)) – CodyChan Mar 8 at 10:47
2

The following function returns a list of the point positions of the first and last non-whitespace characters on the current line:

(defun line-bounds-nonwhitespace ()
  (save-excursion
    (goto-char (point-at-bol))
    (skip-syntax-forward " " (point-at-eol))
    (let ((beg (point)))
      (goto-char (point-at-eol))
      (skip-syntax-backward " " (point-at-bol))
      (list beg (point)))))
  • Thanks for your answer, I replaced the (- (line-end [..] beginning postion) with your (line-bounds-non-whitespace) but it didn't helped in my case. I assume I placed it on the wrong place? Thanks in advance for your reply. – ReneFroger Jun 10 '15 at 18:46
  • any suggestion? – ReneFroger Jun 15 '15 at 20:59
  • @ReneFrogertjuh: this post provides an answer to the question you stated (non-whitespace bounds on a line). If you have a different question, either edit your post or ask a new question that states precisely what the problem is. – Dan Jun 15 '15 at 21:11
  • thanks for your reply. I already understand that. But I couldn't figure out how I could integrate your post into the code that I posted in the OP. Hence my question. – ReneFroger Jun 15 '15 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.