1

im trying to write a function that will be used in dired where i would launch it on the file at point and it will convert it using pandoc. i have the following code chunk (warning: my coding skills are basiclly zero :))

 (defun  z/dired-shell-md2org ()
 ""
 (interactive)
 (shell-command (concat "pandoc -o OUTPUT.org" (dired-file-name-at-point))))

the original pandoc command i use succesfuly is this

pandoc -o EDA.org EDA.md 

can anyone guide me on 2 things:

  1. how does one get the current file variable (i think its dired-file-name-at-point) to be the last word

  2. is there a way that instead of OUTPUT.org it will use the same filename (sans extension) of original file at point?

Basically i want the converted orf file to have the same name as the original md file

  • 1
    You should insert a space at the end of the string "pandoc -o OUTPUT.org" to separate the file names in the shell command. If you use shell-command you should also use quotes around file names, i.e. "\"...\"". Furthermore, file-name-sans-extension is your friend and look up the format function in the emacs-lisp help. – Tobias Jul 22 '16 at 11:32
1

Sounds like you want something like (format "pandoc -o %s.org" (file-name-base (dired-get-filename))) instead of (concat...). You can use dired-file-name-at-point if you want to, but that requires point to actually be on the file name, and not just somewhere on the same line. E.g. (untested):

(defun  z/dired-shell-md2org ()
 "..."
 (interactive)
 (let ((base  (file-name-base (dired-get-filename))))
   (shell-command (format "pandoc -o %s.org %.md" base base))))

Or this (to automatically quote file names for shell etc.):

(require 'dired-aux)

(defun  z/dired-shell-md2org ()
  "..."
  (interactive)
  (let* ((file  (dired-get-filename))
         (base  (file-name-base file)))
    (unless (string= ".md" (file-name-extension file :PERIOD))
      (error "File is not an MD file"))
    (dired-run-shell-command
      (dired-shell-stuff-it "pandoc -o "
                            (list (format " %s.org" base) (format " %s.md" base))
                            nil))))
  • thx drew! when i run this while point is on a md file (index.md) i get this error: file-name-base: Symbol's function definition is void: dired-get-file-name – zeltak Jul 22 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.