emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Bug: [Feature request] generate source blocks from filename [9.0.8 (9.0.8-elpaplus @ /Users/andreagiugliano/.emacs.d/elpa/org-plus-contrib-20170606/)]
@ 2017-06-19 18:44 Andrea
  0 siblings, 0 replies; only message in thread
From: Andrea @ 2017-06-19 18:44 UTC (permalink / raw)
  To: emacs-orgmode


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/)

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-06-19 18:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-19 18:44 Bug: [Feature request] generate source blocks from filename [9.0.8 (9.0.8-elpaplus @ /Users/andreagiugliano/.emacs.d/elpa/org-plus-contrib-20170606/)] Andrea

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).