Add `ChangeLog-2008' files to the distribution.
[bpt/guile.git] / libguile / keywords.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
fca75708 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
fca75708 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
fca75708 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
fca75708
MD
19\f
20
13070bd3
DH
21#include <string.h>
22
a0599745 23#include "libguile/_scm.h"
4e047c3e 24#include "libguile/async.h"
a0599745
MD
25#include "libguile/ports.h"
26#include "libguile/root.h"
27#include "libguile/smob.h"
00ffa0e7 28#include "libguile/hashtab.h"
a0599745
MD
29
30#include "libguile/validate.h"
31#include "libguile/keywords.h"
cc95e00a
MV
32#include "libguile/strings.h"
33
fca75708
MD
34\f
35
92c2555f 36scm_t_bits scm_tc16_keyword;
e841c3e0 37
265a7997
MV
38#define KEYWORDP(X) (SCM_SMOB_PREDICATE (scm_tc16_keyword, (X)))
39#define KEYWORDSYM(X) (SCM_SMOB_OBJECT (X))
40
fca75708 41static int
e81d98ec 42keyword_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
fca75708
MD
43{
44 scm_puts ("#:", port);
265a7997 45 scm_display (KEYWORDSYM (exp), port);
fca75708
MD
46 return 1;
47}
48
265a7997
MV
49SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0,
50 (SCM obj),
51 "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
52 "@code{#f}.")
53#define FUNC_NAME s_scm_keyword_p
54{
55 return scm_from_bool (KEYWORDP (obj));
56}
57#undef FUNC_NAME
58
59SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
60 (SCM symbol),
61 "Return the keyword with the same name as @var{symbol}.")
62#define FUNC_NAME s_scm_symbol_to_keyword
fca75708 63{
86d31dfe 64 SCM keyword;
fca75708 65
d7212289 66 SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
fca75708 67
9de87eea 68 SCM_CRITICAL_SECTION_START;
a61b2054 69 /* njrev: NEWSMOB and hashq_set_x can raise errors */
86d31dfe 70 keyword = scm_hashq_ref (scm_keyword_obarray, symbol, SCM_BOOL_F);
7888309b 71 if (scm_is_false (keyword))
fca75708 72 {
54778cd3 73 SCM_NEWSMOB (keyword, scm_tc16_keyword, SCM_UNPACK (symbol));
86d31dfe 74 scm_hashq_set_x (scm_keyword_obarray, symbol, keyword);
fca75708 75 }
9de87eea 76 SCM_CRITICAL_SECTION_END;
86d31dfe 77 return keyword;
fca75708 78}
1bbd0b84 79#undef FUNC_NAME
fca75708 80
265a7997
MV
81SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
82 (SCM keyword),
83 "Return the symbol with the same name as @var{keyword}.")
84#define FUNC_NAME s_scm_keyword_to_symbol
430a6cc3 85{
265a7997
MV
86 scm_assert_smob_type (scm_tc16_keyword, keyword);
87 return KEYWORDSYM (keyword);
430a6cc3 88}
265a7997 89#undef FUNC_NAME
430a6cc3 90
265a7997
MV
91int
92scm_is_keyword (SCM val)
fca75708 93{
265a7997 94 return KEYWORDP (val);
fca75708
MD
95}
96
265a7997
MV
97SCM
98scm_from_locale_keyword (const char *str)
fca75708 99{
265a7997 100 return scm_symbol_to_keyword (scm_from_locale_symbol (str));
fca75708 101}
fca75708 102
265a7997
MV
103SCM
104scm_from_locale_keywordn (const char *str, size_t len)
105{
106 return scm_symbol_to_keyword (scm_from_locale_symboln (str, len));
107}
fca75708 108
a61b2054 109/* njrev: critical sections reviewed so far up to here */
fca75708
MD
110void
111scm_init_keywords ()
112{
e841c3e0
KN
113 scm_tc16_keyword = scm_make_smob_type ("keyword", 0);
114 scm_set_smob_mark (scm_tc16_keyword, scm_markcdr);
115 scm_set_smob_print (scm_tc16_keyword, keyword_print);
f5f2dcff 116
231a4ea8 117 scm_keyword_obarray = scm_c_make_hash_table (0);
a0599745 118#include "libguile/keywords.x"
fca75708
MD
119}
120
89e00824
ML
121
122/*
123 Local Variables:
124 c-file-style: "gnu"
125 End:
126*/