u
[emacs-helper.git] / eh-functions.el
blob1505de98b531ac22101ee25151f97c60da2adba0
1 ;;; eh-functions.el --- Tumashu's emacs functions -*- lexical-binding: t; -*-
4 ;; * Header
5 ;; Copyright (c) 2011-2019, Feng Shu
7 ;; Author: Feng Shu <tumashu@163.com>
8 ;; URL: https://github.com/tumashu/emacs-helper
9 ;; Version: 0.0.1
11 ;; This file is not part of GNU Emacs.
13 ;;; License:
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License
17 ;; as published by the Free Software Foundation; either version 3
18 ;; of the License, or (at your option) any later version.
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;;; Commentary:
32 ;; * 简介 :README:
33 ;; 这个文件是tumashu个人专用的emacs配置文件,emacs中文用户可以参考。
35 ;;; Code:
37 ;; * 代码 :code:
38 (defun eh-wash-text (text &optional fill-width indent)
39 "Insert text into a temp buffer and wash it,
40 if `fill-width' is a number, the temp buffer will be filled to the number,
41 if `indent' is a number ,the temp buffer will be indent the number,
42 then the formated buffer will be exported with `buffer-string',
43 this function derived from `article-strip-multiple-blank-lines' in
44 `gnus-art.el'."
45 (interactive)
46 (with-temp-buffer
47 (goto-char (point-min))
48 (insert text)
49 ;; Make all blank lines empty.
50 (goto-char (point-min))
51 (while (re-search-forward "^[[:space:] \t]+$" nil t)
52 (replace-match "" nil t))
54 ;; Replace multiple empty lines with a single empty line.
55 (goto-char (point-min))
56 (while (re-search-forward "^\n\\(\n+\\)" nil t)
57 (delete-region (match-beginning 1) (match-end 1)))
59 ;; Remove a leading blank line.
60 (goto-char (point-min))
61 (if (looking-at "\n")
62 (delete-region (match-beginning 0) (match-end 0)))
64 ;; Remove a trailing blank line.
65 (goto-char (point-max))
66 (if (looking-at "\n")
67 (delete-region (match-beginning 0) (match-end 0)))
69 ;; remove "{"
70 (goto-char (point-min))
71 (if (looking-at "^[[:space:] \t]*{")
72 (delete-region (match-beginning 0) (match-end 0)))
74 ;; remove "}"
75 (goto-char (point-max))
76 (if (looking-at "}^[[:space:] \t]*")
77 (delete-region (match-beginning 0) (match-end 0)))
79 ;; fill buffer
80 (when fill-width
81 ;; unindent the buffer
82 (indent-region (point-min) (point-max) 0)
83 ;; unfill the buffer
84 (let ((fill-column 100000))
85 (fill-region (point-min) (point-max)))
86 ;; fill the buffer to fill-width
87 (let ((fill-column fill-width))
88 (fill-region (point-min) (point-max))))
90 ;;indent buffer
91 (when indent
92 (indent-region (point-min) (point-max) indent))
93 (buffer-string)))
95 (defun eh-dos2unix ()
96 "将dos换行方式转换为unix的换行方式,用于去除^M"
97 (interactive)
98 (goto-char (point-min))
99 (while (search-forward "\r" nil t) (replace-match "")))
101 (defun eh-unix2dos ()
102 "将unix换行方式转换为dos的换行方式"
103 (interactive)
104 (goto-char (point-min))
105 (while (search-forward "\n" nil t) (replace-match "\r\n")))
107 (defun eh-revert-buffer-with-gbk ()
108 (interactive)
109 (revert-buffer-with-coding-system 'gbk-dos))
111 (defun eh-revert-buffer-with-utf8 ()
112 (interactive)
113 (revert-buffer-with-coding-system 'utf-8-unix))
115 (defun eh-save-buffer-with-gbk (&optional arg)
116 (interactive)
117 (set-buffer-file-coding-system 'gbk-dos)
118 (save-buffer arg))
120 (defun eh-save-buffer-with-utf8 (&optional arg)
121 (interactive)
122 (set-buffer-file-coding-system 'utf-8-unix)
123 (save-buffer arg))
125 (defun eh-utf8-language-environment ()
126 "设置utf-8语言环境"
127 (interactive)
128 (set-language-environment "UTF-8")
129 (set-buffer-file-coding-system 'utf-8-unix)
130 (set-clipboard-coding-system 'utf-8-unix)
131 (set-file-name-coding-system 'utf-8-unix)
132 (set-keyboard-coding-system 'utf-8-unix)
133 (set-next-selection-coding-system 'utf-8-unix)
134 (set-selection-coding-system 'utf-8-unix)
135 (set-terminal-coding-system 'utf-8-unix))
137 (defun eh-gbk-language-environment ()
138 "设置gbk语言环境"
139 (interactive)
140 (set-language-environment "Chinese-GBK")
141 (set-buffer-file-coding-system 'gbk-dos)
142 (set-clipboard-coding-system 'gbk-dos)
143 (set-file-name-coding-system 'gbk-dos)
144 (set-keyboard-coding-system 'gbk-dos)
145 (set-next-selection-coding-system 'gbk-dos)
146 (set-selection-coding-system 'gbk-dos)
147 (set-terminal-coding-system 'gbk-dos))
149 ;; * Footer
150 (provide 'eh-functions)
152 ;; Local Variables:
153 ;; coding: utf-8-unix
154 ;; End:
156 ;;; eh-functions.el ends here