Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hook and interactive with parameter

2 views
Skip to first unread message

Lajos Bodnar

unread,
Feb 12, 2018, 8:51:41 AM2/12/18
to help-gn...@gnu.org
Hi,
I'm new in emacs scripting but I would like to do that:
- I open a file after the file hook run I would like to run a function.
- I can do this without parameter but if I would like to use this function
with getting input from prompt. It will throw an error.

How can I do it what is the correct way to run this function?

(add-hook '...-hook 'my-function)

(defun my-function (param1)
(interactive "sParam1: ")
....))

regards
Lajos

Emanuel Berg

unread,
Feb 12, 2018, 10:28:49 AM2/12/18
to
Lajos Bodnar wrote:

> Hi, I'm new in emacs scripting but I would
> like to do that: - I open a file after the
> file hook run I would like to run a function.
> - I can do this without parameter but if
> I would like to use this function with
> getting input from prompt. It will throw
> an error.

Can you rephrase this not in terms of the
un-implemented implementation but in terms of
what will happen, where, when, and why?

Might be easier to understand that way and
quite possible there is a better way than
messing with hooks - often there is.

--
underground experts united
http://user.it.uu.se/~embe8573

Lajos Bodnar

unread,
Feb 12, 2018, 10:43:16 AM2/12/18
to Emanuel Berg, help-gn...@gnu.org
Thanks for your advice.
I would like to write a function which run up if I open a haskell file.
This function will ask a question automaticaly about that i would like to
run the intero-mode or not.

regards
Lajos

to...@tuxteam.de

unread,
Feb 12, 2018, 11:15:16 AM2/12/18
to help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Feb 12, 2018 at 04:43:04PM +0100, Lajos Bodnar wrote:
> Thanks for your advice.
> I would like to write a function which run up if I open a haskell file.
> This function will ask a question automaticaly about that i would like to
> run the intero-mode or not.

