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

Contents of /climacs/file-commands.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sat Mar 25 20:58:41 2006 UTC (8 years ago) by crhodes
Branch: MAIN
Changes since 1.3: +1 -1 lines
Make the buffer of a syntax be the buffer created by MAKE-BUFFER rather
than the buffer of (point pane).  (In practice, this means the
climacs-buffer (delegating) rather than the implementation buffer).

(This is largely to facilitate the ability to print tablature along with
the buffer name; there are probably other reasons to allow this.)
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 ;; Adapted from cl-fad/PCL
133 (defun directory-pathname-p (pathspec)
134 "Returns NIL if PATHSPEC does not designate a directory."
135 (let ((name (pathname-name pathspec))
136 (type (pathname-type pathspec)))
137 (and (or (null name) (eql name :unspecific))
138 (or (null type) (eql type :unspecific)))))
139
140 (defun find-file (filepath)
141 (cond ((null filepath)
142 (display-message "No file name given.")
143 (beep))
144 ((directory-pathname-p filepath)
145 (display-message "~A is a directory name." filepath)
146 (beep))
147 (t
148 (let ((existing-buffer (find filepath (buffers *application-frame*)
149 :key #'filepath :test #'equal)))
150 (if existing-buffer
151 (switch-to-buffer existing-buffer)
152 (let ((buffer (make-buffer))
153 (pane (current-window)))
154 (setf (offset (point (buffer pane))) (offset (point pane)))
155 (setf (buffer (current-window)) buffer)
156 (setf (syntax buffer)
157 (make-instance (syntax-class-name-for-filepath filepath)
158 :buffer buffer))
159 ;; Don't want to create the file if it doesn't exist.
160 (when (probe-file filepath)
161 (with-open-file (stream filepath :direction :input)
162 (input-from-stream stream buffer 0)))
163 (setf (filepath buffer) filepath
164 (name buffer) (filepath-filename filepath)
165 (needs-saving buffer) nil)
166 (beginning-of-buffer (point pane))
167 ;; this one is needed so that the buffer modification protocol
168 ;; resets the low and high marks after redisplay
169 (redisplay-frame-panes *application-frame*)
170 buffer))))))
171
172 (defun directory-of-buffer (buffer)
173 "Extract the directory part of the filepath to the file in BUFFER.
174 If BUFFER does not have a filepath, the path to the users home
175 directory will be returned."
176 (make-pathname
177 :directory
178 (pathname-directory
179 (or (filepath buffer)
180 (user-homedir-pathname)))))
181
182 (define-command (com-find-file :name t :command-table buffer-table) ()
183 (let* ((filepath (accept 'pathname :prompt "Find File"
184 :default (directory-of-buffer (buffer (current-window)))
185 :default-type 'pathname
186 :insert-default t)))
187 (find-file filepath)))
188
189 (set-key 'com-find-file
190 'buffer-table
191 '((#\x :control) (#\f :control)))
192
193 (defun find-file-read-only (filepath)
194 (cond ((null filepath)
195 (display-message "No file name given.")
196 (beep))
197 ((directory-pathname-p filepath)
198 (display-message "~A is a directory name." filepath)
199 (beep))
200 (t
201 (let ((existing-buffer (find filepath (buffers *application-frame*)
202 :key #'filepath :test #'equal)))
203 (if (and existing-buffer (read-only-p existing-buffer))
204 (switch-to-buffer existing-buffer)
205 (if (probe-file filepath)
206 (let ((buffer (make-buffer))
207 (pane (current-window)))
208 (setf (offset (point (buffer pane))) (offset (point pane)))
209 (setf (buffer (current-window)) buffer)
210 (setf (syntax buffer)
211 (make-instance (syntax-class-name-for-filepath filepath)
212 :buffer (buffer (point pane))))
213 (with-open-file (stream filepath :direction :input)
214 (input-from-stream stream buffer 0))
215 (setf (filepath buffer) filepath
216 (name buffer) (filepath-filename filepath)
217 (needs-saving buffer) nil
218 (read-only-p buffer) t)
219 (beginning-of-buffer (point pane))
220 ;; this one is needed so that the buffer modification protocol
221 ;; resets the low and high marks after redisplay
222 (redisplay-frame-panes *application-frame*)
223 buffer)
224 (progn
225 (display-message "No such file: ~A" filepath)
226 (beep)
227 nil)))))))
228
229 (define-command (com-find-file-read-only :name t :command-table buffer-table) ()
230 (let ((filepath (accept 'pathname :Prompt "Find file read only"
231 :default (directory-of-buffer (buffer (current-window)))
232 :default-type 'pathname
233 :insert-default t)))
234 (find-file-read-only filepath)))
235
236 (set-key 'com-find-file-read-only
237 'buffer-table
238 '((#\x :control) (#\r :control)))
239
240 (define-command (com-read-only :name t :command-table buffer-table) ()
241 (let ((buffer (buffer (current-window))))
242 (setf (read-only-p buffer) (not (read-only-p buffer)))))
243
244 (set-key 'com-read-only
245 'buffer-table
246 '((#\x :control) (#\q :control)))
247
248 (defun set-visited-file-name (filename buffer)
249 (setf (filepath buffer) filename
250 (name buffer) (filepath-filename filename)
251 (needs-saving buffer) t))
252
253 (define-command (com-set-visited-file-name :name t :command-table buffer-table) ()
254 (let ((filename (accept 'pathname :prompt "New file name"
255 :default (directory-of-buffer (buffer (current-window)))
256 :default-type 'pathname
257 :insert-default t)))
258 (set-visited-file-name filename (buffer (current-window)))))
259
260 (define-command (com-insert-file :name t :command-table buffer-table) ()
261 (let ((filename (accept 'pathname :prompt "Insert File"
262 :default (directory-of-buffer (buffer (current-window)))
263 :default-type 'pathname
264 :insert-default t))
265 (pane (current-window)))
266 (when (probe-file filename)
267 (setf (mark pane) (clone-mark (point pane) :left))
268 (with-open-file (stream filename :direction :input)
269 (input-from-stream stream
270 (buffer pane)
271 (offset (point pane))))
272 (psetf (offset (mark pane)) (offset (point pane))
273 (offset (point pane)) (offset (mark pane))))
274 (redisplay-frame-panes *application-frame*)))
275
276 (set-key 'com-insert-file
277 'buffer-table
278 '((#\x :control) (#\i :control)))
279
280 (define-command (com-revert-buffer :name t :command-table buffer-table) ()
281 (let* ((pane (current-window))
282 (buffer (buffer pane))
283 (filepath (filepath buffer))
284 (save (offset (point pane))))
285 (when (accept 'boolean :prompt (format nil "Revert buffer from file ~A?"
286 (filepath buffer)))
287 (cond ((directory-pathname-p filepath)
288 (display-message "~A is a directory name." filepath)
289 (beep))
290 ((probe-file filepath)
291 (erase-buffer buffer)
292 (with-open-file (stream filepath :direction :input)
293 (input-from-stream stream buffer 0))
294 (setf (offset (point pane))
295 (min (size buffer) save)))
296 (t
297 (display-message "No file ~A" filepath)
298 (beep))))))
299
300 (defun save-buffer (buffer)
301 (let ((filepath (or (filepath buffer)
302 (accept 'pathname :prompt "Save Buffer to File"))))
303 (cond
304 ((directory-pathname-p filepath)
305 (display-message "~A is a directory." filepath)
306 (beep))
307 (t
308 (when (probe-file filepath)
309 (let ((backup-name (pathname-name filepath))
310 (backup-type (concatenate 'string (pathname-type filepath) "~")))
311 (rename-file filepath (make-pathname :name backup-name
312 :type backup-type))))
313 (with-open-file (stream filepath :direction :output :if-exists :supersede)
314 (output-to-stream stream buffer 0 (size buffer)))
315 (setf (filepath buffer) filepath
316 (name buffer) (filepath-filename filepath))
317 (display-message "Wrote: ~a" (filepath buffer))
318 (setf (needs-saving buffer) nil)))))
319
320 (define-command (com-save-buffer :name t :command-table buffer-table) ()
321 (let ((buffer (buffer (current-window))))
322 (if (or (null (filepath buffer))
323 (needs-saving buffer))
324 (save-buffer buffer)
325 (display-message "No changes need to be saved from ~a" (name buffer)))))
326
327 (set-key 'com-save-buffer
328 'buffer-table
329 '((#\x :control) (#\s :control)))
330
331 (defmethod frame-exit :around ((frame climacs) #-mcclim &key)
332 (loop for buffer in (buffers frame)
333 when (and (needs-saving buffer)
334 (filepath buffer)
335 (handler-case (accept 'boolean
336 :prompt (format nil "Save buffer: ~a ?" (name buffer)))
337 (error () (progn (beep)
338 (display-message "Invalid answer")
339 (return-from frame-exit nil)))))
340 do (save-buffer buffer))
341 (when (or (notany #'(lambda (buffer) (and (needs-saving buffer) (filepath buffer)))
342 (buffers frame))
343 (handler-case (accept 'boolean :prompt "Modified buffers exist. Quit anyway?")
344 (error () (progn (beep)
345 (display-message "Invalid answer")
346 (return-from frame-exit nil)))))
347 (call-next-method)))
348
349 (define-command (com-write-buffer :name t :command-table buffer-table) ()
350 (let ((filepath (accept 'pathname :prompt "Write Buffer to File"
351 :default (directory-of-buffer (buffer (current-window)))
352 :default-type 'pathname
353 :insert-default t))
354 (buffer (buffer (current-window))))
355 (cond
356 ((directory-pathname-p filepath)
357 (display-message "~A is a directory name." filepath))
358 (t
359 (with-open-file (stream filepath :direction :output :if-exists :supersede)
360 (output-to-stream stream buffer 0 (size buffer)))
361 (setf (filepath buffer) filepath
362 (name buffer) (filepath-filename filepath)
363 (needs-saving buffer) nil)
364 (display-message "Wrote: ~a" (filepath buffer))))))
365
366 (set-key 'com-write-buffer
367 'buffer-table
368 '((#\x :control) (#\w :control)))
369

  ViewVC Help
Powered by ViewVC 1.1.5