unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Hongyi Zhao <hongyi.zhao@gmail.com>
To: Tassilo Horn <tsdh@gnu.org>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: The definition of orig-fn.
Date: Mon, 4 Oct 2021 21:57:07 +0800	[thread overview]
Message-ID: <CAGP6POJBpqOVSd1G7EnKvNweGo7ag6bG2jj6mRm9+Syx1wxBJg@mail.gmail.com> (raw)
In-Reply-To: <878rz9ht1m.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 4470 bytes --]

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 #2: company-using-digits-to-select-candidates.png --]
[-- Type: image/png, Size: 120581 bytes --]

  reply	other threads:[~2021-10-04 13:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-04 11:33 The definition of orig-fn Hongyi Zhao
2021-10-04 11:59 ` Tassilo Horn
2021-10-04 12:12   ` Hongyi Zhao
2021-10-04 12:23     ` Tassilo Horn
2021-10-04 13:57       ` Hongyi Zhao [this message]
2021-10-04 18:14         ` Tassilo Horn
2021-10-05  2:12           ` Hongyi Zhao
2021-10-05  4:56             ` Tassilo Horn
2021-10-05  6:06               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05  6:25               ` Hongyi Zhao
2021-10-05  6:40                 ` Hongyi Zhao
2021-10-05  7:32                 ` Tassilo Horn
2021-10-05  8:15                   ` Hongyi Zhao
2021-10-05  8:50                     ` Tassilo Horn
2021-10-05  9:41                       ` Hongyi Zhao
2021-10-05  9:43                         ` Tassilo Horn
2021-10-05 10:19                           ` Tassilo Horn
2021-10-05 10:25                         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05 11:55                           ` Hongyi Zhao
2021-10-05 14:21                             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05 10:17                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05 11:52                       ` Hongyi Zhao
2021-10-04 12:01 ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGP6POJBpqOVSd1G7EnKvNweGo7ag6bG2jj6mRm9+Syx1wxBJg@mail.gmail.com \
    --to=hongyi.zhao@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tsdh@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).