find-file-other-frames

Posted by – August 6, 2009

This post is for emacs users.

Every day I start work by opening a bunch of files in emacs frames. Sometimes they’re all .java, sometimes .cc and .h. Sometimes they’re all the files in some directory. C-x 5 f this.cc, C-x 5 f that.cc, C-x 5 f the-other.cc etc. I could use wildcards, but then all the files would be loaded into the same frame. There must be a better way! The following remaps the bindings for find-file-other frame to a function called find-file-other-frames, which loads one file into the current frame and the rest (found by using wildcards) into new frames of their own. If you only want to find one file, it’s opened into a new frame as usual.

;; a find-file-other-frame that for multiple files opens a new frame for each
;; one except the first
(defun find-file-other-frames (filename &optional wildcards)
  "Edit file FILENAME, in another frame.
  Like `find-file-other-frame', but in the case of multiple files loads the
  first one into the current frame and creates new frames to each of
  the remaining ones."
  (interactive (find-file-read-args "Find file(s) in other frame(s): " nil))
  (let ((value (find-file-noselect filename nil nil wildcards)))
    (if (listp value)
      (progn
        (setq value (nreverse value))
        (cons (switch-to-buffer (car value))
          (mapcar 'switch-to-buffer-other-frame (cdr value))))
      (switch-to-buffer-other-frame value))))
 
(define-key ;; replace the keybindings
  (current-global-map) [remap find-file-other-frame] 'find-file-other-frames)

edit: a perhaps better way to do the last two lines:

(substitute-key-definition
  'find-file-other-frame 'find-file-other-frames (current-global-map))
0 Comments on find-file-other-frames

Respond | Trackback

Respond

Comments

Comments