Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inline image feature #128

Merged
merged 2 commits into from
Apr 4, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6526,6 +6526,46 @@ BEG and END are the limits of scanned region."
(progn (goto-char beg) (beginning-of-line) (point))
(progn (goto-char end) (forward-line 1) (point))))))


;;; Display inline image =================================================

(defvar markdown-inline-image-overlays nil)
(make-variable-buffer-local 'markdown-inline-image-overlays)

(defun markdown-remove-inline-images ()
(interactive)
(mapc 'delete-overlay markdown-inline-image-overlays)
(setq markdown-inline-image-overlays nil))

(defun markdown-display-inline-images ()
(interactive)
(unless (display-graphic-p)
(error "Cannot show images."))
(save-excursion
(save-restriction
(widen)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to widen here?

My question's context: I'm thinking for my use-case I'll probably narrow to region of the day I'm writing in, and only want the pictures toggles for that day (toggling them for all days could make things slow, particularly because I intend to hook into this code & use ImageMagik to make the photos no wider than the buffer size).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took most of code from org-mode and org inline image commands uses widen. Commands which I add works same as org-mode commands(org-toggle-inline-images etc).

(goto-char (point-min))
(while (re-search-forward markdown-regex-link-inline nil t)
(let ((start (match-beginning 0))
(end (match-end 0))
(file (match-string-no-properties 6)))
(when (file-exists-p file)
(let* ((abspath (if (file-name-absolute-p file)
file
(concat default-directory file)))
(image (create-image abspath)))
(when image
(let ((ov (make-overlay start end)))
(overlay-put ov 'display image)
(overlay-put ov 'face 'default)
(push ov markdown-inline-image-overlays))))))))))

(defun markdown-toggle-inline-images ()
(interactive)
(if markdown-inline-image-overlays
(markdown-remove-inline-images)
(markdown-display-inline-images)))


;;; Mode Definition ==========================================================

Expand Down