emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Bug: [Feature request] generate source blocks from filename [9.0.8 (


From: Andrea
Subject: [O] 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
User-agent: mu4e 0.9.18; emacs 25.1.2

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 via email to

[Prev in Thread] Current Thread [Next in Thread]