emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* mark parent element?
@ 2014-03-14 15:42 Matt Price
  2014-03-14 17:11 ` Thorsten Jolitz
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Matt Price @ 2014-03-14 15:42 UTC (permalink / raw)
  To: Org Mode

Hi,

I'm trying to write a function that will mark the parent of the
current element.  I think I understand how to do it but for some
reason I can get the mark to persist after the funciton is called.  I
think it's really an elisp problem, not an org problem, but am hoping
someone can ehelp me.  Here's what I have:

(defun er/mark-org-parent-element ()
  "Marks an org parent element"
  (interactive)
  (let ((parent (plist-get (car (cdr (org-element-at-point))) :parent)))
    (let ((parent-props (car (cdr parent))))
      ;; (print parent-props)
      ;; (print (plist-get parent-props :begin))
      ;; (print (plist-get parent-props :end))
      (if (plist-get parent-props :begin)
          (progn
            (goto-char (plist-get parent-props :begin))
            (set-mark (point))
            (goto-char (plist-get parent-props :end))
            (exchange-point-and-mark)
            )))
    )
)

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

* Re: mark parent element?
  2014-03-14 15:42 mark parent element? Matt Price
@ 2014-03-14 17:11 ` Thorsten Jolitz
  2014-03-14 17:57 ` Oleh
  2014-03-14 18:11 ` Nicolas Goaziou
  2 siblings, 0 replies; 5+ messages in thread
From: Thorsten Jolitz @ 2014-03-14 17:11 UTC (permalink / raw)
  To: emacs-orgmode

Matt Price <moptop99@gmail.com> writes:

> Hi,
>
> I'm trying to write a function that will mark the parent of the
> current element.  I think I understand how to do it but for some
> reason I can get the mark to persist after the funciton is called.  I
> think it's really an elisp problem, not an org problem, but am hoping
> someone can ehelp me.  Here's what I have:
>
> (defun er/mark-org-parent-element ()
>   "Marks an org parent element"
>   (interactive)
>   (let ((parent (plist-get (car (cdr (org-element-at-point))) :parent)))
>     (let ((parent-props (car (cdr parent))))
>       ;; (print parent-props)
>       ;; (print (plist-get parent-props :begin))
>       ;; (print (plist-get parent-props :end))
>       (if (plist-get parent-props :begin)
>           (progn
>             (goto-char (plist-get parent-props :begin))
>             (set-mark (point))
>             (goto-char (plist-get parent-props :end))
>             (exchange-point-and-mark)
>             )))
>     )
> )

the set-mark doc give two hints in my eyes:

,-----------------------------------------------------------------------
| set-mark is a compiled Lisp function in `simple.el'.
| 
| (set-mark POS)
| 
| Set this buffer's mark to POS.  Don't use this function!
| That is to say, don't use this function unless you want
| the user to see that the mark has moved, and you want the previous
| mark position to be lost.
| 
| Normally, when a new mark is set, the old one should go on the stack.
| This is why most applications should use `push-mark', not `set-mark'.
| 
| Novice Emacs Lisp programmers often try to use the mark for the wrong
| purposes.  The mark saves a location for the user's convenience.
| Most editing commands should not alter the mark.
| To remember a location for internal use in the Lisp program,
| store it in a Lisp variable.  Example:
| 
|    (let ((beg (point))) (forward-line 1) (delete-region beg (point))).
`-----------------------------------------------------------------------

 1. maybe try push-mark instead of set-mark?  
 
 2. maybe avoid marks completely if you just want make your program act
 on a region


-- 
cheers,
Thorsten

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

* Re: mark parent element?
  2014-03-14 15:42 mark parent element? Matt Price
  2014-03-14 17:11 ` Thorsten Jolitz
@ 2014-03-14 17:57 ` Oleh
  2014-03-14 18:11 ` Nicolas Goaziou
  2 siblings, 0 replies; 5+ messages in thread
From: Oleh @ 2014-03-14 17:57 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode

Hi Matt,

> I'm trying to write a function that will mark the parent of the
> current element.  I think I understand how to do it but for some
> reason I can get the mark to persist after the funciton is called.  I
> think it's really an elisp problem, not an org problem, but am hoping
> someone can ehelp me.  Here's what I have:
>
> (defun er/mark-org-parent-element ()
>   "Marks an org parent element"
>   (interactive)
>   (let ((parent (plist-get (car (cdr (org-element-at-point))) :parent)))
>     (let ((parent-props (car (cdr parent))))
>       ;; (print parent-props)
>       ;; (print (plist-get parent-props :begin))
>       ;; (print (plist-get parent-props :end))
>       (if (plist-get parent-props :begin)
>           (progn
>             (goto-char (plist-get parent-props :begin))
>             (set-mark (point))
>             (goto-char (plist-get parent-props :end))
>             (exchange-point-and-mark)
>             )))
>     )
> )