Note that the `interactive' form is just "declarative", i.e.
it doesn't "do" anything on itself. It just states what kind
of parameters a function is going to take when invoked "as a
command", that is, from a key binding or similar.

The "magic" of asking for parameters happens there.

When that function is invoked from a hook this "magic" will be
missing :-) Besides, most hook functions are parameterless.

One way around it is asking the user actively. See for example
the function "read-from-minibuffer" (and perhaps most of the
chapter on minibuffers from the Emacs manual).

That said, to me, calling user interaction from a hook "feels"
a bit strange. But perhaps it's just me.

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlqBvYQACgkQBcgs9XrR2kZveQCdHQG9Biu3wDCIhUpauInMsnQc
HTIAnil7u+iufFKdB/zDKpFjj1jtL6/6
=r1CJ
-----END PGP SIGNATURE-----

Emanuel Berg

unread,
Feb 12, 2018, 11:16:22 AM2/12/18
to
Lajos Bodnar wrote:

> Thanks for your advice. I would like to write
> a function which run up if I open a haskell
> file. This function will ask a question
> automaticaly about that i would like to run
> the intero-mode or not.

I don't know if that is such a good idea, but
something like this would do it:

(defun ask-text-mode ()
(when (yes-or-no-p "Activate text mode? ")
(text-mode) ))

;; (setq nroff-mode-hook nil)
(defun nroff-mode-hook-temp-f ()
(ask-text-mode) )
(add-hook 'nroff-mode-hook #'nroff-mode-hook-temp-f)

Emanuel Berg

unread,
Feb 12, 2018, 11:22:01 AM2/12/18
to
tomas wrote:

> That said, to me, calling user interaction
> from a hook "feels" a bit strange.
> But perhaps it's just me.

No, hooks are often troublesome and the more you
put into them, the more trouble you get.
In this case while I don't think the computer
will blow up, I think the OP will be annoyed
with this question popping up all the time.
It isn't the Emacs way. And it is inefficient
as well as without it, say 50% of the times it
would be correct without any interference, now
you have to answer yes or no 100% of the
times instead.

Lajos Bodnar

unread,
Feb 13, 2018, 3:23:54 AM2/13/18
to Emanuel Berg, help-gn...@gnu.org
Thanks a lot,
I understood what the problem is with this concept.
What is your advice ?
The problem is that if I open a haskell file then sometimes I don't want to
run intero-mode with it (because the intero mode is very expensive).
In most cases I would like to run intero but not all time.

Lajos Bodnar

unread,
Feb 13, 2018, 3:43:02 AM2/13/18
to help-gn...@gnu.org
Maybe I have to find a project/solution/workspace handler and if the opened
file is into these workspace then I will run intero-mode.

On Tue, Feb 13, 2018 at 9:23 AM, Lajos Bodnar <bodnar...@gmail.com>
wrote:

> Thanks a lot,
> I understood what the problem is with this concept.
> What is your advice ?
> The problem is that if I open a haskell file then sometimes I don't want
> to run intero-mode with it (because the intero mode is very expensive).
> In most cases I would like to run intero but not all time.
>
> On Mon, Feb 12, 2018 at 5:21 PM, Emanuel Berg <moa...@zoho.com> wrote:
>

Yuri Khan

unread,
Feb 13, 2018, 3:44:55 AM2/13/18
to Lajos Bodnar, Emanuel Berg, help-gnu-emacs
On Tue, Feb 13, 2018 at 3:23 PM, Lajos Bodnar <bodnar...@gmail.com> wrote:

> What is your advice ?
> The problem is that if I open a haskell file then sometimes I don't want to
> run intero-mode with it (because the intero mode is very expensive).
> In most cases I would like to run intero but not all time.

Option 1: Bind intero-mode to an easy key, such as <f9>. Press it
after opening each file. In term of keypresses, pressing <f9> is not
much worse than pressing “y” when you want to enable the mode, and not
pressing <f9> is cheaper than pressing “n” when you don’t.

Option 2: Enable intero-mode from the hook unconditionally and
tolerate the expensiveness. Maybe bind a key to disable the mode.

Option 3: Enable intero-mode dependent on a variable, let’s say,
“my-enable-intero-mode”. Manage it as a file-local or directory-local
variable.

to...@tuxteam.de

unread,
Feb 13, 2018, 3:52:02 AM2/13/18
to Lajos Bodnar, Emanuel Berg, help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Feb 13, 2018 at 09:23:41AM +0100, Lajos Bodnar wrote:
> Thanks a lot,
> I understood what the problem is with this concept.
> What is your advice ?
> The problem is that if I open a haskell file then sometimes I don't want to
> run intero-mode with it (because the intero mode is very expensive).
> In most cases I would like to run intero but not all time.

I'd go with experimenting. After all, a diffuse feeling by
Emanuel and me is less worth than hard-nosed experience :-)

So hook away, and either use yes-or-no-p (or its less verbose
sister y-or-n-p), as Emanuel suggested -- or the more complex
(but better tunable) minibuffer stuff. Find out what breaks
(if at all: hunches are, after all, just... hunches :)

Just to get you jumpsterted: this seems to work, more or less.
Fine tuning is left as an exercise for the reader :)

(defun my-confirm-extra-toppings ()
(when (y-or-n-p "With extra toppings? ")
;; just to show something:
(set-background-color "lightblue")))

(add-hook 'find-file-hook 'my-confirm-extra-toppings)

Enjoy :-)

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlqCpyQACgkQBcgs9XrR2kaltACeMLATdhMYn8fFK/0H7FDUhL7m
Q9oAnRN/fm0o/AH9lRGHr6GJOSE18Bke
=Hrn/
-----END PGP SIGNATURE-----

Lajos Bodnar

unread,
Feb 13, 2018, 4:05:57 AM2/13/18
to to...@tuxteam.de, Emanuel Berg, help-gn...@gnu.org
Thanks a lot,
I overall solution will be that when a haskell file is opened and I push
the F9 button then I will look over that where the haskell project root is
(.git folder or stack.yaml file).
I will add this path to my-opened-projects list and if I open a new haskell
file I will check that the file is under these path and I will run
intero-mode automatically.
It will based on the .git folder or the stack.yaml file or ...
The other cases I will run intero-mode manually if it is neccessary.
There is another advantage with this solution that I can open a filtered
minibuffer with only the opened "project" files.

regards
Lajos


On Tue, Feb 13, 2018 at 9:51 AM, <to...@tuxteam.de> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Tue, Feb 13, 2018 at 09:23:41AM +0100, Lajos Bodnar wrote:
> > Thanks a lot,
> > I understood what the problem is with this concept.
> > What is your advice ?
> > The problem is that if I open a haskell file then sometimes I don't want
> to
> > run intero-mode with it (because the intero mode is very expensive).
> > In most cases I would like to run intero but not all time.
>
> I'd go with experimenting. After all, a diffuse feeling by
> Emanuel and me is less worth than hard-nosed experience :-)
>
> So hook away, and either use yes-or-no-p (or its less verbose
> sister y-or-n-p), as Emanuel suggested -- or the more complex
> (but better tunable) minibuffer stuff. Find out what breaks
> (if at all: hunches are, after all, just... hunches :)
>
> Just to get you jumpsterted: this seems to work, more or less.
> Fine tuning is left as an exercise for the reader :)
>
> (defun my-confirm-extra-toppings ()
> (when (y-or-n-p "With extra toppings? ")
> ;; just to show something:
> (set-background-color "lightblue")))
>
> (add-hook 'find-file-hook 'my-confirm-extra-toppings)
>
> Enjoy :-)
>
> Cheers
> - -- t
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.12 (GNU/Linux)
>

Emanuel Berg

unread,
Feb 13, 2018, 2:02:24 PM2/13/18
to
Yuri Khan wrote:

> Option 1: Bind intero-mode to an easy key,
> such as <f9>. Press it after opening each
> file. In term of keypresses, pressing <f9> is
> not much worse than pressing “y” when you
> want to enable the mode, and not pressing
> <f9> is cheaper than pressing “n” when you
> don’t.

Yes, this is the best idea.

If you notice that for certain files
(particular files) you always do this, you can
use this

(setq magic-mode-alist '(("/\\* cpp \\*/" . c++-mode)))

for those files, only adapted to Haskell, not
C++.

To test it, put

/* cpp */

as the first line in a file called cpp.txt -
now, the ".txt" extention should put the file
in text mode. But eval the above form, close
the file, and open it again - now it should be
in C++.

Magic :)
0 new messages