emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-store-link without having to press Enter?
@ 2014-01-14 21:05 Sebastien Vauban
  2014-01-14 22:55 ` Nick Dokos
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastien Vauban @ 2014-01-14 21:05 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hello,

I'd like to use org-store-link in a piece of code. However, it currently
requires the user to select some link from a list of store links.

If I always want to select the last one -- that is, the default
option --, how can I bypass the required RET?

I can do this:

  (execute-kbd-macro "\C-c\C-l\C-m")

But I find it fragile because it relies on key bindings which could
change in time.

How could I do otherwise?

Best regards,
  Seb

-- 
Sebastien Vauban

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

* Re: org-store-link without having to press Enter?
  2014-01-14 21:05 org-store-link without having to press Enter? Sebastien Vauban
@ 2014-01-14 22:55 ` Nick Dokos
  2014-01-15  9:24   ` Sebastien Vauban
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Dokos @ 2014-01-14 22:55 UTC (permalink / raw)
  To: emacs-orgmode

"Sebastien Vauban" <sva-news@mygooglest.com>
writes:

> Hello,
>
> I'd like to use org-store-link in a piece of code. However, it currently
> requires the user to select some link from a list of store links.
>

I think you mean org-insert-link, right?

> If I always want to select the last one -- that is, the default
> option --, how can I bypass the required RET?
>
> I can do this:
>
>   (execute-kbd-macro "\C-c\C-l\C-m")
>
> But I find it fragile because it relies on key bindings which could
> change in time.
>
> How could I do otherwise?
>

I don't know if there is an API, but org-store-link just adds the link
info to the front of the list org-stored-links, so the last link added
is (car org-stored-links).  Each element of org-stored-links is a
two-element list (link description). Both of these are strings (possibly
strings with text properties).

So something like this might work:

    (let ((link (car org-stored-links)))
         (insert (format "[[%s][%s]]" (car link) (cadr link))))

at least for now... It's imo "better" than the keyboard macro but it's
still pretty low level and ugly.

Nick

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

* Re: org-store-link without having to press Enter?
  2014-01-14 22:55 ` Nick Dokos
@ 2014-01-15  9:24   ` Sebastien Vauban
  2014-01-15 16:44     ` Bastien
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastien Vauban @ 2014-01-15  9:24 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hello Nick,

Nick Dokos wrote:
> Sebastien Vauban writes:
>>
>> I'd like to use org-store-link in a piece of code. However, it currently
>> requires the user to select some link from a list of store links.
>
> I think you mean org-insert-link, right?

Yes; you're right!

>> If I always want to select the last one -- that is, the default
>> option --, how can I bypass the required RET?
>>
>> I can do this:
>>
>>   (execute-kbd-macro "\C-c\C-l\C-m")
>>
>> But I find it fragile because it relies on key bindings which could
>> change in time.
>>
>> How could I do otherwise?
>
> I don't know if there is an API, but org-store-link just adds the link
> info to the front of the list org-stored-links, so the last link added
> is (car org-stored-links). Each element of org-stored-links is
> a two-element list (link description). Both of these are strings
> (possibly strings with text properties).
>
> So something like this might work:
>
>     (let ((link (car org-stored-links)))
>          (insert (format "[[%s][%s]]" (car link) (cadr link))))
>
> at least for now... It's imo "better" than the keyboard macro but it's
> still pretty low level and ugly.

It does work. THANKS! -- I prefer it over my own version.

I thought there could exist some way to (pre-)send the input to be read
when prompted by a function. It seems not (I guess you'd have mentioned
it otherwise...).

I wonder as well how I could do the `execute-kbd-macro' if there wasn't
a key bound to the command I'm interested in. I tried things like the
following, with no success:

     (execute-kbd-macro "M-x org-insert-link\C-m")

Best regards,
  Seb

-- 
Sebastien Vauban

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

* Re: org-store-link without having to press Enter?
  2014-01-15  9:24   ` Sebastien Vauban
@ 2014-01-15 16:44     ` Bastien
  2014-01-15 17:22       ` Nick Dokos
  2014-02-18 13:50       ` Sebastien Vauban
  0 siblings, 2 replies; 8+ messages in thread
From: Bastien @ 2014-01-15 16:44 UTC (permalink / raw)
  To: Sebastien Vauban; +Cc: public-emacs-orgmode-mXXj517/zsQ

[-- Attachment #1: Type: text/plain, Size: 788 bytes --]

Hi Sébastien,

please test the attached patch against master.

It creates a new command `org-insert-last-stored-link'
bound to `C-c M-l'.

You can use `C-2 C-c M-l' to insert the last two links.

So the set of commands around inserting links would be:

  C-c l       => store link (the suggested user binding)
