From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: making work by pasting code and examples Date: Fri, 30 Jun 2017 18:28:34 +0000 Message-ID: References: <17c0306f-db9f-774d-b88f-cb3201d54008@gMail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="94eb2c1cd00c6328f10553319661" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35114) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dR0ev-0001OJ-Ph for emacs-orgmode@gnu.org; Fri, 30 Jun 2017 14:28:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dR0eu-0001QK-1C for emacs-orgmode@gnu.org; Fri, 30 Jun 2017 14:28:49 -0400 Received: from mail-lf0-x234.google.com ([2a00:1450:4010:c07::234]:36772) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dR0et-0001Px-KR for emacs-orgmode@gnu.org; Fri, 30 Jun 2017 14:28:47 -0400 Received: by mail-lf0-x234.google.com with SMTP id h22so74801891lfk.3 for ; Fri, 30 Jun 2017 11:28:47 -0700 (PDT) In-Reply-To: <17c0306f-db9f-774d-b88f-cb3201d54008@gMail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Michael Ax , Org-mode --94eb2c1cd00c6328f10553319661 Content-Type: text/plain; charset="UTF-8" On Fri, Jun 30, 2017 at 10:59 AM Michael Ax wrote: > when i paste blog content mixing text & code i usually want to wrap bits > of the paste in an org-structure-template. > > i do it by > - mark a region > - cut the region > - type - paste the region > > i would like to not cut and paste. > > any tips on advising or an advise for the org-structure-template expander? > i would like it to check the lossage and be smart about wrapping itself > around the stuff i mean. > I have been using this[1] for the last year and a half and it works pretty awesome. You would need to install the hydra package. The examples in the comments pretty much describes your use case. How to use the below: 1. Install the hydra package. 2. Evaluate the below code. 3. In an org buffer, select some text. 4. Press " (point) (mark)) (exchange-point-and-mark)) ;; Insert a newline if `beg' is *not* at beginning of the line. ;; Example: You have ^abc$ where ^ is bol and $ is eol. ;; "bc" is selected and
On Fri, Jun 30= , 2017 at 10:59 AM Michael Ax <mi= chaelax@gmail.com> wrote:
=20 =20 =20

when i paste blog content mixing text & code i usually want to wrap bits of the paste in an org-structure-template.

i do it by
- mark a region
- cut the region
- type <x tab to get the template
- paste the region

i would like to not cut and paste.

any tips on advising or an advise for the org-structure-template expander?
i would like it to check the lossage and be smart about wrapping itself around the stuff i mean.


=
I have been using this[1] for the last year and a half and it works pr= etty awesome. You would need to install the hydra package. The examples in = the comments pretty much describes your use case.

= How to use the below:

1. Install the hydra package= .
2. Evaluate the below code.
3. In an org buffer, sele= ct some text.
4. Press "<x" (no tab needed).. and th= e selected text will be wrapped in "#+BEGIN_EXAMPLE" and "#+= END_EXAMPLE".

