【问题标题】:Is it possible to pass variables into an Emacs Org-Mode capture?是否可以将变量传递到 Emacs Org-Mode 捕获中?
【发布时间】:2013-11-15 18:56:34
【问题描述】:

我在 Windows 上使用 emacs-24.3,特别是 Org-Mode,这太棒了。我正在尝试为在工作中遇到的缺陷设置一个新的捕获模板,并希望模板输出如下内容:

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

例如

  • TODO 用户在登录站点 COM-19112 时遇到问题

我遇到的问题是 URL 类似于 http://www.jira.com/browse/COM-19112,我希望它输出为 COM-19112。要在 emacs 中正常执行此操作,我会这样做:

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

但是,在尝试设置组织捕获模板时,我只想输入一次 COM-19112,然后在两个位置都填写它。这是迄今为止我能做到的最好的——它给了我想要的东西,但我必须在提示符下输入两次“COM-19112”:

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

我在http://orgmode.org/manual/Template-expansion.html#fn-2 上看不到任何解释如何创建可在多个地方使用的变量的内容,但我确信有办法做到这一点。

如果有人能指出我正确的方向,我将不胜感激。

问候

BebopSong

【问题讨论】:

    标签: emacs org-mode


    【解决方案1】:

    template expansion 上的页面确实包含有关如何重新插入现有变量的信息。

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

    假设你的模板如上:

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

    你可以替换成

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

    您可能需要根据定义的其他变量更改%\\1。 (由于\ 是转义字符,它确实需要双反斜杠才能工作)

    【讨论】:

    • 谢谢乔纳森。我误解了页面上的解释,所以感谢您指出我的错误。
    【解决方案2】:

    我有一个类似的案例,我使用类似于以下的代码:

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

    在此定义之后,您可以将[[google:stackoverflow]] 之类的链接放入您的组织文件中。 您只需要定义自己的 org-open-google 函数,然后可以将其命名为 org-open-defect 或者您喜欢它。您必须将此名称添加到列表org-open-at-point-functions

    我为你做了一些修改。因此,上面的代码没有经过大量测试。但是,我已经做了一些基本的测试。

    如果前缀始终是 COM-,您已经可以将其作为 org-at-special 的类型,然后像 [[COM-19112]] 这样的链接将被识别,并且您拥有所需的显示。您的特殊情况将是这样的:

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

    然后像下面这样的捕获就足够了

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

    因此,您只需输入一次缺陷编号。

    【讨论】:

    • 为什么不使用 org-link-abbrev-alist 作为您的自定义链接?
    • @user2708138 - 我的 org-open-at-point-functions 列表有问题。我的 emacs 文件不会加载说变量是无效的。
    • @fniessen - 感谢您向我展示这一点。我将来会使用那些有用的链接缩写,但是他们没有回答我的问题,因为我必须在标签和描述中写下缺陷编号才能获得真正的链接。否则我会得到一个看起来像这样的链接:jira:COM-19912。
    • @BebopSong:M-x org-version == 8.0.5 变量org-open-at-point-functionsorg.el 中定义。这意味着(require 'org) 在上述所有内容之上应该可以工作。我假设您在初始化中已经有了类似的东西。也许,您只需要更改顺序即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2011-05-04
    相关资源
    最近更新 更多