unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Toggle WikiLink on and off does not work?
@ 2017-08-16  8:55 Csányi Pál
  2017-08-16 14:50 ` Emanuel Berg
  0 siblings, 1 reply; 13+ messages in thread
From: Csányi Pál @ 2017-08-16  8:55 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I'm running GNU Emacs 25.2.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.16)
 of 2017-08-14 on my Gentoo Linux system.

I have in my .emacs file these lines:
;; Toggle WikiLink on and off
(defun emacs-wiki-unlink-toggle ()
      "Toggle <nop> string in the beginning of the current word, to un/make a
    word emacs-wiki link. The current word depends on the point: if the cursor
    is on a non-whitespace character, it's considered a word surrounded by
    whitespace. If the cursor is on a whitespace character, the next word is
    looked up. This way addressing a word works intuitively after having
    arrived on the spot using forward-word."
      (interactive)
      (save-excursion
    (if (looking-at "[[:space:]]")
        (goto-char (- (re-search-forward "[A-Za-z<]") 1))
      (goto-char (+ (re-search-backward "[[:space:]]") 1)))
    (if (looking-at "<nop>")
        (delete-char 5)
      (insert "<nop>"))))

(add-hook 'emacs-wiki-mode-hook
      '(lambda () (local-set-key "\C-c\C-n" 'emacs-wiki-unlink-toggle)))

but in Wiki mode I can't toggle on or off a WikiLink.
Why?


-- 
Best, Pali



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-16  8:55 Toggle WikiLink on and off does not work? Csányi Pál
@ 2017-08-16 14:50 ` Emanuel Berg
  2017-08-16 15:32   ` Emanuel Berg
  2017-08-17  8:17   ` Csányi Pál
  0 siblings, 2 replies; 13+ messages in thread
From: Emanuel Berg @ 2017-08-16 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

Csányi Pál wrote:

> ;; Toggle WikiLink on and off
> (defun emacs-wiki-unlink-toggle ()
>       "Toggle <nop> string in the beginning of the current word, to un/make a
>     word emacs-wiki link. The current word depends on the point: if the cursor
>     is on a non-whitespace character, it's considered a word surrounded by
>     whitespace. If the cursor is on a whitespace character, the next word is
>     looked up. This way addressing a word works intuitively after having
>     arrived on the spot using forward-word."
>       (interactive)
>       (save-excursion
>     (if (looking-at "[[:space:]]")
>         (goto-char (- (re-search-forward "[A-Za-z<]") 1))
>       (goto-char (+ (re-search-backward "[[:space:]]") 1)))
>     (if (looking-at "<nop>")
>         (delete-char 5)
>       (insert "<nop>"))))

Some comments on the code:

The docstring is very technical - usually it
only describes what happens, leaving out most
implementation details. Usually, but
not always!

Also, the lines are very long so for some
people they won't fit on a screen. Break them
off and insert blank lines. Use \n if need be.

You know about `1+' and `1-' to add/subtract 1?

If the "5" is because (length "<nop>") is five,
it is better to compute that - data that is
derived from other data should be computed so
that, if other data change, so does data.

Indentation: add four spaces before the
first "(if".

> (add-hook 'emacs-wiki-mode-hook
>       '(lambda () (local-set-key "\C-c\C-n" 'emacs-wiki-unlink-toggle)))
>
> but in Wiki mode I can't toggle on or off a WikiLink.
> Why?

You don't need a lambda here (and they need not
be quoted). You can put a # before the
function quote.

Using `local-set-key' is one way to do it,
I would however `require' the emacs-wiki,
and then use `define-key' with the mode map, as
in:

    (require 'erc)
    (define-key erc-mode-map "\C-\M-p" #'erc-previous-command)

Ask again if it didn't help!

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-16 14:50 ` Emanuel Berg
@ 2017-08-16 15:32   ` Emanuel Berg
  2017-08-17  8:17   ` Csányi Pál
  1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2017-08-16 15:32 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg wrote:

