all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* And while we're at it...
@ 2010-12-01  9:57 Gary
  2010-12-01 11:57 ` Tassilo Horn
  0 siblings, 1 reply; 5+ messages in thread
From: Gary @ 2010-12-01  9:57 UTC (permalink / raw)
  To: help-gnu-emacs

'insert' and friends insert their arguments at the current point. If I'm
at a different point in the file to where I want to actually insert the
text?

-- 
Gary        Please do NOT send me 'courtesy' replies off-list.
GNU Emacs 23.2.1
emacsclient 23.2




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

* Re: And while we're at it...
  2010-12-01  9:57 And while we're at it Gary
@ 2010-12-01 11:57 ` Tassilo Horn
  2010-12-01 13:42   ` Gary
  0 siblings, 1 reply; 5+ messages in thread
From: Tassilo Horn @ 2010-12-01 11:57 UTC (permalink / raw)
  To: help-gnu-emacs

Gary <help-gnu-emacs@garydjones.name> writes:

> 'insert' and friends insert their arguments at the current point. If
> I'm at a different point in the file to where I want to actually
> insert the text?

Then you need to do something like:

  (let ((text (compute-text-to-be-inserted)))
    (save-excursion
      (goto-buffer-position-where-text-should-be-inserted)
      (insert text)))

The `save-excursion' will restore the current position after its body
has been executed.

,----[ C-h f save-excursion RET ]
| save-excursion is a special form in `C source code'.
| 
| (save-excursion &rest BODY)
| 
| Save point, mark, and current buffer; execute BODY; restore those things.
| Executes BODY just like `progn'.
| The values of point, mark and the current buffer are restored
| even in case of abnormal exit (throw or error).
| The state of activation of the mark is also restored.
| 
| This construct does not save `deactivate-mark', and therefore
| functions that change the buffer will still cause deactivation
| of the mark at the end of the command.  To prevent that, bind
| `deactivate-mark' with `let'.
| 
| If you only want to save the current buffer but not point nor mark,
| then just use `save-current-buffer', or even `with-current-buffer'.
`----

Bye,
Tassilo




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

* Re: And while we're at it...
  2010-12-01 11:57 ` Tassilo Horn
@ 2010-12-01 13:42   ` Gary
  2010-12-01 16:19     ` Tassilo Horn
  0 siblings, 1 reply; 5+ messages in thread
From: Gary @ 2010-12-01 13:42 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn wrote:
> Gary writes:
>
>> 'insert' and friends insert their arguments at the current point. If
>> I'm at a different point in the file to where I want to actually
>> insert the text?
...
> something like:
>
>   (let ((text (compute-text-to-be-inserted)))
>     (save-excursion
>       (goto-buffer-position-where-text-should-be-inserted)
>       (insert text)))
>
> The `save-excursion' will restore the current position after its body
> has been executed.

Yeah, I'd read and mentally noted that. What I'm missing is how to
goto-buffer-position-where-text-should-be-inserted, as you put it. I
found backward-line which might help, but I don't know what position in
that line the "point" would then be. End? Beginning? Somewhere in the
middle?

The code I am writing is designed to act on text like this:
,----
| foo somethinghere
|   ...
| end
`----

And here it is, somewhat simplified:
,----
| (defun my-fn ()
|   (interactive)
| 
|   ;; get the components of the current line
|   (setq myStr (thing-at-point 'line))
|   (setq myLineComponents (split-string myStr " "))
| 
|   ;; get the type of "thing" on this line
|   (setq mySemanticObj (pop myLineComponents))
| 
|   ;; ..and based on that, insert something appropriate
|   (if (equal mySemanticObj "foo")
|       (progn
|         (backward-line 1) ;; hmm! where in the line do i end up?!?
|         (my-priv-insert-foo-text (car myLineComponents))
|       )
|   ;;else
|   ;;  ...
| 
|   )
| )
`----
(where my-priv-insert-foo-text takes the next part of the line as an
input param, which in the above example would be "somethinghere", I'm
hoping)

-- 
Gary        Please do NOT send me 'courtesy' replies off-list.
GNU Emacs 23.2.1
emacsclient 23.2




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

* Re: And while we're at it...
  2010-12-01 13:42   ` Gary
@ 2010-12-01 16:19     ` Tassilo Horn
  2010-12-02  7:29       ` Gary
  0 siblings, 1 reply; 5+ messages in thread
From: Tassilo Horn @ 2010-12-01 16:19 UTC (permalink / raw)
  To: help-gnu-emacs

Gary <help-gnu-emacs@garydjones.name> writes:

> Yeah, I'd read and mentally noted that. What I'm missing is how to
> goto-buffer-position-where-text-should-be-inserted, as you put it. I
> found backward-line which might help, but I don't know what position
> in that line the "point" would then be. End? Beginning? Somewhere in
> the middle?

No, don't use that in lisp code.  Instead use `forward-line' with a
negative argument:

,----[ C-h f forward-line RET ]
| forward-line is an interactive built-in function in `C source code'.
| 
| (forward-line &optional N)
| 
| Move N lines forward (backward if N is negative).
| Precisely, if point is on line I, move to the start of line I + N
| ("start of line" in the logical order).
| If there isn't room, go as far as possible (no error).
| Returns the count of lines left to move.  If moving forward,
| that is N - number of lines moved; if backward, N + number moved.
| With positive N, a non-empty line at the end counts as one line
| successfully moved (for the return value).
`----

> The code I am writing is designed to act on text like this:
> ,----
> | foo somethinghere
> |   ...
> | end
> `----
>
> And here it is, somewhat simplified:
> ,----
> | (defun my-fn ()
> |   (interactive)
> | 
> |   ;; get the components of the current line
> |   (setq myStr (thing-at-point 'line))
> |   (setq myLineComponents (split-string myStr " "))

Don't use `setq' for local vars, use `let'.

> |   ;; get the type of "thing" on this line
> |   (setq mySemanticObj (pop myLineComponents))
> | 
> |   ;; ..and based on that, insert something appropriate
> |   (if (equal mySemanticObj "foo")

Don't use `equal' but `string=' when comparing strings.

> |       (progn
> |         (backward-line 1) ;; hmm! where in the line do i end up?!?

Use (forward-line -1) and you'll end up in column zero of the previous
line.

HTH,
Tassilo




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

* Re: And while we're at it...
  2010-12-01 16:19     ` Tassilo Horn
@ 2010-12-02  7:29       ` Gary
  0 siblings, 0 replies; 5+ messages in thread
From: Gary @ 2010-12-02  7:29 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn wrote:
> Gary writes:
>
>> Yeah, I'd read and mentally noted that. What I'm missing is how to
>> goto-buffer-position-where-text-should-be-inserted, as you put it. I
>> found backward-line which might help, but I don't know what position
>> in that line the "point" would then be. End? Beginning? Somewhere in
>> the middle?
>
> No, don't use that in lisp code.  Instead use `forward-line' with a
> negative argument:

Ta!

-- 
Gary        Please do NOT send me 'courtesy' replies off-list.
GNU Emacs 23.2.1
emacsclient 23.2





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

end of thread, other threads:[~2010-12-02  7:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-01  9:57 And while we're at it Gary
2010-12-01 11:57 ` Tassilo Horn
2010-12-01 13:42   ` Gary
2010-12-01 16:19     ` Tassilo Horn
2010-12-02  7:29       ` Gary

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.