Elisp:获取格式/ echo的引号

时间:2012-06-19 03:07:59

标签: email emacs format elisp echo

.mailrc中,我想获得第一行,但我得到第二行

alias UIF "UIF Boxning <uifboxning@boxing.se>" # cool (written manually by me)
alias test_alias test_name <test_mail> # no quotation marks (written by below defun)

我不确定问题是format还是echo。看看:

(defun save-mail-address (address name alias)
  "add an alias to the .mailrc file"
  (interactive "sMail address: \nsFull name: \nsAlias: ")
  (let ((compl-alias (format "alias %s \"%s \<%s\>\"" alias name address)))
    (shell-command (concat "echo " compl-alias " >> .mailrc")) ))
(defalias 'sma 'save-mail-address)

修改

好的,目标是保持这样:

alias long      "Long Long <long@long.long>"                    # 123456
alias s         "S S <s@s.s>"                                   # 1

另外,让我检查不要包含重复项:

(defun append-blanks (str len)
  (concat str (make-string (- len (length str)) ? )) )


(defun save-mail-address (mail name alias phone)
"add an alias to the .mailrc file"
(interactive "sMail mail: \nsFull name: \nsAlias: \nnPhone: ")
  (let*(
    (alias-alias (format "alias %s" alias))
    (alias-alias-blanks (append-blanks alias-alias 16))
    (mail-str (append-blanks (format "\"%s \<%s\>\"" name mail) 48))
    (line (format "%s%s\# %d\n" alias-alias-blanks mail-str phone))
    (file "~/.mailrc") )
      (with-temp-buffer
        (insert-file file)
        (if
          (search-forward alias-alias (point-max) t) ; t = return nil on fail
            (message (format "Error: The alias %s is already in use." alias))
            (progn
              (insert line)
              (sort-columns nil (point-min) (point-max)) ; nil = not reversed
              (write-file file nil) ))))) ; nil = inhibit confirm

2 个答案:

答案 0 :(得分:3)

您需要确保传递给shell命令的变量参数始终被适当地转义。

(shell-command (concat "echo " (shell-quote-argument compl-alias)))

答案 1 :(得分:1)

只需通过Elisp附加文本就像这样:

(defun save-mail-address (address name alias)
  "add an alias to the .mailrc file"
  (interactive "sMail address: \nsFull name: \nsAlias: ")
  (let ((compl-alias (format "alias %s \"%s \<%s\>\"" alias name address)))
    (with-temp-buffer
      (insert compl-alias "\n")
      (write-region (point-min) (point-max) "~/.mailrc" t))))