]> gitweb.factorcode.org Git - factor.git/commitdiff
cwd/cd primitives, dir./pwd library words, pipe word in JVM factor'
authorSlava Pestov <slava@factorcode.org>
Sat, 4 Sep 2004 07:06:53 +0000 (07:06 +0000)
committerSlava Pestov <slava@factorcode.org>
Sat, 4 Sep 2004 07:06:53 +0000 (07:06 +0000)
21 files changed:
TODO.FACTOR.txt
actions.xml
factor/FactorLib.java
factor/FactorReader.java
factor/jedit/FactorPlugin.java
factor/jedit/FactorPlugin.props
library/cross-compiler.factor
library/files.factor
library/platform/jvm/boot-sumo.factor
library/platform/jvm/files.factor
library/platform/jvm/kernel.factor
library/platform/jvm/listener.factor
library/platform/jvm/processes.factor [new file with mode: 0644]
library/platform/jvm/stream.factor
library/platform/native/init-stage2.factor
library/platform/native/primitives.factor
library/stdio.factor
native/file.c
native/file.h
native/primitives.c
native/primitives.h

index a4c6d51fdcd4692e293b0020c08c657104ef5d6f..152ae91817a95516d5bb4b402babf86f03368a45 100644 (file)
@@ -6,16 +6,12 @@
 - plugin should not exit jEdit on fatal errors\r
 - IN: format base: work with all types of numbers\r
 - wordpreview: don't show for string literals and comments\r
-- NPE in activate()/deactivate()\r
-- NPE in ErrorHighlight\r
 - 64 bit support\r
 - alist -vs- assoc terminology\r
 - clean up listener's action popups\r
 - jedit ==> jedit-word, jedit takes a file name\r
 - introduce ifte* and ?str-head/?str-tail where appropriate\r
-- cwd, cd, pwd, dir., pwd. words\r
 - namespace clone drops static var bindings\r
-- f usages. --> don't print all words\r
 \r
 + bignums:\r
 \r
@@ -54,6 +50,8 @@
 \r
 + listener/plugin:\r
 \r
+- NPE in activate()/deactivate()\r
+- NPE in ErrorHighlight\r
 - don't use jEdit's word finding API\r
 - some way to not have previous definitions from a source file\r
   clutter the namespace\r
@@ -91,6 +89,9 @@
 \r
 + misc:\r
 \r
+- write-icon kind of messy; " " should be output by the listener\r
+- f usages. --> don't print all words\r
+- pipe support\r
 - telnetd: init-history\r
 - str-reverse primitive\r
 - some way to run httpd from command line\r
 - wiki responder:\r
   - port to native\r
   - text styles\r
-- if user clicks stop in browser, doesn't stop sending?\r
 - log with date\r
  basic authentication, using httpdAuth function from a config file\r
 - file responder; last-modified field\r
index d2b8c2226cafe7027423fc6733c64bdea4b31c54..81e24360639a1ed8c5a98677a32cef1dec4b009d 100644 (file)
@@ -66,4 +66,9 @@
                                FactorPlugin.insertUseDialog(view,word);
                </CODE>
        </ACTION>
+       <ACTION NAME="factor-extract-word">
+               <CODE>
+                       FactorPlugin.extractWord(view);
+               </CODE>
+       </ACTION>
 </ACTIONS>
index 5910650eefce5255c76e8d1d4a6cc1a2f93c917c..03ae9fe3c81f2e002f373c716454e088d32c2512 100644 (file)
@@ -128,13 +128,14 @@ public class FactorLib
        } //}}}
 
        //{{{ exec() method
