emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch] get modification-time from vc
@ 2016-04-28 15:43 Rasmus
  2016-04-30 21:43 ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: Rasmus @ 2016-04-28 15:43 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 914 bytes --]

Hi,

The file system modification time as accessed by {{{modification-time}}}
is often wrong, e.g. when a project is newly cloned or if the file has
been copied in a sloppy manner.  At least if modification time suggest the
time that the file was edited by an author.

Sometimes vc can provide a better answer.  The attached patch tries to get
a sensible time from vc.

Caveats:
- There’s no customization.  I think that’s fine.  If someone feels
  strongly against this behavior a defcustom could be added or we could
  add another a macro, {{{vc-modification-time}}}.
- org-macs will depend on vc.  I don’t really see why this would be an
  issue as vc is bundled with Emacs.

I have only tested this with git, but it should work with other vc
backends to the extend that ‘parse-time-string’ can handle the way they
format dates.

WDYT?

Rasmus

-- 
Even a three-legged dog has three good legs to lose

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-macro-Maybe-get-modification-time-from-vc.el.patch --]
[-- Type: text/x-diff, Size: 3472 bytes --]

From a3298310fe5f399dacd5ab13b725a95d9c626a21 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Thu, 28 Apr 2016 17:15:29 +0200
Subject: [PATCH 1/3] org-macro: Maybe get modification-time from vc.el

* org-macro.el (org-macro-initialize-templates): Check modification-time
  in version control via vc.
* doc/org.texi (Macro replacement): Document new behavior.
* etc/ORG-NEWS: Mention new behavior.
---
 doc/org.texi      |  6 +++---
 etc/ORG-NEWS      |  4 +++-
 lisp/org-macro.el | 27 ++++++++++++++++++++++++++-
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 17b01c2..bc08277 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10251,9 +10251,9 @@ understood by @code{format-time-string}.
 @cindex time, macro
 @cindex modification time, macro
 These macros refer to the date and time when the document is exported and to
-the modification date and time of the file being exported, respectively.
-@var{FORMAT} should be a format string understood by
-@code{format-time-string}.
+the modification date, potentially found via version control, and time of the
+file being exported, respectively.  @var{FORMAT} should be a format string
+understood by @code{format-time-string}.
 
 @item @{@{@{input-file@}@}@}
 @cindex input file, macro
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 99241e2..c8b1627 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -104,7 +104,9 @@ is straightforward.  For example
 becomes
 
 : ("pdf" . (lambda (file link) (foo)))
-
+*** ~{{{modification-time}}}~ tries to obtain time via =vc=
+As a fallback, the modification time will be found via the file
+system.
 ** New features
 *** New org-protocol key=value syntax
 
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 5e9be98..debe6b0 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -59,6 +59,9 @@
 (declare-function org-mode "org" ())
 (declare-function org-remove-double-quotes "org" (s))
 (declare-function org-with-wide-buffer "org-macs" (&rest body))
+(declare-function vc-backend "vc-hooks" (f))
+(declare-function vc-call "vc-hooks" (&rest args))
+(declare-function vc-exec-after "vc-dispatcher" (code))
 
 ;;; Variables
 
