aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2005-08-11 19:49:33 +0000
committerRichard M. Stallman2005-08-11 19:49:33 +0000
commit5e0fc18a86b0e12977195e20241ed67909cfc6dc (patch)
tree31c18d5749fb7a42c6712b68e1f10a05af1a2c44
parent85b6b13c6cc865b81503fc2e0b632408ab60c931 (diff)
downloademacs-5e0fc18a86b0e12977195e20241ed67909cfc6dc.tar.gz
emacs-5e0fc18a86b0e12977195e20241ed67909cfc6dc.zip
(Key Binding Conventions, Programming Tips, Warning Tips):
New nodes split out of Coding Conventions.
-rw-r--r--lispref/tips.texi310
1 files changed, 170 insertions, 140 deletions
diff --git a/lispref/tips.texi b/lispref/tips.texi
index e37c9c3d999..46eb887dce8 100644
--- a/lispref/tips.texi
+++ b/lispref/tips.texi
@@ -23,7 +23,10 @@ all.
23 23
24@menu 24@menu
25* Coding Conventions:: Conventions for clean and robust programs. 25* Coding Conventions:: Conventions for clean and robust programs.
26* Key Binding Conventions:: Which keys should be bound by which programs.
27* Programming Tips:: Making Emacs code fit smoothly in Emacs.
26* Compilation Tips:: Making compiled code run fast. 28* Compilation Tips:: Making compiled code run fast.
29* Warning Tips:: Turning off compiler warnings.
27* Documentation Tips:: Writing readable documentation strings. 30* Documentation Tips:: Writing readable documentation strings.
28* Comment Tips:: Conventions for writing comments. 31* Comment Tips:: Conventions for writing comments.
29* Library Headers:: Standard headers for library packages. 32* Library Headers:: Standard headers for library packages.
@@ -68,7 +71,7 @@ in your program. Call it @code{mylib-twiddle-files} in your program,
68and send mail to @samp{bug-gnu-emacs@@gnu.org} suggesting we add 71and send mail to @samp{bug-gnu-emacs@@gnu.org} suggesting we add
69it to Emacs. If and when we do, we can change the name easily enough. 72it to Emacs. If and when we do, we can change the name easily enough.
70 73
71If one prefix is insufficient, your package may use two or three 74If one prefix is insufficient, your package can use two or three
72alternative common prefixes, so long as they make sense. 75alternative common prefixes, so long as they make sense.
73 76
74Separate the prefix from the rest of the symbol name with a hyphen, 77Separate the prefix from the rest of the symbol name with a hyphen,
@@ -76,12 +79,10 @@ Separate the prefix from the rest of the symbol name with a hyphen,
76Lisp programs. 79Lisp programs.
77 80
78@item 81@item
79It is often useful to put a call to @code{provide} in each separate 82Put a call to @code{provide} at the end of each separate Lisp file.
80library program, at least if there is more than one entry point to the
81program.
82 83
83@item 84@item
84If a file requires certain other library programs to be loaded 85If a file requires certain other Lisp programs to be loaded
85beforehand, then the comments at the beginning of the file should say 86beforehand, then the comments at the beginning of the file should say
86so. Also, use @code{require} to make sure they are loaded. 87so. Also, use @code{require} to make sure they are loaded.
87 88
@@ -138,6 +139,118 @@ to store a list of functions (i.e., the variable is a hook), please
138follow the naming conventions for hooks. @xref{Hooks}. 139follow the naming conventions for hooks. @xref{Hooks}.
139 140
140@item 141@item
142@cindex unloading packages
143If loading the file adds functions to hooks, define a function
144@code{@var{feature}-unload-hook}, where @var{feature} is the name of
145the feature the package provides, and make it undo any such changes.
146Using @code{unload-feature} to unload the file will run this function.
147@xref{Unloading}.
148
149@item
150It is a bad idea to define aliases for the Emacs primitives. Normally
151you should use the standard names instead. The case where an alias
152may be useful is where it facilitates backwards compatibility or
153portability.
154
155@item
156If a package needs to define an alias or a new function for
157compatibility with some other version of Emacs, name it with the package
158prefix, not with the raw name with which it occurs in the other version.
159Here is an example from Gnus, which provides many examples of such
160compatibility issues.
161
162@example
163(defalias 'gnus-point-at-bol
164 (if (fboundp 'point-at-bol)
165 'point-at-bol
166 'line-beginning-position))
167@end example
168
169@item
170Redefining (or advising) an Emacs primitive is discouraged. It may do
171the right thing for a particular program, but there is no telling what
172other programs might break as a result.
173
174@item
175If a file does replace any of the functions or library programs of
176standard Emacs, prominent comments at the beginning of the file should
177say which functions are replaced, and how the behavior of the
178replacements differs from that of the originals.
179
180@item
181Avoid using macros that define functions and variables with names that
182are constructed. It is best for maintenance when the name of the
183function or variable being defined is given explicitly in the source
184code, as the second element of the list---as it is when you use
185@code{defun}, @code{defalias}, @code{defvar} and @code{defcustom}.
186
187@item
188Please keep the names of your Emacs Lisp source files to 13 characters
189or less. This way, if the files are compiled, the compiled files' names
190will be 14 characters or less, which is short enough to fit on all kinds
191of Unix systems.
192
193@item
194In some other systems there is a convention of choosing variable names
195that begin and end with @samp{*}. We don't use that convention in Emacs
196Lisp, so please don't use it in your programs. (Emacs uses such names
197only for special-purpose buffers.) The users will find Emacs more
198coherent if all libraries use the same conventions.
199
200@item
201Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the
202default indentation parameters.
203
204@item
205Don't make a habit of putting close-parentheses on lines by themselves;
206Lisp programmers find this disconcerting. Once in a while, when there
207is a sequence of many consecutive close-parentheses, it may make sense
208to split the sequence in one or two significant places.
209
210@item
211Please put a copyright notice and copying permission notice on the
212file if you distribute copies. Use a notice like this one:
213
214@smallexample
215;; Copyright (C) @var{year} @var{name}
216
217;; This program is free software; you can redistribute it and/or
218;; modify it under the terms of the GNU General Public License as
219;; published by the Free Software Foundation; either version 2 of
220;; the License, or (at your option) any later version.
221
222;; This program is distributed in the hope that it will be
223;; useful, but WITHOUT ANY WARRANTY; without even the implied
224;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
225;; PURPOSE. See the GNU General Public License for more details.
226
227;; You should have received a copy of the GNU General Public
228;; License along with this program; if not, write to the Free
229;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
230;; MA 02110-1301 USA
231@end smallexample
232
233If you have signed papers to assign the copyright to the Foundation,
234then use @samp{Free Software Foundation, Inc.} as @var{name}.
235Otherwise, use your name. See also @xref{Library Headers}.
236@end itemize
237
238@node Key Binding Conventions
239@section Key Binding Conventions
240
241@itemize @bullet
242@item
243@cindex mouse-2
244@cindex references, following
245Special major modes used for read-only text should usually redefine
246@kbd{mouse-2} and @key{RET} to trace some sort of reference in the text.
247Modes such as Dired, Info, Compilation, and Occur redefine it in this
248way.
249
250In addition, they should mark the text as a kind of ``link'' so that
251@kbd{mouse-1} will follow it also. @xref{Links and Mouse-1}.
252
253@item
141@cindex reserved keys 254@cindex reserved keys
142@cindex keys, reserved 255@cindex keys, reserved
143Please do not define @kbd{C-c @var{letter}} as a key in Lisp programs. 256Please do not define @kbd{C-c @var{letter}} as a key in Lisp programs.
@@ -199,67 +312,15 @@ is potentially meaningful, then you must not define @kbd{@key{ESC}
199after @key{ESC}. In these states, you should define @kbd{@key{ESC} 312after @key{ESC}. In these states, you should define @kbd{@key{ESC}
200@key{ESC} @key{ESC}} as the way to escape. Otherwise, define 313@key{ESC} @key{ESC}} as the way to escape. Otherwise, define
201@kbd{@key{ESC} @key{ESC}} instead. 314@kbd{@key{ESC} @key{ESC}} instead.
315@end itemize
202 316
203@item 317@node Programming Tips
204@cindex mouse-2 318@section Emacs Programming Tips
205@cindex references, following
206Special major modes used for read-only text should usually redefine
207@kbd{mouse-2} and @key{RET} to trace some sort of reference in the text.
208Modes such as Dired, Info, Compilation, and Occur redefine it in this
209way.
210
211In addition, they should mark the text as a kind of ``link'' so that
212@kbd{mouse-1} will follow it also. @xref{Links and Mouse-1}.
213
214@cindex unloading packages
215If loading the file adds functions to hooks, define a function
216@code{@var{feature}-unload-hook}, where @var{feature} is the name of
217the feature the package provides, and make it undo any such changes.
218Using @code{unload-feature} to unload the file will run this function.
219@xref{Unloading}.
220
221@item
222It is a bad idea to define aliases for the Emacs primitives. Use the
223standard names instead.
224
225@item
226If a package needs to define an alias or a new function for
227compatibility with some other version of Emacs, name it with the package
228prefix, not with the raw name with which it occurs in the other version.
229Here is an example from Gnus, which provides many examples of such
230compatibility issues.
231
232@example
233(defalias 'gnus-point-at-bol
234 (if (fboundp 'point-at-bol)
235 'point-at-bol
236 'line-beginning-position))
237@end example
238
239@item
240Redefining (or advising) an Emacs primitive is discouraged. It may do
241the right thing for a particular program, but there is no telling what
242other programs might break as a result.
243
244@item
245If a file does replace any of the functions or library programs of
246standard Emacs, prominent comments at the beginning of the file should
247say which functions are replaced, and how the behavior of the
248replacements differs from that of the originals.
249
250@item
251Avoid using macros that define functions and variables with names that
252are constructed. It is best for maintenance when the name of the
253function or variable being defined is given explicitly in the source
254code, as the second element of the list---as it is when you use
255@code{defun}, @code{defalias}, @code{defvar} and @code{defcustom}.
256 319
257@item 320 Following these conventions will make your program fit better
258Please keep the names of your Emacs Lisp source files to 13 characters 321into Emacs when it runs.
259or less. This way, if the files are compiled, the compiled files' names
260will be 14 characters or less, which is short enough to fit on all kinds
261of Unix systems.
262 322
323@itemize @bullet
263@item 324@item
264Don't use @code{next-line} or @code{previous-line} in programs; nearly 325Don't use @code{next-line} or @code{previous-line} in programs; nearly
265always, @code{forward-line} is more convenient as well as more 326always, @code{forward-line} is more convenient as well as more
@@ -278,11 +339,14 @@ In particular, don't use any of these functions:
278@code{beginning-of-buffer}, @code{end-of-buffer} 339@code{beginning-of-buffer}, @code{end-of-buffer}
279@item 340@item
280@code{replace-string}, @code{replace-regexp} 341@code{replace-string}, @code{replace-regexp}
342@item
343@code{insert-file}, @code{insert-buffer}
281@end itemize 344@end itemize
282 345
283If you just want to move point, or replace a certain string, without any 346If you just want to move point, or replace a certain string, or insert
284of the other features intended for interactive users, you can replace 347a file or buffer's contents, without any of the other features
285these functions with one or two lines of simple Lisp code. 348intended for interactive users, you can replace these functions with
349one or two lines of simple Lisp code.
286 350
287@item 351@item
288Use lists rather than vectors, except when there is a particular reason 352Use lists rather than vectors, except when there is a particular reason
@@ -358,80 +422,6 @@ command does: use a new local keymap that contains one command defined
358to switch back to the old local keymap. Or do what the 422to switch back to the old local keymap. Or do what the
359@code{edit-options} command does: switch to another buffer and let the 423@code{edit-options} command does: switch to another buffer and let the
360user switch back at will. @xref{Recursive Editing}. 424user switch back at will. @xref{Recursive Editing}.
361
362@item
363In some other systems there is a convention of choosing variable names
364that begin and end with @samp{*}. We don't use that convention in Emacs
365Lisp, so please don't use it in your programs. (Emacs uses such names
366only for special-purpose buffers.) The users will find Emacs more
367coherent if all libraries use the same conventions.
368
369@item
370Try to avoid compiler warnings about undefined free variables, by adding
371dummy @code{defvar} definitions for these variables, like this:
372
373@example
374(defvar foo)
375@end example
376
377Such a definition has no effect except to tell the compiler
378not to warn about uses of the variable @code{foo} in this file.
379
380@item
381If you use many functions and variables from a certain file, you can
382add a @code{require} for that package to avoid compilation warnings
383for them. For instance,
384
385@example
386(eval-when-compile
387 (require 'foo))
388@end example
389
390@item
391If you bind a variable in one function, and use it or set it in
392another function, the compiler warns about the latter function unless
393the variable has a definition. But adding a definition would be
394unclean if the variable has a short name, since Lisp packages should
395not define short variable names. The right thing to do is to rename
396this variable to start with the name prefix used for the other
397functions and variables in your package.
398
399@item
400Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the
401default indentation parameters.
402
403@item
404Don't make a habit of putting close-parentheses on lines by themselves;
405Lisp programmers find this disconcerting. Once in a while, when there
406is a sequence of many consecutive close-parentheses, it may make sense
407to split the sequence in one or two significant places.
408
409@item
410Please put a copyright notice on the file if you give copies to anyone.
411Use a message like this one:
412
413@smallexample
414;; Copyright (C) @var{year} @var{name}
415
416;; This program is free software; you can redistribute it and/or
417;; modify it under the terms of the GNU General Public License as
418;; published by the Free Software Foundation; either version 2 of
419;; the License, or (at your option) any later version.
420
421;; This program is distributed in the hope that it will be
422;; useful, but WITHOUT ANY WARRANTY; without even the implied
423;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
424;; PURPOSE. See the GNU General Public License for more details.
425
426;; You should have received a copy of the GNU General Public
427;; License along with this program; if not, write to the Free
428;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
429;; MA 02110-1301 USA
430@end smallexample
431
432If you have signed papers to assign the copyright to the Foundation,
433then use @samp{Free Software Foundation, Inc.} as @var{name}.
434Otherwise, use your name. See also @xref{Library Headers}.
435@end itemize 425@end itemize
436 426
437@node Compilation Tips 427@node Compilation Tips
@@ -495,6 +485,46 @@ a noticeable speedup in something slow enough that users care about
495the speed. @xref{Inline Functions}. 485the speed. @xref{Inline Functions}.
496@end itemize 486@end itemize
497 487
488@node Warning Tips
489@section Tips for Avoiding Compiler Warnings
490
491@itemize @bullet
492@item
493Try to avoid compiler warnings about undefined free variables, by adding
494dummy @code{defvar} definitions for these variables, like this:
495
496@example
497(defvar foo)
498@end example
499
500Such a definition has no effect except to tell the compiler
501not to warn about uses of the variable @code{foo} in this file.
502
503@item
504If you use many functions and variables from a certain file, you can
505add a @code{require} for that package to avoid compilation warnings
506for them. For instance,
507
508@example
509(eval-when-compile
510 (require 'foo))
511@end example
512
513@item
514If you bind a variable in one function, and use it or set it in
515another function, the compiler warns about the latter function unless
516the variable has a definition. But adding a definition would be
517unclean if the variable has a short name, since Lisp packages should
518not define short variable names. The right thing to do is to rename
519this variable to start with the name prefix used for the other
520functions and variables in your package.
521
522@item
523The last resort for avoiding a warning, when you want to do something
524that usually is a mistake but it's not a mistake in this one case,
525is to put a call to @code{with-no-warnings} around it.
526@end itemize
527
498@node Documentation Tips 528@node Documentation Tips
499@section Tips for Documentation Strings 529@section Tips for Documentation Strings
500 530