0

Emacs 26.1 In my file custom_functions.el

(defun increment-number-by-one-at-point()
  (interactive)
  (increment-number-at-point-private 1))

;; Increment number at point
(defun increment-number-at-point(number)
  (interactive (list (read-number "Input increment number: " 1)))
  (increment-number-at-point-private number))

(defun increment-number-at-point-private(number)
  (skip-chars-backward "0-9")
  (or (looking-at "[0-9]+")
      (error "No number at point"))
  (replace-match (number-to-string (+ number (string-to-number (match-string 0))))))

The problem is that increment-number-at-point-private is has access in the any place in Emacs List files. But I need that function increment-number-at-point-private must have access only in file custom_functions.el

Drew
  • 75,699
  • 9
  • 109
  • 225
a_subscriber
  • 3,854
  • 1
  • 17
  • 47

1 Answers1

2

If you call defun, it creates the function with a global scope. Emacs has no namespaces. There's some previous discussion here, but I won't go into it here.

So what can we do to make a function that does not get bound to a name? We could use lambda to create an anonymous function, bind it locally with let, and use it the function inside that body. We can use this function inside a named ("public") function:

(let ((my-private-function (lambda () (interactive) "Returned privately!")))
  (defun my-public-function ()
    (interactive)
    (replace-regexp-in-string "private" "public" (funcall my-private-function))))

(my-public-function) => "Returned publicly!"

This works fine, but we can use some functionality built into Emacs to make my-private-function callable by name inside the body, but not callable by name outside it.

(require 'cl-macs)

(cl-labels ((my-private-function () (interactive) "Returned privately!"))
  (defun my-public-function ()
    (interactive)
    (replace-regexp-in-string "private" "public" (my-private-function))))

(my-public-function) => "Returned publicly!"
zck
  • 8,984
  • 2
  • 31
  • 65