2012-10-24 18 views
5

मैं तार कि मैं Emacs तुतलाना का उपयोग कर प्रारूप में तैयार करना की एक सूची है की एक सूची स्वरूपण कैसे करूँ। यह एकमात्र तरीका था जिसके बारे में मैं सोचने के बारे में सोच सकता था:मैं तार

(setq slist '("is there" "any" "way" "directed iteration"))  
(format "%s _%s_ %s to do %S in elisp?" 
     (elt slist 0) 
     (elt slist 1) 
     (elt slist 2) 
     (elt slist 3) 
     (elt slist 4)) 

मुझे जो कुछ चाहिए वह दे रहा है।

is there _any_ way to do "directed iteration" in elisp? 

एक और अधिक सुरुचिपूर्ण तरीका होना चाहिए, लेकिन बहुत सोचा जाने के बाद, मैं इसे नहीं देख रहा हूं। मैं lisp emacs के लिए बहुत नया हूँ और मुझे कुछ स्पष्ट याद आ रही है।

उत्तर

4

उपयोग apply:

(apply 'format "%s _%s_ %s to do %S in elisp?" slist) 

apply समारोह में अपनी पहली तर्क है, तो अलग-अलग तर्क के एक नंबर, तर्कों की सूची के साथ खत्म कर के रूप में एक समारोह (या चिह्न) लेता है।

+0

हां, यह बिल्कुल धन्यवाद है। यह स्पष्ट था और मुझे आवेदन की प्रकृति याद आ गई। और वाह, यह कितना अच्छा काम है! –

2

मैं कुछ और अधिक सुविधाओं, और अधिक कीड़े जोड़ने कुछ बगों और जोड़कर एक स्टैंडअलोन परियोजना में बनाने का फैसला किया गया है! yey :)

आप परियोजना यहाँ पा सकते हैं: http://code.google.com/p/formatting-el/source/browse/trunk/formatting.el

सुनिश्चित नहीं हैं कि कितना गाड़ी इस है, लेकिन पहली नजर में यह काम करने के लिए लगता है:

(defun directive-end (c) 
    (member c "csdoxXeg%")) 

(defun pp-if-nil (spec) 
    (position ?\% spec)) 

(defun pp-list (spec args) 
    (let ((pos 0) (last 0) (fstring "% ") current seen-^) 
    (catch 't 
     (while t 
     (setq pos (1+ (or (position ?% spec :start pos) -1)) 
       current (aref spec pos)) 
     (unless (and seen-^ (char-equal current ?\}) (null args)) 
      (princ (substring spec last (1- pos)))) 
     (setq last pos pos (1+ pos)) 
     (cond 
     ((char-equal current ?^) 
      (incf last) 
      (setq seen-^ t)) 
     ((char-equal current ?\{) 
      (setq pos (+ pos (pp-list (substring spec pos) (car args))) 
       args (cdr args) 
       last pos 
       seen-^ nil)) 
     ((char-equal current ?\}) 
      (if args (setq pos 0 last 0) 
      (throw 't nil))) 
     ((char-equal current ?%) 
      (setq seen-^ nil last (1+ last)) 
      (write-char ?%)) 
     (t (unless args (error "Not enough argumens for list iteration")) 
      (setf (aref fstring 1) current) 
      (princ (format fstring (car args))) 
      (setq args (cdr args) 
        seen-^ nil 
        last 
        (or (position-if #'directive-end spec :start pos) 
         pos)))))) pos)) 

(defun cl-format (spec &rest args) 
    (with-output-to-string 
    (let ((pos 0) (last 0) (fstring "% ") current) 
     (catch 't 
     (while t 
      (setq pos (1+ (or (position ?\% spec :start pos) -1)) 
       current (aref spec pos)) 
      (when (= pos 0) (throw 't nil)) 
      (princ (substring spec last (1- pos))) 
      (setq last pos pos (1+ pos)) 
      (cond 
      ((char-equal current ?^) 
      (unless args 
       (setq last (pp-if-nil spec) 
        pos last))) 
      ((char-equal current ?\{) 
      (setq pos (+ pos (pp-list (substring spec pos) (car args))) 
        args (cdr args) 
        last pos)) 
      ((char-equal current ?\}) 
      (error "Unmatched list iteration termination directive")) 
      ((char-equal current ?%) 
      (write-char ?%) 
      (incf last)) 
      (t (unless args (error "Not enough argumens")) 
       (setf (aref fstring 1) current) 
       (princ (format fstring (car args))) 
       (setq args (cdr args) 
        last 
        (or (position-if #'directive-end spec :start pos) 
         pos)))) 
      (incf pos)))))) 

(cl-format "begin: %{%s = %d%^,%}; %% %c %% %{%{%s -> %d%^.%},%}" 
      '(a 1 b 2 c 3) ?\X '((a 2 b 4 c 6) (a 1 b 3 c 5))) 
"begin: a = 1,b = 2,c = 3; % X % a -> 2.b -> 4.c -> 6,a -> 1.b -> 3.c -> 5," 

यह कुछ को दोहराने की कोशिश करता है (बहुत सरल) ~{ ... ~} निर्देशों के सामान्य लिस्प-जैसे मुद्रण व्यवहार।