log-edit.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords: pcl-cvs cvs commit log vc
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Todo:
  18. ;; - Move in VC's code
  19. ;; - Add compatibility for VC's hook variables
  20. ;;; Code:
  21. (eval-when-compile (require 'cl))
  22. (require 'add-log) ; for all the ChangeLog goodies
  23. (require 'pcvs-util)
  24. (require 'ring)
  25. ;;;;
  26. ;;;; Global Variables
  27. ;;;;
  28. (defgroup log-edit nil
  29. "Major mode for editing RCS and CVS commit messages."
  30. :group 'pcl-cvs
  31. :group 'vc ; It's used by VC.
  32. :version "21.1"
  33. :prefix "log-edit-")
  34. ;; compiler pacifiers
  35. (defvar cvs-buffer)
  36. ;; The main keymap
  37. (easy-mmode-defmap log-edit-mode-map
  38. `(("\C-c\C-c" . log-edit-done)
  39. ("\C-c\C-a" . log-edit-insert-changelog)
  40. ("\C-c\C-d" . log-edit-show-diff)
  41. ("\C-c\C-f" . log-edit-show-files)
  42. ("\M-n" . log-edit-next-comment)
  43. ("\M-p" . log-edit-previous-comment)
  44. ("\M-r" . log-edit-comment-search-backward)
  45. ("\M-s" . log-edit-comment-search-forward)
  46. ("\C-c?" . log-edit-mode-help))
  47. "Keymap for the `log-edit-mode' (to edit version control log messages)."
  48. :group 'log-edit)
  49. ;; Compatibility with old names. Should we bother ?
  50. (defvar vc-log-mode-map log-edit-mode-map)
  51. (defvar vc-log-entry-mode vc-log-mode-map)
  52. (easy-menu-define log-edit-menu log-edit-mode-map
  53. "Menu used for `log-edit-mode'."
  54. '("Log-Edit"
  55. ["Done" log-edit-done
  56. :help "Exit log-edit and proceed with the actual action."]
  57. "--"
  58. ["Insert ChangeLog" log-edit-insert-changelog
  59. :help "Insert a log message by looking at the ChangeLog"]
  60. ["Add to ChangeLog" log-edit-add-to-changelog
  61. :help "Insert this log message into the appropriate ChangeLog file"]
  62. "--"
  63. ["Show diff" log-edit-show-diff
  64. :help "Show the diff for the files to be committed."]
  65. ["List files" log-edit-show-files
  66. :help "Show the list of relevant files."]
  67. "--"
  68. ["Previous comment" log-edit-previous-comment
  69. :help "Cycle backwards through comment history"]
  70. ["Next comment" log-edit-next-comment
  71. :help "Cycle forwards through comment history."]
  72. ["Search comment forward" log-edit-comment-search-forward
  73. :help "Search forwards through comment history for a substring match of str"]
  74. ["Search comment backward" log-edit-comment-search-backward
  75. :help "Search backwards through comment history for substring match of str"]))
  76. (defcustom log-edit-confirm 'changed
  77. "If non-nil, `log-edit-done' will request confirmation.
  78. If 'changed, only request confirmation if the list of files has
  79. changed since the beginning of the log-edit session."
  80. :group 'log-edit
  81. :type '(choice (const changed) (const t) (const nil)))
  82. (defcustom log-edit-keep-buffer nil
  83. "If non-nil, don't hide the buffer after `log-edit-done'."
  84. :group 'log-edit
  85. :type 'boolean)
  86. (defvar cvs-commit-buffer-require-final-newline t)
  87. (make-obsolete-variable 'cvs-commit-buffer-require-final-newline
  88. 'log-edit-require-final-newline
  89. "21.1")
  90. (defcustom log-edit-require-final-newline
  91. cvs-commit-buffer-require-final-newline
  92. "Enforce a newline at the end of commit log messages.
  93. Enforce it silently if t, query if non-nil and don't do anything if nil."
  94. :group 'log-edit
  95. :type '(choice (const ask) (const t) (const nil)))
  96. (defcustom log-edit-setup-invert nil
  97. "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
  98. If SETUP is 'force, this variable has no effect."
  99. :group 'log-edit
  100. :type 'boolean)
  101. (defcustom log-edit-hook '(log-edit-insert-cvs-template
  102. log-edit-show-files
  103. log-edit-insert-changelog)
  104. "Hook run at the end of `log-edit'."
  105. :group 'log-edit
  106. :type '(hook :options (log-edit-insert-changelog
  107. log-edit-insert-cvs-rcstemplate
  108. log-edit-insert-cvs-template
  109. log-edit-insert-filenames)))
  110. (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
  111. "Hook run when entering `log-edit-mode'."
  112. :group 'log-edit
  113. :type 'hook)
  114. (defcustom log-edit-done-hook nil
  115. "Hook run before doing the actual commit.
  116. This hook can be used to cleanup the message, enforce various
  117. conventions, or to allow recording the message in some other database,
  118. such as a bug-tracking system. The list of files about to be committed
  119. can be obtained from `log-edit-files'."
  120. :group 'log-edit
  121. :type '(hook :options (log-edit-set-common-indentation
  122. log-edit-add-to-changelog)))
  123. (defcustom log-edit-strip-single-file-name nil
  124. "If non-nil, remove file name from single-file log entries."
  125. :type 'boolean
  126. :safe 'booleanp
  127. :group 'log-edit
  128. :version "24.1")
  129. (defvar cvs-changelog-full-paragraphs t)
  130. (make-obsolete-variable 'cvs-changelog-full-paragraphs
  131. 'log-edit-changelog-full-paragraphs
  132. "21.1")
  133. (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
  134. "*If non-nil, include full ChangeLog paragraphs in the log.
  135. This may be set in the ``local variables'' section of a ChangeLog, to
  136. indicate the policy for that ChangeLog.
  137. A ChangeLog paragraph is a bunch of log text containing no blank lines;
  138. a paragraph usually describes a set of changes with a single purpose,
  139. but perhaps spanning several functions in several files. Changes in
  140. different paragraphs are unrelated.
  141. You could argue that the log entry for a file should contain the
  142. full ChangeLog paragraph mentioning the change to the file, even though
  143. it may mention other files, because that gives you the full context you
  144. need to understand the change. This is the behavior you get when this
  145. variable is set to t.
  146. On the other hand, you could argue that the log entry for a change
  147. should contain only the text for the changes which occurred in that
  148. file, because the log is per-file. This is the behavior you get
  149. when this variable is set to nil.")
  150. ;;;; Internal global or buffer-local vars
  151. (defconst log-edit-files-buf "*log-edit-files*")
  152. (defvar log-edit-initial-files nil)
  153. (defvar log-edit-callback nil)
  154. (defvar log-edit-diff-function nil)
  155. (defvar log-edit-listfun nil)
  156. (defvar log-edit-parent-buffer nil)
  157. ;;; Originally taken from VC-Log mode
  158. (defconst log-edit-maximum-comment-ring-size 32
  159. "Maximum number of saved comments in the comment ring.")
  160. (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
  161. (defvar log-edit-comment-ring-index nil)
  162. (defvar log-edit-last-comment-match "")
  163. (defun log-edit-new-comment-index (stride len)
  164. "Return the comment index STRIDE elements from the current one.
  165. LEN is the length of `log-edit-comment-ring'."
  166. (mod (cond
  167. (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
  168. ;; Initialize the index on the first use of this command
  169. ;; so that the first M-p gets index 0, and the first M-n gets
  170. ;; index -1.
  171. ((> stride 0) (1- stride))
  172. (t stride))
  173. len))
  174. (defun log-edit-previous-comment (arg)
  175. "Cycle backwards through comment history.
  176. With a numeric prefix ARG, go back ARG comments."
  177. (interactive "*p")
  178. (let ((len (ring-length log-edit-comment-ring)))
  179. (if (<= len 0)
  180. (progn (message "Empty comment ring") (ding))
  181. ;; Don't use `erase-buffer' because we don't want to `widen'.
  182. (delete-region (point-min) (point-max))
  183. (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
  184. (message "Comment %d" (1+ log-edit-comment-ring-index))
  185. (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
  186. (defun log-edit-next-comment (arg)
  187. "Cycle forwards through comment history.
  188. With a numeric prefix ARG, go forward ARG comments."
  189. (interactive "*p")
  190. (log-edit-previous-comment (- arg)))
  191. (defun log-edit-comment-search-backward (str &optional stride)
  192. "Search backwards through comment history for substring match of STR.
  193. If the optional argument STRIDE is present, that is a step-width to use
  194. when going through the comment ring."
  195. ;; Why substring rather than regexp ? -sm
  196. (interactive
  197. (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  198. (unless stride (setq stride 1))
  199. (if (string= str "")
  200. (setq str log-edit-last-comment-match)
  201. (setq log-edit-last-comment-match str))
  202. (let* ((str (regexp-quote str))
  203. (len (ring-length log-edit-comment-ring))
  204. (n (log-edit-new-comment-index stride len)))
  205. (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
  206. (not (string-match str (ring-ref log-edit-comment-ring n))))
  207. (setq n (+ n stride)))
  208. (setq log-edit-comment-ring-index n)
  209. (log-edit-previous-comment 0)))
  210. (defun log-edit-comment-search-forward (str)
  211. "Search forwards through comment history for a substring match of STR."
  212. (interactive
  213. (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  214. (log-edit-comment-search-backward str -1))
  215. (defun log-edit-comment-to-change-log (&optional whoami file-name)
  216. "Enter last VC comment into the change log for the current file.
  217. WHOAMI (interactive prefix) non-nil means prompt for user name
  218. and site. FILE-NAME is the name of the change log; if nil, use
  219. `change-log-default-name'.
  220. This may be useful as a `log-edit-checkin-hook' to update change logs
  221. automatically."
  222. (interactive (if current-prefix-arg
  223. (list current-prefix-arg
  224. (prompt-for-change-log-name))))
  225. (let (;; Extract the comment first so we get any error before doing anything.
  226. (comment (ring-ref log-edit-comment-ring 0))
  227. ;; Don't let add-change-log-entry insert a defun name.
  228. (add-log-current-defun-function 'ignore)
  229. end)
  230. ;; Call add-log to do half the work.
  231. (add-change-log-entry whoami file-name t t)
  232. ;; Insert the VC comment, leaving point before it.
  233. (setq end (save-excursion (insert comment) (point-marker)))
  234. (if (looking-at "\\s *\\s(")
  235. ;; It starts with an open-paren, as in "(foo): Frobbed."
  236. ;; So remove the ": " add-log inserted.
  237. (delete-char -2))
  238. ;; Canonicalize the white space between the file name and comment.
  239. (just-one-space)
  240. ;; Indent rest of the text the same way add-log indented the first line.
  241. (let ((indentation (current-indentation)))
  242. (save-excursion
  243. (while (< (point) end)
  244. (forward-line 1)
  245. (indent-to indentation))
  246. (setq end (point))))
  247. ;; Fill the inserted text, preserving open-parens at bol.
  248. (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
  249. (beginning-of-line)
  250. (fill-region (point) end))
  251. ;; Canonicalize the white space at the end of the entry so it is
  252. ;; separated from the next entry by a single blank line.
  253. (skip-syntax-forward " " end)
  254. (delete-char (- (skip-syntax-backward " ")))
  255. (or (eobp) (looking-at "\n\n")
  256. (insert "\n"))))
  257. ;; Compatibility with old names.
  258. (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
  259. (define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
  260. (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
  261. (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
  262. (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
  263. (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
  264. (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
  265. ;;;
  266. ;;; Actual code
  267. ;;;
  268. (defface log-edit-summary '((t :inherit font-lock-function-name-face))
  269. "Face for the summary in `log-edit-mode' buffers.")
  270. (defface log-edit-header '((t :inherit font-lock-keyword-face))
  271. "Face for the headers in `log-edit-mode' buffers.")
  272. (defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
  273. "Face for unknown headers in `log-edit-mode' buffers.")
  274. (defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
  275. ("Fixes") ("Author"))
  276. "AList of known headers and the face to use to highlight them.")
  277. (defconst log-edit-header-contents-regexp
  278. "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?")
  279. (defun log-edit-match-to-eoh (_limit)
  280. ;; FIXME: copied from message-match-to-eoh.
  281. (let ((start (point)))
  282. (rfc822-goto-eoh)
  283. ;; Typical situation: some temporary change causes the header to be
  284. ;; incorrect, so EOH comes earlier than intended: the last lines of the
  285. ;; intended headers are now not considered part of the header any more,
  286. ;; so they don't have the multiline property set. When the change is
  287. ;; completed and the header has its correct shape again, the lack of the
  288. ;; multiline property means we won't rehighlight the last lines of
  289. ;; the header.
  290. (if (< (point) start)
  291. nil ;No header within start..limit.
  292. ;; Here we disregard LIMIT so that we may extend the area again.
  293. (set-match-data (list start (point)))
  294. (point))))
  295. (defvar log-edit-font-lock-keywords
  296. ;; Copied/inspired by message-font-lock-keywords.
  297. `((log-edit-match-to-eoh
  298. (,(concat "^\\(\\([a-z]+\\):\\)" log-edit-header-contents-regexp)
  299. (progn (goto-char (match-beginning 0)) (match-end 0)) nil
  300. (1 (if (assoc (match-string 2) log-edit-headers-alist)
  301. 'log-edit-header
  302. 'log-edit-unknown-header)
  303. nil lax)
  304. ;; From `log-edit-header-contents-regexp':
  305. (3 (or (cdr (assoc (match-string 2) log-edit-headers-alist))
  306. 'log-edit-header)
  307. nil lax)))))
  308. ;; Used in Emacs 24.3+, and in Emacs's .dir-locals.el file. This is a
  309. ;; convenience setting so that people committing files to Emacs trunk
  310. ;; with Emacs 24.2 do not always get queried about this variable being
  311. ;; potentially unsafe.
  312. (put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)
  313. ;;;###autoload
  314. (defun log-edit (callback &optional setup params buffer mode &rest _ignore)
  315. "Setup a buffer to enter a log message.
  316. \\<log-edit-mode-map>The buffer will be put in mode MODE or `log-edit-mode'
  317. if MODE is nil.
  318. If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
  319. Mark and point will be set around the entire contents of the buffer so
  320. that it is easy to kill the contents of the buffer with \\[kill-region].
  321. Once you're done editing the message, pressing \\[log-edit-done] will call
  322. `log-edit-done' which will end up calling CALLBACK to do the actual commit.
  323. PARAMS if non-nil is an alist. Possible keys and associated values:
  324. `log-edit-listfun' -- function taking no arguments that returns the list of
  325. files that are concerned by the current operation (using relative names);
  326. `log-edit-diff-function' -- function taking no arguments that
  327. displays a diff of the files concerned by the current operation.
  328. If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
  329. log message and go back to the current buffer when done. Otherwise, it
  330. uses the current buffer."
  331. (let ((parent (current-buffer)))
  332. (if buffer (pop-to-buffer buffer))
  333. (when (and log-edit-setup-invert (not (eq setup 'force)))
  334. (setq setup (not setup)))
  335. (when setup
  336. (erase-buffer)
  337. (insert "Summary: \nAuthor: ")
  338. (save-excursion (insert "\n\n")))
  339. (if mode
  340. (funcall mode)
  341. (log-edit-mode))
  342. (set (make-local-variable 'log-edit-callback) callback)
  343. (if (listp params)
  344. (dolist (crt params)
  345. (set (make-local-variable (car crt)) (cdr crt)))
  346. ;; For backward compatibility with log-edit up to version 22.2
  347. ;; accept non-list PARAMS to mean `log-edit-list'.
  348. (set (make-local-variable 'log-edit-listfun) params))
  349. (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
  350. (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
  351. (when setup (run-hooks 'log-edit-hook))
  352. (goto-char (point-min)) (push-mark (point-max))
  353. (message "%s" (substitute-command-keys
  354. "Press \\[log-edit-done] when you are done editing."))))
  355. (define-derived-mode log-edit-mode text-mode "Log-Edit"
  356. "Major mode for editing version-control log messages.
  357. When done editing the log entry, just type \\[log-edit-done] which
  358. will trigger the actual commit of the file(s).
  359. Several other handy support commands are provided of course and
  360. the package from which this is used might also provide additional
  361. commands (under C-x v for VC, for example).
  362. \\{log-edit-mode-map}"
  363. (set (make-local-variable 'font-lock-defaults)
  364. '(log-edit-font-lock-keywords t t))
  365. (make-local-variable 'log-edit-comment-ring-index)
  366. (hack-dir-local-variables-non-file-buffer))
  367. (defun log-edit-hide-buf (&optional buf where)
  368. (when (setq buf (get-buffer (or buf log-edit-files-buf)))
  369. (let ((win (get-buffer-window buf where)))
  370. (if win (ignore-errors (delete-window win))))
  371. (bury-buffer buf)))
  372. (defun log-edit-done ()
  373. "Finish editing the log message and commit the files.
  374. If you want to abort the commit, simply delete the buffer."
  375. (interactive)
  376. ;; Clean up empty headers.
  377. (goto-char (point-min))
  378. (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
  379. (let ((beg (match-beginning 0)))
  380. (goto-char (match-end 0))
  381. (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
  382. (delete-region beg (point)))))
  383. ;; Get rid of leading empty lines.
  384. (goto-char (point-min))
  385. (when (looking-at "\\([ \t]*\n\\)+")
  386. (delete-region (match-beginning 0) (match-end 0)))
  387. ;; Get rid of trailing empty lines
  388. (goto-char (point-max))
  389. (skip-syntax-backward " ")
  390. (when (equal (char-after) ?\n) (forward-char 1))
  391. (delete-region (point) (point-max))
  392. ;; Check for final newline
  393. (if (and (> (point-max) (point-min))
  394. (/= (char-before (point-max)) ?\n)
  395. (or (eq log-edit-require-final-newline t)
  396. (and log-edit-require-final-newline
  397. (y-or-n-p
  398. (format "Buffer %s does not end in newline. Add one? "
  399. (buffer-name))))))
  400. (save-excursion
  401. (goto-char (point-max))
  402. (insert ?\n)))
  403. (let ((comment (buffer-string)))
  404. (when (or (ring-empty-p log-edit-comment-ring)
  405. (not (equal comment (ring-ref log-edit-comment-ring 0))))
  406. (ring-insert log-edit-comment-ring comment)))
  407. (let ((win (get-buffer-window log-edit-files-buf)))
  408. (if (and log-edit-confirm
  409. (not (and (eq log-edit-confirm 'changed)
  410. (equal (log-edit-files) log-edit-initial-files)))
  411. (progn
  412. (log-edit-show-files)
  413. (not (y-or-n-p "Really commit? "))))
  414. (progn (when (not win) (log-edit-hide-buf))
  415. (message "Oh, well! Later maybe?"))
  416. (run-hooks 'log-edit-done-hook)
  417. (log-edit-hide-buf)
  418. (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
  419. (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
  420. (call-interactively log-edit-callback))))
  421. (defun log-edit-files ()
  422. "Return the list of files that are about to be committed."
  423. (ignore-errors (funcall log-edit-listfun)))
  424. (defun log-edit-mode-help ()
  425. "Provide help for the `log-edit-mode-map'."
  426. (interactive)
  427. (if (eq last-command 'log-edit-mode-help)
  428. (describe-function major-mode)
  429. (message "%s"
  430. (substitute-command-keys
  431. "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
  432. (defcustom log-edit-common-indent 0
  433. "Minimum indentation to use in `log-edit-set-common-indentation'."
  434. :group 'log-edit
  435. :type 'integer)
  436. (defun log-edit-set-common-indentation ()
  437. "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
  438. (save-excursion
  439. (let ((common (point-max)))
  440. (rfc822-goto-eoh)
  441. (while (< (point) (point-max))
  442. (if (not (looking-at "^[ \t]*$"))
  443. (setq common (min common (current-indentation))))
  444. (forward-line 1))
  445. (rfc822-goto-eoh)
  446. (indent-rigidly (point) (point-max)
  447. (- log-edit-common-indent common)))))
  448. (defun log-edit-show-diff ()
  449. "Show the diff for the files to be committed."
  450. (interactive)
  451. (if (functionp log-edit-diff-function)
  452. (funcall log-edit-diff-function)
  453. (error "Diff functionality has not been setup")))
  454. (defun log-edit-show-files ()
  455. "Show the list of files to be committed."
  456. (interactive)
  457. (let* ((files (log-edit-files))
  458. (buf (get-buffer-create log-edit-files-buf)))
  459. (with-current-buffer buf
  460. (log-edit-hide-buf buf 'all)
  461. (setq buffer-read-only nil)
  462. (erase-buffer)
  463. (cvs-insert-strings files)
  464. (setq buffer-read-only t)
  465. (goto-char (point-min))
  466. (save-selected-window
  467. (cvs-pop-to-buffer-same-frame buf)
  468. (shrink-window-if-larger-than-buffer)
  469. (selected-window)))))
  470. (defun log-edit-empty-buffer-p ()
  471. "Return non-nil if the buffer is \"empty\"."
  472. (or (= (point-min) (point-max))
  473. (save-excursion
  474. (goto-char (point-min))
  475. (while (and (looking-at "^\\([a-zA-Z]+: \\)?$")
  476. (zerop (forward-line 1))))
  477. (eobp))))
  478. (defun log-edit-insert-cvs-template ()
  479. "Insert the template specified by the CVS administrator, if any.
  480. This simply uses the local CVS/Template file."
  481. (interactive)
  482. (when (or (called-interactively-p 'interactive)
  483. (log-edit-empty-buffer-p))
  484. ;; Should the template take precedence over an empty Summary:,
  485. ;; ie should we first erase the buffer?
  486. (when (file-readable-p "CVS/Template")
  487. (goto-char (point-max))
  488. (insert-file-contents "CVS/Template"))))
  489. (defun log-edit-insert-cvs-rcstemplate ()
  490. "Insert the rcstemplate from the CVS repository.
  491. This contacts the repository to get the rcstemplate file and
  492. can thus take some time."
  493. (interactive)
  494. (when (or (called-interactively-p 'interactive)
  495. (log-edit-empty-buffer-p))
  496. (when (file-readable-p "CVS/Root")
  497. (goto-char (point-max))
  498. ;; Ignore the stderr stuff, even if it's an error.
  499. (call-process "cvs" nil '(t nil) nil
  500. "checkout" "-p" "CVSROOT/rcstemplate"))))
  501. (defun log-edit-insert-filenames ()
  502. "Insert the list of files that are to be committed."
  503. (interactive)
  504. (insert "Affected files: \n"
  505. (mapconcat 'identity (log-edit-files) " \n")))
  506. (defun log-edit-add-to-changelog ()
  507. "Insert this log message into the appropriate ChangeLog file."
  508. (interactive)
  509. ;; Yuck!
  510. (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
  511. (ring-insert log-edit-comment-ring (buffer-string)))
  512. (dolist (f (log-edit-files))
  513. (let ((buffer-file-name (expand-file-name f)))
  514. (save-excursion
  515. (log-edit-comment-to-change-log)))))
  516. (defvar log-edit-changelog-use-first nil)
  517. (defvar log-edit-rewrite-fixes nil
  518. "Rule to rewrite bug numbers into Fixes: headers.
  519. The value should be of the form (REGEXP . REPLACEMENT)
  520. where REGEXP should match the expression referring to a bug number
  521. in the text, and REPLACEMENT is an expression to pass to `replace-match'
  522. to build the Fixes: header.")
  523. (put 'log-edit-rewrite-fixes 'safe-local-variable
  524. (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
  525. (defun log-edit-add-field (field value)
  526. (rfc822-goto-eoh)
  527. (if (save-excursion (re-search-backward (concat "^" field ":\\([ \t]*\\)$")
  528. nil t))
  529. (replace-match (concat " " value) t t nil 1)
  530. (insert field ": " value "\n" (if (looking-at "\n") "" "\n"))))
  531. (defun log-edit-insert-changelog (&optional use-first)
  532. "Insert a log message by looking at the ChangeLog.
  533. The idea is to write your ChangeLog entries first, and then use this
  534. command to commit your changes.
  535. To select default log text, we:
  536. - find the ChangeLog entries for the files to be checked in,
  537. - verify that the top entry in the ChangeLog is on the current date
  538. and by the current user; if not, we don't provide any default text,
  539. - search the ChangeLog entry for paragraphs containing the names of
  540. the files we're checking in, and finally
  541. - use those paragraphs as the log text.
  542. If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
  543. or if the command is repeated a second time in a row, use the first log entry
  544. regardless of user name or time."
  545. (interactive "P")
  546. (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
  547. (when (<= (point) eoh)
  548. (goto-char eoh)
  549. (if (looking-at "\n") (forward-char 1))))
  550. (let ((author
  551. (let ((log-edit-changelog-use-first
  552. (or use-first (eq last-command 'log-edit-insert-changelog))))
  553. (log-edit-insert-changelog-entries (log-edit-files)))))
  554. (log-edit-set-common-indentation)
  555. ;; Add an Author: field if appropriate.
  556. (when author (log-edit-add-field "Author" author))
  557. ;; Add a Fixes: field if applicable.
  558. (when (consp log-edit-rewrite-fixes)
  559. (rfc822-goto-eoh)
  560. (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
  561. (let ((start (match-beginning 0))
  562. (end (match-end 0))
  563. (fixes (match-substitute-replacement
  564. (cdr log-edit-rewrite-fixes))))
  565. (delete-region start end)
  566. (log-edit-add-field "Fixes" fixes))))
  567. (and log-edit-strip-single-file-name
  568. (progn (rfc822-goto-eoh)
  569. (if (looking-at "\n") (forward-char 1))
  570. (looking-at "\\*\\s-+"))
  571. (let ((start (point)))
  572. (forward-line 1)
  573. (when (not (re-search-forward "^\\*\\s-+" nil t))
  574. (goto-char start)
  575. (skip-chars-forward "^():")
  576. (skip-chars-forward ": ")
  577. (delete-region start (point)))))
  578. (goto-char (point-min))))
  579. ;;;;
  580. ;;;; functions for getting commit message from ChangeLog a file...
  581. ;;;; Courtesy Jim Blandy
  582. ;;;;
  583. (defun log-edit-narrow-changelog ()
  584. "Narrow to the top page of the current buffer, a ChangeLog file.
  585. Actually, the narrowed region doesn't include the date line.
  586. A \"page\" in a ChangeLog file is the area between two dates."
  587. (or (eq major-mode 'change-log-mode)
  588. (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
  589. (goto-char (point-min))
  590. ;; Skip date line and subsequent blank lines.
  591. (forward-line 1)
  592. (if (looking-at "[ \t\n]*\n")
  593. (goto-char (match-end 0)))
  594. (let ((start (point)))
  595. (forward-page 1)
  596. (narrow-to-region start (point))
  597. (goto-char (point-min))))
  598. (defun log-edit-changelog-paragraph ()
  599. "Return the bounds of the ChangeLog paragraph containing point.
  600. If we are between paragraphs, return the previous paragraph."
  601. (beginning-of-line)
  602. (if (looking-at "^[ \t]*$")
  603. (skip-chars-backward " \t\n" (point-min)))
  604. (list (progn
  605. (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
  606. (goto-char (match-end 0)))
  607. (point))
  608. (if (re-search-forward "^[ \t\n]*$" nil t)
  609. (match-beginning 0)
  610. (point-max))))
  611. (defun log-edit-changelog-subparagraph ()
  612. "Return the bounds of the ChangeLog subparagraph containing point.
  613. A subparagraph is a block of non-blank lines beginning with an asterisk.
  614. If we are between sub-paragraphs, return the previous subparagraph."
  615. (end-of-line)
  616. (if (search-backward "*" nil t)
  617. (list (progn (beginning-of-line) (point))
  618. (progn
  619. (forward-line 1)
  620. (if (re-search-forward "^[ \t]*[\n*]" nil t)
  621. (match-beginning 0)
  622. (point-max))))
  623. (list (point) (point))))
  624. (defun log-edit-changelog-entry ()
  625. "Return the bounds of the ChangeLog entry containing point.
  626. The variable `log-edit-changelog-full-paragraphs' decides whether an
  627. \"entry\" is a paragraph or a subparagraph; see its documentation string
  628. for more details."
  629. (save-excursion
  630. (if log-edit-changelog-full-paragraphs
  631. (log-edit-changelog-paragraph)
  632. (log-edit-changelog-subparagraph))))
  633. (defvar user-full-name)
  634. (defvar user-mail-address)
  635. (defvar log-edit-author) ;Dynamically scoped.
  636. (defun log-edit-changelog-ours-p ()
  637. "See if ChangeLog entry at point is for the current user, today.
  638. Return non-nil if it is."
  639. ;; Code adapted from add-change-log-entry.
  640. (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
  641. (and (fboundp 'user-full-name) (user-full-name))
  642. (and (boundp 'user-full-name) user-full-name)))
  643. (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
  644. ;;(and (fboundp 'user-mail-address) (user-mail-address))
  645. (and (boundp 'user-mail-address) user-mail-address)))
  646. (time (or (and (boundp 'add-log-time-format)
  647. (functionp add-log-time-format)
  648. (funcall add-log-time-format))
  649. (format-time-string "%Y-%m-%d"))))
  650. (if (null log-edit-changelog-use-first)
  651. (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))
  652. ;; Check the author, to potentially add it as a "Author: " header.
  653. (when (looking-at "[^ \t]")
  654. (when (and (boundp 'log-edit-author)
  655. (not (looking-at (format ".+ .+ <%s>"
  656. (regexp-quote mail))))
  657. (looking-at ".+ \\(.+ <.+>\\)"))
  658. (let ((author (replace-regexp-in-string " " " "
  659. (match-string 1))))
  660. (unless (and log-edit-author
  661. (string-match (regexp-quote author) log-edit-author))
  662. (setq log-edit-author
  663. (if log-edit-author
  664. (concat log-edit-author ", " author)
  665. author)))))
  666. t))))
  667. (defun log-edit-changelog-entries (file)
  668. "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
  669. The return value looks like this:
  670. (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
  671. where LOGBUFFER is the name of the ChangeLog buffer, and each
  672. \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
  673. (let ((changelog-file-name
  674. (let ((default-directory
  675. (file-name-directory (expand-file-name file)))
  676. (visiting-buffer (find-buffer-visiting file)))
  677. ;; If there is a buffer visiting FILE, and it has a local
  678. ;; value for `change-log-default-name', use that.
  679. (if (and visiting-buffer
  680. (local-variable-p 'change-log-default-name
  681. visiting-buffer))
  682. (with-current-buffer visiting-buffer
  683. change-log-default-name)
  684. ;; `find-change-log' uses `change-log-default-name' if set
  685. ;; and sets it before exiting, so we need to work around
  686. ;; that memoizing which is undesired here
  687. (setq change-log-default-name nil)
  688. (find-change-log)))))
  689. (with-current-buffer (find-file-noselect changelog-file-name)
  690. (unless (eq major-mode 'change-log-mode) (change-log-mode))
  691. (goto-char (point-min))
  692. (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
  693. (if (not (log-edit-changelog-ours-p))
  694. (list (current-buffer))
  695. (save-restriction
  696. (log-edit-narrow-changelog)
  697. (goto-char (point-min))
  698. ;; Search for the name of FILE relative to the ChangeLog. If that
  699. ;; doesn't occur anywhere, they're not using full relative
  700. ;; filenames in the ChangeLog, so just look for FILE; we'll accept
  701. ;; some false positives.
  702. (let ((pattern (file-relative-name
  703. file (file-name-directory changelog-file-name))))
  704. (if (or (string= pattern "")
  705. (not (save-excursion
  706. (search-forward pattern nil t))))
  707. (setq pattern (file-name-nondirectory file)))
  708. (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
  709. (regexp-quote pattern)
  710. "\\($\\|[^[:alnum:]]\\)"))
  711. (let (texts
  712. (pos (point)))
  713. (while (and (not (eobp)) (re-search-forward pattern nil t))
  714. (let ((entry (log-edit-changelog-entry)))
  715. (if (< (elt entry 1) (max (1+ pos) (point)))
  716. ;; This is not relevant, actually.
  717. nil
  718. (push entry texts))
  719. ;; Make sure we make progress.
  720. (setq pos (max (1+ pos) (elt entry 1)))
  721. (goto-char pos)))
  722. (cons (current-buffer) texts))))))))
  723. (defun log-edit-changelog-insert-entries (buffer beg end &rest files)
  724. "Insert the text from BUFFER between BEG and END.
  725. Rename relative filenames in the ChangeLog entry as FILES."
  726. (let ((opoint (point))
  727. (log-name (buffer-file-name buffer))
  728. (case-fold-search nil)
  729. bound)
  730. (insert-buffer-substring buffer beg end)
  731. (setq bound (point-marker))
  732. (when log-name
  733. (dolist (f files)
  734. (save-excursion
  735. (goto-char opoint)
  736. (when (re-search-forward
  737. (concat "\\(^\\|[ \t]\\)\\("
  738. (file-relative-name f (file-name-directory log-name))
  739. "\\)[, :\n]")
  740. bound t)
  741. (replace-match f t t nil 2)))))
  742. ;; Eliminate tabs at the beginning of the line.
  743. (save-excursion
  744. (goto-char opoint)
  745. (while (re-search-forward "^\\(\t+\\)" bound t)
  746. (replace-match "")))))
  747. (defun log-edit-insert-changelog-entries (files)
  748. "Given a list of files FILES, insert the ChangeLog entries for them."
  749. (let ((log-entries nil)
  750. (log-edit-author nil))
  751. ;; Note that any ChangeLog entry can apply to more than one file.
  752. ;; Here we construct a log-entries list with elements of the form
  753. ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
  754. (dolist (file files)
  755. (let* ((entries (log-edit-changelog-entries file))
  756. (buf (car entries))
  757. key entry)
  758. (dolist (region (cdr entries))
  759. (setq key (cons buf region))
  760. (if (setq entry (assoc key log-entries))
  761. (setcdr entry (append (cdr entry) (list file)))
  762. (push (list key file) log-entries)))))
  763. ;; Now map over log-entries, and extract the strings.
  764. (dolist (log-entry (nreverse log-entries))
  765. (apply 'log-edit-changelog-insert-entries
  766. (append (car log-entry) (cdr log-entry)))
  767. (insert "\n"))
  768. log-edit-author))
  769. (defun log-edit-extract-headers (headers comment)
  770. "Extract headers from COMMENT to form command line arguments.
  771. HEADERS should be an alist with elements of the form (HEADER . CMDARG)
  772. associating header names to the corresponding cmdline option name and the
  773. result is then a list of the form (MSG CMDARG1 HDRTEXT1 CMDARG2 HDRTEXT2...).
  774. where MSG is the remaining text from STRING.
  775. If \"Summary\" is not in HEADERS, then the \"Summary\" header is extracted
  776. anyway and put back as the first line of MSG."
  777. (with-temp-buffer
  778. (insert comment)
  779. (rfc822-goto-eoh)
  780. (narrow-to-region (point-min) (point))
  781. (let ((case-fold-search t)
  782. (summary ())
  783. (res ()))
  784. (dolist (header (if (assoc "Summary" headers) headers
  785. (cons '("Summary" . t) headers)))
  786. (goto-char (point-min))
  787. (while (re-search-forward (concat "^" (car header)
  788. ":" log-edit-header-contents-regexp)
  789. nil t)
  790. (if (eq t (cdr header))
  791. (setq summary (match-string 1))
  792. (push (match-string 1) res)
  793. (push (or (cdr header) (car header)) res))
  794. (replace-match "" t t)))
  795. ;; Remove header separator if the header is empty.
  796. (widen)
  797. (goto-char (point-min))
  798. (when (looking-at "\\([ \t]*\n\\)+")
  799. (delete-region (match-beginning 0) (match-end 0)))
  800. (if summary (insert summary "\n"))
  801. (cons (buffer-string) res))))
  802. (provide 'log-edit)
  803. ;;; log-edit.el ends here