>> (add-hook 'emacs-wiki-mode-hook '(lambda ()
>> (local-set-key "\C-c\C-n"
>> 'emacs-wiki-unlink-toggle))) but in Wiki
>> mode I can't toggle on or off
>> a WikiLink. Why?
>
> You don't need a lambda here (and they need not
> be quoted). You can put a # before the
> function quote.

... maybe you *do* need a lambda here? Just not
quoted? It seems the form is evaluated. Anyway,
the other suggestion is still better, faster
and more reliable, than to use hooks. If you
still want hooks, here is a construct that
makes it more orderly:

    ;; (setq text-mode-hook nil)
    (defun text-mode-hook-f ()
      (auto-fill-mode)
      (abbrev-mode) )
    (add-hook 'text-mode-hook #'text-mode-hook-f)

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-16 14:50 ` Emanuel Berg
  2017-08-16 15:32   ` Emanuel Berg
@ 2017-08-17  8:17   ` Csányi Pál
  2017-08-17 11:42     ` Emanuel Berg
  2017-08-17 11:54     ` Emanuel Berg
  1 sibling, 2 replies; 13+ messages in thread
From: Csányi Pál @ 2017-08-17  8:17 UTC (permalink / raw)
  To: help-gnu-emacs

2017-08-16 16:50 GMT+02:00 Emanuel Berg <moasen@zoho.com>:
> Csányi Pál wrote:
>
>> ;; Toggle WikiLink on and off
>> (defun emacs-wiki-unlink-toggle ()
>>       "Toggle <nop> string in the beginning of the current word, to un/make a
>>     word emacs-wiki link. The current word depends on the point: if the cursor
>>     is on a non-whitespace character, it's considered a word surrounded by
>>     whitespace. If the cursor is on a whitespace character, the next word is
>>     looked up. This way addressing a word works intuitively after having
>>     arrived on the spot using forward-word."
>>       (interactive)
>>       (save-excursion
>>     (if (looking-at "[[:space:]]")
>>         (goto-char (- (re-search-forward "[A-Za-z<]") 1))
>>       (goto-char (+ (re-search-backward "[[:space:]]") 1)))
>>     (if (looking-at "<nop>")
>>         (delete-char 5)
>>       (insert "<nop>"))))
>
> Some comments on the code:
>
> The docstring is very technical - usually it
> only describes what happens, leaving out most
> implementation details. Usually, but
> not always!
>
> Also, the lines are very long so for some
> people they won't fit on a screen. Break them
> off and insert blank lines. Use \n if need be.
>
> You know about `1+' and `1-' to add/subtract 1?
>
> If the "5" is because (length "<nop>") is five,
> it is better to compute that - data that is
> derived from other data should be computed so
> that, if other data change, so does data.
>
> Indentation: add four spaces before the
> first "(if".
>
>> (add-hook 'emacs-wiki-mode-hook
>>       '(lambda () (local-set-key "\C-c\C-n" 'emacs-wiki-unlink-toggle)))
>>
>> but in Wiki mode I can't toggle on or off a WikiLink.
>> Why?
>
> You don't need a lambda here (and they need not
> be quoted). You can put a # before the
> function quote.
>
> Using `local-set-key' is one way to do it,
> I would however `require' the emacs-wiki,
> and then use `define-key' with the mode map, as
> in:
>
>     (require 'erc)
>     (define-key erc-mode-map "\C-\M-p" #'erc-previous-command)
>
> Ask again if it didn't help!

So I am trying to understand your advices.
This is the elisp program language and I am a newbie here.

Here is the code for the Emacs Wiki mode, which I have now in my .emacs file:
(require 'emacs-wiki)

;; Toggle WikiLink on and off
(defun emacs-wiki-unlink-toggle ()
  (interactive)
  (save-excursion
    (if (looking-at "[[:space:]]")
        (goto-char (- (re-search-forward "[A-Za-z<]") 1))
      (goto-char (+ (re-search-backward "[[:space:]]") 1)))
    (if (looking-at "<nop>")
        (delete-char 5)
      (insert "<nop>"))))

(define-key emacs-wiki-unlink-toggle "\C-\M-n")

When I load the .emacs file with 'M-x load-file' command, I get message:
Wrong number of arguments: define-key, 2

A want to add to this question more.
Actually, with the previous setup the C-c C-n keybinding works,
eg.
if I have in a Wiki page the WikiLink word, and the cursor is on that word, then
the C-c C-n gives:
<nop>WikiLink
but WikiLink remain to be a Wiki Link.
And if I after that hit again C-c C-n then the <nop> disappeared, I get again:
WikiLink

So If I understand right, if one untoggle the Wiki Link, then the link
should disappeared,
and WikiLink should not be underlined. And vica versa. Right?

This is what not works here. Why?

-- 
Best, Pali



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-17  8:17   ` Csányi Pál
@ 2017-08-17 11:42     ` Emanuel Berg
  2017-08-17 18:53       ` Csányi Pál
  2017-08-17 11:54     ` Emanuel Berg
  1 sibling, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2017-08-17 11:42 UTC (permalink / raw)
  To: help-gnu-emacs

Csányi Pál wrote:

> (define-key emacs-wiki-unlink-toggle "\C-\M-n")
>
> When I load the .emacs file with 'M-x
> load-file' command, I get message: Wrong number
> of arguments: define-key, 2

Here you use `C-h f define-key RET' to bring up
the help, where you see that the interface to
`define-key' is

    (define-key KEYMAP KEY DEF)

So it should be

    (define-key emacs-wiki-mode-map "\C-c\C-n" #'emacs-wiki-unlink-toggle)

The reason you do `require' before this is that
otherwise, there isn't any
`emacs-wiki-mode-map' to set!

> A want to add to this question more. Actually,
> with the previous setup the C-c C-n keybinding
> works

OK, yes, I admit I only got confused by that
hook solution with local-set-key. There are
many ways to set keys and naturally I'm the
most into my own ways. So if it works it works.

The reason hooks are considered less reliable
and are a tiny bit slower is that they execute
every time. So the functions there execute over
and over. It shouldn't really influence
the speed. Perhaps the interactive feel, if
your "feelings" are really fine turned.

However with require + define you do it once
and that's it. It is always what I try first
and most time it works. Sometimes it doesn't
work tho and without having digged into that
I guess when some subsection of a software
component is brought to life, it resets keys
that should have been left alone. Whenever that
happens, I still use define, I just enclose it
in a function and have that called from the
hook function. As in:

;; (setq erc-mode-hook nil)
(defun erc-mode-hook-f ()
  ; [...] some unrelated stuff cut for clarity
  (set-erc-keys) ; why needed?
  )
(add-hook 'erc-mode-hook #'erc-mode-hook-f)

;; keys - why is a call needed in the hook as well?
(defun set-erc-keys ()
  (let ((the-map erc-mode-map))
    (define-key the-map "\C-\M-p" #'erc-previous-command)
    (define-key the-map "\C-\M-n" #'erc-next-buffer)
    ; [...]
  ))
(set-erc-keys) ; not enough, apparently

> So If I understand right, if one untoggle the
> Wiki Link, then the link should disappeared,
> and WikiLink should not be underlined. And vica
> versa. Right?

I don't know because I just skimmed thru the
defun looking for simple style things to
spot/correct, I didn't think about what it was
supposed to do. I don't use wikis myself.
But if the key is up and running, debugging
will be much faster :) So what should the
function do, that it doesn't, or, what does it
do, that it shouldn't?

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-17  8:17   ` Csányi Pál
  2017-08-17 11:42     ` Emanuel Berg
@ 2017-08-17 11:54     ` Emanuel Berg
  1 sibling, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2017-08-17 11:54 UTC (permalink / raw)
  To: help-gnu-emacs

