* A couple of emacs lisp questions
@ 2010-12-21 13:18 Daniel Dalton
2010-12-21 18:12 ` Drew Adams
0 siblings, 1 reply; 4+ messages in thread
From: Daniel Dalton @ 2010-12-21 13:18 UTC (permalink / raw)
To: help-gnu-emacs
Hi,
I'm a vision impaired student, and I use LaTeX to typeset my
Mathematics.
I'm working on a project called latex-access, porting it to emacs. I've
got two problems so far:
1. I need to make 5 or 6 key bindings. I presume the way to do this is
to create a prefix and then put my bindings under this. So perhaps the
prefix could be c-c c-l and then I can create key bindings to trigger
certain functions i.e.e c-c c-l t toggles the functionality on/off.
How would I implement something like this?
How could I record the current key bindings which use c-c c-l as a
prefix, and then restore them upon exit of the latex-access
stuff. I.e. when it is disabled.
Note, that I haven't created a minor or major mode, I'm just using hooks
and advice as well as interactive functions to make my work available
under emacs.
2. I display various symbols on the echo area, which represent the
current line of the buffer in Braille. Well, actually, it's slightly
more complicated than this, I can display as many as I want i.e. if I
want to translate three lines above the current line as well as the
current line, the echo area will be four lines tall. I'm just using the
message function. The translation in the echo area must be on screen at
all times, i.e. it's not a splash message, and is updated when
post-command hook is triggered. However, this is blocking important
emacs messages that should be visible in the echo area. My idea was to
create some kind of minibuffer or window at the bottom of screen so I
wouldn't interrupt the echo area. I would like the window to shrink/grow
depending on how many lines I need to display. What would the best way
of doing this be? Could anyone point me to some documentation that could
help me with introducing such a feature?
BTW, if it helps at all, the project I'm working on is at:
http://latex-access.sourceforge.net/
Thanks very much for any help,
Daniel Dalton
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: A couple of emacs lisp questions
2010-12-21 13:18 A couple of emacs lisp questions Daniel Dalton
@ 2010-12-21 18:12 ` Drew Adams
2010-12-23 1:08 ` Daniel Dalton
0 siblings, 1 reply; 4+ messages in thread
From: Drew Adams @ 2010-12-21 18:12 UTC (permalink / raw)
To: 'Daniel Dalton', help-gnu-emacs
> 1. I need to make 5 or 6 key bindings. I presume the way to do this is
> to create a prefix and then put my bindings under this. So perhaps the
> prefix could be c-c c-l and then I can create key bindings to trigger
> certain functions i.e.e c-c c-l t toggles the functionality on/off.
> How would I implement something like this?
C-h r i prefix key RET takes you to the Emacs manual node `Prefix Keys', which
provides some help.
;; Define a keymap variable for your prefix-key commands:
(defvar my-map () "My prefix key.")
;; Define the keymap as a prefix-key command:
(define-prefix-command 'my-map)
That defines `my-map' as a command whose function definition and whose value are
the same, new (empty) keymap. More info:
C-h f define-prefix-command
C-h v my-map
C-h f my-map
;; Bind command `my-map' to a key - the prefix key:
(global-set-key "\C-c\C-l" 'my-map)
;; Bind cmds `foo' & `bar' to `a' & `b' following the prefix key:
(define-key my-map "a" 'foo)
(define-key my-map "b" 'bar)
C-h k C-c C-l a
-> "C-c C-l a runs the command foo, which is"
> How could I record the current key bindings which use c-c c-l as a
> prefix, and then restore them upon exit of the latex-access
> stuff. I.e. when it is disabled.
> Note, that I haven't created a minor or major mode, I'm just
> using hooks
> and advice as well as interactive functions to make my work available
> under emacs.
OK, let's start over from a clean slate (new Emacs session, to get rid of
bindings etc. we created).
You don't want the prefix key defined globally, but just in your (minor) mode.
[And you apparently do want to use a prefix key for this stuff, so you won't
override some global or major-mode binding with a minor-mode binding. If you
didn't care about that, then you could just forget about `C-c C-l' (a prefix
key) and bind your commands to any keys you wanted in the minor-mode map.]
So you need two keymaps: a minor-mode map and a prefix-key map.
;; Create a keymap for your minor mode, and define the mode.
(defvar latax-mode-map (make-sparse-keymap)
"LaTeX access minor mode keymap.")
(define-minor-mode latax-mode "LaTeX access mode."
nil " LaTax" latax-mode-map)
;; Create the prefix-key keymap.
(defvar latax-prefix-map nil
"LaTeX access mode prefix keymap.")
;; Bind command `latax-prefix-map' to `C-c C-l' in `latex-mode-map':
(define-key latax-mode-map "\C-c\C-l" 'latax-prefix-map)
;; Bind other commands in the prefix map:
(define-key latax-prefix-map "a" 'forward-char)
(define-key latax-prefix-map "b" 'emacs-version)
Turn on/off the minor mode: `M-x latax-mode'.
> 2. ... The translation in the echo area must be on screen at
> all times... and is updated when post-command hook is triggered.
> However, this is blocking important emacs messages.... My idea was to
> create some kind of minibuffer or window at the bottom of screen so I
> wouldn't interrupt the echo area.
> I would like the window to shrink/grow depending on how many lines
> I need to display. What would the best way of doing this be?
Dunno. Sounds like you should just display another buffer (always), and send the
output you want there. See `with-current-buffer' and the like.
For fitting the window to the buffer, see `resize-temp-buffer-window' and
`fit-window-to-buffer'.
You can also use a separate frame for the buffer - see `special-display-regexps'
and `special-display-buffer-names'. If you do that, you can use `fit-frame' in
library `fit-frame.el' to fit the frame to the buffer.
http://www.emacswiki.org/emacs/FrameModes#ShrinkWrappedFrames
HTH.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: A couple of emacs lisp questions
2010-12-21 18:12 ` Drew Adams
@ 2010-12-23 1:08 ` Daniel Dalton
2010-12-23 1:34 ` Drew Adams
0 siblings, 1 reply; 4+ messages in thread
From: Daniel Dalton @ 2010-12-23 1:08 UTC (permalink / raw)
To: Drew Adams; +Cc: help-gnu-emacs
Hi Drew,
Thanks so much for the detailed response. Please see my comments below.
On Tue, Dec 21, 2010 at 10:12:03AM -0800, Drew Adams wrote:
> > 1. I need to make 5 or 6 key bindings. I presume the way to do this is
> > to create a prefix and then put my bindings under this. So perhaps the
> > prefix could be c-c c-l and then I can create key bindings to trigger
> > certain functions i.e.e c-c c-l t toggles the functionality on/off.
> > How would I implement something like this?
>
> C-h r i prefix key RET takes you to the Emacs manual node `Prefix Keys', which
> provides some help.
>
I shall take a look.
> ;; Define a keymap variable for your prefix-key commands:
> (defvar my-map () "My prefix key.")
>
> ;; Define the keymap as a prefix-key command:
> (define-prefix-command 'my-map)
>
> That defines `my-map' as a command whose function definition and whose value are
> the same, new (empty) keymap. More info:
>
> C-h f define-prefix-command
> C-h v my-map
> C-h f my-map
>
> ;; Bind command `my-map' to a key - the prefix key:
> (global-set-key "\C-c\C-l" 'my-map)
>
> ;; Bind cmds `foo' & `bar' to `a' & `b' following the prefix key:
> (define-key my-map "a" 'foo)
> (define-key my-map "b" 'bar)
Oh, that all seems to be fairly logical and make sense.
> > How could I record the current key bindings which use c-c c-l as a
> > prefix, and then restore them upon exit of the latex-access
> > stuff. I.e. when it is disabled.
> > Note, that I haven't created a minor or major mode, I'm just
> > using hooks
> > and advice as well as interactive functions to make my work available
> > under emacs.
>
> OK, let's start over from a clean slate (new Emacs session, to get rid of
> bindings etc. we created).
>
> You don't want the prefix key defined globally, but just in your (minor) mode.
Are you suggesting I should make my code into a minor mode? What work is
involved to do this? Perhaps I should look at the docs. Currently I just
have about a dozen different functions, most interactive which are
called when necessary. I use hooks and advice to make my code
execute... How different would my implementation have to be if I built a
minor mode? Would I have to change much? And what benefits would there
be in doing this?
> ;; Create a keymap for your minor mode, and define the mode.
> (defvar latax-mode-map (make-sparse-keymap)
> "LaTeX access minor mode keymap.")
>
> (define-minor-mode latax-mode "LaTeX access mode."
> nil " LaTax" latax-mode-map)
>
What is the Latax-mode stuff for? I thought we are creating a minor mode
for latex-access?
> ;; Create the prefix-key keymap.
> (defvar latax-prefix-map nil
> "LaTeX access mode prefix keymap.")
>
> ;; Bind command `latax-prefix-map' to `C-c C-l' in `latex-mode-map':
> (define-key latax-mode-map "\C-c\C-l" 'latax-prefix-map)
>
> ;; Bind other commands in the prefix map:
> (define-key latax-prefix-map "a" 'forward-char)
> (define-key latax-prefix-map "b" 'emacs-version)
>
> Turn on/off the minor mode: `M-x latax-mode'.
Oh I think I understand now. So where should this code go? In a
particular function definition? Or just straight in the .el
file. Currently I have 3 or 4 toggling functions, to change different
behaviour of the mode, could I still use these? When the mode is
disabled, I suppose all my interactive latex-access functions would
still be available though through m-x?
> Dunno. Sounds like you should just display another buffer (always), and send the
> output you want there. See `with-current-buffer' and the like.
I think that sounds like a good idea.
>
> For fitting the window to the buffer, see `resize-temp-buffer-window' and
> `fit-window-to-buffer'.
>
> You can also use a separate frame for the buffer - see `special-display-regexps'
I had a play, only thing that was a bit annoying, which I wouldn't mind
removing is the header and mode lines from the output buffer... I was
just using a standard buffer, is this different for temp buffer? If I
use a temp buffer, won't it get destroyed or have I misunderstood the
temp buffer setup?
> and `special-display-buffer-names'. If you do that, you can use `fit-frame' in
> library `fit-frame.el' to fit the frame to the buffer.
> http://www.emacswiki.org/emacs/FrameModes#ShrinkWrappedFrames
That's something else I will checkout for sure.
I'm probably going to have a go at implementing this in the next week or
so (after Christmas), may I contact you directly if I run into problems?
Once again thanks very much for your comprehensive response!
Kind regards,
Daniel Dalton
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: A couple of emacs lisp questions
2010-12-23 1:08 ` Daniel Dalton
@ 2010-12-23 1:34 ` Drew Adams
0 siblings, 0 replies; 4+ messages in thread
From: Drew Adams @ 2010-12-23 1:34 UTC (permalink / raw)
To: 'Daniel Dalton'; +Cc: help-gnu-emacs
> > > restore them upon exit of the latex-access stuff.
> > > I.e. when it is disabled.
> > > Note, that I haven't created a minor or major mode...
> >
> > You don't want the prefix key defined globally, but just in
> > your (minor) mode.
>
> Are you suggesting I should make my code into a minor mode?
I guess so. Actually, I thought you were suggesting it. ;-) Why not?
> What work is involved to do this?
I just did it:
(define-minor-mode latax-mode "LaTeX access mode."
nil " LaTax" latax-mode-map)
That's all there is to it, if all you want to do is provide some keys bound to
some commands. If you want to do more, then put that in the BODY of
`define-minor-mode', i.e., after the map.
> Perhaps I should look at the docs.
Or look at some examples in the directory .../lisp/. It can be really simple,
depending on what you need.
> Currently I just
> have about a dozen different functions, most interactive which are
> called when necessary. I use hooks and advice to make my code
> execute... How different would my implementation have to be
> if I built a
> minor mode? Would I have to change much? And what benefits would there
> be in doing this?
I don't know what you do with the hooks and advice. But a minor mode is useful
for the key-binding case you described: have some keys active only while in the
mode. And a minor mode can be either per-buffer or global.
A minor mode is much easier to define than a major mode.
> > ;; Create a keymap for your minor mode, and define the mode.
> > (defvar latax-mode-map (make-sparse-keymap)
> > "LaTeX access minor mode keymap.")
> >
> > (define-minor-mode latax-mode "LaTeX access mode."
> > nil " LaTax" latax-mode-map)
>
> What is the Latax-mode stuff for? I thought we are creating a
> minor mode for latex-access?
I was being cute (trying to be). Call it `latex-access-mode' instead of
`latax-mode', if you like. Call it anything you like.
`C-h f define-minor-mode'.
> > ;; Create the prefix-key keymap.
> > (defvar latax-prefix-map nil
> > "LaTeX access mode prefix keymap.")
> >
> > ;; Bind command `latax-prefix-map' to `C-c C-l' in `latex-mode-map':
> > (define-key latax-mode-map "\C-c\C-l" 'latax-prefix-map)
> >
> > ;; Bind other commands in the prefix map:
> > (define-key latax-prefix-map "a" 'forward-char)
> > (define-key latax-prefix-map "b" 'emacs-version)
> >
> > Turn on/off the minor mode: `M-x latax-mode'.
>
> Oh I think I understand now. So where should this code go? In a
> particular function definition? Or just straight in the .el
> file.
Top level in a *.el file.
> Currently I have 3 or 4 toggling functions, to change different
> behaviour of the mode, could I still use these?
Sure. But to toggle the mode on/off you don't need anything - just use the mode
command `latex-access-mode' (it's a toggle).
> When the mode is
> disabled, I suppose all my interactive latex-access functions would
> still be available though through m-x?
Yes.
> > For fitting the window to the buffer, see
> > `resize-temp-buffer-window' and `fit-window-to-buffer'.
> >
> > You can also use a separate frame for the buffer - see
> > `special-display-regexps'
>
> I had a play, only thing that was a bit annoying, which I
> wouldn't mind removing is the header and mode lines from
> the output buffer... I was just using a standard buffer,
> is this different for temp buffer?
Yes, a bit. But you might not care about the differences. Just try stuff and see
what it does for you.
> If I use a temp buffer, won't it get destroyed or have
> I misunderstood the temp buffer setup?
Not necessarily. *Help* is a temp buffer, but nothing requires you to delete the
buffer or even close its window.
> I'm probably going to have a go at implementing this in the
> next week or so (after Christmas), may I contact you directly
> if I run into problems?
You can, but it's better to use the mailing list or Emacs Wiki. They are great
resources, and it helps to get multiple perspectives and input. Your other
friend is the Elisp manual - use `i' in the manual to look things up.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-12-23 1:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-21 13:18 A couple of emacs lisp questions Daniel Dalton
2010-12-21 18:12 ` Drew Adams
2010-12-23 1:08 ` Daniel Dalton
2010-12-23 1:34 ` Drew Adams
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).