help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The definition of orig-fn.


From: Hongyi Zhao
Subject: Re: The definition of orig-fn.
Date: Mon, 4 Oct 2021 21:57:07 +0800

On Mon, Oct 4, 2021 at 8:29 PM Tassilo Horn <tsdh@gnu.org> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> >> > (defun ora--company-good-prefix-p (orig-fn prefix)
> >> > (unless (and (stringp prefix) (string-match-p "\\`[0-9]+\\'" prefix))
> >> > (funcall orig-fn prefix)))
> >> > (ora-advice-add 'company--good-prefix-p :around 
> >> > #'ora--company-good-prefix-p)
> >> > ;;;
> >> >
> >> > The symbol `orig-fn' used above puzzles me the most. It can't be found
> >> > by `C-h o orig-fn RET', and its definition cannot be found elsewhere
> >> > in the above repository.
> >>
> >> It's just the first argument of the function and will always be
> >> `company--good-prefix-p' according to the device definition.
> >
> > Thank you for pointing this out. But I completely commented out the
> > above code snippet given there [1] as follows, and it still works
> > smoothly:
>
> Did you also delete the *.el{c,n} files and restart emacs?

I just borrowed the code snippet mentioned here [1] and insert them
into my `~/.emacs.d/init.el' file, and not use the whole oremacs
project as my configuration, as shown below:

;;;
(defun ora-company-number ()
"Forward to `company-complete-number'.

Unless the number is potentially part of the candidate.
In that case, insert the number."
(interactive)
(let* ((k (this-command-keys))
(re (concat "^" company-prefix k)))
(if (or (cl-find-if (lambda (s) (string-match re s))
company-candidates)
(> (string-to-number k)
(length company-candidates))
(looking-back "[0-9]+\\.[0-9]*" (line-beginning-position)))
(self-insert-command 1)
(company-complete-number
(if (equal k "0")
10
(string-to-number k))))))

(defun ora--company-good-prefix-p (orig-fn prefix)
(unless (and (stringp prefix) (string-match-p "\\`[0-9]+\\'" prefix))
(funcall orig-fn prefix)))
(ora-advice-add 'company--good-prefix-p :around #'ora--company-good-prefix-p)

(let ((map company-active-map))
(mapc (lambda (x) (define-key map (format "%d" x) 'ora-company-number))
(number-sequence 0 9))
(define-key map " " (lambda ()
(interactive)
(company-abort)
(self-insert-command 1)))
(define-key map (kbd "<return>") nil))
;;;

In my situation, I can't find the *.el{c,n} files corresponding to
`~/.emacs.d/init.el'.

[1] 
https://github.com/abo-abo/oremacs/blob/3809390019a7083c28e8502a82da94ca3a8ebba0/modes/ora-company.el#L22-L53

> When you do `C-h f company--good-prefix-p RET' it should mention that there 
> is an
> advice if there is one.

I tried the above command, but only see the following result:

company--good-prefix-p is a compiled function defined in company.el.

Signature
(company--good-prefix-p PREFIX)

Documentation
This function has :around advice: ora--company-good-prefix-p.

References
Finding references in a .el file is not supported.

Find all references

Advice
This function is advised.

Debugging
Enable tracing

Source Code
// Defined in ~/.emacs.d/straight/build/company/company.el
(defun company--good-prefix-p (prefix)
  (and (stringp (company--prefix-str prefix)) ;excludes 'stop
       (or (eq (cdr-safe prefix) t)
           (let ((len (or (cdr-safe prefix) (length prefix))))
             (if company--manual-prefix
                 (or (not company-abort-manual-when-too-short)
                     ;; Must not be less than minimum or initial length.
                     (>= len (min company-minimum-prefix-length
                                  (length company--manual-prefix))))
               (>= len company-minimum-prefix-length))))))

Symbol Properties
defalias-fset-function
  #[128 "\300\301\302 #\207"
    [apply advice--defalias-fset nil nil]
    5 nil]
  Disassemble
function-documentation
  (advice--make-docstring 'company--good-prefix-p)

> But I don't understand what you are trying to achieve.

"Using digits to select company-mode candidates" without hitting the
default modifier key, as noted here [2]. You can see the attachment to
get a rough impression for the purpose of the code snippet discussed
here.

[2] https://oremacs.com/2017/12/27/company-numbers/

> Why are you commenting that out,

Since it seems to me that the remains part of the code has done the
job described there, so I want to simply the original code snippet.

> and what does "works smoothly" mean in this context?

Enables me to select and insert the candidates using digits only,
without hitting the modifier key, which by default is `meta'.

> (FWIW, I don't use company nor oremacs, so I cannot comment on that.)
>
> Bye,
> Tassilo

Attachment: company-using-digits-to-select-candidates.png
Description: PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]