unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Is there a way to get the last message?
@ 2017-05-28 10:50 Marcin Borkowski
  2017-05-28 11:18 ` Stephen Berman
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Marcin Borkowski @ 2017-05-28 10:50 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I know about (current-message).  However, this won't work:

(defun get-last-message ()
  "Try to get the last message."
  (interactive)
  (with-temp-buffer
    (insert (or (current-message) ""))
    (clipboard-kill-ring-save (point-min) (point-max))))

M-x get-last-message puts the empty string into the clipboard and kill
ring.

Apparently `current-message' is cleared before this command has a chance
to get it.  This makes sense, but there is no `previous-message' command.

Is there any way to get the last thing displayed in the echo area (from
`message' or possibly from `error', too)?  This would be quite useful:
one could then paste it to a search engine to look up the internet for
some hints about the error/message.

Best,

--
Marcin Borkowski



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 10:50 Is there a way to get the last message? Marcin Borkowski
@ 2017-05-28 11:18 ` Stephen Berman
  2017-05-28 11:41   ` John Ankarström
                     ` (2 more replies)
  2017-05-28 11:51 ` tomas
  2017-05-28 18:24 ` Emanuel Berg
  2 siblings, 3 replies; 8+ messages in thread
From: Stephen Berman @ 2017-05-28 11:18 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Help Gnu Emacs mailing list

On Sun, 28 May 2017 12:50:12 +0200 Marcin Borkowski <mbork@mbork.pl> wrote:

> Hi all,
>
> I know about (current-message).  However, this won't work:
>
> (defun get-last-message ()
>   "Try to get the last message."
>   (interactive)
>   (with-temp-buffer
>     (insert (or (current-message) ""))
>     (clipboard-kill-ring-save (point-min) (point-max))))
>
> M-x get-last-message puts the empty string into the clipboard and kill
> ring.
>
> Apparently `current-message' is cleared before this command has a chance
> to get it.  This makes sense, but there is no `previous-message' command.
>
> Is there any way to get the last thing displayed in the echo area (from
> `message' or possibly from `error', too)?  This would be quite useful:
> one could then paste it to a search engine to look up the internet for
> some hints about the error/message.

What about this:

(defun get-last-message ()
  "Try to get the last message."
  (interactive)
  (with-current-buffer "*Messages*"
    (goto-char (point-max))
    (forward-line -1)
    (clipboard-kill-ring-save (line-beginning-position) (line-end-position))))

(add-hook 'post-command-hook 'get-last-message)

Steve Berman



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 11:18 ` Stephen Berman
@ 2017-05-28 11:41   ` John Ankarström
  2017-05-29 18:51     ` Emanuel Berg
  2017-05-28 14:12   ` Marcin Borkowski
  2017-05-28 22:11   ` Emanuel Berg
  2 siblings, 1 reply; 8+ messages in thread
From: John Ankarström @ 2017-05-28 11:41 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Help Gnu Emacs mailing list

Stephen Berman <stephen.berman@gmx.net> writes:

> (add-hook 'post-command-hook 'get-last-message)

Wouldn't post-command-hook be a bit overkill here? Personally,
I'd do something like this:

(advice-add #'message :after #'get-last-message)

I'm not 100% sure that `message' is the only function that
creates messages, but that advice should cover most cases.

- John



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 10:50 Is there a way to get the last message? Marcin Borkowski
  2017-05-28 11:18 ` Stephen Berman
@ 2017-05-28 11:51 ` tomas
  2017-05-28 18:24 ` Emanuel Berg
  2 siblings, 0 replies; 8+ messages in thread
From: tomas @ 2017-05-28 11:51 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, May 28, 2017 at 12:50:12PM +0200, Marcin Borkowski wrote:
> Hi all,
> 
> I know about (current-message).  However, this won't work:
> 
> (defun get-last-message ()
>   "Try to get the last message."
>   (interactive)
>   (with-temp-buffer
>     (insert (or (current-message) ""))
>     (clipboard-kill-ring-save (point-min) (point-max))))
> 
> M-x get-last-message puts the empty string into the clipboard and kill
> ring.

This might get you started:

  (defun parrot ()
    (interactive)
    (save-excursion
      (with-current-buffer (messages-buffer)
        ;; *Messages* seems to have a newline at end
        (goto-char (- (point-max) 1))
        (message "last message was '%S'"
                 (buffer-substring-no-properties
                  (line-beginning-position)
                  (line-end-position))))))

A couple of notes:

 - it seems that messages not always end up in the *Messages* buffer
 - the function (messages-buffer) is the recommended was to retrieve
   the messages buffer, which (most of the time? always?) is called
   *Messages*
 - you might want to double check the strategy for fetching the last
   line (i.e. going to point-max - 1
 - I put the thing into a message with (buffer-substring ...), you
   might want to dump it directly into the kill ring, of course

In the meantime, Stephen's reply seems to have addressed most of the
above points, anyway; but the recommended way to access *Messages*
might still be helpful.

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlkqucsACgkQBcgs9XrR2kaxRQCcD6/D1RasdLi1xPNaqb3GI8TU
1CoAn3ieUdT4cRp8gXOVzyYxZXUYJJr3
=pAqo
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 11:18 ` Stephen Berman
  2017-05-28 11:41   ` John Ankarström
@ 2017-05-28 14:12   ` Marcin Borkowski
  2017-05-28 22:11   ` Emanuel Berg
  2 siblings, 0 replies; 8+ messages in thread
From: Marcin Borkowski @ 2017-05-28 14:12 UTC (permalink / raw)
  To: Stephen Berman; +Cc: Help Gnu Emacs mailing list


On 2017-05-28, at 13:18, Stephen Berman <stephen.berman@gmx.net> wrote:

> On Sun, 28 May 2017 12:50:12 +0200 Marcin Borkowski <mbork@mbork.pl> wrote:
>
>> Hi all,
>>
>> I know about (current-message).  However, this won't work:
>>
>> (defun get-last-message ()
>>   "Try to get the last message."
>>   (interactive)
>>   (with-temp-buffer
>>     (insert (or (current-message) ""))
>>     (clipboard-kill-ring-save (point-min) (point-max))))
>>
>> M-x get-last-message puts the empty string into the clipboard and kill
>> ring.
>>
>> Apparently `current-message' is cleared before this command has a chance
>> to get it.  This makes sense, but there is no `previous-message' command.
>>
>> Is there any way to get the last thing displayed in the echo area (from
>> `message' or possibly from `error', too)?  This would be quite useful:
>> one could then paste it to a search engine to look up the internet for
>> some hints about the error/message.
>
> What about this:
>
> (defun get-last-message ()
>   "Try to get the last message."
>   (interactive)
>   (with-current-buffer "*Messages*"
>     (goto-char (point-max))
>     (forward-line -1)
>     (clipboard-kill-ring-save (line-beginning-position) (line-end-position))))

