cwd/cd primitives, dir./pwd library words, pipe word in JVM factor'

cvs
Slava Pestov 2004-09-04 07:06:53 +00:00
parent 80f80acb7b
commit 0acff64a5f
21 changed files with 197 additions and 36 deletions

View File

@ -6,16 +6,12 @@
- plugin should not exit jEdit on fatal errors
- IN: format base: work with all types of numbers
- wordpreview: don't show for string literals and comments
- NPE in activate()/deactivate()
- NPE in ErrorHighlight
- 64 bit support
- alist -vs- assoc terminology
- clean up listener's action popups
- jedit ==> jedit-word, jedit takes a file name
- introduce ifte* and ?str-head/?str-tail where appropriate
- cwd, cd, pwd, dir., pwd. words
- namespace clone drops static var bindings
- f usages. --> don't print all words
+ bignums:
@ -54,6 +50,8 @@
+ listener/plugin:
- NPE in activate()/deactivate()
- NPE in ErrorHighlight
- don't use jEdit's word finding API
- some way to not have previous definitions from a source file
clutter the namespace
@ -91,6 +89,9 @@
+ misc:
- write-icon kind of messy; " " should be output by the listener
- f usages. --> don't print all words
- pipe support
- telnetd: init-history
- str-reverse primitive
- some way to run httpd from command line
@ -105,7 +106,6 @@
- wiki responder:
- port to native
- text styles
- if user clicks stop in browser, doesn't stop sending?
- log with date
basic authentication, using httpdAuth function from a config file
- file responder; last-modified field

View File

@ -66,4 +66,9 @@
FactorPlugin.insertUseDialog(view,word);
</CODE>
</ACTION>
<ACTION NAME="factor-extract-word">
<CODE>
FactorPlugin.extractWord(view);
</CODE>
</ACTION>
</ACTIONS>

View 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();

View 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)
{

View 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);
} //}}}
}

View 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.

View 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 ;

View File

@ -72,6 +72,9 @@ USE: strings
] ifte
] each drop ;
: pwd cwd print ;
: dir. cwd directory. ;
[
[ "html" | "text/html" ]
[ "txt" | "text/plain" ]

View 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

View 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

View 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.

View 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 )

View File

@ -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> ;

View 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

View 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* ;

View 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

View 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 ;

View 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())));
}

View 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);

View 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)

View File

@ -1,4 +1,4 @@
extern XT primitives[];
#define PRIMITIVE_COUNT 148
#define PRIMITIVE_COUNT 150
CELL primitive_to_xt(CELL primitive);