Here is how I would write it, I think. It seems
to work at least when inserting or removing the
tag is concerned.

If it gets more complicated, you might want to
factor out the searches (the
`re-search-forward's) and put them into
a `let', then check the associated values if
they are nil or not (see the help for
re-search-forward how to make it not report an
error on no hit), and then move point to
(match-beginning 0) - but only if you feel the
need to, using re-search-forward "inline" if
you will is a fine way to do it as well.

See answer one for comments what I did.
Except for [[:alpha:]] which I didn't mention.

In answer two there are some other comments.
This is answer three :)

    (defun emacs-wiki-unlink-toggle ()
      (interactive)
      (save-excursion
        (if (looking-at "[[:space:]]")
            (goto-char (1- (re-search-forward "[[:alpha:]<]")))
          (goto-char (1+ (re-search-backward "[[:space:]]"))))
        (let*((nop "<nop>")
              (nop-len (length nop)))
          (if (looking-at nop)
              (delete-char nop-len)
            (insert nop)))))

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-17 11:42     ` Emanuel Berg
@ 2017-08-17 18:53       ` Csányi Pál
  2017-08-17 21:12         ` Emanuel Berg
  0 siblings, 1 reply; 13+ messages in thread
From: Csányi Pál @ 2017-08-17 18:53 UTC (permalink / raw)
  To: help-gnu-emacs

2017-08-17 13:42 GMT+02:00 Emanuel Berg <moasen@zoho.com>:
> Csányi Pál wrote:

>> So If I understand right, if one untoggle the
>> Wiki Link, then the link should disappeared,
>> and WikiLink should not be underlined. And vica
>> versa. Right?
>
> I don't know because I just skimmed thru the
> defun looking for simple style things to
> spot/correct, I didn't think about what it was
> supposed to do. I don't use wikis myself.
> But if the key is up and running, debugging
> will be much faster :) So what should the
> function do, that it doesn't, or, what does it
> do, that it shouldn't?

Look here what should it do, that it does not:
It toggles the current CamelCaps word between wikilink and non-link
https://www.emacswiki.org/emacs/EmacsWikiMode#toc12

It is all the same whether is a <nop> before a CamelCase or is not
there, the CamelCase remain wikilink.
I expect that, if a <nop> is before a such CamelCase, like
<nop>CamelCase then it should turn into non-link. Right? This turn
thing does not works here.

-- 
Best, Pali



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-17 18:53       ` Csányi Pál
@ 2017-08-17 21:12         ` Emanuel Berg
  2017-08-18  5:06           ` Csányi Pál
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2017-08-17 21:12 UTC (permalink / raw)
  To: help-gnu-emacs