-       public static int exec(String[] args) throws Exception
+       public static int exec(String[] args, String dir) throws Exception
        {
                int exitCode = -1;
 
                try
                {
-                       Process process = Runtime.getRuntime().exec(args);
+                       Process process = Runtime.getRuntime().exec(args,
+                               null,new File(dir));
                        process.getInputStream().close();
                        process.getOutputStream().close();
                        process.getErrorStream().close();
index 95485de782fb070a5e850fd111d30b66b2b97fc0..b82b46791a6afa76951bcedfc297bc1407395ad9 100644 (file)
@@ -360,6 +360,10 @@ public class FactorReader
         */
        public FactorWord nextWord(boolean define) throws Exception
        {
+               // remember the position before the word name
+               int line = scanner.getLineNumber();
+               int col = scanner.getColumnNumber();
+
                Object next = nextNonEOL(true,false);
                if(next instanceof Number)
                {
@@ -369,10 +373,6 @@ public class FactorReader
                }
                else if(next instanceof String)
                {
-                       // remember the position before the word name
-                       int line = scanner.getLineNumber();
-                       int col = scanner.getColumnNumber();
-
                        FactorWord w = intern((String)next,define);
                        if(define && w != null)
                        {
index 8b3bdd724a8bfe28c3e0b51cc4d7821444f7c4fb..656d551760eaf200feef86af0269facea965afcc 100644 (file)
@@ -307,4 +307,49 @@ public class FactorPlugin extends EditPlugin
                buffer.insert(lastUseOffset,decl);
                showStatus(view,"inserted-use",decl);
        } //}}}
+
+       //{{{ extractWord() method
+       public static void extractWord(View view)
+       {
+               JEditTextArea textArea = view.getTextArea();
+               Buffer buffer = textArea.getBuffer();
+               String selection = textArea.getSelectedText();
+               if(selection == null)
+                       selection = "";
+
+               SideKickParsedData data = SideKickParsedData
+                       .getParsedData(view);
+               if(!(data instanceof FactorParsedData))
+               {
+                       view.getToolkit().beep();
+                       return;
+               }
+
+               Asset asset = data.getAssetAtPosition(
+                       textArea.getCaretPosition());
+
+               if(asset == null)
+               {
+                       GUIUtilities.error(view,"factor.extract-word-where",null);
+                       return;
+               }
+
+               String newWord = GUIUtilities.input(view,
+                       "factor.extract-word",null);
+               if(newWord == null)
+                       return;
+
+               int start = asset.start.getOffset();
+
+               String indent = MiscUtilities.createWhiteSpace(
+                       buffer.getIndentSize(),
+                       (buffer.getBooleanProperty("noTabs") ? 0
+                       : buffer.getTabSize()));
+
+               String newDef = ": " + newWord + "\n" + indent
+                       + selection.trim() + " ;\n\n" ;
+
+               buffer.insert(start,newDef);
+               textArea.setSelectedText(newWord);
+       } //}}}
 }
index 8a82429da12cf720b7424f976892a65c4ad70d21..98813efe0d4c18d1c1106b9ab76117af2a94ea42 100644 (file)
@@ -23,6 +23,8 @@ plugin.factor.jedit.FactorPlugin.menu=factor \
        factor-edit-dialog \
        factor-see \
        factor-usages \
+       - \
+       factor-extract-word
 
 factor.label=Factor Listener
 factor-run-file.label=Run current file
@@ -33,6 +35,7 @@ factor-see.label=See word at caret
 factor-edit.label=Edit word at caret
 factor-edit-dialog.label=Edit word...
 factor-usages.label=Word usages at caret
+factor-extract-word.label=Extract word...
 
 factor.title=Factor
 
@@ -54,3 +57,9 @@ factor.insert-use.caption=There are multiple words named "{0}". Select the one t
 
 factor.edit-word.title=Edit Word Definition
 factor.edit-word.caption=Word name:
+
+factor.extract-word.title=Extract Word
+factor.extract-word.message=Enter name of new word:
+
+factor.extract-word-where.title=Cannot Extract Here
+factor.extract-word-where.message=This command can only be used inside a word definition.
index e90314eee58bff17e28212a256e69577f0a6eb62..e9ded25636625f5d90d4f807376565c90c9c67fe 100644 (file)
@@ -60,6 +60,8 @@ DEFER: sbuf-clone
 IN: files
 DEFER: stat
 DEFER: (directory)
+DEFER: cwd
+DEFER: cd
 
 IN: io-internals
 DEFER: port?
@@ -264,6 +266,8 @@ IN: cross-compiler
         allot-count
         set-allot-count
         dump
+        cwd
+        cd
     ] [
         swap succ tuck primitive,
     ] each drop ;
index 3560bcee3c4196cccf9ba234113cab140d2a8648..6ef3f554bbf098fa3a5d07a81c7ec85cca675b02 100644 (file)
@@ -72,6 +72,9 @@ USE: strings
         ] ifte
     ] each drop ;
 
