[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] Minor org mode for achieve code folding effects
From: |
Leo Alekseyev |
Subject: |
Re: [O] Minor org mode for achieve code folding effects |
Date: |
Tue, 10 Jan 2012 16:47:58 -0600 |
On Tue, Jan 10, 2012 at 3:08 PM, Eric S Fraga <address@hidden> wrote:
> Giovanni Giorgi <address@hidden> writes:
>
>>
>>
>> Hi all,
>> I'd like to edit some ruby/python/shell script using
>> functions folding.
>>
>> I'd like to get a way to fold functions or method.
>
> Carsten has already given you one possible solution; another is to use
> org + babel with tangling to have each function or method within a
> separate headline in an org document. Check out the manual
I have used both Carsten's and Eric's solution, as well as
hideshow-org (https://github.com/secelis/hideshow-org), which works
rather well and deserves a mention.
Expanding a bit on Carsten's post: Tassilo Horn wrote some convenience
functions to set the outline minor mode regexps to correspond to the
current comment syntax. Thus, if I'm (for instance) in shell-script
mode, # * and # ** become the outline level 1 and 2 markers.
One issue with this approach is that you might prefer finer control
over what sections appear folded and what sections appear visible by
default. To deal with this, I hacked org.el watch for special
visibility-control markers in the heading line, so that e.g. * A
heading ending with tilde-tilde like so ~~ would appear always
visible. I hope to come up with a better solution for this in the
future.
The other issue is that Tassilo's code is currently broken for c-mode
(possibly due to conflating *'s in /* comment syntax with *'s in the
outline header level syntax). If you can fix it, that would be useful
:)
The code:
(require 'outline)
(add-hook 'outline-minor-mode-hook
'(lambda ()
(define-key outline-minor-mode-map (kbd "TAB") 'org-cycle)
(define-key outline-minor-mode-map [(tab)] 'org-cycle)
(define-key outline-minor-mode-map [(shift tab)] 'org-global-cycle)
(define-key outline-minor-mode-map [backtab] 'org-global-cycle)))
;; Tassilo Horn's outline-minor-mode enhancement: derive regex from
comment syntax
(defvar th-outline-minor-mode-font-lock-keywords
'((eval . (list (concat "^\\(?:" outline-regexp "\\).*")
0 '(outline-font-lock-face) t t)))
"Additional expressions to highlight in Orgstruct Mode and Outline minor mode.
The difference to `outline-font-lock-keywords' is that this will
overwrite other highlighting.")
(defun th-outline-regexp ()
"Calculate the outline regexp for the current mode."
(let ((comment-starter (replace-regexp-in-string
"[[:space:]]+" "" comment-start)))
(when (string= comment-starter ";")
(setq comment-starter ";;"))
(when (string= comment-starter "#")
(setq comment-starter "##"))
(concat comment-starter " [*]+ ")))
(defun th-outline-minor-mode-init ()
(interactive)
(unless (eq major-mode 'latex-mode)
(setq outline-regexp (th-outline-regexp))
(font-lock-add-keywords
nil
th-outline-minor-mode-font-lock-keywords)
(font-lock-fontify-buffer)))
(add-hook 'outline-minor-mode-hook 'th-outline-minor-mode-init)