* lisp/ibuffer.el (ibuffer): Improve 'other-window' case. (Bug#23617)
[emacs.git] / lisp / autoinsert.el
blob43fa31288e68c9b599366fa531a30a7cbddefe2f
1 ;;; autoinsert.el --- automatic mode-dependent insertion of text into new files
3 ;; Copyright (C) 1985-1987, 1994-1995, 1998, 2000-2016 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Charlie Martin <crm@cs.duke.edu>
7 ;; Adapted-By: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: convenience
9 ;; Maintainer: emacs-devel@gnu.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; The following defines an association list for text to be
29 ;; automatically inserted when a new file is created, and a function
30 ;; which automatically inserts these files; the idea is to insert
31 ;; default text much as the mode is automatically set using
32 ;; auto-mode-alist.
34 ;; To use:
35 ;; (add-hook 'find-file-hook 'auto-insert)
36 ;; setq auto-insert-directory to an appropriate slash-terminated value
38 ;; You can also customize the variable `auto-insert-mode' to load the
39 ;; package. Alternatively, add the following to your init file:
40 ;; (auto-insert-mode 1)
42 ;; Author: Charlie Martin
43 ;; Department of Computer Science and
44 ;; National Biomedical Simulation Resource
45 ;; Box 3709
46 ;; Duke University Medical Center
47 ;; Durham, NC 27710
48 ;; (crm@cs.duke.edu,mcnc!duke!crm)
50 ;;; Code:
52 (defgroup auto-insert nil
53 "Automatic mode-dependent insertion of text into new files."
54 :prefix "auto-insert-"
55 :group 'files
56 :group 'convenience
57 :link '(custom-manual "(autotype) Autoinserting"))
60 (defcustom auto-insert 'not-modified
61 "Controls automatic insertion into newly found empty files.
62 Possible values:
63 nil do nothing
64 t insert if possible
65 other insert if possible, but mark as unmodified.
66 Insertion is possible when something appropriate is found in
67 `auto-insert-alist'. When the insertion is marked as unmodified, you can
68 save it with \\[write-file] RET.
69 This variable is used when the function `auto-insert' is called, e.g.
70 when you do (add-hook \\='find-file-hook \\='auto-insert).
71 With \\[auto-insert], this is always treated as if it were t."
72 :type '(choice (const :tag "Insert if possible" t)
73 (const :tag "Do nothing" nil)
74 (other :tag "insert if possible, mark as unmodified."
75 not-modified))
76 :group 'auto-insert)
78 (defcustom auto-insert-query 'function
79 "Non-nil means ask user before auto-inserting.
80 When this is `function', only ask when called non-interactively."
81 :type '(choice (const :tag "Don't ask" nil)
82 (const :tag "Ask if called non-interactively" function)
83 (other :tag "Ask" t))
84 :group 'auto-insert)
86 (defcustom auto-insert-prompt "Perform %s auto-insertion? "
87 "Prompt to use when querying whether to auto-insert.
88 If this contains a %s, that will be replaced by the matching rule."
89 :type 'string
90 :group 'auto-insert)
93 (defcustom auto-insert-alist
94 '((("\\.\\([Hh]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'" . "C / C++ header")
95 (replace-regexp-in-string
96 "[^A-Z0-9]" "_"
97 (replace-regexp-in-string
98 "\\+" "P"
99 (upcase (file-name-nondirectory buffer-file-name))))
100 "#ifndef " str \n
101 "#define " str "\n\n"
102 _ "\n\n#endif")
104 (("\\.\\([Cc]\\|cc\\|cpp\\|cxx\\|c\\+\\+\\)\\'" . "C / C++ program")
106 "#include \""
107 (let ((stem (file-name-sans-extension buffer-file-name))
108 ret)
109 (dolist (ext '("H" "h" "hh" "hpp" "hxx" "h++") ret)
110 (when (file-exists-p (concat stem "." ext))
111 (setq ret (file-name-nondirectory (concat stem "." ext))))))
112 & ?\" | -10)
114 (("[Mm]akefile\\'" . "Makefile") . "makefile.inc")
116 (html-mode . (lambda () (sgml-tag "html")))
118 (plain-tex-mode . "tex-insert.tex")
119 (bibtex-mode . "tex-insert.tex")
120 (latex-mode
121 ;; should try to offer completing read for these
122 "options, RET: "
123 "\\documentclass[" str & ?\] | -1
124 ?{ (read-string "class: ") "}\n"
125 ("package, %s: "
126 "\\usepackage[" (read-string "options, RET: ") & ?\] | -1 ?{ str "}\n")
127 _ "\n\\begin{document}\n" _
128 "\n\\end{document}")
130 (("/bin/.*[^/]\\'" . "Shell-Script mode magic number") .
131 (lambda ()
132 (if (eq major-mode (default-value 'major-mode))
133 (sh-mode))))
135 (ada-mode . ada-header)
137 (("\\.[1-9]\\'" . "Man page skeleton")
138 "Short description: "
139 ".\\\" Copyright (C), " (format-time-string "%Y") " "
140 (getenv "ORGANIZATION") | (progn user-full-name)
142 .\\\" You may distribute this file under the terms of the GNU Free
143 .\\\" Documentation License.
144 .TH " (file-name-base)
145 " " (file-name-extension (buffer-file-name))
146 " " (format-time-string "%Y-%m-%d ")
147 "\n.SH NAME\n"
148 (file-name-base)
149 " \\- " str
150 "\n.SH SYNOPSIS
151 .B " (file-name-base)
152 "\n"
155 .SH DESCRIPTION
156 .SH OPTIONS
157 .SH FILES
158 .SH \"SEE ALSO\"
159 .SH BUGS
160 .SH AUTHOR
161 " (user-full-name)
162 '(if (search-backward "&" (line-beginning-position) t)
163 (replace-match (capitalize (user-login-name)) t t))
164 '(end-of-line 1) " <" (progn user-mail-address) ">\n")
166 (("\\.el\\'" . "Emacs Lisp header")
167 "Short description: "
168 ";;; " (file-name-nondirectory (buffer-file-name)) " --- " str
169 (make-string (max 2 (- 80 (current-column) 27)) ?\s)
170 "-*- lexical-binding: t; -*-" '(setq lexical-binding t)
173 ;; Copyright (C) " (format-time-string "%Y") " "
174 (getenv "ORGANIZATION") | (progn user-full-name) "
176 ;; Author: " (user-full-name)
177 '(if (search-backward "&" (line-beginning-position) t)
178 (replace-match (capitalize (user-login-name)) t t))
179 '(end-of-line 1) " <" (progn user-mail-address) ">
180 ;; Keywords: "
181 '(require 'finder)
182 ;;'(setq v1 (apply 'vector (mapcar 'car finder-known-keywords)))
183 '(setq v1 (mapcar (lambda (x) (list (symbol-name (car x))))
184 finder-known-keywords)
185 v2 (mapconcat (lambda (x) (format "%12s: %s" (car x) (cdr x)))
186 finder-known-keywords
187 "\n"))
188 ((let ((minibuffer-help-form v2))
189 (completing-read "Keyword, C-h: " v1 nil t))
190 str ", ") & -2 "
192 \;; This program is free software; you can redistribute it and/or modify
193 \;; it under the terms of the GNU General Public License as published by
194 \;; the Free Software Foundation, either version 3 of the License, or
195 \;; (at your option) any later version.
197 \;; This program is distributed in the hope that it will be useful,
198 \;; but WITHOUT ANY WARRANTY; without even the implied warranty of
199 \;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
200 \;; GNU General Public License for more details.
202 \;; You should have received a copy of the GNU General Public License
203 \;; along with this program. If not, see <http://www.gnu.org/licenses/>.
205 \;;; Commentary:
207 \;; " _ "
209 \;;; Code:
213 \(provide '"
214 (file-name-base)
216 \;;; " (file-name-nondirectory (buffer-file-name)) " ends here\n")
217 (("\\.texi\\(nfo\\)?\\'" . "Texinfo file skeleton")
218 "Title: "
219 "\\input texinfo @c -*-texinfo-*-
220 @c %**start of header
221 @setfilename "
222 (file-name-base) ".info\n"
223 "@settitle " str "
224 @c %**end of header
225 @copying\n"
226 (setq short-description (read-string "Short description: "))
227 ".\n\n"
228 "Copyright @copyright{} " (format-time-string "%Y") " "
229 (getenv "ORGANIZATION") | (progn user-full-name) "
231 @quotation
232 Permission is granted to copy, distribute and/or modify this document
233 under the terms of the GNU Free Documentation License, Version 1.3
234 or any later version published by the Free Software Foundation;
235 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
236 A copy of the license is included in the section entitled ``GNU
237 Free Documentation License''.
239 A copy of the license is also available from the Free Software
240 Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.
242 @end quotation
244 The document was typeset with
245 @uref{http://www.texinfo.org/, GNU Texinfo}.
247 @end copying
249 @titlepage
250 @title " str "
251 @subtitle " short-description "
252 @author " (getenv "ORGANIZATION") | (progn user-full-name)
253 " <" (progn user-mail-address) ">
254 @page
255 @vskip 0pt plus 1filll
256 @insertcopying
257 @end titlepage
259 @c Output the table of the contents at the beginning.
260 @contents
262 @ifnottex
263 @node Top
264 @top " str "
266 @insertcopying
267 @end ifnottex
269 @c Generate the nodes for this menu with `C-c C-u C-m'.
270 @menu
271 @end menu
273 @c Update all node entries with `C-c C-u C-n'.
274 @c Insert new nodes with `C-c C-c n'.
275 @node Chapter One
276 @chapter Chapter One
278 " _ "
280 @node Copying This Manual
281 @appendix Copying This Manual
283 @menu
284 * GNU Free Documentation License:: License for copying this manual.
285 @end menu
287 @c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
288 @include fdl.texi
290 @node Index
291 @unnumbered Index
293 @printindex cp
295 @bye
297 @c " (file-name-nondirectory (buffer-file-name)) " ends here\n"))
298 "A list specifying text to insert by default into a new file.
299 Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION).
300 CONDITION may be a regexp that must match the new file's name, or it may be
301 a symbol that must match the major mode for this element to apply.
302 Only the first matching element is effective.
303 Optional DESCRIPTION is a string for filling `auto-insert-prompt'.
304 ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute
305 file-name or one relative to `auto-insert-directory' or a function to call.
306 ACTION may also be a vector containing several successive single actions as
307 described above, e.g. [\"header.insert\" date-and-author-update]."
308 :type '(alist :key-type
309 (choice (regexp :tag "Regexp matching file name")
310 (symbol :tag "Major mode")
311 (cons :tag "Condition and description"
312 (choice :tag "Condition"
313 (regexp :tag "Regexp matching file name")
314 (symbol :tag "Major mode"))
315 (string :tag "Description")))
316 ;; There's no custom equivalent of "repeat" for vectors.
317 :value-type (choice file function
318 (sexp :tag "Skeleton or vector")))
319 :version "25.1"
320 :group 'auto-insert)
323 ;; Establish a default value for auto-insert-directory
324 (defcustom auto-insert-directory "~/insert/"
325 "Directory from which auto-inserted files are taken.
326 The value must be an absolute directory name;
327 thus, on a GNU or Unix system, it must end in a slash."
328 :type 'directory
329 :group 'auto-insert)
332 ;;;###autoload
333 (defun auto-insert ()
334 "Insert default contents into new files if variable `auto-insert' is non-nil.
335 Matches the visited file name against the elements of `auto-insert-alist'."
336 (interactive)
337 (and (not buffer-read-only)
338 (or (eq this-command 'auto-insert)
339 (and auto-insert
340 (bobp) (eobp)))
341 (let ((alist auto-insert-alist)
342 case-fold-search cond desc action)
343 (goto-char 1)
344 ;; find first matching alist entry
345 (while alist
346 (if (atom (setq cond (car (car alist))))
347 (setq desc cond)
348 (setq desc (cdr cond)
349 cond (car cond)))
350 (if (if (symbolp cond)
351 (eq cond major-mode)
352 (and buffer-file-name
353 (string-match cond buffer-file-name)))
354 (setq action (cdr (car alist))
355 alist nil)
356 (setq alist (cdr alist))))
358 ;; Now, if we found something, do it
359 (and action
360 (or (not (stringp action))
361 (file-readable-p (expand-file-name
362 action auto-insert-directory)))
363 (or (not auto-insert-query)
364 (if (eq auto-insert-query 'function)
365 (eq this-command 'auto-insert))
366 (y-or-n-p (format auto-insert-prompt desc)))
367 (mapc
368 (lambda (action)
369 (if (stringp action)
370 (if (file-readable-p
371 (setq action (expand-file-name
372 action auto-insert-directory)))
373 (insert-file-contents action))
374 (save-window-excursion
375 ;; make buffer visible before skeleton or function
376 ;; which might ask the user for something
377 (switch-to-buffer (current-buffer))
378 (if (and (consp action)
379 (not (eq (car action) 'lambda)))
380 (skeleton-insert action)
381 (funcall action)))))
382 (if (vectorp action)
383 action
384 (vector action))))
385 (and (buffer-modified-p)
386 (not (eq this-command 'auto-insert))
387 (set-buffer-modified-p (eq auto-insert t)))))
388 ;; Return nil so that it could be used in
389 ;; `find-file-not-found-hooks', though that's probably inadvisable.
390 nil)
393 ;;;###autoload
394 (defun define-auto-insert (condition action &optional after)
395 "Associate CONDITION with (additional) ACTION in `auto-insert-alist'.
396 Optional AFTER means to insert action after all existing actions for CONDITION,
397 or if CONDITION had no actions, after all other CONDITIONs."
398 (let ((elt (assoc condition auto-insert-alist)))
399 (if elt
400 (setcdr elt
401 (if (vectorp (cdr elt))
402 (vconcat (if after (cdr elt))
403 (if (vectorp action) action (vector action))
404 (if after () (cdr elt)))
405 (if after
406 (vector (cdr elt) action)
407 (vector action (cdr elt)))))
408 (if after
409 (nconc auto-insert-alist (list (cons condition action)))
410 (push (cons condition action) auto-insert-alist)))))
412 ;;;###autoload
413 (define-minor-mode auto-insert-mode
414 "Toggle Auto-insert mode, a global minor mode.
415 With a prefix argument ARG, enable Auto-insert mode if ARG is
416 positive, and disable it otherwise. If called from Lisp, enable
417 the mode if ARG is omitted or nil.
419 When Auto-insert mode is enabled, when new files are created you can
420 insert a template for the file depending on the mode of the buffer."
421 :global t :group 'auto-insert
422 (if auto-insert-mode
423 (add-hook 'find-file-hook 'auto-insert)
424 (remove-hook 'find-file-hook 'auto-insert)))
426 (provide 'autoinsert)
428 ;;; autoinsert.el ends here