珞珈山水BBS电脑网络Linux & Unix → 单文区文章阅读

单文区文章阅读 [返回]
发信人: bigben (早起的鸟儿debug), 信区: Linux_Unix
标  题: 用这个把代码pp的贴到bbs上^_^
发信站: 珞珈山水BBS站 (Thu Nov  3 20:58:52 2005), 站内

;; `bbs-color.el' -- Convert buffer or region into special bbs ascii code.
;; Filename: bbs-color.el
;; Author: bigben
;;
;; Usage
;; Just add this to your ~/.emacs file:
;; (require 'bbs-color)
;; There is totally 3 interactive functions:
;;  `bbs-region': convert current region and save into clipboard.
;;  `bbs-buffer': convert current buffer and save into clipboard.
;;  `bbs-get-face': a small utility to get the face at the point.
;;    Then you can add new face color into `bbs-color-map'.
;; You can add key binding for these functions.
;;
;; Custom:
;; The main customable variable is `bbs-color-map', which translate
;; faces into different bbs colors. You can add new face color.
;; For example:
;;  (add-to-list 'bbs-color-map '(font-lock-error-face . "\033[1;33m"))

(defcustom bbs-color-prefix ""
  "This prefix is used in standard terminal which need to input
ESC twice. If in this situation, set it's value to\"033\" If you
are using special BBS Terminal such as FTerm, don't change it.")

(defconst bbs-color-black "\033[1;30m")
(defconst bbs-color-red "\033[1;31m")
(defconst bbs-color-green "\033[1;32m")
(defconst bbs-color-yellow "\033[1;33m")
(defconst bbs-color-blue "\033[1;34m")
(defconst bbs-color-magenta "\033[1;35m")
(defconst bbs-color-cyan "\033[1;36m")
(defconst bbs-color-white "\033[1;37m")

(defcustom bbs-color-map
  '((font-lock-builting-face . bbs-color-yellow)
    (font-lock-comment-face . bbs-color-blue)
    (font-lock-constant-face . bbs-color-magenta)
    (font-lock-doc-face . bbs-color-blue)
    (font-lock-function-name-face . bbs-color-green)
    (font-lock-keyword-face . bbs-color-cyan)
    (font-lock-preprocessor-face . bbs-color-blue)
    (font-lock-string-face . bbs-color-red)
    (font-lock-type-face . bbs-color-cyan)
    (font-lock-variable-name-face . bbs-color-green)
    (font-lock-warning-face . bbs-color-yellow)
    ((font-lock-constant-face font-lock-doc-face) . bbs-color-yellow)
    ((font-lock-constant-face font-lock-comment-face) . bbs-color-yellow))
  "Map the font-lock colors to bbs color.
You can add more faces as you need.")

(defun bbs-face-to-ascii (face)
  "Get the face ascii string from `bbs-color-map'"
  (let ((result (cdr (assoc face bbs-color-map))))
    (cond ((null result) "")
       ((symbolp result)
        (symbol-value result))
       (t result))))

(defun bbs-buffer ()
  "Convert current buffer to ascii string and save it into clipboard."
  (interactive)
  (let ((content (bbs-region-to-ascii (point-min) (point-max))))
    (with-temp-buffer
      (erase-buffer)
      (insert content)
      (kill-region (point-min) (point-max)))))

(defun bbs-region ()
  "Convert current region to ascii string and save it into clipboard."
  (interactive)
  (save-excursion
    (let ((content (bbs-region-to-ascii (mark) (point))))
      (with-temp-buffer
     (erase-buffer)
     (insert content)
     (kill-region (point-min) (point-max))))))

(defun bbs-region-to-ascii (start stop)
  "Convert a region to ascii string as return value."
  (let ((buf (current-buffer))
     (begin (min start stop))
     (end (max start stop))
     (ascii))
    (save-excursion
      (goto-char begin)
      (while (< (point) end)
     (let ((tface (get-text-property (point) 'face))
           (next-change
            (or (next-single-property-change (point) 'face nil end) end))
           (pos (point))
           (next-line
                (or (search-forward "\n" end t) end)))
       (setq ascii (concat ascii
                           (bbs-face-to-ascii tface)
                           (buffer-substring pos (min next-line next-change))
                           "\033[m"))
       (goto-char (min next-line next-change))))
      ascii)))

(defun bbs-get-face ()
  (interactive)
  (message
   (format "%s"
    (get-text-property (point) 'face))))
--
Hi, I'm a signature virus.
Copy me into your signature to help me spread.


※ 来源:·珞珈山水BBS站 bbs.whu.edu.cn·[FROM: 202.114.79.*]
[返回单文区目录]

武汉大学BBS 珞珈山水站 All rights reserved.
wForum , 页面执行时间:13.185毫秒