emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Andrea <agiugliano91@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Bug: [Feature request] generate source blocks from filename [9.0.8 (9.0.8-elpaplus @ /Users/andreagiugliano/.emacs.d/elpa/org-plus-contrib-20170606/)]
Date: Mon, 19 Jun 2017 20:44:03 +0200	[thread overview]
Message-ID: <m2shivq0lo.fsf@gmail.com> (raw)


Hello everyone,

Thanks immensely for your effort in developing/maintaining org mode. I
learned to use it during my Ph.D. and now that I am working is even more
useful (and fun :).

I like literate programming, and I find really useful to work by using
source blocks (as I can swap between org mode bindings and the code mode
bindings with the easy C-c ').

So to simplify my source imports I developed this simple function in
elisp:

#+BEGIN_SRC elisp
;; in this function there is a map from file extensions to org babel identifiers
(defun org-src-from-filename (filepath)
  "Convert filename at point to org src block."
  (interactive)
  (let* (
	 (ext2babelIds '(("clj" . "clojure")
		    ("dot" . "dot")
					;(gnuplot . t)
					;(calc . t)
		    ("hs" . "haskell")
		    ("java" . "java")
		    ("ml" . "ocaml")
		    ("org" . "org")
					;(plantuml . t)
		    ("py" . "python")
		    ("rb" . "ruby")
		    ("scala" . "scala")
		    ("sbt" . "scala")
		    ("sh" . "sh")
		    ("txt" . "text")
		    ("html" . "html")
		    ("xml" . "html")
		    ("xhtml" . "xhtml")
		    ("tex" . "latex")
		    ("el" . "emacs-lisp")
		    ("json" . "javascript")
		    ("js" . "javascript")
		    ("css" . "css")
		    ("sql" . "sql")
		    ("" . "text")
		    ))
	 (bounds (bounds-of-thing-at-point 'filename))
	 (contents (with-temp-buffer
		     (insert-file-contents filepath)
		     (buffer-string)))
	 (extension (file-name-extension filepath))
	 (babel-id (cdr (assoc extension ext2babelIds)))
	 (org-contents (concat "#+BEGIN_SRC " (if babel-id babel-id extension) " :tangle " filepath "\n"  contents "#+END_SRC"))
    )
    (save-excursion (when (and bounds filepath)
      (delete-region (car bounds) (cdr bounds))
      (insert org-contents)))))
#+END_SRC

It basically, given a filename, generates a source block with the
correct extension and the tangle path.

The cool thing is that I can use it to substitute a path at point:

#+BEGIN_SRC emacs-lisp :tangle yes
(defun org-src-from-filename-at-point ()
  (interactive)
  (org-src-from-filename (thing-at-point 'filename)))
(bind-key "C-c s" 'org-src-from-filename-at-point)
#+END_SRC

And even integrate it with helm, by using the kill-ring list:

#+BEGIN_SRC emacs-lisp :tangle yes
(defun org-src-from-helm-kill-ring ()
  (interactive)
  (helm-show-kill-ring)
  (org-src-from-filename-at-point))
(bind-key "C-c y" 'org-src-from-helm-kill-ring)
#+END_SRC

Which becomes so cool having a function that adds the current buffer
filename to the kill-ring:

#+BEGIN_SRC emacs-lisp :tangle yes
(defun copy-buffer-filename ()
  (interactive)
  (let ((path (buffer-file-name)))
    (when path (kill-new path)))
  )
(global-set-key (kbd "C-c b")  'copy-buffer-filename)
#+END_SRC

Do you think it would make sense to add such a functionality to
org-mode? Or maybe it is already there and I missed it?

However, just thanks for your amazing work!

Best regards,

Andrea


Emacs  : GNU Emacs 25.1.2 (x86_64-apple-darwin16.0.0, Carbon Version 157 AppKit 1504)
 of 2017-04-04
Package: Org mode version 9.0.8 (9.0.8-elpaplus @ /Users/andreagiugliano/.emacs.d/elpa/org-plus-contrib-20170606/)

                 reply	other threads:[~2017-06-19 18:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2shivq0lo.fsf@gmail.com \
    --to=agiugliano91@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).