Thanks, I thought about something like that, too.  It has one problem,
though:

(defun long-message ()
  (interactive)
  (message "Long\nmessage"))

Thanks anyway,

-- 
Marcin Borkowski



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 10:50 Is there a way to get the last message? Marcin Borkowski
  2017-05-28 11:18 ` Stephen Berman
  2017-05-28 11:51 ` tomas
@ 2017-05-28 18:24 ` Emanuel Berg
  2 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2017-05-28 18:24 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

> Is there any way to get the last thing
> displayed in the echo area (from `message' or
> possibly from `error', too)? This would be
> quite useful: one could then paste it to
> a search engine to look up the internet for
> some hints about the error/message.

In general, I've heard the message utility is
an underpowered feature in Emacs and it is not
beyond reason it could be useful to have a
data structure with messages and some variable
to set its history length. However, lacking
that (?), why not use the message
buffer itself?

Specifically for your proposed use case, this
seems to bit a bit of over-engineering. Do you
really Google error-messages in
such quantities?

What I would do is, first keybind this

    (defun switch-to-messages-buffer ()
      (interactive)
      (switch-to-buffer (messages-buffer))
      (goto-char (point-max)) )

then, in the message buffer, mark the message,
and

    (defun web-search ()
      (interactive)
      (let*((search-engine "Google")
            (search        (get-search-string search-engine)))
        (unless (empty-string-p search)
          (let ((tab-label (format "%s: %s" search-engine search)))
            (w3m-new-tab tab-label)
            (w3m-search w3m-search-default-engine search) ))))

I don't know if you use Emacs-w3m, if you do,
you can get that above code working with some
additional code found here

    http://user.it.uu.se/~embe8573/conf/emacs-init/w3m/w3m-unisearch.el

otherwise it should be not that difficult to do
the same with your prefered browser.

Really, to get to the message or error buffer
and mark the message is just a few keystrokes
away; and to be able to Google what is marked
in a buffer (or to use it as indata for
whatever search engine) is a good feature for
not only your proposed use-case but for many
other things as well.

So, as always, it is a good idea to optimize
the specifics and put the creativity into what
is general.

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 11:18 ` Stephen Berman
  2017-05-28 11:41   ` John Ankarström
  2017-05-28 14:12   ` Marcin Borkowski
@ 2017-05-28 22:11   ` Emanuel Berg
  2 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2017-05-28 22:11 UTC (permalink / raw)
  To: help-gnu-emacs

Stephen Berman wrote:

> What about this:
>
> (defun get-last-message () "Try to get the last
> message." (interactive) (with-current-buffer
> "*Messages*" (goto-char (point-max))
> (forward-line -1) (clipboard-kill-ring-save
> (line-beginning-position)
> (line-end-position))))
>
> (add-hook 'post-command-hook
> 'get-last-message)

Oh, no!

So there were this suggestion already!

Well, hell, anyway I also attempted it:

    (defun get-last-message ()
      (interactive)
      (let ((message-buffer (get-buffer "*Messages*")))
        (when message-buffer
          (with-temp-buffer
            (insert-buffer-substring message-buffer)
            (goto-char (point-max))
            (forward-line -1)
            (let ((start (point-at-bol))
                  (end   (point-at-eol)) )
              (buffer-substring start end) )))))

;; test:
;;
;;   (message "I was afraid you'd be trapped outside the city.")
;;   (message "Hey, I wasn't worried for a microsecond!")
;;   (message "Then you probably didn't understand the situation.")
;;
;; (get-last-message)

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Is there a way to get the last message?
  2017-05-28 11:41   ` John Ankarström
@ 2017-05-29 18:51     ` Emanuel Berg
  0 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2017-05-29 18:51 UTC (permalink / raw)
  To: help-gnu-emacs

John Ankarström wrote:

> (advice-add #'message :after
> #'get-last-message)
>
> I'm not 100% sure that `message' is the only
> function that creates messages, but that advice
> should cover most cases.

No, that's OK.

But: does it work when `message' is called
from C?

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-05-29 18:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-28 10:50 Is there a way to get the last message? Marcin Borkowski
2017-05-28 11:18 ` Stephen Berman
2017-05-28 11:41   ` John Ankarström
2017-05-29 18:51     ` Emanuel Berg
2017-05-28 14:12   ` Marcin Borkowski
2017-05-28 22:11   ` Emanuel Berg
2017-05-28 11:51 ` tomas
2017-05-28 18:24 ` Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).