comparison lispref/tips.texi @ 6959:3b19456b877a

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Tue, 19 Apr 1994 01:27:51 +0000
parents 3b84ed22f747
children c5927c75b2b5
comparison
equal deleted inserted replaced
6958:6962dec6fe55 6959:3b19456b877a
99 The reason for this rule is that a non-prefix binding for @key{ESC} in 99 The reason for this rule is that a non-prefix binding for @key{ESC} in
100 any context prevents recognition of escape sequences as function keys in 100 any context prevents recognition of escape sequences as function keys in
101 that context. 101 that context.
102 102
103 @item 103 @item
104 Applications should not bind mouse events based on button 1 with the
105 shift key held down. These events include @kbd{S-mouse-1},
106 @kbd{M-S-mouse-1}, @kbd{C-S-mouse-1}, and so on. They are reserved for
107 users.
108
109 @item
110 Modes should redefine @kbd{mouse-2} as a command to follow some sort of
111 reference in the text of a buffer, if users usually would not want to
112 alter the text in that buffer by hand. Modes such as Dired, Info,
113 Compilation, and Occur redefine it in this way.
114
115 @item
104 It is a bad idea to define aliases for the Emacs primitives. 116 It is a bad idea to define aliases for the Emacs primitives.
105 Use the standard names instead. 117 Use the standard names instead.
106 118
107 @item 119 @item
108 Redefining an Emacs primitive is an even worse idea. 120 Redefining an Emacs primitive is an even worse idea.
161 173
162 Do not use @code{message}, @code{throw}, @code{sleep-for}, 174 Do not use @code{message}, @code{throw}, @code{sleep-for},
163 or @code{beep} to report errors. 175 or @code{beep} to report errors.
164 176
165 @item 177 @item
166 Avoid using recursive edits. Instead, do what the Rmail @kbd{w} command 178 Try to avoid using recursive edits. Instead, do what the Rmail @kbd{e}
167 does: use a new local keymap that contains one command defined to 179 command does: use a new local keymap that contains one command defined
168 switch back to the old local keymap. Or do what the @code{edit-options} 180 to switch back to the old local keymap. Or do what the
169 command does: switch to another buffer and let the user switch back at 181 @code{edit-options} command does: switch to another buffer and let the
170 will. @xref{Recursive Editing}. 182 user switch back at will. @xref{Recursive Editing}.
171 183
172 @item 184 @item
173 In some other systems there is a convention of choosing variable names 185 In some other systems there is a convention of choosing variable names
174 that begin and end with @samp{*}. We don't use that convention in Emacs 186 that begin and end with @samp{*}. We don't use that convention in Emacs
175 Lisp, so please don't use it in your library. (In fact, in Emacs names 187 Lisp, so please don't use it in your programs. (Emacs uses such names
176 of this form are conventionally used for program-generated buffers.) The 188 only for program-generated buffers.) The users will find Emacs more
177 users will find Emacs more coherent if all libraries use the same 189 coherent if all libraries use the same conventions.
178 conventions.
179 190
180 @item 191 @item
181 Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the 192 Indent each function with @kbd{C-M-q} (@code{indent-sexp}) using the
182 default indentation parameters. 193 default indentation parameters.
183 194
199 @section Tips for Making Compiled Code Fast 210 @section Tips for Making Compiled Code Fast
200 @cindex execution speed 211 @cindex execution speed
201 @cindex speedups 212 @cindex speedups
202 213
203 Here are ways of improving the execution speed of byte-compiled 214 Here are ways of improving the execution speed of byte-compiled
204 lisp programs. 215 Lisp programs.
205 216
206 @itemize @bullet 217 @itemize @bullet
207 @item 218 @item
208 @cindex profiling 219 @cindex profiling
209 @cindex timing programs 220 @cindex timing programs
221 @code{assoc} is even faster than explicit iteration. It may be worth 232 @code{assoc} is even faster than explicit iteration. It may be worth
222 rearranging a data structure so that one of these primitive search 233 rearranging a data structure so that one of these primitive search
223 functions can be used. 234 functions can be used.
224 235
225 @item 236 @item
226 Certain built-in functions are handled specially by the byte compiler 237 Certain built-in functions are handled specially in byte-compiled code,
227 avoiding the need for an ordinary function call. It is a good idea to 238 avoiding the need for an ordinary function call. It is a good idea to
228 use these functions rather than alternatives. To see whether a function 239 use these functions rather than alternatives. To see whether a function
229 is handled specially by the compiler, examine its @code{byte-compile} 240 is handled specially by the compiler, examine its @code{byte-compile}
230 property. If the property is non-@code{nil}, then the function is 241 property. If the property is non-@code{nil}, then the function is
231 handled specially. 242 handled specially.
232 243
233 For example, the following input will show you that @code{aref} is 244 For example, the following input will show you that @code{aref} is
234 compiled specially (@pxref{Array Functions}) while @code{elt} is not 245 compiled specially (@pxref{Array Functions}) while @code{elt} is not
235 (@pxref{Sequence Functions}): 246 (@pxref{Sequence Functions}):
236 247
237 @smallexample 248 @example
238 @group 249 @group
239 (get 'aref 'byte-compile) 250 (get 'aref 'byte-compile)
240 @result{} byte-compile-two-args 251 @result{} byte-compile-two-args
241 @end group 252 @end group
242 253
243 @group 254 @group
244 (get 'elt 'byte-compile) 255 (get 'elt 'byte-compile)
245 @result{} nil 256 @result{} nil
246 @end group 257 @end group
247 @end smallexample 258 @end example
248 259
249 @item 260 @item
250 If calling a small function accounts for a substantial part of your 261 If calling a small function accounts for a substantial part of your
251 program's running time, make the function inline. This eliminates 262 program's running time, make the function inline. This eliminates
252 the function call overhead. Since making a function inline reduces 263 the function call overhead. Since making a function inline reduces
253 the flexibility of changing the program, don't do it unless it gives 264 the flexibility of changing the program, don't do it unless it gives
254 a noticeable speedup in something slow enough for users to care about 265 a noticeable speedup in something slow enough that users care about
255 the speed. @xref{Inline Functions}. 266 the speed. @xref{Inline Functions}.
256 @end itemize 267 @end itemize
257 268
258 @node Documentation Tips 269 @node Documentation Tips
259 @section Tips for Documentation Strings 270 @section Tips for Documentation Strings
269 An internal subroutine of a Lisp program need not have a documentation 280 An internal subroutine of a Lisp program need not have a documentation
270 string, and you can save space by using a comment instead. 281 string, and you can save space by using a comment instead.
271 282
272 @item 283 @item
273 The first line of the documentation string should consist of one or two 284 The first line of the documentation string should consist of one or two
274 complete sentences which stand on their own as a summary. In particular, 285 complete sentences which stand on their own as a summary. @kbd{M-x
275 start the line with a capital letter and end with a period. 286 apropos} displays just the first line, and if it doesn't stand on its
276 For instance, use ``Return the cons of A and B.'' in preference to 287 own, the result looks bad. In particular, start the first line with a
277 ``Returns the cons of A and B@.'' 288 capital letter and end with a period.
278 289
279 The documentation string can have additional lines which expand on the 290 The documentation string can have additional lines which expand on the
280 details of how to use the function or variable. The additional lines 291 details of how to use the function or variable. The additional lines
281 should be made up of complete sentences also, but they may be filled if 292 should be made up of complete sentences also, but they may be filled if
282 that looks good. 293 that looks good.
294
295 @item
296 For consistency, phrase the verb in the first sentence of a
297 documentation string as an infinitive with ``to'' omitted. For
298 instance, use ``Return the cons of A and B.'' in preference to ``Returns
299 the cons of A and B@.'' Usually it looks good to do likewise for the
300 rest of the first paragraph. Subsequent paragraphs usually look better
301 if they have proper subjects.
283 302
284 @item 303 @item
285 Write documentation strings in the active voice, not the passive, and in 304 Write documentation strings in the active voice, not the passive, and in
286 the present tense, not the future. For instance, use ``Return a list 305 the present tense, not the future. For instance, use ``Return a list
287 containing A and B.'' instead of ``A list containing A and B will be 306 containing A and B.'' instead of ``A list containing A and B will be
312 view the documentation. Remember that the indentation before the 331 view the documentation. Remember that the indentation before the
313 starting double-quote is not part of the string! 332 starting double-quote is not part of the string!
314 333
315 @item 334 @item
316 A variable's documentation string should start with @samp{*} if the 335 A variable's documentation string should start with @samp{*} if the
317 variable is one that users would want to set interactively often. If 336 variable is one that users would often want to set interactively. If
318 the value is a long list, or a function, or if the variable would only 337 the value is a long list, or a function, or if the variable would only
319 be set in init files, then don't start the documentation string with 338 be set in init files, then don't start the documentation string with
320 @samp{*}. @xref{Defining Variables}. 339 @samp{*}. @xref{Defining Variables}.
321 340
322 @item 341 @item
323 The documentation string for a variable that is a yes-or-no flag should 342 The documentation string for a variable that is a yes-or-no flag should
324 start with words such as ``Non-nil means@dots{}'', to make it clear both 343 start with words such as ``Non-nil means@dots{}'', to make it clear that
325 that the variable only has two meaningfully distinct values and which value 344 all non-@code{nil} values are equivalent and indicate explicitly what
326 means ``yes''. 345 @code{nil} and non-@code{nil} mean.
327 346
328 @item 347 @item
329 When a function's documentation string mentions the value of an argument 348 When a function's documentation string mentions the value of an argument
330 of the function, use the argument name in capital letters as if it were 349 of the function, use the argument name in capital letters as if it were
331 a name for that value. Thus, the documentation string of the function 350 a name for that value. Thus, the documentation string of the function
332 @code{/} refers to its second argument as @samp{DIVISOR}. 351 @code{/} refers to its second argument as @samp{DIVISOR}, because the
352 actual argument name is @code{divisor}.
333 353
334 Also use all caps for meta-syntactic variables, such as when you show 354 Also use all caps for meta-syntactic variables, such as when you show
335 the decomposition of a list or vector into subunits, some of which may 355 the decomposition of a list or vector into subunits, some of which may
336 vary. 356 vary.
337 357
351 @end ifinfo 371 @end ifinfo
352 372
353 @item 373 @item
354 Don't write key sequences directly in documentation strings. Instead, 374 Don't write key sequences directly in documentation strings. Instead,
355 use the @samp{\\[@dots{}]} construct to stand for them. For example, 375 use the @samp{\\[@dots{}]} construct to stand for them. For example,
356 instead of writing @samp{C-f}, write @samp{\\[forward-char]}. When the 376 instead of writing @samp{C-f}, write @samp{\\[forward-char]}. When
357 documentation string is printed, Emacs will substitute whatever key is 377 Emacs displays the documentation string, it substitutes whatever key is
358 currently bound to @code{forward-char}. This will usually be 378 currently bound to @code{forward-char}. (This is normally @samp{C-f},
359 @samp{C-f}, but if the user has moved key bindings, it will be the 379 but it may be some other character if the user has moved key bindings.)
360 correct key for that user. @xref{Keys in Documentation}. 380 @xref{Keys in Documentation}.
361 381
362 @item 382 @item
363 In documentation strings for a major mode, you will want to refer to the 383 In documentation strings for a major mode, you will want to refer to the
364 key bindings of that mode's local map, rather than global ones. 384 key bindings of that mode's local map, rather than global ones.
365 Therefore, use the construct @samp{\\<@dots{}>} once in the 385 Therefore, use the construct @samp{\\<@dots{}>} once in the
389 Comments that start with a single semicolon, @samp{;}, should all be 409 Comments that start with a single semicolon, @samp{;}, should all be
390 aligned to the same column on the right of the source code. Such 410 aligned to the same column on the right of the source code. Such
391 comments usually explain how the code on the same line does its job. In 411 comments usually explain how the code on the same line does its job. In
392 Lisp mode and related modes, the @kbd{M-;} (@code{indent-for-comment}) 412 Lisp mode and related modes, the @kbd{M-;} (@code{indent-for-comment})
393 command automatically inserts such a @samp{;} in the right place, or 413 command automatically inserts such a @samp{;} in the right place, or
394 aligns such a comment if it is already inserted. 414 aligns such a comment if it is already present.
395 415
396 (The following examples are taken from the Emacs sources.) 416 (The following examples are taken from the Emacs sources.)
397 417
398 @smallexample 418 @smallexample
399 @group 419 @group
404 @end group 424 @end group
405 @end smallexample 425 @end smallexample
406 426
407 @item ;; 427 @item ;;
408 Comments that start with two semicolons, @samp{;;}, should be aligned to 428 Comments that start with two semicolons, @samp{;;}, should be aligned to
409 the same level of indentation as the code. Such comments are used to 429 the same level of indentation as the code. Such comments usually
410 describe the purpose of the following lines or the state of the program 430 describe the purpose of the following lines or the state of the program
411 at that point. For example: 431 at that point. For example:
412 432
413 @smallexample 433 @smallexample
414 @group 434 @group
415 (prog1 (setq auto-fill-function 435 (prog1 (setq auto-fill-function
416 @dots{} 436 @dots{}
417 @dots{} 437 @dots{}
418 ;; update mode-line 438 ;; update mode line
419 (force-mode-line-update))) 439 (force-mode-line-update)))
420 @end group 440 @end group
421 @end smallexample 441 @end smallexample
422 442
423 These comments are also written before a function definition to explain 443 Every function that has no documentation string (because it is use only
424 what the function does and how to call it properly. 444 internally within the package it belongs to), should have instead a
445 two-semicolon comment right before the function, explaining what the
446 function does and how to call it properly. Explain precisely what each
447 argument means and how the function interprets its possible value.
425 448
426 @item ;;; 449 @item ;;;
427 Comments that start with three semicolons, @samp{;;;}, should start at 450 Comments that start with three semicolons, @samp{;;;}, should start at
428 the left margin. Such comments are not used within function 451 the left margin. Such comments are used outside function definitions to
429 definitions, but are used to make more general comments. For example: 452 make general statements explaining the design principles of the program.
453 For example:
430 454
431 @smallexample 455 @smallexample
432 @group 456 @group
433 ;;; This Lisp code is run in Emacs 457 ;;; This Lisp code is run in Emacs
434 ;;; when it is to operate as a server 458 ;;; when it is to operate as a server
435 ;;; for other processes. 459 ;;; for other processes.
436 @end group 460 @end group
461 @end smallexample
462
463 Another use for triple-semicolon comments is for commenting out line
464 within a function. We use triple-semicolons for this precisely so that
465 they remain at the left margin.
466
467 @smallexample
468 (defun foo (a)
469 ;;; This is no longer necessary.
470 ;;; (force-mode-line-update)
471 (message "Finished with %s" a))
437 @end smallexample 472 @end smallexample
438 473
439 @item ;;;; 474 @item ;;;;
440 Comments that start with four semicolons, @samp{;;;;}, should be aligned 475 Comments that start with four semicolons, @samp{;;;;}, should be aligned
441 to the left margin and are used for headings of major sections of a 476 to the left margin and are used for headings of major sections of a
451 (@code{indent-for-comment}) and @key{TAB} (@code{lisp-indent-line}) 486 (@code{indent-for-comment}) and @key{TAB} (@code{lisp-indent-line})
452 automatically indent comments according to these conventions, 487 automatically indent comments according to these conventions,
453 depending on the the number of semicolons. @xref{Comments,, 488 depending on the the number of semicolons. @xref{Comments,,
454 Manipulating Comments, emacs, The GNU Emacs Manual}. 489 Manipulating Comments, emacs, The GNU Emacs Manual}.
455 490
456 If you wish to ``comment out'' a number of lines of code, use triple
457 semicolons at the beginnings of the lines.
458
459 Any character may be included in a comment, but it is advisable to
460 precede a character with syntactic significance in Lisp (such as
461 @samp{\} or unpaired @samp{(} or @samp{)}) with a @samp{\}, to prevent
462 it from confusing the Emacs commands for editing Lisp.
463
464 @node Library Headers 491 @node Library Headers
465 @section Conventional Headers for Emacs Libraries 492 @section Conventional Headers for Emacs Libraries
466 @cindex header comments 493 @cindex header comments
467 @cindex library header comments 494 @cindex library header comments
468 495
497 524
498 @noindent 525 @noindent
499 The description should be complete in one line. 526 The description should be complete in one line.
500 527
501 After the copyright notice come several @dfn{header comment} lines, 528 After the copyright notice come several @dfn{header comment} lines,
502 each beginning with @samp{;;; @var{header-name}:}. Here is a table of 529 each beginning with @samp{;; @var{header-name}:}. Here is a table of
503 the conventional possibilities for @var{header-name}: 530 the conventional possibilities for @var{header-name}:
504 531
505 @table @samp 532 @table @samp
506 @item Author 533 @item Author
507 This line states the name and net address of at least the principal 534 This line states the name and net address of at least the principal
508 author of the library. 535 author of the library.
509 536
510 If there are multiple authors, you can list them on continuation lines 537 If there are multiple authors, you can list them on continuation lines
511 led by @code{;;<TAB>}, like this: 538 led by @code{;;} and a tab character, like this:
512 539
513 @smallexample 540 @smallexample
514 @group 541 @group
515 ;; Author: Ashwin Ram <Ram-Ashwin@@cs.yale.edu> 542 ;; Author: Ashwin Ram <Ram-Ashwin@@cs.yale.edu>
516 ;; Dave Sill <de5@@ornl.gov> 543 ;; Dave Sill <de5@@ornl.gov>
517 ;; Dave Brennan <brennan@@hal.com> 544 ;; Dave Brennan <brennan@@hal.com>
518 ;; Eric Raymond <esr@@snark.thyrsus.com> 545 ;; Eric Raymond <esr@@snark.thyrsus.com>
519 @end group 546 @end group
520 @end smallexample 547 @end smallexample
521 548
522 @item Maintainer 549 @item Maintainer
523 This line should contain a single name/address as in the Author line, or 550 This line should contain a single name/address as in the Author line, or
524 an address only, or the string ``FSF''. If there is no maintainer line, 551 an address only, or the string @samp{FSF}. If there is no maintainer
525 the person(s) in the Author field are presumed to be the maintainers. 552 line, the person(s) in the Author field are presumed to be the
526 The example above is mildly bogus because the maintainer line is 553 maintainers. The example above is mildly bogus because the maintainer
527 redundant. 554 line is redundant.
528 555
529 The idea behind the @samp{Author} and @samp{Maintainer} lines is to make 556 The idea behind the @samp{Author} and @samp{Maintainer} lines is to make
530 possible a Lisp function to ``send mail to the maintainer'' without 557 possible a Lisp function to ``send mail to the maintainer'' without
531 having to mine the name out by hand. 558 having to mine the name out by hand.
532 559