自动为文件增加可执行权限

用下面的办法对于编写 python, bash, per, awk, sed, 脚本的时候十分有用。在保存文件的时候,会自动给脚本 增加可执行权限。

(setq my-shebang-patterns
      (list "^#!/usr/.*/perl\\(\\( \\)\\|\\( .+ \\)\\)-w *.*"
        "^#!/usr/.*/sh"
        "^#!/usr/.*/bash"
        "^#!/bin/sh"
        "^#!/.*/perl"
        "^#!/.*/awk"
        "^#!/.*/sed"
        "^#!/bin/bash"))
(add-hook
 'after-save-hook
 (lambda ()
 (if (not (= (shell-command (concat "test -x " (buffer-file-name))) 0))
     (progn
       ;; This puts message in *Message* twice, but minibuffer
       ;; output looks better.
       (message (concat "Wrote " (buffer-file-name)))
       (save-excursion
         (goto-char (point-min))
         ;; Always checks every pattern even after
         ;; match.  Inefficient but easy.
         (dolist (my-shebang-pat my-shebang-patterns)
           (if (looking-at my-shebang-pat)
               (if (= (shell-command
                       (concat "chmod u+x " (buffer-file-name)))
                      0)
                   (message (concat
                             "Wrote and made executable "
                             (buffer-file-name))))))))
   ;; This puts message in *Message* twice, but minibuffer output
   ;; looks better.
   (message (concat "Wrote " (buffer-file-name))))))