-
Notifications
You must be signed in to change notification settings - Fork 0
/
clisp-user-commands.lisp
526 lines (487 loc) · 18 KB
/
clisp-user-commands.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: clisp-user-commands.lisp
;;;;LANGUAGE: Common-Lisp
;;;;SYSTEM: Common-Lisp
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Utilities to help creating clisp REPL user commands.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2014-10-18 <PJB> Extracted from clisprc.lisp
;;;;BUGS
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal J. Bourguignon 2014 - 2014
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
(defpackage "COM.INFORMATIMAGO.CLISP.USER-COMMANDS"
(:use "COMMON-LISP" "SPLIT-SEQUENCE")
(:export "DEFINE-USER-COMMAND" "DELETE-USER-COMMAND"))
(in-package "COM.INFORMATIMAGO.CLISP.USER-COMMANDS")
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *user-command-groups* (make-hash-table :test (function equal)))
(defun ensure-list (item) (if (listp item) item (list item)))
(defstruct command names lambda-list docstring body))
;;(setf custom:*user-commands* nil)
(defun read-arguments (stream &key (prompt "> ") (prompt-function nil))
"
DO: Reads from the stream a line and parses the arguments
separated by spaces. Argument characters may be quoted
with double-quotes, single-quotes or backspaces, and may
stand on several lines.
RETURN: A list of string, the parsed arguments.
"
(loop
:with prompt-function = (or prompt-function
(lambda (state)
(case state
((:escape-out)
(format stream "~&\\~A" prompt))
((:escape-in-double :string-double)
(format stream "~&\"~A" prompt))
((:escape-in-single :string-single)
(format stream "~&'~A" prompt))
(otherwise
(format stream "~&~A" prompt)))))
:with line = "\\"
:with state = :initial
:with arguments = '()
:with buffer = nil
:with i = 0
;; :do (print `(:line ,line :i ,i :state ,state
;; :arguments ,arguments
;; :buffer ,buffer))
:do (if (< i (length line))
(let ((ch (prog1 (aref line i) (incf i))))
(case state
(:out
(case ch
((#\") (setf state :string-double))
((#\') (setf state :string-single))
((#\\) (setf state :escape-out))
((#\space) (when buffer
(push (coerce (nreverse buffer) 'string)
arguments)
(setf buffer nil)))
(otherwise (if buffer
(push ch buffer)
(setf buffer (list ch))))))
(:string-double
(case ch
((#\") (setf state :out))
((#\\) (setf state :escape-in-double))
(otherwise (if buffer
(push ch buffer)
(setf buffer (list ch))))))
(:string-single
(case ch
((#\') (setf state :out))
((#\\) (setf state :escape-in-single))
(otherwise (if buffer
(push ch buffer)
(setf buffer (list ch))))))
(:escape-out
(if buffer
(push ch buffer)
(setf buffer (list ch)))
(setf state :out))
(:escape-in-double
(if buffer
(push ch buffer)
(setf buffer (list ch)))
(setf state :string-double))
(:escape-in-single
(if buffer
(push ch buffer)
(setf buffer (list ch)))
(setf state :string-single))))
(progn
(case state
(:out
(when buffer
(push (coerce (nreverse buffer) 'string)
arguments)
(setf buffer nil))
(return (nreverse arguments)))
((:initial :escape-out)
(funcall prompt-function state)
(setf state :out))
((:escape-in-double :escape-in-single)
(funcall prompt-function state)
(if buffer
(push #\newline buffer)
(setf buffer (list #\newline))))
(otherwise
(funcall prompt-function state)))
(finish-output stream)
(setf line (read-line stream)
i 0)))))
(defun parse-arguments (line)
(with-input-from-string (inp line)
(with-input-from-string (nl (string #\Newline))
(read-arguments (make-two-way-stream (make-concatenated-stream inp nl *query-io*) *query-io*)))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun generate-user-commands ()
"DO: sets CUSTOM:*USER-COMMANDS* to the commands generated from *USER-COMMAND-GROUPS*."
(setf custom:*user-commands*
(let ((commands '()))
(maphash
(lambda (category command-list)
(dolist (command command-list)
(push (coerce
(let ((vfun (gensym))
(varg (gensym)))
`(lambda ()
(flet ((,vfun (,varg)
(handler-case
(flet ((,(first (command-names command)) ,(command-lambda-list command)
,@(command-body command)))
(apply (function ,(first (command-names command)))
(parse-arguments ,varg)))
(error (err) (format t "~&~A~%" err)))))
(list
,(format nil "~%~:@(~{~S ~}~) ~A" (command-names command) (command-docstring command))
,@(mapcar (lambda (name)
`(cons ,(format nil "~(~S~)" name)
(function ,vfun)))
(command-names command))))))
'function) commands))
(push (coerce
`(lambda () (list ,(format nil "~2%~A:" category)))
'function) commands))
*user-command-groups*)
commands))
(values)))
(defmacro define-user-command (names category command-lambda-list docstring &body body)
"Defines a user command
NAMES: a designator for a list of symbols by which the command can be invoked."
(let ((names (ensure-list names)))
`(let ((entry (find ',names (gethash ',category *user-command-groups* '())
:test (function equal)
:key (function command-names))))
(if entry
(setf (command-lambda-list entry) ',command-lambda-list
(command-docstring entry) ',docstring
(command-body entry) ',body)
(push (make-command :names ',names
:lambda-list ',command-lambda-list
:docstring ',docstring
:body ',body)
(gethash ',category *user-command-groups* '())))
(generate-user-commands)
',(first names))))
(defun delete-user-command (name)
"Delete the user command with the given NAME.
If the command was defined with several names, it's removed under ALL its names.
If the last command in a category is removed, then the category is removed."
(maphash (lambda (category commands)
(setf commands (delete name commands
:test (function member)
:key (function command-names)))
(if commands
(setf (gethash category *user-command-groups*) commands)
(remhash category *user-command-groups*)))
*user-command-groups*)
(generate-user-commands))
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-user-command (date) "interactive commands" ()
"prints the date."
(com.informatimago.common-lisp.interactive.interactive:date))
(define-user-command (uptime) "interactive commands" ()
"prints the uptime."
(com.informatimago.common-lisp.interactive.interactive:uptime))
(define-user-command (pwd) "interactive commands" ()
"print the working directory."
(format t "~&~a~%" (com.informatimago.common-lisp.interactive.interactive:pwd)))
(define-user-command (cd) "interactive commands" (directory)
"change working directory."
(com.informatimago.common-lisp.interactive.interactive:cd directory))
(define-user-command (ls) "interactive commands" (&rest options)
"list files."
(apply (function com.informatimago.common-lisp.interactive.interactive:ls) options))
(define-user-command (cat less more) "interactive commands" (&rest files)
"catenate files."
(apply (function com.informatimago.common-lisp.interactive.interactive:cat) files))
(define-user-command (panic :pa) "user-defined commands" ()
"hit the panic button!"
(format t "don't panic, ~d~%" (random 42)))
(defparameter *insults*
'(("accapareur" (nm))
("aérolithe" (nm))
("amiral de bateaulavoir" (gnm))
("amphitryon" (nm))
("anacoluthe" (nf))
("analphabète" (n a))
("anthracite" (nm))
("anthropophage" (nm a))
("anthropopithèque" (nm))
("apache" (nm))
("apprenti dictateur à la noix de coco" (gnm))
("arlequin" (nm))
("astronaute d'eau douce" (gn))
("athlète complet" (n))
("autocrate" (nm))
("autodidacte" (n a))
("azteque" (nm))
("babouin" (nm))
("bachibouzouk" (nm))
("bande de" (|...|))
("bandit" (nm))
("bayadère" (nf))
("bibendum" (nm))
("boitsanssoif" (ni))
("brontosaure" (nm))
("bougre de" (|...|))
("brute" (nf))
("bulldozer à réaction" (gnm))
("vieux" (a))
("cachalot" (nm))
("canaille" (nf))
("canaque" (nm a))
("cannibale" (nm))
("carnaval" (nm))
("catachrèse" (nf))
("cataplasme" (nm))
("cercopithèque" (nm))
("chauffard" (nm))
("chenapan" (nm))
("choléra" (nm))
("chouette mal empaillée" (gnf))
("cloporte" (nm))
("clysopompe" (nm))
("coléoptère" (nm))
("coloquinte" (nf))
("coquin" (n a))
("cornemuse" (nf))
("cornichon" (nm))
("corsaire" (nm))
("coupejarret" (nm))
("cowboy de la route" (gnm))
("crétin des alpes" (gnm))
("Cromagnon" (np))
("cyanure" (nm))
("cyclone" (nm))
("cyclotron" (nm))
("Cyrano à quatre pattes" (gnm))
("diablesse" (nf))
("diplodocus" (nm))
("doryphore" (nm))
("dynamiteur" (nm))
("ecornifleur" (nm))
("ecraseur" (nm))
("ectoplasme" (nm))
("egoïste" (nm))
("emplatre" (nm))
("empoisonneur" (nm a))
("enragé" (nm a))
("épouvantail" (nm))
("équilibriste" (nm))
("esclavagiste" (nm))
("escogriffe" (nm))
("espèce de" (|...|))
("extrait de" (|...|))
("faux jeton" (nm))
("flibustier" (nm))
("forban" (nm))
("frères de la côte" (gnm))
("froussard" (nm a))
("galopin" (nm))
("gangster" (nm))
("gardecôte à la mie de pain" (gnm))
("gargarisme" (nm))
("garnement" (nm))
("gibier de potence" (nm))
("goujat" (nm))
("gredin" (nm))
("grenouille" (nf))
("gros plein de soupe" (gnm))
("gyroscope" (nm))
("hérétique" (n a))
("horslaloi" (nm))
("huluberlu" (nm))
("hydrocarbure" (nm))
("iconoclaste" (nm a))
("incas de carnaval" (gnmp))
("individou de général" (gnm))
("invertébré" (nm))
("ivrogne" (n))
("jet d'eau ambulant" (gnm))
("jocrisse" (nm))
("judas" (nm))
("jus de réglisse" (gnm))
("kroumir" (nm))
("ku klux klan" (gnm))
("lâche" (nm))
("lépidoptère" (nm))
("logarithme" (nm))
("loupgarou à la graisse de renoncule" (gnm))
("macaque" (nm))
("macrocéphale" (nm))
("malappris" (n a))
("malotru" (n))
("mamelouk" (nm))
("marchand de guano" (gnm))
("marchand de tapis" (gnm))
("marin d'eau douce" (gnm))
("marmotte" (nf))
("mégalomane" (nm a))
("mégacycle" (nm))
("mercanti" (nm))
("mercenaire" (nm a))
("mérinos" (nm))
("mille sabords" (gnmp))
("misérable" (a))
("mitrailleur à bavette" (gnm))
("moratorium" (nm))
("moricaud" (nm a))
("mouchard" (nm))
("moujik" (nm))
("moule à gaufres" (gnm))
("moussaillon" (nm))
("mrkrpxzkrmtfrz" (nm))
("mufle" (nm))
("Mussolini de carnaval" (nm))
("naufrageur" (nm))
("négrier" (nm))
("noix de coco" (gnm))
("nyctalope" (n a))
("olibrius" (nm))
("ophicléïde" (nm))
("ornithorynque" (nm))
("oryctérope" (nm))
("ostrogoth" (n a))
("ours mal lèché" (gnm))
("pacte à quatre" (gnm))
("paltoquet" (nm))
("pantoufle" (nf))
("Papous" (nm))
("paranoïaque" (nm a))
("parasite" (nm a))
("Patagon" (nm))
("patapouf" (nm))
("patate" (nf))
("péronnelle" (nf))
("perruche bavarde" (gnf))
("phénomène" (nm))
("phlébotome" (nm))
("phylactère" (nm))
("phylloxéra" (nm))
("pignouf" (nm))
("pirate" (nm))
("Polichinelle" (nm))
("polygraphe" (nm))
("porcépic mal embouché" (gnm))
("potentat emplumé" (gnm))
("poussière" (nf))
("profiteur" (nm))
("projectile guidé" (gnm))
("protozoaire" (nm))
("pyromane" (nm))
("pyrophore" (nm))
("rapace" (nm))
("rat" (nm))
("Ravachol" (nm))
("renégat" (nm))
("rhizopode" (nm))
("Rocambole" (nm))
("sacripant" (nm))
("sajou" (nm))
("saltimbanque" (nm))
("sapajou" (nm))
("satané bazar de fourbi de truc" (gnm))
("satrape" (nm))
("sauvage" (n a))
("scélérat" (nm))
("schizophrène" (n a))
("scolopendre" (nf))
("scorpion" (nm))
("serpent" (nm))
("simili martien à la graisse de cabestan" (gnm))
("sinapisme" (nm))
("soulographe" (nm))
("squatter" (nm))
("tchouktchouknougat" (nm))
("technocrate" (nm))
("tête de lard" (gnf))
("tête de mule" (gnf))
("tigresse" (nf))
("tonnerre de Brest" (gnm))
("topinanbour" (nm))
("tortionnaire" (nm))
("traficant de chair humaine" (gnm))
("trainepotence" (nm))
("traitre" (nm a))
("troglodyte" (nm))
("trompelamort" (nm))
("vampire" (nm))
("vandale" (nm a))
("vanupieds" (nm))
("vaurien" (nm))
("végétarien" (nm))
("Vercingétorix de carnaval" (nm))
("ver de terre" (gnm))
("vermicelle" (nm))
("vermine" (nm))
("vipère" (nf))
("vivisectionniste" (nm))
("voleur" (nm))
("wisigoth" (n a))
("zapotèque" (nm))
("zèbre" (nm))
("zigomar" (nm))
("zouave" (nm))
("Zoulou" (nm))))
(defparameter nm (remove-if-not (lambda (x) (intersection '(n np nm gnm) (second x))) *insults*))
(defparameter nf (remove-if-not (lambda (x) (intersection '(nf np) (second x))) *insults*))
(defparameter ad (remove-if-not (lambda (x) (member 'a (second x))) *insults*))
(defparameter *insulte-random-state* (make-random-state t))
(defun insulte ()
(let* ((*random-state* *insulte-random-state*)
(ga (format nil " ~A" (first (elt ad (random (length ad))))))
(gn (let ((n (random (+ (length nf) (length nm)))))
(if (>= n (length nm))
(prog1
(first (elt nf (- n (length nm))))
(cond
((= 0 (length ga)))
((string= "e" ga :start2 (1- (length ga))))
((string= "eux" ga :start2 (- (length ga) 3))
(setf ga (concatenate 'string
(subseq ga 0 (- (length ga) 2)) "ille")))
((string= "eur" ga :start2 (- (length ga) 3))
(setf ga (concatenate 'string
(subseq ga 0 (1- (length ga))) "se")))
(t
(setf ga (concatenate 'string ga "e")))))
(first (elt nm n)))))
(conj (if (position (aref gn 0) "aeiouyh") "d'" "de "))
(ins (case (random 4)
((0) (concatenate 'string "espèce " conj gn ga))
((1) (concatenate 'string "bande " conj gn ga))
(otherwise (concatenate 'string gn ga)))))
(concatenate 'string
(string-capitalize (subseq ins 0 1))
(subseq ins 1)
" !")))
(define-user-command (swear :sw) "user-defined commands" ()
"curse"
(format t "~^~A~%" (insulte)))
;;;; THE END ;;;;