Emacs Berlin


Pizzas by Lambda Island, drinks by Contentful! <3


Contentful Guest

password: casacontenta

(network maintenance at 19:00)

Treating Markdown As org-mode

A presentation by Arne Brasseur

for Emacs Berlin, September 2015.

28 September 2016

What already works

Cycling through headings

What already works

Promoto/demote/move entire subtrees

What already works

Follow links:

Creating links

Bindings are different

Behaviour is different

Creating links

(defun plexus/md-insert-link ()
  (let ((link (read-string "Link: "))
        (desc (read-string "Description: ")))
    (when (string= "" desc)
      (setq desc
          (buffer-substring-no-properties (mark) (point)))
      (delete-region (mark) (point)))
    (insert (concat "[" desc "](" link ")"))))

  (kbd "C-c C-l")

Code blocks

Inserting in org-mode: < s TAB

Inserting in gfm-mode: ```

Editing in org-mode: C-c C-'

Editing in markdown-mode: does not exist

Code blocks

Insert like in org-mode, with Yasnippet

M-x yas-new-snippet

# -*- mode: snippet -*-
# name: source-block
# key: <s
# --
\`\`\` $1

Code blocks

(define-key markdown-mode-map (kbd "C-c '")

(define-key plexus/restore-mode-map (kbd "C-c '")

(defvar plexus/restore-mode-map (make-sparse-keymap)
  "Keymap for plexus/restore-mode.")

(define-minor-mode plexus/restore-mode
  "Temporary minor mode to return to the markdown file"
  :lighter " ♻"

Code blocks

(defun plexus/edit-md-source-block ()
  (let ((buffer nil))
      (re-search-backward "\n```\[a-z- \]+\n")
      (re-search-forward "\n``` *")
      (let ((lang (thing-at-point 'symbol))
            (md-buffer (current-buffer)))
        (let ((start (point)))
          (re-search-forward "\n```")
          (let* (...)
            (setq buffer ...))
    (switch-to-buffer buffer)
    (plexus/restore-mode 1)))

Code blocks

(let* ((end (- (point) 4))
       (source (buffer-substring-no-properties start end)))
  (setq buffer (get-buffer-create (concat "*" lang "*")))
  (set-buffer buffer)
  (insert source)
  (setq restore-start start)
  (setq restore-end end)
  (setq restore-buffer md-buffer)
  (make-local-variable 'restore-start)
  (make-local-variable 'restore-end)
  (make-local-variable 'restore-buffer)
  (funcall (intern (concat (downcase lang) "-mode"))))

Code blocks

(defun plexus/restore-md-source-block ()
  (let ((contents (buffer-string))
        (edit-buffer (current-buffer)))
     (set-buffer restore-buffer)
     (delete-region restore-start restore-end)
     (goto-char restore-start)
     (insert contents))
    (switch-to-buffer restore-buffer)
    (kill-buffer edit-buffer)))