emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Electric insert of headline stars
@ 2007-10-25 10:15 Piotr Zielinski
  2007-10-25 10:59 ` Carsten Dominik
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Piotr Zielinski @ 2007-10-25 10:15 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Here's a small piece of elisp code that might be useful to some of you.
Pressing '*' now inserts '*' as before, but if there are only spaces
between the beginning of the current line and the point, then all of
them are converted to stars.  Useful for inserting new headlines.

Longer explanaition: assume you have the following structure:

* first level headline
_* second level headline
__* third level headline

(_ denotes an invisible star) Since stars are invisible, I often find
myself trying to create a new subheadline by just inserting a single
star

* first level headline
_* second level headline
__* third level headline
   *

which of course doesn't normally work, hence this elisp code.

(defun local-org-insert-stars ()
  (interactive)
  (when (looking-back "^ *" (point-at-bol))
    (replace-string " " "*" nil (point-at-bol) (point)))
  (insert "*"))

(define-key org-mode-map "*" 'local-org-insert-stars)

Haven't thoroughly tested it, but it seems to work ok.

Piotr

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

* Re: Electric insert of headline stars
  2007-10-25 10:15 Electric insert of headline stars Piotr Zielinski
@ 2007-10-25 10:59 ` Carsten Dominik
  2007-10-25 11:29 ` Bastien
  2007-10-25 11:53 ` Seweryn Kokot
  2 siblings, 0 replies; 10+ messages in thread
From: Carsten Dominik @ 2007-10-25 10:59 UTC (permalink / raw)
  To: Piotr Zielinski; +Cc: emacs-orgmode


On Oct 25, 2007, at 12:15 PM, Piotr Zielinski wrote:

> Hi,
>
> Here's a small piece of elisp code that might be useful to some of  
> you.
> Pressing '*' now inserts '*' as before, but if there are only spaces
> between the beginning of the current line and the point, then all of
> them are converted to stars.  Useful for inserting new headlines.
>
> Longer explanaition: assume you have the following structure:
>
> * first level headline
> _* second level headline
> __* third level headline
>
> (_ denotes an invisible star) Since stars are invisible, I often find
> myself trying to create a new subheadline by just inserting a single
> star
>
> * first level headline
> _* second level headline
> __* third level headline
>    *
>
> which of course doesn't normally work, hence this elisp code.


hmm, intresting.

>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ *" (point-at-bol))
>     (replace-string " " "*" nil (point-at-bol) (point)))
>   (insert "*"))

like this it will break table alignment.  it is better to use

(org-self-insert-command 1)

     instead of

(insert "*")


- Carsten

