unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* emacs-uptime - insert text with C-u [patch]
@ 2014-12-25 18:35 Adam Sjøgren
  2014-12-25 19:53 ` Andreas Schwab
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Sjøgren @ 2014-12-25 18:35 UTC (permalink / raw)
  To: emacs-devel

When you want to insert the current Emacs version into a buffer, you go:
C-u M-x emacs-version, and badaboom:

  GNU Emacs 24.4.1 (x86_64-pc-linux-gnu, [...]

It would be cool if emacs-uptime also worked like that. C-u M-x
emacs-uptime, badabing:

  7 hours, 46 minutes, 25 seconds

The following patch tries to make this happen, however I don't know what
the idiom is to distinguish a prefix argument from an optional one, so I
guess there must be a better way:

--- time.el.orig	2014-12-25 19:27:16.166584389 +0100
+++ time.el	2014-12-25 19:26:15.026582109 +0100
@@ -574,15 +574,19 @@
 (defun emacs-uptime (&optional format)
   "Return a string giving the uptime of this instance of Emacs.
 FORMAT is a string to format the result, using `format-seconds'.
-For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"."
-  (interactive)
+For example, the Unix uptime command format is \"%D, %z%2h:%.2m\".
+If called interactively with a prefix argument, insert string at point."
+  (interactive "P")
   (let ((str
-         (format-seconds (or format "%Y, %D, %H, %M, %z%S")
+         (format-seconds (if (stringp format) format
+                           "%Y, %D, %H, %M, %z%S")
                          (float-time
                           (time-subtract (current-time) before-init-time)))))
-    (if (called-interactively-p 'interactive)
-        (message "%s" str)
-      str)))
+    (if (and (not (stringp format)) format)
+        (insert str)
+      (if (called-interactively-p 'interactive)
+          (message "%s" str)
+        str))))
 
 ;;;###autoload
 (defun emacs-init-time ()



  Best regards,

    Adam

-- 
 "I find television very educating. Every time                Adam Sjøgren
  somebody turns on the set, I go into the other room    asjo@koldfront.dk
  and read a book."




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

* Re: emacs-uptime - insert text with C-u [patch]
  2014-12-25 18:35 emacs-uptime - insert text with C-u [patch] Adam Sjøgren
@ 2014-12-25 19:53 ` Andreas Schwab
  2014-12-25 19:56   ` Adam Sjøgren
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2014-12-25 19:53 UTC (permalink / raw)
  To: Adam Sjøgren; +Cc: emacs-devel

asjo@koldfront.dk (Adam Sjøgren) writes:

> The following patch tries to make this happen, however I don't know what
> the idiom is to distinguish a prefix argument from an optional one, so I
> guess there must be a better way:

Add another optional argument that is set by the interactive spec.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



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

* Re: emacs-uptime - insert text with C-u [patch]
  2014-12-25 19:53 ` Andreas Schwab
@ 2014-12-25 19:56   ` Adam Sjøgren
  2014-12-26  4:54     ` Stephen J. Turnbull
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Sjøgren @ 2014-12-25 19:56 UTC (permalink / raw)
  To: emacs-devel

Andreas writes:

> asjo@koldfront.dk (Adam Sjøgren) writes:

>> I don't know what the idiom is to distinguish a prefix argument from
>> an optional one[...]

> Add another optional argument that is set by the interactive spec.

Yeah, I tried to do that, but couldn't make it work - despite reading
the documentation of interactive, and searching through some elisp to
find something to mimick...


  Best regards,

    Adam

-- 
 "This German waltz is not as elegant as the ones from        Adam Sjøgren
  Vienna. But it is louder."                             asjo@koldfront.dk




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

* Re: emacs-uptime - insert text with C-u [patch]
  2014-12-25 19:56   ` Adam Sjøgren
@ 2014-12-26  4:54     ` Stephen J. Turnbull
  2015-02-10 14:51       ` Adam Sjøgren
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen J. Turnbull @ 2014-12-26  4:54 UTC (permalink / raw)
  To: Adam Sjøgren; +Cc: emacs-devel

Adam Sjøgren writes:
 > Andreas writes:
 > 
 > > asjo@koldfront.dk (Adam Sjøgren) writes:
 > 
 > >> I don't know what the idiom is to distinguish a prefix argument from
 > >> an optional one[...]

In this case the argument list is (format insert), and you probably
want a interactive spec of "iP".




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

* Re: emacs-uptime - insert text with C-u [patch]
  2014-12-26  4:54     ` Stephen J. Turnbull
@ 2015-02-10 14:51       ` Adam Sjøgren
  2015-02-10 15:18         ` Andreas Schwab
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Sjøgren @ 2015-02-10 14:51 UTC (permalink / raw)
  To: emacs-devel

Stephen writes:

>>> asjo@koldfront.dk (Adam Sjøgren) writes:

>>>> I don't know what the idiom is to distinguish a prefix argument from
>>>> an optional one[...]

> In this case the argument list is (format insert), and you probably
> want a interactive spec of "iP".

Thanks for the hints; here is a nicer patch, and a ChangeLog entry:

2015-02-10  Adam Sjøgren  <asjo@koldfront.dk>

	* time.el (emacs-uptime): insert at point when called with
        prefix.


--- time.el.orig	2015-02-10 15:42:48.472482974 +0100
+++ time.el	2015-02-10 15:45:27.743592919 +0100
@@ -571,18 +571,22 @@
             (cancel-timer elt)))))))
 
 ;;;###autoload
-(defun emacs-uptime (&optional format)
+(defun emacs-uptime (&optional format here)
   "Return a string giving the uptime of this instance of Emacs.
 FORMAT is a string to format the result, using `format-seconds'.
-For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"."
-  (interactive)
+For example, the Unix uptime command format is \"%D, %z%2h:%.2m\".
+If the optional argument HERE is non-nil, insert string at
+point."
+  (interactive "i\nP")
   (let ((str
          (format-seconds (or format "%Y, %D, %H, %M, %z%S")
                          (float-time
                           (time-subtract (current-time) before-init-time)))))
-    (if (called-interactively-p 'interactive)
-        (message "%s" str)
-      str)))
+    (if here
+        (insert str)
+      (if (called-interactively-p 'interactive)
+          (message "%s" str)
+        str))))
 
 ;;;###autoload
 (defun emacs-init-time ()


-- 
 "[T]he subject matter is so attractive that only             Adam Sjøgren
  extravagant incompetence could make it dull."          asjo@koldfront.dk




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

* Re: emacs-uptime - insert text with C-u [patch]
  2015-02-10 14:51       ` Adam Sjøgren
@ 2015-02-10 15:18         ` Andreas Schwab
  2015-02-10 15:24           ` Adam Sjøgren
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2015-02-10 15:18 UTC (permalink / raw)
  To: Adam Sjøgren; +Cc: emacs-devel

asjo@koldfront.dk (Adam Sjøgren) writes:

> --- time.el.orig	2015-02-10 15:42:48.472482974 +0100
> +++ time.el	2015-02-10 15:45:27.743592919 +0100
> @@ -571,18 +571,22 @@
>              (cancel-timer elt)))))))
>  
>  ;;;###autoload
> -(defun emacs-uptime (&optional format)
> +(defun emacs-uptime (&optional format here)
>    "Return a string giving the uptime of this instance of Emacs.
>  FORMAT is a string to format the result, using `format-seconds'.
> -For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"."
> -  (interactive)
> +For example, the Unix uptime command format is \"%D, %z%2h:%.2m\".
> +If the optional argument HERE is non-nil, insert string at
> +point."
> +  (interactive "i\nP")
>    (let ((str
>           (format-seconds (or format "%Y, %D, %H, %M, %z%S")
>                           (float-time
>                            (time-subtract (current-time) before-init-time)))))
> -    (if (called-interactively-p 'interactive)
> -        (message "%s" str)
> -      str)))
> +    (if here
> +        (insert str)
> +      (if (called-interactively-p 'interactive)
> +          (message "%s" str)
> +        str))))

