blak3mill3r/vmd-mode

Does not show relative path images

CThuleHansen opened this issue · 4 comments

I have an image with a relative path, but it is not rendered:
![C++ Component connections](notes/hybridCosimulation-framework.png)
However, if I execute the md file using vmd manually, then it is displayed.
It is due to this line: https://github.com/blak3mill3r/vmd-mode/blob/master/vmd-mode.el#L72
Changing it to e.g. :
(setq vmd-process (start-process "vmd" "vmd" vmd-binary-path buffer-file-name))
makes it work.

Any reason for using a temp file?

Thanks for reporting this, I will try to fix it soon.

Using the file that is associated with the emacs buffer, instead of a temporary "preview" file, would not update as you edit the contents of the buffer (a feature which I personally consider important, since my eyes are on the vmd window while I type in emacs...). The vmd process does all the real work and it wants a file. Its --help output seems to suggest that it can read Markdown from STDIN but this does not appear to work.

While I am typing this I think I understood what the issue is... the temporary file probably isn't at the same path as the buffer file. I think that's what I will fix... if I cannot make that work I will add an option to pass buffer-file-name to the vmd process.

The temporary "preview file" is not in the same folder, I have checked that. I too believe that is the issue.

I have made a modification on my own - it stores a copy file with an extension .temp - e.g.: a copy of test.md becomes test.md.temp and then uses this file for vmd.
When the md-buffer is quitted, then the .temp file is deleted.
You can see the implementation below:


;; Copyright (C) 2016 Blake Miller

;; Author: Blake Miller <blak3mill3r@gmail.com>
;; Version: 0.2.0
;; Package-Version: 20161106.125
;; Keywords: markdown, preview, live, vmd
;; URL: https://github.com/blak3mill3r/vmd-mode
;; Package-Requires: ((emacs "24.3"))

;;; Commentary:

;; Realtime Markdown previews for Emacs, updates as the contents of the buffer
;; change.
;;
;; The rendered Markdown stays consistent with the markdown source in the
;; buffer, and performance is very good.

;;; Code:

(defvar-local vmd-process nil
  "Handle to the inferior vmd process")

(defvar-local vmd-preview-file nil
  "Temp file which is watched by the vmd process")

(defvar-local vmd-copy-file nil)

(defgroup vmd nil
  "Fast Github-flavored Markdown preview using a vmd subprocess."
  :prefix "vmd-"
  :group 'text)

(defcustom vmd-binary-path (executable-find "vmd")
  "Path to your vmd binary."
  :group 'vmd)


;; GitHub emojis

(defvar vmd-mode--emojis-file "./resources/emojis"
  "File containing emoji names.")

(defun vmd-mode--github-emojis ()
  "Get all GitHub emoji from the GitHub API.

The result is a list of emoji names, e.g. (\"+1\", \"-1\",
\"100\", ...).

See https://developer.github.com/v3/emojis/"
  (defvar url-http-end-of-headers)
  (let ((emoji-alist (with-current-buffer (url-retrieve-synchronously "https://api.github.com/emojis" t t)
                       (goto-char (1+ url-http-end-of-headers))
                       (json-read-object))))
    (mapcar #'symbol-name (mapcar #'car emoji-alist))))

(defun vmd-mode--update-emojis-file ()
  "Update emojis in ./resources/emojis."
  (with-temp-file vmd-mode--emojis-file
    (dolist (emoji (vmd-mode--github-emojis))
      (insert emoji)
      (insert "\n"))))

(defvar vmd-mode-github-emojis-list
  (and (file-exists-p vmd-mode--emojis-file)
       (with-temp-buffer
         (insert-file-contents vmd-mode--emojis-file)
         (split-string (buffer-string) "\n" t)))
  "Emoji for GitHub.")

(defun vmd-mode-start-vmd-process ()
  "Start an asynchronous `vmd' process to generate the `buffer-file-name' file."
  (progn
    (setq vmd-copy-file (concat buffer-file-name ".temp"))
    (copy-file buffer-file-name vmd-copy-file "overwrite")
    (setq vmd-process (start-process "vmd" "vmd" vmd-binary-path vmd-copy-file))))

(defun vmd-mode-delete-temp (&rest _args)
  (progn (message "VMD-Mode deleting file: %s" vmd-copy-file)
         (if (file-exists-p vmd-copy-file)
             (delete-file vmd-copy-file))
         (message "VMD-Mode removing hook: 'kill-buffer-hook")
         (remove-hook 'kill-buffer-hook 'vmd-mode-delete-temp t)
         (message "VMD-Mode removing hook: 'after-change-functions")
         (remove-hook 'after-change-functions 'vmd-mode-refresh t)
         (if vmd-process
             (progn 
               (delete-process vmd-process)
               (message "VMD-Mode deleted: vmd-process"))
           ))
  )

(defun vmd-mode-refresh (&rest _args)
  "Update the `vmd-preview-file'.
The optional ARGS argument is needed as this function is added to the
`after-change-functions' hook."
  (write-region (point-min) (point-max) vmd-copy-file))

;;;###autoload
(define-minor-mode vmd-mode
  "Live Markdown preview with `vmd'."
  :lighter " vmd"
  (if vmd-mode
      (if vmd-binary-path
          (progn
            (setq debug-on-error t)
            (add-hook 'after-change-functions 'vmd-mode-refresh nil t)
            (add-hook 'kill-buffer-hook 'vmd-mode-delete-temp nil t)
            (vmd-mode-start-vmd-process)
            (vmd-mode-refresh))
        (user-error "You need to have `vmd' installed in your environment PATH."))
    ))


(provide 'vmd-mode)

;;; vmd-mode.el ends here

Thanks for the fix! In the future may I suggest PRs for this sort of thing?

Definitely. I will do that next time.