1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs calendar classes.parser
4 classes.singleton combinators.smart continuations debugger
5 definitions io io.launcher io.pathnames kernel lexer namespaces
6 parser.notes prettyprint sequences sets source-files.errors
7 splitting strings threads tools.crossref tools.scaffold vocabs
8 vocabs.files vocabs.hierarchy vocabs.loader vocabs.metadata
14 : available-editors ( -- seq )
15 "editors" disk-child-vocab-names
16 { "editors.ui" "editors.private" } diff
17 [ vocab-platforms supported-platform? ] filter ;
19 : editor-restarts ( -- alist )
21 [ [ "Load " prepend ] keep ] { } map>assoc ;
23 : set-editor ( string -- )
25 [ "editors." prepend t parser-quiet? [ use-vocab ] with-variable ]
27 editor-class set-global ;
29 SYNTAX: EDITOR: scan-token set-editor ;
31 HOOK: editor-command editor-class ( file line -- command )
33 : write-pprint ( obj -- ) dup string? [ write ] [ pprint ] if ;
34 : print-pprint ( obj -- ) dup string? [ print ] [ pprint nl ] if ;
36 : pprint-line ( seq -- )
40 [ unclip-last [ [ write-pprint ] each ] [ print-pprint ] bi* ] if
41 ] unless-empty ; inline
44 "Select an editor" editor-restarts throw-restarts
49 "To make this editor permanent, in your "
50 ".factor-boot-rc" home-path
52 ".factor-rc" home-path
54 "USE: editors EDITOR: " _ append
55 ] output>array pprint-line
59 HOOK: editor-detached? editor-class ( -- ? )
60 M: object editor-detached? t ;
62 HOOK: editor-is-child? editor-class ( -- ? )
63 M: object editor-is-child? f ;
65 : run-and-wait-for-editor ( command -- )
68 editor-detached? >>detached
69 editor-is-child? [ +new-group+ >>group ] unless
71 300 milliseconds sleep
72 dup status>> { 0 f } member?
73 [ drop ] [ process-failed ] if ;
75 ERROR: invalid-location file line ;
77 : edit-location ( file line -- )
78 over [ invalid-location ] unless
80 editor-command [ run-and-wait-for-editor ] when* ;
82 ERROR: cannot-find-source definition ;
84 M: cannot-find-source error.
85 "Cannot find source for ``" write
86 definition>> pprint-short
89 : edit-file ( path -- )
96 : public-vocab-name ( vocab-spec -- name )
97 vocab-name ".private" ?tail drop ;
101 : edit-vocab ( vocab -- )
102 public-vocab-name >vocab-link edit ;
104 GENERIC: edit ( object -- )
107 [ where ] [ first2 edit-location ] [ cannot-find-source ] ??if ;
109 M: string edit edit-vocab ;
111 : edit-error ( error -- )
112 [ error-file ] [ error-line ] bi
113 over [ 1 or edit-location ] [ 2drop ] if ;
116 error get edit-error ;
118 : edit-each ( seq -- )
120 [ "Editing " write . ]
122 "RETURN moves on to the next usage, C+d stops." print
130 [ "Fixing " write pprint " and all usages..." print nl ]
131 [ [ smart-usage ] keep prefix ] bi
134 GENERIC: edit-docs ( object -- )
137 public-vocab-name vocab-docs-path 1 edit-location ;
140 dup "help-loc" word-prop
141 [ nip first2 edit-location ]
142 [ vocabulary>> edit-docs ]
145 GENERIC: edit-tests ( object -- )
148 public-vocab-name vocab-tests-path 1 edit-location ;
150 M: word edit-tests vocabulary>> edit-tests ;
152 : edit-platforms ( vocab -- )
153 public-vocab-name vocab-platforms-path 1 edit-location ;
155 : edit-authors ( vocab -- )
156 public-vocab-name vocab-authors-path 1 edit-location ;
158 : edit-tags ( vocab -- )
159 public-vocab-name vocab-tags-path 1 edit-location ;
161 : edit-summary ( vocab -- )
162 public-vocab-name vocab-summary-path 1 edit-location ;