It think you should always return the same value, which is str.

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."



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

* Re: emacs-uptime - insert text with C-u [patch]
  2015-02-10 15:18         ` Andreas Schwab
@ 2015-02-10 15:24           ` Adam Sjøgren
  0 siblings, 0 replies; 7+ messages in thread
From: Adam Sjøgren @ 2015-02-10 15:24 UTC (permalink / raw)
  To: emacs-devel

Andreas writes:

>> +    (if here
>> +        (insert str)
>> +      (if (called-interactively-p 'interactive)
>> +          (message "%s" str)
>> +        str))))

> It think you should always return the same value, which is str.

I thought the same thing, but then I looked at emacs-version, which
doesn't:

    (if here
        (insert version-string)
      (if (called-interactively-p 'interactive)
          (message "%s" version-string)
        version-string))))

Do you think that should be changed as well?


  Best regards,

    Adam

-- 
 "You make a fair point. Knowing when not to say              Adam Sjøgren
  anything is not something the lazyweb had              asjo@koldfront.dk
  historically been good at."




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

end of thread, other threads:[~2015-02-10 15:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-25 18:35 emacs-uptime - insert text with C-u [patch] Adam Sjøgren
2014-12-25 19:53 ` Andreas Schwab
2014-12-25 19:56   ` Adam Sjøgren
2014-12-26  4:54     ` Stephen J. Turnbull
2015-02-10 14:51       ` Adam Sjøgren
2015-02-10 15:18         ` Andreas Schwab
2015-02-10 15:24           ` Adam Sjøgren

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).