[plt-scheme] obtaining list of opened files in DrScheme

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Wed Oct 11 15:18:17 EDT 2006

> But if you are trying to capture a list of all open files for a particular 
> DrScheme that's running, I'm not so sure yet; I'm going to have to think 
> about that one some more.


Hi Pedro,

Ok, here's that example I promised you.  It's not just a keymapping, but a 
full-fledged tool.  The way it works is by providing DrScheme with a new 
"editor" mixin.  An editor is what's responsible for working with source 
files, and DrScheme instantiates a new editor instance with each new 
tab/window.  For more information on editors, see:

http://download.plt-scheme.org/doc/352/html/framework/framework-Z-H-62.html#node_chap_7

If we keep track of all instances, somewhere, then we can later query them 
all for their respective filenames.  When we instantiate our tool, we 
introduce our own editor mixin by using 
drscheme:unit:add-to-program-editor-mixin:

http://download.plt-scheme.org/doc/352/html/tools/tools-Z-H-291.html#node_tag_Temp_292


I have things set up so that, every five seconds, the code prints out the 
list of opened files to standard output.  I know this is a silly thing to 
do, but oh well... *grin* Just to hammer the point in: this code is not 
production quality.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module tool mzscheme
   ;; This is sample code that shows how to write a tool
   ;; that lists all open files.

   (require (lib "unitsig.ss")
            (lib "class.ss")
            (lib "tool.ss" "drscheme")
            (lib "list.ss"))

   (provide tool@)

   (define all-open-editors empty)

   (define (editor-mixin super%)
     (class super%
       (super-new)

       (set! all-open-editors
             (cons this all-open-editors))

       (define/augment (on-close)
         (set! all-open-editors
               (remove this all-open-editors eq?))
         (inner (void) on-close))))


   (define (get-list-of-opened-files)
     (filter
      (lambda (x) x)
      (map (lambda (editor)
             (send editor get-filename))
           all-open-editors)))

   ;; This code is here just to show that we are really tracking
   ;; files:
   (thread
    (lambda ()
      (let loop ()
        (printf "TESTING: ~a~n" (get-list-of-opened-files))
        (sleep 5)
        (loop))))

   (define tool@
     (unit/sig drscheme:tool-exports^
       (import drscheme:tool^)
       (define (phase1)
         (drscheme:unit:add-to-program-editor-mixin
          editor-mixin))
       (define (phase2) (void)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



If you want to run this, create a new collection called 'find-open-files' 
with this module, along with an info.ss:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module info (lib "infotab.ss" "setup")
   (define name "Find Open Files example")
   ;; This is the file which is loaded on the module start.
   (define tools '(("tool.ss")))
   ;; The icon of the project.
   (define tool-names (list "find-open-files"))
   ;; the url of the plugin.
   (define tool-urls (list #f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Once you have these two modules in the collection's subdirectory, run 
'setup-plt -l find-open-files'.  The next DrScheme you open up should 
annoyingly print out its opened files every few seconds.  *grin*


Hope this helps!


Posted on the users mailing list.