]> code.delx.au - gnu-emacs/blobdiff - lisp/log-edit.el
(top-level): No longer activate on load.
[gnu-emacs] / lisp / log-edit.el
index bc766d0226e77faea2d14a7f4d0349a75ed3a966..1fa48b65aeee2080252187563b9b0893eb287142 100644 (file)
@@ -1,11 +1,9 @@
 ;;; log-edit.el --- Major mode for editing CVS commit messages
 
-;; Copyright (C) 1999-2000  Free Software Foundation, Inc.
+;; Copyright (C) 1999,2000,2003,2004  Free Software Foundation, Inc.
 
 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
 ;; Keywords: pcl-cvs cvs commit log
-;; Version: $Name:  $
-;; Revision: $Id: log-edit.el,v 1.7 2000/06/02 23:03:31 monnier Exp $
 
 ;; This file is part of GNU Emacs.
 
 (require 'add-log)                     ; for all the ChangeLog goodies
 (require 'pcvs-util)
 (require 'ring)
-(require 'vc)
 
-;;;; 
+;;;;
 ;;;; Global Variables
-;;;; 
+;;;;
 
 (defgroup log-edit nil
   "Major mode for editing RCS and CVS commit messages."
 ;; compiler pacifiers
 (defvar cvs-buffer)
 
+\f
+;; The main keymap
+
 (easy-mmode-defmap log-edit-mode-map
   `(("\C-c\C-c" . log-edit-done)
     ("\C-c\C-a" . log-edit-insert-changelog)
     ("\C-c\C-f" . log-edit-show-files)
-    ("\C-c?" . log-edit-mode-help))
-  "Keymap for the `log-edit-mode' (used when editing cvs log messages)."
-  :group 'log-edit
-  :inherit (if (boundp 'vc-log-entry-mode) vc-log-entry-mode
-            (if (boundp 'vc-log-mode-map) vc-log-mode-map)))
-
-(defcustom log-edit-confirm t
+    ("\M-n"    . log-edit-next-comment)
+    ("\M-p"    . log-edit-previous-comment)
+    ("\M-r"    . log-edit-comment-search-backward)
+    ("\M-s"    . log-edit-comment-search-forward)
+    ("\C-c?"   . log-edit-mode-help))
+  "Keymap for the `log-edit-mode' (to edit version control log messages)."
+  :group 'log-edit)
+
+;; Compatibility with old names.  Should we bother ?
+(defvar vc-log-mode-map log-edit-mode-map)
+(defvar vc-log-entry-mode vc-log-mode-map)
+
+(easy-menu-define log-edit-menu log-edit-mode-map
+  "Menu used for `log-edit-mode'."
+  '("Log-Edit"
+    ["Done" log-edit-done
+     :help "Exit log-edit and proceed with the actual action."]
+    "--"
+    ["Insert ChangeLog" log-edit-insert-changelog]
+    ["Add to ChangeLog" log-edit-add-to-changelog]
+    "--"
+    ["List files" log-edit-show-files
+     :help "Show the list of relevant files."]
+    "--"
+    ["Previous comment"                log-edit-previous-comment]
+    ["Next comment"            log-edit-next-comment]
+    ["Search comment forward"  log-edit-comment-search-forward]
+    ["Search comment backward" log-edit-comment-search-backward]))
+
+(defcustom log-edit-confirm 'changed
   "*If non-nil, `log-edit-done' will request confirmation.
 If 'changed, only request confirmation if the list of files has
   changed since the beginning of the log-edit session."
@@ -75,8 +98,9 @@ If 'changed, only request confirmation if the list of files has
   :group 'log-edit
   :type 'boolean)
 
-(defvar cvs-commit-buffer-require-final-newline t
-  "Obsolete, use `log-edit-require-final-newline'.")
+(defvar cvs-commit-buffer-require-final-newline t)
+(make-obsolete-variable 'cvs-commit-buffer-require-final-newline
+                        'log-edit-require-final-newline)
 
 (defcustom log-edit-require-final-newline
   cvs-commit-buffer-require-final-newline
@@ -110,11 +134,12 @@ conventions, or to allow recording the message in some other database,
 such as a bug-tracking system.  The list of files about to be committed
 can be obtained from `log-edit-files'."
   :group 'log-edit
-  :type '(hook :options (log-edit-delete-common-indentation
+  :type '(hook :options (log-edit-set-common-indentation
                         log-edit-add-to-changelog)))
 
-(defvar cvs-changelog-full-paragraphs t
-  "Obsolete, use `log-edit-changelog-full-paragraphs'.")
+(defvar cvs-changelog-full-paragraphs t)
+(make-obsolete-variable 'cvs-changelog-full-paragraphs
+                        'log-edit-changelog-full-paragraphs)
 
 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
   "*If non-nil, include full ChangeLog paragraphs in the log.
@@ -143,31 +168,170 @@ when this variable is set to nil.")
 (defvar log-edit-initial-files nil)
 (defvar log-edit-callback nil)
 (defvar log-edit-listfun nil)
-
-;;;; 
-;;;; Actual code
-;;;; 
+(defvar log-edit-parent-buffer nil)
+
+;;; Originally taken from VC-Log mode
+
+(defconst log-edit-maximum-comment-ring-size 32
+  "Maximum number of saved comments in the comment ring.")
+(defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
+(defvar log-edit-comment-ring-index nil)
+(defvar log-edit-last-comment-match "")
+
+(defun log-edit-new-comment-index (stride len)
+  "Return the comment index STRIDE elements from the current one.
+LEN is the length of `log-edit-comment-ring'."
+  (mod (cond
+       (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
+       ;; Initialize the index on the first use of this command
+       ;; so that the first M-p gets index 0, and the first M-n gets
+       ;; index -1.
+       ((> stride 0) (1- stride))
+       (t stride))
+       len))
+
+(defun log-edit-previous-comment (arg)
+  "Cycle backwards through comment history.
+With a numeric prefix ARG, go back ARG comments."
+  (interactive "*p")
+  (let ((len (ring-length log-edit-comment-ring)))
+    (if (<= len 0)
+       (progn (message "Empty comment ring") (ding))
+      ;; Don't use `erase-buffer' because we don't want to `widen'.
+      (delete-region (point-min) (point-max))
+      (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
+      (message "Comment %d" (1+ log-edit-comment-ring-index))
+      (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
+
+(defun log-edit-next-comment (arg)
+  "Cycle forwards through comment history.
+With a numeric prefix ARG, go forward ARG comments."
+  (interactive "*p")
+  (log-edit-previous-comment (- arg)))
+
+(defun log-edit-comment-search-backward (str &optional stride)
+  "Search backwards through comment history for substring match of STR.
+If the optional argument STRIDE is present, that is a step-width to use
+when going through the comment ring."
+  ;; Why substring rather than regexp ?   -sm
+  (interactive
+   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
+  (unless stride (setq stride 1))
+  (if (string= str "")
+      (setq str log-edit-last-comment-match)
+    (setq log-edit-last-comment-match str))
+  (let* ((str (regexp-quote str))
+        (len (ring-length log-edit-comment-ring))
+        (n (log-edit-new-comment-index stride len)))
+    (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
+                 (not (string-match str (ring-ref log-edit-comment-ring n))))
+      (setq n (+ n stride)))
+    (setq log-edit-comment-ring-index n)
+    (log-edit-previous-comment 0)))
+
+(defun log-edit-comment-search-forward (str)
+  "Search forwards through comment history for a substring match of STR."
+  (interactive
+   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
+  (log-edit-comment-search-backward str -1))
+
+(defun log-edit-comment-to-change-log (&optional whoami file-name)
+  "Enter last VC comment into the change log for the current file.
+WHOAMI (interactive prefix) non-nil means prompt for user name
+and site.  FILE-NAME is the name of the change log; if nil, use
+`change-log-default-name'.
+
+This may be useful as a `log-edit-checkin-hook' to update change logs
+automatically."
+  (interactive (if current-prefix-arg
+                  (list current-prefix-arg
+                        (prompt-for-change-log-name))))
+  (let (;; Extract the comment first so we get any error before doing anything.
+       (comment (ring-ref log-edit-comment-ring 0))
+       ;; Don't let add-change-log-entry insert a defun name.
+       (add-log-current-defun-function 'ignore)
+       end)
+    ;; Call add-log to do half the work.
+    (add-change-log-entry whoami file-name t t)
+    ;; Insert the VC comment, leaving point before it.
+    (setq end (save-excursion (insert comment) (point-marker)))
+    (if (looking-at "\\s *\\s(")
+       ;; It starts with an open-paren, as in "(foo): Frobbed."
+       ;; So remove the ": " add-log inserted.
+       (delete-char -2))
+    ;; Canonicalize the white space between the file name and comment.
+    (just-one-space)
+    ;; Indent rest of the text the same way add-log indented the first line.
+    (let ((indentation (current-indentation)))
+      (save-excursion
+       (while (< (point) end)
+         (forward-line 1)
+         (indent-to indentation))
+       (setq end (point))))
+    ;; Fill the inserted text, preserving open-parens at bol.
+    (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
+      (beginning-of-line)
+      (fill-region (point) end))
+    ;; Canonicalize the white space at the end of the entry so it is
+    ;; separated from the next entry by a single blank line.
+    (skip-syntax-forward " " end)
+    (delete-char (- (skip-syntax-backward " ")))
+    (or (eobp) (looking-at "\n\n")
+       (insert "\n"))))
+
+;; Compatibility with old names.
+(defvaralias 'vc-comment-ring 'log-edit-comment-ring)
+(make-obsolete-variable 'vc-comment-ring 'log-edit-comment-ring "22.1")
+(defvaralias 'vc-comment-ring-index 'log-edit-comment-ring-index)
+(make-obsolete-variable 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
+(defalias 'vc-previous-comment 'log-edit-previous-comment)
+(make-obsolete 'vc-previous-comment 'log-edit-previous-comment "22.1")
+(defalias 'vc-next-comment 'log-edit-next-comment)
+(make-obsolete 'vc-next-comment 'log-edit-next-comment "22.1")
+(defalias 'vc-comment-search-reverse 'log-edit-comment-search-backward)
+(make-obsolete 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
+(defalias 'vc-comment-search-forward 'log-edit-comment-search-forward)
+(make-obsolete 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
+(defalias 'vc-comment-to-change-log 'log-edit-comment-to-change-log)
+(make-obsolete 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
+
+;;;
+;;; Actual code
+;;;
+
+(defvar log-edit-font-lock-keywords
+  '(("\\`\\(Summary:\\)\\(.*\\)"
+     (1 font-lock-keyword-face)
+     (2 font-lock-function-name-face))))
 
 ;;;###autoload
-(defun log-edit (callback &optional setup listfun &rest ignore)
+(defun log-edit (callback &optional setup listfun buffer &rest ignore)
   "Setup a buffer to enter a log message.
-The buffer will be put in `log-edit-mode'.
+\\<log-edit-mode-map>The buffer will be put in `log-edit-mode'.
 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
 Mark and point will be set around the entire contents of the
 buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
 Once you're done editing the message, pressing \\[log-edit-done] will call
-`log-edit-done' which will end up calling CALLBACK to do the actual commit."
-  (when (and log-edit-setup-invert (not (eq setup 'force)))
-    (setq setup (not setup)))
-  (when setup (erase-buffer))
-  (log-edit-mode)
-  (set (make-local-variable 'log-edit-callback) callback)
-  (set (make-local-variable 'log-edit-listfun) listfun)
-  (when setup (run-hooks 'log-edit-hook))
-  (goto-char (point-min)) (push-mark (point-max))
-  (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
-  (message (substitute-command-keys
-           "Press \\[log-edit-done] when you are done editing.")))
+`log-edit-done' which will end up calling CALLBACK to do the actual commit.
+LISTFUN if non-nil is a function of no arguments returning the list of files
+  that are concerned by the current operation (using relative names).
+If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
+  log message and go back to the current buffer when done.  Otherwise, it
+  uses the current buffer."
+  (let ((parent (current-buffer)))
+    (if buffer (pop-to-buffer buffer))
+    (when (and log-edit-setup-invert (not (eq setup 'force)))
+      (setq setup (not setup)))
+    (when setup (erase-buffer))
+    (log-edit-mode)
+    (set (make-local-variable 'log-edit-callback) callback)
+    (set (make-local-variable 'log-edit-listfun) listfun)
+    (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
+    (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
+    (when setup (run-hooks 'log-edit-hook))
+    (goto-char (point-min)) (push-mark (point-max))
+    (message (substitute-command-keys
+             "Press \\[log-edit-done] when you are done editing."))))
 
 (define-derived-mode log-edit-mode text-mode "Log-Edit"
   "Major mode for editing version-control log messages.
@@ -178,7 +342,9 @@ the package from which this is used might also provide additional
 commands (under C-x v for VC, for example).
 
 \\{log-edit-mode-map}"
-  (make-local-variable 'vc-comment-ring-index))
+  (set (make-local-variable 'font-lock-defaults)
+       '(log-edit-font-lock-keywords t))
+  (make-local-variable 'log-edit-comment-ring-index))
 
 (defun log-edit-hide-buf (&optional buf where)
   (when (setq buf (get-buffer (or buf log-edit-files-buf)))
@@ -196,8 +362,8 @@ If you want to abort the commit, simply delete the buffer."
   (when (equal (char-after) ?\n) (forward-char 1))
   (delete-region (point) (point-max))
   ;; Check for final newline
-  (if (and (> (point-max) 1)
-          (/= (char-after (1- (point-max))) ?\n)
+  (if (and (> (point-max) (point-min))
+          (/= (char-before (point-max)) ?\n)
           (or (eq log-edit-require-final-newline t)
               (and log-edit-require-final-newline
                    (y-or-n-p
@@ -207,11 +373,9 @@ If you want to abort the commit, simply delete the buffer."
        (goto-char (point-max))
        (insert ?\n)))
   (let ((comment (buffer-string)))
-    (when (and (boundp 'vc-comment-ring)
-              (ring-p vc-comment-ring)
-              (not (ring-empty-p vc-comment-ring))
-              (not (equal comment (ring-ref vc-comment-ring 0))))
-      (ring-insert vc-comment-ring comment)))
+    (when (or (ring-empty-p log-edit-comment-ring)
+             (not (equal comment (ring-ref log-edit-comment-ring 0))))
+      (ring-insert log-edit-comment-ring comment)))
   (let ((win (get-buffer-window log-edit-files-buf)))
     (if (and log-edit-confirm
             (not (and (eq log-edit-confirm 'changed)
@@ -223,9 +387,8 @@ If you want to abort the commit, simply delete the buffer."
               (message "Oh, well!  Later maybe?"))
       (run-hooks 'log-edit-done-hook)
       (log-edit-hide-buf)
-      (unless log-edit-keep-buffer
-       (cvs-bury-buffer (current-buffer)
-                        (when (boundp 'cvs-buffer) cvs-buffer)))
+      (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
+       (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
       (call-interactively log-edit-callback))))
 
 (defun log-edit-files ()
@@ -247,7 +410,7 @@ To select default log text, we:
 - use those paragraphs as the log text."
   (interactive)
   (log-edit-insert-changelog-entries (log-edit-files))
-  (log-edit-delete-common-indentation)
+  (log-edit-set-common-indentation)
   (goto-char (point-min))
   (when (looking-at "\\*\\s-+")
     (forward-line 1)
@@ -266,8 +429,13 @@ To select default log text, we:
      (substitute-command-keys
       "Type `\\[log-edit-done]' to finish commit.  Try `\\[describe-function] log-edit-done' for more help."))))
 
-(defun log-edit-delete-common-indentation ()
-  "Unindent the current buffer rigidly until at least one line is flush left."
+(defcustom log-edit-common-indent 0
+  "Minimum indentation to use in `log-edit-set-common-indentation'."
+  :group 'log-edit
+  :type 'integer)
+
+(defun log-edit-set-common-indentation ()
+  "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
   (save-excursion
     (let ((common (point-max)))
       (goto-char (point-min))
@@ -275,19 +443,19 @@ To select default log text, we:
         (if (not (looking-at "^[ \t]*$"))
             (setq common (min common (current-indentation))))
         (forward-line 1))
-      (indent-rigidly (point-min) (point-max) (- common)))))
+      (indent-rigidly (point-min) (point-max)
+                     (- log-edit-common-indent common)))))
 
 (defun log-edit-show-files ()
   "Show the list of files to be committed."
   (interactive)
   (let* ((files (log-edit-files))
-        (editbuf (current-buffer))
-        (buf (get-buffer-create "*log-edit-files*")))
+        (buf (get-buffer-create log-edit-files-buf)))
     (with-current-buffer buf
       (log-edit-hide-buf buf 'all)
       (setq buffer-read-only nil)
       (erase-buffer)
-      (insert (mapconcat 'identity files "\n"))
+      (cvs-insert-strings files)
       (setq buffer-read-only t)
       (goto-char (point-min))
       (save-selected-window
@@ -300,23 +468,23 @@ To select default log text, we:
   (interactive)
   (when (file-readable-p "CVS/Template")
     (insert-file-contents "CVS/Template")))
-  
+
 
 (defun log-edit-add-to-changelog ()
   "Insert this log message into the appropriate ChangeLog file."
   (interactive)
   ;; Yuck!
-  (unless (string= (buffer-string) (ring-ref vc-comment-ring 0))
-    (ring-insert vc-comment-ring (buffer-string)))
+  (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
+    (ring-insert log-edit-comment-ring (buffer-string)))
   (dolist (f (log-edit-files))
     (let ((buffer-file-name (expand-file-name f)))
       (save-excursion
-       (vc-comment-to-change-log)))))
+       (log-edit-comment-to-change-log)))))
 
-;;;; 
+;;;;
 ;;;; functions for getting commit message from ChangeLog a file...
 ;;;; Courtesy Jim Blandy
-;;;; 
+;;;;
 
 (defun log-edit-narrow-changelog ()
   "Narrow to the top page of the current buffer, a ChangeLog file.
@@ -403,12 +571,21 @@ where LOGBUFFER is the name of the ChangeLog buffer, and each
   (save-excursion
     (let ((changelog-file-name
           (let ((default-directory
-                  (file-name-directory (expand-file-name file))))
-            ;; `find-change-log' uses `change-log-default-name' if set
-            ;; and sets it before exiting, so we need to work around
-            ;; that memoizing which is undesired here
-            (setq change-log-default-name nil)
-            (find-change-log))))
+                  (file-name-directory (expand-file-name file)))
+                (visiting-buffer (find-buffer-visiting file)))
+            ;; If there is a buffer visiting FILE, and it has a local
+            ;; value for `change-log-default-name', use that.
+            (if (and visiting-buffer
+                     (local-variable-p 'change-log-default-name
+                                       visiting-buffer))
+                (save-excursion
+                  (set-buffer visiting-buffer)
+                  change-log-default-name)
+              ;; `find-change-log' uses `change-log-default-name' if set
+              ;; and sets it before exiting, so we need to work around
+              ;; that memoizing which is undesired here
+              (setq change-log-default-name nil)
+              (find-change-log)))))
       (set-buffer (find-file-noselect changelog-file-name))
       (unless (eq major-mode 'change-log-mode) (change-log-mode))
       (goto-char (point-min))
@@ -418,7 +595,7 @@ where LOGBUFFER is the name of the ChangeLog buffer, and each
        (save-restriction
          (log-edit-narrow-changelog)
          (goto-char (point-min))
-         
+
          ;; Search for the name of FILE relative to the ChangeLog.  If that
          ;; doesn't occur anywhere, they're not using full relative
          ;; filenames in the ChangeLog, so just look for FILE; we'll accept
@@ -469,7 +646,5 @@ Sort REGIONS front-to-back first."
 
 (provide 'log-edit)
 
-;;; Change Log:
-;; $log$
-
+;;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
 ;;; log-edit.el ends here