help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

a few questions about a simple mode (git-comments)


From: Peter Sanford
Subject: a few questions about a simple mode (git-comments)
Date: Sun, 10 Aug 2008 06:49:11 -0700
User-agent: Thunderbird 2.0.0.16 (X11/20080724)

Howdy,

I've written a simple major-mode that displays the recent checkin comments from a git repository. (I use it to quickly find checkins of interest and review them.) It does some simple syntax highlighting based on the output of git-log. The highlighting works as expected, except for lines that have quotation marks in them. The quoted region is colored, but the rest of the line is not. I'm not sure why this is.

I am also using the same regexp in multiple places ("[0-9a-f]\\{40\\}$"). I would like to extract that out into its own construct (probably a constant), but I was unable to figure out how to make that work.

I suspect that there are a number of ways to improve this code. Any and all suggestions would be appreciated.

Thanks,

Peter Sanford


(provide 'git-comments)

(defvar git-comments-font-lock-keywords-1
  (list
   '("^\s*\\(Author\\|Date\\|git-svn-id\\):\\|commit" .
     font-lock-builtin-face)))

(defvar git-comments-font-lock-keywords-2
  (append git-comments-font-lock-keywords-1
          (list
           '("[0-9a-f]\\{40\\}$" . font-lock-keyword-face))))

(defvar git-comments-font-lock-keywords-3
  (append git-comments-font-lock-keywords-2
          (list
           '("^    .*" . font-lock-comment-face))))

(defvar git-comments-font-lock-keywords
  git-comments-font-lock-keywords-3
  "Default highlighting for git-comments mode")

(defvar git-comments-mode-hook nil)
(defvar git-comments-mode-map
  (let ((git-comments-map (make-keymap)))
    (define-key git-comments-map [f4]
      'git-comments-diff-for-current-comment)
    git-comments-map)
  "Keymap for git-comments major mode")

(defun git-comments-diff-for-revision-at-point ()
  "Show git diff for current sha1"
  (interactive)
  (let ((sha1 (current-word)))
    (switch-to-buffer (get-buffer-create
                       (format "*git diff %s*" (substring sha1 0 10))))
    (insert (shell-command-to-string
     (format "git-diff %s^ %s" sha1 sha1))))
  (diff-mode)
  (beginning-of-buffer))

(defun git-comments-diff-for-current-comment ()
  "Shows the diff for the current comment"
  (interactive)
  (save-excursion
    (unless (git-comments-point-on-sha1-commit-line-p)
      (search-backward-regexp "commit [0-9a-f]\\{40\\}$"))
    (end-of-line)
    (search-backward-regexp "\\w")
    (git-comments-diff-for-revision-at-point)))

(defun git-comments-point-on-sha1-commit-line-p ()
  (string-match
   "[0-9a-f]\\{40\\}$"
   (buffer-substring
    (line-beginning-position) (line-end-position))))

(defun git-comments-mode ()
  "Major mode for git-comments"
  (interactive)
  (kill-all-local-variables)
  (use-local-map git-comments-mode-map)
  (set (make-local-variable 'font-lock-defaults)
       '(git-comments-font-lock-keywords))
  (setq major-mode 'git-comments-mode)
  (setq mode-name "Git Comments")
  (run-hooks 'git-comments-mode-hook))

(defun git-recent-checkin-comments (&optional n)
  "Shows recent checkin comments"
  (interactive "P")
  (unless n (setq n 100))
  (switch-to-buffer (get-buffer-create "*git recent checkin comments*"))
  (insert (shell-command-to-string (format "git log -n %d" n)))
  (git-comments-mode)
  (beginning-of-buffer))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]