Mercurial > hg > xemacs-beta
comparison src/lisp.h @ 5277:d804e621add0
Simplify the API of PARSE_KEYWORDS for callers.
src/ChangeLog addition:
2010-09-18 Aidan Kehoe <kehoea@parhasard.net>
Simplify the API of PARSE_KEYWORDS for callers.
* lisp.h (PARSE_KEYWORDS): Simply the API, while making the
implementation a little more complex; work out KEYWORDS_OFFSET
from the appropriate Lisp_Subr struct, take the function name as
the C name of the DEFUN rather than a symbol visible as a
Lisp_Object, on debug builds assert that we're actually in the
function so we choke on badly-done copy-and-pasting,
* lisp.h (PARSE_KEYWORDS_8): New. This is the old PARSE_KEYWORDS.
* fns.c (Fmerge, FsortX, Ffill, Freduce, Freplace):
Change to use the new PARSE_KEYWORDS syntax.
* elhash.c (Fmake_hash_table): Chance to the new PARSE_KEYWORDS
syntax, rename a define to correspond to what other files use.
* symbols.c (intern_massaging_name):
* buffer.c (ADD_INT):
Rename intern_converting_underscores_to_dashes() to
intern_massaging_name(), now it does a little more.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 18 Sep 2010 15:57:20 +0100 |
parents | 808131ba4a57 |
children | d9e65b48e2bf |
comparison
equal
deleted
inserted
replaced
5276:dd2976af8783 | 5277:d804e621add0 |
---|---|
3492 /************************************************************************/ | 3492 /************************************************************************/ |
3493 /* Parsing keyword arguments */ | 3493 /* Parsing keyword arguments */ |
3494 /************************************************************************/ | 3494 /************************************************************************/ |
3495 | 3495 |
3496 /* The C subr must have been declared with MANY as its max args, and this | 3496 /* The C subr must have been declared with MANY as its max args, and this |
3497 PARSE_KEYWORDS call must come before any statements. | 3497 PARSE_KEYWORDS call must come before any statements. Equivalently, it |
3498 | 3498 can appear within braces. |
3499 FUNCTION is the name of the current function, as a symbol. | 3499 |
3500 FUNCTION is the C name of the current DEFUN. If there is no current | |
3501 DEFUN, use the PARSE_KEYWORDS_8 macro, not PARSE_KEYWORDS. If the | |
3502 current DEFUN has optional arguments that are not keywords, you also need | |
3503 to use the PARSE_KEYWORDS_8 macro. This is also the case if there are | |
3504 optional arguments that come before the keywords, as Common Lisp | |
3505 specifies for #'parse-integer. | |
3500 | 3506 |
3501 NARGS is the count of arguments supplied to FUNCTION. | 3507 NARGS is the count of arguments supplied to FUNCTION. |
3502 | 3508 |
3503 ARGS is a pointer to the argument vector (not a Lisp vector) supplied to | 3509 ARGS is a pointer to the argument vector (not a Lisp vector) supplied to |
3504 FUNCTION. | 3510 FUNCTION. |
3505 | |
3506 KEYWORDS_OFFSET is the offset into ARGS where the keyword arguments start. | |
3507 | 3511 |
3508 KEYWORD_COUNT is the number of keywords FUNCTION is normally prepared to | 3512 KEYWORD_COUNT is the number of keywords FUNCTION is normally prepared to |
3509 handle. | 3513 handle. |
3510 | 3514 |
3511 KEYWORDS is a parenthesised list of those keywords, without the initial | 3515 KEYWORDS is a parenthesised list of those keywords, without the initial |
3513 | 3517 |
3514 KEYWORD_DEFAULTS allows you to set non-nil defaults. Put (keywordname = | 3518 KEYWORD_DEFAULTS allows you to set non-nil defaults. Put (keywordname = |
3515 initial_value) in this parameter, a collection of C statements surrounded | 3519 initial_value) in this parameter, a collection of C statements surrounded |
3516 by parentheses and separated by the comma operator. If you don't need | 3520 by parentheses and separated by the comma operator. If you don't need |
3517 this, supply NULL as KEYWORD_DEFAULTS. | 3521 this, supply NULL as KEYWORD_DEFAULTS. |
3518 | |
3519 ALLOW_OTHER_KEYS corresponds to the &allow-other-keys argument list | |
3520 entry in defun*; it is 1 if other keys are normally allowed, 0 | |
3521 otherwise. This may be overridden in the caller by specifying | |
3522 :allow-other-keys t in the argument list. | |
3523 | 3522 |
3524 For keywords which appear multiple times in the called argument list, the | 3523 For keywords which appear multiple times in the called argument list, the |
3525 leftmost one overrides, as specified in section 7.1.1 of the CLHS. | 3524 leftmost one overrides, as specified in section 7.1.1 of the CLHS. |
3526 | 3525 |
3527 If you want to check whether a given keyword argument was set (as in the | 3526 If you want to check whether a given keyword argument was set (as in the |
3532 | 3531 |
3533 There is no elegant way with this macro to have one name for the keyword | 3532 There is no elegant way with this macro to have one name for the keyword |
3534 and an unrelated name for the local variable, as is possible with the | 3533 and an unrelated name for the local variable, as is possible with the |
3535 ((:keyword unrelated-var)) syntax in defun* and in Common Lisp. That | 3534 ((:keyword unrelated-var)) syntax in defun* and in Common Lisp. That |
3536 shouldn't matter in practice. */ | 3535 shouldn't matter in practice. */ |
3537 | 3536 #if defined (DEBUG_XEMACS) && defined (__STDC_VERSION__) && \ |
3538 #define PARSE_KEYWORDS(function, nargs, args, keywords_offset, \ | 3537 __STDC_VERSION__ >= 199901L |
3539 keyword_count, keywords, keyword_defaults, \ | 3538 |
3540 allow_other_keys) \ | 3539 /* This version has the advantage that DEFUN without DEFSUBR still provokes |
3540 a defined but not used warning, and it provokes an assertion failure at | |
3541 runtime if someone has copied and pasted the PARSE_KEYWORDS macro from | |
3542 another function without changing FUNCTION; that would lead to an | |
3543 incorrect determination of KEYWORDS_OFFSET. */ | |
3544 | |
3545 #define PARSE_KEYWORDS(function, nargs, args, keyword_count, keywords, \ | |
3546 keyword_defaults) \ | |
3547 PARSE_KEYWORDS_8 (intern_massaging_name (1 + #function), \ | |
3548 nargs, args, \ | |
3549 keyword_count, keywords, \ | |
3550 keyword_defaults, \ | |
3551 /* Can't XSUBR (Fsymbol_function (...))->min_args, \ | |
3552 the function may be advised. */ \ | |
3553 XINT (Ffunction_min_args \ | |
3554 (intern_massaging_name (1 + #function))), \ | |
3555 0); \ | |
3556 assert (0 == strcmp (__func__, #function)) | |
3557 #else | |
3558 #define PARSE_KEYWORDS(function, nargs, args, keyword_count, keywords, \ | |
3559 keyword_defaults) \ | |
3560 PARSE_KEYWORDS_8 (intern (S##function.name), nargs, args, \ | |
3561 keyword_count, keywords, \ | |
3562 keyword_defaults, S##function.min_args, 0) | |
3563 #endif | |
3564 | |
3565 /* PARSE_KEYWORDS_8 is a more fine-grained version of PARSE_KEYWORDS. The | |
3566 differences are as follows: | |
3567 | |
3568 FUNC_SYM is a symbol reflecting the name of the function for which | |
3569 keywords are being parsed. In PARSE_KEYWORDS, it is the Lisp-visible | |
3570 name of C_FUNC, interned as a symbol in obarray. | |
3571 | |
3572 KEYWORDS_OFFSET is the offset into ARGS where the keyword arguments | |
3573 start. In PARSE_KEYWORDS, this is the index of the first optional | |
3574 argument, determined from the information known about C_FUNC. | |
3575 | |
3576 ALLOW_OTHER_KEYS corresponds to the &allow-other-keys argument list entry | |
3577 in defun*; it is 1 if other keys are normally allowed, 0 otherwise. This | |
3578 may be overridden in the caller by specifying :allow-other-keys t in the | |
3579 argument list. In PARSE_KEYWORDS, ALLOW_OTHER_KEYS is always 0. */ | |
3580 | |
3581 #define PARSE_KEYWORDS_8(func_sym, nargs, args, \ | |
3582 keyword_count, keywords, keyword_defaults, \ | |
3583 keywords_offset, allow_other_keys) \ | |
3541 DECLARE_N_KEYWORDS_##keyword_count keywords; \ | 3584 DECLARE_N_KEYWORDS_##keyword_count keywords; \ |
3542 \ | 3585 \ |
3543 do \ | 3586 do \ |
3544 { \ | 3587 { \ |
3545 Lisp_Object pk_key, pk_value; \ | 3588 Lisp_Object pk_key, pk_value; \ |
3546 Elemcount pk_i = nargs - 1; \ | 3589 Elemcount pk_i = nargs - 1, pk_offset = keywords_offset; \ |
3547 Boolint pk_allow_other_keys = allow_other_keys; \ | 3590 Boolint pk_allow_other_keys = allow_other_keys; \ |
3548 \ | 3591 \ |
3549 if ((nargs - keywords_offset) & 1) \ | 3592 if ((nargs - pk_offset) & 1) \ |
3550 { \ | 3593 { \ |
3551 if (!allow_other_keys \ | 3594 if (!allow_other_keys \ |
3552 && !(pk_allow_other_keys \ | 3595 && !(pk_allow_other_keys \ |
3553 = non_nil_allow_other_keys_p (keywords_offset, \ | 3596 = non_nil_allow_other_keys_p (pk_offset, \ |
3554 nargs, args))) \ | 3597 nargs, args))) \ |
3555 { \ | 3598 { \ |
3556 signal_wrong_number_of_arguments_error (function, nargs); \ | 3599 signal_wrong_number_of_arguments_error (func_sym, nargs); \ |
3557 } \ | 3600 } \ |
3558 else \ | 3601 else \ |
3559 { \ | 3602 { \ |
3560 /* Ignore the trailing arg; so below always sees an even \ | 3603 /* Ignore the trailing arg; so below always sees an even \ |
3561 number of arguments. */ \ | 3604 number of arguments. */ \ |
3564 } \ | 3607 } \ |
3565 \ | 3608 \ |
3566 (void)(keyword_defaults); \ | 3609 (void)(keyword_defaults); \ |
3567 \ | 3610 \ |
3568 /* Start from the end, because the leftmost element overrides. */ \ | 3611 /* Start from the end, because the leftmost element overrides. */ \ |
3569 while (pk_i > keywords_offset) \ | 3612 while (pk_i > pk_offset) \ |
3570 { \ | 3613 { \ |
3571 pk_value = args[pk_i--]; \ | 3614 pk_value = args[pk_i--]; \ |
3572 pk_key = args[pk_i--]; \ | 3615 pk_key = args[pk_i--]; \ |
3573 \ | 3616 \ |
3574 if (0) {} \ | 3617 if (0) {} \ |
3576 else if (allow_other_keys || pk_allow_other_keys) \ | 3619 else if (allow_other_keys || pk_allow_other_keys) \ |
3577 { \ | 3620 { \ |
3578 continue; \ | 3621 continue; \ |
3579 } \ | 3622 } \ |
3580 else if ((pk_allow_other_keys \ | 3623 else if ((pk_allow_other_keys \ |
3581 = non_nil_allow_other_keys_p (keywords_offset, \ | 3624 = non_nil_allow_other_keys_p (pk_offset, \ |
3582 nargs, args))) \ | 3625 nargs, args))) \ |
3583 { \ | 3626 { \ |
3584 continue; \ | 3627 continue; \ |
3585 } \ | 3628 } \ |
3586 else if (EQ (pk_key, Q_allow_other_keys) && \ | 3629 else if (EQ (pk_key, Q_allow_other_keys) && \ |
3588 { \ | 3631 { \ |
3589 continue; \ | 3632 continue; \ |
3590 } \ | 3633 } \ |
3591 else \ | 3634 else \ |
3592 { \ | 3635 { \ |
3593 invalid_keyword_argument (function, pk_key); \ | 3636 invalid_keyword_argument (func_sym, pk_key); \ |
3594 } \ | 3637 } \ |
3595 } \ | 3638 } \ |
3596 } while (0) | 3639 } while (0) |
3597 | 3640 |
3598 #define DECLARE_N_KEYWORDS_1(a) \ | 3641 #define DECLARE_N_KEYWORDS_1(a) \ |
5647 EXFUN (Fsymbol_value, 1); | 5690 EXFUN (Fsymbol_value, 1); |
5648 | 5691 |
5649 unsigned int hash_string (const Ibyte *, Bytecount); | 5692 unsigned int hash_string (const Ibyte *, Bytecount); |
5650 Lisp_Object intern_istring (const Ibyte *str); | 5693 Lisp_Object intern_istring (const Ibyte *str); |
5651 MODULE_API Lisp_Object intern (const CIbyte *str); | 5694 MODULE_API Lisp_Object intern (const CIbyte *str); |
5652 Lisp_Object intern_converting_underscores_to_dashes (const CIbyte *str); | 5695 Lisp_Object intern_massaging_name (const CIbyte *str); |
5653 Lisp_Object oblookup (Lisp_Object, const Ibyte *, Bytecount); | 5696 Lisp_Object oblookup (Lisp_Object, const Ibyte *, Bytecount); |
5654 void map_obarray (Lisp_Object, int (*) (Lisp_Object, void *), void *); | 5697 void map_obarray (Lisp_Object, int (*) (Lisp_Object, void *), void *); |
5655 Lisp_Object indirect_function (Lisp_Object, int); | 5698 Lisp_Object indirect_function (Lisp_Object, int); |
5656 Lisp_Object symbol_value_in_buffer (Lisp_Object, Lisp_Object); | 5699 Lisp_Object symbol_value_in_buffer (Lisp_Object, Lisp_Object); |
5657 void kill_buffer_local_variables (struct buffer *); | 5700 void kill_buffer_local_variables (struct buffer *); |