0

I wrote an interactive function to get the contents of a line from the current position of point to the last non-blank character on a line.

However, when after evaluating the function with C-x C-e, going to a buffer with random-text and then typing M-x get-substring-till-end-of-line I would have expected the answer to be printed in the minibuffer.

But nothing prints in the mini-buffer.

Here is the code.

(defun get-substring-till-end-of-line ()
  "Get as a subtring all the characters from the 
   current-position of point upto the last 
   non-white space character of the current line."
  (interactive)
  (save-excursion
      (let ((start (point)) end)
    (forward-line 1)
    (re-search-backward "[^[:space:]]")
    (setq end (point))
    (buffer-substring-no-properties start end))))
  • 1
    You need to send the result to the mini buffer through a call to message. – Dan Nov 23 '18 at 0:44
  • Was there a particular reason you expected that? The expectation does not hold for any other interactive function you might have looked at, so I'm curious as to how/where you acquired it. – phils Nov 23 '18 at 1:21
  • @phils Well the particular reason I probably unconsciously expected it, was because I thought since the result of evaluating the body of a function is usually the last form, I thought after evaluating the function the result would be displayed in the minibuffer automatically. Kind of like how expressions once typed inside a python repl are evaluated and results are displayed automatically (without any appeal to print function). I am still new to elisp and learning the ropes as I go along. :-D – smilingbuddha Nov 23 '18 at 1:25
  • 3
    Note that you wouldn't want commands to exhibit that behaviour -- every key binding calls a command, including inserting text (where keys are bound to self-insert-command, and which might frequently be happening at a minibuffer prompt), so things would be absurdly noisy if every command generated a message for its return value. – phils Nov 23 '18 at 1:51
1

You can use M-: (get-substring-till-end-of-line) (or M-x eval-expression ...), it will print the value of the function call.

You can also use message for debugging, in your case, print the return value. Remove it when you don't need it.


BTW, re-search-forward has optional argument BOUND and NOERROR, they are helpful

(defun your-move-to-end-of-line ()
  "Move point to the last non-blank character of current line."
  (interactive)
  (and (re-search-forward (rx (1+ (in ?\s ?\t)) eol) (line-end-position) 'move)
       (goto-char (match-beginning 0))))

(defun your-get-substring-till-end-of-line ()
  (let ((start (point))
        (end (save-excursion
               (if (re-search-forward (rx (1+ (in ?\s ?\t)) eol) (line-end-position) t)
                   (match-beginning 0)
                 (line-end-position)))))
    (buffer-substring-no-properties start end)))

You can also use string-trim-right from subr-x.el, the code is very simple.

(string-trim-right (buffer-substring-no-properties (point) (line-end-position)))

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.