Re: Filter interactive function declared in file

[email protected]
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

> How can I pass a pattern.  Without checking a pattern I get an enormous
> long list of matching function names. 
>
> (defun mazonas-smbptn (smb pattern file)
>   "Filter symbol SYM defined in a specific file that are currently loaded."
>   (when (and (fboundp smb)
>              (string= (symbol-file smb) file)
>              (commandp smb)
>              (string-match-p pattern (symbol-name smb)))
>     smb))
>

- Delete (fboundp smb) because if (commandp smb) returns t,
  then (fboundp smb) is guaranteed to return t.
- Move (commandp smb) to where (fboundp smb) is.
- Swap the (string= ...) and (string-match-p ...) so that
  you do not call (symbol-file ...) if the pattern does not match.

New version:

(defun mazonas-smbptn (smb pattern file)
  "Filter symbol SYM defined in a specific file that are currently loaded."
  (when (and (commandp smb)
             (string-match-p pattern (symbol-name smb))           
             (string= (symbol-file smb) file))
    smb))

>
> (defun mazonas-agfun (file pattern)
>   "List all interactive functions defined in FILE."
>   
>   (interactive
>     (list 
>       (read-file-name "File: ")
>       (read-regexp "Symbol Pattern (RET for all): ")))
>
>   (let (smbs)
>     (when (find-file-noselect file)
>       (mapatoms (lambda (smb)
>                   (when (mazonas-smbptn smb pattern file)
>                     (push smb smbs))))
>
>       (message "Interactive functions: %s"
> 	(seq-filter #'commandp smbs))
>       (seq-filter #'commandp smbs))))
>

- Add a counter for use after you have accumulated your
list:
  ...
  (let ((smbs nil)
        (count 0))
    (when (find-file-noselect file)
      (mapatoms (lambda (smb)
                  (when (mazonas-smbptn smb pattern file)
                    (setq count (1+ count))
                    (push smb smbs))))
      (if (> count 0)
        (message "Interactive functions: %s"
                 (seq-filter #'commandp smbs))
        (message "No interactive functions found"))))

- Try it non-interactively, for example, using 

   (mazonas-agfun "/usr/share/emacs/30.2/lisp/subr.elc" "global-set-*")
   => "Interactive functions: (global-set-key)"

  (Use M-x locate to locate files on your operating system.)

- Be careful to specify "file" correctly so that 
    (string= (symbol-file smb) file)
  yields t.
  
  You should be able to check this by using Edebug and
  setting a breakpoint at the expression:
    (string= (symbol-file smb) file)
  in the function ‘mazonas-smbptn’, and examining the values
  that (symbol-file smb) and ‘file’ when the breakpoint is
  triggered.  If they do not match, then you have not
  specified the value of ‘file’ correctly.

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.