+: pwd cwd print ;
+: dir. cwd directory. ;
+
 [
     [ "html"   | "text/html"                        ]
     [ "txt"    | "text/plain"                       ]
index b49932627d555212265a85e2789dddda45eb6b48..946029d75fcb28ddb2a741a62bfc29a0f497d657 100644 (file)
@@ -87,6 +87,7 @@ USE: parser
 "/library/math/simpson.factor"           run-resource ! math
 
 !!! Development tools.
+"/library/platform/jvm/processes.factor"   run-resource ! processes
 "/library/extend-stream.factor"            run-resource ! streams
 "/library/stdio-binary.factor"             run-resource ! stdio
 "/library/vocabulary-style.factor"         run-resource ! style
index 78a5f6a6bbc72797e1329f18588fda185fc0890b..f80830659d456f1ab070a4bab820c718e66c65dd 100644 (file)
@@ -32,6 +32,7 @@ USE: lists
 USE: logic
 USE: stack
 USE: strings
+USE: namespaces
 
 : <file> ( path -- file )
     dup "java.io.File" is not [
@@ -60,3 +61,11 @@ USE: strings
 
 : file-length ( file -- size )
     <file> [ ] "java.io.File" "length" jinvoke ;
+
+: cwd ( -- dir )
+    global [ "cwd" get ] bind ;
+
+: cd ( dir --)
+    global [ "cwd" set ] bind ;
+
+global [ "user.dir" system-property "cwd" set ] bind
index b1b462c1b92a5e2297808598f8b4c9877a618a69..da1bd803f43d32f7af9c2166b3406ec3fc55ccd9 100644 (file)
@@ -74,10 +74,6 @@ IN: kernel
     interpreter
     [ ] "factor.FactorInterpreter" "topLevel" jinvoke ;
 
-: exec ( args -- exitCode )
-    [ [ "java.lang.String" ] ] "factor.FactorLib" "exec"
-    jinvoke-static ;
-
 : exit* ( code -- )
     [ "int" ] "java.lang.System" "exit" jinvoke-static ;
 
@@ -103,8 +99,8 @@ IN: kernel
 : version "factor.FactorInterpreter" "VERSION" jvar-static-get ;
 
 : jvm-runtime ( -- java.lang.Runtime )
-  #! Return the java.lang.Runtime object for the JVM
-  f "java.lang.Runtime" "getRuntime" jinvoke-static ;
+    #! Return the java.lang.Runtime object for the JVM
+    f "java.lang.Runtime" "getRuntime" jinvoke-static ;
 
 : free-memory ( -- int )
   #! Return the free memory in the JVM.
index 0df831f1f2e504468c8dc9fa5a7864d054dfab8e..004cf6d562b0b5b356d83f24ffea3f955369c1c4 100644 (file)
@@ -100,9 +100,10 @@ USE: unparser
 
 : file-actions ( -- list )
     [
-        [ ""               | "Push" ]
-        [ "run-file"       | "Run file" ]
-        [ "directory."     | "List directory" ]
+        [ ""           | "Push" ]
+        [ "run-file"   | "Run file" ]
+        [ "directory." | "List directory" ]
+        [ "cd"         | "Change directory" ]
     ] ;
 
 : <file-actions-menu> ( path -- alist )
diff --git a/library/platform/jvm/processes.factor b/library/platform/jvm/processes.factor
new file mode 100644 (file)
index 0000000..52cbc4d
--- /dev/null
@@ -0,0 +1,58 @@
+! :folding=indent:collapseFolds=1:
+
+! $Id$
+!
+! Copyright (C) 2004 Slava Pestov.
+! 
+! Redistribution and use in source and binary forms, with or without
+! modification, are permitted provided that the following conditions are met:
+! 
+! 1. Redistributions of source code must retain the above copyright notice,
+!    this list of conditions and the following disclaimer.
+! 
+! 2. Redistributions in binary form must reproduce the above copyright notice,
+!    this list of conditions and the following disclaimer in the documentation
+!    and/or other materials provided with the distribution.
+! 
+! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+IN: processes
+USE: files
+USE: kernel
+USE: streams
+USE: stack
+
+: exec ( args -- exitCode )
+    cwd
+    [
+        [ "java.lang.String" ] "java.lang.String"
+    ] "factor.FactorLib" "exec"
+    jinvoke-static ;
+
+: (pipe) ( args -- process )
+    f cwd <file> jvm-runtime
+    [
+        [ "java.lang.String" ]
+        [ "java.lang.String" ]
+        "java.io.File"
+    ] "java.lang.Runtime" "exec" jinvoke ;
+
+: close-stderr ( process -- )
+    [ ] "java.lang.Process" "getErrorStream" jinvoke close ;
+
+: pipe ( args -- stream )
+    #! Start a process, and return a stream for communicating
+    #! with it.
+    (pipe) dup close-stderr
+    dup [ ] "java.lang.Process" "getInputStream" jinvoke
+    swap [ ] "java.lang.Process" "getOutputStream" jinvoke
+    <byte-stream> ;
index 14682586b292d0f1dc4505a9cf30ba6d1889c4ed..89063534e67ea5ef5f70be522fd239b8827c1863 100644 (file)
@@ -35,6 +35,22 @@ USE: namespaces
 USE: stack
 USE: strings
 
+: close ( stream -- )
+    [
+        [ "java.io.InputStream" is ] [
+            [ ] "java.io.InputStream" "close" jinvoke
+        ]
+        [ "java.io.OutputStream" is ] [
+            [ ] "java.io.OutputStream" "close" jinvoke
+        ]
+        [ "java.io.Reader" is ] [
+            [ ] "java.io.Reader" "close" jinvoke
+        ]
+        [ "java.io.Writer" is ] [
+            [ ] "java.io.Writer" "close" jinvoke
+        ]
+    ] cond ;
+
 : fcopy ( from to -- )
     #! Copy the contents of the byte-stream 'from' to the
     #! byte-stream 'to'.
@@ -81,8 +97,8 @@ USE: strings
     "out" get [ ] "java.io.OutputStream" "flush" jinvoke ;
 
 : <byte-stream>/fclose ( -- )
-    "in" get  [ [ ] "java.io.InputStream"  "close" jinvoke ] when* 
-    "out" get [ [ ] "java.io.OutputStream" "close" jinvoke ] when* ;
+    "in" get  [ close ] when* 
+    "out" get [ close ] when* ;
 
 : <bin> ( in -- in )
     [ "java.io.InputStream" ] "java.io.BufferedInputStream" jnew ;
@@ -134,8 +150,8 @@ USE: strings
     "out" get [ ] "java.io.Writer" "flush" jinvoke ;
 
 : <char-stream>/fclose ( -- )
-    "in" get  [ [ ] "java.io.Reader" "close" jinvoke ] when* 
-    "out" get [ [ ] "java.io.Writer" "close" jinvoke ] when* ;
+    "in" get  [ close ] when* 
+    "out" get [ close ] when* ;
 
 : <char-stream> ( in out -- stream )
     #! Creates a new stream for reading from the
@@ -190,13 +206,6 @@ USE: strings
 : <sreader> ( string -- reader )
     [ "java.lang.String" ] "java.io.StringReader" jnew ;
 
-: close ( stream -- )
-    dup "java.io.Reader" is [
-        [ ] "java.io.Reader" "close" jinvoke
-    ] [
-        [ ] "java.io.Writer" "close" jinvoke
-    ] ifte ;
-
 : <server> ( port -- stream )
     #! Starts listening on localhost:port. Returns a stream that
     #! you can close with fclose, and accept connections from
index 404db6990472ec405bc3b1637233138fec87eb4a..4c9c08f9948bdef8c2a802c49476602b7d72f909 100644 (file)
@@ -53,18 +53,19 @@ USE: words
 
     init-error-handler
     init-random
-    "stdio" get <ansi-stream> "stdio" set
 
     ! Some flags are *on* by default, unless user specifies
     ! -no-<flag> CLI switch
     t "user-init" set
     t "interactive" set
+    t "ansi" set
 
     ! The first CLI arg is the image name.
     cli-args uncons parse-command-line "image" set
-    
+
     run-user-init
 
+    "ansi" get [ "stdio" get <ansi-stream> "stdio" set ] when
     "interactive" get [ init-interpreter ] when
 
     0 exit* ;
index fe73a16d8da441be640718e1f106c4efd88fd8c1..ddec07aa65bbd4f768e325130c80f4ff205efd3a 100644 (file)
@@ -187,6 +187,8 @@ USE: unparser
     [ allot-count            | " word -- n " ]
     [ set-allot-count        | " n word -- n " ]
     [ dump                   | " obj -- " ]
+    [ cwd                    | " -- dir " ]
+    [ cd                     | " dir -- " ]
 ] [
     unswons "stack-effect" swap set-word-property
 ] each
index 8b358a60adf041377f0abe2c7d451ae07bf21bec..ec09b393c6ec21d9b841f095a7be8d5b6cb20bba 100644 (file)
@@ -69,7 +69,7 @@ USE: streams
 
 : write-icon ( resource -- )
     #! Write an icon. Eg, /library/icons/File.png
-    "icon" swons unit "" swap write-attr ;
+    "icon" swons unit " " swap write-attr ;
 
 : print ( string -- )
     "stdio" get fprint ;
index ae297bf7d843aca1dbc064f032b4d91ad953cbc6..cb88d026f1f20a8dd199fe2b42fe8396cba738e6 100644 (file)
@@ -69,3 +69,16 @@ void primitive_read_dir(void)
 
        dpush(result);
 }
