| 1 |
;;; cflow.el --- major mode for viewing cflow output files. -*- lexical-binding: t; -*- |
| 2 |
|
| 3 |
;; Authors: 1994, 1995, 2005, 2007, 2021 Sergey Poznyakoff |
| 4 |
;; Version: 0.2.1 |
| 5 |
;; Keywords: cflow |
| 6 |
|
| 7 |
;; This file is part of GNU cflow |
| 8 |
;; Copyright (C) 1994-2025 Sergey Poznyakoff |
| 9 |
;; Copyright (C) 2025 Free Software Foundation, Inc. |
| 10 |
|
| 11 |
;; GNU cflow is free software; you can redistribute it and/or modify |
| 12 |
;; it under the terms of the GNU General Public License as published by |
| 13 |
;; the Free Software Foundation; either version 3 of the License, or |
| 14 |
;; (at your option) any later version. |
| 15 |
|
| 16 |
;; GNU cflow is distributed in the hope that it will be useful, |
| 17 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
;; GNU General Public License for more details. |
| 20 |
|
| 21 |
;; You should have received a copy of the GNU General Public License |
| 22 |
;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
|
| 24 |
;;; Commentary: |
| 25 |
|
| 26 |
;;;; Installation: |
| 27 |
|
| 28 |
;; You may wish to use precompiled version of the module. To create it |
| 29 |
;; run: |
| 30 |
;; emacs -batch -f batch-byte-compile cflow-mode.el |
| 31 |
;; Install the file cflow-mode.elc (and, optionally, cflow-mode.el) to |
| 32 |
;; any directory in your Emacs load-path. |
| 33 |
|
| 34 |
;; Customization: |
| 35 |
;; To your .emacs or site-start.el add: |
| 36 |
;; (autoload 'cflow-mode "cflow-mode") |
| 37 |
;; (add-to-list 'auto-mode-alist '("\\.cflow\\'" . cflow-mode)) |
| 38 |
|
| 39 |
;;; Code: |
| 40 |
|
| 41 |
(require 'font-lock) ;; FIXME: Not needed any more, I think. |
| 42 |
|
| 43 |
(defvar cflow-mode-syntax-table |
| 44 |
(let ((st (make-syntax-table))) |
| 45 |
(modify-syntax-entry ?\# "<" st) |
| 46 |
(modify-syntax-entry ?\n ">" st) |
| 47 |
st)) |
| 48 |
|
| 49 |
|
| 50 |
(defvar cflow-mode-map |
| 51 |
(let ((map (make-sparse-keymap))) |
| 52 |
(define-key map "s" #'cflow-find-function) |
| 53 |
(define-key map "r" #'cflow-recursion-root) |
| 54 |
(define-key map "R" #'cflow-recursion-next) |
| 55 |
(define-key map "x" #'cflow-goto-expand) |
| 56 |
(define-key map "E" #'cflow-edit-out-full) |
| 57 |
(define-key map "c" #'cflow-find-caller) |
| 58 |
(define-key map "n" #'cflow-next-function) |
| 59 |
(define-key map "p" #'cflow-prev-function) |
| 60 |
|
| 61 |
(define-key map [menu-bar] (make-sparse-keymap)) |
| 62 |
|
| 63 |
(define-key map [menu-bar cflow] |
| 64 |
(cons "Cflow" (make-sparse-keymap "Cflow"))) |
| 65 |
|
| 66 |
(define-key map [menu-bar cflow cflow-recursion-next] |
| 67 |
'("Recursion next" . cflow-recursion-next)) |
| 68 |
(define-key map [menu-bar cflow cflow-recursion-root] |
| 69 |
'("Recursion root" . cflow-recursion-root)) |
| 70 |
(define-key map [menu-bar cflow cflow-goto-expand] |
| 71 |
'("Find expansion" . cflow-goto-expand)) |
| 72 |
(define-key map [menu-bar cflow cflow-find-function] |
| 73 |
'("Find function" . cflow-find-function)) |
| 74 |
map) |
| 75 |
"Keymap used in Cflow mode.") |
| 76 |
|
| 77 |
(defun cflow--goto-line (n) |
| 78 |
(goto-char (point-min)) |
| 79 |
(forward-line (1- n))) |
| 80 |
|
| 81 |
;; Find the function under cursor. |
| 82 |
;; Switch to the proper buffer and go to the function header |
| 83 |
(defun cflow-find-function () |
| 84 |
(interactive) |
| 85 |
(let ((lst (cflow-find-default-function))) |
| 86 |
(cond |
| 87 |
(lst |
| 88 |
(switch-to-buffer (find-file-noselect (car lst))) |
| 89 |
(cflow--goto-line (car (cdr lst)))) |
| 90 |
(t |
| 91 |
(error "No source/line information for this function."))))) |
| 92 |
|
| 93 |
;; Parse a cflow listing line |
| 94 |
;; Return (list SOURCE-NAME LINE-NUMBER) |
| 95 |
(defun cflow-find-default-function () |
| 96 |
(save-excursion |
| 97 |
(beginning-of-line) |
| 98 |
(cond |
| 99 |
((re-search-forward "\\([^ \t:]+\\):\\([0-9]+\\)" |
| 100 |
(save-excursion (end-of-line) (point)) |
| 101 |
t) |
| 102 |
(list |
| 103 |
(buffer-substring (match-beginning 1) (match-end 1)) |
| 104 |
(string-to-number |
| 105 |
(buffer-substring (match-beginning 2) (match-end 2))))) |
| 106 |
(t |
| 107 |
nil)))) |
| 108 |
|
| 109 |
;; If the cursor stays on a recursive call, then go to the root of |
| 110 |
;; this call |
| 111 |
(defun cflow-recursion-root () |
| 112 |
(interactive) |
| 113 |
(let ((num (cond |
| 114 |
((save-excursion |
| 115 |
(beginning-of-line) |
| 116 |
(re-search-forward "(recursive: see \\([0-9]+\\))" |
| 117 |
(save-excursion (end-of-line) (point)) |
| 118 |
t)) |
| 119 |
(string-to-number |
| 120 |
(buffer-substring (match-beginning 1) (match-end 1)))) |
| 121 |
(t |
| 122 |
0)))) |
| 123 |
(cond |
| 124 |
((> num 0) |
| 125 |
(push-mark) |
| 126 |
(cflow--goto-line num)) |
| 127 |
(t |
| 128 |
(error "Not a recursive call"))))) |
| 129 |
|
| 130 |
|
| 131 |
(defvar-local cflow-recursion-root-line nil) |
| 132 |
|
| 133 |
(defun cflow-recursion-next () |
| 134 |
"Go to next recursive call" |
| 135 |
(interactive) |
| 136 |
(save-excursion |
| 137 |
(beginning-of-line) |
| 138 |
(cond |
| 139 |
((re-search-forward "(R)" |
| 140 |
(save-excursion (end-of-line) (point)) t) |
| 141 |
(setq cflow-recursion-root-line (count-lines (point-min) (point)))))) |
| 142 |
(cond |
| 143 |
((null cflow-recursion-root-line) |
| 144 |
(error "No recursive functions")) |
| 145 |
(t |
| 146 |
(let ((pos (save-excursion |
| 147 |
(forward-line 1) |
| 148 |
(re-search-forward |
| 149 |
(concat "(recursive: see " |
| 150 |
(number-to-string cflow-recursion-root-line) |
| 151 |
")") |
| 152 |
(point-max) |
| 153 |
t)))) |
| 154 |
(cond |
| 155 |
((null pos) |
| 156 |
(cflow--goto-line cflow-recursion-root-line) |
| 157 |
(error "No more calls.")) |
| 158 |
(t |
| 159 |
(push-mark) |
| 160 |
(goto-char pos) |
| 161 |
(beginning-of-line))))))) |
| 162 |
|
| 163 |
(defun cflow-goto-expand () |
| 164 |
(interactive) |
| 165 |
(let ((num (cond |
| 166 |
((save-excursion |
| 167 |
(beginning-of-line) |
| 168 |
(re-search-forward "\\[see \\([0-9]+\\)\\]" |
| 169 |
(save-excursion (end-of-line) (point)) |
| 170 |
t)) |
| 171 |
(string-to-number |
| 172 |
(buffer-substring (match-beginning 1) (match-end 1)))) |
| 173 |
(t |
| 174 |
0)))) |
| 175 |
(cond |
| 176 |
((> num 0) |
| 177 |
(push-mark) |
| 178 |
(cflow--goto-line num)) |
| 179 |
(t |
| 180 |
(error "Not expandable"))))) |
| 181 |
|
| 182 |
(defun cflow-find-caller () |
| 183 |
"Go to the caller of the current function" |
| 184 |
(interactive) |
| 185 |
(let ((num (save-excursion |
| 186 |
(beginning-of-line) |
| 187 |
(cond |
| 188 |
((looking-at "^\\( +\\)\\S-") |
| 189 |
(let ((indent (- (match-end 1) (match-beginning 1)))) |
| 190 |
(re-search-backward (format "^ \\{,%d\\}\\S-" (- indent 1)) nil t) |
| 191 |
(- (match-end 0) 1))) |
| 192 |
((looking-at "^\\(\\s-+\\)\\S-") |
| 193 |
(let ((indent (- (match-end 1) (match-beginning 1)))) |
| 194 |
(catch 'found |
| 195 |
(while (re-search-backward "^\\(\\s-*\\)\\S-" nil t) |
| 196 |
(if (< (- (match-end 1) (match-beginning 1)) indent) |
| 197 |
(throw 'found (match-end 1)))) |
| 198 |
0))) |
| 199 |
(t |
| 200 |
0))))) |
| 201 |
(cond |
| 202 |
((> num 0) |
| 203 |
(push-mark) |
| 204 |
(goto-char num)) |
| 205 |
(t |
| 206 |
(error "At top-level function"))))) |
| 207 |
|
| 208 |
(defun cflow-goto-sibling (count advance) |
| 209 |
(let ((pos (save-excursion |
| 210 |
(beginning-of-line) |
| 211 |
(cond |
| 212 |
((looking-at "^\\( +\\)\\S-") |
| 213 |
(let ((indent (- (match-end 1) (match-beginning 1)))) |
| 214 |
(let ((pos nil) |
| 215 |
(stop (if (> advance 0) 'eobp 'bobp))) |
| 216 |
(while (not pos) |
| 217 |
(if (funcall stop) |
| 218 |
(error "No more calls")) |
| 219 |
(forward-line advance) |
| 220 |
(cond |
| 221 |
((looking-at "^\\( *\\)\\S-") |
| 222 |
(let ((l (- (match-end 1) (match-beginning 1)))) |
| 223 |
(cond |
| 224 |
((= l indent) |
| 225 |
(setq count (1- count)) |
| 226 |
(if (= count 0) |
| 227 |
(setq pos (match-end 1)))) |
| 228 |
((< l indent) |
| 229 |
(error "No more calls"))))))) |
| 230 |
pos))) |
| 231 |
((looking-at "^\\S-") |
| 232 |
(error "At top-level function")) |
| 233 |
(t |
| 234 |
(error "Not at function")))))) |
| 235 |
(goto-char pos))) |
| 236 |
|
| 237 |
(defun cflow-next-function (&optional arg) |
| 238 |
"Go to the next function at this nesting level" |
| 239 |
(interactive "P") |
| 240 |
(cflow-goto-sibling (or arg 1) 1)) |
| 241 |
|
| 242 |
(defun cflow-prev-function (&optional arg) |
| 243 |
"Go to the previous function at this nesting level" |
| 244 |
(interactive "P") |
| 245 |
(cflow-goto-sibling (or arg 1) -1)) |
| 246 |
|
| 247 |
(defvar cflow--read-only) |
| 248 |
|
| 249 |
(defun cflow-edit-out-full () |
| 250 |
"Get out of Cflow mode, leaving Cflow file buffer in fundamental mode." |
| 251 |
(interactive) |
| 252 |
(if (yes-or-no-p "Should I let you edit the whole Cflow file? ") |
| 253 |
(progn |
| 254 |
(setq buffer-read-only cflow--read-only) |
| 255 |
(fundamental-mode) |
| 256 |
(message "Type 'M-x cflow-mode RET' once done")))) |
| 257 |
|
| 258 |
|
| 259 |
;; Font-lock stuff |
| 260 |
(defconst cflow-font-lock-keywords |
| 261 |
`(("^ *[0-9]+" (0 'font-lock-constant-face)) |
| 262 |
("\\([^ \t\n]+\\)() +\\(<[^>]*>\\)" |
| 263 |
(1 'font-lock-function-name-face) |
| 264 |
(2 'font-lock-type-face)) |
| 265 |
("\\([^ \t\n]+\\) +\\(<[^>]*>\\)" |
| 266 |
(1 'font-lock-variable-name-face) |
| 267 |
(2 'font-lock-type-face)) |
| 268 |
("[^ \t\n]+()$" (0 'font-lock-builtin-face)) |
| 269 |
("\\((R):?\\)\\(?: \\[see [0-9]+]\\)?$" (1 'font-lock-comment-face)) |
| 270 |
("(recursive: see [0-9]+)" (0 'font-lock-comment-face)) |
| 271 |
("\\[see [0-9]+]" (0 'font-lock-comment-face)) |
| 272 |
("^[- \\t+|\\]+" (0 'font-lock-keyword-face)))) |
| 273 |
|
| 274 |
;;;###autoload |
| 275 |
(define-derived-mode cflow-mode nil "Cflow" |
| 276 |
"Major mode for viewing cflow output files." |
| 277 |
|
| 278 |
(set (make-local-variable 'cflow--read-only) buffer-read-only) |
| 279 |
(setq buffer-read-only t) |
| 280 |
|
| 281 |
(set-syntax-table cflow-mode-syntax-table) |
| 282 |
|
| 283 |
(make-local-variable 'font-lock-defaults) |
| 284 |
(setq font-lock-defaults |
| 285 |
'((cflow-font-lock-keywords) nil t |
| 286 |
(("+-*/.<>=!?$%_&~^:" . "w")) |
| 287 |
beginning-of-line))) |
| 288 |
|
| 289 |
;;;###autoload |
| 290 |
(add-to-list 'auto-mode-alist '("\\.cflow\\'" . cflow-mode)) |
| 291 |
|
| 292 |
(provide 'cflow-mode) |
| 293 |
;;; cflow-mode ends |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|