| 1 | ;;; add-log.el --- change log maintenance commands for Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985, 86, 88, 93, 94, 97, 98, 2000 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Keywords: tools |
| 6 | |
| 7 | ;; This file is part of GNU Emacs. |
| 8 | |
| 9 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 10 | ;; it under the terms of the GNU General Public License as published by |
| 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 12 | ;; any later version. |
| 13 | |
| 14 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | ;; GNU General Public License for more details. |
| 18 | |
| 19 | ;; You should have received a copy of the GNU General Public License |
| 20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | ;; Boston, MA 02111-1307, USA. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; This facility is documented in the Emacs Manual. |
| 27 | |
| 28 | ;;; Code: |
| 29 | |
| 30 | (eval-when-compile |
| 31 | (require 'timezone)) |
| 32 | |
| 33 | (defgroup change-log nil |
| 34 | "Change log maintenance" |
| 35 | :group 'tools |
| 36 | :link '(custom-manual "(emacs)Change Log") |
| 37 | :prefix "change-log-" |
| 38 | :prefix "add-log-") |
| 39 | |
| 40 | |
| 41 | (defcustom change-log-default-name nil |
| 42 | "*Name of a change log file for \\[add-change-log-entry]." |
| 43 | :type '(choice (const :tag "default" nil) |
| 44 | string) |
| 45 | :group 'change-log) |
| 46 | |
| 47 | (defcustom change-log-mode-hook nil |
| 48 | "Normal hook run by `change-log-mode'." |
| 49 | :type 'hook |
| 50 | :group 'change-log) |
| 51 | |
| 52 | (defcustom add-log-current-defun-function nil |
| 53 | "*If non-nil, function to guess name of surrounding function. |
| 54 | It is used by `add-log-current-defun' in preference to built-in rules. |
| 55 | Returns function's name as a string, or nil if outside a function." |
| 56 | :type 'function |
| 57 | :group 'change-log) |
| 58 | |
| 59 | ;;;###autoload |
| 60 | (defcustom add-log-full-name nil |
| 61 | "*Full name of user, for inclusion in ChangeLog daily headers. |
| 62 | This defaults to the value returned by the function `user-full-name'." |
| 63 | :type '(choice (const :tag "Default" nil) |
| 64 | string) |
| 65 | :group 'change-log) |
| 66 | |
| 67 | ;;;###autoload |
| 68 | (defcustom add-log-mailing-address nil |
| 69 | "*Electronic mail address of user, for inclusion in ChangeLog daily headers. |
| 70 | This defaults to the value of `user-mail-address'." |
| 71 | :type '(choice (const :tag "Default" nil) |
| 72 | string) |
| 73 | :group 'change-log) |
| 74 | |
| 75 | (defcustom add-log-time-format 'add-log-iso8601-time-string |
| 76 | "*Function that defines the time format. |
| 77 | For example, `add-log-iso8601-time-string', which gives the |
| 78 | date in international ISO 8601 format, |
| 79 | and `current-time-string' are two valid values." |
| 80 | :type '(radio (const :tag "International ISO 8601 format" |
| 81 | add-log-iso8601-time-string) |
| 82 | (const :tag "Old format, as returned by `current-time-string'" |
| 83 | current-time-string) |
| 84 | (function :tag "Other")) |
| 85 | :group 'change-log) |
| 86 | |
| 87 | (defcustom add-log-keep-changes-together nil |
| 88 | "*If non-nil, normally keep day's log entries for one file together. |
| 89 | |
| 90 | Log entries for a given file made with \\[add-change-log-entry] or |
| 91 | \\[add-change-log-entry-other-window] will only be added to others \ |
| 92 | for that file made |
| 93 | today if this variable is non-nil or that file comes first in today's |
| 94 | entries. Otherwise another entry for that file will be started. An |
| 95 | original log: |
| 96 | |
| 97 | * foo (...): ... |
| 98 | * bar (...): change 1 |
| 99 | |
| 100 | in the latter case, \\[add-change-log-entry-other-window] in a \ |
| 101 | buffer visiting `bar', yields: |
| 102 | |
| 103 | * bar (...): -!- |
| 104 | * foo (...): ... |
| 105 | * bar (...): change 1 |
| 106 | |
| 107 | and in the former: |
| 108 | |
| 109 | * foo (...): ... |
| 110 | * bar (...): change 1 |
| 111 | (...): -!- |
| 112 | |
| 113 | The NEW-ENTRY arg to `add-change-log-entry' can override the effect of |
| 114 | this variable." |
| 115 | :version "20.3" |
| 116 | :type 'boolean |
| 117 | :group 'change-log) |
| 118 | |
| 119 | (defcustom add-log-buffer-file-name-function nil |
| 120 | "*If non-nil, function to call to identify the full filename of a buffer. |
| 121 | This function is called with no argument. If this is nil, the default is to |
| 122 | use `buffer-file-name'." |
| 123 | :type 'function |
| 124 | :group 'change-log) |
| 125 | |
| 126 | (defcustom add-log-file-name-function nil |
| 127 | "*If non-nil, function to call to identify the filename for a ChangeLog entry. |
| 128 | This function is called with one argument, the value of variable |
| 129 | `buffer-file-name' in that buffer. If this is nil, the default is to |
| 130 | use the file's name relative to the directory of the change log file." |
| 131 | :type 'function |
| 132 | :group 'change-log) |
| 133 | |
| 134 | |
| 135 | (defcustom change-log-version-info-enabled nil |
| 136 | "*If non-nil, enable recording version numbers with the changes." |
| 137 | :version "21.1" |
| 138 | :type 'boolean |
| 139 | :group 'change-log) |
| 140 | |
| 141 | (defcustom change-log-version-number-regexp-list |
| 142 | (let ((re "\\([0-9]+\.[0-9.]+\\)")) |
| 143 | (list |
| 144 | ;; (defconst ad-version "2.15" |
| 145 | (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re) |
| 146 | ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp |
| 147 | (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re))) |
| 148 | "*List of regexps to search for version number. |
| 149 | The version number must be in group 1. |
| 150 | Note: The search is conducted only within 10%, at the beginning of the file." |
| 151 | :version "21.1" |
| 152 | :type '(repeat regexp) |
| 153 | :group 'change-log) |
| 154 | |
| 155 | (defface change-log-date-face |
| 156 | '((t (:inherit font-lock-string-face))) |
| 157 | "Face used to highlight dates in date lines." |
| 158 | :version "21.1" |
| 159 | :group 'change-log) |
| 160 | |
| 161 | (defface change-log-name-face |
| 162 | '((t (:inherit font-lock-constant-face))) |
| 163 | "Face for highlighting author names." |
| 164 | :version "21.1" |
| 165 | :group 'change-log) |
| 166 | |
| 167 | (defface change-log-email-face |
| 168 | '((t (:inherit font-lock-variable-name-face))) |
| 169 | "Face for highlighting author email addresses." |
| 170 | :version "21.1" |
| 171 | :group 'change-log) |
| 172 | |
| 173 | (defface change-log-file-face |
| 174 | '((t (:inherit font-lock-function-name-face))) |
| 175 | "Face for highlighting file names." |
| 176 | :version "21.1" |
| 177 | :group 'change-log) |
| 178 | |
| 179 | (defface change-log-list-face |
| 180 | '((t (:inherit font-lock-keyword-face))) |
| 181 | "Face for highlighting parenthesized lists of functions or variables." |
| 182 | :version "21.1" |
| 183 | :group 'change-log) |
| 184 | |
| 185 | (defface change-log-conditionals-face |
| 186 | '((t (:inherit font-lock-variable-name-face))) |
| 187 | "Face for highlighting conditionals of the form `[...]'." |
| 188 | :version "21.1" |
| 189 | :group 'change-log) |
| 190 | |
| 191 | (defface change-log-function-face |
| 192 | '((t (:inherit font-lock-variable-name-face))) |
| 193 | "Face for highlighting items of the form `<....>'." |
| 194 | :version "21.1" |
| 195 | :group 'change-log) |
| 196 | |
| 197 | (defface change-log-acknowledgement-face |
| 198 | '((t (:inherit font-lock-comment-face))) |
| 199 | "Face for highlighting acknowledgments." |
| 200 | :version "21.1" |
| 201 | :group 'change-log) |
| 202 | |
| 203 | (defvar change-log-font-lock-keywords |
| 204 | '(;; |
| 205 | ;; Date lines, new and old styles. |
| 206 | ("^\\sw.........[0-9:+ ]*" |
| 207 | (0 'change-log-date-face) |
| 208 | ;; Name and e-mail; some people put e-mail in parens, not angles. |
| 209 | ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil |
| 210 | (1 'change-log-name-face) |
| 211 | (2 'change-log-email-face))) |
| 212 | ;; |
| 213 | ;; File names. |
| 214 | ("^\t\\* \\([^ ,:([\n]+\\)" |
| 215 | (1 'change-log-file-face) |
| 216 | ;; Possibly further names in a list: |
| 217 | ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file-face)) |
| 218 | ;; Possibly a parenthesized list of names: |
| 219 | ("\\= (\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face)) |
| 220 | ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face))) |
| 221 | ;; |
| 222 | ;; Function or variable names. |
| 223 | ("^\t(\\([^) ,:\n]+\\)" |
| 224 | (1 'change-log-list-face) |
| 225 | ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face))) |
| 226 | ;; |
| 227 | ;; Conditionals. |
| 228 | ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals-face)) |
| 229 | ;; |
| 230 | ;; Function of change. |
| 231 | ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function-face)) |
| 232 | ;; |
| 233 | ;; Acknowledgements. |
| 234 | ("\\(^\t\\| \\)\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)" |
| 235 | 2 'change-log-acknowledgement-face)) |
| 236 | "Additional expressions to highlight in Change Log mode.") |
| 237 | |
| 238 | (defvar change-log-mode-map (make-sparse-keymap) |
| 239 | "Keymap for Change Log major mode.") |
| 240 | |
| 241 | (defvar change-log-time-zone-rule nil |
| 242 | "Time zone used for calculating change log time stamps. |
| 243 | It takes the same format as the TZ argument of `set-time-zone-rule'. |
| 244 | If nil, use local time.") |
| 245 | |
| 246 | (defun add-log-iso8601-time-zone (time) |
| 247 | (let* ((utc-offset (or (car (current-time-zone time)) 0)) |
| 248 | (sign (if (< utc-offset 0) ?- ?+)) |
| 249 | (sec (abs utc-offset)) |
| 250 | (ss (% sec 60)) |
| 251 | (min (/ sec 60)) |
| 252 | (mm (% min 60)) |
| 253 | (hh (/ min 60))) |
| 254 | (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d") |
| 255 | ((not (zerop mm)) "%c%02d:%02d") |
| 256 | (t "%c%02d")) |
| 257 | sign hh mm ss))) |
| 258 | |
| 259 | (defun add-log-iso8601-time-string () |
| 260 | (if change-log-time-zone-rule |
| 261 | (let ((tz (getenv "TZ")) |
| 262 | (now (current-time))) |
| 263 | (unwind-protect |
| 264 | (progn |
| 265 | (set-time-zone-rule change-log-time-zone-rule) |
| 266 | (concat |
| 267 | (format-time-string "%Y-%m-%d " now) |
| 268 | (add-log-iso8601-time-zone now))) |
| 269 | (set-time-zone-rule tz))) |
| 270 | (format-time-string "%Y-%m-%d"))) |
| 271 | |
| 272 | (defun change-log-name () |
| 273 | "Return (system-dependent) default name for a change log file." |
| 274 | (or change-log-default-name |
| 275 | (if (eq system-type 'vax-vms) |
| 276 | "$CHANGE_LOG$.TXT" |
| 277 | "ChangeLog"))) |
| 278 | |
| 279 | ;;;###autoload |
| 280 | (defun prompt-for-change-log-name () |
| 281 | "Prompt for a change log name." |
| 282 | (let* ((default (change-log-name)) |
| 283 | (name (expand-file-name |
| 284 | (read-file-name (format "Log file (default %s): " default) |
| 285 | nil default)))) |
| 286 | ;; Handle something that is syntactically a directory name. |
| 287 | ;; Look for ChangeLog or whatever in that directory. |
| 288 | (if (string= (file-name-nondirectory name) "") |
| 289 | (expand-file-name (file-name-nondirectory default) |
| 290 | name) |
| 291 | ;; Handle specifying a file that is a directory. |
| 292 | (if (file-directory-p name) |
| 293 | (expand-file-name (file-name-nondirectory default) |
| 294 | (file-name-as-directory name)) |
| 295 | name)))) |
| 296 | |
| 297 | (defun change-log-version-number-search () |
| 298 | "Return version number of current buffer's file. |
| 299 | This is the value returned by `vc-workfile-version' or, if that is |
| 300 | nil, by matching `change-log-version-number-regexp-list'." |
| 301 | (let* ((size (buffer-size)) |
| 302 | (end |
| 303 | ;; The version number can be anywhere in the file, but |
| 304 | ;; restrict search to the file beginning: 10% should be |
| 305 | ;; enough to prevent some mishits. |
| 306 | ;; |
| 307 | ;; Apply percentage only if buffer size is bigger than |
| 308 | ;; approx 100 lines. |
| 309 | (if (> size (* 100 80)) |
| 310 | (/ size 10) |
| 311 | size)) |
| 312 | version) |
| 313 | (or (and buffer-file-name (vc-workfile-version buffer-file-name)) |
| 314 | (save-restriction |
| 315 | (widen) |
| 316 | (let ((regexps change-log-version-number-regexp-list)) |
| 317 | (while regexps |
| 318 | (save-excursion |
| 319 | (goto-char (point-min)) |
| 320 | (when (re-search-forward (pop regexps) end t) |
| 321 | (setq version (match-string 1) |
| 322 | regexps nil))))))))) |
| 323 | |
| 324 | |
| 325 | ;;;###autoload |
| 326 | (defun find-change-log (&optional file-name buffer-file) |
| 327 | "Find a change log file for \\[add-change-log-entry] and return the name. |
| 328 | |
| 329 | Optional arg FILE-NAME specifies the file to use. |
| 330 | If FILE-NAME is nil, use the value of `change-log-default-name'. |
| 331 | If 'change-log-default-name' is nil, behave as though it were 'ChangeLog' |
| 332 | \(or whatever we use on this operating system). |
| 333 | |
| 334 | If 'change-log-default-name' contains a leading directory component, then |
| 335 | simply find it in the current directory. Otherwise, search in the current |
| 336 | directory and its successive parents for a file so named. |
| 337 | |
| 338 | Once a file is found, `change-log-default-name' is set locally in the |
| 339 | current buffer to the complete file name. |
| 340 | Optional arg BUFFER-FILE overrides `buffer-file-name'." |
| 341 | ;; If user specified a file name or if this buffer knows which one to use, |
| 342 | ;; just use that. |
| 343 | (or file-name |
| 344 | (setq file-name (and change-log-default-name |
| 345 | (file-name-directory change-log-default-name) |
| 346 | change-log-default-name)) |
| 347 | (progn |
| 348 | ;; Chase links in the source file |
| 349 | ;; and use the change log in the dir where it points. |
| 350 | (setq file-name (or (and (or buffer-file buffer-file-name) |
| 351 | (file-name-directory |
| 352 | (file-chase-links |
| 353 | (or buffer-file buffer-file-name)))) |
| 354 | default-directory)) |
| 355 | (if (file-directory-p file-name) |
| 356 | (setq file-name (expand-file-name (change-log-name) file-name))) |
| 357 | ;; Chase links before visiting the file. |
| 358 | ;; This makes it easier to use a single change log file |
| 359 | ;; for several related directories. |
| 360 | (setq file-name (file-chase-links file-name)) |
| 361 | (setq file-name (expand-file-name file-name)) |
| 362 | ;; Move up in the dir hierarchy till we find a change log file. |
| 363 | (let ((file1 file-name) |
| 364 | parent-dir) |
| 365 | (while (and (not (or (get-file-buffer file1) (file-exists-p file1))) |
| 366 | (progn (setq parent-dir |
| 367 | (file-name-directory |
| 368 | (directory-file-name |
| 369 | (file-name-directory file1)))) |
| 370 | ;; Give up if we are already at the root dir. |
| 371 | (not (string= (file-name-directory file1) |
| 372 | parent-dir)))) |
| 373 | ;; Move up to the parent dir and try again. |
| 374 | (setq file1 (expand-file-name |
| 375 | (file-name-nondirectory (change-log-name)) |
| 376 | parent-dir))) |
| 377 | ;; If we found a change log in a parent, use that. |
| 378 | (if (or (get-file-buffer file1) (file-exists-p file1)) |
| 379 | (setq file-name file1))))) |
| 380 | ;; Make a local variable in this buffer so we needn't search again. |
| 381 | (set (make-local-variable 'change-log-default-name) file-name) |
| 382 | file-name) |
| 383 | |
| 384 | (defun add-log-file-name (buffer-file log-file) |
| 385 | ;; Never want to add a change log entry for the ChangeLog file itself. |
| 386 | (unless (or (null buffer-file) (string= buffer-file log-file)) |
| 387 | (if add-log-file-name-function |
| 388 | (funcall add-log-file-name-function buffer-file) |
| 389 | (setq buffer-file |
| 390 | (if (string-match |
| 391 | (concat "^" (regexp-quote (file-name-directory log-file))) |
| 392 | buffer-file) |
| 393 | (substring buffer-file (match-end 0)) |
| 394 | (file-name-nondirectory buffer-file))) |
| 395 | ;; If we have a backup file, it's presumably because we're |
| 396 | ;; comparing old and new versions (e.g. for deleted |
| 397 | ;; functions) and we'll want to use the original name. |
| 398 | (if (backup-file-name-p buffer-file) |
| 399 | (file-name-sans-versions buffer-file) |
| 400 | buffer-file)))) |
| 401 | |
| 402 | ;;;###autoload |
| 403 | (defun add-change-log-entry (&optional whoami file-name other-window new-entry) |
| 404 | "Find change log file and add an entry for today. |
| 405 | Optional arg WHOAMI (interactive prefix) non-nil means prompt for user |
| 406 | name and site. |
| 407 | |
| 408 | Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'. |
| 409 | Third arg OTHER-WINDOW non-nil means visit in other window. |
| 410 | Fourth arg NEW-ENTRY non-nil means always create a new entry at the front; |
| 411 | never append to an existing entry. Option `add-log-keep-changes-together' |
| 412 | otherwise affects whether a new entry is created. |
| 413 | |
| 414 | Today's date is calculated according to `change-log-time-zone-rule' if |
| 415 | non-nil, otherwise in local time." |
| 416 | (interactive (list current-prefix-arg |
| 417 | (prompt-for-change-log-name))) |
| 418 | (or add-log-full-name |
| 419 | (setq add-log-full-name (user-full-name))) |
| 420 | (or add-log-mailing-address |
| 421 | (setq add-log-mailing-address user-mail-address)) |
| 422 | (if whoami |
| 423 | (progn |
| 424 | (setq add-log-full-name (read-input "Full name: " add-log-full-name)) |
| 425 | ;; Note that some sites have room and phone number fields in |
| 426 | ;; full name which look silly when inserted. Rather than do |
| 427 | ;; anything about that here, let user give prefix argument so that |
| 428 | ;; s/he can edit the full name field in prompter if s/he wants. |
| 429 | (setq add-log-mailing-address |
| 430 | (read-input "Mailing address: " add-log-mailing-address)))) |
| 431 | |
| 432 | (let* ((defun (add-log-current-defun)) |
| 433 | (version (and change-log-version-info-enabled |
| 434 | (change-log-version-number-search))) |
| 435 | (buffer-file |
| 436 | (expand-file-name (if add-log-buffer-file-name-function |
| 437 | (funcall add-log-buffer-file-name-function) |
| 438 | buffer-file-name))) |
| 439 | (file-name (expand-file-name |
| 440 | (or file-name (find-change-log file-name buffer-file)))) |
| 441 | ;; Set ENTRY to the file name to use in the new entry. |
| 442 | (entry (add-log-file-name buffer-file file-name)) |
| 443 | bound) |
| 444 | |
| 445 | (if (or (and other-window (not (equal file-name buffer-file-name))) |
| 446 | (window-dedicated-p (selected-window))) |
| 447 | (find-file-other-window file-name) |
| 448 | (find-file file-name)) |
| 449 | (or (eq major-mode 'change-log-mode) |
| 450 | (change-log-mode)) |
| 451 | (undo-boundary) |
| 452 | (goto-char (point-min)) |
| 453 | (let ((new-entry (concat (funcall add-log-time-format) |
| 454 | " " add-log-full-name |
| 455 | " <" add-log-mailing-address ">"))) |
| 456 | (if (looking-at (regexp-quote new-entry)) |
| 457 | (forward-line 1) |
| 458 | (insert new-entry "\n\n"))) |
| 459 | |
| 460 | (setq bound |
| 461 | (progn |
| 462 | (if (looking-at "\n*[^\n* \t]") |
| 463 | (skip-chars-forward "\n") |
| 464 | (if add-log-keep-changes-together |
| 465 | (forward-page) ; page delimits entries for date |
| 466 | (forward-paragraph))) ; paragraph delimits entries for file |
| 467 | (point))) |
| 468 | (goto-char (point-min)) |
| 469 | ;; Now insert the new line for this entry. |
| 470 | (cond ((re-search-forward "^\\s *\\*\\s *$" bound t) |
| 471 | ;; Put this file name into the existing empty entry. |
| 472 | (if entry |
| 473 | (insert entry))) |
| 474 | ((and (not new-entry) |
| 475 | (let (case-fold-search) |
| 476 | (re-search-forward |
| 477 | (concat (regexp-quote (concat "* " entry)) |
| 478 | ;; Don't accept `foo.bar' when |
| 479 | ;; looking for `foo': |
| 480 | "\\(\\s \\|[(),:]\\)") |
| 481 | bound t))) |
| 482 | ;; Add to the existing entry for the same file. |
| 483 | (re-search-forward "^\\s *$\\|^\\s \\*") |
| 484 | (goto-char (match-beginning 0)) |
| 485 | ;; Delete excess empty lines; make just 2. |
| 486 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
| 487 | (delete-region (point) (line-beginning-position 2))) |
| 488 | (insert-char ?\n 2) |
| 489 | (forward-line -2) |
| 490 | (indent-relative-maybe)) |
| 491 | (t |
| 492 | ;; Make a new entry. |
| 493 | (forward-line 1) |
| 494 | (while (looking-at "\\sW") |
| 495 | (forward-line 1)) |
| 496 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
| 497 | (delete-region (point) (line-beginning-position 2))) |
| 498 | (insert-char ?\n 3) |
| 499 | (forward-line -2) |
| 500 | (indent-to left-margin) |
| 501 | (insert "* ") |
| 502 | (if entry (insert entry)))) |
| 503 | ;; Now insert the function name, if we have one. |
| 504 | ;; Point is at the entry for this file, |
| 505 | ;; either at the end of the line or at the first blank line. |
| 506 | (if defun |
| 507 | (progn |
| 508 | ;; Make it easy to get rid of the function name. |
| 509 | (undo-boundary) |
| 510 | (unless (save-excursion |
| 511 | (beginning-of-line 1) |
| 512 | (looking-at "\\s *$")) |
| 513 | (insert ?\ )) |
| 514 | ;; See if the prev function name has a message yet or not |
| 515 | ;; If not, merge the two entries. |
| 516 | (let ((pos (point-marker))) |
| 517 | (if (and (skip-syntax-backward " ") |
| 518 | (skip-chars-backward "):") |
| 519 | (looking-at "):") |
| 520 | (progn (delete-region (+ 1 (point)) (+ 2 (point))) t) |
| 521 | (> fill-column (+ (current-column) (length defun) 3))) |
| 522 | (progn (delete-region (point) pos) |
| 523 | (insert ", ")) |
| 524 | (goto-char pos) |
| 525 | (insert "(")) |
| 526 | (set-marker pos nil)) |
| 527 | (insert defun "): ") |
| 528 | (if version |
| 529 | (insert version ?\ ))) |
| 530 | ;; No function name, so put in a colon unless we have just a star. |
| 531 | (unless (save-excursion |
| 532 | (beginning-of-line 1) |
| 533 | (looking-at "\\s *\\(\\*\\s *\\)?$")) |
| 534 | (insert ": ") |
| 535 | (if version (insert version ?\ )))))) |
| 536 | |
| 537 | ;;;###autoload |
| 538 | (defun add-change-log-entry-other-window (&optional whoami file-name) |
| 539 | "Find change log file in other window and add an entry for today. |
| 540 | Optional arg WHOAMI (interactive prefix) non-nil means prompt for user |
| 541 | name and site. |
| 542 | Second optional arg FILE-NAME is file name of change log. |
| 543 | If nil, use `change-log-default-name'. |
| 544 | |
| 545 | Affected by the same options as `add-change-log-entry'." |
| 546 | (interactive (if current-prefix-arg |
| 547 | (list current-prefix-arg |
| 548 | (prompt-for-change-log-name)))) |
| 549 | (add-change-log-entry whoami file-name t)) |
| 550 | ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window) |
| 551 | |
| 552 | ;;;###autoload |
| 553 | (defun change-log-mode () |
| 554 | "Major mode for editing change logs; like Indented Text Mode. |
| 555 | Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74. |
| 556 | New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window]. |
| 557 | Each entry behaves as a paragraph, and the entries for one day as a page. |
| 558 | Runs `change-log-mode-hook'." |
| 559 | (interactive) |
| 560 | (kill-all-local-variables) |
| 561 | (indented-text-mode) |
| 562 | (setq major-mode 'change-log-mode |
| 563 | mode-name "Change Log" |
| 564 | left-margin 8 |
| 565 | fill-column 74 |
| 566 | indent-tabs-mode t |
| 567 | tab-width 8) |
| 568 | (use-local-map change-log-mode-map) |
| 569 | (set (make-local-variable 'fill-paragraph-function) |
| 570 | 'change-log-fill-paragraph) |
| 571 | ;; We really do want "^" in paragraph-start below: it is only the |
| 572 | ;; lines that begin at column 0 (despite the left-margin of 8) that |
| 573 | ;; we are looking for. Adding `* ' allows eliding the blank line |
| 574 | ;; between entries for different files. |
| 575 | (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<") |
| 576 | (set (make-local-variable 'paragraph-separate) paragraph-start) |
| 577 | ;; Match null string on the date-line so that the date-line |
| 578 | ;; is grouped with what follows. |
| 579 | (set (make-local-variable 'page-delimiter) "^\\<\\|^\f") |
| 580 | (set (make-local-variable 'version-control) 'never) |
| 581 | (set (make-local-variable 'adaptive-fill-regexp) "\\s *") |
| 582 | (set (make-local-variable 'font-lock-defaults) |
| 583 | '(change-log-font-lock-keywords t)) |
| 584 | (run-hooks 'change-log-mode-hook)) |
| 585 | |
| 586 | ;; It might be nice to have a general feature to replace this. The idea I |
| 587 | ;; have is a variable giving a regexp matching text which should not be |
| 588 | ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(". |
| 589 | ;; But I don't feel up to implementing that today. |
| 590 | (defun change-log-fill-paragraph (&optional justify) |
| 591 | "Fill the paragraph, but preserve open parentheses at beginning of lines. |
| 592 | Prefix arg means justify as well." |
| 593 | (interactive "P") |
| 594 | (let ((end (progn (forward-paragraph) (point))) |
| 595 | (beg (progn (backward-paragraph) (point))) |
| 596 | (paragraph-start (concat paragraph-start "\\|\\s *\\s("))) |
| 597 | (fill-region beg end justify) |
| 598 | t)) |
| 599 | \f |
| 600 | (defcustom add-log-current-defun-header-regexp |
| 601 | "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]" |
| 602 | "*Heuristic regexp used by `add-log-current-defun' for unknown major modes." |
| 603 | :type 'regexp |
| 604 | :group 'change-log) |
| 605 | |
| 606 | ;;;###autoload |
| 607 | (defvar add-log-lisp-like-modes |
| 608 | '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode) |
| 609 | "*Modes that look like Lisp to `add-log-current-defun'.") |
| 610 | |
| 611 | ;;;###autoload |
| 612 | (defvar add-log-c-like-modes |
| 613 | '(c-mode c++-mode c++-c-mode objc-mode) |
| 614 | "*Modes that look like C to `add-log-current-defun'.") |
| 615 | |
| 616 | ;;;###autoload |
| 617 | (defvar add-log-tex-like-modes |
| 618 | '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode) |
| 619 | "*Modes that look like TeX to `add-log-current-defun'.") |
| 620 | |
| 621 | ;;;###autoload |
| 622 | (defun add-log-current-defun () |
| 623 | "Return name of function definition point is in, or nil. |
| 624 | |
| 625 | Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...), |
| 626 | Texinfo (@node titles) and Perl. |
| 627 | |
| 628 | Other modes are handled by a heuristic that looks in the 10K before |
| 629 | point for uppercase headings starting in the first column or |
| 630 | identifiers followed by `:' or `='. See variables |
| 631 | `add-log-current-defun-header-regexp' and |
| 632 | `add-log-current-defun-function' |
| 633 | |
| 634 | Has a preference of looking backwards." |
| 635 | (condition-case nil |
| 636 | (save-excursion |
| 637 | (let ((location (point))) |
| 638 | (cond (add-log-current-defun-function |
| 639 | (funcall add-log-current-defun-function)) |
| 640 | ((memq major-mode add-log-lisp-like-modes) |
| 641 | ;; If we are now precisely at the beginning of a defun, |
| 642 | ;; make sure beginning-of-defun finds that one |
| 643 | ;; rather than the previous one. |
| 644 | (or (eobp) (forward-char 1)) |
| 645 | (beginning-of-defun) |
| 646 | ;; Make sure we are really inside the defun found, |
| 647 | ;; not after it. |
| 648 | (when (and (looking-at "\\s(") |
| 649 | (progn (end-of-defun) |
| 650 | (< location (point))) |
| 651 | (progn (forward-sexp -1) |
| 652 | (>= location (point)))) |
| 653 | (if (looking-at "\\s(") |
| 654 | (forward-char 1)) |
| 655 | ;; Skip the defining construct name, typically "defun" |
| 656 | ;; or "defvar". |
| 657 | (forward-sexp 1) |
| 658 | ;; The second element is usually a symbol being defined. |
| 659 | ;; If it is not, use the first symbol in it. |
| 660 | (skip-chars-forward " \t\n'(") |
| 661 | (buffer-substring-no-properties (point) |
| 662 | (progn (forward-sexp 1) |
| 663 | (point))))) |
| 664 | ((and (memq major-mode add-log-c-like-modes) |
| 665 | (save-excursion |
| 666 | (beginning-of-line) |
| 667 | ;; Use eq instead of = here to avoid |
| 668 | ;; error when at bob and char-after |
| 669 | ;; returns nil. |
| 670 | (while (eq (char-after (- (point) 2)) ?\\) |
| 671 | (forward-line -1)) |
| 672 | (looking-at "[ \t]*#[ \t]*define[ \t]"))) |
| 673 | ;; Handle a C macro definition. |
| 674 | (beginning-of-line) |
| 675 | (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above |
| 676 | (forward-line -1)) |
| 677 | (search-forward "define") |
| 678 | (skip-chars-forward " \t") |
| 679 | (buffer-substring-no-properties (point) |
| 680 | (progn (forward-sexp 1) |
| 681 | (point)))) |
| 682 | ((memq major-mode add-log-c-like-modes) |
| 683 | (beginning-of-line) |
| 684 | ;; See if we are in the beginning part of a function, |
| 685 | ;; before the open brace. If so, advance forward. |
| 686 | (while (not (looking-at "{\\|\\(\\s *$\\)")) |
| 687 | (forward-line 1)) |
| 688 | (or (eobp) |
| 689 | (forward-char 1)) |
| 690 | (beginning-of-defun) |
| 691 | (when (progn (end-of-defun) |
| 692 | (< location (point))) |
| 693 | (backward-sexp 1) |
| 694 | (let (beg tem) |
| 695 | |
| 696 | (forward-line -1) |
| 697 | ;; Skip back over typedefs of arglist. |
| 698 | (while (and (not (bobp)) |
| 699 | (looking-at "[ \t\n]")) |
| 700 | (forward-line -1)) |
| 701 | ;; See if this is using the DEFUN macro used in Emacs, |
| 702 | ;; or the DEFUN macro used by the C library. |
| 703 | (if (condition-case nil |
| 704 | (and (save-excursion |
| 705 | (end-of-line) |
| 706 | (while (= (preceding-char) ?\\) |
| 707 | (end-of-line 2)) |
| 708 | (backward-sexp 1) |
| 709 | (beginning-of-line) |
| 710 | (setq tem (point)) |
| 711 | (looking-at "DEFUN\\b")) |
| 712 | (>= location tem)) |
| 713 | (error nil)) |
| 714 | (progn |
| 715 | (goto-char tem) |
| 716 | (down-list 1) |
| 717 | (if (= (char-after (point)) ?\") |
| 718 | (progn |
| 719 | (forward-sexp 1) |
| 720 | (skip-chars-forward " ,"))) |
| 721 | (buffer-substring-no-properties |
| 722 | (point) |
| 723 | (progn (forward-sexp 1) |
| 724 | (point)))) |
| 725 | (if (looking-at "^[+-]") |
| 726 | (change-log-get-method-definition) |
| 727 | ;; Ordinary C function syntax. |
| 728 | (setq beg (point)) |
| 729 | (if (and |
| 730 | ;; Protect against "Unbalanced parens" error. |
| 731 | (condition-case nil |
| 732 | (progn |
| 733 | (down-list 1) ; into arglist |
| 734 | (backward-up-list 1) |
| 735 | (skip-chars-backward " \t") |
| 736 | t) |
| 737 | (error nil)) |
| 738 | ;; Verify initial pos was after |
| 739 | ;; real start of function. |
| 740 | (save-excursion |
| 741 | (goto-char beg) |
| 742 | ;; For this purpose, include the line |
| 743 | ;; that has the decl keywords. This |
| 744 | ;; may also include some of the |
| 745 | ;; comments before the function. |
| 746 | (while (and (not (bobp)) |
| 747 | (save-excursion |
| 748 | (forward-line -1) |
| 749 | (looking-at "[^\n\f]"))) |
| 750 | (forward-line -1)) |
| 751 | (>= location (point))) |
| 752 | ;; Consistency check: going down and up |
| 753 | ;; shouldn't take us back before BEG. |
| 754 | (> (point) beg)) |
| 755 | (let (end middle) |
| 756 | ;; Don't include any final whitespace |
| 757 | ;; in the name we use. |
| 758 | (skip-chars-backward " \t\n") |
| 759 | (setq end (point)) |
| 760 | (backward-sexp 1) |
| 761 | ;; Now find the right beginning of the name. |
| 762 | ;; Include certain keywords if they |
| 763 | ;; precede the name. |
| 764 | (setq middle (point)) |
| 765 | (forward-word -1) |
| 766 | ;; Ignore these subparts of a class decl |
| 767 | ;; and move back to the class name itself. |
| 768 | (while (looking-at "public \\|private ") |
| 769 | (skip-chars-backward " \t:") |
| 770 | (setq end (point)) |
| 771 | (backward-sexp 1) |
| 772 | (setq middle (point)) |
| 773 | (forward-word -1)) |
| 774 | (and (bolp) |
| 775 | (looking-at |
| 776 | "enum \\|struct \\|union \\|class ") |
| 777 | (setq middle (point))) |
| 778 | (goto-char end) |
| 779 | (when (eq (preceding-char) ?=) |
| 780 | (forward-char -1) |
| 781 | (skip-chars-backward " \t") |
| 782 | (setq end (point))) |
| 783 | (buffer-substring-no-properties |
| 784 | middle end)))))))) |
| 785 | ((memq major-mode add-log-tex-like-modes) |
| 786 | (if (re-search-backward |
| 787 | "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" |
| 788 | nil t) |
| 789 | (progn |
| 790 | (goto-char (match-beginning 0)) |
| 791 | (buffer-substring-no-properties |
| 792 | (1+ (point)) ; without initial backslash |
| 793 | (line-end-position))))) |
| 794 | ((eq major-mode 'texinfo-mode) |
| 795 | (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t) |
| 796 | (match-string-no-properties 1))) |
| 797 | ((eq major-mode 'perl-mode) |
| 798 | (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t) |
| 799 | (match-string-no-properties 1))) |
| 800 | ;; Emacs's autoconf-mode installs its own |
| 801 | ;; `add-log-current-defun-function'. This applies to |
| 802 | ;; a different mode apparently for editing .m4 |
| 803 | ;; autoconf source. |
| 804 | ((eq major-mode 'autoconf-mode) |
| 805 | (if (re-search-backward |
| 806 | "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t) |
| 807 | (match-string-no-properties 3))) |
| 808 | (t |
| 809 | ;; If all else fails, try heuristics |
| 810 | (let (case-fold-search |
| 811 | result) |
| 812 | (end-of-line) |
| 813 | (when (re-search-backward |
| 814 | add-log-current-defun-header-regexp |
| 815 | (- (point) 10000) |
| 816 | t) |
| 817 | (setq result (or (match-string-no-properties 1) |
| 818 | (match-string-no-properties 0))) |
| 819 | ;; Strip whitespace away |
| 820 | (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)" |
| 821 | result) |
| 822 | (setq result (match-string-no-properties 1 result))) |
| 823 | result)))))) |
| 824 | (error nil))) |
| 825 | |
| 826 | (defvar change-log-get-method-definition-md) |
| 827 | |
| 828 | ;; Subroutine used within change-log-get-method-definition. |
| 829 | ;; Add the last match in the buffer to the end of `md', |
| 830 | ;; followed by the string END; move to the end of that match. |
| 831 | (defun change-log-get-method-definition-1 (end) |
| 832 | (setq change-log-get-method-definition-md |
| 833 | (concat change-log-get-method-definition-md |
| 834 | (match-string 1) |
| 835 | end)) |
| 836 | (goto-char (match-end 0))) |
| 837 | |
| 838 | (defun change-log-get-method-definition () |
| 839 | "For objective C, return the method name if we are in a method." |
| 840 | (let ((change-log-get-method-definition-md "[")) |
| 841 | (save-excursion |
| 842 | (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t) |
| 843 | (change-log-get-method-definition-1 " "))) |
| 844 | (save-excursion |
| 845 | (cond |
| 846 | ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t) |
| 847 | (change-log-get-method-definition-1 "") |
| 848 | (while (not (looking-at "[{;]")) |
| 849 | (looking-at |
| 850 | "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*") |
| 851 | (change-log-get-method-definition-1 "")) |
| 852 | (concat change-log-get-method-definition-md "]")))))) |
| 853 | \f |
| 854 | (defun change-log-sortable-date-at () |
| 855 | "Return date of log entry in a consistent form for sorting. |
| 856 | Point is assumed to be at the start of the entry." |
| 857 | (require 'timezone) |
| 858 | (if (looking-at "^\\sw.........[0-9:+ ]*") |
| 859 | (let ((date (match-string-no-properties 0))) |
| 860 | (if date |
| 861 | (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date) |
| 862 | (concat (match-string 1 date) (match-string 2 date) |
| 863 | (match-string 3 date)) |
| 864 | (condition-case nil |
| 865 | (timezone-make-date-sortable date) |
| 866 | (error nil))))) |
| 867 | (error "Bad date"))) |
| 868 | |
| 869 | ;;;###autoload |
| 870 | (defun change-log-merge (other-log) |
| 871 | "Merge the contents of ChangeLog file OTHER-LOG with this buffer. |
| 872 | Both must be found in Change Log mode (since the merging depends on |
| 873 | the appropriate motion commands). |
| 874 | |
| 875 | Entries are inserted in chronological order. Both the current and |
| 876 | old-style time formats for entries are supported." |
| 877 | (interactive "*fLog file name to merge: ") |
| 878 | (if (not (eq major-mode 'change-log-mode)) |
| 879 | (error "Not in Change Log mode")) |
| 880 | (let ((other-buf (find-file-noselect other-log)) |
| 881 | (buf (current-buffer)) |
| 882 | date1 start end) |
| 883 | (save-excursion |
| 884 | (goto-char (point-min)) |
| 885 | (set-buffer other-buf) |
| 886 | (goto-char (point-min)) |
| 887 | (if (not (eq major-mode 'change-log-mode)) |
| 888 | (error "%s not found in Change Log mode" other-log)) |
| 889 | ;; Loop through all the entries in OTHER-LOG. |
| 890 | (while (not (eobp)) |
| 891 | (setq date1 (change-log-sortable-date-at)) |
| 892 | (setq start (point) |
| 893 | end (progn (forward-page) (point))) |
| 894 | ;; Look for an entry in original buffer that isn't later. |
| 895 | (with-current-buffer buf |
| 896 | (while (and (not (eobp)) |
| 897 | (string< date1 (change-log-sortable-date-at))) |
| 898 | (forward-page)) |
| 899 | (if (not (eobp)) |
| 900 | (insert-buffer-substring other-buf start end) |
| 901 | ;; At the end of the original buffer, insert a newline to |
| 902 | ;; separate entries and then the rest of the file being |
| 903 | ;; merged. Move to the end of it to terminate outer loop. |
| 904 | (insert "\n") |
| 905 | (insert-buffer-substring other-buf start |
| 906 | (with-current-buffer other-buf |
| 907 | (goto-char (point-max)) |
| 908 | (point))))))))) |
| 909 | |
| 910 | ;;;###autoload |
| 911 | (defun change-log-redate () |
| 912 | "Fix any old-style date entries in the current log file to default format." |
| 913 | (interactive) |
| 914 | (require 'timezone) |
| 915 | (save-excursion |
| 916 | (goto-char (point-min)) |
| 917 | (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t) |
| 918 | (unless (= 12 (- (match-end 0) (match-beginning 0))) |
| 919 | (let* ((date (save-match-data |
| 920 | (timezone-fix-time (match-string 0) nil nil))) |
| 921 | (zone (if (consp (aref date 6)) |
| 922 | (nth 1 (aref date 6))))) |
| 923 | (replace-match (format-time-string |
| 924 | "%Y-%m-%d " |
| 925 | (encode-time (aref date 5) |
| 926 | (aref date 4) |
| 927 | (aref date 3) |
| 928 | (aref date 2) |
| 929 | (aref date 1) |
| 930 | (aref date 0) |
| 931 | zone)))))))) |
| 932 | |
| 933 | (provide 'add-log) |
| 934 | |
| 935 | ;;; add-log.el ends here |