/[cmucl]/src/hemlock/main.lisp
ViewVC logotype

Contents of /src/hemlock/main.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Wed Jun 5 14:50:25 1991 UTC (22 years, 10 months ago) by chiles
Branch: MAIN
Changes since 1.3: +7 -1 lines
Added a doc string to EXT:SAVE-ALL-BUFFERS.
1 ;;; -*- Log: hemlock.log; Package: Hemlock-Internals -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/main.lisp,v 1.4 1991/06/05 14:50:25 chiles Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; Hemlock initialization code and random debugging stuff.
15 ;;;
16 ;;; Written by Bill Chiles and Rob MacLachlan
17 ;;;
18
19 (in-package "HEMLOCK-INTERNALS")
20
21 (export '(*global-variable-names* *mode-names* *buffer-names*
22 *character-attribute-names* *command-names* *buffer-list*
23 *window-list* *last-key-event-typed* after-editor-initializations))
24
25
26 (in-package "EXTENSIONS")
27 (export '(save-all-buffers *hemlock-version*))
28 (in-package "HEMLOCK-INTERNALS")
29
30
31
32 ;;;; Definition of *hemlock-version*.
33
34 (defvar *hemlock-version* "1.1(?)")
35
36
37
38 ;;;; %INIT-HEMLOCK.
39
40 (defun %init-hemlock ()
41 "Initialize hemlock's internal data structures."
42 ;;
43 ;; This function is defined in Buffer.Lisp. It creates fundamental mode
44 ;; and the buffer main. Until this is done it is not possible to define
45 ;; or use Hemlock variables.
46 (setup-initial-buffer)
47 ;;
48 ;; Define some of the system variables.
49 (define-some-variables)
50 ;;
51 ;; Site initializations such as window system variables.
52 (site-init)
53 ;;
54 ;; Set up syntax table data structures.
55 (%init-syntax-table)
56 ;;
57 ;; Define print representations for funny characters.
58 (%init-line-image))
59
60
61
62 ;;;; Define some globals.
63
64 ;;; These globals cannot be defined in the appropriate file due to compilation
65 ;;; or load time constraints.
66 ;;;
67
68 ;;; The following belong in other files, but those files are loaded before
69 ;;; table.lisp which defines MAKE-STRING-TABLE.
70 ;;;
71 ;;; vars.lisp
72 (defvar *global-variable-names* (make-string-table)
73 "A String Table of global variable names, the values are the symbol names.")
74 ;;;
75 ;;; buffer.lisp
76 (defvar *mode-names* (make-string-table) "A String Table of Mode names.")
77 (defvar *buffer-names* (make-string-table)
78 "A String Table of Buffer names and their corresponding objects.")
79 ;;;
80 ;;; interp.lisp
81 (defvar *command-names* (make-string-table) "String table of command names.")
82 ;;;
83 ;;; syntax.lisp
84 (defvar *character-attribute-names* (make-string-table)
85 "String Table of character attribute names and their corresponding keywords.")
86
87
88
89 ;;;; DEFINE-SOME-VARIABLES.
90
91 ;;; This is necessary to define "Default Status Line Fields" which belongs
92 ;;; beside the other modeline variables. This DEFVAR would live in
93 ;;; Morecoms.Lisp, but it is compiled and loaded after this file.
94 ;;;
95 (proclaim '(special ed::*recursive-edit-count*))
96 ;;;
97 (make-modeline-field
98 :name :edit-level :width 15
99 :function #'(lambda (buffer window)
100 (declare (ignore buffer window))
101 (if (zerop ed::*recursive-edit-count*)
102 ""
103 (format nil "Edit Level: ~2,'0D "
104 ed::*recursive-edit-count*))))
105
106 ;;; This is necessary to define "Default Status Line Fields" which belongs
107 ;;; beside the other modeline variables. This DEFVAR would live in
108 ;;; Completion.Lisp, but it is compiled and loaded after this file.
109 ;;;
110 (proclaim '(special ed::*completion-mode-possibility*))
111 ;;; Hack for now until completion mode is added.
112 (defvar ed::*completion-mode-possibility* "")
113 ;;;
114 (make-modeline-field
115 :name :completion :width 40
116 :function #'(lambda (buffer window)
117 (declare (ignore buffer window))
118 ed::*completion-mode-possibility*))
119
120
121 (defun define-some-variables ()
122 (defhvar "Default Modes"
123 "This variable contains the default list of modes for new buffers."
124 :value '("Fundamental" "Save"))
125 (defhvar "Echo Area Height"
126 "Number of lines in the echo area window."
127 :value 3)
128 (defhvar "Make Buffer Hook"
129 "This hook is called with the new buffer whenever a buffer is created.")
130 (defhvar "Delete Buffer Hook"
131 "This hook is called with the buffer whenever a buffer is deleted.")
132 (defhvar "Enter Recursive Edit Hook"
133 "This hook is called with the new buffer when a recursive edit is
134 entered.")
135 (defhvar "Exit Recursive Edit Hook"
136 "This hook is called with the value returned when a recursive edit
137 is exited.")
138 (defhvar "Abort Recursive Edit Hook"
139 "This hook is called with the editor-error args when a recursive
140 edit is aborted.")
141 (defhvar "Buffer Major Mode Hook"
142 "This hook is called with the buffer and the new mode when a buffer's
143 major mode is changed.")
144 (defhvar "Buffer Minor Mode Hook"
145 "This hook is called a minor mode is changed. The arguments are
146 the buffer, the mode affected and T or NIL depending on when the
147 mode is being turned on or off.")
148 (defhvar "Buffer Writable Hook"
149 "This hook is called whenever someone sets whether the buffer is
150 writable.")
151 (defhvar "Buffer Name Hook"
152 "This hook is called with the buffer and the new name when the name of a
153 buffer is changed.")
154 (defhvar "Buffer Pathname Hook"
155 "This hook is called with the buffer and the new Pathname when the Pathname
156 associated with the buffer is changed.")
157 (defhvar "Buffer Modified Hook"
158 "This hook is called whenever a buffer changes from unmodified to modified
159 and vice versa. It takes the buffer and the new value for modification
160 flag.")
161 (defhvar "Set Buffer Hook"
162 "This hook is called with the new buffer when the current buffer is set.")
163 (defhvar "After Set Buffer Hook"
164 "This hook is invoked with the old buffer after the current buffer has
165 been changed.")
166 (defhvar "Set Window Hook"
167 "This hook is called with the new window when the current window
168 is set.")
169 (defhvar "Make Window Hook"
170 "This hook is called with a new window when one is created.")
171 (defhvar "Delete Window Hook"
172 "This hook is called with a window before it is deleted.")
173 (defhvar "Window Buffer Hook"
174 "This hook is invoked with the window and new buffer when a window's
175 buffer is changed.")
176 (defhvar "Delete Variable Hook"
177 "This hook is called when a variable is deleted with the args to
178 delete-variable.")
179 (defhvar "Entry Hook"
180 "this hook is called when the editor is entered.")
181 (defhvar "Exit Hook"
182 "This hook is called when the editor is exited.")
183 (defhvar "Redisplay Hook"
184 "This is called on the current window from REDISPLAY after checking the
185 window display start, window image, and recentering. After calling the
186 functions in this hook, we do the above stuff and call the smart
187 redisplay method for the device."
188 :value nil)
189 (defhvar "Key Echo Delay"
190 "Wait this many seconds before echoing keys in the command loop. This
191 feature is inhibited when nil."
192 :value 1.0)
193 (defhvar "Input Hook"
194 "The functions in this variable are invoked each time a character enters
195 Hemlock."
196 :value nil)
197 (defhvar "Abort Hook"
198 "These functions are invoked when ^G is typed. No arguments are passed."
199 :value nil)
200 (defhvar "Command Abort Hook"
201 "These functions get called when commands are aborted, such as with
202 EDITOR-ERROR."
203 :value nil)
204 (defhvar "Character Attribute Hook"
205 "This hook is called with the attribute, character and new value
206 when the value of a character attribute is changed.")
207 (defhvar "Shadow Attribute Hook"
208 "This hook is called when a mode character attribute is made.")
209 (defhvar "Unshadow Attribute Hook"
210 "This hook is called when a mode character attribute is deleted.")
211 (defhvar "Default Modeline Fields"
212 "The default list of modeline-fields for MAKE-WINDOW."
213 :value *default-modeline-fields*)
214 (defhvar "Default Status Line Fields"
215 "This is the default list of modeline-fields for the echo area window's
216 modeline which is used for general information."
217 :value (list (make-modeline-field
218 :name :hemlock-banner :width 26
219 :function #'(lambda (buffer window)
220 (declare (ignore buffer window))
221 (format nil "Hemlock ~A "
222 *hemlock-version*)))
223 (modeline-field :edit-level)
224 (modeline-field :completion)))
225 (defhvar "Maximum Modeline Pathname Length"
226 "When set, this variable is the maximum length of the display of a pathname
227 in a modeline. When the pathname is too long, the :buffer-pathname
228 modeline-field function chops off leading directory specifications until
229 the pathname fits. \"...\" indicates a truncated pathname."
230 :value nil
231 :hooks (list 'maximum-modeline-pathname-length-hook)))
232
233
234
235 ;;;; ED.
236
237 (defvar *editor-has-been-entered* ()
238 "True if and only if the editor has been entered.")
239 (defvar *in-the-editor* ()
240 "True if we are inside the editor. This is used to prevent ill-advised
241 \"recursive\" edits.")
242
243 (defvar *after-editor-initializations-funs* nil
244 "A list of functions to be called after the editor has been initialized upon
245 entering the first time.")
246
247 (defmacro after-editor-initializations (&rest forms)
248 "Causes forms to be executed after the editor has been initialized.
249 Forms supplied with successive uses of this macro will be executed after
250 forms supplied with previous uses."
251 `(push #'(lambda () ,@forms)
252 *after-editor-initializations-funs*))
253
254 (defun ed (&optional x
255 &key (init t)
256 (display (cdr (assoc :display ext:*environment-list*))))
257 "Invokes the editor, Hemlock. If X is supplied and is a symbol, the
258 definition of X is put into a buffer, and that buffer is selected. If X
259 is a pathname, the file specified by X is visited in a new buffer. If X
260 is not supplied or Nil, the editor is entered in the same state as when
261 last exited. When :init is supplied as t (the default), the file
262 \"hemlock-init.fasl\" or \"hemlock-init.lisp\" is loaded from the home or
263 default directory, but the Lisp command line switch -hinit can be used to
264 specify a different name. If the argument is non-nil and not t, then it
265 should be a pathname that will be merged with the home or default directory.
266 The display argument is not currently supported."
267 (when *in-the-editor* (error "You are already in the editor, you bogon!"))
268 (let ((*in-the-editor* t)
269 (display (unless *editor-has-been-entered*
270 (maybe-load-hemlock-init init)
271 ;; Device dependent initializaiton.
272 (init-raw-io display))))
273 (catch 'editor-top-level-catcher
274 (site-wrapper-macro
275 (unless *editor-has-been-entered*
276 ;; Make an initial window, and set up redisplay's internal
277 ;; data structures.
278 (%init-redisplay display)
279 (setq *editor-has-been-entered* t)
280 ;; Pick up user initializations to be done after initialization.
281 (invoke-hook (reverse *after-editor-initializations-funs*)))
282 (catch 'hemlock-exit
283 (catch 'editor-top-level-catcher
284 (cond ((and x (symbolp x))
285 (let* ((name (nstring-capitalize
286 (concatenate 'simple-string "Edit " (string x))))
287 (buffer (or (getstring name *buffer-names*)
288 (make-buffer name)))
289 (*print-case* :downcase))
290 (delete-region (buffer-region buffer))
291 (with-output-to-mark
292 (*standard-output* (buffer-point buffer))
293 (eval `(grindef ,x)) ; hackish, I know...
294 (terpri)
295 (ed::change-to-buffer buffer)
296 (buffer-start (buffer-point buffer)))))
297 ((or (stringp x) (pathnamep x))
298 (ed::find-file-command () x))
299 (x
300 (error
301 "~S is not a symbol or pathname. I can't edit it!" x))))
302
303 (invoke-hook ed::entry-hook)
304 (unwind-protect
305 (loop
306 (catch 'editor-top-level-catcher
307 (handler-bind ((error #'(lambda (condition)
308 (lisp-error-error-handler condition
309 :internal))))
310 (invoke-hook ed::abort-hook)
311 (%command-loop))))
312 (invoke-hook ed::exit-hook)))))))
313
314 (defun maybe-load-hemlock-init (init)
315 (when init
316 (let* ((name (case init
317 ((t) (let ((switch (find "hinit" *command-line-switches*
318 :test #'string-equal
319 :key #'cmd-switch-name)))
320 (if switch
321 (or (cmd-switch-value switch)
322 (car (cmd-switch-words switch))
323 "hemlock-init")
324 "hemlock-init")))
325 (t (pathname init)))))
326 (load (merge-pathnames name (user-homedir-pathname))
327 :if-does-not-exist nil))))
328
329
330
331 ;;;; SAVE-ALL-BUFFERS.
332
333 ;;; SAVE-ALL-BUFFERS -- Public.
334 ;;;
335 (defun save-all-buffers (&optional (list-unmodified-buffers nil))
336 "This prompts users with each modified buffer as to whether they want to
337 write it out. If the buffer has no associated file, this will also prompt
338 for a file name. Supplying the optional argument non-nil causes this
339 to prompt for every buffer."
340 (dolist (buffer *buffer-list*)
341 (when (or list-unmodified-buffers (buffer-modified buffer))
342 (maybe-save-buffer buffer))))
343
344 (defun maybe-save-buffer (buffer)
345 (let* ((modified (buffer-modified buffer))
346 (pathname (buffer-pathname buffer))
347 (name (buffer-name buffer))
348 (string (if pathname (namestring pathname))))
349 (format t "Buffer ~S is ~:[UNmodified~;modified~], Save it? "
350 name modified)
351 (when (y-or-n-p)
352 (let ((name (read-line-default "File to write" string)))
353 (format t "Writing file ~A..." name)
354 (force-output)
355 (write-file (buffer-region buffer) name)
356 (write-line "write WON")))))
357
358 (defun read-line-default (prompt default)
359 (format t "~A:~@[ [~A]~] " prompt default)
360 (do ((result (read-line) (read-line)))
361 (())
362 (declare (simple-string result))
363 (when (plusp (length result)) (return result))
364 (when default (return default))
365 (format t "~A:~@[ [~A]~] " prompt default)))

  ViewVC Help
Powered by ViewVC 1.1.5