{"action":"created","issue":{"url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/1","repository_url":"https://api.github.com/repos/cute-jumper/embrace.el","labels_url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/1/comments","events_url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/1/events","html_url":"https://github.com/cute-jumper/embrace.el/issues/1","id":149977480,"number":1,"title":"Wow and Org-related idea","user":{"login":"alphapapa","id":601365,"avatar_url":"https://avatars.githubusercontent.com/u/601365?v=3","gravatar_id":"","url":"https://api.github.com/users/alphapapa","html_url":"https://github.com/alphapapa","followers_url":"https://api.github.com/users/alphapapa/followers","following_url":"https://api.github.com/users/alphapapa/following{/other_user}","gists_url":"https://api.github.com/users/alphapapa/gists{/gist_id}","starred_url":"https://api.github.com/users/alphapapa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphapapa/subscriptions","organizations_url":"https://api.github.com/users/alphapapa/orgs","repos_url":"https://api.github.com/users/alphapapa/repos","events_url":"https://api.github.com/users/alphapapa/events{/privacy}","received_events_url":"https://api.github.com/users/alphapapa/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-04-21T06:15:16Z","updated_at":"2016-05-04T20:51:07Z","closed_at":null,"body":"Hi there,\r\n\r\nJust saw your post on /r/emacs, and this looks great!  Can't wait to try it.\r\n\r\nI have an idea: I have this code which I use to enclose regions in Org blocks and change Org block types, and it would seem to go very well with `embrace`.  Maybe you'd like to integrate it?  I can imagine a binding like `C-, a o` to enclose a region in an Org block...\r\n\r\nHere's the code, feel free to use however you like.  :)\r\n\r\n```el\r\n(defun ap/org-insert-structure-template-or-enclose-region ()\r\n    \"Insert structure block template.  When region is active, enclose region in block.\"\r\n    (require 's)\r\n    (interactive)\r\n    (let* ((template (ap/org-read-structure-template))\r\n           (text \"\")\r\n           enclosed-text)\r\n      (when (use-region-p)\r\n        (setq text (buffer-substring-no-properties (region-beginning) (region-end)))\r\n        (delete-region (region-beginning) (region-end)))\r\n      (setq enclosed-text (s-replace \"?\" text template))\r\n      (insert enclosed-text)\r\n      (backward-char (- (length enclosed-text) (length (s-shared-start enclosed-text template))))))\r\n\r\n(defun ap/org-change-block-types ()\r\n    \"Change the type of org-mode block at point, or blocks in region.\"\r\n    (interactive)\r\n    (if (use-region-p)\r\n        (progn\r\n          (deactivate-mark)\r\n          (goto-char (region-beginning))\r\n          (while (re-search-forward  \"^ *#\\\\+BEGIN_\" (region-end) nil)\r\n            (ap/org-change-block-type-at-point)))\r\n      (ap/org-change-block-type-at-point)))\r\n\r\n(defun ap/org-change-block-type-at-point ()\r\n    \"Change type of org-mode block at point.\"\r\n    (interactive)\r\n    (unless (ap/org-in-block-p)\r\n      (error \"Not in an org-mode block.\"))\r\n    (let* ((template (ap/org-read-structure-template))\r\n           (case-fold-search t)\r\n           (re \"#\\\\+begin_\\\\(\\\\sw+\\\\)\")\r\n           (block-bounds (ap/org-block-boundaries))\r\n           (block-beg (car block-bounds))\r\n           (block-end (cdr block-bounds))\r\n           (contents (ap/org-block-contents))\r\n           new-block)\r\n      ;; Insert contents into template\r\n      (setq new-block (replace-regexp-in-string (rx \"?\") contents template))\r\n      ;; Remove extra newline from e.g. SRC blocks\r\n      (setq new-block (replace-regexp-in-string (rx \"\\n\\n#+END\") \"\\n#+END\" new-block))\r\n      ;; Replace old block with new one\r\n      (goto-char block-beg)\r\n      (delete-region block-beg block-end)\r\n      (insert new-block)\r\n      ;; Position cursor (especially for SRC blocks, allowing the user to enter the type)\r\n      (search-backward-regexp re)\r\n      (search-forward-regexp (rx space))))\r\n\r\n(defun ap/org-block-contents (&optional whole)\r\n    \"Return contents of current \\\"BEGIN_...\\\" block.\r\nWhen WHOLE is non-nil, include enclosing meta lines.\"\r\n    (let ((bounds (ap/org-block-boundaries (not whole))))\r\n      (buffer-substring-no-properties (car bounds) (cdr bounds))))\r\n\r\n(defun ap/org-block-boundaries (&optional contents)\r\n    \"Return (BEGINNING . END) of current \\\"#+BEGIN_...\\\" block.\r\nIf CONTENTS is non-nil, return the boundaries of the block's\r\ncontents rather than the entire block.\"\r\n    (let ((case-fold-search t)\r\n          (re \"#\\\\+begin_\\\\(\\\\sw+\\\\)\")\r\n          block-beg block-end contents-beg contents-end)\r\n      (save-excursion\r\n        ;; Get block\r\n        (unless (looking-at re)\r\n          ;; If point is in the middle of the \"#+BEGIN...\",\r\n          ;; `search-backward-regexp' fails, so go to end of line first.\r\n          (end-of-line)\r\n          (condition-case nil\r\n              (search-backward-regexp re)\r\n            (error \"Not in a block.\")))\r\n        (setq block-beg (point))\r\n        (setq block-end (search-forward-regexp (concat (rx bol (optional (1+ space)) \"#+end_\") (match-string 1))))\r\n        (goto-char block-beg)\r\n        (forward-line)\r\n        (setq contents-beg (point))\r\n        (goto-char block-end)\r\n        (end-of-line 0)\r\n        (setq contents-end (point)))\r\n      (if contents\r\n          `(,contents-beg . ,contents-end)\r\n        `(,block-beg . ,block-end))))\r\n\r\n(defun ap/org-in-block-p ()\r\n    \"Non-nil when point belongs to a block.\r\n\r\nReturn first block name matched, or nil.  Beware that in case of\r\nnested blocks, the returned name may not belong to the closest\r\nblock from point.\"\r\n    (save-match-data\r\n      (let ((case-fold-search t)\r\n            (lim-up (save-excursion (outline-previous-heading)))\r\n            (lim-down (save-excursion (outline-next-heading))))\r\n        (org-between-regexps-p \"^[ \\t]*#\\\\+begin_\" \"^[ \\t]*#\\\\+end_\"\r\n                               lim-up lim-down))))\r\n\r\n(defun ap/org-read-structure-template ()\r\n    \"Read org-mode structure template with completion.  Returns template string.\"\r\n    (let* ((templates (map 'list 'second org-structure-template-alist))\r\n           (prefixes (map 'list (lambda (tp)\r\n                                  ;; Get template and pre-whitespace prefix for completion\r\n                                  (reverse (s-match (rx (group\r\n                                                         (1+ (not (any \"\\n\" space))))\r\n                                                        (1+ anything))\r\n                                                    tp)))\r\n                          templates))\r\n           (prefix (completing-read \"Template: \" prefixes nil t))\r\n           (template (second (assoc prefix prefixes))))\r\n      template))\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/comments/216998808","html_url":"https://github.com/cute-jumper/embrace.el/issues/1#issuecomment-216998808","issue_url":"https://api.github.com/repos/cute-jumper/embrace.el/issues/1","id":216998808,"user":{"login":"cute-jumper","id":1386972,"avatar_url":"https://avatars.githubusercontent.com/u/1386972?v=3","gravatar_id":"","url":"https://api.github.com/users/cute-jumper","html_url":"https://github.com/cute-jumper","followers_url":"https://api.github.com/users/cute-jumper/followers","following_url":"https://api.github.com/users/cute-jumper/following{/other_user}","gists_url":"https://api.github.com/users/cute-jumper/gists{/gist_id}","starred_url":"https://api.github.com/users/cute-jumper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cute-jumper/subscriptions","organizations_url":"https://api.github.com/users/cute-jumper/orgs","repos_url":"https://api.github.com/users/cute-jumper/repos","events_url":"https://api.github.com/users/cute-jumper/events{/privacy}","received_events_url":"https://api.github.com/users/cute-jumper/received_events","type":"User","site_admin":false},"created_at":"2016-05-04T20:51:07Z","updated_at":"2016-05-04T20:51:07Z","body":"Hi,\r\n\r\nI've just pushed a quick fix to add org block support. Could you give some feedback if you have time to try out? To use it\r\n```\r\n(add-hook 'org-mode-hook 'embrace-org-mode-hook)\r\n```\r\n\r\nThe key for the block is `l`. If you bind <kbd>C-,</kbd> to `embrace-commander`, you can use <kbd>C-, c l l</kbd> inside a org block to change the block type."}}