I write my projects in FreePascal and use opascal-mode to write it. I'm having a problem that this mode thinks that '\' is not a complete string, and hence all code after it is highlighted as a string. How can I change the opascal-mode (or add a hook) in which it will not try to parse the backslash as an escape character? In FreePascal, strings do not support backslash as an escape char.

I'm using GNU Emacs 24.5.1 and this is a sample code that when run in opascal-mode, it highlights everything after the '\' improperly:

// syntax_highlight.dpr                                                                                                                                                                                                

{$mode objfpc}{$H+}
program my_program;
uses Classes;

var newVal : char;

  newVal := '\'; // syntax highlight breaks here!                                                                                                                                                                                                       
  if newVal = '''' then begin                                                                                                                                                                                          
    writeln('single quote');                                                                                                                                                                                                    
  end else if newVal = '\' then begin

I've tried messing around with modify-syntax-entry, but nothing I tried worked.

EDIT: this is the fixed config in ~/.emacs after I've implemented @JeanPierre's fix

;; associate pascal files as opascal-mode                                                                                                                                                              
(add-to-list 'auto-mode-alist '("\\.pas$" . opascal-mode))
(let* ((pascal-files '(".pas" ".pp" ".inc" ".lpr" ".dpr"))
       (pascal-regexp (concat (regexp-opt pascal-files t) "\\'")))
  (add-to-list 'auto-mode-alist (cons pascal-regexp 'opascal-mode)))

;; don't escape backslashes in pascal    
(add-hook 'opascal-mode-hook
          (modify-syntax-entry ?\\ "."))
  • This would be a font-lock issue rather than syntax-table one. I don't know how the highlighting implemented in this mode, but I'd look for the mode's source: the font-lock rules would be the place to start.
    – wvxvw
    Feb 25, 2016 at 6:46

1 Answer 1


C-h s (describe-syntax) shows that \ indeed has escape syntax. You can change it with the following:

(defun my/make-backslash-punctuation ()
  (modify-syntax-entry ?\\ "."))

(add-hook 'opascal-mode-hook #'my/make-backslash-punctuation)
  • that's perfect! it worked like a charm. Thanks!
    – Unglued
    Feb 29, 2016 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.