help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Open files in a new frames from command line


From: Kevin Rodgers
Subject: Re: Open files in a new frames from command line
Date: Tue, 03 Feb 2004 10:46:20 -0700
User-agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:0.9.4.1) Gecko/20020406 Netscape6/6.2.2

Bingham, Jay wrote:
> On Monday, February 02, 2004 at 4:18 PM Kevin Rodgers wrote
>>It's a lot easier to use command-switch-alist:
>>
>>(setq command-switch-alist
>>      (cons '("--other-frame" . find-file-other-frame-command-line-arg)
>>            command-switch-alist))
>
> I agree it is a lot easier to use command-switch-alist, especially
> when I found that the original suggestion does not work in emacs 21.1
> or 20.4.
>
> However I have a question about your implementation since my reading
> of the documentation on command-switch-alist indicates that the hyphen
> is not included in the CAR of the alist.  I don't know how that plays
> with double hyphen options.

I assume you're alluding to its doc string:

| command-switch-alist's value is nil
|
| Documentation:
| Alist of command-line switches.
| Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
| HANDLER-FUNCTION receives switch name as sole arg;
| remaining command-line args are in the variable `command-line-args-left'.
|
| Defined in `startup'.

I read that to mean whatever SWITCH-STRING you want to define, whether
"-foo", "--foo", or "---foo", etc. is what should be specified in
command-switch-alist.  It does say that HANDLER-FUNCTION receives the
switch name (vs. switch string), which could be interpreted to mean
"foo" in all those cases.  But a simple test proves otherwise, that
switch name means the same thing as switch string:

(defun foo-switch-handler (switch)
  (message "%s" switch))

(setq command-switch-alist
      (cons '("--foo" . foo-switch-handler) command-switch-alist))

The number of hyphens is irrelevant -- it can even be zero!  I just try
to follow the GNU convention that multicharacter (long) options should
be preceded by 2 hyphens.

>>(defun find-file-other-frame-command-line-arg (switch)
>>   "Visit next command line argument (after SWITCH) in a new frame."
>>   ;; (prog1 (car x) (setq x (cdr x))) == (pop x):
>>   (find-file-other-frame (prog1 (car command-line-args-left)
>> (setq command-line-args-left >> (cdr command-line-args-left)))))
>
> If you are looking for easy, why would you want to type that big long
> option before every file name?

Because I was looking for an easier implementation, and I think clarity
and readability are more important for ease of use.  If you want to
minimize typing at the command line, use a shell alias or function.

> Here is what I did to Oliver's function to make it work with only one
> option that affects all the remaining file names on the command line.
>
> (setq
>  command-switch-alist
>  (append
>   '(("fpf" . jcb-frame-per-file))))
>
>  (defun jcb-frame-per-file (arg)
>   "Open each file on the command line in a new frame.
> This function is triggered by the '-fpf' command line option.
> Each file on the command line that follows it is opened in its own
> frame."
>   (while (and command-line-args-left
>              (not ((string-match "^--?" str)
>                    (car command-line-args-left))))

That doesn't look syntactically correct.

>     (find-file-other-frame
>      (pop command-line-args-left) t)
>     t))

Since command line switches don't necessarily begin with a hyphen, you
might want to test each remaining arg like this:

        (and command-line-args-left
             (not (string-match "\\`-" (car command-line-args-left)))
             (not (assoc (car command-line-args-left) command-switch-alist)))

>>Invoke as emacs --other-frame FILE ...
>
> Invoke as:
>  emacs first-file -fpf second-file ...
>
> if the -fpf option is before the first file name then n+1 frames are
> opened and *scratch* is in the first frame.

Very nice.  Here's another way, but it's not quite as nice since it also
pops up a *Buffer List* window:

emacs --eval '(setq pop-up-frames t)' FILE ... \
      --eval '(setq pop-up-frames nil)'

That works best with something like this:

(add-hook 'find-file-hooks
          (lambda ()
            (set-window-dedicated-p (selected-window) t))
(add-hook 'find-file-not-found-hooks
          (lambda ()
            (set-window-dedicated-p (selected-window) t)
            nil)

--
Kevin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]