add-log.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1988, 1993-1994, 1997-1998, 2000-2015 Free
  3. ;; Software Foundation, Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: vc tools
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This facility is documented in the Emacs Manual.
  19. ;; Todo:
  20. ;; - Find/use/create _MTN/log if there's a _MTN directory.
  21. ;; - Find/use/create ++log.* if there's an {arch} directory.
  22. ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
  23. ;; source file.
  24. ;; - Don't add TAB indents (and username?) if inserting entries in those
  25. ;; special places.
  26. ;;; Code:
  27. (defgroup change-log nil
  28. "Change log maintenance."
  29. :group 'tools
  30. :link '(custom-manual "(emacs)Change Log")
  31. :prefix "change-log-"
  32. :prefix "add-log-")
  33. (defcustom change-log-default-name nil
  34. "Name of a change log file for \\[add-change-log-entry]."
  35. :type '(choice (const :tag "default" nil)
  36. string)
  37. :group 'change-log)
  38. ;;;###autoload
  39. (put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
  40. (defcustom change-log-mode-hook nil
  41. "Normal hook run by `change-log-mode'."
  42. :type 'hook
  43. :group 'change-log)
  44. ;; Many modes set this variable, so avoid warnings.
  45. ;;;###autoload
  46. (defcustom add-log-current-defun-function nil
  47. "If non-nil, function to guess name of surrounding function.
  48. It is called by `add-log-current-defun' with no argument, and
  49. should return the function's name as a string, or nil if point is
  50. outside a function."
  51. :type '(choice (const nil) function)
  52. :group 'change-log)
  53. ;;;###autoload
  54. (defcustom add-log-full-name nil
  55. "Full name of user, for inclusion in ChangeLog daily headers.
  56. This defaults to the value returned by the function `user-full-name'."
  57. :type '(choice (const :tag "Default" nil)
  58. string)
  59. :group 'change-log)
  60. ;;;###autoload
  61. (defcustom add-log-mailing-address nil
  62. "Email addresses of user, for inclusion in ChangeLog headers.
  63. This defaults to the value of `user-mail-address'. In addition to
  64. being a simple string, this value can also be a list. All elements
  65. will be recognized as referring to the same user; when creating a new
  66. ChangeLog entry, one element will be chosen at random."
  67. :type '(choice (const :tag "Default" nil)
  68. (string :tag "String")
  69. (repeat :tag "List of Strings" string))
  70. :group 'change-log)
  71. (defcustom add-log-time-format 'add-log-iso8601-time-string
  72. "Function that defines the time format.
  73. For example, `add-log-iso8601-time-string', which gives the
  74. date in international ISO 8601 format,
  75. and `current-time-string' are two valid values."
  76. :type '(radio (const :tag "International ISO 8601 format"
  77. add-log-iso8601-time-string)
  78. (const :tag "Old format, as returned by `current-time-string'"
  79. current-time-string)
  80. (function :tag "Other"))
  81. :group 'change-log)
  82. (defcustom add-log-keep-changes-together nil
  83. "If non-nil, normally keep day's log entries for one file together.
  84. Log entries for a given file made with \\[add-change-log-entry] or
  85. \\[add-change-log-entry-other-window] will only be added to others \
  86. for that file made
  87. today if this variable is non-nil or that file comes first in today's
  88. entries. Otherwise another entry for that file will be started. An
  89. original log:
  90. * foo (...): ...
  91. * bar (...): change 1
  92. in the latter case, \\[add-change-log-entry-other-window] in a \
  93. buffer visiting `bar', yields:
  94. * bar (...): -!-
  95. * foo (...): ...
  96. * bar (...): change 1
  97. and in the former:
  98. * foo (...): ...
  99. * bar (...): change 1
  100. (...): -!-
  101. The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
  102. this variable."
  103. :version "20.3"
  104. :type 'boolean
  105. :group 'change-log)
  106. (defcustom add-log-always-start-new-record nil
  107. "If non-nil, `add-change-log-entry' will always start a new record."
  108. :version "22.1"
  109. :type 'boolean
  110. :group 'change-log)
  111. (defvar add-log-buffer-file-name-function 'buffer-file-name
  112. "If non-nil, function to call to identify the full filename of a buffer.
  113. This function is called with no argument. The default is to
  114. use `buffer-file-name'.")
  115. (defcustom add-log-file-name-function nil
  116. "If non-nil, function to call to identify the filename for a ChangeLog entry.
  117. This function is called with one argument, the value of variable
  118. `buffer-file-name' in that buffer. If this is nil, the default is to
  119. use the file's name relative to the directory of the change log file."
  120. :type '(choice (const nil) function)
  121. :group 'change-log)
  122. (defcustom change-log-version-info-enabled nil
  123. "If non-nil, enable recording version numbers with the changes."
  124. :version "21.1"
  125. :type 'boolean
  126. :group 'change-log)
  127. (defcustom change-log-version-number-regexp-list
  128. (let ((re "\\([0-9]+\.[0-9.]+\\)"))
  129. (list
  130. ;; (defconst ad-version "2.15"
  131. (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
  132. ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
  133. (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
  134. "List of regexps to search for version number.
  135. The version number must be in group 1.
  136. Note: The search is conducted only within 10%, at the beginning of the file."
  137. :version "21.1"
  138. :type '(repeat regexp)
  139. :group 'change-log)
  140. (defface change-log-date
  141. '((t (:inherit font-lock-string-face)))
  142. "Face used to highlight dates in date lines."
  143. :version "21.1"
  144. :group 'change-log)
  145. (define-obsolete-face-alias 'change-log-date-face 'change-log-date "22.1")
  146. (defface change-log-name
  147. '((t (:inherit font-lock-constant-face)))
  148. "Face for highlighting author names."
  149. :version "21.1"
  150. :group 'change-log)
  151. (define-obsolete-face-alias 'change-log-name-face 'change-log-name "22.1")
  152. (defface change-log-email
  153. '((t (:inherit font-lock-variable-name-face)))
  154. "Face for highlighting author email addresses."
  155. :version "21.1"
  156. :group 'change-log)
  157. (define-obsolete-face-alias 'change-log-email-face 'change-log-email "22.1")
  158. (defface change-log-file
  159. '((t (:inherit font-lock-function-name-face)))
  160. "Face for highlighting file names."
  161. :version "21.1"
  162. :group 'change-log)
  163. (define-obsolete-face-alias 'change-log-file-face 'change-log-file "22.1")
  164. (defface change-log-list
  165. '((t (:inherit font-lock-keyword-face)))
  166. "Face for highlighting parenthesized lists of functions or variables."
  167. :version "21.1"
  168. :group 'change-log)
  169. (define-obsolete-face-alias 'change-log-list-face 'change-log-list "22.1")
  170. (defface change-log-conditionals
  171. '((t (:inherit font-lock-variable-name-face)))
  172. "Face for highlighting conditionals of the form `[...]'."
  173. :version "21.1"
  174. :group 'change-log)
  175. (define-obsolete-face-alias 'change-log-conditionals-face
  176. 'change-log-conditionals "22.1")
  177. (defface change-log-function
  178. '((t (:inherit font-lock-variable-name-face)))
  179. "Face for highlighting items of the form `<....>'."
  180. :version "21.1"
  181. :group 'change-log)
  182. (define-obsolete-face-alias 'change-log-function-face
  183. 'change-log-function "22.1")
  184. (defface change-log-acknowledgment
  185. '((t (:inherit font-lock-comment-face)))
  186. "Face for highlighting acknowledgments."
  187. :version "21.1"
  188. :group 'change-log)
  189. (define-obsolete-face-alias 'change-log-acknowledgement
  190. 'change-log-acknowledgment "24.3")
  191. (define-obsolete-face-alias 'change-log-acknowledgement-face
  192. 'change-log-acknowledgment "22.1")
  193. (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
  194. (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
  195. (defvar change-log-font-lock-keywords
  196. `(;;
  197. ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
  198. ;; Fixme: this regexp is just an approximate one and may match
  199. ;; wrongly with a non-date line existing as a random note. In
  200. ;; addition, using any kind of fixed setting like this doesn't
  201. ;; work if a user customizes add-log-time-format.
  202. ("^[0-9-]+ +\\|^ \\{11,\\}\\|^\t \\{3,\\}\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
  203. (0 'change-log-date-face)
  204. ;; Name and e-mail; some people put e-mail in parens, not angles.
  205. ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
  206. (1 'change-log-name)
  207. (2 'change-log-email)))
  208. ;;
  209. ;; File names.
  210. (,change-log-file-names-re
  211. (2 'change-log-file)
  212. ;; Possibly further names in a list:
  213. ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
  214. ;; Possibly a parenthesized list of names:
  215. ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  216. nil nil (1 'change-log-list))
  217. ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  218. nil nil (1 'change-log-list)))
  219. ;;
  220. ;; Function or variable names.
  221. ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  222. (2 'change-log-list)
  223. ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
  224. (1 'change-log-list)))
  225. ;;
  226. ;; Conditionals.
  227. ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
  228. ;;
  229. ;; Function of change.
  230. ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
  231. ;;
  232. ;; Acknowledgments.
  233. ;; Don't include plain "From" because that is vague;
  234. ;; we want to encourage people to say something more specific.
  235. ;; Note that the FSF does not use "Patches by"; our convention
  236. ;; is to put the name of the author of the changes at the top
  237. ;; of the change log entry.
  238. ("\\(^\\( +\\|\t\\)\\| \\)\\(Thanks to\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
  239. 3 'change-log-acknowledgment))
  240. "Additional expressions to highlight in Change Log mode.")
  241. (defun change-log-search-file-name (where)
  242. "Return the file-name for the change under point."
  243. (save-excursion
  244. (goto-char where)
  245. (beginning-of-line 1)
  246. (if (looking-at change-log-start-entry-re)
  247. ;; We are at the start of an entry, search forward for a file
  248. ;; name.
  249. (progn
  250. (re-search-forward change-log-file-names-re nil t)
  251. (match-string-no-properties 2))
  252. (if (looking-at change-log-file-names-re)
  253. ;; We found a file name.
  254. (match-string-no-properties 2)
  255. ;; Look backwards for either a file name or the log entry start.
  256. (if (re-search-backward
  257. (concat "\\(" change-log-start-entry-re
  258. "\\)\\|\\("
  259. change-log-file-names-re "\\)") nil t)
  260. (if (match-beginning 1)
  261. ;; We got the start of the entry, look forward for a
  262. ;; file name.
  263. (progn
  264. (re-search-forward change-log-file-names-re nil t)
  265. (match-string-no-properties 2))
  266. (match-string-no-properties 4))
  267. ;; We must be before any file name, look forward.
  268. (re-search-forward change-log-file-names-re nil t)
  269. (match-string-no-properties 2))))))
  270. (defun change-log-find-file ()
  271. "Visit the file for the change under point."
  272. (interactive)
  273. (let ((file (change-log-search-file-name (point))))
  274. (if (and file (file-exists-p file))
  275. (find-file file)
  276. (message "No such file or directory: %s" file))))
  277. (defun change-log-search-tag-name-1 (&optional from)
  278. "Search for a tag name within subexpression 1 of last match.
  279. Optional argument FROM specifies a buffer position where the tag
  280. name should be located. Return value is a cons whose car is the
  281. string representing the tag and whose cdr is the position where
  282. the tag was found."
  283. (save-restriction
  284. (narrow-to-region (match-beginning 1) (match-end 1))
  285. (when from (goto-char from))
  286. ;; The regexp below skips any symbol near `point' (FROM) followed by
  287. ;; whitespace and another symbol. This should skip, for example,
  288. ;; "struct" in a specification like "(struct buffer)" and move to
  289. ;; "buffer". A leading paren is ignored.
  290. (when (looking-at
  291. "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
  292. (goto-char (match-beginning 1)))
  293. (cons (find-tag-default) (point))))
  294. (defconst change-log-tag-re
  295. "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
  296. "Regexp matching a tag name in change log entries.")
  297. (defun change-log-search-tag-name (&optional at)
  298. "Search for a tag name near `point'.
  299. Optional argument AT non-nil means search near buffer position AT.
  300. Return value is a cons whose car is the string representing
  301. the tag and whose cdr is the position where the tag was found."
  302. (save-excursion
  303. (goto-char (setq at (or at (point))))
  304. (save-restriction
  305. (widen)
  306. (or (condition-case nil
  307. ;; Within parenthesized list?
  308. (save-excursion
  309. (backward-up-list)
  310. (when (looking-at change-log-tag-re)
  311. (change-log-search-tag-name-1 at)))
  312. (error nil))
  313. (condition-case nil
  314. ;; Before parenthesized list on same line?
  315. (save-excursion
  316. (when (and (skip-chars-forward " \t")
  317. (looking-at change-log-tag-re))
  318. (change-log-search-tag-name-1)))
  319. (error nil))
  320. (condition-case nil
  321. ;; Near file name?
  322. (save-excursion
  323. (when (and (progn
  324. (beginning-of-line)
  325. (looking-at change-log-file-names-re))
  326. (goto-char (match-end 0))
  327. (skip-syntax-forward " ")
  328. (looking-at change-log-tag-re))
  329. (change-log-search-tag-name-1)))
  330. (error nil))
  331. (condition-case nil
  332. ;; Anywhere else within current entry?
  333. (let ((from
  334. (save-excursion
  335. (end-of-line)
  336. (if (re-search-backward change-log-start-entry-re nil t)
  337. (match-beginning 0)
  338. (point-min))))
  339. (to
  340. (save-excursion
  341. (end-of-line)
  342. (if (re-search-forward change-log-start-entry-re nil t)
  343. (match-beginning 0)
  344. (point-max)))))
  345. (when (and (< from to) (<= from at) (<= at to))
  346. (save-restriction
  347. ;; Narrow to current change log entry.
  348. (narrow-to-region from to)
  349. (cond
  350. ((re-search-backward change-log-tag-re nil t)
  351. (narrow-to-region (match-beginning 1) (match-end 1))
  352. (goto-char (point-max))
  353. (cons (find-tag-default) (point-max)))
  354. ((re-search-forward change-log-tag-re nil t)
  355. (narrow-to-region (match-beginning 1) (match-end 1))
  356. (goto-char (point-min))
  357. (cons (find-tag-default) (point-min)))))))
  358. (error nil))))))
  359. (defvar change-log-find-head nil)
  360. (defvar change-log-find-tail nil)
  361. (defvar change-log-find-window nil)
  362. (defun change-log-goto-source-1 (tag regexp file buffer
  363. &optional window first last)
  364. "Search for tag TAG in buffer BUFFER visiting file FILE.
  365. REGEXP is a regular expression for TAG. The remaining arguments
  366. are optional: WINDOW denotes the window to display the results of
  367. the search. FIRST is a position in BUFFER denoting the first
  368. match from previous searches for TAG. LAST is the position in
  369. BUFFER denoting the last match for TAG in the last search."
  370. (with-current-buffer buffer
  371. (save-excursion
  372. (save-restriction
  373. (widen)
  374. (if last
  375. (progn
  376. ;; When LAST is set make sure we continue from the next
  377. ;; line end to not find the same tag again.
  378. (goto-char last)
  379. (end-of-line)
  380. (condition-case nil
  381. ;; Try to go to the end of the current defun to avoid
  382. ;; false positives within the current defun's body
  383. ;; since these would match `add-log-current-defun'.
  384. (end-of-defun)
  385. ;; Don't fall behind when `end-of-defun' fails.
  386. (error (progn (goto-char last) (end-of-line))))
  387. (setq last nil))
  388. ;; When LAST was not set start at beginning of BUFFER.
  389. (goto-char (point-min)))
  390. (let (current-defun)
  391. (while (and (not last) (re-search-forward regexp nil t))
  392. ;; Verify that `add-log-current-defun' invoked at the end
  393. ;; of the match returns TAG. This heuristic works well
  394. ;; whenever the name of the defun occurs within the first
  395. ;; line of the defun.
  396. (setq current-defun (add-log-current-defun))
  397. (when (and current-defun (string-equal current-defun tag))
  398. ;; Record this as last match.
  399. (setq last (line-beginning-position))
  400. ;; Record this as first match when there's none.
  401. (unless first (setq first last)))))))
  402. (if (or last first)
  403. (with-selected-window
  404. (setq change-log-find-window (or window (display-buffer buffer)))
  405. (if last
  406. (progn
  407. (when (or (< last (point-min)) (> last (point-max)))
  408. ;; Widen to show TAG.
  409. (widen))
  410. (push-mark)
  411. (goto-char last))
  412. ;; When there are no more matches go (back) to FIRST.
  413. (message "No more matches for tag `%s' in file `%s'" tag file)
  414. (setq last first)
  415. (goto-char first))
  416. ;; Return new "tail".
  417. (list (selected-window) first last))
  418. (message "Source location of tag `%s' not found in file `%s'" tag file)
  419. nil)))
  420. (defun change-log-goto-source ()
  421. "Go to source location of \"change log tag\" near `point'.
  422. A change log tag is a symbol within a parenthesized,
  423. comma-separated list. If no suitable tag can be found nearby,
  424. try to visit the file for the change under `point' instead."
  425. (interactive)
  426. (if (and (eq last-command 'change-log-goto-source)
  427. change-log-find-tail)
  428. (setq change-log-find-tail
  429. (condition-case nil
  430. (apply 'change-log-goto-source-1
  431. (append change-log-find-head change-log-find-tail))
  432. (error
  433. (format "Cannot find more matches for tag `%s' in file `%s'"
  434. (car change-log-find-head)
  435. (nth 2 change-log-find-head)))))
  436. (save-excursion
  437. (let* ((at (point))
  438. (tag-at (change-log-search-tag-name))
  439. (tag (car tag-at))
  440. (file (when tag-at (change-log-search-file-name (cdr tag-at))))
  441. (file-at (when file (match-beginning 2)))
  442. ;; `file-2' is the file `change-log-search-file-name' finds
  443. ;; at `point'. We use `file-2' as a fallback when `tag' or
  444. ;; `file' are not suitable for some reason.
  445. (file-2 (change-log-search-file-name at))
  446. (file-2-at (when file-2 (match-beginning 2))))
  447. (cond
  448. ((and (or (not tag) (not file) (not (file-exists-p file)))
  449. (or (not file-2) (not (file-exists-p file-2))))
  450. (error "Cannot find tag or file near `point'"))
  451. ((and file-2 (file-exists-p file-2)
  452. (or (not tag) (not file) (not (file-exists-p file))
  453. (and (or (and (< file-at file-2-at) (<= file-2-at at))
  454. (and (<= at file-2-at) (< file-2-at file-at))))))
  455. ;; We either have not found a suitable file name or `file-2'
  456. ;; provides a "better" file name wrt `point'. Go to the
  457. ;; buffer of `file-2' instead.
  458. (setq change-log-find-window
  459. (display-buffer (find-file-noselect file-2))))
  460. (t
  461. (setq change-log-find-head
  462. (list tag (concat "\\_<" (regexp-quote tag) "\\_>")
  463. file (find-file-noselect file)))
  464. (condition-case nil
  465. (setq change-log-find-tail
  466. (apply 'change-log-goto-source-1 change-log-find-head))
  467. (error
  468. (format "Cannot find matches for tag `%s' in file `%s'"
  469. tag file)))))))))
  470. (defun change-log-next-error (&optional argp reset)
  471. "Move to the Nth (default 1) next match in a ChangeLog buffer.
  472. Compatibility function for \\[next-error] invocations."
  473. (interactive "p")
  474. (let* ((argp (or argp 0))
  475. (count (abs argp)) ; how many cycles
  476. (down (< argp 0)) ; are we going down? (is argp negative?)
  477. (up (not down))
  478. (search-function (if up 're-search-forward 're-search-backward)))
  479. ;; set the starting position
  480. (goto-char (cond (reset (point-min))
  481. (down (line-beginning-position))
  482. (up (line-end-position))
  483. ((point))))
  484. (funcall search-function change-log-file-names-re nil t count))
  485. (beginning-of-line)
  486. ;; if we found a place to visit...
  487. (when (looking-at change-log-file-names-re)
  488. (let (change-log-find-window)
  489. (change-log-goto-source)
  490. (when change-log-find-window
  491. ;; Select window displaying source file.
  492. (select-window change-log-find-window)))))
  493. (defvar change-log-mode-map
  494. (let ((map (make-sparse-keymap))
  495. (menu-map (make-sparse-keymap)))
  496. (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
  497. (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
  498. (define-key map [?\C-c ?\C-f] 'change-log-find-file)
  499. (define-key map [?\C-c ?\C-c] 'change-log-goto-source)
  500. (define-key map [menu-bar changelog] (cons "ChangeLog" menu-map))
  501. (define-key menu-map [gs]
  502. '(menu-item "Go To Source" change-log-goto-source
  503. :help "Go to source location of ChangeLog tag near point"))
  504. (define-key menu-map [ff]
  505. '(menu-item "Find File" change-log-find-file
  506. :help "Visit the file for the change under point"))
  507. (define-key menu-map [sep] '("--"))
  508. (define-key menu-map [nx]
  509. '(menu-item "Next Log-Edit Comment" add-log-edit-next-comment
  510. :help "Cycle forward through Log-Edit mode comment history"))
  511. (define-key menu-map [pr]
  512. '(menu-item "Previous Log-Edit Comment" add-log-edit-prev-comment
  513. :help "Cycle backward through Log-Edit mode comment history"))
  514. map)
  515. "Keymap for Change Log major mode.")
  516. ;; It used to be called change-log-time-zone-rule but really should be
  517. ;; called add-log-time-zone-rule since it's only used from add-log-* code.
  518. (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
  519. (defvar add-log-time-zone-rule nil
  520. "Time zone used for calculating change log time stamps.
  521. It takes the same format as the TZ argument of `set-time-zone-rule'.
  522. If nil, use local time.
  523. If t, use universal time.")
  524. (put 'add-log-time-zone-rule 'safe-local-variable
  525. (lambda (x) (or (booleanp x) (stringp x))))
  526. (defun add-log-iso8601-time-zone (&optional time zone)
  527. (let* ((utc-offset (or (car (current-time-zone time zone)) 0))
  528. (sign (if (< utc-offset 0) ?- ?+))
  529. (sec (abs utc-offset))
  530. (ss (% sec 60))
  531. (min (/ sec 60))
  532. (mm (% min 60))
  533. (hh (/ min 60)))
  534. (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
  535. ((not (zerop mm)) "%c%02d:%02d")
  536. (t "%c%02d"))
  537. sign hh mm ss)))
  538. (defvar add-log-iso8601-with-time-zone nil)
  539. (defun add-log-iso8601-time-string (&optional time zone)
  540. (let ((date (format-time-string "%Y-%m-%d" time zone)))
  541. (if add-log-iso8601-with-time-zone
  542. (concat date " " (add-log-iso8601-time-zone time zone))
  543. date)))
  544. (defun change-log-name ()
  545. "Return (system-dependent) default name for a change log file."
  546. (or change-log-default-name
  547. "ChangeLog"))
  548. (defun add-log-edit-prev-comment (arg)
  549. "Cycle backward through Log-Edit mode comment history.
  550. With a numeric prefix ARG, go back ARG comments."
  551. (interactive "*p")
  552. (save-restriction
  553. (narrow-to-region (point)
  554. (if (memq last-command '(add-log-edit-prev-comment
  555. add-log-edit-next-comment))
  556. (mark) (point)))
  557. (when (fboundp 'log-edit-previous-comment)
  558. (log-edit-previous-comment arg)
  559. (indent-region (point-min) (point-max))
  560. (goto-char (point-min))
  561. (unless (save-restriction (widen) (bolp))
  562. (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
  563. (set-mark (point-min))
  564. (goto-char (point-max))
  565. (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
  566. (defun add-log-edit-next-comment (arg)
  567. "Cycle forward through Log-Edit mode comment history.
  568. With a numeric prefix ARG, go back ARG comments."
  569. (interactive "*p")
  570. (add-log-edit-prev-comment (- arg)))
  571. ;;;###autoload
  572. (defun prompt-for-change-log-name ()
  573. "Prompt for a change log name."
  574. (let* ((default (change-log-name))
  575. (name (expand-file-name
  576. (read-file-name (format "Log file (default %s): " default)
  577. nil default))))
  578. ;; Handle something that is syntactically a directory name.
  579. ;; Look for ChangeLog or whatever in that directory.
  580. (if (string= (file-name-nondirectory name) "")
  581. (expand-file-name (file-name-nondirectory default)
  582. name)
  583. ;; Handle specifying a file that is a directory.
  584. (if (file-directory-p name)
  585. (expand-file-name (file-name-nondirectory default)
  586. (file-name-as-directory name))
  587. name))))
  588. (defun change-log-version-number-search ()
  589. "Return version number of current buffer's file.
  590. This is the value returned by `vc-working-revision' or, if that is
  591. nil, by matching `change-log-version-number-regexp-list'."
  592. (let* ((size (buffer-size))
  593. (limit
  594. ;; The version number can be anywhere in the file, but
  595. ;; restrict search to the file beginning: 10% should be
  596. ;; enough to prevent some mishits.
  597. ;;
  598. ;; Apply percentage only if buffer size is bigger than
  599. ;; approx 100 lines.
  600. (if (> size (* 100 80)) (+ (point) (/ size 10)))))
  601. (or (and buffer-file-name (vc-working-revision buffer-file-name))
  602. (save-restriction
  603. (widen)
  604. (let ((regexps change-log-version-number-regexp-list)
  605. version)
  606. (while regexps
  607. (save-excursion
  608. (goto-char (point-min))
  609. (when (re-search-forward (pop regexps) limit t)
  610. (setq version (match-string 1)
  611. regexps nil))))
  612. version)))))
  613. (declare-function diff-find-source-location "diff-mode"
  614. (&optional other-file reverse noprompt))
  615. ;;;###autoload
  616. (defun find-change-log (&optional file-name buffer-file)
  617. "Find a change log file for \\[add-change-log-entry] and return the name.
  618. Optional arg FILE-NAME specifies the file to use.
  619. If FILE-NAME is nil, use the value of `change-log-default-name'.
  620. If `change-log-default-name' is nil, behave as though it were \"ChangeLog\"
  621. \(or whatever we use on this operating system).
  622. If `change-log-default-name' contains a leading directory component, then
  623. simply find it in the current directory. Otherwise, search in the current
  624. directory and its successive parents for a file so named.
  625. Once a file is found, `change-log-default-name' is set locally in the
  626. current buffer to the complete file name.
  627. Optional arg BUFFER-FILE overrides `buffer-file-name'."
  628. ;; If we are called from a diff, first switch to the source buffer;
  629. ;; in order to respect buffer-local settings of change-log-default-name, etc.
  630. (with-current-buffer (let ((buff (if (derived-mode-p 'diff-mode)
  631. (car (ignore-errors
  632. (diff-find-source-location))))))
  633. (if (buffer-live-p buff) buff
  634. (current-buffer)))
  635. ;; If user specified a file name or if this buffer knows which one to use,
  636. ;; just use that.
  637. (or file-name
  638. (setq file-name (and change-log-default-name
  639. (file-name-directory change-log-default-name)
  640. change-log-default-name))
  641. (progn
  642. ;; Chase links in the source file
  643. ;; and use the change log in the dir where it points.
  644. (setq file-name (or (and (or buffer-file buffer-file-name)
  645. (file-name-directory
  646. (file-chase-links
  647. (or buffer-file buffer-file-name))))
  648. default-directory))
  649. (if (file-directory-p file-name)
  650. (setq file-name (expand-file-name (change-log-name) file-name)))
  651. ;; Chase links before visiting the file.
  652. ;; This makes it easier to use a single change log file
  653. ;; for several related directories.
  654. (setq file-name (file-chase-links file-name))
  655. (setq file-name (expand-file-name file-name))
  656. ;; Move up in the dir hierarchy till we find a change log file.
  657. (let ((file1 file-name)
  658. parent-dir)
  659. (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  660. (progn (setq parent-dir
  661. (file-name-directory
  662. (directory-file-name
  663. (file-name-directory file1))))
  664. ;; Give up if we are already at the root dir.
  665. (not (string= (file-name-directory file1)
  666. parent-dir))))
  667. ;; Move up to the parent dir and try again.
  668. (setq file1 (expand-file-name
  669. (file-name-nondirectory (change-log-name))
  670. parent-dir)))
  671. ;; If we found a change log in a parent, use that.
  672. (if (or (get-file-buffer file1) (file-exists-p file1))
  673. (setq file-name file1)))))
  674. ;; Make a local variable in this buffer so we needn't search again.
  675. (set (make-local-variable 'change-log-default-name) file-name))
  676. file-name)
  677. (defun add-log-file-name (buffer-file log-file)
  678. ;; Never want to add a change log entry for the ChangeLog file itself.
  679. (unless (or (null buffer-file) (string= buffer-file log-file))
  680. (if add-log-file-name-function
  681. (funcall add-log-file-name-function buffer-file)
  682. (setq buffer-file
  683. (let* ((dir (file-name-directory log-file))
  684. (rel (file-relative-name buffer-file dir)))
  685. ;; Sometimes with symlinks, the two buffers may have names that
  686. ;; appear to belong to different directory trees. So check the
  687. ;; file-truenames, to see if we get a better result.
  688. (if (not (string-match "\\`\\.\\./" rel))
  689. rel
  690. (let ((new (file-relative-name (file-truename buffer-file)
  691. (file-truename dir))))
  692. (if (< (length new) (length rel))
  693. new rel)))))
  694. ;; If we have a backup file, it's presumably because we're
  695. ;; comparing old and new versions (e.g. for deleted
  696. ;; functions) and we'll want to use the original name.
  697. (if (backup-file-name-p buffer-file)
  698. (file-name-sans-versions buffer-file)
  699. buffer-file))))
  700. ;;;###autoload
  701. (defun add-change-log-entry (&optional whoami file-name other-window new-entry
  702. put-new-entry-on-new-line)
  703. "Find change log file, and add an entry for today and an item for this file.
  704. Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
  705. name and email (stored in `add-log-full-name' and `add-log-mailing-address').
  706. Second arg FILE-NAME is file name of the change log.
  707. If nil, use the value of `change-log-default-name'.
  708. Third arg OTHER-WINDOW non-nil means visit in other window.
  709. Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
  710. never append to an existing entry. Option `add-log-keep-changes-together'
  711. otherwise affects whether a new entry is created.
  712. Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
  713. entry is created, put it on a new line by itself, do not put it
  714. after a comma on an existing line.
  715. Option `add-log-always-start-new-record' non-nil means always create a
  716. new record, even when the last record was made on the same date and by
  717. the same person.
  718. The change log file can start with a copyright notice and a copying
  719. permission notice. The first blank line indicates the end of these
  720. notices.
  721. Today's date is calculated according to `add-log-time-zone-rule' if
  722. non-nil, otherwise in local time."
  723. (interactive (list current-prefix-arg
  724. (prompt-for-change-log-name)))
  725. (let* ((defun (add-log-current-defun))
  726. (version (and change-log-version-info-enabled
  727. (change-log-version-number-search)))
  728. (buf-file-name (funcall add-log-buffer-file-name-function))
  729. (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
  730. (file-name (expand-file-name (find-change-log file-name buffer-file)))
  731. ;; Set ITEM to the file name to use in the new item.
  732. (item (add-log-file-name buffer-file file-name)))
  733. (unless (equal file-name buffer-file-name)
  734. (cond
  735. ((equal file-name (buffer-file-name (window-buffer)))
  736. ;; If the selected window already shows the desired buffer don't show
  737. ;; it again (particularly important if other-window is true).
  738. ;; This is important for diff-add-change-log-entries-other-window.
  739. (set-buffer (window-buffer)))
  740. ((or other-window (window-dedicated-p))
  741. (find-file-other-window file-name))
  742. (t (find-file file-name))))
  743. (or (derived-mode-p 'change-log-mode)
  744. (change-log-mode))
  745. (undo-boundary)
  746. (goto-char (point-min))
  747. (let ((full-name (or add-log-full-name (user-full-name)))
  748. (mailing-address (or add-log-mailing-address user-mail-address)))
  749. (when whoami
  750. (setq full-name (read-string "Full name: " full-name))
  751. ;; Note that some sites have room and phone number fields in
  752. ;; full name which look silly when inserted. Rather than do
  753. ;; anything about that here, let user give prefix argument so that
  754. ;; s/he can edit the full name field in prompter if s/he wants.
  755. (setq mailing-address
  756. (read-string "Mailing address: " mailing-address)))
  757. ;; If file starts with a copyright and permission notice, skip them.
  758. ;; Assume they end at first blank line.
  759. (when (looking-at "Copyright")
  760. (search-forward "\n\n")
  761. (skip-chars-forward "\n"))
  762. ;; Advance into first entry if it is usable; else make new one.
  763. (let ((new-entries
  764. (mapcar (lambda (addr)
  765. (concat
  766. (funcall add-log-time-format
  767. nil add-log-time-zone-rule)
  768. " " full-name
  769. " <" addr ">"))
  770. (if (consp mailing-address)
  771. mailing-address
  772. (list mailing-address)))))
  773. (if (and (not add-log-always-start-new-record)
  774. (let ((hit nil))
  775. (dolist (entry new-entries hit)
  776. (and (looking-at (regexp-quote entry))
  777. ;; Reject multiple author entries. (Bug#8645)
  778. (save-excursion
  779. (forward-line 1)
  780. (not (looking-at "[ \t]+.*<.*>$")))
  781. (setq hit t)))))
  782. (forward-line 1)
  783. (insert (nth (random (length new-entries))
  784. new-entries)
  785. (if use-hard-newlines hard-newline "\n")
  786. (if use-hard-newlines hard-newline "\n"))
  787. (forward-line -1))))
  788. ;; Determine where we should stop searching for a usable
  789. ;; item to add to, within this entry.
  790. (let ((bound
  791. (save-excursion
  792. (if (looking-at "\n*[^\n* \t]")
  793. (skip-chars-forward "\n")
  794. (if add-log-keep-changes-together
  795. (forward-page) ; page delimits entries for date
  796. (forward-paragraph))) ; paragraph delimits entries for file
  797. (point))))
  798. ;; Now insert the new line for this item.
  799. (cond ((re-search-forward "^\\s *\\* *$" bound t)
  800. ;; Put this file name into the existing empty item.
  801. (if item
  802. (insert item)))
  803. ((and (not new-entry)
  804. (let (case-fold-search)
  805. (re-search-forward
  806. (concat (regexp-quote (concat "* " item))
  807. ;; Don't accept `foo.bar' when
  808. ;; looking for `foo':
  809. "\\(\\s \\|[(),:]\\)")
  810. bound t)))
  811. ;; Add to the existing item for the same file.
  812. (re-search-forward "^\\s *$\\|^\\s \\*")
  813. (goto-char (match-beginning 0))
  814. ;; Delete excess empty lines; make just 2.
  815. (while (and (not (eobp)) (looking-at "^\\s *$"))
  816. (delete-region (point) (line-beginning-position 2)))
  817. (insert (if use-hard-newlines hard-newline "\n")
  818. (if use-hard-newlines hard-newline "\n"))
  819. (forward-line -2)
  820. (indent-relative-maybe))
  821. (t
  822. ;; Make a new item.
  823. (while (looking-at "\\sW")
  824. (forward-line 1))
  825. (while (and (not (eobp)) (looking-at "^\\s *$"))
  826. (delete-region (point) (line-beginning-position 2)))
  827. (insert (if use-hard-newlines hard-newline "\n")
  828. (if use-hard-newlines hard-newline "\n")
  829. (if use-hard-newlines hard-newline "\n"))
  830. (forward-line -2)
  831. (indent-to left-margin)
  832. (insert "* ")
  833. (if item (insert item)))))
  834. ;; Now insert the function name, if we have one.
  835. ;; Point is at the item for this file,
  836. ;; either at the end of the line or at the first blank line.
  837. (if (not defun)
  838. ;; No function name, so put in a colon unless we have just a star.
  839. (unless (save-excursion
  840. (beginning-of-line 1)
  841. (looking-at "\\s *\\(\\* *\\)?$"))
  842. (insert ": ")
  843. (if version (insert version ?\s)))
  844. ;; Make it easy to get rid of the function name.
  845. (undo-boundary)
  846. (unless (save-excursion
  847. (beginning-of-line 1)
  848. (looking-at "\\s *$"))
  849. (insert ?\s))
  850. ;; See if the prev function name has a message yet or not.
  851. ;; If not, merge the two items.
  852. (let ((pos (point-marker)))
  853. (skip-syntax-backward " ")
  854. (skip-chars-backward "):")
  855. (if (and (not put-new-entry-on-new-line)
  856. (looking-at "):")
  857. (let ((pos (save-excursion (backward-sexp 1) (point))))
  858. (when (equal (buffer-substring pos (point)) defun)
  859. (delete-region pos (point)))
  860. (> fill-column (+ (current-column) (length defun) 4))))
  861. (progn (skip-chars-backward ", ")
  862. (delete-region (point) pos)
  863. (unless (memq (char-before) '(?\()) (insert ", ")))
  864. (when (and (not put-new-entry-on-new-line) (looking-at "):"))
  865. (delete-region (+ 1 (point)) (line-end-position)))
  866. (goto-char pos)
  867. (insert "("))
  868. (set-marker pos nil))
  869. (insert defun "): ")
  870. (if version (insert version ?\s)))))
  871. ;;;###autoload
  872. (defun add-change-log-entry-other-window (&optional whoami file-name)
  873. "Find change log file in other window and add entry and item.
  874. This is just like `add-change-log-entry' except that it displays
  875. the change log file in another window."
  876. (interactive (if current-prefix-arg
  877. (list current-prefix-arg
  878. (prompt-for-change-log-name))))
  879. (add-change-log-entry whoami file-name t))
  880. (defvar change-log-indent-text 0)
  881. (defun change-log-fill-parenthesized-list ()
  882. ;; Fill parenthesized lists of names according to GNU standards.
  883. ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
  884. ;; should be filled as
  885. ;; * file-name.ext (very-long-foo, very-long-bar)
  886. ;; (very-long-foobar):
  887. (save-excursion
  888. (end-of-line 0)
  889. (skip-chars-backward " \t")
  890. (when (and (equal (char-before) ?\,)
  891. (> (point) (1+ (point-min))))
  892. (condition-case nil
  893. (when (save-excursion
  894. (and (prog2
  895. (up-list -1)
  896. (equal (char-after) ?\()
  897. (skip-chars-backward " \t"))
  898. (or (bolp)
  899. ;; Skip everything but a whitespace or asterisk.
  900. (and (not (zerop (skip-chars-backward "^ \t\n*")))
  901. (skip-chars-backward " \t")
  902. ;; We want one asterisk here.
  903. (= (skip-chars-backward "*") -1)
  904. (skip-chars-backward " \t")
  905. (bolp)))))
  906. ;; Delete the comma.
  907. (delete-char -1)
  908. ;; Close list on previous line.
  909. (insert ")")
  910. (skip-chars-forward " \t\n")
  911. ;; Start list on new line.
  912. (insert-before-markers "("))
  913. (error nil)))))
  914. (defun change-log-indent ()
  915. (change-log-fill-parenthesized-list)
  916. (let* ((indent
  917. (save-excursion
  918. (beginning-of-line)
  919. (skip-chars-forward " \t")
  920. (cond
  921. ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
  922. ;; Matching the output of add-log-time-format is difficult,
  923. ;; but I'll get it has at least two adjacent digits.
  924. (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
  925. 0)
  926. ((looking-at "[^*(]")
  927. (+ (current-left-margin) change-log-indent-text))
  928. (t (current-left-margin)))))
  929. (pos (save-excursion (indent-line-to indent) (point))))
  930. (if (> pos (point)) (goto-char pos))))
  931. (defvar smerge-resolve-function)
  932. (defvar copyright-at-end-flag)
  933. ;;;###autoload
  934. (define-derived-mode change-log-mode text-mode "Change Log"
  935. "Major mode for editing change logs; like Indented Text mode.
  936. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  937. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  938. Each entry behaves as a paragraph, and the entries for one day as a page.
  939. Runs `change-log-mode-hook'.
  940. \n\\{change-log-mode-map}"
  941. (setq left-margin 8
  942. fill-column 74
  943. indent-tabs-mode t
  944. tab-width 8
  945. show-trailing-whitespace t)
  946. (set (make-local-variable 'fill-forward-paragraph-function)
  947. 'change-log-fill-forward-paragraph)
  948. (set (make-local-variable 'comment-start) nil)
  949. ;; Make sure we call `change-log-indent' when filling.
  950. (set (make-local-variable 'fill-indent-according-to-mode) t)
  951. ;; Avoid that filling leaves behind a single "*" on a line.
  952. (add-hook 'fill-nobreak-predicate
  953. (lambda ()
  954. (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
  955. nil t)
  956. (set (make-local-variable 'indent-line-function) 'change-log-indent)
  957. (set (make-local-variable 'tab-always-indent) nil)
  958. (set (make-local-variable 'copyright-at-end-flag) t)
  959. ;; We really do want "^" in paragraph-start below: it is only the
  960. ;; lines that begin at column 0 (despite the left-margin of 8) that
  961. ;; we are looking for. Adding `* ' allows eliding the blank line
  962. ;; between entries for different files.
  963. (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
  964. (set (make-local-variable 'paragraph-separate) paragraph-start)
  965. ;; Match null string on the date-line so that the date-line
  966. ;; is grouped with what follows.
  967. (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  968. (set (make-local-variable 'version-control) 'never)
  969. (set (make-local-variable 'smerge-resolve-function)
  970. 'change-log-resolve-conflict)
  971. (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  972. (set (make-local-variable 'font-lock-defaults)
  973. '(change-log-font-lock-keywords t nil nil backward-paragraph))
  974. (set (make-local-variable 'multi-isearch-next-buffer-function)
  975. 'change-log-next-buffer)
  976. (set (make-local-variable 'beginning-of-defun-function)
  977. 'change-log-beginning-of-defun)
  978. (set (make-local-variable 'end-of-defun-function)
  979. 'change-log-end-of-defun)
  980. ;; next-error function glue
  981. (setq next-error-function 'change-log-next-error)
  982. (setq next-error-last-buffer (current-buffer)))
  983. (defun change-log-next-buffer (&optional buffer wrap)
  984. "Return the next buffer in the series of ChangeLog file buffers.
  985. This function is used for multiple buffers isearch.
  986. A sequence of buffers is formed by ChangeLog files with decreasing
  987. numeric file name suffixes in the directory of the initial ChangeLog
  988. file were isearch was started."
  989. (let* ((name (change-log-name))
  990. (files (cons name (sort (file-expand-wildcards
  991. (concat name "[-.][0-9]*"))
  992. (lambda (a b)
  993. ;; The file's extension may not have a valid
  994. ;; version form (e.g. VC backup revisions).
  995. (ignore-errors
  996. (version< (substring b (length name))
  997. (substring a (length name))))))))
  998. (files (if isearch-forward files (reverse files)))
  999. (file (if wrap
  1000. (car files)
  1001. (cadr (member (file-name-nondirectory (buffer-file-name buffer))
  1002. files)))))
  1003. ;; If there are no files that match the default pattern ChangeLog.[0-9],
  1004. ;; return the current buffer to force isearch wrapping to its beginning.
  1005. ;; If file is nil, multi-isearch-search-fun will signal "end of multi".
  1006. (if (file-exists-p file)
  1007. (find-file-noselect file)
  1008. (current-buffer))))
  1009. (defun change-log-fill-forward-paragraph (n)
  1010. "Cut paragraphs so filling preserves open parentheses at beginning of lines."
  1011. (let (;; Add lines starting with whitespace followed by a left paren or an
  1012. ;; asterisk.
  1013. (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)")))
  1014. (forward-paragraph n)))
  1015. (defcustom add-log-current-defun-header-regexp
  1016. "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
  1017. "Heuristic regexp used by `add-log-current-defun' for unknown major modes.
  1018. The regexp's first submatch is placed in the ChangeLog entry, in
  1019. parentheses."
  1020. :type 'regexp
  1021. :group 'change-log)
  1022. (declare-function c-cpp-define-name "cc-cmds" ())
  1023. (declare-function c-defun-name "cc-cmds" ())
  1024. ;;;###autoload
  1025. (defun add-log-current-defun ()
  1026. "Return name of function definition point is in, or nil.
  1027. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  1028. Texinfo (@node titles) and Perl.
  1029. Other modes are handled by a heuristic that looks in the 10K before
  1030. point for uppercase headings starting in the first column or
  1031. identifiers followed by `:' or `='. See variables
  1032. `add-log-current-defun-header-regexp' and
  1033. `add-log-current-defun-function'.
  1034. Has a preference of looking backwards."
  1035. (condition-case nil
  1036. (save-excursion
  1037. (if add-log-current-defun-function
  1038. (funcall add-log-current-defun-function)
  1039. ;; If all else fails, try heuristics
  1040. (let (case-fold-search
  1041. result)
  1042. (end-of-line)
  1043. (when (re-search-backward add-log-current-defun-header-regexp
  1044. (- (point) 10000) t)
  1045. (setq result (or (match-string-no-properties 1)
  1046. (match-string-no-properties 0)))
  1047. ;; Strip whitespace away
  1048. (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
  1049. result)
  1050. (setq result (match-string-no-properties 1 result)))
  1051. result))))
  1052. (error nil)))
  1053. (defvar change-log-get-method-definition-md)
  1054. ;; Subroutine used within change-log-get-method-definition.
  1055. ;; Add the last match in the buffer to the end of `md',
  1056. ;; followed by the string END; move to the end of that match.
  1057. (defun change-log-get-method-definition-1 (end)
  1058. (setq change-log-get-method-definition-md
  1059. (concat change-log-get-method-definition-md
  1060. (match-string 1)
  1061. end))
  1062. (goto-char (match-end 0)))
  1063. (defun change-log-get-method-definition ()
  1064. "For Objective C, return the method name if we are in a method."
  1065. (let ((change-log-get-method-definition-md "["))
  1066. (save-excursion
  1067. (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
  1068. (change-log-get-method-definition-1 " ")))
  1069. (save-excursion
  1070. (cond
  1071. ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
  1072. (change-log-get-method-definition-1 "")
  1073. (while (not (looking-at "[{;]"))
  1074. (looking-at
  1075. "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
  1076. (change-log-get-method-definition-1 ""))
  1077. (concat change-log-get-method-definition-md "]"))))))
  1078. (autoload 'timezone-make-date-sortable "timezone")
  1079. (defun change-log-sortable-date-at ()
  1080. "Return date of log entry in a consistent form for sorting.
  1081. Point is assumed to be at the start of the entry."
  1082. (if (looking-at change-log-start-entry-re)
  1083. (let ((date (match-string-no-properties 0)))
  1084. (if date
  1085. (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
  1086. (concat (match-string 1 date) (match-string 2 date)
  1087. (match-string 3 date))
  1088. (ignore-errors (timezone-make-date-sortable date)))))
  1089. (error "Bad date")))
  1090. (defun change-log-resolve-conflict ()
  1091. "Function to be used in `smerge-resolve-function'."
  1092. (save-excursion
  1093. (save-restriction
  1094. (narrow-to-region (match-beginning 0) (match-end 0))
  1095. (let ((mb1 (match-beginning 1))
  1096. (me1 (match-end 1))
  1097. (mb3 (match-beginning 3))
  1098. (me3 (match-end 3))
  1099. (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
  1100. (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
  1101. (unwind-protect
  1102. (let ((buf (current-buffer)))
  1103. (with-current-buffer tmp1
  1104. (change-log-mode)
  1105. (insert-buffer-substring buf mb1 me1))
  1106. (with-current-buffer tmp2
  1107. (change-log-mode)
  1108. (insert-buffer-substring buf mb3 me3)
  1109. ;; Do the merge here instead of inside `buf' so as to be
  1110. ;; more robust in case change-log-merge fails.
  1111. (change-log-merge tmp1))
  1112. (goto-char (point-max))
  1113. (delete-region (point-min)
  1114. (prog1 (point)
  1115. (insert-buffer-substring tmp2))))
  1116. (kill-buffer tmp1)
  1117. (kill-buffer tmp2))))))
  1118. ;;;###autoload
  1119. (defun change-log-merge (other-log)
  1120. "Merge the contents of change log file OTHER-LOG with this buffer.
  1121. Both must be found in Change Log mode (since the merging depends on
  1122. the appropriate motion commands). OTHER-LOG can be either a file name
  1123. or a buffer.
  1124. Entries are inserted in chronological order. Both the current and
  1125. old-style time formats for entries are supported."
  1126. (interactive "*fLog file name to merge: ")
  1127. (if (not (derived-mode-p 'change-log-mode))
  1128. (error "Not in Change Log mode"))
  1129. (let ((other-buf (if (bufferp other-log) other-log
  1130. (find-file-noselect other-log)))
  1131. (buf (current-buffer))
  1132. date1 start end)
  1133. (save-excursion
  1134. (goto-char (point-min))
  1135. (set-buffer other-buf)
  1136. (goto-char (point-min))
  1137. (if (not (derived-mode-p 'change-log-mode))
  1138. (error "%s not found in Change Log mode" other-log))
  1139. ;; Loop through all the entries in OTHER-LOG.
  1140. (while (not (eobp))
  1141. (setq date1 (change-log-sortable-date-at))
  1142. (setq start (point)
  1143. end (progn (forward-page) (point)))
  1144. ;; Look for an entry in original buffer that isn't later.
  1145. (with-current-buffer buf
  1146. (while (and (not (eobp))
  1147. (string< date1 (change-log-sortable-date-at)))
  1148. (forward-page))
  1149. (if (not (eobp))
  1150. (insert-buffer-substring other-buf start end)
  1151. ;; At the end of the original buffer, insert a newline to
  1152. ;; separate entries and then the rest of the file being
  1153. ;; merged.
  1154. (unless (or (bobp)
  1155. (and (= ?\n (char-before))
  1156. (or (<= (1- (point)) (point-min))
  1157. (= ?\n (char-before (1- (point)))))))
  1158. (insert (if use-hard-newlines hard-newline "\n")))
  1159. ;; Move to the end of it to terminate outer loop.
  1160. (with-current-buffer other-buf
  1161. (goto-char (point-max)))
  1162. (insert-buffer-substring other-buf start)))))))
  1163. (defun change-log-beginning-of-defun ()
  1164. (re-search-backward change-log-start-entry-re nil 'move))
  1165. (defun change-log-end-of-defun ()
  1166. ;; Look back and if there is no entry there it means we are before
  1167. ;; the first ChangeLog entry, so go forward until finding one.
  1168. (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
  1169. (re-search-forward change-log-start-entry-re nil t))
  1170. ;; In case we are at the end of log entry going forward a line will
  1171. ;; make us find the next entry when searching. If we are inside of
  1172. ;; an entry going forward a line will still keep the point inside
  1173. ;; the same entry.
  1174. (forward-line 1)
  1175. ;; In case we are at the beginning of an entry, move past it.
  1176. (when (looking-at change-log-start-entry-re)
  1177. (goto-char (match-end 0))
  1178. (forward-line 1))
  1179. ;; Search for the start of the next log entry. Go to the end of the
  1180. ;; buffer if we could not find a next entry.
  1181. (when (re-search-forward change-log-start-entry-re nil 'move)
  1182. (goto-char (match-beginning 0))
  1183. (forward-line -1)))
  1184. (provide 'add-log)
  1185. ;;; add-log.el ends here