unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to pass a evaluated list as argument of macro?
@ 2020-07-06 13:02 stardiviner
  2020-07-06 13:39 ` Noam Postavsky
  0 siblings, 1 reply; 4+ messages in thread
From: stardiviner @ 2020-07-06 13:02 UTC (permalink / raw)
  To: Emacs Help


I have a prototype of bellowing elisp code. I try to write a macro for construct
ffmpeg commands. But found the passed in arglist is not evaluated or literal
"arglist" in some ways. I marked the problem in source code. Can somebody help
me on this problem? Thanks in advance. :)

#+begin_src emacs-lisp
(defmacro ffmpeg--command-macro (name arglist &rest body)
  "Construct ffmpeg command with ARGS and BODY."
  ;; (declare (debug t))
  `(defun ,(intern (format "ffmpeg-%s" name)) ()
     (interactive)
     (make-process
      :name "ffmpeg"
      ;; FIXME `arglist' is not evaluated
      :command ,(apply 'append '("ffmpeg") (eval arglist)) ; <------------- problem here
      :buffer "*ffmpeg*"
      :sentinel (lambda (_ __)
                  (message "FFmpeg process finished.")))
     ,@body))

;;; NOTE Because ffmpeg command option "-t" accept seconds like 57 as value.
(defun ffmpeg--subtract-timestamps (start-timestamp end-timestamp)
  "Subtract END-TIMESTAMP with START-TIMESTAMP."
  (time-subtract
   (encode-time (parse-time-string
                 (concat "2020-01-01T" end-timestamp)))
   (encode-time (parse-time-string
                 (concat "2020-01-01T" start-timestamp)))))

;; (ffmpeg--subtract-timestamps "00:11:25" "00:12:12")

(defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
  "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME."
  (interactive (list
                (read-file-name "FFmpeg input filename: ")
                (read-string "FFmpeg start timestamp: ")
                (read-string "FFmpeg end timestamp: ")
                (read-file-name "FFmpeg output filename: ")))
  ;; "ffmpeg -i input-filename -ss start-timestamp -t time-timestamp -codec copy output-filename"
  (let ((arglist `("-i" ,input-filename
                   "-ss" ,start-timestamp
                   "-t" ,(ffmpeg--subtract-timestamps start-timestamp end-timestamp)
                   "-codec" "copy"
                   ,output-filename)))
    (ffmpeg--command-macro "cut-clip" arglist)))
#+end_src

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to pass a evaluated list as argument of macro?
  2020-07-06 13:02 How to pass a evaluated list as argument of macro? stardiviner
@ 2020-07-06 13:39 ` Noam Postavsky
  2020-07-06 14:46   ` stardiviner
  0 siblings, 1 reply; 4+ messages in thread
From: Noam Postavsky @ 2020-07-06 13:39 UTC (permalink / raw)
  To: stardiviner; +Cc: Emacs Help

On Mon, 6 Jul 2020 at 09:02, stardiviner <numbchild@gmail.com> wrote:

