changeset 47828:4944e0ddf992

(add-change-log-entry): Don't call find-file at all if we're already in the proper buffer. (change-log-resolve-conflict): New fun. (change-log-mode): Use it and use define-derived-mode. (change-log-merge): Allow other-log to be a buffer. Don't add a \n if there are already enough \n's.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 10 Oct 2002 17:45:17 +0000
parents 9d7b63dcf005
children 353dae5e0134
files lisp/add-log.el
diffstat 1 files changed, 37 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/add-log.el	Thu Oct 10 17:30:20 2002 +0000
+++ b/lisp/add-log.el	Thu Oct 10 17:45:17 2002 +0000
@@ -464,10 +464,10 @@
 	 (item (add-log-file-name buffer-file file-name))
 	 bound)
 
-    (if (or (and other-window (not (equal file-name buffer-file-name)))
-	    (window-dedicated-p (selected-window)))
-	(find-file-other-window file-name)
-      (find-file file-name))
+    (unless (equal file-name buffer-file-name)
+      (if (or other-window (window-dedicated-p (selected-window)))
+	  (find-file-other-window file-name)
+	(find-file file-name)))
     (or (eq major-mode 'change-log-mode)
 	(change-log-mode))
     (undo-boundary)
@@ -588,22 +588,16 @@
 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
 
 ;;;###autoload
-(defun change-log-mode ()
+(define-derived-mode change-log-mode text-mode "Change Log"
   "Major mode for editing change logs; like Indented Text Mode.
 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
 Each entry behaves as a paragraph, and the entries for one day as a page.
 Runs `change-log-mode-hook'."
-  (interactive)
-  (kill-all-local-variables)
-  (indented-text-mode)
-  (setq major-mode 'change-log-mode
-	mode-name "Change Log"
-	left-margin 8
+  (setq left-margin 8
 	fill-column 74
 	indent-tabs-mode t
 	tab-width 8)
-  (use-local-map change-log-mode-map)
   (set (make-local-variable 'fill-paragraph-function)
        'change-log-fill-paragraph)
   ;; We really do want "^" in paragraph-start below: it is only the
@@ -616,10 +610,11 @@
   ;; is grouped with what follows.
   (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
   (set (make-local-variable 'version-control) 'never)
+  (set (make-local-variable 'smerge-resolve-function)
+       'change-log-resolve-conflict)
   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
   (set (make-local-variable 'font-lock-defaults)
-       '(change-log-font-lock-keywords t nil nil backward-paragraph))
-  (run-hooks 'change-log-mode-hook))
+       '(change-log-font-lock-keywords t nil nil backward-paragraph)))
 
 ;; It might be nice to have a general feature to replace this.  The idea I
 ;; have is a variable giving a regexp matching text which should not be
@@ -904,18 +899,34 @@
 		(error nil)))))
     (error "Bad date")))
 
+(defun change-log-resolve-conflict ()
+  "Function to be used in `smerge-resolve-function'."
+  (let ((buf (current-buffer)))
+    (with-temp-buffer
+      (insert-buffer-substring buf (match-beginning 1) (match-end 1))
+      (save-match-data (change-log-mode))
+      (let ((other-buf (current-buffer)))
+	(with-current-buffer buf
+	  (save-excursion
+	    (save-restriction
+	      (narrow-to-region (match-beginning 0) (match-end 0))
+	      (replace-match (match-string 3) t t)
+	      (change-log-merge other-buf))))))))
+
 ;;;###autoload
 (defun change-log-merge (other-log)
   "Merge the contents of ChangeLog file OTHER-LOG with this buffer.
 Both must be found in Change Log mode (since the merging depends on
-the appropriate motion commands).
+the appropriate motion commands).  OTHER-LOG can be either a file name
+or a buffer.
 
 Entries are inserted in chronological order.  Both the current and
 old-style time formats for entries are supported."
   (interactive "*fLog file name to merge: ")
   (if (not (eq major-mode 'change-log-mode))
       (error "Not in Change Log mode"))
-  (let ((other-buf (find-file-noselect other-log))
+  (let ((other-buf (if (bufferp other-log) other-log
+		     (find-file-noselect other-log)))
 	(buf (current-buffer))
 	date1 start end)
     (save-excursion
@@ -938,12 +949,16 @@
 	      (insert-buffer-substring other-buf start end)
 	    ;; At the end of the original buffer, insert a newline to
 	    ;; separate entries and then the rest of the file being
-	    ;; merged.  Move to the end of it to terminate outer loop.
-	    (insert "\n")
-	    (insert-buffer-substring other-buf start
-				     (with-current-buffer other-buf
-				       (goto-char (point-max))
-				       (point)))))))))
+	    ;; merged.
+	    (unless (or (bobp)
+			(and (= ?\n (char-before))
+			     (or (<= (1- (point)) (point-min))
+				 (= ?\n (char-before (1- (point)))))))
+	      (insert "\n"))
+	    ;; Move to the end of it to terminate outer loop.
+	    (with-current-buffer other-buf
+	      (goto-char (point-max)))
+	    (insert-buffer-substring other-buf start)))))))
 
 ;;;###autoload
 (defun change-log-redate ()