初稿:
最終更新日: 2018-12-14 Fri 20:44
ホーム | 文書トップ | 目次
(defun package--ensure-init-file ()
  "Ensure that the user's init file has `package-initialize'.
`package-initialize' doesn't have to be called, as long as it is
present somewhere in the file, even as a comment.  If it is not,
add a call to it along with some explanatory comments."
  ;; Don't mess with the init-file from "emacs -Q".
  (when (and (stringp user-init-file)
             (not package--init-file-ensured)
             (file-readable-p user-init-file)
             (file-writable-p user-init-file))
    (let* ((buffer (find-buffer-visiting user-init-file))
           buffer-name
           (contains-init
            (if buffer
                (with-current-buffer buffer
                  (save-excursion
                    (save-restriction
                      (widen)
                      (goto-char (point-min))
                      (re-search-forward "(package-initialize\\_>" nil 'noerror))))
              ;; Don't visit the file if we don't have to.
              (with-temp-buffer
                (insert-file-contents user-init-file)
                (goto-char (point-min))
                (re-search-forward "(package-initialize\\_>" nil 'noerror)))))
      (unless contains-init
        (with-current-buffer (or buffer
                                 (let ((delay-mode-hooks t)
                                       (find-file-visit-truename t))
                                   (find-file-noselect user-init-file)))
          (when buffer
            (setq buffer-name (buffer-file-name))
            (set-visited-file-name (file-chase-links user-init-file)))
          (save-excursion
            (save-restriction
              (widen)
              (goto-char (point-min))
              (while (and (looking-at-p "[[:blank:]]*\\(;\\|$\\)")
                          (not (eobp)))
                (forward-line 1))
              (insert
               "\n"
               ";; Added by Package.el.  This must come before configurations of\n"
               ";; installed packages.  Don't delete this line.  If you don't want it,\n"
               ";; just comment it out by adding a semicolon to the start of the line.\n"
               ";; You may delete these explanatory comments.\n"
               "(package-initialize)\n")
              (unless (looking-at-p "$")
                (insert "\n"))
              (let ((file-precious-flag t))
                (save-buffer))
              (if buffer
                  (progn
                    (set-visited-file-name buffer-name)
                    (set-buffer-modified-p nil))
                (kill-buffer (current-buffer)))))))))
  (setq package--init-file-ensured t))

著者: Naoya Yamashita

Created: 2018-12-14 Fri 21:04

Validate