Chinaunix首页 | 论坛 | 博客
  • 博客访问: 641709
  • 博文数量: 227
  • 博客积分: 8017
  • 博客等级: 中将
  • 技术积分: 2069
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-08 22:50
文章分类

全部博文(227)

文章存档

2011年(10)

2010年(55)

2009年(28)

2008年(134)

我的朋友

分类: 系统运维

2008-04-09 23:40:31

开始学习emacs lisp了。

1.  第一个程序:  
(message "I saw %d %s"
              (- fill-column 32)
              (concat "red "
                      (substring
                       "The quick brown foxes jumped." 16 21)
                      " leaping."))
将光标放在尾部, C-x C-e: "I saw 48 red foxes leaping."

2. function call :   (function-name)
   variable eval :    var-name     
   symbol        :   'symbol-name  (evaluate value= 'symbol-name')

3. set & setq
    (set 'colors '(red, black, green))  (colors = (red, black, green))
    (setq colors '(red, black, green)) 

4. buffers
  • (buffer-name) /  (buffer-file-name)
  • (current-buffer)
  • (other-buffer)[为最近的buffer]
  • (switch-to-buffer (other-buffer))
  • (switch-to-buffer (other-buffer (current-buffer) t)) [other buffer的第一个参数表示跳过current-buffer, t tells `other-buffer' it is OK to switch to a visible buffer.]
  • (buffer-size) / (point)
5. functions
  • primitive function: written in C, 很少, 但是用户在使用 primitive function 和common function 时没有什么区别。
  • function definition:
    • (defun FUNCTION-NAME (ARGUMENTS...)
             "OPTIONAL-DOCUMENTATION..."
             (interactive ARGUMENT-PASSING-INFO)     ; optional
             BODY...))
    • interactive 表示可以通过M-x 或者keystrokes调用
          (defun plus-by-ten (number)
              "plus number by ten"
              (+ 10 number)) ;; C-x C-e install function
          (plus-by-ten 5)    ;; get result , C-h -f get doc
  • make function ineractive:
    (defun plus-by-ten (number)
  "plus number by ten"
  (interactive "p")
  (message "The result is %d" (+ 10 number)))
     然后 C-x C-e; C-u 5 M-x plus by ten求出结果, a)给ineractive function传递prefix argumnet的方式:C-u number 或者 M-number   b)The `"p"' tells Emacs to pass the prefix argument to the function and use its value for the argument of the function.(这里的prefix argument指的是C-u 后的5)

  •  Install Code Permanently
    • put it in .emacs
    • 把代码放在其它文件中,使用load函数装载文件。
    • 如果所有用户都要使用可以把代码放在site-init.el文件中

  • let
    • let用于修改或者绑定值到符号上。
    • let创建的是本地变量,作用范围止于let表达式范围内,不影响let外部的变量。let可以一次创建多个变量,并给每个变量赋值,初始值也可以是nil。在let执行完后,将返回最后一个语句的值。
    • (let ((VARIABLE VALUE)
                 (VARIABLE VALUE)
                 ...)
             BODY...)
    • eg: (let ((va 'text)
              (vb 100))
          (message "valuse va=%s, vb=%d" va vb))
    • (let ((va 'text)
            vb
            (vc 100))
        (message "The values: va = %s, vb = %s, vc = %d" va vb vc))
             result: "The values: va = text, vb = nil, vc = 100" ;;这里vb为nil
  • if
    •  (if TRUE-OR-FALSE-TEST
               ACTION-TO-CARRY-OUT-IF-TEST-IS-TRUE)
    • eg: (defun type-of-animal (characteristic)
               (if (equal characteristic 'fierce)
                     (message "It is a tiger!")))
          (type-of-animal 'fierce)
          (type-of-animal 'zebra)
    •  (if TRUE-OR-FALSE-TEST
               ACTION-TO-CARRY-OUT-IF-THE-TEST-RETURNS-TRUE
             ACTION-TO-CARRY-OUT-IF-THE-TEST-RETURNS-FALSE)
    • (defun type-of-animal (characteristic)
        (if (equal characteristic 'fierce)
            (message "It's tiger!")
          (message "It is zebra!")))
      (type-of-animal 'fierce)
      (type-of-animal 'zebra)
  • save-excursion 函数: saves the location of point and mark, executes the body of the function, and then restores point and mark to their previous positions if their locations were changed.
    • (let VARLIST
             (save-excursion
               BODY...))
    • (message "We are %d characters into this buffer."
               (- (point)
                  (save-excursion
                    (goto-char (point-min)) (point))))


  • (defun my-begin-to-buffer()
      (interactive)
      (push-mark)
      (goto-char (point-min)))
    (my-begin-to-buffer)

  • cons
    • construct的意思, 用于构造list, 必须要有一个可以被插入list的参数。构造一个list时,至少要提供一个空的list
    • eg:  (cons 'aa ())
           (cons 'bb '(aa))

  • 当文本被剪切出缓冲区时,它将被存储到一个list中。 包含这些文本区的list 称为kill ring

  • while
    • (setq animals '(gazelle giraffe lion tiger))
      (defun print-elem (list)
        (while list
          (print (car list))
          (setq list (cdr list))))
      (print-elem animals)

  • recursion
    • (defun print-elem-recursive (list)
        (if list
            (progn  ;;progn其实是起c 中{}的作用,挨个求值,返回最后一个值
              (print (car list))
              (print-elem-recursive (cdr list)))))
    • (defun triangle-recursively (number)
      "Return the sum of the numbers 1 through NUMBER inclusive.
      Uses recursion."

      (if (= number 1) ; do-again-test
      1 ; then-part
      (+ number ; else-part
      (triangle-recursively ; recursive call
      (1- number))))) ; next-step-expression

      (triangle-recursively 7)
  • cond
    • (cond
      (FIRST-TRUE-OR-FALSE-TEST FIRST-CONSEQUENT)
      (SECOND-TRUE-OR-FALSE-TEST SECOND-CONSEQUENT)
      (THIRD-TRUE-OR-FALSE-TEST THIRD-CONSEQUENT)
      ...) ;; If the true-or-false-test returns `nil' the rest of that expression,
      the consequent, is skipped and the true-or-false-test of the next
      expression is evaluated. When an expression is found whose
      true-or-false-test returns a value that is not `nil', the consequent of
      that expression is evaluated. If the consequent consists of more than one expression,
      the expressions are evaluated in sequence and the value of the last one
      is returned.
    • (defun triangle-using-cond (number)
        (cond
         ((<= number 0) 0)
         ((= number 1) 1)
         ((> number 1)
          (+ number (triangle-using-cond(- number 1))))))
      (triangle-using-cond 5)

  • g
阅读(1287) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~