The probelm here is that `(org-element-at-point)` doesn't always have
a parent. I'm using plain outline functions for this purpose.
See `worf-mark-left' at https://github.com/abo-abo/worf/blob/master/worf.el.

regards,
Oleh

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

* Re: mark parent element?
  2014-03-14 15:42 mark parent element? Matt Price
  2014-03-14 17:11 ` Thorsten Jolitz
  2014-03-14 17:57 ` Oleh
@ 2014-03-14 18:11 ` Nicolas Goaziou
  2014-03-15  2:06   ` Matt Price
  2 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2014-03-14 18:11 UTC (permalink / raw)
  To: Matt Price; +Cc: Org Mode

Hello,

Matt Price <moptop99@gmail.com> writes:

> I'm trying to write a function that will mark the parent of the
> current element.  I think I understand how to do it but for some
> reason I can get the mark to persist after the funciton is called.  I
> think it's really an elisp problem, not an org problem, but am hoping
> someone can ehelp me.  Here's what I have:
>
> (defun er/mark-org-parent-element ()
>   "Marks an org parent element"
>   (interactive)
>   (let ((parent (plist-get (car (cdr (org-element-at-point))) :parent)))

It is better to use (org-element-property :parent (org-element-at-point))

>     (let ((parent-props (car (cdr parent))))

You shouldn't extract properties this way. See below.

>       ;; (print parent-props)
>       ;; (print (plist-get parent-props :begin))
>       ;; (print (plist-get parent-props :end))
>       (if (plist-get parent-props :begin)
>           (progn
>             (goto-char (plist-get parent-props :begin))

It is better to use (org-element-property :begin parent)

>             (set-mark (point))
>             (goto-char (plist-get parent-props :end))

It is better to use (org-element-property :end parent)

Anyway, I suggest to use `org-up-element' and `org-mark-element'
instead.


Regards,

-- 
Nicolas Goaziou

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

* Re: mark parent element?
  2014-03-14 18:11 ` Nicolas Goaziou
@ 2014-03-15  2:06   ` Matt Price
  0 siblings, 0 replies; 5+ messages in thread
From: Matt Price @ 2014-03-15  2:06 UTC (permalink / raw)
  To: Org Mode

On Fri, Mar 14, 2014 at 2:11 PM, Nicolas Goaziou <n.goaziou@gmail.com> wrote:
> Hello,
>
> Matt Price <moptop99@gmail.com> writes:
>
>> I'm trying to write a function that will mark the parent of the
>> current element.  I think I understand how to do it but for some
>> reason I can get the mark to persist after the funciton is called.  I
>> think it's really an elisp problem, not an org problem, but am hoping
>> someone can ehelp me.  Here's what I have:
>>
>> (defun er/mark-org-parent-element ()
>>   "Marks an org parent element"
>>   (interactive)
>>   (let ((parent (plist-get (car (cdr (org-element-at-point))) :parent)))
>
> It is better to use (org-element-property :parent (org-element-at-point))
>
>>     (let ((parent-props (car (cdr parent))))
>
> You shouldn't extract properties this way. See below.
>
>>       ;; (print parent-props)
>>       ;; (print (plist-get parent-props :begin))
>>       ;; (print (plist-get parent-props :end))
>>       (if (plist-get parent-props :begin)
>>           (progn
>>             (goto-char (plist-get parent-props :begin))
>
> It is better to use (org-element-property :begin parent)
>
>>             (set-mark (point))
>>             (goto-char (plist-get parent-props :end))
>
> It is better to use (org-element-property :end parent)
>
> Anyway, I suggest to use `org-up-element' and `org-mark-element'

I am a little stunned by how awesome this is. it was worth not
noticing org-up-element in the docs to learn the proper way to address
org-element properties.  Thank you Nicolas!  And everyone else too of
course.

Matt



> instead.
>
>
> Regards,
>
> --
> Nicolas Goaziou

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

end of thread, other threads:[~2014-03-15  2:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-14 15:42 mark parent element? Matt Price
2014-03-14 17:11 ` Thorsten Jolitz
2014-03-14 17:57 ` Oleh
2014-03-14 18:11 ` Nicolas Goaziou
2014-03-15  2:06   ` Matt Price

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