Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org-mode can't edit C source code

Tags:

emacs

org-mode

When I use emacs's org-mode to edit a C program, that is when I edit the below segment:

#+begin_src c
#define MAX 100
#+end_src

and after I call the function 'org-edit-src-code' to edit the C code in a new buffer, there is an error:

Language mode `c-mode' fails with: stringp

and I can neither save to nor return to the original buffer after I edit the code in the new buffer.

Every other languages can work without any problem, including C++, elisp, sh.

How can I fix this problem? My org-mode version is 7.6 and emacs version is 23.2.

the full debug info is (follow Noufal Ibrahim's method):

Debugger entered--Lisp error: (error "Language mode `c-mode' fails with: stringp")
  signal(error ("Language mode `c-mode' fails with: stringp"))
  error("Language mode `%s' fails with: %S" c-mode stringp)
  (condition-case e (funcall lang-f) (error (error "Language mode `%s' fails with: %S" lang-f ...)))
  (let ((org-inhibit-startup t)) (condition-case e (funcall lang-f) (error ...)))
  (if (and (setq buffer ...) (if org-src-ask-before-returning-to-edit-buffer ... t)) (org-src-switch-to-buffer buffer (quote return)) (when buffer (with-current-buffer buffer ...) (kill-buffer buffer)) (setq buffer (generate-new-buffer ...)) (setq ovl (make-overlay beg end)) (overlay-put ovl (quote edit-buffer) buffer) (overlay-put ovl (quote help-echo) "Click with mouse-1 to switch to buffer editing this segment") (overlay-put ovl (quote face) (quote secondary-selection)) (overlay-put ovl (quote keymap) (let ... ... map)) (overlay-put ovl :read-only "Leave me alone") (setq transmitted-variables (append transmitted-variables ...)) (org-src-switch-to-buffer buffer (quote edit)) (if (eq single ...) (setq code ...)) (insert code) (remove-text-properties (point-min) (point-max) (quote ...)) (unless (cadr ...) (setq total-nindent ...)) (let (...) (condition-case e ... ...)) (dolist (pair transmitted-variables) (org-set-local ... ...)) (when org-mode-p (goto-char ...) (while ... ... ...)) (when markline (org-goto-line ...) (org-move-to-column ...) (push-mark ... ... t) (setq deactivate-mark nil)) (org-goto-line (1+ ...)) (org-move-to-column (if org-src-preserve-indentation col ...)) (org-src-mode) (set-buffer-modified-p nil) (and org-edit-src-persistent-message (org-set-local ... msg)) (let (...) (when ... ...)))
  (if (not info) nil (setq beg (move-marker beg ...) end (move-marker end ...) msg (if allow-write-back-p ... "Exit with C-c ' (C-c and single quote)") code (or code ...) lang (or ... ...) lang (if ... ... lang) single (nth 3 info) block-nindent (nth 5 info) lang-f (intern ...) begline (save-excursion ... ...) transmitted-variables (\` ...)) (if (and mark ... ...) (save-excursion ... ...)) (if (equal lang-f ...) (setq lang-f ...)) (unless (functionp lang-f) (error "No such language mode: %s" lang-f)) (save-excursion (if ... ...) (setq line ... col ...)) (if (and ... ...) (org-src-switch-to-buffer buffer ...) (when buffer ... ...) (setq buffer ...) (setq ovl ...) (overlay-put ovl ... buffer) (overlay-put ovl ... "Click with mouse-1 to switch to buffer editing this segment") (overlay-put ovl ... ...) (overlay-put ovl ... ...) (overlay-put ovl :read-only "Leave me alone") (setq transmitted-variables ...) (org-src-switch-to-buffer buffer ...) (if ... ...) (insert code) (remove-text-properties ... ... ...) (unless ... ...) (let ... ...) (dolist ... ...) (when org-mode-p ... ...) (when markline ... ... ... ...) (org-goto-line ...) (org-move-to-column ...) (org-src-mode) (set-buffer-modified-p nil) (and org-edit-src-persistent-message ...) (let ... ...)) t)
  (let ((mark ...) (case-fold-search t) (info ...) (full-info ...) (org-mode-p ...) (beg ...) (end ...) (allow-write-back-p ...) block-nindent total-nindent ovl lang lang-f single lfmt buffer msg begline markline markcol line col transmitted-variables) (if (not info) nil (setq beg ... end ... msg ... code ... lang ... lang ... single ... block-nindent ... lang-f ... begline ... transmitted-variables ...) (if ... ...) (if ... ...) (unless ... ...) (save-excursion ... ...) (if ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...) t))
  org-edit-src-code()
  (cond ((save-excursion ... ...) (find-file ...)) ((org-edit-src-code)) ((org-edit-fixed-width-region)) ((org-at-table\.el-p) (org-edit-src-code)) ((or ... ...) (call-interactively ...)) (t (call-interactively ...)))
  org-edit-special()
  call-interactively(org-edit-special nil nil)

I am a novice and I don't know what the problem is. Any advice?

like image 706
astropeak Avatar asked Nov 04 '22 10:11

astropeak


1 Answers

As discovered in the comments this is caused by accessing buffer-file-name in a temporary buffer which is not backed by a file and hence buffer-file-name is nil giving rise the it's failing stringp. Anyway, one way to fix it is by replacing instances of buffer-file-name with

(or buffer-file-name "DEFAULT-NAME")

if you need to use it, or by a block like the following

(when buffer-file-name
  (code-going-here-will-only-be-executed-if-buffer-file-name-in-non-nil))

Which is easier/better will of course depend upon what you are doing.

like image 65
Ivan Andrus Avatar answered Nov 09 '22 14:11

Ivan Andrus