all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* a few questions about a simple mode (git-comments)
@ 2008-08-10 13:49 Peter Sanford
  2008-08-11 10:26 ` Nikolaj Schumacher
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Sanford @ 2008-08-10 13:49 UTC (permalink / raw)
  To: help-gnu-emacs

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))




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: a few questions about a simple mode (git-comments)
  2008-08-10 13:49 a few questions about a simple mode (git-comments) Peter Sanford
@ 2008-08-11 10:26 ` Nikolaj Schumacher
  0 siblings, 0 replies; 2+ messages in thread
From: Nikolaj Schumacher @ 2008-08-11 10:26 UTC (permalink / raw)
  To: Peter Sanford; +Cc: help-gnu-emacs

Peter Sanford <pms.mail@gmail.com> wrote:

> The quoted region is colored, but the rest of the line is not. I'm not
> sure why this is.

Sorry, I have no time to review your code, but whenever that has
happened to me, it could be fixed by changing the keyword order.

See the documentation `font-lock-add-keywords'.
It has a parameter called HOW, and the keywords can have a property
called `prepend'.



regards,
Nikolaj Schumacher




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-08-11 10:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-10 13:49 a few questions about a simple mode (git-comments) Peter Sanford
2008-08-11 10:26 ` Nikolaj Schumacher

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.