From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrea 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 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42251) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dN1en-0003Et-Iu for emacs-orgmode@gnu.org; Mon, 19 Jun 2017 14:44:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dN1ej-0000Wk-Kb for emacs-orgmode@gnu.org; Mon, 19 Jun 2017 14:44:13 -0400 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]:37976) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dN1ej-0000WI-Ed for emacs-orgmode@gnu.org; Mon, 19 Jun 2017 14:44:09 -0400 Received: by mail-wm0-x230.google.com with SMTP id u195so1291604wmd.1 for ; Mon, 19 Jun 2017 11:44:09 -0700 (PDT) Received: from Andreas-MacBook-Pro.local.smtp.gmail.com (g50191.upc-g.chello.nl. [80.57.50.191]) by smtp.gmail.com with ESMTPSA id g52sm6526545edg.55.2017.06.19.11.44.07 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Jun 2017 11:44:07 -0700 (PDT) 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@gnu.org 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/)