> (defmacro ffmpeg--command-macro (name arglist &rest body)
>   "Construct ffmpeg command with ARGS and BODY."
>   ;; (declare (debug t))
>   `(defun ,(intern (format "ffmpeg-%s" name)) ()

> (defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
>   "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME."

>     (ffmpeg--command-macro "cut-clip" arglist)))

The way you are using your macro doesn't make sense to me. Do you want
ffmpeg-cut-clip to define a command which runs ffmpeg (in which case,
maybe it should be a macro), or actually just run the ffmpeg command
(in which case, maybe ffmpeg--comand-macro should not be a macro, but
rather a function (and probably called something like
ffmpeg-run-command instead))?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to pass a evaluated list as argument of macro?
  2020-07-06 13:39 ` Noam Postavsky
@ 2020-07-06 14:46   ` stardiviner
  2020-07-06 15:23     ` Noam Postavsky
  0 siblings, 1 reply; 4+ messages in thread
From: stardiviner @ 2020-07-06 14:46 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: Emacs Help


Noam Postavsky <npostavs@gmail.com> writes:

> On Mon, 6 Jul 2020 at 09:02, stardiviner <numbchild@gmail.com> wrote:
>
>> (defmacro ffmpeg--command-macro (name arglist &rest body)
>>   "Construct ffmpeg command with ARGS and BODY."
>>   ;; (declare (debug t))
>>   `(defun ,(intern (format "ffmpeg-%s" name)) ()
>
>> (defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
>>   "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME."
>
>>     (ffmpeg--command-macro "cut-clip" arglist)))
>
> The way you are using your macro doesn't make sense to me. Do you want
> ffmpeg-cut-clip to define a command which runs ffmpeg (in which case,
> maybe it should be a macro), or actually just run the ffmpeg command
> (in which case, maybe ffmpeg--comand-macro should not be a macro, but
> rather a function (and probably called something like
> ffmpeg-run-command instead))?

You're right, recursive (interactive) command definition is nonsense.

I'm trying to abstract out the "make-process" out.

I modified source code to bellowing:

#+begin_src emacs-lisp
(defmacro ffmpeg--run-command (arglist &rest body)
  "Construct ffmpeg command with ARGLIST and BODY."
  (declare (debug t))
  `(make-process
    :name "ffmpeg"
    ;; FIXME `arglist' is not evaluated
    :command ,(apply 'append '("ffmpeg") (eval arglist)) ; <------------- problem here
    :buffer "*ffmpeg*"
    :sentinel (lambda (_ __)
                (message "FFmpeg process finished.")))
  ,@body)

;;; NOTE Because ffmpeg command option "-t" accept seconds like 57 as value.
(defun ffmpeg--subtract-timestamps (start-timestamp end-timestamp)
  "Subtract END-TIMESTAMP with START-TIMESTAMP."
  (time-subtract
   (encode-time (parse-time-string
                 (concat "2020-01-01T" end-timestamp)))
   (encode-time (parse-time-string
                 (concat "2020-01-01T" start-timestamp)))))

;; (ffmpeg--subtract-timestamps "00:11:25" "00:12:12")

(defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
  "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME."
  (interactive (list
                (read-file-name "FFmpeg input filename: ")
                (read-string "FFmpeg start timestamp: ")
                (read-string "FFmpeg end timestamp: ")
                (read-file-name "FFmpeg output filename: ")))
  ;; "ffmpeg -i input-filename -ss start-timestamp -t time-timestamp -codec copy output-filename"
  (let ((arglist `("-i" ,input-filename
                   "-ss" ,start-timestamp
                   "-t" ,(ffmpeg--subtract-timestamps start-timestamp end-timestamp)
                   "-codec" "copy"
                   ,output-filename)))
    (ffmpeg--run-command arglist))) ; <----- error void arglist
#+end_src

In upper "error void arglist" position, If I use:

#+begin_src emacs-lisp
(ffmpeg--run-command '("-i" ,input-filename
                         "-ss" ,start-timestamp
                         "-t" ,(ffmpeg--subtract-timestamps start-timestamp end-timestamp)
                         "-codec" "copy"
                         ,output-filename))
#+end_src

Still wrong. I wander why, can you give some simple explanation hints? Thanks :)

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to pass a evaluated list as argument of macro?
  2020-07-06 14:46   ` stardiviner
@ 2020-07-06 15:23     ` Noam Postavsky
  0 siblings, 0 replies; 4+ messages in thread
From: Noam Postavsky @ 2020-07-06 15:23 UTC (permalink / raw)
  To: stardiviner; +Cc: Emacs Help

On Mon, 6 Jul 2020 at 10:46, stardiviner <numbchild@gmail.com> wrote:

> I'm trying to abstract out the "make-process" out.

> I modified source code to bellowing:
>
> #+begin_src emacs-lisp
> (defmacro ffmpeg--run-command (arglist &rest body)
>   "Construct ffmpeg command with ARGLIST and BODY."

What's the BODY argument supposed to do? Why not drop it and change to
defun instead?

Otherwise, you could do

:command ,(append '("ffmpeg") arglist)

but I don't think you need a macro here.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-06 15:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-06 13:02 How to pass a evaluated list as argument of macro? stardiviner
2020-07-06 13:39 ` Noam Postavsky
2020-07-06 14:46   ` stardiviner
2020-07-06 15:23     ` Noam Postavsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).