- alist -vs- assoc terminology\r
- some way to run httpd from command line\r
- 'default responder' for when we go to root\r
-- file responder:\r
- - directory listings\r
- minimize stage2 initialization code, just move it to source files\r
\r
+ bignums:\r
\r
+ misc:\r
\r
+- directory listings:\r
+ - icons for file responder\r
+ - cwd, cd, pwd, dir., pwd. words\r
+- -1.1 3 ^ shouldn't give a complex number\r
- don't rehash strings on every startup\r
- 'cascading' styles\r
- jedit ==> jedit-word, jedit takes a file name\r
<include name="*.xml"/>
<include name="library/**/*.factor"/>
<include name="library/**/*.txt"/>
+ <include name="library/**/*.png"/>
<include name="org/**/*.class"/>
<include name="*.factor"/>
<include name="doc/**/*.html"/>
IN: files
DEFER: stat
-DEFER: directory
+DEFER: (directory)
IN: io-internals
DEFER: port?
setenv
open-file
stat
- read-dir
+ (directory)
garbage-collection
save-image
datastack
IN: files
USE: combinators
USE: lists
+USE: logic
USE: namespaces
USE: stack
+USE: stdio
USE: strings
: set-mime-types ( assoc -- )
: mime-type ( filename -- mime-type )
file-extension mime-types assoc [ "text/plain" ] unless* ;
+: dir-icon
+ "/library/icons/Folder.png" ;
+
+: file-icon
+ "/library/icons/File.png" ;
+
+: file-icon. ( path -- )
+ directory? dir-icon file-icon ? write-icon ;
+
+: file-link. ( dir name -- )
+ tuck "/" swap cat3 "file-link" swons unit write-attr ;
+
+: file. ( dir name -- )
+ #! If "doc-root" set, create links relative to it.
+ 2dup "/" swap cat3 file-icon. " " write file-link. terpri ;
+
+: directory. ( dir -- )
+ #! If "doc-root" set, create links relative to it.
+ dup directory [
+ dup [ "." ".." ] contains? [
+ drop
+ ] [
+ dupd file.
+ ] ifte
+ ] each drop ;
+
[
[ "html" | "text/html" ]
[ "txt" | "text/plain" ]
USE: combinators
USE: errors
USE: files
+USE: html
USE: httpd
USE: httpd-responder
USE: kernel
"raw-query" get [ CHAR: ? % % ] when*
%> redirect ;
+: list-directory ( directory -- )
+ serving-html dup [ directory. ] simple-html-document ;
+
: serve-directory ( filename -- )
- dup "/" str-tail? dup [
- drop dup "index.html" cat2 dup exists? [
+ "/" ?str-tail [
+ dup "index.html" cat2 dup exists? [
serve-file
] [
- drop
- "Foo bar" log
- drop
+ drop list-directory
] ifte
] [
- 2drop directory-no/
+ drop directory-no/
] ifte ;
: serve-object ( filename -- )
#! Wrap a string in an HTML tag.
<% dupd opening-tag swap % closing-tag %> ;
-: link-attrs ( link -- attrs )
- <% "href=\"/inspect/" % % "\"" % %> ;
-
-: link-tag ( string link -- string )
- url-encode "a" swap link-attrs html-tag ;
-
: >hex-color ( triplet -- hex )
[ >hex 2 digits ] map "#" swons cat ;
: span-tag ( string style -- string )
"span" swap <% "style=\"" % css-style% "\"" % %> html-tag ;
+: link-tag ( string link -- string )
+ url-encode "a" swap <% "href=" % unparse % %> html-tag ;
+
+: resolve-file-link ( path -- link )
+ #! The file responder needs relative links not absolute
+ #! links.
+ "doc-root" get [
+ ?str-head [ "/" ?str-head drop ] when
+ ] when* "/" ?str-tail drop ;
+
+: file-link-href ( path -- href )
+ <% "/file/" % resolve-file-link % %> ;
+
+: object-link-href ( path -- href )
+ <% "/inspect/" % % %> ;
+
: html-attr-string ( string style -- string )
- [ span-tag ] keep "link" swap assoc [ link-tag ] when* ;
+ [ span-tag ] keep
+ [
+ [ "file-link" file-link-href link-tag ]
+ [ "object-link" object-link-href link-tag ]
+ ] assoc-apply ;
: html-write-attr ( string style -- )
swap chars>entities swap html-attr-string write ;
: simple-html-document ( title quot -- )
swap [
- [ [ call ] with-html-stream ] preformatted-html
+ [ with-html-stream ] preformatted-html
] html-document ;
: trim-/ ( url -- url )
#! Trim a leading /, if there is one.
- dup "/" str-head? dup [ nip ] [ drop ] ifte ;
+ "/" ?str-head drop ;
: log-responder ( url -- )
"Calling responder " swap cat2 log ;
vars [ print ] each ;
: link-style ( path -- style )
- relative>absolute-object-path "link" default-style acons ;
+ relative>absolute-object-path
+ "object-link" default-style acons ;
: var. ( [ name | value ] -- )
uncons unparse swap link-style write-attr ;
"/library/stream.factor" run-resource ! streams
"/library/platform/jvm/stream.factor" run-resource ! streams
"/library/platform/jvm/files.factor" run-resource ! files
-"/library/files.factor" run-resource ! files
"/library/stdio.factor" run-resource ! stdio
"/library/platform/jvm/unparser.factor" run-resource ! unparser
"/library/platform/jvm/parser.factor" run-resource ! parser
"/library/stdio-binary.factor" run-resource ! stdio
"/library/vocabulary-style.factor" run-resource ! style
"/library/prettyprint.factor" run-resource ! prettyprint
+"/library/files.factor" run-resource ! files
"/library/platform/jvm/prettyprint.factor" run-resource ! prettyprint
"/library/interpreter.factor" run-resource ! interpreter
"/library/inspector.factor" run-resource ! inspector
[ "java.lang.Object" "java.lang.Object" ]
"factor.FactorLib" "equal" jinvoke-static ;
+: class-of* ( obj -- class )
+ [ ] "java.lang.Object" "getClass" jinvoke ;
+
: class-of ( obj -- class )
- [ ] "java.lang.Object" "getClass" jinvoke
- [ ] "java.lang.Class" "getName" jinvoke ;
+ class-of* [ ] "java.lang.Class" "getName" jinvoke ;
: is ( obj class -- boolean )
! Like "instanceof" in Java.
>bignum ;
: room free-memory total-memory ;
+
+: resource ( path -- url )
+ interpreter class-of*
+ [ "java.lang.String" ]
+ "java.lang.Class" "getResource" jinvoke ;
"javax.swing.text.StyleConstants" swap jvar-static-get
; inline
+: set-icon-style ( attribute-set icon -- )
+ [
+ "javax.swing.text.MutableAttributeSet"
+ "javax.swing.Icon"
+ ] "javax.swing.text.StyleConstants"
+ "setIcon" jinvoke-static ;
+
+: <icon> ( resource -- icon )
+ resource
+ [ "java.net.URL" ]
+ "javax.swing.ImageIcon" jnew ;
+
: swing-attribute+ ( attribute-set value key -- )
style-constant attribute+ ;
"factor.listener.FactorListener" "Actions" jvar-static-get
; inline
-: actions ( -- list )
+: <action-menu-item> ( path pair -- pair )
+ uncons >r " " swap cat3 r> cons ;
+
+: <actions-menu> ( path actions -- alist )
+ [ dupd <action-menu-item> ] map nip ;
+
+: object-actions ( -- list )
[
- [ "describe-path" | "Describe" ]
- [ "lookup" | "Push" ]
+ [ "describe-path" | "Describe" ]
+ [ "lookup" | "Push" ]
[ "lookup execute" | "Execute" ]
- [ "lookup jedit" | "jEdit" ]
+ [ "lookup jedit" | "jEdit" ]
[ "lookup usages." | "Usages" ]
] ;
-: <action-menu-item> ( path pair -- pair )
- uncons >r " " swap cat3 r> cons ;
+: <object-actions-menu> ( path -- alist )
+ unparse object-actions <actions-menu> ;
-: <actions-menu> ( path -- alist )
- unparse actions [ dupd <action-menu-item> ] map nip ;
+: file-actions ( -- list )
+ [
+ [ "" | "Push" ]
+ [ "run-file" | "Run file" ]
+ [ "directory." | "List directory" ]
+ ] ;
+
+: <file-actions-menu> ( path -- alist )
+ unparse file-actions <actions-menu> ;
: underline-attribute ( attribute-set -- )
t "Underline" swing-attribute+ ;
-: link-attribute ( attribute-set target -- )
+: object-link-attribute ( attribute-set target -- )
+ over underline-attribute
+ <object-actions-menu> actions-key attribute+ ;
+
+: file-link-attribute ( attribute-set target -- )
over underline-attribute
- <actions-menu> actions-key attribute+ ;
+ <file-actions-menu> actions-key attribute+ ;
: style>attribute-set ( style -- attribute-set )
<attribute-set> swap [
- [ "link" dupd link-attribute ]
- [ "bold" drop dup t "Bold" swing-attribute+ ]
- [ "italics" drop dup t "Italic" swing-attribute+ ]
- [ "underline" drop dup t "Underline" swing-attribute+ ]
- [ "fg" dupd >color "Foreground" swing-attribute+ ]
- [ "bg" dupd >color "Background" swing-attribute+ ]
- [ "font" dupd "FontFamily" swing-attribute+ ]
- [ "size" dupd "FontSize" swing-attribute+ ]
+ [ "object-link" dupd object-link-attribute ]
+ [ "file-link" dupd file-link-attribute ]
+ [ "bold" drop dup t "Bold" swing-attribute+ ]
+ [ "italics" drop dup t "Italic" swing-attribute+ ]
+ [ "underline" drop dup t "Underline" swing-attribute+ ]
+ [ "fg" dupd >color "Foreground" swing-attribute+ ]
+ [ "bg" dupd >color "Background" swing-attribute+ ]
+ [ "font" dupd "FontFamily" swing-attribute+ ]
+ [ "size" dupd "FontSize" swing-attribute+ ]
+ [ "icon" dupd <icon> set-icon-style ]
] assoc-apply ;
: set-character-attrs ( attrs -- )
IN: files
USE: combinators
+USE: io-internals
USE: lists
USE: logic
USE: stack
+USE: strings
: exists? ( file -- ? )
stat >boolean ;
: directory? ( file -- ? )
stat dup [ car ] when ;
+
+: directory ( dir -- list )
+ #! List a directory.
+ (directory) str-sort ;
: word-attrs ( word -- attrs )
dup defined? [
- dup >r word-link "link" r> word-style acons
+ dup >r word-link "object-link" r> word-style acons
] [
word-style
] ifte ;
<% "vocabularies'" % % %> ;
: vocab-attrs ( word -- attrs )
- vocab-link "link" default-style acons ;
+ vocab-link "object-link" default-style acons ;
: prettyprint-vocab ( vocab -- )
dup vocab-attrs write-attr ;
USE: combinators
USE: errors
USE: kernel
+USE: lists
USE: namespaces
USE: stack
USE: streams
#! Write an attributed string to standard output.
"stdio" get fwrite-attr ;
+: write-icon ( resource -- )
+ #! Write an icon. Eg, /library/icons/File.png
+ "icon" swons unit "" swap write-attr ;
+
: print ( string -- )
"stdio" get fprint ;
[ = ] dip f ?
] ifte ;
+: ?str-head ( str begin -- str ? )
+ dupd str-head? dup [ nip t ] [ drop f ] ifte ;
+
: str-tailcut ( str end -- str str )
str-length [ dup str-length ] dip - str/ ;
[ = ] dip f ?
] ifte ;
+: ?str-tail ( str end -- str ? )
+ dupd str-tail? dup [ nip t ] [ drop f ] ifte ;
+
: split1 ( string split -- before after )
#! The car of the pair is the string up to the first
#! occurrence of split; the cdr is the remainder of
[ [ "fg" 255 0 255 ] [ "font" | "Monospaced" ] ]
span-tag
] unit-test
+
+[ "/file/foo/bar" ]
+[
+ [
+ "/home/slava/doc/" "doc-root" set
+ "/home/slava/doc/foo/bar" file-link-href
+ ] with-scope
+] unit-test
"combinators"
"compiler"
"continuations"
- "errors"
"debugger"
+ "errors"
+ "files"
"hashtables"
"inferior"
"inspector"