1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 88, 89, 90, 91, 1992
4 ;; Free Software Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 (defvar change-log-default-name nil
26 "*Name of a change log file for \\[add-change-log-entry].")
28 (defun change-log-name ()
29 (or change-log-default-name
30 (if (eq system-type
'vax-vms
) "$CHANGE_LOG$.TXT" "ChangeLog")))
32 (defun prompt-for-change-log-name ()
33 "Prompt for a change log name."
34 (let ((default (change-log-name)))
36 (read-file-name (format "Log file (default %s): " default
)
40 (defun add-change-log-entry (&optional whoami file-name other-window
)
41 "Find change log file and add an entry for today.
42 Optional arg (interactive prefix) non-nil means prompt for user name and site.
43 Second arg is file name of change log. If nil, uses `change-log-default-name'.
44 Third arg OTHER-WINDOW non-nil means visit in other window."
45 (interactive (list current-prefix-arg
46 (prompt-for-change-log-name)))
47 (let* ((full-name (if whoami
48 (read-input "Full name: " (user-full-name))
50 ;; Note that some sites have room and phone number fields in
51 ;; full name which look silly when inserted. Rather than do
52 ;; anything about that here, let user give prefix argument so that
53 ;; s/he can edit the full name field in prompter if s/he wants.
54 (login-name (if whoami
55 (read-input "Login name: " (user-login-name))
58 (read-input "Site name: " (system-name))
60 (defun (add-log-current-defun))
61 entry entry-position empty-entry
)
63 (setq file-name
(or change-log-default-name
65 (setq file-name
(if (file-directory-p file-name
)
66 (expand-file-name (change-log-name) file-name
)
67 (expand-file-name file-name
)))
68 (set (make-local-variable 'change-log-default-name
) file-name
)
70 (setq entry
(if (string-match
71 (concat "^" (regexp-quote (file-name-directory
74 (substring buffer-file-name
(match-end 0))
75 (file-name-nondirectory buffer-file-name
))))
76 ;; Never want to add a change log entry for the ChangeLog file itself.
77 (if (equal entry
"ChangeLog")
80 (if (and other-window
(not (equal file-name buffer-file-name
)))
81 (find-file-other-window file-name
)
82 (find-file file-name
))
84 (goto-char (point-min))
85 (if (not (and (looking-at (substring (current-time-string) 0 10))
86 (looking-at (concat ".* " full-name
" (" login-name
"@"))))
87 (progn (insert (current-time-string)
90 "@" site-name
")\n\n")))
91 (goto-char (point-min))
93 (and (search-forward "\n\t* \n" nil t
)
97 ;; Look for today's entry for the same file.
98 ;; If there is an empty entry (just a `*'), take the hint and
99 ;; use it. This is so that C-x a from the ChangeLog buffer
100 ;; itself can be used to force the next entry to be added at
101 ;; the beginning, even if there are today's entries for the
102 ;; same file (but perhaps different revisions).
103 (let ((entry-boundary (save-excursion
104 (and (re-search-forward "\n[A-Z]" nil t
)
106 (setq entry-position
(save-excursion
107 (and (re-search-forward
109 (regexp-quote (concat "* " entry
))
110 ;; don't accept `foo.bar' when
111 ;; looking for `foo':
115 (1- (match-end 0)))))))
116 ;; Now insert the new line for this entry.
117 (cond (entry-position
118 ;; Move to the existing entry for the same file.
119 (goto-char entry-position
)
120 (re-search-forward "^\\s *$")
122 (while (looking-at "^\\s *$")
123 (delete-region (point) (save-excursion (forward-line 1) (point))))
126 (indent-relative-maybe))
128 ;; Put this file name into the existing empty entry.
129 (goto-char empty-entry
)
135 (while (looking-at "\\sW")
137 (while (looking-at "^\\s *$")
138 (delete-region (point) (save-excursion (forward-line 1) (point))))
141 (indent-to left-margin
)
142 (insert "* " (or entry
""))))
143 ;; Now insert the function name, if we have one.
144 ;; Point is at the entry for this file,
145 ;; either at the end of the line or at the first blank line.
148 ;; Make it easy to get rid of the function name.
150 (insert (if (save-excursion
151 (beginning-of-line 1)
152 (looking-at "\\s *$"))
156 ;; No function name, so put in a colon unless we have just a star.
157 (if (not (save-excursion
158 (beginning-of-line 1)
159 (looking-at "\\s *\\(\\*\\s *\\)?$")))
163 (define-key ctl-x-4-map
"a" 'add-change-log-entry-other-window
)
166 (defun add-change-log-entry-other-window (&optional whoami file-name
)
167 "Find change log file in other window and add an entry for today.
168 First arg (interactive prefix) non-nil means prompt for user name and site.
169 Second arg is file name of change log.
170 Interactively, with a prefix argument, the file name is prompted for."
171 (interactive (if current-prefix-arg
172 (list current-prefix-arg
173 (prompt-for-change-log-name))))
174 (add-change-log-entry whoami file-name t
))
176 (defun change-log-mode ()
177 "Major mode for editting change logs; like Indented Text Mode.
178 Prevents numeric backups and sets `left-margin' to 8 and `fill-column'to 74.
179 New log entries are usually made with \\[add-change-log-entry]."
181 (kill-all-local-variables)
183 (setq major-mode
'change-log-mode
)
184 (setq mode-name
"Change Log")
186 (setq fill-column
74)
187 ;; Let each entry behave as one paragraph:
188 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^^L")
189 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^^L\\|^\\sw")
190 ;; Let all entries for one day behave as one page.
191 ;; Note that a page boundary is also a paragraph boundary.
192 ;; Unfortunately the date line of a page actually belongs to
193 ;; the next day, but I don't see how to avoid that since
194 ;; page moving cmds go to the end of the match, and Emacs
195 ;; regexps don't have a context feature.
196 (set (make-local-variable 'page-delimiter
) "^[A-Z][a-z][a-z] .*\n\\|^\f")
197 (set (make-local-variable 'version-control
) 'never
)
198 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
199 (run-hooks 'change-log-mode-hook
))
201 (defvar add-log-current-defun-header-regexp
202 "^\\([A-Z][A-Z_ ]+\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
203 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
205 (defun add-log-current-defun ()
206 "Return name of function definition point is in, or nil.
208 Understands Lisp, LaTeX (\"functions\" are chapters, sections, ...),
209 Texinfo (@node titles), and C.
211 Other modes are handled by a heuristic that looks in the 10K before
212 point for uppercase headings starting in the first column or
213 identifiers followed by `:' or `=', see variable
214 `add-log-current-defun-header-regexp'.
216 Has a preference of looking backwards."
218 (let ((location (point)))
219 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
))
220 ;; If we are now precisely a the beginning of a defun,
221 ;; make sure beginning-of-defun finds that one
222 ;; rather than the previous one.
225 ;; Make sure we are really inside the defun found, not after it.
226 (if (save-excursion (end-of-defun)
227 (< location
(point)))
230 (skip-chars-forward " ")
231 (buffer-substring (point)
232 (progn (forward-sexp 1) (point))))))
233 ((or (eq major-mode
'c-mode
)
234 (eq major-mode
'c
++-mode
))
235 ;; See if we are in the beginning part of a function,
236 ;; before the open brace. If so, advance forward.
237 (while (not (or (looking-at "{")
238 (looking-at "\\s *$")))
242 (if (save-excursion (end-of-defun)
243 (< location
(point)))
246 (while (looking-at "[ \t\n]") ; skip typedefs of arglist
248 (down-list 1) ; into arglist
250 (skip-chars-backward " \t")
251 (buffer-substring (point)
252 (progn (backward-sexp 1)
255 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
256 plain-tex-mode latex-mode
;; cmutex.el
258 (if (re-search-backward
259 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
261 (goto-char (match-beginning 0))
262 (buffer-substring (1+ (point));; without initial backslash
266 ((eq major-mode
'texinfo-mode
)
267 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t
)
268 (buffer-substring (match-beginning 1)
271 ;; If all else fails, try heuristics
272 (let (case-fold-search)
273 (if (re-search-backward add-log-current-defun-header-regexp
276 (buffer-substring (match-beginning 1)
277 (match-end 1)))))))))
279 ;;; add-log.el ends here