* help in writing function to pop indirect buffer
@ 2023-01-25 11:13 Luca Ferrari
2023-01-25 17:55 ` Jean Louis
2023-01-28 13:20 ` Jean Louis
0 siblings, 2 replies; 10+ messages in thread
From: Luca Ferrari @ 2023-01-25 11:13 UTC (permalink / raw)
To: help-gnu-emacs
Hi all,
I'm still working on my need to edit Perl code within a region of an
SQL file, with all my configurations that apply to normal Perl
buffers.
One solution I came up is to launch an indirect buffer to edit the
region I need.
I've written the following:
(defun plperl-indirect-buffer-editing ()
(interactive)
(let ((plperl-buffer-name (generate-new-buffer-name "*plperl-indirect*"))
(plperl-buffer-mode 'cperl-mode)
(start (if mark-active (min (point) (mark)) (point)))
(end (if mark-active (max (point) (mark)) (point))))
(pop-to-buffer (make-indirect-buffer (current-buffer) plperl-buffer-name))
(funcall plperl-buffer-mode)
(font-lock-mode)
(narrow-to-region start end)
(goto-char (point-min))
(shrink-window-if-larger-than-buffer)))
This almost works, but when the indirect buffer is popped, I do not
have syntax highlight while I have indentation and apparently all the
other Perl things.
How can I turn on syntax highlight?
Another question: how can I let start and end to be bound to the
region between a defined tag like '$code$'? I thought about
word-search-forward and backward, but it seems ugly and not really
stable.
Thanks,
Luca
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-25 11:13 help in writing function to pop indirect buffer Luca Ferrari
@ 2023-01-25 17:55 ` Jean Louis
2023-01-26 6:45 ` Emanuel Berg
` (2 more replies)
2023-01-28 13:20 ` Jean Louis
1 sibling, 3 replies; 10+ messages in thread
From: Jean Louis @ 2023-01-25 17:55 UTC (permalink / raw)
To: Luca Ferrari; +Cc: help-gnu-emacs
* Luca Ferrari <fluca1978@gmail.com> [2023-01-25 14:16]:
> This almost works, but when the indirect buffer is popped, I do not
> have syntax highlight while I have indentation and apparently all the
> other Perl things.
> How can I turn on syntax highlight?
I can see error:
Not enabling jit-lock: it does not work in indirect buffer
but how to solve it, I do not know.
I find great what you are doing.
> Another question: how can I let start and end to be bound to the
> region between a defined tag like '$code$'? I thought about
> word-search-forward and backward, but it seems ugly and not really
> stable.
Why not define thing-at-point?
;;; Thing at point 'thing-within-$code$
(defun rcd-tap-thing-within-$code$-start ()
"Move point to the beginning of thing within `$code$'."
(re-search-backward "\\$code\\$")
(forward-char 7))
(defun rcd-tap-thing-within-$code$-end ()
"Move point to the end of thing within `$code$."
(re-search-forward "\\$code\\$")
(backward-char 7))
(put 'thing-within-$code$ 'beginning-op 'rcd-tap-thing-within-$code$-start)
(put 'thing-within-$code$ 'end-op 'rcd-tap-thing-within-$code$-end)
$code$
print 1 + 2;
$code$
try:
(thing-at-point 'thing-within-$code$) within $code$ and
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-25 17:55 ` Jean Louis
@ 2023-01-26 6:45 ` Emanuel Berg
2023-01-27 18:30 ` Bruno Barbier
[not found] ` <notmuch-sha1-03eb8baf922f469fd287c8e7e6f142e660e155d3>
2 siblings, 0 replies; 10+ messages in thread
From: Emanuel Berg @ 2023-01-26 6:45 UTC (permalink / raw)
To: help-gnu-emacs
Jean Louis wrote:
> Why not define thing-at-point?
>
> ;;; Thing at point 'thing-within-$code$
>
> (defun rcd-tap-thing-within-$code$-start ()
> "Move point to the beginning of thing within `$code$'."
> (re-search-backward "\\$code\\$")
> (forward-char 7))
>
> (defun rcd-tap-thing-within-$code$-end ()
> "Move point to the end of thing within `$code$."
> (re-search-forward "\\$code\\$")
> (backward-char 7))
Why not stop hard-coding everything ...
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-25 17:55 ` Jean Louis
2023-01-26 6:45 ` Emanuel Berg
@ 2023-01-27 18:30 ` Bruno Barbier
[not found] ` <notmuch-sha1-03eb8baf922f469fd287c8e7e6f142e660e155d3>
2 siblings, 0 replies; 10+ messages in thread
From: Bruno Barbier @ 2023-01-27 18:30 UTC (permalink / raw)
To: Jean Louis, Luca Ferrari; +Cc: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> * Luca Ferrari <fluca1978@gmail.com> [2023-01-25 14:16]:
>> This almost works, but when the indirect buffer is popped, I do not
>> have syntax highlight while I have indentation and apparently all the
>> other Perl things.
>> How can I turn on syntax highlight?
>
> I can see error:
> Not enabling jit-lock: it does not work in indirect buffer
>
> but how to solve it, I do not know.
>
I'm getting the same warning, but fontification seems to work and is
updated in both buffers.
In case it matters, I'm using:
GNU Emacs 29.0.60 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.17.6) of 2023-01-24
Did you try using a bare emacs ? (emacs -Q)
> I find great what you are doing.
Me too. I've never used indirect buffers, but, your function looks very
useful to focus on several small parts of huge files.
Thanks!
Bruno
^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <notmuch-sha1-03eb8baf922f469fd287c8e7e6f142e660e155d3>]
* Re: help in writing function to pop indirect buffer
[not found] ` <notmuch-sha1-03eb8baf922f469fd287c8e7e6f142e660e155d3>
@ 2023-01-28 11:59 ` Bruno Barbier
2023-01-30 13:30 ` Luca Ferrari
0 siblings, 1 reply; 10+ messages in thread
From: Bruno Barbier @ 2023-01-28 11:59 UTC (permalink / raw)
To: Luca Ferrari; +Cc: help-gnu-emacs, Jean Louis
Bruno Barbier <brubar.cs@gmail.com> writes:
> I'm getting the same warning, but fontification seems to work and is
> updated in both buffers.
Playing with your idea with an org buffer, I discovered that I was
wrong. It works only if the base buffer and the indirect buffer are in
the same major mode (and, also, when forcing fontification of the
invisible/undisplayed pieces of text after any change).
In your use case, it might not be possible to use indirect buffers: the
base buffer and the indirect buffer share the same text and its
properties, and, syntax highlighting is mostly done using text
properties.
Org mode allows to edit pieces of text in any mode (using standard buffers).
The lisp function doing most of the work is `org-edit-src-code'. Maybe
you could call/reuse something from there.
Bruno
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-28 11:59 ` Bruno Barbier
@ 2023-01-30 13:30 ` Luca Ferrari
2023-01-30 18:38 ` Bruno Barbier
2023-01-31 7:36 ` Jean Louis
0 siblings, 2 replies; 10+ messages in thread
From: Luca Ferrari @ 2023-01-30 13:30 UTC (permalink / raw)
To: Bruno Barbier; +Cc: help-gnu-emacs, Jean Louis
On Sat, Jan 28, 2023 at 12:51 PM Bruno Barbier <brubar.cs@gmail.com> wrote:
> In your use case, it might not be possible to use indirect buffers: the
> base buffer and the indirect buffer share the same text and its
> properties, and, syntax highlighting is mostly done using text
> properties.
>
Gosh!
I've tested that if I enable cperl-mode in the original buffer, then
the indirect buffer inherits the syntax highligthing also for new
code.
> Org mode allows to edit pieces of text in any mode (using standard buffers).
> The lisp function doing most of the work is `org-edit-src-code'. Maybe
> you could call/reuse something from there.
It is a little too complex for me to understand it.
Another idea could be to use a normal buffer, narrowed to the region I
would like to have. The problem is that clone-buffer cannot be used
for a file visiting buffer.
Any idea?
Thanks,
Luca
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-30 13:30 ` Luca Ferrari
@ 2023-01-30 18:38 ` Bruno Barbier
2023-01-31 7:36 ` Jean Louis
1 sibling, 0 replies; 10+ messages in thread
From: Bruno Barbier @ 2023-01-30 18:38 UTC (permalink / raw)
To: Luca Ferrari; +Cc: help-gnu-emacs, Jean Louis
Luca Ferrari <fluca1978@gmail.com> writes:
> Another idea could be to use a normal buffer, narrowed to the region I
> would like to have. The problem is that clone-buffer cannot be used
> for a file visiting buffer.
> Any idea?
>
Jean posted a link to a wiki page about indirect buffers:
https://www.emacswiki.org/emacs/IndirectBuffers
From that page, it looks like the package 'edit-indirect' should do what you want:
https://github.com/Fanael/edit-indirect
(edit-indirect doesn't use "indirect" buffers)
Bruno
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-30 13:30 ` Luca Ferrari
2023-01-30 18:38 ` Bruno Barbier
@ 2023-01-31 7:36 ` Jean Louis
2023-09-07 12:53 ` Luca Ferrari
1 sibling, 1 reply; 10+ messages in thread
From: Jean Louis @ 2023-01-31 7:36 UTC (permalink / raw)
To: Luca Ferrari; +Cc: Bruno Barbier, help-gnu-emacs
* Luca Ferrari <fluca1978@gmail.com> [2023-01-30 16:31]:
> On Sat, Jan 28, 2023 at 12:51 PM Bruno Barbier <brubar.cs@gmail.com> wrote:
> > In your use case, it might not be possible to use indirect buffers: the
> > base buffer and the indirect buffer share the same text and its
> > properties, and, syntax highlighting is mostly done using text
> > properties.
> >
>
> Gosh!
> I've tested that if I enable cperl-mode in the original buffer, then
> the indirect buffer inherits the syntax highligthing also for new
> code.
I gave you reference:
EmacsWiki: Indirect Buffers:
https://www.emacswiki.org/emacs/IndirectBuffers
Where it says:
--------------
It seems one can also set the mode from a fake buffer-file-name (using
auto-mode-alist in the usual way). At least with Emacs v20. So a
fake-file-name of say “x.el” will result in emacs-lisp-mode.
(with-current-buffer the-indirect-buffer
(set 'buffer-file-name fake-file-name)
(set-auto-mode) ; in files.el
; (set 'buffer-file-name nil) doesn't appear necessary.
)
Does using fake file name works?
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-31 7:36 ` Jean Louis
@ 2023-09-07 12:53 ` Luca Ferrari
0 siblings, 0 replies; 10+ messages in thread
From: Luca Ferrari @ 2023-09-07 12:53 UTC (permalink / raw)
To: Luca Ferrari, Bruno Barbier, help-gnu-emacs
On Tue, Jan 31, 2023 at 8:39 AM Jean Louis <bugs@gnu.support> wrote:
>
> * Luca Ferrari <fluca1978@gmail.com> [2023-01-30 16:31]:
> > On Sat, Jan 28, 2023 at 12:51 PM Bruno Barbier <brubar.cs@gmail.com> wrote:
> > > In your use case, it might not be possible to use indirect buffers: the
> > > base buffer and the indirect buffer share the same text and its
> > > properties, and, syntax highlighting is mostly done using text
> > > properties.
> > >
> >
> > Gosh!
> > I've tested that if I enable cperl-mode in the original buffer, then
> > the indirect buffer inherits the syntax highligthing also for new
> > code.
>
> I gave you reference:
>
> EmacsWiki: Indirect Buffers:
> https://www.emacswiki.org/emacs/IndirectBuffers
>
> Where it says:
> --------------
>
> It seems one can also set the mode from a fake buffer-file-name (using
> auto-mode-alist in the usual way). At least with Emacs v20. So a
> fake-file-name of say “x.el” will result in emacs-lisp-mode.
I had some time to try over again, and apparently this is working fine:
(defun plperl-indirect-buffer-editing ()
(interactive)
(let ((plperl-buffer-mode 'cperl-mode)
(plperl-buffer (make-indirect-buffer (current-buffer) "indirect.pl"))
(start (if mark-active (min (point) (mark)) (point)))
(end (if mark-active (max (point) (mark)) (point))))
(pop-to-buffer plperl-buffer)
(funcall plperl-buffer-mode)
(font-lock-mode)
(narrow-to-region start end)
(goto-char (point-min))
(shrink-window-if-larger-than-buffer)))
Note that without the funcall plperl-buffer-mode and without adding
the fake extension to the buffer name, the perl mode is not selected.
I'm not fully satisfied however, I need to work on this a little more.
Thanks,
Luca
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: help in writing function to pop indirect buffer
2023-01-25 11:13 help in writing function to pop indirect buffer Luca Ferrari
2023-01-25 17:55 ` Jean Louis
@ 2023-01-28 13:20 ` Jean Louis
1 sibling, 0 replies; 10+ messages in thread
From: Jean Louis @ 2023-01-28 13:20 UTC (permalink / raw)
To: Luca Ferrari; +Cc: help-gnu-emacs
More information:
EmacsWiki: Indirect Buffers:
https://www.emacswiki.org/emacs/IndirectBuffers
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2023-09-07 12:53 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-25 11:13 help in writing function to pop indirect buffer Luca Ferrari
2023-01-25 17:55 ` Jean Louis
2023-01-26 6:45 ` Emanuel Berg
2023-01-27 18:30 ` Bruno Barbier
[not found] ` <notmuch-sha1-03eb8baf922f469fd287c8e7e6f142e660e155d3>
2023-01-28 11:59 ` Bruno Barbier
2023-01-30 13:30 ` Luca Ferrari
2023-01-30 18:38 ` Bruno Barbier
2023-01-31 7:36 ` Jean Louis
2023-09-07 12:53 ` Luca Ferrari
2023-01-28 13:20 ` Jean Louis
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.