Csányi Pál wrote:

> It is all the same whether is a <nop> before
> a CamelCase or is not there, the CamelCase
> remain wikilink. I expect that, if a <nop> is
> before a such CamelCase, like <nop>CamelCase
> then it should turn into non-link. Right?
> This turn thing does not works here.

There is nothing specifically about CamelCase
in that function!

Can you give us a piece of code and tell where
point is and what you want to happen when the
function is invoked?

This issue is a bit trivial - why not just
type? - but OK, I sense it has a trivial
solution as well where that function
fails... :)

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-17 21:12         ` Emanuel Berg
@ 2017-08-18  5:06           ` Csányi Pál
  2017-08-19 14:13             ` Emanuel Berg
  0 siblings, 1 reply; 13+ messages in thread
From: Csányi Pál @ 2017-08-18  5:06 UTC (permalink / raw)
  To: help-gnu-emacs

2017-08-17 23:12 GMT+02:00 Emanuel Berg <moasen@zoho.com>:
> Csányi Pál wrote:
>
>> It is all the same whether is a <nop> before
>> a CamelCase or is not there, the CamelCase
>> remain wikilink. I expect that, if a <nop> is
>> before a such CamelCase, like <nop>CamelCase
>> then it should turn into non-link. Right?
>> This turn thing does not works here.
>
> There is nothing specifically about CamelCase
> in that function!
>
> Can you give us a piece of code and tell where
> point is and what you want to happen when the
> function is invoked?

As you know, I am not who wrote this code down, but yason:

Toggle WikiLink on and off

Depending on the type of the text you write, you might be typing a lot
of CamelCase words that are, however, not links. Writing <nop> is
cumbersome after a few iterations, and sometimes you’ll have to remove
it again. Here’s a short function that toggles the current CamelCaps
word between wikilink and non-link:

  (defun emacs-wiki-unlink-toggle ()
    "Toggle <nop> string in the beginning of the current word, to un/make a
    word emacs-wiki link. The current word depends on the point: if the cursor
    is on a non-whitespace character, it's considered a word surrounded by
    whitespace. If the cursor is on a whitespace character, the next word is
    looked up. This way addressing a word works intuitively after having
    arrived on the spot using forward-word."
    (interactive)
    (save-excursion
      (if (looking-at "[[:space:]]")
          (goto-char (- (re-search-forward "[A-Za-z<]") 1))
          (goto-char (+ (re-search-backward "[[:space:]]") 1)))
      (if (looking-at "<nop>")
        (delete-char 5)
        (insert "<nop>"))))

I hook it up to C-n with this:

  (add-hook 'emacs-wiki-mode-hook
    '(lambda () (local-set-key "\C-c\C-n" 'emacs-wiki-unlink-toggle)))

--yason


> This issue is a bit trivial - why not just
> type? - but OK, I sense it has a trivial
> solution as well where that function
> fails... :)

In Wiki Mode if one write down a CamelCase word, it turns
automatically in to wikilink.
I thought the code abowe was for turning off wikilink into non-link in
such case when one want to write down a CamelCase word in Wiki Mode,
but want it not to be a wikilink.

If this code write down by yason does this not, then what is it for??
What is the purpose of the <nop> at the start of such CamelCase word
in Wiki Mode then??

-- 
Best, Pali



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-18  5:06           ` Csányi Pál
@ 2017-08-19 14:13             ` Emanuel Berg
  2017-08-19 14:41               ` Csányi Pál
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2017-08-19 14:13 UTC (permalink / raw)
  To: help-gnu-emacs

