add-log.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1988, 1993-1994, 1997-1998, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  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 used by `add-log-current-defun' in preference to built-in rules.
  49. Returns function's name as a string, or nil if outside a function."
  50. :type '(choice (const nil) function)
  51. :group 'change-log)
  52. ;;;###autoload
  53. (defcustom add-log-full-name nil
  54. "Full name of user, for inclusion in ChangeLog daily headers.
  55. This defaults to the value returned by the function `user-full-name'."
  56. :type '(choice (const :tag "Default" nil)
  57. string)
  58. :group 'change-log)
  59. ;;;###autoload
  60. (defcustom add-log-mailing-address nil
  61. "Email addresses of user, for inclusion in ChangeLog headers.
  62. This defaults to the value of `user-mail-address'. In addition to
  63. being a simple string, this value can also be a list. All elements
  64. will be recognized as referring to the same user; when creating a new
  65. ChangeLog entry, one element will be chosen at random."
  66. :type '(choice (const :tag "Default" nil)
  67. (string :tag "String")
  68. (repeat :tag "List of Strings" string))
  69. :group 'change-log)
  70. (defcustom add-log-time-format 'add-log-iso8601-time-string
  71. "Function that defines the time format.
  72. For example, `add-log-iso8601-time-string', which gives the
  73. date in international ISO 8601 format,
  74. and `current-time-string' are two valid values."
  75. :type '(radio (const :tag "International ISO 8601 format"
  76. add-log-iso8601-time-string)
  77. (const :tag "Old format, as returned by `current-time-string'"
  78. current-time-string)
  79. (function :tag "Other"))
  80. :group 'change-log)
  81. (defcustom add-log-keep-changes-together nil
  82. "If non-nil, normally keep day's log entries for one file together.
  83. Log entries for a given file made with \\[add-change-log-entry] or
  84. \\[add-change-log-entry-other-window] will only be added to others \
  85. for that file made
  86. today if this variable is non-nil or that file comes first in today's
  87. entries. Otherwise another entry for that file will be started. An
  88. original log:
  89. * foo (...): ...
  90. * bar (...): change 1
  91. in the latter case, \\[add-change-log-entry-other-window] in a \
  92. buffer visiting `bar', yields:
  93. * bar (...): -!-
  94. * foo (...): ...
  95. * bar (...): change 1
  96. and in the former:
  97. * foo (...): ...
  98. * bar (...): change 1
  99. (...): -!-
  100. The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
  101. this variable."
  102. :version "20.3"
  103. :type 'boolean
  104. :group 'change-log)
  105. (defcustom add-log-always-start-new-record nil
  106. "If non-nil, `add-change-log-entry' will always start a new record."
  107. :version "22.1"
  108. :type 'boolean
  109. :group 'change-log)
  110. (defcustom add-log-buffer-file-name-function nil
  111. "If non-nil, function to call to identify the full filename of a buffer.
  112. This function is called with no argument. If this is nil, the default is to
  113. use `buffer-file-name'."
  114. :type '(choice (const nil) function)
  115. :group 'change-log)
  116. (defcustom add-log-file-name-function nil
  117. "If non-nil, function to call to identify the filename for a ChangeLog entry.
  118. This function is called with one argument, the value of variable
  119. `buffer-file-name' in that buffer. If this is nil, the default is to
  120. use the file's name relative to the directory of the change log file."
  121. :type '(choice (const nil) function)
  122. :group 'change-log)
  123. (defcustom change-log-version-info-enabled nil
  124. "If non-nil, enable recording version numbers with the changes."
  125. :version "21.1"
  126. :type 'boolean
  127. :group 'change-log)
  128. (defcustom change-log-version-number-regexp-list
  129. (let ((re "\\([0-9]+\.[0-9.]+\\)"))
  130. (list
  131. ;; (defconst ad-version "2.15"
  132. (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
  133. ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
  134. (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
  135. "List of regexps to search for version number.
  136. The version number must be in group 1.
  137. Note: The search is conducted only within 10%, at the beginning of the file."
  138. :version "21.1"
  139. :type '(repeat regexp)
  140. :group 'change-log)
  141. (defface change-log-date
  142. '((t (:inherit font-lock-string-face)))
  143. "Face used to highlight dates in date lines."
  144. :version "21.1"
  145. :group 'change-log)
  146. (define-obsolete-face-alias 'change-log-date-face 'change-log-date "22.1")
  147. (defface change-log-name
  148. '((t (:inherit font-lock-constant-face)))
  149. "Face for highlighting author names."
  150. :version "21.1"
  151. :group 'change-log)
  152. (define-obsolete-face-alias 'change-log-name-face 'change-log-name "22.1")
  153. (defface change-log-email
  154. '((t (:inherit font-lock-variable-name-face)))
  155. "Face for highlighting author email addresses."
  156. :version "21.1"
  157. :group 'change-log)
  158. (define-obsolete-face-alias 'change-log-email-face 'change-log-email "22.1")
  159. (defface change-log-file
  160. '((t (:inherit font-lock-function-name-face)))
  161. "Face for highlighting file names."
  162. :version "21.1"
  163. :group 'change-log)
  164. (define-obsolete-face-alias 'change-log-file-face 'change-log-file "22.1")
  165. (defface change-log-list
  166. '((t (:inherit font-lock-keyword-face)))
  167. "Face for highlighting parenthesized lists of functions or variables."
  168. :version "21.1"
  169. :group 'change-log)
  170. (define-obsolete-face-alias 'change-log-list-face 'change-log-list "22.1")
  171. (defface change-log-conditionals
  172. '((t (:inherit font-lock-variable-name-face)))
  173. "Face for highlighting conditionals of the form `[...]'."
  174. :version "21.1"
  175. :group 'change-log)
  176. (define-obsolete-face-alias 'change-log-conditionals-face
  177. 'change-log-conditionals "22.1")
  178. (defface change-log-function
  179. '((t (:inherit font-lock-variable-name-face)))
  180. "Face for highlighting items of the form `<....>'."
  181. :version "21.1"
  182. :group 'change-log)
  183. (define-obsolete-face-alias 'change-log-function-face
  184. 'change-log-function "22.1")
  185. (defface change-log-acknowledgement
  186. '((t (:inherit font-lock-comment-face)))
  187. "Face for highlighting acknowledgments."
  188. :version "21.1"
  189. :group 'change-log)
  190. (define-obsolete-face-alias 'change-log-acknowledgement-face
  191. 'change-log-acknowledgement "22.1")
  192. (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
  193. (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
  194. (defvar change-log-font-lock-keywords
  195. `(;;
  196. ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
  197. ;; Fixme: this regexp is just an approximate one and may match
  198. ;; wrongly with a non-date line existing as a random note. In
  199. ;; addition, using any kind of fixed setting like this doesn't
  200. ;; work if a user customizes add-log-time-format.
  201. ("^[0-9-]+ +\\|^ \\{11,\\}\\|^\t \\{3,\\}\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
  202. (0 'change-log-date-face)
  203. ;; Name and e-mail; some people put e-mail in parens, not angles.
  204. ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
  205. (1 'change-log-name)
  206. (2 'change-log-email)))
  207. ;;
  208. ;; File names.
  209. (,change-log-file-names-re
  210. (2 'change-log-file)
  211. ;; Possibly further names in a list:
  212. ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
  213. ;; Possibly a parenthesized list of names:
  214. ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  215. nil nil (1 'change-log-list))
  216. ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  217. nil nil (1 'change-log-list)))
  218. ;;
  219. ;; Function or variable names.
  220. ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
  221. (2 'change-log-list)
  222. ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
  223. (1 'change-log-list)))
  224. ;;
  225. ;; Conditionals.
  226. ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
  227. ;;
  228. ;; Function of change.
  229. ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
  230. ;;
  231. ;; Acknowledgements.
  232. ;; Don't include plain "From" because that is vague;
  233. ;; we want to encourage people to say something more specific.
  234. ;; Note that the FSF does not use "Patches by"; our convention
  235. ;; is to put the name of the author of the changes at the top
  236. ;; of the change log entry.
  237. ("\\(^\\( +\\|\t\\)\\| \\)\\(Thanks to\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
  238. 3 'change-log-acknowledgement))
  239. "Additional expressions to highlight in Change Log mode.")
  240. (defun change-log-search-file-name (where)
  241. "Return the file-name for the change under point."
  242. (save-excursion
  243. (goto-char where)
  244. (beginning-of-line 1)
  245. (if (looking-at change-log-start-entry-re)
  246. ;; We are at the start of an entry, search forward for a file
  247. ;; name.
  248. (progn
  249. (re-search-forward change-log-file-names-re nil t)
  250. (match-string-no-properties 2))
  251. (if (looking-at change-log-file-names-re)
  252. ;; We found a file name.
  253. (match-string-no-properties 2)
  254. ;; Look backwards for either a file name or the log entry start.
  255. (if (re-search-backward
  256. (concat "\\(" change-log-start-entry-re
  257. "\\)\\|\\("
  258. change-log-file-names-re "\\)") nil t)
  259. (if (match-beginning 1)
  260. ;; We got the start of the entry, look forward for a
  261. ;; file name.
  262. (progn
  263. (re-search-forward change-log-file-names-re nil t)
  264. (match-string-no-properties 2))
  265. (match-string-no-properties 4))
  266. ;; We must be before any file name, look forward.
  267. (re-search-forward change-log-file-names-re nil t)
  268. (match-string-no-properties 2))))))
  269. (defun change-log-find-file ()
  270. "Visit the file for the change under point."
  271. (interactive)
  272. (let ((file (change-log-search-file-name (point))))
  273. (if (and file (file-exists-p file))
  274. (find-file file)
  275. (message "No such file or directory: %s" file))))
  276. (defun change-log-search-tag-name-1 (&optional from)
  277. "Search for a tag name within subexpression 1 of last match.
  278. Optional argument FROM specifies a buffer position where the tag
  279. name should be located. Return value is a cons whose car is the
  280. string representing the tag and whose cdr is the position where
  281. the tag was found."
  282. (save-restriction
  283. (narrow-to-region (match-beginning 1) (match-end 1))
  284. (when from (goto-char from))
  285. ;; The regexp below skips any symbol near `point' (FROM) followed by
  286. ;; whitespace and another symbol. This should skip, for example,
  287. ;; "struct" in a specification like "(struct buffer)" and move to
  288. ;; "buffer". A leading paren is ignored.
  289. (when (looking-at
  290. "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
  291. (goto-char (match-beginning 1)))
  292. (cons (find-tag-default) (point))))
  293. (defconst change-log-tag-re
  294. "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
  295. "Regexp matching a tag name in change log entries.")
  296. (defun change-log-search-tag-name (&optional at)
  297. "Search for a tag name near `point'.
  298. Optional argument AT non-nil means search near buffer position AT.
  299. Return value is a cons whose car is the string representing
  300. the tag and whose cdr is the position where the tag was found."
  301. (save-excursion
  302. (goto-char (setq at (or at (point))))
  303. (save-restriction
  304. (widen)
  305. (or (condition-case nil
  306. ;; Within parenthesized list?
  307. (save-excursion
  308. (backward-up-list)
  309. (when (looking-at change-log-tag-re)
  310. (change-log-search-tag-name-1 at)))
  311. (error nil))
  312. (condition-case nil
  313. ;; Before parenthesized list on same line?
  314. (save-excursion
  315. (when (and (skip-chars-forward " \t")
  316. (looking-at change-log-tag-re))
  317. (change-log-search-tag-name-1)))
  318. (error nil))
  319. (condition-case nil
  320. ;; Near file name?
  321. (save-excursion
  322. (when (and (progn
  323. (beginning-of-line)
  324. (looking-at change-log-file-names-re))
  325. (goto-char (match-end 0))
  326. (skip-syntax-forward " ")
  327. (looking-at change-log-tag-re))
  328. (change-log-search-tag-name-1)))
  329. (error nil))
  330. (condition-case nil
  331. ;; Anywhere else within current entry?
  332. (let ((from
  333. (save-excursion
  334. (end-of-line)
  335. (if (re-search-backward change-log-start-entry-re nil t)
  336. (match-beginning 0)
  337. (point-min))))
  338. (to
  339. (save-excursion
  340. (end-of-line)
  341. (if (re-search-forward change-log-start-entry-re nil t)
  342. (match-beginning 0)
  343. (point-max)))))
  344. (when (and (< from to) (<= from at) (<= at to))
  345. (save-restriction
  346. ;; Narrow to current change log entry.
  347. (narrow-to-region from to)
  348. (cond
  349. ((re-search-backward change-log-tag-re nil t)
  350. (narrow-to-region (match-beginning 1) (match-end 1))
  351. (goto-char (point-max))
  352. (cons (find-tag-default) (point-max)))
  353. ((re-search-forward change-log-tag-re nil t)
  354. (narrow-to-region (match-beginning 1) (match-end 1))
  355. (goto-char (point-min))
  356. (cons (find-tag-default) (point-min)))))))
  357. (error nil))))))
  358. (defvar change-log-find-head nil)
  359. (defvar change-log-find-tail nil)
  360. (defvar change-log-find-window nil)
  361. (defun change-log-goto-source-1 (tag regexp file buffer
  362. &optional window first last)
  363. "Search for tag TAG in buffer BUFFER visiting file FILE.
  364. REGEXP is a regular expression for TAG. The remaining arguments
  365. are optional: WINDOW denotes the window to display the results of
  366. the search. FIRST is a position in BUFFER denoting the first
  367. match from previous searches for TAG. LAST is the position in
  368. BUFFER denoting the last match for TAG in the last search."
  369. (with-current-buffer buffer
  370. (save-excursion
  371. (save-restriction
  372. (widen)
  373. (if last
  374. (progn
  375. ;; When LAST is set make sure we continue from the next
  376. ;; line end to not find the same tag again.
  377. (goto-char last)
  378. (end-of-line)
  379. (condition-case nil
  380. ;; Try to go to the end of the current defun to avoid
  381. ;; false positives within the current defun's body
  382. ;; since these would match `add-log-current-defun'.
  383. (end-of-defun)
  384. ;; Don't fall behind when `end-of-defun' fails.
  385. (error (progn (goto-char last) (end-of-line))))
  386. (setq last nil))
  387. ;; When LAST was not set start at beginning of BUFFER.
  388. (goto-char (point-min)))
  389. (let (current-defun)
  390. (while (and (not last) (re-search-forward regexp nil t))
  391. ;; Verify that `add-log-current-defun' invoked at the end
  392. ;; of the match returns TAG. This heuristic works well
  393. ;; whenever the name of the defun occurs within the first
  394. ;; line of the defun.
  395. (setq current-defun (add-log-current-defun))
  396. (when (and current-defun (string-equal current-defun tag))
  397. ;; Record this as last match.
  398. (setq last (line-beginning-position))
  399. ;; Record this as first match when there's none.
  400. (unless first (setq first last)))))))
  401. (if (or last first)
  402. (with-selected-window
  403. (setq change-log-find-window (or window (display-buffer buffer)))
  404. (if last
  405. (progn
  406. (when (or (< last (point-min)) (> last (point-max)))
  407. ;; Widen to show TAG.
  408. (widen))
  409. (push-mark)
  410. (goto-char last))
  411. ;; When there are no more matches go (back) to FIRST.
  412. (message "No more matches for tag `%s' in file `%s'" tag file)
  413. (setq last first)
  414. (goto-char first))
  415. ;; Return new "tail".
  416. (list (selected-window) first last))
  417. (message "Source location of tag `%s' not found in file `%s'" tag file)
  418. nil)))
  419. (defun change-log-goto-source ()
  420. "Go to source location of \"change log tag\" near `point'.
  421. A change log tag is a symbol within a parenthesized,
  422. comma-separated list. If no suitable tag can be found nearby,
  423. try to visit the file for the change under `point' instead."
  424. (interactive)
  425. (if (and (eq last-command 'change-log-goto-source)
  426. change-log-find-tail)
  427. (setq change-log-find-tail
  428. (condition-case nil
  429. (apply 'change-log-goto-source-1
  430. (append change-log-find-head change-log-find-tail))
  431. (error
  432. (format "Cannot find more matches for tag `%s' in file `%s'"
  433. (car change-log-find-head)
  434. (nth 2 change-log-find-head)))))
  435. (save-excursion
  436. (let* ((at (point))
  437. (tag-at (change-log-search-tag-name))
  438. (tag (car tag-at))
  439. (file (when tag-at (change-log-search-file-name (cdr tag-at))))
  440. (file-at (when file (match-beginning 2)))
  441. ;; `file-2' is the file `change-log-search-file-name' finds
  442. ;; at `point'. We use `file-2' as a fallback when `tag' or
  443. ;; `file' are not suitable for some reason.
  444. (file-2 (change-log-search-file-name at))
  445. (file-2-at (when file-2 (match-beginning 2))))
  446. (cond
  447. ((and (or (not tag) (not file) (not (file-exists-p file)))
  448. (or (not file-2) (not (file-exists-p file-2))))
  449. (error "Cannot find tag or file near `point'"))
  450. ((and file-2 (file-exists-p file-2)
  451. (or (not tag) (not file) (not (file-exists-p file))
  452. (and (or (and (< file-at file-2-at) (<= file-2-at at))
  453. (and (<= at file-2-at) (< file-2-at file-at))))))
  454. ;; We either have not found a suitable file name or `file-2'
  455. ;; provides a "better" file name wrt `point'. Go to the
  456. ;; buffer of `file-2' instead.
  457. (setq change-log-find-window
  458. (display-buffer (find-file-noselect file-2))))
  459. (t
  460. (setq change-log-find-head
  461. (list tag (concat "\\_<" (regexp-quote tag) "\\_>")
  462. file (find-file-noselect file)))
  463. (condition-case nil
  464. (setq change-log-find-tail
  465. (apply 'change-log-goto-source-1 change-log-find-head))
  466. (error
  467. (format "Cannot find matches for tag `%s' in file `%s'"
  468. tag file)))))))))
  469. (defun change-log-next-error (&optional argp reset)
  470. "Move to the Nth (default 1) next match in a ChangeLog buffer.
  471. Compatibility function for \\[next-error] invocations."
  472. (interactive "p")
  473. (let* ((argp (or argp 0))
  474. (count (abs argp)) ; how many cycles
  475. (down (< argp 0)) ; are we going down? (is argp negative?)
  476. (up (not down))
  477. (search-function (if up 're-search-forward 're-search-backward)))
  478. ;; set the starting position
  479. (goto-char (cond (reset (point-min))
  480. (down (line-beginning-position))
  481. (up (line-end-position))
  482. ((point))))
  483. (funcall search-function change-log-file-names-re nil t count))
  484. (beginning-of-line)
  485. ;; if we found a place to visit...
  486. (when (looking-at change-log-file-names-re)
  487. (let (change-log-find-window)
  488. (change-log-goto-source)
  489. (when change-log-find-window
  490. ;; Select window displaying source file.
  491. (select-window change-log-find-window)))))
  492. (defvar change-log-mode-map
  493. (let ((map (make-sparse-keymap))
  494. (menu-map (make-sparse-keymap)))
  495. (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
  496. (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
  497. (define-key map [?\C-c ?\C-f] 'change-log-find-file)
  498. (define-key map [?\C-c ?\C-c] 'change-log-goto-source)
  499. (define-key map [menu-bar changelog] (cons "ChangeLog" menu-map))
  500. (define-key menu-map [gs]
  501. '(menu-item "Go To Source" change-log-goto-source
  502. :help "Go to source location of ChangeLog tag near point"))
  503. (define-key menu-map [ff]
  504. '(menu-item "Find File" change-log-find-file
  505. :help "Visit the file for the change under point"))
  506. (define-key menu-map [sep] '("--"))
  507. (define-key menu-map [nx]
  508. '(menu-item "Next Log-Edit Comment" add-log-edit-next-comment
  509. :help "Cycle forward through Log-Edit mode comment history"))
  510. (define-key menu-map [pr]
  511. '(menu-item "Previous Log-Edit Comment" add-log-edit-prev-comment
  512. :help "Cycle backward through Log-Edit mode comment history"))
  513. map)
  514. "Keymap for Change Log major mode.")
  515. ;; It used to be called change-log-time-zone-rule but really should be
  516. ;; called add-log-time-zone-rule since it's only used from add-log-* code.
  517. (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
  518. (defvar add-log-time-zone-rule nil
  519. "Time zone used for calculating change log time stamps.
  520. It takes the same format as the TZ argument of `set-time-zone-rule'.
  521. If nil, use local time.
  522. If t, use universal time.")
  523. (put 'add-log-time-zone-rule 'safe-local-variable
  524. (lambda (x) (or (booleanp x) (stringp x))))
  525. (defun add-log-iso8601-time-zone (&optional time)
  526. (let* ((utc-offset (or (car (current-time-zone time)) 0))
  527. (sign (if (< utc-offset 0) ?- ?+))
  528. (sec (abs utc-offset))
  529. (ss (% sec 60))
  530. (min (/ sec 60))
  531. (mm (% min 60))
  532. (hh (/ min 60)))
  533. (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
  534. ((not (zerop mm)) "%c%02d:%02d")
  535. (t "%c%02d"))
  536. sign hh mm ss)))
  537. (defvar add-log-iso8601-with-time-zone nil)
  538. (defun add-log-iso8601-time-string ()
  539. (let ((time (format-time-string "%Y-%m-%d"
  540. nil (eq t add-log-time-zone-rule))))
  541. (if add-log-iso8601-with-time-zone
  542. (concat time " " (add-log-iso8601-time-zone))
  543. time)))
  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 (if add-log-buffer-file-name-function
  729. (funcall add-log-buffer-file-name-function)
  730. buffer-file-name))
  731. (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
  732. (file-name (expand-file-name (find-change-log file-name buffer-file)))
  733. ;; Set ITEM to the file name to use in the new item.
  734. (item (add-log-file-name buffer-file file-name)))
  735. (unless (equal file-name buffer-file-name)
  736. (cond
  737. ((equal file-name (buffer-file-name (window-buffer (selected-window))))
  738. ;; If the selected window already shows the desired buffer don't show
  739. ;; it again (particularly important if other-window is true).
  740. ;; This is important for diff-add-change-log-entries-other-window.
  741. (set-buffer (window-buffer (selected-window))))
  742. ((or other-window (window-dedicated-p (selected-window)))
  743. (find-file-other-window file-name))
  744. (t (find-file file-name))))
  745. (or (derived-mode-p 'change-log-mode)
  746. (change-log-mode))
  747. (undo-boundary)
  748. (goto-char (point-min))
  749. (let ((full-name (or add-log-full-name (user-full-name)))
  750. (mailing-address (or add-log-mailing-address user-mail-address)))
  751. (when whoami
  752. (setq full-name (read-string "Full name: " full-name))
  753. ;; Note that some sites have room and phone number fields in
  754. ;; full name which look silly when inserted. Rather than do
  755. ;; anything about that here, let user give prefix argument so that
  756. ;; s/he can edit the full name field in prompter if s/he wants.
  757. (setq mailing-address
  758. (read-string "Mailing address: " mailing-address)))
  759. ;; If file starts with a copyright and permission notice, skip them.
  760. ;; Assume they end at first blank line.
  761. (when (looking-at "Copyright")
  762. (search-forward "\n\n")
  763. (skip-chars-forward "\n"))
  764. ;; Advance into first entry if it is usable; else make new one.
  765. (let ((new-entries
  766. (mapcar (lambda (addr)
  767. (concat
  768. (if (stringp add-log-time-zone-rule)
  769. (let ((tz (getenv "TZ")))
  770. (unwind-protect
  771. (progn
  772. (setenv "TZ" add-log-time-zone-rule)
  773. (funcall add-log-time-format))
  774. (setenv "TZ" tz)))
  775. (funcall add-log-time-format))
  776. " " full-name
  777. " <" addr ">"))
  778. (if (consp mailing-address)
  779. mailing-address
  780. (list mailing-address)))))
  781. (if (and (not add-log-always-start-new-record)
  782. (let ((hit nil))
  783. (dolist (entry new-entries hit)
  784. (and (looking-at (regexp-quote entry))
  785. ;; Reject multiple author entries. (Bug#8645)
  786. (save-excursion
  787. (forward-line 1)
  788. (not (looking-at "[ \t]+.*<.*>$")))
  789. (setq hit t)))))
  790. (forward-line 1)
  791. (insert (nth (random (length new-entries))
  792. new-entries)
  793. (if use-hard-newlines hard-newline "\n")
  794. (if use-hard-newlines hard-newline "\n"))
  795. (forward-line -1))))
  796. ;; Determine where we should stop searching for a usable
  797. ;; item to add to, within this entry.
  798. (let ((bound
  799. (save-excursion
  800. (if (looking-at "\n*[^\n* \t]")
  801. (skip-chars-forward "\n")
  802. (if add-log-keep-changes-together
  803. (forward-page) ; page delimits entries for date
  804. (forward-paragraph))) ; paragraph delimits entries for file
  805. (point))))
  806. ;; Now insert the new line for this item.
  807. (cond ((re-search-forward "^\\s *\\* *$" bound t)
  808. ;; Put this file name into the existing empty item.
  809. (if item
  810. (insert item)))
  811. ((and (not new-entry)
  812. (let (case-fold-search)
  813. (re-search-forward
  814. (concat (regexp-quote (concat "* " item))
  815. ;; Don't accept `foo.bar' when
  816. ;; looking for `foo':
  817. "\\(\\s \\|[(),:]\\)")
  818. bound t)))
  819. ;; Add to the existing item for the same file.
  820. (re-search-forward "^\\s *$\\|^\\s \\*")
  821. (goto-char (match-beginning 0))
  822. ;; Delete excess empty lines; make just 2.
  823. (while (and (not (eobp)) (looking-at "^\\s *$"))
  824. (delete-region (point) (line-beginning-position 2)))
  825. (insert (if use-hard-newlines hard-newline "\n")
  826. (if use-hard-newlines hard-newline "\n"))
  827. (forward-line -2)
  828. (indent-relative-maybe))
  829. (t
  830. ;; Make a new item.
  831. (while (looking-at "\\sW")
  832. (forward-line 1))
  833. (while (and (not (eobp)) (looking-at "^\\s *$"))
  834. (delete-region (point) (line-beginning-position 2)))
  835. (insert (if use-hard-newlines hard-newline "\n")
  836. (if use-hard-newlines hard-newline "\n")
  837. (if use-hard-newlines hard-newline "\n"))
  838. (forward-line -2)
  839. (indent-to left-margin)
  840. (insert "* ")
  841. (if item (insert item)))))
  842. ;; Now insert the function name, if we have one.
  843. ;; Point is at the item for this file,
  844. ;; either at the end of the line or at the first blank line.
  845. (if (not defun)
  846. ;; No function name, so put in a colon unless we have just a star.
  847. (unless (save-excursion
  848. (beginning-of-line 1)
  849. (looking-at "\\s *\\(\\* *\\)?$"))
  850. (insert ": ")
  851. (if version (insert version ?\s)))
  852. ;; Make it easy to get rid of the function name.
  853. (undo-boundary)
  854. (unless (save-excursion
  855. (beginning-of-line 1)
  856. (looking-at "\\s *$"))
  857. (insert ?\s))
  858. ;; See if the prev function name has a message yet or not.
  859. ;; If not, merge the two items.
  860. (let ((pos (point-marker)))
  861. (skip-syntax-backward " ")
  862. (skip-chars-backward "):")
  863. (if (and (not put-new-entry-on-new-line)
  864. (looking-at "):")
  865. (let ((pos (save-excursion (backward-sexp 1) (point))))
  866. (when (equal (buffer-substring pos (point)) defun)
  867. (delete-region pos (point)))
  868. (> fill-column (+ (current-column) (length defun) 4))))
  869. (progn (skip-chars-backward ", ")
  870. (delete-region (point) pos)
  871. (unless (memq (char-before) '(?\()) (insert ", ")))
  872. (when (and (not put-new-entry-on-new-line) (looking-at "):"))
  873. (delete-region (+ 1 (point)) (line-end-position)))
  874. (goto-char pos)
  875. (insert "("))
  876. (set-marker pos nil))
  877. (insert defun "): ")
  878. (if version (insert version ?\s)))))
  879. ;;;###autoload
  880. (defun add-change-log-entry-other-window (&optional whoami file-name)
  881. "Find change log file in other window and add entry and item.
  882. This is just like `add-change-log-entry' except that it displays
  883. the change log file in another window."
  884. (interactive (if current-prefix-arg
  885. (list current-prefix-arg
  886. (prompt-for-change-log-name))))
  887. (add-change-log-entry whoami file-name t))
  888. (defvar change-log-indent-text 0)
  889. (defun change-log-fill-parenthesized-list ()
  890. ;; Fill parenthesized lists of names according to GNU standards.
  891. ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
  892. ;; should be filled as
  893. ;; * file-name.ext (very-long-foo, very-long-bar)
  894. ;; (very-long-foobar):
  895. (save-excursion
  896. (end-of-line 0)
  897. (skip-chars-backward " \t")
  898. (when (and (equal (char-before) ?\,)
  899. (> (point) (1+ (point-min))))
  900. (condition-case nil
  901. (when (save-excursion
  902. (and (prog2
  903. (up-list -1)
  904. (equal (char-after) ?\()
  905. (skip-chars-backward " \t"))
  906. (or (bolp)
  907. ;; Skip everything but a whitespace or asterisk.
  908. (and (not (zerop (skip-chars-backward "^ \t\n*")))
  909. (skip-chars-backward " \t")
  910. ;; We want one asterisk here.
  911. (= (skip-chars-backward "*") -1)
  912. (skip-chars-backward " \t")
  913. (bolp)))))
  914. ;; Delete the comma.
  915. (delete-char -1)
  916. ;; Close list on previous line.
  917. (insert ")")
  918. (skip-chars-forward " \t\n")
  919. ;; Start list on new line.
  920. (insert-before-markers "("))
  921. (error nil)))))
  922. (defun change-log-indent ()
  923. (change-log-fill-parenthesized-list)
  924. (let* ((indent
  925. (save-excursion
  926. (beginning-of-line)
  927. (skip-chars-forward " \t")
  928. (cond
  929. ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
  930. ;; Matching the output of add-log-time-format is difficult,
  931. ;; but I'll get it has at least two adjacent digits.
  932. (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
  933. 0)
  934. ((looking-at "[^*(]")
  935. (+ (current-left-margin) change-log-indent-text))
  936. (t (current-left-margin)))))
  937. (pos (save-excursion (indent-line-to indent) (point))))
  938. (if (> pos (point)) (goto-char pos))))
  939. (defvar smerge-resolve-function)
  940. (defvar copyright-at-end-flag)
  941. ;;;###autoload
  942. (define-derived-mode change-log-mode text-mode "Change Log"
  943. "Major mode for editing change logs; like Indented Text mode.
  944. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  945. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  946. Each entry behaves as a paragraph, and the entries for one day as a page.
  947. Runs `change-log-mode-hook'.
  948. \n\\{change-log-mode-map}"
  949. (setq left-margin 8
  950. fill-column 74
  951. indent-tabs-mode t
  952. tab-width 8
  953. show-trailing-whitespace t)
  954. (set (make-local-variable 'fill-forward-paragraph-function)
  955. 'change-log-fill-forward-paragraph)
  956. ;; Make sure we call `change-log-indent' when filling.
  957. (set (make-local-variable 'fill-indent-according-to-mode) t)
  958. ;; Avoid that filling leaves behind a single "*" on a line.
  959. (add-hook 'fill-nobreak-predicate
  960. (lambda ()
  961. (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
  962. nil t)
  963. (set (make-local-variable 'indent-line-function) 'change-log-indent)
  964. (set (make-local-variable 'tab-always-indent) nil)
  965. (set (make-local-variable 'copyright-at-end-flag) t)
  966. ;; We really do want "^" in paragraph-start below: it is only the
  967. ;; lines that begin at column 0 (despite the left-margin of 8) that
  968. ;; we are looking for. Adding `* ' allows eliding the blank line
  969. ;; between entries for different files.
  970. (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
  971. (set (make-local-variable 'paragraph-separate) paragraph-start)
  972. ;; Match null string on the date-line so that the date-line
  973. ;; is grouped with what follows.
  974. (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  975. (set (make-local-variable 'version-control) 'never)
  976. (set (make-local-variable 'smerge-resolve-function)
  977. 'change-log-resolve-conflict)
  978. (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  979. (set (make-local-variable 'font-lock-defaults)
  980. '(change-log-font-lock-keywords t nil nil backward-paragraph))
  981. (set (make-local-variable 'multi-isearch-next-buffer-function)
  982. 'change-log-next-buffer)
  983. (set (make-local-variable 'beginning-of-defun-function)
  984. 'change-log-beginning-of-defun)
  985. (set (make-local-variable 'end-of-defun-function)
  986. 'change-log-end-of-defun)
  987. ;; next-error function glue
  988. (setq next-error-function 'change-log-next-error)
  989. (setq next-error-last-buffer (current-buffer)))
  990. (defun change-log-next-buffer (&optional buffer wrap)
  991. "Return the next buffer in the series of ChangeLog file buffers.
  992. This function is used for multiple buffers isearch.
  993. A sequence of buffers is formed by ChangeLog files with decreasing
  994. numeric file name suffixes in the directory of the initial ChangeLog
  995. file were isearch was started."
  996. (let* ((name (change-log-name))
  997. (files (cons name (sort (file-expand-wildcards
  998. (concat name "[-.][0-9]*"))
  999. (lambda (a b)
  1000. ;; The file's extension may not have a valid
  1001. ;; version form (e.g. VC backup revisions).
  1002. (ignore-errors
  1003. (version< (substring b (length name))
  1004. (substring a (length name))))))))
  1005. (files (if isearch-forward files (reverse files))))
  1006. (find-file-noselect
  1007. (if wrap
  1008. (car files)
  1009. (cadr (member (file-name-nondirectory (buffer-file-name buffer))
  1010. files))))))
  1011. (defun change-log-fill-forward-paragraph (n)
  1012. "Cut paragraphs so filling preserves open parentheses at beginning of lines."
  1013. (let (;; Add lines starting with whitespace followed by a left paren or an
  1014. ;; asterisk.
  1015. (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)")))
  1016. (forward-paragraph n)))
  1017. (defcustom add-log-current-defun-header-regexp
  1018. "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
  1019. "Heuristic regexp used by `add-log-current-defun' for unknown major modes.
  1020. The regexp's first submatch is placed in the ChangeLog entry, in
  1021. parentheses."
  1022. :type 'regexp
  1023. :group 'change-log)
  1024. ;;;###autoload
  1025. (defvar add-log-lisp-like-modes
  1026. '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
  1027. "*Modes that look like Lisp to `add-log-current-defun'.")
  1028. ;;;###autoload
  1029. (defvar add-log-c-like-modes
  1030. '(c-mode c++-mode c++-c-mode objc-mode)
  1031. "*Modes that look like C to `add-log-current-defun'.")
  1032. ;;;###autoload
  1033. (defvar add-log-tex-like-modes
  1034. '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
  1035. "*Modes that look like TeX to `add-log-current-defun'.")
  1036. (declare-function c-cpp-define-name "cc-cmds" ())
  1037. (declare-function c-defun-name "cc-cmds" ())
  1038. ;;;###autoload
  1039. (defun add-log-current-defun ()
  1040. "Return name of function definition point is in, or nil.
  1041. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  1042. Texinfo (@node titles) and Perl.
  1043. Other modes are handled by a heuristic that looks in the 10K before
  1044. point for uppercase headings starting in the first column or
  1045. identifiers followed by `:' or `='. See variables
  1046. `add-log-current-defun-header-regexp' and
  1047. `add-log-current-defun-function'.
  1048. Has a preference of looking backwards."
  1049. (condition-case nil
  1050. (save-excursion
  1051. (let ((location (point)))
  1052. (cond (add-log-current-defun-function
  1053. (funcall add-log-current-defun-function))
  1054. ((apply 'derived-mode-p add-log-lisp-like-modes)
  1055. ;; If we are now precisely at the beginning of a defun,
  1056. ;; make sure beginning-of-defun finds that one
  1057. ;; rather than the previous one.
  1058. (or (eobp) (forward-char 1))
  1059. (beginning-of-defun)
  1060. ;; Make sure we are really inside the defun found,
  1061. ;; not after it.
  1062. (when (and (looking-at "\\s(")
  1063. (progn (end-of-defun)
  1064. (< location (point)))
  1065. (progn (forward-sexp -1)
  1066. (>= location (point))))
  1067. (if (looking-at "\\s(")
  1068. (forward-char 1))
  1069. ;; Skip the defining construct name, typically "defun"
  1070. ;; or "defvar".
  1071. (forward-sexp 1)
  1072. ;; The second element is usually a symbol being defined.
  1073. ;; If it is not, use the first symbol in it.
  1074. (skip-chars-forward " \t\n'(")
  1075. (buffer-substring-no-properties (point)
  1076. (progn (forward-sexp 1)
  1077. (point)))))
  1078. ((apply 'derived-mode-p add-log-c-like-modes)
  1079. (or (c-cpp-define-name)
  1080. (c-defun-name)))
  1081. ((apply #'derived-mode-p add-log-tex-like-modes)
  1082. (if (re-search-backward
  1083. "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
  1084. nil t)
  1085. (progn
  1086. (goto-char (match-beginning 0))
  1087. (buffer-substring-no-properties
  1088. (1+ (point)) ; without initial backslash
  1089. (line-end-position)))))
  1090. ((derived-mode-p 'texinfo-mode)
  1091. (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
  1092. (match-string-no-properties 1)))
  1093. ((derived-mode-p 'perl-mode 'cperl-mode)
  1094. (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
  1095. (match-string-no-properties 1)))
  1096. ;; Emacs's autoconf-mode installs its own
  1097. ;; `add-log-current-defun-function'. This applies to
  1098. ;; a different mode apparently for editing .m4
  1099. ;; autoconf source.
  1100. ((derived-mode-p 'autoconf-mode)
  1101. (if (re-search-backward
  1102. "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
  1103. (match-string-no-properties 3)))
  1104. (t
  1105. ;; If all else fails, try heuristics
  1106. (let (case-fold-search
  1107. result)
  1108. (end-of-line)
  1109. (when (re-search-backward
  1110. add-log-current-defun-header-regexp
  1111. (- (point) 10000)
  1112. t)
  1113. (setq result (or (match-string-no-properties 1)
  1114. (match-string-no-properties 0)))
  1115. ;; Strip whitespace away
  1116. (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
  1117. result)
  1118. (setq result (match-string-no-properties 1 result)))
  1119. result))))))
  1120. (error nil)))
  1121. (defvar change-log-get-method-definition-md)
  1122. ;; Subroutine used within change-log-get-method-definition.
  1123. ;; Add the last match in the buffer to the end of `md',
  1124. ;; followed by the string END; move to the end of that match.
  1125. (defun change-log-get-method-definition-1 (end)
  1126. (setq change-log-get-method-definition-md
  1127. (concat change-log-get-method-definition-md
  1128. (match-string 1)
  1129. end))
  1130. (goto-char (match-end 0)))
  1131. (defun change-log-get-method-definition ()
  1132. "For Objective C, return the method name if we are in a method."
  1133. (let ((change-log-get-method-definition-md "["))
  1134. (save-excursion
  1135. (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
  1136. (change-log-get-method-definition-1 " ")))
  1137. (save-excursion
  1138. (cond
  1139. ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
  1140. (change-log-get-method-definition-1 "")
  1141. (while (not (looking-at "[{;]"))
  1142. (looking-at
  1143. "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
  1144. (change-log-get-method-definition-1 ""))
  1145. (concat change-log-get-method-definition-md "]"))))))
  1146. (autoload 'timezone-make-date-sortable "timezone")
  1147. (defun change-log-sortable-date-at ()
  1148. "Return date of log entry in a consistent form for sorting.
  1149. Point is assumed to be at the start of the entry."
  1150. (if (looking-at change-log-start-entry-re)
  1151. (let ((date (match-string-no-properties 0)))
  1152. (if date
  1153. (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
  1154. (concat (match-string 1 date) (match-string 2 date)
  1155. (match-string 3 date))
  1156. (ignore-errors (timezone-make-date-sortable date)))))
  1157. (error "Bad date")))
  1158. (defun change-log-resolve-conflict ()
  1159. "Function to be used in `smerge-resolve-function'."
  1160. (save-excursion
  1161. (save-restriction
  1162. (narrow-to-region (match-beginning 0) (match-end 0))
  1163. (let ((mb1 (match-beginning 1))
  1164. (me1 (match-end 1))
  1165. (mb3 (match-beginning 3))
  1166. (me3 (match-end 3))
  1167. (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
  1168. (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
  1169. (unwind-protect
  1170. (let ((buf (current-buffer)))
  1171. (with-current-buffer tmp1
  1172. (change-log-mode)
  1173. (insert-buffer-substring buf mb1 me1))
  1174. (with-current-buffer tmp2
  1175. (change-log-mode)
  1176. (insert-buffer-substring buf mb3 me3)
  1177. ;; Do the merge here instead of inside `buf' so as to be
  1178. ;; more robust in case change-log-merge fails.
  1179. (change-log-merge tmp1))
  1180. (goto-char (point-max))
  1181. (delete-region (point-min)
  1182. (prog1 (point)
  1183. (insert-buffer-substring tmp2))))
  1184. (kill-buffer tmp1)
  1185. (kill-buffer tmp2))))))
  1186. ;;;###autoload
  1187. (defun change-log-merge (other-log)
  1188. "Merge the contents of change log file OTHER-LOG with this buffer.
  1189. Both must be found in Change Log mode (since the merging depends on
  1190. the appropriate motion commands). OTHER-LOG can be either a file name
  1191. or a buffer.
  1192. Entries are inserted in chronological order. Both the current and
  1193. old-style time formats for entries are supported."
  1194. (interactive "*fLog file name to merge: ")
  1195. (if (not (derived-mode-p 'change-log-mode))
  1196. (error "Not in Change Log mode"))
  1197. (let ((other-buf (if (bufferp other-log) other-log
  1198. (find-file-noselect other-log)))
  1199. (buf (current-buffer))
  1200. date1 start end)
  1201. (save-excursion
  1202. (goto-char (point-min))
  1203. (set-buffer other-buf)
  1204. (goto-char (point-min))
  1205. (if (not (derived-mode-p 'change-log-mode))
  1206. (error "%s not found in Change Log mode" other-log))
  1207. ;; Loop through all the entries in OTHER-LOG.
  1208. (while (not (eobp))
  1209. (setq date1 (change-log-sortable-date-at))
  1210. (setq start (point)
  1211. end (progn (forward-page) (point)))
  1212. ;; Look for an entry in original buffer that isn't later.
  1213. (with-current-buffer buf
  1214. (while (and (not (eobp))
  1215. (string< date1 (change-log-sortable-date-at)))
  1216. (forward-page))
  1217. (if (not (eobp))
  1218. (insert-buffer-substring other-buf start end)
  1219. ;; At the end of the original buffer, insert a newline to
  1220. ;; separate entries and then the rest of the file being
  1221. ;; merged.
  1222. (unless (or (bobp)
  1223. (and (= ?\n (char-before))
  1224. (or (<= (1- (point)) (point-min))
  1225. (= ?\n (char-before (1- (point)))))))
  1226. (insert (if use-hard-newlines hard-newline "\n")))
  1227. ;; Move to the end of it to terminate outer loop.
  1228. (with-current-buffer other-buf
  1229. (goto-char (point-max)))
  1230. (insert-buffer-substring other-buf start)))))))
  1231. (defun change-log-beginning-of-defun ()
  1232. (re-search-backward change-log-start-entry-re nil 'move))
  1233. (defun change-log-end-of-defun ()
  1234. ;; Look back and if there is no entry there it means we are before
  1235. ;; the first ChangeLog entry, so go forward until finding one.
  1236. (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
  1237. (re-search-forward change-log-start-entry-re nil t))
  1238. ;; In case we are at the end of log entry going forward a line will
  1239. ;; make us find the next entry when searching. If we are inside of
  1240. ;; an entry going forward a line will still keep the point inside
  1241. ;; the same entry.
  1242. (forward-line 1)
  1243. ;; In case we are at the beginning of an entry, move past it.
  1244. (when (looking-at change-log-start-entry-re)
  1245. (goto-char (match-end 0))
  1246. (forward-line 1))
  1247. ;; Search for the start of the next log entry. Go to the end of the
  1248. ;; buffer if we could not find a next entry.
  1249. (when (re-search-forward change-log-start-entry-re nil 'move)
  1250. (goto-char (match-beginning 0))
  1251. (forward-line -1)))
  1252. (provide 'add-log)
  1253. ;;; add-log.el ends here