(wait_reading_process_input): Use getpid when generating SIGIO.
[emacs.git] / lisp / add-log.el
blob0791fe1fe301d6b3b9cf65c3a5c9734cf7956948
1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
5 ;; Keywords: maint
7 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
26 ;; This facility is documented in the Emacs Manual.
28 ;;; Code:
30 (defvar change-log-default-name nil
31 "*Name of a change log file for \\[add-change-log-entry].")
33 (defvar add-log-current-defun-function nil
35 *If non-nil, function to guess name of current function from surrounding text.
36 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
37 instead) with no arguments. It returns a string or nil if it cannot guess.")
39 (defvar add-log-full-name nil
40 "*Full name of user, for inclusion in ChangeLog daily headers.
41 This defaults to the value returned by the `user-full-name' function.")
43 (defvar add-log-mailing-address nil
44 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
45 This defaults to the value of `user-mail-address'.")
47 (defvar change-log-font-lock-keywords
48 '(("^[SMTWF].+" . font-lock-function-name-face) ; Date line.
49 ("^\t\\* \\([^ :\n]+\\)" 1 font-lock-comment-face) ; File name.
50 ("\(\\([^)\n]+\\)\)" 1 font-lock-keyword-face)) ; Function name.
51 "Additional expressions to highlight in Change Log mode.")
53 (defvar change-log-mode-map nil
54 "Keymap for Change Log major mode.")
55 (if change-log-mode-map
56 nil
57 (setq change-log-mode-map (make-sparse-keymap))
58 (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
60 (defun change-log-name ()
61 (or change-log-default-name
62 (if (eq system-type 'vax-vms)
63 "$CHANGE_LOG$.TXT"
64 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
65 "changelo"
66 "ChangeLog"))))
68 ;;;###autoload
69 (defun prompt-for-change-log-name ()
70 "Prompt for a change log name."
71 (let* ((default (change-log-name))
72 (name (expand-file-name
73 (read-file-name (format "Log file (default %s): " default)
74 nil default))))
75 ;; Handle something that is syntactically a directory name.
76 ;; Look for ChangeLog or whatever in that directory.
77 (if (string= (file-name-nondirectory name) "")
78 (expand-file-name (file-name-nondirectory default)
79 name)
80 ;; Handle specifying a file that is a directory.
81 (if (file-directory-p name)
82 (expand-file-name (file-name-nondirectory default)
83 (file-name-as-directory name))
84 name))))
86 ;;;###autoload
87 (defun find-change-log (&optional file-name)
88 "Find a change log file for \\[add-change-log-entry] and return the name.
90 Optional arg FILE-NAME specifies the file to use.
91 If FILE-NAME is nil, use the value of `change-log-default-name'.
92 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
93 \(or whatever we use on this operating system).
95 If 'change-log-default-name' contains a leading directory component, then
96 simply find it in the current directory. Otherwise, search in the current
97 directory and its successive parents for a file so named.
99 Once a file is found, `change-log-default-name' is set locally in the
100 current buffer to the complete file name."
101 ;; If user specified a file name or if this buffer knows which one to use,
102 ;; just use that.
103 (or file-name
104 (setq file-name (and change-log-default-name
105 (file-name-directory change-log-default-name)
106 change-log-default-name))
107 (progn
108 ;; Chase links in the source file
109 ;; and use the change log in the dir where it points.
110 (setq file-name (or (and buffer-file-name
111 (file-name-directory
112 (file-chase-links buffer-file-name)))
113 default-directory))
114 (if (file-directory-p file-name)
115 (setq file-name (expand-file-name (change-log-name) file-name)))
116 ;; Chase links before visiting the file.
117 ;; This makes it easier to use a single change log file
118 ;; for several related directories.
119 (setq file-name (file-chase-links file-name))
120 (setq file-name (expand-file-name file-name))
121 ;; Move up in the dir hierarchy till we find a change log file.
122 (let ((file1 file-name)
123 parent-dir)
124 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
125 (progn (setq parent-dir
126 (file-name-directory
127 (directory-file-name
128 (file-name-directory file1))))
129 ;; Give up if we are already at the root dir.
130 (not (string= (file-name-directory file1)
131 parent-dir))))
132 ;; Move up to the parent dir and try again.
133 (setq file1 (expand-file-name
134 (file-name-nondirectory (change-log-name))
135 parent-dir)))
136 ;; If we found a change log in a parent, use that.
137 (if (or (get-file-buffer file1) (file-exists-p file1))
138 (setq file-name file1)))))
139 ;; Make a local variable in this buffer so we needn't search again.
140 (set (make-local-variable 'change-log-default-name) file-name)
141 file-name)
143 ;;;###autoload
144 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
145 "Find change log file and add an entry for today.
146 Optional arg (interactive prefix) non-nil means prompt for user name and site.
147 Second arg is file name of change log. If nil, uses `change-log-default-name'.
148 Third arg OTHER-WINDOW non-nil means visit in other window.
149 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
150 never append to an existing entry."
151 (interactive (list current-prefix-arg
152 (prompt-for-change-log-name)))
153 (or add-log-full-name
154 (setq add-log-full-name (user-full-name)))
155 (or add-log-mailing-address
156 (setq add-log-mailing-address user-mail-address))
157 (if whoami
158 (progn
159 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
160 ;; Note that some sites have room and phone number fields in
161 ;; full name which look silly when inserted. Rather than do
162 ;; anything about that here, let user give prefix argument so that
163 ;; s/he can edit the full name field in prompter if s/he wants.
164 (setq add-log-mailing-address
165 (read-input "Mailing address: " add-log-mailing-address))))
166 (let ((defun (funcall (or add-log-current-defun-function
167 'add-log-current-defun)))
168 paragraph-end entry)
170 (setq file-name (expand-file-name (find-change-log file-name)))
172 ;; Set ENTRY to the file name to use in the new entry.
173 (and buffer-file-name
174 ;; Never want to add a change log entry for the ChangeLog file itself.
175 (not (string= buffer-file-name file-name))
176 (setq entry (if (string-match
177 (concat "^" (regexp-quote (file-name-directory
178 file-name)))
179 buffer-file-name)
180 (substring buffer-file-name (match-end 0))
181 (file-name-nondirectory buffer-file-name))))
183 (if (and other-window (not (equal file-name buffer-file-name)))
184 (find-file-other-window file-name)
185 (find-file file-name))
186 (or (eq major-mode 'change-log-mode)
187 (change-log-mode))
188 (undo-boundary)
189 (goto-char (point-min))
190 (if (looking-at (concat (regexp-quote (substring (current-time-string)
191 0 10))
192 ".* " (regexp-quote add-log-full-name)
193 " <" (regexp-quote add-log-mailing-address)))
194 (forward-line 1)
195 (insert (current-time-string)
196 " " add-log-full-name
197 " <" add-log-mailing-address ">\n\n"))
199 ;; Search only within the first paragraph.
200 (if (looking-at "\n*[^\n* \t]")
201 (skip-chars-forward "\n")
202 (forward-paragraph 1))
203 (setq paragraph-end (point))
204 (goto-char (point-min))
206 ;; Now insert the new line for this entry.
207 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
208 ;; Put this file name into the existing empty entry.
209 (if entry
210 (insert entry)))
211 ((and (not new-entry)
212 (let (case-fold-search)
213 (re-search-forward
214 (concat (regexp-quote (concat "* " entry))
215 ;; Don't accept `foo.bar' when
216 ;; looking for `foo':
217 "\\(\\s \\|[(),:]\\)")
218 paragraph-end t)))
219 ;; Add to the existing entry for the same file.
220 (re-search-forward "^\\s *$\\|^\\s \\*")
221 (goto-char (match-beginning 0))
222 ;; Delete excess empty lines; make just 2.
223 (while (and (not (eobp)) (looking-at "^\\s *$"))
224 (delete-region (point) (save-excursion (forward-line 1) (point))))
225 (insert "\n\n")
226 (forward-line -2)
227 (indent-relative-maybe))
229 ;; Make a new entry.
230 (forward-line 1)
231 (while (looking-at "\\sW")
232 (forward-line 1))
233 (while (and (not (eobp)) (looking-at "^\\s *$"))
234 (delete-region (point) (save-excursion (forward-line 1) (point))))
235 (insert "\n\n\n")
236 (forward-line -2)
237 (indent-to left-margin)
238 (insert "* " (or entry ""))))
239 ;; Now insert the function name, if we have one.
240 ;; Point is at the entry for this file,
241 ;; either at the end of the line or at the first blank line.
242 (if defun
243 (progn
244 ;; Make it easy to get rid of the function name.
245 (undo-boundary)
246 (insert (if (save-excursion
247 (beginning-of-line 1)
248 (looking-at "\\s *$"))
250 " ")
251 "(" defun "): "))
252 ;; No function name, so put in a colon unless we have just a star.
253 (if (not (save-excursion
254 (beginning-of-line 1)
255 (looking-at "\\s *\\(\\*\\s *\\)?$")))
256 (insert ": ")))))
258 ;;;###autoload
259 (defun add-change-log-entry-other-window (&optional whoami file-name)
260 "Find change log file in other window and add an entry for today.
261 Optional arg (interactive prefix) non-nil means prompt for user name and site.
262 Second arg is file name of change log. \
263 If nil, uses `change-log-default-name'."
264 (interactive (if current-prefix-arg
265 (list current-prefix-arg
266 (prompt-for-change-log-name))))
267 (add-change-log-entry whoami file-name t))
268 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
270 ;;;###autoload
271 (defun change-log-mode ()
272 "Major mode for editing change logs; like Indented Text Mode.
273 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
274 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
275 Each entry behaves as a paragraph, and the entries for one day as a page.
276 Runs `change-log-mode-hook'."
277 (interactive)
278 (kill-all-local-variables)
279 (indented-text-mode)
280 (setq major-mode 'change-log-mode
281 mode-name "Change Log"
282 left-margin 8
283 fill-column 74
284 indent-tabs-mode t
285 tab-width 8)
286 (use-local-map change-log-mode-map)
287 ;; Let each entry behave as one paragraph:
288 ;; We really do want "^" in paragraph-start below: it is only the lines that
289 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
290 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\sw")
291 (set (make-local-variable 'paragraph-separate) "\\s *$\\|\f\\|^\\sw")
292 ;; Let all entries for one day behave as one page.
293 ;; Match null string on the date-line so that the date-line
294 ;; is grouped with what follows.
295 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
296 (set (make-local-variable 'version-control) 'never)
297 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
298 (set (make-local-variable 'font-lock-defaults)
299 '(change-log-font-lock-keywords t))
300 (run-hooks 'change-log-mode-hook))
302 ;; It might be nice to have a general feature to replace this. The idea I
303 ;; have is a variable giving a regexp matching text which should not be
304 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
305 ;; But I don't feel up to implementing that today.
306 (defun change-log-fill-paragraph (&optional justify)
307 "Fill the paragraph, but preserve open parentheses at beginning of lines.
308 Prefix arg means justify as well."
309 (interactive "P")
310 (let ((end (save-excursion (forward-paragraph) (point)))
311 (beg (save-excursion (backward-paragraph)(point)))
312 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
313 (fill-region beg end justify)))
315 (defvar add-log-current-defun-header-regexp
316 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
317 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
319 ;;;###autoload
320 (defun add-log-current-defun ()
321 "Return name of function definition point is in, or nil.
323 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
324 Texinfo (@node titles), Perl, and Fortran.
326 Other modes are handled by a heuristic that looks in the 10K before
327 point for uppercase headings starting in the first column or
328 identifiers followed by `:' or `=', see variable
329 `add-log-current-defun-header-regexp'.
331 Has a preference of looking backwards."
332 (condition-case nil
333 (save-excursion
334 (let ((location (point)))
335 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode
336 lisp-interaction-mode))
337 ;; If we are now precisely at the beginning of a defun,
338 ;; make sure beginning-of-defun finds that one
339 ;; rather than the previous one.
340 (or (eobp) (forward-char 1))
341 (beginning-of-defun)
342 ;; Make sure we are really inside the defun found, not after it.
343 (if (and (looking-at "\\s(")
344 (progn (end-of-defun)
345 (< location (point)))
346 (progn (forward-sexp -1)
347 (>= location (point))))
348 (progn
349 (if (looking-at "\\s(")
350 (forward-char 1))
351 (forward-sexp 1)
352 (skip-chars-forward " '")
353 (buffer-substring (point)
354 (progn (forward-sexp 1) (point))))))
355 ((and (memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
356 (save-excursion (beginning-of-line)
357 ;; Use eq instead of = here to avoid
358 ;; error when at bob and char-after
359 ;; returns nil.
360 (while (eq (char-after (- (point) 2)) ?\\)
361 (forward-line -1))
362 (looking-at "[ \t]*#[ \t]*define[ \t]")))
363 ;; Handle a C macro definition.
364 (beginning-of-line)
365 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
366 (forward-line -1))
367 (search-forward "define")
368 (skip-chars-forward " \t")
369 (buffer-substring (point)
370 (progn (forward-sexp 1) (point))))
371 ((memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
372 (beginning-of-line)
373 ;; See if we are in the beginning part of a function,
374 ;; before the open brace. If so, advance forward.
375 (while (not (looking-at "{\\|\\(\\s *$\\)"))
376 (forward-line 1))
377 (or (eobp)
378 (forward-char 1))
379 (beginning-of-defun)
380 (if (progn (end-of-defun)
381 (< location (point)))
382 (progn
383 (backward-sexp 1)
384 (let (beg tem)
386 (forward-line -1)
387 ;; Skip back over typedefs of arglist.
388 (while (and (not (bobp))
389 (looking-at "[ \t\n]"))
390 (forward-line -1))
391 ;; See if this is using the DEFUN macro used in Emacs,
392 ;; or the DEFUN macro used by the C library.
393 (if (condition-case nil
394 (and (save-excursion
395 (end-of-line)
396 (while (= (preceding-char) ?\\)
397 (end-of-line 2))
398 (backward-sexp 1)
399 (beginning-of-line)
400 (setq tem (point))
401 (looking-at "DEFUN\\b"))
402 (>= location tem))
403 (error nil))
404 (progn
405 (goto-char tem)
406 (down-list 1)
407 (if (= (char-after (point)) ?\")
408 (progn
409 (forward-sexp 1)
410 (skip-chars-forward " ,")))
411 (buffer-substring (point)
412 (progn (forward-sexp 1) (point))))
413 (if (looking-at "^[+-]")
414 (get-method-definition)
415 ;; Ordinary C function syntax.
416 (setq beg (point))
417 (if (and (condition-case nil
418 ;; Protect against "Unbalanced parens" error.
419 (progn
420 (down-list 1) ; into arglist
421 (backward-up-list 1)
422 (skip-chars-backward " \t")
424 (error nil))
425 ;; Verify initial pos was after
426 ;; real start of function.
427 (save-excursion
428 (goto-char beg)
429 ;; For this purpose, include the line
430 ;; that has the decl keywords. This
431 ;; may also include some of the
432 ;; comments before the function.
433 (while (and (not (bobp))
434 (save-excursion
435 (forward-line -1)
436 (looking-at "[^\n\f]")))
437 (forward-line -1))
438 (>= location (point)))
439 ;; Consistency check: going down and up
440 ;; shouldn't take us back before BEG.
441 (> (point) beg))
442 (let (end middle)
443 ;; Don't include any final newline
444 ;; in the name we use.
445 (if (= (preceding-char) ?\n)
446 (forward-char -1))
447 (setq end (point))
448 (backward-sexp 1)
449 ;; Now find the right beginning of the name.
450 ;; Include certain keywords if they
451 ;; precede the name.
452 (setq middle (point))
453 (forward-word -1)
454 ;; Ignore these subparts of a class decl
455 ;; and move back to the class name itself.
456 (while (looking-at "public \\|private ")
457 (skip-chars-backward " \t:")
458 (setq end (point))
459 (backward-sexp 1)
460 (setq middle (point))
461 (forward-word -1))
462 (and (bolp)
463 (looking-at "struct \\|union \\|class ")
464 (setq middle (point)))
465 (buffer-substring middle end)))))))))
466 ((memq major-mode
467 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
468 plain-tex-mode latex-mode;; cmutex.el
470 (if (re-search-backward
471 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
472 (progn
473 (goto-char (match-beginning 0))
474 (buffer-substring (1+ (point));; without initial backslash
475 (progn
476 (end-of-line)
477 (point))))))
478 ((eq major-mode 'texinfo-mode)
479 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
480 (buffer-substring (match-beginning 1)
481 (match-end 1))))
482 ((eq major-mode 'perl-mode)
483 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
484 (buffer-substring (match-beginning 1)
485 (match-end 1))))
486 ((eq major-mode 'fortran-mode)
487 ;; must be inside function body for this to work
488 (beginning-of-fortran-subprogram)
489 (let ((case-fold-search t)) ; case-insensitive
490 ;; search for fortran subprogram start
491 (if (re-search-forward
492 "^[ \t]*\\(program\\|subroutine\\|function\
493 \\|[ \ta-z0-9*]*[ \t]+function\\)"
494 nil t)
495 (progn
496 ;; move to EOL or before first left paren
497 (if (re-search-forward "[(\n]" nil t)
498 (progn (forward-char -1)
499 (skip-chars-backward " \t"))
500 (end-of-line))
501 ;; Use the name preceding that.
502 (buffer-substring (point)
503 (progn (forward-sexp -1)
504 (point)))))))
506 ;; If all else fails, try heuristics
507 (let (case-fold-search)
508 (end-of-line)
509 (if (re-search-backward add-log-current-defun-header-regexp
510 (- (point) 10000)
512 (buffer-substring (match-beginning 1)
513 (match-end 1))))))))
514 (error nil)))
516 (defvar get-method-definition-md)
518 ;; Subroutine used within get-method-definition.
519 ;; Add the last match in the buffer to the end of `md',
520 ;; followed by the string END; move to the end of that match.
521 (defun get-method-definition-1 (end)
522 (setq get-method-definition-md
523 (concat get-method-definition-md
524 (buffer-substring (match-beginning 1) (match-end 1))
525 end))
526 (goto-char (match-end 0)))
528 ;; For objective C, return the method name if we are in a method.
529 (defun get-method-definition ()
530 (let ((get-method-definition-md "["))
531 (save-excursion
532 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
533 (get-method-definition-1 " ")))
534 (save-excursion
535 (cond
536 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
537 (get-method-definition-1 "")
538 (while (not (looking-at "[{;]"))
539 (looking-at
540 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
541 (get-method-definition-1 ""))
542 (concat get-method-definition-md "]"))))))
545 (provide 'add-log)
547 ;;; add-log.el ends here