Function: helpful--open-if-needed

helpful--open-if-needed is a byte-compiled function defined in helpful.el.

Signature

(helpful--open-if-needed PATH)

Documentation

Return a list (BUF OPENED) where BUF is a buffer visiting PATH.

If a buffer already exists, return that. If not, open PATH with the emacs-lisp-mode syntax table active but skip any hooks.

Source Code

;; Defined in ~/.emacs.d/elpa/helpful-20250408.334/helpful.el
(defun helpful--open-if-needed (path)
  "Return a list (BUF OPENED) where BUF is a buffer visiting PATH.
If a buffer already exists, return that. If not, open PATH with
the `emacs-lisp-mode' syntax table active but skip any hooks."
  (let ((initial-buffers (buffer-list))
        (buf nil)
        (opened nil)
        ;; Skip running hooks that may prompt the user.
        (find-file-hook nil)
        ;; If we end up opening a buffer, don't bother with file
        ;; variables. It prompts the user, and we discard the buffer
        ;; afterwards anyway.
        (enable-local-variables nil))
    ;; Opening large .c files can be slow (e.g. when looking at
    ;; `defalias'), especially if the user has configured mode hooks.
    ;;
    ;; Bind `auto-mode-alist' to nil, so we open the buffer in
    ;; `fundamental-mode' if it isn't already open.
    (let ((auto-mode-alist nil))
      (setq buf (find-file-noselect path)))

    (unless (-contains-p initial-buffers buf)
      (setq opened t)

      (let ((syntax-table emacs-lisp-mode-syntax-table))
        (when (s-ends-with-p ".c" path)
          (setq syntax-table (make-syntax-table))
          (c-populate-syntax-table syntax-table))

        ;; If it's a freshly opened buffer, we need to set the syntax
        ;; table so we can search correctly.
        (with-current-buffer buf
          (set-syntax-table syntax-table))))

    (list buf opened)))