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