/[climacs]/climacs/file-commands.lisp
ViewVC logotype

Contents of /climacs/file-commands.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Sun Apr 23 12:36:19 2006 UTC (7 years, 11 months ago) by thenriksen
Branch: MAIN
Changes since 1.6: +1 -0 lines
Set (syntax buffer) to NIL so we can check whether or not the local
options line has set a syntax.
1 ;;; -*- Mode: Lisp; Package: CLIMACS-GUI -*-
2
3 ;;; (c) copyright 2004-2005 by
4 ;;; Robert Strandh (strandh@labri.fr)
5 ;;; (c) copyright 2004-2005 by
6 ;;; Elliott Johnson (ejohnson@fasl.info)
7 ;;; (c) copyright 2005 by
8 ;;; Matthieu Villeneuve (matthieu.villeneuve@free.fr)
9 ;;; (c) copyright 2005 by
10 ;;; Aleksandar Bakic (a_bakic@yahoo.com)
11
12 ;;; This library is free software; you can redistribute it and/or
13 ;;; modify it under the terms of the GNU Library General Public
14 ;;; License as published by the Free Software Foundation; either
15 ;;; version 2 of the License, or (at your option) any later version.
16 ;;;
17 ;;; This library is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;;; Library General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU Library General Public
23 ;;; License along with this library; if not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307 USA.
26
27 ;;; File commands for the Climacs editor.
28
29 (in-package :climacs-gui)
30
31 (defun filename-completer (so-far mode)
32 (flet ((remove-trail (s)
33 (subseq s 0 (let ((pos (position #\/ s :from-end t)))
34 (if pos (1+ pos) 0)))))
35 (let* ((directory-prefix
36 (if (and (plusp (length so-far)) (eql (aref so-far 0) #\/))
37 ""
38 (namestring #+sbcl *default-pathname-defaults*
39 #+cmu (ext:default-directory)
40 #-(or sbcl cmu) *default-pathname-defaults*)))
41 (full-so-far (concatenate 'string directory-prefix so-far))
42 (pathnames
43 (loop with length = (length full-so-far)
44 and wildcard = (concatenate 'string (remove-trail so-far) "*.*")
45 for path in
46 #+(or sbcl cmu lispworks) (directory wildcard)
47 #+openmcl (directory wildcard :directories t)
48 #+allegro (directory wildcard :directories-are-files nil)
49 #+cormanlisp (nconc (directory wildcard)
50 (cl::directory-subdirs dirname))
51 #-(or sbcl cmu lispworks openmcl allegro cormanlisp)
52 (directory wildcard)
53 when (let ((mismatch (mismatch (namestring path) full-so-far)))
54 (or (null mismatch) (= mismatch length)))
55 collect path))
56 (strings (mapcar #'namestring pathnames))
57 (first-string (car strings))
58 (length-common-prefix nil)
59 (completed-string nil)
60 (full-completed-string nil))
61 (unless (null pathnames)
62 (setf length-common-prefix
63 (loop with length = (length first-string)
64 for string in (cdr strings)
65 do (setf length (min length (or (mismatch string first-string) length)))
66 finally (return length))))
67 (unless (null pathnames)
68 (setf completed-string
69 (subseq first-string (length directory-prefix)
70 (if (null (cdr pathnames)) nil length-common-prefix)))
71 (setf full-completed-string
72 (concatenate 'string directory-prefix completed-string)))
73 (case mode
74 ((:complete-limited :complete-maximal)
75 (cond ((null pathnames)
76 (values so-far nil nil 0 nil))
77 ((null (cdr pathnames))
78 (values completed-string t (car pathnames) 1 nil))
79 (t
80 (values completed-string nil nil (length pathnames) nil))))
81 (:complete
82 (cond ((null pathnames)
83 (values so-far t so-far 1 nil))
84 ((null (cdr pathnames))
85 (values completed-string t (car pathnames) 1 nil))
86 ((find full-completed-string strings :test #'string-equal)
87 (let ((pos (position full-completed-string strings :test #'string-equal)))
88 (values completed-string
89 t (elt pathnames pos) (length pathnames) nil)))
90 (t
91 (values completed-string nil nil (length pathnames) nil))))
92 (:possibilities
93 (values nil nil nil (length pathnames)
94 (loop with length = (length directory-prefix)
95 for name in pathnames
96 collect (list (subseq (namestring name) length nil)
97 name))))))))
98
99 (define-presentation-method present (object (type pathname)
100 stream (view climacs-textual-view) &key)
101 (princ (namestring object) stream))
102
103 (define-presentation-method accept ((type pathname) stream (view climacs-textual-view)
104 &key (default nil defaultp) (default-type type))
105 (multiple-value-bind (pathname success string)
106 (complete-input stream
107 #'filename-completer
108 :allow-any-input t)
109 (cond (success
110 (values (or pathname (parse-namestring string)) type))
111 ((and (zerop (length string))
112 defaultp)
113 (values default default-type))
114 (t (values string 'string)))))
115
116 (defun filepath-filename (pathname)
117 (if (null (pathname-type pathname))
118 (pathname-name pathname)
119 (concatenate 'string (pathname-name pathname)
120 "." (pathname-type pathname))))
121
122 (defun syntax-class-name-for-filepath (filepath)
123 (or (climacs-syntax::syntax-description-class-name
124 (find (or (pathname-type filepath)
125 (pathname-name filepath))
126 climacs-syntax::*syntaxes*
127 :test (lambda (x y)
128 (member x y :test #'string-equal))
129 :key #'climacs-syntax::syntax-description-pathname-types))
130 'basic-syntax))
131
132 (defun parse-local-options-line (line)
133 "Parse the local options line `line' and return an alist
134 mapping options to values. All option names will be coerced to
135 uppercase. `Line' must be stripped of the leading and
136 terminating -*- tokens."
137 (loop for pair in (split-sequence:split-sequence #\; line)
138 when (find #\: pair)
139 collect (destructuring-bind (key value)
140 (loop for elem in (split-sequence:split-sequence #\: pair)
141 collecting (string-trim " " elem))
142 (list (string-upcase key) value))))
143
144 (defun evaluate-local-options (buffer options)
145 "Evaluate the local options `options' and modify `buffer' as
146 appropriate. `Options' should be an alist mapping option names
147 to their values."
148 ;; First, check whether we need to change the syntax (via the SYNTAX
149 ;; option). MODE is an alias for SYNTAX for compatibility with
150 ;; Emacs. If there is more than one option with one of these names,
151 ;; only the first will be acted upon.
152 (let ((specified-syntax
153 (syntax-from-name
154 (second (find-if #'(lambda (name)
155 (or (string= name "SYNTAX")
156 (string= name "MODE")))
157 options
158 :key #'first)))))
159 (when specified-syntax
160 (setf (syntax buffer)
161 (make-instance specified-syntax
162 :buffer buffer))))
163 ;; Now we iterate through the options (discarding SYNTAX and MODE
164 ;; options).
165 (loop for (name value) in options
166 unless (or (string= name "SYNTAX")
167 (string= name "MODE"))
168 do (eval-option (syntax buffer) name value)))
169
170 (defun evaluate-local-options-line (buffer)
171 "Evaluate the local options line of `buffer'. If `buffer' does
172 not have a local options line, this function is a no-op."
173 ;; This could be simplified a bit by using regexps.
174 (let* ((beginning-mark (beginning-of-buffer
175 (clone-mark (point buffer))))
176 (end-mark (end-of-line (clone-mark beginning-mark)))
177 (line (buffer-sequence buffer (offset beginning-mark) (offset end-mark)))
178 (first-occurence (search "-*-" line))
179 (second-occurence
180 (when first-occurence
181 (search "-*-" line :start2 (1+ first-occurence)))))
182 (when (and first-occurence
183 second-occurence)
184 ;; Strip away the -*-s.
185 (let ((cleaned-options-line (coerce (subseq line
186 (+ first-occurence 3)
187 second-occurence)
188 'string)))
189 (evaluate-local-options
190 buffer
191 (parse-local-options-line cleaned-options-line))))))
192
193 ;; Adapted from cl-fad/PCL
194 (defun directory-pathname-p (pathspec)
195 "Returns NIL if PATHSPEC does not designate a directory."
196 (let ((name (pathname-name pathspec))
197 (type (pathname-type pathspec)))
198 (and (or (null name) (eql name :unspecific))
199 (or (null type) (eql type :unspecific)))))
200
201 (defun find-file (filepath)
202 (cond ((null filepath)
203 (display-message "No file name given.")
204 (beep))
205 ((directory-pathname-p filepath)
206 (display-message "~A is a directory name." filepath)
207 (beep))
208 (t
209 (let ((existing-buffer (find filepath (buffers *application-frame*)
210 :key #'filepath :test #'equal)))
211 (if existing-buffer
212 (switch-to-buffer existing-buffer)
213 (let ((buffer (make-buffer))
214 (pane (current-window)))
215 (setf (syntax buffer) nil)
216 (setf (offset (point (buffer pane))) (offset (point pane)))
217 (setf (buffer (current-window)) buffer)
218 ;; Don't want to create the file if it doesn't exist.
219 (when (probe-file filepath)
220 (with-open-file (stream filepath :direction :input)
221 (input-from-stream stream buffer 0))
222 ;; A file! That means we may have a local options
223 ;; line to parse.
224 (evaluate-local-options-line buffer))
225 ;; If the local options line didn't set a syntax, do
226 ;; it now.
227 (when (null (syntax buffer))
228 (setf (syntax buffer)
229 (make-instance (syntax-class-name-for-filepath filepath)
230 :buffer buffer)))
231 (setf (filepath buffer) filepath
232 (name buffer) (filepath-filename filepath)
233 (needs-saving buffer) nil)
234 (beginning-of-buffer (point pane))
235 (update-syntax buffer (syntax buffer))
236 (clear-modify buffer)
237 buffer))))))
238
239 (defun directory-of-buffer (buffer)
240 "Extract the directory part of the filepath to the file in BUFFER.
241 If BUFFER does not have a filepath, the path to the users home
242 directory will be returned."
243 (make-pathname
244 :directory
245 (pathname-directory
246 (or (filepath buffer)
247 (user-homedir-pathname)))))
248
249 (define-command (com-find-file :name t :command-table buffer-table) ()
250 (let* ((filepath (accept 'pathname :prompt "Find File"
251 :default (directory-of-buffer (buffer (current-window)))
252 :default-type 'pathname
253 :insert-default t)))
254 (find-file filepath)))
255
256 (set-key 'com-find-file
257 'buffer-table
258 '((#\x :control) (#\f :control)))
259
260 (defun find-file-read-only (filepath)
261 (cond ((null filepath)
262 (display-message "No file name given.")
263 (beep))
264 ((directory-pathname-p filepath)
265 (display-message "~A is a directory name." filepath)
266 (beep))
267 (t
268 (let ((existing-buffer (find filepath (buffers *application-frame*)
269 :key #'filepath :test #'equal)))
270 (if (and existing-buffer (read-only-p existing-buffer))
271 (switch-to-buffer existing-buffer)
272 (if (probe-file filepath)
273 (let ((buffer (make-buffer))
274 (pane (current-window)))
275 (setf (offset (point (buffer pane))) (offset (point pane)))
276 (setf (buffer (current-window)) buffer)
277 (setf (syntax buffer)
278 (make-instance (syntax-class-name-for-filepath filepath)
279 :buffer (buffer (point pane))))
280 (with-open-file (stream filepath :direction :input)
281 (input-from-stream stream buffer 0))
282 (setf (filepath buffer) filepath
283 (name buffer) (filepath-filename filepath)
284 (needs-saving buffer) nil
285 (read-only-p buffer) t)
286 (beginning-of-buffer (point pane))
287 ;; this one is needed so that the buffer modification protocol
288 ;; resets the low and high marks after redisplay
289 (redisplay-frame-panes *application-frame*)
290 buffer)
291 (progn
292 (display-message "No such file: ~A" filepath)
293 (beep)
294 nil)))))))
295
296 (define-command (com-find-file-read-only :name t :command-table buffer-table) ()
297 (let ((filepath (accept 'pathname :Prompt "Find file read only"
298 :default (directory-of-buffer (buffer (current-window)))
299 :default-type 'pathname
300 :insert-default t)))
301 (find-file-read-only filepath)))
302
303 (set-key 'com-find-file-read-only
304 'buffer-table
305 '((#\x :control) (#\r :control)))
306
307 (define-command (com-read-only :name t :command-table buffer-table) ()
308 (let ((buffer (buffer (current-window))))
309 (setf (read-only-p buffer) (not (read-only-p buffer)))))
310
311 (set-key 'com-read-only
312 'buffer-table
313 '((#\x :control) (#\q :control)))
314
315 (defun set-visited-file-name (filename buffer)
316 (setf (filepath buffer) filename
317 (name buffer) (filepath-filename filename)
318 (needs-saving buffer) t))
319
320 (define-command (com-set-visited-file-name :name t :command-table buffer-table) ()
321 (let ((filename (accept 'pathname :prompt "New file name"
322 :default (directory-of-buffer (buffer (current-window)))
323 :default-type 'pathname
324 :insert-default t)))
325 (set-visited-file-name filename (buffer (current-window)))))
326
327 (define-command (com-insert-file :name t :command-table buffer-table) ()
328 (let ((filename (accept 'pathname :prompt "Insert File"
329 :default (directory-of-buffer (buffer (current-window)))
330 :default-type 'pathname
331 :insert-default t))
332 (pane (current-window)))
333 (when (probe-file filename)
334 (setf (mark pane) (clone-mark (point pane) :left))
335 (with-open-file (stream filename :direction :input)
336 (input-from-stream stream
337 (buffer pane)
338 (offset (point pane))))
339 (psetf (offset (mark pane)) (offset (point pane))
340 (offset (point pane)) (offset (mark pane))))
341 (redisplay-frame-panes *application-frame*)))
342
343 (set-key 'com-insert-file
344 'buffer-table
345 '((#\x :control) (#\i :control)))
346
347 (define-command (com-revert-buffer :name t :command-table buffer-table) ()
348 (let* ((pane (current-window))
349 (buffer (buffer pane))
350 (filepath (filepath buffer))
351 (save (offset (point pane))))
352 (when (accept 'boolean :prompt (format nil "Revert buffer from file ~A?"
353 (filepath buffer)))
354 (cond ((directory-pathname-p filepath)
355 (display-message "~A is a directory name." filepath)
356 (beep))
357 ((probe-file filepath)
358 (erase-buffer buffer)
359 (with-open-file (stream filepath :direction :input)
360 (input-from-stream stream buffer 0))
361 (setf (offset (point pane))
362 (min (size buffer) save)))
363 (t
364 (display-message "No file ~A" filepath)
365 (beep))))))
366
367 (defun save-buffer (buffer)
368 (let ((filepath (or (filepath buffer)
369 (accept 'pathname :prompt "Save Buffer to File"))))
370 (cond
371 ((directory-pathname-p filepath)
372 (display-message "~A is a directory." filepath)
373 (beep))
374 (t
375 (when (probe-file filepath)
376 (let ((backup-name (pathname-name filepath))
377 (backup-type (concatenate 'string (pathname-type filepath) "~")))
378 (rename-file filepath (make-pathname :name backup-name
379 :type backup-type))))
380 (with-open-file (stream filepath :direction :output :if-exists :supersede)
381 (output-to-stream stream buffer 0 (size buffer)))
382 (setf (filepath buffer) filepath
383 (name buffer) (filepath-filename filepath))
384 (display-message "Wrote: ~a" (filepath buffer))
385 (setf (needs-saving buffer) nil)))))
386
387 (define-command (com-save-buffer :name t :command-table buffer-table) ()
388 (let ((buffer (buffer (current-window))))
389 (if (or (null (filepath buffer))
390 (needs-saving buffer))
391 (save-buffer buffer)
392 (display-message "No changes need to be saved from ~a" (name buffer)))))
393
394 (set-key 'com-save-buffer
395 'buffer-table
396 '((#\x :control) (#\s :control)))
397
398 (defmethod frame-exit :around ((frame climacs) #-mcclim &key)
399 (loop for buffer in (buffers frame)
400 when (and (needs-saving buffer)
401 (filepath buffer)
402 (handler-case (accept 'boolean
403 :prompt (format nil "Save buffer: ~a ?" (name buffer)))
404 (error () (progn (beep)
405 (display-message "Invalid answer")
406 (return-from frame-exit nil)))))
407 do (save-buffer buffer))
408 (when (or (notany #'(lambda (buffer) (and (needs-saving buffer) (filepath buffer)))
409 (buffers frame))
410 (handler-case (accept 'boolean :prompt "Modified buffers exist. Quit anyway?")
411 (error () (progn (beep)
412 (display-message "Invalid answer")
413 (return-from frame-exit nil)))))
414 (call-next-method)))
415
416 (define-command (com-write-buffer :name t :command-table buffer-table) ()
417 (let ((filepath (accept 'pathname :prompt "Write Buffer to File"
418 :default (directory-of-buffer (buffer (current-window)))
419 :default-type 'pathname
420 :insert-default t))
421 (buffer (buffer (current-window))))
422 (cond
423 ((directory-pathname-p filepath)
424 (display-message "~A is a directory name." filepath))
425 (t
426 (with-open-file (stream filepath :direction :output :if-exists :supersede)
427 (output-to-stream stream buffer 0 (size buffer)))
428 (setf (filepath buffer) filepath
429 (name buffer) (filepath-filename filepath)
430 (needs-saving buffer) nil)
431 (display-message "Wrote: ~a" (filepath buffer))))))
432
433 (set-key 'com-write-buffer
434 'buffer-table
435 '((#\x :control) (#\w :control)))
436

  ViewVC Help
Powered by ViewVC 1.1.5