>
> (define-key org-mode-map "*" 'local-org-insert-stars)
>
> Haven't thoroughly tested it, but it seems to work ok.
>
> Piotr
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Electric insert of headline stars
  2007-10-25 10:15 Electric insert of headline stars Piotr Zielinski
  2007-10-25 10:59 ` Carsten Dominik
@ 2007-10-25 11:29 ` Bastien
  2007-10-25 11:53 ` Seweryn Kokot
  2 siblings, 0 replies; 10+ messages in thread
From: Bastien @ 2007-10-25 11:29 UTC (permalink / raw)
  To: emacs-orgmode

Hi Piotr,

"Piotr Zielinski" <piotr.zielinski@gmail.com> writes:

> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ *" (point-at-bol))
                           ^
                           ^

This should be "+", otherwise you get a message saying "Replaced 0
occurrence" when you insert a "*" at the beginning of the line.

Also remark that this is not suitable for people using "*" for list
items, although the manual suggests using "*" for list items is not 
a very good idea.

Anyway, i guess this might be useful for some people, thanks!

-- 
Bastien

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

* Re: Electric insert of headline stars
  2007-10-25 10:15 Electric insert of headline stars Piotr Zielinski
  2007-10-25 10:59 ` Carsten Dominik
  2007-10-25 11:29 ` Bastien
@ 2007-10-25 11:53 ` Seweryn Kokot
  2007-10-25 12:11   ` Piotr Zielinski
  2007-10-25 12:14   ` Carsten Dominik
  2 siblings, 2 replies; 10+ messages in thread
From: Seweryn Kokot @ 2007-10-25 11:53 UTC (permalink / raw)
  To: emacs-orgmode

"Piotr Zielinski" <piotr.zielinski@gmail.com> writes:

> which of course doesn't normally work, hence this elisp code.
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ *" (point-at-bol))
>     (replace-string " " "*" nil (point-at-bol) (point)))
>   (insert "*"))
>
> (define-key org-mode-map "*" 'local-org-insert-stars)
>

Very nice idea! 
A minor inconvenience is a warning when compiling the code

.emacs:2604:30:Warning: `replace-string' used from Lisp code
That command is designed for interactive use only. 

How to get rid of this?

regards,
-- 
Seweryn Kokot

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

* Re: Re: Electric insert of headline stars
  2007-10-25 11:53 ` Seweryn Kokot
@ 2007-10-25 12:11   ` Piotr Zielinski
  2007-10-25 12:25     ` Carsten Dominik
                       ` (2 more replies)
  2007-10-25 12:14   ` Carsten Dominik
  1 sibling, 3 replies; 10+ messages in thread
From: Piotr Zielinski @ 2007-10-25 12:11 UTC (permalink / raw)
  To: Seweryn Kokot; +Cc: emacs-orgmode

On 25/10/2007, Seweryn Kokot <s.kokot@po.opole.pl> wrote:

> A minor inconvenience is a warning when compiling the code

First, here's the version after Bastien's and Carsten's comments:

(defun local-org-insert-stars ()
  (interactive)
  (when (looking-back "^ +" (point-at-bol))
    (replace-string " " "*" nil (point-at-bol) (point)))
  (org-self-insert-command 1))

> .emacs:2604:30:Warning: `replace-string' used from Lisp code
> That command is designed for interactive use only.

> How to get rid of this?

This is what I came up with after following the suggestion from the
manual, but it looks complicated to me, so I don't really like it:


(defun local-org-insert-stars ()
  (interactive)
  (when (looking-back "^ +" (point-at-bol))
    (save-excursion
      (while (search-backward " " (point-at-bol) t)
	(replace-match "*" nil t))))
  (org-self-insert-command 1))


Piotr

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

* Re: Re: Electric insert of headline stars
  2007-10-25 11:53 ` Seweryn Kokot
  2007-10-25 12:11   ` Piotr Zielinski
@ 2007-10-25 12:14   ` Carsten Dominik
  1 sibling, 0 replies; 10+ messages in thread
From: Carsten Dominik @ 2007-10-25 12:14 UTC (permalink / raw)
  To: Seweryn Kokot; +Cc: emacs-orgmode


On Oct 25, 2007, at 1:53 PM, Seweryn Kokot wrote:

> "Piotr Zielinski" <piotr.zielinski@gmail.com> writes:
>
>> which of course doesn't normally work, hence this elisp code.
>>
>> (defun local-org-insert-stars ()
>>   (interactive)
>>   (when (looking-back "^ *" (point-at-bol))
>>     (replace-string " " "*" nil (point-at-bol) (point)))
>>   (insert "*"))
>>
>> (define-key org-mode-map "*" 'local-org-insert-stars)
>>
>
> Very nice idea!
> A minor inconvenience is a warning when compiling the code
>
> .emacs:2604:30:Warning: `replace-string' used from Lisp code
> That command is designed for interactive use only.
>
> How to get rid of this?

The docstring of replace-string shows what replacement code should be  
used,
a combination of re-search-forward and replace-match.
Or you can wrap the call into a `with-no-warnings' form.

However, is the code is just in your own .emacs and works well, who  
cares.

- Carsten

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

* Re: Re: Electric insert of headline stars
  2007-10-25 12:11   ` Piotr Zielinski
@ 2007-10-25 12:25     ` Carsten Dominik
  2007-10-25 12:41     ` Seweryn Kokot
  2007-10-25 13:17     ` Bastien
  2 siblings, 0 replies; 10+ messages in thread
From: Carsten Dominik @ 2007-10-25 12:25 UTC (permalink / raw)
  To: Piotr Zielinski; +Cc: Seweryn Kokot, emacs-orgmode


On Oct 25, 2007, at 2:11 PM, Piotr Zielinski wrote:

> On 25/10/2007, Seweryn Kokot <s.kokot@po.opole.pl> wrote:
>
>> A minor inconvenience is a warning when compiling the code
>
> First, here's the version after Bastien's and Carsten's comments:
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ +" (point-at-bol))
>     (replace-string " " "*" nil (point-at-bol) (point)))
>   (org-self-insert-command 1))
>
>> .emacs:2604:30:Warning: `replace-string' used from Lisp code
>> That command is designed for interactive use only.
>
>> How to get rid of this?
>
> This is what I came up with after following the suggestion from the
> manual, but it looks complicated to me, so I don't really like it:
>
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ +" (point-at-bol))
>     (save-excursion
>       (while (search-backward " " (point-at-bol) t)
> 	(replace-match "*" nil t))))
>   (org-self-insert-command 1))
>

Another option would be:

(defun local-org-insert-stars ()
   (interactive)
   (if (looking-back "^ +" (point-at-bol))
       (insert (make-string (prog1 (current-column) (replace-match  
"")) ?*)
	      "* ")
     (org-self-insert-command 1)))

- Carsten

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

* Re: Electric insert of headline stars
  2007-10-25 12:11   ` Piotr Zielinski
  2007-10-25 12:25     ` Carsten Dominik
@ 2007-10-25 12:41     ` Seweryn Kokot
  2007-10-25 13:03       ` Seweryn Kokot
  2007-10-25 13:17     ` Bastien
  2 siblings, 1 reply; 10+ messages in thread
From: Seweryn Kokot @ 2007-10-25 12:41 UTC (permalink / raw)
  To: emacs-orgmode

