20110714

标 题: 我写了一个用来在上班时偷偷看小说的工具 转载

发信人: wolfgang (狂云), 信区: Emacs
标 题: 我写了一个用来在上班时偷偷看小说的工具
发信站: 水木社区 (Mon Jul 11 10:24:04 2011), 转信

版面需要话题,我们需要有一些激动人心的事,
比方说,用emacs在上班时偷偷看小说。

我认为一个很好的办法是,把小说写到源代码里去!
满屏幕的源代码,其中夹杂着一行小说,还是被注释掉的,
匆匆路过的领导总不会注意到吧?

把以下代码贴到.emacs中,
把你想读的小说放在 ~/misc/book.txt 中,
按C-.看前一行,按C-,看后一行,按C-'清除作案痕迹。
我也不用多说废话,谁用谁知道。


;;;; Leisureread by wolfgang 2011-7-12 10:55 ver1.3

(require 'bookmark)
(when (not (file-exists-p bookmark-default-file))
(bookmark-save))
(bookmark-load bookmark-default-file t t)

(defvar *leisureread-my-book-path* "~/misc/book.txt")
(defvar *leisureread-bookmark-name* "leisureread")
(defvar *leisureread-window-height* 1)

(defun leisureread-initialize-bookmark-if-necessary ()
;; if no previous bookmark, create it at first line
(when (or (null bookmark-alist)
(null (assoc *leisureread-bookmark-name* bookmark-alist)))
(find-file *leisureread-my-book-path*)
(bookmark-set *leisureread-bookmark-name*)
(bury-buffer))
;; if not opened book.txt yet, open it and keep it open
(when (not (get-file-buffer *leisureread-my-book-path*))
(save-excursion
(find-file *leisureread-my-book-path*)
(bookmark-jump *leisureread-bookmark-name*)
(bury-buffer))))

(defun leisureread-line-prefix ()
(concat comment-start "+"))

(defun leisureread-decorate-lines (text)
(let ((lines (split-string text "\n")))
(let ((decorated-lines
(mapcar (lambda (line) (concat (leisureread-line-prefix) line))
lines)))
(reduce (lambda (acc next) (concat acc "\n" next))
decorated-lines))))


(defun leisureread-on-leisure-line-p ()
(let ((text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(start-with-p text (leisureread-line-prefix))))

(defun start-with-p (big small)
(and (>= (length big) (length small))
(string= small (substring big 0 (length small)))))

(defun leisureread-clear-line ()
(interactive)
(while (leisureread-on-leisure-line-p)
(kill-whole-line)))

(defun leisureread-insert-next-line ()
(interactive)
(leisureread-insert-line 'forward-line))

(defun leisureread-insert-previous-line ()
(interactive)
(leisureread-insert-line 'previous-line))

(defun leisureread-insert-line (func)
(leisureread-initialize-bookmark-if-necessary)
(move-beginning-of-line nil)
(let ((text ""))
(while (<= (length text) 1)
(save-excursion
(set-buffer (get-file-buffer *leisureread-my-book-path*))
(funcall func)
(setq text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position *leisureread-window-height*)))
(bookmark-set *leisureread-bookmark-name*)
(bury-buffer)))
(save-excursion
(leisureread-clear-line)
(insert (leisureread-decorate-lines text))
(newline))))


(global-set-key (kbd "C-.") 'leisureread-insert-next-line)
(global-set-key (kbd "C-,") 'leisureread-insert-previous-line)
(global-set-key (kbd "C-'") 'leisureread-clear-line)

;;;; emacs的用户有福了!让其它编辑器的用户颤抖吧!

--
热二定律比动量守恒更高
它告诉我们世界的原初是恨

爱, 就是对恨的战斗!


※ 修改:·wolfgang 于 Jul 12 12:27:23 2011 修改本文·[FROM: 221.133.241.*]
※ 来源:·水木社区 newsmth.net·[FROM: 221.133.241.*]

No comments: