2014-05-17 10 views
5

ऑर्गमोड में, का एक तरीका है जो एक विशिष्ट टैग के उप-मिलान मिलान (या मिलान नहीं) में केवल ब्लॉक है?ऑर्गमोड: ब्लॉक को टेंगल करने के लिए फ़िल्टर कैसे करें?

निम्नलिखित कोड

* A 
#+BEGIN_SRC c 
    printf("Not exported"); 
#+END_SRC 

* B    :D: 

#+BEGIN_SRC c 
    printf("Exported"); 
#+END_SRC 

टैग डी साथ निर्यात के साथ

उदाहरण के लिए, उलझन सी फ़ाइल केवल printf("Exported");

मैं अपने Emacs config व्यवस्थित करने के लिए org-mode उपयोग कर रहा हूँ, और मेरा लक्ष्य होता है को मास्टर एक emacs-config.org से अलग कॉन्फ़िगरेशन प्राप्त करें। (उदाहरण के लिए केवल विशिष्ट को चिह्नित करके एक लाइट कॉन्फिग)

उत्तर

2

मैंने कुछ समय पहले शोध करने की कोशिश की और मुझे कोई त्वरित उत्तर नहीं मिला। मैंने इस कार्यक्षमता को लागू करने के लिए org-babel-tangle-collect-blocks को संशोधित करना समाप्त किया

यहां संशोधित फ़ंक्शन है। सूची org-babel-tags ठीक टैग की एक सूची है। आपके उदाहरण के लिए, आप (setq org-babel-tags '("D"))

साथ यह निर्धारित करने की आवश्यकता

(defvar org-babel-tags nil 
    "only tangle entries that has a tag in this list") 

(defun org-babel-tangle-collect-blocks (&optional language) 
    "Collect source blocks in the current Org-mode file. 
Return an association list of source-code block specifications of 
the form used by `org-babel-spec-to-string' grouped by language. 
Optional argument LANG can be used to limit the collected source 
code blocks by language." 
    (let ((block-counter 1) (current-heading "") blocks) 
    (org-babel-map-src-blocks (buffer-file-name) 
     ((lambda (new-heading) 
     (if (not (string= new-heading current-heading)) 
      (progn 
       (setq block-counter 1) 
       (setq current-heading new-heading)) 
      (setq block-counter (+ 1 block-counter)))) 
     (replace-regexp-in-string "[ \t]" "-" 
           (condition-case nil 
            (or (nth 4 (org-heading-components)) 
             "(dummy for heading without text)") 
            (error (buffer-file-name))))) 
     (let* ((start-line (save-restriction (widen) 
              (+ 1 (line-number-at-pos (point))))) 
      (file (buffer-file-name)) 
      (info (org-babel-get-src-block-info 'light)) 
      (src-lang (nth 0 info))) 
     (unless (or (string= (cdr (assoc :tangle (nth 2 info))) "no") 
        (null (intersection (mapcar 'intern org-babel-tags) 
             (save-excursion 
              (org-back-to-heading) 
              (mapcar 'intern (org-get-tags)))))) 

        (unless (and language (not (string= language src-lang))) 
         (let* ((info (org-babel-get-src-block-info)) 
          (params (nth 2 info)) 
          (extra (nth 3 info)) 
          (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra) 
               (match-string 1 extra)) 
              org-coderef-label-format)) 
          (link ((lambda (link) 
             (and (string-match org-bracket-link-regexp link) 
              (match-string 1 link))) 
            (org-no-properties 
            (org-store-link nil)))) 
          (source-name 
           (intern (or (nth 4 info) 
              (format "%s:%d" 
                current-heading block-counter)))) 
          (expand-cmd 
           (intern (concat "org-babel-expand-body:" src-lang))) 
          (assignments-cmd 
           (intern (concat "org-babel-variable-assignments:" src-lang))) 
          (body 
           ((lambda (body) ;; run the tangle-body-hook 
           (with-temp-buffer 
            (insert body) 
            (when (string-match "-r" extra) 
            (goto-char (point-min)) 
            (while (re-search-forward 
              (replace-regexp-in-string "%s" ".+" cref-fmt) nil t) 
             (replace-match ""))) 
            (run-hooks 'org-babel-tangle-body-hook) 
            (buffer-string))) 
           ((lambda (body) ;; expand the body in language specific manner 
            (if (assoc :no-expand params) 
             body 
            (if (fboundp expand-cmd) 
             (funcall expand-cmd body params) 
             (org-babel-expand-body:generic 
             body params 
             (and (fboundp assignments-cmd) 
              (funcall assignments-cmd params)))))) 
           (if (org-babel-noweb-p params :tangle) 
            (org-babel-expand-noweb-references info) 
            (nth 1 info))))) 
          (comment 
           (when (or (string= "both" (cdr (assoc :comments params))) 
             (string= "org" (cdr (assoc :comments params)))) 
           ;; from the previous heading or code-block end 
           (funcall 
           org-babel-process-comment-text 
           (buffer-substring 
            (max (condition-case nil 
              (save-excursion 
              (org-back-to-heading t) ; sets match data 
              (match-end 0)) 
             (error (point-min))) 
             (save-excursion 
             (if (re-search-backward 
               org-babel-src-block-regexp nil t) 
              (match-end 0) 
              (point-min)))) 
            (point))))) 
          by-lang) 
         ;; add the spec for this block to blocks under it's language 
         (setq by-lang (cdr (assoc src-lang blocks))) 
         (setq blocks (delq (assoc src-lang blocks) blocks)) 
         (setq blocks (cons 
             (cons src-lang 
              (cons (list start-line file link 
                 source-name params body comment) 
                by-lang)) blocks))))))) 
    ;; ensure blocks in the correct order 
    (setq blocks 
      (mapcar 
      (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang)))) 
      blocks)) 
blocks)) 
+0

हे @ माइक, ऑर्गमोड का किस संस्करण पर आधारित था? मैं 'ओब-टैंगल' गया जहां समारोह से है, और कोड और हस्ताक्षर दोनों अलग हैं: हस्ताक्षर '(डिफन ऑर्ग-बेबेल-टेंगल-कलेक्ट-ब्लॉकों (और वैकल्पिक भाषा टेंगल-फाइल) ' – AdrieanKhisbe

+0

@AdrieanKhisbe मैं उपयोग कर रहा हूं 7.9.3 एफ – michaelJohn

+0

ओके डोकी, इसलिए उन्होंने बड़े संस्करण 8 के लिए पूरे फ़ंक्शन को दोबारा रिफैक्टर कर दिया होगा। मैंने अभी तक इसे प्राप्त करने के लिए एक रास्ता खोजना छोड़ दिया है क्योंकि 'उपयोग-पैकेज' के साथ मेरे पास एक कामकाज है। धन्यवाद अभी भी! :) – AdrieanKhisbe

3

(मैं 'जब तक' के लिए पहली कॉल के बाद पहले 4 लाइनों जोड़ा) इस व्यवहार को आप तथ्य यह है कि का उपयोग कर सकते प्राप्त करने के लिए yes और no से अलग, :tangle ऑरग बेबेल कोड ब्लॉक के लिए हेडर तर्क भी understands file names; यानी, किसी भी दिए गए कोड ब्लॉक के लिए आप ऑर्ग बेबेल को बता सकते हैं कि आप किस फ़ाइल को ब्लॉक करना चाहते हैं। मेरा विचार स्वचालित रूप से एक निश्चित शीर्षक के अंतर्गत प्रत्येक कोड ब्लॉक के लिए फ़ाइल नाम सेट जब शीर्षक पर टैग जोड़कर के लिए है:

(defun org-babel-set-tangle-file() 
    (let ((tag (car (org-get-local-tags)))) 
    (org-narrow-to-subtree) 
    (while (re-search-forward "\\(:tangle \\).*" nil t) 
     (replace-match (concat "\\1" tag ".el"))) 
    (widen))) 

(add-hook 'org-after-tags-change-hook 'org-babel-set-tangle-file) 

जिसके परिणामस्वरूप व्यवहार है कि जब आप वर्तमान फ़ाइल के लिए org-babel-tangle कहते हैं, सब कोड एक टैग बिना

  • सुर्खियों से संबंधित ब्लॉक डिफ़ॉल्ट उलझन फ़ाइल (रों)
  • एक टैग किए गए शीर्षक टैग के नाम पर एक फाइल करने के लिए उलझ जाएगा उलझ कर दिया जाएगा।

ध्यान दें कि ऊपर दिया गया कार्य टैग-विशिष्ट टेंगल फ़ाइलों के फ़ाइल एक्सटेंशन को .el पर सेट करता है; चूंकि आप उल्लेख करते हैं कि आप विभिन्न Emacs कॉन्फ़िगरेशन का उत्पादन करना चाहते हैं, मुझे लगा कि यह एक उचित डिफ़ॉल्ट होगा (भले ही आप अपने उदाहरण में सी कोड दिखा रहे हों)।

+0

मैंने अभी कोशिश की है लेकिन वास्तव में उम्मीद के अनुसार काम नहीं किया है।(चूंकि आप इसे छोड़कर हैं: टैंगल टैग है, और मैं सभी ब्लॉक में नहीं जोड़ना पसंद करता हूं) मैं एकत्रित ब्लॉक को फ़िल्टर करने के लिए माइक विचार पसंद करता हूं। अधिक लचीला लगता है, और इसे और अधिक तर्क कर सकते हैं – AdrieanKhisbe

संबंधित मुद्दे

 संबंधित मुद्दे