Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find aliases in emacs

Tags:

alias

emacs

I want to check if there is a default\ existing aliasing for a function (in this case:x-clipboard-yank, but the question is general).
Is there an emacs function that displays active aliases I can use to figure it up?
The expected behavior is like the shell alias command.

like image 642
user2141046 Avatar asked Apr 30 '15 14:04

user2141046


1 Answers

You can check the value of (symbol-function 'THE-FUNCTION). If the value is a symbol then THE-FUNCTION is an alias.

However, if the value is not a symbol THE-FUNCTION might nevertheless have been defined using defalias (or fset). In that case, THE-FUNCTION was aliased not to another function's symbol but to a function definition (e.g. the current function definition of some symbol, instead of the symbol itself) or to a variable definition (e.g. the value of a keymap variable).

You probably do not care about this case anyway - you probably do not even think of it as an alias. So testing whether the symbol-function value is a non-nil symbol is probably sufficient. (A value of nil means the there is neither a function alias nor any other function definition for the given symbol.)

So for example:

(defun aliased-p (fn)
  "Return non-nil if function FN is aliased to a function symbol."
  (let ((val  (symbol-function fn)))
    (and val                            ; `nil' means not aliased
         (symbolp val))))

In response to the question in your comment: Here is a command version of the function:

(defun aliased-p (fn &optional msgp)
  "Prompt for a function name and say whether it is an alias.
Return non-nil if the function is aliased."
  (interactive "aFunction: \np")
  (let* ((val  (symbol-function fn))
         (ret  (and val  (symbolp val))))
    (when msgp (message "`%s' %s an alias" fn (if ret "IS" "is NOT")))
    ret))

To be clear about the non-symbol case - If the code does this:

(defalias 'foo (symbol-function 'bar))

then foo is aliased to the current function definition of bar. If the definition of bar is subsequently changed, that will have no effect on the definition of foo. foo's definition is a snapshot of bar's definition at the time of the defaliasing.

But if the code does this:

(defalias 'foo 'bar)

then foo is aliased to the symbol bar. foo's function definition is the symbol bar: (symbol-function 'foo) = bar. So if bar's function definition gets changed then foo's definition follows accordingly.

like image 79
Drew Avatar answered Oct 05 '22 23:10

Drew