@@ -148,7 +151,29 @@ function installs the following ones: \"property\",
 		    (cons "modification-time"
 			  (format "(eval (format-time-string \"$1\" '%s))"
 				  (prin1-to-string
-				   (nth 5 (file-attributes visited-file)))))))))
+				   (or
+				    ;; First, check if file has a VC modification time.
+				    (save-window-excursion
+				      (when (vc-backend visited-file)
+					(let ((buf (get-buffer-create " *org-vc*"))
+					      date)
+					  (vc-call print-log visited-file buf nil nil 1)
+					  (with-current-buffer buf
+					    (vc-exec-after
+					     (lambda ()
+					       (goto-char (point-min))
+					       (when (search-forward-regexp "Date:?[ \t]*" nil t)
+						 (let ((time (parse-time-string
+							      (buffer-substring
+							       (point) (point-at-eol)))))
+						   (when (org-some 'identity time)
+						     (setq date (apply 'encode-time time))))))))
+					  (let ((proc (get-buffer-process buf)))
+					    (while (and proc (accept-process-output proc .5 nil t))))
+					  (kill-buffer " *org-vc*")
+					  date)))
+				    ;; Otherwise use the time registered by the file system.
+				    (nth 5 (file-attributes visited-file))))))))))
     (setq org-macro-templates templates)))
 
 (defun org-macro-expand (macro templates)
-- 
2.8.0


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

* Re: [patch] get modification-time from vc
  2016-04-28 15:43 [patch] get modification-time from vc Rasmus
@ 2016-04-30 21:43 ` Nicolas Goaziou
  2016-05-18 10:12   ` Rasmus
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2016-04-30 21:43 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> The file system modification time as accessed by {{{modification-time}}}
> is often wrong, e.g. when a project is newly cloned or if the file has
> been copied in a sloppy manner.  At least if modification time suggest the
> time that the file was edited by an author.
>
> Sometimes vc can provide a better answer.  The attached patch tries to get
> a sensible time from vc.

Thank you for the patch.

> WDYT?

Well, if you ask, I do not like much when some software tries to
outsmart me (note that I like it even less when it actually succeeds).
So, I think {{{modification-time}}} should expand to modification time.

If you think that is useful, then I vote for a different macro.

Usual nitpicking follows.

> +					       (when (search-forward-regexp "Date:?[ \t]*" nil t)

Any reason not to use the genuine `re-search-forward'?

Is the search case sensitive? I suggest to bind appropriately
`case-fold-search' in any case.

> +						 (let ((time (parse-time-string
> +							      (buffer-substring
> +							       (point) (point-at-eol)))))

Any reason not to use the genuine `line-end-position'?

> +						   (when (org-some 'identity time)

  (cl-some #'identity time)

> +						     (setq date (apply 'encode-time time))))))))

  (setq date (apply #'encode-time time))

> +					  (let ((proc (get-buffer-process buf)))
> +					    (while (and proc (accept-process-output proc .5 nil t))))
> +					  (kill-buffer " *org-vc*")

  (kill-buffer buf)

However, I suggest to use `unwind-protect' for this and kill-buffer
during as an unwind form.

Also, I suggest to factor this code out of
`org-macro-initialize-templates'.


Regards,

-- 
Nicolas Goaziou

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

* Re: [patch] get modification-time from vc
  2016-04-30 21:43 ` Nicolas Goaziou
@ 2016-05-18 10:12   ` Rasmus
  2016-05-18 16:45     ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: Rasmus @ 2016-05-18 10:12 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 915 bytes --]

Hi,

An overdue "Thanks" for the feedback.

Here’s a new version.

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Well, if you ask, I do not like much when some software tries to
> outsmart me (note that I like it even less when it actually succeeds).
> So, I think {{{modification-time}}} should expand to modification time.

New idea, an optional 2nd argument to modification-time.  E.g.

    {{{modification-time(%Y, vc)}}}

> If you think that is useful, then I vote for a different macro.

I prefer an argument as it gives a clear ranking: get the modified time
from vc, if possible (if the second argument is vc), otherwise get it from
the file system (also default behavior).

>> +						   (when (org-some 'identity time)
>
>   (cl-some #'identity time)

Is cl-lib OK now?!

Thanks,
Rasmus

-- 
... The proofs are technical in nature and provide no real understanding

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-macro.el-Allow-modification-time-from-vc.el.patch --]
[-- Type: text/x-diff, Size: 4267 bytes --]

From 20f2268c7555c08e26654c77c0191012e176e059 Mon Sep 17 00:00:00 2001
From: Rasmus <rasmus@gmx.us>
Date: Wed, 18 May 2016 11:23:46 +0200
Subject: [PATCH 1/3] org-macro.el: Allow modification-time from vc.el

* org-macro.el (org-macro--vc-modified-time): New function.
* org-macro.el (org-macro-initialize-templates): Add support for new
  function.
* doc/org.texi (Macro replacement): Document new behavior.
* etc/ORG-NEWS: Mention new behavior.
---
 doc/org.texi      |  9 +++++----
 etc/ORG-NEWS      |  3 +++
 lisp/org-macro.el | 32 +++++++++++++++++++++++++++++++-
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 0bfee39..f0fd8bb 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10247,13 +10247,14 @@ argument to the @code{@{@{@{date@}@}@}} macro that will be used only if
 understood by @code{format-time-string}.
 
 @item @{@{@{time(@var{FORMAT})@}@}@}
-@itemx @{@{@{modification-time(@var{FORMAT})@}@}@}
+@itemx @{@{@{modification-time(@var{FORMAT}, @var{VC})@}@}@}
 @cindex time, macro
 @cindex modification time, macro
 These macros refer to the date and time when the document is exported and to
-the modification date and time of the file being exported, respectively.
-@var{FORMAT} should be a format string understood by
-@code{format-time-string}.
+the modification date and time, respectively.  @var{FORMAT} should be a
+format string understood by @code{format-time-string}.  If the second
+argument to the @code{modification-time} macro is @samp{vc}, Org will try to
+find the modification time via version control (using @code{vc.el}).
 
 @item @{@{@{input-file@}@}@}
 @cindex input file, macro
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 7a0a890..370963e 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -105,6 +105,9 @@ becomes
 
 : ("pdf" . (lambda (file link) (foo)))
 
+*** The ~{{{modification-time}}}~ macro can obtain time via =vc=
+The modification time will be determined via =vc.el= if the second
+argument is =vc=.  See the manual for details.
 ** New features
 *** New org-protocol key=value syntax
 
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index e135374..d86aaee 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -58,6 +58,10 @@
 (declare-function org-file-contents "org" (file &optional noerror))
 (declare-function org-mode "org" ())
 (declare-function org-remove-double-quotes "org" (s))
+(declare-function org-with-wide-buffer "org-macs" (&rest body))
+(declare-function vc-backend "vc-hooks" (f))
+(declare-function vc-call "vc-hooks" (&rest args))
+(declare-function vc-exec-after "vc-dispatcher" (code))
 
 ;;; Variables
 
@@ -145,11 +149,13 @@ function installs the following ones: \"property\",
 	(mapc update-templates
 	      (list (cons "input-file" (file-name-nondirectory visited-file))
 		    (cons "modification-time"
-			  (format "(eval (format-time-string \"$1\" '%s))"
+			  (format "(eval (format-time-string \"$1\" (or (when (equal (org-trim (downcase \"$2\")) \"vc\") (org-macro--vc-modified-time %s)) '%s)))"
+				  (prin1-to-string visited-file)
 				  (prin1-to-string
 				   (nth 5 (file-attributes visited-file)))))))))
     (setq org-macro-templates templates)))
 
+
 (defun org-macro-expand (macro templates)
   "Return expanded MACRO, as a string.
 MACRO is an object, obtained, for example, with
@@ -276,6 +282,30 @@ Return a list of arguments, as strings.  This is the opposite of
     s nil t)
    "\000"))
 
+(defun org-macro--vc-modified-time (file)
+  (save-window-excursion
+    (when (vc-backend file)
+      (let ((buf (get-buffer-create " *org-vc*"))
+	    (case-fold-search t)
+	    date)
+	(unwind-protect
+	    (progn
+	      (vc-call print-log file buf nil nil 1)
+	      (with-current-buffer buf
+		(vc-exec-after
+		 (lambda ()
+		   (goto-char (point-min))
+		   (when (re-search-forward "Date:?[ \t]*" nil t)
+		     (let ((time (parse-time-string
+				  (buffer-substring
+				   (point) (line-end-position)))))
+		       (when (cl-some 'identity time)
+			 (setq date (apply #'encode-time time))))))))
+	      (let ((proc (get-buffer-process buf)))
+		(while (and proc (accept-process-output proc .5 nil t)))))
+	  (kill-buffer buf))
+	date))))
+
 
 (provide 'org-macro)
 ;;; org-macro.el ends here
-- 
2.8.2


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

* Re: [patch] get modification-time from vc
  2016-05-18 10:12   ` Rasmus
@ 2016-05-18 16:45     ` Nicolas Goaziou
  2016-05-18 19:45       ` Rasmus
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2016-05-18 16:45 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> Here’s a new version.

Thank you.

> New idea, an optional 2nd argument to modification-time.  E.g.
>
>     {{{modification-time(%Y, vc)}}}

Even better!

>> If you think that is useful, then I vote for a different macro.
>
> I prefer an argument as it gives a clear ranking: get the modified time
> from vc, if possible (if the second argument is vc), otherwise get it from
> the file system (also default behavior).

Why enforcing "vc" and not any non-nil/non-empty second argument instead ?

> Is cl-lib OK now?!

cl-lib is OK in master. Minimal requirement for master is Emacs 24.3.

> +the modification date and time, respectively.  @var{FORMAT} should be a
> +format string understood by @code{format-time-string}.  If the second
> +argument to the @code{modification-time} macro is @samp{vc}, Org will try to
> +find the modification time via version control (using @code{vc.el}).

Suggestion:

  Org retrieves the information from the version control system (using
  @file{vc.el}) instead of the file attributes.

> +			  (format "(eval (format-time-string \"$1\" (or (when (equal (org-trim (downcase \"$2\")) \"vc\") (org-macro--vc-modified-time %s)) '%s)))"

when => and

> +		       (when (cl-some 'identity time)
> +			 (setq date (apply #'encode-time time))))))))

Why #'encode-time and not #'identity ?

In any case, this looks good. Please push when you're happy with the
result.

Regards,

-- 
Nicolas Goaziou

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

* Re: [patch] get modification-time from vc
  2016-05-18 16:45     ` Nicolas Goaziou
@ 2016-05-18 19:45       ` Rasmus
  2016-05-19  7:47         ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: Rasmus @ 2016-05-18 19:45 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Thanks for the comments!

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Why enforcing "vc" and not any non-nil/non-empty second argument instead ?

No point, really.

>> Is cl-lib OK now?!
>
> cl-lib is OK in master. Minimal requirement for master is Emacs 24.3.

Cool.
>> +		       (when (cl-some 'identity time)
>> +			 (setq date (apply #'encode-time time))))))))
>
> Why #'encode-time and not #'identity ?

To test if you review the all the way to the end!  Or...

> In any case, this looks good. Please push when you're happy with the
> result.

Pushed. 

Rasmus

-- 
And when I’m finished thinking, I have to die a lot

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

* Re: [patch] get modification-time from vc
  2016-05-18 19:45       ` Rasmus
@ 2016-05-19  7:47         ` Nicolas Goaziou
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Goaziou @ 2016-05-19  7:47 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> Pushed.

Thank you.

Regards,

-- 
Nicolas Goaziou

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

end of thread, other threads:[~2016-05-19  7:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-28 15:43 [patch] get modification-time from vc Rasmus
2016-04-30 21:43 ` Nicolas Goaziou
2016-05-18 10:12   ` Rasmus
2016-05-18 16:45     ` Nicolas Goaziou
2016-05-18 19:45       ` Rasmus
2016-05-19  7:47         ` Nicolas Goaziou

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

	https://git.savannah.gnu.org/cgit/emacs/org-mode.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).