1 ;;; guile-c.el --- Guile C editing commands
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to the
17 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 ;; (add-hook 'c-mode-hook
25 ;; (define-key c-mode-map "\C-c\C-g\C-e" 'guile-c-edit-docstring)
26 ;; (define-key c-mode-map "\C-c\C-g\C-p" 'guile-c-insert-define)
27 ;; (define-key c-mode-map "\C-c\C-g\C-d" 'guile-c-deprecate-region)
34 (defvar guile-c-prefix
"scm_")
41 (defun guile-c-insert-define ()
42 "Insert a template of a Scheme procedure.
44 M-x guile-c-insert-define RET foo arg , opt . rest =>
46 SCM_DEFINE (scm_foo, \"foo\", 1, 1, 1,
47 (SCM arg, SCM opt, SCM rest),
49 #define FUNC_NAME s_scm_foo
55 (let ((tokens (split-string (read-string "Procedure: ")))
58 (if (not tokens
) (error "No procedure name"))
59 (setq name
(car tokens
) tokens
(cdr tokens
))
60 ;; Get requisite arguments
61 (while (and tokens
(not (member (car tokens
) '("," "."))))
62 (setq args
(cons (car tokens
) args
) tokens
(cdr tokens
)))
63 (setq args
(nreverse args
))
64 ;; Get optional arguments
65 (when (string= (car tokens
) ",")
66 (setq tokens
(cdr tokens
))
67 (while (and tokens
(not (string= (car tokens
) ".")))
68 (setq opts
(cons (car tokens
) opts
) tokens
(cdr tokens
)))
69 (setq opts
(nreverse opts
)))
71 (when (string= (car tokens
) ".")
72 (setq rest
(list (cadr tokens
))))
74 (let ((c-name (guile-c-name-from-scheme-name name
)))
75 (insert (format "SCM_DEFINE (%s, \"%s\", %d, %d, %d,\n"
76 c-name name
(length args
) (length opts
) (length rest
))
78 (mapconcat (lambda (a) (concat "SCM " a
))
79 (append args opts rest
) ", ")
82 "#define FUNC_NAME s_" c-name
"\n"
84 "#undef FUNC_NAME\n\n")
86 (indent-for-tab-command))))
88 (defun guile-c-name-from-scheme-name (name)
89 (while (string-match "\\?$" name
) (setq name
(replace-match "_p" t t name
)))
90 (while (string-match "!$" name
) (setq name
(replace-match "_x" t t name
)))
91 (while (string-match "^%" name
) (setq name
(replace-match "sys_" t t name
)))
92 (while (string-match "->" name
) (setq name
(replace-match "_to_" t t name
)))
93 (while (string-match "[-:]" name
) (setq name
(replace-match "_" t t name
)))
94 (concat guile-c-prefix name
))
101 (defun guile-c-edit-docstring ()
103 (let* ((region (guile-c-find-docstring))
104 (doc (if region
(buffer-substring (car region
) (cdr region
)))))
106 (error "No docstring!")
107 (with-current-buffer (get-buffer-create "*Guile Docstring*")
110 (goto-char (point-min))
112 (if (looking-at "[ \t]*\"")
113 (delete-region (match-beginning 0) (match-end 0)))
115 (if (eq (char-before (point)) ?
\")
116 (delete-backward-char 1))
117 (if (and (eq (char-before (point)) ?n
)
118 (eq (char-before (1- (point))) ?
\\))
119 (delete-backward-char 2))
121 (goto-char (point-min))
123 (if global-font-lock-mode
124 (font-lock-fontify-buffer))
125 (local-set-key "\C-c\C-c" 'guile-c-edit-finish
)
126 (switch-to-buffer-other-window (current-buffer))
127 (message "Type `C-c C-c' to finish")))))
129 (defun guile-c-edit-finish ()
131 (goto-char (point-max))
132 (while (eq (char-before) ?
\n) (backward-delete-char 1))
133 (goto-char (point-min))
139 (insert (if (eobp) "\"" "\\n\""))
141 (let ((doc (buffer-string)))
142 (kill-buffer (current-buffer))
143 (delete-window (selected-window))
144 (let ((region (guile-c-find-docstring)))
145 (goto-char (car region
))
146 (delete-region (car region
) (cdr region
)))
149 (defun guile-c-find-docstring ()
151 (if (re-search-backward "^SCM_DEFINE" nil t
)
152 (let ((start (progn (forward-line 2) (point))))
153 (while (looking-at "[ \t]*\"")
155 (cons start
(- (point) 2))))))
162 (defun guile-c-deprecate-region (start end
)
165 (let ((marker (make-marker)))
166 (set-marker marker end
)
168 (insert "#if (SCM_DEBUG_DEPRECATED == 0)\n\n")
170 (insert "\n#endif /* (SCM_DEBUG_DEPRECATED == 0) */\n"))))
174 ;; guile-c.el ends here