org-babel-ui.el 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;;; org-babel-ui.el --- UI elements for org-babel
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
  3. ;; Author: Eric Schulte, Dan Davison, Austin F. Frank
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; UI elements of org-babel
  24. ;; - code folding
  25. ;; - marking working code blocks
  26. ;;; Code:
  27. (require 'org-babel)
  28. (defun org-babel-ui-src-block-cycle-maybe ()
  29. "Detect if this is context for a org-babel src-block and if so
  30. then run `org-babel-execute-src-block'."
  31. (let ((case-fold-search t))
  32. (if (save-excursion
  33. (beginning-of-line 1)
  34. (looking-at org-babel-src-block-regexp))
  35. (progn (call-interactively 'org-babel-ui-src-block-cycle)
  36. t) ;; to signal that we took action
  37. nil))) ;; to signal that we did not
  38. (defun org-babel-ui-src-block-cycle ()
  39. "Cycle the visibility of the current source code block"
  40. (interactive)
  41. ;; should really do this once in an (org-mode hook)
  42. (add-to-invisibility-spec '(org-babel-ui . t))
  43. (message "trying out source block")
  44. (save-excursion
  45. (beginning-of-line)
  46. (if (re-search-forward org-babel-src-block-regexp nil t)
  47. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  48. (end (match-end 0))) ;; end of entire body
  49. (if (memq t (mapcar (lambda (overlay)
  50. (eq (overlay-get overlay 'invisible) 'org-babel-ui))
  51. (overlays-at start)))
  52. (remove-overlays start end 'invisible 'org-babel-ui)
  53. (overlay-put (make-overlay start end) 'invisible 'org-babel-ui)))
  54. (error "not looking at a source block"))))
  55. ;; org-tab-after-check-for-cycling-hook
  56. (add-hook 'org-tab-first-hook 'org-babel-ui-src-block-cycle-maybe)
  57. (provide 'org-babel-ui)
  58. ;;; org-babel-ui ends here