Source

.emacs.d / general-helper.el

Full commit
;; smart-ime-mode会干扰此函数,用前先关闭smart-ime-mode
;; (defun insert-key-sequence (key)
;;   "插入按键序列"
;;     (interactive "kInseret key sequence: ")
;;     (insert (key-description key) "\t\t\t"))

;; 直接通过interactive来读取按键序列是不行的,因为该函数执行后第一件事情就是切换到minibuffer,以至于来不及在原来的buffer中关闭smart-ime-mode
;; 所以,改用如下这个函数。

(defun insert-key-sequence ()
  "插入按键序列"
    (interactive)
    (ime-save-and-set-status 0)
    (let ((on smart-ime-mode))
      (smart-ime-mode -1)
      (insert (key-description
               (read-key-sequence "Insert key sequence: "))
              "\t\t\t")
      (when on
        (smart-ime-mode)))
    (ime-restore-status))


(defun go-tail (from to)
  "把选中的region移到文档末尾"
  (interactive (list (region-beginning) (region-end)))
  (let ((a t))
    (if mark-active
        (progn
          (save-restriction
            (widen)
            (save-excursion
              (goto-char (point-max))
              (newline)
              (append-to-buffer (current-buffer) from to))) ; save-restriction
          (delete-region from to)) ; progn

      (message "region is not active!!!"))))


(defun toggle-line-spacing ()
  "开关行间距"
  (interactive)
  (if (or (not (boundp 'line-spacing))
          (equal line-spacing nil))
      (setq line-spacing 5)
    (setq line-spacing nil)))

;; (toggle-line-spacing)


;; 模仿其他编辑器,创建一个不关联任何文件的buffer。
(defun new-untitled ()
  "create a new untitled file"
  (interactive)
  (switch-to-buffer (generate-new-buffer "Untitled"))
  (setq buffer-offer-save t)            ; 关闭emacs时询问是否保存
  ;; kill-buffer时询问是否保存
  (add-hook 'kill-buffer-hook '(lambda () 
                                 (when (and (buffer-modified-p)
                                            (y-or-n-p "Buffer modified; save it?"))
                                   (save-buffer)))
            nil t)
  )


;; 复制文件路径到剪贴板
(defun cp-path ()
  "copy the full path of the file in current buffer to the clipboard"
  (interactive)
  (kill-new (message (buffer-file-name))))


;; 打开.emacs文件
(defun open-init-file ()
  "Open the .emacs file"
  (interactive)
  (find-file user-init-file))