Debian Codegraph @

elisp/cafeobj-mode.el

raw
1 ;;; cafeobj-mode.el --- CafeOBJ code editing and Interaction with
2 ;;; CafeOBJ interpreter.
3 ;;;
4 ;;; Copyright (c) 2000-2016, Toshimi Sawada. All rights reserved.
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ;;;
30
31 ;;; Keywords: cafeobj chaos languages processes tools
32 ;;;
33 ;;; BRIEF DESCRIPTION:
34 ;;; CafeOBJ-mode supports formatting CafeOBJ source codes and sending of
35 ;;; declarations/regions/files to a CafeOBJ interpreter. An interpreter
36 ;;; (see variable `cafeobj-default-application') will be started if you try
37 ;;; to send some code and none is running. You can use the process-buffer
38 ;;; (named after the application you chose) as if it were an
39 ;;; interactive shell. See the documentation for `comint.el' for
40 ;;; details.
41 ;;; *NOTE* : This mode supports Chaos with CafeOBJ syntax only.
42
43 ;;; ====== DESCRIPTION =========================================================
44 ;;;
45 ;;; USAGE:
46 ;;; The following statements, placed in your .emacs file or
47 ;;; site-init.el, will cause this file to be autoloaded, and
48 ;;; cafeobj-mode invoked, when visiting .mod files (assuming this file is
49 ;;; in your load-path):
50 ;;;
51 ;;; (autoload 'cafeobj-mode "cafeobj-mode" "CafeOBJ mode." t)
52 ;;; (setq auto-mode-alist
53 ;;; (cons '("\\.mod$" . cafeobj-mode) auto-mode-alist))
54 ;;;
55 ;;; and, also the following is handy for running CafeOBJ interpreter:
56 ;;;
57 ;;; (autoload 'cafeobj "cafeobj-mode" "Run CafeOBJ interpreter." t)
58 ;;;
59 ;;; If you want font-lock support for CafeOBJ source code (a.k.a. syntax
60 ;;; coloring, highlighting), add this to your .emacs file:
61 ;;; (cond ((string-match "XEmacs\\Lucid" emacs-version)
62 ;;; ;; XEmacs
63 ;;; (add-hook 'cafeobj-mode-hook 'turn-on-font-lock))
64 ;;; (t ;; FSF19.XX, assumes the version better than 19.28..
65 ;;; (add-hook 'cafeobj-mode-hook (function
66 ;;; (lambda () (font-lock-mode 1))))))
67 ;;;
68 ;;; Notes on FONT-LOCK:
69 ;;; You better be sure you're version of Emacs supports
70 ;;; font-lock-mode! As of this writing, the latest Emacs and XEmacs
71 ;;; 19's do. This file is known to be run on Emacs19.28(Mule 2.3), and
72 ;;; XEmacs(20.02).
73 ;;;
74 ;;; KEY BINDINGS:
75 ;;; To see all the keybindings for folding mode, look at `cafeobj-setup-keymap'
76 ;;; or start `cafeobj-mode' and type `\C-h m'.
77 ;;; The keybindings are defined with prefixed by 'cafeobj-prefix-key',
78 ;;; the default for `cafeobj-prefix-key' is `\C-c', which is the conventional
79 ;;; prefix for major-mode commands.
80 ;;; You can customise the keybindings either by setting `choas-prefix-key'
81 ;;; or by putting the following in your .emacs
82 ;;; (setq cafeobj-mode-map (make-sparse-keymap))
83 ;;; and
84 ;;; (define-key cafeobj-mode-map <your-key> <function>)
85 ;;; for all the functions you need.
86 ;;;
87 ;;; ABBREV-MODE:
88 ;;; CafeOBJ mode provides a mode-specific abbrev table 'cafeobj-mode-abbrev-table',
89 ;;; and there defined some abbriviations already.
90 ;;; You can use abbrev by `M-x abbrev-mode' or putting the following in your
91 ;;; .emacs
92 ;;; (add-hook 'cafeobj-mode-hook (function (lambda () (abbrev-mode 1))))
93 ;;;
94 ;;; The predefined abbribiations are minimum, these are derived from my own
95 ;;; experience. I want these definitions to be useful for most of the users,
96 ;;; but ofcourse, you may feel it does not match to your taste. In that case,
97 ;;; add your own definitions for preventing my deinitions.
98 ;;; To edit the entire table, `M-x edit-abbrevs' and `M-x write-abbrev-file'
99 ;;; are handy. Here is an example of adding some extra abbreviations:
100 ;;; I want the following abbreviations:
101 ;;; "mod" -> "module"
102 ;;; "mod*" -> "module*"
103 ;;; "mod!" -> "module!"
104 ;;; "obj" -> "module!"
105 ;;; "th" -> "module*"
106 ;;; For this, we can use `M-x edit-abbrevs' and adds the above definitions in
107 ;;; cafeobj-abbrev-table like this:
108 ;;;
109 ;;; (cafeobj-mode-abbrev-table)
110 ;;;
111 ;;; "mod" 0 "module"
112 ;;; "m*" 0 "module* "
113 ;;; "m!" 0 "module! "
114 ;;; "obj" 0 "module!"
115 ;;; "th" 0 "module*"
116 ;;; "comm" 0 "commutative"
117 ;;; :
118 ;;; :
119 ;;; After editting, type `C-c C-c'(M-x edit-abbrevs-redefine) to install
120 ;;; abbrev definitions as specified.
121 ;;; Then use `M-x write-abbrev-file' specifying ~/.abbrev_defs to
122 ;;; the target file name, and adds a line in my .emacs
123 ;;; (read-abbrev-file "~/.abbrev_defs").
124 ;;; See Emacs manual of Infos for detail.
125 ;;;
126 ;;; CUTOMIZABLE VARIABLES:
127 ;;; You may want to customize the following variables, see the comment strings
128 ;;; for the descriptions of these variables.
129 ;;;
130 ;;; cafeobj-always-show
131 ;;; cafeobj-mode-map
132 ;;; cafeobj-prefix-key
133 ;;; cafeobj-mode-hook
134 ;;; cafeobj-default-application
135 ;;; cafeobj-default-command-switches
136 ;;; cafeobj-decl-keyword-face
137 ;;; cafeobj-keyword-face
138 ;;; cafeobj-command-face
139 ;;; cafeobj-comment-face
140 ;;; cafeobj-indent-level
141 ;;; cafeobj-brace-imaginary-offset
142 ;;; cafeobj-brace-offset
143 ;;; cafeobj-psort-indent
144 ;;; cafeobj-continued-statement-offset
145 ;;; cafeobj-continued-brace-offset
146 ;;;
147 ;;; see document strings for some brief description of each variable.
148 ;;;
149
150 (require 'comint)
151 (require 'custom)
152 (require 'font-lock)
153 (require 'abbrev)
154 (require 'imenu)
155
156 (defgroup cafeobj nil
157 "CafeOBJ code editting."
158 :group 'unix
159 :group 'languages)
160
161 ;;; -----------------------
162 ;;; Customizable variables _____________________________________________________
163 ;;; -----------------------
164
165 (defcustom cafeobj-always-show t
166 "*Non-nil means display cafeobj-process-buffer after sending a command."
167 :type 'boolean
168 :group 'cafeobj)
169
170 (defcustom cafeobj-prefix-key "\C-c"
171 "Prefix for all cafeobj-mode commands."
172 :type 'string
173 :group 'cafeobj)
174
175 (defcustom cafeobj-mode-hook nil
176 "*Hook called when CafeOBJ mode fires up."
177 :type 'hook
178 :group 'cafeobj)
179
180 (defcustom cafeobj-default-application "cafeobj"
181 "Default Chaos dialect to run in chaos subprocess."
182 :type 'string
183 :group 'cafeobj)
184
185 (defcustom cafeobj-application-name "cafeobj"
186 "Default name of Chaos dialect ")
187
188 (defcustom cafeobj-default-command-switches nil
189 "Command switches for `cafeobj-default-application'.
190 Should be a list of strings which will be given to the cafeobj process when star up."
191 :group 'cafeobj)
192
193 ;;; simple imenu support
194 (defcustom cafeobj-imenu-generic-expression-alist
195 '(("Modules" "^mod.* \\(\\w+\\b\\)" 1)
196 ("Vars" "var.* \\(\\w+\\b\\):" 1)
197 )
198 "Alist of regular expressions for the imode index.
199 Each element form (submenu-name regexp index).
200 Where submenu-name is the name of the submenu under which
201 items matching regexp are placed. When submenu-name is nil
202 the matching entries appear in the root imenu list.
203 Regexp idex indicates which regexp text group defines the
204 text entry. When the index is 0 the entire :text that matches
205 regexp appers."
206 :type '(repeat (list (choice :tag "Submenu Name" string (const nil))
207 regexp (integer :tag "Regexp index")))
208 :group 'cafeobj
209 )
210
211 (add-hook 'cafeobj-mode-hook
212 (lambda ()
213 (setq imenu-generic-expression cafeobj-imenu-generic-expression-alist)))
214
215 ;;; FACES for CafeOBJ/CafeOBJ-Process
216
217 (defgroup cafeobj-faces nil
218 "CafeOBJ faces."
219 :group 'cafeobj)
220
221 (defface cafeobj-comment-face-1
222 '((((class color) (background dark)) (:foreground "DarkKhaki"))
223 (((class color) (background light)) (:foreground "chocolate"))
224 (((class grayscale) (background light))
225 (:foreground "DimGray" :bold t :italic t))
226 (((class grayscale) (background dark))
227 (:foreground "LightGray" :bold t :italic t))
228 (t (:bold t)))
229 "Face used to highlight comments."
230 :group 'cafeobj-faces)
231
232 (defface cafeobj-comment-face-2
233 '((((class color) (background dark)) (:foreground "honeydew3"))
234 (((class color) (background light)) (:foreground "blue4"))
235 (((class grayscale) (background light))
236 (:foreground "DimGray" :bold t :italic t))
237 (((class grayscale) (background dark))
238 (:foreground "LightGray" :bold t :italic t))
239 (t (:bold t)))
240 "Face used to highlight comments."
241 :group 'cafeobj-faces)
242
243 (defface cafeobj-string-face
244 '((((class color) (background dark)) (:foreground "goldenrod"))
245 (((class color) (background light)) (:foreground "green4"))
246 (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
247 (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
248 (t (:bold t)))
249 "CafeOBJ face used to highlight strings."
250 :group 'cafeobj-faces)
251
252 (defface cafeobj-string-face-2
253 '((((class color) (background dark)) (:foreground "dark khaki"))
254 (((class color) (background light)) (:foreground "green4"))
255 (t (:bold t)))
256 "CafeOBJ face used to highlight strings."
257 :group 'cafeobj-faces)
258
259 (defface cafeobj-keyword-face
260 '((((class color) (background dark)) (:foreground "DarkSeaGreen3"))
261 (((class color) (background light)) (:foreground "red4"))
262 (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
263 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
264 (t (:bold t)))
265 "CafeOBJ face used to highlight keywords."
266 :group 'cafeobj-faces)
267
268 (defface cafeobj-decl-keyword-face
269 '((((class color) (background dark))
270 (:foreground "LightGoldenrod3" :background "DimGray"))
271 (((class color) (background light))
272 (:foreground "LightGoldenrod3" :background "DimGray"))
273 (((class grayscale) (background light)) (:foreground "LightGray":bold t))
274 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
275 (t (:bold t)))
276 "CafeOBJ face used to highlight keywords."
277 :group 'cafeobj-faces)
278
279 (defface cafeobj-command-name-face
280 '((((class color) (background dark)) (:foreground "LightSkyBlue1"))
281 (((class color) (background light)) (:foreground "brown4"))
282 (t (:bold t :underline t)))
283 "CafeOBJ face used to highlight command names."
284 :group 'cafeobj-faces)
285
286 (defface cafeobj-message-1-face
287 '((((class color) (background dark))
288 (:foreground "LightGoldenrod1" :background "DimGray"))
289 (((class color) (background light))
290 (:foreground "LightGoldenrod1" :background "DimGray"))
291 (((class grayscale) (background light))
292 (:foreground "Gray90" :bold t :italic t))
293 (((class grayscale) (background dark))
294 (:foreground "DimGray" :bold t :italic t))
295 (t (:underline t)))
296 "CafeOBJ face used to highlight message type 1."
297 :group 'cafeobj-faces)
298
299 (defface cafeobj-message-2-face
300 '((((class color) (background dark)) (:foreground "LightGoldenrod3"))
301 (((class color) (background light)) (:foreground "steelblue"))
302 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
303 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
304 (t (:bold t)))
305 "CafeOBJ face used to highlight message type 2."
306 :group 'cafeobj-faces)
307
308 (defface cafeobj-message-3-face
309 '((((class color) (background dark))
310 (:foreground "IndianRed1"))
311 (((class color) (background light)) (:foreground "IndianRed3"))
312 (((class grayscale) (background light)) (:foreground "Gray90" :bold t))
313 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
314 (t (:bold t)))
315 "CafeOBJ face used to hightliht message type 3."
316 :group 'cafeobj-face)
317
318
319 ;;; INDENTATION CONTROL
320
321 (defcustom cafeobj-indent-level 2
322 "*Indentation of CafeOBJ statements with respect to containing block."
323 :type 'integer
324 :group 'cafeobj)
325
326 (defcustom cafeobj-brace-imaginary-offset 0
327 "*Imagined indentation of a CafeOBJ open brace that actually follows a statement."
328 :type 'integer
329 :group 'cafeobj)
330
331 (defcustom cafeobj-brace-offset 0
332 "*Extra indentation for braces, compared with other text in same context."
333 :type 'integer
334 :group 'cafeobj)
335
336 (defcustom cafeobj-psort-indent 5
337 "*Indentation level of principal-sort declaration of CafeOBJ."
338 :type 'integer
339 :group 'cafeobj)
340
341 (defcustom cafeobj-continued-statement-offset 2
342 "*Extra indent for lines not starting new statements."
343 :type 'integer
344 :group 'cafeobj)
345
346 (defcustom cafeobj-continued-brace-offset 0
347 "*Extra indent for substatements that start with open-braces.
348 This is in addition to cafeobj-continued-statement-offset."
349 :type 'integer
350 :group 'cafeobj)
351
352 ;;; ---------------------------------
353 ;;; globals for internal use only....
354 ;;; ---------------------------------
355
356 (defvar cafeobj-mode-map nil
357 "Keymap used with CafeOBJ editting mode.")
358
359 (defvar cafeobj-process nil
360 "The active CafeOBJ subprocess corresponding to current buffer.")
361
362 (defvar cafeobj-process-buffer nil
363 "Buffer used for communication with CafeOBJ subprocess for current buffer.")
364
365 (defvar cafeobj-region-start (make-marker)
366 "Start of special region for CafeOBJ communication.")
367
368 (defvar cafeobj-region-end (make-marker)
369 "End of special region for CafeOBJ communication.")
370
371 ;;; -----------------
372 ;;; Portability stuff___________________________________________________________
373 ;;; -----------------
374
375 (defconst cafeobj-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
376
377 (defmacro cafeobj-define-key (fsf-key definition &optional xemacs-key)
378 `(define-key cafeobj-mode-map
379 ,(if xemacs-key
380 `(if cafeobj-xemacs-p ,xemacs-key ,fsf-key)
381 fsf-key)
382 ,definition))
383
384 (defvar del-back-ch (car (append (where-is-internal 'delete-backward-char)
385 (where-is-internal 'backward-delete-char-untabify)))
386 "Character generated by key bound to delete-backward-char.")
387
388 (and (vectorp del-back-ch) (= (length del-back-ch) 1)
389 (setq del-back-ch (aref del-back-ch 0)))
390
391 ;;;---------------------------
392 ;;; CafeOBJ Keywords/Commands __________________________________________________
393 ;;; essential syntactic structures are defined here.
394 ;;;---------------------------
395
396 (defconst cafeobj-comment-pat-1 "[ \t]*\\*\\*>?[ \t]+.*$")
397 (defconst cafeobj-comment-pat-2 "[ \t]*-->?[ \t]+.*$")
398 (defconst cafeobj-comment-pat-3 "[ \t]*\\*\\*?[ \t]+.*$")
399 (defconst cafeobj-comment-pat-4 "[ \t]*--?[ \t]+.*$")
400
401 (defun looking-at-cafeobj-comment-pat ()
402 (or (looking-at cafeobj-comment-pat-1)
403 (looking-at cafeobj-comment-pat-2)
404 (looking-at cafeobj-comment-pat-3)
405 (looking-at cafeobj-comment-pat-4)))
406
407 (defconst cafeobj-keywords-1
408 '("op"
409 "ops"
410 "sort"
411 "hsort"
412 "bop"
413 "pred"
414 "bops"
415 "#define"
416 "bpred"
417 "var"
418 "vars"
419 "eq"
420 "cq"
421 "ceq"
422 "bq"
423 "beq"
424 "bcq"
425 "bceq"
426 "trans"
427 "trns"
428 "rl"
429 "crl"
430 "ctrans"
431 "ctrns"
432 "btrans"
433 "btrns"
434 "bctrans"
435 "bctrns"
436 "class"
437 "record"
438 "attr"
439 "view"
440 "sort"
441 "hsort"
442 "psort"
443 "principal-sort"
444 "ax"
445 "fax"
446 "protecting"
447 "pr"
448 "extending"
449 "ex"
450 "using"
451 "us"
452 "including"
453 "inc"
454 "goal"
455 )
456 "keywords appearing inside module declaration or view declaration."
457 )
458
459 (defconst cafeobj-keyword-pat-1
460 (concat "^[ \t]*\\(" (regexp-opt cafeobj-keywords-1) "\\)\\>"))
461
462 (defconst cafeobj-keywords-2
463 '("imports"
464 "signature"
465 "axioms"
466 )
467 "keywords 2."
468 )
469
470 (defconst cafeobj-keyword-pat-2
471 (if cafeobj-xemacs-p
472 ;; (concat "^[ \t]*" (regexp-opt cafeobj-keywords-2 t t) "\\>")
473 (concat "^[ \t]*" (regexp-opt cafeobj-keywords-2 ) "\\>")
474 (concat "^[ \t]*\\("
475 (regexp-opt cafeobj-keywords-2)
476 "\\)\\>")))
477
478
479 (defconst cafeobj-keyword-pat-3
480 "\\(^[ \t]*\\*?\\[ \\| \\]\\*?\\)")
481
482 (defconst cafeobj-keyword-pat-4
483 (concat "[ \t]*" (regexp-opt '("sort"
484 "hsort"
485 "bop"
486 "op"
487 "if"
488 "then"
489 "else"
490 "fi"
491 "."))
492 "\\>"))
493
494 (defun looking-at-cafeobj-keyword-pat ()
495 (or (looking-at cafeobj-keyword-pat-1)
496 (looking-at cafeobj-keyword-pat-2)
497 (looking-at cafeobj-keyword-pat-3)
498 (looking-at cafeobj-keyword-pat-4)))
499
500 (defconst cafeobj-commands-1
501 '("let"
502 "red"
503 "reduce"
504 "bred"
505 "breduce"
506 "cbred"
507 "input"
508 "in"
509 "execute"
510 "exec"
511 "make"
512 "apply"
513 "start"
514 "match"
515 "unify"
516 "parse"
517 "select"
518 "set"
519 "show"
520 "describe"
521 "sh"
522 "tram"
523 "check"
524 "choose"
525 "open"
526 "close"
527 "desc"
528 "eof"
529 "protect"
530 "unprotect"
531 "require"
532 "provide"
533 "resolve"
534 "full"
535 "reset"
536 "option"
537 "db"
538 "sos"
539 "passive"
540 "save-option"
541 "sigmatch"
542 "list"
543 "flag"
544 "param"
545 "lex"
546 "name"
547 "names"
548 "look"
549 "inspect"
550 "inspect-term"
551 ":goal"
552 ":apply"
553 ":auto"
554 ":ind"
555 ":init"
556 ":imp"
557 ":cp"
558 ":ctf"
559 ":ctf-"
560 ":csp"
561 ":csp-"
562 ":show"
563 ":sh"
564 ":describe"
565 ":desc"
566 ":verbose"
567 ":backward"
568 ":equation"
569 ":rule"
570 ":select"
571 ":order"
572 ":use"
573 ":embed"
574 ":red"
575 ":define"
576 ":set"
577 )
578 "CafeOBJ top-level commands")
579
580
581 (defconst cafeobj-command-pat-1
582 (concat "^[ \t]*\\(" (regexp-opt cafeobj-commands-1) "\\)\\>"))
583
584 (defun looking-at-cafeobj-command-pat ()
585 (looking-at cafeobj-command-pat-1))
586
587 (defconst cafeobj-top-keywords
588 (append cafeobj-keywords-1 cafeobj-commands-1)
589 "CafeOBJ keyowrds may apper at top-level.")
590
591 (defun looking-at-cafeobj-top-keywords ()
592 (looking-at cafeobj-top-keywords))
593
594 (defconst cafeobj-top-key-pat
595 (concat "^\\<\\("
596 (regexp-opt cafeobj-top-keywords)
597 "\\)\\>"))
598
599 (defconst cafeobj-top-decl-pat
600 (concat "^[ \t]*\\("
601 "module\\|module\\*\\|mod\\*\\|module\\|mod\\|module!\\|mod!\\|view\\|hwd:mod!\\|sys:mod!"
602 "\\)\\>"))
603
604 (defun looking-at-cafeobj-top-decl ()
605 (looking-at cafeobj-top-decl-pat))
606
607 (defconst cafeobj-block-start-pat
608 (concat "[ \t]*"
609 (regexp-opt '("signature" "axioms" "imports" "record" "class")
610 t)
611 "\\>"))
612
613 (defun looking-at-cafeobj-block-start-pat ()
614 (looking-at cafeobj-block-start-pat))
615
616 (defun looking-at-cafeobj-module-decl ()
617 (looking-at (concat "^\\<\\("
618 (regexp-opt '("module" "mod" "module*" "mod*"
619 "module!" "mod!"))
620 "\\)\\>")))
621
622 (defun looking-at-cafeobj-view-decl ()
623 (looking-at "^\\<view\\>"))
624
625 ;;;----------
626 ;;; FONT-LOCK___________________________________________________________________
627 ;;;----------
628
629 (defun get-cafeobj-face (name) (identity name))
630
631 (defvar cafeobj-font-lock-keywords nil
632 "Expressions to highlight in CafeOBJ mode.")
633
634 (setq cafeobj-font-lock-keywords
635 (list
636 ;; comments
637 (list cafeobj-comment-pat-1 0 'cafeobj-comment-face-1 nil)
638 (list cafeobj-comment-pat-2 0 'cafeobj-comment-face-2 nil)
639 ;; keywords
640 (cons cafeobj-keyword-pat-1 'cafeobj-keyword-face)
641 (list cafeobj-keyword-pat-2 1 'cafeobj-decl-keyword-face)
642 (cons cafeobj-keyword-pat-3 'cafeobj-keyword-face)
643 ;; commands
644 (cons cafeobj-command-pat-1 'cafeobj-command-name-face)
645 ;; module/view
646 (list cafeobj-top-decl-pat 1 'cafeobj-decl-keyword-face)
647 ;; declaration appering at the top level.
648 (cons cafeobj-top-key-pat 'cafeobj-keyword-face)
649 ))
650
651 (put 'cafeobj-mode 'font-lock-defaults '(cafeobj-font-lock-keywords t))
652
653 ;;; -------------
654 ;;; MENU SUPPORTS_______________________________________________________________
655 ;;; -------------
656
657 ;; For XEmacs
658 (defvar cafeobj-mode-popup-menu nil)
659 (defvar cafeobj-mode-menubar-menu nil)
660
661 ;; For FSF19
662 (defvar cafeobj-mode-menu nil
663 "Keymap for cafeobj-mode's menu.")
664
665 (cond (cafeobj-xemacs-p
666 (setq cafeobj-mode-popup-menu
667 (purecopy '("CafeOBJ Interaction Menu"
668 ["Evaluate This Declaration"
669 cafeobj-send-decl t]
670 ["Evaluate Current Line" cafeobj-send-line t]
671 ["Evaluate Entire Buffer" cafeobj-send-buffer t]
672 ["Evaluate Region" cafeobj-send-region
673 (region-exists-p)]
674 "---"
675 ["Comment Out Region" comment-region (region-exists-p)]
676 ["Indent Region" indent-region (region-exists-p)]
677 ["Indent Line" cafeobj-indent-line t]
678 ["Beginning of Declaration"
679 cafeobj-beginning-of-decl t]
680 )))
681 (setq cafeobj-mode-menubar-menu
682 (purecopy (cons "CafeOBJ" (cdr cafeobj-mode-popup-menu)))))
683 (t ;;
684 (setq cafeobj-mode-menu (make-sparse-keymap "CafeOBJ"))
685 (define-key cafeobj-mode-menu [cafeobj-send-line]
686 '("Evaluate Current Line" . cafeobj-send-line))
687 (define-key cafeobj-mode-menu [cafeobj-send-region]
688 '("Evaluate Cafeobj-Region" . cafeobj-send-region))
689 (define-key cafeobj-mode-menu [cafeobj-send-proc]
690 '("Evaluate This Declaration" . cafeobj-send-decl))
691 (define-key cafeobj-mode-menu [cafeobj-send-buffer]
692 '("Send Buffer" . cafeobj-send-buffer))
693 (define-key cafeobj-mode-menu [cafeobj-beginning-of-decl]
694 '("Beginning Of Proc" . cafeobj-beginning-of-decl))
695 (define-key cafeobj-mode-menu [comment-region]
696 '("Comment Out Region" . comment-region))
697 (define-key cafeobj-mode-menu [indent-region]
698 '("Indent Region" . indent-region))
699 (define-key cafeobj-mode-menu [cafeobj-indent-line]
700 '("Indent Line" . cafeobj-indent-line))
701 (define-key cafeobj-mode-menu [cafeobj-beginning-of-decl]
702 '("Beginning of Declaration" . cafeobj-beginning-of-decl))
703 ))
704
705 ;;; ------------
706 ;;; CafeOBJ Mode________________________________________________________________
707 ;;; ------------
708
709 (defvar cafeobj-mode-abbrev-table nil
710 "Abbrev table in use in CafeOBJ mode.")
711
712 ;;; se use extended abbriv
713 (autoload 'expand-abbrev-hook "expand")
714
715 ;;; some default abbreviations defined here
716 ;;;
717 '(if cafeobj-mode-abbrev-table
718 nil
719 (define-abbrev-table 'cafeobj-mode-abbrev-table
720 '(;; top level declaration
721 ("mod" ["module {\n\n}\n" 5 () nil] expand-abbrev-hook 0)
722 ("pmod" ["module () {\n\n}\n" 8 () nil] expand-abbrev-hook 0)
723 ("vw" ["view {\n\n}\n" 5 () nil] expand-abbrev-hook 0)
724 ("mk" ["make ()" 3 () nil] expand-abbrev-hook 0)
725 ;; module constructs
726 ("ps" "pricipal-sort" nil 0)
727 ;; imports
728 ("imp" ["imports {\n\n}\n" 3 () nil] expand-abbrev-hook 0)
729 ("pr" ["protecting()" 1 () nil] expand-abbrev-hook 0)
730 ("pa" ["protecting as ()" 3 () nil] expand-abbrev-hook 0)
731 ("ex" ["extending()" 1 () nil] expand-abbrev-hook 0)
732 ("ea" ["extending as ()" 2 () nil] expand-abbrev-hook 0)
733 ("us" ["using()" 1 () nil] expand-abbrev-hook 0)
734 ("ua" ["using as ()" 2 () nil] expand-abbrev-hook 0)
735 ("inc" ["including()" 1 () nil] expand-abbrev-hook 0)
736 ("ias" ["including as ()" 2 () nil] expand-abbrev-hook 0)
737 ;; signature
738 ("sig" ["signature {\n\n}\n" 3 () nil] expand-abbrev-hook 0)
739 ;; sort declarations
740 ("[" ["[]" 1 () nil] expand-abbrev-hook 0)
741 ("*[" ["*[]*" 2 () nil] expand-abbrev-hook 0)
742 ;; operator declaration
743 ("ope" ["op : -> ." 10 () nil] expand-abbrev-hook 0)
744 ("opt" ["op : -> {} ." 12 () nil] expand-abbrev-hook 0)
745 ("ops" ["ops : -> ." 9 () nil] expand-abbrev-hook 0)
746 ("pred" ["pred : ." 4 () nil] expand-abbrev-hook 0)
747 ;; operator attributes
748 ("str" ["strat: ()" 1 () nil] expand-abbrev-hook 0)
749 ("ctr" "ctor" nil 0)
750 ("ra" "r-assoc" nil 0)
751 ("la" "l-assoc" nil 0)
752 ("pre" "prec:" nil 0)
753 ("ass" "assoc" nil 0)
754 ("com" "comm" nil 0)
755 ;; axiom
756 ("axi" ["axioms {\n\n}\n" 3 () nil] expand-abbrev-hook 0)
757 ("bt" ["btrans => ." 7 () nil] expand-abbrev-hook 0)
758 ("bct"["bctrans => if ." 10 () nil] expand-abbrev-hook 0)
759 ("ctr" ["ctrans => if ." 10 () nil] expand-abbrev-hook 0)
760 ("tr" ["trans => ." 7 () nil] expand-abbrev-hook 0)
761 ("eq" ["eq = ." 7 () nil] expand-abbrev-hook 0)
762 ("cq" ["ceq = if ." 10 () nil] expand-abbrev-hook 0)
763 ("bq" ["beq = ." 7 () nil] expand-abbrev-hook 0)
764 ("bcq" ["bceq = if ." 10 () nil] expand-abbrev-hook 0)
765 ("var" ["var : " 3 () nil] expand-abbrev-hook 0)
766 ("vars" ["vars : " 3 () nil] expand-abbrev-hook 0)
767 ;; commands
768 ("ch" "check" nil 0)
769 ("comp" "compatibility " nil 0)
770 ("reg" "regularity " nil 0)
771 ("red" ["reduce ." 2 () nil] expand-abbrev-hook 0)
772 ("rin" ["reduce in : ." 5 () nil] expand-abbrev-hook 0)
773 ("exe" ["execute ." 2 () nil] expand-abbrev-hook 0)
774 ("ein" ["execute in : ." 5 () nil] expand-abbrev-hook 0)
775 ("par" ["parse ." 2 () nil] expand-abbrev-hook 0)
776 ("parse" ["parse ." 2 () nil] expand-abbrev-hook 0)
777 ("pin" ["parse in : ." 5 () nil] expand-abbrev-hook 0)
778 ("sh" "show" nil 0)
779 ("des" "describe " nil 0)
780 ("regu" "regualize " nil 0)
781 ("verb" "verbose" nil 0)
782 ("sw" "switch" nil 0)
783 ("sws" "switches" nil 0)
784 ("req" "require" nil 0)
785 ("prov" "provide" nil 0)
786 ("sel" ["select ." 2 () nil] expand-abbrev-hook 0)
787 ;; CITP
788 (":goal" [":goal {}" 1 () nil] expand-abbrev-hook 0)
789 (":go" [":goal {}" 1 () nil] expand-abbrev-hook 0)
790 (":apply" [":apply ()" 1 () nil] expand-abbrev-hook 0)
791 (":app" [":apply ()" 1 () nil] expand-abbrev-hook 0)
792 (":ind" [":ind on ()" 1 () nil] expand-abbrev-hook 0)
793 (":ini" [":init () by { ;}" 9 () nil] expand-abbrev-hook 0)
794 (":inil" [":init [] by { ;}" 9 () nil] expand-abbrev-hook 0)
795 (":cp" [":cp () >< ()" 7 () nil] expand-abbrev-hook 0)
796 (":cpl" [":cp [] >< []" 7 () nil] expand-abbrev-hook 0)
797 (":eq" ":equation" nil 0)
798 (":rl" ":rule" nil 0)
799 (":bk" ":backward" nil 0)
800 (":vb" ":verbose" nil 0)
801 (":norm" ":normalize" nil 0)
802 ;; (":ctf" [":ctf{}" 1 () nil] expand-abbrev-hook 0)
803 (":csp" [":csp{}" 1 () nil] expand-abbrev-hook 0)
804 (":sh" ":show" nil 0)
805 (":des" ":describe" nil 0)
806 ))
807 )
808
809
810 (defvar cafeobj-mode-syntax-table nil
811 "Syntax table in use in cafeobj-mode buffers.")
812
813 (if cafeobj-mode-syntax-table
814 ()
815 (setq cafeobj-mode-syntax-table (make-syntax-table))
816 (mapc (function
817 (lambda (x) (modify-syntax-entry
818 (car x) (cdr x) cafeobj-mode-syntax-table)))
819 '((?\( . "()" ) ( ?\) . ")(" )
820 ;; ( ?\[ . "(]" ) ( ?\] . ")[" )
821 (?\{ . "(}" ) ( ?\} . "){" )
822 (?\[ . "w") (?\] . "w")
823 (?\* . "w")
824 ;; underscore etc. is word class
825 ( ?\_ . "w" )
826 ( ?\# . "w" )
827 ( ?\! . "w" )
828 ( ?\$ . "w" )
829 ( ?\% . "w" )
830 ( ?\: . "w")
831 ( ?\" . "\"" ) ; double quote is string quote too
832 ( ?\n . ">"))
833 ))
834 (defvar mode-popup-menu) ;only for XEmacs
835
836 (defun cafeobj-mode ()
837 "Major mode for editing CafeOBJ programs.
838 The following keys are bound:
839 \\{cafeobj-mode-map}
840 "
841 (interactive)
842 (let (s)
843 (kill-all-local-variables)
844 (setq major-mode 'cafeobj-mode)
845 (setq mode-name "CafeOBJ")
846 ;; keymap
847 (or cafeobj-mode-map (cafeobj-setup-keymap))
848 (use-local-map cafeobj-mode-map)
849 ;; abbrev
850 (setq local-abbrev-table cafeobj-mode-abbrev-table)
851 ;; settting menu.
852 (if cafeobj-xemacs-p
853 (setq mode-popup-menu
854 cafeobj-mode-popup-menu)
855 (progn
856 (define-key cafeobj-mode-map [menu-bar cafeobj-mode]
857 (cons "CafeOBJ" cafeobj-mode-menu))))
858 ;;
859 (set (make-local-variable 'cafeobj-process) nil)
860 (set (make-local-variable 'cafeobj-process-buffer) nil)
861 (make-local-variable 'cafeobj-default-command-switches)
862 ;;
863 (cond (cafeobj-xemacs-p
864 (make-local-variable 'font-lock-defaults))
865 (t (set (make-local-variable 'font-lock-keywords)
866 cafeobj-font-lock-keywords)))
867 ;;
868 (make-local-variable 'font-lock-defaults)
869 (setq font-lock-defaults '(cafeobj-font-lock-keywords t))
870 ;;
871 (set (make-local-variable 'indent-line-function) 'cafeobj-indent-line)
872 (set (make-local-variable 'comment-start) "-- ") ; obsolate? ... should be fixed.
873 ;; todo --
874 (set (make-local-variable 'comment-start-skip) "\\(\\(^\\|;\\)[ \t]*\\)--")
875 ;; syntax-------
876 (set-syntax-table cafeobj-mode-syntax-table)
877 ;; (modify-syntax-entry ?# "<")
878 (modify-syntax-entry ?\n ">")
879 ;;
880 (make-local-variable 'require-final-newline)
881 (setq require-final-newline t)
882 ;;
883 (setq comment-column 32)
884 (make-local-variable 'comment-start-skip)
885 (make-local-variable 'parse-sexp-ignore-comments)
886 (setq parse-sexp-ignore-comments t)
887 ;; look for a #!.../cafeobj -f line at bob
888 (save-excursion
889 (goto-char (point-min))
890 (if (looking-at "#![ \t]*\\([^ \t]*\\)[ \t]\\(.*[ \t]\\)-f")
891 (progn
892 (set (make-local-variable 'cafeobj-application-name)
893 (buffer-substring (match-beginning 1)
894 (match-end 1)))
895 (if (match-beginning 2)
896 (progn
897 (goto-char (match-beginning 2))
898 (set (make-local-variable 'cafeobj-default-command-switches) nil)
899 (while (< (point) (match-end 2))
900 (setq s (read (current-buffer)))
901 (if (<= (point) (match-end 2))
902 (setq cafeobj-default-command-switches
903 (append cafeobj-default-command-switches
904 (list (prin1-to-string s)))))))))
905 ;; if this fails, look for the #!/bin/csh ... exec hack
906 (while (eq (following-char) ?#)
907 (forward-line 1))
908 (or (bobp)
909 (forward-char -1))
910 (if (eq (preceding-char) ?\\)
911 (progn
912 (forward-char 1)
913 (if (looking-at "exec[ \t]+\\([^ \t]*\\)[ \t]\\(.*[ \t]\\)*-f")
914 (progn
915 (set (make-local-variable 'cafeobj-application-name)
916 (buffer-substring (match-beginning 1)
917 (match-end 1)))
918 (if (match-beginning 2)
919 (progn
920 (goto-char (match-beginning 2))
921 (set (make-local-variable
922 'cafeobj-default-command-switches)
923 nil)
924 (while (< (point) (match-end 2))
925 (setq s (read (current-buffer)))
926 (if (<= (point) (match-end 2))
927 (setq cafeobj-default-command-switches
928 (append cafeobj-default-command-switches
929 (list (prin1-to-string s)))))))))
930 )))))
931 ;;
932 ;; (if (and (featurep 'menubar)
933 ;; current-menubar)
934 ;; (progn
935 ;; ;; make a local copy of the menubar, so our modes don't
936 ;; ;; change the global menubar
937 ;; (set-buffer-menubar current-menubar)
938 ;; (add-submenu nil cafeobj-mode-menubar-menu)))
939 ;;
940 (run-hooks 'cafeobj-mode-hook)))
941
942 ;;; --------------------
943 ;;; Default Key Bindings________________________________________________________
944 ;;; --------------------
945
946 (defun cafeobj-setup-keymap ()
947 "Set up keymap for cafeobj mode.
948 If the variable `cafeobj-prefix-key' is nil, the bindings go directly
949 to `cafeobj-mode-map', otherwise they are prefixed with `cafeobj-prefix-key'."
950 (setq cafeobj-mode-map (make-sparse-keymap))
951 ;;
952 (let ((map (if cafeobj-prefix-key
953 (make-sparse-keymap)
954 cafeobj-mode-map)))
955 ;; indentation
956 (define-key cafeobj-mode-map [?}] 'cafeobj-electric-brace)
957 ;; communication
958 (define-key map "l" 'cafeobj-send-line)
959 (define-key map "e" 'cafeobj-send-decl)
960 (define-key map "r" 'cafeobj-send-region)
961 (define-key map "b" 'cafeobj-send-buffer)
962 (define-key map "[" 'cafeobj-beginning-of-decl)
963 (define-key map "]" 'cafeobj-end-of-decl)
964 (define-key map "q" 'cafeobj-kill-process)
965 (define-key map "s" 'cafeobj-show-process-buffer)
966 (define-key map "h" 'cafeobj-hide-process-buffer)
967 ;;
968 (if cafeobj-prefix-key
969 (define-key cafeobj-mode-map cafeobj-prefix-key map))
970 ))
971
972 ;;; ----------------
973 ;;; CafeOBJ Editting____________________________________________________________
974 ;;; ----------------
975
976 (defconst cafeobj-auto-newline nil
977 "*Non-nil means automatically newline before and after braces,
978 and after colons and semicolons, inserted in CafeOBJ code.")
979
980 (defconst cafeobj-tab-always-indent t
981 "*Non-nil means TAB in C mode should always reindent the current line,
982 regardless of where in the line point is when the TAB command is used.")
983
984 (defun cafeobj-electric-brace (arg)
985 "Insert character and correct line's indentation."
986 (interactive "P")
987 (let (insertpos)
988 (if (and (not arg)
989 (eolp)
990 (or (save-excursion
991 (skip-chars-backward " \t")
992 (bolp))
993 (if cafeobj-auto-newline
994 (progn (cafeobj-indent-line) (newline) t)
995 nil)))
996 (progn
997 ;; (insert last-command-char)
998 (insert last-command-event)
999 (cafeobj-indent-line)
1000 (if cafeobj-auto-newline
1001 (progn
1002 (newline)
1003 ;; (newline) may have done auto-fill
1004 (setq insertpos (- (point) 2))
1005 (cafeobj-indent-line)))
1006 (save-excursion
1007 (if insertpos (goto-char (1+ insertpos)))
1008 (delete-char -1))))
1009 (if insertpos
1010 (save-excursion
1011 (goto-char insertpos)
1012 (self-insert-command (prefix-numeric-value arg)))
1013 (self-insert-command (prefix-numeric-value arg)))))
1014
1015 (defun cafeobj-beginning-of-block (&optional arg)
1016 "Move backward to the beginning of a CafeOBJ block structure.
1017 With argument, do it that many times. Negative arg -N
1018 means move forward to Nth following beginning of declaration.
1019 Returns t unless search stops due to beginning or end of buffer."
1020 (interactive "P")
1021 (or arg (setq arg 1))
1022 (let ((found nil)
1023 (ret t))
1024 (if (and (< arg 0)
1025 (looking-at-cafeobj-block-start-pat))
1026 (forward-char 1))
1027 (while (< arg 0)
1028 (if (re-search-forward cafeobj-block-start-pat nil t)
1029 (setq arg (1+ arg)
1030 found t)
1031 (setq ret nil
1032 arg 0)))
1033 (if found
1034 (beginning-of-line))
1035 (while (> arg 0)
1036 (if (re-search-backward cafeobj-block-start-pat nil t)
1037 (setq arg (1- arg))
1038 (setq ret nil
1039 arg 0)))
1040 ret))
1041
1042 (defun cafeobj-beginning-of-decl (&optional arg)
1043 "Move backward to the beginning of a CafeOBJ top-level decl.
1044 With argument, do it that many times. Negative arg -N
1045 means move forward to Nth following beginning of declaration.
1046 Returns t unless search stops due to beginning or end of buffer."
1047 (interactive "P")
1048 (or arg (setq arg 1))
1049 (let ((found nil)
1050 (ret t))
1051 (if (and (< arg 0)
1052 (looking-at-cafeobj-top-decl))
1053 (forward-char 1))
1054 (while (< arg 0)
1055 (if (re-search-forward cafeobj-top-decl-pat nil t)
1056 (setq arg (1+ arg)
1057 found t)
1058 (setq ret nil
1059 arg 0)))
1060 (if found
1061 (beginning-of-line))
1062 (while (> arg 0)
1063 (if (re-search-backward cafeobj-top-decl-pat nil t)
1064 (setq arg (1- arg))
1065 (setq ret nil
1066 arg 0)))
1067 ret))
1068
1069 (defun cafeobj-end-of-decl (&optional arg)
1070 "Move forward to next end of chaos declaration (or similar).
1071 With argument, do it that many times. Negative argument -N means move
1072 back to Nth preceding end of proc.
1073
1074 This function just searches for a `}' at the beginning of a line."
1075 (interactive "P")
1076 (or arg
1077 (setq arg 1))
1078 (let ((found nil)
1079 (ret t))
1080 (if (and (< arg 0)
1081 (not (bolp))
1082 (save-excursion
1083 (beginning-of-line)
1084 (eq (following-char) ?})))
1085 (forward-char -1))
1086 (while (> arg 0)
1087 (if (re-search-forward "^}" nil t)
1088 (setq arg (1- arg)
1089 found t)
1090 (setq ret nil
1091 arg 0)))
1092 (while (< arg 0)
1093 (if (re-search-backward "^}" nil t)
1094 (setq arg (1+ arg)
1095 found t)
1096 (setq ret nil
1097 arg 0)))
1098 (if found
1099 (end-of-line))
1100 ret))
1101
1102 (defun cafeobj-outline-level ()
1103 (save-excursion
1104 (skip-chars-forward "\t ")
1105 (current-column)))
1106
1107 (defun cafeobj-inside-parens-p ()
1108 (condition-case ()
1109 (save-excursion
1110 (save-restriction
1111 (narrow-to-region (point)
1112 (progn (cafeobj-beginning-of-decl) (point)))
1113 (goto-char (point-max))
1114 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))
1115 (error nil)))
1116
1117 (defun cafeobj-indent-command (&optional whole-exp)
1118 "Indent current line as CafeOBJ code, or in some cases insert a tab character.
1119 If `cafeobj-tab-always-indent' is non-nil (the default), always indent current line.
1120 Otherwise, indent the current line only if point is at the left margin or
1121 in the line's indentation; otherwise insert a tab.
1122
1123 A numeric argument, regardless of its value, means indent rigidly all the
1124 lines of the expression starting after point so that this line becomes
1125 properly indented. The relative indentation among the lines of the
1126 expression are preserved."
1127 (interactive "P")
1128 (if whole-exp
1129 ;; If arg, always indent this line as CafeOBJ
1130 ;; and shift remaining lines of expression the same amount.
1131 (let ((shift-amt (cafeobj-indent-line))
1132 beg end)
1133 (save-excursion
1134 (if cafeobj-tab-always-indent
1135 (beginning-of-line))
1136 ;; Find beginning of following line.
1137 (save-excursion
1138 (forward-line 1) (setq beg (point)))
1139 ;; Find first beginning-of-sexp for sexp extending past this line.
1140 (while (< (point) beg)
1141 (forward-sexp 1)
1142 (setq end (point))
1143 (skip-chars-forward " \t\n")))
1144 (if (> end beg)
1145 (indent-code-rigidly beg end shift-amt "#")))
1146 (if (and (not cafeobj-tab-always-indent)
1147 (save-excursion
1148 (skip-chars-backward " \t")
1149 (not (bolp))))
1150 (insert-tab)
1151 (cafeobj-indent-line))))
1152
1153 (defun cafeobj-indent-line ()
1154 "Indent current line as CafeOBJ code.
1155 Return the amount the indentation changed by."
1156 (let ((indent (calculate-cafeobj-indent nil))
1157 beg shift-amt
1158 (case-fold-search nil)
1159 (pos (- (point-max) (point))))
1160 (beginning-of-line)
1161 (setq beg (point))
1162 (cond ((eq indent nil)
1163 (setq indent (current-indentation)))
1164 ((eq indent t)
1165 (setq indent (current-indentation)))
1166 (t
1167 (skip-chars-forward " \t")
1168 (if (listp indent) (setq indent (car indent)))
1169 (cond ((= (following-char) ?})
1170 (setq indent (- indent cafeobj-indent-level)))
1171 ((= (following-char) ?{)
1172 (setq indent (+ indent cafeobj-brace-offset))))))
1173 (skip-chars-forward " \t")
1174 (setq shift-amt (- indent (current-column)))
1175 (if (zerop shift-amt)
1176 (if (> (- (point-max) pos) (point))
1177 (goto-char (- (point-max) pos)))
1178 (delete-region beg (point))
1179 (indent-to indent)
1180 ;; If initial point was within line's indentation,
1181 ;; position after the indentation. Else stay at same point in text.
1182 (if (> (- (point-max) pos) (point))
1183 (goto-char (- (point-max) pos))))
1184 shift-amt))
1185
1186 (defun calculate-cafeobj-indent (&optional parse-start)
1187 "Return appropriate indentation for current line as CafeOBJ code.
1188 In usual case returns an integer: the column to indent to.
1189 Returns nil if line starts inside a string, t if in a comment."
1190 (save-excursion
1191 (beginning-of-line)
1192 (let ((indent-point (point))
1193 (case-fold-search nil)
1194 state
1195 containing-sexp)
1196 (if parse-start
1197 (goto-char parse-start)
1198 (cafeobj-beginning-of-decl))
1199 (while (< (point) indent-point)
1200 (setq parse-start (point))
1201 (setq state (parse-partial-sexp (point) indent-point 0))
1202 (setq containing-sexp (car (cdr state))))
1203 ;;
1204 (cond ((or (nth 3 state) (nth 4 state))
1205 ;; return nil or t if should not change this line
1206 (nth 4 state)) ; t if inside a comment, else nil.
1207 ;;
1208 ((null containing-sexp) ; we are at top-level
1209 ;; -- TOP-LEVEL------------------------------------------------
1210 ;; Line is at top level. May be module/view declaration or
1211 ;; top-level commands.
1212 (goto-char indent-point) ; start from original pos.
1213 (skip-chars-forward " \t")
1214 (cond ((= (following-char) ?{) 0)
1215 ((looking-at-cafeobj-top-decl) 0)
1216 ((looking-at-cafeobj-comment-pat) (current-column))
1217 (t
1218 (cafeobj-backward-to-noncomment (or parse-start (point-min)))
1219 ;; Look at previous line that's at column 0
1220 ;; to determine whether we are in top-level decl.
1221 (let ((basic-indent
1222 (save-excursion
1223 (re-search-backward "^[^ \^L\t\n]" nil 'move)
1224 (if (and (looking-at-cafeobj-top-decl)
1225 (not (progn
1226 (condition-case nil
1227 (progn
1228 (search-forward "\{" parse-start)
1229 (forward-list))
1230 (error nil))
1231 (looking-at "\}"))))
1232 cafeobj-psort-indent
1233 0))))
1234 basic-indent))))
1235 ;; NON TOPLEVEL ----------------------------------------------
1236 ((and (/= (char-after containing-sexp) ?{)
1237 (< (car state) 2))
1238 ;; indent to just after the surrounding open.
1239 (goto-char (+ 2 containing-sexp))
1240 (current-column))
1241 ;; WITHIN A BRACE --------------------------------------------
1242 (t
1243 (goto-char indent-point)
1244 (if (and (not (is-cafeobj-beginning-of-statement))
1245 (progn (cafeobj-backward-to-noncomment containing-sexp)
1246 (not (memq (preceding-char)
1247 '(0 ?\, ?\} ?\{ ?\. ?\] ?\)))))
1248 ;; don't treat a line with a close-brace
1249 ;; as a continuation. It is probably the
1250 ;; end of a block.
1251 (save-excursion
1252 (goto-char indent-point)
1253 (skip-chars-forward " \t")
1254 (not (= (following-char) ?})))
1255 )
1256 ;; This line is continuation of preceding line's statement;
1257 ;; indent cafeobj-continued-statement-offset more than the
1258 ;; previous line of the statement.
1259 (progn
1260 (cafeobj-backward-to-start-of-continued-exp containing-sexp)
1261 (+ cafeobj-continued-statement-offset (current-column)
1262 (if (save-excursion (goto-char indent-point)
1263 (skip-chars-forward " \t")
1264 (eq (following-char) ?{))
1265 cafeobj-continued-brace-offset
1266 0)))
1267 ;; This line starts a new statement.
1268 ;; if we are looking at a comment line, leave it as is.
1269 (if (progn
1270 (goto-char indent-point)
1271 (skip-chars-forward " \t")
1272 (looking-at-cafeobj-comment-pat))
1273 (current-column)
1274 (progn
1275 ;; Position following last unclosed open-brace.
1276 (goto-char containing-sexp)
1277 ;; Is line first statement after an open-brace?
1278 (or
1279 ;; If no, find that first statement and indent like it.
1280 (save-excursion
1281 (forward-char 1)
1282 (while (progn (skip-chars-forward " \t\n")
1283 (looking-at "--[ \t].*\\|\*\*[ \t].*"))
1284 (forward-line 1))
1285 ;; The first following code counts
1286 ;; if it is before the line we want to indent.
1287 (skip-chars-forward " \t\n")
1288 (and (< (point) indent-point)
1289 (- (current-column)
1290 ;; If prev stmt starts with open-brace, that
1291 ;; open brace was offset by cafeobj-brace-offset.
1292 ;; Compensate to get the column where
1293 ;; an ordinary statement would start.
1294 (if (= (following-char) ?\{)
1295 cafeobj-brace-offset
1296 0))
1297 ))
1298 ;; If no previous statement,
1299 ;; indent it relative to line brace is on.
1300 ;; For open brace in column zero, don't let statement
1301 ;; start there too. If cafeobj-indent-level is zero,
1302 ;; use cafeobj-brace-offset +
1303 ;; cafeobj-continued-statement-offset instead.
1304 ;; For open-braces not the first thing in a line,
1305 ;; add in cafeobj-brace-imaginary-offset.
1306 (+ (if (and (bolp) (zerop cafeobj-indent-level))
1307 (+ cafeobj-brace-offset
1308 cafeobj-continued-statement-offset)
1309 cafeobj-indent-level)
1310 ;; Move back over whitespace before the openbrace.
1311 ;; If openbrace is not first nonwhite thing on the line,
1312 ;; add the cafeobj-brace-imaginary-offset.
1313 (progn (skip-chars-backward " \t")
1314 (if (bolp)
1315 0
1316 cafeobj-brace-imaginary-offset))
1317 ;; If the openbrace is preceded by a parenthesized exp,
1318 ;; move to the beginning of that;
1319 ;; possibly a different line
1320 (progn
1321 (if (memq (preceding-char) '(?\) \]))
1322 (forward-sexp -1))
1323 ;; Get initial indentation of the line we are on.
1324 (current-indentation))))))))))))
1325
1326 (defun is-cafeobj-beginning-of-statement ()
1327 (save-excursion
1328 (beginning-of-line)
1329 ;; (skip-chars-forward " \t")
1330 (or (looking-at-cafeobj-keyword-pat)
1331 (looking-at-cafeobj-command-pat))))
1332
1333 (defun cafeobj-backward-to-noncomment (lim)
1334 (let (stop)
1335 (while (not stop)
1336 (skip-chars-backward " \t\n\f" lim)
1337 (setq stop (or (<= (point) lim)
1338 (save-excursion
1339 (beginning-of-line)
1340 (skip-chars-forward " \t")
1341 (not (looking-at-cafeobj-comment-pat)))))
1342 (or stop (beginning-of-line)))))
1343
1344 (defun cafeobj-backward-to-start-of-continued-exp (lim)
1345 (if (memq (preceding-char) '(?\) ?\" ?\]))
1346 (forward-sexp -1))
1347 (beginning-of-line)
1348 (if (<= (point) lim)
1349 (goto-char (1+ lim)))
1350 (skip-chars-forward " \t"))
1351
1352
1353
1354 ;;; -----------------------
1355 ;;; RUN CafeOBJ Interpreter_____________________________________________________
1356 ;;; and Interact with it
1357 ;;; -----------------------
1358 (defvar cafeobj-prompt-pattern "^[A-Za-z0-9!#$%']+> ")
1359
1360 (defgroup cafeobj-process nil
1361 "Running CafeOBJ from within Emacs buffers"
1362 :group 'processes
1363 :group 'unix)
1364
1365 (defgroup cafeobj-directories nil
1366 "Directory support in CafeOBJ process mode"
1367 :group 'cafeobj-process)
1368
1369 (defgroup cafeobj-process-faces nil
1370 "Faces in CafeOBJ buffers"
1371 :group 'cafeobj-process)
1372
1373 (defcustom cafeobj-completion-fignore nil
1374 "*List of suffixes to be disregarded during file/command completion.
1375 This variable is used to initialize `comint-completion-fignore' in the
1376 CafeOBJ process buffer. The default is nil.
1377
1378 This is a fine thing to set in your `.emacs' file."
1379 :type '(repeat (string :tag "Suffix"))
1380 :group 'cafeobj-process)
1381
1382 (defvar cafeobj-delimiter-argument-list nil ;'(?\| ?& ?< ?> ?\( ?\) ?\;
1383 "List of characters to recognise as separate arguments.
1384 This variable is used to initialize `comint-delimiter-argument-list' in the
1385 CafeOBJ buffer.
1386
1387 This is a fine thing to set in your `.emacs' file.")
1388
1389 (defvar cafeobj-file-name-chars
1390 (if (memq system-type '(ms-dos windows-nt))
1391 "~/A-Za-z0-9_^$!#%&{}@`'.()-"
1392 "~/A-Za-z0-9+@:_.$#%,={}-")
1393 "String of characters valid in a file name.
1394 This variable is used to initialize `comint-file-name-chars' in the
1395 CafeOBJ buffer. The value may depend on the operating system.
1396
1397 This is a fine thing to set in your `.emacs' file.")
1398
1399 (defvar cafeobj-file-name-quote-list
1400 (if (memq system-type '(ms-dos windows-nt))
1401 nil
1402 (append cafeobj-delimiter-argument-list '(?\ ?\* ?\! ?\" ?\' ?\`)))
1403 "List of characters to quote when in a file name.
1404 This variable is used to initialize `comint-file-name-quote-list' in the
1405 shell buffer. The value may depend on the operating system or shell.
1406
1407 This is a fine thing to set in your `.emacs' file.")
1408
1409 (defvar cafeobj-dynamic-complete-functions
1410 '(comint-replace-by-expanded-history
1411 cafeobj-dynamic-complete-environment-variable
1412 cafeobj-dynamic-complete-command
1413 cafeobj-replace-by-expanded-directory
1414 cafeobj-dynamic-complete-filename
1415 comint-dynamic-complete-filename)
1416 "List of functions called to perform completion.
1417 This variable is used to initialise `comint-dynamic-complete-functions' in the
1418 CafeOBJ process buffer.
1419
1420 This is a fine thing to set in your `.emacs' file.")
1421
1422 (defcustom cafeobj-command-regexp "[^;&|\n]+"
1423 "*Regexp to match a single command within a pipeline.
1424 This is used for directory tracking and does not do a perfect job."
1425 :type 'regexp
1426 :group 'cafeobj-process)
1427
1428 (defcustom cafeobj-pushd-regexp "pushd"
1429 "*Regexp to match subshell commands equivalent to pushd."
1430 :type 'regexp
1431 :group 'cafeobj-directories)
1432
1433 (defcustom cafeobj-popd-regexp "popd"
1434 "*Regexp to match CafeOBJ commands equivalent to popd."
1435 :type 'regexp
1436 :group 'cafeobj-directories)
1437
1438 (defcustom cafeobj-pushd-tohome nil
1439 "*If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd)."
1440 :type 'boolean
1441 :group 'cafeobj-directories)
1442
1443 (defcustom cafeobj-pushd-dextract nil
1444 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top."
1445 :type 'boolean
1446 :group 'cafeobj-directories)
1447
1448 (defcustom cafeobj-pushd-dunique nil
1449 "*If non-nil, make pushd only add unique directories to the stack."
1450 :type 'boolean
1451 :group 'cafeobj-directories)
1452
1453 (defcustom cafeobj-chdrive-regexp
1454 (if (memq system-type '(ms-dos windows-nt))
1455 ; NetWare allows the five chars between upper and lower alphabetics.
1456 "[]a-zA-Z^_`\\[\\\\]:"
1457 nil)
1458 "*If non-nil, is regexp used to track drive changes."
1459 :type '(choice regexp
1460 (const nil))
1461 :group 'cafeobj-directories)
1462
1463 (defcustom cafeobj-cd-regexp "cd"
1464 "*Regexp to match CafeOBJ commands equivalent to cd."
1465 :type 'regexp
1466 :group 'cafeobj-directories)
1467
1468 (defcustom cafeobj-input-autoexpand 'history
1469 "*If non-nil, expand input command history references on completion.
1470 This mirrors the optional behavior of tcsh (its autoexpand and histlit).
1471
1472 If the value is `input', then the expansion is seen on input.
1473 If the value is `history', then the expansion is only when inserting
1474 into the buffer's input ring. See also `comint-magic-space' and
1475 `comint-dynamic-complete'.
1476
1477 This variable supplies a default for `comint-input-autoexpand',
1478 for CafeOBJ process mode only."
1479 :type '(choice (const nil) (const input) (const history))
1480 :type 'cafeobj)
1481
1482 (defvar cafeobj-dirstack nil
1483 "List of directories saved by pushd in this buffer's CafeOBJ.
1484 Thus, this does not include the CafeOBJ's current directory.")
1485
1486 (defvar cafeobj-dirstack-query nil)
1487
1488 (defvar cafeobj-dirtrackp t
1489 "Non-nil in a CafeOBJ buffer means directory tracking is enabled.")
1490
1491 (defvar cafeobj-last-dir nil
1492 "Keep track of last directory.")
1493
1494 (defvar cafeobj-process-mode-map nil)
1495
1496 (cond ((not cafeobj-process-mode-map)
1497 (setq cafeobj-process-mode-map
1498 (nconc (make-sparse-keymap) comint-mode-map))
1499 (define-key cafeobj-process-mode-map
1500 "\C-c\C-f" 'cafeobj-forward-command)
1501 (define-key cafeobj-process-mode-map
1502 "\C-c\C-b" 'cafeobj-backward-command)
1503 (define-key cafeobj-process-mode-map
1504 "\t" 'comint-dynamic-complete)
1505 (define-key cafeobj-process-mode-map
1506 "\M-?" 'comint-dynamic-list-filename-completions)
1507 (define-key cafeobj-process-mode-map
1508 "\C-c\C-g" 'cafeobj-put-esc-esc)
1509 (define-key cafeobj-process-mode-map [menu-bar completion]
1510 (cons "Complete"
1511 (copy-keymap (lookup-key comint-mode-map [menu-bar completion]))))
1512 (define-key-after (lookup-key cafeobj-process-mode-map
1513 [menu-bar completion])
1514 [complete-env-variable] '("Complete Env. Variable Name" .
1515 cafeobj-dynamic-complete-environment-variable)
1516 'complete-file)
1517 (define-key-after (lookup-key cafeobj-process-mode-map [menu-bar completion])
1518 [expand-directory] '("Expand Directory Reference" .
1519 cafeobj-replace-by-expanded-directory)
1520 'complete-expand)
1521 (unless (featurep 'infodock)
1522 (define-key cafeobj-process-mode-map "\M-\C-m" 'cafeobj-resync-dirs))
1523 ))
1524
1525 (defcustom cafeobj-process-mode-hook nil
1526 "*Hook for customising CafeOBJ process mode."
1527 :type 'hook
1528 :group 'cafeobj-process)
1529
1530 ;;; FONT-LOCKING
1531
1532 (defface cafeobj-error-face
1533 '((((class color) (background dark))
1534 (:foreground "Red3" :background "DimGray"))
1535 (((class color) (background light))
1536 (:foreground "Red3" :background "DimGray"))
1537 (((class grayscale) (background light))
1538 (:foreground "LightGray" :bold t :underline t))
1539 (((class grayscale) (background dark))
1540 (:foreground "Gray50" :bold t :underline t)))
1541 "CafeOBJ face used to highlight error messages."
1542 :group 'cafeobj-faces)
1543
1544 (defface cafeobj-warning-face
1545 '((((class color) (background dark))
1546 (:foreground "yellow3" :background "DimGray"))
1547 (((class color) (background light))
1548 (:foreground "yellow3" :background "DimGray"))
1549 (t (:inverse-video t :bold t)))
1550 "Font Lock mode face used to highlight warnings."
1551 :group 'cafeobj-faces)
1552
1553 (defface cafeobj-pignose-keyword-face
1554 '((((class color) (background dark)) (:foreground "steelblue1"))
1555 (((class color) (background light)) (:foreground "blue3"))
1556 (t (:underline t)))
1557 "CafeOBJ face used to highlight keywords of PigNose."
1558 :group 'cafeobj-faces)
1559
1560 (defcustom cafeobj-prompt-face 'cafeobj-prompt-face
1561 "Face for shell prompts."
1562 :type 'face
1563 :group 'cafeobj-process-faces)
1564
1565 (defcustom cafeobj-process-keyword-face 'cafeobj-process-keyword-face
1566 "Face for CafeOBJ keywords."
1567 :type 'face
1568 :group 'cafeobj-process-faces)
1569
1570 (defcustom cafeobj-option-face 'cafeobj-option-face
1571 "Face for command line options."
1572 :type 'face
1573 :group 'cafeobj-process-faces)
1574
1575 (defcustom cafeobj-output-face 'cafeobj-output-face
1576 "Face for generic CafeOBJ output."
1577 :type 'face
1578 :group 'cafeobj-process-faces)
1579
1580
1581 (make-face cafeobj-prompt-face)
1582 (make-face cafeobj-process-keyword-face)
1583 (make-face cafeobj-option-face)
1584
1585 (defun cafeobj-process-font-lock-mode-hook ()
1586 (or (face-differs-from-default-p cafeobj-prompt-face)
1587 (copy-face 'cafeobj-comment-face-2 cafeobj-prompt-face))
1588 (or (face-differs-from-default-p cafeobj-process-keyword-face)
1589 (copy-face 'cafeobj-comment-face-2 cafeobj-process-keyword-face))
1590 (or (face-differs-from-default-p cafeobj-option-face)
1591 (copy-face 'cafeobj-comment-face-2 cafeobj-option-face))
1592 ;; (or (face-differs-from-default-p cafeobj-error-face)
1593 ;; (copy-face 'red cafeobj-error-face))
1594
1595 ;; we only need to do this once
1596 (remove-hook 'font-lock-mode-hook 'cafeobj-process-font-lock-mode-hook))
1597
1598 (add-hook 'font-lock-mode-hook 'cafeobj-process-font-lock-mode-hook)
1599
1600 (defvar cafeobj-process-font-lock-keywords nil)
1601
1602 (progn
1603 (setq cafeobj-process-font-lock-keywords
1604 (append
1605 (list '(eval . (cons cafeobj-prompt-pattern
1606 cafeobj-prompt-face))
1607 '("\\[Error\\]:" . cafeobj-error-face)
1608 '("\\[Warning\\]:" . cafeobj-warning-face)
1609 '("^\\[Properties .*$" . cafeobj-process-keyword-face)
1610 '("^\\[selected .*$" . cafeobj-comment-face-2)
1611 '("^\\* kept .*$" . cafeobj-comment-face-1)
1612 '("^\\*\\* success$" . cafeobj-message-3-face)
1613 '("^\\*\\* fail$" . cafeobj-message-3-face)
1614 '("^\\*\\* found .*$" . cafeobj-message-3-face)
1615 '("^\\*\\* ok, .*$" . cafeobj-message-3-face)
1616 '("\\<\\(trying .*\\)\\>:" 0 cafeobj-message-2-face)
1617 '("^\\*\\* check .*:" . cafeobj-message-2-face)
1618 '("^\\*\\* Fail.*$" . cafeobj-message-3-face)
1619 '("^\\*\\* fail!$" . cafeobj-message-3-face)
1620 '("^\\*\\* Predicate .*$". cafeobj-message-3-face)
1621 '("^\\*\\* PigNose .*$" . cafeobj-message-2-face)
1622 '("^\\*\\* Search .*$" . cafeobj-process-keyword-face)
1623 ;; reduction
1624 '("^\\(-- reduce in .* :\\) " 1 cafeobj-message-1-face)
1625 '("^\\(-- behavioural reduce in .* :\\) " 1 cafeobj-message-1-face)
1626 '("^\\(-- cbred in .* :\\)" 1 cafeobj-message-1-face)
1627 '("^\\(#[0-9]+\\)(" 1 cafeobj-comment-face-1)
1628 ;; CITP
1629 '("^\\[.+\\].*$" . cafeobj-message-1-face)
1630 ;; '("\\<done.$" . cafeobj-message-2-face)
1631 '("^\\(case #.*:\\) " 1 cafeobj-message-1-face)
1632 '("^| .*|$" . cafeobj-message-2-face)
1633 '("** __+$" . cafeobj-message-1-face)
1634 ;; '("^\\*\\*.*$" . cafeobj-comment-face-1)
1635 '("^\\*\\* USABLE " . cafeobj-process-keyword-face)
1636 '("^\\*\\* SOS " . cafeobj-process-keyword-face)
1637 '("^\\*\\* PASSIVE " . cafeobj-process-keyword-face)
1638 '("^\\*\\* Starting PigNose " . cafeobj-process-keyword-face)
1639 '("^\\*\\* DEMODULATORS " . cafeobj-process-keyword-face)
1640 '("^\\*\\* PROOF " . cafeobj-process-keyword-face)
1641 '("^___+$" . cafeobj-message-2-face)
1642 '("^---+$" . cafeobj-message-2-face)
1643 '("^adding axiom:" . cafeobj-comment-face-2)
1644 '("^goal:" . cafeobj-message-1-face)
1645 '("^hypo:" . cafeobj-comment-face-2)
1646 '("^ax:" . cafeobj-message-1-face)
1647 '("^==.*$" . cafeobj-message-2-face)
1648 '("[ \t]\\([+-][^ \t\n>]+\\)" 1 cafeobj-option-face)
1649 ;; '("^[^\n]+.*$" . cafeobj-output-face)
1650 )
1651 cafeobj-font-lock-keywords)))
1652
1653 (put 'cafeobj-process-mode 'font-lock-defaults
1654 '(cafeobj-process-font-lock-keywords t))
1655
1656 ;;; CafeOBJ Process Mode
1657
1658 (defun cafeobj-process-mode ()
1659 (interactive)
1660 (comint-mode)
1661 (setq major-mode 'CafeOBJ-process-mode)
1662 (setq mode-name "CafeOBJ process")
1663 (use-local-map cafeobj-process-mode-map)
1664 (make-local-variable 'comint-prompt-regexp)
1665 (setq comint-prompt-regexp cafeobj-prompt-pattern)
1666 (setq comint-completion-fignore cafeobj-completion-fignore)
1667 (make-local-variable 'comint-delimiter-argument-list)
1668 (setq comint-delimiter-argument-list cafeobj-delimiter-argument-list)
1669 (setq comint-file-name-chars cafeobj-file-name-chars)
1670 (setq comint-file-name-quote-list cafeobj-file-name-quote-list)
1671 (set (make-local-variable 'comint-dynamic-complete-functions)
1672 cafeobj-dynamic-complete-functions)
1673 (make-local-variable 'paragraph-start)
1674 (setq paragraph-start comint-prompt-regexp)
1675 (make-local-variable 'font-lock-defaults)
1676 (setq font-lock-defaults '(cafeobj-process-font-lock-keywords t))
1677 (make-local-variable 'cafeobj-dirstack)
1678 (setq cafeobj-dirstack nil)
1679 (make-local-variable 'cafeobj-last-dir)
1680 (setq cafeobj-last-dir nil)
1681 (make-local-variable 'cafeobj-dirtrackp)
1682 (setq cafeobj-dirtrackp t)
1683 (add-hook 'comint-input-filter-functions 'cafeobj-directory-tracker nil t)
1684 (setq comint-input-autoexpand cafeobj-input-autoexpand)
1685 ;; This is not really correct, since the CafeOBJ buffer does not really
1686 ;; edit this directory. But it is useful in the buffer list and menus.
1687 (make-local-variable 'list-buffers-directory)
1688 (setq list-buffers-directory (expand-file-name default-directory))
1689 (setq cafeobj-dirstack-query "pwd")
1690 (run-hooks 'cafeobj-process-mode-hook)
1691 (comint-read-input-ring t)
1692 (cafeobj-dirstack-message)
1693 )
1694
1695 (defun cafeobj-start-process (name program &optional startfile &rest switches)
1696 "Start a chaos process named NAME, running PROGRAM."
1697 (or switches
1698 (setq switches cafeobj-default-command-switches))
1699 (when cafeobj-xemacs-p
1700 (setq switches (append switches '("-no-banner"))))
1701 (setq cafeobj-process-buffer (apply 'make-comint name program startfile switches))
1702 (setq cafeobj-process (get-buffer-process cafeobj-process-buffer))
1703 (save-excursion
1704 (set-buffer cafeobj-process-buffer)
1705 (cafeobj-process-mode)))
1706
1707 (defcustom cafeobj-logo-file "/usr/local/share/doc/cafeobj/cafeobj-logo-small.png"
1708 "CafeOBJ's logo file displayed at start up time of the interpreter."
1709 :type 'string
1710 :group 'cafeobj)
1711
1712 (defun cafeobj-startup-message (&optional x y)
1713 "Insert startup message in current buffer."
1714 (erase-buffer)
1715 (when (and (display-graphic-p)
1716 (file-exists-p cafeobj-logo-file))
1717 (let* ((img (create-image cafeobj-logo-file))
1718 (img-size (image-size img))
1719 (char-per-pixel (/ (* 1.0 (window-width)) (window-pixel-width))))
1720 (goto-char (point-min))
1721 (insert " \n")
1722 (insert-image img)
1723 (while (not (eobp))
1724 (insert (make-string (truncate (* (/ (max (- (window-pixel-width)
1725 (or x (car img-size)))
1726 0)
1727 2)
1728 char-per-pixel))
1729 ?\ ))
1730 (forward-line 1))))
1731 (goto-char (point-max))
1732 (insert (make-string 2 ?\n))
1733 (set-buffer-modified-p t))
1734
1735 (defun cafeobj (&rest ignore)
1736 (interactive)
1737 (if (and cafeobj-process
1738 (eq (process-status cafeobj-process) 'run))
1739 (switch-to-buffer cafeobj-process-buffer)
1740 (progn
1741 (switch-to-buffer (get-buffer-create (concat "*" cafeobj-application-name "*")))
1742 ;; show cafeobj logo
1743 (cafeobj-startup-message)
1744 ;; start the process
1745 (cafeobj-start-process cafeobj-application-name cafeobj-default-application))))
1746
1747 (defun cafeobj-kill-process ()
1748 "Kill chaos subprocess and its buffer."
1749 (interactive)
1750 (if cafeobj-process-buffer
1751 (kill-buffer cafeobj-process-buffer)))
1752
1753 (defun cafeobj-set-cafeobj-region-start (&optional arg)
1754 "Set start of region for use with `cafeobj-send-cafeobj-region'."
1755 (interactive)
1756 (set-marker cafeobj-region-start (or arg (point))))
1757
1758 (defun cafeobj-set-cafeobj-region-end (&optional arg)
1759 "Set end of region for use with `cafeobj-send-cafeobj-region'."
1760 (interactive)
1761 (set-marker cafeobj-region-end (or arg (point))))
1762
1763 (defun cafeobj-send-line ()
1764 "Send current line to chaos subprocess, found in `cafeobj-process'.
1765 If `cafeobj-process' is nil or dead, start a new process first."
1766 (interactive)
1767 (let ((start (save-excursion (beginning-of-line) (point)))
1768 (end (save-excursion (end-of-line) (point))))
1769 (or (and cafeobj-process
1770 (eq (process-status cafeobj-process) 'run))
1771 (cafeobj-start-process cafeobj-application-name cafeobj-default-application))
1772 (comint-simple-send cafeobj-process (buffer-substring start end))
1773 (forward-line 1)
1774 (if cafeobj-always-show
1775 (display-buffer cafeobj-process-buffer))))
1776
1777 (defun cafeobj-send-region (start end)
1778 "Send region to chaos subprocess."
1779 (interactive "r")
1780 (when (and start end)
1781 (or (and cafeobj-process
1782 (comint-check-proc cafeobj-process-buffer))
1783 (cafeobj-start-process cafeobj-application-name cafeobj-default-application))
1784 (comint-simple-send cafeobj-process
1785 (concat (buffer-substring start end) "\n"))
1786 (if cafeobj-always-show
1787 (display-buffer cafeobj-process-buffer))))
1788
1789 (defun cafeobj-send-decl ()
1790 "Send proc around point to chaos subprocess."
1791 (interactive)
1792 (let (beg end)
1793 (save-excursion
1794 (cafeobj-beginning-of-decl)
1795 (setq beg (point))
1796 (cafeobj-end-of-decl)
1797 (setq end (point)))
1798 (or (and cafeobj-process
1799 (comint-check-proc cafeobj-process-buffer))
1800 (cafeobj-start-process cafeobj-application-name cafeobj-default-application))
1801 (comint-simple-send cafeobj-process
1802 (concat (buffer-substring beg end) "\n"))
1803 (if cafeobj-always-show
1804 (display-buffer cafeobj-process-buffer))))
1805
1806 (defun cafeobj-send-buffer ()
1807 "Send whole buffer to chaos subprocess."
1808 (interactive)
1809 (or (and cafeobj-process
1810 (comint-check-proc cafeobj-process-buffer))
1811 (cafeobj-start-process cafeobj-application-name cafeobj-default-application))
1812 (if (buffer-modified-p)
1813 (comint-simple-send cafeobj-process
1814 (concat
1815 (buffer-substring (point-min) (point-max))
1816 "\n"))
1817 (comint-simple-send cafeobj-process
1818 (concat "input "
1819 (buffer-file-name)
1820 "\n")))
1821 (if cafeobj-always-show
1822 (display-buffer cafeobj-process-buffer)))
1823
1824 (defun cafeobj-show-process-buffer ()
1825 "Make sure `cafeobj-process-buffer' is being displayed."
1826 (interactive)
1827 (display-buffer cafeobj-process-buffer))
1828
1829 (defun cafeobj-hide-process-buffer ()
1830 "Delete all windows that display `cafeobj-process-buffer'."
1831 (interactive)
1832 (delete-windows-on cafeobj-process-buffer))
1833
1834 ;;; Directory tracking
1835 ;;;
1836 ;;; This code provides the shell mode input sentinel
1837 ;;; CAFEOBJ-DIRECTORY-TRACKER
1838 ;;; that tracks cd, pushd, and popd commands issued to the shell, and
1839 ;;; changes the current directory of the shell buffer accordingly.
1840 ;;;
1841 ;;; This is basically a fragile hack, although it's more accurate than
1842 ;;; the version in Emacs 18's shell.el. It has the following failings:
1843 ;;; 1. It doesn't know about the cdpath shell variable.
1844 ;;; 2. It cannot infallibly deal with command sequences, though it does well
1845 ;;; with these and with ignoring commands forked in another shell with ()s.
1846 ;;; 3. More generally, any complex command is going to throw it. Otherwise,
1847 ;;; you'd have to build an entire shell interpreter in emacs lisp. Failing
1848 ;;; that, there's no way to catch shell commands where cd's are buried
1849 ;;; inside conditional expressions, aliases, and so forth.
1850 ;;;
1851 ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
1852 ;;; messes it up. You run other processes under the shell; these each have
1853 ;;; separate working directories, and some have commands for manipulating
1854 ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
1855 ;;; commands that do *not* affect the current w.d. at all, but look like they
1856 ;;; do (e.g., the cd command in ftp). In shells that allow you job
1857 ;;; control, you can switch between jobs, all having different w.d.'s. So
1858 ;;; simply saying %3 can shift your w.d..
1859 ;;;
1860 ;;; The solution is to relax, not stress out about it, and settle for
1861 ;;; a hack that works pretty well in typical circumstances. Remember
1862 ;;; that a half-assed solution is more in keeping with the spirit of Unix,
1863 ;;; anyway. Blech.
1864 ;;;
1865 ;;; One good hack not implemented here for users of programmable shells
1866 ;;; is to program up the shell w.d. manipulation commands to output
1867 ;;; a coded command sequence to the tty. Something like
1868 ;;; ESC | <cwd> |
1869 ;;; where <cwd> is the new current working directory. Then trash the
1870 ;;; directory tracking machinery currently used in this package, and
1871 ;;; replace it with a process filter that watches for and strips out
1872 ;;; these messages.
1873
1874 (defun cafeobj-directory-tracker (str)
1875 "Tracks cd, pushd and popd commands issued to the shell.
1876 This function is called on each input passed to the shell.
1877 It watches for cd, pushd and popd commands and sets the buffer's
1878 default directory to track these commands.
1879
1880 You may toggle this tracking on and off with \\[dirtrack-toggle].
1881 If emacs gets confused, you can resync with the shell with \\[dirs].
1882
1883 See variables `cafeobj-cd-regexp', `cafeobj-chdrive-regexp', `cafeobj-pushd-regexp',
1884 and `cafeobj-popd-regexp', while `cafeobj-pushd-tohome', `cafeobj-pushd-dextract',
1885 and `cafeobj-pushd-dunique' control the behavior of the relevant command.
1886
1887 Environment variables are expanded, see function `substitute-in-file-name'."
1888 (if cafeobj-dirtrackp
1889 ;; We fail gracefully if we think the command will fail in the shell.
1890 (condition-case chdir-failure
1891 (let ((start (progn (string-match "^[; \t]*" str) ; skip whitespace
1892 (match-end 0)))
1893 end cmd arg1)
1894 (while (string-match cafeobj-command-regexp str start)
1895 (setq end (match-end 0)
1896 cmd (comint-arguments (substring str start end) 0 0)
1897 arg1 (comint-arguments (substring str start end) 1 1))
1898 (cond ((string-match (concat "\\`\\(" cafeobj-popd-regexp
1899 "\\)\\($\\|[ \t]\\)")
1900 cmd)
1901 (cafeobj-process-popd (comint-substitute-in-file-name arg1)))
1902 ((string-match (concat "\\`\\(" cafeobj-pushd-regexp
1903 "\\)\\($\\|[ \t]\\)")
1904 cmd)
1905 (cafeobj-process-pushd (comint-substitute-in-file-name arg1)))
1906 ((string-match (concat "\\`\\(" cafeobj-cd-regexp
1907 "\\)\\($\\|[ \t]\\)")
1908 cmd)
1909 (cafeobj-process-cd (comint-substitute-in-file-name arg1)))
1910 ((and cafeobj-chdrive-regexp
1911 (string-match (concat "\\`\\(" cafeobj-chdrive-regexp
1912 "\\)\\($\\|[ \t]\\)")
1913 cmd))
1914 (cafeobj-process-cd (comint-substitute-in-file-name cmd))))
1915 (setq start (progn (string-match "[; \t]*" str end) ; skip again
1916 (match-end 0)))))
1917 (error "Couldn't cd"))))
1918
1919
1920 ;; Like `cd', but prepends comint-file-name-prefix to absolute names.
1921 (defun cafeobj-cd-1 (dir dirstack)
1922 (if cafeobj-dirtrackp
1923 (setq list-buffers-directory (file-name-as-directory
1924 (expand-file-name dir))))
1925 (condition-case nil
1926 (progn (if (file-name-absolute-p dir)
1927 ;;(cd-absolute (concat comint-file-name-prefix dir))
1928 (cd-absolute dir)
1929 (cd dir))
1930 (setq cafeobj-dirstack dirstack)
1931 (cafeobj-dirstack-message))
1932 (file-error (message "Couldn't cd."))))
1933
1934 ;;; popd [+n]
1935 (defun cafeobj-process-popd (arg)
1936 (let ((num (or (cafeobj-extract-num arg) 0)))
1937 (cond ((and num (= num 0) cafeobj-dirstack)
1938 (cafeobj-cd-1 (car cafeobj-dirstack) (cdr cafeobj-dirstack)))
1939 ((and num (> num 0) (<= num (length cafeobj-dirstack)))
1940 (let* ((ds (cons nil cafeobj-dirstack))
1941 (cell (nthcdr (1- num) ds)))
1942 (rplacd cell (cdr (cdr cell)))
1943 (setq cafeobj-dirstack (cdr ds))
1944 (cafeobj-dirstack-message)))
1945 (t
1946 (error "Couldn't popd")))))
1947
1948 ;; Return DIR prefixed with comint-file-name-prefix as appropriate.
1949 (defun cafeobj-prefixed-directory-name (dir)
1950 (if (= (length comint-file-name-prefix) 0)
1951 dir
1952 (if (file-name-absolute-p dir)
1953 ;; The name is absolute, so prepend the prefix.
1954 (concat comint-file-name-prefix dir)
1955 ;; For relative name we assume default-directory already has the prefix.
1956 (expand-file-name dir))))
1957
1958 ;;; cd [dir]
1959 (defun cafeobj-process-cd (arg)
1960 (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix
1961 "~"))
1962 ((string-equal "-" arg) cafeobj-last-dir)
1963 (t (cafeobj-prefixed-directory-name arg)))))
1964 (setq cafeobj-last-dir default-directory)
1965 (cafeobj-cd-1 new-dir cafeobj-dirstack)))
1966
1967 ;;; pushd [+n | dir]
1968 (defun cafeobj-process-pushd (arg)
1969 (let ((num (cafeobj-extract-num arg)))
1970 (cond ((zerop (length arg))
1971 ;; no arg -- swap pwd and car of stack unless cafeobj-pushd-tohome
1972 (cond (cafeobj-pushd-tohome
1973 (cafeobj-process-pushd (concat comint-file-name-prefix "~")))
1974 (cafeobj-dirstack
1975 (let ((old default-directory))
1976 (cafeobj-cd-1 (car cafeobj-dirstack)
1977 (cons old (cdr cafeobj-dirstack)))))
1978 (t
1979 (message "Directory stack empty."))))
1980 ((numberp num)
1981 ;; pushd +n
1982 (cond ((> num (length cafeobj-dirstack))
1983 (message "Directory stack not that deep."))
1984 ((= num 0)
1985 (error (message "Couldn't cd.")))
1986 (cafeobj-pushd-dextract
1987 (let ((dir (nth (1- num) cafeobj-dirstack)))
1988 (cafeobj-process-popd arg)
1989 (cafeobj-process-pushd default-directory)
1990 (cafeobj-cd-1 dir cafeobj-dirstack)))
1991 (t
1992 (let* ((ds (cons default-directory cafeobj-dirstack))
1993 (dslen (length ds))
1994 (front (nthcdr num ds))
1995 (back (reverse (nthcdr (- dslen num) (reverse ds))))
1996 (new-ds (append front back)))
1997 (cafeobj-cd-1 (car new-ds) (cdr new-ds))))))
1998 (t
1999 ;; pushd <dir>
2000 (let ((old-wd default-directory))
2001 (cafeobj-cd-1 (cafeobj-prefixed-directory-name arg)
2002 (if (or (null cafeobj-pushd-dunique)
2003 (not (member old-wd cafeobj-dirstack)))
2004 (cons old-wd cafeobj-dirstack)
2005 cafeobj-dirstack)))))))
2006
2007 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
2008 (defun cafeobj-extract-num (str)
2009 (and (string-match "^\\+[1-9][0-9]*$" str)
2010 (string-to-number str)))
2011
2012 (defun cafeobj-dirtrack-toggle ()
2013 "Turn directory tracking on and off in a shell buffer."
2014 (interactive)
2015 (if (setq cafeobj-dirtrackp (not cafeobj-dirtrackp))
2016 (setq list-buffers-directory default-directory)
2017 (setq list-buffers-directory nil))
2018 (message "Directory tracking %s" (if cafeobj-dirtrackp "ON" "OFF")))
2019
2020 ;;; For your typing convenience:
2021 (defalias 'dirtrack-toggle 'cafeobj-dirtrack-toggle)
2022
2023 (defun cafeobj-cd (dir)
2024 "Do normal `cd' to DIR, and set `list-buffers-directory'."
2025 (if cafeobj-dirtrackp
2026 (setq list-buffers-directory (file-name-as-directory
2027 (expand-file-name dir))))
2028 (cd dir))
2029
2030 (defun cafeobj-resync-dirs ()
2031 "Resync the buffer's idea of the current directory stack.
2032 This command queries the shell with the command bound to
2033 `cafeobj-dirstack-query' (default \"dirs\"), reads the next
2034 line output and parses it to form the new directory stack.
2035 DON'T issue this command unless the buffer is at a shell prompt.
2036 Also, note that if some other subprocess decides to do output
2037 immediately after the query, its output will be taken as the
2038 new directory stack -- you lose. If this happens, just do the
2039 command again."
2040 (interactive)
2041 (let* ((proc (get-buffer-process (current-buffer)))
2042 (pmark (process-mark proc)))
2043 (goto-char pmark)
2044 (insert cafeobj-dirstack-query) (insert "\n")
2045 (sit-for 0) ; force redisplay
2046 (comint-send-string proc cafeobj-dirstack-query)
2047 (comint-send-string proc "\n")
2048 (set-marker pmark (point))
2049 (let ((pt (point))) ; wait for 1 line
2050 ;; This extra newline prevents the user's pending input from spoofing us.
2051 (insert "\n") (backward-char 1)
2052 (while (not (looking-at ".+\n"))
2053 (accept-process-output proc)
2054 (goto-char pt)
2055 ;; kludge to cope with shells that have "stty echo" turned on.
2056 ;; of course this will lose if there is only one dir on the stack
2057 ;; and it is named "dirs"... -jwz
2058 (if (looking-at "^dirs\r?\n") (delete-region (point) (match-end 0)))
2059 ))
2060 (goto-char pmark) (delete-char 1) ; remove the extra newline
2061 ;; That's the dirlist. grab it & parse it.
2062 (let* ((dl (buffer-substring (match-beginning 0) (1- (match-end 0))))
2063 (dl-len (length dl))
2064 (ds '()) ; new dir stack
2065 (i 0))
2066 (while (< i dl-len)
2067 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
2068 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
2069 (setq ds (cons (concat comint-file-name-prefix
2070 (substring dl (match-beginning 1)
2071 (match-end 1)))
2072 ds))
2073 (setq i (match-end 0)))
2074 (let ((ds (reverse ds)))
2075 (cafeobj-cd-1 (car ds) (cdr ds))))))
2076
2077 ;;; For your typing convenience:
2078 (defalias 'dirs 'cafeobj-resync-dirs)
2079
2080 ;; XEmacs addition
2081 (defvar cafeobj-dirstack-message-hook nil
2082 "Hook to run after a cd, pushd or popd event")
2083
2084 ;;; Show the current dirstack on the message line.
2085 ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
2086 ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
2087 ;;; All the commands that mung the buffer's dirstack finish by calling
2088 ;;; this guy.
2089 (defun user-home-directory ()
2090 (getenv "HOME"))
2091
2092 (defun cafeobj-dirstack-message ()
2093 (let* ((msg "")
2094 (ds (cons default-directory cafeobj-dirstack))
2095 (home (if cafeobj-xemacs-p
2096 (format "^%s\\(/\\|$\\)"
2097 (regexp-quote (user-home-directory)))
2098 abbreviated-home-dir))
2099 (prefix (and comint-file-name-prefix
2100 ;; XEmacs addition: don't turn "/foo" into "foo" !!
2101 (not (= 0 (length comint-file-name-prefix)))
2102 (format "^%s\\(/\\|$\\)"
2103 (regexp-quote comint-file-name-prefix)))))
2104 (while ds
2105 (let ((dir (car ds)))
2106 (if (string-match home dir)
2107 (setq dir (concat "~/" (substring dir (match-end 0)))))
2108 ;; Strip off comint-file-name-prefix if present.
2109 (and prefix (string-match prefix dir)
2110 (setq dir (substring dir (match-end 0)))
2111 (setcar ds dir)
2112 )
2113 (setq msg (concat msg dir " "))
2114 (setq ds (cdr ds))))
2115 (run-hooks 'cafeobj-dirstack-message-hook)
2116 (message "%s" msg)))
2117
2118 ;; This was mostly copied from cafeobj-resync-dirs.
2119 (defun cafeobj-snarf-envar (var)
2120 "Return as a string the shell's value of environment variable VAR."
2121 (let* ((cmd (format "printenv '%s'\n" var))
2122 (proc (get-buffer-process (current-buffer)))
2123 (pmark (process-mark proc)))
2124 (goto-char pmark)
2125 (insert cmd)
2126 (sit-for 0) ; force redisplay
2127 (comint-send-string proc cmd)
2128 (set-marker pmark (point))
2129 (let ((pt (point))) ; wait for 1 line
2130 ;; This extra newline prevents the user's pending input from spoofing us.
2131 (insert "\n") (backward-char 1)
2132 (while (not (looking-at ".+\n"))
2133 (accept-process-output proc)
2134 (goto-char pt)))
2135 (goto-char pmark) (delete-char 1) ; remove the extra newline
2136 (buffer-substring (match-beginning 0) (1- (match-end 0)))))
2137
2138 (defun cafeobj-copy-environment-variable (variable)
2139 "Copy the environment variable VARIABLE from the subshell to Emacs.
2140 This command reads the value of the specified environment variable
2141 in the shell, and sets the same environment variable in Emacs
2142 \(what `getenv' in Emacs would return) to that value.
2143 That value will affect any new subprocesses that you subsequently start
2144 from Emacs."
2145 (interactive (list (read-envvar-name "\
2146 Copy Shell environment variable to Emacs: ")))
2147 (setenv variable (cafeobj-snarf-envar variable)))
2148
2149 (defun cafeobj-forward-command (&optional arg)
2150 "Move forward across ARG shell command(s). Does not cross lines.
2151 See `cafeobj-command-regexp'."
2152 (interactive "p")
2153 (let ((limit (save-excursion (end-of-line nil) (point))))
2154 (if (re-search-forward (concat cafeobj-command-regexp "\\([;&|][\t ]*\\)+")
2155 limit 'move arg)
2156 (skip-syntax-backward " "))))
2157
2158
2159 (defun cafeobj-backward-command (&optional arg)
2160 "Move backward across ARG shell command(s). Does not cross lines.
2161 See `cafeobj-command-regexp'."
2162 (interactive "p")
2163 (let ((limit (save-excursion (comint-bol nil) (point))))
2164 (if (> limit (point))
2165 (save-excursion (beginning-of-line) (setq limit (point))))
2166 (skip-syntax-backward " " limit)
2167 (if (re-search-backward
2168 (format "[;&|]+[\t ]*\\(%s\\)" cafeobj-command-regexp) limit 'move arg)
2169 (progn (goto-char (match-beginning 1))
2170 (skip-chars-forward ";&|")))))
2171
2172 (defvar esc-esc "")
2173
2174 (defun cafeobj-put-esc-esc ()
2175 "send EscEsc to CafeOBJ process. This makes the interpeter
2176 to cancel current input."
2177 (interactive)
2178 (let* ((proc (get-buffer-process (current-buffer)))
2179 (pmark (process-mark proc)))
2180 (goto-char pmark)
2181 (comint-send-string proc esc-esc)
2182 (comint-send-string proc "\n")
2183 (set-marker pmark (point))))
2184
2185 (defun cafeobj-dynamic-complete-command ()
2186 "Dynamically complete the command at point.
2187 This function is similar to `comint-dynamic-complete-filename', except that it
2188 searches `exec-path' (minus the trailing emacs library path) for completion
2189 candidates. Note that this may not be the same as the shell's idea of the
2190 path.
2191
2192 Returns t if successful."
2193 (interactive)
2194 (let ((filename (comint-match-partial-filename)))
2195 (if (and filename
2196 (save-match-data (not (string-match "[~/]" filename)))
2197 (eq (match-beginning 0)
2198 (save-excursion (cafeobj-backward-command 1) (point))))
2199 (prog2 (message "Completing command name...")
2200 (cafeobj-dynamic-complete-as-command)))))
2201
2202
2203 (defun cafeobj-dynamic-complete-as-command ()
2204 "Dynamically complete at point as a command.
2205 See `cafeobj-dynamic-complete-filename'. Returns t if successful."
2206 (let* ((filename (or (comint-match-partial-filename) ""))
2207 (pathnondir (file-name-nondirectory filename))
2208 (paths (cdr (reverse exec-path)))
2209 (cwd (file-name-as-directory (expand-file-name default-directory)))
2210 (ignored-extensions
2211 (and comint-completion-fignore
2212 (mapconcat (function (lambda (x) (concat (regexp-quote x) "$")))
2213 comint-completion-fignore "\\|")))
2214 (path "") (comps-in-path ()) (file "") (filepath "") (completions ()))
2215 ;; Go thru each path in the search path, finding completions.
2216 (while paths
2217 (setq path (file-name-as-directory (comint-directory (or (car paths) ".")))
2218 comps-in-path (and (file-accessible-directory-p path)
2219 (file-name-all-completions pathnondir path)))
2220 ;; Go thru each completion found, to see whether it should be used.
2221 (while comps-in-path
2222 (setq file (car comps-in-path)
2223 filepath (concat path file))
2224 (if (and (not (member file completions))
2225 (not (and ignored-extensions
2226 (string-match ignored-extensions file)))
2227 (or (string-equal path cwd)
2228 (not (file-directory-p filepath)))
2229 )
2230 (setq completions (cons file completions)))
2231 (setq comps-in-path (cdr comps-in-path)))
2232 (setq paths (cdr paths)))
2233 ;; OK, we've got a list of completions.
2234 (let ((success (let ((comint-completion-addsuffix nil))
2235 (comint-dynamic-simple-complete pathnondir completions))))
2236 (if (and (memq success '(sole shortest)) comint-completion-addsuffix
2237 (not (file-directory-p (comint-match-partial-filename))))
2238 (insert " "))
2239 success)))
2240
2241 (defun cafeobj-dynamic-complete-filename ()
2242 "Dynamically complete the filename at point.
2243 This completes only if point is at a suitable position for a
2244 filename argument."
2245 (interactive)
2246 (let ((opoint (point))
2247 (beg (comint-line-beginning-position)))
2248 (when (save-excursion
2249 (goto-char (if (re-search-backward "[;|&]" beg t)
2250 (match-end 0)
2251 beg))
2252 (re-search-forward "[^ \t][ \t]" opoint t))
2253 (comint-dynamic-complete-as-filename))))
2254
2255 (defun cafeobj-match-partial-variable ()
2256 "Return the shell variable at point, or nil if none is found."
2257 (save-excursion
2258 (let ((limit (point)))
2259 (if (re-search-backward "[^A-Za-z0-9_{}]" nil 'move)
2260 (or (looking-at "\\$") (forward-char 1)))
2261 ;; Anchor the search forwards.
2262 (if (or (eolp) (looking-at "[^A-Za-z0-9_{}$]"))
2263 nil
2264 (re-search-forward "\\$?{?[A-Za-z0-9_]*}?" limit)
2265 (buffer-substring (match-beginning 0) (match-end 0))))))
2266
2267
2268 (defun cafeobj-dynamic-complete-environment-variable ()
2269 "Dynamically complete the environment variable at point.
2270 Completes if after a variable, i.e., if it starts with a \"$\".
2271 See `cafeobj-dynamic-complete-as-environment-variable'.
2272
2273 This function is similar to `comint-dynamic-complete-filename', except that it
2274 searches `process-environment' for completion candidates. Note that this may
2275 not be the same as the interpreter's idea of variable names. The main problem
2276 with this type of completion is that `process-environment' is the environment
2277 which Emacs started with. Emacs does not track changes to the environment made
2278 by the interpreter. Perhaps it would be more accurate if this function was
2279 called `cafeobj-dynamic-complete-process-environment-variable'.
2280
2281 Returns non-nil if successful."
2282 (interactive)
2283 (let ((variable (cafeobj-match-partial-variable)))
2284 (if (and variable (string-match "^\\$" variable))
2285 (prog2 (message "Completing variable name...")
2286 (cafeobj-dynamic-complete-as-environment-variable)))))
2287
2288
2289 (defun cafeobj-dynamic-complete-as-environment-variable ()
2290 "Dynamically complete at point as an environment variable.
2291 Used by `cafeobj-dynamic-complete-environment-variable'.
2292 Uses `comint-dynamic-simple-complete'."
2293 (let* ((var (or (cafeobj-match-partial-variable) ""))
2294 (variable (substring var (or (string-match "[^$({]\\|$" var) 0)))
2295 (variables (mapcar (function (lambda (x)
2296 (substring x 0 (string-match "=" x))))
2297 process-environment))
2298 (addsuffix comint-completion-addsuffix)
2299 (comint-completion-addsuffix nil)
2300 (success (comint-dynamic-simple-complete variable variables)))
2301 (if (memq success '(sole shortest))
2302 (let* ((var (cafeobj-match-partial-variable))
2303 (variable (substring var (string-match "[^$({]" var)))
2304 (protection (cond ((string-match "{" var) "}")
2305 ((string-match "(" var) ")")
2306 (t "")))
2307 (suffix (cond ((null addsuffix) "")
2308 ((file-directory-p
2309 (comint-directory (getenv variable))) "/")
2310 (t " "))))
2311 (insert protection suffix)))
2312 success))
2313
2314
2315 (defun cafeobj-replace-by-expanded-directory ()
2316 "Expand directory stack reference before point.
2317 Directory stack references are of the form \"=digit\" or \"=-\".
2318 See `default-directory' and `cafeobj-dirstack'.
2319
2320 Returns t if successful."
2321 (interactive)
2322 (if (comint-match-partial-filename)
2323 (save-excursion
2324 (goto-char (match-beginning 0))
2325 (let ((stack (cons default-directory cafeobj-dirstack))
2326 (index (cond ((looking-at "=-/?")
2327 (length cafeobj-dirstack))
2328 ((looking-at "=\\([0-9]+\\)")
2329 (string-to-number
2330 (buffer-substring
2331 (match-beginning 1) (match-end 1)))))))
2332 (cond ((null index)
2333 nil)
2334 ((>= index (length stack))
2335 (error "Directory stack not that deep."))
2336 (t
2337 (replace-match (file-name-as-directory (nth index stack)) t t)
2338 (message "Directory item: %d" index)
2339 t))))))
2340
2341
2342
2343 ;;;
2344 (defvar menu-bar-mode (featurep 'menubar))
2345
2346 (defgroup cafeobj-xmas nil
2347 "XEmacs support for CafeOBJ"
2348 :group 'cafeobj)
2349
2350 (defcustom cafeobj-glyph-directory "/usr/local/cafeobj-1.4/icons"
2351 "Directory where CafeOBJ logos and icons are located."
2352 :type '(choice (const :tag "autodetect" nil)
2353 directory)
2354 :group 'cafeobj)
2355
2356 (defvar cafeobj-xmas-logo-color-alist
2357 '((flame "#cc3300" "#ff2200")
2358 (pine "#c0cc93" "#f8ffb8")
2359 (moss "#a1cc93" "#d2ffb8")
2360 (irish "#04cc90" "#05ff97")
2361 (sky "#049acc" "#05deff")
2362 (tin "#6886cc" "#82b6ff")
2363 (velvet "#7c68cc" "#8c82ff")
2364 (grape "#b264cc" "#cf7df")
2365 (labia "#cc64c2" "#fd7dff")
2366 (berry "#cc6485" "#ff7db5")
2367 (neutral "#b4b4b4" "#878787")
2368 (september "#bf9900" "#ffcc00"))
2369 "Color alist used for the Gnus logo.")
2370
2371 (defcustom cafeobj-xmas-logo-color-style 'moss
2372 "*Color styles used for the CafeOBJ logo."
2373 :type '(choice (const flame) (const pine) (const moss)
2374 (const irish) (const sky) (const tin)
2375 (const velvet) (const grape) (const labia)
2376 (const berry) (const neutral) (const september))
2377 :group 'cafeobj-xmas)
2378
2379 (defvar cafeobj-xmas-logo-colors
2380 (cdr (assq cafeobj-xmas-logo-color-style cafeobj-xmas-logo-color-alist))
2381 "Colors used for the CafeOBJ logo.")
2382
2383 (defvar cafeobj-xpm-size '(142 . 165))
2384
2385 ;; (defun cafeobj-startup-message (&optional x y)
2386 ;; "insert startup message in current buffer."
2387 ;; ;; Insert the message.
2388 ;; (setq cafeobj-glyph-directory
2389 ;; "/usr/local/cafeobj-1.4/icons")
2390 ;; (erase-buffer)
2391 ;; (cond
2392 ;; ((and (console-on-window-system-p)
2393 ;; (or (featurep 'xpm)
2394 ;; (featurep 'xbm)))
2395 ;; (let* ((logo-xpm (expand-file-name "cafe-logo.xpm" cafeobj-glyph-directory))
2396 ;; (logo-xbm (expand-file-name "cafe-logo.xbm" cafeobj-glyph-directory))
2397 ;; (glyph (make-glyph
2398 ;; (cond ((featurep 'xpm)
2399 ;; `[xpm
2400 ;; :file ,logo-xpm
2401 ;; :color-symbols
2402 ;; (("thing" . ,(car cafeobj-xmas-logo-colors))
2403 ;; ("shadow" . ,(cadr cafeobj-xmas-logo-colors))
2404 ;; ("background" . ,(face-background 'default)))])
2405 ;; ((featurep 'xbm)
2406 ;; `[xbm :file ,logo-xbm])
2407 ;; (t [nothing]))))
2408 ;; (char-per-pixel
2409 ;; (/ (* 1.0 (window-width)) (window-pixel-width)))
2410 ;; )
2411 ;; (insert " ")
2412 ;; (set-extent-begin-glyph (make-extent (point) (point)) glyph)
2413 ;; (goto-char (point-min))
2414 ;; (while (not (eobp))
2415 ;; (insert (make-string (truncate
2416 ;; (* (/ (max (- (window-pixel-width)
2417 ;; (or x
2418 ;; (car cafeobj-xpm-size)))
2419 ;; 0)
2420 ;; 2)
2421 ;; char-per-pixel))
2422 ;; ?\ ))
2423 ;; (forward-line 1))
2424 ;; )))
2425 ;; ;;
2426 ;; (goto-char (point-max))
2427 ;; (insert (make-string 2 ?\n))
2428 ;; ;;
2429 ;; (set-buffer-modified-p t))
2430
2431 ;;;
2432 ;;;
2433 (defvar cafeobj-comment-face-1 'cafeobj-comment-face-1)
2434 (defvar cafeobj-comment-face-2 'cafeobj-comment-face-2)
2435 (defvar cafeobj-string-face 'cafeobj-string-face)
2436 (defvar cafeobj-string-face-2 'cafeobj-string-face-2)
2437 (defvar cafeobj-keyword-face 'cafeobj-keyword-face)
2438 (defvar cafeobj-decl-keyword-face 'cafeobj-decl-keyword-face)
2439 (defvar cafeobj-command-name-face 'cafeobj-command-name-face)
2440 (defvar cafeobj-message-1-face 'cafeobj-message-1-face)
2441 (defvar cafeobj-message-2-face 'cafeobj-message-2-face)
2442 (defvar cafeobj-message-3-face 'cafeobj-message-3-face)
2443 (defvar cafeobj-error-face 'cafeobj-error-face)
2444 (defvar cafeobj-warning-face 'cafeobj-warning-face)
2445 (defvar cafeobj-pignose-keyword-face 'cafeobj-pignose-keyword-face)
2446 (defvar cafeobj-prompt-face 'cafeobj-prompt-face)
2447 (defvar cafeobj-process-keyword-face 'cafeobj-process-keyword-face)
2448 (defvar cafeobj-option-face 'cafeobj-option-face)
2449 (defvar cafeobj-output-face 'cafeobj-output-face)
2450
2451
2452
2453 (provide 'cafeobj-mode)
2454
2455 ;; Local Variables:
2456 ;; folded-file: t
2457 ;; End:
2458
2459 ;;; cafeobj-mode.el ends here