Rearrange.
[elisp/liece.git] / lisp / liece-misc.el
index 496a372..e479e43 100644 (file)
   (if liece-display-frame-title
       (liece-set-frame-title-format)))
 
+(defun liece-set-beep (buffer &optional arg)
+  (with-current-buffer buffer
+    (setq liece-beep (if arg (plusp arg) (not liece-beep))
+          liece-beep-indicator (if liece-beep "B" "-"))
+    (force-mode-line-update)))
+
+(defmacro liece-beep (&optional arg)
+  (list 'funcall 'liece-beep-function arg))
+
 (defun liece-freeze (buffer &optional arg)
   (with-current-buffer buffer
     (setq liece-freeze (if arg (plusp arg) (not liece-freeze))
          (setq args (nconc args (list line))))
       args))))
 
-(defmacro liece-message (&rest msg)
+(defmacro liece-message (&rest message)
   `(message "%s: %s"
            (product-name (product-find 'liece-version))
-           (format ,@msg)))
+           (format ,@message)))
 
 (defmacro liece-insert-change (buffer msg)
   `(liece-insert ,buffer (concat liece-change-prefix ,msg)))
 (defmacro liece-server-host ()
   '(if (listp liece-server)
        (plist-get liece-server ':host)
-     liece-server))
+     (if (or (string-match "^\\[\\([^]]+\\)\\]:?[0-9]*" liece-server)
+            (string-match "^\\([^:]+\\):?[0-9]*" liece-server))
+         (match-string 1 liece-server)
+       liece-server)))
 
 (defmacro liece-clean-hostname (hostname)
   "Return the arg HOSTNAME, but if is a dotted-quad, put brackets around it."
               (setq who (cdr who)))))))
      found))
 
-(defmacro liece-time-difference (t0 t1)
-  "Difference in seconds between T0 and T1.
-Both T0 and T1 are in the encoded time format."
-  `(+ (* (- (car ,t1) (car ,t0)) 65536)
-      (- (cadr ,t1)) (cadr ,t0)))
-
-(defmacro liece-time-add (t0 t1)
-  "Add T0 seconds to time T1.
-t0 is in `three integer lists'-format  returned by `current-time' function."
-  `(list (+ (car ,t0) (/ (+ (cadr ,t0) ,t1) 65536))
-        (% (+ (cadr ,t0) ,t1) 65536)
-        0))
-
+;;; stolen (and renamed) from time-date.el.
+(defun liece-subtract-time (t1 t2)
+  "Subtract two internal times."
+  (let ((borrow (< (cadr t1) (cadr t2))))
+    (list (- (car t1) (car t2) (if borrow 1 0))
+         (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
+
+(defun liece-time-difference (t1 t2)
+  "Return the differnce between two internal times in seconds."
+  (let ((sub (liece-subtract-time t1 t2)))
+    (+ (* (car sub) 65536) (cadr sub))))
+
+(defun liece-time-elapsed (time seconds)
+  "Add SECONDS to TIME."
+  (list (+ (car time) (/ (+ (cadr time) seconds) 65536))
+       (% (+ (cadr time) seconds) 65536)
+       (nth 2 time)))
+
+;;; stolen (and renamed) from time-date.el.
 (defun liece-seconds-to-time (seconds)
   "Convert SECONDS (a floating point number) to an Emacs time structure."
   (list (floor seconds 65536)
        (floor (mod seconds 65536))
        (floor (* (- seconds (ffloor seconds)) 1000000))))
 
-(defmacro liece-generate-hex-timestamp (&optional time)
+(defun liece-generate-hex-timestamp (&optional time)
   "Generate timestamp string as hexadecimal.
 If optional argument TIME is nil, calculate timestamp using current time."
-  `(let ((time (or ,time (current-time))))
-     (format "%04x%04x" (car time) (cadr time))))
+  (or time
+      (setq time (current-time)))
+  (format "%04x%04x" (car time) (cadr time)))
 
 (defmacro liece-hex-timestamp-valid (timestamp limit)
   "Is TIMESTAMP valid within LIMIT?"