(article-lapsed-string): Refactor out and allow specifying how many segments you...
authorLars Magne Ingebrigtsen <larsi@gnus.org>
Wed, 26 Jan 2011 23:31:06 +0000 (15:31 -0800)
committerLars Magne Ingebrigtsen <larsi@gnus.org>
Wed, 26 Jan 2011 23:31:06 +0000 (15:31 -0800)
lisp/ChangeLog
lisp/gnus-art.el

index 4c93ce0..ba42762 100644 (file)
@@ -3,6 +3,8 @@
        * gnus-art.el (gnus-article-next-page): When the last line of the
        article is displayed, scroll down once more instead of going to the
        next article at once.
+       (article-lapsed-string): Refactor out and allow specifying how many
+       segments you want.
 
        * shr.el: Revert change that made headings use different-sized faces.
        The Emacs display engine isn't advanced enough that, for instance,
index 110e5c8..96f3136 100644 (file)
@@ -3500,7 +3500,7 @@ should replace the \"Date:\" one, or should be added below it."
 
 (defun article-make-date-line (date type)
   "Return a DATE line of TYPE."
-  (unless (memq type '(local ut original user iso8601 lapsed english))
+  (unless (memq type '(local ut original user iso8601 lapsed english date-lapsed))
     (error "Unknown conversion type: %s" type))
   (condition-case ()
       (let ((time (date-to-time date)))
@@ -3548,47 +3548,10 @@ should replace the \"Date:\" one, or should be added below it."
                     (/ (% (abs tz) 3600) 60)))))
         ;; Do an X-Sent lapsed format.
         ((eq type 'lapsed)
-         ;; If the date is seriously mangled, the timezone functions are
-         ;; liable to bug out, so we ignore all errors.
-         (let* ((now (current-time))
-                (real-time (subtract-time now time))
-                (real-sec (and real-time
-                               (+ (* (float (car real-time)) 65536)
-                                  (cadr real-time))))
-                (sec (and real-time (abs real-sec)))
-                num prev)
-           (cond
-            ((null real-time)
-             "X-Sent: Unknown")
-            ((zerop sec)
-             "X-Sent: Now")
-            (t
-             (concat
-              "X-Sent: "
-              ;; This is a bit convoluted, but basically we go
-              ;; through the time units for years, weeks, etc,
-              ;; and divide things to see whether that results
-              ;; in positive answers.
-              (mapconcat
-               (lambda (unit)
-                 (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
-                     ;; The (remaining) seconds are too few to
-                     ;; be divided into this time unit.
-                     ""
-                   ;; It's big enough, so we output it.
-                   (setq sec (- sec (* num (cdr unit))))
-                   (prog1
-                       (concat (if prev ", " "") (int-to-string
-                                                  (floor num))
-                               " " (symbol-name (car unit))
-                               (if (> num 1) "s" ""))
-                     (setq prev t))))
-               article-time-units "")
-              ;; If dates are odd, then it might appear like the
-              ;; article was sent in the future.
-              (if (> real-sec 0)
-                  " ago"
-                " in the future"))))))
+         (concat "X-Sent: " (article-lapsed-string time)))
+        ;; A combined date/lapsed format.
+        ((eq type 'date-lapsed)
+         (concat "Date: " (article-lapsed-string time 3)))
         ;; Display the date in proper English
         ((eq type 'english)
          (let ((dtime (decode-time time)))
@@ -3610,9 +3573,57 @@ should replace the \"Date:\" one, or should be added below it."
             (format "%02d" (nth 2 dtime))
             ":"
             (format "%02d" (nth 1 dtime)))))))
-    (error
+    (foo
      (format "Date: %s (from Gnus)" date))))
 
+(defun article-lapsed-string (time &optional max-segments)
+  ;; If the date is seriously mangled, the timezone functions are
+  ;; liable to bug out, so we ignore all errors.
+  (let* ((now (current-time))
+        (real-time (subtract-time now time))
+        (real-sec (and real-time
+                       (+ (* (float (car real-time)) 65536)
+                          (cadr real-time))))
+        (sec (and real-time (abs real-sec)))
+        (segments 0)
+        num prev)
+    (unless max-segments
+      (setq max-segments (length article-time-units)))
+    (cond
+     ((null real-time)
+      "X-Sent: Unknown")
+     ((zerop sec)
+      "X-Sent: Now")
+     (t
+      (concat
+       "X-Sent: "
+       ;; This is a bit convoluted, but basically we go
+       ;; through the time units for years, weeks, etc,
+       ;; and divide things to see whether that results
+       ;; in positive answers.
+       (mapconcat
+       (lambda (unit)
+         (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
+                 (>= segments max-segments))
+             ;; The (remaining) seconds are too few to
+             ;; be divided into this time unit.
+             ""
+           ;; It's big enough, so we output it.
+           (setq sec (- sec (* num (cdr unit))))
+           (prog1
+               (concat (if prev ", " "") (int-to-string
+                                          (floor num))
+                       " " (symbol-name (car unit))
+                       (if (> num 1) "s" ""))
+             (setq prev t
+                   segments (1+ segments)))))
+       article-time-units "")
+       ;; If dates are odd, then it might appear like the
+       ;; article was sent in the future.
+       (if (> real-sec 0)
+          " ago"
+        " in the future"))))))
+
 (defun article-date-local (&optional highlight)
   "Convert the current article date to the local timezone."
   (interactive (list t))