Csányi Pál wrote:

> If this code write down by yason does this
> not, then what is it for?? What is the
> purpose of the <nop> at the start of such
> CamelCase word in Wiki Mode then??

I'm not a wiki user (at least not an editor) so
I don't know, the purpose of that ill-written
code tho is a DWIM function to insert/delete
the word "<nop>" depending on the state, i.e.
point (the cursor position) and what text/code
is before or after point.

Because you are not the writer of that code,
which I realized when you posted the link, it
isn't really meaningful for me to help you
improve it.

As for the issue it amends, I would instead
recommend using the everyday Emacs commands and
editing facilities to insert/delete that word.

At the most, one could think of an abbrev that
expands into it, for example "nop" -> "<nop>".
Even so, unless it is your day job editing the
wiki, I'd say that'd be over the top.

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-19 14:13             ` Emanuel Berg
@ 2017-08-19 14:41               ` Csányi Pál
  2017-08-19 14:52                 ` Emanuel Berg
  0 siblings, 1 reply; 13+ messages in thread
From: Csányi Pál @ 2017-08-19 14:41 UTC (permalink / raw)
  To: help-gnu-emacs

2017-08-19 16:13 GMT+02:00 Emanuel Berg <moasen@zoho.com>:
> Csányi Pál wrote:
>
>> If this code write down by yason does this
>> not, then what is it for?? What is the
>> purpose of the <nop> at the start of such
>> CamelCase word in Wiki Mode then??
>
> I'm not a wiki user (at least not an editor) so
> I don't know, the purpose of that ill-written
> code tho is a DWIM function to insert/delete
> the word "<nop>" depending on the state, i.e.
> point (the cursor position) and what text/code
> is before or after point.
>
> Because you are not the writer of that code,
> which I realized when you posted the link, it
> isn't really meaningful for me to help you
> improve it.
>
> As for the issue it amends, I would instead
> recommend using the everyday Emacs commands and
> editing facilities to insert/delete that word.
>
> At the most, one could think of an abbrev that
> expands into it, for example "nop" -> "<nop>".
> Even so, unless it is your day job editing the
> wiki, I'd say that'd be over the top.

OK, thank you for the conversation, and the help.
I can live without Emacs Wiki Mode. Maybe it is better for me to use
Emacs Muse then.

-- 
Best, Pali



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-19 14:41               ` Csányi Pál
@ 2017-08-19 14:52                 ` Emanuel Berg
  2017-08-19 15:15                   ` Csányi Pál
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2017-08-19 14:52 UTC (permalink / raw)
  To: help-gnu-emacs

Csányi Pál wrote:

> OK, thank you for the conversation, and the
> help. I can live without Emacs Wiki Mode.
> Maybe it is better for me to use Emacs
> Muse then.

Well, maybe is it overkill to ditch the entire
mode just because of this detail?

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




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Toggle WikiLink on and off does not work?
  2017-08-19 14:52                 ` Emanuel Berg
@ 2017-08-19 15:15                   ` Csányi Pál
  0 siblings, 0 replies; 13+ messages in thread
From: Csányi Pál @ 2017-08-19 15:15 UTC (permalink / raw)
  To: help-gnu-emacs

2017-08-19 16:52 GMT+02:00 Emanuel Berg <moasen@zoho.com>:
> Csányi Pál wrote:
>
>> OK, thank you for the conversation, and the
>> help. I can live without Emacs Wiki Mode.
>> Maybe it is better for me to use Emacs
>> Muse then.
>
> Well, maybe is it overkill to ditch the entire
> mode just because of this detail?

Believe me this is very important to me.
However, I can live without this detail in Wiki Mode.



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-08-19 15:15 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-16  8:55 Toggle WikiLink on and off does not work? Csányi Pál
2017-08-16 14:50 ` Emanuel Berg
2017-08-16 15:32   ` Emanuel Berg
2017-08-17  8:17   ` Csányi Pál
2017-08-17 11:42     ` Emanuel Berg
2017-08-17 18:53       ` Csányi Pál
2017-08-17 21:12         ` Emanuel Berg
2017-08-18  5:06           ` Csányi Pál
2017-08-19 14:13             ` Emanuel Berg
2017-08-19 14:41               ` Csányi Pál
2017-08-19 14:52                 ` Emanuel Berg
2017-08-19 15:15                   ` Csányi Pál
2017-08-17 11:54     ` Emanuel Berg

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).