繁体   English   中英

如何改进emacs的vc-mode注释?

[英]How can I improve emacs' vc-mode annotation?

我发现hg annotate在跟踪代码的来源方面非常有用,但是我发现在emacs的vc-mode中, vc-annotate是......不太好。 这是一个片段:

297 Wed Oct 06 15:21:30 2010 -0600 aws/lib/survey/creator/dbTemplates/web/views.sql: $$,$$
687 Mon Dec 20 10:25:41 2010 -0700 aws/lib/survey/creator/dbTemplates/web/views.sql: GRANT SELECT ON survey_length_view TO reportuser,surveyuser,sampleserver,sampleloader;
687 Mon Dec 20 10:25:41 2010 -0700 aws/lib/survey/creator/dbTemplates/web/views.sql: GRANT ALL ON survey_length_view TO adminuser, GROUP staff;
297 Wed Oct 06 15:21:30 2010 -0600 aws/lib/survey/creator/dbTemplates/web/views.sql: $$);

正如您所看到的,那里有很多序言,还有相当多的冗余信息。 我不需要知道完整的ISO时间戳(大多数时候,无论如何),当我选择注释时,我在文件中的事实涵盖了文件的路径。 确实需要知道是谁做出了改变,这在这里显然不存在。

如何修复此注释工具以使其变得有用?

vc-hg.el似乎将参数硬编码为hg annotate ,因此在加载vc-hg后需要重新定义命令:

(require 'vc-hg)
(defun vc-hg-annotate-command (file buffer &optional revision)
  "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
Optional arg REVISION is a revision to annotate from."
  (vc-hg-command buffer 0 file "annotate" "-d" "-n" "--follow"
                 (when revision (concat "-r" revision))))

简单地删除参数可能会导致您因缺少信息而失去功能,因此更好的解决方案是遵循vc-bzr.el中的示例,其中一些信息被删除并放入工具提示中。 如果您沿着这条路走下去,请考虑将您的改进贡献给Emacs。

我也有这个问题,这是我的解决方案:

;; vc-mode
(defconst vc-hg-annotate-better-re
  "^[   ]*\\(.+?\\) \\([0-9]+\\) \\([0-9a-f]+\\) \\(.+?\\+[0-9]+\\) +\\(.+?\\):\\([0-9]+\\):\\(.*\n?\\)")

(defconst vc-hg-annotate-result-re
  "^[\t ]*\\([0-9]+\\) +\\(.+?\\) .*")

(eval-after-load "vc-hg"
  '(defun vc-hg-annotate-command (file buffer &optional revision)
    "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
Optional arg REVISION is a revision to annotate from."
    (vc-hg-command buffer 'async file "annotate" "-d" "-n" "-u" "-c" "-l" "--follow"
                   (when revision (concat "-r" revision)))
    (lexical-let ((table (make-hash-table :test 'equal)))
      (set-process-filter
       (get-buffer-process buffer)
       (lambda (proc string)
         (when (process-buffer proc)
           (with-current-buffer (process-buffer proc)
             (setq string (concat (process-get proc :vc-left-over) string))

             (while (string-match vc-hg-annotate-better-re string)
               (let* ((author (match-string 1 string))
                      (rev (match-string 2 string))
                      (changeset (match-string 3 string))
                      (date (match-string 4 string))
                      (key (concat rev author date))
                      (file (match-string 5 string))
                      (lineno (match-string 6 string))
                      (content (match-string 7 string))
                      (tag (gethash key table))
                      (inhibit-read-only t))
                 (setq string (substring string (match-end 0)))
                 (unless tag
                   (setq tag
                         (propertize
                          (format "%-5s %-8.8s" rev author)
                          'help-echo (format "Revision: %d, author: %s, date: %s"
                                             (string-to-number rev)
                                             author date)
                          'mouse-face 'highlight))
                   (puthash key tag table))
                 (goto-char (process-mark proc))
                 (insert tag content)
                 (move-marker (process-mark proc) (point))))
             (process-put proc :vc-left-over string))))))))

(eval-after-load "vc-hg"
  '(defun vc-hg-annotate-time ()
    (save-excursion
      (beginning-of-line)
      (when (looking-at vc-hg-annotate-result-re)
        (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
          (string-match ", date: \\(.+\\)\\'" prop)
          (let ((str (match-string-no-properties 1 prop)))
            (vc-annotate-convert-time (date-to-time str))))))))


(eval-after-load "vc-hg"
  '(defun vc-hg-annotate-extract-revision-at-line ()
    (save-excursion
      (beginning-of-line)
      (when (looking-at vc-hg-annotate-result-re)
        (match-string-no-properties 1)))))

将其添加到您的init文件中,您将获得与bzr annotate类似的输出格式:

266          jimb@re | /* Window creation, deletion and examination for GNU Emacs.
266          jimb@re |    Does not include redisplay.
102971       rgm@gnu |    Copyright (C) 1985-1987, 1993-1998, 2000-2011
77438.1.2234 rgm@gnu |                  Free Software Foundation, Inc.
266          jimb@re | 
266          jimb@re | This file is part of GNU Emacs.

遗憾的是,像这样过滤注释输出确实会使大文件上的注释变慢,我还没有找到解决方法(对bzr中的文件进行注释也很慢)。

这在Emacs 25中得到了解决。

;; One line printed by "hg annotate -dq -n -u --follow" looks like this:
;;   b56girard 114590 2012-03-13 CLOBBER: Lorem ipsum dolor sit
;; i.e. AUTHOR REVISION DATE FILENAME: CONTENTS
;; The user can omit options "-u" and/or "--follow".  Then it'll look like:
;;   114590 2012-03-13 CLOBBER:
;; or
;;   b56girard 114590 2012-03-13:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM