* [PATCH] add outline regexp to ruby-mode
@ 2022-07-05 23:28 Yilkal Argaw
2022-07-05 23:48 ` Yilkal Argaw
0 siblings, 1 reply; 8+ messages in thread
From: Yilkal Argaw @ 2022-07-05 23:28 UTC (permalink / raw)
To: emacs-devel
Hello guys.
Some programming modes built into emacs provide outline-regexp to
allow for code folding and an example of this is python-mode.
"ruby-mode" on the other hand does does not have this feature. Since
it is a very handy feature to have it might be worth adding with the
following simple patch.
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index a197724634..6437b84628 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -2447,6 +2447,7 @@ ruby--prettify-symbols-alist
("lambda" . ?λ))
"Value for `prettify-symbols-alist' in `ruby-mode'.")
+
;;;###autoload
(define-derived-mode ruby-mode prog-mode "Ruby"
"Major mode for editing Ruby code."
@@ -2465,7 +2466,8 @@ ruby-mode
((?_ . "w"))))
(setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
- (setq-local syntax-propertize-function #'ruby-syntax-propertize))
+ (setq-local syntax-propertize-function #'ruby-syntax-propertize)
+ (setq-local outline-regexp ruby-indent-beg-re))
;;; Invoke ruby-mode when appropriate
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-05 23:28 [PATCH] add outline regexp to ruby-mode Yilkal Argaw
@ 2022-07-05 23:48 ` Yilkal Argaw
2022-07-06 7:04 ` Bozhidar Batsov
0 siblings, 1 reply; 8+ messages in thread
From: Yilkal Argaw @ 2022-07-05 23:48 UTC (permalink / raw)
To: emacs-devel
> Hello guys.
> Some programming modes built into emacs provide outline-regexp to
> allow for code folding and an example of this is python-mode.
> "ruby-mode" on the other hand does does not have this feature. Since
> it is a very handy feature to have it might be worth adding with the
> following simple patch.
>
> diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
> index a197724634..6437b84628 100644
> --- a/lisp/progmodes/ruby-mode.el
> +++ b/lisp/progmodes/ruby-mode.el
> @@ -2447,6 +2447,7 @@ ruby--prettify-symbols-alist
> ("lambda" . ?λ))
> "Value for `prettify-symbols-alist' in `ruby-mode'.")
>
> +
> ;;;###autoload
> (define-derived-mode ruby-mode prog-mode "Ruby"
> "Major mode for editing Ruby code."
> @@ -2465,7 +2466,8 @@ ruby-mode
> ((?_ . "w"))))
> (setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
>
> - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
> + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
> + (setq-local outline-regexp ruby-indent-beg-re))
>
> ;;; Invoke ruby-mode when appropriate
> Sorry I just noticed the extra line break after I pressed send. Here is the
> patch without that line break.
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index a197724634..7a62d72e06 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -2465,7 +2465,8 @@ ruby-mode
((?_ . "w"))))
(setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
- (setq-local syntax-propertize-function #'ruby-syntax-propertize))
+ (setq-local syntax-propertize-function #'ruby-syntax-propertize)
+ (setq-local outline-regexp ruby-indent-beg-re))
;;; Invoke ruby-mode when appropriate
On Wed, Jul 6, 2022 at 2:28 AM Yilkal Argaw
<yilkalargawworkneh@gmail.com> wrote:
>
> Hello guys.
> Some programming modes built into emacs provide outline-regexp to
> allow for code folding and an example of this is python-mode.
> "ruby-mode" on the other hand does does not have this feature. Since
> it is a very handy feature to have it might be worth adding with the
> following simple patch.
>
> diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
> index a197724634..6437b84628 100644
> --- a/lisp/progmodes/ruby-mode.el
> +++ b/lisp/progmodes/ruby-mode.el
> @@ -2447,6 +2447,7 @@ ruby--prettify-symbols-alist
> ("lambda" . ?λ))
> "Value for `prettify-symbols-alist' in `ruby-mode'.")
>
> +
> ;;;###autoload
> (define-derived-mode ruby-mode prog-mode "Ruby"
> "Major mode for editing Ruby code."
> @@ -2465,7 +2466,8 @@ ruby-mode
> ((?_ . "w"))))
> (setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
>
> - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
> + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
> + (setq-local outline-regexp ruby-indent-beg-re))
>
> ;;; Invoke ruby-mode when appropriate
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-05 23:48 ` Yilkal Argaw
@ 2022-07-06 7:04 ` Bozhidar Batsov
2022-07-06 8:06 ` Juri Linkov
0 siblings, 1 reply; 8+ messages in thread
From: Bozhidar Batsov @ 2022-07-06 7:04 UTC (permalink / raw)
To: Emacs Devel
[-- Attachment #1: Type: text/plain, Size: 3527 bytes --]
I like this suggestion, although I don't think that's the right regexp to use for an outline. Shouldn't it be limited to definitions only (similar to what imenu typically does)?
On Wed, Jul 6, 2022, at 2:48 AM, Yilkal Argaw wrote:
> > Hello guys.
> > Some programming modes built into emacs provide outline-regexp to
> > allow for code folding and an example of this is python-mode.
> > "ruby-mode" on the other hand does does not have this feature. Since
> > it is a very handy feature to have it might be worth adding with the
> > following simple patch.
> >
> > diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
> > index a197724634..6437b84628 100644
> > --- a/lisp/progmodes/ruby-mode.el
> > +++ b/lisp/progmodes/ruby-mode.el
> > @@ -2447,6 +2447,7 @@ ruby--prettify-symbols-alist
> > ("lambda" . ?λ))
> > "Value for `prettify-symbols-alist' in `ruby-mode'.")
> >
> > +
> > ;;;###autoload
> > (define-derived-mode ruby-mode prog-mode "Ruby"
> > "Major mode for editing Ruby code."
> > @@ -2465,7 +2466,8 @@ ruby-mode
> > ((?_ . "w"))))
> > (setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
> >
> > - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
> > + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
> > + (setq-local outline-regexp ruby-indent-beg-re))
> >
> > ;;; Invoke ruby-mode when appropriate
> > Sorry I just noticed the extra line break after I pressed send. Here is the
> > patch without that line break.
>
> diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
> index a197724634..7a62d72e06 100644
> --- a/lisp/progmodes/ruby-mode.el
> +++ b/lisp/progmodes/ruby-mode.el
> @@ -2465,7 +2465,8 @@ ruby-mode
> ((?_ . "w"))))
> (setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
>
> - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
> + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
> + (setq-local outline-regexp ruby-indent-beg-re))
>
> ;;; Invoke ruby-mode when appropriate
>
>
>
> On Wed, Jul 6, 2022 at 2:28 AM Yilkal Argaw
> <yilkalargawworkneh@gmail.com> wrote:
> >
> > Hello guys.
> > Some programming modes built into emacs provide outline-regexp to
> > allow for code folding and an example of this is python-mode.
> > "ruby-mode" on the other hand does does not have this feature. Since
> > it is a very handy feature to have it might be worth adding with the
> > following simple patch.
> >
> > diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
> > index a197724634..6437b84628 100644
> > --- a/lisp/progmodes/ruby-mode.el
> > +++ b/lisp/progmodes/ruby-mode.el
> > @@ -2447,6 +2447,7 @@ ruby--prettify-symbols-alist
> > ("lambda" . ?λ))
> > "Value for `prettify-symbols-alist' in `ruby-mode'.")
> >
> > +
> > ;;;###autoload
> > (define-derived-mode ruby-mode prog-mode "Ruby"
> > "Major mode for editing Ruby code."
> > @@ -2465,7 +2466,8 @@ ruby-mode
> > ((?_ . "w"))))
> > (setq-local prettify-symbols-alist ruby--prettify-symbols-alist)
> >
> > - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
> > + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
> > + (setq-local outline-regexp ruby-indent-beg-re))
> >
> > ;;; Invoke ruby-mode when appropriate
>
>
[-- Attachment #2: Type: text/html, Size: 5644 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-06 7:04 ` Bozhidar Batsov
@ 2022-07-06 8:06 ` Juri Linkov
2022-07-08 1:12 ` Dmitry Gutov
0 siblings, 1 reply; 8+ messages in thread
From: Juri Linkov @ 2022-07-06 8:06 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: Emacs Devel
>> Some programming modes built into emacs provide outline-regexp to
>> allow for code folding and an example of this is python-mode.
>> "ruby-mode" on the other hand does does not have this feature. Since
>> it is a very handy feature to have it might be worth adding with the
>> following simple patch.
>>
>> - (setq-local syntax-propertize-function #'ruby-syntax-propertize))
>> + (setq-local syntax-propertize-function #'ruby-syntax-propertize)
>> + (setq-local outline-regexp ruby-indent-beg-re))
>
> I like this suggestion, although I don't think that's the right regexp to
> use for an outline. Shouldn't it be limited to definitions only (similar to
> what imenu typically does)?
I have been successfully using outlines in ruby-mode for a long time
with such simple customization because I didn't want to make outlines
on if/unless/case/..., only on definitions class/module/def.
(add-hook 'ruby-mode-hook
(lambda ()
;; Copied some keywords from ‘ruby-block-beg-keywords’ and
;; ‘ruby-imenu-create-index-in-block’
(setq-local outline-regexp (concat "^\\s *" (regexp-opt '("class" "module" "def")) "\\s +"))
(setq-local outline-level (lambda () (1+ (/ (current-indentation) ruby-indent-level))))
(outline-minor-mode +1)))
But now I noticed that actually if/unless/case/... in
ruby-indent-beg-re has no effect on outlines, because
they are on a separate regexp group. So using it for
outline-regexp would be fine. Then I only suggest
to add outline-level as well.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-06 8:06 ` Juri Linkov
@ 2022-07-08 1:12 ` Dmitry Gutov
2022-07-08 5:38 ` Bozhidar Batsov
0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Gutov @ 2022-07-08 1:12 UTC (permalink / raw)
To: Juri Linkov, Bozhidar Batsov; +Cc: Emacs Devel
On 06.07.2022 11:06, Juri Linkov wrote:
> But now I noticed that actually if/unless/case/... in
> ruby-indent-beg-re has no effect on outlines, because
> they are on a separate regexp group. So using it for
> outline-regexp would be fine. Then I only suggest
> to add outline-level as well.
If the result looks good to you, please go ahead.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-08 1:12 ` Dmitry Gutov
@ 2022-07-08 5:38 ` Bozhidar Batsov
2022-07-08 8:27 ` Yilkal Argaw
0 siblings, 1 reply; 8+ messages in thread
From: Bozhidar Batsov @ 2022-07-08 5:38 UTC (permalink / raw)
To: Emacs Devel
[-- Attachment #1: Type: text/plain, Size: 564 bytes --]
I'd still prefer the simpler regexp suggested by Juri, as it's more intention revealing, but I can live with using ruby-indent-beg-re as well.
On Fri, Jul 8, 2022, at 4:12 AM, Dmitry Gutov wrote:
> On 06.07.2022 11:06, Juri Linkov wrote:
> > But now I noticed that actually if/unless/case/... in
> > ruby-indent-beg-re has no effect on outlines, because
> > they are on a separate regexp group. So using it for
> > outline-regexp would be fine. Then I only suggest
> > to add outline-level as well.
>
> If the result looks good to you, please go ahead.
>
>
[-- Attachment #2: Type: text/html, Size: 958 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-08 5:38 ` Bozhidar Batsov
@ 2022-07-08 8:27 ` Yilkal Argaw
2022-07-08 17:18 ` Juri Linkov
0 siblings, 1 reply; 8+ messages in thread
From: Yilkal Argaw @ 2022-07-08 8:27 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: Emacs Devel
> I'd still prefer the simpler regexp suggested by Juri, as it's more intention revealing, but I can live with using ruby-indent-beg-re as well.
For my personal use I used to use a similar approach where I defined
outline-regexp through ruby-mode-hook. But I did not want to add an
additional regex that might be left trailing when the regex for the
syntax parsing changes. It might turn out to be an additional load for
the package maintainer and it might not be noticed by new contributors
when changing the syntax parsing rules up top.
It might also be good to add ```outline-heading-end-regexp``` and
```outline-level``` for completeness but my knowledge of regular
expressions and outline-mode is a bit limited. Maybe someone might
help with that. But for now only defining outline-regexp covers most
use cases so I don't mind if mine or Juri's solution was merged.
The regex I used to use personally is the one below incase anyone
finds it helpful
"^\\([ \t]*\\)class \\|^\\([ \t]*\\)module \\|^\\([ \t]*\\)def "
On Fri, Jul 8, 2022 at 8:41 AM Bozhidar Batsov <bozhidar@batsov.dev> wrote:
>
> I'd still prefer the simpler regexp suggested by Juri, as it's more intention revealing, but I can live with using ruby-indent-beg-re as well.
>
> On Fri, Jul 8, 2022, at 4:12 AM, Dmitry Gutov wrote:
>
> On 06.07.2022 11:06, Juri Linkov wrote:
> > But now I noticed that actually if/unless/case/... in
> > ruby-indent-beg-re has no effect on outlines, because
> > they are on a separate regexp group. So using it for
> > outline-regexp would be fine. Then I only suggest
> > to add outline-level as well.
>
> If the result looks good to you, please go ahead.
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] add outline regexp to ruby-mode
2022-07-08 8:27 ` Yilkal Argaw
@ 2022-07-08 17:18 ` Juri Linkov
0 siblings, 0 replies; 8+ messages in thread
From: Juri Linkov @ 2022-07-08 17:18 UTC (permalink / raw)
To: Yilkal Argaw; +Cc: Bozhidar Batsov, Dmitry Gutov, Emacs Devel
>>>> But now I noticed that actually if/unless/case/... in
>>>> ruby-indent-beg-re has no effect on outlines, because
>>>> they are on a separate regexp group. So using it for
>>>> outline-regexp would be fine. Then I only suggest
>>>> to add outline-level as well.
>>>
>>> If the result looks good to you, please go ahead.
>>
>> I'd still prefer the simpler regexp suggested by Juri, as it's more
>> intention revealing, but I can live with using ruby-indent-beg-re as well.
>
> For my personal use I used to use a similar approach where I defined
> outline-regexp through ruby-mode-hook. But I did not want to add an
> additional regex that might be left trailing when the regex for the
> syntax parsing changes. It might turn out to be an additional load for
> the package maintainer and it might not be noticed by new contributors
> when changing the syntax parsing rules up top.
When this will become a real problem then it would be possible
to refactor ruby-indent-beg-re into separate variables one of which
could be used for outline-regexp.
> It might also be good to add ```outline-heading-end-regexp``` and
> ```outline-level``` for completeness but my knowledge of regular
> expressions and outline-mode is a bit limited.
It seems outline-heading-end-regexp is not required here.
> Maybe someone might help with that. But for now only defining
> outline-regexp covers most use cases so I don't mind if mine
> or Juri's solution was merged.
For now I pushed a simple version where outline-regexp is an exact copy
of the first part of ruby-indent-beg-re that contains only a regexp
for top-level definitions.
> The regex I used to use personally is the one below incase anyone
> finds it helpful
>
> "^\\([ \t]*\\)class \\|^\\([ \t]*\\)module \\|^\\([ \t]*\\)def "
This looks equivalent to the regexp produced by the first part of
ruby-indent-beg-re. Thanks for the suggestion!
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2022-07-08 17:18 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-05 23:28 [PATCH] add outline regexp to ruby-mode Yilkal Argaw
2022-07-05 23:48 ` Yilkal Argaw
2022-07-06 7:04 ` Bozhidar Batsov
2022-07-06 8:06 ` Juri Linkov
2022-07-08 1:12 ` Dmitry Gutov
2022-07-08 5:38 ` Bozhidar Batsov
2022-07-08 8:27 ` Yilkal Argaw
2022-07-08 17:18 ` Juri Linkov
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.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).