C-c C-l       => insert a link
C-c C-M-l     => insert all links as a list
C-u C-c C-M-l => insert all links as a list and keep them
C-1 C-c C-M-l => insert the last stored link
      C-c M-l => short for the previous keybinding

I also find myself in this workflow:

1. collect various links through a session
2. store the last one for a new task
3. dump all links into some "read later" heading

so I think this feature deserves to be in core.

What do you and others think?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-insert-all-links.patch --]
[-- Type: text/x-diff, Size: 2044 bytes --]

diff --git a/lisp/org.el b/lisp/org.el
index ecd84e9..85e7ce5 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9930,14 +9930,29 @@ This command can be called in any mode to insert a link in Org-mode syntax."
   (org-load-modules-maybe)
   (org-run-like-in-org-mode 'org-insert-link))
 
-(defun org-insert-all-links (&optional keep)
-  "Insert all links in `org-stored-links'."
+(defun org-insert-all-links (&optional keep not-as-list-item)
+  "Insert all links in `org-stored-links'.
+When `keep' is non-nil, do not delete then link from `org-stored-links'.
+When `not-as-list-item', insert the link directly, not as a list item."
   (interactive "P")
-  (let ((links (copy-sequence org-stored-links)) l)
-    (while (setq l (if keep (pop links) (pop org-stored-links)))
-      (insert "- ")
+  (let ((org-keep-stored-link-after-insertion (equal keep '(4)))
+	(links (copy-seq org-stored-links))
+	(cnt 1) l)
+    (if (null org-stored-links)
+	(message "No link to insert")
+    (while (and (or (listp keep) (>= keep cnt))
+		(setq l (if (listp keep)
+			    (pop links)
+		      	(pop org-stored-links))))
+      (setq cnt (1+ cnt))
+      (unless not-as-list-item (insert "- "))
       (org-insert-link nil (car l) (or (cadr l) "<no description>"))
-      (insert "\n"))))
+      (unless not-as-list-item (insert "\n"))))))
+
+(defun org-insert-last-stored-link (arg)
+  "Insert the last link stored in `org-stored-links'."
+  (interactive "p")
+  (org-insert-all-links arg t))
 
 (defun org-link-fontify-links-to-this-file ()
   "Fontify links to the current file in `org-stored-links'."
@@ -19198,6 +19213,7 @@ boundaries."
 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
+(org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
 (org-defkey org-mode-map "\C-c%"    'org-mark-ring-push)

[-- Attachment #3: Type: text/plain, Size: 14 bytes --]


-- 
 Bastien

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

* Re: org-store-link without having to press Enter?
  2014-01-15 16:44     ` Bastien
@ 2014-01-15 17:22       ` Nick Dokos
  2014-01-17  9:17         ` Bastien
  2014-01-20 14:38         ` Bastien
  2014-02-18 13:50       ` Sebastien Vauban
  1 sibling, 2 replies; 8+ messages in thread
From: Nick Dokos @ 2014-01-15 17:22 UTC (permalink / raw)
  To: emacs-orgmode

Bastien <bzg@gnu.org> writes:

> 1. collect various links through a session
> 2. store the last one for a new task
> 3. dump all links into some "read later" heading
>
> so I think this feature deserves to be in core.
>
> What do you and others think?
>

Much better!

>
> diff --git a/lisp/org.el b/lisp/org.el
> index ecd84e9..85e7ce5 100644
> --- a/lisp/org.el
> +++ b/lisp/org.el
> @@ -9930,14 +9930,29 @@ This command can be called in any mode to insert a link in Org-mode syntax."
>    (org-load-modules-maybe)
>    (org-run-like-in-org-mode 'org-insert-link))
>  
> -(defun org-insert-all-links (&optional keep)
> -  "Insert all links in `org-stored-links'."
> +(defun org-insert-all-links (&optional keep not-as-list-item)
> +  "Insert all links in `org-stored-links'.
> +When `keep' is non-nil, do not delete then link from `org-stored-links'.
                                         ^^^the
> +When `not-as-list-item', insert the link directly, not as a list item."
>    (interactive "P")
> -  (let ((links (copy-sequence org-stored-links)) l)
> -    (while (setq l (if keep (pop links) (pop org-stored-links)))
> -      (insert "- ")
> +  (let ((org-keep-stored-link-after-insertion (equal keep '(4)))
> +	(links (copy-seq org-stored-links))
> +	(cnt 1) l)
> +    (if (null org-stored-links)
> +	(message "No link to insert")
> +    (while (and (or (listp keep) (>= keep cnt))
> +		(setq l (if (listp keep)
> +			    (pop links)
> +		      	(pop org-stored-links))))
> +      (setq cnt (1+ cnt))
> +      (unless not-as-list-item (insert "- "))
>        (org-insert-link nil (car l) (or (cadr l) "<no description>"))
> -      (insert "\n"))))
> +      (unless not-as-list-item (insert "\n"))))))
> +
> +(defun org-insert-last-stored-link (arg)
> +  "Insert the last link stored in `org-stored-links'."

      Not sure whether it's worth adding a comment here: I had to experiment a bit to
      convince myself that an interactive call without a prefix arg passed 1
      as the value of arg. I found it unclear but that may be just my ignorance.
      
> +  (interactive "p")
> +  (org-insert-all-links arg t))
>  
>  (defun org-link-fontify-links-to-this-file ()
>    "Fontify links to the current file in `org-stored-links'."
> @@ -19198,6 +19213,7 @@ boundaries."
>  (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
>  (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
>  (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
> +(org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
>  (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
>  (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
>  (org-defkey org-mode-map "\C-c%"    'org-mark-ring-push)

Nick

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

* Re: org-store-link without having to press Enter?
  2014-01-15 17:22       ` Nick Dokos
@ 2014-01-17  9:17         ` Bastien
  2014-01-20 14:38         ` Bastien
  1 sibling, 0 replies; 8+ messages in thread
From: Bastien @ 2014-01-17  9:17 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Nick Dokos <ndokos@gmail.com> writes:

> Bastien <bzg@gnu.org> writes:
>
>> 1. collect various links through a session
>> 2. store the last one for a new task
>> 3. dump all links into some "read later" heading
>>
>> so I think this feature deserves to be in core.
>>
>> What do you and others think?
>>
>
> Much better!

Thanks -- and thanks for fixing the typos in the patch.

I'll wait for Sébastien's feedback on this and install the
patch in master if it fits his needs.

>> +(defun org-insert-last-stored-link (arg)
>> +  "Insert the last link stored in `org-stored-links'."
>
>       Not sure whether it's worth adding a comment here: I had to experiment a bit to
>       convince myself that an interactive call without a prefix arg passed 1
>       as the value of arg. I found it unclear but that may be just
>       my ignorance.

My (highly subjective) stand on this: people who will not read the
code don't need to understand that the default ARG value will be 1,
those who want to read the code also want to make the extra effort
to understand why "p" makes the ARG default value to 1.  This is
fairly standard Elisp code :)

-- 
 Bastien

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

* Re: org-store-link without having to press Enter?
  2014-01-15 17:22       ` Nick Dokos
  2014-01-17  9:17         ` Bastien
@ 2014-01-20 14:38         ` Bastien
  1 sibling, 0 replies; 8+ messages in thread
From: Bastien @ 2014-01-20 14:38 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Nick Dokos <ndokos@gmail.com> writes:

>> What do you and others think?
>
> Much better!

I've now pushed this patch in master.

Sébastien, please test C-c M-l to insert the last stored link and let
us know what you think.

Thanks,

-- 
 Bastien

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

* Re: org-store-link without having to press Enter?
  2014-01-15 16:44     ` Bastien
  2014-01-15 17:22       ` Nick Dokos
@ 2014-02-18 13:50       ` Sebastien Vauban
  1 sibling, 0 replies; 8+ messages in thread
From: Sebastien Vauban @ 2014-02-18 13:50 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi Bastien,

Bastien wrote:
> please test the attached patch against master.

This fills my needs. Thanks!

> It creates a new command `org-insert-last-stored-link'
> bound to `C-c M-l'.
>
> You can use `C-2 C-c M-l' to insert the last two links.
>
> So the set of commands around inserting links would be:
>
>   C-c l       => store link (the suggested user binding)
> C-c C-l       => insert a link
> C-c C-M-l     => insert all links as a list
> C-u C-c C-M-l => insert all links as a list and keep them
> C-1 C-c C-M-l => insert the last stored link
>       C-c M-l => short for the previous keybinding
>
> I also find myself in this workflow:
>
> 1. collect various links through a session
> 2. store the last one for a new task
> 3. dump all links into some "read later" heading
>
> so I think this feature deserves to be in core.
>
> What do you and others think?

Best regards,
  Seb

-- 
Sebastien Vauban

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

end of thread, other threads:[~2014-02-18 13:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-14 21:05 org-store-link without having to press Enter? Sebastien Vauban
2014-01-14 22:55 ` Nick Dokos
2014-01-15  9:24   ` Sebastien Vauban
2014-01-15 16:44     ` Bastien
2014-01-15 17:22       ` Nick Dokos
2014-01-17  9:17         ` Bastien
2014-01-20 14:38         ` Bastien
2014-02-18 13:50       ` Sebastien Vauban

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.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).