;;; Looks up a source of "{module}.{proc}" ;;; Last edited on 2021-05-30 11:01:17 by jstolfi (defun stolfi-visit-module-and-proc (arg) "Expects to be called when the point is within or adjacent to a function call of the form {module}.{proc}. Searches for the file {module}.{ext} in the current directory or some near ancestor thereof, where {ext} is the extension of the current buffer's file. If found, visits that file, then searches in it for the presumed definition of {proc}. With a prefix arg, uses the current window. [stolfi]" (interactive "P") (let* ( ( fname (buffer-file-name) ) ( fext (file-name-extension fname) ) ( modproc (stolfi-grab-module-and-proc nil) ) ) (message (concat "modproc = " modproc)) (if modproc (let* ( ( mname (file-name-sans-extension modproc) ) ( mproc (file-name-extension modproc) ) ( mfile (concat mname "." fext) ) ( mfile-ex mfile ) ( defkey (if (equal fext "py") "\\bdef\\b *" "") ) ) (message (concat "mfile = " mfile)) ; Try to find {mfile} in the current directory or near ancestor: (if (not (file-exists-p mfile-ex)) (setq mfile-ex (concat "../" mfile-ex))) (if (not (file-exists-p mfile-ex)) (setq mfile-ex (concat "../" mfile-ex))) (if (not (file-exists-p mfile-ex)) (setq mfile-ex (concat "../" mfile-ex))) (if (not (file-exists-p mfile-ex)) (error (concat "file " mfile " not found")) (progn (if (null arg) (find-file-other-window mfile-ex) (find-file mfile-ex) ) (if (and mproc (> (length mproc) 1)) (progn (goto-char (point-min)) (re-search-forward (concat defkey "\\b" mproc "\\b")) ) ) ) ) ) (error "no valid {module}.{proc} around") ) ) ) (defun stolfi-grab-module-and-proc(arg) "Expects to be called when the point is within or adjacent to qualified function name of the form \"{proc}\" or \"{module}.{proc}\". Returns that name as a string." (interactive "p") ; (message "stolfi-grab-module-and-proc called with arg = %S" arg) (save-excursion (let* (beg end) ; Search backwars for beginning of qualified name: (if (re-search-backward "[^._A-Za-z0-9]" (- (point) 1000) 'beg) (forward-char) ) (setq beg (point)) (if (re-search-forward "[^._A-Za-z0-9]" (+ (point) 1000) 'end) (backward-char) ) (setq end (point)) (if (< beg end) (buffer-substring-no-properties beg end) nil ) ) ) )