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