diff options
| author | Richard M. Stallman | 2005-08-11 19:49:33 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2005-08-11 19:49:33 +0000 |
| commit | 5e0fc18a86b0e12977195e20241ed67909cfc6dc (patch) | |
| tree | 31c18d5749fb7a42c6712b68e1f10a05af1a2c44 | |
| parent | 85b6b13c6cc865b81503fc2e0b632408ab60c931 (diff) | |
| download | emacs-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.texi | 310 |
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, | |||
| 68 | and send mail to @samp{bug-gnu-emacs@@gnu.org} suggesting we add | 71 | and send mail to @samp{bug-gnu-emacs@@gnu.org} suggesting we add |
| 69 | it to Emacs. If and when we do, we can change the name easily enough. | 72 | it to Emacs. If and when we do, we can change the name easily enough. |
| 70 | 73 | ||
| 71 | If one prefix is insufficient, your package may use two or three | 74 | If one prefix is insufficient, your package can use two or three |
| 72 | alternative common prefixes, so long as they make sense. | 75 | alternative common prefixes, so long as they make sense. |
| 73 | 76 | ||
| 74 | Separate the prefix from the rest of the symbol name with a hyphen, | 77 | Separate 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, | |||
| 76 | Lisp programs. | 79 | Lisp programs. |
| 77 | 80 | ||
| 78 | @item | 81 | @item |
| 79 | It is often useful to put a call to @code{provide} in each separate | 82 | Put a call to @code{provide} at the end of each separate Lisp file. |
| 80 | library program, at least if there is more than one entry point to the | ||
| 81 | program. | ||
| 82 | 83 | ||
| 83 | @item | 84 | @item |
| 84 | If a file requires certain other library programs to be loaded | 85 | If a file requires certain other Lisp programs to be loaded |
| 85 | beforehand, then the comments at the beginning of the file should say | 86 | beforehand, then the comments at the beginning of the file should say |
| 86 | so. Also, use @code{require} to make sure they are loaded. | 87 | so. 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 | |||
| 138 | follow the naming conventions for hooks. @xref{Hooks}. | 139 | follow the naming conventions for hooks. @xref{Hooks}. |
| 139 | 140 | ||
| 140 | @item | 141 | @item |
| 142 | @cindex unloading packages | ||
| 143 | If loading the file adds functions to hooks, define a function | ||
| 144 | @code{@var{feature}-unload-hook}, where @var{feature} is the name of | ||
| 145 | the feature the package provides, and make it undo any such changes. | ||
| 146 | Using @code{unload-feature} to unload the file will run this function. | ||
| 147 | @xref{Unloading}. | ||
| 148 | |||
| 149 | @item | ||
| 150 | It is a bad idea to define aliases for the Emacs primitives. Normally | ||
| 151 | you should use the standard names instead. The case where an alias | ||
| 152 | may be useful is where it facilitates backwards compatibility or | ||
| 153 | portability. | ||
| 154 | |||
| 155 | @item | ||
| 156 | If a package needs to define an alias or a new function for | ||
| 157 | compatibility with some other version of Emacs, name it with the package | ||
| 158 | prefix, not with the raw name with which it occurs in the other version. | ||
| 159 | Here is an example from Gnus, which provides many examples of such | ||
| 160 | compatibility 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 | ||
| 170 | Redefining (or advising) an Emacs primitive is discouraged. It may do | ||
| 171 | the right thing for a particular program, but there is no telling what | ||
| 172 | other programs might break as a result. | ||
| 173 | |||
| 174 | @item | ||
| 175 | If a file does replace any of the functions or library programs of | ||
| 176 | standard Emacs, prominent comments at the beginning of the file should | ||
| 177 | say which functions are replaced, and how the behavior of the | ||
| 178 | replacements differs from that of the originals. | ||
| 179 | |||
| 180 | @item | ||
| 181 | Avoid using macros that define functions and variables with names that | ||
| 182 | are constructed. It is best for maintenance when the name of the | ||
| 183 | function or variable being defined is given explicitly in the source | ||
| 184 | code, 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 | ||
| 188 | Please keep the names of your Emacs Lisp source files to 13 characters | ||
| 189 | or less. This way, if the files are compiled, the compiled files' names | ||
| 190 | will be 14 characters or less, which is short enough to fit on all kinds | ||
| 191 | of Unix systems. | ||
| 192 | |||
| 193 | @item | ||
| 194 | In some other systems there is a convention of choosing variable names | ||
| 195 | that begin and end with @samp{*}. We don't use that convention in Emacs | ||
| 196 | Lisp, so please don't use it in your programs. (Emacs uses such names | ||
| 197 | only for special-purpose buffers.) The users will find Emacs more | ||
| 198 | coherent if all libraries use the same conventions. | ||
| 199 | |||
| 200 | @item | ||
| 201 | Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the | ||
| 202 | default indentation parameters. | ||
| 203 | |||
| 204 | @item | ||
| 205 | Don't make a habit of putting close-parentheses on lines by themselves; | ||
| 206 | Lisp programmers find this disconcerting. Once in a while, when there | ||
| 207 | is a sequence of many consecutive close-parentheses, it may make sense | ||
| 208 | to split the sequence in one or two significant places. | ||
| 209 | |||
| 210 | @item | ||
| 211 | Please put a copyright notice and copying permission notice on the | ||
| 212 | file 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 | |||
| 233 | If you have signed papers to assign the copyright to the Foundation, | ||
| 234 | then use @samp{Free Software Foundation, Inc.} as @var{name}. | ||
| 235 | Otherwise, 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 | ||
| 245 | Special 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. | ||
| 247 | Modes such as Dired, Info, Compilation, and Occur redefine it in this | ||
| 248 | way. | ||
| 249 | |||
| 250 | In 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 |
| 143 | Please do not define @kbd{C-c @var{letter}} as a key in Lisp programs. | 256 | Please 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} | |||
| 199 | after @key{ESC}. In these states, you should define @kbd{@key{ESC} | 312 | after @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 | ||
| 206 | Special 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. | ||
| 208 | Modes such as Dired, Info, Compilation, and Occur redefine it in this | ||
| 209 | way. | ||
| 210 | |||
| 211 | In 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 | ||
| 215 | If loading the file adds functions to hooks, define a function | ||
| 216 | @code{@var{feature}-unload-hook}, where @var{feature} is the name of | ||
| 217 | the feature the package provides, and make it undo any such changes. | ||
| 218 | Using @code{unload-feature} to unload the file will run this function. | ||
| 219 | @xref{Unloading}. | ||
| 220 | |||
| 221 | @item | ||
| 222 | It is a bad idea to define aliases for the Emacs primitives. Use the | ||
| 223 | standard names instead. | ||
| 224 | |||
| 225 | @item | ||
| 226 | If a package needs to define an alias or a new function for | ||
| 227 | compatibility with some other version of Emacs, name it with the package | ||
| 228 | prefix, not with the raw name with which it occurs in the other version. | ||
| 229 | Here is an example from Gnus, which provides many examples of such | ||
| 230 | compatibility 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 | ||
| 240 | Redefining (or advising) an Emacs primitive is discouraged. It may do | ||
| 241 | the right thing for a particular program, but there is no telling what | ||
| 242 | other programs might break as a result. | ||
| 243 | |||
| 244 | @item | ||
| 245 | If a file does replace any of the functions or library programs of | ||
| 246 | standard Emacs, prominent comments at the beginning of the file should | ||
| 247 | say which functions are replaced, and how the behavior of the | ||
| 248 | replacements differs from that of the originals. | ||
| 249 | |||
| 250 | @item | ||
| 251 | Avoid using macros that define functions and variables with names that | ||
| 252 | are constructed. It is best for maintenance when the name of the | ||
| 253 | function or variable being defined is given explicitly in the source | ||
| 254 | code, 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 |
| 258 | Please keep the names of your Emacs Lisp source files to 13 characters | 321 | into Emacs when it runs. |
| 259 | or less. This way, if the files are compiled, the compiled files' names | ||
| 260 | will be 14 characters or less, which is short enough to fit on all kinds | ||
| 261 | of Unix systems. | ||
| 262 | 322 | ||
| 323 | @itemize @bullet | ||
| 263 | @item | 324 | @item |
| 264 | Don't use @code{next-line} or @code{previous-line} in programs; nearly | 325 | Don't use @code{next-line} or @code{previous-line} in programs; nearly |
| 265 | always, @code{forward-line} is more convenient as well as more | 326 | always, @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 | ||
| 283 | If you just want to move point, or replace a certain string, without any | 346 | If you just want to move point, or replace a certain string, or insert |
| 284 | of the other features intended for interactive users, you can replace | 347 | a file or buffer's contents, without any of the other features |
| 285 | these functions with one or two lines of simple Lisp code. | 348 | intended for interactive users, you can replace these functions with |
| 349 | one or two lines of simple Lisp code. | ||
| 286 | 350 | ||
| 287 | @item | 351 | @item |
| 288 | Use lists rather than vectors, except when there is a particular reason | 352 | Use 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 | |||
| 358 | to switch back to the old local keymap. Or do what the | 422 | to 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 |
| 360 | user switch back at will. @xref{Recursive Editing}. | 424 | user switch back at will. @xref{Recursive Editing}. |
| 361 | |||
| 362 | @item | ||
| 363 | In some other systems there is a convention of choosing variable names | ||
| 364 | that begin and end with @samp{*}. We don't use that convention in Emacs | ||
| 365 | Lisp, so please don't use it in your programs. (Emacs uses such names | ||
| 366 | only for special-purpose buffers.) The users will find Emacs more | ||
| 367 | coherent if all libraries use the same conventions. | ||
| 368 | |||
| 369 | @item | ||
| 370 | Try to avoid compiler warnings about undefined free variables, by adding | ||
| 371 | dummy @code{defvar} definitions for these variables, like this: | ||
| 372 | |||
| 373 | @example | ||
| 374 | (defvar foo) | ||
| 375 | @end example | ||
| 376 | |||
| 377 | Such a definition has no effect except to tell the compiler | ||
| 378 | not to warn about uses of the variable @code{foo} in this file. | ||
| 379 | |||
| 380 | @item | ||
| 381 | If you use many functions and variables from a certain file, you can | ||
| 382 | add a @code{require} for that package to avoid compilation warnings | ||
| 383 | for them. For instance, | ||
| 384 | |||
| 385 | @example | ||
| 386 | (eval-when-compile | ||
| 387 | (require 'foo)) | ||
| 388 | @end example | ||
| 389 | |||
| 390 | @item | ||
| 391 | If you bind a variable in one function, and use it or set it in | ||
| 392 | another function, the compiler warns about the latter function unless | ||
| 393 | the variable has a definition. But adding a definition would be | ||
| 394 | unclean if the variable has a short name, since Lisp packages should | ||
| 395 | not define short variable names. The right thing to do is to rename | ||
| 396 | this variable to start with the name prefix used for the other | ||
| 397 | functions and variables in your package. | ||
| 398 | |||
| 399 | @item | ||
| 400 | Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the | ||
| 401 | default indentation parameters. | ||
| 402 | |||
| 403 | @item | ||
| 404 | Don't make a habit of putting close-parentheses on lines by themselves; | ||
| 405 | Lisp programmers find this disconcerting. Once in a while, when there | ||
| 406 | is a sequence of many consecutive close-parentheses, it may make sense | ||
| 407 | to split the sequence in one or two significant places. | ||
| 408 | |||
| 409 | @item | ||
| 410 | Please put a copyright notice on the file if you give copies to anyone. | ||
| 411 | Use 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 | |||
| 432 | If you have signed papers to assign the copyright to the Foundation, | ||
| 433 | then use @samp{Free Software Foundation, Inc.} as @var{name}. | ||
| 434 | Otherwise, 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 | |||
| 495 | the speed. @xref{Inline Functions}. | 485 | the 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 | ||
| 493 | Try to avoid compiler warnings about undefined free variables, by adding | ||
| 494 | dummy @code{defvar} definitions for these variables, like this: | ||
| 495 | |||
| 496 | @example | ||
| 497 | (defvar foo) | ||
| 498 | @end example | ||
| 499 | |||
| 500 | Such a definition has no effect except to tell the compiler | ||
| 501 | not to warn about uses of the variable @code{foo} in this file. | ||
| 502 | |||
| 503 | @item | ||
| 504 | If you use many functions and variables from a certain file, you can | ||
| 505 | add a @code{require} for that package to avoid compilation warnings | ||
| 506 | for them. For instance, | ||
| 507 | |||
| 508 | @example | ||
| 509 | (eval-when-compile | ||
| 510 | (require 'foo)) | ||
| 511 | @end example | ||
| 512 | |||
| 513 | @item | ||
| 514 | If you bind a variable in one function, and use it or set it in | ||
| 515 | another function, the compiler warns about the latter function unless | ||
| 516 | the variable has a definition. But adding a definition would be | ||
| 517 | unclean if the variable has a short name, since Lisp packages should | ||
| 518 | not define short variable names. The right thing to do is to rename | ||
| 519 | this variable to start with the name prefix used for the other | ||
| 520 | functions and variables in your package. | ||
| 521 | |||
| 522 | @item | ||
| 523 | The last resort for avoiding a warning, when you want to do something | ||
| 524 | that usually is a mistake but it's not a mistake in this one case, | ||
| 525 | is 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 | ||