=3D=3D=3D=3D=3D
(defu= n modi/org-template-expand (str &optional lang)
=C2=A0 "= Expand org template."
=C2=A0 (let (beg old-beg end content)<= /div>
=C2=A0 =C2=A0 ;; Save restriction to automatically undo the upcom= ing `narrow-to-region'
=C2=A0 =C2=A0 (save-restriction
<= div>=C2=A0 =C2=A0 =C2=A0 (when (use-region-p)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 (setq beg (region-beginning))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 (setq end (region-end))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; Note t= hat regardless of the direction of selection, we will always
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 ;; have (region-beginning) < (region-end).
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (save-excursion
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 ;; If `point' is at `end', exchange point and = mark so that now the
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; `point= ' is now at `beg'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (whe= n (> (point) (mark))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= (exchange-point-and-mark))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;;= Insert a newline if `beg' is *not* at beginning of the line.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; Example: You have ^abc$ where ^ is b= ol and $ is eol.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0"bc" is selected and <e is pressed to = result in:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0a
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;= ; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#+BEGIN_EXAMPLE
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0bc=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0#+END_EXAMPLE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= (when (/=3D beg (line-beginning-position))
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 (electric-indent-just-newline 1)
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq old-beg beg)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq beg (point))
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 ;; Adjust the `end' due to newline
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq end (+ end (- beg old-beg))= )))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (save-excursion
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; If `point' is at `beg', exchange poi= nt and mark so that now the
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;;= `point' is now at `end'
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (when (< (point) (mark))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (exchange-point-and-mark))
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 ;; If the `end' position is at the beginning of a line decremen= t
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; the position by 1, so tha= t the resultant position is eol on
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 ;; the previous line.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (= when (=3D end (line-beginning-position))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 (setq end (1- end)))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 ;; Insert a newline if `point'/`end' is *not* at end of = the line.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; Example: You have= ^abc$ where ^ is bol and $ is eol.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 ;; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"a" is selected and &= lt;e is pressed to result in:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = ;; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#+BEGIN_EXAMPLE
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0a
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0#+END_EXAMPLE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 ;; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0bc
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 (when (not (looking-at "\\s-*$"))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (electric-indent-just-newline 1))= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; Narrow to region so that the tex= t surround the region does
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; not mes= s up the upcoming `org-try-structure-completion' eval
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (narrow-to-region beg end)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 (setq content (delete-and-extract-region beg end)))
= =C2=A0 =C2=A0 =C2=A0 (insert str)
=C2=A0 =C2=A0 =C2=A0 (org-try-s= tructure-completion)
=C2=A0 =C2=A0 =C2=A0 (when (string=3D "= <s" str)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (cond
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(lang
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (insert lang)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forward-= line))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0((and content (not lang)= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (insert "???")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forward-line))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0(t
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = )))
=C2=A0 =C2=A0 =C2=A0 ;; At this point the cursor will be betw= een the #+BEGIN and #+END lines
=C2=A0 =C2=A0 =C2=A0 (when conten= t
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (insert content)
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (deactivate-mark)))))

(defhyd= ra hydra-org-template (:color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A0 =C2=A0 =C2=A0 =C2=A0_p= _ython =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^^ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 ^^
"
=C2=A0 ("s" (modi/org= -template-expand "<s")) ; #+BEGIN_SRC ... #+END_SRC
= =C2=A0 ("E" (modi/org-template-expand "<s" "ema= cs-lisp"))
=C2=A0 ("v" (modi/org-template-expand &= quot;<s" "systemverilog"))
=C2=A0 ("m"= ; (modi/org-template-expand "<s" "matlab"))
=C2=A0 ("S" (modi/org-template-expand "<s" "s= hell"))
=C2=A0 ("p" (modi/org-template-expand &quo= t;<s" "python"))
=C2=A0 ("t" (modi/or= g-template-expand "<s" "text"))
=C2=A0 (&q= uot;e" (modi/org-template-expand "<e")) ; #+BEGIN_EXAMPLE= ... #+END_EXAMPLE
=C2=A0 ("x" (modi/org-template-expan= d "<e")) ; #+BEGIN_EXAMPLE ... #+END_EXAMPLE
=C2=A0 = ("q" (modi/org-template-expand "<q")) ; #+BEGIN_QUOT= E ... #+END_QUOTE
=C2=A0 ("V" (modi/org-template-expand= "<v")) ; #+BEGIN_VERSE ... #+END_VERSE
=C2=A0 (&quo= t;c" (modi/org-template-expand "<c")) ; #+BEGIN_CENTER ..= . #+END_CENTER
=C2=A0 ("l" (modi/org-template-expand &q= uot;<l")) ; #+BEGIN_EXPORT latex ... #+END_EXPORT
=C2=A0 = ("L" (modi/org-template-expand "<L")) ; #+LaTeX:
=C2=A0 ("h" (modi/org-template-expand "<h")) = ; #+BEGIN_EXPORT html ... #+END_EXPORT
=C2=A0 ("H" (mod= i/org-template-expand "<H")) ; #+HTML:
=C2=A0 ("= ;a" (modi/org-template-expand "<a")) ; #+BEGIN_EXPORT asc= ii ... #+END_EXPORT
=C2=A0 ("A" (modi/org-template-expa= nd "<A")) ; #+ASCII:
=C2=A0 ("i" (modi/org= -template-expand "<i")) ; #+INDEX: line
=C2=A0 (&quo= t;I" (modi/org-template-expand "<I")) ; #+INCLUDE: line
=C2=A0 ("<" self-insert-command "<")
=C2=A0 ("o" nil "quit"))

(defun modi/org-template-maybe ()
=C2=A0 "Insert org-templ= ate if point is at the beginning of the line, or is a
region is s= elected. Else call `self-insert-command'."
=C2=A0 (inter= active)
=C2=A0 (let ((regionp (use-region-p)))
=C2=A0 = =C2=A0 (if (or regionp
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = (and (not regionp)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0(looking-back "^")))
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 (hydra-org-template/body)
=C2=A0 =C2=A0 =C2=A0 (self-i= nsert-command 1))))

(define-key org-mode-map (kbd = "<") #'modi/org-template-maybe)=C2=A0
=3D=3D=3D= =3D=3D

--94eb2c1cd00c6328f10553319661--