Emacs 26.1 In my file custom_functions.el

(defun increment-number-by-one-at-point()
  (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

  • 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? – Drew Nov 17 '19 at 18:30
  • I need custom function with private scope – a_subscriber Nov 17 '19 at 19:25
  • stackoverflow.com/q/39550578 – phils Nov 17 '19 at 21:36

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 ()
    (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 ()
    (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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.