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

3
  • The question as posed is unclear, IMO. But at least @zck seems to have understood what was asked. Perhaps someone could edit the question a bit, to make it more clear? Commented Nov 17, 2019 at 18:30
  • I need custom function with private scope Commented Nov 17, 2019 at 19:25
  • stackoverflow.com/q/39550578 Commented Nov 17, 2019 at 21:36

1 Answer 1

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!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.