在缓冲区中添加时间

时间:2011-08-30 12:13:54

标签: emacs lisp elisp

假设我有一个emacs缓冲区,其中包含格式“minutes'seconds”的时间,格式为“minutes”以及“seconds”,例如5'30'',6'15'',10'和1 ''。是否可以使用输出(在迷你缓冲区中)以“minutes'seconds''(此处= 21'46'')格式自动添加缓冲区中的所有时间?

2 个答案:

答案 0 :(得分:2)

您可以使用:

(defun add-times ()
  (interactive)
  (save-excursion
    (let ((mins 0) (secs 0) 
          (accum-nums 
           (lambda (regexp)
             (let ((value 0))
               (beginning-of-buffer)
               (while (re-search-forward regexp nil t)
                 (setq value (+ value (string-to-int (match-string-no-properties 1)))))
               value))))
      (setq mins (funcall accum-nums "\\([0-9]+\\)'\\([^']\\|$\\)"))
      (setq secs (funcall accum-nums "\\([0-9]+\\)''"))
      ; adjust > 60 seconds
      (setq mins (+ mins (/ secs 60)))
      (setq secs (mod secs 60))
      (format "%d'%d''" mins secs))))

你可以像这样使用它:在带有时间的文件的缓冲区中,键入 ESC 然后评估:

(insert (add-times))

如果你想要一个能够执行此操作的函数,请求缓冲区:

(defun insert-add-times-to-buffer-at-point (buffer)
  (interactive "BBuffer to add and insert times: ")
  (with-current-buffer buffer
    (insert (add-times))))

答案 1 :(得分:1)

这是你想要的吗?

(defun add-times ()                 
  (interactive)                                
  (let ((minutes 0) (seconds 0))     
    (save-excursion                        
      (goto-char (point-min))
      (while (re-search-forward "\\([0-9]+\\)'\\('\\)?" (point-max) t)
        (if (match-string 2)
            (setq seconds (+ seconds (string-to-number (match-string 1))))
          (setq minutes (+ minutes (string-to-number (match-string 1)))))))
    (insert (format "%d'%d''"(+ minutes (/ seconds 60)) (% seconds 60)))))