"Piotr Zielinski" <piotr.zielinski@gmail.com> writes:

> On 25/10/2007, Seweryn Kokot <s.kokot@po.opole.pl> wrote:
>
>> A minor inconvenience is a warning when compiling the code
>
> First, here's the version after Bastien's and Carsten's comments:
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ +" (point-at-bol))
>     (replace-string " " "*" nil (point-at-bol) (point)))
>   (org-self-insert-command 1))
>
>> .emacs:2604:30:Warning: `replace-string' used from Lisp code
>> That command is designed for interactive use only.
>
>> How to get rid of this?
>
> This is what I came up with after following the suggestion from the
> manual, but it looks complicated to me, so I don't really like it:
>
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ +" (point-at-bol))
>     (save-excursion
>       (while (search-backward " " (point-at-bol) t)
> 	(replace-match "*" nil t))))
>   (org-self-insert-command 1))

It seems that the line
"(when (looking-back "^ +" (point-at-bol))"
is not required in the second version. And for me it's better to add and
extra whitespace to directly type a heading name. I don't know if the
line (insert " ") is correct in this case or it should be replaced with
something (org-self-insert-command x)?

(defun ks-org-insert-stars ()
  (interactive)
  (save-excursion 
	(while (search-backward " " (point-at-bol) t)
	  (replace-match "*" nil t)))
  (org-self-insert-command 1)
  (insert " "))

Anyway I prefer the version without a warning and it is politically
correct according to the manual :)

-- 
Seweryn Kokot

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

* Re: Electric insert of headline stars
  2007-10-25 12:41     ` Seweryn Kokot
@ 2007-10-25 13:03       ` Seweryn Kokot
  0 siblings, 0 replies; 10+ messages in thread
From: Seweryn Kokot @ 2007-10-25 13:03 UTC (permalink / raw)
  To: emacs-orgmode

Seweryn Kokot <s.kokot@po.opole.pl> writes:

>>
>> (defun local-org-insert-stars ()
>>   (interactive)
>>   (when (looking-back "^ +" (point-at-bol))
>>     (save-excursion
>>       (while (search-backward " " (point-at-bol) t)
>> 	(replace-match "*" nil t))))
>>   (org-self-insert-command 1))
>
> It seems that the line
> "(when (looking-back "^ +" (point-at-bol))"
> is not required in the second version.
  ^^^^^^^^^^^^^^^ 1.

> And for me it's better to add and
> extra whitespace to directly type a heading name. I don't know if the
> line (insert " ") is correct in this case or it should be replaced with
> something (org-self-insert-command x)?
  ^^^^^^^^^^^^^^^ 2.
>
> (defun ks-org-insert-stars ()
>   (interactive)
>   (save-excursion 
> 	(while (search-backward " " (point-at-bol) t)
> 	  (replace-match "*" nil t)))
>   (org-self-insert-command 1)
>   (insert " "))
>
> Anyway I prefer the version without a warning and it is politically
> correct according to the manual :)

After some testing I withdraw the two things I wrote in my previous
post. Forget it! So I'm staying with the following version

(defun local-org-insert-stars ()
  (interactive)
  (when (looking-back "^ +" (point-at-bol))
  (save-excursion 
	(while (search-backward " " (point-at-bol) t)
	  (replace-match "*" nil t))))
  (org-self-insert-command 1))

-- 
Seweryn Kokot

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

* Re: Re: Electric insert of headline stars
  2007-10-25 12:11   ` Piotr Zielinski
  2007-10-25 12:25     ` Carsten Dominik
  2007-10-25 12:41     ` Seweryn Kokot
@ 2007-10-25 13:17     ` Bastien
  2 siblings, 0 replies; 10+ messages in thread
From: Bastien @ 2007-10-25 13:17 UTC (permalink / raw)
  To: emacs-orgmode

"Piotr Zielinski" <piotr.zielinski@gmail.com> writes:

> This is what I came up with after following the suggestion from the
> manual, but it looks complicated to me, so I don't really like it:
>
> (defun local-org-insert-stars ()
>   (interactive)
>   (when (looking-back "^ +" (point-at-bol))
>     (save-excursion
>       (while (search-backward " " (point-at-bol) t)
> 	(replace-match "*" nil t))))
>   (org-self-insert-command 1))

A bit more complicated but using `replace-match' instead of
`replace-string' has also the advantage of getting rid of the messages.

-- 
Bastien

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

end of thread, other threads:[~2007-10-25 13:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-25 10:15 Electric insert of headline stars Piotr Zielinski
2007-10-25 10:59 ` Carsten Dominik
2007-10-25 11:29 ` Bastien
2007-10-25 11:53 ` Seweryn Kokot
2007-10-25 12:11   ` Piotr Zielinski
2007-10-25 12:25     ` Carsten Dominik
2007-10-25 12:41     ` Seweryn Kokot
2007-10-25 13:03       ` Seweryn Kokot
2007-10-25 13:17     ` Bastien
2007-10-25 12:14   ` Carsten Dominik

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