help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem positioning cursor


From: Victor Kirk
Subject: Problem positioning cursor
Date: Fri, 25 Apr 2003 12:01:26 +0100

Hi,

I've wrote a function to insert a skeleton for a java try-catch
block, but I've run into a problem leaving the cursor in the
correct place.

If there is no active region I want the following code to be
inserted (note X marks the spot I want to leave the cursor at.

    try {
        X
    } catch(Exception e) {
        e.printStackTrace();
    } 

This works fine.

If there is code selected I want:

    try {
        someCode(toBeWrapped);
    } catch(Exception e) {
        e.printStackTrace();
        X
    } 

unfortunatly the cursor is left at the `n' in Exception.

If anyone has the time to point me in the right direction
it would be appreciated.


(defun vics-java-catch-insert(&optional has-finally)
  "Generate a skeleton for a java try-catch block. If the optional
has-finally is true then a finally block is also inserted."
  (interactive)
  (let ((ex-type (read-from-minibuffer "Exception Type: " "Exception" nil
nil nil nil nil))
                (try-start)    ;; start of try catch block
                (try-end)      ;; end of 
                (try-body nil) ;; code to insert within try/catch
                (edit-point))  ;; point to leave user at
        ;; If a region is selected save this to try-body
        (if (c-region-is-active-p)
                (setq try-body (delete-and-extract-region (mark) (point))))
        (setq try-start (point))
        (insert "try {\n")
        (if (not try-body)
                (setq edit-point (point))
          (insert try-body))
        (insert (format "\n} catch (%s e) {\n" ex-type))
        (insert "e.printStackTrace();\n")
        ;; if we had some text leave save location at the end of the catch
        ;; block
        (if (eq nil (not try-body))
                (setq edit-point (point)))
        (insert "}")    ;
        (if (eq nil (not has-finally))
                (insert " finally {\n\n}"))
        (setq try-end (point))
        (indent-region try-start try-end nil)
        (goto-char edit-point)
        (c-indent-command)))



Vic
--
Victor Kirk
Analyst
Serco Integrated Transport
--

This message, including attachments, is intended only for the use by the
person(s) to whom it is addressed. It may contain information which is
privileged and confidential. Copying or use by anybody else is not
authorised. If you are not the intended recipient, please contact the sender
as soon as possible. The views expressed in this communication may not
necessarily be the views held by Serco Integrated Transport.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]