2
votes

I'm using emacs-24.3 for Windows and specifically Org-Mode, which is fantastic. I'm trying to setup a new capture template for defects that get handed me at work and would like the template to output something like this:

  • TODO description-of-defect link-to-defect-in-jira

e.g.

  • TODO User having issue logging in to site COM-19112

The problem I am having is The URL is similar to this http://www.jira.com/browse/COM-19112 and I want it to be output as COM-19112. To do this normally in emacs I would do this:

[[http://www.jira.com/browse/COM-19112][COM-19112]]

However, when trying to setup an org capture template I only want to have to type COM-19112 once and it be filled in both places. Here is the best I can get so far - it gives me what I want but I have to type 'COM-19112' twice at prompt:

(setq org-capture-templates
  '(("d" "Defects" entry (file+headline "C:/EmacsOrg/Defects.org" "Tasks")
      "* TODO %? [[http://www.jira.com/browse/%^{Defect}][%^{Defect}]]")))

I can't see anything on http://orgmode.org/manual/Template-expansion.html#fn-2 that explains how to create a variable that can be used in multiple places but I am sure there is a way to do this.

If anyone can point me in the right direction I'd be very grateful.

Regards

BebopSong

2

2 Answers

5
votes

The page on template expansion does include information on how to re-insert an existing variable.

 %\n         Insert the text entered at the nth %^{prompt}, where n is
             a number, starting from 1.

Assuming your template is as above:

(setq org-capture-templates
      '(("d" "Defects" entry (file+headline "C:/EmacsOrg/Defects.org" "Tasks")
      "* TODO %? [[http://www.jira.com/browse/%^{Defect}][%^{Defect}]]")))

You can replace it with

(setq org-capture-templates
      '(("d" "Defects" entry (file+headline "C:/EmacsOrg/Defects.org" "Tasks")
      "* TODO %? [[http://www.jira.com/browse/%^{Defect}][%\\1]]")))

You may need to change the %\\1 depending on what other variables are being defined. (It does require the double backslash to work since \ is an escape character)

2
votes

I have a similar case for which I use code similar to the following:

(defun org-at-special (type)
  "Check whether point is at a special link of the form [[TYPE:address]]
TYPE is given as string.
Returns the address."
  (save-excursion
    (when (and (looking-back (concat "\\[\\[\\(?:" type "\\([^][\n\r]+\\)?\\]\\[\\)?[^]]*\\(\\]\\)?"))
           (goto-char (match-beginning 0))
           (looking-at (concat "\\[\\[" type "\\([^][\n\r]+\\)\\]")))
      (match-string-no-properties 1))))

(require 'browse-url)

(defun org-open-google ()
  (let ((q (org-at-special "google:")))
    (when q
      (apply browse-url-browser-function  (list (concat "http://www.google.com/#q=" q)))
      t)))

(add-to-list 'org-open-at-point-functions 'org-open-google)

After this definition you can put links like [[google:stackoverflow]] into your org-file. You only need to define your own org-open-google function which then may be named org-open-defect or however you like it. You have to add this name to the list org-open-at-point-functions.

I did some modifications for you. Therefore, the above code is not heavily tested. But, I did already some basic tests.

If the prefix is always COM- you can already take this as type for org-at-special then links like [[COM-19112]] are reckognized and you have exactly the display you want. Your special case would then be something like that:

(defun org-open-COM- ()
  (let ((q (org-at-special "COM-")))
    (when q
      (apply browse-url-browser-function  (list (concat "http://www.jira.com/browse/COM-" q)))
      t)))

(add-to-list 'org-open-at-point-functions 'org-open-COM-)

After that a capture like the following is sufficient

(setq org-capture-templates
  '(("d" "Defects" entry (file+headline "/c/temp/Defects.org" "Tasks")
      "* TODO %? [[COM-%^{Defect}]]")))

Therewith, you only need to input the defect number once.