*** empty log message ***
[bpt/emacs.git] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 87, 88, 89, 90, 91, 1992
4 ;; Free Software Foundation, Inc.
5
6 ;; This file is part of GNU Emacs.
7
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)
11 ;; any later version.
12
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.
17
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.
21
22 ;;; Code:
23
24 ;;;###autoload
25 (defvar change-log-default-name nil
26 "*Name of a change log file for \\[add-change-log-entry].")
27
28 (defun change-log-name ()
29 (or change-log-default-name
30 (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog")))
31
32 (defun prompt-for-change-log-name ()
33 "Prompt for a change log name."
34 (let ((default (change-log-name)))
35 (expand-file-name
36 (read-file-name (format "Log file (default %s): " default)
37 nil default))))
38
39 ;;;###autoload
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))
49 (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))
56 (user-login-name)))
57 (site-name (if whoami
58 (read-input "Site name: " (system-name))
59 (system-name)))
60 (defun (add-log-current-defun))
61 entry entry-position empty-entry)
62 (or file-name
63 (setq file-name (or change-log-default-name
64 default-directory)))
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)
69 (if buffer-file-name
70 (setq entry (if (string-match
71 (concat "^" (regexp-quote (file-name-directory
72 file-name)))
73 buffer-file-name)
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")
78 (setq entry nil
79 defun nil))
80 (if (and other-window (not (equal file-name buffer-file-name)))
81 (find-file-other-window file-name)
82 (find-file file-name))
83 (undo-boundary)
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)
88 " " full-name
89 " (" login-name
90 "@" site-name ")\n\n")))
91 (goto-char (point-min))
92 (setq empty-entry
93 (and (search-forward "\n\t* \n" nil t)
94 (1- (point))))
95 (if (and entry
96 (not empty-entry))
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)
105 (point)))))
106 (setq entry-position (save-excursion
107 (and (re-search-forward
108 (concat
109 (regexp-quote (concat "* " entry))
110 ;; don't accept `foo.bar' when
111 ;; looking for `foo':
112 "[ \n\t,:]")
113 entry-boundary
114 t)
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 *$")
121 (beginning-of-line)
122 (while (looking-at "^\\s *$")
123 (delete-region (point) (save-excursion (forward-line 1) (point))))
124 (insert "\n\n")
125 (forward-line -2)
126 (indent-relative-maybe))
127 (empty-entry
128 ;; Put this file name into the existing empty entry.
129 (goto-char empty-entry)
130 (if entry
131 (insert entry)))
132 (t
133 ;; Make a new entry.
134 (forward-line 1)
135 (while (looking-at "\\sW")
136 (forward-line 1))
137 (while (looking-at "^\\s *$")
138 (delete-region (point) (save-excursion (forward-line 1) (point))))
139 (insert "\n\n\n")
140 (forward-line -2)
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.
146 (if defun
147 (progn
148 ;; Make it easy to get rid of the function name.
149 (undo-boundary)
150 (insert (if (save-excursion
151 (beginning-of-line 1)
152 (looking-at "\\s *$"))
153 ""
154 " ")
155 "(" defun "): "))
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 *\\)?$")))
160 (insert ": ")))))
161
162 ;;;###autoload
163 (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
164
165 ;;;###autoload
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))
175
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]."
180 (interactive)
181 (kill-all-local-variables)
182 (indented-text-mode)
183 (setq major-mode 'change-log-mode)
184 (setq mode-name "Change Log")
185 (setq left-margin 8)
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))
200
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.")
204
205 (defun add-log-current-defun ()
206 "Return name of function definition point is in, or nil.
207
208 Understands Lisp, LaTeX (\"functions\" are chapters, sections, ...),
209 Texinfo (@node titles), and C.
210
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'.
215
216 Has a preference of looking backwards."
217 (save-excursion
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.
223 (forward-char 1)
224 (beginning-of-defun)
225 ;; Make sure we are really inside the defun found, not after it.
226 (if (save-excursion (end-of-defun)
227 (< location (point)))
228 (progn
229 (forward-word 1)
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 *$")))
239 (forward-line 1))
240 (forward-char 1)
241 (beginning-of-defun)
242 (if (save-excursion (end-of-defun)
243 (< location (point)))
244 (progn
245 (forward-line -1)
246 (while (looking-at "[ \t\n]") ; skip typedefs of arglist
247 (forward-line -1))
248 (down-list 1) ; into arglist
249 (backward-up-list 1)
250 (skip-chars-backward " \t")
251 (buffer-substring (point)
252 (progn (backward-sexp 1)
253 (point))))))
254 ((memq major-mode
255 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
256 plain-tex-mode latex-mode;; cmutex.el
257 ))
258 (if (re-search-backward
259 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
260 (progn
261 (goto-char (match-beginning 0))
262 (buffer-substring (1+ (point));; without initial backslash
263 (progn
264 (end-of-line)
265 (point))))))
266 ((eq major-mode 'texinfo-mode)
267 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
268 (buffer-substring (match-beginning 1)
269 (match-end 1))))
270 (t
271 ;; If all else fails, try heuristics
272 (let (case-fold-search)
273 (if (re-search-backward add-log-current-defun-header-regexp
274 (- (point) 10000)
275 t)
276 (buffer-substring (match-beginning 1)
277 (match-end 1)))))))))
278
279 ;;; add-log.el ends here