+
+void primitive_cwd(void)
+{
+       char wd[MAXPATHLEN];
+       if(getcwd(wd,MAXPATHLEN) < 0)
+               io_error(__FUNCTION__);
+       dpush(tag_object(from_c_string(wd)));
+}
+
+void primitive_cd(void)
+{
+       chdir(to_c_string(untag_string(dpop())));
+}
index 5532601410322f29f35c1f6ef5d199a9b4e0efd7..978c0627465eaa11ae6672ef3303c9f4591dda79 100644 (file)
@@ -3,3 +3,5 @@
 void primitive_open_file(void);
 void primitive_stat(void);
 void primitive_read_dir(void);
+void primitive_cwd(void);
+void primitive_cd(void);
index b2607d68f1c0aa256b8ce2cdeeb99528a61688f5..2efb548361ebbd1f8714afb8736e31f913ff95fd 100644 (file)
@@ -148,7 +148,9 @@ XT primitives[] = {
        primitive_allot_profiling,
        primitive_word_allot_count,
        primitive_set_word_allot_count,
-       primitive_dump
+       primitive_dump,
+       primitive_cwd,
+       primitive_cd
 };
 
 CELL primitive_to_xt(CELL primitive)
index 95ef45ac2f83315721cff7212fecd8757cfcfafb..63e733c766cc0d978ccc020ac20c41efe5b953f9 100644 (file)
@@ -1,4 +1,4 @@
 extern XT primitives[];
-#define PRIMITIVE_COUNT 148
+#define PRIMITIVE_COUNT 150
 
 CELL primitive_to_xt(CELL primitive);