log-edit.el 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1999-2015 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. (require 'add-log) ; for all the ChangeLog goodies
  22. (require 'pcvs-util)
  23. (require 'ring)
  24. (require 'message)
  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. ("\C-c\C-k" . log-edit-kill-buffer)
  43. ("\C-a" . log-edit-beginning-of-line)
  44. ("\M-n" . log-edit-next-comment)
  45. ("\M-p" . log-edit-previous-comment)
  46. ("\M-r" . log-edit-comment-search-backward)
  47. ("\M-s" . log-edit-comment-search-forward)
  48. ("\C-c?" . log-edit-mode-help))
  49. "Keymap for the `log-edit-mode' (to edit version control log messages)."
  50. :group 'log-edit)
  51. ;; Compatibility with old names. Should we bother ?
  52. (defvar vc-log-mode-map log-edit-mode-map)
  53. (defvar vc-log-entry-mode vc-log-mode-map)
  54. (easy-menu-define log-edit-menu log-edit-mode-map
  55. "Menu used for `log-edit-mode'."
  56. '("Log-Edit"
  57. ["Done" log-edit-done
  58. :help "Exit log-edit and proceed with the actual action."]
  59. "--"
  60. ["Insert ChangeLog" log-edit-insert-changelog
  61. :help "Insert a log message by looking at the ChangeLog"]
  62. ["Add to ChangeLog" log-edit-add-to-changelog
  63. :help "Insert this log message into the appropriate ChangeLog file"]
  64. "--"
  65. ["Show diff" log-edit-show-diff
  66. :help "Show the diff for the files to be committed."]
  67. ["List files" log-edit-show-files
  68. :help "Show the list of relevant files."]
  69. "--"
  70. ["Previous comment" log-edit-previous-comment
  71. :help "Cycle backwards through comment history"]
  72. ["Next comment" log-edit-next-comment
  73. :help "Cycle forwards through comment history."]
  74. ["Search comment forward" log-edit-comment-search-forward
  75. :help "Search forwards through comment history for a substring match of str"]
  76. ["Search comment backward" log-edit-comment-search-backward
  77. :help "Search backwards through comment history for substring match of str"]))
  78. (defcustom log-edit-confirm 'changed
  79. "If non-nil, `log-edit-done' will request confirmation.
  80. If 'changed, only request confirmation if the list of files has
  81. changed since the beginning of the log-edit session."
  82. :group 'log-edit
  83. :type '(choice (const changed) (const t) (const nil)))
  84. (defcustom log-edit-keep-buffer nil
  85. "If non-nil, don't hide the buffer after `log-edit-done'."
  86. :group 'log-edit
  87. :type 'boolean)
  88. (defcustom log-edit-require-final-newline t
  89. "Enforce a newline at the end of commit log messages.
  90. Enforce it silently if t, query if non-nil and don't do anything if nil."
  91. :group 'log-edit
  92. :type '(choice (const ask) (const t) (const nil)))
  93. (defcustom log-edit-setup-invert nil
  94. "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
  95. If SETUP is 'force, this variable has no effect."
  96. :group 'log-edit
  97. :type 'boolean)
  98. (defcustom log-edit-setup-add-author nil
  99. "Non-nil means `log-edit' may add the `Author:' header.
  100. This applies when its SETUP argument is non-nil."
  101. :version "24.4"
  102. :group 'log-edit
  103. :type 'boolean
  104. :safe 'booleanp)
  105. (defcustom log-edit-hook '(log-edit-insert-message-template
  106. log-edit-insert-cvs-template
  107. log-edit-insert-changelog
  108. log-edit-show-files)
  109. "Hook run at the end of `log-edit'."
  110. ;; Added log-edit-insert-message-template, moved log-edit-show-files.
  111. :version "24.4"
  112. :group 'log-edit
  113. :type '(hook :options (log-edit-insert-message-template
  114. log-edit-insert-cvs-rcstemplate
  115. log-edit-insert-cvs-template
  116. log-edit-insert-changelog
  117. log-edit-insert-filenames
  118. log-edit-insert-filenames-without-changelog
  119. log-edit-show-files)))
  120. (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
  121. "Hook run when entering `log-edit-mode'."
  122. :group 'log-edit
  123. :type 'hook)
  124. (defcustom log-edit-done-hook nil
  125. "Hook run before doing the actual commit.
  126. This hook can be used to cleanup the message, enforce various
  127. conventions, or to allow recording the message in some other database,
  128. such as a bug-tracking system. The list of files about to be committed
  129. can be obtained from `log-edit-files'."
  130. :group 'log-edit
  131. :type '(hook :options (log-edit-set-common-indentation
  132. log-edit-add-to-changelog)))
  133. (defcustom log-edit-strip-single-file-name nil
  134. "If non-nil, remove file name from single-file log entries."
  135. :type 'boolean
  136. :safe 'booleanp
  137. :group 'log-edit
  138. :version "24.1")
  139. (defvar log-edit-changelog-full-paragraphs t
  140. "If non-nil, include full ChangeLog paragraphs in the log.
  141. This may be set in the \"local variables\" section of a ChangeLog, to
  142. indicate the policy for that ChangeLog.
  143. A ChangeLog paragraph is a bunch of log text containing no blank lines;
  144. a paragraph usually describes a set of changes with a single purpose,
  145. but perhaps spanning several functions in several files. Changes in
  146. different paragraphs are unrelated.
  147. You could argue that the log entry for a file should contain the
  148. full ChangeLog paragraph mentioning the change to the file, even though
  149. it may mention other files, because that gives you the full context you
  150. need to understand the change. This is the behavior you get when this
  151. variable is set to t.
  152. On the other hand, you could argue that the log entry for a change
  153. should contain only the text for the changes which occurred in that
  154. file, because the log is per-file. This is the behavior you get
  155. when this variable is set to nil.")
  156. ;;;; Internal global or buffer-local vars
  157. (defconst log-edit-files-buf "*log-edit-files*")
  158. (defvar log-edit-initial-files nil)
  159. (defvar log-edit-callback nil)
  160. (defvar log-edit-diff-function nil)
  161. (defvar log-edit-listfun nil)
  162. (defvar log-edit-parent-buffer nil)
  163. (defvar log-edit-vc-backend nil
  164. "VC fileset corresponding to the current log.")
  165. ;;; Originally taken from VC-Log mode
  166. (defconst log-edit-maximum-comment-ring-size 32
  167. "Maximum number of saved comments in the comment ring.")
  168. (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
  169. (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
  170. (define-obsolete-variable-alias 'vc-comment-ring-index
  171. 'log-edit-comment-ring-index "22.1")
  172. (defvar log-edit-comment-ring-index nil)
  173. (defvar log-edit-last-comment-match "")
  174. (defun log-edit-new-comment-index (stride len)
  175. "Return the comment index STRIDE elements from the current one.
  176. LEN is the length of `log-edit-comment-ring'."
  177. (mod (cond
  178. (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
  179. ;; Initialize the index on the first use of this command
  180. ;; so that the first M-p gets index 0, and the first M-n gets
  181. ;; index -1.
  182. ((> stride 0) (1- stride))
  183. (t stride))
  184. len))
  185. (defun log-edit-previous-comment (arg)
  186. "Cycle backwards through comment history.
  187. With a numeric prefix ARG, go back ARG comments."
  188. (interactive "*p")
  189. (let ((len (ring-length log-edit-comment-ring)))
  190. (if (<= len 0)
  191. (progn (message "Empty comment ring") (ding))
  192. ;; Don't use `erase-buffer' because we don't want to `widen'.
  193. (delete-region (point-min) (point-max))
  194. (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
  195. (message "Comment %d" (1+ log-edit-comment-ring-index))
  196. (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
  197. (defun log-edit-next-comment (arg)
  198. "Cycle forwards through comment history.
  199. With a numeric prefix ARG, go forward ARG comments."
  200. (interactive "*p")
  201. (log-edit-previous-comment (- arg)))
  202. (defun log-edit-comment-search-backward (str &optional stride)
  203. "Search backwards through comment history for substring match of STR.
  204. If the optional argument STRIDE is present, that is a step-width to use
  205. when going through the comment ring."
  206. ;; Why substring rather than regexp ? -sm
  207. (interactive
  208. (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  209. (unless stride (setq stride 1))
  210. (if (string= str "")
  211. (setq str log-edit-last-comment-match)
  212. (setq log-edit-last-comment-match str))
  213. (let* ((str (regexp-quote str))
  214. (len (ring-length log-edit-comment-ring))
  215. (n (log-edit-new-comment-index stride len)))
  216. (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
  217. (not (string-match str (ring-ref log-edit-comment-ring n))))
  218. (setq n (+ n stride)))
  219. (setq log-edit-comment-ring-index n)
  220. (log-edit-previous-comment 0)))
  221. (defun log-edit-comment-search-forward (str)
  222. "Search forwards through comment history for a substring match of STR."
  223. (interactive
  224. (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  225. (log-edit-comment-search-backward str -1))
  226. (defun log-edit-comment-to-change-log (&optional whoami file-name)
  227. "Enter last VC comment into the change log for the current file.
  228. WHOAMI (interactive prefix) non-nil means prompt for user name
  229. and site. FILE-NAME is the name of the change log; if nil, use
  230. `change-log-default-name'.
  231. This may be useful as a `vc-checkin-hook' to update change logs
  232. automatically."
  233. (interactive (if current-prefix-arg
  234. (list current-prefix-arg
  235. (prompt-for-change-log-name))))
  236. (let (;; Extract the comment first so we get any error before doing anything.
  237. (comment (ring-ref log-edit-comment-ring 0))
  238. ;; Don't let add-change-log-entry insert a defun name.
  239. (add-log-current-defun-function 'ignore)
  240. end)
  241. ;; Call add-log to do half the work.
  242. (add-change-log-entry whoami file-name t t)
  243. ;; Insert the VC comment, leaving point before it.
  244. (setq end (save-excursion (insert comment) (point-marker)))
  245. (if (looking-at "\\s *\\s(")
  246. ;; It starts with an open-paren, as in "(foo): Frobbed."
  247. ;; So remove the ": " add-log inserted.
  248. (delete-char -2))
  249. ;; Canonicalize the white space between the file name and comment.
  250. (just-one-space)
  251. ;; Indent rest of the text the same way add-log indented the first line.
  252. (let ((indentation (current-indentation)))
  253. (save-excursion
  254. (while (< (point) end)
  255. (forward-line 1)
  256. (indent-to indentation))
  257. (setq end (point))))
  258. ;; Fill the inserted text, preserving open-parens at bol.
  259. (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
  260. (beginning-of-line)
  261. (fill-region (point) end))
  262. ;; Canonicalize the white space at the end of the entry so it is
  263. ;; separated from the next entry by a single blank line.
  264. (skip-syntax-forward " " end)
  265. (delete-char (- (skip-syntax-backward " ")))
  266. (or (eobp) (looking-at "\n\n")
  267. (insert "\n"))))
  268. ;; Compatibility with old names.
  269. (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
  270. (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
  271. (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
  272. (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
  273. (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
  274. ;;;
  275. ;;; Actual code
  276. ;;;
  277. (defface log-edit-summary '((t :inherit font-lock-function-name-face))
  278. "Face for the summary in `log-edit-mode' buffers.")
  279. (defface log-edit-header '((t :inherit font-lock-keyword-face))
  280. "Face for the headers in `log-edit-mode' buffers.")
  281. (defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
  282. "Face for unknown headers in `log-edit-mode' buffers.")
  283. (defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
  284. ("Fixes") ("Author"))
  285. "AList of known headers and the face to use to highlight them.")
  286. (defconst log-edit-header-contents-regexp
  287. "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?"
  288. "Regular expression matching a header field.
  289. The first subexpression is the actual text of the field.")
  290. (defun log-edit-match-to-eoh (_limit)
  291. ;; FIXME: copied from message-match-to-eoh.
  292. (let ((start (point)))
  293. (rfc822-goto-eoh)
  294. ;; Typical situation: some temporary change causes the header to be
  295. ;; incorrect, so EOH comes earlier than intended: the last lines of the
  296. ;; intended headers are now not considered part of the header any more,
  297. ;; so they don't have the multiline property set. When the change is
  298. ;; completed and the header has its correct shape again, the lack of the
  299. ;; multiline property means we won't rehighlight the last lines of
  300. ;; the header.
  301. (if (< (point) start)
  302. nil ;No header within start..limit.
  303. ;; Here we disregard LIMIT so that we may extend the area again.
  304. (set-match-data (list start (point)))
  305. (point))))
  306. (defun log-edit-goto-eoh () ;FIXME: Almost rfc822-goto-eoh!
  307. (goto-char (point-min))
  308. (when (re-search-forward
  309. "^\\([^[:alpha:]]\\|[[:alnum:]-]+[^[:alnum:]-:]\\)" nil 'move)
  310. (goto-char (match-beginning 0))))
  311. (defun log-edit--match-first-line (limit)
  312. (let ((start (point)))
  313. (log-edit-goto-eoh)
  314. (skip-chars-forward "\n")
  315. (and (< start (line-end-position))
  316. (< (point) limit)
  317. (save-excursion
  318. (not (re-search-backward "^Summary:[ \t]*[^ \t\n]" nil t)))
  319. (looking-at ".+")
  320. (progn
  321. (goto-char (match-end 0))
  322. (put-text-property (point-min) (point)
  323. 'jit-lock-defer-multiline t)
  324. (point)))))
  325. (defvar log-edit-font-lock-keywords
  326. ;; Copied/inspired by message-font-lock-keywords.
  327. `((log-edit-match-to-eoh
  328. (,(concat "^\\(\\([[:alpha:]-]+\\):\\)" log-edit-header-contents-regexp)
  329. (progn (goto-char (match-beginning 0)) (match-end 0)) nil
  330. (1 (if (assoc-string (match-string 2) log-edit-headers-alist t)
  331. 'log-edit-header
  332. 'log-edit-unknown-header)
  333. nil lax)
  334. ;; From `log-edit-header-contents-regexp':
  335. (3 (or (cdr (assoc-string (match-string 2) log-edit-headers-alist t))
  336. 'log-edit-header)
  337. nil lax))
  338. ("^\n"
  339. (progn (goto-char (match-end 0)) (1+ (match-end 0))) nil
  340. (0 '(:height 0.1 :inverse-video t))))
  341. (log-edit--match-first-line (0 'log-edit-summary))))
  342. (defvar log-edit-font-lock-gnu-style nil
  343. "If non-nil, highlight common failures to follow the GNU coding standards.")
  344. (put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)
  345. (defconst log-edit-font-lock-gnu-keywords
  346. ;; Use
  347. ;; * foo.el (bla, bli)
  348. ;; (blo, blu): Toto.
  349. ;; Rather than
  350. ;; * foo.el (bla, bli,
  351. ;; blo, blu): Toto.
  352. '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
  353. (1 '(face font-lock-warning-face
  354. help-echo "Continue function lists with \")\\n(\".") t))
  355. ;; Don't leave a lone word on a single line.
  356. ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
  357. ;; Don't cut a sentence right after the first word (better to move
  358. ;; the sentence on the next line, then).
  359. ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
  360. ;; Change Log entries should use present tense.
  361. ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
  362. (1 '(face font-lock-warning-face help-echo "Use present tense.") t))
  363. ;; Change log entries start with a capital letter.
  364. ("): [a-z]" (0 '(face font-lock-warning-face help-echo "Capitalize.") t))
  365. ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
  366. (1 '(face font-lock-warning-face
  367. help-echo "Use two spaces to end a sentence") t))
  368. ("^("
  369. (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
  370. (put-text-property beg (match-end 0) 'font-lock-multiline t)
  371. (if (eq (char-syntax (char-after beg)) ?w)
  372. '(face font-lock-warning-face
  373. help-echo "Punctuate previous line.")))
  374. t))
  375. ))
  376. (defun log-edit-font-lock-keywords ()
  377. (if log-edit-font-lock-gnu-style
  378. (append log-edit-font-lock-keywords
  379. log-edit-font-lock-gnu-keywords)
  380. log-edit-font-lock-keywords))
  381. ;;;###autoload
  382. (defun log-edit (callback &optional setup params buffer mode &rest _ignore)
  383. "Setup a buffer to enter a log message.
  384. The buffer is put in mode MODE or `log-edit-mode' if MODE is nil.
  385. \\<log-edit-mode-map>
  386. If SETUP is non-nil, erase the buffer and run `log-edit-hook'.
  387. Set mark and point around the entire contents of the buffer, so
  388. that it is easy to kill the contents of the buffer with
  389. \\[kill-region]. Once the user is done editing the message,
  390. invoking the command \\[log-edit-done] (`log-edit-done') will
  391. call CALLBACK to do the actual commit.
  392. PARAMS if non-nil is an alist of variables and buffer-local
  393. values to give them in the Log Edit buffer. Possible keys and
  394. associated values:
  395. `log-edit-listfun' -- function taking no arguments that returns the list of
  396. files that are concerned by the current operation (using relative names);
  397. `log-edit-diff-function' -- function taking no arguments that
  398. displays a diff of the files concerned by the current operation.
  399. `vc-log-fileset' -- the VC fileset to be committed (if any).
  400. If BUFFER is non-nil `log-edit' will jump to that buffer, use it
  401. to edit the log message and go back to the current buffer when
  402. done. Otherwise, it uses the current buffer."
  403. (let ((parent (current-buffer)))
  404. (if buffer (pop-to-buffer buffer))
  405. (when (and log-edit-setup-invert (not (eq setup 'force)))
  406. (setq setup (not setup)))
  407. (if mode
  408. (funcall mode)
  409. (log-edit-mode))
  410. (set (make-local-variable 'log-edit-callback) callback)
  411. (if (listp params)
  412. (dolist (crt params)
  413. (set (make-local-variable (car crt)) (cdr crt)))
  414. ;; For backward compatibility with log-edit up to version 22.2
  415. ;; accept non-list PARAMS to mean `log-edit-list'.
  416. (set (make-local-variable 'log-edit-listfun) params))
  417. (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
  418. (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
  419. (when setup
  420. (erase-buffer)
  421. (run-hooks 'log-edit-hook))
  422. (push-mark (point-max))
  423. (message "%s" (substitute-command-keys
  424. "Press \\[log-edit-done] when you are done editing."))))
  425. (define-derived-mode log-edit-mode text-mode "Log-Edit"
  426. "Major mode for editing version-control log messages.
  427. When done editing the log entry, just type \\[log-edit-done] which
  428. will trigger the actual commit of the file(s).
  429. Several other handy support commands are provided of course and
  430. the package from which this is used might also provide additional
  431. commands (under C-x v for VC, for example).
  432. \\{log-edit-mode-map}"
  433. (set (make-local-variable 'font-lock-defaults)
  434. '(log-edit-font-lock-keywords t))
  435. (setq-local jit-lock-contextually t) ;For the "first line is summary".
  436. (make-local-variable 'log-edit-comment-ring-index)
  437. (add-hook 'kill-buffer-hook 'log-edit-remember-comment nil t)
  438. (hack-dir-local-variables-non-file-buffer))
  439. (defun log-edit-hide-buf (&optional buf where)
  440. (when (setq buf (get-buffer (or buf log-edit-files-buf)))
  441. ;; FIXME: Should use something like `quit-windows-on' here, but
  442. ;; that function never deletes this buffer's window because it
  443. ;; was created using `cvs-pop-to-buffer-same-frame'.
  444. (save-selected-window
  445. (let ((win (get-buffer-window buf where)))
  446. (if win (ignore-errors (delete-window win))))
  447. (bury-buffer buf))))
  448. (defun log-edit-remember-comment (&optional comment)
  449. (unless comment (setq comment (buffer-string)))
  450. (when (or (ring-empty-p log-edit-comment-ring)
  451. (not (equal comment (ring-ref log-edit-comment-ring 0))))
  452. (ring-insert log-edit-comment-ring comment)))
  453. (defun log-edit-done ()
  454. "Finish editing the log message and commit the files.
  455. If you want to abort the commit, simply delete the buffer."
  456. (interactive)
  457. ;; Clean up empty headers.
  458. (goto-char (point-min))
  459. (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
  460. (let ((beg (match-beginning 0)))
  461. (goto-char (match-end 0))
  462. (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
  463. (delete-region beg (point)))))
  464. ;; Get rid of leading empty lines.
  465. (goto-char (point-min))
  466. (when (looking-at "\\([ \t]*\n\\)+")
  467. (delete-region (match-beginning 0) (match-end 0)))
  468. ;; Get rid of trailing empty lines
  469. (goto-char (point-max))
  470. (skip-syntax-backward " ")
  471. (when (equal (char-after) ?\n) (forward-char 1))
  472. (delete-region (point) (point-max))
  473. ;; Check for final newline
  474. (if (and (> (point-max) (point-min))
  475. (/= (char-before (point-max)) ?\n)
  476. (or (eq log-edit-require-final-newline t)
  477. (and log-edit-require-final-newline
  478. (y-or-n-p
  479. (format "Buffer %s does not end in newline. Add one? "
  480. (buffer-name))))))
  481. (save-excursion
  482. (goto-char (point-max))
  483. (insert ?\n)))
  484. (log-edit-remember-comment)
  485. (let ((win (get-buffer-window log-edit-files-buf)))
  486. (if (and log-edit-confirm
  487. (not (and (eq log-edit-confirm 'changed)
  488. (equal (log-edit-files) log-edit-initial-files)))
  489. (progn
  490. (log-edit-show-files)
  491. (not (y-or-n-p "Really commit? "))))
  492. (progn (when (not win) (log-edit-hide-buf))
  493. (message "Oh, well! Later maybe?"))
  494. (run-hooks 'log-edit-done-hook)
  495. (log-edit-hide-buf)
  496. (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
  497. (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
  498. (call-interactively log-edit-callback))))
  499. (defun log-edit-kill-buffer ()
  500. "Kill the current buffer.
  501. Also saves its contents in the comment history and hides
  502. `log-edit-files-buf'."
  503. (interactive)
  504. (log-edit-hide-buf)
  505. (let ((buf (current-buffer)))
  506. (quit-windows-on buf)
  507. (kill-buffer buf)))
  508. (defun log-edit-files ()
  509. "Return the list of files that are about to be committed."
  510. (ignore-errors (funcall log-edit-listfun)))
  511. (defun log-edit-mode-help ()
  512. "Provide help for the `log-edit-mode-map'."
  513. (interactive)
  514. (if (eq last-command 'log-edit-mode-help)
  515. (describe-function major-mode)
  516. (message "%s"
  517. (substitute-command-keys
  518. "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
  519. (defcustom log-edit-common-indent 0
  520. "Minimum indentation to use in `log-edit-set-common-indentation'."
  521. :group 'log-edit
  522. :type 'integer)
  523. (defun log-edit-set-common-indentation ()
  524. "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
  525. (save-excursion
  526. (let ((common (point-max)))
  527. (rfc822-goto-eoh)
  528. (while (< (point) (point-max))
  529. (if (not (looking-at "^[ \t]*$"))
  530. (setq common (min common (current-indentation))))
  531. (forward-line 1))
  532. (rfc822-goto-eoh)
  533. (indent-rigidly (point) (point-max)
  534. (- log-edit-common-indent common)))))
  535. (defun log-edit-show-diff ()
  536. "Show the diff for the files to be committed."
  537. (interactive)
  538. (if (functionp log-edit-diff-function)
  539. (funcall log-edit-diff-function)
  540. (error "Diff functionality has not been setup")))
  541. (defun log-edit-show-files ()
  542. "Show the list of files to be committed."
  543. (interactive)
  544. (let* ((files (log-edit-files))
  545. (buf (get-buffer-create log-edit-files-buf)))
  546. (with-current-buffer buf
  547. (log-edit-hide-buf buf 'all)
  548. (setq buffer-read-only nil)
  549. (erase-buffer)
  550. (cvs-insert-strings files)
  551. (setq buffer-read-only t)
  552. (goto-char (point-min))
  553. (save-selected-window
  554. (cvs-pop-to-buffer-same-frame buf)
  555. (shrink-window-if-larger-than-buffer)
  556. (set-window-dedicated-p (selected-window) t)
  557. (selected-window)))))
  558. (defun log-edit-beginning-of-line (&optional n)
  559. "Move point to beginning of header value or to beginning of line.
  560. It works the same as `message-beginning-of-line', but it uses a
  561. different header separator appropriate for `log-edit-mode'."
  562. (interactive "p")
  563. (let ((mail-header-separator ""))
  564. (message-beginning-of-line n)))
  565. (defun log-edit-empty-buffer-p ()
  566. "Return non-nil if the buffer is \"empty\"."
  567. (or (= (point-min) (point-max))
  568. (save-excursion
  569. (goto-char (point-min))
  570. (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
  571. (zerop (forward-line 1))))
  572. (eobp))))
  573. (defun log-edit-insert-message-template ()
  574. "Insert the default template with Summary and Author."
  575. (interactive)
  576. (when (or (called-interactively-p 'interactive)
  577. (log-edit-empty-buffer-p))
  578. (insert "Summary: ")
  579. (when log-edit-setup-add-author
  580. (insert "\nAuthor: "))
  581. (insert "\n\n")
  582. (message-position-point)))
  583. (defun log-edit-insert-cvs-template ()
  584. "Insert the template specified by the CVS administrator, if any.
  585. This simply uses the local CVS/Template file."
  586. (interactive)
  587. (when (or (called-interactively-p 'interactive)
  588. (log-edit-empty-buffer-p))
  589. ;; Should the template take precedence over an empty Summary:,
  590. ;; ie should we first erase the buffer?
  591. (when (file-readable-p "CVS/Template")
  592. (goto-char (point-max))
  593. (insert-file-contents "CVS/Template"))))
  594. (defun log-edit-insert-cvs-rcstemplate ()
  595. "Insert the rcstemplate from the CVS repository.
  596. This contacts the repository to get the rcstemplate file and
  597. can thus take some time."
  598. (interactive)
  599. (when (or (called-interactively-p 'interactive)
  600. (log-edit-empty-buffer-p))
  601. (when (file-readable-p "CVS/Root")
  602. (goto-char (point-max))
  603. ;; Ignore the stderr stuff, even if it's an error.
  604. (call-process "cvs" nil '(t nil) nil
  605. "checkout" "-p" "CVSROOT/rcstemplate"))))
  606. (defun log-edit-insert-filenames ()
  607. "Insert the list of files that are to be committed."
  608. (interactive)
  609. (insert "Affected files: \n"
  610. (mapconcat 'identity (log-edit-files) " \n")))
  611. (defun log-edit-insert-filenames-without-changelog ()
  612. "Insert the list of files that have no ChangeLog message."
  613. (interactive)
  614. (let ((files
  615. (delq nil
  616. (mapcar
  617. (lambda (file)
  618. (unless (or (cdr-safe (log-edit-changelog-entries file))
  619. (equal (file-name-nondirectory file) "ChangeLog"))
  620. file))
  621. (log-edit-files)))))
  622. (when files
  623. (goto-char (point-max))
  624. (insert (mapconcat 'identity files ", ") ": "))))
  625. (defun log-edit-add-to-changelog ()
  626. "Insert this log message into the appropriate ChangeLog file."
  627. (interactive)
  628. (log-edit-remember-comment)
  629. (dolist (f (log-edit-files))
  630. (let ((buffer-file-name (expand-file-name f)))
  631. (save-excursion
  632. (log-edit-comment-to-change-log)))))
  633. (defvar log-edit-changelog-use-first nil)
  634. (defvar log-edit-rewrite-tiny-change t
  635. "Non-nil means rewrite (tiny change).")
  636. (defvar log-edit-rewrite-fixes nil
  637. "Rule to rewrite bug numbers into Fixes: headers.
  638. The value should be of the form (REGEXP . REPLACEMENT)
  639. where REGEXP should match the expression referring to a bug number
  640. in the text, and REPLACEMENT is an expression to pass to `replace-match'
  641. to build the Fixes: header.")
  642. (put 'log-edit-rewrite-fixes 'safe-local-variable
  643. (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
  644. (defun log-edit-add-field (field value)
  645. (rfc822-goto-eoh)
  646. (if (save-excursion (re-search-backward (concat "^" field ":\\([ \t]*\\)$")
  647. nil t))
  648. (replace-match (concat " " value) t t nil 1)
  649. (insert field ": " value "\n" (if (looking-at "\n") "" "\n"))))
  650. (defun log-edit-insert-changelog (&optional use-first)
  651. "Insert a log message by looking at the ChangeLog.
  652. The idea is to write your ChangeLog entries first, and then use this
  653. command to commit your changes.
  654. To select default log text, we:
  655. - find the ChangeLog entries for the files to be checked in,
  656. - verify that the top entry in the ChangeLog is on the current date
  657. and by the current user; if not, we don't provide any default text,
  658. - search the ChangeLog entry for paragraphs containing the names of
  659. the files we're checking in, and finally
  660. - use those paragraphs as the log text.
  661. If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
  662. or if the command is repeated a second time in a row, use the first log entry
  663. regardless of user name or time."
  664. (interactive "P")
  665. (save-excursion
  666. (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
  667. (when (<= (point) eoh)
  668. (goto-char eoh)
  669. (if (looking-at "\n") (forward-char 1))))
  670. (let ((author
  671. (let ((log-edit-changelog-use-first
  672. (or use-first (eq last-command 'log-edit-insert-changelog))))
  673. (log-edit-insert-changelog-entries (log-edit-files)))))
  674. (log-edit-set-common-indentation)
  675. ;; Add an Author: field if appropriate.
  676. (when author (log-edit-add-field "Author" (car author)))
  677. ;; Add a Fixes: field if applicable.
  678. (when (consp log-edit-rewrite-fixes)
  679. (rfc822-goto-eoh)
  680. (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
  681. (let ((start (match-beginning 0))
  682. (end (match-end 0))
  683. (fixes (match-substitute-replacement
  684. (cdr log-edit-rewrite-fixes))))
  685. (delete-region start end)
  686. (log-edit-add-field "Fixes" fixes))))
  687. (and log-edit-strip-single-file-name
  688. (progn (rfc822-goto-eoh)
  689. (if (looking-at "\n") (forward-char 1))
  690. (looking-at "\\*\\s-+"))
  691. (let ((start (point)))
  692. (forward-line 1)
  693. (when (not (re-search-forward "^\\*\\s-+" nil t))
  694. (goto-char start)
  695. (skip-chars-forward "^():")
  696. (skip-chars-forward ": ")
  697. (delete-region start (point)))))
  698. ;; FIXME also add "Co-authored-by" when appropriate.
  699. ;; Bzr accepts multiple --author arguments, others (?) don't.
  700. (and log-edit-rewrite-tiny-change
  701. (eq 'tiny (cdr author))
  702. (goto-char (point-max))
  703. (insert "\nCopyright-paperwork-exempt: yes\n")))))
  704. ;;;;
  705. ;;;; functions for getting commit message from ChangeLog a file...
  706. ;;;; Courtesy Jim Blandy
  707. ;;;;
  708. (defun log-edit-narrow-changelog ()
  709. "Narrow to the top page of the current buffer, a ChangeLog file.
  710. Actually, the narrowed region doesn't include the date line.
  711. A \"page\" in a ChangeLog file is the area between two dates."
  712. (or (eq major-mode 'change-log-mode)
  713. (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
  714. (goto-char (point-min))
  715. ;; Skip date line and subsequent blank lines.
  716. (forward-line 1)
  717. (if (looking-at "[ \t\n]*\n")
  718. (goto-char (match-end 0)))
  719. (let ((start (point)))
  720. (forward-page 1)
  721. (narrow-to-region start (point))
  722. (goto-char (point-min))))
  723. (defun log-edit-changelog-paragraph ()
  724. "Return the bounds of the ChangeLog paragraph containing point.
  725. If we are between paragraphs, return the previous paragraph."
  726. (beginning-of-line)
  727. (if (looking-at "^[ \t]*$")
  728. (skip-chars-backward " \t\n" (point-min)))
  729. (list (progn
  730. (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
  731. (goto-char (match-end 0)))
  732. (point))
  733. (if (re-search-forward "^[ \t\n]*$" nil t)
  734. (match-beginning 0)
  735. (point-max))))
  736. (defun log-edit-changelog-subparagraph ()
  737. "Return the bounds of the ChangeLog subparagraph containing point.
  738. A subparagraph is a block of non-blank lines beginning with an asterisk.
  739. If we are between sub-paragraphs, return the previous subparagraph."
  740. (end-of-line)
  741. (if (search-backward "*" nil t)
  742. (list (progn (beginning-of-line) (point))
  743. (progn
  744. (forward-line 1)
  745. (if (re-search-forward "^[ \t]*[\n*]" nil t)
  746. (match-beginning 0)
  747. (point-max))))
  748. (list (point) (point))))
  749. (defun log-edit-changelog-entry ()
  750. "Return the bounds of the ChangeLog entry containing point.
  751. The variable `log-edit-changelog-full-paragraphs' decides whether an
  752. \"entry\" is a paragraph or a subparagraph; see its documentation string
  753. for more details."
  754. (save-excursion
  755. (if log-edit-changelog-full-paragraphs
  756. (log-edit-changelog-paragraph)
  757. (log-edit-changelog-subparagraph))))
  758. (defvar user-full-name)
  759. (defvar user-mail-address)
  760. (defvar log-edit-author) ;Dynamically scoped.
  761. (defun log-edit-changelog-ours-p ()
  762. "See if ChangeLog entry at point is for the current user, today.
  763. Return non-nil if it is."
  764. ;; Code adapted from add-change-log-entry.
  765. (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
  766. (and (fboundp 'user-full-name) (user-full-name))
  767. (and (boundp 'user-full-name) user-full-name)))
  768. (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
  769. ;;(and (fboundp 'user-mail-address) (user-mail-address))
  770. (and (boundp 'user-mail-address) user-mail-address)))
  771. (time (or (and (boundp 'add-log-time-format)
  772. (functionp add-log-time-format)
  773. (funcall add-log-time-format
  774. nil add-log-time-zone-rule))
  775. (format-time-string "%Y-%m-%d"))))
  776. (if (null log-edit-changelog-use-first)
  777. (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))
  778. ;; Check the author, to potentially add it as a "Author: " header.
  779. ;; FIXME This accumulates multiple authors, but only when there
  780. ;; are multiple ChangeLog files. It should also check for
  781. ;; multiple authors in each individual entry.
  782. (when (looking-at "[^ \t]")
  783. (when (and (boundp 'log-edit-author)
  784. (not (looking-at (format ".+ .+ <%s>"
  785. (regexp-quote mail))))
  786. (looking-at ".+ \\(.+ <.+>\\) *\\((tiny change)\\)?"))
  787. (let ((author (replace-regexp-in-string " " " "
  788. (match-string 1))))
  789. (unless (and log-edit-author
  790. (string-match (regexp-quote author)
  791. (car log-edit-author)))
  792. (if (not log-edit-author)
  793. (setq log-edit-author
  794. (cons author (if (match-string 2) 'tiny)))
  795. (setcar log-edit-author
  796. (concat (car log-edit-author) ", " author))
  797. (and (match-string 2) (not (cdr log-edit-author))
  798. (setcdr log-edit-author 'tiny))))))
  799. t))))
  800. (defun log-edit-changelog-entries (file)
  801. "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
  802. The return value looks like this:
  803. (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
  804. where LOGBUFFER is the name of the ChangeLog buffer, and each
  805. \(ENTRYSTART . ENTRYEND) pair is a buffer region."
  806. (let ((changelog-file-name
  807. (let ((default-directory
  808. (file-name-directory (expand-file-name file)))
  809. (visiting-buffer (find-buffer-visiting file)))
  810. ;; If there is a buffer visiting FILE, and it has a local
  811. ;; value for `change-log-default-name', use that.
  812. (if (and visiting-buffer
  813. (local-variable-p 'change-log-default-name
  814. visiting-buffer))
  815. (with-current-buffer visiting-buffer
  816. change-log-default-name)
  817. ;; `find-change-log' uses `change-log-default-name' if set
  818. ;; and sets it before exiting, so we need to work around
  819. ;; that memoizing which is undesired here.
  820. (setq change-log-default-name nil)
  821. (find-change-log)))))
  822. (when (or (find-buffer-visiting changelog-file-name)
  823. (file-exists-p changelog-file-name))
  824. (with-current-buffer (find-file-noselect changelog-file-name)
  825. (unless (eq major-mode 'change-log-mode) (change-log-mode))
  826. (goto-char (point-min))
  827. (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
  828. (if (not (log-edit-changelog-ours-p))
  829. (list (current-buffer))
  830. (save-restriction
  831. (log-edit-narrow-changelog)
  832. (goto-char (point-min))
  833. (let ((pattern (log-edit-changelog--pattern file
  834. changelog-file-name)))
  835. (let (texts
  836. (pos (point)))
  837. (while (and (not (eobp)) (re-search-forward pattern nil t))
  838. (let ((entry (log-edit-changelog-entry)))
  839. (if (< (elt entry 1) (max (1+ pos) (point)))
  840. ;; This is not relevant, actually.
  841. nil
  842. (push entry texts))
  843. ;; Make sure we make progress.
  844. (setq pos (max (1+ pos) (elt entry 1)))
  845. (goto-char pos)))
  846. (cons (current-buffer) texts)))))))))
  847. (defun log-edit-changelog--pattern (file changelog-file-name)
  848. (if (eq (aref file (1- (length file))) ?/)
  849. ;; Match any files inside this directory.
  850. (concat "^\t\\* " (unless (string= file "./") file))
  851. ;; Search for the name of FILE relative to the ChangeLog. If that
  852. ;; doesn't occur anywhere, they're not using full relative
  853. ;; filenames in the ChangeLog, so just look for FILE; we'll accept
  854. ;; some false positives.
  855. (let ((pattern (file-relative-name
  856. file (file-name-directory changelog-file-name))))
  857. ;; FIXME: When can the above return an empty string?
  858. (if (or (string= pattern "")
  859. (not (save-excursion
  860. (search-forward pattern nil t))))
  861. (setq pattern (file-name-nondirectory file)))
  862. (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
  863. (regexp-quote pattern)
  864. "\\($\\|[^[:alnum:]]\\)")))))
  865. (defun log-edit-changelog-insert-entries (buffer beg end &rest files)
  866. "Insert the text from BUFFER between BEG and END.
  867. Rename relative filenames in the ChangeLog entry as FILES."
  868. (let ((opoint (point))
  869. (log-name (buffer-file-name buffer))
  870. (case-fold-search nil)
  871. bound)
  872. (insert-buffer-substring buffer beg end)
  873. (setq bound (point-marker))
  874. (when log-name
  875. (dolist (f files)
  876. ;; FIXME: f can be a directory, a (possibly indirect) parent
  877. ;; of the ChangeLog file.
  878. (save-excursion
  879. (goto-char opoint)
  880. (when (re-search-forward
  881. (concat "\\(^\\|[ \t]\\)\\("
  882. (file-relative-name f (file-name-directory log-name))
  883. "\\)[, :\n]")
  884. bound t)
  885. (replace-match f t t nil 2)))))
  886. ;; Eliminate tabs at the beginning of the line.
  887. (save-excursion
  888. (goto-char opoint)
  889. (while (re-search-forward "^\\(\t+\\)" bound t)
  890. (replace-match "")))))
  891. (defun log-edit-insert-changelog-entries (files)
  892. "Given a list of files FILES, insert the ChangeLog entries for them."
  893. (let ((log-entries nil)
  894. (log-edit-author nil))
  895. ;; Note that any ChangeLog entry can apply to more than one file.
  896. ;; Here we construct a log-entries list with elements of the form
  897. ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
  898. (dolist (file files)
  899. (let* ((entries (log-edit-changelog-entries file))
  900. (buf (car entries))
  901. key entry)
  902. (dolist (region (cdr entries))
  903. (setq key (cons buf region))
  904. (if (setq entry (assoc key log-entries))
  905. (setcdr entry (append (cdr entry) (list file)))
  906. (push (list key file) log-entries)))))
  907. ;; Now map over log-entries, and extract the strings.
  908. (dolist (log-entry (nreverse log-entries))
  909. (apply 'log-edit-changelog-insert-entries
  910. (append (car log-entry) (cdr log-entry)))
  911. (insert "\n"))
  912. ;; No newline after the last entry.
  913. (when log-entries
  914. (delete-char -1))
  915. log-edit-author))
  916. (defun log-edit-toggle-header (header value)
  917. "Toggle a boolean-type header in the current buffer.
  918. See `log-edit-set-header' for details."
  919. (log-edit-set-header header value t))
  920. (defun log-edit-set-header (header value &optional toggle)
  921. "Set the value of HEADER to VALUE in the current buffer.
  922. If TOGGLE is non-nil, and the value of HEADER already is VALUE,
  923. clear it. Make sure there is an empty line after the headers.
  924. Return t if toggled on (or TOGGLE is nil), otherwise nil."
  925. (let ((val t)
  926. (line (concat header ": " value "\n")))
  927. (save-excursion
  928. (save-restriction
  929. (rfc822-goto-eoh)
  930. (narrow-to-region (point-min) (point))
  931. (goto-char (point-min))
  932. (if (re-search-forward (concat "^" header ":"
  933. log-edit-header-contents-regexp)
  934. nil t)
  935. (if (setq val (not (and toggle (string= (match-string 1) value))))
  936. (replace-match line t t)
  937. (replace-match "" t t nil 1))
  938. (insert line)))
  939. (rfc822-goto-eoh)
  940. (delete-horizontal-space)
  941. (unless (looking-at "\n")
  942. (insert "\n")))
  943. val))
  944. (defun log-edit-extract-headers (headers comment)
  945. "Extract headers from COMMENT to form command line arguments.
  946. HEADERS should be an alist with elements (HEADER . CMDARG)
  947. or (HEADER . FUNCTION) associating headers to command line
  948. options and the result is then a list of the form (MSG ARGUMENTS...)
  949. where MSG is the remaining text from COMMENT.
  950. FUNCTION should be a function of one argument that takes the
  951. header value and returns the list of strings to be appended to
  952. ARGUMENTS. CMDARG will be added to ARGUMENTS followed by the
  953. header value. If \"Summary\" is not in HEADERS, then the
  954. \"Summary\" header is extracted anyway and put back as the first
  955. line of MSG."
  956. (with-temp-buffer
  957. (insert comment)
  958. (rfc822-goto-eoh)
  959. (narrow-to-region (point-min) (point))
  960. (let ((case-fold-search t)
  961. (summary ())
  962. (res ()))
  963. (dolist (header (if (assoc "Summary" headers) headers
  964. (cons '("Summary" . t) headers)))
  965. (goto-char (point-min))
  966. (while (re-search-forward (concat "^" (car header)
  967. ":" log-edit-header-contents-regexp)
  968. nil t)
  969. (let ((txt (match-string 1)))
  970. (replace-match "" t t)
  971. (if (eq t (cdr header))
  972. (setq summary txt)
  973. (if (functionp (cdr header))
  974. (setq res (nconc res (funcall (cdr header) txt)))
  975. (push txt res)
  976. (push (or (cdr header) (car header)) res))))))
  977. ;; Remove header separator if the header is empty.
  978. (widen)
  979. (goto-char (point-min))
  980. (when (looking-at "\\([ \t]*\n\\)+")
  981. (delete-region (match-beginning 0) (match-end 0)))
  982. (if summary (insert summary "\n\n"))
  983. (cons (buffer-string) res))))
  984. (provide 'log-edit)
  985. ;;; log-edit.el ends here