From mboxrd@z Thu Jan 1 00:00:00 1970 From: dmg Subject: Re: how to select a source code block and print it to a postscript file Date: Sat, 9 Sep 2017 14:57:43 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="089e082682b8e674a50558c8ca84" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59761) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dqnlj-0001Hi-Cl for emacs-orgmode@gnu.org; Sat, 09 Sep 2017 17:58:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dqnlh-0002rm-Pb for emacs-orgmode@gnu.org; Sat, 09 Sep 2017 17:58:27 -0400 Received: from mail-ua0-x22f.google.com ([2607:f8b0:400c:c08::22f]:37491) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dqnlh-0002rT-IR for emacs-orgmode@gnu.org; Sat, 09 Sep 2017 17:58:25 -0400 Received: by mail-ua0-x22f.google.com with SMTP id k23so7567505uaf.4 for ; Sat, 09 Sep 2017 14:58:25 -0700 (PDT) In-Reply-To: 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: emacs-orgmode --089e082682b8e674a50558c8ca84 Content-Type: text/plain; charset="UTF-8" I invested a bit more time (plus a couple of suggestions of members of this list and stackoverflow.) I was able to create a module that prints the current source block to PDF without using the exporter. It now uses the pdf viewer to open it. It might be useful to some people who simply need to create a PDF from a source block and have it open in the corresponding PDF application: I look at your suggestions, which pointed me in the direction I needed. At the end I realized what I wanted was simply to export the current block to PDF without using the latex exporter. So I simply make the selection and print that selection to ps, then convert to pdf, and open using org-open ---------------------------------------------------------------------- (defcustom dmg-org-src-export-pdf-font-size 12 "Size of font to use " :type 'number :version 25 :group 'dmg-org-src-export-pdf) (defcustom dmg-org-src-export-pdf-file-name "/tmp/source.code" "Name of the file to export as postscript " :type 'string :version 25 :group 'dmg-org-src-export-pdf) (defun dmg-org-src-export-pdf () "show the source code in xournal as a PDF" (interactive) (save-restriction (save-excursion (unless (executable-find "ps2pdf") (error "ps2pdf not found")) (let ((element (org-element-at-point)) ) (unless (eq (org-element-type element) 'src-block) (error "Not in org-src-block")) ) (let* ( (output-file (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'light)))) dmg-org-src-export-pdf-file-name)) (ps-file (concat output-file ".ps")) (pdf-file (concat output-file ".pdf")) ;; this uses dynamic scoping to set the parameters of ps temporarily (ps-font-size dmg-org-src-export-pdf-font-size) ) (message "Exporting: %s" ps-file) (org-babel-mark-block) (narrow-to-region (region-beginning) (region-end)) (ps-print-buffer-with-faces ps-file) (shell-command (concat "ps2pdf " ps-file " " pdf-file)) (delete-file ps-file) (org-open-file pdf-file) ) ))) On Sat, Sep 9, 2017 at 2:25 AM, dmg wrote: > hi everybody, > > I teach programming and I have been using org-mode for a couple years to > do it. I absolutely love it. > Lately I have been thinking that I would like to be able to draw on the > source code using xournal > (using a tablet) > > To do that, I need to generate a pdf. But I don't want to generate the PDF > of the entire file, > just of the block I am currently positioned at. I wrote the following > code, but it feels clumsy, and > I am not a very good emacs-lisp programmer. I put it together by > extracting code here and there. > > Is there a better way to run ps-print-buffer (or ps-print-region) on the > current block? > I am currently using the :tangle parameter as a filename to be created > (adding the extension .ps) > > the script I am running takes the postscript file, generate a pdf, and > then runs xournal on it. > > thank you in advance for any suggestions, > > (defun org-src-xournal () > "show the source code in xournal as a PDF" > (interactive) > (save-restriction > (save-excursion > (let* ((case-fold-search t) > (tangle-file > (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info > 'light)))) > (user-error "Point is not in a source code block or it > does not have a tangle name"))) > (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*" > "^[ \t]*#\\+end_.*")) > (ps-file (concat tangle-file ".ps")) > ) > > (message "Exporting: %s" ps-file) > (if blockp > (let ((block-start > (progn (goto-char (car blockp)) > (next-line) > (point) > )) > (block-end > (progn (goto-char (cdr blockp)) > (previous-line) > (point) > )) > ) > (narrow-to-region block-start block-end)) > (user-error "Not in a block")) > (ps-print-buffer-with-faces ps-file) > (shell-command (concat "code-xournal " ps-file "&")) > ) > ))) > > > -- > --dmg > > --- > Daniel M. German > http://turingmachine.org > -- --dmg --- Daniel M. German http://turingmachine.org --089e082682b8e674a50558c8ca84 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I invested a bit more time (plus a coup= le of suggestions of members of this list and stackoverflow.) I was able to= create a module that prints the current source block to PDF
without using the exporter. It now uses the pdf viewer to open it= .

It might be useful t= o some people who simply need to create a PDF from a source block and have = it open in the corresponding PDF application:

<= /div>

I look at yo= ur suggestions, which pointed me in the direction I needed.
At th= e end I realized what I wanted was simply to export the current
b= lock to PDF without using the latex exporter. So I simply make the
using org-open



=
----------------------------------------------------------------------<= /font>
(defcustom dmg-org-src-export-pdf-font-size 12
=C2=A0 = =C2=A0"Size of font to use "
=C2=A0 :type 'number
=C2=A0 :version 25
=C2=A0 :group 'dmg-org-src-export= -pdf)

(defcustom dmg-org-src-export-pdf-file-name = "/tmp/source.code"
=C2=A0 =C2=A0"Name of the file = to export as postscript "
=C2=A0 :type 'string
=C2=A0 :version 25
=C2=A0 :group 'dmg-org-src-export-pdf)


(defun dmg-org-src-export-pdf ()
=C2=A0 "show the source code in xournal as a PDF"
=C2=A0 (interactive)
=C2=A0 (save-restriction
=C2=A0= =C2=A0 (save-excursion
=C2=A0 =C2=A0 =C2=A0 =C2=A0(unless (execu= table-find "ps2pdf")=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (error "ps2pdf not found"))

=C2= =A0 =C2=A0 =C2=A0 (let ((element (org-element-at-point))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = (unless (eq (org-element-type element) 'src-block)
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (error "Not in org-src-block"))

=C2=A0 =C2=A0 =C2= =A0 (let* (
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(outp= ut-file
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (or (cdr= (assq :tangle (nth 2 (org-babel-get-src-block-info 'light))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(ps-file (concat output-file ".ps"))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(pdf-file (concat output-file &quo= t;.pdf"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;;= this uses dynamic scoping to set the parameters of ps temporarily
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (message "Exporting: %s= " ps-file)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (org-babel-mark-block= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (narrow-to-region (region-beginning= ) (region-end))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (ps-print-buffer-with= -faces =C2=A0ps-file)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (shell-comman= d (concat "ps2pdf " ps-file " " pdf-file))
=
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 (delete-file ps-file)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 (org-open-file pdf-file)
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 )
=C2=A0 =C2=A0 =C2=A0 )))


On Sat, Sep 9, 2017 at= 2:25 AM, dmg <dmg@turingmachine.org> wrote:
hi everybody,

I teach programming and= I have been using org-mode for a couple years to do it. I absolutely love = it.
Lately I have been thinking that I would like t= o be able to draw on the source code using xournal
= (using a tablet)

To do= that, I need to generate a pdf. But I don't want to generate the PDF o= f the entire file,
just of the block I am currently= positioned at. I wrote the following code, but it feels clumsy, and
<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ;font-size:small">I am not a very good emacs-lisp programmer. I put it toge= ther by extracting code here and there.

<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ;font-size:small">Is there a better way to run ps-print-buffer (or ps-print= -region) on the current block?
I am currently using= the :tangle parameter as a filename to be created (adding the extension .p= s)

the script I am run= ning takes the postscript file, generate a pdf, and then runs xournal on it= .

thank you in advance= for any suggestions,

=
(defun org-src-xournal ()
=C2=A0 "show the source code in xournal as a PDF"
=C2=A0 (interactive)
=C2=A0 (save-restriction
=C2= =A0 =C2=A0 (save-excursion
=C2=A0 =C2=A0 = =C2=A0 (let* ((case-fold-search t)
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (tangle-file
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(or (cdr (assq :tan= gle (nth 2 (org-babel-get-src-block-info 'light))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0(user-error "Point is not in a source code block or it does not hav= e a tangle name")))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (blockp (org-between-regexps-p "^[ \t]*#\\= +begin_.*"
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"^[ \t]*#\\+end= _.*"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (ps-file (concat tangle-file ".ps"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 )
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (message "Exporting: %s&qu= ot; ps-file)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = (if blockp
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (let ((block-start=C2=A0
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (prog= n (goto-char (car blockp))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (next= -line)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (point)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 ))
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (block-end
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (progn (goto-char (cdr blockp))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (previous-line)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (poin= t)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (= narrow-to-region block-start block-end))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (user-error "Not in a block"))=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (ps-print-bu= ffer-with-faces ps-file)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 (shell-command (concat "code-xournal " ps-file &quo= t;&"))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 )
=C2=A0 =C2=A0 =C2=A0 )))


--
--dmg

---
Daniel = M. German
http://= turingmachine.org



--
--dmg

---Daniel M. German
http://turingmachine.org
--089e082682b8e674a50558c8ca84--