Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created January 14, 2015 10:54
Show Gist options
  • Save xuchunyang/5bef780b61a4473d4e57 to your computer and use it in GitHub Desktop.
Save xuchunyang/5bef780b61a4473d4e57 to your computer and use it in GitHub Desktop.
demo-code-for-elisp-guide.el
;;; demo-code-for-elisp-guide.el --- practice code for elisp-guide
;;; Code:
;; Basic concepts
;; Buffers
(current-buffer)
#<buffer demo-code-for-elisp-guide.el>
(buffer-file-name)
"/Users/xcy/gists/demo-code-for-elisp-guide.el"
;; Global variable
(defvar my-name nil "Here should be doc string.")
my-name
;; use it in local buffer
(set (make-local-variable 'my-name) "xoxo")
"xoxo"
;; Project wide buffer-local
;; see ==> (elisp) Directory Local Variables
;; The point
(point)
489
(point-max)
507
(point-min)
1
;; The region
(region-beginning)
(region-end)
(use-region-p)
nil
t
nil
(region-active-p)
595
nil
(defun print-upper-region ()
"Demo to print the uppercased version of the active region."
(interactive)
(when (region-active-p)
(message "%S" (upcase (buffer-substring (region-beginning)
(region-end))))))
(upcase "skin")
;; 操作 the buffer
(insert "foo" "bar")
foobarnil
(delete-region (1- (point-max)) (point-max))
(insert-buffer-substring-no-properties (get-buffer "*scratch*") 1 3)
myt
(put-text-property 0 1 )
(get-text-property (point-min) )
;; Navigating the buffer
(goto-char (point-min))
(forward-char -1)
(end-of-line)
(beginning-of-line)
(search-backward "line")
;; Save excursion
(save-excursion (beginning-of-line) (looking-at "("))
;;; Query the buffer
(buffer-substring 0 1)
(buffer-substring-no-properties 1 2)
(buffer-string)
(looking-at "[a-zA-Z]+")
(looking-back "[a-zA-Z]+")
;;; Temporary buffers
(with-temp-buffer
(insert "hello!"))
;;; define interactive function
;; Defining my own major mode
(define-derived-mode hypertext-mode
text-mode "Hypertext"
"Major mode for hypertext
\\{hppertext-mode-ma]}")
(define-key hypertext-mode-map
[down-mouse-3] 'do-hyper link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment