2009-01-05 120 views
4

如何在emacs中快速編寫以下代碼?從Emacs生成代碼

​​

是否有辦法不是寫

A 
B 
C 
D 
. 
. 
. 
Y 
Z 

,然後在每一行做宏觀更快? (將A更改爲\ newcommand {\ cA} {\ mathcal A})

+0

你不應該需要這麼多的數學字母。 :-) – ShreevatsaR 2009-01-05 21:27:56

回答

5

一般來說每一次,第一次你面對一個這種問題,你會用鍵盤宏,就像JB已經說過的那樣。

第二次,請查看Steve Yegge撰寫的這篇非常有趣的文章:http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html,其中包含與您的問題完全相同的解決方案。

爲了您的方便,和我自己的照明,其實我去前進,測試它:

我將開始與

 
A 
... 
A 

26倍

,並做了

的Mx replace-regexp

 
A 
\\newcommand{\\c\,(string (+ ?A \#))}{\\mathcal \,(string (+ ?A \#))} 
+0

現在,這是真正的人的做法:) – 2009-01-05 17:25:36

4

A到Z只有26行。你會浪費更多的時間來自動生成,而不僅僅是自然地使用鍵盤宏,恕我直言。

+0

+1只是在你做第一個宏時(包括移動到下一行)定義一個宏,然後執行一個重複計數爲25的宏,你就完成了。 – 2009-01-05 16:54:39

7

我同意鍵盤宏將得到最快的結果。

更好玩的是一種編程方法:

(loop 
     for n from (string-to-char "A") to (string-to-char "Z") 
     for c = (char-to-string n) 
     do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n"))) 
0

我喜歡高清的環比我好一點,但這裏有一個替代,更廣義的方法:

(defun my-append (str) 
    "Substitute A-Z in STR and insert into current buffer. 

Looks for '--HERE--' (without quotes) in string STR, substitutes the 
letters A-Z in the string and then inserts the resulting string into 
the current buffer." 
    (interactive "sInput String: ") 
    (let ((lcv 0) 
     letter newstr) 
    (while (< lcv 26) 
     (setq letter (+ ?A lcv)) 

     (insert (replace-regexp-in-string "--HERE--" (char-to-string letter) str t t) "\n") 

     (setq lcv (+ lcv 1))))) 

應該不會太難將兩者結合起來。

1

或交互的方式,如果你想要做他們在同一時間

(defun somefun(inputval) 
    (interactive "sInputVal: ") 
    (insert (format "\\newcommand{\\c%s}{\\mathcal %s}" inputval inputval)) 
) 

只是EVAL和MX somefun你想要的文字

3

這取決於你的背景。我只需鍵入M-!和:

perl -e"print map {q(\newcommand{\c).$_.q(}{\mathcal).qq($_}\n)} A..Z 
3

你知道rectangle selection

還有string-rectangle

  • 地方點(光標)在文本的開頭,標記(C-SPC
  • 發生在最後一行的起點
  • M-x string-rectangle
  • 鍵入一個字符串(\ newcommand {\ c)

This will inser從標記開始,每行之前的那個字符串。

3

我沒有足夠的業力或任何評論,但我想改善HD的風格一點。

原來這裏是:

(loop 
    for n from (string-to-char "A") to (string-to-char "Z") 
    for c = (char-to-string n) 
    do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n"))) 

首先,的Emacs Lisp有字符閱讀器語法。而不是(string-to-char "X"),你可以寫?X。然後,你可以使用printf風格format代替char-to-stringconcat產生最終結果:

(loop for n from ?A to ?Z 
     do (insert (format "\\newcommand{\\c%s}{\\mathcal %s}\n" n n))) 

現在是不夠簡潔想也沒想到M-:提示符下鍵入。

我也會指出TeX也有宏,如果這確實是TeX的話。

編輯:喬Casadonte的另一位風格建議; (incf foo)(setq foo (+ foo 1))更容易輸入。

1

不完全是答案。

對於不使用emacs或vim的人,我想補充一點,您可以打開Excel並使用其自動填充功能和文本串聯功能來生成此類代碼。